]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/utils/DumpOntologyStructure.java
Support for creating shared ontology dump to git
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / utils / DumpOntologyStructure.java
1 package org.simantics.modeling.utils;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.HashMap;
6 import java.util.HashSet;
7 import java.util.Map;
8 import java.util.Set;
9 import java.util.TreeMap;
10
11 import org.simantics.databoard.Bindings;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.common.NamedResource;
15 import org.simantics.db.common.utils.CommonDBUtils;
16 import org.simantics.db.common.utils.NameUtils;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.layer0.variable.Variable;
19 import org.simantics.db.layer0.variable.Variables;
20 import org.simantics.graphfile.ontology.GraphFileResource;
21 import org.simantics.layer0.Layer0;
22 import org.simantics.structural.stubs.StructuralResource2;
23 import org.simantics.structural2.variables.Connection;
24 import org.simantics.structural2.variables.VariableConnectionPointDescriptor;
25 import org.simantics.utils.FileUtils;
26
27 public class DumpOntologyStructure {
28
29     private Resource ontology;
30     
31     private Set<Resource> containers = new HashSet<>(); 
32     private Set<Resource> folders = new HashSet<>();
33     private Map<Resource, String> names = new HashMap<>();
34     private Map<Resource,Resource> parents = new HashMap<>();
35     private Map<Resource, File> libraryFolders = new HashMap<>();
36     private Set<Resource> modules = new HashSet<>();
37     private Map<Resource, String> moduleCodes = new HashMap<>();
38     private Set<Resource> pgraphs = new HashSet<>();
39     private Map<Resource, String> pgraphCodes = new HashMap<>();
40     private Set<Resource> graphFiles = new HashSet<>();
41     private Map<Resource, byte[]> graphFileBytes = new HashMap<>();
42     private Set<Resource> componentTypes = new HashSet<>();
43     private Map<Resource, String> componentTypeDumps = new HashMap<>();
44     private Set<Resource> components = new HashSet<>();
45     private Map<Resource, String> componentDumps = new HashMap<>();
46
47     private void readNameAndParent(ReadGraph graph, Resource container, Resource r) throws DatabaseException {
48         parents.put(r, container);
49         names.put(r, NameUtils.getSafeName(graph, r));
50     }
51     
52     private void readLibraries(ReadGraph graph, Resource container) throws DatabaseException {
53         Layer0 L0 = Layer0.getInstance(graph);
54         for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, L0.Library)) {
55             folders.add(r);
56             names.put(r, NameUtils.getSafeName(graph, r));
57             parents.put(r, container);
58             readNameAndParent(graph, container, r);
59             readLibraries(graph, r);
60         }
61         containers.addAll(folders);
62     }
63     
64     private void readComponentTypes(ReadGraph graph) throws DatabaseException {
65         Layer0 L0 = Layer0.getInstance(graph);
66         StructuralResource2 STR = StructuralResource2.getInstance(graph);
67         for(Resource container : containers) {
68             for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, STR.ComponentType)) {
69                 folders.add(r);
70                 componentTypes.add(r);
71                 readNameAndParent(graph, container, r);
72                 ComponentTypePropertiesResult data = graph.syncRequest(new HeadlessComponentTypePropertiesResultRequest(r));
73                 StringBuilder dump = new StringBuilder();
74                 for(ComponentTypeViewerPropertyInfo pi : data.getProperties()) {
75                     dump.append(pi.name);
76                     dump.append(" ");
77                     dump.append(pi.type);
78                     dump.append(" ");
79                     dump.append(pi.defaultValue);
80                     if(pi.unit != null) {
81                         dump.append(" ");
82                         dump.append(pi.unit);
83                     }
84                     dump.append(" ");
85                     dump.append(pi.label);
86                     if(pi.description != null) {
87                         dump.append(" ");
88                         dump.append(pi.description);
89                     }
90                     dump.append("\n");
91                 }
92                 for(NamedResource nr : data.getConnectionPoints()) {
93                     dump.append("cp ");
94                     dump.append(nr.getName());
95                     dump.append("\n");
96                 }
97                 componentTypeDumps.put(r, dump.toString());
98             }
99         }
100         containers.addAll(folders);
101     }
102
103     private void readComposites(ReadGraph graph) throws DatabaseException {
104         Layer0 L0 = Layer0.getInstance(graph);
105         StructuralResource2 STR = StructuralResource2.getInstance(graph);
106         for(Resource container : containers) {
107             for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, STR.Composite)) {
108                 folders.add(r);
109                 readNameAndParent(graph, container, r);
110             }
111         }
112         containers.addAll(folders);
113     }
114
115     private void readComponents(ReadGraph graph) throws DatabaseException {
116         Layer0 L0 = Layer0.getInstance(graph);
117         StructuralResource2 STR = StructuralResource2.getInstance(graph);
118         for(Resource container : containers) {
119             for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, STR.Component)) {
120                 if(folders.contains(r))
121                     continue;
122                 components.add(r);
123                 readNameAndParent(graph, container, r);
124                 Variable v = Variables.getVariable(graph, r);
125                 TreeMap<String,Variable> properties = new TreeMap<>();
126                 for(Variable property : v.getProperties(graph))
127                     properties.put(property.getName(graph), property);
128                 StringBuilder dump = new StringBuilder();
129                 for(Variable property : properties.values()) {
130                     String possibleValue = property.getPossiblePropertyValue(graph, "HasDisplayValue", Bindings.STRING);
131                     if(possibleValue != null) {
132                         dump.append(property.getName(graph));
133                         dump.append(" ");
134                         dump.append(possibleValue);
135                         dump.append("\n");
136                     }
137                     if(property.getClassifications(graph).contains(StructuralResource2.URIs.ConnectionRelation)) {
138                         dump.append(property.getName(graph));
139                         Connection c = property.getValue(graph);
140                         for(VariableConnectionPointDescriptor desc : c.getConnectionPointDescriptors(graph, null)) {
141                             dump.append(" ");
142                             dump.append(desc.getRelativeRVI(graph, v));
143                         }
144                         dump.append("\n");
145                     }
146                 }
147                 componentDumps.put(r, dump.toString());
148             }
149         }
150     }
151
152     private void readSCLModules(ReadGraph graph) throws DatabaseException {
153         Layer0 L0 = Layer0.getInstance(graph);
154         for(Resource container : containers) {
155             for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, L0.SCLModule)) {
156                 String code = graph.getPossibleRelatedValue(r, L0.SCLModule_definition, Bindings.STRING);
157                 if(code != null) {
158                     moduleCodes.put(r, code);
159                 }
160                 modules.add(r);
161                 readNameAndParent(graph, container, r);
162             }
163         }
164     }
165     
166     private void readPGraphs(ReadGraph graph) throws DatabaseException {
167         Layer0 L0 = Layer0.getInstance(graph);
168         for(Resource container : containers) {
169             for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, L0.PGraph)) {
170                 String code = graph.getPossibleRelatedValue(r, L0.PGraph_definition, Bindings.STRING);
171                 if(code != null) {
172                     pgraphCodes.put(r, code);
173                 }
174                 pgraphs.add(r);
175                 readNameAndParent(graph, container, r);
176             }
177         }
178     }
179
180     private void readGraphFiles(ReadGraph graph) throws DatabaseException {
181         Layer0 L0 = Layer0.getInstance(graph);
182         GraphFileResource GF = GraphFileResource.getInstance(graph);
183         for(Resource container : containers) {
184             for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, GF.File)) {
185                 byte[] bytes = graph.getPossibleRelatedValue(r, GF.HasFiledata, Bindings.BYTE_ARRAY);
186                 if(bytes != null) {
187                     graphFileBytes.put(r, bytes);
188                 }
189                 graphFiles.add(r);
190                 readNameAndParent(graph, container, r);
191             }
192         }
193     }
194
195     public void read(ReadGraph graph, Resource ontology) throws DatabaseException {
196         this.ontology = ontology;
197         containers.add(ontology);
198         readLibraries(graph, ontology);
199         readComponentTypes(graph);
200         readComposites(graph);
201         readComponents(graph);
202         readSCLModules(graph);
203         readPGraphs(graph);
204         readGraphFiles(graph);
205     }
206     
207     private File escapeFile(File file) {
208
209         if(file.exists())
210             return file;
211         
212         return new File(escapeFile(file.getParentFile()), FileUtils.escapeFileName(file.getName()));
213         
214     }
215     
216     public void write(File unsafeFolder) throws IOException {
217         File folder = escapeFile(unsafeFolder);
218         if(folder.exists())
219             FileUtils.deleteAll(folder);
220         folder.mkdirs();
221         writeLibraries(folder);
222         writeSCLModules(folder);
223         writePGraphs(folder);
224         writeGraphFiles(folder);
225         writeComponentTypes(folder);
226         writeComponents(folder);
227     }
228     
229     private File getFolder(File root, Resource library) {
230         if(ontology.equals(library))
231             return root;
232         Resource parent = parents.get(library);
233         File parentFolder = getFolder(root, parent);
234         return new File(parentFolder, FileUtils.escapeFileName(names.get(library))); 
235     }
236     
237     private File getParentFolder(File root, Resource r) {
238         Resource parent = parents.get(r);
239         return getFolder(root, parent);
240     }
241
242     private File getFile(File root, Resource r) {
243         return new File(getParentFolder(root, r), FileUtils.escapeFileName(names.get(r)));
244     }
245
246     private void writeLibraries(File rootFolder) {
247         for(Resource library : folders) {
248             File folder = getFolder(rootFolder, library);
249             folder.mkdirs();
250             libraryFolders.put(library, folder);
251         }
252     }
253     
254     private void writeSCLModules(File rootFolder) throws IOException {
255         for(Resource r : modules) {
256             FileUtils.writeFile(getFile(rootFolder, r), moduleCodes.get(r).getBytes());
257         }
258     }
259     
260     private void writePGraphs(File rootFolder) throws IOException {
261         for(Resource r : pgraphs) {
262             FileUtils.writeFile(getFile(rootFolder, r), pgraphCodes.get(r).getBytes());
263         }
264     }
265
266     private void writeGraphFiles(File rootFolder) throws IOException {
267         for(Resource r : graphFiles) {
268             FileUtils.writeFile(getFile(rootFolder, r), graphFileBytes.get(r));
269         }
270     }
271
272     private void writeComponentTypes(File rootFolder) throws IOException {
273         for(Resource r : componentTypes) {
274             File folder = getFolder(rootFolder, r);
275             File file = new File(folder, "__interface__");
276             FileUtils.writeFile(file, componentTypeDumps.get(r).getBytes());
277         }
278     }
279
280     private void writeComponents(File rootFolder) throws IOException {
281         for(Resource r : components) {
282             FileUtils.writeFile(getFile(rootFolder, r), componentDumps.get(r).getBytes());
283         }
284     }
285     
286 }