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.ExternalValueSupport;
49 import org.simantics.db.ReadGraph;
50 import org.simantics.db.RelationContext;
51 import org.simantics.db.Resource;
52 import org.simantics.db.Session;
53 import org.simantics.db.Statement;
54 import org.simantics.db.adaption.AdaptionService;
55 import org.simantics.db.common.primitiverequest.Adapter;
56 import org.simantics.db.common.primitiverequest.Builtin;
57 import org.simantics.db.common.primitiverequest.DatatypeBinding;
58 import org.simantics.db.common.primitiverequest.ForEachAssertedObject;
59 import org.simantics.db.common.primitiverequest.ForEachAssertedStatement;
60 import org.simantics.db.common.primitiverequest.HasStatement;
61 import org.simantics.db.common.primitiverequest.HasStatementSubject;
62 import org.simantics.db.common.primitiverequest.HasStatementSubjectObject;
63 import org.simantics.db.common.primitiverequest.HasValue;
64 import org.simantics.db.common.primitiverequest.Inverse;
65 import org.simantics.db.common.primitiverequest.IsInheritedFrom;
66 import org.simantics.db.common.primitiverequest.IsInstanceOf;
67 import org.simantics.db.common.primitiverequest.IsSubrelationOf;
68 import org.simantics.db.common.primitiverequest.OrderedSet;
69 import org.simantics.db.common.primitiverequest.PossibleAdapter;
70 import org.simantics.db.common.primitiverequest.PossibleInverse;
71 import org.simantics.db.common.primitiverequest.PossibleObject;
72 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;
73 import org.simantics.db.common.primitiverequest.PossibleRelatedValueImplied;
74 import org.simantics.db.common.primitiverequest.PossibleStatement;
75 import org.simantics.db.common.primitiverequest.PossibleType;
76 import org.simantics.db.common.primitiverequest.PossibleUniqueAdapter;
77 import org.simantics.db.common.primitiverequest.PossibleValue;
78 import org.simantics.db.common.primitiverequest.PossibleValueImplied;
79 import org.simantics.db.common.primitiverequest.RelatedValue;
80 import org.simantics.db.common.primitiverequest.RelatedValueImplied;
81 import org.simantics.db.common.primitiverequest.SingleObject;
82 import org.simantics.db.common.primitiverequest.SingleStatement;
83 import org.simantics.db.common.primitiverequest.SingleType;
84 import org.simantics.db.common.primitiverequest.SingleTypeAny;
85 import org.simantics.db.common.primitiverequest.Types;
86 import org.simantics.db.common.primitiverequest.UniqueAdapter;
87 import org.simantics.db.common.primitiverequest.Value;
88 import org.simantics.db.common.primitiverequest.ValueImplied;
89 import org.simantics.db.common.primitiverequest.VariantValueImplied;
90 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
91 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
92 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
93 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
94 import org.simantics.db.common.procedure.single.SyncReadProcedure;
95 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrErrorProcedure;
96 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrNullProcedure;
97 import org.simantics.db.common.procedure.single.wrapper.ExceptionToNullProcedure;
98 import org.simantics.db.common.procedure.single.wrapper.NullSingleOrNullProcedure;
99 import org.simantics.db.common.procedure.single.wrapper.SingleFunctionalOrNullProcedure;
100 import org.simantics.db.common.procedure.single.wrapper.SingleOrErrorProcedure;
101 import org.simantics.db.common.procedure.single.wrapper.SingleOrNullProcedure;
102 import org.simantics.db.common.procedure.wrapper.NoneToAsyncListener;
103 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiListener;
104 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiProcedure;
105 import org.simantics.db.common.procedure.wrapper.NoneToAsyncProcedure;
106 import org.simantics.db.common.procedure.wrapper.NoneToAsyncSetProcedure;
107 import org.simantics.db.common.procedure.wrapper.SyncToAsyncListener;
108 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiListener;
109 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiProcedure;
110 import org.simantics.db.common.procedure.wrapper.SyncToAsyncProcedure;
111 import org.simantics.db.common.procedure.wrapper.SyncToAsyncSetProcedure;
112 import org.simantics.db.common.request.AdaptValue;
113 import org.simantics.db.common.request.ResourceRead;
114 import org.simantics.db.common.utils.Logger;
115 import org.simantics.db.common.utils.NameUtils;
116 import org.simantics.db.common.validation.L0Validations;
117 import org.simantics.db.exception.AdaptionException;
118 import org.simantics.db.exception.ArgumentException;
119 import org.simantics.db.exception.AssumptionException;
120 import org.simantics.db.exception.BindingException;
121 import org.simantics.db.exception.DatabaseException;
122 import org.simantics.db.exception.DoesNotContainValueException;
123 import org.simantics.db.exception.EmptyResourceException;
124 import org.simantics.db.exception.InternalException;
125 import org.simantics.db.exception.InvalidLiteralException;
126 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
127 import org.simantics.db.exception.NoInverseException;
128 import org.simantics.db.exception.NoSingleResultException;
129 import org.simantics.db.exception.ResourceNotFoundException;
130 import org.simantics.db.exception.ServiceException;
131 import org.simantics.db.exception.ValidationException;
132 import org.simantics.db.impl.RelationContextImpl;
133 import org.simantics.db.impl.ResourceImpl;
134 import org.simantics.db.impl.internal.RandomAccessValueSupport;
135 import org.simantics.db.impl.internal.ResourceData;
136 import org.simantics.db.impl.procedure.CallWrappedSingleQueryProcedure4;
137 import org.simantics.db.impl.procedure.ResultCallWrappedQueryProcedure4;
138 import org.simantics.db.impl.procedure.ResultCallWrappedSingleQueryProcedure4;
139 import org.simantics.db.impl.query.CacheEntry;
140 import org.simantics.db.impl.query.QueryProcessor;
141 import org.simantics.db.impl.query.QuerySupport;
142 import org.simantics.db.impl.query.TripleIntProcedure;
143 import org.simantics.db.impl.support.ResourceSupport;
144 import org.simantics.db.procedure.AsyncListener;
145 import org.simantics.db.procedure.AsyncMultiListener;
146 import org.simantics.db.procedure.AsyncMultiProcedure;
147 import org.simantics.db.procedure.AsyncProcedure;
148 import org.simantics.db.procedure.AsyncSetListener;
149 import org.simantics.db.procedure.Listener;
150 import org.simantics.db.procedure.ListenerBase;
151 import org.simantics.db.procedure.MultiListener;
152 import org.simantics.db.procedure.MultiProcedure;
153 import org.simantics.db.procedure.Procedure;
154 import org.simantics.db.procedure.SetListener;
155 import org.simantics.db.procedure.StatementProcedure;
156 import org.simantics.db.procedure.SyncListener;
157 import org.simantics.db.procedure.SyncMultiListener;
158 import org.simantics.db.procedure.SyncMultiProcedure;
159 import org.simantics.db.procedure.SyncProcedure;
160 import org.simantics.db.procedure.SyncSetListener;
161 import org.simantics.db.request.AsyncMultiRead;
162 import org.simantics.db.request.AsyncRead;
163 import org.simantics.db.request.DelayedWrite;
164 import org.simantics.db.request.DelayedWriteResult;
165 import org.simantics.db.request.ExternalRead;
166 import org.simantics.db.request.MultiRead;
167 import org.simantics.db.request.Read;
168 import org.simantics.db.request.ReadInterface;
169 import org.simantics.db.request.RequestFlags;
170 import org.simantics.db.request.Write;
171 import org.simantics.db.request.WriteInterface;
172 import org.simantics.db.request.WriteOnly;
173 import org.simantics.db.request.WriteOnlyResult;
174 import org.simantics.db.request.WriteResult;
175 import org.simantics.layer0.Layer0;
176 import org.simantics.scl.compiler.types.Type;
177 import org.simantics.scl.compiler.types.exceptions.SCLTypeParseException;
178 import org.simantics.scl.reflection.ReflectionUtils;
179 import org.simantics.scl.reflection.ValueNotFoundException;
180 import org.simantics.scl.runtime.function.Function3;
181 import org.simantics.utils.DataContainer;
182 import org.simantics.utils.Development;
183 import org.simantics.utils.datastructures.Pair;
184 import org.simantics.utils.datastructures.collections.CollectionUtils;
186 import gnu.trove.map.hash.TObjectIntHashMap;
188 public class ReadGraphImpl implements ReadGraph {
190 final static boolean EMPTY_RESOURCE_CHECK = false;
192 final public CacheEntry parent;
193 final public QueryProcessor processor;
195 final static Binding DATA_TYPE_BINDING_INTERNAL = Bindings.getBindingUnchecked(Datatype.class);
196 final static Serializer DATA_TYPE_SERIALIZER = Bindings.getSerializerUnchecked(DATA_TYPE_BINDING_INTERNAL);
198 final public static TObjectIntHashMap<String> counters = new TObjectIntHashMap<String>();
200 public static void resetCounters() {
204 public static String listCounters(File file) throws IOException {
206 PrintStream b = new PrintStream(new BufferedOutputStream(new FileOutputStream(file)));
208 for(Pair<String,Integer> p : CollectionUtils.valueSortedEntries(counters)) {
209 b.print(-p.second + " " + p.first + "\n");
214 return "Dumped " + counters.size() + " queries.";
219 * Implementation of the interface ReadGraph
221 final public String getURI(final Resource resource) throws AssumptionException, ValidationException, ServiceException {
223 assert (resource != null);
227 return syncRequest(new org.simantics.db.common.uri.ResourceToURI(resource));
229 } catch (AssumptionException e) {
231 throw new AssumptionException(e);
233 } catch (ValidationException e) {
235 throw new ValidationException(e);
237 } catch (ServiceException e) {
239 throw new ServiceException(e);
241 } catch (DatabaseException e) {
243 throw new ServiceException(INTERNAL_ERROR_STRING, e);
249 final public String getPossibleURI(final Resource resource) throws ValidationException, ServiceException {
251 assert (resource != null);
255 return syncRequest(new org.simantics.db.common.uri.ResourceToPossibleURI(resource));
257 } catch (ValidationException e) {
259 throw new ValidationException(e);
261 } catch (ServiceException e) {
263 throw new ServiceException(e);
265 } catch (DatabaseException e) {
267 throw new ServiceException(INTERNAL_ERROR_STRING, e);
273 final public Resource getResource(final String id)
274 throws ResourceNotFoundException, ValidationException,
281 return syncRequest(new org.simantics.db.common.primitiverequest.Resource(
284 } catch (ResourceNotFoundException e) {
286 throw new ResourceNotFoundException(id, e);
288 } catch (ValidationException e) {
290 throw new ValidationException(e);
292 } catch (ServiceException e) {
294 throw new ServiceException(e);
296 } catch (DatabaseException e) {
298 throw new ServiceException(INTERNAL_ERROR_STRING, e);
304 final public Resource getPossibleResource(final String id)
305 throws ResourceNotFoundException, ValidationException,
312 return syncRequest(new org.simantics.db.common.primitiverequest.Resource(
315 } catch (ResourceNotFoundException e) {
319 } catch (ValidationException e) {
321 throw new ValidationException(e);
323 } catch (ServiceException e) {
325 throw new ServiceException(e);
327 } catch (DatabaseException e) {
329 throw new ServiceException(INTERNAL_ERROR_STRING, e);
335 final public Resource getRootLibrary() {
336 return processor.getRootLibraryResource();
339 final public Resource getBuiltin(final String id)
340 throws ResourceNotFoundException, ServiceException {
346 return syncRequest(new Builtin(id));
348 } catch (ResourceNotFoundException e) {
350 throw new ResourceNotFoundException(id, e);
352 } catch (ServiceException e) {
354 throw new ServiceException(e);
356 } catch (DatabaseException e) {
358 throw new ServiceException(INTERNAL_ERROR_STRING, e);
364 static class StatementReadProcedure extends TIntArrayListInternal implements StatementProcedure {
366 private static Throwable DONE = new Throwable();
368 Throwable exception = null;
370 final ResourceSupport support;
372 public StatementReadProcedure(ResourceSupport support) {
373 this.support = support;
377 public synchronized void execute(AsyncReadGraph graph, int s, int p, int o) {
384 public void finished(AsyncReadGraph graph) {
389 public void exception(AsyncReadGraph graph, Throwable t) {
393 public void checkAndThrow() throws DatabaseException {
394 if(exception != DONE) {
395 if (exception instanceof DatabaseException)
396 throw (DatabaseException) exception;
398 throw new DatabaseException(
399 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
404 public boolean done() {
405 return exception != null;
409 public boolean contains(Object obj) {
410 if(!(obj instanceof InternalStatement))
412 InternalStatement statement = (InternalStatement)obj;
416 for(int i=0;i<sizeInternal();i+=3)
417 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
422 @SuppressWarnings("unchecked")
424 public <T> T[] toArray(T[] a) {
425 int length = sizeInternal() / 3;
426 if(length > a.length) {
427 Class<?> arrayType = a.getClass();
428 a = (arrayType == Object[].class)
429 ? (T[]) new Object[length]
430 : (T[]) Array.newInstance(arrayType.getComponentType(), length);
433 for(int i=length;i<a.length;++i)
436 for(int i=0,j=0;i<sizeInternal();i+=3,++j)
437 a[j] = (T)new InternalStatement(support, getQuick(i), getQuick(i+1), getQuick(i+2));
442 public boolean add(Statement e) {
443 throw new UnsupportedOperationException();
447 public boolean remove(Object o) {
448 throw new UnsupportedOperationException();
452 public boolean addAll(Collection<? extends Statement> c) {
453 throw new UnsupportedOperationException();
456 class IteratorImpl implements ListIterator<Statement> {
460 public IteratorImpl(int index) {
465 public boolean hasNext() {
466 return index < sizeInternal();
470 public Statement next() {
471 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
477 public void remove() {
478 throw new Error("Not supported");
482 public boolean hasPrevious() {
487 public Statement previous() {
489 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
494 public int nextIndex() {
499 public int previousIndex() {
504 public void set(Statement e) {
505 throw new UnsupportedOperationException();
509 public void add(Statement e) {
510 throw new UnsupportedOperationException();
516 public Iterator<Statement> iterator() {
517 return new IteratorImpl(0);
522 return sizeInternal() / 3;
526 public Object[] toArray() {
527 Object[] result = new Object[sizeInternal() / 3];
528 for(int i=0,j=0;j<sizeInternal();i++,j+=3)
529 result[i] = new InternalStatement(support, getQuick(j), getQuick(j+1), getQuick(j+2));
534 public boolean addAll(int index, Collection<? extends Statement> c) {
535 throw new UnsupportedOperationException();
539 public Statement get(int index) {
541 if(index < 0 || index >= sizeInternal())
542 throw new IndexOutOfBoundsException();
543 return new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
547 public Statement set(int index, Statement element) {
548 throw new UnsupportedOperationException();
552 public void add(int index, Statement element) {
553 throw new UnsupportedOperationException();
557 public Statement remove(int index) {
558 throw new UnsupportedOperationException();
562 public int indexOf(Object obj) {
563 if(!(obj instanceof InternalStatement))
565 InternalStatement statement = (InternalStatement)obj;
569 for(int i=0;i<sizeInternal();i+=3)
570 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
576 public int lastIndexOf(Object obj) {
577 if(!(obj instanceof InternalStatement))
579 InternalStatement statement = (InternalStatement)obj;
583 for(int i=sizeInternal()-3;i>=0;i-=3)
584 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
590 public ListIterator<Statement> listIterator() {
591 return new IteratorImpl(0);
595 public ListIterator<Statement> listIterator(int index) {
596 return new IteratorImpl(index*3);
600 public List<Statement> subList(int fromIndex, int toIndex) {
601 if(fromIndex < 0 || toIndex*3 >= sizeInternal() || fromIndex > toIndex)
602 throw new IndexOutOfBoundsException();
603 return new RandomAccessSubList<Statement>(this, fromIndex, toIndex-fromIndex);
608 final public Collection<Statement> getStatements(final Resource subject,
609 final Resource relation)
610 throws ManyObjectsForFunctionalRelationException, ServiceException {
612 assert (subject != null);
613 assert (relation != null);
617 StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
618 processor.forEachStatement(this, subject, relation, procedure);
619 procedure.checkAndThrow();
622 } catch (DatabaseException e) {
624 System.err.println(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation);
626 StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
627 processor.forEachStatement(this, subject, relation, procedure);
629 return Collections.emptyList();
631 // throw new ServiceException(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation, e);
638 final public Collection<Statement> getAssertedStatements(final Resource subject, final Resource relation)
639 throws ManyObjectsForFunctionalRelationException, ServiceException {
641 assert (subject != null);
642 assert (relation != null);
646 return syncRequest(new ForEachAssertedStatement(subject, relation));
648 } catch (ManyObjectsForFunctionalRelationException e) {
650 throw new ManyObjectsForFunctionalRelationException(e);
652 } catch (ServiceException e) {
654 throw new ServiceException(e);
656 } catch (DatabaseException e) {
658 throw new ServiceException(INTERNAL_ERROR_STRING, e);
665 final public Collection<Resource> getPredicates(final Resource subject) throws ServiceException {
667 assert (subject != null);
671 return processor.getPredicates(this, subject);
673 // AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
674 // processor.forEachPredicate(this, subject, procedure);
675 // procedure.checkAndThrow();
678 } catch (ServiceException e) {
680 throw new ServiceException(e);
682 } catch (DatabaseException e) {
684 throw new ServiceException(INTERNAL_ERROR_STRING, e);
686 } catch (Throwable e) {
688 throw new ServiceException(e);
695 final public Collection<Resource> getPrincipalTypes(final Resource subject)
696 throws ServiceException {
698 assert (subject != null);
702 AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
703 processor.forEachPrincipalType(this, subject, procedure);
704 procedure.checkAndThrow();
707 } catch (ServiceException e) {
709 throw new ServiceException(e);
711 } catch (DatabaseException e) {
713 throw new ServiceException(INTERNAL_ERROR_STRING, e);
720 final public Set<Resource> getTypes(final Resource subject) throws ServiceException {
722 assert (subject != null);
726 return processor.getTypes(this, subject);
728 } catch (ServiceException e) {
730 throw new ServiceException(e);
732 } catch (DatabaseException e) {
734 throw new ServiceException(INTERNAL_ERROR_STRING, e);
736 } catch (Throwable e) {
738 throw new ServiceException(e);
745 final public Set<Resource> getSupertypes(final Resource subject)
746 throws ServiceException {
748 assert (subject != null);
752 SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
753 processor.forSupertypes(this, subject, procedure);
754 procedure.checkAndThrow();
755 return procedure.result;
757 } catch (ServiceException e) {
759 throw new ServiceException(e);
761 } catch (DatabaseException e) {
763 throw new ServiceException(INTERNAL_ERROR_STRING, e);
770 final public Set<Resource> getSuperrelations(final Resource subject)
771 throws ServiceException {
773 assert (subject != null);
777 SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
778 processor.forSuperrelations(this, subject, procedure);
779 procedure.checkAndThrow();
780 return procedure.result;
782 } catch (ServiceException e) {
784 throw new ServiceException(e);
786 } catch (DatabaseException e) {
788 throw new ServiceException(INTERNAL_ERROR_STRING, e);
795 public Resource getPossibleSuperrelation(Resource subject) throws ServiceException {
799 SyncReadProcedure<Resource> procedure = new SyncReadProcedure<Resource>();
800 processor.forPossibleSuperrelation(this, subject, procedure);
801 procedure.checkAndThrow();
802 return procedure.result;
804 } catch (ServiceException e) {
806 throw new ServiceException(e);
808 } catch (DatabaseException e) {
810 throw new ServiceException(INTERNAL_ERROR_STRING, e);
817 final public Collection<Resource> getObjects(final Resource subject, final Resource relation)
818 throws ServiceException {
820 assert (subject != null);
821 assert (relation != null);
823 if(Development.DEVELOPMENT) {
824 if(Development.isTrue(DevelopmentKeys.READGRAPH_COUNT)) {
825 counters.adjustOrPutValue("objects $" + subject.getResourceId() + " $" + relation.getResourceId(), 1, 1);
827 //if(subject.getResourceId()==xx && relation.getResourceId()==xx) new Exception().printStackTrace();
832 AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
833 processor.forEachObject(this, subject, relation, procedure);
834 procedure.checkAndThrow();
837 } catch (DatabaseException e) {
839 throw new ServiceException(INTERNAL_ERROR_STRING, e);
846 final public Collection<Resource> getAssertedObjects(
847 final Resource subject, final Resource relation)
848 throws ManyObjectsForFunctionalRelationException, ServiceException {
851 throw new ArgumentException("Subject must not be null.");
852 if (relation == null)
853 throw new ArgumentException("Relation must not be null. Subject=" + subject);
857 return syncRequest(new ForEachAssertedObject(subject, relation));
859 } catch (ManyObjectsForFunctionalRelationException e) {
861 throw new ManyObjectsForFunctionalRelationException(e);
863 } catch (ServiceException e) {
865 throw new ServiceException(e);
867 } catch (DatabaseException e) {
869 throw new ServiceException(INTERNAL_ERROR_STRING, e);
876 final public Resource getInverse(final Resource relation) throws NoInverseException, ServiceException {
878 assert (relation != null);
882 return getSingleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
884 } catch (NoSingleResultException e) {
886 throw new NoInverseException(e);
888 } catch (ServiceException e) {
890 throw new ServiceException(e);
897 final public Resource getSingleObject(final Resource subject, final Resource relation) throws NoSingleResultException, ServiceException {
899 if( subject == null) throw new IllegalArgumentException("subject can not be null");
900 if( relation == null) throw new IllegalArgumentException("relation can not be null");
903 int single = processor.getSingleObject(this, subject, relation);
905 if (EMPTY_RESOURCE_CHECK) {
906 if (!hasStatement(subject)) {
907 throw new EmptyResourceException("Resource " + debugString(subject));
910 throw new NoSingleResultException("No single object for subject " + debugString(subject)
911 + " and relation " + debugString(relation), single);
913 return processor.querySupport.getResource(single);
914 } catch (NoSingleResultException e) {
916 } catch (DatabaseException e) {
917 throw new ServiceException(e);
922 final public Statement getSingleStatement(final Resource subject, final Resource relation) throws NoSingleResultException, ManyObjectsForFunctionalRelationException, ServiceException {
923 assert (subject != null);
924 assert (relation != null);
926 Collection<Statement> statements = getStatements(subject, relation);
927 if (statements.size() == 1) {
928 return statements.iterator().next();
930 if (EMPTY_RESOURCE_CHECK)
931 if (!hasStatement(subject))
932 throw new EmptyResourceException("Resource " + debugString(subject));
933 throw new NoSingleResultException("No single statement for subject " + debugString(subject)
934 + " and relation " + debugString(relation), statements.size());
936 } catch (ServiceException e) {
937 throw new ServiceException(e);
942 final public Resource getSingleType(final Resource subject) throws NoSingleResultException, ServiceException {
943 assert (subject != null);
945 ArrayList<Resource> principalTypes = (ArrayList<Resource>)getPrincipalTypes(subject);
946 if (principalTypes.size() == 1) {
947 return principalTypes.get(0);
949 throw new NoSingleResultException("No single type for subject " + debugString(subject), principalTypes.size());
951 } catch (ServiceException e) {
952 throw new ServiceException(e);
957 final public Resource getSingleType(final Resource subject,
958 final Resource baseType) throws NoSingleResultException,
961 assert (subject != null);
962 assert (baseType != null);
965 return syncRequest(new SingleType(subject, baseType));
966 } catch (DatabaseException e) {
967 throw new NoSingleResultException("subject=" + subject + ", baseType=" + baseType, 0, e);
972 final public <T> T getValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
974 assert (subject != null);
978 Layer0 L0 = processor.getL0(this);
979 int object = processor.getSingleObject(this, subject, L0.HasDataType);
980 if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
982 if(processor.isImmutable(object)) {
983 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
984 return getValue(subject, binding);
986 byte[] dt = processor.getValue(this, object);
987 if(dt == null) throw new ServiceException("No data type for " + subject);
988 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
989 Binding binding = Bindings.getBinding(datatype);
990 return getValue(subject, binding);
993 } catch (IOException e) {
995 throw new ServiceException(e);
997 } catch (DoesNotContainValueException e) {
999 throw new DoesNotContainValueException(e, subject);
1001 } catch (ServiceException e) {
1003 throw new ServiceException(e);
1005 } catch (DatabaseException e) {
1007 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1014 final public Variant getVariantValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
1016 assert (subject != null);
1020 Layer0 L0 = processor.getL0(this);
1021 int object = processor.getSingleObject(this, subject, L0.HasDataType);
1022 if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
1024 if(processor.isImmutable(object)) {
1025 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1026 return new Variant(binding, getValue(subject, binding));
1028 byte[] dt = processor.getValue(this, object);
1029 if(dt == null) throw new ServiceException("No data type for " + subject);
1030 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1031 Binding binding = Bindings.getBinding(datatype);
1032 return new Variant(binding, getValue(subject, binding));
1035 } catch (IOException e) {
1037 throw new ServiceException(e);
1039 } catch (DoesNotContainValueException e) {
1041 throw new DoesNotContainValueException(e, subject);
1043 } catch (ServiceException e) {
1045 throw new ServiceException(e);
1047 } catch (DatabaseException e) {
1049 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1054 static final IdentityHashMap<Binding,Serializer> serializers = new IdentityHashMap<Binding,Serializer>();
1057 serializers.put(Bindings.STRING, Bindings.STRING.serializer());
1060 final protected Serializer getSerializer(Binding binding) {
1061 return binding.serializer();
1065 final public <T> T getValue(final Resource subject, final Binding binding) throws DoesNotContainValueException, BindingException,
1068 assert (subject != null);
1072 byte[] bytes = processor.getValue(this, subject);
1073 if (bytes == null) throw new DoesNotContainValueException("No value for resource " + subject);
1075 Serializer serializer = getSerializer(binding);
1076 return (T)serializer.deserialize(bytes);
1078 } catch (DoesNotContainValueException e) {
1080 throw new DoesNotContainValueException(e);
1082 } catch (IOException e) {
1084 throw new ServiceException(e);
1086 } catch (DatabaseException e) {
1088 throw new ServiceException(e);
1090 } catch (BufferUnderflowException e) {
1091 // This is sometimes thrown when deserialize fails because wrong format.
1092 // For callers of this method this is just an service exception.
1093 throw new ServiceException(e);
1099 final public <T> T getRelatedValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1100 DoesNotContainValueException, ServiceException {
1102 assert (subject != null);
1103 assert (relation != null);
1106 Resource object = getSingleObject(subject, relation);
1107 return getValue(object);
1108 } catch (NoSingleResultException e) {
1109 throw new NoSingleResultException("No single value found for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1110 } catch (DoesNotContainValueException e) {
1112 Layer0 L0 = processor.getL0(this);
1113 Resource object = getPossibleObject(subject, relation);
1114 if(isInstanceOf(object, L0.Value)) {
1115 if(isInstanceOf(object, L0.Literal)) {
1116 throw new DoesNotContainValueException(e);
1118 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1121 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1123 } catch (DoesNotContainValueException e2) {
1125 } catch (DatabaseException e2) {
1126 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1128 } catch (ServiceException e) {
1129 throw new ServiceException(e);
1134 final public Variant getRelatedVariantValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1135 DoesNotContainValueException, ServiceException {
1137 assert (subject != null);
1138 assert (relation != null);
1141 Resource object = getSingleObject(subject, relation);
1142 return getVariantValue(object);
1143 } catch (NoSingleResultException e) {
1144 throw new NoSingleResultException("No single object for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1145 } catch (DoesNotContainValueException e) {
1147 Layer0 L0 = processor.getL0(this);
1148 Resource object = getPossibleObject(subject, relation);
1149 if(isInstanceOf(object, L0.Value)) {
1150 if(isInstanceOf(object, L0.Literal)) {
1151 throw new DoesNotContainValueException(e);
1153 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1156 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1158 } catch (DoesNotContainValueException e2) {
1160 } catch (DatabaseException e2) {
1161 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1163 } catch (ServiceException e) {
1164 throw new ServiceException(e);
1169 final public <T> T getRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1170 throws NoSingleResultException, DoesNotContainValueException, BindingException, ServiceException {
1172 assert (subject != null);
1173 assert (relation != null);
1176 Resource object = getSingleObject(subject, relation);
1177 return getValue(object, binding);
1178 } catch (NoSingleResultException e) {
1179 String message = "";
1181 String subjectName = NameUtils.getSafeName(this, subject, true);
1182 String relationName = NameUtils.getSafeName(this, relation, true);
1183 message = "Subject: " + subjectName + ", Relation: " + relationName;
1184 } catch (DatabaseException e2) {
1187 throw new NoSingleResultException(message, e.getResultCount(), e);
1188 } catch (DoesNotContainValueException e) {
1189 throw new DoesNotContainValueException(e);
1190 } catch (ServiceException e) {
1191 throw new ServiceException(e);
1196 final public <T> T adapt(final Resource resource, final Class<T> clazz)
1197 throws AdaptionException, ValidationException, ServiceException {
1199 assert (resource != null);
1200 assert (clazz != null);
1204 return syncRequest(new Adapter<T>(resource, clazz));
1206 } catch (AdaptionException e) {
1208 throw new AdaptionException(e);
1210 } catch (ValidationException e) {
1212 throw new ValidationException(e);
1214 } catch (ServiceException e) {
1216 throw new ServiceException(e);
1218 } catch (DatabaseException e) {
1220 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1227 final public <T,C> T adaptContextual(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1228 throws AdaptionException, ValidationException, ServiceException {
1230 assert (resource != null);
1231 assert (context != null);
1233 class ContextualAdapter implements AsyncRead<T> {
1235 final private Resource resource;
1236 final private C context;
1237 final private Class<T> clazz;
1240 public int hashCode() {
1241 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1245 final public int threadHash() {
1246 return resource.getThreadHash();
1250 public boolean equals(Object object) {
1253 else if (object == null)
1255 else if (getClass() != object.getClass())
1257 ContextualAdapter r = (ContextualAdapter)object;
1258 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1262 public int getFlags() {
1266 public ContextualAdapter(Resource resource, C context, Class<T> clazz) {
1267 this.resource = resource;
1268 this.context = context;
1273 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1275 final AdaptionService service = getSession().peekService(AdaptionService.class);
1276 if (service == null)
1277 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1279 service.adapt(graph, resource, context, contextClass, clazz, false, procedure);
1284 public String toString() {
1285 return "Adapter for (" + resource + "," + context + ") as " + clazz.getName();
1292 return syncRequest(new ContextualAdapter(resource, context, clazz));
1294 } catch (AdaptionException e) {
1296 throw new AdaptionException(e);
1298 } catch (ValidationException e) {
1300 throw new ValidationException(e);
1302 } catch (ServiceException e) {
1304 throw new ServiceException(e);
1306 } catch (DatabaseException e) {
1308 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1315 final public <T> T adaptRelated(final Resource resource, final Resource relation, final Class<T> clazz)
1316 throws AdaptionException, NoSingleResultException, ValidationException, ServiceException {
1318 assert (resource != null);
1319 assert (clazz != null);
1321 Statement stm = getSingleStatement(resource, relation);
1323 return adaptContextual(stm.getObject(), new RelationContextImpl(resource, stm), RelationContext.class, clazz);
1328 final public <T> T getPossibleRelatedAdapter(final Resource resource, final Resource relation, final Class<T> clazz)
1329 throws ValidationException, ServiceException {
1332 return adaptRelated(resource, relation, clazz);
1333 } catch (DatabaseException e) {
1340 final public <T,C> T getPossibleContextualAdapter(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1341 throws ValidationException, ServiceException {
1343 assert (resource != null);
1344 assert (context != null);
1346 class PossibleContextualAdapter implements AsyncRead<T> {
1348 final private Resource resource;
1349 final private C context;
1350 final private Class<T> clazz;
1353 public int hashCode() {
1354 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1358 final public int threadHash() {
1359 return resource.getThreadHash();
1363 public boolean equals(Object object) {
1366 else if (object == null)
1368 else if (getClass() != object.getClass())
1370 PossibleContextualAdapter r = (PossibleContextualAdapter)object;
1371 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1375 public int getFlags() {
1379 public PossibleContextualAdapter(Resource resource, C context, Class<T> clazz) {
1380 this.resource = resource;
1381 this.context = context;
1386 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1388 final AdaptionService service = getSession().peekService(AdaptionService.class);
1389 if (service == null)
1390 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1392 service.adapt(graph, resource, context, contextClass, clazz, true, procedure);
1397 public String toString() {
1398 return "Possible adapter for (" + resource + "," + context + ") as " + clazz.getName();
1405 return syncRequest(new PossibleContextualAdapter(resource, context, clazz));
1407 } catch (ValidationException e) {
1409 throw new ValidationException(e);
1411 } catch (ServiceException e) {
1413 throw new ServiceException(e);
1415 } catch (DatabaseException e) {
1417 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1424 final public <T> T adaptUnique(final Resource resource, final Class<T> clazz)
1425 throws AdaptionException, ValidationException, ServiceException {
1427 assert (resource != null);
1428 assert (clazz != null);
1432 return syncRequest(new UniqueAdapter<T>(resource, clazz));
1434 } catch (AdaptionException e) {
1436 throw new AdaptionException(e);
1438 } catch (ValidationException e) {
1440 throw new ValidationException(e);
1442 } catch (ServiceException e) {
1444 throw new ServiceException(e);
1446 } catch (DatabaseException e) {
1448 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1455 final public Resource getPossibleInverse(final Resource relation)
1456 throws ServiceException {
1458 assert (relation != null);
1462 return getPossibleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
1464 } catch (ServiceException e) {
1466 throw new ServiceException(e);
1468 } catch (DatabaseException e) {
1470 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1477 public Resource getPossibleObject(final Resource subject, final Resource relation)
1478 throws ManyObjectsForFunctionalRelationException, ServiceException {
1480 assert (subject != null);
1481 assert (relation != null);
1485 int result = processor.getSingleObject(this, subject, relation);
1486 if(result == 0) return null;
1488 return processor.querySupport.getResource(result);
1490 } catch (ManyObjectsForFunctionalRelationException e) {
1492 throw new ManyObjectsForFunctionalRelationException("subject=" + subject + ", relation=" + relation, e);
1494 } catch (DatabaseException e) {
1496 throw new ServiceException(e);
1503 final public Statement getPossibleStatement(final Resource subject, final Resource relation)
1504 throws ManyObjectsForFunctionalRelationException, ServiceException {
1506 assert (subject != null);
1507 assert (relation != null);
1511 Collection<Statement> statements = getStatements(subject, relation);
1512 if(statements.size() == 1) return statements.iterator().next();
1515 } catch (ManyObjectsForFunctionalRelationException e) {
1517 throw new ManyObjectsForFunctionalRelationException("Many objects in " + subject + " for functional relation " + relation);
1519 } catch (ServiceException e) {
1521 throw new ServiceException(e);
1528 final public Resource getPossibleType(final Resource subject, final Resource baseType) throws ServiceException {
1530 assert (subject != null);
1531 assert (baseType != null);
1535 AsyncReadProcedure<Resource> procedure = new AsyncReadProcedure<Resource>();
1536 forPossibleType(subject, baseType, procedure);
1537 procedure.checkAndThrow();
1538 return procedure.result;
1540 } catch (ServiceException e) {
1542 throw new ServiceException(e);
1544 } catch (DatabaseException e) {
1546 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1553 final public <T> T getPossibleValue(final Resource subject) throws ServiceException {
1555 assert (subject != null);
1559 int object = processor.getSingleObject(this, subject, processor.getL0(this).HasDataType);
1560 if(object == 0) return null;
1562 if(processor.isImmutable(object)) {
1563 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1564 return getPossibleValue(subject, binding);
1566 byte[] dt = processor.getValue(this, object);
1567 if(dt == null) return null;
1568 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1569 Binding binding = Bindings.getBinding(datatype);
1570 return getPossibleValue(subject, binding);
1573 } catch (IOException e) {
1575 throw new ServiceException(e);
1577 } catch (ServiceException e) {
1579 throw new ServiceException(e);
1581 } catch (DatabaseException e) {
1583 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1590 final public <T> T getPossibleValue(final Resource subject, final Binding binding) throws BindingException, ServiceException {
1592 assert (subject != null);
1593 assert (binding != null);
1597 byte[] dt = processor.getValue(this, subject);
1598 if(dt == null) return null;
1599 Serializer serializer = getSerializer(binding);
1600 return (T)serializer.deserialize(dt);
1602 } catch (IOException e) {
1604 throw new ServiceException(e);
1606 } catch (BindingException e) {
1608 throw new BindingException(e);
1610 } catch (ServiceException e) {
1612 throw new ServiceException(e);
1614 } catch (DatabaseException e) {
1615 e.printStackTrace();
1616 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1622 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation)
1623 throws ManyObjectsForFunctionalRelationException, ServiceException {
1625 assert (subject != null);
1626 assert (relation != null);
1630 Resource object = getPossibleObject(subject, relation);
1631 if(object == null) return null;
1632 else return getPossibleValue(object);
1634 } catch (ManyObjectsForFunctionalRelationException e) {
1636 throw new ManyObjectsForFunctionalRelationException(e);
1638 } catch (ServiceException e) {
1640 throw new ServiceException(e);
1647 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1648 throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
1650 assert (subject != null);
1651 assert (relation != null);
1652 assert (binding != null);
1656 Resource object = getPossibleObject(subject, relation);
1657 if(object == null) return null;
1658 else return getPossibleValue(object, binding);
1660 } catch (ManyObjectsForFunctionalRelationException e) {
1662 throw new ManyObjectsForFunctionalRelationException(e);
1664 } catch (BindingException e) {
1666 throw new BindingException(e);
1668 } catch (ServiceException e) {
1670 throw new ServiceException(e);
1677 public <T> T getPossibleAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1679 assert (resource != null);
1680 assert (clazz != null);
1684 return syncRequest(new PossibleAdapter<T>(resource, clazz));
1686 } catch (ValidationException e) {
1688 throw new ValidationException(e);
1690 } catch (AdaptionException e) {
1694 } catch (DatabaseException e) {
1696 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1702 public <T> T getPossibleUniqueAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1704 assert (resource != null);
1705 assert (clazz != null);
1709 return syncRequest(new PossibleUniqueAdapter<T>(resource, clazz));
1711 } catch (AdaptionException e) {
1715 } catch (ValidationException e) {
1717 throw new ValidationException(e);
1719 } catch (DatabaseException e) {
1721 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1728 final public boolean isInstanceOf(final Resource resource, final Resource type) throws ServiceException {
1730 assert (resource != null);
1731 assert (type != null);
1733 Set<Resource> resources = getTypes(resource);
1734 // This check was necessary because some of the callers of this method got stuck when the NPE was thrown from here.
1735 if (null == resources)
1738 if(EMPTY_RESOURCE_CHECK) {
1739 if (resources.isEmpty()) {
1740 if(!hasStatement(resource)) throw new EmptyResourceException("Resource " + debugString(resource));
1744 return resources.contains(type);
1749 final public boolean isInheritedFrom(final Resource resource, final Resource type) throws ServiceException {
1751 assert (resource != null);
1752 assert (type != null);
1756 if(resource.equals(type)) return true;
1758 return getSupertypes(resource).contains(type);
1760 } catch (ServiceException e) {
1762 throw new ServiceException(e);
1769 final public boolean isSubrelationOf(final Resource resource, final Resource type) throws ServiceException {
1771 assert (resource != null);
1772 assert (type != null);
1776 if(resource.equals(type)) return true;
1778 return getSuperrelations(resource).contains(type);
1780 } catch (ServiceException e) {
1782 throw new ServiceException(e);
1789 final public boolean hasStatement(final Resource subject) throws ServiceException {
1791 assert (subject != null);
1795 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1796 processor.forHasStatement(this, subject, procedure);
1797 procedure.checkAndThrow();
1798 return procedure.result;
1800 } catch (ServiceException e) {
1802 throw new ServiceException(e);
1804 } catch (DatabaseException e) {
1806 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1813 final public boolean hasStatement(final Resource subject, final Resource relation) throws ServiceException {
1815 assert (subject != null);
1816 assert (relation != null);
1820 Collection<Resource> objects = getObjects(subject, relation);
1821 return !objects.isEmpty();
1823 } catch (ServiceException e) {
1825 throw new ServiceException(e);
1832 final public boolean hasStatement(final Resource subject, final Resource relation, final Resource object) throws ServiceException {
1834 assert (subject != null);
1835 assert (relation != null);
1836 assert (object != null);
1840 for(Resource o : getObjects(subject, relation)) {
1841 if(object.equals(o)) return true;
1846 } catch (ServiceException e) {
1848 throw new ServiceException(e);
1855 final public boolean hasValue(final Resource subject) throws ServiceException {
1857 assert (subject != null);
1861 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1862 processor.forHasValue(this, subject, procedure);
1863 procedure.checkAndThrow();
1864 return procedure.result;
1866 } catch (ServiceException e) {
1868 throw new ServiceException(e);
1870 } catch (DatabaseException e) {
1872 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1878 final AsyncProcedure<?> NONE = new AsyncProcedure<Object>() {
1881 public void execute(AsyncReadGraph graph, Object result) {
1885 public void exception(AsyncReadGraph graph, Throwable throwable) {
1891 * Implementation of the interface RequestProcessor
1895 public <T> T syncRequest(final Read<T> request) throws DatabaseException {
1897 assert (request != null);
1899 return processor.query(this, request, parent, null, null);
1901 // if (parent != null) {
1904 // } catch (Throwable e) {
1905 // if(e instanceof DatabaseException) throw (DatabaseException)e;
1906 // else throw new DatabaseException(e);
1913 // return processor.tryQuery(this, request);
1915 // } catch (Throwable throwable) {
1917 // //Logger.defaultLogError("Internal read request failure", throwable);
1919 // if (throwable instanceof DatabaseException)
1920 // throw (DatabaseException) throwable;
1922 // throw new DatabaseException(
1923 // "Unexpected exception in ReadGraph.syncRequest(Read,Procedure)",
1933 public <T> T syncRequest(Read<T> request, SyncListener<T> procedure)
1934 throws DatabaseException {
1935 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
1939 public <T> T syncRequest(Read<T> request, final Listener<T> procedure)
1940 throws DatabaseException {
1941 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
1945 public <T> T syncRequest(final Read<T> request, final AsyncProcedure<T> procedure) throws DatabaseException {
1947 assert (request != null);
1949 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
1951 return processor.query(this, request, parent, procedure, listener);
1954 // if (parent != null || listener != null) {
1957 // } catch (Throwable e) {
1958 // if(e instanceof DatabaseException) throw (DatabaseException)e;
1959 // else throw new DatabaseException(e);
1966 // T t = processor.tryQuery(this, request);
1967 // if(procedure != null)
1968 // procedure.execute(this, t);
1972 // } catch (Throwable throwable) {
1974 // Logger.defaultLogError("Internal read request failure", throwable);
1976 // if(procedure != null)
1977 // procedure.exception(this, throwable);
1979 // if (throwable instanceof DatabaseException)
1980 // throw (DatabaseException) throwable;
1982 // throw new DatabaseException(
1983 // "Unexpected exception in ReadGraph.syncRequest(Read,Procedure)",
1993 public <T> T syncRequest(final Read<T> request,
1994 final SyncProcedure<T> procedure) throws DatabaseException {
1995 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
1999 public <T> T syncRequest(Read<T> request, Procedure<T> procedure)
2000 throws DatabaseException {
2001 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2004 static class AsyncReadProcedure<T> implements AsyncProcedure<T> {
2006 private static Throwable DONE = new Throwable();
2009 Throwable exception = null;
2012 public void execute(AsyncReadGraph graph, T t) {
2018 public void exception(AsyncReadGraph graph, Throwable t) {
2022 public void checkAndThrow() throws DatabaseException {
2023 if(exception != DONE) {
2024 if (exception instanceof DatabaseException)
2025 throw (DatabaseException) exception;
2027 throw new DatabaseException(
2028 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
2033 public boolean done() {
2034 return exception != null;
2040 public <T> T syncRequest(final AsyncRead<T> request)
2041 throws DatabaseException {
2043 assert (request != null);
2044 AsyncReadProcedure<T> procedure = new AsyncReadProcedure<T>();
2045 syncRequest(request, procedure);
2046 procedure.checkAndThrow();
2047 return procedure.result;
2049 // return syncRequest(request, new AsyncProcedureAdapter<T>());
2054 public <T> T syncRequest(AsyncRead<T> request, AsyncListener<T> procedure)
2055 throws DatabaseException {
2056 return syncRequest(request, (AsyncProcedure<T>) procedure);
2060 public <T> T syncRequest(AsyncRead<T> request, SyncListener<T> procedure)
2061 throws DatabaseException {
2062 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
2066 public <T> T syncRequest(AsyncRead<T> request, Listener<T> procedure)
2067 throws DatabaseException {
2068 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
2072 final public <T> T syncRequest(final AsyncRead<T> request,
2073 final AsyncProcedure<T> procedure) throws DatabaseException {
2075 assert (request != null);
2077 ListenerBase listener = getListenerBase(procedure);
2079 final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
2080 procedure, request);
2082 processor.query(this, request, parent, wrapper, listener);
2084 return wrapper.getResult();
2086 // if (parent != null || listener != null || ((request.getFlags() & RequestFlags.SCHEDULE) > 0)) {
2088 //// Object syncParent = request;
2090 //// final ReadGraphImpl newGraph = newSync();
2093 //// newGraph.waitAsync(syncParent);
2095 // Throwable e = wrapper.getException();
2097 // // The request was async - produce meaningful stack trace by
2099 // if (e instanceof DatabaseException)
2100 // throw (DatabaseException) e;
2102 // throw new DatabaseException(e);
2105 // return wrapper.getResult();
2109 // // System.out.println("direct call " + request );
2111 // // Do not set the sync state.parent for external threads
2112 //// Object syncParent = request;
2114 //// final ReadGraphImpl newGraph = newSync();
2116 // final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
2117 // procedure, request);
2121 // processor.tryQuery(this, request, wrapper);
2123 // } catch (Throwable t) {
2125 // wrapper.exception(this, t);
2129 // Throwable e = wrapper.getException();
2131 // // The request was async - produce meaningful stack trace by
2133 // if (e instanceof DatabaseException)
2134 // throw (DatabaseException) e;
2136 // throw new DatabaseException(e);
2139 // return wrapper.getResult();
2145 final private <T> void syncRequest(final AsyncRead<T> request, final AsyncReadProcedure<T> procedure) throws DatabaseException {
2147 assert (request != null);
2149 ListenerBase listener = getListenerBase(procedure);
2151 final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
2152 procedure, request);
2154 processor.query(this, request, parent, wrapper, listener);
2156 // if (parent != null || listener != null || ((request.getFlags() & RequestFlags.SCHEDULE) > 0)) {
2163 //// final ReadGraphImpl newGraph = newSync();
2164 // processor.tryQuery(this, request, procedure);
2165 //// newGraph.waitAsync(null);
2166 // waitAsyncProcedure(procedure);
2168 // } catch (Throwable t) {
2169 // if(Development.DEVELOPMENT) {
2170 // if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
2171 // t.printStackTrace();
2174 // procedure.exception(this, t);
2175 // waitAsyncProcedure(procedure);
2183 public <T> T syncRequest(AsyncRead<T> request,
2184 final SyncProcedure<T> procedure) throws DatabaseException {
2185 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2189 final public <T> T syncRequest(final AsyncRead<T> request,
2190 final Procedure<T> procedure) throws DatabaseException {
2191 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2195 public <T> Collection<T> syncRequest(final MultiRead<T> request)
2196 throws DatabaseException {
2198 assert (request != null);
2200 final ArrayList<T> result = new ArrayList<T>();
2201 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2203 syncRequest(request, new AsyncMultiProcedure<T>() {
2206 public void execute(AsyncReadGraph graph, T t) {
2207 synchronized (result) {
2213 public void finished(AsyncReadGraph graph) {
2217 public void exception(AsyncReadGraph graph, Throwable t) {
2222 public String toString() {
2223 return "syncRequest(MultiRead) -> " + request;
2228 Throwable t = exception.get();
2230 if (t instanceof DatabaseException)
2231 throw (DatabaseException) t;
2233 throw new DatabaseException(
2234 "Unexpected exception in ReadGraph.syncRequest(Read)",
2243 public <T> Collection<T> syncRequest(MultiRead<T> request,
2244 AsyncMultiListener<T> procedure) {
2245 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2249 public <T> Collection<T> syncRequest(MultiRead<T> request,
2250 SyncMultiListener<T> procedure) {
2251 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2255 public <T> Collection<T> syncRequest(MultiRead<T> request,
2256 MultiListener<T> procedure) {
2257 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2261 public <T> Collection<T> syncRequest(MultiRead<T> request,
2262 AsyncMultiProcedure<T> procedure) {
2264 assert (request != null);
2266 ListenerBase listener = getListenerBase(procedure);
2268 if (parent != null || listener != null) {
2270 // Object syncParent = request;
2272 // final ReadGraphImpl newGraph = newSync();
2274 processor.query(this, request, parent, procedure, listener);
2276 // newGraph.waitAsync(syncParent);
2280 // Object syncParent = request;
2282 // final ReadGraphImpl newGraph = newSync();
2284 final ResultCallWrappedQueryProcedure4<T> wrapper = new ResultCallWrappedQueryProcedure4<T>(procedure);
2288 request.perform(this, wrapper);
2290 } catch (Throwable t) {
2292 wrapper.exception(this, t);
2293 // newGraph.waitAsync(syncParent);
2305 public <T> Collection<T> syncRequest(MultiRead<T> request,
2306 SyncMultiProcedure<T> procedure) {
2307 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2311 public <T> Collection<T> syncRequest(MultiRead<T> request,
2312 MultiProcedure<T> procedure) {
2313 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2316 static class AsyncMultiReadProcedure<T> extends ArrayList<T> implements AsyncMultiProcedure<T> {
2318 private static Throwable DONE = new Throwable();
2320 private static final long serialVersionUID = -6494230465108115812L;
2322 Throwable exception = null;
2325 public synchronized void execute(AsyncReadGraph graph, T t) {
2330 public void finished(AsyncReadGraph graph) {
2335 public void exception(AsyncReadGraph graph, Throwable t) {
2339 public void checkAndThrow() throws DatabaseException {
2340 if(exception != DONE) {
2341 if (exception instanceof DatabaseException)
2342 throw (DatabaseException) exception;
2344 throw new DatabaseException(
2345 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
2350 public boolean done() {
2351 return exception != null;
2357 final public <T> Collection<T> syncRequest(AsyncMultiRead<T> request)
2358 throws DatabaseException {
2360 assert (request != null);
2362 final AsyncMultiReadProcedure<T> procedure = new AsyncMultiReadProcedure<T>();
2364 syncRequest(request, procedure);
2366 procedure.checkAndThrow();
2372 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2373 AsyncMultiListener<T> procedure) {
2374 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2378 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2379 SyncMultiListener<T> procedure) {
2380 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2384 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2385 MultiListener<T> procedure) {
2386 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2389 final private <T> void syncRequest(final AsyncMultiRead<T> request,
2390 final AsyncMultiReadProcedure<T> procedure) {
2392 assert (request != null);
2393 assert (procedure != null);
2395 ListenerBase listener = getListenerBase(procedure);
2397 if (parent != null || listener != null) {
2399 // Object syncParent = request;
2401 // final ReadGraphImpl newGraph = newSync();
2403 processor.query(this, request, parent, procedure, listener);
2405 // newGraph.waitAsync(syncParent);
2406 waitAsyncProcedure(procedure);
2410 // Object syncParent = callerThread == Integer.MIN_VALUE ? null
2413 // final ReadGraphImpl newGraph = newSyncAsync(syncParent);
2418 // ReadGraphImpl sync = newSync();
2419 request.perform(this, procedure);
2420 // sync.waitAsync(null);
2421 waitAsyncProcedure(procedure);
2424 } catch (Throwable t) {
2426 waitAsyncProcedure(procedure);
2437 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2438 final AsyncMultiProcedure<T> procedure) {
2440 assert (request != null);
2441 assert (procedure != null);
2443 ListenerBase listener = getListenerBase(procedure);
2445 if (parent != null || listener != null) {
2447 // Object syncParent = request;
2449 // final ReadGraphImpl newGraph = newSync();
2451 processor.query(this, request, parent, procedure, listener);
2453 // newGraph.waitAsync(syncParent);
2457 // Object syncParent = request;
2459 // final ReadGraphImpl newGraph = newSync();
2463 request.perform(this, new AsyncMultiProcedure<T>() {
2466 public void execute(AsyncReadGraph graph, T result) {
2467 procedure.execute(graph, result);
2471 public void finished(AsyncReadGraph graph) {
2472 procedure.finished(graph);
2476 public void exception(AsyncReadGraph graph, Throwable t) {
2477 procedure.exception(graph, t);
2481 public String toString() {
2482 return "syncRequest(AsyncMultiRead) -> " + procedure;
2487 } catch (Throwable t) {
2499 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2500 final SyncMultiProcedure<T> procedure) {
2501 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2505 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2506 final MultiProcedure<T> procedure) {
2507 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2511 public <T> T syncRequest(final ExternalRead<T> request)
2512 throws DatabaseException {
2514 assert (request != null);
2516 return syncRequest(request, new Procedure<T>() {
2519 public void execute(T t) {
2523 public void exception(Throwable t) {
2527 public String toString() {
2528 return "syncRequest(AsyncRead) -> " + request;
2536 public <T> T syncRequest(ExternalRead<T> request, Listener<T> procedure) throws DatabaseException {
2537 return syncRequest(request, (Procedure<T>) procedure);
2541 final public <T> T syncRequest(final ExternalRead<T> request,
2542 final Procedure<T> procedure) throws DatabaseException {
2544 assert (request != null);
2546 ListenerBase listener = getListenerBase(procedure);
2548 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2549 final DataContainer<T> result = new DataContainer<T>();
2551 processor.query(this, request, parent, new Procedure<T>() {
2554 public void exception(Throwable throwable) {
2555 exception.set(throwable);
2556 procedure.exception(throwable);
2560 public void execute(T t) {
2562 procedure.execute(t);
2569 // if (parent != null || listener != null) {
2571 //// final ReadGraphImpl newGraph = newSync();
2574 //// newGraph.waitAsync(request);
2580 // T t = processor.tryQuery(this, request);
2582 // procedure.execute(t);
2584 // } catch (Throwable t) {
2586 // if (t instanceof DatabaseException) {
2587 // exception.set((DatabaseException)t);
2588 // procedure.exception(exception.get());
2590 // exception.set(new DatabaseException(
2591 // "Unexpected exception in ReadGraph.syncRequest(Read)",
2593 // procedure.exception(exception.get());
2600 Throwable t = exception.get();
2602 if (t instanceof DatabaseException)
2603 throw (DatabaseException) t;
2605 throw new DatabaseException(
2606 "Unexpected exception in ReadGraph.syncRequest(Read)",
2610 return result.get();
2615 public void syncRequest(final Write request) throws DatabaseException {
2617 assert (request != null);
2619 throw new DatabaseException(
2620 "Write operations are not supported during read transactions!");
2625 public <T> T syncRequest(final WriteResult<T> request) throws DatabaseException {
2627 assert (request != null);
2629 throw new DatabaseException(
2630 "Write operations are not supported during read transactions!");
2635 public void syncRequest(final DelayedWrite request)
2636 throws DatabaseException {
2638 assert (request != null);
2640 throw new DatabaseException(
2641 "Write operations are not supported during read transactions!");
2646 public <T> T syncRequest(final DelayedWriteResult<T> request) throws DatabaseException {
2648 assert (request != null);
2650 throw new DatabaseException(
2651 "Write operations are not supported during read transactions!");
2656 public void syncRequest(final WriteOnly request) throws DatabaseException {
2658 assert (request != null);
2660 throw new DatabaseException(
2661 "Write operations are not supported during read transactions!");
2666 public <T> T syncRequest(final WriteOnlyResult<T> request) throws DatabaseException {
2668 assert (request != null);
2670 throw new DatabaseException(
2671 "Write operations are not supported during read transactions!");
2676 public <T> void async(ReadInterface<T> r, AsyncProcedure<T> procedure) {
2677 r.request(this, procedure);
2681 public <T> void async(ReadInterface<T> r, Procedure<T> procedure) {
2682 r.request(this, procedure);
2686 public <T> void async(ReadInterface<T> r, SyncProcedure<T> procedure) {
2687 r.request(this, procedure);
2691 public <T> void async(ReadInterface<T> r, AsyncListener<T> procedure) {
2692 r.request(this, procedure);
2696 public <T> void async(ReadInterface<T> r, Listener<T> procedure) {
2697 r.request(this, procedure);
2701 public <T> void async(ReadInterface<T> r, SyncListener<T> procedure) {
2702 r.request(this, procedure);
2706 public <T> T sync(ReadInterface<T> r) throws DatabaseException {
2707 return r.request(this);
2711 public <T> T sync(WriteInterface<T> r) throws DatabaseException {
2712 return r.request(this);
2716 public <T> void async(WriteInterface<T> r, Procedure<T> procedure) {
2717 r.request(this, procedure);
2721 public <T> void async(WriteInterface<T> r) {
2722 r.request(this, new ProcedureAdapter<T>());
2726 * Implementation of the interface AsyncReadGraph
2730 public void forURI(Resource resource, AsyncListener<String> listener) {
2731 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2736 public void forURI(Resource resource, SyncListener<String> listener) {
2737 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2742 public void forURI(Resource resource, Listener<String> listener) {
2743 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2748 final public void forURI(final Resource resource,
2749 final AsyncProcedure<String> procedure) {
2751 assert (resource != null);
2752 assert (procedure != null);
2754 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2760 public void forURI(Resource resource, SyncProcedure<String> procedure) {
2761 forURI(resource, new SyncToAsyncProcedure<String>(procedure));
2765 public void forURI(Resource resource, Procedure<String> procedure) {
2766 forURI(resource, new NoneToAsyncProcedure<String>(procedure));
2770 public void forResource(String id, AsyncListener<Resource> listener) {
2771 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2776 public void forResource(String id, SyncListener<Resource> listener) {
2777 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2782 public void forResource(String id, Listener<Resource> listener) {
2783 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2788 final public void forResource(final String id,
2789 final AsyncProcedure<Resource> procedure) {
2791 assert (id != null);
2792 assert (procedure != null);
2794 processor.forResource(this, id, procedure);
2799 public void forResource(String id, SyncProcedure<Resource> procedure) {
2800 forResource(id, new SyncToAsyncProcedure<Resource>(procedure));
2804 public void forResource(String id, Procedure<Resource> procedure) {
2805 forResource(id, new NoneToAsyncProcedure<Resource>(procedure));
2809 public void forBuiltin(String id, AsyncListener<Resource> listener) {
2810 asyncRequest(new Builtin(id), listener);
2814 public void forBuiltin(String id, SyncListener<Resource> listener) {
2815 asyncRequest(new Builtin(id), listener);
2819 public void forBuiltin(String id, Listener<Resource> listener) {
2820 asyncRequest(new Builtin(id), listener);
2824 final public void forBuiltin(final String id,
2825 final AsyncProcedure<Resource> procedure) {
2827 assert (id != null);
2828 assert (procedure != null);
2830 processor.forBuiltin(this, id, procedure);
2835 public void forBuiltin(String id, SyncProcedure<Resource> procedure) {
2836 forBuiltin(id, new SyncToAsyncProcedure<Resource>(procedure));
2840 public void forBuiltin(String id, Procedure<Resource> procedure) {
2841 forBuiltin(id, new NoneToAsyncProcedure<Resource>(procedure));
2845 final public void forEachStatement(Resource subject, Resource relation,
2846 AsyncMultiProcedure<Statement> procedure) {
2848 assert (subject != null);
2849 assert (relation != null);
2850 assert (procedure != null);
2852 processor.forEachStatement(this, subject, relation, procedure);
2857 public void forEachStatement(Resource subject, Resource relation,
2858 SyncMultiProcedure<Statement> procedure) {
2859 forEachStatement(subject, relation,
2860 new SyncToAsyncMultiProcedure<Statement>(procedure));
2864 final public void forEachStatement(Resource subject, Resource relation,
2865 MultiProcedure<Statement> procedure) {
2867 assert (subject != null);
2868 assert (relation != null);
2869 assert (procedure != null);
2871 processor.forEachStatement(this, subject, relation, procedure);
2876 final public void forStatementSet(Resource subject, Resource relation,
2877 AsyncSetListener<Statement> procedure) {
2879 assert (subject != null);
2880 assert (relation != null);
2881 assert (procedure != null);
2883 processor.forStatementSet(this, subject, relation, procedure);
2888 final public void forStatementSet(Resource subject, Resource relation,
2889 SyncSetListener<Statement> procedure) {
2890 forStatementSet(subject, relation,
2891 new SyncToAsyncSetProcedure<Statement>(procedure));
2895 public void forStatementSet(Resource subject, Resource relation,
2896 SetListener<Statement> listener) {
2897 forStatementSet(subject, relation,
2898 new NoneToAsyncSetProcedure<Statement>(listener));
2902 final public void forEachAssertedStatement(final Resource subject,
2903 final Resource relation,
2904 final AsyncMultiProcedure<Statement> procedure) {
2906 assert (subject != null);
2907 assert (relation != null);
2908 assert (procedure != null);
2910 processor.forEachAssertedStatement(this, subject, relation, procedure);
2915 public void forEachAssertedStatement(Resource subject, Resource relation,
2916 SyncMultiProcedure<Statement> procedure) {
2917 forEachAssertedStatement(subject, relation,
2918 new SyncToAsyncMultiProcedure<Statement>(procedure));
2922 public void forEachAssertedStatement(Resource subject, Resource relation,
2923 MultiProcedure<Statement> procedure) {
2924 forEachAssertedStatement(subject, relation,
2925 new NoneToAsyncMultiProcedure<Statement>(procedure));
2929 public void forAssertedStatementSet(Resource subject, Resource relation,
2930 AsyncSetListener<Statement> procedure) {
2932 assert (subject != null);
2933 assert (relation != null);
2934 assert (procedure != null);
2936 processor.forAssertedStatementSet(this, subject, relation, procedure);
2941 public void forAssertedStatementSet(Resource subject, Resource relation,
2942 SyncSetListener<Statement> procedure) {
2944 assert (subject != null);
2945 assert (relation != null);
2946 assert (procedure != null);
2948 forAssertedStatementSet(subject, relation,
2949 new SyncToAsyncSetProcedure<Statement>(procedure));
2954 public void forAssertedStatementSet(Resource subject, Resource relation,
2955 SetListener<Statement> procedure) {
2957 assert (subject != null);
2958 assert (relation != null);
2959 assert (procedure != null);
2961 forAssertedStatementSet(subject, relation,
2962 new NoneToAsyncSetProcedure<Statement>(procedure));
2967 final public void forEachPredicate(final Resource subject,
2968 final AsyncMultiProcedure<Resource> procedure) {
2970 assert (subject != null);
2971 assert (procedure != null);
2973 processor.forEachPredicate(this, subject, procedure);
2978 public void forEachPredicate(Resource subject,
2979 SyncMultiProcedure<Resource> procedure) {
2980 forEachPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(
2985 final public void forEachPredicate(final Resource subject,
2986 final MultiProcedure<Resource> procedure) {
2988 assert (subject != null);
2989 assert (procedure != null);
2991 processor.forEachPredicate(this, subject, procedure);
2996 final public void forPredicateSet(final Resource subject,
2997 final AsyncSetListener<Resource> procedure) {
2999 assert (subject != null);
3000 assert (procedure != null);
3002 processor.forPredicateSet(this, subject, procedure);
3007 final public void forPredicateSet(final Resource subject,
3008 final SyncSetListener<Resource> procedure) {
3010 assert (subject != null);
3011 assert (procedure != null);
3013 forPredicateSet(subject, new SyncToAsyncSetProcedure<Resource>(
3019 final public void forPredicateSet(final Resource subject,
3020 final SetListener<Resource> procedure) {
3022 assert (subject != null);
3023 assert (procedure != null);
3025 forPredicateSet(subject, new NoneToAsyncSetProcedure<Resource>(
3031 final public void forEachPrincipalType(final Resource subject,
3032 final AsyncMultiProcedure<Resource> procedure) {
3034 assert (subject != null);
3035 assert (procedure != null);
3037 processor.forEachPrincipalType(this, subject, procedure);
3042 public void forEachPrincipalType(Resource subject,
3043 SyncMultiProcedure<Resource> procedure) {
3044 forEachPrincipalType(subject, new SyncToAsyncMultiProcedure<Resource>(
3049 final public void forEachPrincipalType(final Resource subject,
3050 final MultiProcedure<Resource> procedure) {
3052 assert (subject != null);
3053 assert (procedure != null);
3055 processor.forEachPrincipalType(this, subject, procedure);
3060 final public void forPrincipalTypeSet(final Resource subject,
3061 final AsyncSetListener<Resource> procedure) {
3063 assert (subject != null);
3064 assert (procedure != null);
3066 processor.forPrincipalTypeSet(this, subject, procedure);
3071 final public void forPrincipalTypeSet(final Resource subject,
3072 final SyncSetListener<Resource> procedure) {
3074 assert (subject != null);
3075 assert (procedure != null);
3077 forPrincipalTypeSet(subject, new SyncToAsyncSetProcedure<Resource>(
3083 final public void forPrincipalTypeSet(final Resource subject,
3084 final SetListener<Resource> procedure) {
3086 assert (subject != null);
3087 assert (procedure != null);
3089 forPrincipalTypeSet(subject, new NoneToAsyncSetProcedure<Resource>(
3095 public void forTypes(Resource subject, AsyncListener<Set<Resource>> listener) {
3096 asyncRequest(new Types(subject), listener);
3100 public void forTypes(Resource subject, SyncListener<Set<Resource>> listener) {
3101 asyncRequest(new Types(subject), listener);
3105 public void forTypes(Resource subject, Listener<Set<Resource>> listener) {
3106 asyncRequest(new Types(subject), listener);
3110 final public void forTypes(final Resource subject,
3111 final AsyncProcedure<Set<Resource>> procedure) {
3113 assert (subject != null);
3114 assert (procedure != null);
3116 processor.forTypes(this, subject, procedure);
3121 public void forTypes(Resource subject,
3122 SyncProcedure<Set<Resource>> procedure) {
3123 forTypes(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
3127 public void forTypes(Resource subject, Procedure<Set<Resource>> procedure) {
3128 forTypes(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
3132 public void forSupertypes(Resource subject,
3133 AsyncListener<Set<Resource>> listener) {
3134 asyncRequest(new Types(subject), listener);
3138 public void forSupertypes(Resource subject,
3139 SyncListener<Set<Resource>> listener) {
3140 asyncRequest(new Types(subject), listener);
3144 public void forSupertypes(Resource subject, Listener<Set<Resource>> listener) {
3145 asyncRequest(new Types(subject), listener);
3149 final public void forSupertypes(final Resource subject,
3150 final AsyncProcedure<Set<Resource>> procedure) {
3152 assert (subject != null);
3153 assert (procedure != null);
3155 processor.forSupertypes(this, subject, procedure);
3160 public void forSupertypes(Resource subject,
3161 SyncProcedure<Set<Resource>> procedure) {
3162 forSupertypes(subject, new SyncToAsyncProcedure<Set<Resource>>(
3167 public void forSupertypes(Resource subject,
3168 Procedure<Set<Resource>> procedure) {
3169 forSupertypes(subject, new NoneToAsyncProcedure<Set<Resource>>(
3174 public void forDirectSuperrelations(Resource subject,
3175 AsyncMultiProcedure<Resource> procedure) {
3177 assert (subject != null);
3178 assert (procedure != null);
3180 processor.forDirectSuperrelations(this, subject, procedure);
3185 public void forPossibleSuperrelation(Resource subject, AsyncProcedure<Resource> procedure) {
3187 assert (subject != null);
3188 assert (procedure != null);
3190 processor.forPossibleSuperrelation(this, subject, procedure);
3195 public void forSuperrelations(Resource subject,
3196 AsyncListener<Set<Resource>> listener) {
3197 asyncRequest(new Types(subject), listener);
3201 public void forSuperrelations(Resource subject,
3202 SyncListener<Set<Resource>> listener) {
3203 asyncRequest(new Types(subject), listener);
3207 public void forSuperrelations(Resource subject,
3208 Listener<Set<Resource>> listener) {
3209 asyncRequest(new Types(subject), listener);
3213 final public void forSuperrelations(final Resource subject,
3214 final AsyncProcedure<Set<Resource>> procedure) {
3216 assert (subject != null);
3217 assert (procedure != null);
3219 processor.forSuperrelations(this, subject, procedure);
3224 public void forSuperrelations(Resource subject,
3225 SyncProcedure<Set<Resource>> procedure) {
3226 forSuperrelations(subject, new SyncToAsyncProcedure<Set<Resource>>(
3231 public void forSuperrelations(Resource subject,
3232 Procedure<Set<Resource>> procedure) {
3233 forSuperrelations(subject, new NoneToAsyncProcedure<Set<Resource>>(
3238 final public void forEachObject(final Resource subject, final Resource relation, final AsyncMultiProcedure<Resource> procedure) {
3239 processor.forEachObject(this, subject, relation, procedure);
3243 public void forEachObject(Resource subject, Resource relation,
3244 SyncMultiProcedure<Resource> procedure) {
3245 forEachObject(subject, relation,
3246 new SyncToAsyncMultiProcedure<Resource>(procedure));
3250 public void forEachObject(Resource subject, Resource relation,
3251 MultiProcedure<Resource> procedure) {
3253 processor.forEachObject(this, subject, relation, procedure);
3258 // final public void forEachDirectObject(final Resource subject,
3259 // final Resource relation,
3260 // final AsyncMultiProcedure<Resource> procedure) {
3262 // processor.forEachDirectObject(this, subject, relation, procedure);
3267 // public void forEachDirectObject(Resource subject, Resource relation,
3268 // SyncMultiProcedure<Resource> procedure) {
3269 // forEachDirectObject(subject, relation,
3270 // new SyncToAsyncMultiProcedure<Resource>(procedure));
3274 // public void forEachDirectObject(Resource subject, Resource relation,
3275 // MultiProcedure<Resource> procedure) {
3276 // forEachDirectObject(subject, relation,
3277 // new NoneToAsyncMultiProcedure<Resource>(procedure));
3281 final public void forEachDirectPredicate(final Resource subject, final AsyncMultiProcedure<Resource> procedure) {
3282 processor.forEachDirectPredicate(this, subject, procedure);
3286 public void forEachDirectPredicate(Resource subject, SyncMultiProcedure<Resource> procedure) {
3287 forEachDirectPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(procedure));
3291 public void forEachDirectPredicate(Resource subject, MultiProcedure<Resource> procedure) {
3292 forEachDirectPredicate(subject, new NoneToAsyncMultiProcedure<Resource>(procedure));
3296 final public void forObjectSet(final Resource subject,
3297 final Resource relation, final AsyncSetListener<Resource> procedure) {
3299 assert (subject != null);
3300 assert (relation != null);
3301 assert (procedure != null);
3303 processor.forObjectSet(this, subject, relation, procedure);
3308 final public void forObjectSet(final Resource subject,
3309 final Resource relation, final SyncSetListener<Resource> procedure) {
3311 assert (subject != null);
3312 assert (relation != null);
3313 assert (procedure != null);
3315 forObjectSet(subject, relation, new SyncToAsyncSetProcedure<Resource>(
3321 final public void forObjectSet(final Resource subject,
3322 final Resource relation, final SetListener<Resource> procedure) {
3324 assert (subject != null);
3325 assert (relation != null);
3326 assert (procedure != null);
3328 forObjectSet(subject, relation, new NoneToAsyncSetProcedure<Resource>(
3334 final public void forEachAssertedObject(final Resource subject,
3335 final Resource relation,
3336 final AsyncMultiProcedure<Resource> procedure) {
3338 assert (subject != null);
3339 assert (relation != null);
3340 assert (procedure != null);
3342 processor.forEachAssertedObject(this, subject, relation, procedure);
3347 public void forEachAssertedObject(Resource subject, Resource relation,
3348 SyncMultiProcedure<Resource> procedure) {
3350 assert (subject != null);
3351 assert (relation != null);
3352 assert (procedure != null);
3354 forEachAssertedObject(subject, relation,
3355 new SyncToAsyncMultiProcedure<Resource>(procedure));
3360 public void forEachAssertedObject(Resource subject, Resource relation,
3361 MultiProcedure<Resource> procedure) {
3363 assert (subject != null);
3364 assert (relation != null);
3365 assert (procedure != null);
3367 forEachAssertedObject(subject, relation,
3368 new NoneToAsyncMultiProcedure<Resource>(procedure));
3373 public void forAssertedObjectSet(Resource subject, Resource relation,
3374 AsyncSetListener<Resource> procedure) {
3376 assert (subject != null);
3377 assert (relation != null);
3378 assert (procedure != null);
3380 processor.forAssertedObjectSet(this, subject, relation, procedure);
3385 public void forAssertedObjectSet(Resource subject, Resource relation,
3386 SyncSetListener<Resource> procedure) {
3388 assert (subject != null);
3389 assert (relation != null);
3390 assert (procedure != null);
3392 forAssertedObjectSet(subject, relation,
3393 new SyncToAsyncSetProcedure<Resource>(procedure));
3398 public void forAssertedObjectSet(Resource subject, Resource relation,
3399 SetListener<Resource> procedure) {
3401 assert (subject != null);
3402 assert (relation != null);
3403 assert (procedure != null);
3405 forAssertedObjectSet(subject, relation,
3406 new NoneToAsyncSetProcedure<Resource>(procedure));
3411 public void forInverse(Resource relation, AsyncListener<Resource> listener) {
3412 asyncRequest(new Inverse(relation), listener);
3416 public void forInverse(Resource relation, SyncListener<Resource> listener) {
3417 asyncRequest(new Inverse(relation), listener);
3421 public void forInverse(Resource relation, Listener<Resource> listener) {
3422 asyncRequest(new Inverse(relation), listener);
3426 final public void forInverse(final Resource relation,
3427 final AsyncProcedure<Resource> procedure) {
3429 assert (relation != null);
3430 assert (procedure != null);
3432 processor.forInverse(this, relation, new AsyncProcedure<Resource>() {
3435 public void execute(AsyncReadGraph graph, Resource result) {
3437 procedure.execute(graph, result);
3439 procedure.exception(graph, new NoInverseException(relation
3445 public void exception(AsyncReadGraph graph, Throwable throwable) {
3446 procedure.exception(graph, throwable);
3450 public String toString() {
3451 return "forInverse -> " + procedure;
3459 public void forInverse(Resource relation, SyncProcedure<Resource> procedure) {
3460 forInverse(relation, new SyncToAsyncProcedure<Resource>(procedure));
3464 public void forInverse(Resource relation, Procedure<Resource> procedure) {
3465 forInverse(relation, new NoneToAsyncProcedure<Resource>(procedure));
3469 public void forSingleObject(Resource subject, Resource relation,
3470 AsyncListener<Resource> listener) {
3471 asyncRequest(new SingleObject(subject, relation), listener);
3475 public void forSingleObject(Resource subject, Resource relation,
3476 SyncListener<Resource> listener) {
3477 asyncRequest(new SingleObject(subject, relation), listener);
3481 public void forSingleObject(Resource subject, Resource relation,
3482 Listener<Resource> listener) {
3483 asyncRequest(new SingleObject(subject, relation), listener);
3487 final public void forSingleObject(final Resource subject,
3488 final Resource relation, final AsyncProcedure<Resource> procedure) {
3490 assert (subject != null);
3491 assert (relation != null);
3492 assert (procedure != null);
3494 processor.forEachObject(this, subject, relation,
3495 new SingleOrErrorProcedure<Resource>(procedure));
3500 public void forSingleObject(Resource subject, Resource relation,
3501 SyncProcedure<Resource> procedure) {
3502 forSingleObject(subject, relation, new SyncToAsyncProcedure<Resource>(
3507 public void forSingleObject(Resource subject, Resource relation,
3508 Procedure<Resource> procedure) {
3509 forSingleObject(subject, relation, new NoneToAsyncProcedure<Resource>(
3514 public void forSingleStatement(Resource subject, Resource relation,
3515 AsyncListener<Statement> listener) {
3516 asyncRequest(new SingleStatement(subject, relation), listener);
3520 public void forSingleStatement(Resource subject, Resource relation,
3521 SyncListener<Statement> listener) {
3522 asyncRequest(new SingleStatement(subject, relation), listener);
3526 public void forSingleStatement(Resource subject, Resource relation,
3527 Listener<Statement> listener) {
3528 asyncRequest(new SingleStatement(subject, relation), listener);
3532 final public void forSingleStatement(final Resource subject,
3533 final Resource relation, final AsyncProcedure<Statement> procedure) {
3535 assert (subject != null);
3536 assert (relation != null);
3537 assert (procedure != null);
3539 processor.forEachStatement(this, subject, relation,
3540 new SingleOrErrorProcedure<Statement>(procedure));
3545 public void forSingleStatement(Resource subject, Resource relation,
3546 SyncProcedure<Statement> procedure) {
3547 forSingleStatement(subject, relation,
3548 new SyncToAsyncProcedure<Statement>(procedure));
3552 public void forSingleStatement(Resource subject, Resource relation,
3553 Procedure<Statement> procedure) {
3554 forSingleStatement(subject, relation,
3555 new NoneToAsyncProcedure<Statement>(procedure));
3559 public void forSingleType(Resource subject,
3560 AsyncListener<Resource> listener) {
3561 asyncRequest(new SingleTypeAny(subject), listener);
3565 public void forSingleType(Resource subject,
3566 SyncListener<Resource> listener) {
3567 asyncRequest(new SingleTypeAny(subject), listener);
3571 public void forSingleType(Resource subject,
3572 Listener<Resource> listener) {
3573 asyncRequest(new SingleTypeAny(subject), listener);
3577 final public void forSingleType(final Resource subject, final AsyncProcedure<Resource> procedure) {
3579 assert (subject != null);
3580 assert (procedure != null);
3582 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3584 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
3587 public void execute(AsyncReadGraph graph, final Resource principalType) {
3588 checkedProcedure.offer(graph, principalType);
3592 public void finished(AsyncReadGraph graph) {
3593 checkedProcedure.dec(graph);
3597 public void exception(AsyncReadGraph graph, Throwable t) {
3598 checkedProcedure.exception(graph, t);
3602 public String toString() {
3603 return "forSingleType -> " + procedure;
3611 public void forSingleType(Resource subject, SyncProcedure<Resource> procedure) {
3612 forSingleType(subject, new SyncToAsyncProcedure<Resource>(
3617 public void forSingleType(Resource subject, Procedure<Resource> procedure) {
3618 forSingleType(subject, new NoneToAsyncProcedure<Resource>(
3623 public void forSingleType(Resource subject, Resource relation,
3624 AsyncListener<Resource> listener) {
3625 asyncRequest(new SingleType(subject, relation), listener);
3629 public void forSingleType(Resource subject, Resource relation,
3630 SyncListener<Resource> listener) {
3631 asyncRequest(new SingleType(subject, relation), listener);
3635 public void forSingleType(Resource subject, Resource relation,
3636 Listener<Resource> listener) {
3637 asyncRequest(new SingleType(subject, relation), listener);
3641 final public void forSingleType(final Resource subject,
3642 final Resource baseType, final AsyncProcedure<Resource> procedure) {
3644 assert (subject != null);
3645 assert (procedure != null);
3647 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3649 processor.forEachPrincipalType(this, subject,
3650 new AsyncMultiProcedureAdapter<Resource>() {
3653 public void execute(AsyncReadGraph graph,
3654 final Resource principalType) {
3656 checkedProcedure.inc();
3658 if(baseType == null) {
3660 checkedProcedure.offer(graph, principalType);
3661 checkedProcedure.dec(graph);
3663 } else if(principalType.equals(baseType)) {
3665 checkedProcedure.offer(graph, principalType);
3666 checkedProcedure.dec(graph);
3670 processor.forSupertypes((ReadGraphImpl)graph, principalType,
3671 new AsyncProcedure<Set<Resource>>() {
3674 public void execute(
3675 AsyncReadGraph graph,
3676 Set<Resource> result) {
3678 if (result.contains(baseType))
3679 checkedProcedure.offer(graph,
3681 checkedProcedure.dec(graph);
3686 public void exception(
3687 AsyncReadGraph graph,
3690 .exception(graph, t);
3700 public void finished(AsyncReadGraph graph) {
3701 checkedProcedure.dec(graph);
3705 public void exception(AsyncReadGraph graph, Throwable t) {
3706 checkedProcedure.exception(graph, t);
3710 public String toString() {
3711 return "forSingleType -> " + procedure;
3719 public void forSingleType(Resource subject, Resource relation,
3720 SyncProcedure<Resource> procedure) {
3721 forSingleType(subject, relation, new SyncToAsyncProcedure<Resource>(
3726 public void forSingleType(Resource subject, Resource relation,
3727 Procedure<Resource> procedure) {
3728 forSingleType(subject, relation, new NoneToAsyncProcedure<Resource>(
3733 public <T> void forValue(Resource subject, Binding binding,
3734 AsyncListener<T> listener) {
3735 asyncRequest(new Value<T>(subject, binding), listener);
3739 public <T> void forValue(Resource subject, Binding binding,
3740 SyncListener<T> listener) {
3741 asyncRequest(new Value<T>(subject, binding), listener);
3745 public <T> void forValue(Resource subject, Binding binding,
3746 Listener<T> listener) {
3747 asyncRequest(new Value<T>(subject, binding), listener);
3751 public <T> void forValue(final Resource resource, final Binding binding,
3752 final AsyncProcedure<T> procedure) {
3754 assert (resource != null);
3755 assert (binding != null);
3756 assert (procedure != null);
3758 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
3761 public void execute(AsyncReadGraph graph, byte[] result) {
3765 if (result == null) {
3766 procedure.exception(graph,
3767 new DoesNotContainValueException(
3768 "No value for resource " + resource));
3772 Serializer serializer = binding.serializer();
3773 // Serializer serializer = Bindings.getSerializer( binding );
3774 Object obj = serializer.deserialize(result);
3775 // if (!binding.isInstance(obj))
3776 // procedure.exception(graph, new ClassCastException(
3777 // "Cannot get value " + obj + " with binding "
3780 procedure.execute(graph, (T) obj);
3782 } catch (IOException e) {
3783 procedure.exception(graph, e);
3784 } catch (BufferUnderflowException e) {
3785 procedure.exception(graph, e);
3786 } catch (Throwable t) {
3787 procedure.exception(graph, t);
3793 public void exception(AsyncReadGraph graph, Throwable t) {
3795 procedure.exception(graph, t);
3796 } catch (Throwable t2) {
3797 Logger.defaultLogError(t2);
3802 public String toString() {
3803 return "forValue -> " + procedure;
3811 public <T> void forValue(Resource subject, Binding binding,
3812 SyncProcedure<T> procedure) {
3813 forValue(subject, binding, new SyncToAsyncProcedure<T>(procedure));
3817 public <T> void forValue(Resource subject, Binding binding,
3818 Procedure<T> procedure) {
3819 forValue(subject, binding, new NoneToAsyncProcedure<T>(procedure));
3823 public <T> void forValue(Resource subject, AsyncListener<T> listener) {
3824 asyncRequest(new ValueImplied<T>(subject), listener);
3828 public <T> void forValue(Resource subject, SyncListener<T> listener) {
3829 asyncRequest(new ValueImplied<T>(subject), listener);
3833 public <T> void forValue(Resource subject, Listener<T> listener) {
3834 asyncRequest(new ValueImplied<T>(subject), listener);
3838 final public <T> void forValue(final Resource subject, final AsyncProcedure<T> procedure) {
3840 assert (subject != null);
3841 assert (procedure != null);
3843 forRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
3846 public void execute(AsyncReadGraph graph, Datatype type) {
3847 // TODO: consider trying Bindings.getBeanBinding(type);
3848 Binding binding = Bindings.getBinding(type);
3849 graph.forValue(subject, binding, procedure);
3853 public void exception(AsyncReadGraph graph, Throwable throwable) {
3854 procedure.exception(graph, new DoesNotContainValueException("Invalid data type", throwable));
3862 public <T> void forValue(Resource subject, SyncProcedure<T> procedure) {
3863 forValue(subject, new SyncToAsyncProcedure<T>(procedure));
3867 public <T> void forValue(Resource subject, Procedure<T> procedure) {
3868 forValue(subject, new NoneToAsyncProcedure<T>(procedure));
3872 public <T> void forRelatedValue(Resource subject, Resource relation,
3873 AsyncListener<T> listener) {
3874 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3878 public <T> void forRelatedValue(Resource subject, Resource relation,
3879 SyncListener<T> listener) {
3880 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3884 public <T> void forRelatedValue(Resource subject, Resource relation,
3885 Listener<T> listener) {
3886 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3890 final public <T> void forRelatedValue(final Resource subject,
3891 final Resource relation, final AsyncProcedure<T> procedure) {
3893 assert (subject != null);
3894 assert (relation != null);
3895 assert (procedure != null);
3897 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3899 processor.forEachObject(this, subject, relation,
3900 new AsyncMultiProcedureAdapter<Resource>() {
3903 public void execute(AsyncReadGraph graph,
3904 final Resource object) {
3906 checkedProcedure.inc();
3908 graph.forValue(object, new AsyncProcedure<Object>() {
3911 public void execute(AsyncReadGraph graph,
3913 checkedProcedure.offer(graph, (T) result);
3914 checkedProcedure.dec(graph);
3918 public void exception(AsyncReadGraph graph,
3920 checkedProcedure.exception(graph, t);
3924 public String toString() {
3925 return "forRelatedValue -> " + procedure;
3933 public void finished(AsyncReadGraph graph) {
3934 checkedProcedure.dec(graph);
3938 public void exception(AsyncReadGraph graph, Throwable t) {
3939 checkedProcedure.exception(graph, t);
3947 public <T> void forRelatedValue(Resource subject, Resource relation,
3948 SyncProcedure<T> procedure) {
3949 forRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
3954 public <T> void forRelatedValue(Resource subject, Resource relation,
3955 Procedure<T> procedure) {
3956 forRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
3961 public <T> void forRelatedValue(Resource subject, Resource relation,
3962 Binding binding, AsyncListener<T> listener) {
3963 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3967 public <T> void forRelatedValue(Resource subject, Resource relation,
3968 Binding binding, SyncListener<T> listener) {
3969 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3973 public <T> void forRelatedValue(Resource subject, Resource relation,
3974 Binding binding, Listener<T> listener) {
3975 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3979 final public <T> void forRelatedValue(final Resource subject,
3980 final Resource relation, final Binding binding,
3981 final AsyncProcedure<T> procedure) {
3983 assert (subject != null);
3984 assert (relation != null);
3985 assert (binding != null);
3986 assert (procedure != null);
3988 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3990 processor.forEachObject(this, subject, relation,
3991 new AsyncMultiProcedureAdapter<Resource>() {
3994 public void execute(AsyncReadGraph graph,
3995 final Resource object) {
3997 checkedProcedure.inc();
3999 graph.forValue(object, binding, new AsyncProcedure<Object>() {
4002 public void execute(AsyncReadGraph graph,
4005 checkedProcedure.offer(graph,
4007 checkedProcedure.dec(graph);
4011 public void exception(AsyncReadGraph graph,
4013 checkedProcedure.exception(graph, t);
4017 public String toString() {
4018 return "forRelatedValue -> "
4027 public void finished(AsyncReadGraph graph) {
4028 checkedProcedure.dec(graph);
4032 public void exception(AsyncReadGraph graph, Throwable t) {
4033 checkedProcedure.exception(graph, t);
4041 public <T> void forRelatedValue(Resource subject, Resource relation,
4042 Binding binding, SyncProcedure<T> procedure) {
4043 forRelatedValue(subject, relation, binding,
4044 new SyncToAsyncProcedure<T>(procedure));
4048 public <T> void forRelatedValue(Resource subject, Resource relation,
4049 Binding binding, Procedure<T> procedure) {
4050 forRelatedValue(subject, relation, binding,
4051 new NoneToAsyncProcedure<T>(procedure));
4055 public <T> void forAdapted(Resource resource, Class<T> clazz,
4056 AsyncListener<T> listener) {
4057 asyncRequest(new Adapter<T>(resource, clazz), listener);
4061 public <T> void forAdapted(Resource resource, Class<T> clazz,
4062 SyncListener<T> listener) {
4063 asyncRequest(new Adapter<T>(resource, clazz), listener);
4067 public <T> void forAdapted(Resource resource, Class<T> clazz,
4068 Listener<T> listener) {
4069 asyncRequest(new Adapter<T>(resource, clazz), listener);
4073 final public <T> void forAdapted(final Resource resource,
4074 final Class<T> clazz, final AsyncProcedure<T> procedure) {
4076 assert (resource != null);
4077 assert (clazz != null);
4078 assert (procedure != null);
4080 final AdaptionService service = getSession().peekService(AdaptionService.class);
4081 if (service == null)
4082 procedure.exception(this, new ServiceException("No AdaptionService available"));
4084 service.adapt(this, resource, resource, Resource.class, clazz, false, procedure);
4089 public <T> void forAdapted(Resource resource, Class<T> clazz,
4090 SyncProcedure<T> procedure) {
4091 forAdapted(resource, clazz, new SyncToAsyncProcedure<T>(procedure));
4095 public <T> void forAdapted(Resource resource, Class<T> clazz,
4096 Procedure<T> procedure) {
4097 forAdapted(resource, clazz, new NoneToAsyncProcedure<T>(procedure));
4101 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
4102 AsyncListener<T> listener) {
4103 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
4107 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
4108 SyncListener<T> listener) {
4109 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
4113 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
4114 Listener<T> listener) {
4115 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
4119 final public <T> void forUniqueAdapted(final Resource resource,
4120 final Class<T> clazz, final AsyncProcedure<T> procedure) {
4122 assert (resource != null);
4123 assert (clazz != null);
4124 assert (procedure != null);
4126 final AdaptionService service = getSession().peekService(AdaptionService.class);
4127 if (service == null)
4128 procedure.exception(this, new ServiceException("No AdaptionService available"));
4130 service.adaptNew(this, resource, clazz, false, procedure);
4135 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
4136 SyncProcedure<T> procedure) {
4137 forUniqueAdapted(resource, clazz,
4138 new SyncToAsyncProcedure<T>(procedure));
4142 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
4143 Procedure<T> procedure) {
4144 forUniqueAdapted(resource, clazz,
4145 new NoneToAsyncProcedure<T>(procedure));
4149 public void forPossibleInverse(Resource subject,
4150 AsyncListener<Resource> listener) {
4151 asyncRequest(new PossibleInverse(subject), listener);
4155 public void forPossibleInverse(Resource subject,
4156 SyncListener<Resource> listener) {
4157 asyncRequest(new PossibleInverse(subject), listener);
4161 public void forPossibleInverse(Resource subject, Listener<Resource> listener) {
4162 asyncRequest(new PossibleInverse(subject), listener);
4166 final public void forPossibleInverse(final Resource relation,
4167 final AsyncProcedure<Resource> procedure) {
4169 assert (relation != null);
4170 assert (procedure != null);
4172 processor.forInverse(this, relation, new ExceptionToNullProcedure<Resource>(procedure));
4177 public void forPossibleInverse(Resource subject,
4178 SyncProcedure<Resource> procedure) {
4179 forPossibleInverse(subject, new SyncToAsyncProcedure<Resource>(
4184 public void forPossibleInverse(Resource subject,
4185 Procedure<Resource> procedure) {
4186 forPossibleInverse(subject, new NoneToAsyncProcedure<Resource>(
4191 public void forPossibleObject(Resource subject, Resource relation,
4192 AsyncListener<Resource> listener) {
4193 asyncRequest(new PossibleObject(subject, relation), listener);
4197 public void forPossibleObject(Resource subject, Resource relation,
4198 SyncListener<Resource> listener) {
4199 asyncRequest(new PossibleObject(subject, relation), listener);
4203 public void forPossibleObject(Resource subject, Resource relation,
4204 Listener<Resource> listener) {
4205 asyncRequest(new PossibleObject(subject, relation), listener);
4209 final public void forPossibleObject(final Resource subject,
4210 final Resource relation, final AsyncProcedure<Resource> procedure) {
4212 assert (subject != null);
4213 assert (relation != null);
4214 assert (procedure != null);
4216 processor.forEachObject(this, subject, relation,
4217 new SingleOrNullProcedure<Resource>(procedure));
4222 public void forPossibleObject(Resource subject, Resource relation,
4223 SyncProcedure<Resource> procedure) {
4224 forPossibleObject(subject, relation,
4225 new SyncToAsyncProcedure<Resource>(procedure));
4229 public void forPossibleObject(Resource subject, Resource relation,
4230 Procedure<Resource> procedure) {
4231 forPossibleObject(subject, relation,
4232 new NoneToAsyncProcedure<Resource>(procedure));
4236 public void forPossibleStatement(Resource subject, Resource relation,
4237 AsyncListener<Statement> listener) {
4238 asyncRequest(new PossibleStatement(subject, relation), listener);
4242 public void forPossibleStatement(Resource subject, Resource relation,
4243 SyncListener<Statement> listener) {
4244 asyncRequest(new PossibleStatement(subject, relation), listener);
4248 public void forPossibleStatement(Resource subject, Resource relation,
4249 Listener<Statement> listener) {
4250 asyncRequest(new PossibleStatement(subject, relation), listener);
4254 final public void forPossibleStatement(final Resource subject,
4255 final Resource relation, final AsyncProcedure<Statement> procedure) {
4257 assert (subject != null);
4258 assert (relation != null);
4259 assert (procedure != null);
4261 processor.forEachStatement(this, subject, relation,
4262 new SingleFunctionalOrNullProcedure<Statement>(
4263 "forPossibleStatement", procedure));
4268 public void forPossibleStatement(Resource subject, Resource relation,
4269 SyncProcedure<Statement> procedure) {
4270 forPossibleStatement(subject, relation,
4271 new SyncToAsyncProcedure<Statement>(procedure));
4275 public void forPossibleStatement(Resource subject, Resource relation,
4276 Procedure<Statement> procedure) {
4277 forPossibleStatement(subject, relation,
4278 new NoneToAsyncProcedure<Statement>(procedure));
4282 public void forPossibleType(Resource subject, Resource relation,
4283 AsyncListener<Resource> listener) {
4284 asyncRequest(new PossibleType(subject, relation), listener);
4288 public void forPossibleType(Resource subject, Resource relation,
4289 SyncListener<Resource> listener) {
4290 asyncRequest(new PossibleType(subject, relation), listener);
4294 public void forPossibleType(Resource subject, Resource relation,
4295 Listener<Resource> listener) {
4296 asyncRequest(new PossibleType(subject, relation), listener);
4300 final public void forPossibleType(final Resource subject,
4301 final Resource baseType, final AsyncProcedure<Resource> procedure) {
4303 assert (subject != null);
4304 assert (procedure != null);
4306 final NullSingleOrNullProcedure<Resource> checkedProcedure = new NullSingleOrNullProcedure<Resource>(procedure);
4308 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
4311 public void execute(AsyncReadGraph graph,
4312 final Resource principalType) {
4314 if (baseType == null) {
4316 checkedProcedure.offer(graph, principalType);
4318 } else if (principalType.equals(baseType)) {
4320 checkedProcedure.offer(graph, principalType);
4324 checkedProcedure.inc();
4326 processor.forSupertypes((ReadGraphImpl)graph, principalType,
4327 new AsyncProcedure<Set<Resource>>() {
4330 public void execute(
4331 AsyncReadGraph graph,
4332 Set<Resource> result) {
4334 if (result.contains(baseType)) {
4335 checkedProcedure.offer(graph,
4339 checkedProcedure.dec(graph);
4344 public void exception(
4345 AsyncReadGraph graph,
4347 checkedProcedure.exception(graph, t);
4348 checkedProcedure.dec(graph);
4352 public String toString() {
4353 return "forPossibleType -> "
4364 public void finished(AsyncReadGraph graph) {
4365 checkedProcedure.dec(graph);
4369 public void exception(AsyncReadGraph graph, Throwable t) {
4370 checkedProcedure.exception(graph, t);
4371 checkedProcedure.dec(graph);
4379 public void forPossibleType(Resource subject, Resource relation,
4380 SyncProcedure<Resource> procedure) {
4381 forPossibleType(subject, relation, new SyncToAsyncProcedure<Resource>(
4386 public void forPossibleType(Resource subject, Resource relation,
4387 Procedure<Resource> procedure) {
4388 forPossibleType(subject, relation, new NoneToAsyncProcedure<Resource>(
4393 public <T> void forPossibleValue(Resource subject, AsyncListener<T> listener) {
4394 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4398 public <T> void forPossibleValue(Resource subject, SyncListener<T> listener) {
4399 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4403 public <T> void forPossibleValue(Resource subject, Listener<T> listener) {
4404 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4408 final public <T> void forPossibleValue(final Resource subject,
4409 final AsyncProcedure<T> procedure) {
4411 assert (subject != null);
4412 assert (procedure != null);
4414 forPossibleRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
4417 public void execute(AsyncReadGraph graph, final Datatype type) {
4419 procedure.execute(graph, null);
4422 // TODO: consider trying Bindings.getBeanBinding(type);
4423 Binding binding = Bindings.getBinding(type);
4424 graph.forPossibleValue(subject, binding, procedure);
4425 } catch (RuntimeBindingConstructionException e) {
4426 procedure.exception(graph, e);
4432 public void exception(AsyncReadGraph graph, Throwable t) {
4433 procedure.exception(graph, t);
4437 public String toString() {
4438 return "forPossibleValue -> " + procedure;
4446 public <T> void forPossibleValue(Resource subject,
4447 SyncProcedure<T> procedure) {
4448 forPossibleValue(subject, new SyncToAsyncProcedure<T>(procedure));
4452 public <T> void forPossibleValue(Resource subject, Procedure<T> procedure) {
4453 forPossibleValue(subject, new NoneToAsyncProcedure<T>(procedure));
4457 public <T> void forPossibleValue(Resource subject, Binding binding,
4458 AsyncListener<T> listener) {
4459 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4463 public <T> void forPossibleValue(Resource subject, Binding binding,
4464 SyncListener<T> listener) {
4465 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4469 public <T> void forPossibleValue(Resource subject, Binding binding,
4470 Listener<T> listener) {
4471 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4475 final public <T> void forPossibleValue(final Resource resource,
4476 final Binding binding, final AsyncProcedure<T> procedure) {
4478 assert (resource != null);
4479 assert (binding != null);
4480 assert (procedure != null);
4482 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
4485 public void execute(AsyncReadGraph graph, byte[] result) {
4489 if (result == null) {
4490 procedure.execute(graph, null);
4494 Serializer serializer = Bindings.getSerializer( binding );
4495 Object obj = serializer.deserialize(result);
4496 if (!binding.isInstance(obj))
4497 procedure.exception(graph, new ClassCastException(
4498 "Cannot get value " + obj + " with binding "
4501 procedure.execute(graph, (T) obj);
4503 } catch (IOException e) {
4504 procedure.exception(graph, e);
4505 } catch (BufferUnderflowException e) {
4506 procedure.exception(graph, e);
4507 } catch (Throwable t) {
4508 procedure.exception(graph, t);
4514 public void exception(AsyncReadGraph graph, Throwable t) {
4516 procedure.exception(graph, t);
4517 } catch (Throwable t2) {
4518 Logger.defaultLogError(t2);
4523 public String toString() {
4524 return "forPossibleValue -> " + procedure;
4532 public <T> void forPossibleValue(Resource subject, Binding binding,
4533 SyncProcedure<T> procedure) {
4534 forPossibleValue(subject, binding, new SyncToAsyncProcedure<T>(
4539 public <T> void forPossibleValue(Resource subject, Binding binding,
4540 Procedure<T> procedure) {
4541 forPossibleValue(subject, binding, new NoneToAsyncProcedure<T>(
4546 public <T> void forPossibleRelatedValue(Resource subject,
4547 Resource relation, AsyncListener<T> listener) {
4548 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4553 public <T> void forPossibleRelatedValue(Resource subject,
4554 Resource relation, SyncListener<T> listener) {
4555 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4560 public <T> void forPossibleRelatedValue(Resource subject,
4561 Resource relation, Listener<T> listener) {
4562 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4567 final public <T> void forPossibleRelatedValue(final Resource subject,
4568 final Resource relation, final AsyncProcedure<T> procedure) {
4570 assert (subject != null);
4571 assert (relation != null);
4572 assert (procedure != null);
4574 final DeepSingleOrNullProcedure<T> checkedProcedure = new DeepSingleOrNullProcedure<T>(procedure);
4576 processor.forEachObject(this, subject, relation,
4577 new AsyncMultiProcedureAdapter<Resource>() {
4580 public void execute(AsyncReadGraph graph,
4581 final Resource object) {
4583 checkedProcedure.inc();
4585 graph.forValue(object, new AsyncProcedure<Object>() {
4588 public void execute(AsyncReadGraph graph,
4590 checkedProcedure.offer(graph, (T) result);
4591 checkedProcedure.dec(graph);
4595 public void exception(AsyncReadGraph graph,
4597 checkedProcedure.exception(graph, t);
4598 checkedProcedure.dec(graph);
4606 public void finished(AsyncReadGraph graph) {
4608 checkedProcedure.dec(graph);
4612 public void exception(AsyncReadGraph graph, Throwable t) {
4613 checkedProcedure.exception(graph, t);
4614 checkedProcedure.dec(graph);
4618 public String toString() {
4619 return "forPossibleRelatedValue -> " + procedure;
4626 public <T> void forPossibleRelatedValue(Resource subject,
4627 Resource relation, SyncProcedure<T> procedure) {
4628 forPossibleRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
4633 public <T> void forPossibleRelatedValue(Resource subject,
4634 Resource relation, Procedure<T> procedure) {
4635 forPossibleRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
4640 public <T> void forPossibleRelatedValue(Resource subject,
4641 Resource relation, Binding binding, AsyncListener<T> listener) {
4642 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4647 public <T> void forPossibleRelatedValue(Resource subject,
4648 Resource relation, Binding binding, SyncListener<T> listener) {
4649 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4654 public <T> void forPossibleRelatedValue(Resource subject,
4655 Resource relation, Binding binding, Listener<T> listener) {
4656 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4661 // final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4662 // final AsyncProcedure<T> procedure) {
4664 // forPossibleRelatedValue(subject, relation, binding, procedure, false);
4668 final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4669 final AsyncProcedure<T> procedure) {
4671 assert (subject != null);
4672 assert (relation != null);
4673 assert (procedure != null);
4675 processor.forPossibleObject(this, subject, relation, new AsyncProcedure<Resource>() {
4678 public void execute(AsyncReadGraph graph, Resource object) {
4680 if(object == null) {
4681 procedure.execute(graph, null);
4685 processor.forPossibleValue((ReadGraphImpl)graph, object, new AsyncProcedure<byte[]>() {
4688 public void execute(AsyncReadGraph graph, byte[] bytes) {
4694 Serializer serializer = binding.serializer();
4695 Object obj = serializer.deserialize(bytes);
4696 if (!binding.isInstance(obj)) {
4697 procedure.exception(graph, new ClassCastException("Cannot get value " + obj + " with binding " + binding));
4699 procedure.execute(graph, (T) obj);
4702 } catch (Throwable t) {
4704 procedure.exception(graph, t);
4710 procedure.execute(graph, null);
4717 public void exception(AsyncReadGraph graph, Throwable t) {
4718 procedure.exception(graph, t);
4726 public void exception(AsyncReadGraph graph, Throwable throwable) {
4727 throwable.printStackTrace();
4728 procedure.exception(graph, throwable);
4736 public <T> void forPossibleRelatedValue(Resource subject,
4737 Resource relation, Binding binding, SyncProcedure<T> procedure) {
4738 forPossibleRelatedValue(subject, relation, binding,
4739 new SyncToAsyncProcedure<T>(procedure));
4743 public <T> void forPossibleRelatedValue(Resource subject,
4744 Resource relation, Binding binding, Procedure<T> procedure) {
4745 forPossibleRelatedValue(subject, relation, binding,
4746 new NoneToAsyncProcedure<T>(procedure));
4750 public void forIsInstanceOf(Resource subject, Resource relation,
4751 AsyncListener<Boolean> listener) {
4752 asyncRequest(new IsInstanceOf(subject, relation), listener);
4756 public void forIsInstanceOf(Resource subject, Resource relation,
4757 SyncListener<Boolean> listener) {
4758 asyncRequest(new IsInstanceOf(subject, relation), listener);
4762 public void forIsInstanceOf(Resource subject, Resource relation,
4763 Listener<Boolean> listener) {
4764 asyncRequest(new IsInstanceOf(subject, relation), listener);
4768 final public void forIsInstanceOf(final Resource resource,
4769 final Resource type, final AsyncProcedure<Boolean> procedure) {
4771 assert (resource != null);
4772 assert (type != null);
4773 assert (procedure != null);
4775 forTypes(resource, new AsyncProcedure<Set<Resource>>() {
4778 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4781 if (result.contains(type))
4782 procedure.execute(graph, true);
4784 procedure.execute(graph, false);
4785 } catch (Throwable t) {
4786 Logger.defaultLogError(t);
4791 public void exception(AsyncReadGraph graph, Throwable t) {
4793 procedure.exception(graph, t);
4794 } catch (Throwable t2) {
4795 Logger.defaultLogError(t2);
4800 public String toString() {
4801 return "forIsInstanceOf -> " + procedure;
4809 public void forIsInstanceOf(Resource subject, Resource relation,
4810 SyncProcedure<Boolean> procedure) {
4811 forIsInstanceOf(subject, relation, new SyncToAsyncProcedure<Boolean>(
4816 public void forIsInstanceOf(Resource subject, Resource relation,
4817 Procedure<Boolean> procedure) {
4818 forIsInstanceOf(subject, relation, new NoneToAsyncProcedure<Boolean>(
4823 public void forIsInheritedFrom(Resource subject, Resource relation,
4824 AsyncListener<Boolean> listener) {
4825 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4829 public void forIsInheritedFrom(Resource subject, Resource relation,
4830 SyncListener<Boolean> listener) {
4831 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4835 public void forIsInheritedFrom(Resource subject, Resource relation,
4836 Listener<Boolean> listener) {
4837 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4841 final public void forIsInheritedFrom(final Resource resource,
4842 final Resource type, final AsyncProcedure<Boolean> procedure) {
4844 assert (resource != null);
4845 assert (type != null);
4846 assert (procedure != null);
4848 if (resource.equals(type)) {
4850 procedure.execute(this, true);
4851 } catch (Throwable t) {
4852 Logger.defaultLogError(t);
4857 forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
4860 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4862 if (result.contains(type))
4863 procedure.execute(graph, true);
4865 procedure.execute(graph, false);
4866 } catch (Throwable t) {
4867 Logger.defaultLogError(t);
4872 public void exception(AsyncReadGraph graph, Throwable t) {
4874 procedure.exception(graph, t);
4875 } catch (Throwable t2) {
4876 Logger.defaultLogError(t2);
4881 public String toString() {
4882 return "forIsInheritedFrom -> " + procedure;
4890 public void forIsInheritedFrom(Resource subject, Resource relation,
4891 SyncProcedure<Boolean> procedure) {
4892 forIsInheritedFrom(subject, relation,
4893 new SyncToAsyncProcedure<Boolean>(procedure));
4897 public void forIsInheritedFrom(Resource subject, Resource relation,
4898 Procedure<Boolean> procedure) {
4899 forIsInheritedFrom(subject, relation,
4900 new NoneToAsyncProcedure<Boolean>(procedure));
4904 public void forIsSubrelationOf(Resource subject, Resource relation,
4905 AsyncListener<Boolean> listener) {
4906 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4910 public void forIsSubrelationOf(Resource subject, Resource relation,
4911 SyncListener<Boolean> listener) {
4912 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4916 public void forIsSubrelationOf(Resource subject, Resource relation,
4917 Listener<Boolean> listener) {
4918 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4922 final public void forIsSubrelationOf(final Resource resource,
4923 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4925 assert (resource != null);
4926 assert (relation != null);
4927 assert (procedure != null);
4929 if (resource.equals(relation)) {
4930 procedure.execute(this, true);
4934 forSuperrelations(resource, new AsyncProcedure<Set<Resource>>() {
4937 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4939 if (result.contains(relation))
4940 procedure.execute(graph, true);
4942 procedure.execute(graph, false);
4943 } catch (Throwable t) {
4944 Logger.defaultLogError(t);
4949 public void exception(AsyncReadGraph graph, Throwable t) {
4951 procedure.exception(graph, t);
4952 } catch (Throwable t2) {
4953 Logger.defaultLogError(t2);
4958 public String toString() {
4959 return "forIsSubrelationOf -> " + procedure;
4967 public void forIsSubrelationOf(Resource subject, Resource relation,
4968 SyncProcedure<Boolean> procedure) {
4969 forIsSubrelationOf(subject, relation,
4970 new SyncToAsyncProcedure<Boolean>(procedure));
4974 public void forIsSubrelationOf(Resource subject, Resource relation,
4975 Procedure<Boolean> procedure) {
4976 forIsSubrelationOf(subject, relation,
4977 new NoneToAsyncProcedure<Boolean>(procedure));
4981 public void forHasStatement(Resource subject,
4982 AsyncListener<Boolean> listener) {
4983 asyncRequest(new HasStatementSubject(subject), listener);
4987 public void forHasStatement(Resource subject, SyncListener<Boolean> listener) {
4988 asyncRequest(new HasStatementSubject(subject), listener);
4992 public void forHasStatement(Resource subject, Listener<Boolean> listener) {
4993 asyncRequest(new HasStatementSubject(subject), listener);
4997 final public void forHasStatement(final Resource subject,
4998 final AsyncProcedure<Boolean> procedure) {
5000 assert (subject != null);
5001 assert (procedure != null);
5003 processor.forHasStatement(this, subject, procedure);
5008 public void forHasStatement(Resource subject,
5009 SyncProcedure<Boolean> procedure) {
5010 forHasStatement(subject, new SyncToAsyncProcedure<Boolean>(procedure));
5014 public void forHasStatement(Resource subject, Procedure<Boolean> procedure) {
5015 forHasStatement(subject, new NoneToAsyncProcedure<Boolean>(procedure));
5019 public void forHasStatement(Resource subject, Resource relation,
5020 AsyncListener<Boolean> listener) {
5021 asyncRequest(new HasStatement(subject, relation), listener);
5025 public void forHasStatement(Resource subject, Resource relation,
5026 SyncListener<Boolean> listener) {
5027 asyncRequest(new HasStatement(subject, relation), listener);
5031 public void forHasStatement(Resource subject, Resource relation,
5032 Listener<Boolean> listener) {
5033 asyncRequest(new HasStatement(subject, relation), listener);
5037 final public void forHasStatement(final Resource subject,
5038 final Resource relation, final AsyncProcedure<Boolean> procedure) {
5040 assert (subject != null);
5041 assert (relation != null);
5042 assert (procedure != null);
5044 processor.forHasStatement(this, subject, relation, procedure);
5049 public void forHasStatement(Resource subject, Resource relation,
5050 SyncProcedure<Boolean> procedure) {
5051 forHasStatement(subject, relation, new SyncToAsyncProcedure<Boolean>(
5056 public void forHasStatement(Resource subject, Resource relation,
5057 Procedure<Boolean> procedure) {
5058 forHasStatement(subject, relation, new NoneToAsyncProcedure<Boolean>(
5063 public void forHasStatement(Resource subject, Resource relation,
5064 Resource object, AsyncListener<Boolean> listener) {
5065 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
5070 public void forHasStatement(Resource subject, Resource relation,
5071 Resource object, SyncListener<Boolean> listener) {
5072 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
5077 public void forHasStatement(Resource subject, Resource relation,
5078 Resource object, Listener<Boolean> listener) {
5079 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
5084 final public void forHasStatement(final Resource subject,
5085 final Resource relation, final Resource object,
5086 final AsyncProcedure<Boolean> procedure) {
5088 assert (subject != null);
5089 assert (relation != null);
5090 assert (object != null);
5091 assert (procedure != null);
5093 processor.forHasStatement(this, subject, relation, object, procedure);
5098 public void forHasStatement(Resource subject, Resource relation,
5099 Resource object, SyncProcedure<Boolean> procedure) {
5100 forHasStatement(subject, relation, object,
5101 new SyncToAsyncProcedure<Boolean>(procedure));
5105 public void forHasStatement(Resource subject, Resource relation,
5106 Resource object, Procedure<Boolean> procedure) {
5107 forHasStatement(subject, relation, object,
5108 new NoneToAsyncProcedure<Boolean>(procedure));
5112 public void forHasValue(Resource subject, AsyncListener<Boolean> listener) {
5113 asyncRequest(new HasValue(subject), listener);
5117 public void forHasValue(Resource subject, SyncListener<Boolean> listener) {
5118 asyncRequest(new HasValue(subject), listener);
5122 public void forHasValue(Resource subject, Listener<Boolean> listener) {
5123 asyncRequest(new HasValue(subject), listener);
5127 final public void forHasValue(final Resource subject,
5128 final AsyncProcedure<Boolean> procedure) {
5130 assert (subject != null);
5131 assert (procedure != null);
5133 processor.forValue(this, subject, new AsyncProcedure<byte[]>() {
5136 public void execute(AsyncReadGraph graph, byte[] result) {
5139 procedure.execute(graph, false);
5141 procedure.execute(graph, true);
5142 } catch (Throwable t) {
5143 Logger.defaultLogError(t);
5148 public void exception(AsyncReadGraph graph, Throwable t) {
5150 procedure.exception(graph, t);
5151 } catch (Throwable t2) {
5152 Logger.defaultLogError(t2);
5157 public String toString() {
5158 return "forHasValue -> " + procedure;
5166 public void forHasValue(Resource subject, SyncProcedure<Boolean> procedure) {
5167 forHasValue(subject, new SyncToAsyncProcedure<Boolean>(procedure));
5171 public void forHasValue(Resource subject, Procedure<Boolean> procedure) {
5172 forHasValue(subject, new NoneToAsyncProcedure<Boolean>(procedure));
5176 public void forOrderedSet(Resource subject,
5177 AsyncMultiListener<Resource> listener) {
5178 asyncRequest(new OrderedSet(subject), listener);
5182 public void forOrderedSet(Resource subject,
5183 SyncMultiListener<Resource> listener) {
5184 asyncRequest(new OrderedSet(subject), listener);
5188 public void forOrderedSet(Resource subject, MultiListener<Resource> listener) {
5189 asyncRequest(new OrderedSet(subject), listener);
5193 final public void forOrderedSet(final Resource subject,
5194 final AsyncMultiProcedure<Resource> procedure) {
5196 assert (subject != null);
5197 assert (procedure != null);
5199 processor.forOrderedSet(this, subject,
5200 new AsyncMultiProcedure<Resource>() {
5203 public void finished(AsyncReadGraph graph) {
5205 procedure.finished(graph);
5206 } catch (Throwable t) {
5207 Logger.defaultLogError(t);
5212 public void execute(AsyncReadGraph graph, Resource result) {
5214 procedure.execute(graph, result);
5215 } catch (Throwable t) {
5216 Logger.defaultLogError(t);
5221 public void exception(AsyncReadGraph graph, Throwable t) {
5223 procedure.exception(graph, t);
5224 } catch (Throwable t2) {
5225 Logger.defaultLogError(t2);
5230 public String toString() {
5231 return "forOrderedSet -> " + procedure;
5239 public void forOrderedSet(Resource subject,
5240 SyncMultiProcedure<Resource> procedure) {
5241 forOrderedSet(subject, new SyncToAsyncMultiProcedure<Resource>(
5246 public void forOrderedSet(Resource subject,
5247 MultiProcedure<Resource> procedure) {
5248 forOrderedSet(subject, new NoneToAsyncMultiProcedure<Resource>(
5253 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5254 AsyncListener<T> listener) {
5255 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5259 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5260 SyncListener<T> listener) {
5261 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5265 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5266 Listener<T> listener) {
5267 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5271 final public <T> void forPossibleAdapted(final Resource resource,
5272 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5274 assert (resource != null);
5275 assert (clazz != null);
5276 assert (procedure != null);
5278 final AdaptionService service = getSession().peekService(AdaptionService.class);
5279 if (service == null)
5280 procedure.exception(this, new ServiceException("No AdaptionService available"));
5282 service.adapt(this, resource, resource, Resource.class, clazz, true, procedure);
5286 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5287 SyncProcedure<T> procedure) {
5288 forPossibleAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5293 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5294 Procedure<T> procedure) {
5295 forPossibleAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5300 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5301 AsyncListener<T> listener) {
5302 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5306 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5307 SyncListener<T> listener) {
5308 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5312 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5313 Listener<T> listener) {
5314 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5318 final public <T> void forPossibleUniqueAdapted(final Resource resource,
5319 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5321 assert (resource != null);
5322 assert (clazz != null);
5323 assert (procedure != null);
5325 final AdaptionService service = getSession().peekService(AdaptionService.class);
5326 if (service == null)
5327 procedure.exception(this, new ServiceException("No AdaptionService available"));
5329 service.adaptNew(this, resource, clazz, true, procedure);
5334 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5335 SyncProcedure<T> procedure) {
5336 forPossibleUniqueAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5341 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5342 Procedure<T> procedure) {
5343 forPossibleUniqueAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5348 * Implementation of the interface AsyncRequestProcessor
5352 final public Session getSession() {
5353 return processor.getSession();
5357 // final public Builtins getBuiltins() {
5358 // return processor.getSession().getBuiltins();
5362 public <T> void asyncRequest(final Read<T> request) {
5364 asyncRequest(request, new AsyncProcedure<T>() {
5367 public void execute(AsyncReadGraph graph, T result) {
5371 public void exception(AsyncReadGraph graph, Throwable t) {
5372 Logger.defaultLogError(t);
5376 public String toString() {
5377 return "asyncRequest(Read) -> " + request;
5385 public <T> void asyncRequest(Read<T> request, AsyncListener<T> procedure) {
5386 asyncRequest(request, (AsyncProcedure<T>) procedure);
5390 public <T> void asyncRequest(Read<T> request,
5391 final SyncListener<T> procedure) {
5392 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5396 public <T> void asyncRequest(Read<T> request, final Listener<T> procedure) {
5397 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5401 public <T> void asyncRequest(final Read<T> request, final AsyncProcedure<T> procedure) {
5403 assert (request != null);
5404 assert (procedure != null);
5406 final ListenerBase listener = getListenerBase(procedure);
5408 if (parent != null || listener != null) {
5411 processor.query(this, request, parent, procedure,listener);
5412 } catch (DatabaseException e) {
5413 Logger.defaultLogError(e);
5414 // This throwable has already been transferred to procedure at this point - do nothing about it
5420 // final ReadGraphImpl newGraph = newSync();
5424 T result = request.perform(this);
5427 procedure.execute(this, result);
5428 } catch (Throwable t) {
5429 Logger.defaultLogError(t);
5432 } catch (Throwable t) {
5435 procedure.exception(this, t);
5436 } catch (Throwable t2) {
5437 Logger.defaultLogError(t2);
5448 public static ReadGraphImpl createAsync(QueryProcessor support) {
5449 return new ReadGraphImpl(null, support);
5452 public static ReadGraphImpl forRecompute(CacheEntry entry, QueryProcessor support) {
5453 return new ReadGraphImpl(entry, support);
5457 public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
5458 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5462 public <T> void asyncRequest(Read<T> request, final Procedure<T> procedure) {
5463 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5467 final public <T> void asyncRequest(final AsyncRead<T> request) {
5469 assert (request != null);
5471 asyncRequest(request, new AsyncProcedure<T>() {
5474 public void execute(AsyncReadGraph graph, T result) {
5478 public void exception(AsyncReadGraph graph, Throwable t) {
5479 Logger.defaultLogError(t);
5483 public String toString() {
5484 return "asyncRequest(AsyncRead) -> " + request;
5492 public <T> void asyncRequest(AsyncRead<T> request,
5493 AsyncListener<T> procedure) {
5494 asyncRequest(request, (AsyncProcedure<T>) procedure);
5498 final public <T> void asyncRequest(AsyncRead<T> request,
5499 final SyncListener<T> procedure) {
5500 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5504 final public <T> void asyncRequest(AsyncRead<T> request,
5505 final Listener<T> procedure) {
5506 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5510 final public <T> void asyncRequest(final AsyncRead<T> request,
5511 final AsyncProcedure<T> procedure) {
5513 assert (request != null);
5514 assert (procedure != null);
5516 final ListenerBase listener = getListenerBase(procedure);
5518 if (parent != null || listener != null) {
5521 processor.query(this, request, parent, procedure, listener);
5522 } catch (DatabaseException e) {
5523 Logger.defaultLogError(e);
5530 request.perform(this, new CallWrappedSingleQueryProcedure4<T>(procedure, request));
5532 } catch (Throwable t) {
5534 if (t instanceof DatabaseException)
5535 procedure.exception(this, t);
5540 new DatabaseException(
5541 "Unexpected exception in ReadGraph.asyncRequest(SingleAsyncRead, SingleProcedure)",
5551 public <T> void asyncRequest(AsyncRead<T> request,
5552 SyncProcedure<T> procedure) {
5553 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5557 final public <T> void asyncRequest(final AsyncRead<T> request,
5558 final Procedure<T> procedure) {
5559 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5563 public <T> void asyncRequest(final MultiRead<T> request) {
5565 assert (request != null);
5567 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5569 public void exception(AsyncReadGraph graph, Throwable t) {
5570 Logger.defaultLogError(t);
5574 public String toString() {
5575 return "asyncRequest(MultiRead) -> " + request;
5582 public <T> void asyncRequest(MultiRead<T> request,
5583 AsyncMultiListener<T> procedure) {
5584 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5588 public <T> void asyncRequest(MultiRead<T> request,
5589 SyncMultiListener<T> procedure) {
5590 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5594 public <T> void asyncRequest(MultiRead<T> request,
5595 MultiListener<T> procedure) {
5596 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5600 public <T> void asyncRequest(final MultiRead<T> request,
5601 final AsyncMultiProcedure<T> procedure) {
5603 assert (request != null);
5604 assert (procedure != null);
5606 final ListenerBase listener = getListenerBase(procedure);
5608 if (parent != null || listener != null) {
5610 // final ReadGraphImpl newGraph = newSync();
5611 processor.query(this, request, parent, procedure,listener);
5615 // final ReadGraphImpl newGraph = newSync();
5619 request.perform(this, procedure);
5621 } catch (Throwable t) {
5623 procedure.exception(this, t);
5632 public <T> void asyncRequest(MultiRead<T> request,
5633 SyncMultiProcedure<T> procedure) {
5634 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5638 public <T> void asyncRequest(MultiRead<T> request,
5639 MultiProcedure<T> procedure) {
5640 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5644 final public <T> void asyncRequest(final AsyncMultiRead<T> request) {
5646 assert (request != null);
5648 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5650 public void exception(AsyncReadGraph graph, Throwable t) {
5651 Logger.defaultLogError(t);
5655 public String toString() {
5656 return "asyncRequest(AsyncMultiRead) -> " + request;
5663 public <T> void asyncRequest(AsyncMultiRead<T> request,
5664 AsyncMultiListener<T> procedure) {
5665 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5669 public <T> void asyncRequest(AsyncMultiRead<T> request,
5670 SyncMultiListener<T> procedure) {
5671 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5675 public <T> void asyncRequest(AsyncMultiRead<T> request,
5676 MultiListener<T> procedure) {
5677 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5681 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5682 final AsyncMultiProcedure<T> procedure) {
5684 assert (request != null);
5685 assert (procedure != null);
5687 ListenerBase listener = getListenerBase(procedure);
5689 if (parent != null || listener != null) {
5691 processor.query(this, request, parent, procedure, listener);
5697 request.perform(this, new AsyncMultiProcedure<T>() {
5700 public void execute(AsyncReadGraph graph, T result) {
5701 procedure.execute(graph, result);
5705 public void finished(AsyncReadGraph graph) {
5706 procedure.finished(graph);
5710 public void exception(AsyncReadGraph graph, Throwable t) {
5711 procedure.exception(graph, t);
5715 public String toString() {
5716 return "asyncRequest(AsyncMultiRead) -> " + procedure;
5721 } catch (Throwable t) {
5723 procedure.exception(this, new DatabaseException(t));
5731 public <T> void asyncRequest(AsyncMultiRead<T> request,
5732 SyncMultiProcedure<T> procedure) {
5733 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5737 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5738 final MultiProcedure<T> procedure) {
5739 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5743 final public <T> void asyncRequest(final ExternalRead<T> request) {
5745 assert (request != null);
5747 asyncRequest(request, new Procedure<T>() {
5750 public void execute(T result) {
5754 public void exception(Throwable t) {
5755 Logger.defaultLogError(t);
5759 public String toString() {
5760 return "asyncRequest(PrimitiveRead) -> " + request;
5768 public <T> void asyncRequest(ExternalRead<T> request,
5769 final Listener<T> procedure) {
5770 asyncRequest(request, (Procedure<T>) procedure);
5774 final public <T> void asyncRequest(final ExternalRead<T> request,
5775 final Procedure<T> procedure) {
5777 assert (request != null);
5778 assert (procedure != null);
5780 ListenerBase listener = getListenerBase(procedure);
5782 if (parent != null || listener != null) {
5784 processor.query(this, request, parent, procedure, listener);
5790 request.register(this, new Listener<T>() {
5793 public void execute(T result) {
5794 procedure.execute(result);
5798 public void exception(Throwable t) {
5799 procedure.exception(t);
5803 public String toString() {
5804 return "asyncRequest(PrimitiveRead) -> " + request;
5808 public boolean isDisposed() {
5814 } catch (Throwable t) {
5816 if (t instanceof DatabaseException)
5817 procedure.exception(t);
5820 .exception(new DatabaseException(
5821 "Unexpected exception in ReadGraph.asyncRequest(SingleAsyncRead, SingleProcedure)",
5831 public void asyncRequest(final Write request) {
5833 assert (request != null);
5835 getSession().asyncRequest(request);
5837 // processor.asyncWrite(request);
5842 public <T> void asyncRequest(final WriteResult<T> request, Procedure<T> procedure) {
5843 throw new Error("Not implemented.");
5847 public void asyncRequest(Write request, Consumer<DatabaseException> callback) {
5849 assert (request != null);
5851 getSession().asyncRequest(request, callback);
5856 public void asyncRequest(final DelayedWrite request) {
5858 assert (request != null);
5860 getSession().asyncRequest(request);
5865 public <T> void asyncRequest(final DelayedWriteResult<T> request, Procedure<T> procedure) {
5866 throw new Error("Not implemented.");
5870 public void asyncRequest(DelayedWrite r,
5871 Consumer<DatabaseException> callback) {
5872 throw new Error("Not implemented.");
5876 public void asyncRequest(final WriteOnly request) {
5878 assert (request != null);
5880 getSession().asyncRequest(request);
5885 public <T> void asyncRequest(final WriteOnlyResult<T> request, Procedure<T> procedure) {
5886 throw new Error("Not implemented.");
5890 public void asyncRequest(WriteOnly r, Consumer<DatabaseException> callback) {
5891 throw new Error("Not implemented.");
5895 * Implementation of the interface ServiceLocator
5899 public <T> T getService(Class<T> api) {
5900 if(WriteSupport.class == api) {
5901 if(this instanceof WriteGraphImpl) {
5902 WriteGraphImpl impl = (WriteGraphImpl)this;
5903 return (T)impl.writeSupport;
5906 return getSession().getService(api);
5910 public <T> T peekService(Class<T> api) {
5911 return getSession().peekService(api);
5915 public boolean hasService(Class<?> api) {
5916 return getSession().hasService(api);
5920 public <T> void registerService(Class<T> api, T service) {
5921 getSession().registerService(api, service);
5925 public boolean isImmutable(Resource resource) throws DatabaseException {
5926 ResourceImpl impl = (ResourceImpl)resource;
5927 return processor.isImmutable(impl.id);
5934 protected static String INTERNAL_ERROR_STRING = "Transaction aborted due to internal client error. Contact application support.";
5937 * callerThread is the currently running thread state.syncThread is blocking for
5938 * this execution state.syncParent is the blocking request
5941 final private boolean isExternal(int thread) {
5942 return thread == Integer.MIN_VALUE;
5945 // final private boolean isSync(int thread) {
5946 // return thread < -1 && thread > Integer.MIN_VALUE;
5949 ReadGraphImpl(ReadGraphImpl graph) {
5950 this(graph.parent, graph.processor);
5953 ReadGraphImpl(CacheEntry parent, QueryProcessor support) {
5954 // this.state = new ReadGraphState(barrier, support);
5955 this.parent = parent;
5956 this.processor = support;
5959 ReadGraphImpl(final QueryProcessor support) {
5961 // this.state = state;
5962 this.processor = support;
5967 // public static ReadGraphImpl createSync(int syncThread, Object syncParent,
5968 // ReadGraphSupportImpl support) {
5969 // return new ReadGraphImpl(syncThread, syncThread, syncParent, null,
5970 // support, new AsyncBarrierImpl(null));
5973 public static ReadGraphImpl create(QueryProcessor support) {
5974 return new ReadGraphImpl(support);
5977 // public ReadGraphImpl newAsync() {
5979 //// if(!state.synchronizedExecution) {
5982 //// return new ReadGraphImpl(false, parent, state.support, state.barrier);
5986 // public ReadGraphImpl newSync() {
5987 // return new ReadGraphImpl(parent, processor);
5990 public ReadGraphImpl newSync(CacheEntry parentEntry) {
5991 return new ReadGraphImpl(parentEntry, processor);
5994 public ReadGraphImpl newRestart(ReadGraphImpl impl) {
5996 WriteGraphImpl write = processor.getSession().getService(
5997 WriteGraphImpl.class);
5999 // if (write.callerThread != impl.callerThread)
6000 // return new WriteGraphImpl(impl.callerThread, parent, state.support, write.writeSupport, write.provider, write.state.barrier);
6005 // public ReadGraphImpl newSync(Object parentRequest) {
6006 // return new ReadGraphImpl(callerThread, state.parent, state.support, new AsyncBarrierImpl(state.barrier));
6009 // public ReadGraphImpl newSync(final int callerThread, Object parentRequest) {
6010 // assert (state.syncThread == callerThread || (state.syncThread == Integer.MIN_VALUE && callerThread != Integer.MIN_VALUE));
6011 // return new ReadGraphImpl(callerThread, callerThread, parentRequest,
6012 // state.parent, state.support, new AsyncBarrierImpl(state.barrier));
6015 // public ReadGraphImpl newSyncAsync(Object parentRequest) {
6016 //// assert (callerThread < 0);
6017 // return new ReadGraphImpl(callerThread, state.syncThread, parentRequest,
6018 // state.parent, state.support, new AsyncBarrierImpl(state.barrier));
6021 // public ReadGraphImpl newSyncAsync(final int callerThread,
6022 // Object parentRequest) {
6023 //// assert (callerThread < 0);
6024 // // assert(state.syncThread == callerThread || (state.syncThread == Integer.MIN_VALUE
6025 // // && callerThread != Integer.MIN_VALUE) );
6026 // return new ReadGraphImpl(callerThread, callerThread, parentRequest,
6027 // state.parent, state.support, new AsyncBarrierImpl(state.barrier));
6030 public ReadGraphImpl withAsyncParent(CacheEntry parent) {
6031 return new ReadGraphImpl(parent, processor);
6034 public ReadGraphImpl withParent(CacheEntry parent) {
6035 if(parent == this.parent) return this;
6036 else return new ReadGraphImpl(parent, processor);
6039 final private ListenerBase getListenerBase(final Object procedure) {
6040 if (procedure instanceof ListenerBase)
6041 return (ListenerBase) procedure;
6046 public <T> void waitAsyncProcedure(AsyncMultiReadProcedure<T> procedure) {
6048 assert(procedure.done());
6050 // while (!procedure.done()) {
6052 // boolean executed = processor.resumeTasks(callerThread, null, null);
6056 // // sema.tryAcquire(1, TimeUnit.MILLISECONDS);
6057 // } catch (InterruptedException e) {
6058 // e.printStackTrace();
6066 public <T> void waitAsyncProcedure(AsyncReadProcedure<T> procedure) {
6068 assert(procedure.done());
6070 // while (!procedure.done()) {
6072 // boolean executed = processor.processor.resume(this);
6076 // // sema.tryAcquire(1, TimeUnit.MILLISECONDS);
6077 // } catch (InterruptedException e) {
6078 // e.printStackTrace();
6086 // public void waitAsync(Object request) {
6088 // state.barrier.waitBarrier(request, this);
6089 // } catch (Throwable t) {
6090 // t.printStackTrace();
6091 // processor.scanPending();
6092 // processor.querySupport.checkTasks();
6093 // throw new RuntimeDatabaseException(t);
6097 // public void restart() {
6098 // state.barrier.restart();
6101 public boolean resumeTasks() {
6102 return processor.resumeTasks(this);
6105 Class<?> singleClass(Set<Resource> types) {
6106 Class<?> result = null;
6107 for (Resource type : types) {
6108 Class<?> clazz = processor.getBuiltinValue(type);
6109 if (clazz != null) {
6119 private String debugString(Resource r) {
6122 name = getPossibleRelatedValue(r, processor.getL0(this).HasName);
6123 } catch (ManyObjectsForFunctionalRelationException e) {
6124 Logger.defaultLogError(e);
6125 } catch (ServiceException e) {
6126 Logger.defaultLogError(e);
6128 return "[" + name + " - " + r + "]";
6132 public String toString() {
6133 return "ReadGraphImpl[thread=" + Thread.currentThread() + "]";
6137 final public int thread() {
6141 static class MultiTripleIntProcedure implements TripleIntProcedure {
6143 final private AsyncMultiProcedure<Statement> procedure;
6144 final private ReadGraphImpl impl;
6145 final private QuerySupport support;
6147 public MultiTripleIntProcedure(AsyncMultiProcedure<Statement> procedure, ReadGraphImpl impl, QuerySupport support) {
6148 this.procedure = procedure;
6150 this.support = support;
6154 public void execute(ReadGraphImpl graph, int s, int p, int o) {
6156 procedure.execute(graph, support.getStatement(s, p, o));
6157 } catch (Throwable t2) {
6158 Logger.defaultLogError(t2);
6163 public void finished(ReadGraphImpl graph) {
6165 procedure.finished(graph);
6166 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
6167 } catch (Throwable t2) {
6168 Logger.defaultLogError(t2);
6173 public void exception(ReadGraphImpl graph, Throwable t) {
6175 procedure.exception(graph, t);
6176 } catch (Throwable t2) {
6177 Logger.defaultLogError(t2);
6179 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
6183 public String toString() {
6184 return "forEachObject with " + procedure;
6189 // private AsyncMultiProcedure<Resource> cacheKey = null;
6190 // private MultiIntProcedure cacheResult = null;
6192 // final IntProcedure forMultiProcedure(final AsyncMultiProcedure<Resource> procedure) {
6194 // if(procedure == cacheKey) return cacheResult;
6196 // cacheResult = new MultiIntProcedure(procedure, this, processor.support);
6197 // cacheKey = procedure;
6199 // return cacheResult;
6203 // private AsyncMultiProcedure<Statement> cacheKey2 = null;
6204 // private MultiTripleIntProcedure cacheResult2 = null;
6206 // final synchronized TripleIntProcedure forMultiProcedure(final AsyncMultiProcedure<Statement> procedure) {
6208 // if(procedure == cacheKey2) return cacheResult2;
6210 // cacheResult2 = new MultiTripleIntProcedure(procedure, this, processor.support);
6211 // cacheKey2 = procedure;
6213 // return cacheResult2;
6218 public Datatype getDataType(Resource subject) throws DatabaseException {
6219 for(Resource dataTypeResource : getObjects(subject, processor.getL0(this).HasDataType))
6220 return getValue(dataTypeResource, Bindings.getBindingUnchecked(Datatype.class));
6221 throw new DoesNotContainValueException("The literal has no data type.");
6224 protected <T extends Accessor> T getAccessor4File(Resource subject)
6225 throws DatabaseException {
6228 byte[] bytes = processor.support.getValue(g, subject);
6232 BinaryVariant va = (BinaryVariant) Accessors.getAccessor(bytes, Datatypes.VARIANT);
6233 Accessor ca = va.getContentAccessor();
6235 } catch (AccessorConstructionException e) {
6236 throw new DatabaseException(e);
6242 Binding datatype_binding = Bindings.getBindingUnchecked(DataType.class);
6243 Serializer datatype_serializer = datatype_binding.serializer();
6246 BinaryMemory in = new BinaryMemory(ByteBuffer.wrap(bytes));
6247 datatype = (DataType)datatype_serializer.deserialize(in);
6248 Binding data_binding = Bindings.getBinding(datatype);
6249 Serializer data_serializer = data_binding.serializer(BinarySerializationFormat.INSTANCE);
6250 Object o = data_serializer.deserialize(in);
6252 return (T)Accessors.getAccessor(data_binding, o);
6253 } catch(AccessorConstructionException e) {
6256 } catch (Exception e) {
6257 throw new DatabaseException(e);
6260 @SuppressWarnings("unchecked")
6262 public <T extends Accessor> T getAccessor(Resource subject) throws DatabaseException {
6263 RandomAccessBinary rab = getRandomAccessBinary(subject);
6265 return (T)Accessors.getAccessor(rab, getDataType(subject));
6266 } catch(AccessorConstructionException e) {
6267 throw new DatabaseException(e);
6270 @SuppressWarnings("unchecked")
6271 protected <T extends Accessor> T createAccessor(Resource resource, Datatype datatype, Object intialValue)
6272 throws DatabaseException {
6273 RandomAccessBinary rab = createRandomAccessBinary(resource, datatype, intialValue);
6275 return (T)Accessors.getAccessor(rab, datatype);
6276 } catch(AccessorConstructionException e) {
6277 throw new DatabaseException(e);
6281 public RandomAccessBinary getRandomAccessBinary(Resource subject) throws DatabaseException {
6282 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
6283 ResourceData rd = ravs.get(subject);
6287 ExternalValueSupport evs = getService(ExternalValueSupport.class);
6288 long size = evs.getValueSize(this, subject); // Throws DatabaseException if no old external value.
6290 File platform = Platform.getLocation().toFile();
6291 File tempFiles = new File(platform, "tempFiles");
6292 File dbDir = new File(tempFiles, "db");
6294 File file = new File(dbDir, "ResourceFile" + subject.getResourceId());
6295 rd = new ResourceData(new BinaryFile(file), true); // Old external value.
6296 final int N = 1<<20;
6300 int length = N < left ? N : (int)left;
6301 byte[] bytes = evs.readValue(this, subject, offset, length);
6302 offset += bytes.length;
6303 left -= bytes.length;
6304 rd.binaryFile.write(bytes);
6306 ravs.put(subject, rd);
6308 } catch (Exception e) {
6309 throw new DatabaseException("Resource " + subject + " have value but there was problem with accessing value data.", e);
6311 } catch (Exception e) {
6312 if(Development.DEVELOPMENT) {
6313 if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
6314 e.printStackTrace();
6318 Datatype datatype = getDataType(subject);
6319 Object value = getPossibleValue(subject, Bindings.getBinding(datatype));
6320 return createRandomAccessBinary(subject, datatype, value);
6322 public RandomAccessBinary createRandomAccessBinary(Resource resource, Datatype datatype, Object initialValue)
6323 throws DatabaseException {
6324 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
6326 File platform = Platform.getLocation().toFile();
6327 File tempFiles = new File(platform, "tempFiles");
6328 File dbDir = new File(tempFiles, "db");
6330 File file = new File(dbDir, "ResourceFile" + resource.getResourceId());
6331 ResourceData rd = new ResourceData(new BinaryFile(file), false);
6332 Binding binding = Bindings.getBinding(datatype);
6333 if (null == initialValue) {
6334 initialValue = binding.createDefault();
6336 Serializer serializer = binding.serializer();
6337 byte[] bytes = serializer.serialize(initialValue);
6338 rd.binaryFile.write(bytes);
6339 ravs.put(resource, rd);
6341 } catch (Exception e) {
6342 if (e instanceof DatabaseException)
6343 throw (DatabaseException)e;
6345 throw new DatabaseException(e);
6349 // static class ExternalValueRequest<T> extends ResourceRead<T> {
6351 // public ExternalValueRequest(Resource resource) {
6355 // @SuppressWarnings("unchecked")
6357 // public T perform(ReadGraph graph) throws DatabaseException {
6360 // String uri = graph.getURI(resource);
6361 // if(Layer0.URIs.Functions_functionApplication.equals(uri)) return (T)functionApplication;
6363 // return (T)ReflectionUtils.getValue(uri).getValue();
6365 // } catch(ValueNotFoundException e) {
6366 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
6367 // } catch(ClassCastException e) {
6368 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
6374 @SuppressWarnings("unchecked")
6376 public <T> T getValue2(Resource r, Object context) throws DatabaseException {
6377 Layer0 L0 = processor.getL0(this);
6378 Set<Resource> types = getTypes(r);
6380 if(types.contains(L0.Literal)) {
6381 if(isImmutable(r)) {
6382 return syncRequest(new ValueImplied<T>(r));
6387 else if(types.contains(L0.ExternalValue)) {
6388 return (T)syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6392 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6393 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6395 return function.apply(this, r, context);
6396 } catch(RuntimeException e) {
6397 DatabaseException dte = findPossibleRootException(e);
6398 if(dte != null) throw dte;
6399 else throw new DatabaseException(e);
6406 public Variant getVariantValue2(Resource r, Object context) throws DatabaseException {
6407 Layer0 L0 = processor.getL0(this);
6408 Set<Resource> types = getTypes(r);
6410 if(types.contains(L0.Literal)) {
6411 if(isImmutable(r)) {
6412 return syncRequest(new VariantValueImplied(r));
6414 return getVariantValue(r);
6417 else if(types.contains(L0.ExternalValue)) {
6418 Object value = syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6420 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6421 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6422 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6427 Function3<ReadGraph,Resource,Object,Object> function = requestValueFunction(r);
6428 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6430 Object value = function.apply(this, r, context);
6432 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6433 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6434 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6436 } catch(RuntimeException e) {
6437 DatabaseException dte = findPossibleRootException(e);
6438 if(dte != null) throw dte;
6439 else throw new DatabaseException(e);
6445 public <T> T getPossibleValue2(Resource subject, Object context) throws DatabaseException {
6447 return getValue2(subject, context);
6448 } catch (DatabaseException e) {
6453 static class PossibleConverterFunction<T> extends ResourceRead<Function3<ReadGraph,Resource,Object,T>> {
6455 public PossibleConverterFunction(Resource resource) {
6460 public Function3<ReadGraph,Resource,Object,T> perform(ReadGraph graph) throws DatabaseException {
6461 return compute(graph, resource);
6464 @SuppressWarnings("unchecked")
6465 public static <T> Function3<ReadGraph,Resource,Object,T> compute(ReadGraph graph, Resource resource) throws DatabaseException {
6466 Layer0 L0 = Layer0.getInstance(graph);
6467 for(Resource converter : graph.getObjects(resource, L0.ConvertsToValueWith)) {
6469 if(L0.Functions_functionApplication.equals(converter)) {
6470 return (Function3<ReadGraph,Resource,Object,T>)graph.syncRequest(new AdaptValue(resource));
6472 return graph.getValue2(converter, resource);
6474 } catch(RuntimeException e) {
6475 DatabaseException dte = findPossibleRootException(e);
6476 if(dte != null) throw dte;
6477 else throw new DatabaseException(e);
6485 <T> Function3<ReadGraph,Resource,Object,T> requestValueFunction(Resource r) throws DatabaseException {
6487 return syncRequest(new PossibleConverterFunction<T>(r), TransientCacheAsyncListener.<Function3<ReadGraph,Resource,Object,T>>instance());
6489 return syncRequest(new PossibleConverterFunction<T>(r));
6493 * Get a value associated with a graph {@link Resource}, using a possible context object and
6494 * a desired value binding. The following methods are tried in order to retreive the value:
6496 * <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>
6497 * <li>If the resource is a {@code L0.ExternalValue}, the value is acquired using
6498 * {@link ReflectionUtils#getValue(String)}.</li>
6499 * <li>If the resource is associated with a suitable value converter with relation {@code L0.ConvertsToValueWith}
6500 * (see {@link #requestValueFunction(Resource)}), the value function is called with the graph, the resource
6501 * and the context object.</li>
6504 * @param r A graph resource with which the value is associated
6505 * @param context A context object that is used for acquiring the value (only applied in case 3)
6506 * @param binding A binding for the value type (only applied in case 1)
6507 * @return The value of the graph node.
6508 * @throws DoesNotContainValueException No value is associated with the graph node.
6509 * @throws DatabaseException Other errors, such as an error in casting the value to the return type or
6510 * a runtime error in the value function.
6512 @SuppressWarnings("unchecked")
6514 public <T> T getValue2(Resource r, Object context, Binding binding) throws DatabaseException {
6515 if (binding instanceof ObjectVariantBinding)
6516 return getValue2(r, context);
6518 Layer0 L0 = processor.getL0(this);
6519 Set<Resource> types = getTypes(r);
6520 if(types.contains(L0.Literal)) {
6521 if(isImmutable(r)) {
6522 return syncRequest(new Value<T>(r, binding));
6524 return getValue(r, binding);
6526 } else if(types.contains(L0.ExternalValue)) {
6528 return (T)ReflectionUtils.getValue(getURI(r)).getValue();
6529 } catch(ValueNotFoundException e) {
6530 throw new DatabaseException(e);
6531 } catch(ClassCastException e) {
6532 throw new DatabaseException(e);
6536 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6537 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function.");
6539 Object value = function.apply(this, r, context);
6540 if(binding.isInstance(value)) return (T)value;
6541 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
6542 return (T)Bindings.adapt(value, srcBinding, binding);
6543 } catch(RuntimeException e) {
6544 DatabaseException dte = findPossibleRootException(e);
6545 if(dte != null) throw dte;
6546 else throw new DatabaseException(e);
6547 } catch (AdaptException e) {
6548 throw new DatabaseException(e);
6549 } catch (org.simantics.databoard.binding.error.BindingException e) {
6550 throw new DatabaseException(e);
6556 public <T> T getPossibleValue2(Resource subject, Object context, Binding binding) throws DatabaseException {
6558 return getValue2(subject, context, binding);
6559 } catch (DatabaseException e) {
6564 private static DatabaseException findPossibleRootException(Throwable t) {
6565 if(t == null) return null;
6566 if(t instanceof DatabaseException) return (DatabaseException)t;
6567 if(t instanceof RuntimeException || t instanceof InvocationTargetException) {
6568 return findPossibleRootException(t.getCause());
6574 public <T> T getRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6575 return getRelatedValue2(subject, relation, subject);
6579 public Variant getRelatedVariantValue2(Resource subject, Resource relation) throws DatabaseException {
6580 return getRelatedVariantValue2(subject, relation, subject);
6584 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6586 Resource object = getPossibleObject(subject, relation);
6587 if(object == null) return null;
6588 else return getValue2(object, subject);
6589 } catch (DatabaseException e) {
6595 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6596 if(Development.DEVELOPMENT) {
6597 String error = L0Validations.checkValueType(this, subject, relation);
6599 Logger.defaultLogError(new ValidationException(error));
6600 //throw new ValidationException(error);
6601 new ValidationException(error).printStackTrace();
6604 return getValue2(getSingleObject(subject, relation), context);
6608 public Variant getRelatedVariantValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6609 if(Development.DEVELOPMENT) {
6610 String error = L0Validations.checkValueType(this, subject, relation);
6612 Logger.defaultLogError(new ValidationException(error));
6613 //throw new ValidationException(error);
6614 new ValidationException(error).printStackTrace();
6617 return getVariantValue2(getSingleObject(subject, relation), context);
6621 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6623 Resource object = getPossibleObject(subject, relation);
6624 if(object == null) return null;
6625 else return getValue2(object, context);
6626 } catch (DatabaseException e) {
6632 public <T> T getRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6633 return getRelatedValue2(subject, relation, subject, binding);
6637 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6639 Resource object = getPossibleObject(subject, relation);
6640 if(object == null) return null;
6641 return getValue2(object, subject, binding);
6642 } catch (DatabaseException e) {
6648 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6649 return getValue2(getSingleObject(subject, relation), context, binding);
6653 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6655 Resource object = getPossibleObject(subject, relation);
6656 if(object == null) return null;
6657 else return getValue2(object, context, binding);
6658 } catch (DatabaseException e) {
6664 public Type getRelatedValueType(Resource subject, Resource relation) throws DatabaseException {
6665 Layer0 L0 = processor.getL0(this);
6666 Resource property = getSingleObject(subject, relation);
6667 String typeText = (String)getRelatedValue(property, L0.HasValueType, Bindings.STRING);
6669 return org.simantics.scl.compiler.types.Types.parseType(typeText);
6670 } catch (SCLTypeParseException e) {
6671 throw new DatabaseException(e);
6676 public boolean setSynchronous(boolean value) {
6677 boolean old = processor.synch;
6678 processor.synch = value;
6683 public boolean getSynchronous() {
6684 return processor.synch;
6687 public void ensureLoaded(int resource) {
6688 processor.querySupport.ensureLoaded(this, resource);
6691 public void ensureLoaded(int resource, int predicate) {
6692 processor.querySupport.ensureLoaded(this, resource, predicate);
6695 public byte[] getValue(int resource) {
6696 return processor.querySupport.getValue(this, resource);
6699 public int thread(int resource) {
6700 return (resource >>> 16) & processor.THREAD_MASK;
6703 public int thread(Resource resource) {
6704 return (((ResourceImpl)resource).id >>> 16) & processor.THREAD_MASK;
6707 public ResourceSupport getResourceSupport() {
6708 return processor.getResourceSupport();
6712 public Object getModificationCounter() {
6713 return processor.getSession().getModificationCounter();