/******************************************************************************* * 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; } }