]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
(refs #7563) Mark parents of new resource optional in graph.tg 28/1128/2 feature/tgOptionalResource
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Thu, 19 Oct 2017 13:03:08 +0000 (16:03 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Thu, 19 Oct 2017 17:53:07 +0000 (20:53 +0300)
Change-Id: I2c1d2bbf35fbac865323c6fdb6f9974cf4e05d8b

bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/PropagateNewMarks.java
bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/validation/ValidateGraph.java
bundles/org.simantics.graph.db/src/org/simantics/graph/db/TGToGraphMap.java
bundles/org.simantics.graph.db/src/org/simantics/graph/db/TransferableGraphImportProcess.java
bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphQueries.java
bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphUtils.java
bundles/org.simantics.graph/src/org/simantics/graph/store/IdentityStore.java

index 58eae257f3dfc094fe41dd27b291d0589177890f..c9235361bae0bdb7000d2eb8a66e28d490daee26 100644 (file)
@@ -15,10 +15,23 @@ public class PropagateNewMarks implements Runnable {
                        if(identities.markNew(child))
                                propagateNewMarks(child);
        }
-       
+
+       /**
+        * Mark all parents of resources marked new as optional provisionally. This mark is removed
+        * in ValidateGraph.run, if the resource can be found in dependencies.
+        */
+       private void propagateOptionalMarks(int child) {
+               int parent = identities.getParent(child);
+               if(parent >= 0 && !identities.isRoot(parent) && !identities.isNewResource(parent) && identities.markOptional(parent))
+                       propagateOptionalMarks(parent);
+       }
+
        @Override
        public void run() {
-               for(int resource : identities.getNewResources())
-                       propagateNewMarks(resource);    
+               int[] originalNewResources = identities.getNewResources();
+               for(int resource : originalNewResources)
+                       propagateNewMarks(resource);
+               for(int resource : originalNewResources)
+                       propagateOptionalMarks(resource);
        }
 }
index 13d84bfc21ef8b7f7eb6bda12c60d7a8fd934465..b86991fb6ff59e654adbafacde7bdedae9b4e821 100644 (file)
@@ -94,6 +94,10 @@ public class ValidateGraph implements Runnable {
                                                                        continue;
                                                                }
                                                        }
+                                                       else if(store.identities.isOptionalResource(id)) {
+                                                               if(graph.countOccurences(res) > 1)
+                                                                       store.identities.removeOptionalMark(id);
+                                                       }
                                                        else {
                                                                if(graph.countOccurences(res) <= 1) {
                                                                        emit(id, ValidationMode.ERROR, "Resource " + res + " is not defined in dependencies and it is not marked new in this graph.");
index bc1c4eb5223ae12cda19f542b5536384f5cd8ae5..ca79b1ee5aabf35771ca1ea44a982d44fd848044 100644 (file)
@@ -16,6 +16,7 @@ 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.Value;
@@ -123,6 +124,17 @@ public class TGToGraphMap {
                        else if(definition instanceof Root) {
                                resources[identity.resource] = RootLibrary;             
                        }
+                       else if(definition instanceof Optional) {
+                               Optional def = (Optional)definition;
+                               Resource parent = resources[def.parent];
+                               if(parent == null)
+                                       continue;
+                               Resource child = graph.syncRequest(
+                                               new UnescapedChildMapOfResource(parent),
+                                               new TransientCacheAsyncListener<Map<String, Resource>>())
+                                               .get(def.name);
+                               resources[identity.resource] = child; // might be null
+                       }
                }
        }
        
index 586562fa34df83ef4bccdffaebe9e5a373bd90f6..4e6aaad08a578f3fa79f290d7f1c8ff3f45280a9 100644 (file)
@@ -226,7 +226,7 @@ public class TransferableGraphImportProcess implements TransferableGraphImporter
                                }
                        }
                        else if(definition instanceof Optional) {
-                               External def = (External)definition;
+                               Optional def = (Optional)definition;
                                Resource parent = resources[def.parent];
                                if(parent != null)
                                        resources[identity.resource] = 
index 5d4c1850b38040db18ddf527271a496a99388720..59d4fc84b6825c6ab9520892eca53351adb2388f 100644 (file)
@@ -66,6 +66,9 @@ public class TransferableGraphQueries {
         } else if (definition instanceof Internal) {
             Internal def = (Internal)definition;
             return getURI(def.parent) + "/" + def.name;
+        } else if (definition instanceof Optional) {
+            Optional def = (Optional)definition;
+            return getURI(def.parent) + "/" + def.name;
         } else {
             return "";
         }
index 10637ad65caf0d861d52e3d1adfffdee66d5f2ca..8ee29cc953a3eacc745edb77ad1854ce6ac3b56c 100644 (file)
@@ -57,6 +57,10 @@ public class TransferableGraphUtils {
                 External ext = (External)id.definition;
                 if(ext.name.equals(name) && ext.parent == parent) return id;
             }
+            else if(id.definition instanceof Optional) {
+                Optional ext = (Optional)id.definition;
+                if(ext.name.equals(name) && ext.parent == parent) return id;
+            }
         }
         return null;
         
@@ -309,6 +313,9 @@ public class TransferableGraphUtils {
                 } else if (definition instanceof Internal) {
                     Internal def = (Internal)definition;
                     return getURI(identities, def.parent) + "/" + def.name;
+                } else if (definition instanceof Optional) {
+                    Optional def = (Optional)definition;
+                    return getURI(identities, def.parent) + "/" + def.name;
                 } else {
                     return "";
                 }
@@ -344,6 +351,9 @@ public class TransferableGraphUtils {
             } else if (definition instanceof Internal) {
                 Internal def = (Internal)definition;
                 return getURI(resourceCount, identities, def.parent) + "/" + URIStringUtils.escape(def.name);
+            } else if (definition instanceof Optional) {
+                Optional def = (Optional)definition;
+                return getURI(resourceCount, identities, def.parent) + "/" + URIStringUtils.escape(def.name);
             } else {
                 return "";
             }
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);
+    }
+
 }