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