X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.layer0%2Fsrc%2Forg%2Fsimantics%2Fdb%2Flayer0%2Futil%2FConsistsOfProcess.java;h=a2e64868f7c21d43c1456c4662810c20f6ca4ed6;hb=71ec74823894138693ed9cde9e22270453fb4127;hp=39c661e4be2d6649b8bb85074b59fcc8e04a9eb0;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ConsistsOfProcess.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ConsistsOfProcess.java index 39c661e4b..a2e64868f 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ConsistsOfProcess.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ConsistsOfProcess.java @@ -1,53 +1,125 @@ package org.simantics.db.layer0.util; +import java.util.ArrayList; import java.util.Collection; +import java.util.HashSet; +import java.util.List; import java.util.Set; import org.simantics.db.AsyncReadGraph; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.ResourceMap; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.utils.Logger; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus; import org.simantics.db.procedure.AsyncContextMultiProcedure; -import org.simantics.db.service.CollectionSupport; +import org.simantics.db.procedure.Procedure; import org.simantics.db.service.DirectQuerySupport; import org.simantics.layer0.Layer0; +import org.simantics.utils.datastructures.Pair; class ConsistsOfProcess { - final Set result; - final AsyncContextMultiProcedure structure; + final List result; + final Set childrenWithNoName; + final AsyncContextMultiProcedure structure; + final AsyncContextMultiProcedure names; - public static Set walk(ReadGraph graph, Collection resources, Set exclusions, boolean ignoreVirtual) throws DatabaseException { - ConsistsOfProcess process = new ConsistsOfProcess(graph, resources, exclusions, ignoreVirtual); - return process.result; + public static Pair,Set> walk(ReadGraph graph, ResourceMap status, Collection resources, Set exclusions, boolean ignoreVirtual) throws DatabaseException { + ConsistsOfProcess process = new ConsistsOfProcess(graph, status, resources, exclusions, ignoreVirtual); + return Pair.make(process.result, process.childrenWithNoName); + } + + static class InternalEntry { + public InternalEntry parent; + public Resource resource; + public String name; + public boolean valid = true; + InternalEntry(InternalEntry parent, Resource resource, String name) { + this.parent = parent; + this.resource = resource; + this.name = name; + } } - private ConsistsOfProcess(ReadGraph graph, final Collection resources, final Set exclusions, final boolean ignoreVirtual) throws DatabaseException { + private ConsistsOfProcess(ReadGraph graph, ResourceMap status, final Collection resources, final Set exclusions, final boolean ignoreVirtual) throws DatabaseException { final Layer0 L0 = Layer0.getInstance(graph); final DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class); - CollectionSupport cs = graph.getService(CollectionSupport.class); - result = cs.createSet(); + result = new ArrayList(); + childrenWithNoName = new HashSet<>(); + names = dqs.compileForEachObject(graph, L0.HasName, new AsyncContextMultiProcedure() { + + @Override + public void execute(AsyncReadGraph graph, InternalEntry entry, Resource nameResource) { - structure = dqs.compileForEachObject(graph, L0.ConsistsOf, new AsyncContextMultiProcedure() { + if(status != null) + status.put(nameResource, ExtentStatus.EXCLUDED); + + graph.forPossibleValue(nameResource, new Procedure() { + + @Override + public void execute(String name) { + if(!entry.valid) return; + + if(name == null) { + entry.valid = false; + } else if (entry.name != null) { + entry.valid = false; + } else { + entry.name = name; + } + } + + @Override + public void exception(Throwable t) { + Logger.defaultLogError(t); + } + + }); + } @Override - public void execute(AsyncReadGraph graph, Resource parent, Resource child) { + public void exception(AsyncReadGraph graph, Throwable throwable) { + Logger.defaultLogError(throwable); + } + + @Override + public void finished(AsyncReadGraph graph, InternalEntry entry) { + if(entry.valid) { + if(entry.name != null) { + result.add(entry); + } else { + // This one did not have a name - not a valid internal + childrenWithNoName.add(entry.resource); + } + } else { + // Something wrong has happened. Do not treat as valid internal + childrenWithNoName.add(entry.resource); + } + } + }); + + structure = dqs.compileForEachObject(graph, L0.ConsistsOf, new AsyncContextMultiProcedure() { + + @Override + public void execute(AsyncReadGraph graph, InternalEntry parent, Resource child) { if(exclusions.contains(child)) return; - if(!ignoreVirtual || child.isPersistent()) - if(result.add(child)) { - dqs.forEachObjectCompiled(graph, child, child, structure); - } + if(!ignoreVirtual || child.isPersistent()) { + InternalEntry entry = new InternalEntry(parent, child, null); + dqs.forEachObjectCompiled(graph, child, entry, structure); + dqs.forEachObjectCompiled(graph, child, entry, names); + } } @Override - public void finished(AsyncReadGraph graph) { + public void finished(AsyncReadGraph graph, InternalEntry parent) { } @Override @@ -61,8 +133,10 @@ class ConsistsOfProcess { @Override public void run(ReadGraph graph) throws DatabaseException { - for(Resource r : resources) - dqs.forEachObjectCompiled(graph, r, r, structure); + for(Resource r : resources) { + InternalEntry root = new InternalEntry(null, r, null); + dqs.forEachObjectCompiled(graph, r, root, structure); + } } });