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