-
- TreeMap<String,ResourceInfo> order = new TreeMap<>();
- for(ResourceInfo info : infos.valueCollection())
- order.put(info.name, info);
-
- for(ResourceInfo info : order.values()) {
- String uri = printURI(graph, info, true, 0, false);
- if (uri != null)
- output.append(uri);
- }
-
- for (ResourceInfo info : order.values()) {
- if (!info.hasURI && info.ownedResourcesWithPredicates.size() != 1) {
- String uri = printURI(graph, info, false, 0, false);
- if (uri != null)
- output.append(uri);
- }
- }
-// for(ResourceInfo info : order.values())
-// if(!info.hasURI && info.owner < 0)
-// printURI(graph, info, false, 0);
-
- StringBuilder refs = new StringBuilder();
- for(String ontology : referencedOntologies) {
- String key = ontologies.get(ontology);
- refs.append(key + " = <" + ontology + ">\n");
- }
- if (!referencedOntologies.isEmpty())
- refs.append("\n");
- output.insert(0, refs.toString());
-
- }
-
- public static String print(TransferableGraph1 tg) throws Exception {
- StringBuilder b = new StringBuilder();
- new PrettyPrintTG(b).prettyPrint(tg);
- return b.toString();
- }
-
- public static void main(String[] args) throws Exception {
- if (args.length < 1) {
- System.out.println("Required arguments: <input .sharedOntology file> [<output .tg file>]");
- } 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]));
- }
- }
+
+ Identity routeGraphConn = query.findExternalByURI("http://www.simantics.org/Diagram-2.2/RouteGraphConnection");
+ Identity instanceOf = query.findExternalByURI("http://www.simantics.org/Layer0-1.1/InstanceOf");
+ Identity diagramConnetionToConnection = query.findExternalByURI("http://www.simantics.org/Modeling-1.2/DiagramConnectionToConnection");
+
+ Identity elemTo = query.findExternalByURI("http://www.simantics.org/Modeling-1.2/ElementToComponent");
+ for (ResourceInfo infoo : infos.valueCollection()) {
+ if (elemTo != null) {
+ int elemToComponent = query.getPossibleObject(infoo.resource, elemTo);
+ if (elemToComponent != TransferableGraphUtils.NOT_FOUND) {
+
+ Identity component = query.getIdentity(elemToComponent);
+ Identity internal = query.getIdentity(infoo.resource);
+ if (internal.definition instanceof Internal && component.definition instanceof Internal) {
+ Internal iComponent = (Internal) component.definition;
+ infoo.name = infoo.name.substring(0, infoo.name.lastIndexOf(".") + 1) + iComponent.name;
+ }
+ }
+ }
+
+ if (instanceOf != null) {
+ int instOf = query.getPossibleObject( infoo.resource, instanceOf);
+ if (instOf != TransferableGraphUtils.NOT_FOUND && routeGraphConn != null) {
+ if (instOf == routeGraphConn.resource) {
+ // Found routegraphconnection, change name
+ // Lets go to configuration
+
+ int connection = query.getPossibleObject( infoo.resource,
+ diagramConnetionToConnection);
+ if (connection != TransferableGraphUtils.NOT_FOUND) {
+ // Gather all inverse statements to construct unique
+ // name
+ List<String> nameParts = new ArrayList<>();
+ TIntArrayList statements = query.getStatements(connection);
+ for (int i = 0; i < statements.size(); i += 2) {
+ int predicate = statements.get(i);
+ Identity possibleInverse = query.getIdentity(predicate);
+ if (possibleInverse != null) {
+ int inverseRelation = TransferableGraphUtils.NOT_FOUND;
+ int parentId = TransferableGraphUtils.NOT_FOUND;
+ if (possibleInverse.definition instanceof Internal) {
+ Internal iPossibleInverse = (Internal) possibleInverse.definition;
+ if (iPossibleInverse.name.equals("Inverse")) {
+ inverseRelation = query.getPossibleObject(
+ connection, possibleInverse);
+ parentId = iPossibleInverse.parent;
+ } else {
+ log("Unsupported inverse relation found for {} {}", infoo, iPossibleInverse);
+ }
+ } else if (possibleInverse.definition instanceof External) {
+ External ePossibleInverse = (External) possibleInverse.definition;
+ if (ePossibleInverse.name.equals("Inverse")) {
+ inverseRelation = query.getPossibleObject(
+ connection, possibleInverse);
+ parentId = ePossibleInverse.parent;
+ } else {
+ log("This external inverse is unsupported for {} {}", infoo, ePossibleInverse);
+ }
+ } else {
+ log("This type of definition is not supported {}", infoo);
+ }
+ if (inverseRelation != TransferableGraphUtils.NOT_FOUND) {
+ // Ok found something
+ Identity object = query.getIdentity(inverseRelation);
+ Identity parent = query.getIdentity(parentId);
+ String objectName, parentName;
+ if (object.definition instanceof Internal) {
+ objectName = ((Internal) object.definition).name;
+ } else if (object.definition instanceof External) {
+ objectName = ((External) object.definition).name;
+ } else {
+ log("This type of definition is not supported {}", infoo);
+ throw new Error("UNSUPPORTED " + infoo);
+ }
+ if (parent.definition instanceof Internal) {
+ parentName = ((Internal) parent.definition).name;
+ } else if (parent.definition instanceof External) {
+ parentName = ((External) parent.definition).name;
+ } else {
+ log("This type of definition is not supported {}", infoo);
+ throw new Error("UNSUPPORTED " + infoo);
+ }
+ String fullName = parentName + "_" + objectName;
+ nameParts.add(fullName);
+ } else {
+ log("No inverse relation found for {}", infoo);
+ }
+ } else {
+ log("Did not find possible inverse relation for {}", infoo);
+ }
+ }
+ nameParts.sort((o1, o2) -> o1.compareTo(o2));
+ String name = "";
+ for (String namep : nameParts) {
+ name += namep;
+ }
+ infoo.name = infoo.name.substring(0, infoo.name.lastIndexOf(".") + 1) + name;
+ } else {
+ LOGGER.error("Could not find connection for " + infoo + ". Statements of graph below");
+ LOGGER.error("Subject -> Predicate : " + infoo.resource + " -> "
+ + diagramConnetionToConnection.resource);
+ }
+ }
+ }
+ }
+ }
+ for (ResourceInfo info : infos.valueCollection()) {
+ if (info.name.startsWith("blank")) {
+ info.name = "blank" + findHash(info);
+ }
+ }
+
+ TreeMap<String, ResourceInfo> order = new TreeMap<>();
+ for (ResourceInfo info : infos.valueCollection())
+ order.put(info.name, info);
+
+ for (ResourceInfo info : order.values()) {
+ if (DEBUG)
+ System.out.print("info ");
+ String uri = printURI(info, true, 0, false);
+ if (uri != null)
+ output.append(uri);
+ }
+
+ TreeMap<String, ResourceInfo> rblanks = new TreeMap<>();
+
+ for (ResourceInfo info : order.values()) {
+ if (!info.hasURI && info.ownedResourcesWithPredicates.size() != 1) {
+ if (DEBUG)
+ System.out.print("ownedResources ");
+ if (info.name.startsWith("blank")) {
+ // These will be printed later
+ rblanks.put(getBlankRewrite(info.name), info);
+ } else {
+ String uri = printURI(info, false, 0, false);
+ if (uri != null)
+ output.append(uri);
+ }
+ }
+ }
+ // Now print blanks in order
+ for (ResourceInfo info : rblanks.values()) {
+ if (!info.hasURI && info.ownedResourcesWithPredicates.size() != 1) {
+ if (DEBUG)
+ System.out.print("ownedResources ");
+ String uri = printURI(info, false, 0, false);
+ if (uri != null)
+ output.append(uri);
+ }
+ }
+
+ // for(ResourceInfo info : order.values())
+ // if(!info.hasURI && info.owner < 0)
+ // printURI(graph, info, false, 0);
+
+ StringBuilder refs = new StringBuilder();
+ for (String ontology : referencedOntologies) {
+ String key = ontologies.get(ontology);
+ refs.append(key + " = <" + ontology + ">\n");
+ }
+ if (!referencedOntologies.isEmpty())
+ refs.append("\n");
+ output.insert(0, refs.toString());
+
+ }
+
+ private String calculateHash(ResourceInfo info) {
+ StringBuilder statementHash = new StringBuilder();
+ TreeSet<String> parts = new TreeSet<>();
+ for (int i = 0; i < info.owned.size(); i += 2) {
+ int predicate = info.owned.get(i);
+ int object = info.owned.get(i + 1);
+ // Lets resolve a unique name for this based on the statements this
+ // one has
+
+ String predicatee = rewritePredicateURI(predicate);
+ ResourceInfo objInfo = infos.get(object);
+ parts.add(predicatee + "->" + objInfo.name + ";;;");
+ }
+ // Remove this from the list
+ List<ResourceInfo> filtered = info.ownedBy.stream().filter(ri -> !ri.name.startsWith("blank"))
+ .collect(Collectors.toList());
+ for (ResourceInfo ownedBy : filtered) {
+ parts.add(ownedBy.name);
+ }
+ // check parent
+ ResourceInfo parentInfo = infos.get(info.parent);
+ if (parentInfo != null && !parentInfo.name.startsWith("blank")) {
+ parts.add("parent" + parentInfo.name);
+ } else {
+ // LOGGER.error("This should not happen");
+ }
+ for (String s : parts) {
+ statementHash.append(s);
+ }
+ String hash = makeHash(statementHash.toString().getBytes());
+ if (DEBUG)
+ System.out.println(statementHash + " -> " + hash);
+ return hash;
+ }
+
+ private String findHash(ResourceInfo info) {
+ if (info.name.startsWith("blank")) {
+ String hash = hashes.get(info.name);
+ if (hash == null) {
+ String oldName = info.name;
+ if (DEBUG)
+ System.out.print("calculating hash for " + oldName + " ");
+ hash = calculateHash(info);
+ if (hashes.put(oldName, hash) != null) {
+ System.err.println("!!!!A clash occured for " + info + " with hash " + hash);
+ }
+ }
+ return hash;
+ } else {
+ return info.name;
+ }
+ }
+
+ private THashMap<String, String> hashes = new THashMap<>();
+
+ public static String print(TransferableGraph1 tg, boolean ignoreIdentifiers) throws Exception {
+ StringBuilder b = new StringBuilder();
+ new PrettyPrintTG(tg, b, ignoreIdentifiers).prettyPrint();
+ return b.toString();
+ }
+
+ public static void main(String[] args) throws Exception {
+ if (args.length < 1) {
+ System.out.println("Required arguments: <input .sharedOntology file> [<output .tg file>]");
+ }
+ Path input;
+ Path output;
+ if (args.length < 2) {
+ input = Paths.get(args[0]);
+ output = input.getParent().resolve(input.getName(input.getNameCount() - 1) + ".fixed");
+ } else {
+ input = Paths.get(args[0]);
+ output = Paths.get(args[1]);
+ }
+ 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);
+ new PrettyPrintTG(graph).prettyPrint();
+ }
+ }
+
+ private static void log(String string, Object... args) {
+ if (LOGGER.isDebugEnabled() && DEBUG)
+ LOGGER.debug(string, args);
+ }