1 package org.simantics.modeling;
3 import java.nio.charset.Charset;
4 import java.nio.charset.StandardCharsets;
5 import java.util.TreeMap;
6 import java.util.TreeSet;
8 import org.simantics.databoard.Bindings;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.common.utils.CommonDBUtils;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.variable.Variable;
14 import org.simantics.db.layer0.variable.Variables;
15 import org.simantics.graphfile.ontology.GraphFileResource;
16 import org.simantics.layer0.Layer0;
17 import org.simantics.modeling.utils.ComponentTypeViewerPropertyInfo;
18 import org.simantics.modeling.utils.HeadlessComponentTypePropertiesResultRequest;
19 import org.simantics.structural.stubs.StructuralResource2;
20 import org.simantics.structural2.variables.Connection;
21 import org.simantics.structural2.variables.VariableConnectionPointDescriptor;
23 public class ContentDumps {
25 private static Charset UTF8 = StandardCharsets.UTF_8;
27 public static byte[] sclModuleContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
28 Layer0 L0 = Layer0.getInstance(graph);
29 String def = graph.getRelatedValue(resource, L0.SCLModule_definition, Bindings.STRING);
30 return def.getBytes(UTF8);
33 public static byte[] sclScriptContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
34 Layer0 L0 = Layer0.getInstance(graph);
35 String def = graph.getRelatedValue(resource, L0.SCLScript_definition, Bindings.STRING);
36 return def.getBytes(UTF8);
39 public static byte[] pgraphContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
40 Layer0 L0 = Layer0.getInstance(graph);
41 String def = graph.getRelatedValue(resource, L0.PGraph_definition, Bindings.STRING);
42 return def.getBytes(UTF8);
45 public static byte[] graphFileContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
46 GraphFileResource GF = GraphFileResource.getInstance(graph);
47 return graph.getRelatedValue(resource, GF.HasFiledata, Bindings.BYTE_ARRAY);
50 public static byte[] structuralComponentContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
52 StringBuilder dump = new StringBuilder();
54 Variable v = Variables.getVariable(graph, resource);
56 TreeSet<String> types = new TreeSet<>();
57 for(Resource t : graph.getPrincipalTypes(resource)) {
58 types.add(graph.getURI(t));
60 for(String uri : types) {
65 TreeMap<String,Variable> properties = new TreeMap<>();
66 for(Variable property : v.getProperties(graph))
67 properties.put(property.getName(graph), property);
68 for(Variable property : properties.values()) {
69 String possibleValue = property.getPossiblePropertyValue(graph, "HasDisplayValue", Bindings.STRING);
70 if(possibleValue != null) {
71 dump.append(property.getName(graph));
73 dump.append(possibleValue);
76 if(property.getClassifications(graph).contains(StructuralResource2.URIs.ConnectionRelation)) {
77 dump.append(property.getName(graph));
78 Connection c = property.getValue(graph);
79 TreeSet<String> rvis = new TreeSet<>();
80 for(VariableConnectionPointDescriptor desc : c.getConnectionPointDescriptors(graph, null)) {
81 rvis.add(desc.getRelativeRVI(graph, v));
83 for(String rvi : rvis) {
90 return dump.toString().getBytes(UTF8);
93 public static byte[] genericParameterTypeContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
95 Layer0 L0 = Layer0.getInstance(graph);
96 StructuralResource2 STR = StructuralResource2.getInstance(graph);
97 Resource componentType = CommonDBUtils.possibleObjectWithType(graph, resource, L0.PartOf, STR.ComponentType);
99 ComponentTypeViewerPropertyInfo pi = HeadlessComponentTypePropertiesResultRequest.readPropertyInfo(graph, resource, componentType, false);
100 StringBuilder dump = new StringBuilder();
101 dump.append(pi.type);
103 dump.append(pi.defaultValue);
104 if(pi.unit != null) {
106 dump.append(pi.unit);
109 dump.append(pi.label);
110 if(pi.description != null) {
112 dump.append(pi.description);
114 return dump.toString().getBytes(UTF8);