]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
index 687544e78a52d99306436408d0211f23fa157157..39ed968dd5385034f76ccdbb520d050e9995c63c 100644 (file)
@@ -2,6 +2,8 @@ package org.simantics.modeling;
 
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
+import java.util.Collection;
+import java.util.Map;
 import java.util.TreeMap;
 import java.util.TreeSet;
 
@@ -9,6 +11,7 @@ import org.simantics.databoard.Bindings;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.common.utils.CommonDBUtils;
+import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.variable.Variable;
 import org.simantics.db.layer0.variable.Variables;
@@ -47,12 +50,10 @@ public class ContentDumps {
         return graph.getRelatedValue(resource, GF.HasFiledata, Bindings.BYTE_ARRAY); 
     }
 
-    public static byte[] structuralComponentContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
-
-        StringBuilder dump = new StringBuilder();
+    private static StringBuilder structuralComponentContentDump(ReadGraph graph, Resource resource, StringBuilder dump) throws DatabaseException {
 
         Variable v = Variables.getVariable(graph, resource);
-        
+
         TreeSet<String> types = new TreeSet<>();
         for(Resource t : graph.getPrincipalTypes(resource)) {
             types.add(graph.getURI(t));
@@ -87,6 +88,47 @@ public class ContentDumps {
                 dump.append("\n");
             }
         }
+
+        return dump;
+    }
+
+    public static byte[] structuralComponentContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
+        return structuralComponentContentDump(graph, resource, new StringBuilder()).toString().getBytes(UTF8);
+    }
+
+    public static byte[] structuralComponentTypeContentDump(ReadGraph graph, Resource resource) throws DatabaseException {
+        StringBuilder dump = structuralComponentContentDump(graph, resource, new StringBuilder());
+
+        StructuralResource2 STR = StructuralResource2.getInstance(graph);
+
+        // Dump procedural component type code if present
+        String proceduralCode = graph.getPossibleRelatedValue(resource, STR.ProceduralComponentType_code, Bindings.STRING);
+        if (proceduralCode != null) {
+            dump
+            .append("\n---- ProceduralComponentType.code begins ----\n")
+            .append(proceduralCode)
+            .append("---- ProceduralComponentType.code ends ----\n");
+        }
+
+        // Dump component type SCL scripts
+        Collection<Resource> scripts = graph.getObjects(resource, STR.ComponentType_hasScript);
+        if (!scripts.isEmpty()) {
+            dump.append("\nComponentType.hasScript (").append(scripts.size()).append(")\n");
+            TreeMap<String,Resource> sortedScripts = new TreeMap<>();
+            for (Resource script : scripts)
+                sortedScripts.put(NameUtils.getSafeName(graph, script), script);
+            for (Map.Entry<String, Resource> entry : sortedScripts.entrySet()) {
+                String name = entry.getKey();
+                Resource script = entry.getValue();
+                String type = graph.getPossibleRelatedValue(script, STR.ComponentTypeScript_type, Bindings.STRING);
+                String code = graph.getPossibleRelatedValue(script, STR.ComponentTypeScript_code, Bindings.STRING);
+                dump
+                .append("---- script `").append(name).append("` of type `").append(type).append("` begins ----\n")
+                .append(code)
+                .append("\n---- script `").append(name).append("` of type `").append(type).append("` ends ----\n");
+            }
+        }
+
         return dump.toString().getBytes(UTF8);
     }