1 package org.simantics.modeling.utils;
4 import java.io.IOException;
5 import java.util.HashMap;
6 import java.util.HashSet;
9 import java.util.TreeMap;
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;
27 public class DumpOntologyStructure {
29 private Resource ontology;
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<>();
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));
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)) {
56 names.put(r, NameUtils.getSafeName(graph, r));
57 parents.put(r, container);
58 readNameAndParent(graph, container, r);
59 readLibraries(graph, r);
61 containers.addAll(folders);
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)) {
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()) {
79 dump.append(pi.defaultValue);
85 dump.append(pi.label);
86 if(pi.description != null) {
88 dump.append(pi.description);
92 for(NamedResource nr : data.getConnectionPoints()) {
94 dump.append(nr.getName());
97 componentTypeDumps.put(r, dump.toString());
100 containers.addAll(folders);
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)) {
109 readNameAndParent(graph, container, r);
112 containers.addAll(folders);
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))
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));
134 dump.append(possibleValue);
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)) {
142 dump.append(desc.getRelativeRVI(graph, v));
147 componentDumps.put(r, dump.toString());
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);
158 moduleCodes.put(r, code);
161 readNameAndParent(graph, container, r);
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);
172 pgraphCodes.put(r, code);
175 readNameAndParent(graph, container, r);
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);
187 graphFileBytes.put(r, bytes);
190 readNameAndParent(graph, container, r);
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);
204 readGraphFiles(graph);
207 private File escapeFile(File file) {
212 return new File(escapeFile(file.getParentFile()), FileUtils.escapeFileName(file.getName()));
216 public void write(File unsafeFolder) throws IOException {
217 File folder = escapeFile(unsafeFolder);
219 FileUtils.deleteAll(folder);
221 writeLibraries(folder);
222 writeSCLModules(folder);
223 writePGraphs(folder);
224 writeGraphFiles(folder);
225 writeComponentTypes(folder);
226 writeComponents(folder);
229 private File getFolder(File root, Resource library) {
230 if(ontology.equals(library))
232 Resource parent = parents.get(library);
233 File parentFolder = getFolder(root, parent);
234 return new File(parentFolder, FileUtils.escapeFileName(names.get(library)));
237 private File getParentFolder(File root, Resource r) {
238 Resource parent = parents.get(r);
239 return getFolder(root, parent);
242 private File getFile(File root, Resource r) {
243 return new File(getParentFolder(root, r), FileUtils.escapeFileName(names.get(r)));
246 private void writeLibraries(File rootFolder) {
247 for(Resource library : folders) {
248 File folder = getFolder(rootFolder, library);
250 libraryFolders.put(library, folder);
254 private void writeSCLModules(File rootFolder) throws IOException {
255 for(Resource r : modules) {
256 FileUtils.writeFile(getFile(rootFolder, r), moduleCodes.get(r).getBytes());
260 private void writePGraphs(File rootFolder) throws IOException {
261 for(Resource r : pgraphs) {
262 FileUtils.writeFile(getFile(rootFolder, r), pgraphCodes.get(r).getBytes());
266 private void writeGraphFiles(File rootFolder) throws IOException {
267 for(Resource r : graphFiles) {
268 FileUtils.writeFile(getFile(rootFolder, r), graphFileBytes.get(r));
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());
280 private void writeComponents(File rootFolder) throws IOException {
281 for(Resource r : components) {
282 FileUtils.writeFile(getFile(rootFolder, r), componentDumps.get(r).getBytes());