]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.graph/src/org/simantics/graph/refactoring/GraphRefactoringUtils.java
Make prettyPrintTG available via SCL from Simantics/DB-module
[simantics/platform.git] / bundles / org.simantics.graph / src / org / simantics / graph / refactoring / GraphRefactoringUtils.java
index 9882048b788144c74de9ac5ec56b69f6954ff384..90b134e33762b5f991baa5288a2974906f2bad7b 100644 (file)
@@ -1,6 +1,5 @@
 package org.simantics.graph.refactoring;
 
-import java.util.ArrayList;
 import java.util.Arrays;
 
 import org.simantics.databoard.util.URIStringUtils;
@@ -11,23 +10,19 @@ import org.simantics.graph.query.UriUtils;
 import org.simantics.graph.refactoring.MappingSpecification.MappingRule;
 import org.simantics.graph.representation.External;
 import org.simantics.graph.representation.Identity;
-import org.simantics.graph.representation.IdentityDefinition;
 import org.simantics.graph.representation.Internal;
-import org.simantics.graph.representation.Optional;
 import org.simantics.graph.representation.Root;
 import org.simantics.graph.representation.TransferableGraph1;
 import org.simantics.graph.representation.TransferableGraphUtils;
-import org.simantics.graph.representation.old.OldTransferableGraph1;
-import org.simantics.graph.representation.old.OldValue1;
 import org.simantics.graph.store.GraphStore;
 import org.simantics.graph.store.IdentityStore;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-import gnu.trove.list.array.TIntArrayList;
-import gnu.trove.map.hash.TIntIntHashMap;
 import gnu.trove.set.hash.TIntHashSet;
 
 public class GraphRefactoringUtils {
-
+    private static final Logger LOGGER = LoggerFactory.getLogger(GraphRefactoringUtils.class);
     /**
      * Moves an external resource. Returns true if did something.
      * @param parentsAffected 
@@ -48,7 +43,7 @@ public class GraphRefactoringUtils {
         // Find parent id
         int toParentId = ids.createPathToId(to.parent);
         if(ids.hasChild(toParentId, to.name)) {
-               System.err.println("refactor statements from " + from + " to " + to);
+            LOGGER.info("refactor statements from " + from + " to " + to);
             //throw new GraphRefactoringException("External reference to " + to + " already exists.");
                int toId = ids.pathToId(to);
             int[] statements = tg.statements;
@@ -68,7 +63,7 @@ public class GraphRefactoringUtils {
             if(!(rule.to instanceof PathChild))
                 throw new GraphRefactoringException("Invalid target URI " + rule.to);
             if(!moveExternal(tg, ids, rule.from, (PathChild)rule.to, parentsAffected))
-                System.err.println("Didn't find " + rule.from);
+                LOGGER.warn("Didn't find " + rule.from);
         }
     }
 
@@ -133,6 +128,22 @@ public class GraphRefactoringUtils {
                                
                        return;
                        
+                } else if (ext.type.startsWith("http://")) {
+                    String first = "http://Projects/Development Project";
+                    Identity path = recursePath(tg, first);
+                    id.definition = new Internal(path.resource, ext.name);
+                    
+                    GraphStore store = TransferableGraphConversion.convert(tg);
+                    int rootId = store.identities.createPathToId(UriUtils.uriToPath(first + "/" + ext.name));
+                    propagateNewMarks(store.identities, rootId);
+
+                    TransferableGraph1 tgNew = TransferableGraphConversion.convert(store);
+                        
+                    tg.resourceCount = tgNew.resourceCount;
+                    tg.identities = tgNew.identities;
+                    tg.values = tgNew.values;
+                    tg.statements = tgNew.statements;
+
                 }
             }
         }
@@ -159,69 +170,4 @@ public class GraphRefactoringUtils {
         }
     }
 
-    public static void compactify(OldTransferableGraph1 tg,
-            TIntHashSet removed) {
-        // Filter removed set
-        for(Identity id : tg.identities) {
-            IdentityDefinition def = id.definition;
-            if(def instanceof Root)
-                removed.remove(id.resource);
-            else if(def instanceof External)
-                removed.remove(((External)def).parent);
-            else if(def instanceof Internal)
-                removed.remove(((Internal)def).parent);
-            else if(def instanceof Optional)
-                removed.remove(((Optional)def).parent);
-        }
-        for(int r : tg.statements)
-            removed.remove(r);
-        for(OldValue1 value : tg.values)
-            removed.remove(value.resource);
-        
-        // Compactify
-        if(!removed.isEmpty()) {
-            // create map
-            int resourceCount = tg.resourceCount;
-            int[] map = new int[resourceCount];
-            for(int i=0;i<resourceCount;++i)
-                map[i] = i;
-            for(int r : removed.toArray()) {
-                map[--resourceCount] = map[r];
-            }
-            
-            // map
-            ArrayList<Identity> newIdentities = new ArrayList<Identity>(tg.identities.length);
-            for(Identity id : tg.identities) {
-                if(removed.contains(id.resource))
-                    continue;
-                else
-                    newIdentities.add(id);                
-                id.resource = map[id.resource];
-                IdentityDefinition def = id.definition;
-                if(def instanceof External) {
-                    External d = (External)def;
-                    d.parent = map[d.parent];
-                }
-                else if(def instanceof Internal) {
-                    External d = (External)def;
-                    d.parent = map[d.parent];
-                }
-                else if(def instanceof Optional) {
-                    External d = (External)def;
-                    d.parent = map[d.parent];
-                }
-            }
-            tg.identities = newIdentities.toArray(new Identity[newIdentities.size()]);
-            int[] statements = tg.statements;
-            for(int i=0;i<statements.length;++i) {
-                int r = statements[i];                        
-                if(r >= 0)
-                    statements[i] = map[r];
-            }
-            for(OldValue1 value : tg.values)
-                value.resource = map[value.resource];
-            tg.resourceCount = resourceCount;
-        }
-    }
-
 }