]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph.db/src/org/simantics/graph/db/CoreInitialization.java
Minor fix and optimization for exporting TG and Pgraph with sharedlib
[simantics/platform.git] / bundles / org.simantics.graph.db / src / org / simantics / graph / db / CoreInitialization.java
1 package org.simantics.graph.db;
2
3 import java.util.HashSet;
4
5 import org.simantics.db.Session;
6 import org.simantics.db.WriteOnlyGraph;
7 import org.simantics.db.common.request.WriteOnlyRequest;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.service.InitSupport;
10 import org.simantics.db.service.SerialisationSupport;
11 import org.simantics.graph.representation.TransferableGraph1;
12 import org.simantics.scl.reflection.OntologyVersions;
13
14 public class CoreInitialization {
15
16         static final public String LAYER0 = OntologyVersions.getInstance().currentVersion("http://www.simantics.org/Layer0-0.0/");
17         static final HashSet<InitSupport.Builtin> BUILTINS =
18                 new HashSet<InitSupport.Builtin>();
19         static public HashSet<InitSupport.Builtin> getBuiltins() {
20             return BUILTINS;
21         }
22         private static int builtinId = 0;
23         private static void addBuiltin(String uri) {
24                 BUILTINS.add(new InitSupport.Builtin(uri, ++builtinId));
25         }
26         private static void addLayer0Builtin(String name) {
27                 addBuiltin(LAYER0 + name);
28         }
29
30         static {
31                 // IMPORTANT NOTICE:
32                 // DO NOT alter the order of these definitions in any way
33                 // unless you deliberately want to make the system incompatible
34                 // with databases that have been created before changing these.
35                 addLayer0Builtin("InstanceOf");
36                 addLayer0Builtin("Inherits");
37                 addLayer0Builtin("SubrelationOf");
38                 addLayer0Builtin("InverseOf");
39
40                 addLayer0Builtin("HasName");
41                 addLayer0Builtin("NameOf");
42
43                 addLayer0Builtin("ConsistsOf");
44                 addLayer0Builtin("PartOf");
45
46                 addLayer0Builtin("String");
47                 addLayer0Builtin("Library");
48                 addLayer0Builtin("FunctionalRelation");
49                 addLayer0Builtin("SuperrelationOf");
50                 addLayer0Builtin("Asserts");
51                 addLayer0Builtin("HasInstance");
52                 addLayer0Builtin("HasPredicate");
53                 addLayer0Builtin("HasPredicateInverse");
54                 addLayer0Builtin("HasObject");
55
56                 // #7016: This bogus URI replaces the builtin entry
57                 // that was originally http://Projects.
58                 //
59                 // addBuiltin("http://Projects") was removed because its existence
60                 // resulted in a non-functioning URI space because:
61                 //
62                 // 1. http://Projects was added as a "built-in resource" when the database
63                 //    is first initialized which means that ReadGraph.getResource("http://Projects")
64                 //    would succeed even when the resource was not yet properly imported
65                 //    from the project ontology.
66                 //
67                 // 2. When the project ontology is imported, a new resource is
68                 //    created to represent http://Projects instead of using the existing
69                 //    uninitialized "built-in resource". The L0.ExternalEntity changes made
70                 //    to the standard transferable graph import in this patch caused the
71                 //    code to find the built-in resource for "http://Projects" and think
72                 //    that it has to be a resource that's already properly initialized.
73                 //    This led to the TG import not initializing the resource at all.
74                 //    Previously there was a bug here as well but it was hidden by the fact
75                 //    that the system would import the actual/new "http://Projects"
76                 //    resource from the ontology anyway. This effectively left the
77                 //    uninitialized built-in resource just hanging in the database as
78                 //    trash.
79                 //addBuiltin("http://Projects");
80                 addBuiltin("75e23fb39121b4b37cf2e6a26ccb34c52f77522d-do-not-remove");
81                 addBuiltin("http:/");
82                 addLayer0Builtin("ExternalEntity");
83         }
84
85     public static void initializeBuiltins(Session session) throws DatabaseException {
86         session.getService(InitSupport.class).addBuiltins(BUILTINS);
87     }
88
89         public static long[] initializeGraph(final Session session, TransferableGraph1 tg) throws DatabaseException {
90                 final TransferableGraphImportProcess process = new TransferableGraphImportProcess(tg,
91                                 new ImportAdvisor());
92                 session.syncRequest(new WriteOnlyRequest() {
93                         @Override
94                         public void perform(WriteOnlyGraph graph) throws DatabaseException {
95                             graph.markUndoPoint();
96
97 //                              // Hackety hack
98 //                              session.getService(XSupport.class).setImmutable(session.getRootLibrary(), true);
99
100                                 process.initialPrepare(graph);
101                                 process.write(graph);
102                                 graph.clearUndoList(this);
103
104                         }
105                 });
106                 return process.getResourceIds(
107                                 session.getService(SerialisationSupport.class)
108                         );
109         }
110
111 }