From 4a2458a1c9ca13f0056d0a82ffea44d5e0a298e0 Mon Sep 17 00:00:00 2001 From: Antti Villberg Date: Sat, 13 May 2017 21:16:12 +0300 Subject: [PATCH] PrettyPrintTG enhancements refs #7224 Change-Id: Ieff6b12540bc1c1e076ddda376d59e0b3529a791 --- .../graph/representation/PrettyPrintTG.java | 175 +++++++++++++++--- 1 file changed, 147 insertions(+), 28 deletions(-) diff --git a/bundles/org.simantics.graph/src/org/simantics/graph/representation/PrettyPrintTG.java b/bundles/org.simantics.graph/src/org/simantics/graph/representation/PrettyPrintTG.java index 5968b6822..3243b82bd 100644 --- a/bundles/org.simantics.graph/src/org/simantics/graph/representation/PrettyPrintTG.java +++ b/bundles/org.simantics.graph/src/org/simantics/graph/representation/PrettyPrintTG.java @@ -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 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> statements, String predicate, String object) { + List objects = statements.get(predicate); + if(objects == null) { + objects = new ArrayList(); + 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=\n"); - } else { - output.append(info.name + "\n"); - } - if(info.newResource) - output.append(" @L0.new\n"); + + Map> statements = new HashMap<>(); + Identity consistsOf = findExternal(graph, "http://www.simantics.org/Layer0-1.1/ConsistsOf"); TLongHashSet processed = new TLongHashSet(); for(int i=0;i"); + } else if (info.aliasURI != null) { + output.append(info.name + " = <" + info.aliasURI + ">"); + } else { + output.append(info.name); + } + + List instanceOfs = statements.get("L0.InstanceOf"); + if(instanceOfs != null) { + for(String instanceOf : instanceOfs) { + output.append(" : " + instanceOf); } } + + List subrelationOfs = statements.get("L0.SubrelationOf"); + if(subrelationOfs != null) { + for(String subrelationOf : subrelationOfs) { + output.append(" inherits = statements.get("L0.Inherits"); + if(inherits != null) { + for(String inherit : inherits) { + output.append(" > 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 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 = \n"); this.output.append("G2D = \n"); this.output.append("SEL = \n"); + this.output.append("VP = \n"); this.output.append("IMAGE2 = \n"); this.output.append("GRAPHFILE = \n"); this.output.append("APROS_OPER = \n"); @@ -276,15 +395,15 @@ public class PrettyPrintTG extends TransferableGraphUtils { this.output.append("SIMUPEDIA_WB = \n"); this.output.append("SIMUPEDIA_STD = \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 { -- 2.47.1