-
- void addStatement(Map<String,Set<String>> statements, String predicate, String object) {
- // TODO: fix this
- if (predicate.endsWith("Inverse"))
- return;
- Set<String> objects = statements.get(predicate);
- if(objects == null) {
- objects = new TreeSet<>();
- statements.put(predicate, objects);
- }
- objects.add(object);
- }
-
- String printURI(TransferableGraph1 graph, ResourceInfo info, boolean requireURI, int indent, boolean inline) {
-
- if(requireURI && !info.hasURI)
- return null;
-
- // Check if this ResourceInfo is already inlined with some other ResourceInfo
- if (info.inlined)
- return null;
-
- Map<String,Set<String>> statements = new TreeMap<>();
- Identity consistsOf = TransferableGraphUtils.findExternal(graph, "http://www.simantics.org/Layer0-1.1/ConsistsOf");
-// Identity partOf = TransferableGraphUtils.findExternal(graph, "http://www.simantics.org/Layer0-1.1/PartOf");
- TLongHashSet processed = new TLongHashSet();
- if (DEBUG)
- System.out.println("info.owned.size " + info.owned.size() + info.owned);
- for(int i=0;i<info.owned.size();i+=2) {
- int predicate = info.owned.get(i);
- int object = info.owned.get(i+1);
- long stmId = longStm(predicate, object);
- if (DEBUG)
- System.out.println(" " + stmId + " is already processed as it is owned (" + predicate + " " + object + ")");
- processed.add(stmId);
- }
-
- TreeMap<String, Integer> predicateURIs = new TreeMap<>();
-
- TIntArrayList rawStatements = TransferableGraphUtils.getStatements(graph, info.resource);
- if (DEBUG)
- System.out.println("rawStatements size for " + info.name + " : " + rawStatements.size() + " " + rawStatements);
- for(int i=0;i<rawStatements.size();i+=2) {
- int predicate = rawStatements.get(i);
- int object = rawStatements.get(i+1);
- long stmId = longStm(predicate, object);
- if(!processed.add(stmId)) {
- if (DEBUG)
- System.out.println(" " + stmId + " is already processed (" + (predicate & 0xffffffffL) + " " + (object & 0xffffffffL) + ")");
- continue;
- }
- if (DEBUG)
- System.out.println(" " + stmId + " is currently being processed (" + (predicate & 0xffffffffL) + " " + (object & 0xffffffffL) + ")");
-// if (partOf.resource == rawStatements.get(i))
-// continue;
- if(consistsOf.resource == predicate) {
-// if (!info.owned.isEmpty() && !info.name.startsWith("blank")) {
- if (DEBUG)
- System.out.println(" is consistsof " + predicate + " (" + consistsOf.resource + ")");
- continue;
-// } else {
-// // this will be inlined so lets indent
-// indent++;
-// }
- }
- String predicateURI = rewritePredicateURI(graph, predicate);
- predicateURIs.put(predicateURI, object);
- }
- for (Entry<String, Integer> entry : predicateURIs.entrySet()) {
- String predicateURI = entry.getKey();
- int object = entry.getValue();
-
- ResourceInfo objectInfo = infos.get(object);
- if(objectInfo == null) {
- String objectURI = rewritePredicateURI(graph, object);
- if (DEBUG)
- System.out.println(" adding statement " + predicateURI + " " + objectURI);
- addStatement(statements, predicateURI, objectURI);
- } else if (objectInfo.ownedBy.size() == 1 && objectInfo.ownedBy.contains(info)) {
- // inline printing with _
- if (DEBUG)
- System.out.println(" adding inline statement " + predicateURI + " " + objectInfo.name);
- addInlineStatement(graph, statements, predicateURI, objectInfo, indent);
- } else {
- String objectName = objectInfo.name;
- if (objectName.startsWith("blank")) {
- objectName = getBlankRewrite(objectName);
+ void addStatement(Map<String, Set<String>> statements, String predicate, String object) {
+ // TODO: fix this
+ if (predicate.endsWith("Inverse"))
+ return;
+ Set<String> objects = statements.get(predicate);
+ if (objects == null) {
+ objects = new TreeSet<>();
+ statements.put(predicate, objects);
+ }
+ objects.add(object);
+ }
+
+ String printURI(ResourceInfo info, boolean requireURI, int indent, boolean inline) {
+
+ if (requireURI && !info.hasURI)
+ return null;
+
+ // Check if this ResourceInfo is already inlined with some other
+ // ResourceInfo
+ if (info.inlined)
+ return null;
+
+ Map<String, Set<String>> statements = new TreeMap<>();
+ Identity consistsOf = query.findExternalByURI("http://www.simantics.org/Layer0-1.1/ConsistsOf");
+ // Identity partOf = TransferableGraphUtils.findExternal(graph,
+ // "http://www.simantics.org/Layer0-1.1/PartOf");
+ TLongHashSet processed = new TLongHashSet();
+ if (DEBUG)
+ System.out.println("info.owned.size " + info.owned.size() + info.owned);
+ for (int i = 0; i < info.owned.size(); i += 2) {
+ int predicate = info.owned.get(i);
+ int object = info.owned.get(i + 1);
+ long stmId = longStm(predicate, object);
+ if (DEBUG)
+ System.out.println(
+ " " + stmId + " is already processed as it is owned (" + predicate + " " + object + ")");
+ processed.add(stmId);
+ }
+
+ TreeMap<String, List<Integer>> predicateURIs = new TreeMap<>();
+
+ TIntArrayList rawStatements = query.getStatements(info.resource);
+ if (DEBUG)
+ System.out.println(
+ "rawStatements size for " + info.name + " : " + rawStatements.size() + " " + rawStatements);
+ for (int i = 0; i < rawStatements.size(); i += 2) {
+ int predicate = rawStatements.get(i);
+ int object = rawStatements.get(i + 1);
+ long stmId = longStm(predicate, object);
+ if (!processed.add(stmId)) {
+ if (DEBUG)
+ System.out.println(" " + stmId + " is already processed (" + (predicate & 0xffffffffL) + " "
+ + (object & 0xffffffffL) + ")");
+ continue;
+ }
+ if (DEBUG)
+ System.out.println(" " + stmId + " is currently being processed (" + (predicate & 0xffffffffL) + " "
+ + (object & 0xffffffffL) + ")");
+ // if (partOf.resource == rawStatements.get(i))
+ // continue;
+ if (consistsOf.resource == predicate) {
+ // if (!info.owned.isEmpty() && !info.name.startsWith("blank"))
+ // {
+ if (DEBUG)
+ System.out.println(" is consistsof " + predicate + " (" + consistsOf.resource + ")");
+ continue;
+ // } else {
+ // // this will be inlined so lets indent
+ // indent++;
+ // }
+ }
+ String predicateURI = rewritePredicateURI(predicate);
+ List<Integer> objects = predicateURIs.get(predicateURI);
+ if (objects == null) {
+ objects = new ArrayList<>();
+ predicateURIs.put(predicateURI, objects);
+ }
+ objects.add(object);
+ }
+ for (Entry<String, List<Integer>> entry : predicateURIs.entrySet()) {
+ String predicateURI = entry.getKey();
+ List<Integer> objects = entry.getValue();
+ for (int object : objects) {
+ ResourceInfo objectInfo = infos.get(object);
+ if (objectInfo == null) {
+ String objectURI = rewritePredicateURI(object);
+ if (DEBUG)
+ System.out.println(" adding statement " + predicateURI + " " + objectURI);
+ addStatement(statements, predicateURI, objectURI);
+ } else if (objectInfo.ownedBy.size() == 1 && objectInfo.ownedBy.contains(info)) {
+ // inline printing with _
+ if (DEBUG)
+ System.out.println(" adding inline statement " + predicateURI + " " + objectInfo.name);
+ addInlineStatement(statements, predicateURI, objectInfo, indent);
+ } else {
+ String objectName = objectInfo.name;
+ if (objectName.startsWith("blank")) {
+ objectName = getBlankRewrite(objectName);
+ }
+ if (DEBUG)
+ System.out.println(" adding statement " + predicateURI + " " + objectName);
+ addStatement(statements, predicateURI, objectName);
+ }
+ }
+ }
+
+ if (DEBUG)
+ System.out.println(
+ "statements size for " + info.name + " : " + statements.size() + " " + statements.keySet());
+
+ StringBuilder output = new StringBuilder();
+
+ if (indent == 0 || inline) {
+ if ("ROOT".equals(info.name)) {
+ output.append("ROOT=<http:/>");
+ } else if (info.aliasURI != null) {
+ output.append(info.name + " = <" + info.aliasURI + ">");
+ } else {
+ String infoName = info.name;
+ if (infoName.startsWith("blank")) {
+ infoName = getBlankRewrite(infoName);
+ }
+ output.append(inline ? "_" : infoName);
+ }
+ Set<String> instanceOfs = statements.get("L0.InstanceOf");
+ if (instanceOfs != null) {
+ for (String instanceOf : instanceOfs) {
+ output.append(" : " + instanceOf);
+ }
+ }
+ Set<String> subrelationOfs = statements.get("L0.SubrelationOf");
+ if (subrelationOfs != null) {
+ for (String subrelationOf : subrelationOfs) {
+ output.append(" <R " + subrelationOf);
+ }
+ }
+ Set<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, Set<String>> entry : statements.entrySet()) {
+ String predicate = entry.getKey();
+ if (ignoreIdentifiers) {
+ if (predicate.equals("L0.identifier")) {
+ continue;
+ }
+ }
+ if ("L0.InstanceOf".equals(predicate))
+ continue;
+ if ("L0.SubrelationOf".equals(predicate))
+ continue;
+ if ("L0.Inherits".equals(predicate))
+ continue;
+ if ("L0.PartOf".equals(predicate))
+ continue;
+
+ // predicates can be blank
+ if (predicate.startsWith("blan")) {
+ predicate = getBlankRewrite(predicate);
+ }
+
+ Set<String> objects = entry.getValue();
+ indent(output, indent + 1);
+ if (objects.size() == 1) {
+ output.append(predicate + " " + objects.iterator().next() + "\n");
+ } else {
+ output.append(predicate + "\n");
+ for (String object : objects) {
+ indent(output, indent + 1);
+ output.append(" " + object + "\n");
+ }
+ }
+ }
+
+ TreeMap<String, Set<Integer>> ownedOrdered = new TreeMap<>();
+
+ for (int i = 0; i < info.owned.size(); i += 2) {
+ String predicateURI = rewritePredicateURI(info.owned.get(i));
+ Set<Integer> owneds = ownedOrdered.get(predicateURI);
+ if (owneds == null) {
+ owneds = new TreeSet<>();
+ ownedOrdered.put(predicateURI, owneds);
+ }
+ owneds.add(info.owned.get(i + 1));
+ }
+
+ if (DEBUG)
+ System.out.println(info.name + " : " + ownedOrdered.keySet());
+
+ for (Entry<String, Set<Integer>> entry : ownedOrdered.entrySet()) {
+ String predicateURI = entry.getKey();
+ Set<Integer> owneds = entry.getValue();
+ for (int owned : owneds) {
+ ResourceInfo ownedInfo = infos.get(owned);
+
+ String blank = printBlank(predicateURI, ownedInfo, indent + 1);
+ if (blank != null) {
+ output.append(blank);
+ }
+ }
+ }
+
+ return output.toString();
+ }
+
+ static Map<String, String> knownOntologies = new HashMap<>();
+
+ static {
+ knownOntologies.put("http://www.simantics.org/Layer0-1.1", "L0");
+ knownOntologies.put("http://www.simantics.org/Layer0X-1.1", "L0X");
+ knownOntologies.put("http://www.simantics.org/Modeling-1.2", "MOD");
+ knownOntologies.put("http://www.simantics.org/Diagram-2.2", "DIA");
+ knownOntologies.put("http://www.simantics.org/Structural-1.2", "STR");
+ knownOntologies.put("http://www.simantics.org/Document-1.2", "DOC");
+ knownOntologies.put("http://www.simantics.org/Documentation-1.2", "DOCU");
+ knownOntologies.put("http://www.simantics.org/G2D-1.1", "G2D");
+ knownOntologies.put("http://www.simantics.org/SelectionView-1.2", "SEL");
+ knownOntologies.put("http://www.simantics.org/Viewpoint-1.2", "VP");
+ knownOntologies.put("http://www.simantics.org/Image2-1.2", "IMAGE2");
+ knownOntologies.put("http://www.simantics.org/GraphFile-0.1", "GRAPHFILE");
+ knownOntologies.put("http://www.simantics.org/Project-1.2", "PROJECT");
+ knownOntologies.put("http://www.semantum.fi/Simupedia-1.0", "SIMUPEDIA");
+ knownOntologies.put("http://www.semantum.fi/SimupediaWorkbench-1.0", "SIMUPEDIA_WORKBENCH");
+ }
+
+ void prettyPrint() throws Exception {
+ log("Starting prettyPrint for TransferableGraph with {} resources, {} identities, {} statements and {} values",
+ query.getGraph().resourceCount, query.getGraph().identities, query.getGraph().statements.length, query.getGraph().values.length);
+
+ query.forIdentities(id -> {
+ int identityResource = id.resource;
+ if (id.definition instanceof Internal) {
+ Internal internal = (Internal) id.definition;
+ Identity parent = query.getIdentity(internal.parent);
+ if (parent.definition instanceof External || parent.definition instanceof Root) {
+ log("Resolving internal identity {}", id);
+ String name = "BASE";
+ ResourceInfo info = new ResourceInfo(true, name, identityResource, -1);
+ info.aliasURI = query.getURI(identityResource);
+ info.newResource = true;
+ orderedInfos.put(name, info);
+ // infos.put(id.resource, info);
+ log(" which parent is external {} and has an aliasURI {}", parent, info.aliasURI);
+ for (Identity child : query.getChildren(id))
+ recurseURI(orderedInfos, child, name, identityResource);
+ log(" and has {} children", infos.size());