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;
31 import java.util.function.Consumer;
33 import org.eclipse.core.runtime.Platform;
34 import org.simantics.databoard.Accessors;
35 import org.simantics.databoard.Bindings;
36 import org.simantics.databoard.accessor.Accessor;
37 import org.simantics.databoard.accessor.error.AccessorConstructionException;
38 import org.simantics.databoard.adapter.AdaptException;
39 import org.simantics.databoard.binding.Binding;
40 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;
41 import org.simantics.databoard.binding.impl.ObjectVariantBinding;
42 import org.simantics.databoard.binding.mutable.Variant;
43 import org.simantics.databoard.serialization.Serializer;
44 import org.simantics.databoard.type.Datatype;
45 import org.simantics.databoard.util.binary.BinaryFile;
46 import org.simantics.databoard.util.binary.RandomAccessBinary;
47 import org.simantics.db.AsyncReadGraph;
48 import org.simantics.db.DevelopmentKeys;
49 import org.simantics.db.ExternalValue;
50 import org.simantics.db.ExternalValueSupport;
51 import org.simantics.db.ReadGraph;
52 import org.simantics.db.RelationContext;
53 import org.simantics.db.Resource;
54 import org.simantics.db.Session;
55 import org.simantics.db.Statement;
56 import org.simantics.db.adaption.AdaptionService;
57 import org.simantics.db.common.primitiverequest.Adapter;
58 import org.simantics.db.common.primitiverequest.Builtin;
59 import org.simantics.db.common.primitiverequest.DatatypeBinding;
60 import org.simantics.db.common.primitiverequest.ForEachAssertedObject;
61 import org.simantics.db.common.primitiverequest.ForEachAssertedStatement;
62 import org.simantics.db.common.primitiverequest.HasStatement;
63 import org.simantics.db.common.primitiverequest.HasStatementSubject;
64 import org.simantics.db.common.primitiverequest.HasStatementSubjectObject;
65 import org.simantics.db.common.primitiverequest.HasValue;
66 import org.simantics.db.common.primitiverequest.Inverse;
67 import org.simantics.db.common.primitiverequest.IsInheritedFrom;
68 import org.simantics.db.common.primitiverequest.IsInstanceOf;
69 import org.simantics.db.common.primitiverequest.IsSubrelationOf;
70 import org.simantics.db.common.primitiverequest.OrderedSet;
71 import org.simantics.db.common.primitiverequest.PossibleAdapter;
72 import org.simantics.db.common.primitiverequest.PossibleInverse;
73 import org.simantics.db.common.primitiverequest.PossibleObject;
74 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;
75 import org.simantics.db.common.primitiverequest.PossibleRelatedValueImplied;
76 import org.simantics.db.common.primitiverequest.PossibleStatement;
77 import org.simantics.db.common.primitiverequest.PossibleType;
78 import org.simantics.db.common.primitiverequest.PossibleUniqueAdapter;
79 import org.simantics.db.common.primitiverequest.PossibleValue;
80 import org.simantics.db.common.primitiverequest.PossibleValueImplied;
81 import org.simantics.db.common.primitiverequest.RelatedValue;
82 import org.simantics.db.common.primitiverequest.RelatedValueImplied;
83 import org.simantics.db.common.primitiverequest.SingleObject;
84 import org.simantics.db.common.primitiverequest.SingleStatement;
85 import org.simantics.db.common.primitiverequest.SingleType;
86 import org.simantics.db.common.primitiverequest.SingleTypeAny;
87 import org.simantics.db.common.primitiverequest.Types;
88 import org.simantics.db.common.primitiverequest.UniqueAdapter;
89 import org.simantics.db.common.primitiverequest.Value;
90 import org.simantics.db.common.primitiverequest.ValueImplied;
91 import org.simantics.db.common.primitiverequest.VariantValueImplied;
92 import org.simantics.db.common.procedure.BlockingAsyncProcedure;
93 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
94 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
95 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
96 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
97 import org.simantics.db.common.procedure.single.SyncReadProcedure;
98 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrErrorProcedure;
99 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrNullProcedure;
100 import org.simantics.db.common.procedure.single.wrapper.ExceptionToNullProcedure;
101 import org.simantics.db.common.procedure.single.wrapper.NullSingleOrNullProcedure;
102 import org.simantics.db.common.procedure.single.wrapper.SingleFunctionalOrNullProcedure;
103 import org.simantics.db.common.procedure.single.wrapper.SingleOrErrorProcedure;
104 import org.simantics.db.common.procedure.single.wrapper.SingleOrNullProcedure;
105 import org.simantics.db.common.procedure.wrapper.NoneToAsyncListener;
106 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiListener;
107 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiProcedure;
108 import org.simantics.db.common.procedure.wrapper.NoneToAsyncProcedure;
109 import org.simantics.db.common.procedure.wrapper.NoneToAsyncSetProcedure;
110 import org.simantics.db.common.procedure.wrapper.SyncToAsyncListener;
111 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiListener;
112 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiProcedure;
113 import org.simantics.db.common.procedure.wrapper.SyncToAsyncProcedure;
114 import org.simantics.db.common.procedure.wrapper.SyncToAsyncSetProcedure;
115 import org.simantics.db.common.request.AdaptValue;
116 import org.simantics.db.common.request.ResourceRead;
117 import org.simantics.db.common.utils.Functions;
118 import org.simantics.db.common.utils.Logger;
119 import org.simantics.db.common.utils.NameUtils;
120 import org.simantics.db.common.validation.L0Validations;
121 import org.simantics.db.exception.AdaptionException;
122 import org.simantics.db.exception.ArgumentException;
123 import org.simantics.db.exception.AssumptionException;
124 import org.simantics.db.exception.BindingException;
125 import org.simantics.db.exception.DatabaseException;
126 import org.simantics.db.exception.DoesNotContainValueException;
127 import org.simantics.db.exception.EmptyResourceException;
128 import org.simantics.db.exception.InternalException;
129 import org.simantics.db.exception.InvalidLiteralException;
130 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
131 import org.simantics.db.exception.NoInverseException;
132 import org.simantics.db.exception.NoSingleResultException;
133 import org.simantics.db.exception.ResourceNotFoundException;
134 import org.simantics.db.exception.ServiceException;
135 import org.simantics.db.exception.ValidationException;
136 import org.simantics.db.impl.RelationContextImpl;
137 import org.simantics.db.impl.ResourceImpl;
138 import org.simantics.db.impl.internal.RandomAccessValueSupport;
139 import org.simantics.db.impl.internal.ResourceData;
140 import org.simantics.db.impl.procedure.CallWrappedSingleQueryProcedure4;
141 import org.simantics.db.impl.procedure.InternalProcedure;
142 import org.simantics.db.impl.procedure.ResultCallWrappedQueryProcedure4;
143 import org.simantics.db.impl.procedure.ResultCallWrappedSingleQueryProcedure4;
144 import org.simantics.db.impl.query.CacheEntry;
145 import org.simantics.db.impl.query.QueryCache;
146 import org.simantics.db.impl.query.QueryCacheBase;
147 import org.simantics.db.impl.query.QueryProcessor;
148 import org.simantics.db.impl.query.QuerySupport;
149 import org.simantics.db.impl.query.TripleIntProcedure;
150 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
151 import org.simantics.db.impl.support.ResourceSupport;
152 import org.simantics.db.procedure.AsyncListener;
153 import org.simantics.db.procedure.AsyncMultiListener;
154 import org.simantics.db.procedure.AsyncMultiProcedure;
155 import org.simantics.db.procedure.AsyncProcedure;
156 import org.simantics.db.procedure.AsyncSetListener;
157 import org.simantics.db.procedure.Listener;
158 import org.simantics.db.procedure.ListenerBase;
159 import org.simantics.db.procedure.MultiListener;
160 import org.simantics.db.procedure.MultiProcedure;
161 import org.simantics.db.procedure.Procedure;
162 import org.simantics.db.procedure.SetListener;
163 import org.simantics.db.procedure.StatementProcedure;
164 import org.simantics.db.procedure.SyncListener;
165 import org.simantics.db.procedure.SyncMultiListener;
166 import org.simantics.db.procedure.SyncMultiProcedure;
167 import org.simantics.db.procedure.SyncProcedure;
168 import org.simantics.db.procedure.SyncSetListener;
169 import org.simantics.db.request.AsyncMultiRead;
170 import org.simantics.db.request.AsyncRead;
171 import org.simantics.db.request.DelayedWrite;
172 import org.simantics.db.request.DelayedWriteResult;
173 import org.simantics.db.request.ExternalRead;
174 import org.simantics.db.request.MultiRead;
175 import org.simantics.db.request.Read;
176 import org.simantics.db.request.ReadInterface;
177 import org.simantics.db.request.RequestFlags;
178 import org.simantics.db.request.Write;
179 import org.simantics.db.request.WriteInterface;
180 import org.simantics.db.request.WriteOnly;
181 import org.simantics.db.request.WriteOnlyResult;
182 import org.simantics.db.request.WriteResult;
183 import org.simantics.layer0.Layer0;
184 import org.simantics.scl.compiler.types.Type;
185 import org.simantics.scl.compiler.types.exceptions.SCLTypeParseException;
186 import org.simantics.scl.reflection.ReflectionUtils;
187 import org.simantics.scl.reflection.ValueNotFoundException;
188 import org.simantics.scl.runtime.function.Function1;
189 import org.simantics.scl.runtime.function.Function3;
190 import org.simantics.utils.DataContainer;
191 import org.simantics.utils.Development;
192 import org.simantics.utils.datastructures.Pair;
193 import org.simantics.utils.datastructures.collections.CollectionUtils;
195 import gnu.trove.map.hash.TObjectIntHashMap;
197 public class ReadGraphImpl implements ReadGraph {
199 final static boolean EMPTY_RESOURCE_CHECK = false;
201 final public CacheEntry parent;
202 final public QueryProcessor processor;
204 final static Binding DATA_TYPE_BINDING_INTERNAL = Bindings.getBindingUnchecked(Datatype.class);
205 final static Serializer DATA_TYPE_SERIALIZER = Bindings.getSerializerUnchecked(DATA_TYPE_BINDING_INTERNAL);
207 final public static TObjectIntHashMap<String> counters = new TObjectIntHashMap<String>();
209 public static void resetCounters() {
213 public static String listCounters(File file) throws IOException {
215 PrintStream b = new PrintStream(new BufferedOutputStream(new FileOutputStream(file)));
217 for(Pair<String,Integer> p : CollectionUtils.valueSortedEntries(counters)) {
218 b.print(-p.second + " " + p.first + "\n");
223 return "Dumped " + counters.size() + " queries.";
228 * Implementation of the interface ReadGraph
230 final public String getURI(final Resource resource) throws AssumptionException, ValidationException, ServiceException {
232 assert (resource != null);
236 return syncRequest(new org.simantics.db.common.uri.ResourceToURI(resource));
238 } catch (AssumptionException e) {
240 throw new AssumptionException(e);
242 } catch (ValidationException e) {
244 throw new ValidationException(e);
246 } catch (ServiceException e) {
248 throw new ServiceException(e);
250 } catch (DatabaseException e) {
252 throw new ServiceException(INTERNAL_ERROR_STRING, e);
258 final public String getPossibleURI(final Resource resource) throws ValidationException, ServiceException {
260 assert (resource != null);
264 return syncRequest(new org.simantics.db.common.uri.ResourceToPossibleURI(resource));
266 } catch (ValidationException e) {
268 throw new ValidationException(e);
270 } catch (ServiceException e) {
272 throw new ServiceException(e);
274 } catch (DatabaseException e) {
276 throw new ServiceException(INTERNAL_ERROR_STRING, e);
282 final public Resource getResource(final String id)
283 throws ResourceNotFoundException, ValidationException,
290 // assert (id != null);
291 // assert (procedure != null);
293 // processor.forResource(this, id, procedure);
295 //// return syncRequest(new org.simantics.db.common.primitiverequest.Resource(
298 Integer rid = QueryCache.resultURIToResource(this, id, parent, null);
299 if(rid == 0) throw new ResourceNotFoundException(id);
300 return processor.querySupport.getResource(rid);
302 } catch (ResourceNotFoundException e) {
304 throw new ResourceNotFoundException(id, e);
306 } catch (ValidationException e) {
308 throw new ValidationException(e);
310 } catch (ServiceException e) {
312 throw new ServiceException(e);
314 } catch (DatabaseException e) {
316 throw new ServiceException(INTERNAL_ERROR_STRING, e);
322 final public Resource getPossibleResource(final String id)
323 throws ResourceNotFoundException, ValidationException,
330 return getResource(id);
332 // return syncRequest(new org.simantics.db.common.primitiverequest.Resource(
335 } catch (ResourceNotFoundException e) {
339 } catch (ValidationException e) {
341 throw new ValidationException(e);
343 } catch (ServiceException e) {
345 throw new ServiceException(e);
347 } catch (DatabaseException e) {
349 throw new ServiceException(INTERNAL_ERROR_STRING, e);
356 public Map<String, Resource> getChildren(Resource resource) throws ValidationException, ServiceException {
358 assert (resource != null);
362 int rId = processor.querySupport.getId(resource);
363 return QueryCache.resultChildMap(this, rId, parent, null);
365 } catch (ValidationException e) {
367 throw new ValidationException(e);
369 } catch (ServiceException e) {
371 throw new ServiceException(e);
373 } catch (DatabaseException e) {
375 throw new ServiceException(INTERNAL_ERROR_STRING, e);
381 final public Resource getRootLibrary() {
382 return processor.getRootLibraryResource();
385 final public Resource getBuiltin(final String id)
386 throws ResourceNotFoundException, ServiceException {
392 return syncRequest(new Builtin(id));
394 } catch (ResourceNotFoundException e) {
396 throw new ResourceNotFoundException(id, e);
398 } catch (ServiceException e) {
400 throw new ServiceException(e);
402 } catch (DatabaseException e) {
404 throw new ServiceException(INTERNAL_ERROR_STRING, e);
410 static class StatementReadProcedure extends TIntArrayListInternal implements StatementProcedure {
412 private static Throwable DONE = new Throwable();
414 Throwable exception = null;
416 final ResourceSupport support;
418 public StatementReadProcedure(ResourceSupport support) {
419 this.support = support;
423 public synchronized void execute(AsyncReadGraph graph, int s, int p, int o) {
430 public void finished(AsyncReadGraph graph) {
435 public void exception(AsyncReadGraph graph, Throwable t) {
439 public void checkAndThrow() throws DatabaseException {
440 if(exception != DONE) {
441 if (exception instanceof DatabaseException)
442 throw (DatabaseException) exception;
444 throw new DatabaseException(
445 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
450 public boolean done() {
451 return exception != null;
455 public boolean contains(Object obj) {
456 if(!(obj instanceof InternalStatement))
458 InternalStatement statement = (InternalStatement)obj;
462 for(int i=0;i<sizeInternal();i+=3)
463 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
468 @SuppressWarnings("unchecked")
470 public <T> T[] toArray(T[] a) {
471 int length = sizeInternal() / 3;
472 if(length > a.length) {
473 Class<?> arrayType = a.getClass();
474 a = (arrayType == Object[].class)
475 ? (T[]) new Object[length]
476 : (T[]) Array.newInstance(arrayType.getComponentType(), length);
479 for(int i=length;i<a.length;++i)
482 for(int i=0,j=0;i<sizeInternal();i+=3,++j)
483 a[j] = (T)new InternalStatement(support, getQuick(i), getQuick(i+1), getQuick(i+2));
488 public boolean add(Statement e) {
489 throw new UnsupportedOperationException();
493 public boolean remove(Object o) {
494 throw new UnsupportedOperationException();
498 public boolean addAll(Collection<? extends Statement> c) {
499 throw new UnsupportedOperationException();
502 class IteratorImpl implements ListIterator<Statement> {
506 public IteratorImpl(int index) {
511 public boolean hasNext() {
512 return index < sizeInternal();
516 public Statement next() {
517 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
523 public void remove() {
524 throw new Error("Not supported");
528 public boolean hasPrevious() {
533 public Statement previous() {
535 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
540 public int nextIndex() {
545 public int previousIndex() {
550 public void set(Statement e) {
551 throw new UnsupportedOperationException();
555 public void add(Statement e) {
556 throw new UnsupportedOperationException();
562 public Iterator<Statement> iterator() {
563 return new IteratorImpl(0);
568 return sizeInternal() / 3;
572 public Object[] toArray() {
573 Object[] result = new Object[sizeInternal() / 3];
574 for(int i=0,j=0;j<sizeInternal();i++,j+=3)
575 result[i] = new InternalStatement(support, getQuick(j), getQuick(j+1), getQuick(j+2));
580 public boolean addAll(int index, Collection<? extends Statement> c) {
581 throw new UnsupportedOperationException();
585 public Statement get(int index) {
587 if(index < 0 || index >= sizeInternal())
588 throw new IndexOutOfBoundsException();
589 return new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
593 public Statement set(int index, Statement element) {
594 throw new UnsupportedOperationException();
598 public void add(int index, Statement element) {
599 throw new UnsupportedOperationException();
603 public Statement remove(int index) {
604 throw new UnsupportedOperationException();
608 public int indexOf(Object obj) {
609 if(!(obj instanceof InternalStatement))
611 InternalStatement statement = (InternalStatement)obj;
615 for(int i=0;i<sizeInternal();i+=3)
616 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
622 public int lastIndexOf(Object obj) {
623 if(!(obj instanceof InternalStatement))
625 InternalStatement statement = (InternalStatement)obj;
629 for(int i=sizeInternal()-3;i>=0;i-=3)
630 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
636 public ListIterator<Statement> listIterator() {
637 return new IteratorImpl(0);
641 public ListIterator<Statement> listIterator(int index) {
642 return new IteratorImpl(index*3);
646 public List<Statement> subList(int fromIndex, int toIndex) {
647 if(fromIndex < 0 || toIndex*3 >= sizeInternal() || fromIndex > toIndex)
648 throw new IndexOutOfBoundsException();
649 return new RandomAccessSubList<Statement>(this, fromIndex, toIndex-fromIndex);
654 final public Collection<Statement> getStatements(final Resource subject,
655 final Resource relation)
656 throws ManyObjectsForFunctionalRelationException, ServiceException {
658 assert (subject != null);
659 assert (relation != null);
663 StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
664 processor.forEachStatement(this, subject, relation, procedure);
665 procedure.checkAndThrow();
668 } catch (DatabaseException e) {
670 System.err.println(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation);
672 StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
673 processor.forEachStatement(this, subject, relation, procedure);
675 return Collections.emptyList();
677 // throw new ServiceException(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation, e);
684 final public Collection<Statement> getAssertedStatements(final Resource subject, final Resource relation)
685 throws ManyObjectsForFunctionalRelationException, ServiceException {
687 assert (subject != null);
688 assert (relation != null);
692 return syncRequest(new ForEachAssertedStatement(subject, relation));
694 } catch (ManyObjectsForFunctionalRelationException e) {
696 throw new ManyObjectsForFunctionalRelationException(e);
698 } catch (ServiceException e) {
700 throw new ServiceException(e);
702 } catch (DatabaseException e) {
704 throw new ServiceException(INTERNAL_ERROR_STRING, e);
711 final public Collection<Resource> getPredicates(final Resource subject) throws ServiceException {
713 assert (subject != null);
717 return processor.getPredicates(this, subject);
719 // AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
720 // processor.forEachPredicate(this, subject, procedure);
721 // procedure.checkAndThrow();
724 } catch (ServiceException e) {
726 throw new ServiceException(e);
728 } catch (DatabaseException e) {
730 throw new ServiceException(INTERNAL_ERROR_STRING, e);
732 } catch (Throwable e) {
734 throw new ServiceException(e);
741 final public Collection<Resource> getPrincipalTypes(final Resource subject)
742 throws ServiceException {
744 assert (subject != null);
748 AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
749 processor.forEachPrincipalType(this, subject, procedure);
750 procedure.checkAndThrow();
753 } catch (ServiceException e) {
755 throw new ServiceException(e);
757 } catch (DatabaseException e) {
759 throw new ServiceException(INTERNAL_ERROR_STRING, e);
766 final public Set<Resource> getTypes(final Resource subject) throws ServiceException {
768 assert (subject != null);
772 return processor.getTypes(this, subject);
774 } catch (ServiceException e) {
776 throw new ServiceException(e);
778 } catch (DatabaseException e) {
780 throw new ServiceException(INTERNAL_ERROR_STRING, e);
782 } catch (Throwable e) {
784 throw new ServiceException(e);
791 final public Set<Resource> getSupertypes(final Resource subject)
792 throws ServiceException {
794 assert (subject != null);
798 SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
799 processor.forSupertypes(this, subject, procedure);
800 procedure.checkAndThrow();
801 return procedure.result;
803 } catch (ServiceException e) {
805 throw new ServiceException(e);
807 } catch (DatabaseException e) {
809 throw new ServiceException(INTERNAL_ERROR_STRING, e);
816 final public Set<Resource> getSuperrelations(final Resource subject)
817 throws ServiceException {
819 assert (subject != null);
823 SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
824 processor.forSuperrelations(this, subject, procedure);
825 procedure.checkAndThrow();
826 return procedure.result;
828 } catch (ServiceException e) {
830 throw new ServiceException(e);
832 } catch (DatabaseException e) {
834 throw new ServiceException(INTERNAL_ERROR_STRING, e);
841 public Resource getPossibleSuperrelation(Resource subject) throws ServiceException {
845 SyncReadProcedure<Resource> procedure = new SyncReadProcedure<Resource>();
846 processor.forPossibleSuperrelation(this, subject, procedure);
847 procedure.checkAndThrow();
848 return procedure.result;
850 } catch (ServiceException e) {
852 throw new ServiceException(e);
854 } catch (DatabaseException e) {
856 throw new ServiceException(INTERNAL_ERROR_STRING, e);
863 final public Collection<Resource> getObjects(final Resource subject, final Resource relation)
864 throws ServiceException {
866 assert (subject != null);
867 assert (relation != null);
869 if(Development.DEVELOPMENT) {
870 if(Development.isTrue(DevelopmentKeys.READGRAPH_COUNT)) {
871 counters.adjustOrPutValue("objects $" + subject.getResourceId() + " $" + relation.getResourceId(), 1, 1);
873 //if(subject.getResourceId()==xx && relation.getResourceId()==xx) new Exception().printStackTrace();
878 AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
879 processor.forEachObject(this, subject, relation, procedure);
880 procedure.checkAndThrow();
883 } catch (DatabaseException e) {
885 throw new ServiceException(INTERNAL_ERROR_STRING, e);
892 final public Collection<Resource> getAssertedObjects(
893 final Resource subject, final Resource relation)
894 throws ManyObjectsForFunctionalRelationException, ServiceException {
897 throw new ArgumentException("Subject must not be null.");
898 if (relation == null)
899 throw new ArgumentException("Relation must not be null. Subject=" + subject);
903 return syncRequest(new ForEachAssertedObject(subject, relation));
905 } catch (ManyObjectsForFunctionalRelationException e) {
907 throw new ManyObjectsForFunctionalRelationException(e);
909 } catch (ServiceException e) {
911 throw new ServiceException(e);
913 } catch (DatabaseException e) {
915 throw new ServiceException(INTERNAL_ERROR_STRING, e);
922 final public Resource getInverse(final Resource relation) throws NoInverseException, ServiceException {
924 assert (relation != null);
928 return getSingleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
930 } catch (NoSingleResultException e) {
932 throw new NoInverseException(e);
934 } catch (ServiceException e) {
936 throw new ServiceException(e);
943 final public Resource getSingleObject(final Resource subject, final Resource relation) throws NoSingleResultException, ServiceException {
945 if( subject == null) throw new IllegalArgumentException("subject can not be null");
946 if( relation == null) throw new IllegalArgumentException("relation can not be null");
949 int single = processor.getSingleObject(this, subject, relation);
951 if (EMPTY_RESOURCE_CHECK) {
952 if (!hasStatement(subject)) {
953 throw new EmptyResourceException("Resource " + debugString(subject));
956 throw new NoSingleResultException("No single object for subject " + debugString(subject)
957 + " and relation " + debugString(relation), single);
959 return processor.querySupport.getResource(single);
960 } catch (NoSingleResultException e) {
962 } catch (DatabaseException e) {
963 throw new ServiceException(e);
968 final public Statement getSingleStatement(final Resource subject, final Resource relation) throws NoSingleResultException, ManyObjectsForFunctionalRelationException, ServiceException {
969 assert (subject != null);
970 assert (relation != null);
972 Collection<Statement> statements = getStatements(subject, relation);
973 if (statements.size() == 1) {
974 return statements.iterator().next();
976 if (EMPTY_RESOURCE_CHECK)
977 if (!hasStatement(subject))
978 throw new EmptyResourceException("Resource " + debugString(subject));
979 throw new NoSingleResultException("No single statement for subject " + debugString(subject)
980 + " and relation " + debugString(relation), statements.size());
982 } catch (ServiceException e) {
983 throw new ServiceException(e);
988 final public Resource getSingleType(final Resource subject) throws NoSingleResultException, ServiceException {
989 assert (subject != null);
991 ArrayList<Resource> principalTypes = (ArrayList<Resource>)getPrincipalTypes(subject);
992 if (principalTypes.size() == 1) {
993 return principalTypes.get(0);
995 throw new NoSingleResultException("No single type for subject " + debugString(subject), principalTypes.size());
997 } catch (ServiceException e) {
998 throw new ServiceException(e);
1003 final public Resource getSingleType(final Resource subject,
1004 final Resource baseType) throws NoSingleResultException,
1007 assert (subject != null);
1008 assert (baseType != null);
1011 return syncRequest(new SingleType(subject, baseType));
1012 } catch (DatabaseException e) {
1013 throw new NoSingleResultException("subject=" + subject + ", baseType=" + baseType, 0, e);
1018 final public <T> T getValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
1020 assert (subject != null);
1024 Layer0 L0 = processor.getL0(this);
1025 int object = processor.getSingleObject(this, subject, L0.HasDataType);
1026 if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
1028 if(processor.isImmutable(object)) {
1029 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1030 return getValue(subject, binding);
1032 byte[] dt = processor.getValue(this, object);
1033 if(dt == null) throw new ServiceException("No data type for " + subject);
1034 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1035 Binding binding = Bindings.getBinding(datatype);
1036 return getValue(subject, binding);
1039 } catch (IOException e) {
1041 throw new ServiceException(e);
1043 } catch (DoesNotContainValueException e) {
1045 throw new DoesNotContainValueException(e, subject);
1047 } catch (ServiceException e) {
1049 throw new ServiceException(e);
1051 } catch (DatabaseException e) {
1053 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1060 final public Variant getVariantValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
1062 assert (subject != null);
1066 Layer0 L0 = processor.getL0(this);
1067 int object = processor.getSingleObject(this, subject, L0.HasDataType);
1068 if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
1070 if(processor.isImmutable(object)) {
1071 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1072 return new Variant(binding, getValue(subject, binding));
1074 byte[] dt = processor.getValue(this, object);
1075 if(dt == null) throw new ServiceException("No data type for " + subject);
1076 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1077 Binding binding = Bindings.getBinding(datatype);
1078 return new Variant(binding, getValue(subject, binding));
1081 } catch (IOException e) {
1083 throw new ServiceException(e);
1085 } catch (DoesNotContainValueException e) {
1087 throw new DoesNotContainValueException(e, subject);
1089 } catch (ServiceException e) {
1091 throw new ServiceException(e);
1093 } catch (DatabaseException e) {
1095 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1100 static final IdentityHashMap<Binding,Serializer> serializers = new IdentityHashMap<Binding,Serializer>();
1103 serializers.put(Bindings.STRING, Bindings.STRING.serializer());
1106 final protected Serializer getSerializer(Binding binding) {
1107 return binding.serializer();
1111 final public <T> T getValue(final Resource subject, final Binding binding) throws DoesNotContainValueException, BindingException,
1114 assert (subject != null);
1118 byte[] bytes = processor.getValue(this, subject);
1119 if (bytes == null) throw new DoesNotContainValueException("No value for resource " + subject);
1121 Serializer serializer = getSerializer(binding);
1122 return (T)serializer.deserialize(bytes);
1124 } catch (DoesNotContainValueException e) {
1126 throw new DoesNotContainValueException(e);
1128 } catch (IOException e) {
1130 throw new ServiceException(e);
1132 } catch (DatabaseException e) {
1134 throw new ServiceException(e);
1136 } catch (BufferUnderflowException e) {
1137 // This is sometimes thrown when deserialize fails because wrong format.
1138 // For callers of this method this is just an service exception.
1139 throw new ServiceException(e);
1145 final public <T> T getRelatedValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1146 DoesNotContainValueException, ServiceException {
1148 assert (subject != null);
1149 assert (relation != null);
1152 Resource object = getSingleObject(subject, relation);
1153 return getValue(object);
1154 } catch (NoSingleResultException e) {
1155 throw new NoSingleResultException("No single value found for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1156 } catch (DoesNotContainValueException e) {
1158 Layer0 L0 = processor.getL0(this);
1159 Resource object = getPossibleObject(subject, relation);
1160 if(isInstanceOf(object, L0.Value)) {
1161 if(isInstanceOf(object, L0.Literal)) {
1162 throw new DoesNotContainValueException(e);
1164 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1167 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1169 } catch (DoesNotContainValueException e2) {
1171 } catch (DatabaseException e2) {
1172 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1174 } catch (ServiceException e) {
1175 throw new ServiceException(e);
1180 final public Variant getRelatedVariantValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1181 DoesNotContainValueException, ServiceException {
1183 assert (subject != null);
1184 assert (relation != null);
1187 Resource object = getSingleObject(subject, relation);
1188 return getVariantValue(object);
1189 } catch (NoSingleResultException e) {
1190 throw new NoSingleResultException("No single object for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1191 } catch (DoesNotContainValueException e) {
1193 Layer0 L0 = processor.getL0(this);
1194 Resource object = getPossibleObject(subject, relation);
1195 if(isInstanceOf(object, L0.Value)) {
1196 if(isInstanceOf(object, L0.Literal)) {
1197 throw new DoesNotContainValueException(e);
1199 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1202 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1204 } catch (DoesNotContainValueException e2) {
1206 } catch (DatabaseException e2) {
1207 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1209 } catch (ServiceException e) {
1210 throw new ServiceException(e);
1215 final public <T> T getRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1216 throws NoSingleResultException, DoesNotContainValueException, BindingException, ServiceException {
1218 assert (subject != null);
1219 assert (relation != null);
1222 Resource object = getSingleObject(subject, relation);
1223 return getValue(object, binding);
1224 } catch (NoSingleResultException e) {
1225 String message = "";
1227 String subjectName = NameUtils.getSafeName(this, subject, true);
1228 String relationName = NameUtils.getSafeName(this, relation, true);
1229 message = "Subject: " + subjectName + ", Relation: " + relationName;
1230 } catch (DatabaseException e2) {
1233 throw new NoSingleResultException(message, e.getResultCount(), e);
1234 } catch (DoesNotContainValueException e) {
1235 throw new DoesNotContainValueException(e);
1236 } catch (ServiceException e) {
1237 throw new ServiceException(e);
1242 final public <T> T adapt(final Resource resource, final Class<T> clazz)
1243 throws AdaptionException, ValidationException, ServiceException {
1245 assert (resource != null);
1246 assert (clazz != null);
1250 return syncRequest(new Adapter<T>(resource, clazz));
1252 } catch (AdaptionException e) {
1254 throw new AdaptionException(e);
1256 } catch (ValidationException e) {
1258 throw new ValidationException(e);
1260 } catch (ServiceException e) {
1262 throw new ServiceException(e);
1264 } catch (DatabaseException e) {
1266 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1273 final public <T,C> T adaptContextual(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1274 throws AdaptionException, ValidationException, ServiceException {
1276 assert (resource != null);
1277 assert (context != null);
1279 class ContextualAdapter implements AsyncRead<T> {
1281 final private Resource resource;
1282 final private C context;
1283 final private Class<T> clazz;
1286 public int hashCode() {
1287 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1291 final public int threadHash() {
1292 return resource.getThreadHash();
1296 public boolean equals(Object object) {
1299 else if (object == null)
1301 else if (getClass() != object.getClass())
1303 ContextualAdapter r = (ContextualAdapter)object;
1304 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1308 public int getFlags() {
1312 public ContextualAdapter(Resource resource, C context, Class<T> clazz) {
1313 this.resource = resource;
1314 this.context = context;
1319 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1321 final AdaptionService service = getSession().peekService(AdaptionService.class);
1322 if (service == null)
1323 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1325 service.adapt(graph, resource, context, contextClass, clazz, false, procedure);
1330 public String toString() {
1331 return "Adapter for (" + resource + "," + context + ") as " + clazz.getName();
1338 return syncRequest(new ContextualAdapter(resource, context, clazz));
1340 } catch (AdaptionException e) {
1342 throw new AdaptionException(e);
1344 } catch (ValidationException e) {
1346 throw new ValidationException(e);
1348 } catch (ServiceException e) {
1350 throw new ServiceException(e);
1352 } catch (DatabaseException e) {
1354 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1361 final public <T> T adaptRelated(final Resource resource, final Resource relation, final Class<T> clazz)
1362 throws AdaptionException, NoSingleResultException, ValidationException, ServiceException {
1364 assert (resource != null);
1365 assert (clazz != null);
1367 Statement stm = getSingleStatement(resource, relation);
1369 return adaptContextual(stm.getObject(), new RelationContextImpl(resource, stm), RelationContext.class, clazz);
1374 final public <T> T getPossibleRelatedAdapter(final Resource resource, final Resource relation, final Class<T> clazz)
1375 throws ValidationException, ServiceException {
1378 return adaptRelated(resource, relation, clazz);
1379 } catch (DatabaseException e) {
1386 final public <T,C> T getPossibleContextualAdapter(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1387 throws ValidationException, ServiceException {
1389 assert (resource != null);
1390 assert (context != null);
1392 class PossibleContextualAdapter implements AsyncRead<T> {
1394 final private Resource resource;
1395 final private C context;
1396 final private Class<T> clazz;
1399 public int hashCode() {
1400 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1404 final public int threadHash() {
1405 return resource.getThreadHash();
1409 public boolean equals(Object object) {
1412 else if (object == null)
1414 else if (getClass() != object.getClass())
1416 PossibleContextualAdapter r = (PossibleContextualAdapter)object;
1417 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1421 public int getFlags() {
1425 public PossibleContextualAdapter(Resource resource, C context, Class<T> clazz) {
1426 this.resource = resource;
1427 this.context = context;
1432 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1434 final AdaptionService service = getSession().peekService(AdaptionService.class);
1435 if (service == null)
1436 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1438 service.adapt(graph, resource, context, contextClass, clazz, true, procedure);
1443 public String toString() {
1444 return "Possible adapter for (" + resource + "," + context + ") as " + clazz.getName();
1451 return syncRequest(new PossibleContextualAdapter(resource, context, clazz));
1453 } catch (ValidationException e) {
1455 throw new ValidationException(e);
1457 } catch (ServiceException e) {
1459 throw new ServiceException(e);
1461 } catch (DatabaseException e) {
1463 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1470 final public <T> T adaptUnique(final Resource resource, final Class<T> clazz)
1471 throws AdaptionException, ValidationException, ServiceException {
1473 assert (resource != null);
1474 assert (clazz != null);
1478 return syncRequest(new UniqueAdapter<T>(resource, clazz));
1480 } catch (AdaptionException e) {
1482 throw new AdaptionException(e);
1484 } catch (ValidationException e) {
1486 throw new ValidationException(e);
1488 } catch (ServiceException e) {
1490 throw new ServiceException(e);
1492 } catch (DatabaseException e) {
1494 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1501 final public Resource getPossibleInverse(final Resource relation)
1502 throws ServiceException {
1504 assert (relation != null);
1508 return getPossibleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
1510 } catch (ServiceException e) {
1512 throw new ServiceException(e);
1514 } catch (DatabaseException e) {
1516 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1523 public Resource getPossibleObject(final Resource subject, final Resource relation)
1524 throws ManyObjectsForFunctionalRelationException, ServiceException {
1526 assert (subject != null);
1527 assert (relation != null);
1531 int result = processor.getSingleObject(this, subject, relation);
1532 if(result == 0) return null;
1534 return processor.querySupport.getResource(result);
1536 } catch (ManyObjectsForFunctionalRelationException e) {
1538 throw new ManyObjectsForFunctionalRelationException("subject=" + subject + ", relation=" + relation, e);
1540 } catch (DatabaseException e) {
1542 throw new ServiceException(e);
1549 final public Statement getPossibleStatement(final Resource subject, final Resource relation)
1550 throws ManyObjectsForFunctionalRelationException, ServiceException {
1552 assert (subject != null);
1553 assert (relation != null);
1557 Collection<Statement> statements = getStatements(subject, relation);
1558 if(statements.size() == 1) return statements.iterator().next();
1561 } catch (ManyObjectsForFunctionalRelationException e) {
1563 throw new ManyObjectsForFunctionalRelationException("Many objects in " + subject + " for functional relation " + relation);
1565 } catch (ServiceException e) {
1567 throw new ServiceException(e);
1574 final public Resource getPossibleType(final Resource subject, final Resource baseType) throws ServiceException {
1576 assert (subject != null);
1577 assert (baseType != null);
1581 AsyncReadProcedure<Resource> procedure = new AsyncReadProcedure<Resource>();
1582 forPossibleType(subject, baseType, procedure);
1583 procedure.checkAndThrow();
1584 return procedure.result;
1586 } catch (ServiceException e) {
1588 throw new ServiceException(e);
1590 } catch (DatabaseException e) {
1592 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1599 final public <T> T getPossibleValue(final Resource subject) throws ServiceException {
1601 assert (subject != null);
1605 int object = processor.getSingleObject(this, subject, processor.getL0(this).HasDataType);
1606 if(object == 0) return null;
1608 if(processor.isImmutable(object)) {
1609 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1610 return getPossibleValue(subject, binding);
1612 byte[] dt = processor.getValue(this, object);
1613 if(dt == null) return null;
1614 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1615 Binding binding = Bindings.getBinding(datatype);
1616 return getPossibleValue(subject, binding);
1619 } catch (IOException e) {
1621 throw new ServiceException(e);
1623 } catch (ServiceException e) {
1625 throw new ServiceException(e);
1627 } catch (DatabaseException e) {
1629 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1636 final public <T> T getPossibleValue(final Resource subject, final Binding binding) throws BindingException, ServiceException {
1638 assert (subject != null);
1639 assert (binding != null);
1643 byte[] dt = processor.getValue(this, subject);
1644 if(dt == null) return null;
1645 Serializer serializer = getSerializer(binding);
1646 return (T)serializer.deserialize(dt);
1648 } catch (IOException e) {
1650 throw new ServiceException(e);
1652 } catch (BindingException e) {
1654 throw new BindingException(e);
1656 } catch (ServiceException e) {
1658 throw new ServiceException(e);
1660 } catch (DatabaseException e) {
1661 e.printStackTrace();
1662 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1668 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation)
1669 throws ManyObjectsForFunctionalRelationException, ServiceException {
1671 assert (subject != null);
1672 assert (relation != null);
1676 Resource object = getPossibleObject(subject, relation);
1677 if(object == null) return null;
1678 else return getPossibleValue(object);
1680 } catch (ManyObjectsForFunctionalRelationException e) {
1682 throw new ManyObjectsForFunctionalRelationException(e);
1684 } catch (ServiceException e) {
1686 throw new ServiceException(e);
1693 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1694 throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
1696 assert (subject != null);
1697 assert (relation != null);
1698 assert (binding != null);
1702 Resource object = getPossibleObject(subject, relation);
1703 if(object == null) return null;
1704 else return getPossibleValue(object, binding);
1706 } catch (ManyObjectsForFunctionalRelationException e) {
1708 throw new ManyObjectsForFunctionalRelationException(e);
1710 } catch (BindingException e) {
1712 throw new BindingException(e);
1714 } catch (ServiceException e) {
1716 throw new ServiceException(e);
1723 public <T> T getPossibleAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1725 assert (resource != null);
1726 assert (clazz != null);
1730 return syncRequest(new PossibleAdapter<T>(resource, clazz));
1732 } catch (ValidationException e) {
1734 throw new ValidationException(e);
1736 } catch (AdaptionException e) {
1740 } catch (DatabaseException e) {
1742 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1748 public <T> T getPossibleUniqueAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1750 assert (resource != null);
1751 assert (clazz != null);
1755 return syncRequest(new PossibleUniqueAdapter<T>(resource, clazz));
1757 } catch (AdaptionException e) {
1761 } catch (ValidationException e) {
1763 throw new ValidationException(e);
1765 } catch (DatabaseException e) {
1767 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1774 final public boolean isInstanceOf(final Resource resource, final Resource type) throws ServiceException {
1776 assert (resource != null);
1777 assert (type != null);
1779 Set<Resource> resources = getTypes(resource);
1780 // This check was necessary because some of the callers of this method got stuck when the NPE was thrown from here.
1781 if (null == resources)
1784 if(EMPTY_RESOURCE_CHECK) {
1785 if (resources.isEmpty()) {
1786 if(!hasStatement(resource)) throw new EmptyResourceException("Resource " + debugString(resource));
1790 return resources.contains(type);
1795 final public boolean isInheritedFrom(final Resource resource, final Resource type) throws ServiceException {
1797 assert (resource != null);
1798 assert (type != null);
1802 if(resource.equals(type)) return true;
1804 return getSupertypes(resource).contains(type);
1806 } catch (ServiceException e) {
1808 throw new ServiceException(e);
1815 final public boolean isSubrelationOf(final Resource resource, final Resource type) throws ServiceException {
1817 assert (resource != null);
1818 assert (type != null);
1822 if(resource.equals(type)) return true;
1824 return getSuperrelations(resource).contains(type);
1826 } catch (ServiceException e) {
1828 throw new ServiceException(e);
1835 final public boolean hasStatement(final Resource subject) throws ServiceException {
1837 assert (subject != null);
1841 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1842 processor.forHasStatement(this, subject, procedure);
1843 procedure.checkAndThrow();
1844 return procedure.result;
1846 } catch (ServiceException e) {
1848 throw new ServiceException(e);
1850 } catch (DatabaseException e) {
1852 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1859 final public boolean hasStatement(final Resource subject, final Resource relation) throws ServiceException {
1861 assert (subject != null);
1862 assert (relation != null);
1866 Collection<Resource> objects = getObjects(subject, relation);
1867 return !objects.isEmpty();
1869 } catch (ServiceException e) {
1871 throw new ServiceException(e);
1878 final public boolean hasStatement(final Resource subject, final Resource relation, final Resource object) throws ServiceException {
1880 assert (subject != null);
1881 assert (relation != null);
1882 assert (object != null);
1886 for(Resource o : getObjects(subject, relation)) {
1887 if(object.equals(o)) return true;
1892 } catch (ServiceException e) {
1894 throw new ServiceException(e);
1901 final public boolean hasValue(final Resource subject) throws ServiceException {
1903 assert (subject != null);
1907 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1908 processor.forHasValue(this, subject, procedure);
1909 procedure.checkAndThrow();
1910 return procedure.result;
1912 } catch (ServiceException e) {
1914 throw new ServiceException(e);
1916 } catch (DatabaseException e) {
1918 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1924 final AsyncProcedure<?> NONE = new AsyncProcedure<Object>() {
1927 public void execute(AsyncReadGraph graph, Object result) {
1931 public void exception(AsyncReadGraph graph, Throwable throwable) {
1937 * Implementation of the interface RequestProcessor
1941 public <T> T syncRequest(final Read<T> request) throws DatabaseException {
1943 assert (request != null);
1945 return QueryCache.resultReadEntry(this, request, parent, null, null);
1947 //return processor.query(this, request, parent, null, null);
1949 // if (parent != null) {
1952 // } catch (Throwable e) {
1953 // if(e instanceof DatabaseException) throw (DatabaseException)e;
1954 // else throw new DatabaseException(e);
1961 // return processor.tryQuery(this, request);
1963 // } catch (Throwable throwable) {
1965 // //Logger.defaultLogError("Internal read request failure", throwable);
1967 // if (throwable instanceof DatabaseException)
1968 // throw (DatabaseException) throwable;
1970 // throw new DatabaseException(
1971 // "Unexpected exception in ReadGraph.syncRequest(Read,Procedure)",
1981 public <T> T syncRequest(Read<T> request, SyncListener<T> procedure)
1982 throws DatabaseException {
1983 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
1987 public <T> T syncRequest(Read<T> request, final Listener<T> procedure)
1988 throws DatabaseException {
1989 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
1993 public <T> T syncRequest(final Read<T> request, final AsyncProcedure<T> procedure) throws DatabaseException {
1995 assert (request != null);
1997 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
1999 return QueryCache.resultReadEntry(this, request, parent, listener, procedure);
2001 // return processor.query(this, request, parent, procedure, listener);
2004 // if (parent != null || listener != null) {
2007 // } catch (Throwable e) {
2008 // if(e instanceof DatabaseException) throw (DatabaseException)e;
2009 // else throw new DatabaseException(e);
2016 // T t = processor.tryQuery(this, request);
2017 // if(procedure != null)
2018 // procedure.execute(this, t);
2022 // } catch (Throwable throwable) {
2024 // Logger.defaultLogError("Internal read request failure", throwable);
2026 // if(procedure != null)
2027 // procedure.exception(this, throwable);
2029 // if (throwable instanceof DatabaseException)
2030 // throw (DatabaseException) throwable;
2032 // throw new DatabaseException(
2033 // "Unexpected exception in ReadGraph.syncRequest(Read,Procedure)",
2043 public <T> T syncRequest(final Read<T> request,
2044 final SyncProcedure<T> procedure) throws DatabaseException {
2045 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2049 public <T> T syncRequest(Read<T> request, Procedure<T> procedure)
2050 throws DatabaseException {
2051 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2054 static class AsyncReadProcedure<T> implements AsyncProcedure<T> {
2056 private static Throwable DONE = new Throwable();
2059 Throwable exception = null;
2062 public void execute(AsyncReadGraph graph, T t) {
2068 public void exception(AsyncReadGraph graph, Throwable t) {
2072 public void checkAndThrow() throws DatabaseException {
2073 if(exception != DONE) {
2074 if (exception instanceof DatabaseException)
2075 throw (DatabaseException) exception;
2077 throw new DatabaseException(
2078 "Unexpected exception in ReadGraph.syncRequest(AsyncRead)",
2083 public boolean done() {
2084 return exception != null;
2090 public <T> T syncRequest(final AsyncRead<T> request)
2091 throws DatabaseException {
2093 assert (request != null);
2094 // AsyncReadProcedure<T> procedure = new AsyncReadProcedure<T>();
2095 BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, null, request);
2096 syncRequest(request, ap);
2098 // procedure.checkAndThrow();
2099 // return procedure.result;
2101 // return syncRequest(request, new AsyncProcedureAdapter<T>());
2106 public <T> T syncRequest(AsyncRead<T> request, AsyncListener<T> procedure)
2107 throws DatabaseException {
2108 return syncRequest(request, (AsyncProcedure<T>) procedure);
2112 public <T> T syncRequest(AsyncRead<T> request, SyncListener<T> procedure)
2113 throws DatabaseException {
2114 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
2118 public <T> T syncRequest(AsyncRead<T> request, Listener<T> procedure)
2119 throws DatabaseException {
2120 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
2124 final public <T> T syncRequest(final AsyncRead<T> request,
2125 final AsyncProcedure<T> procedure) throws DatabaseException {
2127 assert (request != null);
2129 ListenerBase listener = getListenerBase(procedure);
2131 BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, procedure, request);
2133 // final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
2134 // procedure, request);
2136 QueryCache.runnerAsyncReadEntry(this, request, parent, listener, ap, true);
2138 //processor.query(this, request, parent, wrapper, listener);
2142 // return wrapper.getResult();
2144 // if (parent != null || listener != null || ((request.getFlags() & RequestFlags.SCHEDULE) > 0)) {
2146 //// Object syncParent = request;
2148 //// final ReadGraphImpl newGraph = newSync();
2151 //// newGraph.waitAsync(syncParent);
2153 // Throwable e = wrapper.getException();
2155 // // The request was async - produce meaningful stack trace by
2157 // if (e instanceof DatabaseException)
2158 // throw (DatabaseException) e;
2160 // throw new DatabaseException(e);
2163 // return wrapper.getResult();
2167 // // System.out.println("direct call " + request );
2169 // // Do not set the sync state.parent for external threads
2170 //// Object syncParent = request;
2172 //// final ReadGraphImpl newGraph = newSync();
2174 // final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
2175 // procedure, request);
2179 // processor.tryQuery(this, request, wrapper);
2181 // } catch (Throwable t) {
2183 // wrapper.exception(this, t);
2187 // Throwable e = wrapper.getException();
2189 // // The request was async - produce meaningful stack trace by
2191 // if (e instanceof DatabaseException)
2192 // throw (DatabaseException) e;
2194 // throw new DatabaseException(e);
2197 // return wrapper.getResult();
2203 final private <T> void syncRequest(final AsyncRead<T> request, final AsyncReadProcedure<T> procedure) throws DatabaseException {
2205 assert (request != null);
2207 ListenerBase listener = getListenerBase(procedure);
2208 assert(listener == null);
2210 BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, procedure, request);
2212 // final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
2213 // procedure, request);
2215 QueryCache.runnerAsyncReadEntry(this, request, parent, listener, ap, true);
2222 public <T> T syncRequest(AsyncRead<T> request,
2223 final SyncProcedure<T> procedure) throws DatabaseException {
2224 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2228 final public <T> T syncRequest(final AsyncRead<T> request,
2229 final Procedure<T> procedure) throws DatabaseException {
2230 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2234 public <T> Collection<T> syncRequest(final MultiRead<T> request)
2235 throws DatabaseException {
2237 assert (request != null);
2239 final ArrayList<T> result = new ArrayList<T>();
2240 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2242 syncRequest(request, new AsyncMultiProcedure<T>() {
2245 public void execute(AsyncReadGraph graph, T t) {
2246 synchronized (result) {
2252 public void finished(AsyncReadGraph graph) {
2256 public void exception(AsyncReadGraph graph, Throwable t) {
2261 public String toString() {
2262 return "syncRequest(MultiRead) -> " + request;
2267 Throwable t = exception.get();
2269 if (t instanceof DatabaseException)
2270 throw (DatabaseException) t;
2272 throw new DatabaseException(
2273 "Unexpected exception in ReadGraph.syncRequest(Read)",
2282 public <T> Collection<T> syncRequest(MultiRead<T> request,
2283 AsyncMultiListener<T> procedure) {
2284 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2288 public <T> Collection<T> syncRequest(MultiRead<T> request,
2289 SyncMultiListener<T> procedure) {
2290 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2294 public <T> Collection<T> syncRequest(MultiRead<T> request,
2295 MultiListener<T> procedure) {
2296 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2300 public <T> Collection<T> syncRequest(MultiRead<T> request,
2301 AsyncMultiProcedure<T> procedure) {
2303 assert (request != null);
2305 ListenerBase listener = getListenerBase(procedure);
2307 if (parent != null || listener != null) {
2309 // Object syncParent = request;
2311 // final ReadGraphImpl newGraph = newSync();
2313 processor.query(this, request, parent, procedure, listener);
2315 // newGraph.waitAsync(syncParent);
2319 // Object syncParent = request;
2321 // final ReadGraphImpl newGraph = newSync();
2323 final ResultCallWrappedQueryProcedure4<T> wrapper = new ResultCallWrappedQueryProcedure4<T>(procedure);
2327 request.perform(this, wrapper);
2329 } catch (Throwable t) {
2331 wrapper.exception(this, t);
2332 // newGraph.waitAsync(syncParent);
2344 public <T> Collection<T> syncRequest(MultiRead<T> request,
2345 SyncMultiProcedure<T> procedure) {
2346 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2350 public <T> Collection<T> syncRequest(MultiRead<T> request,
2351 MultiProcedure<T> procedure) {
2352 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2355 static class AsyncMultiReadProcedure<T> extends ArrayList<T> implements AsyncMultiProcedure<T> {
2357 private static Throwable DONE = new Throwable();
2359 private static final long serialVersionUID = -6494230465108115812L;
2361 Throwable exception = null;
2364 public synchronized void execute(AsyncReadGraph graph, T t) {
2369 public void finished(AsyncReadGraph graph) {
2374 public void exception(AsyncReadGraph graph, Throwable t) {
2378 public void checkAndThrow() throws DatabaseException {
2379 if(exception != DONE) {
2380 if (exception instanceof DatabaseException)
2381 throw (DatabaseException) exception;
2383 throw new DatabaseException(
2384 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
2389 public boolean done() {
2390 return exception != null;
2396 final public <T> Collection<T> syncRequest(AsyncMultiRead<T> request)
2397 throws DatabaseException {
2399 assert (request != null);
2401 final AsyncMultiReadProcedure<T> procedure = new AsyncMultiReadProcedure<T>();
2403 syncRequest(request, procedure);
2405 procedure.checkAndThrow();
2411 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2412 AsyncMultiListener<T> procedure) {
2413 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2417 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2418 SyncMultiListener<T> procedure) {
2419 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2423 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2424 MultiListener<T> procedure) {
2425 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2428 final private <T> void syncRequest(final AsyncMultiRead<T> request,
2429 final AsyncMultiReadProcedure<T> procedure) {
2431 assert (request != null);
2432 assert (procedure != null);
2434 ListenerBase listener = getListenerBase(procedure);
2436 if (parent != null || listener != null) {
2438 // Object syncParent = request;
2440 // final ReadGraphImpl newGraph = newSync();
2442 processor.query(this, request, parent, procedure, listener);
2444 // newGraph.waitAsync(syncParent);
2445 waitAsyncProcedure(procedure);
2449 // Object syncParent = callerThread == Integer.MIN_VALUE ? null
2452 // final ReadGraphImpl newGraph = newSyncAsync(syncParent);
2457 // ReadGraphImpl sync = newSync();
2458 request.perform(this, procedure);
2459 // sync.waitAsync(null);
2460 waitAsyncProcedure(procedure);
2463 } catch (Throwable t) {
2465 waitAsyncProcedure(procedure);
2476 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2477 final AsyncMultiProcedure<T> procedure) {
2479 assert (request != null);
2480 assert (procedure != null);
2482 ListenerBase listener = getListenerBase(procedure);
2484 if (parent != null || listener != null) {
2486 // Object syncParent = request;
2488 // final ReadGraphImpl newGraph = newSync();
2490 processor.query(this, request, parent, procedure, listener);
2492 // newGraph.waitAsync(syncParent);
2496 // Object syncParent = request;
2498 // final ReadGraphImpl newGraph = newSync();
2502 request.perform(this, new AsyncMultiProcedure<T>() {
2505 public void execute(AsyncReadGraph graph, T result) {
2506 procedure.execute(graph, result);
2510 public void finished(AsyncReadGraph graph) {
2511 procedure.finished(graph);
2515 public void exception(AsyncReadGraph graph, Throwable t) {
2516 procedure.exception(graph, t);
2520 public String toString() {
2521 return "syncRequest(AsyncMultiRead) -> " + procedure;
2526 } catch (Throwable t) {
2538 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2539 final SyncMultiProcedure<T> procedure) {
2540 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2544 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2545 final MultiProcedure<T> procedure) {
2546 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2550 public <T> T syncRequest(final ExternalRead<T> request)
2551 throws DatabaseException {
2553 assert (request != null);
2555 return syncRequest(request, new Procedure<T>() {
2558 public void execute(T t) {
2562 public void exception(Throwable t) {
2566 public String toString() {
2567 return "syncRequest(AsyncRead) -> " + request;
2575 public <T> T syncRequest(ExternalRead<T> request, Listener<T> procedure) throws DatabaseException {
2576 return syncRequest(request, (Procedure<T>) procedure);
2580 final public <T> T syncRequest(final ExternalRead<T> request,
2581 final Procedure<T> procedure) throws DatabaseException {
2583 assert (request != null);
2585 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
2586 return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
2589 // ListenerBase listener = getListenerBase(procedure);
2591 // return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
2593 // assert (request != null);
2595 // ListenerBase listener = getListenerBase(procedure);
2597 // final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2598 // final DataContainer<T> result = new DataContainer<T>();
2600 // processor.query(this, request, parent, new Procedure<T>() {
2603 // public void exception(Throwable throwable) {
2604 // exception.set(throwable);
2605 // procedure.exception(throwable);
2609 // public void execute(T t) {
2611 // procedure.execute(t);
2618 // if (parent != null || listener != null) {
2620 //// final ReadGraphImpl newGraph = newSync();
2623 //// newGraph.waitAsync(request);
2629 // T t = processor.tryQuery(this, request);
2631 // procedure.execute(t);
2633 // } catch (Throwable t) {
2635 // if (t instanceof DatabaseException) {
2636 // exception.set((DatabaseException)t);
2637 // procedure.exception(exception.get());
2639 // exception.set(new DatabaseException(
2640 // "Unexpected exception in ReadGraph.syncRequest(Read)",
2642 // procedure.exception(exception.get());
2649 // Throwable t = exception.get();
2651 // if (t instanceof DatabaseException)
2652 // throw (DatabaseException) t;
2654 // throw new DatabaseException(
2655 // "Unexpected exception in ReadGraph.syncRequest(Read)",
2659 // return result.get();
2664 public void syncRequest(final Write request) throws DatabaseException {
2666 assert (request != null);
2668 throw new DatabaseException(
2669 "Write operations are not supported during read transactions!");
2674 public <T> T syncRequest(final WriteResult<T> request) throws DatabaseException {
2676 assert (request != null);
2678 throw new DatabaseException(
2679 "Write operations are not supported during read transactions!");
2684 public void syncRequest(final DelayedWrite request)
2685 throws DatabaseException {
2687 assert (request != null);
2689 throw new DatabaseException(
2690 "Write operations are not supported during read transactions!");
2695 public <T> T syncRequest(final DelayedWriteResult<T> request) throws DatabaseException {
2697 assert (request != null);
2699 throw new DatabaseException(
2700 "Write operations are not supported during read transactions!");
2705 public void syncRequest(final WriteOnly request) throws DatabaseException {
2707 assert (request != null);
2709 throw new DatabaseException(
2710 "Write operations are not supported during read transactions!");
2715 public <T> T syncRequest(final WriteOnlyResult<T> request) throws DatabaseException {
2717 assert (request != null);
2719 throw new DatabaseException(
2720 "Write operations are not supported during read transactions!");
2725 public <T> void async(ReadInterface<T> r, AsyncProcedure<T> procedure) {
2726 r.request(this, procedure);
2730 public <T> void async(ReadInterface<T> r, Procedure<T> procedure) {
2731 r.request(this, procedure);
2735 public <T> void async(ReadInterface<T> r, SyncProcedure<T> procedure) {
2736 r.request(this, procedure);
2740 public <T> void async(ReadInterface<T> r, AsyncListener<T> procedure) {
2741 r.request(this, procedure);
2745 public <T> void async(ReadInterface<T> r, Listener<T> procedure) {
2746 r.request(this, procedure);
2750 public <T> void async(ReadInterface<T> r, SyncListener<T> procedure) {
2751 r.request(this, procedure);
2755 public <T> T sync(ReadInterface<T> r) throws DatabaseException {
2756 return r.request(this);
2760 public <T> T sync(WriteInterface<T> r) throws DatabaseException {
2761 return r.request(this);
2765 public <T> void async(WriteInterface<T> r, Procedure<T> procedure) {
2766 r.request(this, procedure);
2770 public <T> void async(WriteInterface<T> r) {
2771 r.request(this, new ProcedureAdapter<T>());
2775 * Implementation of the interface AsyncReadGraph
2779 public void forURI(Resource resource, AsyncListener<String> listener) {
2780 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2785 public void forURI(Resource resource, SyncListener<String> listener) {
2786 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2791 public void forURI(Resource resource, Listener<String> listener) {
2792 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2797 final public void forURI(final Resource resource,
2798 final AsyncProcedure<String> procedure) {
2800 assert (resource != null);
2801 assert (procedure != null);
2803 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2809 public void forURI(Resource resource, SyncProcedure<String> procedure) {
2810 forURI(resource, new SyncToAsyncProcedure<String>(procedure));
2814 public void forURI(Resource resource, Procedure<String> procedure) {
2815 forURI(resource, new NoneToAsyncProcedure<String>(procedure));
2819 public void forResource(String id, AsyncListener<Resource> listener) {
2820 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2825 public void forResource(String id, SyncListener<Resource> listener) {
2826 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2831 public void forResource(String id, Listener<Resource> listener) {
2832 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2837 final public void forResource(final String id,
2838 final AsyncProcedure<Resource> procedure) {
2840 assert (id != null);
2841 assert (procedure != null);
2843 processor.forResource(this, id, procedure);
2848 public void forResource(String id, SyncProcedure<Resource> procedure) {
2849 forResource(id, new SyncToAsyncProcedure<Resource>(procedure));
2853 public void forResource(String id, Procedure<Resource> procedure) {
2854 forResource(id, new NoneToAsyncProcedure<Resource>(procedure));
2858 public void forBuiltin(String id, AsyncListener<Resource> listener) {
2859 asyncRequest(new Builtin(id), listener);
2863 public void forBuiltin(String id, SyncListener<Resource> listener) {
2864 asyncRequest(new Builtin(id), listener);
2868 public void forBuiltin(String id, Listener<Resource> listener) {
2869 asyncRequest(new Builtin(id), listener);
2873 final public void forBuiltin(final String id,
2874 final AsyncProcedure<Resource> procedure) {
2876 assert (id != null);
2877 assert (procedure != null);
2879 processor.forBuiltin(this, id, procedure);
2884 public void forBuiltin(String id, SyncProcedure<Resource> procedure) {
2885 forBuiltin(id, new SyncToAsyncProcedure<Resource>(procedure));
2889 public void forBuiltin(String id, Procedure<Resource> procedure) {
2890 forBuiltin(id, new NoneToAsyncProcedure<Resource>(procedure));
2894 final public void forEachStatement(Resource subject, Resource relation,
2895 AsyncMultiProcedure<Statement> procedure) {
2897 assert (subject != null);
2898 assert (relation != null);
2899 assert (procedure != null);
2901 processor.forEachStatement(this, subject, relation, procedure);
2906 public void forEachStatement(Resource subject, Resource relation,
2907 SyncMultiProcedure<Statement> procedure) {
2908 forEachStatement(subject, relation,
2909 new SyncToAsyncMultiProcedure<Statement>(procedure));
2913 final public void forEachStatement(Resource subject, Resource relation,
2914 MultiProcedure<Statement> procedure) {
2916 assert (subject != null);
2917 assert (relation != null);
2918 assert (procedure != null);
2920 processor.forEachStatement(this, subject, relation, procedure);
2925 final public void forStatementSet(Resource subject, Resource relation,
2926 AsyncSetListener<Statement> procedure) {
2928 assert (subject != null);
2929 assert (relation != null);
2930 assert (procedure != null);
2932 processor.forStatementSet(this, subject, relation, procedure);
2937 final public void forStatementSet(Resource subject, Resource relation,
2938 SyncSetListener<Statement> procedure) {
2939 forStatementSet(subject, relation,
2940 new SyncToAsyncSetProcedure<Statement>(procedure));
2944 public void forStatementSet(Resource subject, Resource relation,
2945 SetListener<Statement> listener) {
2946 forStatementSet(subject, relation,
2947 new NoneToAsyncSetProcedure<Statement>(listener));
2951 final public void forEachAssertedStatement(final Resource subject,
2952 final Resource relation,
2953 final AsyncMultiProcedure<Statement> procedure) {
2955 assert (subject != null);
2956 assert (relation != null);
2957 assert (procedure != null);
2959 processor.forEachAssertedStatement(this, subject, relation, procedure);
2964 public void forEachAssertedStatement(Resource subject, Resource relation,
2965 SyncMultiProcedure<Statement> procedure) {
2966 forEachAssertedStatement(subject, relation,
2967 new SyncToAsyncMultiProcedure<Statement>(procedure));
2971 public void forEachAssertedStatement(Resource subject, Resource relation,
2972 MultiProcedure<Statement> procedure) {
2973 forEachAssertedStatement(subject, relation,
2974 new NoneToAsyncMultiProcedure<Statement>(procedure));
2978 public void forAssertedStatementSet(Resource subject, Resource relation,
2979 AsyncSetListener<Statement> procedure) {
2981 assert (subject != null);
2982 assert (relation != null);
2983 assert (procedure != null);
2985 processor.forAssertedStatementSet(this, subject, relation, procedure);
2990 public void forAssertedStatementSet(Resource subject, Resource relation,
2991 SyncSetListener<Statement> procedure) {
2993 assert (subject != null);
2994 assert (relation != null);
2995 assert (procedure != null);
2997 forAssertedStatementSet(subject, relation,
2998 new SyncToAsyncSetProcedure<Statement>(procedure));
3003 public void forAssertedStatementSet(Resource subject, Resource relation,
3004 SetListener<Statement> procedure) {
3006 assert (subject != null);
3007 assert (relation != null);
3008 assert (procedure != null);
3010 forAssertedStatementSet(subject, relation,
3011 new NoneToAsyncSetProcedure<Statement>(procedure));
3016 final public void forEachPredicate(final Resource subject,
3017 final AsyncMultiProcedure<Resource> procedure) {
3019 assert (subject != null);
3020 assert (procedure != null);
3022 processor.forEachPredicate(this, subject, procedure);
3027 public void forEachPredicate(Resource subject,
3028 SyncMultiProcedure<Resource> procedure) {
3029 forEachPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(
3034 final public void forEachPredicate(final Resource subject,
3035 final MultiProcedure<Resource> procedure) {
3037 assert (subject != null);
3038 assert (procedure != null);
3040 processor.forEachPredicate(this, subject, procedure);
3045 final public void forPredicateSet(final Resource subject,
3046 final AsyncSetListener<Resource> procedure) {
3048 assert (subject != null);
3049 assert (procedure != null);
3051 processor.forPredicateSet(this, subject, procedure);
3056 final public void forPredicateSet(final Resource subject,
3057 final SyncSetListener<Resource> procedure) {
3059 assert (subject != null);
3060 assert (procedure != null);
3062 forPredicateSet(subject, new SyncToAsyncSetProcedure<Resource>(
3068 final public void forPredicateSet(final Resource subject,
3069 final SetListener<Resource> procedure) {
3071 assert (subject != null);
3072 assert (procedure != null);
3074 forPredicateSet(subject, new NoneToAsyncSetProcedure<Resource>(
3080 final public void forEachPrincipalType(final Resource subject,
3081 final AsyncMultiProcedure<Resource> procedure) {
3083 assert (subject != null);
3084 assert (procedure != null);
3086 processor.forEachPrincipalType(this, subject, procedure);
3091 public void forEachPrincipalType(Resource subject,
3092 SyncMultiProcedure<Resource> procedure) {
3093 forEachPrincipalType(subject, new SyncToAsyncMultiProcedure<Resource>(
3098 final public void forEachPrincipalType(final Resource subject,
3099 final MultiProcedure<Resource> procedure) {
3101 assert (subject != null);
3102 assert (procedure != null);
3104 processor.forEachPrincipalType(this, subject, procedure);
3109 final public void forPrincipalTypeSet(final Resource subject,
3110 final AsyncSetListener<Resource> procedure) {
3112 assert (subject != null);
3113 assert (procedure != null);
3115 processor.forPrincipalTypeSet(this, subject, procedure);
3120 final public void forPrincipalTypeSet(final Resource subject,
3121 final SyncSetListener<Resource> procedure) {
3123 assert (subject != null);
3124 assert (procedure != null);
3126 forPrincipalTypeSet(subject, new SyncToAsyncSetProcedure<Resource>(
3132 final public void forPrincipalTypeSet(final Resource subject,
3133 final SetListener<Resource> procedure) {
3135 assert (subject != null);
3136 assert (procedure != null);
3138 forPrincipalTypeSet(subject, new NoneToAsyncSetProcedure<Resource>(
3144 public void forTypes(Resource subject, AsyncListener<Set<Resource>> listener) {
3145 asyncRequest(new Types(subject), listener);
3149 public void forTypes(Resource subject, SyncListener<Set<Resource>> listener) {
3150 asyncRequest(new Types(subject), listener);
3154 public void forTypes(Resource subject, Listener<Set<Resource>> listener) {
3155 asyncRequest(new Types(subject), listener);
3159 final public void forTypes(final Resource subject,
3160 final AsyncProcedure<Set<Resource>> procedure) {
3162 assert (subject != null);
3163 assert (procedure != null);
3165 processor.forTypes(this, subject, procedure);
3170 public void forTypes(Resource subject,
3171 SyncProcedure<Set<Resource>> procedure) {
3172 forTypes(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
3176 public void forTypes(Resource subject, Procedure<Set<Resource>> procedure) {
3177 forTypes(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
3181 public void forSupertypes(Resource subject,
3182 AsyncListener<Set<Resource>> listener) {
3183 asyncRequest(new Types(subject), listener);
3187 public void forSupertypes(Resource subject,
3188 SyncListener<Set<Resource>> listener) {
3189 asyncRequest(new Types(subject), listener);
3193 public void forSupertypes(Resource subject, Listener<Set<Resource>> listener) {
3194 asyncRequest(new Types(subject), listener);
3198 final public void forSupertypes(final Resource subject,
3199 final AsyncProcedure<Set<Resource>> procedure) {
3201 assert (subject != null);
3202 assert (procedure != null);
3204 processor.forSupertypes(this, subject, procedure);
3209 public void forSupertypes(Resource subject,
3210 SyncProcedure<Set<Resource>> procedure) {
3211 forSupertypes(subject, new SyncToAsyncProcedure<Set<Resource>>(
3216 public void forSupertypes(Resource subject,
3217 Procedure<Set<Resource>> procedure) {
3218 forSupertypes(subject, new NoneToAsyncProcedure<Set<Resource>>(
3223 public void forDirectSuperrelations(Resource subject,
3224 AsyncMultiProcedure<Resource> procedure) {
3226 assert (subject != null);
3227 assert (procedure != null);
3229 processor.forDirectSuperrelations(this, subject, procedure);
3234 public void forPossibleSuperrelation(Resource subject, AsyncProcedure<Resource> procedure) {
3236 assert (subject != null);
3237 assert (procedure != null);
3239 processor.forPossibleSuperrelation(this, subject, procedure);
3244 public void forSuperrelations(Resource subject,
3245 AsyncListener<Set<Resource>> listener) {
3246 asyncRequest(new Types(subject), listener);
3250 public void forSuperrelations(Resource subject,
3251 SyncListener<Set<Resource>> listener) {
3252 asyncRequest(new Types(subject), listener);
3256 public void forSuperrelations(Resource subject,
3257 Listener<Set<Resource>> listener) {
3258 asyncRequest(new Types(subject), listener);
3262 final public void forSuperrelations(final Resource subject,
3263 final AsyncProcedure<Set<Resource>> procedure) {
3265 assert (subject != null);
3266 assert (procedure != null);
3268 processor.forSuperrelations(this, subject, procedure);
3273 public void forSuperrelations(Resource subject,
3274 SyncProcedure<Set<Resource>> procedure) {
3275 forSuperrelations(subject, new SyncToAsyncProcedure<Set<Resource>>(
3280 public void forSuperrelations(Resource subject,
3281 Procedure<Set<Resource>> procedure) {
3282 forSuperrelations(subject, new NoneToAsyncProcedure<Set<Resource>>(
3287 final public void forEachObject(final Resource subject, final Resource relation, final AsyncMultiProcedure<Resource> procedure) {
3288 processor.forEachObject(this, subject, relation, procedure);
3292 public void forEachObject(Resource subject, Resource relation,
3293 SyncMultiProcedure<Resource> procedure) {
3294 forEachObject(subject, relation,
3295 new SyncToAsyncMultiProcedure<Resource>(procedure));
3299 public void forEachObject(Resource subject, Resource relation,
3300 MultiProcedure<Resource> procedure) {
3302 processor.forEachObject(this, subject, relation, procedure);
3307 // final public void forEachDirectObject(final Resource subject,
3308 // final Resource relation,
3309 // final AsyncMultiProcedure<Resource> procedure) {
3311 // processor.forEachDirectObject(this, subject, relation, procedure);
3316 // public void forEachDirectObject(Resource subject, Resource relation,
3317 // SyncMultiProcedure<Resource> procedure) {
3318 // forEachDirectObject(subject, relation,
3319 // new SyncToAsyncMultiProcedure<Resource>(procedure));
3323 // public void forEachDirectObject(Resource subject, Resource relation,
3324 // MultiProcedure<Resource> procedure) {
3325 // forEachDirectObject(subject, relation,
3326 // new NoneToAsyncMultiProcedure<Resource>(procedure));
3330 final public void forEachDirectPredicate(final Resource subject, final AsyncMultiProcedure<Resource> procedure) {
3331 processor.forEachDirectPredicate(this, subject, procedure);
3335 public void forEachDirectPredicate(Resource subject, SyncMultiProcedure<Resource> procedure) {
3336 forEachDirectPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(procedure));
3340 public void forEachDirectPredicate(Resource subject, MultiProcedure<Resource> procedure) {
3341 forEachDirectPredicate(subject, new NoneToAsyncMultiProcedure<Resource>(procedure));
3345 final public void forObjectSet(final Resource subject,
3346 final Resource relation, final AsyncSetListener<Resource> procedure) {
3348 assert (subject != null);
3349 assert (relation != null);
3350 assert (procedure != null);
3352 processor.forObjectSet(this, subject, relation, procedure);
3357 final public void forObjectSet(final Resource subject,
3358 final Resource relation, final SyncSetListener<Resource> procedure) {
3360 assert (subject != null);
3361 assert (relation != null);
3362 assert (procedure != null);
3364 forObjectSet(subject, relation, new SyncToAsyncSetProcedure<Resource>(
3370 final public void forObjectSet(final Resource subject,
3371 final Resource relation, final SetListener<Resource> procedure) {
3373 assert (subject != null);
3374 assert (relation != null);
3375 assert (procedure != null);
3377 forObjectSet(subject, relation, new NoneToAsyncSetProcedure<Resource>(
3383 final public void forEachAssertedObject(final Resource subject,
3384 final Resource relation,
3385 final AsyncMultiProcedure<Resource> procedure) {
3387 assert (subject != null);
3388 assert (relation != null);
3389 assert (procedure != null);
3391 processor.forEachAssertedObject(this, subject, relation, procedure);
3396 public void forEachAssertedObject(Resource subject, Resource relation,
3397 SyncMultiProcedure<Resource> procedure) {
3399 assert (subject != null);
3400 assert (relation != null);
3401 assert (procedure != null);
3403 forEachAssertedObject(subject, relation,
3404 new SyncToAsyncMultiProcedure<Resource>(procedure));
3409 public void forEachAssertedObject(Resource subject, Resource relation,
3410 MultiProcedure<Resource> procedure) {
3412 assert (subject != null);
3413 assert (relation != null);
3414 assert (procedure != null);
3416 forEachAssertedObject(subject, relation,
3417 new NoneToAsyncMultiProcedure<Resource>(procedure));
3422 public void forAssertedObjectSet(Resource subject, Resource relation,
3423 AsyncSetListener<Resource> procedure) {
3425 assert (subject != null);
3426 assert (relation != null);
3427 assert (procedure != null);
3429 processor.forAssertedObjectSet(this, subject, relation, procedure);
3434 public void forAssertedObjectSet(Resource subject, Resource relation,
3435 SyncSetListener<Resource> procedure) {
3437 assert (subject != null);
3438 assert (relation != null);
3439 assert (procedure != null);
3441 forAssertedObjectSet(subject, relation,
3442 new SyncToAsyncSetProcedure<Resource>(procedure));
3447 public void forAssertedObjectSet(Resource subject, Resource relation,
3448 SetListener<Resource> procedure) {
3450 assert (subject != null);
3451 assert (relation != null);
3452 assert (procedure != null);
3454 forAssertedObjectSet(subject, relation,
3455 new NoneToAsyncSetProcedure<Resource>(procedure));
3460 public void forInverse(Resource relation, AsyncListener<Resource> listener) {
3461 asyncRequest(new Inverse(relation), listener);
3465 public void forInverse(Resource relation, SyncListener<Resource> listener) {
3466 asyncRequest(new Inverse(relation), listener);
3470 public void forInverse(Resource relation, Listener<Resource> listener) {
3471 asyncRequest(new Inverse(relation), listener);
3475 final public void forInverse(final Resource relation,
3476 final AsyncProcedure<Resource> procedure) {
3478 assert (relation != null);
3479 assert (procedure != null);
3481 processor.forInverse(this, relation, new AsyncProcedure<Resource>() {
3484 public void execute(AsyncReadGraph graph, Resource result) {
3486 procedure.execute(graph, result);
3488 procedure.exception(graph, new NoInverseException(relation
3494 public void exception(AsyncReadGraph graph, Throwable throwable) {
3495 procedure.exception(graph, throwable);
3499 public String toString() {
3500 return "forInverse -> " + procedure;
3508 public void forInverse(Resource relation, SyncProcedure<Resource> procedure) {
3509 forInverse(relation, new SyncToAsyncProcedure<Resource>(procedure));
3513 public void forInverse(Resource relation, Procedure<Resource> procedure) {
3514 forInverse(relation, new NoneToAsyncProcedure<Resource>(procedure));
3518 public void forSingleObject(Resource subject, Resource relation,
3519 AsyncListener<Resource> listener) {
3520 asyncRequest(new SingleObject(subject, relation), listener);
3524 public void forSingleObject(Resource subject, Resource relation,
3525 SyncListener<Resource> listener) {
3526 asyncRequest(new SingleObject(subject, relation), listener);
3530 public void forSingleObject(Resource subject, Resource relation,
3531 Listener<Resource> listener) {
3532 asyncRequest(new SingleObject(subject, relation), listener);
3536 final public void forSingleObject(final Resource subject,
3537 final Resource relation, final AsyncProcedure<Resource> procedure) {
3539 assert (subject != null);
3540 assert (relation != null);
3541 assert (procedure != null);
3543 processor.forEachObject(this, subject, relation,
3544 new SingleOrErrorProcedure<Resource>(procedure));
3549 public void forSingleObject(Resource subject, Resource relation,
3550 SyncProcedure<Resource> procedure) {
3551 forSingleObject(subject, relation, new SyncToAsyncProcedure<Resource>(
3556 public void forSingleObject(Resource subject, Resource relation,
3557 Procedure<Resource> procedure) {
3558 forSingleObject(subject, relation, new NoneToAsyncProcedure<Resource>(
3563 public void forSingleStatement(Resource subject, Resource relation,
3564 AsyncListener<Statement> listener) {
3565 asyncRequest(new SingleStatement(subject, relation), listener);
3569 public void forSingleStatement(Resource subject, Resource relation,
3570 SyncListener<Statement> listener) {
3571 asyncRequest(new SingleStatement(subject, relation), listener);
3575 public void forSingleStatement(Resource subject, Resource relation,
3576 Listener<Statement> listener) {
3577 asyncRequest(new SingleStatement(subject, relation), listener);
3581 final public void forSingleStatement(final Resource subject,
3582 final Resource relation, final AsyncProcedure<Statement> procedure) {
3584 assert (subject != null);
3585 assert (relation != null);
3586 assert (procedure != null);
3588 processor.forEachStatement(this, subject, relation,
3589 new SingleOrErrorProcedure<Statement>(procedure));
3594 public void forSingleStatement(Resource subject, Resource relation,
3595 SyncProcedure<Statement> procedure) {
3596 forSingleStatement(subject, relation,
3597 new SyncToAsyncProcedure<Statement>(procedure));
3601 public void forSingleStatement(Resource subject, Resource relation,
3602 Procedure<Statement> procedure) {
3603 forSingleStatement(subject, relation,
3604 new NoneToAsyncProcedure<Statement>(procedure));
3608 public void forSingleType(Resource subject,
3609 AsyncListener<Resource> listener) {
3610 asyncRequest(new SingleTypeAny(subject), listener);
3614 public void forSingleType(Resource subject,
3615 SyncListener<Resource> listener) {
3616 asyncRequest(new SingleTypeAny(subject), listener);
3620 public void forSingleType(Resource subject,
3621 Listener<Resource> listener) {
3622 asyncRequest(new SingleTypeAny(subject), listener);
3626 final public void forSingleType(final Resource subject, final AsyncProcedure<Resource> procedure) {
3628 assert (subject != null);
3629 assert (procedure != null);
3631 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3633 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
3636 public void execute(AsyncReadGraph graph, final Resource principalType) {
3637 checkedProcedure.offer(graph, principalType);
3641 public void finished(AsyncReadGraph graph) {
3642 checkedProcedure.dec(graph);
3646 public void exception(AsyncReadGraph graph, Throwable t) {
3647 checkedProcedure.exception(graph, t);
3651 public String toString() {
3652 return "forSingleType -> " + procedure;
3660 public void forSingleType(Resource subject, SyncProcedure<Resource> procedure) {
3661 forSingleType(subject, new SyncToAsyncProcedure<Resource>(
3666 public void forSingleType(Resource subject, Procedure<Resource> procedure) {
3667 forSingleType(subject, new NoneToAsyncProcedure<Resource>(
3672 public void forSingleType(Resource subject, Resource relation,
3673 AsyncListener<Resource> listener) {
3674 asyncRequest(new SingleType(subject, relation), listener);
3678 public void forSingleType(Resource subject, Resource relation,
3679 SyncListener<Resource> listener) {
3680 asyncRequest(new SingleType(subject, relation), listener);
3684 public void forSingleType(Resource subject, Resource relation,
3685 Listener<Resource> listener) {
3686 asyncRequest(new SingleType(subject, relation), listener);
3690 final public void forSingleType(final Resource subject,
3691 final Resource baseType, final AsyncProcedure<Resource> procedure) {
3693 assert (subject != null);
3694 assert (procedure != null);
3696 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3698 processor.forEachPrincipalType(this, subject,
3699 new AsyncMultiProcedureAdapter<Resource>() {
3702 public void execute(AsyncReadGraph graph,
3703 final Resource principalType) {
3705 checkedProcedure.inc();
3707 if(baseType == null) {
3709 checkedProcedure.offer(graph, principalType);
3710 checkedProcedure.dec(graph);
3712 } else if(principalType.equals(baseType)) {
3714 checkedProcedure.offer(graph, principalType);
3715 checkedProcedure.dec(graph);
3719 processor.forSupertypes((ReadGraphImpl)graph, principalType,
3720 new AsyncProcedure<Set<Resource>>() {
3723 public void execute(
3724 AsyncReadGraph graph,
3725 Set<Resource> result) {
3727 if (result.contains(baseType))
3728 checkedProcedure.offer(graph,
3730 checkedProcedure.dec(graph);
3735 public void exception(
3736 AsyncReadGraph graph,
3739 .exception(graph, t);
3749 public void finished(AsyncReadGraph graph) {
3750 checkedProcedure.dec(graph);
3754 public void exception(AsyncReadGraph graph, Throwable t) {
3755 checkedProcedure.exception(graph, t);
3759 public String toString() {
3760 return "forSingleType -> " + procedure;
3768 public void forSingleType(Resource subject, Resource relation,
3769 SyncProcedure<Resource> procedure) {
3770 forSingleType(subject, relation, new SyncToAsyncProcedure<Resource>(
3775 public void forSingleType(Resource subject, Resource relation,
3776 Procedure<Resource> procedure) {
3777 forSingleType(subject, relation, new NoneToAsyncProcedure<Resource>(
3782 public <T> void forValue(Resource subject, Binding binding,
3783 AsyncListener<T> listener) {
3784 asyncRequest(new Value<T>(subject, binding), listener);
3788 public <T> void forValue(Resource subject, Binding binding,
3789 SyncListener<T> listener) {
3790 asyncRequest(new Value<T>(subject, binding), listener);
3794 public <T> void forValue(Resource subject, Binding binding,
3795 Listener<T> listener) {
3796 asyncRequest(new Value<T>(subject, binding), listener);
3800 public <T> void forValue(final Resource resource, final Binding binding,
3801 final AsyncProcedure<T> procedure) {
3803 assert (resource != null);
3804 assert (binding != null);
3805 assert (procedure != null);
3807 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
3810 public void execute(AsyncReadGraph graph, byte[] result) {
3814 if (result == null) {
3815 procedure.exception(graph,
3816 new DoesNotContainValueException(
3817 "No value for resource " + resource));
3821 Serializer serializer = binding.serializer();
3822 // Serializer serializer = Bindings.getSerializer( binding );
3823 Object obj = serializer.deserialize(result);
3824 // if (!binding.isInstance(obj))
3825 // procedure.exception(graph, new ClassCastException(
3826 // "Cannot get value " + obj + " with binding "
3829 procedure.execute(graph, (T) obj);
3831 } catch (IOException e) {
3832 procedure.exception(graph, e);
3833 } catch (BufferUnderflowException e) {
3834 procedure.exception(graph, e);
3835 } catch (Throwable t) {
3836 procedure.exception(graph, t);
3842 public void exception(AsyncReadGraph graph, Throwable t) {
3844 procedure.exception(graph, t);
3845 } catch (Throwable t2) {
3846 Logger.defaultLogError(t2);
3851 public String toString() {
3852 return "forValue -> " + procedure;
3860 public <T> void forValue(Resource subject, Binding binding,
3861 SyncProcedure<T> procedure) {
3862 forValue(subject, binding, new SyncToAsyncProcedure<T>(procedure));
3866 public <T> void forValue(Resource subject, Binding binding,
3867 Procedure<T> procedure) {
3868 forValue(subject, binding, new NoneToAsyncProcedure<T>(procedure));
3872 public <T> void forValue(Resource subject, AsyncListener<T> listener) {
3873 asyncRequest(new ValueImplied<T>(subject), listener);
3877 public <T> void forValue(Resource subject, SyncListener<T> listener) {
3878 asyncRequest(new ValueImplied<T>(subject), listener);
3882 public <T> void forValue(Resource subject, Listener<T> listener) {
3883 asyncRequest(new ValueImplied<T>(subject), listener);
3887 final public <T> void forValue(final Resource subject, final AsyncProcedure<T> procedure) {
3889 assert (subject != null);
3890 assert (procedure != null);
3892 forRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
3895 public void execute(AsyncReadGraph graph, Datatype type) {
3896 // TODO: consider trying Bindings.getBeanBinding(type);
3897 Binding binding = Bindings.getBinding(type);
3898 graph.forValue(subject, binding, procedure);
3902 public void exception(AsyncReadGraph graph, Throwable throwable) {
3903 procedure.exception(graph, new DoesNotContainValueException("Invalid data type", throwable));
3911 public <T> void forValue(Resource subject, SyncProcedure<T> procedure) {
3912 forValue(subject, new SyncToAsyncProcedure<T>(procedure));
3916 public <T> void forValue(Resource subject, Procedure<T> procedure) {
3917 forValue(subject, new NoneToAsyncProcedure<T>(procedure));
3921 public <T> void forRelatedValue(Resource subject, Resource relation,
3922 AsyncListener<T> listener) {
3923 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3927 public <T> void forRelatedValue(Resource subject, Resource relation,
3928 SyncListener<T> listener) {
3929 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3933 public <T> void forRelatedValue(Resource subject, Resource relation,
3934 Listener<T> listener) {
3935 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3939 final public <T> void forRelatedValue(final Resource subject,
3940 final Resource relation, final AsyncProcedure<T> procedure) {
3942 assert (subject != null);
3943 assert (relation != null);
3944 assert (procedure != null);
3946 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3948 processor.forEachObject(this, subject, relation,
3949 new AsyncMultiProcedureAdapter<Resource>() {
3952 public void execute(AsyncReadGraph graph,
3953 final Resource object) {
3955 checkedProcedure.inc();
3957 graph.forValue(object, new AsyncProcedure<Object>() {
3960 public void execute(AsyncReadGraph graph,
3962 checkedProcedure.offer(graph, (T) result);
3963 checkedProcedure.dec(graph);
3967 public void exception(AsyncReadGraph graph,
3969 checkedProcedure.exception(graph, t);
3973 public String toString() {
3974 return "forRelatedValue -> " + procedure;
3982 public void finished(AsyncReadGraph graph) {
3983 checkedProcedure.dec(graph);
3987 public void exception(AsyncReadGraph graph, Throwable t) {
3988 checkedProcedure.exception(graph, t);
3996 public <T> void forRelatedValue(Resource subject, Resource relation,
3997 SyncProcedure<T> procedure) {
3998 forRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
4003 public <T> void forRelatedValue(Resource subject, Resource relation,
4004 Procedure<T> procedure) {
4005 forRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
4010 public <T> void forRelatedValue(Resource subject, Resource relation,
4011 Binding binding, AsyncListener<T> listener) {
4012 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
4016 public <T> void forRelatedValue(Resource subject, Resource relation,
4017 Binding binding, SyncListener<T> listener) {
4018 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
4022 public <T> void forRelatedValue(Resource subject, Resource relation,
4023 Binding binding, Listener<T> listener) {
4024 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
4028 final public <T> void forRelatedValue(final Resource subject,
4029 final Resource relation, final Binding binding,
4030 final AsyncProcedure<T> procedure) {
4032 assert (subject != null);
4033 assert (relation != null);
4034 assert (binding != null);
4035 assert (procedure != null);
4037 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
4039 processor.forEachObject(this, subject, relation,
4040 new AsyncMultiProcedureAdapter<Resource>() {
4043 public void execute(AsyncReadGraph graph,
4044 final Resource object) {
4046 checkedProcedure.inc();
4048 graph.forValue(object, binding, new AsyncProcedure<Object>() {
4051 public void execute(AsyncReadGraph graph,
4054 checkedProcedure.offer(graph,
4056 checkedProcedure.dec(graph);
4060 public void exception(AsyncReadGraph graph,
4062 checkedProcedure.exception(graph, t);
4066 public String toString() {
4067 return "forRelatedValue -> "
4076 public void finished(AsyncReadGraph graph) {
4077 checkedProcedure.dec(graph);
4081 public void exception(AsyncReadGraph graph, Throwable t) {
4082 checkedProcedure.exception(graph, t);
4090 public <T> void forRelatedValue(Resource subject, Resource relation,
4091 Binding binding, SyncProcedure<T> procedure) {
4092 forRelatedValue(subject, relation, binding,
4093 new SyncToAsyncProcedure<T>(procedure));
4097 public <T> void forRelatedValue(Resource subject, Resource relation,
4098 Binding binding, Procedure<T> procedure) {
4099 forRelatedValue(subject, relation, binding,
4100 new NoneToAsyncProcedure<T>(procedure));
4104 public <T> void forAdapted(Resource resource, Class<T> clazz,
4105 AsyncListener<T> listener) {
4106 asyncRequest(new Adapter<T>(resource, clazz), listener);
4110 public <T> void forAdapted(Resource resource, Class<T> clazz,
4111 SyncListener<T> listener) {
4112 asyncRequest(new Adapter<T>(resource, clazz), listener);
4116 public <T> void forAdapted(Resource resource, Class<T> clazz,
4117 Listener<T> listener) {
4118 asyncRequest(new Adapter<T>(resource, clazz), listener);
4122 final public <T> void forAdapted(final Resource resource,
4123 final Class<T> clazz, final AsyncProcedure<T> procedure) {
4125 assert (resource != null);
4126 assert (clazz != null);
4127 assert (procedure != null);
4129 final AdaptionService service = getSession().peekService(AdaptionService.class);
4130 if (service == null)
4131 procedure.exception(this, new ServiceException("No AdaptionService available"));
4133 service.adapt(this, resource, resource, Resource.class, clazz, false, procedure);
4138 public <T> void forAdapted(Resource resource, Class<T> clazz,
4139 SyncProcedure<T> procedure) {
4140 forAdapted(resource, clazz, new SyncToAsyncProcedure<T>(procedure));
4144 public <T> void forAdapted(Resource resource, Class<T> clazz,
4145 Procedure<T> procedure) {
4146 forAdapted(resource, clazz, new NoneToAsyncProcedure<T>(procedure));
4150 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
4151 AsyncListener<T> listener) {
4152 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
4156 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
4157 SyncListener<T> listener) {
4158 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
4162 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
4163 Listener<T> listener) {
4164 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
4168 final public <T> void forUniqueAdapted(final Resource resource,
4169 final Class<T> clazz, final AsyncProcedure<T> procedure) {
4171 assert (resource != null);
4172 assert (clazz != null);
4173 assert (procedure != null);
4175 final AdaptionService service = getSession().peekService(AdaptionService.class);
4176 if (service == null)
4177 procedure.exception(this, new ServiceException("No AdaptionService available"));
4179 service.adaptNew(this, resource, clazz, false, procedure);
4184 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
4185 SyncProcedure<T> procedure) {
4186 forUniqueAdapted(resource, clazz,
4187 new SyncToAsyncProcedure<T>(procedure));
4191 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
4192 Procedure<T> procedure) {
4193 forUniqueAdapted(resource, clazz,
4194 new NoneToAsyncProcedure<T>(procedure));
4198 public void forPossibleInverse(Resource subject,
4199 AsyncListener<Resource> listener) {
4200 asyncRequest(new PossibleInverse(subject), listener);
4204 public void forPossibleInverse(Resource subject,
4205 SyncListener<Resource> listener) {
4206 asyncRequest(new PossibleInverse(subject), listener);
4210 public void forPossibleInverse(Resource subject, Listener<Resource> listener) {
4211 asyncRequest(new PossibleInverse(subject), listener);
4215 final public void forPossibleInverse(final Resource relation,
4216 final AsyncProcedure<Resource> procedure) {
4218 assert (relation != null);
4219 assert (procedure != null);
4221 processor.forInverse(this, relation, new ExceptionToNullProcedure<Resource>(procedure));
4226 public void forPossibleInverse(Resource subject,
4227 SyncProcedure<Resource> procedure) {
4228 forPossibleInverse(subject, new SyncToAsyncProcedure<Resource>(
4233 public void forPossibleInverse(Resource subject,
4234 Procedure<Resource> procedure) {
4235 forPossibleInverse(subject, new NoneToAsyncProcedure<Resource>(
4240 public void forPossibleObject(Resource subject, Resource relation,
4241 AsyncListener<Resource> listener) {
4242 asyncRequest(new PossibleObject(subject, relation), listener);
4246 public void forPossibleObject(Resource subject, Resource relation,
4247 SyncListener<Resource> listener) {
4248 asyncRequest(new PossibleObject(subject, relation), listener);
4252 public void forPossibleObject(Resource subject, Resource relation,
4253 Listener<Resource> listener) {
4254 asyncRequest(new PossibleObject(subject, relation), listener);
4258 final public void forPossibleObject(final Resource subject,
4259 final Resource relation, final AsyncProcedure<Resource> procedure) {
4261 assert (subject != null);
4262 assert (relation != null);
4263 assert (procedure != null);
4265 processor.forEachObject(this, subject, relation,
4266 new SingleOrNullProcedure<Resource>(procedure));
4271 public void forPossibleObject(Resource subject, Resource relation,
4272 SyncProcedure<Resource> procedure) {
4273 forPossibleObject(subject, relation,
4274 new SyncToAsyncProcedure<Resource>(procedure));
4278 public void forPossibleObject(Resource subject, Resource relation,
4279 Procedure<Resource> procedure) {
4280 forPossibleObject(subject, relation,
4281 new NoneToAsyncProcedure<Resource>(procedure));
4285 public void forPossibleStatement(Resource subject, Resource relation,
4286 AsyncListener<Statement> listener) {
4287 asyncRequest(new PossibleStatement(subject, relation), listener);
4291 public void forPossibleStatement(Resource subject, Resource relation,
4292 SyncListener<Statement> listener) {
4293 asyncRequest(new PossibleStatement(subject, relation), listener);
4297 public void forPossibleStatement(Resource subject, Resource relation,
4298 Listener<Statement> listener) {
4299 asyncRequest(new PossibleStatement(subject, relation), listener);
4303 final public void forPossibleStatement(final Resource subject,
4304 final Resource relation, final AsyncProcedure<Statement> procedure) {
4306 assert (subject != null);
4307 assert (relation != null);
4308 assert (procedure != null);
4310 processor.forEachStatement(this, subject, relation,
4311 new SingleFunctionalOrNullProcedure<Statement>(
4312 "forPossibleStatement", procedure));
4317 public void forPossibleStatement(Resource subject, Resource relation,
4318 SyncProcedure<Statement> procedure) {
4319 forPossibleStatement(subject, relation,
4320 new SyncToAsyncProcedure<Statement>(procedure));
4324 public void forPossibleStatement(Resource subject, Resource relation,
4325 Procedure<Statement> procedure) {
4326 forPossibleStatement(subject, relation,
4327 new NoneToAsyncProcedure<Statement>(procedure));
4331 public void forPossibleType(Resource subject, Resource relation,
4332 AsyncListener<Resource> listener) {
4333 asyncRequest(new PossibleType(subject, relation), listener);
4337 public void forPossibleType(Resource subject, Resource relation,
4338 SyncListener<Resource> listener) {
4339 asyncRequest(new PossibleType(subject, relation), listener);
4343 public void forPossibleType(Resource subject, Resource relation,
4344 Listener<Resource> listener) {
4345 asyncRequest(new PossibleType(subject, relation), listener);
4349 final public void forPossibleType(final Resource subject,
4350 final Resource baseType, final AsyncProcedure<Resource> procedure) {
4352 assert (subject != null);
4353 assert (procedure != null);
4355 final NullSingleOrNullProcedure<Resource> checkedProcedure = new NullSingleOrNullProcedure<Resource>(procedure);
4357 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
4360 public void execute(AsyncReadGraph graph,
4361 final Resource principalType) {
4363 if (baseType == null) {
4365 checkedProcedure.offer(graph, principalType);
4367 } else if (principalType.equals(baseType)) {
4369 checkedProcedure.offer(graph, principalType);
4373 checkedProcedure.inc();
4375 processor.forSupertypes((ReadGraphImpl)graph, principalType,
4376 new AsyncProcedure<Set<Resource>>() {
4379 public void execute(
4380 AsyncReadGraph graph,
4381 Set<Resource> result) {
4383 if (result.contains(baseType)) {
4384 checkedProcedure.offer(graph,
4388 checkedProcedure.dec(graph);
4393 public void exception(
4394 AsyncReadGraph graph,
4396 checkedProcedure.exception(graph, t);
4397 checkedProcedure.dec(graph);
4401 public String toString() {
4402 return "forPossibleType -> "
4413 public void finished(AsyncReadGraph graph) {
4414 checkedProcedure.dec(graph);
4418 public void exception(AsyncReadGraph graph, Throwable t) {
4419 checkedProcedure.exception(graph, t);
4420 checkedProcedure.dec(graph);
4428 public void forPossibleType(Resource subject, Resource relation,
4429 SyncProcedure<Resource> procedure) {
4430 forPossibleType(subject, relation, new SyncToAsyncProcedure<Resource>(
4435 public void forPossibleType(Resource subject, Resource relation,
4436 Procedure<Resource> procedure) {
4437 forPossibleType(subject, relation, new NoneToAsyncProcedure<Resource>(
4442 public <T> void forPossibleValue(Resource subject, AsyncListener<T> listener) {
4443 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4447 public <T> void forPossibleValue(Resource subject, SyncListener<T> listener) {
4448 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4452 public <T> void forPossibleValue(Resource subject, Listener<T> listener) {
4453 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4457 final public <T> void forPossibleValue(final Resource subject,
4458 final AsyncProcedure<T> procedure) {
4460 assert (subject != null);
4461 assert (procedure != null);
4463 forPossibleRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
4466 public void execute(AsyncReadGraph graph, final Datatype type) {
4468 procedure.execute(graph, null);
4471 // TODO: consider trying Bindings.getBeanBinding(type);
4472 Binding binding = Bindings.getBinding(type);
4473 graph.forPossibleValue(subject, binding, procedure);
4474 } catch (RuntimeBindingConstructionException e) {
4475 procedure.exception(graph, e);
4481 public void exception(AsyncReadGraph graph, Throwable t) {
4482 procedure.exception(graph, t);
4486 public String toString() {
4487 return "forPossibleValue -> " + procedure;
4495 public <T> void forPossibleValue(Resource subject,
4496 SyncProcedure<T> procedure) {
4497 forPossibleValue(subject, new SyncToAsyncProcedure<T>(procedure));
4501 public <T> void forPossibleValue(Resource subject, Procedure<T> procedure) {
4502 forPossibleValue(subject, new NoneToAsyncProcedure<T>(procedure));
4506 public <T> void forPossibleValue(Resource subject, Binding binding,
4507 AsyncListener<T> listener) {
4508 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4512 public <T> void forPossibleValue(Resource subject, Binding binding,
4513 SyncListener<T> listener) {
4514 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4518 public <T> void forPossibleValue(Resource subject, Binding binding,
4519 Listener<T> listener) {
4520 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4524 final public <T> void forPossibleValue(final Resource resource,
4525 final Binding binding, final AsyncProcedure<T> procedure) {
4527 assert (resource != null);
4528 assert (binding != null);
4529 assert (procedure != null);
4531 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
4534 public void execute(AsyncReadGraph graph, byte[] result) {
4538 if (result == null) {
4539 procedure.execute(graph, null);
4543 Serializer serializer = Bindings.getSerializer( binding );
4544 Object obj = serializer.deserialize(result);
4545 if (!binding.isInstance(obj))
4546 procedure.exception(graph, new ClassCastException(
4547 "Cannot get value " + obj + " with binding "
4550 procedure.execute(graph, (T) obj);
4552 } catch (IOException e) {
4553 procedure.exception(graph, e);
4554 } catch (BufferUnderflowException e) {
4555 procedure.exception(graph, e);
4556 } catch (Throwable t) {
4557 procedure.exception(graph, t);
4563 public void exception(AsyncReadGraph graph, Throwable t) {
4565 procedure.exception(graph, t);
4566 } catch (Throwable t2) {
4567 Logger.defaultLogError(t2);
4572 public String toString() {
4573 return "forPossibleValue -> " + procedure;
4581 public <T> void forPossibleValue(Resource subject, Binding binding,
4582 SyncProcedure<T> procedure) {
4583 forPossibleValue(subject, binding, new SyncToAsyncProcedure<T>(
4588 public <T> void forPossibleValue(Resource subject, Binding binding,
4589 Procedure<T> procedure) {
4590 forPossibleValue(subject, binding, new NoneToAsyncProcedure<T>(
4595 public <T> void forPossibleRelatedValue(Resource subject,
4596 Resource relation, AsyncListener<T> listener) {
4597 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4602 public <T> void forPossibleRelatedValue(Resource subject,
4603 Resource relation, SyncListener<T> listener) {
4604 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4609 public <T> void forPossibleRelatedValue(Resource subject,
4610 Resource relation, Listener<T> listener) {
4611 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4616 final public <T> void forPossibleRelatedValue(final Resource subject,
4617 final Resource relation, final AsyncProcedure<T> procedure) {
4619 assert (subject != null);
4620 assert (relation != null);
4621 assert (procedure != null);
4623 final DeepSingleOrNullProcedure<T> checkedProcedure = new DeepSingleOrNullProcedure<T>(procedure);
4625 processor.forEachObject(this, subject, relation,
4626 new AsyncMultiProcedureAdapter<Resource>() {
4629 public void execute(AsyncReadGraph graph,
4630 final Resource object) {
4632 checkedProcedure.inc();
4634 graph.forValue(object, new AsyncProcedure<Object>() {
4637 public void execute(AsyncReadGraph graph,
4639 checkedProcedure.offer(graph, (T) result);
4640 checkedProcedure.dec(graph);
4644 public void exception(AsyncReadGraph graph,
4646 checkedProcedure.exception(graph, t);
4647 checkedProcedure.dec(graph);
4655 public void finished(AsyncReadGraph graph) {
4657 checkedProcedure.dec(graph);
4661 public void exception(AsyncReadGraph graph, Throwable t) {
4662 checkedProcedure.exception(graph, t);
4663 checkedProcedure.dec(graph);
4667 public String toString() {
4668 return "forPossibleRelatedValue -> " + procedure;
4675 public <T> void forPossibleRelatedValue(Resource subject,
4676 Resource relation, SyncProcedure<T> procedure) {
4677 forPossibleRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
4682 public <T> void forPossibleRelatedValue(Resource subject,
4683 Resource relation, Procedure<T> procedure) {
4684 forPossibleRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
4689 public <T> void forPossibleRelatedValue(Resource subject,
4690 Resource relation, Binding binding, AsyncListener<T> listener) {
4691 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4696 public <T> void forPossibleRelatedValue(Resource subject,
4697 Resource relation, Binding binding, SyncListener<T> listener) {
4698 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4703 public <T> void forPossibleRelatedValue(Resource subject,
4704 Resource relation, Binding binding, Listener<T> listener) {
4705 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4710 // final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4711 // final AsyncProcedure<T> procedure) {
4713 // forPossibleRelatedValue(subject, relation, binding, procedure, false);
4717 final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4718 final AsyncProcedure<T> procedure) {
4720 assert (subject != null);
4721 assert (relation != null);
4722 assert (procedure != null);
4724 processor.forPossibleObject(this, subject, relation, new AsyncProcedure<Resource>() {
4727 public void execute(AsyncReadGraph graph, Resource object) {
4729 if(object == null) {
4730 procedure.execute(graph, null);
4734 processor.forPossibleValue((ReadGraphImpl)graph, object, new AsyncProcedure<byte[]>() {
4737 public void execute(AsyncReadGraph graph, byte[] bytes) {
4743 Serializer serializer = binding.serializer();
4744 Object obj = serializer.deserialize(bytes);
4745 if (!binding.isInstance(obj)) {
4746 procedure.exception(graph, new ClassCastException("Cannot get value " + obj + " with binding " + binding));
4748 procedure.execute(graph, (T) obj);
4751 } catch (Throwable t) {
4753 procedure.exception(graph, t);
4759 procedure.execute(graph, null);
4766 public void exception(AsyncReadGraph graph, Throwable t) {
4767 procedure.exception(graph, t);
4775 public void exception(AsyncReadGraph graph, Throwable throwable) {
4776 throwable.printStackTrace();
4777 procedure.exception(graph, throwable);
4785 public <T> void forPossibleRelatedValue(Resource subject,
4786 Resource relation, Binding binding, SyncProcedure<T> procedure) {
4787 forPossibleRelatedValue(subject, relation, binding,
4788 new SyncToAsyncProcedure<T>(procedure));
4792 public <T> void forPossibleRelatedValue(Resource subject,
4793 Resource relation, Binding binding, Procedure<T> procedure) {
4794 forPossibleRelatedValue(subject, relation, binding,
4795 new NoneToAsyncProcedure<T>(procedure));
4799 public void forIsInstanceOf(Resource subject, Resource relation,
4800 AsyncListener<Boolean> listener) {
4801 asyncRequest(new IsInstanceOf(subject, relation), listener);
4805 public void forIsInstanceOf(Resource subject, Resource relation,
4806 SyncListener<Boolean> listener) {
4807 asyncRequest(new IsInstanceOf(subject, relation), listener);
4811 public void forIsInstanceOf(Resource subject, Resource relation,
4812 Listener<Boolean> listener) {
4813 asyncRequest(new IsInstanceOf(subject, relation), listener);
4817 final public void forIsInstanceOf(final Resource resource,
4818 final Resource type, final AsyncProcedure<Boolean> procedure) {
4820 assert (resource != null);
4821 assert (type != null);
4822 assert (procedure != null);
4824 forTypes(resource, new AsyncProcedure<Set<Resource>>() {
4827 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4830 if (result.contains(type))
4831 procedure.execute(graph, true);
4833 procedure.execute(graph, false);
4834 } catch (Throwable t) {
4835 Logger.defaultLogError(t);
4840 public void exception(AsyncReadGraph graph, Throwable t) {
4842 procedure.exception(graph, t);
4843 } catch (Throwable t2) {
4844 Logger.defaultLogError(t2);
4849 public String toString() {
4850 return "forIsInstanceOf -> " + procedure;
4858 public void forIsInstanceOf(Resource subject, Resource relation,
4859 SyncProcedure<Boolean> procedure) {
4860 forIsInstanceOf(subject, relation, new SyncToAsyncProcedure<Boolean>(
4865 public void forIsInstanceOf(Resource subject, Resource relation,
4866 Procedure<Boolean> procedure) {
4867 forIsInstanceOf(subject, relation, new NoneToAsyncProcedure<Boolean>(
4872 public void forIsInheritedFrom(Resource subject, Resource relation,
4873 AsyncListener<Boolean> listener) {
4874 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4878 public void forIsInheritedFrom(Resource subject, Resource relation,
4879 SyncListener<Boolean> listener) {
4880 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4884 public void forIsInheritedFrom(Resource subject, Resource relation,
4885 Listener<Boolean> listener) {
4886 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4890 final public void forIsInheritedFrom(final Resource resource,
4891 final Resource type, final AsyncProcedure<Boolean> procedure) {
4893 assert (resource != null);
4894 assert (type != null);
4895 assert (procedure != null);
4897 if (resource.equals(type)) {
4899 procedure.execute(this, true);
4900 } catch (Throwable t) {
4901 Logger.defaultLogError(t);
4906 forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
4909 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4911 if (result.contains(type))
4912 procedure.execute(graph, true);
4914 procedure.execute(graph, false);
4915 } catch (Throwable t) {
4916 Logger.defaultLogError(t);
4921 public void exception(AsyncReadGraph graph, Throwable t) {
4923 procedure.exception(graph, t);
4924 } catch (Throwable t2) {
4925 Logger.defaultLogError(t2);
4930 public String toString() {
4931 return "forIsInheritedFrom -> " + procedure;
4939 public void forIsInheritedFrom(Resource subject, Resource relation,
4940 SyncProcedure<Boolean> procedure) {
4941 forIsInheritedFrom(subject, relation,
4942 new SyncToAsyncProcedure<Boolean>(procedure));
4946 public void forIsInheritedFrom(Resource subject, Resource relation,
4947 Procedure<Boolean> procedure) {
4948 forIsInheritedFrom(subject, relation,
4949 new NoneToAsyncProcedure<Boolean>(procedure));
4953 public void forIsSubrelationOf(Resource subject, Resource relation,
4954 AsyncListener<Boolean> listener) {
4955 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4959 public void forIsSubrelationOf(Resource subject, Resource relation,
4960 SyncListener<Boolean> listener) {
4961 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4965 public void forIsSubrelationOf(Resource subject, Resource relation,
4966 Listener<Boolean> listener) {
4967 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4971 final public void forIsSubrelationOf(final Resource resource,
4972 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4974 assert (resource != null);
4975 assert (relation != null);
4976 assert (procedure != null);
4978 if (resource.equals(relation)) {
4979 procedure.execute(this, true);
4983 forSuperrelations(resource, new AsyncProcedure<Set<Resource>>() {
4986 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4988 if (result.contains(relation))
4989 procedure.execute(graph, true);
4991 procedure.execute(graph, false);
4992 } catch (Throwable t) {
4993 Logger.defaultLogError(t);
4998 public void exception(AsyncReadGraph graph, Throwable t) {
5000 procedure.exception(graph, t);
5001 } catch (Throwable t2) {
5002 Logger.defaultLogError(t2);
5007 public String toString() {
5008 return "forIsSubrelationOf -> " + procedure;
5016 public void forIsSubrelationOf(Resource subject, Resource relation,
5017 SyncProcedure<Boolean> procedure) {
5018 forIsSubrelationOf(subject, relation,
5019 new SyncToAsyncProcedure<Boolean>(procedure));
5023 public void forIsSubrelationOf(Resource subject, Resource relation,
5024 Procedure<Boolean> procedure) {
5025 forIsSubrelationOf(subject, relation,
5026 new NoneToAsyncProcedure<Boolean>(procedure));
5030 public void forHasStatement(Resource subject,
5031 AsyncListener<Boolean> listener) {
5032 asyncRequest(new HasStatementSubject(subject), listener);
5036 public void forHasStatement(Resource subject, SyncListener<Boolean> listener) {
5037 asyncRequest(new HasStatementSubject(subject), listener);
5041 public void forHasStatement(Resource subject, Listener<Boolean> listener) {
5042 asyncRequest(new HasStatementSubject(subject), listener);
5046 final public void forHasStatement(final Resource subject,
5047 final AsyncProcedure<Boolean> procedure) {
5049 assert (subject != null);
5050 assert (procedure != null);
5052 processor.forHasStatement(this, subject, procedure);
5057 public void forHasStatement(Resource subject,
5058 SyncProcedure<Boolean> procedure) {
5059 forHasStatement(subject, new SyncToAsyncProcedure<Boolean>(procedure));
5063 public void forHasStatement(Resource subject, Procedure<Boolean> procedure) {
5064 forHasStatement(subject, new NoneToAsyncProcedure<Boolean>(procedure));
5068 public void forHasStatement(Resource subject, Resource relation,
5069 AsyncListener<Boolean> listener) {
5070 asyncRequest(new HasStatement(subject, relation), listener);
5074 public void forHasStatement(Resource subject, Resource relation,
5075 SyncListener<Boolean> listener) {
5076 asyncRequest(new HasStatement(subject, relation), listener);
5080 public void forHasStatement(Resource subject, Resource relation,
5081 Listener<Boolean> listener) {
5082 asyncRequest(new HasStatement(subject, relation), listener);
5086 final public void forHasStatement(final Resource subject,
5087 final Resource relation, final AsyncProcedure<Boolean> procedure) {
5089 assert (subject != null);
5090 assert (relation != null);
5091 assert (procedure != null);
5093 processor.forHasStatement(this, subject, relation, procedure);
5098 public void forHasStatement(Resource subject, Resource relation,
5099 SyncProcedure<Boolean> procedure) {
5100 forHasStatement(subject, relation, new SyncToAsyncProcedure<Boolean>(
5105 public void forHasStatement(Resource subject, Resource relation,
5106 Procedure<Boolean> procedure) {
5107 forHasStatement(subject, relation, new NoneToAsyncProcedure<Boolean>(
5112 public void forHasStatement(Resource subject, Resource relation,
5113 Resource object, AsyncListener<Boolean> listener) {
5114 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
5119 public void forHasStatement(Resource subject, Resource relation,
5120 Resource object, SyncListener<Boolean> listener) {
5121 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
5126 public void forHasStatement(Resource subject, Resource relation,
5127 Resource object, Listener<Boolean> listener) {
5128 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
5133 final public void forHasStatement(final Resource subject,
5134 final Resource relation, final Resource object,
5135 final AsyncProcedure<Boolean> procedure) {
5137 assert (subject != null);
5138 assert (relation != null);
5139 assert (object != null);
5140 assert (procedure != null);
5142 processor.forHasStatement(this, subject, relation, object, procedure);
5147 public void forHasStatement(Resource subject, Resource relation,
5148 Resource object, SyncProcedure<Boolean> procedure) {
5149 forHasStatement(subject, relation, object,
5150 new SyncToAsyncProcedure<Boolean>(procedure));
5154 public void forHasStatement(Resource subject, Resource relation,
5155 Resource object, Procedure<Boolean> procedure) {
5156 forHasStatement(subject, relation, object,
5157 new NoneToAsyncProcedure<Boolean>(procedure));
5161 public void forHasValue(Resource subject, AsyncListener<Boolean> listener) {
5162 asyncRequest(new HasValue(subject), listener);
5166 public void forHasValue(Resource subject, SyncListener<Boolean> listener) {
5167 asyncRequest(new HasValue(subject), listener);
5171 public void forHasValue(Resource subject, Listener<Boolean> listener) {
5172 asyncRequest(new HasValue(subject), listener);
5176 final public void forHasValue(final Resource subject,
5177 final AsyncProcedure<Boolean> procedure) {
5179 assert (subject != null);
5180 assert (procedure != null);
5182 processor.forValue(this, subject, new AsyncProcedure<byte[]>() {
5185 public void execute(AsyncReadGraph graph, byte[] result) {
5188 procedure.execute(graph, false);
5190 procedure.execute(graph, true);
5191 } catch (Throwable t) {
5192 Logger.defaultLogError(t);
5197 public void exception(AsyncReadGraph graph, Throwable t) {
5199 procedure.exception(graph, t);
5200 } catch (Throwable t2) {
5201 Logger.defaultLogError(t2);
5206 public String toString() {
5207 return "forHasValue -> " + procedure;
5215 public void forHasValue(Resource subject, SyncProcedure<Boolean> procedure) {
5216 forHasValue(subject, new SyncToAsyncProcedure<Boolean>(procedure));
5220 public void forHasValue(Resource subject, Procedure<Boolean> procedure) {
5221 forHasValue(subject, new NoneToAsyncProcedure<Boolean>(procedure));
5225 public void forOrderedSet(Resource subject,
5226 AsyncMultiListener<Resource> listener) {
5227 asyncRequest(new OrderedSet(subject), listener);
5231 public void forOrderedSet(Resource subject,
5232 SyncMultiListener<Resource> listener) {
5233 asyncRequest(new OrderedSet(subject), listener);
5237 public void forOrderedSet(Resource subject, MultiListener<Resource> listener) {
5238 asyncRequest(new OrderedSet(subject), listener);
5242 final public void forOrderedSet(final Resource subject,
5243 final AsyncMultiProcedure<Resource> procedure) {
5245 assert (subject != null);
5246 assert (procedure != null);
5248 processor.forOrderedSet(this, subject,
5249 new AsyncMultiProcedure<Resource>() {
5252 public void finished(AsyncReadGraph graph) {
5254 procedure.finished(graph);
5255 } catch (Throwable t) {
5256 Logger.defaultLogError(t);
5261 public void execute(AsyncReadGraph graph, Resource result) {
5263 procedure.execute(graph, result);
5264 } catch (Throwable t) {
5265 Logger.defaultLogError(t);
5270 public void exception(AsyncReadGraph graph, Throwable t) {
5272 procedure.exception(graph, t);
5273 } catch (Throwable t2) {
5274 Logger.defaultLogError(t2);
5279 public String toString() {
5280 return "forOrderedSet -> " + procedure;
5288 public void forOrderedSet(Resource subject,
5289 SyncMultiProcedure<Resource> procedure) {
5290 forOrderedSet(subject, new SyncToAsyncMultiProcedure<Resource>(
5295 public void forOrderedSet(Resource subject,
5296 MultiProcedure<Resource> procedure) {
5297 forOrderedSet(subject, new NoneToAsyncMultiProcedure<Resource>(
5302 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5303 AsyncListener<T> listener) {
5304 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5308 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5309 SyncListener<T> listener) {
5310 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5314 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5315 Listener<T> listener) {
5316 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5320 final public <T> void forPossibleAdapted(final Resource resource,
5321 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5323 assert (resource != null);
5324 assert (clazz != null);
5325 assert (procedure != null);
5327 final AdaptionService service = getSession().peekService(AdaptionService.class);
5328 if (service == null)
5329 procedure.exception(this, new ServiceException("No AdaptionService available"));
5331 service.adapt(this, resource, resource, Resource.class, clazz, true, procedure);
5335 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5336 SyncProcedure<T> procedure) {
5337 forPossibleAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5342 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5343 Procedure<T> procedure) {
5344 forPossibleAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5349 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5350 AsyncListener<T> listener) {
5351 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5355 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5356 SyncListener<T> listener) {
5357 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5361 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5362 Listener<T> listener) {
5363 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5367 final public <T> void forPossibleUniqueAdapted(final Resource resource,
5368 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5370 assert (resource != null);
5371 assert (clazz != null);
5372 assert (procedure != null);
5374 final AdaptionService service = getSession().peekService(AdaptionService.class);
5375 if (service == null)
5376 procedure.exception(this, new ServiceException("No AdaptionService available"));
5378 service.adaptNew(this, resource, clazz, true, procedure);
5383 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5384 SyncProcedure<T> procedure) {
5385 forPossibleUniqueAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5390 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5391 Procedure<T> procedure) {
5392 forPossibleUniqueAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5397 * Implementation of the interface AsyncRequestProcessor
5401 final public Session getSession() {
5402 return processor.getSession();
5406 public <T> void asyncRequest(final Read<T> request) {
5408 asyncRequest(request, new AsyncProcedure<T>() {
5411 public void execute(AsyncReadGraph graph, T result) {
5415 public void exception(AsyncReadGraph graph, Throwable t) {
5416 Logger.defaultLogError(t);
5420 public String toString() {
5421 return "asyncRequest(Read) -> " + request;
5429 public <T> void asyncRequest(Read<T> request, AsyncListener<T> procedure) {
5430 asyncRequest(request, (AsyncProcedure<T>) procedure);
5434 public <T> void asyncRequest(Read<T> request,
5435 final SyncListener<T> procedure) {
5436 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5440 public <T> void asyncRequest(Read<T> request, final Listener<T> procedure) {
5441 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5445 public <T> void asyncRequest(final Read<T> request, final AsyncProcedure<T> procedure) {
5447 assert (request != null);
5448 assert (procedure != null);
5450 processor.schedule(new SessionTask(false) {
5453 public void run(int thread) {
5455 final ListenerBase listener = getListenerBase(procedure);
5456 QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5457 } catch (DatabaseException e) {
5458 Logger.defaultLogError(e);
5466 public static ReadGraphImpl createAsync(QueryProcessor support) {
5467 return new ReadGraphImpl(null, support);
5471 public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
5472 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5476 public <T> void asyncRequest(Read<T> request, final Procedure<T> procedure) {
5477 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5481 final public <T> void asyncRequest(final AsyncRead<T> request) {
5483 assert (request != null);
5485 asyncRequest(request, new AsyncProcedure<T>() {
5488 public void execute(AsyncReadGraph graph, T result) {
5492 public void exception(AsyncReadGraph graph, Throwable t) {
5493 Logger.defaultLogError(t);
5497 public String toString() {
5498 return "asyncRequest(AsyncRead) -> " + request;
5506 public <T> void asyncRequest(AsyncRead<T> request,
5507 AsyncListener<T> procedure) {
5508 asyncRequest(request, (AsyncProcedure<T>) procedure);
5512 final public <T> void asyncRequest(AsyncRead<T> request,
5513 final SyncListener<T> procedure) {
5514 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5518 final public <T> void asyncRequest(AsyncRead<T> request,
5519 final Listener<T> procedure) {
5520 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5524 final public <T> void asyncRequest(final AsyncRead<T> request,
5525 final AsyncProcedure<T> procedure) {
5527 assert (request != null);
5528 assert (procedure != null);
5530 processor.schedule(new SessionTask(false) {
5533 public void run(int thread) {
5535 final ListenerBase listener = getListenerBase(procedure);
5536 QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5537 } catch (DatabaseException e) {
5538 Logger.defaultLogError(e);
5547 public <T> void asyncRequest(AsyncRead<T> request,
5548 SyncProcedure<T> procedure) {
5549 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5553 final public <T> void asyncRequest(final AsyncRead<T> request,
5554 final Procedure<T> procedure) {
5555 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5559 public <T> void asyncRequest(final MultiRead<T> request) {
5561 assert (request != null);
5563 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5565 public void exception(AsyncReadGraph graph, Throwable t) {
5566 Logger.defaultLogError(t);
5570 public String toString() {
5571 return "asyncRequest(MultiRead) -> " + request;
5578 public <T> void asyncRequest(MultiRead<T> request,
5579 AsyncMultiListener<T> procedure) {
5580 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5584 public <T> void asyncRequest(MultiRead<T> request,
5585 SyncMultiListener<T> procedure) {
5586 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5590 public <T> void asyncRequest(MultiRead<T> request,
5591 MultiListener<T> procedure) {
5592 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5596 public <T> void asyncRequest(final MultiRead<T> request,
5597 final AsyncMultiProcedure<T> procedure) {
5599 assert (request != null);
5600 assert (procedure != null);
5602 final ListenerBase listener = getListenerBase(procedure);
5604 if (parent != null || listener != null) {
5606 // final ReadGraphImpl newGraph = newSync();
5607 processor.query(this, request, parent, procedure,listener);
5611 // final ReadGraphImpl newGraph = newSync();
5615 request.perform(this, procedure);
5617 } catch (Throwable t) {
5619 procedure.exception(this, t);
5628 public <T> void asyncRequest(MultiRead<T> request,
5629 SyncMultiProcedure<T> procedure) {
5630 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5634 public <T> void asyncRequest(MultiRead<T> request,
5635 MultiProcedure<T> procedure) {
5636 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5640 final public <T> void asyncRequest(final AsyncMultiRead<T> request) {
5642 assert (request != null);
5644 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5646 public void exception(AsyncReadGraph graph, Throwable t) {
5647 Logger.defaultLogError(t);
5651 public String toString() {
5652 return "asyncRequest(AsyncMultiRead) -> " + request;
5659 public <T> void asyncRequest(AsyncMultiRead<T> request,
5660 AsyncMultiListener<T> procedure) {
5661 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5665 public <T> void asyncRequest(AsyncMultiRead<T> request,
5666 SyncMultiListener<T> procedure) {
5667 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5671 public <T> void asyncRequest(AsyncMultiRead<T> request,
5672 MultiListener<T> procedure) {
5673 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5677 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5678 final AsyncMultiProcedure<T> procedure) {
5680 assert (request != null);
5681 assert (procedure != null);
5683 ListenerBase listener = getListenerBase(procedure);
5685 if (parent != null || listener != null) {
5687 processor.query(this, request, parent, procedure, listener);
5693 request.perform(this, new AsyncMultiProcedure<T>() {
5696 public void execute(AsyncReadGraph graph, T result) {
5697 procedure.execute(graph, result);
5701 public void finished(AsyncReadGraph graph) {
5702 procedure.finished(graph);
5706 public void exception(AsyncReadGraph graph, Throwable t) {
5707 procedure.exception(graph, t);
5711 public String toString() {
5712 return "asyncRequest(AsyncMultiRead) -> " + procedure;
5717 } catch (Throwable t) {
5719 procedure.exception(this, new DatabaseException(t));
5727 public <T> void asyncRequest(AsyncMultiRead<T> request,
5728 SyncMultiProcedure<T> procedure) {
5729 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5733 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5734 final MultiProcedure<T> procedure) {
5735 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5739 final public <T> void asyncRequest(final ExternalRead<T> request) {
5741 assert (request != null);
5743 asyncRequest(request, new Procedure<T>() {
5746 public void execute(T result) {
5750 public void exception(Throwable t) {
5751 Logger.defaultLogError(t);
5755 public String toString() {
5756 return "asyncRequest(PrimitiveRead) -> " + request;
5764 public <T> void asyncRequest(ExternalRead<T> request,
5765 final Listener<T> procedure) {
5766 asyncRequest(request, (Procedure<T>) procedure);
5770 final public <T> void asyncRequest(final ExternalRead<T> request,
5771 final Procedure<T> procedure) {
5774 assert (request != null);
5775 assert (procedure != null);
5777 final ListenerBase listener = getListenerBase(procedure);
5779 if (parent != null || listener != null) {
5782 QueryCacheBase.resultExternalReadEntry(this, request, parent, listener, procedure);
5783 //processor.query(this, request, parent, procedure,listener);
5784 } catch (DatabaseException e) {
5785 Logger.defaultLogError(e);
5786 // This throwable has already been transferred to procedure at this point - do nothing about it
5792 request.register(this, new Listener<T>() {
5795 public void execute(T result) {
5796 procedure.execute(result);
5800 public void exception(Throwable t) {
5801 procedure.exception(t);
5805 public String toString() {
5806 return "asyncRequest(PrimitiveRead) -> " + request;
5810 public boolean isDisposed() {
5816 // final ReadGraphImpl newGraph = newSync();
5820 // T result = request.perform(this);
5823 // procedure.execute(this, result);
5824 // } catch (Throwable t) {
5825 // Logger.defaultLogError(t);
5828 // } catch (Throwable t) {
5831 // procedure.exception(this, t);
5832 // } catch (Throwable t2) {
5833 // Logger.defaultLogError(t2);
5842 // assert (request != null);
5843 // assert (procedure != null);
5845 // ListenerBase listener = getListenerBase(procedure);
5847 // if (parent != null || listener != null) {
5849 // processor.query(this, request, parent, procedure, listener);
5855 // request.register(this, new Listener<T>() {
5858 // public void execute(T result) {
5859 // procedure.execute(result);
5863 // public void exception(Throwable t) {
5864 // procedure.exception(t);
5868 // public String toString() {
5869 // return "asyncRequest(PrimitiveRead) -> " + request;
5873 // public boolean isDisposed() {
5879 // } catch (Throwable t) {
5881 // if (t instanceof DatabaseException)
5882 // procedure.exception(t);
5885 // .exception(new DatabaseException(
5886 // "Unexpected exception in ReadGraph.asyncRequest(SingleAsyncRead, SingleProcedure)",
5896 public void asyncRequest(final Write request) {
5898 assert (request != null);
5900 getSession().asyncRequest(request);
5902 // processor.asyncWrite(request);
5907 public <T> void asyncRequest(final WriteResult<T> request, Procedure<T> procedure) {
5908 throw new Error("Not implemented.");
5912 public void asyncRequest(Write request, Consumer<DatabaseException> callback) {
5914 assert (request != null);
5916 getSession().asyncRequest(request, callback);
5921 public void asyncRequest(final DelayedWrite request) {
5923 assert (request != null);
5925 getSession().asyncRequest(request);
5930 public <T> void asyncRequest(final DelayedWriteResult<T> request, Procedure<T> procedure) {
5931 throw new Error("Not implemented.");
5935 public void asyncRequest(DelayedWrite r,
5936 Consumer<DatabaseException> callback) {
5937 throw new Error("Not implemented.");
5941 public void asyncRequest(final WriteOnly request) {
5943 assert (request != null);
5945 getSession().asyncRequest(request);
5950 public <T> void asyncRequest(final WriteOnlyResult<T> request, Procedure<T> procedure) {
5951 throw new Error("Not implemented.");
5955 public void asyncRequest(WriteOnly r, Consumer<DatabaseException> callback) {
5956 throw new Error("Not implemented.");
5960 * Implementation of the interface ServiceLocator
5964 public <T> T getService(Class<T> api) {
5965 if(WriteSupport.class == api) {
5966 if(this instanceof WriteGraphImpl) {
5967 WriteGraphImpl impl = (WriteGraphImpl)this;
5968 return (T)impl.writeSupport;
5971 return getSession().getService(api);
5975 public <T> T peekService(Class<T> api) {
5976 return getSession().peekService(api);
5980 public boolean hasService(Class<?> api) {
5981 return getSession().hasService(api);
5985 public <T> void registerService(Class<T> api, T service) {
5986 getSession().registerService(api, service);
5990 public boolean isImmutable(Resource resource) throws DatabaseException {
5991 ResourceImpl impl = (ResourceImpl)resource;
5992 return processor.isImmutable(impl.id);
5999 protected static String INTERNAL_ERROR_STRING = "Transaction aborted due to internal client error. Contact application support.";
6002 * callerThread is the currently running thread state.syncThread is blocking for
6003 * this execution state.syncParent is the blocking request
6006 final private boolean isExternal(int thread) {
6007 return thread == Integer.MIN_VALUE;
6010 ReadGraphImpl(ReadGraphImpl graph) {
6011 this(graph.parent, graph.processor);
6014 ReadGraphImpl(CacheEntry parent, QueryProcessor support) {
6015 this.parent = parent;
6016 this.processor = support;
6019 public static ReadGraphImpl create(QueryProcessor support) {
6020 return new ReadGraphImpl(null, support);
6023 public ReadGraphImpl newRestart(ReadGraphImpl impl) {
6025 WriteGraphImpl write = processor.getSession().getService(
6026 WriteGraphImpl.class);
6032 public ReadGraphImpl withParent(CacheEntry parent) {
6033 if(parent == this.parent) return this;
6034 else return new ReadGraphImpl(parent, processor);
6037 final private ListenerBase getListenerBase(final Object procedure) {
6038 if (procedure instanceof ListenerBase)
6039 return (ListenerBase) procedure;
6044 public <T> void waitAsyncProcedure(AsyncMultiReadProcedure<T> procedure) {
6046 assert(procedure.done());
6050 public <T> void waitAsyncProcedure(AsyncReadProcedure<T> procedure) {
6052 assert(procedure.done());
6056 public boolean resumeTasks() {
6057 return processor.resumeTasks(this);
6060 Class<?> singleClass(Set<Resource> types) {
6061 Class<?> result = null;
6062 for (Resource type : types) {
6063 Class<?> clazz = processor.getBuiltinValue(type);
6064 if (clazz != null) {
6074 private String debugString(Resource r) {
6077 name = getPossibleRelatedValue(r, processor.getL0(this).HasName);
6078 } catch (ManyObjectsForFunctionalRelationException e) {
6079 Logger.defaultLogError(e);
6080 } catch (ServiceException e) {
6081 Logger.defaultLogError(e);
6083 return "[" + name + " - " + r + "]";
6087 public String toString() {
6088 return "ReadGraphImpl[thread=" + Thread.currentThread() + "]";
6092 final public int thread() {
6096 static class MultiTripleIntProcedure implements TripleIntProcedure {
6098 final private AsyncMultiProcedure<Statement> procedure;
6099 final private ReadGraphImpl impl;
6100 final private QuerySupport support;
6102 public MultiTripleIntProcedure(AsyncMultiProcedure<Statement> procedure, ReadGraphImpl impl, QuerySupport support) {
6103 this.procedure = procedure;
6105 this.support = support;
6109 public void execute(ReadGraphImpl graph, int s, int p, int o) {
6111 procedure.execute(graph, support.getStatement(s, p, o));
6112 } catch (Throwable t2) {
6113 Logger.defaultLogError(t2);
6118 public void finished(ReadGraphImpl graph) {
6120 procedure.finished(graph);
6121 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
6122 } catch (Throwable t2) {
6123 Logger.defaultLogError(t2);
6128 public void exception(ReadGraphImpl graph, Throwable t) {
6130 procedure.exception(graph, t);
6131 } catch (Throwable t2) {
6132 Logger.defaultLogError(t2);
6134 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
6138 public String toString() {
6139 return "forEachObject with " + procedure;
6144 // private AsyncMultiProcedure<Resource> cacheKey = null;
6145 // private MultiIntProcedure cacheResult = null;
6147 // final IntProcedure forMultiProcedure(final AsyncMultiProcedure<Resource> procedure) {
6149 // if(procedure == cacheKey) return cacheResult;
6151 // cacheResult = new MultiIntProcedure(procedure, this, processor.support);
6152 // cacheKey = procedure;
6154 // return cacheResult;
6158 // private AsyncMultiProcedure<Statement> cacheKey2 = null;
6159 // private MultiTripleIntProcedure cacheResult2 = null;
6161 // final synchronized TripleIntProcedure forMultiProcedure(final AsyncMultiProcedure<Statement> procedure) {
6163 // if(procedure == cacheKey2) return cacheResult2;
6165 // cacheResult2 = new MultiTripleIntProcedure(procedure, this, processor.support);
6166 // cacheKey2 = procedure;
6168 // return cacheResult2;
6173 public Datatype getDataType(Resource subject) throws DatabaseException {
6174 for(Resource dataTypeResource : getObjects(subject, processor.getL0(this).HasDataType))
6175 return getValue(dataTypeResource, Bindings.getBindingUnchecked(Datatype.class));
6176 throw new DoesNotContainValueException("The literal has no data type.");
6179 protected <T extends Accessor> T getAccessor4File(Resource subject)
6180 throws DatabaseException {
6183 byte[] bytes = processor.support.getValue(g, subject);
6187 BinaryVariant va = (BinaryVariant) Accessors.getAccessor(bytes, Datatypes.VARIANT);
6188 Accessor ca = va.getContentAccessor();
6190 } catch (AccessorConstructionException e) {
6191 throw new DatabaseException(e);
6197 Binding datatype_binding = Bindings.getBindingUnchecked(DataType.class);
6198 Serializer datatype_serializer = datatype_binding.serializer();
6201 BinaryMemory in = new BinaryMemory(ByteBuffer.wrap(bytes));
6202 datatype = (DataType)datatype_serializer.deserialize(in);
6203 Binding data_binding = Bindings.getBinding(datatype);
6204 Serializer data_serializer = data_binding.serializer(BinarySerializationFormat.INSTANCE);
6205 Object o = data_serializer.deserialize(in);
6207 return (T)Accessors.getAccessor(data_binding, o);
6208 } catch(AccessorConstructionException e) {
6211 } catch (Exception e) {
6212 throw new DatabaseException(e);
6215 @SuppressWarnings("unchecked")
6217 public <T extends Accessor> T getAccessor(Resource subject) throws DatabaseException {
6218 RandomAccessBinary rab = getRandomAccessBinary(subject);
6220 return (T)Accessors.getAccessor(rab, getDataType(subject));
6221 } catch(AccessorConstructionException e) {
6222 throw new DatabaseException(e);
6225 @SuppressWarnings("unchecked")
6226 protected <T extends Accessor> T createAccessor(Resource resource, Datatype datatype, Object intialValue)
6227 throws DatabaseException {
6228 RandomAccessBinary rab = createRandomAccessBinary(resource, datatype, intialValue);
6230 return (T)Accessors.getAccessor(rab, datatype);
6231 } catch(AccessorConstructionException e) {
6232 throw new DatabaseException(e);
6236 public RandomAccessBinary getRandomAccessBinary(Resource subject) throws DatabaseException {
6237 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
6238 ResourceData rd = ravs.get(subject);
6242 ExternalValueSupport evs = getService(ExternalValueSupport.class);
6243 long size = evs.getValueSize(this, subject); // Throws DatabaseException if no old external value.
6245 File platform = Platform.getLocation().toFile();
6246 File tempFiles = new File(platform, "tempFiles");
6247 File dbDir = new File(tempFiles, "db");
6249 File file = new File(dbDir, "ResourceFile" + subject.getResourceId());
6250 rd = new ResourceData(new BinaryFile(file), true); // Old external value.
6251 final int N = 1<<20;
6255 int length = N < left ? N : (int)left;
6256 byte[] bytes = evs.readValue(this, subject, offset, length);
6257 offset += bytes.length;
6258 left -= bytes.length;
6259 rd.binaryFile.write(bytes);
6261 ravs.put(subject, rd);
6263 } catch (Exception e) {
6264 throw new DatabaseException("Resource " + subject + " have value but there was problem with accessing value data.", e);
6266 } catch (Exception e) {
6267 if(Development.DEVELOPMENT) {
6268 if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
6269 e.printStackTrace();
6273 Datatype datatype = getDataType(subject);
6274 Object value = getPossibleValue(subject, Bindings.getBinding(datatype));
6275 return createRandomAccessBinary(subject, datatype, value);
6277 public RandomAccessBinary createRandomAccessBinary(Resource resource, Datatype datatype, Object initialValue)
6278 throws DatabaseException {
6279 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
6281 File platform = Platform.getLocation().toFile();
6282 File tempFiles = new File(platform, "tempFiles");
6283 File dbDir = new File(tempFiles, "db");
6285 File file = new File(dbDir, "ResourceFile" + resource.getResourceId());
6286 ResourceData rd = new ResourceData(new BinaryFile(file), false);
6287 Binding binding = Bindings.getBinding(datatype);
6288 if (null == initialValue) {
6289 initialValue = binding.createDefault();
6291 Serializer serializer = binding.serializer();
6292 byte[] bytes = serializer.serialize(initialValue);
6293 rd.binaryFile.write(bytes);
6294 ravs.put(resource, rd);
6296 } catch (Exception e) {
6297 if (e instanceof DatabaseException)
6298 throw (DatabaseException)e;
6300 throw new DatabaseException(e);
6304 // static class ExternalValueRequest<T> extends ResourceRead<T> {
6306 // public ExternalValueRequest(Resource resource) {
6310 // @SuppressWarnings("unchecked")
6312 // public T perform(ReadGraph graph) throws DatabaseException {
6315 // String uri = graph.getURI(resource);
6316 // if(Layer0.URIs.Functions_functionApplication.equals(uri)) return (T)functionApplication;
6318 // return (T)ReflectionUtils.getValue(uri).getValue();
6320 // } catch(ValueNotFoundException e) {
6321 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
6322 // } catch(ClassCastException e) {
6323 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
6329 @SuppressWarnings("unchecked")
6331 public <T> T getValue2(Resource r, Object context) throws DatabaseException {
6332 Layer0 L0 = processor.getL0(this);
6333 Set<Resource> types = getTypes(r);
6335 if(types.contains(L0.Literal)) {
6336 if(isImmutable(r)) {
6337 return syncRequest(new ValueImplied<T>(r));
6342 else if(types.contains(L0.Function)) {
6343 return Functions.exec(this, r, this, r, context);
6345 else if(types.contains(L0.ExternalValue)) {
6346 return (T)syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6350 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6351 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6353 return function.apply(this, r, context);
6354 } catch(RuntimeException e) {
6355 DatabaseException dte = findPossibleRootException(e);
6356 if(dte != null) throw dte;
6357 else throw new DatabaseException(e);
6364 public Variant getVariantValue2(Resource r, Object context) throws DatabaseException {
6365 Layer0 L0 = processor.getL0(this);
6366 Set<Resource> types = getTypes(r);
6368 if(types.contains(L0.Literal)) {
6369 if(isImmutable(r)) {
6370 return syncRequest(new VariantValueImplied(r));
6372 return getVariantValue(r);
6375 else if(types.contains(L0.Function)) {
6376 Object value = Functions.exec(this, r, this, r, context);
6378 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6379 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6380 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6383 else if(types.contains(L0.ExternalValue)) {
6384 Object value = syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6386 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6387 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6388 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6393 Function3<ReadGraph,Resource,Object,Object> function = requestValueFunction(r);
6394 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6396 Object value = function.apply(this, r, context);
6398 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6399 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6400 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6402 } catch(RuntimeException e) {
6403 DatabaseException dte = findPossibleRootException(e);
6404 if(dte != null) throw dte;
6405 else throw new DatabaseException(e);
6411 public <T> T getPossibleValue2(Resource subject, Object context) throws DatabaseException {
6413 return getValue2(subject, context);
6414 } catch (DatabaseException e) {
6419 static class PossibleConverterFunction<T> extends ResourceRead<Function3<ReadGraph,Resource,Object,T>> {
6421 public PossibleConverterFunction(Resource resource) {
6426 public Function3<ReadGraph,Resource,Object,T> perform(ReadGraph graph) throws DatabaseException {
6427 return compute(graph, resource);
6430 @SuppressWarnings("unchecked")
6431 public static <T> Function3<ReadGraph,Resource,Object,T> compute(ReadGraph graph, Resource resource) throws DatabaseException {
6432 Layer0 L0 = Layer0.getInstance(graph);
6433 for(Resource converter : graph.getObjects(resource, L0.ConvertsToValueWith)) {
6435 if(L0.Functions_functionApplication.equals(converter)) {
6436 return AdaptValue.functionApplication;
6437 //return (Function3<ReadGraph,Resource,Object,T>)graph.syncRequest(new AdaptValue(resource));
6439 return graph.getValue2(converter, resource);
6441 } catch(RuntimeException e) {
6442 DatabaseException dte = findPossibleRootException(e);
6443 if(dte != null) throw dte;
6444 else throw new DatabaseException(e);
6452 <T> Function3<ReadGraph,Resource,Object,T> requestValueFunction(Resource r) throws DatabaseException {
6454 return syncRequest(new PossibleConverterFunction<T>(r), TransientCacheAsyncListener.<Function3<ReadGraph,Resource,Object,T>>instance());
6456 return syncRequest(new PossibleConverterFunction<T>(r));
6460 * Get a value associated with a graph {@link Resource}, using a possible context object and
6461 * a desired value binding. The following methods are tried in order to retreive the value:
6463 * <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>
6464 * <li>If the resource is a {@code L0.ExternalValue}, the value is acquired using
6465 * {@link ReflectionUtils#getValue(String)}.</li>
6466 * <li>If the resource is associated with a suitable value converter with relation {@code L0.ConvertsToValueWith}
6467 * (see {@link #requestValueFunction(Resource)}), the value function is called with the graph, the resource
6468 * and the context object.</li>
6471 * @param r A graph resource with which the value is associated
6472 * @param context A context object that is used for acquiring the value (only applied in case 3)
6473 * @param binding A binding for the value type (only applied in case 1)
6474 * @return The value of the graph node.
6475 * @throws DoesNotContainValueException No value is associated with the graph node.
6476 * @throws DatabaseException Other errors, such as an error in casting the value to the return type or
6477 * a runtime error in the value function.
6479 @SuppressWarnings("unchecked")
6481 public <T> T getValue2(Resource r, Object context, Binding binding) throws DatabaseException {
6482 if (binding instanceof ObjectVariantBinding)
6483 return getValue2(r, context);
6485 Layer0 L0 = processor.getL0(this);
6486 Set<Resource> types = getTypes(r);
6487 if(types.contains(L0.Literal)) {
6488 if(isImmutable(r)) {
6489 return syncRequest(new Value<T>(r, binding));
6491 return getValue(r, binding);
6493 } else if(types.contains(L0.Function)) {
6494 Object value = Functions.exec(this, r, this, r, context);
6495 if(binding.isInstance(value)) return (T)value;
6497 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
6498 return (T)Bindings.adapt(value, srcBinding, binding);
6499 } catch(RuntimeException e) {
6500 DatabaseException dte = findPossibleRootException(e);
6501 if(dte != null) throw dte;
6502 else throw new DatabaseException(e);
6503 } catch (AdaptException e) {
6504 throw new DatabaseException(e);
6505 } catch (org.simantics.databoard.binding.error.BindingException e) {
6506 throw new DatabaseException(e);
6508 } else if(types.contains(L0.ExternalValue)) {
6510 ExternalValue ev = adapt(r, ExternalValue.class);
6511 return ev.getValue(this, r);
6512 //return (T)ReflectionUtils.getValue(getURI(r)).getValue();
6513 // } catch(ValueNotFoundException e) {
6514 // throw new DatabaseException(e);
6515 } catch(ClassCastException e) {
6516 throw new DatabaseException(e);
6520 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6521 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function.");
6523 Object value = function.apply(this, r, context);
6524 if(binding.isInstance(value)) return (T)value;
6525 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
6526 return (T)Bindings.adapt(value, srcBinding, binding);
6527 } catch(RuntimeException e) {
6528 DatabaseException dte = findPossibleRootException(e);
6529 if(dte != null) throw dte;
6530 else throw new DatabaseException(e);
6531 } catch (AdaptException e) {
6532 throw new DatabaseException(e);
6533 } catch (org.simantics.databoard.binding.error.BindingException e) {
6534 throw new DatabaseException(e);
6540 public <T> T getPossibleValue2(Resource subject, Object context, Binding binding) throws DatabaseException {
6542 return getValue2(subject, context, binding);
6543 } catch (DatabaseException e) {
6548 private static DatabaseException findPossibleRootException(Throwable t) {
6549 if(t == null) return null;
6550 if(t instanceof DatabaseException) return (DatabaseException)t;
6551 if(t instanceof RuntimeException || t instanceof InvocationTargetException) {
6552 return findPossibleRootException(t.getCause());
6558 public <T> T getRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6559 return getRelatedValue2(subject, relation, subject);
6563 public Variant getRelatedVariantValue2(Resource subject, Resource relation) throws DatabaseException {
6564 return getRelatedVariantValue2(subject, relation, subject);
6568 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6570 Resource object = getPossibleObject(subject, relation);
6571 if(object == null) return null;
6572 else return getValue2(object, subject);
6573 } catch (DatabaseException e) {
6579 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6580 if(Development.DEVELOPMENT) {
6581 String error = L0Validations.checkValueType(this, subject, relation);
6583 Logger.defaultLogError(new ValidationException(error));
6584 //throw new ValidationException(error);
6585 new ValidationException(error).printStackTrace();
6588 return getValue2(getSingleObject(subject, relation), context);
6592 public Variant getRelatedVariantValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6593 if(Development.DEVELOPMENT) {
6594 String error = L0Validations.checkValueType(this, subject, relation);
6596 Logger.defaultLogError(new ValidationException(error));
6597 //throw new ValidationException(error);
6598 new ValidationException(error).printStackTrace();
6601 return getVariantValue2(getSingleObject(subject, relation), context);
6605 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6607 Resource object = getPossibleObject(subject, relation);
6608 if(object == null) return null;
6609 else return getValue2(object, context);
6610 } catch (DatabaseException e) {
6616 public <T> T getRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6617 return getRelatedValue2(subject, relation, subject, binding);
6621 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6623 Resource object = getPossibleObject(subject, relation);
6624 if(object == null) return null;
6625 return getValue2(object, subject, binding);
6626 } catch (DatabaseException e) {
6632 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6633 return getValue2(getSingleObject(subject, relation), context, binding);
6637 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6639 Resource object = getPossibleObject(subject, relation);
6640 if(object == null) return null;
6641 else return getValue2(object, context, binding);
6642 } catch (DatabaseException e) {
6648 public Type getRelatedValueType(Resource subject, Resource relation) throws DatabaseException {
6649 Layer0 L0 = processor.getL0(this);
6650 Resource property = getSingleObject(subject, relation);
6651 String typeText = (String)getRelatedValue(property, L0.HasValueType, Bindings.STRING);
6653 return org.simantics.scl.compiler.types.Types.parseType(typeText);
6654 } catch (SCLTypeParseException e) {
6655 throw new DatabaseException(e);
6659 private static ThreadLocal<Boolean> syncGraph = new ThreadLocal<Boolean>() {
6660 protected Boolean initialValue() {
6666 public boolean setSynchronous(boolean value) {
6667 boolean old = getSynchronous();
6668 syncGraph.set(value);
6673 public boolean getSynchronous() {
6674 return syncGraph.get();
6677 public void ensureLoaded(int resource) {
6678 processor.querySupport.ensureLoaded(this, resource);
6681 public void ensureLoaded(int resource, int predicate) {
6682 processor.querySupport.ensureLoaded(this, resource, predicate);
6685 public byte[] getValue(int resource) {
6686 return processor.querySupport.getValue(this, resource);
6689 public int thread(int resource) {
6690 return (resource >>> 16) & processor.THREAD_MASK;
6693 public int thread(Resource resource) {
6694 return (((ResourceImpl)resource).id >>> 16) & processor.THREAD_MASK;
6697 public ResourceSupport getResourceSupport() {
6698 return processor.getResourceSupport();
6702 public Object getModificationCounter() {
6703 return processor.getSession().getModificationCounter();
6707 public boolean performPending() {
6708 return processor.performPending(processor.thread.get());