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[] pgraphContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
34 Layer0 L0 = Layer0.getInstance(graph);
35 String def = graph.getRelatedValue(resource, L0.PGraph_definition, Bindings.STRING);
36 return def.getBytes(UTF8);
39 public static byte[] graphFileContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
40 GraphFileResource GF = GraphFileResource.getInstance(graph);
41 return graph.getRelatedValue(resource, GF.HasFiledata, Bindings.BYTE_ARRAY);
44 public static byte[] structuralComponentContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
46 StringBuilder dump = new StringBuilder();
48 Variable v = Variables.getVariable(graph, resource);
50 TreeSet<String> types = new TreeSet<>();
51 for(Resource t : graph.getPrincipalTypes(resource)) {
52 types.add(graph.getURI(t));
54 for(String uri : types) {
59 TreeMap<String,Variable> properties = new TreeMap<>();
60 for(Variable property : v.getProperties(graph))
61 properties.put(property.getName(graph), property);
62 for(Variable property : properties.values()) {
63 String possibleValue = property.getPossiblePropertyValue(graph, "HasDisplayValue", Bindings.STRING);
64 if(possibleValue != null) {
65 dump.append(property.getName(graph));
67 dump.append(possibleValue);
70 if(property.getClassifications(graph).contains(StructuralResource2.URIs.ConnectionRelation)) {
71 dump.append(property.getName(graph));
72 Connection c = property.getValue(graph);
73 TreeSet<String> rvis = new TreeSet<>();
74 for(VariableConnectionPointDescriptor desc : c.getConnectionPointDescriptors(graph, null)) {
75 rvis.add(desc.getRelativeRVI(graph, v));
77 for(String rvi : rvis) {
84 return dump.toString().getBytes(UTF8);
87 public static byte[] genericParameterTypeContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
89 Layer0 L0 = Layer0.getInstance(graph);
90 StructuralResource2 STR = StructuralResource2.getInstance(graph);
91 Resource componentType = CommonDBUtils.possibleObjectWithType(graph, resource, L0.PartOf, STR.ComponentType);
93 ComponentTypeViewerPropertyInfo pi = HeadlessComponentTypePropertiesResultRequest.readPropertyInfo(graph, resource, componentType, false);
94 StringBuilder dump = new StringBuilder();
97 dump.append(pi.defaultValue);
100 dump.append(pi.unit);
103 dump.append(pi.label);
104 if(pi.description != null) {
106 dump.append(pi.description);
108 return dump.toString().getBytes(UTF8);