]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Lots of fixes for PrettyPrintTG
authorjsimomaa <jani.simomaa@gmail.com>
Thu, 15 Jun 2017 06:30:44 +0000 (09:30 +0300)
committerjsimomaa <jani.simomaa@gmail.com>
Thu, 15 Jun 2017 07:06:41 +0000 (10:06 +0300)
* Ordering predicates alphanumerically for deterministic ordering
between two databases
* Filtering L0.identifier statements
* Hash-based naming for blanks for ordering output print

Still needs some optimiziations to improve performance lost with these
changes..

refs #7276

Change-Id: I6848b319094863a8c85e4deca91df721b1642ce7

bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/Layer0Utils.java
bundles/org.simantics.graph/src/org/simantics/graph/refactoring/FixExportedOntology.java
bundles/org.simantics.graph/src/org/simantics/graph/representation/PrettyPrintTG.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/PGraphEditorDocumentProvider.java
bundles/org.simantics.scl.db/scl/Simantics/DB.scl

index 08dd63ce0e25c4bf11084bc8402eb802cd51a49d..198ee41b77e060ad4cdf205e46a10038cbaa494b 100644 (file)
@@ -1379,11 +1379,12 @@ public class Layer0Utils {
        
     }
 
-    public static String prettyPrintResource(ReadGraph graph, Resource resource) throws Exception {
+    public static String prettyPrintResource(ReadGraph graph, Resource resource, boolean ignoreIdentifiers) throws Exception {
         TransferableGraphSource source = makeTGSource(graph, resource);
         TransferableGraph1 tg = TransferableGraphs.create(graph, source);
         GraphRefactoringUtils.fixOntologyExport(tg);
-        return PrettyPrintTG.print(tg);
+        System.out.println("Printing resoure " + graph.getURI(resource));
+        return PrettyPrintTG.print(tg, ignoreIdentifiers);
     }
 
 }
