]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Better and prettier printing 40/540/1
authorjsimomaa <jani.simomaa@gmail.com>
Mon, 22 May 2017 09:05:09 +0000 (12:05 +0300)
committerjsimomaa <jani.simomaa@gmail.com>
Mon, 22 May 2017 09:05:09 +0000 (12:05 +0300)
refs 7224

Change-Id: I6afdcfe04f4cef811c514b1c2f42478ebee30646

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

index bf0224103228a0838952cf553b9fea9b54d14ed6..ea449417d305ee7eac034af76e3ceadec07e7b79 100644 (file)
@@ -7,11 +7,13 @@ import java.io.InputStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
 
 import org.simantics.databoard.binding.Binding;
 import org.simantics.databoard.binding.mutable.Variant;
 import org.simantics.databoard.container.DataContainer;
 import org.simantics.databoard.container.DataContainers;
+import org.simantics.graph.representation.PrettyPrintTG;
 import org.simantics.graph.representation.TransferableGraph1;
 
 /**
@@ -20,7 +22,7 @@ import org.simantics.graph.representation.TransferableGraph1;
  */
 public class FixExportedOntology {
        
-       static void convertExportedSharedOntologyIntoBundleOntology(Path input, Path output) throws Exception {
+       static TransferableGraph1 convertExportedSharedOntologyIntoBundleOntology(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);
@@ -33,16 +35,22 @@ public class FixExportedOntology {
                        DataContainers.writeFile(output.toFile(), new DataContainer(
                                        container.format, container.version,
                                        container.metadata, new Variant(TransferableGraph1.BINDING, graph)));
+                       
+                       return graph;
+                       
                }
        }
 
        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) {
+               if (args.length == 0) {
+                       System.out.println("Required arguments: <input .sharedLibrary file> [<output .tg file>]");
+               } else if (args.length == 1) {
                        Path input = Paths.get(args[0]);
-                       Path output = input.getParent().resolve(input.getName(input.getNameCount()-1) + ".fixed");
-                       convertExportedSharedOntologyIntoBundleOntology(input, output);
+                       Path output1 = input.getParent().resolve("graph.tg");
+                       TransferableGraph1 tg = convertExportedSharedOntologyIntoBundleOntology(input, output1);
+                       String listing = PrettyPrintTG.print(tg);
+                       Path output2 = Paths.get(args[0].substring(0, args[0].length() - ".sharedLibrary".length()) + ".pgraph");
+                       Files.write(output2, listing.getBytes(),StandardOpenOption.CREATE);
                } else {
                        convertExportedSharedOntologyIntoBundleOntology(Paths.get(args[0]), Paths.get(args[1]));
                }
index 3243b82bd1c3aeb6be8e424ac7b907845ddaa240..00d17100ec461bb5800a63e93e1b40175d90a204 100644 (file)
@@ -4,15 +4,22 @@ import java.io.BufferedInputStream;
 import java.io.DataInput;
 import java.io.DataInputStream;
 import java.io.InputStream;
+import java.math.BigInteger;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.util.ArrayList;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.TreeMap;
+import java.util.TreeSet;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.Datatypes;
 import org.simantics.databoard.binding.Binding;
 import org.simantics.databoard.binding.mutable.Variant;
 import org.simantics.databoard.container.DataContainers;
@@ -29,41 +36,49 @@ import gnu.trove.set.hash.TLongHashSet;
 public class PrettyPrintTG extends TransferableGraphUtils {
 
        int blankCounter = 0;
-       
+       MessageDigest m;
+
+       private final Pattern versionExtractPattern = Pattern.compile("^.*-(\\d+\\.\\d+)");
+
        final StringBuilder output;
-       
+       final Map<String,String> ontologies = new HashMap<>(knownOntologies);
+       final Set<String> referencedOntologies = new TreeSet<>();
+
        static class ResourceInfo {
                final boolean hasURI;
                final String name;
                final int resource;
                boolean newResource = false;
-               int owner = 0;
+               
+               // -1 = no owner, -2 = multiple owners
+               int owner = -1;
                int ownerPredicate = 0;
                String aliasURI = null;
                TIntArrayList owned = new TIntArrayList();
-               TIntArrayList statements = new TIntArrayList();
+               //TIntObjectHashMap<TIntHashSet> statements = new TIntObjectHashMap<TIntHashSet>();
                public ResourceInfo(boolean hasURI, String name, int resource) {
                        this.hasURI = hasURI;
                        this.name = name;
                        this.resource = resource;
                }
        }
-       
-       public PrettyPrintTG(StringBuilder b) {
+
+       public PrettyPrintTG(StringBuilder b) throws NoSuchAlgorithmException {
                output = b;
+               m = MessageDigest.getInstance("SHA-256");
        }
-       
-       public PrettyPrintTG() {
+
+       public PrettyPrintTG() throws NoSuchAlgorithmException {
                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 + "." + tgNodeName(getName(parent));
                ResourceInfo info = new ResourceInfo(true, name, parent.resource); 
@@ -73,8 +88,8 @@ public class PrettyPrintTG extends TransferableGraphUtils {
                }
                return info;
        }
-       
-       void discoverBlank(TransferableGraph1 graph, int resource, TIntArrayList todo) {
+
+       void discoverBlank(TransferableGraph1 graph, int resource, TIntArrayList todo) throws Exception {
                TIntArrayList statements = getStatements(graph, resource);
                for(int i=0;i<statements.size();i+=2) {
                        int object = statements.get(i+1);
@@ -87,7 +102,6 @@ public class PrettyPrintTG extends TransferableGraphUtils {
                                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);
@@ -96,7 +110,7 @@ public class PrettyPrintTG extends TransferableGraphUtils {
                        }
                }
        }
-       
+
        void discoverOwners(TransferableGraph1 graph, ResourceInfo info) {
                int resource = info.resource;
                TIntArrayList statements = getStatements(graph, resource);
@@ -105,51 +119,73 @@ public class PrettyPrintTG extends TransferableGraphUtils {
                        int object = statements.get(i+1);
                        ResourceInfo existing = infos.get(object);
                        if(existing == null) continue;
-                       if(existing.owner == 0) {
+                       if(existing.owner == -1) {
                                existing.owner = resource;
                                existing.ownerPredicate = predicate;
                                //System.err.println("First owner " + info.name + " => " + predicateURI + " " + existing.name);
                        } else {
-                               existing.owner = -1;
+                               existing.owner = -2;
                                //System.err.println("Multiple owners " + info.name + " => " + predicateURI + " " + existing.name);
                        }
                }
-               info.statements = statements;
+               
        }
-       
-       String printValue(Value value) {
+
+       String printValue(Value value) throws Exception {
                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;
-               if(id.definition instanceof Internal) {
-                       Identity instanceOf = findExternal(graph, "http://www.simantics.org/Layer0-1.1/InstanceOf");
-                       Identity library = findExternal(graph, "http://www.simantics.org/Layer0-1.1/Library");
-                       info.statements.add(instanceOf.resource);
-                       info.statements.add(library.resource);
+               if(Datatypes.STRING.equals(dt)) {
+                       String s = (String)variant.getValue(Bindings.STRING);
+                       if(s.contains("\n")) {
+                               return "\"\"\"" + s + "\"\"\"";
+                       } else {
+                               return "\"" + s + "\"";         
+                       }
+               } else if(Datatypes.BOOLEAN.equals(dt)) {
+                       Boolean i = (Boolean)variant.getValue(Bindings.BOOLEAN);
+                       return i ? "true" : "false";
+               } else if(Datatypes.INTEGER.equals(dt)) {
+                       Integer i = (Integer)variant.getValue(Bindings.INTEGER);
+                       return i.toString();
+               } else if(Datatypes.LONG.equals(dt)) {
+                       Long i = (Long)variant.getValue(Bindings.LONG);
+                       return i.toString();
+               } else {
+                       byte[] data = variant.getBinding().serializer().serialize(variant.getValue());
+                       m.reset();
+                       m.update(data, 0, data.length);
+                       return "\"" + new BigInteger(1, m.digest()).toString(16) + "\"";
                }
+
        }
-       
+
+       //      void fixInstanceOf(TransferableGraph1 graph, ResourceInfo info) {
+       //              Identity id = getIdentity(graph, info.resource);
+       //              if(id == null) return;
+       //              if(id.definition instanceof Internal) {
+       //                      Identity instanceOf = findExternal(graph, "http://www.simantics.org/Layer0-1.1/InstanceOf");
+       //                      Identity library = findExternal(graph, "http://www.simantics.org/Layer0-1.1/Library");
+       //                      info.statements.add(instanceOf.resource);
+       //                      info.statements.add(library.resource);
+       //              }
+       //      }
+
        public static String getExternalURI(TransferableGraph1 tg, External ext) {
                String name = ext.name;
                if(name.contains(" ")) name = name.replace(" ", "_").replaceAll("@", "_");//name = "\"" + name + "\"";
                int parentId = ext.parent;
                //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 if(id.definition instanceof Root) {
-                               Root root = (Root)id.definition;
-                               return "http:/" + root.name + "/" + name; 
-                       } else {
-                               return null;
-                       }
-//             }
+               //              else {
+               Identity id = getIdentity(tg, parentId);
+               if(id.definition instanceof External) {
+                       return getExternalURI(tg, (External)id.definition) + "/" + name;
+               } else if(id.definition instanceof Root) {
+                       Root root = (Root)id.definition;
+                       return "http:/" + root.name + "/" + name; 
+               } else {
+                       return null;
+               }
+               //              }
        }
 
        public static String getExternalURI(TransferableGraph1 tg, int resource) {
@@ -161,148 +197,143 @@ public class PrettyPrintTG extends TransferableGraphUtils {
                }
                return null;
        }
-       
+
        String rewritePredicateURI(TransferableGraph1 graph, int predicate) {
+               
                String uri = getExternalURI(graph, predicate);
-               if(uri == null) return null;
-               uri = uri.replace("http://www.simantics.org/Modeling-1.2", "MOD");
-               uri = uri.replace("http://www.simantics.org/Layer0-1.1", "L0");
-               uri = uri.replace("http://www.simantics.org/Layer0X-1.1", "L0X");
-               uri = uri.replace("http://www.simantics.org/Diagram-2.2", "DIA");
-               uri = uri.replace("http://www.simantics.org/Structural-1.2", "STR");
-               uri = uri.replace("http://www.simantics.org/Documentation-1.2", "DOCU");
-               uri = uri.replace("http://www.simantics.org/Document-1.2", "DOC");
-               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");
-               uri = uri.replace("http://www.apros.fi/OperationUI-6.6", "APROS_OPER");
-               uri = uri.replace("http://semantum.fi/SimupediaStandardLibrary@1.3-trunk", "SIMUPEDIA_STD");
+               if(uri == null) {
+                       ResourceInfo info = infos.get(predicate);
+                       if(info != null) return info.name;
+                       return "_";
+               }
+               
+               for(String ontology : ontologies.keySet()) {
+                       if(uri.contains(ontology)) {
+                               String key = ontologies.get(ontology);
+                               uri = uri.replace(ontology, key);
+                               referencedOntologies.add(ontology);
+                       }
+               }
+               
                uri = uri.replace("/", ".");
+               
                return uri;
+               
+       }
+
+       void indent(int indent) {
+               for(int i=0;i<indent;i++) output.append("  ");
        }
        
-       void printBlank(TransferableGraph1 graph, String predicateURI2, ResourceInfo info) {
-               
+       void printBlank(TransferableGraph1 graph, String predicateURI2, ResourceInfo info, int indent) {
+
                if(info.hasURI) return;
-               output.append("  " + predicateURI2 + " " + info.name + "\n");
-               
-               Value value = findValue(graph, info.resource);
-               if(value != null) {
-                       
+               indent(indent);
+               output.append(predicateURI2 + " " + info.name + "\n");
+
+               if(info.owner < 0) {
+                       printURI(graph, info, false, indent);
                }
                
-//             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));
-//                     if(ownedInfo == null) {
-//                             System.err.println("null owned");
-//                             continue;
-//                     }
-//                     printBlank(graph, predicateURI, ownedInfo);
-//             }
-               
        }
-       
+
        long longStm(int predicate, int object) {
-        return (predicate<<32) | (object & 0xffffffffL); 
+               return (predicate<<32) | (object & 0xffffffffL); 
        }
 
-       void addStatement(Map<String,List<String>> statements, String predicate, String object) {
-               List<String> objects = statements.get(predicate);
+       void addStatement(Map<String,Set<String>> statements, String predicate, String object) {
+               Set<String> objects = statements.get(predicate);
                if(objects == null) {
-                       objects = new ArrayList<String>();
+                       objects = new TreeSet<>();
                        statements.put(predicate, objects);
                }
                objects.add(object);
        }
-       
-       void printURI(TransferableGraph1 graph, ResourceInfo info) {
-               
-               if(!info.hasURI) return;
-               
-               Map<String,List<String>> statements = new HashMap<>();
+
+       void printURI(TransferableGraph1 graph, ResourceInfo info, boolean requireURI, int indent) {
+
+               if(requireURI && !info.hasURI) return;
+
+               Map<String,Set<String>> statements = new TreeMap<>();
                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));
-                       ResourceInfo ownedInfo = infos.get(info.owned.get(i+1));
-                       if(ownedInfo == null) {
-                               System.err.println("null owned");
-                               continue;
-                       }
                        long stmId = longStm(info.owned.get(i), info.owned.get(i+1));
                        processed.add(stmId);
-                       printBlank(graph, predicateURI, ownedInfo);
                }
-               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;
-                       if(consistsOf.resource == info.statements.get(i)) continue;
-                       String predicateURI = rewritePredicateURI(graph, info.statements.get(i));
-                       ResourceInfo objectInfo = infos.get(info.statements.get(i+1));
+               
+               TIntArrayList rawStatements = getStatements(graph, info.resource);
+               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));
                        if(objectInfo == null) {
-                               String objectURI = rewritePredicateURI(graph, info.statements.get(i+1));
-                               //output.append("  " + predicateURI + " " + objectURI + "\n");
+                               String objectURI = rewritePredicateURI(graph, rawStatements.get(i+1));
                                addStatement(statements, predicateURI, objectURI);
                        } else {
-                               //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);          
+               if(indent == 0) {
+                       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> inherits = statements.get("L0.Inherits");
-               if(inherits != null) {
-                       for(String inherit : inherits) {
-                               output.append(" <T " + inherit);                
+                       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");
                }
 
-               output.append("\n");
-               
                if(info.newResource)
                        output.append("  @L0.new\n");
-               
-               for(Map.Entry<String, List<String>> entry : statements.entrySet()) {
+
+               for(Map.Entry<String, Set<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("L0.PartOf".equals(predicate)) continue;
+                       Set<String> objects = entry.getValue();
                        if(objects.size() == 1) {
-                               output.append("  " + predicate + " " + objects.iterator().next() + "\n");       
+                               indent(indent+1);
+                               output.append(predicate + " " + objects.iterator().next() + "\n");      
                        } else{
-                               output.append("  " + predicate + "\n"); 
+                               indent(indent+1);
+                               output.append(predicate + "\n");        
                                for(String object : objects) {
-                                       output.append("    " + object + "\n");  
+                                       indent(indent+1);
+                                       output.append("  " + object + "\n");    
                                }
                        }
                }
+
+               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));
+                       printBlank(graph, predicateURI, ownedInfo, indent+1);
+               }
                
        }
 
@@ -310,7 +341,6 @@ public class PrettyPrintTG extends TransferableGraphUtils {
 
                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); 
@@ -318,89 +348,118 @@ public class PrettyPrintTG extends TransferableGraphUtils {
                        TransferableGraph1 graph = (TransferableGraph1)container.content.getValue(binding);
                        prettyPrint(graph);
                        Files.write(output, this.output.toString().getBytes());
-                       
+
                }
 
        }
+       
+       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(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 : 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);
-                                               }
+       void prettyPrint(TransferableGraph1 graph) throws Exception {
+
+               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);
                                        }
                                }
+                       } else if (id.definition instanceof External) {
+                               External ext = (External)id.definition;
+                               // Try to detect shared libraries
+                               if(ext.name.contains("@")) {
+                                       
+                                       int index = ext.name.indexOf('@');
+                                       String prefix = ext.name.substring(0, index);
+                                       int index2 = ext.name.indexOf('/', index);
+                                       String ontology = index2 == -1 ? ext.name : ext.name.substring(0, index2);  
+                                       String uri = TransferableGraphUtils.getURI(graph, id.resource);
+                                       ontologies.put(uri, prefix);
+                                       
+                               } else if (ext.name.contains("-")) {
+
+                                       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);
+                                                       ontologies.put(uri, prefix);
+                                       }
+                               }
+
+                               }
+                               
+
+                               
                        }
