From: Hannu Niemistö Date: Thu, 19 Oct 2017 13:03:08 +0000 (+0300) Subject: (refs #7563) Mark parents of new resource optional in graph.tg X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=687435a49e86496511f8e92ff743211cb191838a;p=simantics%2Fplatform.git (refs #7563) Mark parents of new resource optional in graph.tg Change-Id: I2c1d2bbf35fbac865323c6fdb6f9974cf4e05d8b --- diff --git a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/PropagateNewMarks.java b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/PropagateNewMarks.java index 58eae257f..c9235361b 100644 --- a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/PropagateNewMarks.java +++ b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/procedures/PropagateNewMarks.java @@ -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); } } diff --git a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/validation/ValidateGraph.java b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/validation/ValidateGraph.java index 13d84bfc2..b86991fb6 100644 --- a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/validation/ValidateGraph.java +++ b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/validation/ValidateGraph.java @@ -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."); diff --git a/bundles/org.simantics.graph.db/src/org/simantics/graph/db/TGToGraphMap.java b/bundles/org.simantics.graph.db/src/org/simantics/graph/db/TGToGraphMap.java index bc1c4eb52..ca79b1ee5 100644 --- a/bundles/org.simantics.graph.db/src/org/simantics/graph/db/TGToGraphMap.java +++ b/bundles/org.simantics.graph.db/src/org/simantics/graph/db/TGToGraphMap.java @@ -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>()) + .get(def.name); + resources[identity.resource] = child; // might be null + } } } diff --git a/bundles/org.simantics.graph.db/src/org/simantics/graph/db/TransferableGraphImportProcess.java b/bundles/org.simantics.graph.db/src/org/simantics/graph/db/TransferableGraphImportProcess.java index 586562fa3..4e6aaad08 100644 --- a/bundles/org.simantics.graph.db/src/org/simantics/graph/db/TransferableGraphImportProcess.java +++ b/bundles/org.simantics.graph.db/src/org/simantics/graph/db/TransferableGraphImportProcess.java @@ -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] = diff --git a/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphQueries.java b/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphQueries.java index 5d4c1850b..59d4fc84b 100644 --- a/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphQueries.java +++ b/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphQueries.java @@ -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 ""; } diff --git a/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphUtils.java b/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphUtils.java index 10637ad65..8ee29cc95 100644 --- a/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphUtils.java +++ b/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphUtils.java @@ -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 ""; } diff --git a/bundles/org.simantics.graph/src/org/simantics/graph/store/IdentityStore.java b/bundles/org.simantics.graph/src/org/simantics/graph/store/IdentityStore.java index 5ed32c078..18bacade6 100644 --- a/bundles/org.simantics.graph/src/org/simantics/graph/store/IdentityStore.java +++ b/bundles/org.simantics.graph/src/org/simantics/graph/store/IdentityStore.java @@ -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>(); TIntObjectHashMap childMap = new TIntObjectHashMap(); 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 identities = new ArrayList(); 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); + } + }