index ea449417d305ee7eac034af76e3ceadec07e7b79..1c567b1e21d39294f00652a0e8467e33cf803044 100644 (file)
@@ -48,7 +48,7 @@ public class FixExportedOntology {
                        Path input = Paths.get(args[0]);
                        Path output1 = input.getParent().resolve("graph.tg");
                        TransferableGraph1 tg = convertExportedSharedOntologyIntoBundleOntology(input, output1);
-                       String listing = PrettyPrintTG.print(tg);
+                       String listing = PrettyPrintTG.print(tg, false);
                        Path output2 = Paths.get(args[0].substring(0, args[0].length() - ".sharedLibrary".length()) + ".pgraph");
                        Files.write(output2, listing.getBytes(),StandardOpenOption.CREATE);
                } else {
index 0640f42f495ccc646f4770435ccf300bb834afef..23ce5e56c1258033749ad5695ad4d6635b048b39 100644 (file)
@@ -10,14 +10,19 @@ import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Set;
 import java.util.TreeMap;
 import java.util.TreeSet;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+import java.util.stream.Collectors;
 
 import org.simantics.databoard.Bindings;
 import org.simantics.databoard.binding.Binding;
@@ -30,6 +35,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import gnu.trove.list.array.TIntArrayList;
+import gnu.trove.map.hash.THashMap;
 import gnu.trove.map.hash.TIntIntHashMap;
 import gnu.trove.map.hash.TIntObjectHashMap;
 import gnu.trove.procedure.TIntIntProcedure;
@@ -46,6 +52,8 @@ public class PrettyPrintTG {
     private static final boolean DEBUG = false;
     
        int blankCounter = 0;
+       int newBlankCounter = 0;
+       Map<String, String> blankRewrites = new HashMap<>();
        MessageDigest m;
 
        private final Pattern versionExtractPattern = Pattern.compile("^.*-(\\d+\\.\\d+)");
@@ -54,9 +62,11 @@ public class PrettyPrintTG {
        final Map<String,String> ontologies = new HashMap<>(knownOntologies);
        final Set<String> referencedOntologies = new TreeSet<>();
 
+    private boolean ignoreIdentifiers;
+
        static class ResourceInfo {
                final boolean hasURI;
-               final String name;
+               String name;
                final int resource;
                boolean newResource = false;
                
@@ -90,15 +100,17 @@ public class PrettyPrintTG {
                }
        }
 
-       public PrettyPrintTG(StringBuilder b) throws NoSuchAlgorithmException {
+       public PrettyPrintTG(StringBuilder b, boolean ignoreIdentifiers) throws NoSuchAlgorithmException {
                output = b;
                m = MessageDigest.getInstance("SHA-256");
+               this.ignoreIdentifiers = ignoreIdentifiers;
        }
 
        public PrettyPrintTG() throws NoSuchAlgorithmException {
-               this(new StringBuilder());
+               this(new StringBuilder(), false);
        }
 
+       TreeMap<String, ResourceInfo> orderedInfos = new TreeMap<>();
        TIntObjectHashMap<ResourceInfo> infos = new TIntObjectHashMap<>();
 
        String tgNodeName(String name) {
@@ -108,36 +120,69 @@ public class PrettyPrintTG {
 
        ResourceInfo recurseURI(TransferableGraph1 graph, Identity parent, String parentName, int parentId) {
                String name = parentName + "." + tgNodeName(TransferableGraphUtils.getName(parent));
-               ResourceInfo info = new ResourceInfo(true, name, parent.resource, parentId); 
-               infos.put(parent.resource, info);
+               ResourceInfo info = new ResourceInfo(true, name, parent.resource, parentId);
+               orderedInfos.put(name, info);
+//             infos.put(parent.resource, info);
                for(Identity child : TransferableGraphUtils.getChildren(graph, parent)) {
                        recurseURI(graph, child, name, info.resource);
                }
                return info;
        }
 
+    private TreeMap<String, TreeSet<Integer>> sortByPredicateUniqueStatements(TransferableGraph1 graph, int resource) {
+        TreeMap<String, TreeSet<Integer>> results = new TreeMap<>();
+        TIntArrayList statements = TransferableGraphUtils.getStatements(graph, resource);
+        for (int i = 0; i < statements.size(); i += 2) {
+            int predicate = statements.get(i);
+            String predicateURI = TransferableGraphUtils.getURI(graph, predicate);
+            TreeSet<Integer> objects = results.get(predicateURI);
+            if (objects == null) {
+                objects = new TreeSet<>();
+            }
+            objects.add(statements.get(i+1));
+            results.put(predicateURI, objects);
+        }
+        return results;
+    }
+       
        void discoverBlank(TransferableGraph1 graph, int resource, TIntArrayList todo) throws Exception {
-               TIntArrayList statements = TransferableGraphUtils.getStatements(graph, resource);
-               for(int i=0;i<statements.size();i+=2) {
-                       int object = statements.get(i+1);
-                       Identity objectId = TransferableGraphUtils.getIdentity(graph, object);
-                       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, resource));
-                               continue;
-                       }
-                       ResourceInfo existing = infos.get(object);
-                       if(existing == null) {
-                               existing = new ResourceInfo(false, "blank" + blankCounter++, object, resource);
-                               infos.put(object, existing);
-                               todo.add(object);
-                       }
+//             TIntArrayList statements = TransferableGraphUtils.getStatements(graph, resource);
+//             for(int i=0;i<statements.size();i+=2) {
+               for (TreeSet<Integer> objects : sortByPredicateUniqueStatements(graph, resource).values()) {
+                   for (int object : objects) {
+    //                 int object = statements.get(i+1);
+                       Identity objectId = TransferableGraphUtils.getIdentity(graph, object);
+                       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, resource));
+                               continue;
+                       }
+                       ResourceInfo existing = infos.get(object);
+                       if(existing == null) {
+                           
+                    existing = new ResourceInfo(false, "blank" + blankCounter++, object, resource);
+//                    existing = new ResourceInfo(false, "blank" + makeHash(statementHash.toString().getBytes()), object, resource);
+                               
+                               String resourceURI = TransferableGraphUtils.getURI(graph, resource);
+                               String objectURI = TransferableGraphUtils.getURI(graph, object);
+                               
+    //                         System.out.println("created blank" + blankCounter + " with object " + object + " resource " + resource);
+                               infos.put(object, existing);
+                               todo.add(object);
+                       }
+               }
                }
        }
-
+       
+       private String makeHash(byte[] data) {
+        m.reset();
+        m.update(data, 0, data.length);
+        return new BigInteger(1, m.digest()).toString(16);
+       }
+       
        void discoverOwners(TransferableGraph1 graph, ResourceInfo info) {
            log("Discovering owners for " + info);
                int resource = info.resource;
@@ -148,14 +193,19 @@ public class PrettyPrintTG {
                        ResourceInfo existing = infos.get(object);
                        if(existing != null) {
                 // Add all owners here for now and resolve the best owner later
-                existing.ownedResourcesWithPredicates.put(resource, predicate);
+                
                            // Check if predicate is inverse, this just resolves all predicates to be inverse with ending "Inverse"..
                            String predicateUri = rewritePredicateURI(graph, predicate);
-                           if (!predicateUri.endsWith("Inverse")) {
-                               existing.ownedBy.add(info);
-                            if (LOGGER.isDebugEnabled()) {
-                                   LOGGER.debug("    " + existing + " owns " + info + " with " + predicateUri);
-                            }
+                           if (!predicateUri.endsWith("Inverse") && !predicateUri.endsWith("Of")) {
+                               existing.ownedResourcesWithPredicates.put(resource, predicate);
+//                             if (predicateUri.endsWith("Of")) {
+//                                 System.out.println("asd");
+//                             } else {
+                               existing.ownedBy.add(info);
+                               log("    " + existing + " owns " + info + " with " + predicateUri);
+//                             }
+                           } else {
+//                             System.out.println("asd");
                            }
                        }
                }
@@ -305,10 +355,27 @@ public class PrettyPrintTG {
                    return null;
                
                StringBuilder output = new StringBuilder();
-               indent(output, indent);
-               output.append(predicateURI2 + " " + info.name + "\n");
+               
+               String infoName = info.name;
+               if (infoName.startsWith("blank")) {
+                   infoName = getBlankRewrite(infoName);
+               }
+               
+               if (ignoreIdentifiers) {
+                   if (predicateURI2.contains("L0.identifier")) {
+                       // Do nothing
+                   } else {
+                       indent(output, indent);
+                       output.append(predicateURI2 + " " + infoName + "\n");
+                   }
+               } else {
+                   indent(output, indent);
+                   output.append(predicateURI2 + " " + infoName + "\n");
+               }
 
                if (info.ownedResourcesWithPredicates.isEmpty()) {
+                   if (DEBUG)
+                       System.out.print("printBlank");
                    String uri = printURI(graph, info, false, indent, false);
                    if (uri != null)
                        output.append(uri);
@@ -319,10 +386,21 @@ public class PrettyPrintTG {
                return output.toString();
        }
 
-       static long longStm(int predicate, int object) {
-               return (predicate<<32) | (object & 0xffffffffL); 
-       }
+    private String getBlankRewrite(String infoName) {
+        String rewrite = blankRewrites.get(infoName);
+        if (rewrite == null) {
+            rewrite = "rblank" + newBlankCounter++;
+            if (DEBUG)
+                System.out.println("rewrote " + infoName + " to " + rewrite);
+            blankRewrites.put(infoName, rewrite);
+        }
+        return rewrite;
+//        return infoName;
+    }
 
+    static long longStm(int predicate, int object) {
+               return ((predicate & 0xffffffffL)<<32) | (object & 0xffffffffL); 
+       }
 
     private void addInlineStatement(TransferableGraph1 graph, Map<String, Set<String>> statements, String predicate, ResourceInfo objectInfo, int indent) {
         Set<String> objects = statements.get(predicate);
@@ -363,43 +441,79 @@ public class PrettyPrintTG {
 
                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) {
-                       long stmId = longStm(info.owned.get(i), info.owned.get(i+1));
+                   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) {
-                       long stmId = longStm(rawStatements.get(i), rawStatements.get(i+1));
-                       if(!processed.add(stmId)) continue;
-                       if(consistsOf.resource == rawStatements.get(i)) continue;
-                       String predicateURI = rewritePredicateURI(graph, rawStatements.get(i));
-                       ResourceInfo objectInfo = infos.get(rawStatements.get(i+1));
+                   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, rawStatements.get(i+1));
+                               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 {
-                               addStatement(statements, predicateURI, objectInfo.name);
+                String objectName = objectInfo.name;
+                if (objectName.startsWith("blank")) {
+                    objectName = getBlankRewrite(objectName);
+                }
+                           if (DEBUG)
+                               System.out.println("  adding statement " + predicateURI + " " + objectName);
+                               addStatement(statements, predicateURI, objectName);
                        }
                }
 
-               HashSet<ResourceInfo> debug = new HashSet<>();
-               info.ownedResourcesWithPredicates.forEachEntry(new TIntIntProcedure() {
-            
-            @Override
-            public boolean execute(int owner, int predicate) {
-                ResourceInfo ownerInfo = infos.get(owner);
-                debug.add(ownerInfo);
-//                ResourceInfo predicateInfo = infos.get(predicate);
-//                debug.add(predicateInfo);
-                return true;
-            }
-        });
-
+               if (DEBUG)
+                   System.out.println("statements size for " + info.name + " : " + statements.size() + " " + statements.keySet());
+               
                StringBuilder output = new StringBuilder();
                
                if(indent == 0 || inline) {
@@ -408,9 +522,11 @@ public class PrettyPrintTG {
                        } else if (info.aliasURI != null) {
                                output.append(info.name + " = <" + info.aliasURI + ">");
                        } else {
-                           if (inline)
-                               System.out.println("asdasd");
-                               output.append(inline ? "_" : info.name);
+                           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) {
@@ -438,10 +554,21 @@ public class PrettyPrintTG {
 
                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) {
@@ -455,13 +582,27 @@ public class PrettyPrintTG {
                        }
                }
 
+               TreeMap<String, Integer> ownedOrdered = new TreeMap<>();
+               
                for(int i=0;i<info.owned.size();i+=2) {
                        String predicateURI = rewritePredicateURI(graph, info.owned.get(i));
-                       ResourceInfo ownedInfo = infos.get(info.owned.get(i+1));
+                       ownedOrdered.put(predicateURI, info.owned.get(i+1));
+               }
+               
+               if (DEBUG)
+                   System.out.println(info.name + " : " + ownedOrdered.keySet());
+               
+               
+               for (Entry<String, Integer> entry : ownedOrdered.entrySet()) {
+                   String predicateURI = entry.getKey();
+                   int owned = entry.getValue();
+                       ResourceInfo ownedInfo = infos.get(owned);
+
                        String blank = printBlank(graph, predicateURI, ownedInfo, indent+1);
-                       if (blank != null)
+                       if (blank != null) {
                            output.append(blank);
-               }
+                       }
+               }               
                
                return output.toString();
        }
@@ -504,37 +645,32 @@ public class PrettyPrintTG {
 
        
        void prettyPrint(TransferableGraph1 graph) throws Exception {
-
-           if (LOGGER.isDebugEnabled())
-               LOGGER.debug("Starting prettyPrint for TransferableGraph with " + graph.resourceCount + " resources, " + graph.identities + " identities, " + graph.statements.length + " statements and " + graph.values.length + " values");
+           log("Starting prettyPrint for TransferableGraph with " + graph.resourceCount + " resources, " + graph.identities + " identities, " + graph.statements.length + " statements and " + graph.values.length + " values");
            
                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) {
-                       if (LOGGER.isDebugEnabled())
-                           LOGGER.debug("Resolving internal identity " + id);
+                                   log("Resolving internal identity " + id);
                                        String name = "BASE";
                                        ResourceInfo info = new ResourceInfo(true, name, id.resource, -1);
                                        info.aliasURI = TransferableGraphUtils.getURI(graph, id.resource);
                                        info.newResource = true;
-                                       infos.put(id.resource, info);
-                    if (LOGGER.isDebugEnabled())
-                        LOGGER.debug("    which parent is external " + parent + " and has an aliasURI " + info.aliasURI) ;
+                                       orderedInfos.put(name, info);
+//                                     infos.put(id.resource, info);
+                                       log("    which parent is external " + parent + " and has an aliasURI " + info.aliasURI);
                                        for(Identity child : TransferableGraphUtils.getChildren(graph, id)) {
                                                recurseURI(graph, child, name, info.resource);
                                        }
-                                       if (LOGGER.isDebugEnabled())
-                                           LOGGER.debug("    and has " + (infos.size() - 1) + " children");
+                                       log("    and has " + (infos.size() - 1) + " children");
                                }
                        } else if (id.definition instanceof External) {
                                External ext = (External)id.definition;
                                // Try to detect shared libraries
                                if(ext.name.contains("@")) {
                                    
-                                   if (LOGGER.isDebugEnabled())
-                                       LOGGER.debug("Detected an external shared library " + ext);
+                                   log("Detected an external shared library " + ext);
                                    
                                        int index = ext.name.indexOf('@');
                                        String prefix = ext.name.substring(0, index);
@@ -542,35 +678,36 @@ public class PrettyPrintTG {
                                        String ontology = index2 == -1 ? ext.name : ext.name.substring(0, index2);  
                                        String uri = TransferableGraphUtils.getURI(graph, id.resource);
                                        
-                                       if (LOGGER.isDebugEnabled())
-                                           LOGGER.debug("    which was resolved as URI=" + uri + " and prefix " + prefix);
+                                       log("    which was resolved as URI=" + uri + " and prefix " + prefix);
                                        
                                        ontologies.put(uri, prefix);
                                        
                                } else if (ext.name.contains("-")) {
-                                   if (LOGGER.isDebugEnabled())
-                                       LOGGER.debug("Resolving possible ontology " + ext);
+                                   log("Resolving possible ontology " + ext);
                                        String uri = TransferableGraphUtils.getURI(graph, id.resource);
                                Matcher m = versionExtractPattern.matcher(uri);
                                if (m.matches()) {
                                        if(!ontologies.containsKey(uri)) {
                                                        int index = ext.name.indexOf('-');
                             String prefix = ext.name.substring(0, index);
-                            if (LOGGER.isDebugEnabled())
-                                LOGGER.debug("    and it was resolved as URI=" + uri + " and prefix " + prefix);
+                            log("    and it was resolved as URI=" + uri + " and prefix " + prefix);
                             ontologies.put(uri, prefix);
                                        }
                                }
                                }
                        }
                }
+               
                // Discover other resources
-               if (LOGGER.isDebugEnabled())
-                   LOGGER.debug("Discovering other resources..");
+               log("Discovering other resources..");
                
                TIntArrayList todo = new TIntArrayList();
-               for(ResourceInfo info : infos.valueCollection())
+               for(ResourceInfo info : orderedInfos.values()) {
                        todo.add(info.resource);
+                       
+                    // put orderedInfos to infos
+                       infos.put(info.resource, info);
+               }
                
                while(!todo.isEmpty()) {
                        int resource = todo.removeAt(todo.size()-1);
@@ -615,23 +752,153 @@ public class PrettyPrintTG {
             }
         }
         
+        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) {
-                       String uri = printURI(graph, info, false, 0, false);
-                       if (uri != null)
-                           output.append(uri);
+                       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);
@@ -646,10 +913,64 @@ public class PrettyPrintTG {
                output.insert(0, refs.toString());
                
        }
+       
+       private String calculateHash(TransferableGraph1 graph, 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(graph, 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(TransferableGraph1 graph, 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(graph, 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) throws Exception {
+    public static String print(TransferableGraph1 tg, boolean ignoreIdentifiers) throws Exception {
                StringBuilder b = new StringBuilder();
-               new PrettyPrintTG(b).prettyPrint(tg);
+               new PrettyPrintTG(b, ignoreIdentifiers).prettyPrint(tg);
                return b.toString();
        }
 
index eeaab985c2d6d5816c725655addfe4b03d2eb257..39152636b0e80aab3d9b18eaa439ce26ca839cf1 100644 (file)
@@ -64,7 +64,7 @@ public class PGraphEditorDocumentProvider extends AbstractDocumentProvider {
                                if(indexRoot != null && graph.isInstanceOf(indexRoot, L0.Ontology)) {
                                        TransferableGraph1 tg = ModelingUtils.exportSharedOntology(graph, indexRoot, null, Constants.SHARED_LIBRARY_FORMAT, Constants.SHARED_LIBRARY_CURRENT_VERSION);
                                                GraphRefactoringUtils.fixOntologyExport(tg);
-                                   currentText = PrettyPrintTG.print(tg);
+                                   currentText = PrettyPrintTG.print(tg, false);
                                    errorHappened = false;
                                }
                             return new Document(currentText != null ? currentText : "");
index 19e83d30b571a7dfd9cf197090b1fecbe72939ea..df556086ed6ab61d0aa1458f5d6da06e24955172 100644 (file)
@@ -343,7 +343,7 @@ importJava "org.simantics.db.layer0.util.Layer0Utils" where
     listOntologies :: () -> <ReadGraph> [Resource]
     emptyTrashBin :: () -> <Proc> ()
     purgeDatabase :: () -> <Proc> ()
-    prettyPrintResource :: Resource -> <ReadGraph> String
+    prettyPrintResource :: Resource -> Boolean -> <ReadGraph> String
 
     @private
     @JavaName copyTo