X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FAssertedStatements.java;h=8649de32eeee907b1e551a4e681b165c1edd1768;hp=2c5314b7955f1d9174c6935683021207756a6240;hb=c4d9561b1b35a0e8e594158fbb01a9c632997808;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AssertedStatements.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AssertedStatements.java index 2c5314b79..8649de32e 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AssertedStatements.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/AssertedStatements.java @@ -1,534 +1,455 @@ -/******************************************************************************* - * 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 java.util.ArrayList; -import java.util.concurrent.Semaphore; -import java.util.concurrent.atomic.AtomicInteger; - -import org.simantics.db.RelationInfo; -import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; -import org.simantics.db.impl.graph.ReadGraphImpl; -import org.simantics.db.impl.procedure.InternalProcedure; -import org.simantics.db.impl.procedure.TripleIntProcedureAdapter; -import org.simantics.db.procedure.ListenerBase; -import org.simantics.db.request.RequestFlags; - - -final public class AssertedStatements extends CollectionBinaryQuery { - -// public ArrayList procs; - - public AssertedStatements(final int r1, final int r2) { - super(r1, r2); - } - - public static AssertedStatements newInstance(final int r1, final int r2) { - return new AssertedStatements(r1, r2); - } - - final static AssertedStatements runner(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final AssertedStatements cached, final CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) { - - AssertedStatements entry = cached != null ? cached : (AssertedStatements)provider.assertedStatementsMap.get(id(r1,r2)); - if(entry == null) { - - entry = new AssertedStatements(r1, r2); - entry.setPending(); - entry.clearResult(provider.querySupport); - entry.putEntry(provider); - - provider.performForEach(graph, entry, parent, listener, procedure); - - return entry; - - } else { - - if(entry.isPending()) { - synchronized(entry) { - if(entry.isPending()) { - throw new IllegalStateException(); -// if(entry.procs == null) entry.procs = new ArrayList(); -// entry.procs.add(procedure); -// provider.registerDependencies(graph, entry, parent, listener, procedure, false); -// return entry; - } - } - } - provider.performForEach(graph, entry, parent, listener, procedure); - } - - return entry; - - } - - final public static AssertedStatements queryEach(ReadGraphImpl graph, final int r1, final int r2, final QueryProcessor provider, final CacheEntry parent, final ListenerBase listener, final TripleIntProcedure procedure) { - - assert(r1 != 0); - assert(r2 != 0); - - final AssertedStatements entry = (AssertedStatements)provider.assertedStatementsMap.get(id(r1,r2)); - - if(parent == null && !(listener != null)) { - if(entry != null && entry.isReady()) { - entry.performFromCache(graph, provider, procedure); - return entry; - } - } - - return runner(graph, r1, r2, provider, entry, parent, listener, procedure); - - } - - @Override - public BinaryQuery getEntry(QueryProcessor provider) { - return provider.assertedStatementsMap.get(id); - } - - @Override - public void putEntry(QueryProcessor provider) { - provider.assertedStatementsMap.put(id, this); - } - - @Override - final public void removeEntry(QueryProcessor provider) { - provider.assertedStatementsMap.remove(id); - } - - void computeInheritedAssertions(ReadGraphImpl graph, int type, final int predicate, final RelationInfo ri, final QueryProcessor queryProvider, final TripleIntProcedure proc) { - -// final AtomicBoolean found = new AtomicBoolean(0); - - DirectObjects.queryEach(graph, type, queryProvider.getInherits(), queryProvider, this, null, new SyncIntProcedure() { - - @Override - public void run(ReadGraphImpl graph) { - -// if(ri.isFunctional && found.get() == 1) { -// -// ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has assertions from multiple inherited types."); -// except(exception); -// proc.exception(graph, exception); -// return; -// -// } - - finish(graph, queryProvider); - proc.finished(graph); - - } - - @Override - public void execute(ReadGraphImpl graph, int inh) { - -// if(ri.isFunctional && found.get() == 1) return; - - inc(); - - AssertedStatements.queryEach(graph, inh, predicate, queryProvider, AssertedStatements.this, null, new TripleIntProcedureAdapter() { - - @Override - public void execute(ReadGraphImpl graph, int s, int p, int o) { - -// if(ri.isFunctional) { -// -//// if(found.get() == 1) return; -// -// if(found.compareAndSet(0, o)) { - if(addOrSet(s,p,o)) - proc.execute(graph, s, p, o); -// } -// // If this was a duplicate, we can ignore this -// else if (found.compareAndSet(o, o)) { -// //System.err.println("duplicates!"); -// } -// // Otherwise we have error -// else { -// found.set(1); -// } -// -// } else { -// -// addOrSet(s, p, o); -// proc.execute(graph, s, p, o); -// -// } - - } - - @Override - public void finished(ReadGraphImpl graph) { - dec(graph); - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - proc.exception(graph, t); - dec(graph); - } - - }); - - } - - @Override - public void finished(ReadGraphImpl graph) { - dec(graph); - } - - }); - - } - - void computeLocalAssertions(ReadGraphImpl graph, final int type, final int predicate, final RelationInfo ri, final QueryProcessor queryProvider, final TripleIntProcedure proc) { - - final AtomicInteger found = new AtomicInteger(0); - - DirectObjects.queryEach(graph, type, queryProvider.getAsserts(), queryProvider, this, null, new SyncIntProcedure() { - - @Override - public void run(ReadGraphImpl graph) { - - if(ri.isFunctional && found.get() > 1) { - - ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one asserted statement."); - except(exception); - proc.exception(graph, exception); - return; - - } - - if(ri.isFunctional && found.get() == 1) { - - finish(graph, queryProvider); - proc.finished(graph); - return; - - } - - computeInheritedAssertions(graph, type, predicate, ri, queryProvider, proc); - - } - - @Override - public void execute(ReadGraphImpl graph, final int ass) { - - if(ri.isFunctional && found.get() > 1) return; - - inc(); - - DirectObjects.queryEach(graph, ass, queryProvider.getHasPredicate(), queryProvider, AssertedStatements.this, null, new IntProcedure() { - - @Override - public void execute(ReadGraphImpl graph, final int pred) { - - if(ri.isFunctional) { - - if(found.get() > 1) return; - - inc(); - - DirectObjects.queryEach(graph, ass, queryProvider.getHasObject(), queryProvider, AssertedStatements.this, null, new IntProcedure() { - - @Override - public void execute(ReadGraphImpl graph, final int object) { - - if(found.get() > 1) return; - - if(pred == predicate) { - - if(found.getAndIncrement() == 0) { - if(addOrSet(type, pred, object)) - proc.execute(graph, type, pred, object); - } - - return; - - } - - if ( !ri.isFinal ) { - - inc(); - - SuperRelations.queryEach(graph, pred, queryProvider, AssertedStatements.this, null, new InternalProcedure() { - - @Override - public void execute(ReadGraphImpl graph, IntSet result) { - - if(found.get() > 1) { - dec(graph); - return; - } - - if(result.contains(predicate)) { - - if(found.getAndIncrement() == 0) { - if(addOrSet(type, pred, object)) - proc.execute(graph, type, pred, object); - } - - } - - dec(graph); - - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - - proc.exception(graph, t); - dec(graph); - - } - - }); - - } - - } - - @Override - public void finished(ReadGraphImpl graph) { - dec(graph); - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - proc.exception(graph, t); - dec(graph); - } - - }); - - } else { - - inc(); - - DirectObjects.queryEach(graph, ass, queryProvider.getHasObject(), queryProvider, AssertedStatements.this, null, new IntProcedure() { - - @Override - public void execute(ReadGraphImpl graph, final int object) { - - if(pred == predicate) { - - addOrSet(type, pred, object); - proc.execute(graph, type, pred, object); - return; - - } - - if ( !ri.isFinal ) { - - inc(); - - SuperRelations.queryEach(graph, pred, queryProvider, AssertedStatements.this, null, new InternalProcedure() { - - @Override - public void execute(ReadGraphImpl graph, IntSet result) { - - if(result.contains(predicate)) { - - addOrSet(type, pred, object); - proc.execute(graph, type, pred, object); - - } - - dec(graph); - - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - proc.exception(graph, t); - dec(graph); - } - - }); - - } - - } - - @Override - public void finished(ReadGraphImpl graph) { - dec(graph); - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - proc.exception(graph, t); - dec(graph); - } - - }); - - } - - } - - @Override - public void finished(ReadGraphImpl graph) { - dec(graph); - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - proc.exception(graph, t); - dec(graph); - } - - }); - - } - - @Override - public void finished(ReadGraphImpl graph) { - dec(graph); - } - - }); - - } - - @Override - public void computeForEach(ReadGraphImpl graph, final QueryProcessor provider, final TripleIntProcedure procedure, boolean store) { - - RelationInfoQuery.queryEach(graph, r2(), provider, this, null, new InternalProcedure() { - - @Override - public void execute(ReadGraphImpl graph, RelationInfo ri) { - - computeLocalAssertions(graph, r1(), r2(), ri, provider, procedure); - - } - - @Override - public void exception(ReadGraphImpl graph, Throwable t) { - procedure.exception(graph, t); - } - - }); - - } - - @Override - public String toString() { - return "AssertedStatements2[" + r1() + " - " + r2() + "]"; - } - - private boolean addOrSet(int s, int p, int o) { - - assert(isPending()); - - IntArray value = (IntArray)getResult(); - if(value.data != null) { - for(int i = 0;i < value.sizeOrData ; i+=3) { - int existingP = value.data[i+1]; - if(p == existingP) { - int existingO = value.data[i+2]; - if(existingO == o) return false; - } - } - value.add(s); - value.add(p); - value.add(o); - } else { - value.add(s); - value.add(p); - value.add(o); - } - - return true; - - } - - final private void finish(ReadGraphImpl graph, QueryProcessor provider) { - - assert(isPending()); - -// ArrayList p = null; - - synchronized(this) { - - setReady(); -// p = procs; -// procs = null; - - } - -// if(p != null) { -// final IntArray value = (IntArray)getResult(); -// for(TripleIntProcedure proc : p) { -// for(int i=0;i { + + public AssertedStatements(final int r1, final int r2) { + super(r1, r2); + } + + public static AssertedStatements newInstance(final int r1, final int r2) { + return new AssertedStatements(r1, r2); + } + + @Override + final public void removeEntry(QueryProcessor provider) { + provider.cache.remove(this); + } + + static void computeInheritedAssertions(ReadGraphImpl graph, int type, final int predicate, final RelationInfo ri, final AssertedStatements entry, final TripleIntProcedure proc) throws DatabaseException { + + QueryProcessor processor = graph.processor; + + QueryCache.runnerDirectObjects(graph, type, processor.getInherits(), entry, null, new SyncIntProcedure() { + + @Override + public void run(ReadGraphImpl graph) throws DatabaseException { + +// if(ri.isFunctional && found.get() == 1) { +// +// ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has assertions from multiple inherited types."); +// except(exception); +// proc.exception(graph, exception); +// return; +// +// } + + finish(graph, entry); + proc.finished(graph); + + } + + @Override + public void execute(ReadGraphImpl graph, int inh) throws DatabaseException { + +// if(ri.isFunctional && found.get() == 1) return; + + inc(); + + QueryCache.runnerAssertedStatements(graph, inh, predicate, entry, null, new TripleIntProcedureAdapter() { + + @Override + public void execute(ReadGraphImpl graph, int s, int p, int o) throws DatabaseException { + +// if(ri.isFunctional) { +// +//// if(found.get() == 1) return; +// +// if(found.compareAndSet(0, o)) { + if(addOrSet(s,p,o, entry)) + proc.execute(graph, s, p, o); +// } +// // If this was a duplicate, we can ignore this +// else if (found.compareAndSet(o, o)) { +// //System.err.println("duplicates!"); +// } +// // Otherwise we have error +// else { +// found.set(1); +// } +// +// } else { +// +// addOrSet(s, p, o); +// proc.execute(graph, s, p, o); +// +// } + + } + + @Override + public void finished(ReadGraphImpl graph) throws DatabaseException { + dec(graph); + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { + proc.exception(graph, t); + dec(graph); + } + + }); + + } + + @Override + public void finished(ReadGraphImpl graph) throws DatabaseException { + dec(graph); + } + + }); + + } + + static void computeLocalAssertions(ReadGraphImpl graph, final int type, final int predicate, final RelationInfo ri, final AssertedStatements entry, final TripleIntProcedure proc) throws DatabaseException { + + + } + + //@Override + public Object compute(ReadGraphImpl graph, final TripleIntProcedure procedure) throws DatabaseException { + computeForEach(graph, r1(), r2(), this, procedure); + return getResult(); + } + + public static void computeForEach(ReadGraphImpl graph, final int type, final int predicate, final AssertedStatements entry, final TripleIntProcedure procedure) throws DatabaseException { + + RelationInfo ri = QueryCache.resultRelationInfoQuery(graph, predicate, entry, null); + + final AtomicInteger found = new AtomicInteger(0); + + QueryProcessor processor = graph.processor; + + QueryCache.runnerDirectObjects(graph, type, processor.getAsserts(), entry, null, new SyncIntProcedure() { + + @Override + public void run(ReadGraphImpl graph) throws DatabaseException { + + if(ri.isFunctional && found.get() > 1) { + + ManyObjectsForFunctionalRelationException exception = new ManyObjectsForFunctionalRelationException("Functional relation has more than one asserted statement."); + except(exception, entry); + procedure.exception(graph, exception); + return; + + } + + if(ri.isFunctional && found.get() == 1) { + + finish(graph, entry); + procedure.finished(graph); + return; + + } + + computeInheritedAssertions(graph, type, predicate, ri, entry, procedure); + + } + + @Override + public void execute(ReadGraphImpl graph, final int ass) throws DatabaseException { + + if(ri.isFunctional && found.get() > 1) return; + + inc(); + + QueryCache.runnerDirectObjects(graph, ass, processor.getHasPredicate(), entry, null, new IntProcedure() { + + @Override + public void execute(ReadGraphImpl graph, final int pred) throws DatabaseException { + + if(ri.isFunctional) { + + if(found.get() > 1) return; + + inc(); + + QueryCache.runnerDirectObjects(graph, ass, processor.getHasObject(), entry, null, new IntProcedure() { + + @Override + public void execute(ReadGraphImpl graph, final int object) throws DatabaseException { + + if(found.get() > 1) return; + + if(pred == predicate) { + + if(found.getAndIncrement() == 0) { + if(addOrSet(type, pred, object, entry)) + procedure.execute(graph, type, pred, object); + } + + return; + + } + + if ( !ri.isFinal ) { + + inc(); + + QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure() { + + @Override + public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException { + + if(found.get() > 1) { + dec(graph); + return; + } + + if(result.contains(predicate)) { + + if(found.getAndIncrement() == 0) { + if(addOrSet(type, pred, object, entry)) + procedure.execute(graph, type, pred, object); + } + + } + + dec(graph); + + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { + + procedure.exception(graph, t); + dec(graph); + + } + + }); + + } + + } + + @Override + public void finished(ReadGraphImpl graph) throws DatabaseException { + dec(graph); + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { + procedure.exception(graph, t); + dec(graph); + } + + }); + + } else { + + inc(); + + QueryCache.runnerDirectObjects(graph, ass, processor.getHasObject(), entry, null, new IntProcedure() { + + @Override + public void execute(ReadGraphImpl graph, final int object) throws DatabaseException { + + if(pred == predicate) { + + addOrSet(type, pred, object, entry); + procedure.execute(graph, type, pred, object); + return; + + } + + if ( !ri.isFinal ) { + + inc(); + + QueryCache.runnerSuperRelations(graph, pred, entry, null, new InternalProcedure() { + + @Override + public void execute(ReadGraphImpl graph, IntSet result) throws DatabaseException { + + if(result.contains(predicate)) { + + addOrSet(type, pred, object, entry); + procedure.execute(graph, type, pred, object); + + } + + dec(graph); + + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { + procedure.exception(graph, t); + dec(graph); + } + + }); + + } + + } + + @Override + public void finished(ReadGraphImpl graph) throws DatabaseException { + dec(graph); + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { + procedure.exception(graph, t); + dec(graph); + } + + }); + + } + + } + + @Override + public void finished(ReadGraphImpl graph) throws DatabaseException { + dec(graph); + } + + @Override + public void exception(ReadGraphImpl graph, Throwable t) throws DatabaseException { + procedure.exception(graph, t); + dec(graph); + } + + }); + + } + + @Override + public void finished(ReadGraphImpl graph) throws DatabaseException { + dec(graph); + } + + }); + + } + + @Override + public String toString() { + return "AssertedStatements[" + r1() + " - " + r2() + "]"; + } + + private boolean addOrSet(int s, int p, int o) { + + assert(isPending()); + + IntArray value = (IntArray)getResult(); + if(value.data != null) { + for(int i = 0;i < value.sizeOrData ; i+=3) { + int existingP = value.data[i+1]; + if(p == existingP) { + int existingO = value.data[i+2]; + if(existingO == o) return false; + } + } + value.add(s); + value.add(p); + value.add(o); + } else { + value.add(s); + value.add(p); + value.add(o); + } + + return true; + + } + + static boolean addOrSet(int s, int p, int o, AssertedStatements entry) { + if(entry != null) { + return entry.addOrSet(s, p, o); + } else { + return true; + } + } + + static void finish(ReadGraphImpl graph, AssertedStatements entry) { + + assert(entry.isPending()); + if(entry != null) { + synchronized(entry) { + entry.setReady(); + } + } + + } + + static void except(Throwable t, AssertedStatements entry) { + if(entry != null) { + synchronized(entry) { + entry.except(t); + } + } + } + + @Override + public Object performFromCache(ReadGraphImpl graph, final TripleIntProcedure procedure) throws DatabaseException { + + assert(isReady()); + + if(handleException(graph, procedure)) return getResult(); + + final IntArray value = (IntArray)getResult(); + for(int i=0;i