X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2Frequest%2FDependentInstances3.java;fp=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2Frequest%2FDependentInstances3.java;h=9ef98c8bbec771ec968dad0fbad1753f87a2d8a3;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/request/DependentInstances3.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/request/DependentInstances3.java new file mode 100644 index 000000000..9ef98c8bb --- /dev/null +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/request/DependentInstances3.java @@ -0,0 +1,84 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.db.common.request; + +import java.util.Set; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.Statement; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.service.CollectionSupport; +import org.simantics.layer0.Layer0; +import org.simantics.utils.Development; + +import gnu.trove.map.hash.THashMap; + +public class DependentInstances3 extends ResourceRead { + + private static final boolean DEBUG = false; + + private static ThreadLocal> workArea = new ThreadLocal>() { + protected gnu.trove.map.hash.THashMap initialValue() { + return new THashMap<>(); + } + }; + + public DependentInstances3(Resource resource) { + super(resource); + } + + @Override + public ResourceSetGraph perform(ReadGraph graph) throws DatabaseException { + + if(resource.equals(graph.getRootLibrary())) return new ResourceSetGraph(graph, resource); + + if(Development.DEVELOPMENT) + if(graph.isImmutable(resource)) + System.err.println("Immutable resource in DependentInstances3: " + NameUtils.getSafeName(graph, resource, true)); + + // This is a loop + THashMap workingSet = workArea.get(); + ResourceSetGraph result = workingSet.get(this); + if(result != null) return result; + + result = new ResourceSetGraph(graph, resource); + + workingSet.put(this, result); + + CollectionSupport cs = graph.getService(CollectionSupport.class); + Set objects = cs.createSet(); + + Layer0 L0 = Layer0.getInstance(graph); + for(Statement dep : graph.getStatements(resource, L0.IsDependencyOf)) { + if(graph.isSubrelationOf(dep.getPredicate(), L0.HasPrevious)) continue; + Resource object = dep.getObject(); + if(graph.isImmutable(object)) continue; + objects.add(object); + } + + for(Resource object : objects ) { + if(DEBUG) + System.err.println(NameUtils.getSafeName(graph, object, true) + " depends on " + NameUtils.getSafeName(graph, resource, true)); + DependentInstances3 query = new DependentInstances3(object); + ResourceSetGraph instances = workingSet.get(query); + if(instances == null) instances = graph.syncRequest(query); + result.references.add(instances); + } + + workingSet.remove(this); + return result; + + } + +}