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=55fe1ac0111f008f90c3d0b816720b01ce34ce03;hb=48bb50bb6640506d1f150ca8e4fa5a6e878464be;hp=0b461bcc133e766af4d45850328c120722cbc11c;hpb=969bd23cab98a79ca9101af33334000879fb60c5;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 0b461bcc1..55fe1ac01 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,73 +1,157 @@ -package org.simantics.db.layer0.util; - -import java.util.Collection; -import java.util.Set; - -import org.simantics.db.AsyncReadGraph; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.request.ReadRequest; -import org.simantics.db.common.utils.Logger; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.procedure.AsyncContextMultiProcedure; -import org.simantics.db.service.CollectionSupport; -import org.simantics.db.service.DirectQuerySupport; -import org.simantics.layer0.Layer0; - -class ConsistsOfProcess { - - final Set result; - final AsyncContextMultiProcedure structure; - - 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; - } - - private ConsistsOfProcess(ReadGraph graph, 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(); - - structure = dqs.compileForEachObject(graph, L0.ConsistsOf, new AsyncContextMultiProcedure() { - - @Override - public void execute(AsyncReadGraph graph, Resource parent, Resource child) { - - if(exclusions.contains(child)) return; - - if(!ignoreVirtual || child.isPersistent()) - if(result.add(child)) { - dqs.forEachObjectCompiled(graph, child, child, structure); - } - - } - - @Override - public void finished(AsyncReadGraph graph) { - } - - @Override - public void exception(AsyncReadGraph graph, Throwable throwable) { - Logger.defaultLogError(throwable); - } - - }); - - graph.syncRequest(new ReadRequest() { - - @Override - public void run(ReadGraph graph) throws DatabaseException { - for(Resource r : resources) - dqs.forEachObjectCompiled(graph, r, r, structure); - } - - }); - - } - - -} +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.databoard.Bindings; +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.layer0.util.TransferableGraphConfiguration2.SeedSpec; +import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec.SeedSpecType; +import org.simantics.db.procedure.SyncContextMultiProcedure; +import org.simantics.db.service.DirectQuerySupport; +import org.simantics.layer0.Layer0; +import org.simantics.utils.datastructures.Pair; + +class ConsistsOfProcess { + + final List result; + final Set childrenWithNoName; + final SyncContextMultiProcedure structure; + final SyncContextMultiProcedure names; + + public static Pair,Set> walk(ReadGraph graph, Collection specs, boolean ignoreVirtual) throws DatabaseException { + return walk(graph, null, specs, ignoreVirtual); + } + + public static Pair,Set> walk(ReadGraph graph, ResourceMap status, Collection specs, boolean ignoreVirtual) throws DatabaseException { + + ConsistsOfProcess process = new ConsistsOfProcess(graph, status, specs, ignoreVirtual); + return Pair.make(process.result, process.childrenWithNoName); + + } + + static class ConsistsOfProcessEntry { + public ConsistsOfProcessEntry parent; + public Resource resource; + public boolean valid = true; + public String name = null; + ConsistsOfProcessEntry(ConsistsOfProcessEntry parent, Resource resource) { + this.parent = parent; + this.resource = resource; + } + } + + private ConsistsOfProcess(ReadGraph graph, ResourceMap status, final Collection seeds, final boolean ignoreVirtual) throws DatabaseException { + + final Layer0 L0 = Layer0.getInstance(graph); + final DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class); + + result = new ArrayList<>(); + childrenWithNoName = new HashSet<>(); + names = dqs.compileForEachObject(graph, L0.HasName, new SyncContextMultiProcedure() { + + @Override + public void execute(ReadGraph graph, ConsistsOfProcessEntry entry, Resource nameResource) throws DatabaseException { + + if(status != null) + status.put(nameResource, ExtentStatus.EXCLUDED); + + if(!entry.valid) return; + + String name = graph.getValue(nameResource, Bindings.STRING); + if(name == null) { + entry.valid = false; + } else if (entry.name != null) { + entry.valid = false; + } else { + entry.name = name; + } + + + } + + @Override + public void exception(ReadGraph graph, Throwable throwable) { + Logger.defaultLogError(throwable); + } + + @Override + public void finished(ReadGraph graph, ConsistsOfProcessEntry 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 SyncContextMultiProcedure() { + + @Override + public void execute(ReadGraph graph, ConsistsOfProcessEntry parent, Resource child) { + + if(status != null) + if(ExtentStatus.EXCLUDED.equals(status.get(child))) return; + + if(!ignoreVirtual || child.isPersistent()) { + ConsistsOfProcessEntry entry = new ConsistsOfProcessEntry(parent, child); + dqs.forEachObjectCompiled(graph, child, entry, structure); + dqs.forEachObjectCompiled(graph, child, entry, names); + } + + } + + @Override + public void finished(ReadGraph graph, ConsistsOfProcessEntry parent) { + } + + @Override + public void exception(ReadGraph graph, Throwable throwable) { + Logger.defaultLogError(throwable); + } + + }); + + graph.syncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + for(SeedSpec seed : seeds) { + + if(status != null) { + ExtentStatus es = status.get(seed.resource); + if(ExtentStatus.EXCLUDED.equals(es)) continue; + if(ExtentStatus.EXTERNAL.equals(es)) continue; + } + + ConsistsOfProcessEntry entry = new ConsistsOfProcessEntry(null, seed.resource); + + dqs.forEachObjectCompiled(graph, seed.resource, entry, structure); + if(SeedSpecType.INTERNAL.equals(seed.specType)) { + // Process names only for internal seeds + dqs.forEachObjectCompiled(graph, seed.resource, entry, names); + } + } + } + + }); + + } + + +}