+ // put orderedInfos to infos
+ infos.put(info.resource, info);
+ }
+
+ while(!todo.isEmpty()) {
+ int resource = todo.removeAt(todo.size()-1);
+ discoverBlank(graph, resource, todo);
+ }
+ for(ResourceInfo info : infos.valueCollection())
+ discoverOwners(graph, info);
+ // for(ResourceInfo info : infos.valueCollection())
+ // fixInstanceOf(graph, info);
+
+ for (ResourceInfo info : infos.valueCollection()) {
+// Old implementation
+// if (info.owner >= 0) {
+// ResourceInfo ownerInfo = infos.get(info.owner);
+// System.out.println("originalOwner : " + info.owner + " originalPredicate: " + info.ownerPredicate);
+// ownerInfo.owned.add(info.ownerPredicate);
+// ownerInfo.owned.add(info.resource);
+// }
+
+ if (!info.ownedResourcesWithPredicates.isEmpty() && info.ownedResourcesWithPredicates.size() == 1) {
+ info.ownedResourcesWithPredicates.forEachEntry(new TIntIntProcedure() {
+
+ @Override
+ public boolean execute(int owner, int predicate) {
+ ResourceInfo ownerInfo = infos.get(owner);
+ ownerInfo.owned.add(predicate);
+ ownerInfo.owned.add(info.resource);
+ return false;
+ }
+ });
+ }
+ }
+
+ // Resolve inverses from ownedBy list
+ for (ResourceInfo info : infos.valueCollection()) {
+ for (int i = 0; i < info.owned.size(); i+=2) {
+ int object = info.owned.get(i+1);
+ ResourceInfo inf = infos.get(object);
+ if (inf != null) {
+ info.ownedBy.remove(inf);
+ }
+ }
+ }
+
+ Identity routeGraphConn = TransferableGraphUtils.findExternal(graph, "http://www.simantics.org/Diagram-2.2/RouteGraphConnection");
+ Identity instanceOf = TransferableGraphUtils.findExternal(graph, "http://www.simantics.org/Layer0-1.1/InstanceOf");
+ Identity diagramConnetionToConnection = TransferableGraphUtils.findExternal(graph, "http://www.simantics.org/Modeling-1.2/DiagramConnectionToConnection");
+
+ for (ResourceInfo infoo : infos.valueCollection()) {
+ Identity elemTo = TransferableGraphUtils.findExternal(graph, "http://www.simantics.org/Modeling-1.2/ElementToComponent");
+ if (elemTo != null) {
+ int elemToComponent = TransferableGraphUtils.getPossibleObject2(graph, infoo.resource, elemTo);
+ if (elemToComponent != TransferableGraphUtils.NOT_FOUND) {
+
+ Identity component = TransferableGraphUtils.getIdentity(graph, elemToComponent);
+ Identity internal = TransferableGraphUtils.getIdentity(graph, 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 = TransferableGraphUtils.getPossibleObject2(graph, infoo.resource, instanceOf);
+ if (instOf != TransferableGraphUtils.NOT_FOUND && routeGraphConn != null) {
+ if (instOf == routeGraphConn.resource) {
+ // Found routegraphconnection, change name
+ // Lets go to configuration
+
+ int connection = TransferableGraphUtils.getPossibleObject2(graph, infoo.resource, diagramConnetionToConnection);
+ if (connection != TransferableGraphUtils.NOT_FOUND) {
+ // Gather all inverse statements to construct unique name
+ List<String> nameParts = new ArrayList<>();
+ TIntArrayList statements = TransferableGraphUtils.getStatements(graph, connection);
+ for (int i = 0; i < statements.size(); i+=2) {
+ int predicate = statements.get(i);
+ Identity possibleInverse = TransferableGraphUtils.getIdentity(graph, 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 = TransferableGraphUtils.getPossibleObject2(graph, connection, possibleInverse);
+ parentId = iPossibleInverse.parent;
+ } else {
+ LOGGER.error("THIS UNSUPPORTED for " + infoo + " " + iPossibleInverse);
+ }
+ } else if (possibleInverse.definition instanceof External) {
+ External ePossibleInverse = (External) possibleInverse.definition;
+ if (ePossibleInverse.name.equals("Inverse")) {
+ inverseRelation = TransferableGraphUtils.getPossibleObject2(graph, connection, possibleInverse);
+ parentId = ePossibleInverse.parent;
+ } else {
+ // This is not an inverse
+// LOGGER.error("THIS UNSUPPORTED TOO");
+ }
+ } else {
+ LOGGER.error("UNSUPPORTED for " + infoo + " " );
+ }
+ if (inverseRelation != TransferableGraphUtils.NOT_FOUND) {
+ // Ok found something
+ Identity object = TransferableGraphUtils.getIdentity(graph, inverseRelation);
+ Identity parent = TransferableGraphUtils.getIdentity(graph, 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 {
+ LOGGER.error("UNSUPPORTED " + 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 {
+ LOGGER.error("UNSUPPORTED " + infoo);
+ throw new Error("UNSUPPORTED " + infoo);
+ }
+ String fullName = parentName + "_" + objectName;
+ nameParts.add(fullName);
+ } else {
+ LOGGER.error("THIS IS ALSO UNSupported");
+ }
+ } else {
+ LOGGER.error("HERE");
+ }
+ }
+ 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(Arrays.toString(graph.statements));
+ LOGGER.error("Subject -> Predicate : " + infoo.resource + " -> " + diagramConnetionToConnection.resource);
+ }
+ }
+ }
+ }
+ }
+ for (ResourceInfo info : infos.valueCollection()) {
+ if (info.name.startsWith("blank")) {
+ info.name = "blank" + findHash(graph, 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(graph, 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(graph, 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(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());
+