X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FTypeHierarchy.java;fp=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FTypeHierarchy.java;h=a8a3ea3179867c3bb81319e8bbcfbabcb28e3e30;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/TypeHierarchy.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/TypeHierarchy.java new file mode 100644 index 000000000..a8a3ea317 --- /dev/null +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/TypeHierarchy.java @@ -0,0 +1,199 @@ +/******************************************************************************* + * 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.impl.query; + +import gnu.trove.procedure.TIntProcedure; + +import java.util.concurrent.Semaphore; + +import org.simantics.db.impl.graph.ReadGraphImpl; +import org.simantics.db.impl.procedure.InternalProcedure; +import org.simantics.db.procedure.ListenerBase; + +final public class TypeHierarchy extends UnaryQuery> { + +// public ArrayList> procs = null; + + private TypeHierarchy(final int resource) { + super(resource); + } + + final static void runner(ReadGraphImpl graph, final int r, final CacheEntry parent, final QueryProcessor provider, final ListenerBase listener, final InternalProcedure procedure) { + + TypeHierarchy entry = (TypeHierarchy)provider.typeHierarchyMap.get(r); + if(entry == null) { + + entry = new TypeHierarchy(r); + entry.setPending(); + entry.clearResult(provider.querySupport); + entry.putEntry(provider); + + provider.performForEach(graph, entry, parent, listener, procedure); + + } else { + + if(!entry.isReady()) { + synchronized(entry) { + if(!entry.isReady()) { + throw new IllegalStateException(); +// if(entry.procs == null) entry.procs = new ArrayList>(); +// entry.procs.add(procedure); +// provider.registerDependencies(graph, entry, parent, listener, procedure, false); +// return; + } + } + } + provider.performForEach(graph, entry, parent, listener, procedure); + } + + } + + final public static void queryEach(ReadGraphImpl graph, final int r, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final InternalProcedure procedure) { + + if(parent == null && listener == null) { + TypeHierarchy entry = (TypeHierarchy)provider.typeHierarchyMap.get(r); + if(entry != null && entry.isReady()) { + entry.performFromCache(graph, provider, procedure); + return; + } + } + + runner(graph, r, parent, provider, listener, procedure); + + } + + @Override + public UnaryQuery> getEntry(QueryProcessor provider) { + return provider.typeHierarchyMap.get(id); + } + + @Override + public void putEntry(QueryProcessor provider) { + provider.typeHierarchyMap.put(id, this); + } + + @Override + final public void removeEntry(QueryProcessor provider) { + provider.typeHierarchyMap.remove(id); + } + + @Override + public IntSet computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final InternalProcedure procedure, boolean store) { + + final IntSet result = new IntSet(provider.querySupport, id); + + final TIntProcedure addToResult = new TIntProcedure() { + @Override + public boolean execute(int r) { + result.add(r); + return true; + } + }; + + SuperTypes.queryEach(graph, id, provider, TypeHierarchy.this, null, new InternalProcedure() { + + @Override + public void execute(ReadGraphImpl graph, IntSet types) { + + types.forEach(addToResult); + addOrSet(graph, result, provider); + procedure.execute(graph, result); + + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) { + procedure.exception(graph, t); + } + + }); + + return result; + + } + + @Override + public String toString() { + return "TypeHierarchy[" + id + "]"; + } + + private void addOrSet(ReadGraphImpl graph, final IntSet value, QueryProcessor provider) { + + assert(!isReady()); + +// ArrayList> p = null; + + synchronized(this) { + + value.trim(); + setResult(value); + setReady(); +// p = procs; +// procs = null; + + } + +// if(p != null) { +// IntSet v = (IntSet)getResult(); +// if(v != null) { +// for(InternalProcedure proc : p) proc.execute(graph, v); +// } +// } + + } + + @Override + public Object performFromCache(ReadGraphImpl graph, QueryProcessor provider, InternalProcedure procedure) { + + assert(isReady()); + + if(handleException(graph, procedure)) return null; + + IntSet result = getResult(); + + procedure.execute(graph, result); + + return result; + + } + + @Override + public void recompute(ReadGraphImpl graph, QueryProcessor provider) { + + final Semaphore s = new Semaphore(0); + + computeForEach(graph, provider, new InternalProcedure() { + + @Override + public void execute(ReadGraphImpl graph, IntSet result) { + s.release(); + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) { + s.release(); + new Error("Error in recompute.", t).printStackTrace(); + } + + }, true); + + while(!s.tryAcquire()) { + provider.resume(graph); + } + + } + + @Override + boolean isImmutable(ReadGraphImpl graph) { + return graph.processor.isImmutable(id); + } +}