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;
int blankCounter = 0;
- StringBuilder output = new StringBuilder();
+ final StringBuilder output;
static class ResourceInfo {
final boolean hasURI;
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) {
}
}
+ 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)) {
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);
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;
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;
} else {
return null;
}
- }
+// }
}
public static String getExternalURI(TransferableGraph1 tg, int resource) {
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");
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));
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;
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
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");
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 {