X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.graph%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Frepresentation%2FPrettyPrintTG.java;fp=bundles%2Forg.simantics.graph%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Frepresentation%2FPrettyPrintTG.java;h=5968b6822e35a122fac68158119151783247c4d8;hp=0000000000000000000000000000000000000000;hb=9acebe9584f8f2a78f0322b6e0e24438e7ceb984;hpb=081bf7e08a67c0af23c5846e163be39bb19f12cb 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 new file mode 100644 index 000000000..5968b6822 --- /dev/null +++ b/bundles/org.simantics.graph/src/org/simantics/graph/representation/PrettyPrintTG.java @@ -0,0 +1,302 @@ +package org.simantics.graph.representation; + +import java.io.BufferedInputStream; +import java.io.DataInput; +import java.io.DataInputStream; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.TreeMap; + +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.container.DataContainers; + +import gnu.trove.list.array.TIntArrayList; +import gnu.trove.map.hash.TIntObjectHashMap; +import gnu.trove.set.hash.TLongHashSet; + +/** + * @author Antti Villberg + * @since 1.24.0 + */ +public class PrettyPrintTG extends TransferableGraphUtils { + + int blankCounter = 0; + + StringBuilder output = new StringBuilder(); + + static class ResourceInfo { + final boolean hasURI; + final String name; + final int resource; + boolean newResource = false; + int owner = 0; + int ownerPredicate = 0; + TIntArrayList owned = new TIntArrayList(); + TIntArrayList statements = new TIntArrayList(); + public ResourceInfo(boolean hasURI, String name, int resource) { + this.hasURI = hasURI; + this.name = name; + this.resource = resource; + } + } + + TIntObjectHashMap infos = new TIntObjectHashMap<>(); + + ResourceInfo recurseURI(TransferableGraph1 graph, Identity parent, String parentName) { + String name = parentName + ".\"" + getName(parent) + "\""; + ResourceInfo info = new ResourceInfo(true, name, parent.resource); + infos.put(parent.resource, info); + for(Identity child : getChildren(graph, parent)) { + recurseURI(graph, child, name); + } + return info; + } + + void discoverBlank(TransferableGraph1 graph, int resource, TIntArrayList todo) { + TIntArrayList statements = getStatements(graph, resource); + for(int i=0;i " + predicateURI + " " + existing.name); + } else { + existing.owner = -1; + //System.err.println("Multiple owners " + info.name + " => " + predicateURI + " " + existing.name); + } + } + info.statements = statements; + } + + void fixInstanceOf(TransferableGraph1 graph, ResourceInfo info) { + Identity id = getIdentity(graph, info.resource); + if(id == null) return; + if(id.definition instanceof Internal) { + Identity instanceOf = findExternal(graph, "http://www.simantics.org/Layer0-1.1/InstanceOf"); + Identity library = findExternal(graph, "http://www.simantics.org/Layer0-1.1/Library"); + info.statements.add(instanceOf.resource); + info.statements.add(library.resource); + } + } + + public static String getExternalURI(TransferableGraph1 tg, External ext) { + String name = ext.name; + if(name.contains(" ")) name = name.replace(" ", "_").replaceAll("@", "_");//name = "\"" + name + "\""; + int parentId = ext.parent; + if(parentId == 0) return ext.name; + else { + Identity id = getIdentity(tg, parentId); + if(id.definition instanceof External) { + return getExternalURI(tg, (External)id.definition) + "/" + name; + } else if(id.definition instanceof Root) { + Root root = (Root)id.definition; + return "http:/" + root.name + "/" + name; + } else { + return null; + } + } + } + + public static String getExternalURI(TransferableGraph1 tg, int resource) { + Identity id = getIdentity(tg, resource); + if(id == null) return null; + if(id.definition instanceof External) { + External ext = (External)id.definition; + return getExternalURI(tg, ext); + } + return null; + } + + String rewritePredicateURI(TransferableGraph1 graph, int predicate) { + String uri = getExternalURI(graph, predicate); + if(uri == null) return null; + uri = uri.replace("http://www.simantics.org/Modeling-1.2", "MOD"); + uri = uri.replace("http://www.simantics.org/Layer0-1.1", "L0"); + uri = uri.replace("http://www.simantics.org/Layer0X-1.1", "L0X"); + uri = uri.replace("http://www.simantics.org/Diagram-2.2", "DIA"); + uri = uri.replace("http://www.simantics.org/Structural-1.2", "STR"); + uri = uri.replace("http://www.simantics.org/Documentation-1.2", "DOCU"); + uri = uri.replace("http://www.simantics.org/Document-1.2", "DOC"); + 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/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"); + uri = uri.replace("http://www.apros.fi/OperationUI-6.6", "APROS_OPER"); + uri = uri.replace("http://semantum.fi/SimupediaStandardLibrary@1.3-trunk", "SIMUPEDIA_STD"); + uri = uri.replace("/", "."); + return uri; + } + + void printBlank(TransferableGraph1 graph, String predicateURI2, ResourceInfo info) { + + if(info.hasURI) return; + output.append(" " + predicateURI2 + " " + info.name + "\n"); + + Value value = findValue(graph, info.resource); + if(value != null) { + + } + +// for(int i=0;i\n"); + } else { + output.append(info.name + "\n"); + } + if(info.newResource) + output.append(" @L0.new\n"); + TLongHashSet processed = new TLongHashSet(); + for(int i=0;i 0) { + ResourceInfo ownerInfo = infos.get(info.owner); + ownerInfo.owned.add(info.ownerPredicate); + ownerInfo.owned.add(info.resource); + } else if (info.owner == 0) { + //System.err.println("faf1"); + } else if (info.owner == -1) { + //System.err.println("faf2"); + } + + TreeMap order = new TreeMap<>(); + for(ResourceInfo info : infos.valueCollection()) + order.put(info.name, info); + + this.output.append("MOD = \n"); + this.output.append("L0 = \n"); + this.output.append("L0X = \n"); + this.output.append("DIA = \n"); + this.output.append("STR = \n"); + this.output.append("DOCU = \n"); + this.output.append("DOC = \n"); + this.output.append("G2D = \n"); + this.output.append("SEL = \n"); + this.output.append("IMAGE2 = \n"); + this.output.append("GRAPHFILE = \n"); + this.output.append("APROS_OPER = \n"); + this.output.append("SIMUPEDIA = \n"); + 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 void main(String[] args) throws Exception { + if (args.length < 1) { + System.out.println("Required arguments: []"); + } else if (args.length < 2) { + Path input = Paths.get(args[0]); + Path output = input.getParent().resolve(input.getName(input.getNameCount()-1) + ".fixed"); + new PrettyPrintTG().prettyPrint(input, output); + } else { + new PrettyPrintTG().prettyPrint(Paths.get(args[0]), Paths.get(args[1])); + } + } + +}