]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
PrettyPrintTG enhancements 21/521/2
authorAntti Villberg <antti.villberg@semantum.fi>
Sat, 13 May 2017 18:16:12 +0000 (21:16 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 15 May 2017 11:41:32 +0000 (14:41 +0300)
refs #7224

Change-Id: Ieff6b12540bc1c1e076ddda376d59e0b3529a791

bundles/org.simantics.graph/src/org/simantics/graph/representation/PrettyPrintTG.java

index 5968b6822e35a122fac68158119151783247c4d8..3243b82bd1c3aeb6be8e424ac7b907845ddaa240 100644 (file)
@@ -7,10 +7,16 @@ import java.io.InputStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 import java.util.TreeMap;
 
 import org.simantics.databoard.binding.Binding;
+import org.simantics.databoard.binding.mutable.Variant;
 import org.simantics.databoard.container.DataContainers;
+import org.simantics.databoard.type.Datatype;
 
 import gnu.trove.list.array.TIntArrayList;
 import gnu.trove.map.hash.TIntObjectHashMap;
@@ -24,7 +30,7 @@ public class PrettyPrintTG extends TransferableGraphUtils {
 
        int blankCounter = 0;
        
-       StringBuilder output = new StringBuilder();
+       final StringBuilder output;
        
        static class ResourceInfo {
                final boolean hasURI;
@@ -33,6 +39,7 @@ public class PrettyPrintTG extends TransferableGraphUtils {
                boolean newResource = false;
                int owner = 0;
                int ownerPredicate = 0;
+               String aliasURI = null;
                TIntArrayList owned = new TIntArrayList();
                TIntArrayList statements = new TIntArrayList();
                public ResourceInfo(boolean hasURI, String name, int resource) {
@@ -42,10 +49,23 @@ public class PrettyPrintTG extends TransferableGraphUtils {
                }
        }
        
+       public PrettyPrintTG(StringBuilder b) {
+               output = b;
+       }
+       
+       public PrettyPrintTG() {
+               this(new StringBuilder());
+       }
+
        TIntObjectHashMap<ResourceInfo> infos = new TIntObjectHashMap<>();
        
+       String tgNodeName(String name) {
+               if(name.contains(" ")) return "\"" + name + "\"";
+               else return name;
+       }
+       
        ResourceInfo recurseURI(TransferableGraph1 graph, Identity parent, String parentName) {
-               String name = parentName + ".\"" + getName(parent) + "\"";
+               String name = parentName + "." + tgNodeName(getName(parent));
                ResourceInfo info = new ResourceInfo(true, name, parent.resource); 
                infos.put(parent.resource, info);
                for(Identity child : getChildren(graph, parent)) {
@@ -62,6 +82,12 @@ public class PrettyPrintTG extends TransferableGraphUtils {
                        if(objectId != null) {
                                if(objectId.definition instanceof External) continue;
                        }
+                       Value value = TransferableGraphUtils.findValue(graph, object);
+                       if(value != null) {
+                               infos.put(object, new ResourceInfo(false, printValue(value), object));
+                               continue;
+                       }
+                       
                        ResourceInfo existing = infos.get(object);
                        if(existing == null) {
                                existing = new ResourceInfo(false, "blank" + blankCounter++, object);
@@ -91,6 +117,12 @@ public class PrettyPrintTG extends TransferableGraphUtils {
                info.statements = statements;
        }
        
+       String printValue(Value value) {
+               Variant variant = value.value;
+               Datatype dt = variant.getBinding().type();
+               return "\"" + variant.getValue() + "\"";
+       }
+       
        void fixInstanceOf(TransferableGraph1 graph, ResourceInfo info) {
                Identity id = getIdentity(graph, info.resource);
                if(id == null) return;
@@ -106,8 +138,8 @@ public class PrettyPrintTG extends TransferableGraphUtils {
                String name = ext.name;
                if(name.contains(" ")) name = name.replace(" ", "_").replaceAll("@", "_");//name = "\"" + name + "\"";
                int parentId = ext.parent;
-               if(parentId == 0) return ext.name;
-               else {
+               //if(parentId == 0) return ext.name;
+//             else {
                        Identity id = getIdentity(tg, parentId);
                        if(id.definition instanceof External) {
                                return getExternalURI(tg, (External)id.definition) + "/" + name;
@@ -117,7 +149,7 @@ public class PrettyPrintTG extends TransferableGraphUtils {
                        } else {
                                return null;
                        }
-               }
+//             }
        }
 
        public static String getExternalURI(TransferableGraph1 tg, int resource) {
@@ -143,6 +175,7 @@ public class PrettyPrintTG extends TransferableGraphUtils {
                uri = uri.replace("http://www.simantics.org/G2D-1.1", "G2D");
                uri = uri.replace("http://www.simantics.org/Image2-1.2", "IMAGE2");
                uri = uri.replace("http://www.simantics.org/SelectionView-1.2", "SEL");
+               uri = uri.replace("http://www.simantics.org/Viewpoint-1.2", "VP");
                uri = uri.replace("http://www.simantics.org/GraphFile-0.1", "GRAPHFILE");
                uri = uri.replace("http://www.semantum.fi/Simupedia-1.0", "SIMUPEDIA");
                uri = uri.replace("http://www.semantum.fi/SimupediaWorkbench-1.0", "SIMUPEDIA_WB");
@@ -177,16 +210,22 @@ public class PrettyPrintTG extends TransferableGraphUtils {
        long longStm(int predicate, int object) {
         return (predicate<<32) | (object & 0xffffffffL); 
        }
+
+       void addStatement(Map<String,List<String>> statements, String predicate, String object) {
+               List<String> objects = statements.get(predicate);
+               if(objects == null) {
+                       objects = new ArrayList<String>();
+                       statements.put(predicate, objects);
+               }
+               objects.add(object);
+       }
        
        void printURI(TransferableGraph1 graph, ResourceInfo info) {
+               
                if(!info.hasURI) return;
-               if("ROOT".equals(info.name)) {
-                       output.append("ROOT=<http:/>\n");
-               } else {
-                       output.append(info.name + "\n");
-               }
-               if(info.newResource)
-                       output.append("  @L0.new\n");
+               
+               Map<String,List<String>> statements = new HashMap<>();
+               Identity consistsOf = findExternal(graph, "http://www.simantics.org/Layer0-1.1/ConsistsOf");
                TLongHashSet processed = new TLongHashSet();
                for(int i=0;i<info.owned.size();i+=2) {
                        String predicateURI = rewritePredicateURI(graph, info.owned.get(i));
@@ -199,7 +238,6 @@ public class PrettyPrintTG extends TransferableGraphUtils {
                        processed.add(stmId);
                        printBlank(graph, predicateURI, ownedInfo);
                }
-               Identity consistsOf = findExternal(graph, "http://www.simantics.org/Layer0-1.1/ConsistsOf");
                for(int i=0;i<info.statements.size();i+=2) {
                        long stmId = longStm(info.statements.get(i), info.statements.get(i+1));
                        if(processed.contains(stmId)) continue;
@@ -208,29 +246,109 @@ public class PrettyPrintTG extends TransferableGraphUtils {
                        ResourceInfo objectInfo = infos.get(info.statements.get(i+1));
                        if(objectInfo == null) {
                                String objectURI = rewritePredicateURI(graph, info.statements.get(i+1));
-                               output.append("  " + predicateURI + " " + objectURI + "\n");
+                               //output.append("  " + predicateURI + " " + objectURI + "\n");
+                               addStatement(statements, predicateURI, objectURI);
                        } else {
-                               output.append("  " + predicateURI + " " + objectInfo.name + "\n");
+                               //output.append("  " + predicateURI + " " + objectInfo.name + "\n");
+                               addStatement(statements, predicateURI, objectInfo.name);
+                       }
+               }
+               
+               if("ROOT".equals(info.name)) {
+                       output.append("ROOT=<http:/>");
+               } else if (info.aliasURI != null) {
+                       output.append(info.name + " = <" + info.aliasURI + ">");
+               } else {
+                       output.append(info.name);
+               }
+               
+               List<String> instanceOfs = statements.get("L0.InstanceOf");
+               if(instanceOfs != null) {
+                       for(String instanceOf : instanceOfs) {
+                               output.append(" : " + instanceOf);              
                        }
                }
+
+               List<String> subrelationOfs = statements.get("L0.SubrelationOf");
+               if(subrelationOfs != null) {
+                       for(String subrelationOf : subrelationOfs) {
+                               output.append(" <R " + subrelationOf);          
+                       }
+               }
+               
+               List<String> inherits = statements.get("L0.Inherits");
+               if(inherits != null) {
+                       for(String inherit : inherits) {
+                               output.append(" <T " + inherit);                
+                       }
+               }
+
+               output.append("\n");
+               
+               if(info.newResource)
+                       output.append("  @L0.new\n");
+               
+               for(Map.Entry<String, List<String>> entry : statements.entrySet()) {
+                       String predicate = entry.getKey();
+                       if("L0.InstanceOf".equals(predicate)) continue;
+                       if("L0.SubrelationOf".equals(predicate)) continue;
+                       if("L0.Inherits".equals(predicate)) continue;
+                       List<String> objects = entry.getValue();
+                       if(objects.size() == 1) {
+                               output.append("  " + predicate + " " + objects.iterator().next() + "\n");       
+                       } else{
+                               output.append("  " + predicate + "\n"); 
+                               for(String object : objects) {
+                                       output.append("    " + object + "\n");  
+                               }
+                       }
+               }
+               
        }
 
        void prettyPrint(Path input, Path output) throws Exception {
+
                System.out.format("Converting exported shared ontology%n\t" + input.toString() + "%nto bundle-compatible ontology%n\t" + output.toString());
                try (InputStream is = new BufferedInputStream(Files.newInputStream(input), 128*1024)) {
+                       
                        DataInput dis = new DataInputStream(is);
                        org.simantics.databoard.container.DataContainer container = 
                                        DataContainers.readFile(dis); 
                        Binding binding = TransferableGraph1.BINDING;
                        TransferableGraph1 graph = (TransferableGraph1)container.content.getValue(binding);
+                       prettyPrint(graph);
+                       Files.write(output, this.output.toString().getBytes());
+                       
+               }
+
+       }
+
+       void prettyPrint(TransferableGraph1 graph) {
+       
                        // Discover resources with URI
-                       for(Identity id : TransferableGraphUtils.getRoots(graph)) {
-                               String name = "ROOT";
-                               ResourceInfo info = new ResourceInfo(true, name, id.resource); 
-                               infos.put(id.resource, info);
-                               for(Identity child : getChildren(graph, id)) {
-                                       ResourceInfo childInfo = recurseURI(graph, child, name);
-                                       childInfo.newResource = true;
+//                     for(Identity id : TransferableGraphUtils.getRoots(graph)) {
+//                             String name = "ROOT";
+//                             ResourceInfo info = new ResourceInfo(true, name, id.resource); 
+//                             infos.put(id.resource, info);
+//                             for(Identity child : getChildren(graph, id)) {
+//                                     ResourceInfo childInfo = recurseURI(graph, child, name);
+//                                     childInfo.newResource = true;
+//                             }
+//                     }
+                       for(Identity id : graph.identities) {
+                               if(id.definition instanceof Internal) {
+                                       Internal internal = (Internal)id.definition;
+                                       Identity parent = TransferableGraphUtils.getIdentity(graph, internal.parent);
+                                       if(parent.definition instanceof External) {
+                                               String name = "BASE";
+                                               ResourceInfo info = new ResourceInfo(true, name, id.resource);
+                                               info.aliasURI = TransferableGraphUtils.getURI(graph, id.resource);
+                                               info.newResource = true;
+                                               infos.put(id.resource, info);
+                                               for(Identity child : getChildren(graph, id)) {
+                                                       recurseURI(graph, child, name);
+                                               }
+                                       }
                                }
                        }
                        // Discover other resources
@@ -269,6 +387,7 @@ public class PrettyPrintTG extends TransferableGraphUtils {
                        this.output.append("DOC = <http://www.simantics.org/Document-1.2>\n");
                        this.output.append("G2D = <http://www.simantics.org/G2D-1.1>\n");
                        this.output.append("SEL = <http://www.simantics.org/SelectionView-1.2>\n");
+                       this.output.append("VP =  <http://www.simantics.org.Viewpoint-1.2>\n");
                        this.output.append("IMAGE2 = <http://www.simantics.org/Image2-1.2>\n");
                        this.output.append("GRAPHFILE = <http://www.simantics.org/GraphFile-0.1>\n");
                        this.output.append("APROS_OPER = <http://www.apros.fi/OperationUI-6.6>\n");
@@ -276,15 +395,15 @@ public class PrettyPrintTG extends TransferableGraphUtils {
                        this.output.append("SIMUPEDIA_WB = <http://www.semantum.fi/SimupediaWorkbench-1.0>\n");
                        this.output.append("SIMUPEDIA_STD = <http://semantum.fi/SimupediaStandardLibrary@1.3-trunk>\n");
                        
-//                     uri = uri.replace("http://semantum.fi/SimupediaStandardLibrary@1.3-trunk/", "SIMUPEDIA_STD.");
-
-                       
                        for(ResourceInfo info : order.values())
                                printURI(graph, info);
                        
-                       Files.write(output, this.output.toString().getBytes());
-                               
-               }
+       }
+       
+       public static String print(TransferableGraph1 tg) {
+               StringBuilder b = new StringBuilder();
+               new PrettyPrintTG(b).prettyPrint(tg);
+               return b.toString();
        }
 
        public static void main(String[] args) throws Exception {