]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/ContentDumps.java
Include component type SCL scripts in textual ontology dump
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / ContentDumps.java
1 package org.simantics.modeling;
2
3 import java.nio.charset.Charset;
4 import java.nio.charset.StandardCharsets;
5 import java.util.Collection;
6 import java.util.Map;
7 import java.util.TreeMap;
8 import java.util.TreeSet;
9
10 import org.simantics.databoard.Bindings;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.common.utils.CommonDBUtils;
14 import org.simantics.db.common.utils.NameUtils;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.variable.Variable;
17 import org.simantics.db.layer0.variable.Variables;
18 import org.simantics.graphfile.ontology.GraphFileResource;
19 import org.simantics.layer0.Layer0;
20 import org.simantics.modeling.utils.ComponentTypeViewerPropertyInfo;
21 import org.simantics.modeling.utils.HeadlessComponentTypePropertiesResultRequest;
22 import org.simantics.structural.stubs.StructuralResource2;
23 import org.simantics.structural2.variables.Connection;
24 import org.simantics.structural2.variables.VariableConnectionPointDescriptor;
25
26 public class ContentDumps {
27     
28     private static Charset UTF8 = StandardCharsets.UTF_8;
29
30     public static byte[] sclModuleContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
31         Layer0 L0 = Layer0.getInstance(graph);
32         String def = graph.getRelatedValue(resource, L0.SCLModule_definition, Bindings.STRING); 
33         return def.getBytes(UTF8);
34     }
35
36     public static byte[] sclScriptContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
37         Layer0 L0 = Layer0.getInstance(graph);
38         String def = graph.getRelatedValue(resource, L0.SCLScript_definition, Bindings.STRING); 
39         return def.getBytes(UTF8);
40     }
41
42     public static byte[] pgraphContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
43         Layer0 L0 = Layer0.getInstance(graph);
44         String def = graph.getRelatedValue(resource, L0.PGraph_definition, Bindings.STRING); 
45         return def.getBytes(UTF8);
46     }
47
48     public static byte[] graphFileContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
49         GraphFileResource GF = GraphFileResource.getInstance(graph);
50         return graph.getRelatedValue(resource, GF.HasFiledata, Bindings.BYTE_ARRAY); 
51     }
52
53     private static StringBuilder structuralComponentContentDump(ReadGraph graph, Resource resource, StringBuilder dump) throws DatabaseException {
54
55         Variable v = Variables.getVariable(graph, resource);
56
57         TreeSet<String> types = new TreeSet<>();
58         for(Resource t : graph.getPrincipalTypes(resource)) {
59             types.add(graph.getURI(t));
60         }
61         for(String uri : types) {
62             dump.append(uri);
63             dump.append("\n");
64         }
65
66         TreeMap<String,Variable> properties = new TreeMap<>();
67         for(Variable property : v.getProperties(graph))
68             properties.put(property.getName(graph), property);
69         for(Variable property : properties.values()) {
70             String possibleValue = property.getPossiblePropertyValue(graph, "HasDisplayValue", Bindings.STRING);
71             if(possibleValue != null) {
72                 dump.append(property.getName(graph));
73                 dump.append(" ");
74                 dump.append(possibleValue);
75                 dump.append("\n");
76             }
77             if(property.getClassifications(graph).contains(StructuralResource2.URIs.ConnectionRelation)) {
78                 dump.append(property.getName(graph));
79                 Connection c = property.getValue(graph);
80                 TreeSet<String> rvis = new TreeSet<>();
81                 for(VariableConnectionPointDescriptor desc : c.getConnectionPointDescriptors(graph, null)) {
82                     rvis.add(desc.getRelativeRVI(graph, v));
83                 }
84                 for(String rvi : rvis) {
85                     dump.append(" ");
86                     dump.append(rvi);
87                 }
88                 dump.append("\n");
89             }
90         }
91
92         return dump;
93     }
94
95     public static byte[] structuralComponentContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
96         return structuralComponentContentDump(graph, resource, new StringBuilder()).toString().getBytes(UTF8);
97     }
98
99     public static byte[] structuralComponentTypeContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
100         StringBuilder dump = structuralComponentContentDump(graph, resource, new StringBuilder());
101
102         StructuralResource2 STR = StructuralResource2.getInstance(graph);
103
104         // Dump procedural component type code if present
105         String proceduralCode = graph.getPossibleRelatedValue(resource, STR.ProceduralComponentType_code, Bindings.STRING);
106         if (proceduralCode != null) {
107             dump
108             .append("\n---- ProceduralComponentType.code begins ----\n")
109             .append(proceduralCode)
110             .append("---- ProceduralComponentType.code ends ----\n");
111         }
112
113         // Dump component type SCL scripts
114         Collection<Resource> scripts = graph.getObjects(resource, STR.ComponentType_hasScript);
115         if (!scripts.isEmpty()) {
116             dump.append("\nComponentType.hasScript (").append(scripts.size()).append(")\n");
117             TreeMap<String,Resource> sortedScripts = new TreeMap<>();
118             for (Resource script : scripts)
119                 sortedScripts.put(NameUtils.getSafeName(graph, script), script);
120             for (Map.Entry<String, Resource> entry : sortedScripts.entrySet()) {
121                 String name = entry.getKey();
122                 Resource script = entry.getValue();
123                 String type = graph.getPossibleRelatedValue(script, STR.ComponentTypeScript_type, Bindings.STRING);
124                 String code = graph.getPossibleRelatedValue(script, STR.ComponentTypeScript_code, Bindings.STRING);
125                 dump
126                 .append("---- script `").append(name).append("` of type `").append(type).append("` begins ----\n")
127                 .append(code)
128                 .append("\n---- script `").append(name).append("` of type `").append(type).append("` ends ----\n");
129             }
130         }
131
132         return dump.toString().getBytes(UTF8);
133     }
134
135     public static byte[] genericParameterTypeContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
136
137         Layer0 L0 = Layer0.getInstance(graph);
138         StructuralResource2 STR = StructuralResource2.getInstance(graph);
139         Resource componentType = CommonDBUtils.possibleObjectWithType(graph, resource, L0.PartOf, STR.ComponentType);
140         
141         ComponentTypeViewerPropertyInfo pi = HeadlessComponentTypePropertiesResultRequest.readPropertyInfo(graph, resource, componentType, false);
142         StringBuilder dump = new StringBuilder();
143         dump.append(pi.type);
144         dump.append(" ");
145         dump.append(pi.defaultValue);
146         if(pi.unit != null) {
147             dump.append(" ");
148             dump.append(pi.unit);
149         }
150         dump.append(" ");
151         dump.append(pi.label);
152         if(pi.description != null) {
153             dump.append(" ");
154             dump.append(pi.description);
155         }
156         return dump.toString().getBytes(UTF8);
157         
158     }
159
160 }