1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl.graph;
14 import java.io.BufferedOutputStream;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.io.PrintStream;
19 import java.lang.reflect.Array;
20 import java.lang.reflect.InvocationTargetException;
21 import java.nio.BufferUnderflowException;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.IdentityHashMap;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.ListIterator;
30 import java.util.function.Consumer;
32 import org.eclipse.core.runtime.Platform;
33 import org.simantics.databoard.Accessors;
34 import org.simantics.databoard.Bindings;
35 import org.simantics.databoard.accessor.Accessor;
36 import org.simantics.databoard.accessor.error.AccessorConstructionException;
37 import org.simantics.databoard.adapter.AdaptException;
38 import org.simantics.databoard.binding.Binding;
39 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;
40 import org.simantics.databoard.binding.impl.ObjectVariantBinding;
41 import org.simantics.databoard.binding.mutable.Variant;
42 import org.simantics.databoard.serialization.Serializer;
43 import org.simantics.databoard.type.Datatype;
44 import org.simantics.databoard.util.binary.BinaryFile;
45 import org.simantics.databoard.util.binary.RandomAccessBinary;
46 import org.simantics.db.AsyncReadGraph;
47 import org.simantics.db.DevelopmentKeys;
48 import org.simantics.db.ExternalValue;
49 import org.simantics.db.ExternalValueSupport;
50 import org.simantics.db.ReadGraph;
51 import org.simantics.db.RelationContext;
52 import org.simantics.db.Resource;
53 import org.simantics.db.Session;
54 import org.simantics.db.Statement;
55 import org.simantics.db.adaption.AdaptionService;
56 import org.simantics.db.common.primitiverequest.Adapter;
57 import org.simantics.db.common.primitiverequest.Builtin;
58 import org.simantics.db.common.primitiverequest.DatatypeBinding;
59 import org.simantics.db.common.primitiverequest.ForEachAssertedObject;
60 import org.simantics.db.common.primitiverequest.ForEachAssertedStatement;
61 import org.simantics.db.common.primitiverequest.HasStatement;
62 import org.simantics.db.common.primitiverequest.HasStatementSubject;
63 import org.simantics.db.common.primitiverequest.HasStatementSubjectObject;
64 import org.simantics.db.common.primitiverequest.HasValue;
65 import org.simantics.db.common.primitiverequest.Inverse;
66 import org.simantics.db.common.primitiverequest.IsInheritedFrom;
67 import org.simantics.db.common.primitiverequest.IsInstanceOf;
68 import org.simantics.db.common.primitiverequest.IsSubrelationOf;
69 import org.simantics.db.common.primitiverequest.OrderedSet;
70 import org.simantics.db.common.primitiverequest.PossibleAdapter;
71 import org.simantics.db.common.primitiverequest.PossibleInverse;
72 import org.simantics.db.common.primitiverequest.PossibleObject;
73 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;
74 import org.simantics.db.common.primitiverequest.PossibleRelatedValueImplied;
75 import org.simantics.db.common.primitiverequest.PossibleStatement;
76 import org.simantics.db.common.primitiverequest.PossibleType;
77 import org.simantics.db.common.primitiverequest.PossibleUniqueAdapter;
78 import org.simantics.db.common.primitiverequest.PossibleValue;
79 import org.simantics.db.common.primitiverequest.PossibleValueImplied;
80 import org.simantics.db.common.primitiverequest.RelatedValue;
81 import org.simantics.db.common.primitiverequest.RelatedValueImplied;
82 import org.simantics.db.common.primitiverequest.SingleObject;
83 import org.simantics.db.common.primitiverequest.SingleStatement;
84 import org.simantics.db.common.primitiverequest.SingleType;
85 import org.simantics.db.common.primitiverequest.SingleTypeAny;
86 import org.simantics.db.common.primitiverequest.Types;
87 import org.simantics.db.common.primitiverequest.UniqueAdapter;
88 import org.simantics.db.common.primitiverequest.Value;
89 import org.simantics.db.common.primitiverequest.ValueImplied;
90 import org.simantics.db.common.primitiverequest.VariantValueImplied;
91 import org.simantics.db.common.procedure.BlockingAsyncProcedure;
92 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
93 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
94 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
95 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
96 import org.simantics.db.common.procedure.single.SyncReadProcedure;
97 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrErrorProcedure;
98 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrNullProcedure;
99 import org.simantics.db.common.procedure.single.wrapper.ExceptionToNullProcedure;
100 import org.simantics.db.common.procedure.single.wrapper.NullSingleOrNullProcedure;
101 import org.simantics.db.common.procedure.single.wrapper.SingleFunctionalOrNullProcedure;
102 import org.simantics.db.common.procedure.single.wrapper.SingleOrErrorProcedure;
103 import org.simantics.db.common.procedure.single.wrapper.SingleOrNullProcedure;
104 import org.simantics.db.common.procedure.wrapper.NoneToAsyncListener;
105 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiListener;
106 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiProcedure;
107 import org.simantics.db.common.procedure.wrapper.NoneToAsyncProcedure;
108 import org.simantics.db.common.procedure.wrapper.NoneToAsyncSetProcedure;
109 import org.simantics.db.common.procedure.wrapper.SyncToAsyncListener;
110 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiListener;
111 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiProcedure;
112 import org.simantics.db.common.procedure.wrapper.SyncToAsyncProcedure;
113 import org.simantics.db.common.procedure.wrapper.SyncToAsyncSetProcedure;
114 import org.simantics.db.common.request.AdaptValue;
115 import org.simantics.db.common.request.ResourceRead;
116 import org.simantics.db.common.utils.Functions;
117 import org.simantics.db.common.utils.Logger;
118 import org.simantics.db.common.utils.NameUtils;
119 import org.simantics.db.common.validation.L0Validations;
120 import org.simantics.db.exception.AdaptionException;
121 import org.simantics.db.exception.ArgumentException;
122 import org.simantics.db.exception.AssumptionException;
123 import org.simantics.db.exception.BindingException;
124 import org.simantics.db.exception.DatabaseException;
125 import org.simantics.db.exception.DoesNotContainValueException;
126 import org.simantics.db.exception.EmptyResourceException;
127 import org.simantics.db.exception.InternalException;
128 import org.simantics.db.exception.InvalidLiteralException;
129 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
130 import org.simantics.db.exception.NoInverseException;
131 import org.simantics.db.exception.NoSingleResultException;
132 import org.simantics.db.exception.ResourceNotFoundException;
133 import org.simantics.db.exception.ServiceException;
134 import org.simantics.db.exception.ValidationException;
135 import org.simantics.db.impl.RelationContextImpl;
136 import org.simantics.db.impl.ResourceImpl;
137 import org.simantics.db.impl.internal.RandomAccessValueSupport;
138 import org.simantics.db.impl.internal.ResourceData;
139 import org.simantics.db.impl.procedure.CallWrappedSingleQueryProcedure4;
140 import org.simantics.db.impl.procedure.InternalProcedure;
141 import org.simantics.db.impl.procedure.ResultCallWrappedQueryProcedure4;
142 import org.simantics.db.impl.procedure.ResultCallWrappedSingleQueryProcedure4;
143 import org.simantics.db.impl.query.CacheEntry;
144 import org.simantics.db.impl.query.QueryCache;
145 import org.simantics.db.impl.query.QueryCacheBase;
146 import org.simantics.db.impl.query.QueryProcessor;
147 import org.simantics.db.impl.query.QuerySupport;
148 import org.simantics.db.impl.query.TripleIntProcedure;
149 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
150 import org.simantics.db.impl.support.ResourceSupport;
151 import org.simantics.db.procedure.AsyncListener;
152 import org.simantics.db.procedure.AsyncMultiListener;
153 import org.simantics.db.procedure.AsyncMultiProcedure;
154 import org.simantics.db.procedure.AsyncProcedure;
155 import org.simantics.db.procedure.AsyncSetListener;
156 import org.simantics.db.procedure.Listener;
157 import org.simantics.db.procedure.ListenerBase;
158 import org.simantics.db.procedure.MultiListener;
159 import org.simantics.db.procedure.MultiProcedure;
160 import org.simantics.db.procedure.Procedure;
161 import org.simantics.db.procedure.SetListener;
162 import org.simantics.db.procedure.StatementProcedure;
163 import org.simantics.db.procedure.SyncListener;
164 import org.simantics.db.procedure.SyncMultiListener;
165 import org.simantics.db.procedure.SyncMultiProcedure;
166 import org.simantics.db.procedure.SyncProcedure;
167 import org.simantics.db.procedure.SyncSetListener;
168 import org.simantics.db.request.AsyncMultiRead;
169 import org.simantics.db.request.AsyncRead;
170 import org.simantics.db.request.DelayedWrite;
171 import org.simantics.db.request.DelayedWriteResult;
172 import org.simantics.db.request.ExternalRead;
173 import org.simantics.db.request.MultiRead;
174 import org.simantics.db.request.Read;
175 import org.simantics.db.request.ReadInterface;
176 import org.simantics.db.request.RequestFlags;
177 import org.simantics.db.request.Write;
178 import org.simantics.db.request.WriteInterface;
179 import org.simantics.db.request.WriteOnly;
180 import org.simantics.db.request.WriteOnlyResult;
181 import org.simantics.db.request.WriteResult;
182 import org.simantics.layer0.Layer0;
183 import org.simantics.scl.compiler.types.Type;
184 import org.simantics.scl.compiler.types.exceptions.SCLTypeParseException;
185 import org.simantics.scl.reflection.ReflectionUtils;
186 import org.simantics.scl.reflection.ValueNotFoundException;
187 import org.simantics.scl.runtime.function.Function1;
188 import org.simantics.scl.runtime.function.Function3;
189 import org.simantics.utils.DataContainer;
190 import org.simantics.utils.Development;
191 import org.simantics.utils.datastructures.Pair;
192 import org.simantics.utils.datastructures.collections.CollectionUtils;
194 import gnu.trove.map.hash.TObjectIntHashMap;
196 public class ReadGraphImpl implements ReadGraph {
198 final static boolean EMPTY_RESOURCE_CHECK = false;
200 final public CacheEntry parent;
201 final public QueryProcessor processor;
203 final static Binding DATA_TYPE_BINDING_INTERNAL = Bindings.getBindingUnchecked(Datatype.class);
204 final static Serializer DATA_TYPE_SERIALIZER = Bindings.getSerializerUnchecked(DATA_TYPE_BINDING_INTERNAL);
206 final public static TObjectIntHashMap<String> counters = new TObjectIntHashMap<String>();
208 public static void resetCounters() {
212 public static String listCounters(File file) throws IOException {
214 PrintStream b = new PrintStream(new BufferedOutputStream(new FileOutputStream(file)));
216 for(Pair<String,Integer> p : CollectionUtils.valueSortedEntries(counters)) {
217 b.print(-p.second + " " + p.first + "\n");
222 return "Dumped " + counters.size() + " queries.";
227 * Implementation of the interface ReadGraph
229 final public String getURI(final Resource resource) throws AssumptionException, ValidationException, ServiceException {
231 assert (resource != null);
235 return syncRequest(new org.simantics.db.common.uri.ResourceToURI(resource));
237 } catch (AssumptionException e) {
239 throw new AssumptionException(e);
241 } catch (ValidationException e) {
243 throw new ValidationException(e);
245 } catch (ServiceException e) {
247 throw new ServiceException(e);
249 } catch (DatabaseException e) {
251 throw new ServiceException(INTERNAL_ERROR_STRING, e);
257 final public String getPossibleURI(final Resource resource) throws ValidationException, ServiceException {
259 assert (resource != null);
263 return syncRequest(new org.simantics.db.common.uri.ResourceToPossibleURI(resource));
265 } catch (ValidationException e) {
267 throw new ValidationException(e);
269 } catch (ServiceException e) {
271 throw new ServiceException(e);
273 } catch (DatabaseException e) {
275 throw new ServiceException(INTERNAL_ERROR_STRING, e);
281 final public Resource getResource(final String id)
282 throws ResourceNotFoundException, ValidationException,
289 // assert (id != null);
290 // assert (procedure != null);
292 // processor.forResource(this, id, procedure);
294 //// return syncRequest(new org.simantics.db.common.primitiverequest.Resource(
297 Integer rid = QueryCache.resultURIToResource(this, id, parent, null);
298 if(rid == 0) throw new ResourceNotFoundException(id);
299 return processor.querySupport.getResource(rid);
301 } catch (ResourceNotFoundException e) {
303 throw new ResourceNotFoundException(id, e);
305 } catch (ValidationException e) {
307 throw new ValidationException(e);
309 } catch (ServiceException e) {
311 throw new ServiceException(e);
313 } catch (DatabaseException e) {
315 throw new ServiceException(INTERNAL_ERROR_STRING, e);
321 final public Resource getPossibleResource(final String id)
322 throws ResourceNotFoundException, ValidationException,
329 return getResource(id);
331 // return syncRequest(new org.simantics.db.common.primitiverequest.Resource(
334 } catch (ResourceNotFoundException e) {
338 } catch (ValidationException e) {
340 throw new ValidationException(e);
342 } catch (ServiceException e) {
344 throw new ServiceException(e);
346 } catch (DatabaseException e) {
348 throw new ServiceException(INTERNAL_ERROR_STRING, e);
354 final public Resource getRootLibrary() {
355 return processor.getRootLibraryResource();
358 final public Resource getBuiltin(final String id)
359 throws ResourceNotFoundException, ServiceException {
365 return syncRequest(new Builtin(id));
367 } catch (ResourceNotFoundException e) {
369 throw new ResourceNotFoundException(id, e);
371 } catch (ServiceException e) {
373 throw new ServiceException(e);
375 } catch (DatabaseException e) {
377 throw new ServiceException(INTERNAL_ERROR_STRING, e);
383 static class StatementReadProcedure extends TIntArrayListInternal implements StatementProcedure {
385 private static Throwable DONE = new Throwable();
387 Throwable exception = null;
389 final ResourceSupport support;
391 public StatementReadProcedure(ResourceSupport support) {
392 this.support = support;
396 public synchronized void execute(AsyncReadGraph graph, int s, int p, int o) {
403 public void finished(AsyncReadGraph graph) {
408 public void exception(AsyncReadGraph graph, Throwable t) {
412 public void checkAndThrow() throws DatabaseException {
413 if(exception != DONE) {
414 if (exception instanceof DatabaseException)
415 throw (DatabaseException) exception;
417 throw new DatabaseException(
418 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
423 public boolean done() {
424 return exception != null;
428 public boolean contains(Object obj) {
429 if(!(obj instanceof InternalStatement))
431 InternalStatement statement = (InternalStatement)obj;
435 for(int i=0;i<sizeInternal();i+=3)
436 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
441 @SuppressWarnings("unchecked")
443 public <T> T[] toArray(T[] a) {
444 int length = sizeInternal() / 3;
445 if(length > a.length) {
446 Class<?> arrayType = a.getClass();
447 a = (arrayType == Object[].class)
448 ? (T[]) new Object[length]
449 : (T[]) Array.newInstance(arrayType.getComponentType(), length);
452 for(int i=length;i<a.length;++i)
455 for(int i=0,j=0;i<sizeInternal();i+=3,++j)
456 a[j] = (T)new InternalStatement(support, getQuick(i), getQuick(i+1), getQuick(i+2));
461 public boolean add(Statement e) {
462 throw new UnsupportedOperationException();
466 public boolean remove(Object o) {
467 throw new UnsupportedOperationException();
471 public boolean addAll(Collection<? extends Statement> c) {
472 throw new UnsupportedOperationException();
475 class IteratorImpl implements ListIterator<Statement> {
479 public IteratorImpl(int index) {
484 public boolean hasNext() {
485 return index < sizeInternal();
489 public Statement next() {
490 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
496 public void remove() {
497 throw new Error("Not supported");
501 public boolean hasPrevious() {
506 public Statement previous() {
508 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
513 public int nextIndex() {
518 public int previousIndex() {
523 public void set(Statement e) {
524 throw new UnsupportedOperationException();
528 public void add(Statement e) {
529 throw new UnsupportedOperationException();
535 public Iterator<Statement> iterator() {
536 return new IteratorImpl(0);
541 return sizeInternal() / 3;
545 public Object[] toArray() {
546 Object[] result = new Object[sizeInternal() / 3];
547 for(int i=0,j=0;j<sizeInternal();i++,j+=3)
548 result[i] = new InternalStatement(support, getQuick(j), getQuick(j+1), getQuick(j+2));
553 public boolean addAll(int index, Collection<? extends Statement> c) {
554 throw new UnsupportedOperationException();
558 public Statement get(int index) {
560 if(index < 0 || index >= sizeInternal())
561 throw new IndexOutOfBoundsException();
562 return new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
566 public Statement set(int index, Statement element) {
567 throw new UnsupportedOperationException();
571 public void add(int index, Statement element) {
572 throw new UnsupportedOperationException();
576 public Statement remove(int index) {
577 throw new UnsupportedOperationException();
581 public int indexOf(Object obj) {
582 if(!(obj instanceof InternalStatement))
584 InternalStatement statement = (InternalStatement)obj;
588 for(int i=0;i<sizeInternal();i+=3)
589 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
595 public int lastIndexOf(Object obj) {
596 if(!(obj instanceof InternalStatement))
598 InternalStatement statement = (InternalStatement)obj;
602 for(int i=sizeInternal()-3;i>=0;i-=3)
603 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
609 public ListIterator<Statement> listIterator() {
610 return new IteratorImpl(0);
614 public ListIterator<Statement> listIterator(int index) {
615 return new IteratorImpl(index*3);
619 public List<Statement> subList(int fromIndex, int toIndex) {
620 if(fromIndex < 0 || toIndex*3 >= sizeInternal() || fromIndex > toIndex)
621 throw new IndexOutOfBoundsException();
622 return new RandomAccessSubList<Statement>(this, fromIndex, toIndex-fromIndex);
627 final public Collection<Statement> getStatements(final Resource subject,
628 final Resource relation)
629 throws ManyObjectsForFunctionalRelationException, ServiceException {
631 assert (subject != null);
632 assert (relation != null);
636 StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
637 processor.forEachStatement(this, subject, relation, procedure);
638 procedure.checkAndThrow();
641 } catch (DatabaseException e) {
643 System.err.println(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation);
645 StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
646 processor.forEachStatement(this, subject, relation, procedure);
648 return Collections.emptyList();
650 // throw new ServiceException(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation, e);
657 final public Collection<Statement> getAssertedStatements(final Resource subject, final Resource relation)
658 throws ManyObjectsForFunctionalRelationException, ServiceException {
660 assert (subject != null);
661 assert (relation != null);
665 return syncRequest(new ForEachAssertedStatement(subject, relation));
667 } catch (ManyObjectsForFunctionalRelationException e) {
669 throw new ManyObjectsForFunctionalRelationException(e);
671 } catch (ServiceException e) {
673 throw new ServiceException(e);
675 } catch (DatabaseException e) {
677 throw new ServiceException(INTERNAL_ERROR_STRING, e);
684 final public Collection<Resource> getPredicates(final Resource subject) throws ServiceException {
686 assert (subject != null);
690 return processor.getPredicates(this, subject);
692 // AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
693 // processor.forEachPredicate(this, subject, procedure);
694 // procedure.checkAndThrow();
697 } catch (ServiceException e) {
699 throw new ServiceException(e);
701 } catch (DatabaseException e) {
703 throw new ServiceException(INTERNAL_ERROR_STRING, e);
705 } catch (Throwable e) {
707 throw new ServiceException(e);
714 final public Collection<Resource> getPrincipalTypes(final Resource subject)
715 throws ServiceException {
717 assert (subject != null);
721 AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
722 processor.forEachPrincipalType(this, subject, procedure);
723 procedure.checkAndThrow();
726 } catch (ServiceException e) {
728 throw new ServiceException(e);
730 } catch (DatabaseException e) {
732 throw new ServiceException(INTERNAL_ERROR_STRING, e);
739 final public Set<Resource> getTypes(final Resource subject) throws ServiceException {
741 assert (subject != null);
745 return processor.getTypes(this, subject);
747 } catch (ServiceException e) {
749 throw new ServiceException(e);
751 } catch (DatabaseException e) {
753 throw new ServiceException(INTERNAL_ERROR_STRING, e);
755 } catch (Throwable e) {
757 throw new ServiceException(e);
764 final public Set<Resource> getSupertypes(final Resource subject)
765 throws ServiceException {
767 assert (subject != null);
771 SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
772 processor.forSupertypes(this, subject, procedure);
773 procedure.checkAndThrow();
774 return procedure.result;
776 } catch (ServiceException e) {
778 throw new ServiceException(e);
780 } catch (DatabaseException e) {
782 throw new ServiceException(INTERNAL_ERROR_STRING, e);
789 final public Set<Resource> getSuperrelations(final Resource subject)
790 throws ServiceException {
792 assert (subject != null);
796 SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
797 processor.forSuperrelations(this, subject, procedure);
798 procedure.checkAndThrow();
799 return procedure.result;
801 } catch (ServiceException e) {
803 throw new ServiceException(e);
805 } catch (DatabaseException e) {
807 throw new ServiceException(INTERNAL_ERROR_STRING, e);
814 public Resource getPossibleSuperrelation(Resource subject) throws ServiceException {
818 SyncReadProcedure<Resource> procedure = new SyncReadProcedure<Resource>();
819 processor.forPossibleSuperrelation(this, subject, procedure);
820 procedure.checkAndThrow();
821 return procedure.result;
823 } catch (ServiceException e) {
825 throw new ServiceException(e);
827 } catch (DatabaseException e) {
829 throw new ServiceException(INTERNAL_ERROR_STRING, e);
836 final public Collection<Resource> getObjects(final Resource subject, final Resource relation)
837 throws ServiceException {
839 assert (subject != null);
840 assert (relation != null);
842 if(Development.DEVELOPMENT) {
843 if(Development.isTrue(DevelopmentKeys.READGRAPH_COUNT)) {
844 counters.adjustOrPutValue("objects $" + subject.getResourceId() + " $" + relation.getResourceId(), 1, 1);
846 //if(subject.getResourceId()==xx && relation.getResourceId()==xx) new Exception().printStackTrace();
851 AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
852 processor.forEachObject(this, subject, relation, procedure);
853 procedure.checkAndThrow();
856 } catch (DatabaseException e) {
858 throw new ServiceException(INTERNAL_ERROR_STRING, e);
865 final public Collection<Resource> getAssertedObjects(
866 final Resource subject, final Resource relation)
867 throws ManyObjectsForFunctionalRelationException, ServiceException {
870 throw new ArgumentException("Subject must not be null.");
871 if (relation == null)
872 throw new ArgumentException("Relation must not be null. Subject=" + subject);
876 return syncRequest(new ForEachAssertedObject(subject, relation));
878 } catch (ManyObjectsForFunctionalRelationException e) {
880 throw new ManyObjectsForFunctionalRelationException(e);
882 } catch (ServiceException e) {
884 throw new ServiceException(e);
886 } catch (DatabaseException e) {
888 throw new ServiceException(INTERNAL_ERROR_STRING, e);
895 final public Resource getInverse(final Resource relation) throws NoInverseException, ServiceException {
897 assert (relation != null);
901 return getSingleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
903 } catch (NoSingleResultException e) {
905 throw new NoInverseException(e);
907 } catch (ServiceException e) {
909 throw new ServiceException(e);
916 final public Resource getSingleObject(final Resource subject, final Resource relation) throws NoSingleResultException, ServiceException {
918 if( subject == null) throw new IllegalArgumentException("subject can not be null");
919 if( relation == null) throw new IllegalArgumentException("relation can not be null");
922 int single = processor.getSingleObject(this, subject, relation);
924 if (EMPTY_RESOURCE_CHECK) {
925 if (!hasStatement(subject)) {
926 throw new EmptyResourceException("Resource " + debugString(subject));
929 throw new NoSingleResultException("No single object for subject " + debugString(subject)
930 + " and relation " + debugString(relation), single);
932 return processor.querySupport.getResource(single);
933 } catch (NoSingleResultException e) {
935 } catch (DatabaseException e) {
936 throw new ServiceException(e);
941 final public Statement getSingleStatement(final Resource subject, final Resource relation) throws NoSingleResultException, ManyObjectsForFunctionalRelationException, ServiceException {
942 assert (subject != null);
943 assert (relation != null);
945 Collection<Statement> statements = getStatements(subject, relation);
946 if (statements.size() == 1) {
947 return statements.iterator().next();
949 if (EMPTY_RESOURCE_CHECK)
950 if (!hasStatement(subject))
951 throw new EmptyResourceException("Resource " + debugString(subject));
952 throw new NoSingleResultException("No single statement for subject " + debugString(subject)
953 + " and relation " + debugString(relation), statements.size());
955 } catch (ServiceException e) {
956 throw new ServiceException(e);
961 final public Resource getSingleType(final Resource subject) throws NoSingleResultException, ServiceException {
962 assert (subject != null);
964 ArrayList<Resource> principalTypes = (ArrayList<Resource>)getPrincipalTypes(subject);
965 if (principalTypes.size() == 1) {
966 return principalTypes.get(0);
968 throw new NoSingleResultException("No single type for subject " + debugString(subject), principalTypes.size());
970 } catch (ServiceException e) {
971 throw new ServiceException(e);
976 final public Resource getSingleType(final Resource subject,
977 final Resource baseType) throws NoSingleResultException,
980 assert (subject != null);
981 assert (baseType != null);
984 return syncRequest(new SingleType(subject, baseType));
985 } catch (DatabaseException e) {
986 throw new NoSingleResultException("subject=" + subject + ", baseType=" + baseType, 0, e);
991 final public <T> T getValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
993 assert (subject != null);
997 Layer0 L0 = processor.getL0(this);
998 int object = processor.getSingleObject(this, subject, L0.HasDataType);
999 if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
1001 if(processor.isImmutable(object)) {
1002 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1003 return getValue(subject, binding);
1005 byte[] dt = processor.getValue(this, object);
1006 if(dt == null) throw new ServiceException("No data type for " + subject);
1007 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1008 Binding binding = Bindings.getBinding(datatype);
1009 return getValue(subject, binding);
1012 } catch (IOException e) {
1014 throw new ServiceException(e);
1016 } catch (DoesNotContainValueException e) {
1018 throw new DoesNotContainValueException(e, subject);
1020 } catch (ServiceException e) {
1022 throw new ServiceException(e);
1024 } catch (DatabaseException e) {
1026 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1033 final public Variant getVariantValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
1035 assert (subject != null);
1039 Layer0 L0 = processor.getL0(this);
1040 int object = processor.getSingleObject(this, subject, L0.HasDataType);
1041 if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
1043 if(processor.isImmutable(object)) {
1044 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1045 return new Variant(binding, getValue(subject, binding));
1047 byte[] dt = processor.getValue(this, object);
1048 if(dt == null) throw new ServiceException("No data type for " + subject);
1049 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1050 Binding binding = Bindings.getBinding(datatype);
1051 return new Variant(binding, getValue(subject, binding));
1054 } catch (IOException e) {
1056 throw new ServiceException(e);
1058 } catch (DoesNotContainValueException e) {
1060 throw new DoesNotContainValueException(e, subject);
1062 } catch (ServiceException e) {
1064 throw new ServiceException(e);
1066 } catch (DatabaseException e) {
1068 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1073 static final IdentityHashMap<Binding,Serializer> serializers = new IdentityHashMap<Binding,Serializer>();
1076 serializers.put(Bindings.STRING, Bindings.STRING.serializer());
1079 final protected Serializer getSerializer(Binding binding) {
1080 return binding.serializer();
1084 final public <T> T getValue(final Resource subject, final Binding binding) throws DoesNotContainValueException, BindingException,
1087 assert (subject != null);
1091 byte[] bytes = processor.getValue(this, subject);
1092 if (bytes == null) throw new DoesNotContainValueException("No value for resource " + subject);
1094 Serializer serializer = getSerializer(binding);
1095 return (T)serializer.deserialize(bytes);
1097 } catch (DoesNotContainValueException e) {
1099 throw new DoesNotContainValueException(e);
1101 } catch (IOException e) {
1103 throw new ServiceException(e);
1105 } catch (DatabaseException e) {
1107 throw new ServiceException(e);
1109 } catch (BufferUnderflowException e) {
1110 // This is sometimes thrown when deserialize fails because wrong format.
1111 // For callers of this method this is just an service exception.
1112 throw new ServiceException(e);
1118 final public <T> T getRelatedValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1119 DoesNotContainValueException, ServiceException {
1121 assert (subject != null);
1122 assert (relation != null);
1125 Resource object = getSingleObject(subject, relation);
1126 return getValue(object);
1127 } catch (NoSingleResultException e) {
1128 throw new NoSingleResultException("No single value found for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1129 } catch (DoesNotContainValueException e) {
1131 Layer0 L0 = processor.getL0(this);
1132 Resource object = getPossibleObject(subject, relation);
1133 if(isInstanceOf(object, L0.Value)) {
1134 if(isInstanceOf(object, L0.Literal)) {
1135 throw new DoesNotContainValueException(e);
1137 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1140 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1142 } catch (DoesNotContainValueException e2) {
1144 } catch (DatabaseException e2) {
1145 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1147 } catch (ServiceException e) {
1148 throw new ServiceException(e);
1153 final public Variant getRelatedVariantValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1154 DoesNotContainValueException, ServiceException {
1156 assert (subject != null);
1157 assert (relation != null);
1160 Resource object = getSingleObject(subject, relation);
1161 return getVariantValue(object);
1162 } catch (NoSingleResultException e) {
1163 throw new NoSingleResultException("No single object for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1164 } catch (DoesNotContainValueException e) {
1166 Layer0 L0 = processor.getL0(this);
1167 Resource object = getPossibleObject(subject, relation);
1168 if(isInstanceOf(object, L0.Value)) {
1169 if(isInstanceOf(object, L0.Literal)) {
1170 throw new DoesNotContainValueException(e);
1172 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1175 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1177 } catch (DoesNotContainValueException e2) {
1179 } catch (DatabaseException e2) {
1180 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1182 } catch (ServiceException e) {
1183 throw new ServiceException(e);
1188 final public <T> T getRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1189 throws NoSingleResultException, DoesNotContainValueException, BindingException, ServiceException {
1191 assert (subject != null);
1192 assert (relation != null);
1195 Resource object = getSingleObject(subject, relation);
1196 return getValue(object, binding);
1197 } catch (NoSingleResultException e) {
1198 String message = "";
1200 String subjectName = NameUtils.getSafeName(this, subject, true);
1201 String relationName = NameUtils.getSafeName(this, relation, true);
1202 message = "Subject: " + subjectName + ", Relation: " + relationName;
1203 } catch (DatabaseException e2) {
1206 throw new NoSingleResultException(message, e.getResultCount(), e);
1207 } catch (DoesNotContainValueException e) {
1208 throw new DoesNotContainValueException(e);
1209 } catch (ServiceException e) {
1210 throw new ServiceException(e);
1215 final public <T> T adapt(final Resource resource, final Class<T> clazz)
1216 throws AdaptionException, ValidationException, ServiceException {
1218 assert (resource != null);
1219 assert (clazz != null);
1223 return syncRequest(new Adapter<T>(resource, clazz));
1225 } catch (AdaptionException e) {
1227 throw new AdaptionException(e);
1229 } catch (ValidationException e) {
1231 throw new ValidationException(e);
1233 } catch (ServiceException e) {
1235 throw new ServiceException(e);
1237 } catch (DatabaseException e) {
1239 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1246 final public <T,C> T adaptContextual(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1247 throws AdaptionException, ValidationException, ServiceException {
1249 assert (resource != null);
1250 assert (context != null);
1252 class ContextualAdapter implements AsyncRead<T> {
1254 final private Resource resource;
1255 final private C context;
1256 final private Class<T> clazz;
1259 public int hashCode() {
1260 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1264 final public int threadHash() {
1265 return resource.getThreadHash();
1269 public boolean equals(Object object) {
1272 else if (object == null)
1274 else if (getClass() != object.getClass())
1276 ContextualAdapter r = (ContextualAdapter)object;
1277 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1281 public int getFlags() {
1285 public ContextualAdapter(Resource resource, C context, Class<T> clazz) {
1286 this.resource = resource;
1287 this.context = context;
1292 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1294 final AdaptionService service = getSession().peekService(AdaptionService.class);
1295 if (service == null)
1296 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1298 service.adapt(graph, resource, context, contextClass, clazz, false, procedure);
1303 public String toString() {
1304 return "Adapter for (" + resource + "," + context + ") as " + clazz.getName();
1311 return syncRequest(new ContextualAdapter(resource, context, clazz));
1313 } catch (AdaptionException e) {
1315 throw new AdaptionException(e);
1317 } catch (ValidationException e) {
1319 throw new ValidationException(e);
1321 } catch (ServiceException e) {
1323 throw new ServiceException(e);
1325 } catch (DatabaseException e) {
1327 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1334 final public <T> T adaptRelated(final Resource resource, final Resource relation, final Class<T> clazz)
1335 throws AdaptionException, NoSingleResultException, ValidationException, ServiceException {
1337 assert (resource != null);
1338 assert (clazz != null);
1340 Statement stm = getSingleStatement(resource, relation);
1342 return adaptContextual(stm.getObject(), new RelationContextImpl(resource, stm), RelationContext.class, clazz);
1347 final public <T> T getPossibleRelatedAdapter(final Resource resource, final Resource relation, final Class<T> clazz)
1348 throws ValidationException, ServiceException {
1351 return adaptRelated(resource, relation, clazz);
1352 } catch (DatabaseException e) {
1359 final public <T,C> T getPossibleContextualAdapter(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1360 throws ValidationException, ServiceException {
1362 assert (resource != null);
1363 assert (context != null);
1365 class PossibleContextualAdapter implements AsyncRead<T> {
1367 final private Resource resource;
1368 final private C context;
1369 final private Class<T> clazz;
1372 public int hashCode() {
1373 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1377 final public int threadHash() {
1378 return resource.getThreadHash();
1382 public boolean equals(Object object) {
1385 else if (object == null)
1387 else if (getClass() != object.getClass())
1389 PossibleContextualAdapter r = (PossibleContextualAdapter)object;
1390 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1394 public int getFlags() {
1398 public PossibleContextualAdapter(Resource resource, C context, Class<T> clazz) {
1399 this.resource = resource;
1400 this.context = context;
1405 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1407 final AdaptionService service = getSession().peekService(AdaptionService.class);
1408 if (service == null)
1409 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1411 service.adapt(graph, resource, context, contextClass, clazz, true, procedure);
1416 public String toString() {
1417 return "Possible adapter for (" + resource + "," + context + ") as " + clazz.getName();
1424 return syncRequest(new PossibleContextualAdapter(resource, context, clazz));
1426 } catch (ValidationException e) {
1428 throw new ValidationException(e);
1430 } catch (ServiceException e) {
1432 throw new ServiceException(e);
1434 } catch (DatabaseException e) {
1436 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1443 final public <T> T adaptUnique(final Resource resource, final Class<T> clazz)
1444 throws AdaptionException, ValidationException, ServiceException {
1446 assert (resource != null);
1447 assert (clazz != null);
1451 return syncRequest(new UniqueAdapter<T>(resource, clazz));
1453 } catch (AdaptionException e) {
1455 throw new AdaptionException(e);
1457 } catch (ValidationException e) {
1459 throw new ValidationException(e);
1461 } catch (ServiceException e) {
1463 throw new ServiceException(e);
1465 } catch (DatabaseException e) {
1467 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1474 final public Resource getPossibleInverse(final Resource relation)
1475 throws ServiceException {
1477 assert (relation != null);
1481 return getPossibleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
1483 } catch (ServiceException e) {
1485 throw new ServiceException(e);
1487 } catch (DatabaseException e) {
1489 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1496 public Resource getPossibleObject(final Resource subject, final Resource relation)
1497 throws ManyObjectsForFunctionalRelationException, ServiceException {
1499 assert (subject != null);
1500 assert (relation != null);
1504 int result = processor.getSingleObject(this, subject, relation);
1505 if(result == 0) return null;
1507 return processor.querySupport.getResource(result);
1509 } catch (ManyObjectsForFunctionalRelationException e) {
1511 throw new ManyObjectsForFunctionalRelationException("subject=" + subject + ", relation=" + relation, e);
1513 } catch (DatabaseException e) {
1515 throw new ServiceException(e);
1522 final public Statement getPossibleStatement(final Resource subject, final Resource relation)
1523 throws ManyObjectsForFunctionalRelationException, ServiceException {
1525 assert (subject != null);
1526 assert (relation != null);
1530 Collection<Statement> statements = getStatements(subject, relation);
1531 if(statements.size() == 1) return statements.iterator().next();
1534 } catch (ManyObjectsForFunctionalRelationException e) {
1536 throw new ManyObjectsForFunctionalRelationException("Many objects in " + subject + " for functional relation " + relation);
1538 } catch (ServiceException e) {
1540 throw new ServiceException(e);
1547 final public Resource getPossibleType(final Resource subject, final Resource baseType) throws ServiceException {
1549 assert (subject != null);
1550 assert (baseType != null);
1554 AsyncReadProcedure<Resource> procedure = new AsyncReadProcedure<Resource>();
1555 forPossibleType(subject, baseType, procedure);
1556 procedure.checkAndThrow();
1557 return procedure.result;
1559 } catch (ServiceException e) {
1561 throw new ServiceException(e);
1563 } catch (DatabaseException e) {
1565 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1572 final public <T> T getPossibleValue(final Resource subject) throws ServiceException {
1574 assert (subject != null);
1578 int object = processor.getSingleObject(this, subject, processor.getL0(this).HasDataType);
1579 if(object == 0) return null;
1581 if(processor.isImmutable(object)) {
1582 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1583 return getPossibleValue(subject, binding);
1585 byte[] dt = processor.getValue(this, object);
1586 if(dt == null) return null;
1587 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1588 Binding binding = Bindings.getBinding(datatype);
1589 return getPossibleValue(subject, binding);
1592 } catch (IOException e) {
1594 throw new ServiceException(e);
1596 } catch (ServiceException e) {
1598 throw new ServiceException(e);
1600 } catch (DatabaseException e) {
1602 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1609 final public <T> T getPossibleValue(final Resource subject, final Binding binding) throws BindingException, ServiceException {
1611 assert (subject != null);
1612 assert (binding != null);
1616 byte[] dt = processor.getValue(this, subject);
1617 if(dt == null) return null;
1618 Serializer serializer = getSerializer(binding);
1619 return (T)serializer.deserialize(dt);
1621 } catch (IOException e) {
1623 throw new ServiceException(e);
1625 } catch (BindingException e) {
1627 throw new BindingException(e);
1629 } catch (ServiceException e) {
1631 throw new ServiceException(e);
1633 } catch (DatabaseException e) {
1634 e.printStackTrace();
1635 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1641 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation)
1642 throws ManyObjectsForFunctionalRelationException, ServiceException {
1644 assert (subject != null);
1645 assert (relation != null);
1649 Resource object = getPossibleObject(subject, relation);
1650 if(object == null) return null;
1651 else return getPossibleValue(object);
1653 } catch (ManyObjectsForFunctionalRelationException e) {
1655 throw new ManyObjectsForFunctionalRelationException(e);
1657 } catch (ServiceException e) {
1659 throw new ServiceException(e);
1666 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1667 throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
1669 assert (subject != null);
1670 assert (relation != null);
1671 assert (binding != null);
1675 Resource object = getPossibleObject(subject, relation);
1676 if(object == null) return null;
1677 else return getPossibleValue(object, binding);
1679 } catch (ManyObjectsForFunctionalRelationException e) {
1681 throw new ManyObjectsForFunctionalRelationException(e);
1683 } catch (BindingException e) {
1685 throw new BindingException(e);
1687 } catch (ServiceException e) {
1689 throw new ServiceException(e);
1696 public <T> T getPossibleAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1698 assert (resource != null);
1699 assert (clazz != null);
1703 return syncRequest(new PossibleAdapter<T>(resource, clazz));
1705 } catch (ValidationException e) {
1707 throw new ValidationException(e);
1709 } catch (AdaptionException e) {
1713 } catch (DatabaseException e) {
1715 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1721 public <T> T getPossibleUniqueAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1723 assert (resource != null);
1724 assert (clazz != null);
1728 return syncRequest(new PossibleUniqueAdapter<T>(resource, clazz));
1730 } catch (AdaptionException e) {
1734 } catch (ValidationException e) {
1736 throw new ValidationException(e);
1738 } catch (DatabaseException e) {
1740 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1747 final public boolean isInstanceOf(final Resource resource, final Resource type) throws ServiceException {
1749 assert (resource != null);
1750 assert (type != null);
1752 Set<Resource> resources = getTypes(resource);
1753 // This check was necessary because some of the callers of this method got stuck when the NPE was thrown from here.
1754 if (null == resources)
1757 if(EMPTY_RESOURCE_CHECK) {
1758 if (resources.isEmpty()) {
1759 if(!hasStatement(resource)) throw new EmptyResourceException("Resource " + debugString(resource));
1763 return resources.contains(type);
1768 final public boolean isInheritedFrom(final Resource resource, final Resource type) throws ServiceException {
1770 assert (resource != null);
1771 assert (type != null);
1775 if(resource.equals(type)) return true;
1777 return getSupertypes(resource).contains(type);
1779 } catch (ServiceException e) {
1781 throw new ServiceException(e);
1788 final public boolean isSubrelationOf(final Resource resource, final Resource type) throws ServiceException {
1790 assert (resource != null);
1791 assert (type != null);
1795 if(resource.equals(type)) return true;
1797 return getSuperrelations(resource).contains(type);
1799 } catch (ServiceException e) {
1801 throw new ServiceException(e);
1808 final public boolean hasStatement(final Resource subject) throws ServiceException {
1810 assert (subject != null);
1814 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1815 processor.forHasStatement(this, subject, procedure);
1816 procedure.checkAndThrow();
1817 return procedure.result;
1819 } catch (ServiceException e) {
1821 throw new ServiceException(e);
1823 } catch (DatabaseException e) {
1825 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1832 final public boolean hasStatement(final Resource subject, final Resource relation) throws ServiceException {
1834 assert (subject != null);
1835 assert (relation != null);
1839 Collection<Resource> objects = getObjects(subject, relation);
1840 return !objects.isEmpty();
1842 } catch (ServiceException e) {
1844 throw new ServiceException(e);
1851 final public boolean hasStatement(final Resource subject, final Resource relation, final Resource object) throws ServiceException {
1853 assert (subject != null);
1854 assert (relation != null);
1855 assert (object != null);
1859 for(Resource o : getObjects(subject, relation)) {
1860 if(object.equals(o)) return true;
1865 } catch (ServiceException e) {
1867 throw new ServiceException(e);
1874 final public boolean hasValue(final Resource subject) throws ServiceException {
1876 assert (subject != null);
1880 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1881 processor.forHasValue(this, subject, procedure);
1882 procedure.checkAndThrow();
1883 return procedure.result;
1885 } catch (ServiceException e) {
1887 throw new ServiceException(e);
1889 } catch (DatabaseException e) {
1891 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1897 final AsyncProcedure<?> NONE = new AsyncProcedure<Object>() {
1900 public void execute(AsyncReadGraph graph, Object result) {
1904 public void exception(AsyncReadGraph graph, Throwable throwable) {
1910 * Implementation of the interface RequestProcessor
1914 public <T> T syncRequest(final Read<T> request) throws DatabaseException {
1916 assert (request != null);
1918 return QueryCache.resultReadEntry(this, request, parent, null, null);
1920 //return processor.query(this, request, parent, null, null);
1922 // if (parent != null) {
1925 // } catch (Throwable e) {
1926 // if(e instanceof DatabaseException) throw (DatabaseException)e;
1927 // else throw new DatabaseException(e);
1934 // return processor.tryQuery(this, request);
1936 // } catch (Throwable throwable) {
1938 // //Logger.defaultLogError("Internal read request failure", throwable);
1940 // if (throwable instanceof DatabaseException)
1941 // throw (DatabaseException) throwable;
1943 // throw new DatabaseException(
1944 // "Unexpected exception in ReadGraph.syncRequest(Read,Procedure)",
1954 public <T> T syncRequest(Read<T> request, SyncListener<T> procedure)
1955 throws DatabaseException {
1956 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
1960 public <T> T syncRequest(Read<T> request, final Listener<T> procedure)
1961 throws DatabaseException {
1962 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
1966 public <T> T syncRequest(final Read<T> request, final AsyncProcedure<T> procedure) throws DatabaseException {
1968 assert (request != null);
1970 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
1972 return QueryCache.resultReadEntry(this, request, parent, listener, procedure);
1974 // return processor.query(this, request, parent, procedure, listener);
1977 // if (parent != null || listener != null) {
1980 // } catch (Throwable e) {
1981 // if(e instanceof DatabaseException) throw (DatabaseException)e;
1982 // else throw new DatabaseException(e);
1989 // T t = processor.tryQuery(this, request);
1990 // if(procedure != null)
1991 // procedure.execute(this, t);
1995 // } catch (Throwable throwable) {
1997 // Logger.defaultLogError("Internal read request failure", throwable);
1999 // if(procedure != null)
2000 // procedure.exception(this, throwable);
2002 // if (throwable instanceof DatabaseException)
2003 // throw (DatabaseException) throwable;
2005 // throw new DatabaseException(
2006 // "Unexpected exception in ReadGraph.syncRequest(Read,Procedure)",
2016 public <T> T syncRequest(final Read<T> request,
2017 final SyncProcedure<T> procedure) throws DatabaseException {
2018 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2022 public <T> T syncRequest(Read<T> request, Procedure<T> procedure)
2023 throws DatabaseException {
2024 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2027 static class AsyncReadProcedure<T> implements AsyncProcedure<T> {
2029 private static Throwable DONE = new Throwable();
2032 Throwable exception = null;
2035 public void execute(AsyncReadGraph graph, T t) {
2041 public void exception(AsyncReadGraph graph, Throwable t) {
2045 public void checkAndThrow() throws DatabaseException {
2046 if(exception != DONE) {
2047 if (exception instanceof DatabaseException)
2048 throw (DatabaseException) exception;
2050 throw new DatabaseException(
2051 "Unexpected exception in ReadGraph.syncRequest(AsyncRead)",
2056 public boolean done() {
2057 return exception != null;
2063 public <T> T syncRequest(final AsyncRead<T> request)
2064 throws DatabaseException {
2066 assert (request != null);
2067 // AsyncReadProcedure<T> procedure = new AsyncReadProcedure<T>();
2068 BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(null, request);
2069 syncRequest(request, ap);
2071 // procedure.checkAndThrow();
2072 // return procedure.result;
2074 // return syncRequest(request, new AsyncProcedureAdapter<T>());
2079 public <T> T syncRequest(AsyncRead<T> request, AsyncListener<T> procedure)
2080 throws DatabaseException {
2081 return syncRequest(request, (AsyncProcedure<T>) procedure);
2085 public <T> T syncRequest(AsyncRead<T> request, SyncListener<T> procedure)
2086 throws DatabaseException {
2087 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
2091 public <T> T syncRequest(AsyncRead<T> request, Listener<T> procedure)
2092 throws DatabaseException {
2093 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
2097 final public <T> T syncRequest(final AsyncRead<T> request,
2098 final AsyncProcedure<T> procedure) throws DatabaseException {
2100 assert (request != null);
2102 ListenerBase listener = getListenerBase(procedure);
2104 BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(procedure, request);
2106 // final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
2107 // procedure, request);
2109 QueryCache.runnerAsyncReadEntry(this, request, parent, listener, ap, true);
2111 //processor.query(this, request, parent, wrapper, listener);
2115 // return wrapper.getResult();
2117 // if (parent != null || listener != null || ((request.getFlags() & RequestFlags.SCHEDULE) > 0)) {
2119 //// Object syncParent = request;
2121 //// final ReadGraphImpl newGraph = newSync();
2124 //// newGraph.waitAsync(syncParent);
2126 // Throwable e = wrapper.getException();
2128 // // The request was async - produce meaningful stack trace by
2130 // if (e instanceof DatabaseException)
2131 // throw (DatabaseException) e;
2133 // throw new DatabaseException(e);
2136 // return wrapper.getResult();
2140 // // System.out.println("direct call " + request );
2142 // // Do not set the sync state.parent for external threads
2143 //// Object syncParent = request;
2145 //// final ReadGraphImpl newGraph = newSync();
2147 // final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
2148 // procedure, request);
2152 // processor.tryQuery(this, request, wrapper);
2154 // } catch (Throwable t) {
2156 // wrapper.exception(this, t);
2160 // Throwable e = wrapper.getException();
2162 // // The request was async - produce meaningful stack trace by
2164 // if (e instanceof DatabaseException)
2165 // throw (DatabaseException) e;
2167 // throw new DatabaseException(e);
2170 // return wrapper.getResult();
2176 final private <T> void syncRequest(final AsyncRead<T> request, final AsyncReadProcedure<T> procedure) throws DatabaseException {
2178 assert (request != null);
2180 ListenerBase listener = getListenerBase(procedure);
2181 assert(listener == null);
2183 BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(procedure, request);
2185 // final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
2186 // procedure, request);
2188 QueryCache.runnerAsyncReadEntry(this, request, parent, listener, ap, true);
2195 public <T> T syncRequest(AsyncRead<T> request,
2196 final SyncProcedure<T> procedure) throws DatabaseException {
2197 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2201 final public <T> T syncRequest(final AsyncRead<T> request,
2202 final Procedure<T> procedure) throws DatabaseException {
2203 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2207 public <T> Collection<T> syncRequest(final MultiRead<T> request)
2208 throws DatabaseException {
2210 assert (request != null);
2212 final ArrayList<T> result = new ArrayList<T>();
2213 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2215 syncRequest(request, new AsyncMultiProcedure<T>() {
2218 public void execute(AsyncReadGraph graph, T t) {
2219 synchronized (result) {
2225 public void finished(AsyncReadGraph graph) {
2229 public void exception(AsyncReadGraph graph, Throwable t) {
2234 public String toString() {
2235 return "syncRequest(MultiRead) -> " + request;
2240 Throwable t = exception.get();
2242 if (t instanceof DatabaseException)
2243 throw (DatabaseException) t;
2245 throw new DatabaseException(
2246 "Unexpected exception in ReadGraph.syncRequest(Read)",
2255 public <T> Collection<T> syncRequest(MultiRead<T> request,
2256 AsyncMultiListener<T> procedure) {
2257 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2261 public <T> Collection<T> syncRequest(MultiRead<T> request,
2262 SyncMultiListener<T> procedure) {
2263 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2267 public <T> Collection<T> syncRequest(MultiRead<T> request,
2268 MultiListener<T> procedure) {
2269 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2273 public <T> Collection<T> syncRequest(MultiRead<T> request,
2274 AsyncMultiProcedure<T> procedure) {
2276 assert (request != null);
2278 ListenerBase listener = getListenerBase(procedure);
2280 if (parent != null || listener != null) {
2282 // Object syncParent = request;
2284 // final ReadGraphImpl newGraph = newSync();
2286 processor.query(this, request, parent, procedure, listener);
2288 // newGraph.waitAsync(syncParent);
2292 // Object syncParent = request;
2294 // final ReadGraphImpl newGraph = newSync();
2296 final ResultCallWrappedQueryProcedure4<T> wrapper = new ResultCallWrappedQueryProcedure4<T>(procedure);
2300 request.perform(this, wrapper);
2302 } catch (Throwable t) {
2304 wrapper.exception(this, t);
2305 // newGraph.waitAsync(syncParent);
2317 public <T> Collection<T> syncRequest(MultiRead<T> request,
2318 SyncMultiProcedure<T> procedure) {
2319 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2323 public <T> Collection<T> syncRequest(MultiRead<T> request,
2324 MultiProcedure<T> procedure) {
2325 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2328 static class AsyncMultiReadProcedure<T> extends ArrayList<T> implements AsyncMultiProcedure<T> {
2330 private static Throwable DONE = new Throwable();
2332 private static final long serialVersionUID = -6494230465108115812L;
2334 Throwable exception = null;
2337 public synchronized void execute(AsyncReadGraph graph, T t) {
2342 public void finished(AsyncReadGraph graph) {
2347 public void exception(AsyncReadGraph graph, Throwable t) {
2351 public void checkAndThrow() throws DatabaseException {
2352 if(exception != DONE) {
2353 if (exception instanceof DatabaseException)
2354 throw (DatabaseException) exception;
2356 throw new DatabaseException(
2357 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
2362 public boolean done() {
2363 return exception != null;
2369 final public <T> Collection<T> syncRequest(AsyncMultiRead<T> request)
2370 throws DatabaseException {
2372 assert (request != null);
2374 final AsyncMultiReadProcedure<T> procedure = new AsyncMultiReadProcedure<T>();
2376 syncRequest(request, procedure);
2378 procedure.checkAndThrow();
2384 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2385 AsyncMultiListener<T> procedure) {
2386 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2390 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2391 SyncMultiListener<T> procedure) {
2392 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2396 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2397 MultiListener<T> procedure) {
2398 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2401 final private <T> void syncRequest(final AsyncMultiRead<T> request,
2402 final AsyncMultiReadProcedure<T> procedure) {
2404 assert (request != null);
2405 assert (procedure != null);
2407 ListenerBase listener = getListenerBase(procedure);
2409 if (parent != null || listener != null) {
2411 // Object syncParent = request;
2413 // final ReadGraphImpl newGraph = newSync();
2415 processor.query(this, request, parent, procedure, listener);
2417 // newGraph.waitAsync(syncParent);
2418 waitAsyncProcedure(procedure);
2422 // Object syncParent = callerThread == Integer.MIN_VALUE ? null
2425 // final ReadGraphImpl newGraph = newSyncAsync(syncParent);
2430 // ReadGraphImpl sync = newSync();
2431 request.perform(this, procedure);
2432 // sync.waitAsync(null);
2433 waitAsyncProcedure(procedure);
2436 } catch (Throwable t) {
2438 waitAsyncProcedure(procedure);
2449 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2450 final AsyncMultiProcedure<T> procedure) {
2452 assert (request != null);
2453 assert (procedure != null);
2455 ListenerBase listener = getListenerBase(procedure);
2457 if (parent != null || listener != null) {
2459 // Object syncParent = request;
2461 // final ReadGraphImpl newGraph = newSync();
2463 processor.query(this, request, parent, procedure, listener);
2465 // newGraph.waitAsync(syncParent);
2469 // Object syncParent = request;
2471 // final ReadGraphImpl newGraph = newSync();
2475 request.perform(this, new AsyncMultiProcedure<T>() {
2478 public void execute(AsyncReadGraph graph, T result) {
2479 procedure.execute(graph, result);
2483 public void finished(AsyncReadGraph graph) {
2484 procedure.finished(graph);
2488 public void exception(AsyncReadGraph graph, Throwable t) {
2489 procedure.exception(graph, t);
2493 public String toString() {
2494 return "syncRequest(AsyncMultiRead) -> " + procedure;
2499 } catch (Throwable t) {
2511 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2512 final SyncMultiProcedure<T> procedure) {
2513 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2517 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2518 final MultiProcedure<T> procedure) {
2519 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2523 public <T> T syncRequest(final ExternalRead<T> request)
2524 throws DatabaseException {
2526 assert (request != null);
2528 return syncRequest(request, new Procedure<T>() {
2531 public void execute(T t) {
2535 public void exception(Throwable t) {
2539 public String toString() {
2540 return "syncRequest(AsyncRead) -> " + request;
2548 public <T> T syncRequest(ExternalRead<T> request, Listener<T> procedure) throws DatabaseException {
2549 return syncRequest(request, (Procedure<T>) procedure);
2553 final public <T> T syncRequest(final ExternalRead<T> request,
2554 final Procedure<T> procedure) throws DatabaseException {
2556 assert (request != null);
2558 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
2559 return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
2562 // ListenerBase listener = getListenerBase(procedure);
2564 // return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
2566 // assert (request != null);
2568 // ListenerBase listener = getListenerBase(procedure);
2570 // final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2571 // final DataContainer<T> result = new DataContainer<T>();
2573 // processor.query(this, request, parent, new Procedure<T>() {
2576 // public void exception(Throwable throwable) {
2577 // exception.set(throwable);
2578 // procedure.exception(throwable);
2582 // public void execute(T t) {
2584 // procedure.execute(t);
2591 // if (parent != null || listener != null) {
2593 //// final ReadGraphImpl newGraph = newSync();
2596 //// newGraph.waitAsync(request);
2602 // T t = processor.tryQuery(this, request);
2604 // procedure.execute(t);
2606 // } catch (Throwable t) {
2608 // if (t instanceof DatabaseException) {
2609 // exception.set((DatabaseException)t);
2610 // procedure.exception(exception.get());
2612 // exception.set(new DatabaseException(
2613 // "Unexpected exception in ReadGraph.syncRequest(Read)",
2615 // procedure.exception(exception.get());
2622 // Throwable t = exception.get();
2624 // if (t instanceof DatabaseException)
2625 // throw (DatabaseException) t;
2627 // throw new DatabaseException(
2628 // "Unexpected exception in ReadGraph.syncRequest(Read)",
2632 // return result.get();
2637 public void syncRequest(final Write request) throws DatabaseException {
2639 assert (request != null);
2641 throw new DatabaseException(
2642 "Write operations are not supported during read transactions!");
2647 public <T> T syncRequest(final WriteResult<T> request) throws DatabaseException {
2649 assert (request != null);
2651 throw new DatabaseException(
2652 "Write operations are not supported during read transactions!");
2657 public void syncRequest(final DelayedWrite request)
2658 throws DatabaseException {
2660 assert (request != null);
2662 throw new DatabaseException(
2663 "Write operations are not supported during read transactions!");
2668 public <T> T syncRequest(final DelayedWriteResult<T> request) throws DatabaseException {
2670 assert (request != null);
2672 throw new DatabaseException(
2673 "Write operations are not supported during read transactions!");
2678 public void syncRequest(final WriteOnly request) throws DatabaseException {
2680 assert (request != null);
2682 throw new DatabaseException(
2683 "Write operations are not supported during read transactions!");
2688 public <T> T syncRequest(final WriteOnlyResult<T> request) throws DatabaseException {
2690 assert (request != null);
2692 throw new DatabaseException(
2693 "Write operations are not supported during read transactions!");
2698 public <T> void async(ReadInterface<T> r, AsyncProcedure<T> procedure) {
2699 r.request(this, procedure);
2703 public <T> void async(ReadInterface<T> r, Procedure<T> procedure) {
2704 r.request(this, procedure);
2708 public <T> void async(ReadInterface<T> r, SyncProcedure<T> procedure) {
2709 r.request(this, procedure);
2713 public <T> void async(ReadInterface<T> r, AsyncListener<T> procedure) {
2714 r.request(this, procedure);
2718 public <T> void async(ReadInterface<T> r, Listener<T> procedure) {
2719 r.request(this, procedure);
2723 public <T> void async(ReadInterface<T> r, SyncListener<T> procedure) {
2724 r.request(this, procedure);
2728 public <T> T sync(ReadInterface<T> r) throws DatabaseException {
2729 return r.request(this);
2733 public <T> T sync(WriteInterface<T> r) throws DatabaseException {
2734 return r.request(this);
2738 public <T> void async(WriteInterface<T> r, Procedure<T> procedure) {
2739 r.request(this, procedure);
2743 public <T> void async(WriteInterface<T> r) {
2744 r.request(this, new ProcedureAdapter<T>());
2748 * Implementation of the interface AsyncReadGraph
2752 public void forURI(Resource resource, AsyncListener<String> listener) {
2753 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2758 public void forURI(Resource resource, SyncListener<String> listener) {
2759 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2764 public void forURI(Resource resource, Listener<String> listener) {
2765 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2770 final public void forURI(final Resource resource,
2771 final AsyncProcedure<String> procedure) {
2773 assert (resource != null);
2774 assert (procedure != null);
2776 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2782 public void forURI(Resource resource, SyncProcedure<String> procedure) {
2783 forURI(resource, new SyncToAsyncProcedure<String>(procedure));
2787 public void forURI(Resource resource, Procedure<String> procedure) {
2788 forURI(resource, new NoneToAsyncProcedure<String>(procedure));
2792 public void forResource(String id, AsyncListener<Resource> listener) {
2793 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2798 public void forResource(String id, SyncListener<Resource> listener) {
2799 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2804 public void forResource(String id, Listener<Resource> listener) {
2805 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2810 final public void forResource(final String id,
2811 final AsyncProcedure<Resource> procedure) {
2813 assert (id != null);
2814 assert (procedure != null);
2816 processor.forResource(this, id, procedure);
2821 public void forResource(String id, SyncProcedure<Resource> procedure) {
2822 forResource(id, new SyncToAsyncProcedure<Resource>(procedure));
2826 public void forResource(String id, Procedure<Resource> procedure) {
2827 forResource(id, new NoneToAsyncProcedure<Resource>(procedure));
2831 public void forBuiltin(String id, AsyncListener<Resource> listener) {
2832 asyncRequest(new Builtin(id), listener);
2836 public void forBuiltin(String id, SyncListener<Resource> listener) {
2837 asyncRequest(new Builtin(id), listener);
2841 public void forBuiltin(String id, Listener<Resource> listener) {
2842 asyncRequest(new Builtin(id), listener);
2846 final public void forBuiltin(final String id,
2847 final AsyncProcedure<Resource> procedure) {
2849 assert (id != null);
2850 assert (procedure != null);
2852 processor.forBuiltin(this, id, procedure);
2857 public void forBuiltin(String id, SyncProcedure<Resource> procedure) {
2858 forBuiltin(id, new SyncToAsyncProcedure<Resource>(procedure));
2862 public void forBuiltin(String id, Procedure<Resource> procedure) {
2863 forBuiltin(id, new NoneToAsyncProcedure<Resource>(procedure));
2867 final public void forEachStatement(Resource subject, Resource relation,
2868 AsyncMultiProcedure<Statement> procedure) {
2870 assert (subject != null);
2871 assert (relation != null);
2872 assert (procedure != null);
2874 processor.forEachStatement(this, subject, relation, procedure);
2879 public void forEachStatement(Resource subject, Resource relation,
2880 SyncMultiProcedure<Statement> procedure) {
2881 forEachStatement(subject, relation,
2882 new SyncToAsyncMultiProcedure<Statement>(procedure));
2886 final public void forEachStatement(Resource subject, Resource relation,
2887 MultiProcedure<Statement> procedure) {
2889 assert (subject != null);
2890 assert (relation != null);
2891 assert (procedure != null);
2893 processor.forEachStatement(this, subject, relation, procedure);
2898 final public void forStatementSet(Resource subject, Resource relation,
2899 AsyncSetListener<Statement> procedure) {
2901 assert (subject != null);
2902 assert (relation != null);
2903 assert (procedure != null);
2905 processor.forStatementSet(this, subject, relation, procedure);
2910 final public void forStatementSet(Resource subject, Resource relation,
2911 SyncSetListener<Statement> procedure) {
2912 forStatementSet(subject, relation,
2913 new SyncToAsyncSetProcedure<Statement>(procedure));
2917 public void forStatementSet(Resource subject, Resource relation,
2918 SetListener<Statement> listener) {
2919 forStatementSet(subject, relation,
2920 new NoneToAsyncSetProcedure<Statement>(listener));
2924 final public void forEachAssertedStatement(final Resource subject,
2925 final Resource relation,
2926 final AsyncMultiProcedure<Statement> procedure) {
2928 assert (subject != null);
2929 assert (relation != null);
2930 assert (procedure != null);
2932 processor.forEachAssertedStatement(this, subject, relation, procedure);
2937 public void forEachAssertedStatement(Resource subject, Resource relation,
2938 SyncMultiProcedure<Statement> procedure) {
2939 forEachAssertedStatement(subject, relation,
2940 new SyncToAsyncMultiProcedure<Statement>(procedure));
2944 public void forEachAssertedStatement(Resource subject, Resource relation,
2945 MultiProcedure<Statement> procedure) {
2946 forEachAssertedStatement(subject, relation,
2947 new NoneToAsyncMultiProcedure<Statement>(procedure));
2951 public void forAssertedStatementSet(Resource subject, Resource relation,
2952 AsyncSetListener<Statement> procedure) {
2954 assert (subject != null);
2955 assert (relation != null);
2956 assert (procedure != null);
2958 processor.forAssertedStatementSet(this, subject, relation, procedure);
2963 public void forAssertedStatementSet(Resource subject, Resource relation,
2964 SyncSetListener<Statement> procedure) {
2966 assert (subject != null);
2967 assert (relation != null);
2968 assert (procedure != null);
2970 forAssertedStatementSet(subject, relation,
2971 new SyncToAsyncSetProcedure<Statement>(procedure));
2976 public void forAssertedStatementSet(Resource subject, Resource relation,
2977 SetListener<Statement> procedure) {
2979 assert (subject != null);
2980 assert (relation != null);
2981 assert (procedure != null);
2983 forAssertedStatementSet(subject, relation,
2984 new NoneToAsyncSetProcedure<Statement>(procedure));
2989 final public void forEachPredicate(final Resource subject,
2990 final AsyncMultiProcedure<Resource> procedure) {
2992 assert (subject != null);
2993 assert (procedure != null);
2995 processor.forEachPredicate(this, subject, procedure);
3000 public void forEachPredicate(Resource subject,
3001 SyncMultiProcedure<Resource> procedure) {
3002 forEachPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(
3007 final public void forEachPredicate(final Resource subject,
3008 final MultiProcedure<Resource> procedure) {
3010 assert (subject != null);
3011 assert (procedure != null);
3013 processor.forEachPredicate(this, subject, procedure);
3018 final public void forPredicateSet(final Resource subject,
3019 final AsyncSetListener<Resource> procedure) {
3021 assert (subject != null);
3022 assert (procedure != null);
3024 processor.forPredicateSet(this, subject, procedure);
3029 final public void forPredicateSet(final Resource subject,
3030 final SyncSetListener<Resource> procedure) {
3032 assert (subject != null);
3033 assert (procedure != null);
3035 forPredicateSet(subject, new SyncToAsyncSetProcedure<Resource>(
3041 final public void forPredicateSet(final Resource subject,
3042 final SetListener<Resource> procedure) {
3044 assert (subject != null);
3045 assert (procedure != null);
3047 forPredicateSet(subject, new NoneToAsyncSetProcedure<Resource>(
3053 final public void forEachPrincipalType(final Resource subject,
3054 final AsyncMultiProcedure<Resource> procedure) {
3056 assert (subject != null);
3057 assert (procedure != null);
3059 processor.forEachPrincipalType(this, subject, procedure);
3064 public void forEachPrincipalType(Resource subject,
3065 SyncMultiProcedure<Resource> procedure) {
3066 forEachPrincipalType(subject, new SyncToAsyncMultiProcedure<Resource>(
3071 final public void forEachPrincipalType(final Resource subject,
3072 final MultiProcedure<Resource> procedure) {
3074 assert (subject != null);
3075 assert (procedure != null);
3077 processor.forEachPrincipalType(this, subject, procedure);
3082 final public void forPrincipalTypeSet(final Resource subject,
3083 final AsyncSetListener<Resource> procedure) {
3085 assert (subject != null);
3086 assert (procedure != null);
3088 processor.forPrincipalTypeSet(this, subject, procedure);
3093 final public void forPrincipalTypeSet(final Resource subject,
3094 final SyncSetListener<Resource> procedure) {
3096 assert (subject != null);
3097 assert (procedure != null);
3099 forPrincipalTypeSet(subject, new SyncToAsyncSetProcedure<Resource>(
3105 final public void forPrincipalTypeSet(final Resource subject,
3106 final SetListener<Resource> procedure) {
3108 assert (subject != null);
3109 assert (procedure != null);
3111 forPrincipalTypeSet(subject, new NoneToAsyncSetProcedure<Resource>(
3117 public void forTypes(Resource subject, AsyncListener<Set<Resource>> listener) {
3118 asyncRequest(new Types(subject), listener);
3122 public void forTypes(Resource subject, SyncListener<Set<Resource>> listener) {
3123 asyncRequest(new Types(subject), listener);
3127 public void forTypes(Resource subject, Listener<Set<Resource>> listener) {
3128 asyncRequest(new Types(subject), listener);
3132 final public void forTypes(final Resource subject,
3133 final AsyncProcedure<Set<Resource>> procedure) {
3135 assert (subject != null);
3136 assert (procedure != null);
3138 processor.forTypes(this, subject, procedure);
3143 public void forTypes(Resource subject,
3144 SyncProcedure<Set<Resource>> procedure) {
3145 forTypes(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
3149 public void forTypes(Resource subject, Procedure<Set<Resource>> procedure) {
3150 forTypes(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
3154 public void forSupertypes(Resource subject,
3155 AsyncListener<Set<Resource>> listener) {
3156 asyncRequest(new Types(subject), listener);
3160 public void forSupertypes(Resource subject,
3161 SyncListener<Set<Resource>> listener) {
3162 asyncRequest(new Types(subject), listener);
3166 public void forSupertypes(Resource subject, Listener<Set<Resource>> listener) {
3167 asyncRequest(new Types(subject), listener);
3171 final public void forSupertypes(final Resource subject,
3172 final AsyncProcedure<Set<Resource>> procedure) {
3174 assert (subject != null);
3175 assert (procedure != null);
3177 processor.forSupertypes(this, subject, procedure);
3182 public void forSupertypes(Resource subject,
3183 SyncProcedure<Set<Resource>> procedure) {
3184 forSupertypes(subject, new SyncToAsyncProcedure<Set<Resource>>(
3189 public void forSupertypes(Resource subject,
3190 Procedure<Set<Resource>> procedure) {
3191 forSupertypes(subject, new NoneToAsyncProcedure<Set<Resource>>(
3196 public void forDirectSuperrelations(Resource subject,
3197 AsyncMultiProcedure<Resource> procedure) {
3199 assert (subject != null);
3200 assert (procedure != null);
3202 processor.forDirectSuperrelations(this, subject, procedure);
3207 public void forPossibleSuperrelation(Resource subject, AsyncProcedure<Resource> procedure) {
3209 assert (subject != null);
3210 assert (procedure != null);
3212 processor.forPossibleSuperrelation(this, subject, procedure);
3217 public void forSuperrelations(Resource subject,
3218 AsyncListener<Set<Resource>> listener) {
3219 asyncRequest(new Types(subject), listener);
3223 public void forSuperrelations(Resource subject,
3224 SyncListener<Set<Resource>> listener) {
3225 asyncRequest(new Types(subject), listener);
3229 public void forSuperrelations(Resource subject,
3230 Listener<Set<Resource>> listener) {
3231 asyncRequest(new Types(subject), listener);
3235 final public void forSuperrelations(final Resource subject,
3236 final AsyncProcedure<Set<Resource>> procedure) {
3238 assert (subject != null);
3239 assert (procedure != null);
3241 processor.forSuperrelations(this, subject, procedure);
3246 public void forSuperrelations(Resource subject,
3247 SyncProcedure<Set<Resource>> procedure) {
3248 forSuperrelations(subject, new SyncToAsyncProcedure<Set<Resource>>(
3253 public void forSuperrelations(Resource subject,
3254 Procedure<Set<Resource>> procedure) {
3255 forSuperrelations(subject, new NoneToAsyncProcedure<Set<Resource>>(
3260 final public void forEachObject(final Resource subject, final Resource relation, final AsyncMultiProcedure<Resource> procedure) {
3261 processor.forEachObject(this, subject, relation, procedure);
3265 public void forEachObject(Resource subject, Resource relation,
3266 SyncMultiProcedure<Resource> procedure) {
3267 forEachObject(subject, relation,
3268 new SyncToAsyncMultiProcedure<Resource>(procedure));
3272 public void forEachObject(Resource subject, Resource relation,
3273 MultiProcedure<Resource> procedure) {
3275 processor.forEachObject(this, subject, relation, procedure);
3280 // final public void forEachDirectObject(final Resource subject,
3281 // final Resource relation,
3282 // final AsyncMultiProcedure<Resource> procedure) {
3284 // processor.forEachDirectObject(this, subject, relation, procedure);
3289 // public void forEachDirectObject(Resource subject, Resource relation,
3290 // SyncMultiProcedure<Resource> procedure) {
3291 // forEachDirectObject(subject, relation,
3292 // new SyncToAsyncMultiProcedure<Resource>(procedure));
3296 // public void forEachDirectObject(Resource subject, Resource relation,
3297 // MultiProcedure<Resource> procedure) {
3298 // forEachDirectObject(subject, relation,
3299 // new NoneToAsyncMultiProcedure<Resource>(procedure));
3303 final public void forEachDirectPredicate(final Resource subject, final AsyncMultiProcedure<Resource> procedure) {
3304 processor.forEachDirectPredicate(this, subject, procedure);
3308 public void forEachDirectPredicate(Resource subject, SyncMultiProcedure<Resource> procedure) {
3309 forEachDirectPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(procedure));
3313 public void forEachDirectPredicate(Resource subject, MultiProcedure<Resource> procedure) {
3314 forEachDirectPredicate(subject, new NoneToAsyncMultiProcedure<Resource>(procedure));
3318 final public void forObjectSet(final Resource subject,
3319 final Resource relation, final AsyncSetListener<Resource> procedure) {
3321 assert (subject != null);
3322 assert (relation != null);
3323 assert (procedure != null);
3325 processor.forObjectSet(this, subject, relation, procedure);
3330 final public void forObjectSet(final Resource subject,
3331 final Resource relation, final SyncSetListener<Resource> procedure) {
3333 assert (subject != null);
3334 assert (relation != null);
3335 assert (procedure != null);
3337 forObjectSet(subject, relation, new SyncToAsyncSetProcedure<Resource>(
3343 final public void forObjectSet(final Resource subject,
3344 final Resource relation, final SetListener<Resource> procedure) {
3346 assert (subject != null);
3347 assert (relation != null);
3348 assert (procedure != null);
3350 forObjectSet(subject, relation, new NoneToAsyncSetProcedure<Resource>(
3356 final public void forEachAssertedObject(final Resource subject,
3357 final Resource relation,
3358 final AsyncMultiProcedure<Resource> procedure) {
3360 assert (subject != null);
3361 assert (relation != null);
3362 assert (procedure != null);
3364 processor.forEachAssertedObject(this, subject, relation, procedure);
3369 public void forEachAssertedObject(Resource subject, Resource relation,
3370 SyncMultiProcedure<Resource> procedure) {
3372 assert (subject != null);
3373 assert (relation != null);
3374 assert (procedure != null);
3376 forEachAssertedObject(subject, relation,
3377 new SyncToAsyncMultiProcedure<Resource>(procedure));
3382 public void forEachAssertedObject(Resource subject, Resource relation,
3383 MultiProcedure<Resource> procedure) {
3385 assert (subject != null);
3386 assert (relation != null);
3387 assert (procedure != null);
3389 forEachAssertedObject(subject, relation,
3390 new NoneToAsyncMultiProcedure<Resource>(procedure));
3395 public void forAssertedObjectSet(Resource subject, Resource relation,
3396 AsyncSetListener<Resource> procedure) {
3398 assert (subject != null);
3399 assert (relation != null);
3400 assert (procedure != null);
3402 processor.forAssertedObjectSet(this, subject, relation, procedure);
3407 public void forAssertedObjectSet(Resource subject, Resource relation,
3408 SyncSetListener<Resource> procedure) {
3410 assert (subject != null);
3411 assert (relation != null);
3412 assert (procedure != null);
3414 forAssertedObjectSet(subject, relation,
3415 new SyncToAsyncSetProcedure<Resource>(procedure));
3420 public void forAssertedObjectSet(Resource subject, Resource relation,
3421 SetListener<Resource> procedure) {
3423 assert (subject != null);
3424 assert (relation != null);
3425 assert (procedure != null);
3427 forAssertedObjectSet(subject, relation,
3428 new NoneToAsyncSetProcedure<Resource>(procedure));
3433 public void forInverse(Resource relation, AsyncListener<Resource> listener) {
3434 asyncRequest(new Inverse(relation), listener);
3438 public void forInverse(Resource relation, SyncListener<Resource> listener) {
3439 asyncRequest(new Inverse(relation), listener);
3443 public void forInverse(Resource relation, Listener<Resource> listener) {
3444 asyncRequest(new Inverse(relation), listener);
3448 final public void forInverse(final Resource relation,
3449 final AsyncProcedure<Resource> procedure) {
3451 assert (relation != null);
3452 assert (procedure != null);
3454 processor.forInverse(this, relation, new AsyncProcedure<Resource>() {
3457 public void execute(AsyncReadGraph graph, Resource result) {
3459 procedure.execute(graph, result);
3461 procedure.exception(graph, new NoInverseException(relation
3467 public void exception(AsyncReadGraph graph, Throwable throwable) {
3468 procedure.exception(graph, throwable);
3472 public String toString() {
3473 return "forInverse -> " + procedure;
3481 public void forInverse(Resource relation, SyncProcedure<Resource> procedure) {
3482 forInverse(relation, new SyncToAsyncProcedure<Resource>(procedure));
3486 public void forInverse(Resource relation, Procedure<Resource> procedure) {
3487 forInverse(relation, new NoneToAsyncProcedure<Resource>(procedure));
3491 public void forSingleObject(Resource subject, Resource relation,
3492 AsyncListener<Resource> listener) {
3493 asyncRequest(new SingleObject(subject, relation), listener);
3497 public void forSingleObject(Resource subject, Resource relation,
3498 SyncListener<Resource> listener) {
3499 asyncRequest(new SingleObject(subject, relation), listener);
3503 public void forSingleObject(Resource subject, Resource relation,
3504 Listener<Resource> listener) {
3505 asyncRequest(new SingleObject(subject, relation), listener);
3509 final public void forSingleObject(final Resource subject,
3510 final Resource relation, final AsyncProcedure<Resource> procedure) {
3512 assert (subject != null);
3513 assert (relation != null);
3514 assert (procedure != null);
3516 processor.forEachObject(this, subject, relation,
3517 new SingleOrErrorProcedure<Resource>(procedure));
3522 public void forSingleObject(Resource subject, Resource relation,
3523 SyncProcedure<Resource> procedure) {
3524 forSingleObject(subject, relation, new SyncToAsyncProcedure<Resource>(
3529 public void forSingleObject(Resource subject, Resource relation,
3530 Procedure<Resource> procedure) {
3531 forSingleObject(subject, relation, new NoneToAsyncProcedure<Resource>(
3536 public void forSingleStatement(Resource subject, Resource relation,
3537 AsyncListener<Statement> listener) {
3538 asyncRequest(new SingleStatement(subject, relation), listener);
3542 public void forSingleStatement(Resource subject, Resource relation,
3543 SyncListener<Statement> listener) {
3544 asyncRequest(new SingleStatement(subject, relation), listener);
3548 public void forSingleStatement(Resource subject, Resource relation,
3549 Listener<Statement> listener) {
3550 asyncRequest(new SingleStatement(subject, relation), listener);
3554 final public void forSingleStatement(final Resource subject,
3555 final Resource relation, final AsyncProcedure<Statement> procedure) {
3557 assert (subject != null);
3558 assert (relation != null);
3559 assert (procedure != null);
3561 processor.forEachStatement(this, subject, relation,
3562 new SingleOrErrorProcedure<Statement>(procedure));
3567 public void forSingleStatement(Resource subject, Resource relation,
3568 SyncProcedure<Statement> procedure) {
3569 forSingleStatement(subject, relation,
3570 new SyncToAsyncProcedure<Statement>(procedure));
3574 public void forSingleStatement(Resource subject, Resource relation,
3575 Procedure<Statement> procedure) {
3576 forSingleStatement(subject, relation,
3577 new NoneToAsyncProcedure<Statement>(procedure));
3581 public void forSingleType(Resource subject,
3582 AsyncListener<Resource> listener) {
3583 asyncRequest(new SingleTypeAny(subject), listener);
3587 public void forSingleType(Resource subject,
3588 SyncListener<Resource> listener) {
3589 asyncRequest(new SingleTypeAny(subject), listener);
3593 public void forSingleType(Resource subject,
3594 Listener<Resource> listener) {
3595 asyncRequest(new SingleTypeAny(subject), listener);
3599 final public void forSingleType(final Resource subject, final AsyncProcedure<Resource> procedure) {
3601 assert (subject != null);
3602 assert (procedure != null);
3604 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3606 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
3609 public void execute(AsyncReadGraph graph, final Resource principalType) {
3610 checkedProcedure.offer(graph, principalType);
3614 public void finished(AsyncReadGraph graph) {
3615 checkedProcedure.dec(graph);
3619 public void exception(AsyncReadGraph graph, Throwable t) {
3620 checkedProcedure.exception(graph, t);
3624 public String toString() {
3625 return "forSingleType -> " + procedure;
3633 public void forSingleType(Resource subject, SyncProcedure<Resource> procedure) {
3634 forSingleType(subject, new SyncToAsyncProcedure<Resource>(
3639 public void forSingleType(Resource subject, Procedure<Resource> procedure) {
3640 forSingleType(subject, new NoneToAsyncProcedure<Resource>(
3645 public void forSingleType(Resource subject, Resource relation,
3646 AsyncListener<Resource> listener) {
3647 asyncRequest(new SingleType(subject, relation), listener);
3651 public void forSingleType(Resource subject, Resource relation,
3652 SyncListener<Resource> listener) {
3653 asyncRequest(new SingleType(subject, relation), listener);
3657 public void forSingleType(Resource subject, Resource relation,
3658 Listener<Resource> listener) {
3659 asyncRequest(new SingleType(subject, relation), listener);
3663 final public void forSingleType(final Resource subject,
3664 final Resource baseType, final AsyncProcedure<Resource> procedure) {
3666 assert (subject != null);
3667 assert (procedure != null);
3669 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3671 processor.forEachPrincipalType(this, subject,
3672 new AsyncMultiProcedureAdapter<Resource>() {
3675 public void execute(AsyncReadGraph graph,
3676 final Resource principalType) {
3678 checkedProcedure.inc();
3680 if(baseType == null) {
3682 checkedProcedure.offer(graph, principalType);
3683 checkedProcedure.dec(graph);
3685 } else if(principalType.equals(baseType)) {
3687 checkedProcedure.offer(graph, principalType);
3688 checkedProcedure.dec(graph);
3692 processor.forSupertypes((ReadGraphImpl)graph, principalType,
3693 new AsyncProcedure<Set<Resource>>() {
3696 public void execute(
3697 AsyncReadGraph graph,
3698 Set<Resource> result) {
3700 if (result.contains(baseType))
3701 checkedProcedure.offer(graph,
3703 checkedProcedure.dec(graph);
3708 public void exception(
3709 AsyncReadGraph graph,
3712 .exception(graph, t);
3722 public void finished(AsyncReadGraph graph) {
3723 checkedProcedure.dec(graph);
3727 public void exception(AsyncReadGraph graph, Throwable t) {
3728 checkedProcedure.exception(graph, t);
3732 public String toString() {
3733 return "forSingleType -> " + procedure;
3741 public void forSingleType(Resource subject, Resource relation,
3742 SyncProcedure<Resource> procedure) {
3743 forSingleType(subject, relation, new SyncToAsyncProcedure<Resource>(
3748 public void forSingleType(Resource subject, Resource relation,
3749 Procedure<Resource> procedure) {
3750 forSingleType(subject, relation, new NoneToAsyncProcedure<Resource>(
3755 public <T> void forValue(Resource subject, Binding binding,
3756 AsyncListener<T> listener) {
3757 asyncRequest(new Value<T>(subject, binding), listener);
3761 public <T> void forValue(Resource subject, Binding binding,
3762 SyncListener<T> listener) {
3763 asyncRequest(new Value<T>(subject, binding), listener);
3767 public <T> void forValue(Resource subject, Binding binding,
3768 Listener<T> listener) {
3769 asyncRequest(new Value<T>(subject, binding), listener);
3773 public <T> void forValue(final Resource resource, final Binding binding,
3774 final AsyncProcedure<T> procedure) {
3776 assert (resource != null);
3777 assert (binding != null);
3778 assert (procedure != null);
3780 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
3783 public void execute(AsyncReadGraph graph, byte[] result) {
3787 if (result == null) {
3788 procedure.exception(graph,
3789 new DoesNotContainValueException(
3790 "No value for resource " + resource));
3794 Serializer serializer = binding.serializer();
3795 // Serializer serializer = Bindings.getSerializer( binding );
3796 Object obj = serializer.deserialize(result);
3797 // if (!binding.isInstance(obj))
3798 // procedure.exception(graph, new ClassCastException(
3799 // "Cannot get value " + obj + " with binding "
3802 procedure.execute(graph, (T) obj);
3804 } catch (IOException e) {
3805 procedure.exception(graph, e);
3806 } catch (BufferUnderflowException e) {
3807 procedure.exception(graph, e);
3808 } catch (Throwable t) {
3809 procedure.exception(graph, t);
3815 public void exception(AsyncReadGraph graph, Throwable t) {
3817 procedure.exception(graph, t);
3818 } catch (Throwable t2) {
3819 Logger.defaultLogError(t2);
3824 public String toString() {
3825 return "forValue -> " + procedure;
3833 public <T> void forValue(Resource subject, Binding binding,
3834 SyncProcedure<T> procedure) {
3835 forValue(subject, binding, new SyncToAsyncProcedure<T>(procedure));
3839 public <T> void forValue(Resource subject, Binding binding,
3840 Procedure<T> procedure) {
3841 forValue(subject, binding, new NoneToAsyncProcedure<T>(procedure));
3845 public <T> void forValue(Resource subject, AsyncListener<T> listener) {
3846 asyncRequest(new ValueImplied<T>(subject), listener);
3850 public <T> void forValue(Resource subject, SyncListener<T> listener) {
3851 asyncRequest(new ValueImplied<T>(subject), listener);
3855 public <T> void forValue(Resource subject, Listener<T> listener) {
3856 asyncRequest(new ValueImplied<T>(subject), listener);
3860 final public <T> void forValue(final Resource subject, final AsyncProcedure<T> procedure) {
3862 assert (subject != null);
3863 assert (procedure != null);
3865 forRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
3868 public void execute(AsyncReadGraph graph, Datatype type) {
3869 // TODO: consider trying Bindings.getBeanBinding(type);
3870 Binding binding = Bindings.getBinding(type);
3871 graph.forValue(subject, binding, procedure);
3875 public void exception(AsyncReadGraph graph, Throwable throwable) {
3876 procedure.exception(graph, new DoesNotContainValueException("Invalid data type", throwable));
3884 public <T> void forValue(Resource subject, SyncProcedure<T> procedure) {
3885 forValue(subject, new SyncToAsyncProcedure<T>(procedure));
3889 public <T> void forValue(Resource subject, Procedure<T> procedure) {
3890 forValue(subject, new NoneToAsyncProcedure<T>(procedure));
3894 public <T> void forRelatedValue(Resource subject, Resource relation,
3895 AsyncListener<T> listener) {
3896 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3900 public <T> void forRelatedValue(Resource subject, Resource relation,
3901 SyncListener<T> listener) {
3902 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3906 public <T> void forRelatedValue(Resource subject, Resource relation,
3907 Listener<T> listener) {
3908 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3912 final public <T> void forRelatedValue(final Resource subject,
3913 final Resource relation, final AsyncProcedure<T> procedure) {
3915 assert (subject != null);
3916 assert (relation != null);
3917 assert (procedure != null);
3919 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3921 processor.forEachObject(this, subject, relation,
3922 new AsyncMultiProcedureAdapter<Resource>() {
3925 public void execute(AsyncReadGraph graph,
3926 final Resource object) {
3928 checkedProcedure.inc();
3930 graph.forValue(object, new AsyncProcedure<Object>() {
3933 public void execute(AsyncReadGraph graph,
3935 checkedProcedure.offer(graph, (T) result);
3936 checkedProcedure.dec(graph);
3940 public void exception(AsyncReadGraph graph,
3942 checkedProcedure.exception(graph, t);
3946 public String toString() {
3947 return "forRelatedValue -> " + procedure;
3955 public void finished(AsyncReadGraph graph) {
3956 checkedProcedure.dec(graph);
3960 public void exception(AsyncReadGraph graph, Throwable t) {
3961 checkedProcedure.exception(graph, t);
3969 public <T> void forRelatedValue(Resource subject, Resource relation,
3970 SyncProcedure<T> procedure) {
3971 forRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
3976 public <T> void forRelatedValue(Resource subject, Resource relation,
3977 Procedure<T> procedure) {
3978 forRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
3983 public <T> void forRelatedValue(Resource subject, Resource relation,
3984 Binding binding, AsyncListener<T> listener) {
3985 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3989 public <T> void forRelatedValue(Resource subject, Resource relation,
3990 Binding binding, SyncListener<T> listener) {
3991 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3995 public <T> void forRelatedValue(Resource subject, Resource relation,
3996 Binding binding, Listener<T> listener) {
3997 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
4001 final public <T> void forRelatedValue(final Resource subject,
4002 final Resource relation, final Binding binding,
4003 final AsyncProcedure<T> procedure) {
4005 assert (subject != null);
4006 assert (relation != null);
4007 assert (binding != null);
4008 assert (procedure != null);
4010 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
4012 processor.forEachObject(this, subject, relation,
4013 new AsyncMultiProcedureAdapter<Resource>() {
4016 public void execute(AsyncReadGraph graph,
4017 final Resource object) {
4019 checkedProcedure.inc();
4021 graph.forValue(object, binding, new AsyncProcedure<Object>() {
4024 public void execute(AsyncReadGraph graph,
4027 checkedProcedure.offer(graph,
4029 checkedProcedure.dec(graph);
4033 public void exception(AsyncReadGraph graph,
4035 checkedProcedure.exception(graph, t);
4039 public String toString() {
4040 return "forRelatedValue -> "
4049 public void finished(AsyncReadGraph graph) {
4050 checkedProcedure.dec(graph);
4054 public void exception(AsyncReadGraph graph, Throwable t) {
4055 checkedProcedure.exception(graph, t);
4063 public <T> void forRelatedValue(Resource subject, Resource relation,
4064 Binding binding, SyncProcedure<T> procedure) {
4065 forRelatedValue(subject, relation, binding,
4066 new SyncToAsyncProcedure<T>(procedure));
4070 public <T> void forRelatedValue(Resource subject, Resource relation,
4071 Binding binding, Procedure<T> procedure) {
4072 forRelatedValue(subject, relation, binding,
4073 new NoneToAsyncProcedure<T>(procedure));
4077 public <T> void forAdapted(Resource resource, Class<T> clazz,
4078 AsyncListener<T> listener) {
4079 asyncRequest(new Adapter<T>(resource, clazz), listener);
4083 public <T> void forAdapted(Resource resource, Class<T> clazz,
4084 SyncListener<T> listener) {
4085 asyncRequest(new Adapter<T>(resource, clazz), listener);
4089 public <T> void forAdapted(Resource resource, Class<T> clazz,
4090 Listener<T> listener) {
4091 asyncRequest(new Adapter<T>(resource, clazz), listener);
4095 final public <T> void forAdapted(final Resource resource,
4096 final Class<T> clazz, final AsyncProcedure<T> procedure) {
4098 assert (resource != null);
4099 assert (clazz != null);
4100 assert (procedure != null);
4102 final AdaptionService service = getSession().peekService(AdaptionService.class);
4103 if (service == null)
4104 procedure.exception(this, new ServiceException("No AdaptionService available"));
4106 service.adapt(this, resource, resource, Resource.class, clazz, false, procedure);
4111 public <T> void forAdapted(Resource resource, Class<T> clazz,
4112 SyncProcedure<T> procedure) {
4113 forAdapted(resource, clazz, new SyncToAsyncProcedure<T>(procedure));
4117 public <T> void forAdapted(Resource resource, Class<T> clazz,
4118 Procedure<T> procedure) {
4119 forAdapted(resource, clazz, new NoneToAsyncProcedure<T>(procedure));
4123 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
4124 AsyncListener<T> listener) {
4125 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
4129 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
4130 SyncListener<T> listener) {
4131 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
4135 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
4136 Listener<T> listener) {
4137 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
4141 final public <T> void forUniqueAdapted(final Resource resource,
4142 final Class<T> clazz, final AsyncProcedure<T> procedure) {
4144 assert (resource != null);
4145 assert (clazz != null);
4146 assert (procedure != null);
4148 final AdaptionService service = getSession().peekService(AdaptionService.class);
4149 if (service == null)
4150 procedure.exception(this, new ServiceException("No AdaptionService available"));
4152 service.adaptNew(this, resource, clazz, false, procedure);
4157 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
4158 SyncProcedure<T> procedure) {
4159 forUniqueAdapted(resource, clazz,
4160 new SyncToAsyncProcedure<T>(procedure));
4164 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
4165 Procedure<T> procedure) {
4166 forUniqueAdapted(resource, clazz,
4167 new NoneToAsyncProcedure<T>(procedure));
4171 public void forPossibleInverse(Resource subject,
4172 AsyncListener<Resource> listener) {
4173 asyncRequest(new PossibleInverse(subject), listener);
4177 public void forPossibleInverse(Resource subject,
4178 SyncListener<Resource> listener) {
4179 asyncRequest(new PossibleInverse(subject), listener);
4183 public void forPossibleInverse(Resource subject, Listener<Resource> listener) {
4184 asyncRequest(new PossibleInverse(subject), listener);
4188 final public void forPossibleInverse(final Resource relation,
4189 final AsyncProcedure<Resource> procedure) {
4191 assert (relation != null);
4192 assert (procedure != null);
4194 processor.forInverse(this, relation, new ExceptionToNullProcedure<Resource>(procedure));
4199 public void forPossibleInverse(Resource subject,
4200 SyncProcedure<Resource> procedure) {
4201 forPossibleInverse(subject, new SyncToAsyncProcedure<Resource>(
4206 public void forPossibleInverse(Resource subject,
4207 Procedure<Resource> procedure) {
4208 forPossibleInverse(subject, new NoneToAsyncProcedure<Resource>(
4213 public void forPossibleObject(Resource subject, Resource relation,
4214 AsyncListener<Resource> listener) {
4215 asyncRequest(new PossibleObject(subject, relation), listener);
4219 public void forPossibleObject(Resource subject, Resource relation,
4220 SyncListener<Resource> listener) {
4221 asyncRequest(new PossibleObject(subject, relation), listener);
4225 public void forPossibleObject(Resource subject, Resource relation,
4226 Listener<Resource> listener) {
4227 asyncRequest(new PossibleObject(subject, relation), listener);
4231 final public void forPossibleObject(final Resource subject,
4232 final Resource relation, final AsyncProcedure<Resource> procedure) {
4234 assert (subject != null);
4235 assert (relation != null);
4236 assert (procedure != null);
4238 processor.forEachObject(this, subject, relation,
4239 new SingleOrNullProcedure<Resource>(procedure));
4244 public void forPossibleObject(Resource subject, Resource relation,
4245 SyncProcedure<Resource> procedure) {
4246 forPossibleObject(subject, relation,
4247 new SyncToAsyncProcedure<Resource>(procedure));
4251 public void forPossibleObject(Resource subject, Resource relation,
4252 Procedure<Resource> procedure) {
4253 forPossibleObject(subject, relation,
4254 new NoneToAsyncProcedure<Resource>(procedure));
4258 public void forPossibleStatement(Resource subject, Resource relation,
4259 AsyncListener<Statement> listener) {
4260 asyncRequest(new PossibleStatement(subject, relation), listener);
4264 public void forPossibleStatement(Resource subject, Resource relation,
4265 SyncListener<Statement> listener) {
4266 asyncRequest(new PossibleStatement(subject, relation), listener);
4270 public void forPossibleStatement(Resource subject, Resource relation,
4271 Listener<Statement> listener) {
4272 asyncRequest(new PossibleStatement(subject, relation), listener);
4276 final public void forPossibleStatement(final Resource subject,
4277 final Resource relation, final AsyncProcedure<Statement> procedure) {
4279 assert (subject != null);
4280 assert (relation != null);
4281 assert (procedure != null);
4283 processor.forEachStatement(this, subject, relation,
4284 new SingleFunctionalOrNullProcedure<Statement>(
4285 "forPossibleStatement", procedure));
4290 public void forPossibleStatement(Resource subject, Resource relation,
4291 SyncProcedure<Statement> procedure) {
4292 forPossibleStatement(subject, relation,
4293 new SyncToAsyncProcedure<Statement>(procedure));
4297 public void forPossibleStatement(Resource subject, Resource relation,
4298 Procedure<Statement> procedure) {
4299 forPossibleStatement(subject, relation,
4300 new NoneToAsyncProcedure<Statement>(procedure));
4304 public void forPossibleType(Resource subject, Resource relation,
4305 AsyncListener<Resource> listener) {
4306 asyncRequest(new PossibleType(subject, relation), listener);
4310 public void forPossibleType(Resource subject, Resource relation,
4311 SyncListener<Resource> listener) {
4312 asyncRequest(new PossibleType(subject, relation), listener);
4316 public void forPossibleType(Resource subject, Resource relation,
4317 Listener<Resource> listener) {
4318 asyncRequest(new PossibleType(subject, relation), listener);
4322 final public void forPossibleType(final Resource subject,
4323 final Resource baseType, final AsyncProcedure<Resource> procedure) {
4325 assert (subject != null);
4326 assert (procedure != null);
4328 final NullSingleOrNullProcedure<Resource> checkedProcedure = new NullSingleOrNullProcedure<Resource>(procedure);
4330 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
4333 public void execute(AsyncReadGraph graph,
4334 final Resource principalType) {
4336 if (baseType == null) {
4338 checkedProcedure.offer(graph, principalType);
4340 } else if (principalType.equals(baseType)) {
4342 checkedProcedure.offer(graph, principalType);
4346 checkedProcedure.inc();
4348 processor.forSupertypes((ReadGraphImpl)graph, principalType,
4349 new AsyncProcedure<Set<Resource>>() {
4352 public void execute(
4353 AsyncReadGraph graph,
4354 Set<Resource> result) {
4356 if (result.contains(baseType)) {
4357 checkedProcedure.offer(graph,
4361 checkedProcedure.dec(graph);
4366 public void exception(
4367 AsyncReadGraph graph,
4369 checkedProcedure.exception(graph, t);
4370 checkedProcedure.dec(graph);
4374 public String toString() {
4375 return "forPossibleType -> "
4386 public void finished(AsyncReadGraph graph) {
4387 checkedProcedure.dec(graph);
4391 public void exception(AsyncReadGraph graph, Throwable t) {
4392 checkedProcedure.exception(graph, t);
4393 checkedProcedure.dec(graph);
4401 public void forPossibleType(Resource subject, Resource relation,
4402 SyncProcedure<Resource> procedure) {
4403 forPossibleType(subject, relation, new SyncToAsyncProcedure<Resource>(
4408 public void forPossibleType(Resource subject, Resource relation,
4409 Procedure<Resource> procedure) {
4410 forPossibleType(subject, relation, new NoneToAsyncProcedure<Resource>(
4415 public <T> void forPossibleValue(Resource subject, AsyncListener<T> listener) {
4416 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4420 public <T> void forPossibleValue(Resource subject, SyncListener<T> listener) {
4421 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4425 public <T> void forPossibleValue(Resource subject, Listener<T> listener) {
4426 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4430 final public <T> void forPossibleValue(final Resource subject,
4431 final AsyncProcedure<T> procedure) {
4433 assert (subject != null);
4434 assert (procedure != null);
4436 forPossibleRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
4439 public void execute(AsyncReadGraph graph, final Datatype type) {
4441 procedure.execute(graph, null);
4444 // TODO: consider trying Bindings.getBeanBinding(type);
4445 Binding binding = Bindings.getBinding(type);
4446 graph.forPossibleValue(subject, binding, procedure);
4447 } catch (RuntimeBindingConstructionException e) {
4448 procedure.exception(graph, e);
4454 public void exception(AsyncReadGraph graph, Throwable t) {
4455 procedure.exception(graph, t);
4459 public String toString() {
4460 return "forPossibleValue -> " + procedure;
4468 public <T> void forPossibleValue(Resource subject,
4469 SyncProcedure<T> procedure) {
4470 forPossibleValue(subject, new SyncToAsyncProcedure<T>(procedure));
4474 public <T> void forPossibleValue(Resource subject, Procedure<T> procedure) {
4475 forPossibleValue(subject, new NoneToAsyncProcedure<T>(procedure));
4479 public <T> void forPossibleValue(Resource subject, Binding binding,
4480 AsyncListener<T> listener) {
4481 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4485 public <T> void forPossibleValue(Resource subject, Binding binding,
4486 SyncListener<T> listener) {
4487 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4491 public <T> void forPossibleValue(Resource subject, Binding binding,
4492 Listener<T> listener) {
4493 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4497 final public <T> void forPossibleValue(final Resource resource,
4498 final Binding binding, final AsyncProcedure<T> procedure) {
4500 assert (resource != null);
4501 assert (binding != null);
4502 assert (procedure != null);
4504 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
4507 public void execute(AsyncReadGraph graph, byte[] result) {
4511 if (result == null) {
4512 procedure.execute(graph, null);
4516 Serializer serializer = Bindings.getSerializer( binding );
4517 Object obj = serializer.deserialize(result);
4518 if (!binding.isInstance(obj))
4519 procedure.exception(graph, new ClassCastException(
4520 "Cannot get value " + obj + " with binding "
4523 procedure.execute(graph, (T) obj);
4525 } catch (IOException e) {
4526 procedure.exception(graph, e);
4527 } catch (BufferUnderflowException e) {
4528 procedure.exception(graph, e);
4529 } catch (Throwable t) {
4530 procedure.exception(graph, t);
4536 public void exception(AsyncReadGraph graph, Throwable t) {
4538 procedure.exception(graph, t);
4539 } catch (Throwable t2) {
4540 Logger.defaultLogError(t2);
4545 public String toString() {
4546 return "forPossibleValue -> " + procedure;
4554 public <T> void forPossibleValue(Resource subject, Binding binding,
4555 SyncProcedure<T> procedure) {
4556 forPossibleValue(subject, binding, new SyncToAsyncProcedure<T>(
4561 public <T> void forPossibleValue(Resource subject, Binding binding,
4562 Procedure<T> procedure) {
4563 forPossibleValue(subject, binding, new NoneToAsyncProcedure<T>(
4568 public <T> void forPossibleRelatedValue(Resource subject,
4569 Resource relation, AsyncListener<T> listener) {
4570 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4575 public <T> void forPossibleRelatedValue(Resource subject,
4576 Resource relation, SyncListener<T> listener) {
4577 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4582 public <T> void forPossibleRelatedValue(Resource subject,
4583 Resource relation, Listener<T> listener) {
4584 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4589 final public <T> void forPossibleRelatedValue(final Resource subject,
4590 final Resource relation, final AsyncProcedure<T> procedure) {
4592 assert (subject != null);
4593 assert (relation != null);
4594 assert (procedure != null);
4596 final DeepSingleOrNullProcedure<T> checkedProcedure = new DeepSingleOrNullProcedure<T>(procedure);
4598 processor.forEachObject(this, subject, relation,
4599 new AsyncMultiProcedureAdapter<Resource>() {
4602 public void execute(AsyncReadGraph graph,
4603 final Resource object) {
4605 checkedProcedure.inc();
4607 graph.forValue(object, new AsyncProcedure<Object>() {
4610 public void execute(AsyncReadGraph graph,
4612 checkedProcedure.offer(graph, (T) result);
4613 checkedProcedure.dec(graph);
4617 public void exception(AsyncReadGraph graph,
4619 checkedProcedure.exception(graph, t);
4620 checkedProcedure.dec(graph);
4628 public void finished(AsyncReadGraph graph) {
4630 checkedProcedure.dec(graph);
4634 public void exception(AsyncReadGraph graph, Throwable t) {
4635 checkedProcedure.exception(graph, t);
4636 checkedProcedure.dec(graph);
4640 public String toString() {
4641 return "forPossibleRelatedValue -> " + procedure;
4648 public <T> void forPossibleRelatedValue(Resource subject,
4649 Resource relation, SyncProcedure<T> procedure) {
4650 forPossibleRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
4655 public <T> void forPossibleRelatedValue(Resource subject,
4656 Resource relation, Procedure<T> procedure) {
4657 forPossibleRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
4662 public <T> void forPossibleRelatedValue(Resource subject,
4663 Resource relation, Binding binding, AsyncListener<T> listener) {
4664 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4669 public <T> void forPossibleRelatedValue(Resource subject,
4670 Resource relation, Binding binding, SyncListener<T> listener) {
4671 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4676 public <T> void forPossibleRelatedValue(Resource subject,
4677 Resource relation, Binding binding, Listener<T> listener) {
4678 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4683 // final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4684 // final AsyncProcedure<T> procedure) {
4686 // forPossibleRelatedValue(subject, relation, binding, procedure, false);
4690 final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4691 final AsyncProcedure<T> procedure) {
4693 assert (subject != null);
4694 assert (relation != null);
4695 assert (procedure != null);
4697 processor.forPossibleObject(this, subject, relation, new AsyncProcedure<Resource>() {
4700 public void execute(AsyncReadGraph graph, Resource object) {
4702 if(object == null) {
4703 procedure.execute(graph, null);
4707 processor.forPossibleValue((ReadGraphImpl)graph, object, new AsyncProcedure<byte[]>() {
4710 public void execute(AsyncReadGraph graph, byte[] bytes) {
4716 Serializer serializer = binding.serializer();
4717 Object obj = serializer.deserialize(bytes);
4718 if (!binding.isInstance(obj)) {
4719 procedure.exception(graph, new ClassCastException("Cannot get value " + obj + " with binding " + binding));
4721 procedure.execute(graph, (T) obj);
4724 } catch (Throwable t) {
4726 procedure.exception(graph, t);
4732 procedure.execute(graph, null);
4739 public void exception(AsyncReadGraph graph, Throwable t) {
4740 procedure.exception(graph, t);
4748 public void exception(AsyncReadGraph graph, Throwable throwable) {
4749 throwable.printStackTrace();
4750 procedure.exception(graph, throwable);
4758 public <T> void forPossibleRelatedValue(Resource subject,
4759 Resource relation, Binding binding, SyncProcedure<T> procedure) {
4760 forPossibleRelatedValue(subject, relation, binding,
4761 new SyncToAsyncProcedure<T>(procedure));
4765 public <T> void forPossibleRelatedValue(Resource subject,
4766 Resource relation, Binding binding, Procedure<T> procedure) {
4767 forPossibleRelatedValue(subject, relation, binding,
4768 new NoneToAsyncProcedure<T>(procedure));
4772 public void forIsInstanceOf(Resource subject, Resource relation,
4773 AsyncListener<Boolean> listener) {
4774 asyncRequest(new IsInstanceOf(subject, relation), listener);
4778 public void forIsInstanceOf(Resource subject, Resource relation,
4779 SyncListener<Boolean> listener) {
4780 asyncRequest(new IsInstanceOf(subject, relation), listener);
4784 public void forIsInstanceOf(Resource subject, Resource relation,
4785 Listener<Boolean> listener) {
4786 asyncRequest(new IsInstanceOf(subject, relation), listener);
4790 final public void forIsInstanceOf(final Resource resource,
4791 final Resource type, final AsyncProcedure<Boolean> procedure) {
4793 assert (resource != null);
4794 assert (type != null);
4795 assert (procedure != null);
4797 forTypes(resource, new AsyncProcedure<Set<Resource>>() {
4800 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4803 if (result.contains(type))
4804 procedure.execute(graph, true);
4806 procedure.execute(graph, false);
4807 } catch (Throwable t) {
4808 Logger.defaultLogError(t);
4813 public void exception(AsyncReadGraph graph, Throwable t) {
4815 procedure.exception(graph, t);
4816 } catch (Throwable t2) {
4817 Logger.defaultLogError(t2);
4822 public String toString() {
4823 return "forIsInstanceOf -> " + procedure;
4831 public void forIsInstanceOf(Resource subject, Resource relation,
4832 SyncProcedure<Boolean> procedure) {
4833 forIsInstanceOf(subject, relation, new SyncToAsyncProcedure<Boolean>(
4838 public void forIsInstanceOf(Resource subject, Resource relation,
4839 Procedure<Boolean> procedure) {
4840 forIsInstanceOf(subject, relation, new NoneToAsyncProcedure<Boolean>(
4845 public void forIsInheritedFrom(Resource subject, Resource relation,
4846 AsyncListener<Boolean> listener) {
4847 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4851 public void forIsInheritedFrom(Resource subject, Resource relation,
4852 SyncListener<Boolean> listener) {
4853 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4857 public void forIsInheritedFrom(Resource subject, Resource relation,
4858 Listener<Boolean> listener) {
4859 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4863 final public void forIsInheritedFrom(final Resource resource,
4864 final Resource type, final AsyncProcedure<Boolean> procedure) {
4866 assert (resource != null);
4867 assert (type != null);
4868 assert (procedure != null);
4870 if (resource.equals(type)) {
4872 procedure.execute(this, true);
4873 } catch (Throwable t) {
4874 Logger.defaultLogError(t);
4879 forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
4882 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4884 if (result.contains(type))
4885 procedure.execute(graph, true);
4887 procedure.execute(graph, false);
4888 } catch (Throwable t) {
4889 Logger.defaultLogError(t);
4894 public void exception(AsyncReadGraph graph, Throwable t) {
4896 procedure.exception(graph, t);
4897 } catch (Throwable t2) {
4898 Logger.defaultLogError(t2);
4903 public String toString() {
4904 return "forIsInheritedFrom -> " + procedure;
4912 public void forIsInheritedFrom(Resource subject, Resource relation,
4913 SyncProcedure<Boolean> procedure) {
4914 forIsInheritedFrom(subject, relation,
4915 new SyncToAsyncProcedure<Boolean>(procedure));
4919 public void forIsInheritedFrom(Resource subject, Resource relation,
4920 Procedure<Boolean> procedure) {
4921 forIsInheritedFrom(subject, relation,
4922 new NoneToAsyncProcedure<Boolean>(procedure));
4926 public void forIsSubrelationOf(Resource subject, Resource relation,
4927 AsyncListener<Boolean> listener) {
4928 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4932 public void forIsSubrelationOf(Resource subject, Resource relation,
4933 SyncListener<Boolean> listener) {
4934 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4938 public void forIsSubrelationOf(Resource subject, Resource relation,
4939 Listener<Boolean> listener) {
4940 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4944 final public void forIsSubrelationOf(final Resource resource,
4945 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4947 assert (resource != null);
4948 assert (relation != null);
4949 assert (procedure != null);
4951 if (resource.equals(relation)) {
4952 procedure.execute(this, true);
4956 forSuperrelations(resource, new AsyncProcedure<Set<Resource>>() {
4959 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4961 if (result.contains(relation))
4962 procedure.execute(graph, true);
4964 procedure.execute(graph, false);
4965 } catch (Throwable t) {
4966 Logger.defaultLogError(t);
4971 public void exception(AsyncReadGraph graph, Throwable t) {
4973 procedure.exception(graph, t);
4974 } catch (Throwable t2) {
4975 Logger.defaultLogError(t2);
4980 public String toString() {
4981 return "forIsSubrelationOf -> " + procedure;
4989 public void forIsSubrelationOf(Resource subject, Resource relation,
4990 SyncProcedure<Boolean> procedure) {
4991 forIsSubrelationOf(subject, relation,
4992 new SyncToAsyncProcedure<Boolean>(procedure));
4996 public void forIsSubrelationOf(Resource subject, Resource relation,
4997 Procedure<Boolean> procedure) {
4998 forIsSubrelationOf(subject, relation,
4999 new NoneToAsyncProcedure<Boolean>(procedure));
5003 public void forHasStatement(Resource subject,
5004 AsyncListener<Boolean> listener) {
5005 asyncRequest(new HasStatementSubject(subject), listener);
5009 public void forHasStatement(Resource subject, SyncListener<Boolean> listener) {
5010 asyncRequest(new HasStatementSubject(subject), listener);
5014 public void forHasStatement(Resource subject, Listener<Boolean> listener) {
5015 asyncRequest(new HasStatementSubject(subject), listener);
5019 final public void forHasStatement(final Resource subject,
5020 final AsyncProcedure<Boolean> procedure) {
5022 assert (subject != null);
5023 assert (procedure != null);
5025 processor.forHasStatement(this, subject, procedure);
5030 public void forHasStatement(Resource subject,
5031 SyncProcedure<Boolean> procedure) {
5032 forHasStatement(subject, new SyncToAsyncProcedure<Boolean>(procedure));
5036 public void forHasStatement(Resource subject, Procedure<Boolean> procedure) {
5037 forHasStatement(subject, new NoneToAsyncProcedure<Boolean>(procedure));
5041 public void forHasStatement(Resource subject, Resource relation,
5042 AsyncListener<Boolean> listener) {
5043 asyncRequest(new HasStatement(subject, relation), listener);
5047 public void forHasStatement(Resource subject, Resource relation,
5048 SyncListener<Boolean> listener) {
5049 asyncRequest(new HasStatement(subject, relation), listener);
5053 public void forHasStatement(Resource subject, Resource relation,
5054 Listener<Boolean> listener) {
5055 asyncRequest(new HasStatement(subject, relation), listener);
5059 final public void forHasStatement(final Resource subject,
5060 final Resource relation, final AsyncProcedure<Boolean> procedure) {
5062 assert (subject != null);
5063 assert (relation != null);
5064 assert (procedure != null);
5066 processor.forHasStatement(this, subject, relation, procedure);
5071 public void forHasStatement(Resource subject, Resource relation,
5072 SyncProcedure<Boolean> procedure) {
5073 forHasStatement(subject, relation, new SyncToAsyncProcedure<Boolean>(
5078 public void forHasStatement(Resource subject, Resource relation,
5079 Procedure<Boolean> procedure) {
5080 forHasStatement(subject, relation, new NoneToAsyncProcedure<Boolean>(
5085 public void forHasStatement(Resource subject, Resource relation,
5086 Resource object, AsyncListener<Boolean> listener) {
5087 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
5092 public void forHasStatement(Resource subject, Resource relation,
5093 Resource object, SyncListener<Boolean> listener) {
5094 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
5099 public void forHasStatement(Resource subject, Resource relation,
5100 Resource object, Listener<Boolean> listener) {
5101 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
5106 final public void forHasStatement(final Resource subject,
5107 final Resource relation, final Resource object,
5108 final AsyncProcedure<Boolean> procedure) {
5110 assert (subject != null);
5111 assert (relation != null);
5112 assert (object != null);
5113 assert (procedure != null);
5115 processor.forHasStatement(this, subject, relation, object, procedure);
5120 public void forHasStatement(Resource subject, Resource relation,
5121 Resource object, SyncProcedure<Boolean> procedure) {
5122 forHasStatement(subject, relation, object,
5123 new SyncToAsyncProcedure<Boolean>(procedure));
5127 public void forHasStatement(Resource subject, Resource relation,
5128 Resource object, Procedure<Boolean> procedure) {
5129 forHasStatement(subject, relation, object,
5130 new NoneToAsyncProcedure<Boolean>(procedure));
5134 public void forHasValue(Resource subject, AsyncListener<Boolean> listener) {
5135 asyncRequest(new HasValue(subject), listener);
5139 public void forHasValue(Resource subject, SyncListener<Boolean> listener) {
5140 asyncRequest(new HasValue(subject), listener);
5144 public void forHasValue(Resource subject, Listener<Boolean> listener) {
5145 asyncRequest(new HasValue(subject), listener);
5149 final public void forHasValue(final Resource subject,
5150 final AsyncProcedure<Boolean> procedure) {
5152 assert (subject != null);
5153 assert (procedure != null);
5155 processor.forValue(this, subject, new AsyncProcedure<byte[]>() {
5158 public void execute(AsyncReadGraph graph, byte[] result) {
5161 procedure.execute(graph, false);
5163 procedure.execute(graph, true);
5164 } catch (Throwable t) {
5165 Logger.defaultLogError(t);
5170 public void exception(AsyncReadGraph graph, Throwable t) {
5172 procedure.exception(graph, t);
5173 } catch (Throwable t2) {
5174 Logger.defaultLogError(t2);
5179 public String toString() {
5180 return "forHasValue -> " + procedure;
5188 public void forHasValue(Resource subject, SyncProcedure<Boolean> procedure) {
5189 forHasValue(subject, new SyncToAsyncProcedure<Boolean>(procedure));
5193 public void forHasValue(Resource subject, Procedure<Boolean> procedure) {
5194 forHasValue(subject, new NoneToAsyncProcedure<Boolean>(procedure));
5198 public void forOrderedSet(Resource subject,
5199 AsyncMultiListener<Resource> listener) {
5200 asyncRequest(new OrderedSet(subject), listener);
5204 public void forOrderedSet(Resource subject,
5205 SyncMultiListener<Resource> listener) {
5206 asyncRequest(new OrderedSet(subject), listener);
5210 public void forOrderedSet(Resource subject, MultiListener<Resource> listener) {
5211 asyncRequest(new OrderedSet(subject), listener);
5215 final public void forOrderedSet(final Resource subject,
5216 final AsyncMultiProcedure<Resource> procedure) {
5218 assert (subject != null);
5219 assert (procedure != null);
5221 processor.forOrderedSet(this, subject,
5222 new AsyncMultiProcedure<Resource>() {
5225 public void finished(AsyncReadGraph graph) {
5227 procedure.finished(graph);
5228 } catch (Throwable t) {
5229 Logger.defaultLogError(t);
5234 public void execute(AsyncReadGraph graph, Resource result) {
5236 procedure.execute(graph, result);
5237 } catch (Throwable t) {
5238 Logger.defaultLogError(t);
5243 public void exception(AsyncReadGraph graph, Throwable t) {
5245 procedure.exception(graph, t);
5246 } catch (Throwable t2) {
5247 Logger.defaultLogError(t2);
5252 public String toString() {
5253 return "forOrderedSet -> " + procedure;
5261 public void forOrderedSet(Resource subject,
5262 SyncMultiProcedure<Resource> procedure) {
5263 forOrderedSet(subject, new SyncToAsyncMultiProcedure<Resource>(
5268 public void forOrderedSet(Resource subject,
5269 MultiProcedure<Resource> procedure) {
5270 forOrderedSet(subject, new NoneToAsyncMultiProcedure<Resource>(
5275 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5276 AsyncListener<T> listener) {
5277 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5281 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5282 SyncListener<T> listener) {
5283 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5287 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5288 Listener<T> listener) {
5289 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5293 final public <T> void forPossibleAdapted(final Resource resource,
5294 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5296 assert (resource != null);
5297 assert (clazz != null);
5298 assert (procedure != null);
5300 final AdaptionService service = getSession().peekService(AdaptionService.class);
5301 if (service == null)
5302 procedure.exception(this, new ServiceException("No AdaptionService available"));
5304 service.adapt(this, resource, resource, Resource.class, clazz, true, procedure);
5308 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5309 SyncProcedure<T> procedure) {
5310 forPossibleAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5315 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5316 Procedure<T> procedure) {
5317 forPossibleAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5322 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5323 AsyncListener<T> listener) {
5324 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5328 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5329 SyncListener<T> listener) {
5330 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5334 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5335 Listener<T> listener) {
5336 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5340 final public <T> void forPossibleUniqueAdapted(final Resource resource,
5341 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5343 assert (resource != null);
5344 assert (clazz != null);
5345 assert (procedure != null);
5347 final AdaptionService service = getSession().peekService(AdaptionService.class);
5348 if (service == null)
5349 procedure.exception(this, new ServiceException("No AdaptionService available"));
5351 service.adaptNew(this, resource, clazz, true, procedure);
5356 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5357 SyncProcedure<T> procedure) {
5358 forPossibleUniqueAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5363 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5364 Procedure<T> procedure) {
5365 forPossibleUniqueAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5370 * Implementation of the interface AsyncRequestProcessor
5374 final public Session getSession() {
5375 return processor.getSession();
5379 // final public Builtins getBuiltins() {
5380 // return processor.getSession().getBuiltins();
5384 public <T> void asyncRequest(final Read<T> request) {
5386 asyncRequest(request, new AsyncProcedure<T>() {
5389 public void execute(AsyncReadGraph graph, T result) {
5393 public void exception(AsyncReadGraph graph, Throwable t) {
5394 Logger.defaultLogError(t);
5398 public String toString() {
5399 return "asyncRequest(Read) -> " + request;
5407 public <T> void asyncRequest(Read<T> request, AsyncListener<T> procedure) {
5408 asyncRequest(request, (AsyncProcedure<T>) procedure);
5412 public <T> void asyncRequest(Read<T> request,
5413 final SyncListener<T> procedure) {
5414 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5418 public <T> void asyncRequest(Read<T> request, final Listener<T> procedure) {
5419 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5423 public <T> void asyncRequest(final Read<T> request, final AsyncProcedure<T> procedure) {
5425 assert (request != null);
5426 assert (procedure != null);
5428 processor.schedule(Integer.MIN_VALUE, new SessionTask(request, processor.THREAD_MASK+1, -1) {
5431 public void run(int thread) {
5433 final ListenerBase listener = getListenerBase(procedure);
5434 QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5435 } catch (DatabaseException e) {
5436 Logger.defaultLogError(e);
5445 // final ListenerBase listener = getListenerBase(procedure);
5447 // if (parent != null || listener != null) {
5450 // QueryCache.runnerReadEntry(this, request, parent, listener, procedure);
5451 // //processor.query(this, request, parent, procedure,listener);
5452 // } catch (DatabaseException e) {
5453 // Logger.defaultLogError(e);
5454 // // This throwable has already been transferred to procedure at this point - do nothing about it
5460 //// final ReadGraphImpl newGraph = newSync();
5464 // T result = request.perform(this);
5467 // procedure.execute(this, result);
5468 // } catch (Throwable t) {
5469 // Logger.defaultLogError(t);
5472 // } catch (Throwable t) {
5475 // procedure.exception(this, t);
5476 // } catch (Throwable t2) {
5477 // Logger.defaultLogError(t2);
5488 public static ReadGraphImpl createAsync(QueryProcessor support) {
5489 return new ReadGraphImpl(null, support);
5492 public static ReadGraphImpl forRecompute(CacheEntry entry, QueryProcessor support) {
5493 return new ReadGraphImpl(entry, support);
5497 public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
5498 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5502 public <T> void asyncRequest(Read<T> request, final Procedure<T> procedure) {
5503 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5507 final public <T> void asyncRequest(final AsyncRead<T> request) {
5509 assert (request != null);
5511 asyncRequest(request, new AsyncProcedure<T>() {
5514 public void execute(AsyncReadGraph graph, T result) {
5518 public void exception(AsyncReadGraph graph, Throwable t) {
5519 Logger.defaultLogError(t);
5523 public String toString() {
5524 return "asyncRequest(AsyncRead) -> " + request;
5532 public <T> void asyncRequest(AsyncRead<T> request,
5533 AsyncListener<T> procedure) {
5534 asyncRequest(request, (AsyncProcedure<T>) procedure);
5538 final public <T> void asyncRequest(AsyncRead<T> request,
5539 final SyncListener<T> procedure) {
5540 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5544 final public <T> void asyncRequest(AsyncRead<T> request,
5545 final Listener<T> procedure) {
5546 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5550 final public <T> void asyncRequest(final AsyncRead<T> request,
5551 final AsyncProcedure<T> procedure) {
5553 assert (request != null);
5554 assert (procedure != null);
5556 //final ListenerBase listener = getListenerBase(procedure);
5558 processor.schedule(Integer.MIN_VALUE, new SessionTask(request, processor.THREAD_MASK+1, -1) {
5561 public void run(int thread) {
5563 final ListenerBase listener = getListenerBase(procedure);
5564 QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5565 } catch (DatabaseException e) {
5566 Logger.defaultLogError(e);
5575 // if (parent != null || listener != null) {
5578 // QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure);
5579 // //processor.query(this, request, parent, procedure, listener);
5580 // } catch (DatabaseException e) {
5581 // Logger.defaultLogError(e);
5588 // request.perform(this, new CallWrappedSingleQueryProcedure4<T>(procedure, request));
5590 // } catch (Throwable t) {
5592 // if (t instanceof DatabaseException)
5593 // procedure.exception(this, t);
5598 // new DatabaseException(
5599 // "Unexpected exception in ReadGraph.asyncRequest(SingleAsyncRead, SingleProcedure)",
5609 public <T> void asyncRequest(AsyncRead<T> request,
5610 SyncProcedure<T> procedure) {
5611 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5615 final public <T> void asyncRequest(final AsyncRead<T> request,
5616 final Procedure<T> procedure) {
5617 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5621 public <T> void asyncRequest(final MultiRead<T> request) {
5623 assert (request != null);
5625 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5627 public void exception(AsyncReadGraph graph, Throwable t) {
5628 Logger.defaultLogError(t);
5632 public String toString() {
5633 return "asyncRequest(MultiRead) -> " + request;
5640 public <T> void asyncRequest(MultiRead<T> request,
5641 AsyncMultiListener<T> procedure) {
5642 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5646 public <T> void asyncRequest(MultiRead<T> request,
5647 SyncMultiListener<T> procedure) {
5648 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5652 public <T> void asyncRequest(MultiRead<T> request,
5653 MultiListener<T> procedure) {
5654 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5658 public <T> void asyncRequest(final MultiRead<T> request,
5659 final AsyncMultiProcedure<T> procedure) {
5661 assert (request != null);
5662 assert (procedure != null);
5664 final ListenerBase listener = getListenerBase(procedure);
5666 if (parent != null || listener != null) {
5668 // final ReadGraphImpl newGraph = newSync();
5669 processor.query(this, request, parent, procedure,listener);
5673 // final ReadGraphImpl newGraph = newSync();
5677 request.perform(this, procedure);
5679 } catch (Throwable t) {
5681 procedure.exception(this, t);
5690 public <T> void asyncRequest(MultiRead<T> request,
5691 SyncMultiProcedure<T> procedure) {
5692 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5696 public <T> void asyncRequest(MultiRead<T> request,
5697 MultiProcedure<T> procedure) {
5698 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5702 final public <T> void asyncRequest(final AsyncMultiRead<T> request) {
5704 assert (request != null);
5706 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5708 public void exception(AsyncReadGraph graph, Throwable t) {
5709 Logger.defaultLogError(t);
5713 public String toString() {
5714 return "asyncRequest(AsyncMultiRead) -> " + request;
5721 public <T> void asyncRequest(AsyncMultiRead<T> request,
5722 AsyncMultiListener<T> procedure) {
5723 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5727 public <T> void asyncRequest(AsyncMultiRead<T> request,
5728 SyncMultiListener<T> procedure) {
5729 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5733 public <T> void asyncRequest(AsyncMultiRead<T> request,
5734 MultiListener<T> procedure) {
5735 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5739 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5740 final AsyncMultiProcedure<T> procedure) {
5742 assert (request != null);
5743 assert (procedure != null);
5745 ListenerBase listener = getListenerBase(procedure);
5747 if (parent != null || listener != null) {
5749 processor.query(this, request, parent, procedure, listener);
5755 request.perform(this, new AsyncMultiProcedure<T>() {
5758 public void execute(AsyncReadGraph graph, T result) {
5759 procedure.execute(graph, result);
5763 public void finished(AsyncReadGraph graph) {
5764 procedure.finished(graph);
5768 public void exception(AsyncReadGraph graph, Throwable t) {
5769 procedure.exception(graph, t);
5773 public String toString() {
5774 return "asyncRequest(AsyncMultiRead) -> " + procedure;
5779 } catch (Throwable t) {
5781 procedure.exception(this, new DatabaseException(t));
5789 public <T> void asyncRequest(AsyncMultiRead<T> request,
5790 SyncMultiProcedure<T> procedure) {
5791 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5795 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5796 final MultiProcedure<T> procedure) {
5797 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5801 final public <T> void asyncRequest(final ExternalRead<T> request) {
5803 assert (request != null);
5805 asyncRequest(request, new Procedure<T>() {
5808 public void execute(T result) {
5812 public void exception(Throwable t) {
5813 Logger.defaultLogError(t);
5817 public String toString() {
5818 return "asyncRequest(PrimitiveRead) -> " + request;
5826 public <T> void asyncRequest(ExternalRead<T> request,
5827 final Listener<T> procedure) {
5828 asyncRequest(request, (Procedure<T>) procedure);
5832 final public <T> void asyncRequest(final ExternalRead<T> request,
5833 final Procedure<T> procedure) {
5836 assert (request != null);
5837 assert (procedure != null);
5839 final ListenerBase listener = getListenerBase(procedure);
5841 if (parent != null || listener != null) {
5844 QueryCacheBase.resultExternalReadEntry(this, request, parent, listener, procedure);
5845 //processor.query(this, request, parent, procedure,listener);
5846 } catch (DatabaseException e) {
5847 Logger.defaultLogError(e);
5848 // This throwable has already been transferred to procedure at this point - do nothing about it
5854 request.register(this, new Listener<T>() {
5857 public void execute(T result) {
5858 procedure.execute(result);
5862 public void exception(Throwable t) {
5863 procedure.exception(t);
5867 public String toString() {
5868 return "asyncRequest(PrimitiveRead) -> " + request;
5872 public boolean isDisposed() {
5878 // final ReadGraphImpl newGraph = newSync();
5882 // T result = request.perform(this);
5885 // procedure.execute(this, result);
5886 // } catch (Throwable t) {
5887 // Logger.defaultLogError(t);
5890 // } catch (Throwable t) {
5893 // procedure.exception(this, t);
5894 // } catch (Throwable t2) {
5895 // Logger.defaultLogError(t2);
5904 // assert (request != null);
5905 // assert (procedure != null);
5907 // ListenerBase listener = getListenerBase(procedure);
5909 // if (parent != null || listener != null) {
5911 // processor.query(this, request, parent, procedure, listener);
5917 // request.register(this, new Listener<T>() {
5920 // public void execute(T result) {
5921 // procedure.execute(result);
5925 // public void exception(Throwable t) {
5926 // procedure.exception(t);
5930 // public String toString() {
5931 // return "asyncRequest(PrimitiveRead) -> " + request;
5935 // public boolean isDisposed() {
5941 // } catch (Throwable t) {
5943 // if (t instanceof DatabaseException)
5944 // procedure.exception(t);
5947 // .exception(new DatabaseException(
5948 // "Unexpected exception in ReadGraph.asyncRequest(SingleAsyncRead, SingleProcedure)",
5958 public void asyncRequest(final Write request) {
5960 assert (request != null);
5962 getSession().asyncRequest(request);
5964 // processor.asyncWrite(request);
5969 public <T> void asyncRequest(final WriteResult<T> request, Procedure<T> procedure) {
5970 throw new Error("Not implemented.");
5974 public void asyncRequest(Write request, Consumer<DatabaseException> callback) {
5976 assert (request != null);
5978 getSession().asyncRequest(request, callback);
5983 public void asyncRequest(final DelayedWrite request) {
5985 assert (request != null);
5987 getSession().asyncRequest(request);
5992 public <T> void asyncRequest(final DelayedWriteResult<T> request, Procedure<T> procedure) {
5993 throw new Error("Not implemented.");
5997 public void asyncRequest(DelayedWrite r,
5998 Consumer<DatabaseException> callback) {
5999 throw new Error("Not implemented.");
6003 public void asyncRequest(final WriteOnly request) {
6005 assert (request != null);
6007 getSession().asyncRequest(request);
6012 public <T> void asyncRequest(final WriteOnlyResult<T> request, Procedure<T> procedure) {
6013 throw new Error("Not implemented.");
6017 public void asyncRequest(WriteOnly r, Consumer<DatabaseException> callback) {
6018 throw new Error("Not implemented.");
6022 * Implementation of the interface ServiceLocator
6026 public <T> T getService(Class<T> api) {
6027 if(WriteSupport.class == api) {
6028 if(this instanceof WriteGraphImpl) {
6029 WriteGraphImpl impl = (WriteGraphImpl)this;
6030 return (T)impl.writeSupport;
6033 return getSession().getService(api);
6037 public <T> T peekService(Class<T> api) {
6038 return getSession().peekService(api);
6042 public boolean hasService(Class<?> api) {
6043 return getSession().hasService(api);
6047 public <T> void registerService(Class<T> api, T service) {
6048 getSession().registerService(api, service);
6052 public boolean isImmutable(Resource resource) throws DatabaseException {
6053 ResourceImpl impl = (ResourceImpl)resource;
6054 return processor.isImmutable(impl.id);
6061 protected static String INTERNAL_ERROR_STRING = "Transaction aborted due to internal client error. Contact application support.";
6064 * callerThread is the currently running thread state.syncThread is blocking for
6065 * this execution state.syncParent is the blocking request
6068 final private boolean isExternal(int thread) {
6069 return thread == Integer.MIN_VALUE;
6072 // final private boolean isSync(int thread) {
6073 // return thread < -1 && thread > Integer.MIN_VALUE;
6076 ReadGraphImpl(ReadGraphImpl graph) {
6077 this(graph.parent, graph.processor);
6080 ReadGraphImpl(CacheEntry parent, QueryProcessor support) {
6081 // this.state = new ReadGraphState(barrier, support);
6082 this.parent = parent;
6083 this.processor = support;
6086 ReadGraphImpl(final QueryProcessor support) {
6088 // this.state = state;
6089 this.processor = support;
6094 // public static ReadGraphImpl createSync(int syncThread, Object syncParent,
6095 // ReadGraphSupportImpl support) {
6096 // return new ReadGraphImpl(syncThread, syncThread, syncParent, null,
6097 // support, new AsyncBarrierImpl(null));
6100 public static ReadGraphImpl create(QueryProcessor support) {
6101 return new ReadGraphImpl(support);
6104 // public ReadGraphImpl newAsync() {
6106 //// if(!state.synchronizedExecution) {
6109 //// return new ReadGraphImpl(false, parent, state.support, state.barrier);
6113 // public ReadGraphImpl newSync() {
6114 // return new ReadGraphImpl(parent, processor);
6117 public ReadGraphImpl newSync(CacheEntry parentEntry) {
6118 return new ReadGraphImpl(parentEntry, processor);
6121 public ReadGraphImpl newRestart(ReadGraphImpl impl) {
6123 WriteGraphImpl write = processor.getSession().getService(
6124 WriteGraphImpl.class);
6126 // if (write.callerThread != impl.callerThread)
6127 // return new WriteGraphImpl(impl.callerThread, parent, state.support, write.writeSupport, write.provider, write.state.barrier);
6132 // public ReadGraphImpl newSync(Object parentRequest) {
6133 // return new ReadGraphImpl(callerThread, state.parent, state.support, new AsyncBarrierImpl(state.barrier));
6136 // public ReadGraphImpl newSync(final int callerThread, Object parentRequest) {
6137 // assert (state.syncThread == callerThread || (state.syncThread == Integer.MIN_VALUE && callerThread != Integer.MIN_VALUE));
6138 // return new ReadGraphImpl(callerThread, callerThread, parentRequest,
6139 // state.parent, state.support, new AsyncBarrierImpl(state.barrier));
6142 // public ReadGraphImpl newSyncAsync(Object parentRequest) {
6143 //// assert (callerThread < 0);
6144 // return new ReadGraphImpl(callerThread, state.syncThread, parentRequest,
6145 // state.parent, state.support, new AsyncBarrierImpl(state.barrier));
6148 // public ReadGraphImpl newSyncAsync(final int callerThread,
6149 // Object parentRequest) {
6150 //// assert (callerThread < 0);
6151 // // assert(state.syncThread == callerThread || (state.syncThread == Integer.MIN_VALUE
6152 // // && callerThread != Integer.MIN_VALUE) );
6153 // return new ReadGraphImpl(callerThread, callerThread, parentRequest,
6154 // state.parent, state.support, new AsyncBarrierImpl(state.barrier));
6157 public ReadGraphImpl withAsyncParent(CacheEntry parent) {
6158 return new ReadGraphImpl(parent, processor);
6161 public ReadGraphImpl withParent(CacheEntry parent) {
6162 if(parent == this.parent) return this;
6163 else return new ReadGraphImpl(parent, processor);
6166 final private ListenerBase getListenerBase(final Object procedure) {
6167 if (procedure instanceof ListenerBase)
6168 return (ListenerBase) procedure;
6173 public <T> void waitAsyncProcedure(AsyncMultiReadProcedure<T> procedure) {
6175 assert(procedure.done());
6177 // while (!procedure.done()) {
6179 // boolean executed = processor.resumeTasks(callerThread, null, null);
6183 // // sema.tryAcquire(1, TimeUnit.MILLISECONDS);
6184 // } catch (InterruptedException e) {
6185 // e.printStackTrace();
6193 public <T> void waitAsyncProcedure(AsyncReadProcedure<T> procedure) {
6195 assert(procedure.done());
6197 // while (!procedure.done()) {
6199 // boolean executed = processor.processor.resume(this);
6203 // // sema.tryAcquire(1, TimeUnit.MILLISECONDS);
6204 // } catch (InterruptedException e) {
6205 // e.printStackTrace();
6213 // public void waitAsync(Object request) {
6215 // state.barrier.waitBarrier(request, this);
6216 // } catch (Throwable t) {
6217 // t.printStackTrace();
6218 // processor.scanPending();
6219 // processor.querySupport.checkTasks();
6220 // throw new RuntimeDatabaseException(t);
6224 // public void restart() {
6225 // state.barrier.restart();
6228 public boolean resumeTasks() {
6229 return processor.resumeTasks(this);
6232 Class<?> singleClass(Set<Resource> types) {
6233 Class<?> result = null;
6234 for (Resource type : types) {
6235 Class<?> clazz = processor.getBuiltinValue(type);
6236 if (clazz != null) {
6246 private String debugString(Resource r) {
6249 name = getPossibleRelatedValue(r, processor.getL0(this).HasName);
6250 } catch (ManyObjectsForFunctionalRelationException e) {
6251 Logger.defaultLogError(e);
6252 } catch (ServiceException e) {
6253 Logger.defaultLogError(e);
6255 return "[" + name + " - " + r + "]";
6259 public String toString() {
6260 return "ReadGraphImpl[thread=" + Thread.currentThread() + "]";
6264 final public int thread() {
6268 static class MultiTripleIntProcedure implements TripleIntProcedure {
6270 final private AsyncMultiProcedure<Statement> procedure;
6271 final private ReadGraphImpl impl;
6272 final private QuerySupport support;
6274 public MultiTripleIntProcedure(AsyncMultiProcedure<Statement> procedure, ReadGraphImpl impl, QuerySupport support) {
6275 this.procedure = procedure;
6277 this.support = support;
6281 public void execute(ReadGraphImpl graph, int s, int p, int o) {
6283 procedure.execute(graph, support.getStatement(s, p, o));
6284 } catch (Throwable t2) {
6285 Logger.defaultLogError(t2);
6290 public void finished(ReadGraphImpl graph) {
6292 procedure.finished(graph);
6293 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
6294 } catch (Throwable t2) {
6295 Logger.defaultLogError(t2);
6300 public void exception(ReadGraphImpl graph, Throwable t) {
6302 procedure.exception(graph, t);
6303 } catch (Throwable t2) {
6304 Logger.defaultLogError(t2);
6306 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
6310 public String toString() {
6311 return "forEachObject with " + procedure;
6316 // private AsyncMultiProcedure<Resource> cacheKey = null;
6317 // private MultiIntProcedure cacheResult = null;
6319 // final IntProcedure forMultiProcedure(final AsyncMultiProcedure<Resource> procedure) {
6321 // if(procedure == cacheKey) return cacheResult;
6323 // cacheResult = new MultiIntProcedure(procedure, this, processor.support);
6324 // cacheKey = procedure;
6326 // return cacheResult;
6330 // private AsyncMultiProcedure<Statement> cacheKey2 = null;
6331 // private MultiTripleIntProcedure cacheResult2 = null;
6333 // final synchronized TripleIntProcedure forMultiProcedure(final AsyncMultiProcedure<Statement> procedure) {
6335 // if(procedure == cacheKey2) return cacheResult2;
6337 // cacheResult2 = new MultiTripleIntProcedure(procedure, this, processor.support);
6338 // cacheKey2 = procedure;
6340 // return cacheResult2;
6345 public Datatype getDataType(Resource subject) throws DatabaseException {
6346 for(Resource dataTypeResource : getObjects(subject, processor.getL0(this).HasDataType))
6347 return getValue(dataTypeResource, Bindings.getBindingUnchecked(Datatype.class));
6348 throw new DoesNotContainValueException("The literal has no data type.");
6351 protected <T extends Accessor> T getAccessor4File(Resource subject)
6352 throws DatabaseException {
6355 byte[] bytes = processor.support.getValue(g, subject);
6359 BinaryVariant va = (BinaryVariant) Accessors.getAccessor(bytes, Datatypes.VARIANT);
6360 Accessor ca = va.getContentAccessor();
6362 } catch (AccessorConstructionException e) {
6363 throw new DatabaseException(e);
6369 Binding datatype_binding = Bindings.getBindingUnchecked(DataType.class);
6370 Serializer datatype_serializer = datatype_binding.serializer();
6373 BinaryMemory in = new BinaryMemory(ByteBuffer.wrap(bytes));
6374 datatype = (DataType)datatype_serializer.deserialize(in);
6375 Binding data_binding = Bindings.getBinding(datatype);
6376 Serializer data_serializer = data_binding.serializer(BinarySerializationFormat.INSTANCE);
6377 Object o = data_serializer.deserialize(in);
6379 return (T)Accessors.getAccessor(data_binding, o);
6380 } catch(AccessorConstructionException e) {
6383 } catch (Exception e) {
6384 throw new DatabaseException(e);
6387 @SuppressWarnings("unchecked")
6389 public <T extends Accessor> T getAccessor(Resource subject) throws DatabaseException {
6390 RandomAccessBinary rab = getRandomAccessBinary(subject);
6392 return (T)Accessors.getAccessor(rab, getDataType(subject));
6393 } catch(AccessorConstructionException e) {
6394 throw new DatabaseException(e);
6397 @SuppressWarnings("unchecked")
6398 protected <T extends Accessor> T createAccessor(Resource resource, Datatype datatype, Object intialValue)
6399 throws DatabaseException {
6400 RandomAccessBinary rab = createRandomAccessBinary(resource, datatype, intialValue);
6402 return (T)Accessors.getAccessor(rab, datatype);
6403 } catch(AccessorConstructionException e) {
6404 throw new DatabaseException(e);
6408 public RandomAccessBinary getRandomAccessBinary(Resource subject) throws DatabaseException {
6409 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
6410 ResourceData rd = ravs.get(subject);
6414 ExternalValueSupport evs = getService(ExternalValueSupport.class);
6415 long size = evs.getValueSize(this, subject); // Throws DatabaseException if no old external value.
6417 File platform = Platform.getLocation().toFile();
6418 File tempFiles = new File(platform, "tempFiles");
6419 File dbDir = new File(tempFiles, "db");
6421 File file = new File(dbDir, "ResourceFile" + subject.getResourceId());
6422 rd = new ResourceData(new BinaryFile(file), true); // Old external value.
6423 final int N = 1<<20;
6427 int length = N < left ? N : (int)left;
6428 byte[] bytes = evs.readValue(this, subject, offset, length);
6429 offset += bytes.length;
6430 left -= bytes.length;
6431 rd.binaryFile.write(bytes);
6433 ravs.put(subject, rd);
6435 } catch (Exception e) {
6436 throw new DatabaseException("Resource " + subject + " have value but there was problem with accessing value data.", e);
6438 } catch (Exception e) {
6439 if(Development.DEVELOPMENT) {
6440 if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
6441 e.printStackTrace();
6445 Datatype datatype = getDataType(subject);
6446 Object value = getPossibleValue(subject, Bindings.getBinding(datatype));
6447 return createRandomAccessBinary(subject, datatype, value);
6449 public RandomAccessBinary createRandomAccessBinary(Resource resource, Datatype datatype, Object initialValue)
6450 throws DatabaseException {
6451 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
6453 File platform = Platform.getLocation().toFile();
6454 File tempFiles = new File(platform, "tempFiles");
6455 File dbDir = new File(tempFiles, "db");
6457 File file = new File(dbDir, "ResourceFile" + resource.getResourceId());
6458 ResourceData rd = new ResourceData(new BinaryFile(file), false);
6459 Binding binding = Bindings.getBinding(datatype);
6460 if (null == initialValue) {
6461 initialValue = binding.createDefault();
6463 Serializer serializer = binding.serializer();
6464 byte[] bytes = serializer.serialize(initialValue);
6465 rd.binaryFile.write(bytes);
6466 ravs.put(resource, rd);
6468 } catch (Exception e) {
6469 if (e instanceof DatabaseException)
6470 throw (DatabaseException)e;
6472 throw new DatabaseException(e);
6476 // static class ExternalValueRequest<T> extends ResourceRead<T> {
6478 // public ExternalValueRequest(Resource resource) {
6482 // @SuppressWarnings("unchecked")
6484 // public T perform(ReadGraph graph) throws DatabaseException {
6487 // String uri = graph.getURI(resource);
6488 // if(Layer0.URIs.Functions_functionApplication.equals(uri)) return (T)functionApplication;
6490 // return (T)ReflectionUtils.getValue(uri).getValue();
6492 // } catch(ValueNotFoundException e) {
6493 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
6494 // } catch(ClassCastException e) {
6495 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
6501 @SuppressWarnings("unchecked")
6503 public <T> T getValue2(Resource r, Object context) throws DatabaseException {
6504 Layer0 L0 = processor.getL0(this);
6505 Set<Resource> types = getTypes(r);
6507 if(types.contains(L0.Literal)) {
6508 if(isImmutable(r)) {
6509 return syncRequest(new ValueImplied<T>(r));
6514 else if(types.contains(L0.Function)) {
6515 return Functions.exec(this, r, this, r, context);
6517 else if(types.contains(L0.ExternalValue)) {
6518 return (T)syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6522 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6523 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6525 return function.apply(this, r, context);
6526 } catch(RuntimeException e) {
6527 DatabaseException dte = findPossibleRootException(e);
6528 if(dte != null) throw dte;
6529 else throw new DatabaseException(e);
6536 public Variant getVariantValue2(Resource r, Object context) throws DatabaseException {
6537 Layer0 L0 = processor.getL0(this);
6538 Set<Resource> types = getTypes(r);
6540 if(types.contains(L0.Literal)) {
6541 if(isImmutable(r)) {
6542 return syncRequest(new VariantValueImplied(r));
6544 return getVariantValue(r);
6547 else if(types.contains(L0.Function)) {
6548 Object value = Functions.exec(this, r, this, r, context);
6550 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6551 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6552 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6555 else if(types.contains(L0.ExternalValue)) {
6556 Object value = syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6558 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6559 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6560 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6565 Function3<ReadGraph,Resource,Object,Object> function = requestValueFunction(r);
6566 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6568 Object value = function.apply(this, r, context);
6570 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6571 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6572 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6574 } catch(RuntimeException e) {
6575 DatabaseException dte = findPossibleRootException(e);
6576 if(dte != null) throw dte;
6577 else throw new DatabaseException(e);
6583 public <T> T getPossibleValue2(Resource subject, Object context) throws DatabaseException {
6585 return getValue2(subject, context);
6586 } catch (DatabaseException e) {
6591 static class PossibleConverterFunction<T> extends ResourceRead<Function3<ReadGraph,Resource,Object,T>> {
6593 public PossibleConverterFunction(Resource resource) {
6598 public Function3<ReadGraph,Resource,Object,T> perform(ReadGraph graph) throws DatabaseException {
6599 return compute(graph, resource);
6602 @SuppressWarnings("unchecked")
6603 public static <T> Function3<ReadGraph,Resource,Object,T> compute(ReadGraph graph, Resource resource) throws DatabaseException {
6604 Layer0 L0 = Layer0.getInstance(graph);
6605 for(Resource converter : graph.getObjects(resource, L0.ConvertsToValueWith)) {
6607 if(L0.Functions_functionApplication.equals(converter)) {
6608 return AdaptValue.functionApplication;
6609 //return (Function3<ReadGraph,Resource,Object,T>)graph.syncRequest(new AdaptValue(resource));
6611 return graph.getValue2(converter, resource);
6613 } catch(RuntimeException e) {
6614 DatabaseException dte = findPossibleRootException(e);
6615 if(dte != null) throw dte;
6616 else throw new DatabaseException(e);
6624 <T> Function3<ReadGraph,Resource,Object,T> requestValueFunction(Resource r) throws DatabaseException {
6626 return syncRequest(new PossibleConverterFunction<T>(r), TransientCacheAsyncListener.<Function3<ReadGraph,Resource,Object,T>>instance());
6628 return syncRequest(new PossibleConverterFunction<T>(r));
6632 * Get a value associated with a graph {@link Resource}, using a possible context object and
6633 * a desired value binding. The following methods are tried in order to retreive the value:
6635 * <li>If the given resource is a {@code L0.Literal}, the value of the literal is returned, using the binding specified by {@code binding}.</li>
6636 * <li>If the resource is a {@code L0.ExternalValue}, the value is acquired using
6637 * {@link ReflectionUtils#getValue(String)}.</li>
6638 * <li>If the resource is associated with a suitable value converter with relation {@code L0.ConvertsToValueWith}
6639 * (see {@link #requestValueFunction(Resource)}), the value function is called with the graph, the resource
6640 * and the context object.</li>
6643 * @param r A graph resource with which the value is associated
6644 * @param context A context object that is used for acquiring the value (only applied in case 3)
6645 * @param binding A binding for the value type (only applied in case 1)
6646 * @return The value of the graph node.
6647 * @throws DoesNotContainValueException No value is associated with the graph node.
6648 * @throws DatabaseException Other errors, such as an error in casting the value to the return type or
6649 * a runtime error in the value function.
6651 @SuppressWarnings("unchecked")
6653 public <T> T getValue2(Resource r, Object context, Binding binding) throws DatabaseException {
6654 if (binding instanceof ObjectVariantBinding)
6655 return getValue2(r, context);
6657 Layer0 L0 = processor.getL0(this);
6658 Set<Resource> types = getTypes(r);
6659 if(types.contains(L0.Literal)) {
6660 if(isImmutable(r)) {
6661 return syncRequest(new Value<T>(r, binding));
6663 return getValue(r, binding);
6665 } else if(types.contains(L0.Function)) {
6666 Object value = Functions.exec(this, r, this, r, context);
6667 if(binding.isInstance(value)) return (T)value;
6669 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
6670 return (T)Bindings.adapt(value, srcBinding, binding);
6671 } catch(RuntimeException e) {
6672 DatabaseException dte = findPossibleRootException(e);
6673 if(dte != null) throw dte;
6674 else throw new DatabaseException(e);
6675 } catch (AdaptException e) {
6676 throw new DatabaseException(e);
6677 } catch (org.simantics.databoard.binding.error.BindingException e) {
6678 throw new DatabaseException(e);
6680 } else if(types.contains(L0.ExternalValue)) {
6682 ExternalValue ev = adapt(r, ExternalValue.class);
6683 return ev.getValue(this, r);
6684 //return (T)ReflectionUtils.getValue(getURI(r)).getValue();
6685 // } catch(ValueNotFoundException e) {
6686 // throw new DatabaseException(e);
6687 } catch(ClassCastException e) {
6688 throw new DatabaseException(e);
6692 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6693 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function.");
6695 Object value = function.apply(this, r, context);
6696 if(binding.isInstance(value)) return (T)value;
6697 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
6698 return (T)Bindings.adapt(value, srcBinding, binding);
6699 } catch(RuntimeException e) {
6700 DatabaseException dte = findPossibleRootException(e);
6701 if(dte != null) throw dte;
6702 else throw new DatabaseException(e);
6703 } catch (AdaptException e) {
6704 throw new DatabaseException(e);
6705 } catch (org.simantics.databoard.binding.error.BindingException e) {
6706 throw new DatabaseException(e);
6712 public <T> T getPossibleValue2(Resource subject, Object context, Binding binding) throws DatabaseException {
6714 return getValue2(subject, context, binding);
6715 } catch (DatabaseException e) {
6720 private static DatabaseException findPossibleRootException(Throwable t) {
6721 if(t == null) return null;
6722 if(t instanceof DatabaseException) return (DatabaseException)t;
6723 if(t instanceof RuntimeException || t instanceof InvocationTargetException) {
6724 return findPossibleRootException(t.getCause());
6730 public <T> T getRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6731 return getRelatedValue2(subject, relation, subject);
6735 public Variant getRelatedVariantValue2(Resource subject, Resource relation) throws DatabaseException {
6736 return getRelatedVariantValue2(subject, relation, subject);
6740 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6742 Resource object = getPossibleObject(subject, relation);
6743 if(object == null) return null;
6744 else return getValue2(object, subject);
6745 } catch (DatabaseException e) {
6751 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6752 if(Development.DEVELOPMENT) {
6753 String error = L0Validations.checkValueType(this, subject, relation);
6755 Logger.defaultLogError(new ValidationException(error));
6756 //throw new ValidationException(error);
6757 new ValidationException(error).printStackTrace();
6760 return getValue2(getSingleObject(subject, relation), context);
6764 public Variant getRelatedVariantValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6765 if(Development.DEVELOPMENT) {
6766 String error = L0Validations.checkValueType(this, subject, relation);
6768 Logger.defaultLogError(new ValidationException(error));
6769 //throw new ValidationException(error);
6770 new ValidationException(error).printStackTrace();
6773 return getVariantValue2(getSingleObject(subject, relation), context);
6777 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6779 Resource object = getPossibleObject(subject, relation);
6780 if(object == null) return null;
6781 else return getValue2(object, context);
6782 } catch (DatabaseException e) {
6788 public <T> T getRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6789 return getRelatedValue2(subject, relation, subject, binding);
6793 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6795 Resource object = getPossibleObject(subject, relation);
6796 if(object == null) return null;
6797 return getValue2(object, subject, binding);
6798 } catch (DatabaseException e) {
6804 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6805 return getValue2(getSingleObject(subject, relation), context, binding);
6809 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6811 Resource object = getPossibleObject(subject, relation);
6812 if(object == null) return null;
6813 else return getValue2(object, context, binding);
6814 } catch (DatabaseException e) {
6820 public Type getRelatedValueType(Resource subject, Resource relation) throws DatabaseException {
6821 Layer0 L0 = processor.getL0(this);
6822 Resource property = getSingleObject(subject, relation);
6823 String typeText = (String)getRelatedValue(property, L0.HasValueType, Bindings.STRING);
6825 return org.simantics.scl.compiler.types.Types.parseType(typeText);
6826 } catch (SCLTypeParseException e) {
6827 throw new DatabaseException(e);
6831 private static ThreadLocal<Boolean> syncGraph = new ThreadLocal<Boolean>() {
6832 protected Boolean initialValue() {
6838 public boolean setSynchronous(boolean value) {
6839 boolean old = getSynchronous();
6840 syncGraph.set(value);
6845 public boolean getSynchronous() {
6846 return syncGraph.get();
6849 public void ensureLoaded(int resource) {
6850 processor.querySupport.ensureLoaded(this, resource);
6853 public void ensureLoaded(int resource, int predicate) {
6854 processor.querySupport.ensureLoaded(this, resource, predicate);
6857 public byte[] getValue(int resource) {
6858 return processor.querySupport.getValue(this, resource);
6861 public int thread(int resource) {
6862 return (resource >>> 16) & processor.THREAD_MASK;
6865 public int thread(Resource resource) {
6866 return (((ResourceImpl)resource).id >>> 16) & processor.THREAD_MASK;
6869 public ResourceSupport getResourceSupport() {
6870 return processor.getResourceSupport();
6874 public Object getModificationCounter() {
6875 return processor.getSession().getModificationCounter();