Support for creating shared ontology dump to git
[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.TreeMap;
6 import java.util.TreeSet;
7
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;
22
23 public class ContentDumps {
24     
25     private static Charset UTF8 = StandardCharsets.UTF_8;
26
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);
31     }
32
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);
37     }
38
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); 
42     }
43
44     public static byte[] structuralComponentContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
45
46         StringBuilder dump = new StringBuilder();
47
48         Variable v = Variables.getVariable(graph, resource);
49         
50         TreeSet<String> types = new TreeSet<>();
51         for(Resource t : graph.getPrincipalTypes(resource)) {
52             types.add(graph.getURI(t));
53         }
54         for(String uri : types) {
55             dump.append(uri);
56             dump.append("\n");
57         }
58
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));
66                 dump.append(" ");
67                 dump.append(possibleValue);
68                 dump.append("\n");
69             }
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));
76                 }
77                 for(String rvi : rvis) {
78                     dump.append(" ");
79                     dump.append(rvi);
80                 }
81                 dump.append("\n");
82             }
83         }
84         return dump.toString().getBytes(UTF8);
85     }
86
87     public static byte[] genericParameterTypeContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
88
89         Layer0 L0 = Layer0.getInstance(graph);
90         StructuralResource2 STR = StructuralResource2.getInstance(graph);
91         Resource componentType = CommonDBUtils.possibleObjectWithType(graph, resource, L0.PartOf, STR.ComponentType);
92         
93         ComponentTypeViewerPropertyInfo pi = HeadlessComponentTypePropertiesResultRequest.readPropertyInfo(graph, resource, componentType, false);
94         StringBuilder dump = new StringBuilder();
95         dump.append(pi.type);
96         dump.append(" ");
97         dump.append(pi.defaultValue);
98         if(pi.unit != null) {
99             dump.append(" ");
100             dump.append(pi.unit);
101         }
102         dump.append(" ");
103         dump.append(pi.label);
104         if(pi.description != null) {
105             dump.append(" ");
106             dump.append(pi.description);
107         }
108         return dump.toString().getBytes(UTF8);
109         
110     }
111
112 }