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;
10 import java.util.TreeSet;
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;
28 public class DumpOntologyStructure {
30 private Resource ontology;
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<>();
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));
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)) {
57 names.put(r, NameUtils.getSafeName(graph, r));
58 parents.put(r, container);
59 readNameAndParent(graph, container, r);
60 readLibraries(graph, r);
62 containers.addAll(folders);
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)) {
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()) {
80 dump.append(pi.defaultValue);
86 dump.append(pi.label);
87 if(pi.description != null) {
89 dump.append(pi.description);
93 for(NamedResource nr : data.getConnectionPoints()) {
95 dump.append(nr.getName());
98 componentTypeDumps.put(r, dump.toString());
101 containers.addAll(folders);
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)) {
110 readNameAndParent(graph, container, r);
113 containers.addAll(folders);
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))
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));
135 dump.append(possibleValue);
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));
145 for(String rvi : rvis) {
152 componentDumps.put(r, dump.toString());
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);
163 moduleCodes.put(r, code);
166 readNameAndParent(graph, container, r);
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);
177 pgraphCodes.put(r, code);
180 readNameAndParent(graph, container, r);
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);
192 graphFileBytes.put(r, bytes);
195 readNameAndParent(graph, container, r);
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);
209 readGraphFiles(graph);
212 private File escapeFile(File file) {
217 return new File(escapeFile(file.getParentFile()), FileUtils.escapeFileName(file.getName()));
221 public void write(File unsafeFolder) throws IOException {
222 File folder = escapeFile(unsafeFolder);
224 FileUtils.deleteAll(folder);
226 writeLibraries(folder);
227 writeSCLModules(folder);
228 writePGraphs(folder);
229 writeGraphFiles(folder);
230 writeComponentTypes(folder);
231 writeComponents(folder);
234 private File getFolder(File root, Resource library) {
235 if(ontology.equals(library))
237 Resource parent = parents.get(library);
238 File parentFolder = getFolder(root, parent);
239 return new File(parentFolder, FileUtils.escapeFileName(names.get(library)));
242 private File getParentFolder(File root, Resource r) {
243 Resource parent = parents.get(r);
244 return getFolder(root, parent);
247 private File getFile(File root, Resource r) {
248 return new File(getParentFolder(root, r), FileUtils.escapeFileName(names.get(r)));
251 private void writeLibraries(File rootFolder) {
252 for(Resource library : folders) {
253 File folder = getFolder(rootFolder, library);
255 libraryFolders.put(library, folder);
259 private void writeSCLModules(File rootFolder) throws IOException {
260 for(Resource r : modules) {
261 FileUtils.writeFile(getFile(rootFolder, r), moduleCodes.get(r).getBytes());
265 private void writePGraphs(File rootFolder) throws IOException {
266 for(Resource r : pgraphs) {
267 FileUtils.writeFile(getFile(rootFolder, r), pgraphCodes.get(r).getBytes());
271 private void writeGraphFiles(File rootFolder) throws IOException {
272 for(Resource r : graphFiles) {
273 FileUtils.writeFile(getFile(rootFolder, r), graphFileBytes.get(r));
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());
285 private void writeComponents(File rootFolder) throws IOException {
286 for(Resource r : components) {
287 FileUtils.writeFile(getFile(rootFolder, r), componentDumps.get(r).getBytes());