-                       // Discover other resources
-                       TIntArrayList todo = new TIntArrayList();
-                       for(ResourceInfo info : infos.valueCollection())
-                               todo.add(info.resource);
-                       while(!todo.isEmpty()) {
-                               int resource = todo.removeAt(todo.size()-1);
-                               discoverBlank(graph, resource, todo);
+               }
+               // Discover other resources
+               TIntArrayList todo = new TIntArrayList();
+               for(ResourceInfo info : infos.valueCollection())
+                       todo.add(info.resource);
+               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())
+                       if(info.owner >= 0) {
+                               ResourceInfo ownerInfo = infos.get(info.owner);
+                               ownerInfo.owned.add(info.ownerPredicate);
+                               ownerInfo.owned.add(info.resource);
                        }
-                       for(ResourceInfo info : infos.valueCollection())
-                               discoverOwners(graph, info);
-                       for(ResourceInfo info : infos.valueCollection())
-                               fixInstanceOf(graph, info);
-                       for(ResourceInfo info : infos.valueCollection())
-                               if(info.owner > 0) {
-                                       ResourceInfo ownerInfo = infos.get(info.owner);
-                                       ownerInfo.owned.add(info.ownerPredicate);
-                                       ownerInfo.owned.add(info.resource);
-                               } else if (info.owner == 0) {
-                                       //System.err.println("faf1");
-                               } else if (info.owner == -1) {
-                                       //System.err.println("faf2");
-                               }
-                       
-                       TreeMap<String,ResourceInfo> order = new TreeMap<>();
-                       for(ResourceInfo info : infos.valueCollection())
-                               order.put(info.name, info);
-                       
-                       this.output.append("MOD = <http://www.simantics.org/Modeling-1.2>\n");
-                       this.output.append("L0 = <http://www.simantics.org/Layer0-1.1>\n");
-                       this.output.append("L0X = <http://www.simantics.org/Layer0X-1.1>\n");
-                       this.output.append("DIA = <http://www.simantics.org/Diagram-2.2>\n");
-                       this.output.append("STR = <http://www.simantics.org/Structural-1.2>\n");
-                       this.output.append("DOCU = <http://www.simantics.org/Documentation-1.2>\n");
-                       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 = <http://www.semantum.fi/Simupedia-1.0>\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");
-                       
-                       for(ResourceInfo info : order.values())
-                               printURI(graph, info);
-                       
+
+               TreeMap<String,ResourceInfo> order = new TreeMap<>();
+               for(ResourceInfo info : infos.valueCollection())
+                       order.put(info.name, info);
+
+               for(ResourceInfo info : order.values())
+                       printURI(graph, info, true, 0);
+
+               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");   
+               }
+               output.insert(0, refs.toString());
+               
        }
-       
-       public static String print(TransferableGraph1 tg) {
+
+       public static String print(TransferableGraph1 tg) throws Exception {
                StringBuilder b = new StringBuilder();
                new PrettyPrintTG(b).prettyPrint(tg);
                return b.toString();
index ad247b6bd8cf70e0bf8dd993ed2b96ff9ea0ae4a..eeaab985c2d6d5816c725655addfe4b03d2eb257 100644 (file)
@@ -60,14 +60,17 @@ public class PGraphEditorDocumentProvider extends AbstractDocumentProvider {
                         return new Document(currentText != null ? currentText : "");
                     } else {
                        Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(resource));
-                       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);
-                            errorHappened = false;
+                       try {
+                               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);
+                                   errorHappened = false;
+                               }
                             return new Document(currentText != null ? currentText : "");
+                       } catch (Exception e) {
+                               throw new DatabaseException("Could not get PGraph from " + resource);
                        }
-                       throw new DatabaseException("Could not get PGraph from " + resource);
                     }
                 }
             });