]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics/src/org/simantics/OntologyImportAdvisor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics / src / org / simantics / OntologyImportAdvisor.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics;\r
13 \r
14 import org.simantics.db.ReadGraph;\r
15 import org.simantics.db.Resource;\r
16 import org.simantics.db.WriteOnlyGraph;\r
17 import org.simantics.db.common.WriteBindings;\r
18 import org.simantics.db.exception.DatabaseException;\r
19 import org.simantics.db.exception.RuntimeDatabaseException;\r
20 import org.simantics.db.service.SerialisationSupport;\r
21 import org.simantics.graph.db.AbstractImportAdvisor2;\r
22 import org.simantics.graph.db.TransferableGraphImporter;\r
23 import org.simantics.graph.representation.Root;\r
24 import org.simantics.layer0.Layer0;\r
25 import org.simantics.project.management.DatabaseManagement;\r
26 import org.simantics.project.management.GraphBundle;\r
27 \r
28 public class OntologyImportAdvisor extends AbstractImportAdvisor2 {\r
29 \r
30         final private GraphBundle tg;\r
31         final private DatabaseManagement mgmt;\r
32         final private String rootName;\r
33         private Resource ontology;\r
34         private Resource ontologyName;\r
35         \r
36         public OntologyImportAdvisor(GraphBundle tg, DatabaseManagement mgmt) {\r
37             this.tg = tg;\r
38             this.mgmt = mgmt;\r
39             if(tg.getName().contains("@")) {\r
40                     rootName = tg.getName().substring(tg.getName().lastIndexOf("/")+1);\r
41             } else {\r
42                     rootName = tg.getName().substring(tg.getName().lastIndexOf("/")+1) + "-" + tg.getMajor() + "." + tg.getMinor();\r
43             }\r
44         }\r
45         \r
46         @Override\r
47         public Resource getTarget() {\r
48                 return null;\r
49         }\r
50         \r
51         @Override\r
52         public Resource analyzeRoot(ReadGraph graph, Root root) throws DatabaseException {\r
53                 return null;\r
54         }\r
55         \r
56         @Override\r
57         public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {\r
58                 return createRoot(graph, root, null); \r
59         }\r
60 \r
61         @Override\r
62         public Resource createRoot(WriteOnlyGraph graph, Root root, Resource resource) throws DatabaseException {\r
63                 if(resource == null) resource = graph.newResource();\r
64                 addRootInfo(root, root.name, resource);\r
65                 return resource;\r
66         }\r
67 \r
68     @Override\r
69     public void beforeWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {\r
70         ontology = graph.newResource(graph.getRootLibrary());\r
71         ontologyName = graph.newResource(graph.getRootLibrary());\r
72         graph.newClusterSet(ontology);\r
73         graph.setClusterSet4NewResource(ontology);\r
74     }\r
75 \r
76     @Override\r
77     public void afterWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {\r
78         \r
79         try {\r
80             long[] ids = process.getResourceIds(graph.getService(SerialisationSupport.class));\r
81             tg.setResourceArray(ids);\r
82             mgmt.createGraphBundle(graph, tg);\r
83             //graph.flushCluster();\r
84         } catch (DatabaseException e) {\r
85             throw new RuntimeDatabaseException(e);\r
86         }\r
87         \r
88     }\r
89 \r
90     @Override\r
91     public boolean allowImmutableModifications() {\r
92         return true;\r
93     }\r
94 \r
95     @Override\r
96     public Resource createChild(WriteOnlyGraph graph, TransferableGraphImporter process, Resource parent,\r
97             String name) throws DatabaseException {\r
98         if(rootName.equals(name)) {\r
99                 \r
100                 Layer0 L0 = graph.getService(Layer0.class);\r
101                 graph.claim(ontologyName, L0.InstanceOf, null, L0.String);\r
102                 graph.claimValue(ontologyName, name, WriteBindings.STRING);\r
103                 graph.claim(ontology, L0.HasName, L0.NameOf, ontologyName);\r
104                 return ontology;\r
105 \r
106         } else {\r
107                 \r
108             return process.createChild(graph, parent, null, name);\r
109             \r
110         }\r
111     }\r
112 \r
113     @Override\r
114     public Resource createChild(WriteOnlyGraph graph, TransferableGraphImporter process, Resource parent, Resource child,\r
115             String name) throws DatabaseException {\r
116 \r
117         if(rootName.equals(name)) {\r
118                 \r
119                 Layer0 L0 = graph.getService(Layer0.class);\r
120                 graph.claim(ontologyName, L0.InstanceOf, null, L0.String);\r
121                 graph.claimValue(ontologyName, name, WriteBindings.STRING);\r
122                 graph.claim(ontology, L0.HasName, L0.NameOf, ontologyName);\r
123                 return ontology;\r
124                 \r
125         } else {\r
126                 \r
127             return process.createChild(graph, parent, null, name);\r
128             \r
129         }\r
130     }\r
131     \r
132 }\r