1 package org.simantics.modeling;
3 import java.nio.charset.Charset;
4 import java.nio.charset.StandardCharsets;
5 import java.util.Collection;
7 import java.util.TreeMap;
8 import java.util.TreeSet;
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;
26 public class ContentDumps {
28 private static Charset UTF8 = StandardCharsets.UTF_8;
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);
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);
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);
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);
53 private static StringBuilder structuralComponentContentDump(ReadGraph graph, Resource resource, StringBuilder dump) throws DatabaseException {
55 Variable v = Variables.getVariable(graph, resource);
57 TreeSet<String> types = new TreeSet<>();
58 for(Resource t : graph.getPrincipalTypes(resource)) {
59 types.add(graph.getURI(t));
61 for(String uri : types) {
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));
74 dump.append(possibleValue);
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));
84 for(String rvi : rvis) {
95 public static byte[] structuralComponentContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
96 return structuralComponentContentDump(graph, resource, new StringBuilder()).toString().getBytes(UTF8);
99 public static byte[] structuralComponentTypeContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
100 StringBuilder dump = structuralComponentContentDump(graph, resource, new StringBuilder());
102 StructuralResource2 STR = StructuralResource2.getInstance(graph);
104 // Dump procedural component type code if present
105 String proceduralCode = graph.getPossibleRelatedValue(resource, STR.ProceduralComponentType_code, Bindings.STRING);
106 if (proceduralCode != null) {
108 .append("\n---- ProceduralComponentType.code begins ----\n")
109 .append(proceduralCode)
110 .append("---- ProceduralComponentType.code ends ----\n");
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);
126 .append("---- script `").append(name).append("` of type `").append(type).append("` begins ----\n")
128 .append("\n---- script `").append(name).append("` of type `").append(type).append("` ends ----\n");
132 return dump.toString().getBytes(UTF8);
135 public static byte[] genericParameterTypeContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
137 Layer0 L0 = Layer0.getInstance(graph);
138 StructuralResource2 STR = StructuralResource2.getInstance(graph);
139 Resource componentType = CommonDBUtils.possibleObjectWithType(graph, resource, L0.PartOf, STR.ComponentType);
141 ComponentTypeViewerPropertyInfo pi = HeadlessComponentTypePropertiesResultRequest.readPropertyInfo(graph, resource, componentType, false);
142 StringBuilder dump = new StringBuilder();
143 dump.append(pi.type);
145 dump.append(pi.defaultValue);
146 if(pi.unit != null) {
148 dump.append(pi.unit);
151 dump.append(pi.label);
152 if(pi.description != null) {
154 dump.append(pi.description);
156 return dump.toString().getBytes(UTF8);