]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.graph/src/org/simantics/graph/store/IdentityStore.java
(refs #7563) Mark parents of new resource optional in graph.tg
[simantics/platform.git] / bundles / org.simantics.graph / src / org / simantics / graph / store / IdentityStore.java
index 5ed32c0786bde4778a2125eb358be6a461020232..18bacade6d265b9d443dfb9a74747a35a82c98c2 100644 (file)
@@ -1,5 +1,17 @@
 package org.simantics.graph.store;
 
+import java.util.ArrayList;
+import java.util.regex.Pattern;
+
+import org.simantics.graph.query.Path;
+import org.simantics.graph.query.PathChild;
+import org.simantics.graph.query.PathRoot;
+import org.simantics.graph.representation.External;
+import org.simantics.graph.representation.Identity;
+import org.simantics.graph.representation.Internal;
+import org.simantics.graph.representation.Optional;
+import org.simantics.graph.representation.Root;
+
 import gnu.trove.list.array.TIntArrayList;
 import gnu.trove.map.hash.THashMap;
 import gnu.trove.map.hash.TIntIntHashMap;
@@ -12,17 +24,6 @@ import gnu.trove.procedure.TObjectProcedure;
 import gnu.trove.set.hash.THashSet;
 import gnu.trove.set.hash.TIntHashSet;
 
-import java.util.ArrayList;
-import java.util.regex.Pattern;
-
-import org.simantics.graph.query.Path;
-import org.simantics.graph.query.PathChild;
-import org.simantics.graph.query.PathRoot;
-import org.simantics.graph.representation.External;
-import org.simantics.graph.representation.Identity;
-import org.simantics.graph.representation.Internal;
-import org.simantics.graph.representation.Root;
-
 public class IdentityStore implements IStore {
        
        private static int[] EMPTY_INT_ARRAY = new int[0];
@@ -46,6 +47,7 @@ public class IdentityStore implements IStore {
                new TIntObjectHashMap<THashMap<String, ConsistsOf>>();
        TIntObjectHashMap<ConsistsOf> childMap = new TIntObjectHashMap<ConsistsOf>();
        TIntHashSet newResources = new TIntHashSet();
+       TIntHashSet optionalResources = new TIntHashSet();
        int resourceCount = 0;
        TIntIntHashMap unifications = new TIntIntHashMap();
        TIntHashSet collisions = new TIntHashSet();
@@ -57,7 +59,11 @@ public class IdentityStore implements IStore {
        public boolean markNew(int resource) {
                return newResources.add(resource);
        }
-       
+
+       public boolean markOptional(int resource) {
+               return optionalResources.add(resource);
+       }
+
        public int[] getNewResources() {
                return newResources.toArray();
        }
@@ -164,10 +170,13 @@ public class IdentityStore implements IStore {
                                
                                @Override
                                public boolean execute(String a, ConsistsOf b) {
-                                       if(newResources.contains(b.child))
-                                               identities.add(new Identity(b.child, new Internal(parent, a)));
+                                       int child = b.child;
+                                       if(newResources.contains(child))
+                                               identities.add(new Identity(child, new Internal(parent, a)));
+                                       else if(optionalResources.contains(child))
+                                           identities.add(new Identity(child, new Optional(parent, a)));
                                        else
-                                               identities.add(new Identity(b.child, new External(parent, a)));
+                                               identities.add(new Identity(child, new External(parent, a)));
                                        collectIdentities(b.child, identities);
                                        return true;
                                }
@@ -280,6 +289,14 @@ public class IdentityStore implements IStore {
                return newResources.contains(id);
        }
 
+       public boolean isOptionalResource(int id) {
+               return optionalResources.contains(id);
+       }
+
+       public boolean isRoot(int id) {
+               return invRoots.contains(id);
+       }
+
        public Identity[] toArray() {
                final ArrayList<Identity> identities = new ArrayList<Identity>();
                collectIdentities(identities);
@@ -313,6 +330,13 @@ public class IdentityStore implements IStore {
                return childMap.containsKey(id);
        }
 
+       public int getParent(int id) {
+               ConsistsOf consistsOf = childMap.get(id);
+               if(consistsOf == null)
+                       return -1;
+               return consistsOf.parent;
+       }
+
        public void definePath(Path path, int resource) {
                if(path instanceof PathChild) {
                        PathChild child = (PathChild)path;
@@ -426,5 +450,9 @@ public class IdentityStore implements IStore {
         map.put(name, consistsOf);
         childMap.put(child, consistsOf);
     }
-       
+
+    public void removeOptionalMark(int id) {
+        optionalResources.remove(id);
+    }
+
 }