1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 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.ExternalValueSupport;
50 import org.simantics.db.ReadGraph;
51 import org.simantics.db.RelationContext;
52 import org.simantics.db.Resource;
53 import org.simantics.db.Session;
54 import org.simantics.db.Statement;
55 import org.simantics.db.adaption.AdaptionService;
56 import org.simantics.db.common.primitiverequest.Adapter;
57 import org.simantics.db.common.primitiverequest.Builtin;
58 import org.simantics.db.common.primitiverequest.DatatypeBinding;
59 import org.simantics.db.common.primitiverequest.ForEachAssertedObject;
60 import org.simantics.db.common.primitiverequest.ForEachAssertedStatement;
61 import org.simantics.db.common.primitiverequest.HasStatement;
62 import org.simantics.db.common.primitiverequest.HasStatementSubject;
63 import org.simantics.db.common.primitiverequest.HasStatementSubjectObject;
64 import org.simantics.db.common.primitiverequest.HasValue;
65 import org.simantics.db.common.primitiverequest.Inverse;
66 import org.simantics.db.common.primitiverequest.IsInheritedFrom;
67 import org.simantics.db.common.primitiverequest.IsInstanceOf;
68 import org.simantics.db.common.primitiverequest.IsSubrelationOf;
69 import org.simantics.db.common.primitiverequest.OrderedSet;
70 import org.simantics.db.common.primitiverequest.PossibleAdapter;
71 import org.simantics.db.common.primitiverequest.PossibleInverse;
72 import org.simantics.db.common.primitiverequest.PossibleObject;
73 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;
74 import org.simantics.db.common.primitiverequest.PossibleRelatedValueImplied;
75 import org.simantics.db.common.primitiverequest.PossibleStatement;
76 import org.simantics.db.common.primitiverequest.PossibleType;
77 import org.simantics.db.common.primitiverequest.PossibleUniqueAdapter;
78 import org.simantics.db.common.primitiverequest.PossibleValue;
79 import org.simantics.db.common.primitiverequest.PossibleValueImplied;
80 import org.simantics.db.common.primitiverequest.RelatedValue;
81 import org.simantics.db.common.primitiverequest.RelatedValueImplied;
82 import org.simantics.db.common.primitiverequest.SingleObject;
83 import org.simantics.db.common.primitiverequest.SingleStatement;
84 import org.simantics.db.common.primitiverequest.SingleType;
85 import org.simantics.db.common.primitiverequest.SingleTypeAny;
86 import org.simantics.db.common.primitiverequest.Types;
87 import org.simantics.db.common.primitiverequest.UniqueAdapter;
88 import org.simantics.db.common.primitiverequest.Value;
89 import org.simantics.db.common.primitiverequest.ValueImplied;
90 import org.simantics.db.common.primitiverequest.VariantValueImplied;
91 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
92 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
93 import org.simantics.db.common.procedure.adapter.SyncMultiProcedureAdapter;
94 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
95 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
96 import org.simantics.db.common.procedure.single.SyncReadProcedure;
97 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrErrorProcedure;
98 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrNullProcedure;
99 import org.simantics.db.common.procedure.single.wrapper.ExceptionToNullProcedure;
100 import org.simantics.db.common.procedure.single.wrapper.NullSingleOrNullProcedure;
101 import org.simantics.db.common.procedure.single.wrapper.SingleFunctionalOrNullProcedure;
102 import org.simantics.db.common.procedure.single.wrapper.SingleOrErrorProcedure;
103 import org.simantics.db.common.procedure.single.wrapper.SingleOrNullProcedure;
104 import org.simantics.db.common.procedure.wrapper.NoneToAsyncListener;
105 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiListener;
106 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiProcedure;
107 import org.simantics.db.common.procedure.wrapper.NoneToAsyncProcedure;
108 import org.simantics.db.common.procedure.wrapper.NoneToAsyncSetProcedure;
109 import org.simantics.db.common.procedure.wrapper.NoneToSyncMultiListener;
110 import org.simantics.db.common.procedure.wrapper.NoneToSyncMultiProcedure;
111 import org.simantics.db.common.procedure.wrapper.SyncToAsyncListener;
112 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiListener;
113 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiProcedure;
114 import org.simantics.db.common.procedure.wrapper.SyncToAsyncProcedure;
115 import org.simantics.db.common.procedure.wrapper.SyncToAsyncSetProcedure;
116 import org.simantics.db.common.request.AdaptValue;
117 import org.simantics.db.common.request.ResourceRead;
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.BlockingAsyncProcedure;
137 import org.simantics.db.impl.RelationContextImpl;
138 import org.simantics.db.impl.ResourceImpl;
139 import org.simantics.db.impl.internal.RandomAccessValueSupport;
140 import org.simantics.db.impl.internal.ResourceData;
141 import org.simantics.db.impl.procedure.ResultCallWrappedSyncQueryProcedure;
142 import org.simantics.db.impl.query.CacheEntry;
143 import org.simantics.db.impl.query.QueryCache;
144 import org.simantics.db.impl.query.QueryCacheBase;
145 import org.simantics.db.impl.query.QueryProcessor;
146 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
147 import org.simantics.db.impl.query.QuerySupport;
148 import org.simantics.db.impl.query.TripleIntProcedure;
149 import org.simantics.db.impl.support.ResourceSupport;
150 import org.simantics.db.procedure.AsyncListener;
151 import org.simantics.db.procedure.AsyncMultiListener;
152 import org.simantics.db.procedure.AsyncMultiProcedure;
153 import org.simantics.db.procedure.AsyncProcedure;
154 import org.simantics.db.procedure.AsyncSetListener;
155 import org.simantics.db.procedure.Listener;
156 import org.simantics.db.procedure.ListenerBase;
157 import org.simantics.db.procedure.MultiListener;
158 import org.simantics.db.procedure.MultiProcedure;
159 import org.simantics.db.procedure.Procedure;
160 import org.simantics.db.procedure.SetListener;
161 import org.simantics.db.procedure.StatementProcedure;
162 import org.simantics.db.procedure.SyncListener;
163 import org.simantics.db.procedure.SyncMultiListener;
164 import org.simantics.db.procedure.SyncMultiProcedure;
165 import org.simantics.db.procedure.SyncProcedure;
166 import org.simantics.db.procedure.SyncSetListener;
167 import org.simantics.db.request.AsyncMultiRead;
168 import org.simantics.db.request.AsyncRead;
169 import org.simantics.db.request.DelayedWrite;
170 import org.simantics.db.request.DelayedWriteResult;
171 import org.simantics.db.request.ExternalRead;
172 import org.simantics.db.request.MultiRead;
173 import org.simantics.db.request.Read;
174 import org.simantics.db.request.ReadInterface;
175 import org.simantics.db.request.Write;
176 import org.simantics.db.request.WriteInterface;
177 import org.simantics.db.request.WriteOnly;
178 import org.simantics.db.request.WriteOnlyResult;
179 import org.simantics.db.request.WriteResult;
180 import org.simantics.layer0.Layer0;
181 import org.simantics.scl.compiler.types.Type;
182 import org.simantics.scl.compiler.types.exceptions.SCLTypeParseException;
183 import org.simantics.scl.reflection.ReflectionUtils;
184 import org.simantics.scl.reflection.ValueNotFoundException;
185 import org.simantics.scl.runtime.function.Function3;
186 import org.simantics.utils.DataContainer;
187 import org.simantics.utils.Development;
188 import org.simantics.utils.datastructures.Pair;
189 import org.simantics.utils.datastructures.collections.CollectionUtils;
190 import org.slf4j.LoggerFactory;
192 import gnu.trove.map.hash.TObjectIntHashMap;
194 public class ReadGraphImpl implements AsyncReadGraph {
196 private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ReadGraphImpl.class);
198 final static boolean EMPTY_RESOURCE_CHECK = false;
200 final public CacheEntry parent;
201 final public QueryProcessor processor;
203 public AsyncBarrierImpl asyncBarrier = null;
205 final static Binding DATA_TYPE_BINDING_INTERNAL = Bindings.getBindingUnchecked(Datatype.class);
206 final static Serializer DATA_TYPE_SERIALIZER = Bindings.getSerializerUnchecked(DATA_TYPE_BINDING_INTERNAL);
208 final public static TObjectIntHashMap<String> counters = new TObjectIntHashMap<String>();
210 public static void resetCounters() {
214 public static String listCounters(File file) throws IOException {
216 PrintStream b = new PrintStream(new BufferedOutputStream(new FileOutputStream(file)));
218 for(Pair<String,Integer> p : CollectionUtils.valueSortedEntries(counters)) {
219 b.print(-p.second + " " + p.first + "\n");
224 return "Dumped " + counters.size() + " queries.";
229 * Implementation of the interface ReadGraph
231 final public String getURI(final Resource resource) throws AssumptionException, ValidationException, ServiceException {
233 assert (resource != null);
237 return syncRequest(new org.simantics.db.common.uri.ResourceToURI(resource));
239 } catch (AssumptionException e) {
241 throw new AssumptionException(e);
243 } catch (ValidationException e) {
245 throw new ValidationException(e);
247 } catch (ServiceException e) {
249 throw new ServiceException(e);
251 } catch (DatabaseException e) {
253 throw new ServiceException(INTERNAL_ERROR_STRING, e);
259 final public String getPossibleURI(final Resource resource) throws ValidationException, ServiceException {
261 assert (resource != null);
265 return syncRequest(new org.simantics.db.common.uri.ResourceToPossibleURI(resource));
267 } catch (ValidationException e) {
269 throw new ValidationException(e);
271 } catch (ServiceException e) {
273 throw new ServiceException(e);
275 } catch (DatabaseException e) {
277 throw new ServiceException(INTERNAL_ERROR_STRING, e);
283 final public Resource getResource(final String id)
284 throws ResourceNotFoundException, ValidationException,
291 Integer rid = QueryCache.resultURIToResource(this, id, parent, null);
292 // FIXME: stupid to throw this here and catch and wrap it right away
293 if(rid == 0) throw new ResourceNotFoundException(id);
294 return processor.querySupport.getResource(rid);
296 } catch (ResourceNotFoundException e) {
298 throw new ResourceNotFoundException(id, e);
300 } catch (ValidationException e) {
302 throw new ValidationException(e);
304 } catch (ServiceException e) {
306 throw new ServiceException(e);
308 } catch (DatabaseException e) {
310 throw new ServiceException(INTERNAL_ERROR_STRING, e);
316 final public Resource getPossibleResource(final String id)
317 throws ResourceNotFoundException, ValidationException,
324 return getResource(id);
326 } catch (ResourceNotFoundException e) {
330 } catch (ValidationException e) {
332 throw new ValidationException(e);
334 } catch (ServiceException e) {
336 throw new ServiceException(e);
338 } catch (DatabaseException e) {
340 throw new ServiceException(INTERNAL_ERROR_STRING, e);
347 public Map<String, Resource> getChildren(Resource resource) throws ValidationException, ServiceException {
349 assert (resource != null);
353 int rId = processor.querySupport.getId(resource);
354 return QueryCache.resultChildMap(this, rId, parent, null);
356 } catch (ValidationException e) {
358 throw new ValidationException(e);
360 } catch (ServiceException e) {
362 throw new ServiceException(e);
364 } catch (DatabaseException e) {
366 throw new ServiceException(INTERNAL_ERROR_STRING, e);
372 final public Resource getRootLibrary() {
373 return processor.getRootLibraryResource();
376 final public Resource getBuiltin(final String id)
377 throws ResourceNotFoundException, ServiceException {
383 return syncRequest(new Builtin(id));
385 } catch (ResourceNotFoundException e) {
387 throw new ResourceNotFoundException(id, e);
389 } catch (ServiceException e) {
391 throw new ServiceException(e);
393 } catch (DatabaseException e) {
395 throw new ServiceException(INTERNAL_ERROR_STRING, e);
401 static class StatementReadProcedure extends TIntArrayListInternal implements StatementProcedure {
403 private static Throwable DONE = new Throwable();
405 Throwable exception = null;
407 final ResourceSupport support;
409 public StatementReadProcedure(ResourceSupport support) {
410 this.support = support;
414 public synchronized void execute(AsyncReadGraph graph, int s, int p, int o) {
421 public void finished(AsyncReadGraph graph) {
426 public void exception(AsyncReadGraph graph, Throwable t) {
430 public void checkAndThrow() throws DatabaseException {
431 if(exception != DONE) {
432 if (exception instanceof DatabaseException)
433 throw (DatabaseException) exception;
435 throw new DatabaseException(
436 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
441 public boolean done() {
442 return exception != null;
446 public boolean contains(Object obj) {
447 if(!(obj instanceof InternalStatement))
449 InternalStatement statement = (InternalStatement)obj;
453 for(int i=0;i<sizeInternal();i+=3)
454 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
459 @SuppressWarnings("unchecked")
461 public <T> T[] toArray(T[] a) {
462 int length = sizeInternal() / 3;
463 if(length > a.length) {
464 Class<?> arrayType = a.getClass();
465 a = (arrayType == Object[].class)
466 ? (T[]) new Object[length]
467 : (T[]) Array.newInstance(arrayType.getComponentType(), length);
470 for(int i=length;i<a.length;++i)
473 for(int i=0,j=0;i<sizeInternal();i+=3,++j)
474 a[j] = (T)new InternalStatement(support, getQuick(i), getQuick(i+1), getQuick(i+2));
479 public boolean add(Statement e) {
480 throw new UnsupportedOperationException();
484 public boolean remove(Object o) {
485 throw new UnsupportedOperationException();
489 public boolean addAll(Collection<? extends Statement> c) {
490 throw new UnsupportedOperationException();
493 class IteratorImpl implements ListIterator<Statement> {
497 public IteratorImpl(int index) {
502 public boolean hasNext() {
503 return index < sizeInternal();
507 public Statement next() {
508 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
514 public void remove() {
515 throw new Error("Not supported");
519 public boolean hasPrevious() {
524 public Statement previous() {
526 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
531 public int nextIndex() {
536 public int previousIndex() {
541 public void set(Statement e) {
542 throw new UnsupportedOperationException();
546 public void add(Statement e) {
547 throw new UnsupportedOperationException();
553 public Iterator<Statement> iterator() {
554 return new IteratorImpl(0);
559 return sizeInternal() / 3;
563 public Object[] toArray() {
564 Object[] result = new Object[sizeInternal() / 3];
565 for(int i=0,j=0;j<sizeInternal();i++,j+=3)
566 result[i] = new InternalStatement(support, getQuick(j), getQuick(j+1), getQuick(j+2));
571 public boolean addAll(int index, Collection<? extends Statement> c) {
572 throw new UnsupportedOperationException();
576 public Statement get(int index) {
578 if(index < 0 || index >= sizeInternal())
579 throw new IndexOutOfBoundsException();
580 return new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
584 public Statement set(int index, Statement element) {
585 throw new UnsupportedOperationException();
589 public void add(int index, Statement element) {
590 throw new UnsupportedOperationException();
594 public Statement remove(int index) {
595 throw new UnsupportedOperationException();
599 public int indexOf(Object obj) {
600 if(!(obj instanceof InternalStatement))
602 InternalStatement statement = (InternalStatement)obj;
606 for(int i=0;i<sizeInternal();i+=3)
607 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
613 public int lastIndexOf(Object obj) {
614 if(!(obj instanceof InternalStatement))
616 InternalStatement statement = (InternalStatement)obj;
620 for(int i=sizeInternal()-3;i>=0;i-=3)
621 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
627 public ListIterator<Statement> listIterator() {
628 return new IteratorImpl(0);
632 public ListIterator<Statement> listIterator(int index) {
633 return new IteratorImpl(index*3);
637 public List<Statement> subList(int fromIndex, int toIndex) {
638 if(fromIndex < 0 || toIndex*3 >= sizeInternal() || fromIndex > toIndex)
639 throw new IndexOutOfBoundsException();
640 return new RandomAccessSubList<Statement>(this, fromIndex, toIndex-fromIndex);
645 final public Collection<Statement> getStatements(final Resource subject,
646 final Resource relation)
647 throws ManyObjectsForFunctionalRelationException, ServiceException {
649 assert (subject != null);
650 assert (relation != null);
654 StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
655 processor.forEachStatement(this, subject, relation, procedure);
656 procedure.checkAndThrow();
659 } catch (DatabaseException e) {
661 System.err.println(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation);
663 StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
664 processor.forEachStatement(this, subject, relation, procedure);
666 return Collections.emptyList();
668 // throw new ServiceException(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation, e);
675 final public Collection<Statement> getAssertedStatements(final Resource subject, final Resource relation)
676 throws ManyObjectsForFunctionalRelationException, ServiceException {
678 assert (subject != null);
679 assert (relation != null);
683 return syncRequest(new ForEachAssertedStatement(subject, relation));
685 } catch (ManyObjectsForFunctionalRelationException e) {
687 throw new ManyObjectsForFunctionalRelationException(e);
689 } catch (ServiceException e) {
691 throw new ServiceException(e);
693 } catch (DatabaseException e) {
695 throw new ServiceException(INTERNAL_ERROR_STRING, e);
702 final public Collection<Resource> getPredicates(final Resource subject) throws ServiceException {
704 assert (subject != null);
708 return processor.getPredicates(this, subject);
710 // AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
711 // processor.forEachPredicate(this, subject, procedure);
712 // procedure.checkAndThrow();
715 } catch (ServiceException e) {
717 throw new ServiceException(e);
719 } catch (DatabaseException e) {
721 throw new ServiceException(INTERNAL_ERROR_STRING, e);
723 } catch (Throwable e) {
725 throw new ServiceException(e);
732 final public Collection<Resource> getPrincipalTypes(final Resource subject)
733 throws ServiceException {
735 assert (subject != null);
739 AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
740 processor.forEachPrincipalType(this, subject, procedure);
741 procedure.checkAndThrow();
744 } catch (ServiceException e) {
746 throw new ServiceException(e);
748 } catch (DatabaseException e) {
750 throw new ServiceException(INTERNAL_ERROR_STRING, e);
757 final public Set<Resource> getTypes(final Resource subject) throws ServiceException {
759 assert (subject != null);
763 return processor.getTypes(this, subject);
765 } catch (ServiceException e) {
767 throw new ServiceException(e);
769 } catch (DatabaseException e) {
771 throw new ServiceException(INTERNAL_ERROR_STRING, e);
773 } catch (Throwable e) {
775 throw new ServiceException(e);
782 final public Set<Resource> getSupertypes(final Resource subject)
783 throws ServiceException {
785 assert (subject != null);
789 SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
790 processor.forSupertypes(this, subject, procedure);
791 procedure.checkAndThrow();
792 return procedure.result;
794 } catch (ServiceException e) {
796 throw new ServiceException(e);
798 } catch (DatabaseException e) {
800 throw new ServiceException(INTERNAL_ERROR_STRING, e);
807 final public Set<Resource> getSuperrelations(final Resource subject)
808 throws ServiceException {
810 assert (subject != null);
814 SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
815 processor.forSuperrelations(this, subject, procedure);
816 procedure.checkAndThrow();
817 return procedure.result;
819 } catch (ServiceException e) {
821 throw new ServiceException(e);
823 } catch (DatabaseException e) {
825 throw new ServiceException(INTERNAL_ERROR_STRING, e);
832 public Resource getPossibleSuperrelation(Resource subject) throws ServiceException {
836 SyncReadProcedure<Resource> procedure = new SyncReadProcedure<Resource>();
837 processor.forPossibleSuperrelation(this, subject, procedure);
838 procedure.checkAndThrow();
839 return procedure.result;
841 } catch (ServiceException e) {
843 throw new ServiceException(e);
845 } catch (DatabaseException e) {
847 throw new ServiceException(INTERNAL_ERROR_STRING, e);
854 final public Collection<Resource> getObjects(final Resource subject, final Resource relation)
855 throws ServiceException {
857 assert (subject != null);
858 assert (relation != null);
860 if(Development.DEVELOPMENT) {
861 if(Development.isTrue(DevelopmentKeys.READGRAPH_COUNT)) {
862 counters.adjustOrPutValue("objects $" + subject.getResourceId() + " $" + relation.getResourceId(), 1, 1);
864 //if(subject.getResourceId()==xx && relation.getResourceId()==xx) new Exception().printStackTrace();
869 AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
870 processor.forEachObject(this, subject, relation, procedure);
871 procedure.checkAndThrow();
874 } catch (DatabaseException e) {
876 throw new ServiceException(INTERNAL_ERROR_STRING, e);
883 final public Collection<Resource> getAssertedObjects(
884 final Resource subject, final Resource relation)
885 throws ManyObjectsForFunctionalRelationException, ServiceException {
888 throw new ArgumentException("Subject must not be null.");
889 if (relation == null)
890 throw new ArgumentException("Relation must not be null. Subject=" + subject);
894 return syncRequest(new ForEachAssertedObject(subject, relation));
896 } catch (ManyObjectsForFunctionalRelationException e) {
898 throw new ManyObjectsForFunctionalRelationException(e);
900 } catch (ServiceException e) {
902 throw new ServiceException(e);
904 } catch (DatabaseException e) {
906 throw new ServiceException(INTERNAL_ERROR_STRING, e);
913 final public Resource getInverse(final Resource relation) throws NoInverseException, ServiceException {
915 assert (relation != null);
919 return getSingleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
921 } catch (NoSingleResultException e) {
923 throw new NoInverseException(e);
925 } catch (ServiceException e) {
927 throw new ServiceException(e);
934 final public Resource getSingleObject(final Resource subject, final Resource relation) throws NoSingleResultException, ServiceException {
936 if( subject == null) throw new IllegalArgumentException("subject can not be null");
937 if( relation == null) throw new IllegalArgumentException("relation can not be null");
940 int single = processor.getSingleObject(this, subject, relation);
942 if (EMPTY_RESOURCE_CHECK) {
943 if (!hasStatement(subject)) {
944 throw new EmptyResourceException("Resource " + debugString(subject));
947 throw new NoSingleResultException("No single object for subject " + debugString(subject)
948 + " and relation " + debugString(relation), single);
950 return processor.querySupport.getResource(single);
951 } catch (NoSingleResultException e) {
953 } catch (DatabaseException e) {
954 throw new ServiceException(e);
959 final public Statement getSingleStatement(final Resource subject, final Resource relation) throws NoSingleResultException, ManyObjectsForFunctionalRelationException, ServiceException {
960 assert (subject != null);
961 assert (relation != null);
963 Collection<Statement> statements = getStatements(subject, relation);
964 if (statements.size() == 1) {
965 return statements.iterator().next();
967 if (EMPTY_RESOURCE_CHECK)
968 if (!hasStatement(subject))
969 throw new EmptyResourceException("Resource " + debugString(subject));
970 throw new NoSingleResultException("No single statement for subject " + debugString(subject)
971 + " and relation " + debugString(relation), statements.size());
973 } catch (ServiceException e) {
974 throw new ServiceException(e);
979 final public Resource getSingleType(final Resource subject) throws NoSingleResultException, ServiceException {
980 assert (subject != null);
982 ArrayList<Resource> principalTypes = (ArrayList<Resource>)getPrincipalTypes(subject);
983 if (principalTypes.size() == 1) {
984 return principalTypes.get(0);
986 throw new NoSingleResultException("No single type for subject " + debugString(subject), principalTypes.size());
988 } catch (ServiceException e) {
989 throw new ServiceException(e);
994 final public Resource getSingleType(final Resource subject,
995 final Resource baseType) throws NoSingleResultException,
998 assert (subject != null);
999 assert (baseType != null);
1002 return syncRequest(new SingleType(subject, baseType));
1003 } catch (DatabaseException e) {
1004 throw new NoSingleResultException("subject=" + subject + ", baseType=" + baseType, 0, e);
1009 final public <T> T getValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
1011 assert (subject != null);
1015 Layer0 L0 = processor.getL0(this);
1016 int object = processor.getSingleObject(this, subject, L0.HasDataType);
1017 if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
1019 if(processor.isImmutable(object)) {
1020 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1021 return getValue(subject, binding);
1023 byte[] dt = processor.getValue(this, object);
1024 if(dt == null) throw new ServiceException("No data type for " + subject);
1025 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1026 Binding binding = Bindings.getBinding(datatype);
1027 return getValue(subject, binding);
1030 } catch (IOException e) {
1032 throw new ServiceException(e);
1034 } catch (DoesNotContainValueException e) {
1036 throw new DoesNotContainValueException(e, subject);
1038 } catch (ServiceException e) {
1040 throw new ServiceException(e);
1042 } catch (DatabaseException e) {
1044 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1051 final public Variant getVariantValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
1053 assert (subject != null);
1057 Layer0 L0 = processor.getL0(this);
1058 int object = processor.getSingleObject(this, subject, L0.HasDataType);
1059 if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
1061 if(processor.isImmutable(object)) {
1062 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1063 return new Variant(binding, getValue(subject, binding));
1065 byte[] dt = processor.getValue(this, object);
1066 if(dt == null) throw new ServiceException("No data type for " + subject);
1067 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1068 Binding binding = Bindings.getBinding(datatype);
1069 return new Variant(binding, getValue(subject, binding));
1072 } catch (IOException e) {
1074 throw new ServiceException(e);
1076 } catch (DoesNotContainValueException e) {
1078 throw new DoesNotContainValueException(e, subject);
1080 } catch (ServiceException e) {
1082 throw new ServiceException(e);
1084 } catch (DatabaseException e) {
1086 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1091 static final IdentityHashMap<Binding,Serializer> serializers = new IdentityHashMap<Binding,Serializer>();
1094 serializers.put(Bindings.STRING, Bindings.STRING.serializer());
1097 final protected Serializer getSerializer(Binding binding) {
1098 return binding.serializer();
1102 final public <T> T getValue(final Resource subject, final Binding binding) throws DoesNotContainValueException, BindingException,
1105 assert (subject != null);
1109 byte[] bytes = processor.getValue(this, subject);
1110 if (bytes == null) throw new DoesNotContainValueException("No value for resource " + subject);
1112 Serializer serializer = getSerializer(binding);
1113 return (T)serializer.deserialize(bytes);
1115 } catch (DoesNotContainValueException e) {
1117 throw new DoesNotContainValueException(e);
1119 } catch (IOException e) {
1121 throw new ServiceException(e);
1123 } catch (DatabaseException e) {
1125 throw new ServiceException(e);
1127 } catch (BufferUnderflowException e) {
1128 // This is sometimes thrown when deserialize fails because wrong format.
1129 // For callers of this method this is just an service exception.
1130 throw new ServiceException(e);
1136 final public <T> T getRelatedValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1137 DoesNotContainValueException, ServiceException {
1139 assert (subject != null);
1140 assert (relation != null);
1143 Resource object = getSingleObject(subject, relation);
1144 return getValue(object);
1145 } catch (NoSingleResultException e) {
1146 throw new NoSingleResultException("No single value found for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1147 } catch (DoesNotContainValueException e) {
1149 Layer0 L0 = processor.getL0(this);
1150 Resource object = getPossibleObject(subject, relation);
1151 if(isInstanceOf(object, L0.Value)) {
1152 if(isInstanceOf(object, L0.Literal)) {
1153 throw new DoesNotContainValueException(e);
1155 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1158 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1160 } catch (DoesNotContainValueException e2) {
1162 } catch (DatabaseException e2) {
1163 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1165 } catch (ServiceException e) {
1166 throw new ServiceException(e);
1171 final public Variant getRelatedVariantValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1172 DoesNotContainValueException, ServiceException {
1174 assert (subject != null);
1175 assert (relation != null);
1178 Resource object = getSingleObject(subject, relation);
1179 return getVariantValue(object);
1180 } catch (NoSingleResultException e) {
1181 throw new NoSingleResultException("No single object for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1182 } catch (DoesNotContainValueException e) {
1184 Layer0 L0 = processor.getL0(this);
1185 Resource object = getPossibleObject(subject, relation);
1186 if(isInstanceOf(object, L0.Value)) {
1187 if(isInstanceOf(object, L0.Literal)) {
1188 throw new DoesNotContainValueException(e);
1190 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1193 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1195 } catch (DoesNotContainValueException e2) {
1197 } catch (DatabaseException e2) {
1198 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1200 } catch (ServiceException e) {
1201 throw new ServiceException(e);
1206 final public <T> T getRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1207 throws NoSingleResultException, DoesNotContainValueException, BindingException, ServiceException {
1209 assert (subject != null);
1210 assert (relation != null);
1213 Resource object = getSingleObject(subject, relation);
1214 return getValue(object, binding);
1215 } catch (NoSingleResultException e) {
1216 String message = "";
1218 String subjectName = NameUtils.getSafeName(this, subject, true);
1219 String relationName = NameUtils.getSafeName(this, relation, true);
1220 message = "Subject: " + subjectName + ", Relation: " + relationName;
1221 } catch (DatabaseException e2) {
1224 throw new NoSingleResultException(message, e.getResultCount(), e);
1225 } catch (DoesNotContainValueException e) {
1226 throw new DoesNotContainValueException(e);
1227 } catch (ServiceException e) {
1228 throw new ServiceException(e);
1233 final public <T> T adapt(final Resource resource, final Class<T> clazz)
1234 throws AdaptionException, ValidationException, ServiceException {
1236 assert (resource != null);
1237 assert (clazz != null);
1241 return syncRequest(new Adapter<T>(resource, clazz));
1243 } catch (AdaptionException e) {
1245 throw new AdaptionException(e);
1247 } catch (ValidationException e) {
1249 throw new ValidationException(e);
1251 } catch (ServiceException e) {
1253 throw new ServiceException(e);
1255 } catch (DatabaseException e) {
1257 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1264 final public <T,C> T adaptContextual(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1265 throws AdaptionException, ValidationException, ServiceException {
1267 assert (resource != null);
1268 assert (context != null);
1270 class ContextualAdapter implements AsyncRead<T> {
1272 final private Resource resource;
1273 final private C context;
1274 final private Class<T> clazz;
1277 public int hashCode() {
1278 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1282 final public int threadHash() {
1283 return resource.getThreadHash();
1287 public boolean equals(Object object) {
1290 else if (object == null)
1292 else if (getClass() != object.getClass())
1294 ContextualAdapter r = (ContextualAdapter)object;
1295 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1299 public int getFlags() {
1303 public ContextualAdapter(Resource resource, C context, Class<T> clazz) {
1304 this.resource = resource;
1305 this.context = context;
1310 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1312 final AdaptionService service = getSession().peekService(AdaptionService.class);
1313 if (service == null)
1314 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1316 service.adapt(graph, resource, context, contextClass, clazz, false, procedure);
1321 public String toString() {
1322 return "Adapter for (" + resource + "," + context + ") as " + clazz.getName();
1329 return syncRequest(new ContextualAdapter(resource, context, clazz));
1331 } catch (AdaptionException e) {
1333 throw new AdaptionException(e);
1335 } catch (ValidationException e) {
1337 throw new ValidationException(e);
1339 } catch (ServiceException e) {
1341 throw new ServiceException(e);
1343 } catch (DatabaseException e) {
1345 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1352 final public <T> T adaptRelated(final Resource resource, final Resource relation, final Class<T> clazz)
1353 throws AdaptionException, NoSingleResultException, ValidationException, ServiceException {
1355 assert (resource != null);
1356 assert (clazz != null);
1358 Statement stm = getSingleStatement(resource, relation);
1360 return adaptContextual(stm.getObject(), new RelationContextImpl(resource, stm), RelationContext.class, clazz);
1365 final public <T> T getPossibleRelatedAdapter(final Resource resource, final Resource relation, final Class<T> clazz)
1366 throws ValidationException, ServiceException {
1369 return adaptRelated(resource, relation, clazz);
1370 } catch (DatabaseException e) {
1377 final public <T,C> T getPossibleContextualAdapter(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1378 throws ValidationException, ServiceException {
1380 assert (resource != null);
1381 assert (context != null);
1383 class PossibleContextualAdapter implements AsyncRead<T> {
1385 final private Resource resource;
1386 final private C context;
1387 final private Class<T> clazz;
1390 public int hashCode() {
1391 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1395 final public int threadHash() {
1396 return resource.getThreadHash();
1400 public boolean equals(Object object) {
1403 else if (object == null)
1405 else if (getClass() != object.getClass())
1407 PossibleContextualAdapter r = (PossibleContextualAdapter)object;
1408 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1412 public int getFlags() {
1416 public PossibleContextualAdapter(Resource resource, C context, Class<T> clazz) {
1417 this.resource = resource;
1418 this.context = context;
1423 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1425 final AdaptionService service = getSession().peekService(AdaptionService.class);
1426 if (service == null)
1427 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1429 service.adapt(graph, resource, context, contextClass, clazz, true, procedure);
1434 public String toString() {
1435 return "Possible adapter for (" + resource + "," + context + ") as " + clazz.getName();
1442 return syncRequest(new PossibleContextualAdapter(resource, context, clazz));
1444 } catch (ValidationException e) {
1446 throw new ValidationException(e);
1448 } catch (ServiceException e) {
1450 throw new ServiceException(e);
1452 } catch (DatabaseException e) {
1454 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1461 final public <T> T adaptUnique(final Resource resource, final Class<T> clazz)
1462 throws AdaptionException, ValidationException, ServiceException {
1464 assert (resource != null);
1465 assert (clazz != null);
1469 return syncRequest(new UniqueAdapter<T>(resource, clazz));
1471 } catch (AdaptionException e) {
1473 throw new AdaptionException(e);
1475 } catch (ValidationException e) {
1477 throw new ValidationException(e);
1479 } catch (ServiceException e) {
1481 throw new ServiceException(e);
1483 } catch (DatabaseException e) {
1485 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1492 final public Resource getPossibleInverse(final Resource relation)
1493 throws ServiceException {
1495 assert (relation != null);
1499 return getPossibleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
1501 } catch (ServiceException e) {
1503 throw new ServiceException(e);
1505 } catch (DatabaseException e) {
1507 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1514 public Resource getPossibleObject(final Resource subject, final Resource relation)
1515 throws ManyObjectsForFunctionalRelationException, ServiceException {
1517 assert (subject != null);
1518 assert (relation != null);
1522 int result = processor.getSingleObject(this, subject, relation);
1523 if(result == 0) return null;
1525 return processor.querySupport.getResource(result);
1527 } catch (ManyObjectsForFunctionalRelationException e) {
1529 throw new ManyObjectsForFunctionalRelationException("subject=" + subject + ", relation=" + relation, e);
1531 } catch (DatabaseException e) {
1533 throw new ServiceException(e);
1540 final public Statement getPossibleStatement(final Resource subject, final Resource relation)
1541 throws ManyObjectsForFunctionalRelationException, ServiceException {
1543 assert (subject != null);
1544 assert (relation != null);
1548 Collection<Statement> statements = getStatements(subject, relation);
1549 if(statements.size() == 1) return statements.iterator().next();
1552 } catch (ManyObjectsForFunctionalRelationException e) {
1554 throw new ManyObjectsForFunctionalRelationException("Many objects in " + subject + " for functional relation " + relation);
1556 } catch (ServiceException e) {
1558 throw new ServiceException(e);
1565 final public Resource getPossibleType(final Resource subject, final Resource baseType) throws ServiceException {
1567 assert (subject != null);
1568 assert (baseType != null);
1572 AsyncReadProcedure<Resource> procedure = new AsyncReadProcedure<Resource>();
1573 forPossibleType(subject, baseType, procedure);
1574 procedure.checkAndThrow();
1575 return procedure.result;
1577 } catch (ServiceException e) {
1579 throw new ServiceException(e);
1581 } catch (DatabaseException e) {
1583 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1590 final public <T> T getPossibleValue(final Resource subject) throws ServiceException {
1592 assert (subject != null);
1596 int object = processor.getSingleObject(this, subject, processor.getL0(this).HasDataType);
1597 if(object == 0) return null;
1599 if(processor.isImmutable(object)) {
1600 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1601 return getPossibleValue(subject, binding);
1603 byte[] dt = processor.getValue(this, object);
1604 if(dt == null) return null;
1605 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1606 Binding binding = Bindings.getBinding(datatype);
1607 return getPossibleValue(subject, binding);
1610 } catch (IOException e) {
1612 throw new ServiceException(e);
1614 } catch (ServiceException e) {
1616 throw new ServiceException(e);
1618 } catch (DatabaseException e) {
1620 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1627 final public <T> T getPossibleValue(final Resource subject, final Binding binding) throws BindingException, ServiceException {
1629 assert (subject != null);
1630 assert (binding != null);
1634 byte[] dt = processor.getValue(this, subject);
1635 if(dt == null) return null;
1636 Serializer serializer = getSerializer(binding);
1637 return (T)serializer.deserialize(dt);
1639 } catch (IOException e) {
1641 throw new ServiceException(e);
1643 } catch (BindingException e) {
1645 throw new BindingException(e);
1647 } catch (ServiceException e) {
1649 throw new ServiceException(e);
1651 } catch (DatabaseException e) {
1652 e.printStackTrace();
1653 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1659 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation)
1660 throws ManyObjectsForFunctionalRelationException, ServiceException {
1662 assert (subject != null);
1663 assert (relation != null);
1667 Resource object = getPossibleObject(subject, relation);
1668 if(object == null) return null;
1669 else return getPossibleValue(object);
1671 } catch (ManyObjectsForFunctionalRelationException e) {
1673 throw new ManyObjectsForFunctionalRelationException(e);
1675 } catch (ServiceException e) {
1677 throw new ServiceException(e);
1684 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1685 throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
1687 assert (subject != null);
1688 assert (relation != null);
1689 assert (binding != null);
1693 Resource object = getPossibleObject(subject, relation);
1694 if(object == null) return null;
1695 else return getPossibleValue(object, binding);
1697 } catch (ManyObjectsForFunctionalRelationException e) {
1699 throw new ManyObjectsForFunctionalRelationException(e);
1701 } catch (BindingException e) {
1703 throw new BindingException(e);
1705 } catch (ServiceException e) {
1707 throw new ServiceException(e);
1714 public <T> T getPossibleAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1716 assert (resource != null);
1717 assert (clazz != null);
1721 return syncRequest(new PossibleAdapter<T>(resource, clazz));
1723 } catch (ValidationException e) {
1725 throw new ValidationException(e);
1727 } catch (AdaptionException e) {
1731 } catch (DatabaseException e) {
1733 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1739 public <T> T getPossibleUniqueAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1741 assert (resource != null);
1742 assert (clazz != null);
1746 return syncRequest(new PossibleUniqueAdapter<T>(resource, clazz));
1748 } catch (AdaptionException e) {
1752 } catch (ValidationException e) {
1754 throw new ValidationException(e);
1756 } catch (DatabaseException e) {
1758 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1765 final public boolean isInstanceOf(final Resource resource, final Resource type) throws ServiceException {
1767 assert (resource != null);
1768 assert (type != null);
1770 Set<Resource> resources = getTypes(resource);
1771 // This check was necessary because some of the callers of this method got stuck when the NPE was thrown from here.
1772 if (null == resources)
1775 if(EMPTY_RESOURCE_CHECK) {
1776 if (resources.isEmpty()) {
1777 if(!hasStatement(resource)) throw new EmptyResourceException("Resource " + debugString(resource));
1781 return resources.contains(type);
1786 final public boolean isInheritedFrom(final Resource resource, final Resource type) throws ServiceException {
1788 assert (resource != null);
1789 assert (type != null);
1793 if(resource.equals(type)) return true;
1795 return getSupertypes(resource).contains(type);
1797 } catch (ServiceException e) {
1799 throw new ServiceException(e);
1806 final public boolean isSubrelationOf(final Resource resource, final Resource type) throws ServiceException {
1808 assert (resource != null);
1809 assert (type != null);
1813 if(resource.equals(type)) return true;
1815 return getSuperrelations(resource).contains(type);
1817 } catch (ServiceException e) {
1819 throw new ServiceException(e);
1826 final public boolean hasStatement(final Resource subject) throws ServiceException {
1828 assert (subject != null);
1832 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1833 processor.forHasStatement(this, subject, procedure);
1834 procedure.checkAndThrow();
1835 return procedure.result;
1837 } catch (ServiceException e) {
1839 throw new ServiceException(e);
1841 } catch (DatabaseException e) {
1843 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1850 final public boolean hasStatement(final Resource subject, final Resource relation) throws ServiceException {
1852 assert (subject != null);
1853 assert (relation != null);
1857 Collection<Resource> objects = getObjects(subject, relation);
1858 return !objects.isEmpty();
1860 } catch (ServiceException e) {
1862 throw new ServiceException(e);
1869 final public boolean hasStatement(final Resource subject, final Resource relation, final Resource object) throws ServiceException {
1871 assert (subject != null);
1872 assert (relation != null);
1873 assert (object != null);
1877 for(Resource o : getObjects(subject, relation)) {
1878 if(object.equals(o)) return true;
1883 } catch (ServiceException e) {
1885 throw new ServiceException(e);
1892 final public boolean hasValue(final Resource subject) throws ServiceException {
1894 assert (subject != null);
1898 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1899 processor.forHasValue(this, subject, procedure);
1900 procedure.checkAndThrow();
1901 return procedure.result;
1903 } catch (ServiceException e) {
1905 throw new ServiceException(e);
1907 } catch (DatabaseException e) {
1909 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1915 final AsyncProcedure<?> NONE = new AsyncProcedure<Object>() {
1918 public void execute(AsyncReadGraph graph, Object result) {
1922 public void exception(AsyncReadGraph graph, Throwable throwable) {
1928 * Implementation of the interface RequestProcessor
1932 public <T> T syncRequest(final Read<T> request) throws DatabaseException {
1934 assert (request != null);
1936 return QueryCache.resultReadEntry(this, request, parent, null, null);
1941 public <T> T syncRequest(Read<T> request, SyncListener<T> procedure)
1942 throws DatabaseException {
1943 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
1947 public <T> T syncRequest(Read<T> request, final Listener<T> procedure)
1948 throws DatabaseException {
1949 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
1953 public <T> T syncRequest(final Read<T> request, final AsyncProcedure<T> procedure) throws DatabaseException {
1955 assert (request != null);
1957 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
1959 return QueryCache.resultReadEntry(this, request, parent, listener, procedure);
1964 public <T> T syncRequest(final Read<T> request,
1965 final SyncProcedure<T> procedure) throws DatabaseException {
1966 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
1970 public <T> T syncRequest(Read<T> request, Procedure<T> procedure)
1971 throws DatabaseException {
1972 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
1975 static class AsyncReadProcedure<T> implements AsyncProcedure<T> {
1977 private static Throwable DONE = new Throwable();
1980 Throwable exception = null;
1983 public void execute(AsyncReadGraph graph, T t) {
1989 public void exception(AsyncReadGraph graph, Throwable t) {
1993 public void checkAndThrow() throws DatabaseException {
1994 if(exception != DONE) {
1995 if (exception instanceof DatabaseException)
1996 throw (DatabaseException) exception;
1998 throw new DatabaseException(
1999 "Unexpected exception in ReadGraph.syncRequest(AsyncRead)",
2004 public boolean done() {
2005 return exception != null;
2011 public <T> T syncRequest(final AsyncRead<T> request)
2012 throws DatabaseException {
2014 assert (request != null);
2015 asyncBarrier = new AsyncBarrierImpl(null);
2016 BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, null, request);
2017 syncRequest(request, ap);
2023 public <T> T syncRequest(AsyncRead<T> request, AsyncListener<T> procedure)
2024 throws DatabaseException {
2025 return syncRequest(request, (AsyncProcedure<T>) procedure);
2029 public <T> T syncRequest(AsyncRead<T> request, SyncListener<T> procedure)
2030 throws DatabaseException {
2031 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
2035 public <T> T syncRequest(AsyncRead<T> request, Listener<T> procedure)
2036 throws DatabaseException {
2037 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
2041 final public <T> T syncRequest(final AsyncRead<T> request,
2042 final AsyncProcedure<T> procedure) throws DatabaseException {
2044 assert (request != null);
2046 ListenerBase listener = getListenerBase(procedure);
2048 BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, procedure, request);
2050 QueryCache.runnerAsyncReadEntry(this, request, parent, listener, ap, true);
2056 final private <T> void syncRequest(final AsyncRead<T> request, final AsyncReadProcedure<T> procedure) throws DatabaseException {
2058 assert (request != null);
2060 ListenerBase listener = getListenerBase(procedure);
2061 assert(listener == null);
2063 BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, procedure, request);
2065 QueryCache.runnerAsyncReadEntry(this, request, parent, listener, ap, true);
2072 public <T> T syncRequest(AsyncRead<T> request,
2073 final SyncProcedure<T> procedure) throws DatabaseException {
2074 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2078 final public <T> T syncRequest(final AsyncRead<T> request,
2079 final Procedure<T> procedure) throws DatabaseException {
2080 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2084 public <T> Collection<T> syncRequest(final MultiRead<T> request)
2085 throws DatabaseException {
2087 assert (request != null);
2089 final ArrayList<T> result = new ArrayList<T>();
2090 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2092 syncRequest(request, new SyncMultiProcedure<T>() {
2095 public void execute(ReadGraph graph, T t) {
2096 synchronized (result) {
2102 public void finished(ReadGraph graph) {
2106 public void exception(ReadGraph graph, Throwable t) {
2111 public String toString() {
2112 return "syncRequest(MultiRead) -> " + request;
2117 Throwable t = exception.get();
2119 if (t instanceof DatabaseException)
2120 throw (DatabaseException) t;
2122 throw new DatabaseException(
2123 "Unexpected exception in ReadGraph.syncRequest(Read)",
2132 public <T> Collection<T> syncRequest(MultiRead<T> request,
2133 SyncMultiListener<T> procedure) {
2134 return syncRequest(request, (SyncMultiProcedure<T>)procedure);
2138 public <T> Collection<T> syncRequest(MultiRead<T> request,
2139 MultiListener<T> procedure) {
2140 return syncRequest(request, new NoneToSyncMultiListener<T>(procedure));
2144 public <T> Collection<T> syncRequest(MultiRead<T> request,
2145 SyncMultiProcedure<T> procedure) {
2147 assert (request != null);
2149 ListenerBase listener = getListenerBase(procedure);
2151 final ResultCallWrappedSyncQueryProcedure<T> wrapper = new ResultCallWrappedSyncQueryProcedure<T>(procedure);
2153 if (parent != null || listener != null) {
2155 // Object syncParent = request;
2157 // final ReadGraphImpl newGraph = newSync();
2159 processor.query(this, request, parent, wrapper, listener);
2161 // newGraph.waitAsync(syncParent);
2165 // Object syncParent = request;
2167 // final ReadGraphImpl newGraph = newSync();
2170 request.perform(this, wrapper);
2171 } catch (Throwable t) {
2172 wrapper.exception(this, t);
2177 return wrapper.get();
2182 public <T> Collection<T> syncRequest(MultiRead<T> request,
2183 MultiProcedure<T> procedure) {
2184 return syncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
2187 static class AsyncMultiReadProcedure<T> extends ArrayList<T> implements AsyncMultiProcedure<T> {
2189 private static Throwable DONE = new Throwable();
2191 private static final long serialVersionUID = -6494230465108115812L;
2193 Throwable exception = null;
2196 public synchronized void execute(AsyncReadGraph graph, T t) {
2201 public void finished(AsyncReadGraph graph) {
2206 public void exception(AsyncReadGraph graph, Throwable t) {
2210 public void checkAndThrow() throws DatabaseException {
2211 if(exception != DONE) {
2212 if (exception instanceof DatabaseException)
2213 throw (DatabaseException) exception;
2215 throw new DatabaseException(
2216 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
2221 public boolean done() {
2222 return exception != null;
2228 final public <T> Collection<T> syncRequest(AsyncMultiRead<T> request)
2229 throws DatabaseException {
2231 assert (request != null);
2233 final AsyncMultiReadProcedure<T> procedure = new AsyncMultiReadProcedure<T>();
2235 syncRequest(request, procedure);
2237 procedure.checkAndThrow();
2243 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2244 AsyncMultiListener<T> procedure) {
2245 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2249 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2250 SyncMultiListener<T> procedure) {
2251 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2255 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2256 MultiListener<T> procedure) {
2257 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2260 final private <T> void syncRequest(final AsyncMultiRead<T> request,
2261 final AsyncMultiReadProcedure<T> procedure) {
2263 assert (request != null);
2264 assert (procedure != null);
2266 ListenerBase listener = getListenerBase(procedure);
2268 if (parent != null || listener != null) {
2270 // Object syncParent = request;
2272 // final ReadGraphImpl newGraph = newSync();
2274 processor.query(this, request, parent, procedure, listener);
2276 // newGraph.waitAsync(syncParent);
2277 waitAsyncProcedure(procedure);
2281 // Object syncParent = callerThread == Integer.MIN_VALUE ? null
2284 // final ReadGraphImpl newGraph = newSyncAsync(syncParent);
2289 // ReadGraphImpl sync = newSync();
2290 request.perform(this, procedure);
2291 // sync.waitAsync(null);
2292 waitAsyncProcedure(procedure);
2295 } catch (Throwable t) {
2297 waitAsyncProcedure(procedure);
2308 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2309 final AsyncMultiProcedure<T> procedure) {
2311 assert (request != null);
2312 assert (procedure != null);
2314 ListenerBase listener = getListenerBase(procedure);
2316 if (parent != null || listener != null) {
2318 // Object syncParent = request;
2320 // final ReadGraphImpl newGraph = newSync();
2322 processor.query(this, request, parent, procedure, listener);
2324 // newGraph.waitAsync(syncParent);
2328 // Object syncParent = request;
2330 // final ReadGraphImpl newGraph = newSync();
2334 request.perform(this, new AsyncMultiProcedure<T>() {
2337 public void execute(AsyncReadGraph graph, T result) {
2338 procedure.execute(graph, result);
2342 public void finished(AsyncReadGraph graph) {
2343 procedure.finished(graph);
2347 public void exception(AsyncReadGraph graph, Throwable t) {
2348 procedure.exception(graph, t);
2352 public String toString() {
2353 return "syncRequest(AsyncMultiRead) -> " + procedure;
2358 } catch (Throwable t) {
2370 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2371 final SyncMultiProcedure<T> procedure) {
2372 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2376 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2377 final MultiProcedure<T> procedure) {
2378 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2382 public <T> T syncRequest(final ExternalRead<T> request)
2383 throws DatabaseException {
2385 assert (request != null);
2387 return syncRequest(request, new Procedure<T>() {
2390 public void execute(T t) {
2394 public void exception(Throwable t) {
2398 public String toString() {
2399 return "syncRequest(AsyncRead) -> " + request;
2407 public <T> T syncRequest(ExternalRead<T> request, Listener<T> procedure) throws DatabaseException {
2408 return syncRequest(request, (Procedure<T>) procedure);
2412 final public <T> T syncRequest(final ExternalRead<T> request,
2413 final Procedure<T> procedure) throws DatabaseException {
2415 assert (request != null);
2417 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
2418 return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
2423 public void syncRequest(final Write request) throws DatabaseException {
2425 assert (request != null);
2427 throw new DatabaseException(
2428 "Write operations are not supported during read transactions!");
2433 public <T> T syncRequest(final WriteResult<T> request) throws DatabaseException {
2435 assert (request != null);
2437 throw new DatabaseException(
2438 "Write operations are not supported during read transactions!");
2443 public void syncRequest(final DelayedWrite request)
2444 throws DatabaseException {
2446 assert (request != null);
2448 throw new DatabaseException(
2449 "Write operations are not supported during read transactions!");
2454 public <T> T syncRequest(final DelayedWriteResult<T> request) throws DatabaseException {
2456 assert (request != null);
2458 throw new DatabaseException(
2459 "Write operations are not supported during read transactions!");
2464 public void syncRequest(final WriteOnly request) throws DatabaseException {
2466 assert (request != null);
2468 throw new DatabaseException(
2469 "Write operations are not supported during read transactions!");
2474 public <T> T syncRequest(final WriteOnlyResult<T> request) throws DatabaseException {
2476 assert (request != null);
2478 throw new DatabaseException(
2479 "Write operations are not supported during read transactions!");
2484 public <T> void async(ReadInterface<T> r, AsyncProcedure<T> procedure) {
2485 r.request(this, procedure);
2489 public <T> void async(ReadInterface<T> r, Procedure<T> procedure) {
2490 r.request(this, procedure);
2494 public <T> void async(ReadInterface<T> r, SyncProcedure<T> procedure) {
2495 r.request(this, procedure);
2499 public <T> void async(ReadInterface<T> r, AsyncListener<T> procedure) {
2500 r.request(this, procedure);
2504 public <T> void async(ReadInterface<T> r, Listener<T> procedure) {
2505 r.request(this, procedure);
2509 public <T> void async(ReadInterface<T> r, SyncListener<T> procedure) {
2510 r.request(this, procedure);
2514 public <T> T sync(ReadInterface<T> r) throws DatabaseException {
2515 return r.request(this);
2519 public <T> T sync(WriteInterface<T> r) throws DatabaseException {
2520 return r.request(this);
2524 public <T> void async(WriteInterface<T> r, Procedure<T> procedure) {
2525 r.request(this, procedure);
2529 public <T> void async(WriteInterface<T> r) {
2530 r.request(this, new ProcedureAdapter<T>());
2534 * Implementation of the interface AsyncReadGraph
2538 public void forURI(Resource resource, AsyncListener<String> listener) {
2539 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2544 public void forURI(Resource resource, SyncListener<String> listener) {
2545 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2550 public void forURI(Resource resource, Listener<String> listener) {
2551 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2556 final public void forURI(final Resource resource,
2557 final AsyncProcedure<String> procedure) {
2559 assert (resource != null);
2560 assert (procedure != null);
2562 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2568 public void forURI(Resource resource, SyncProcedure<String> procedure) {
2569 forURI(resource, new SyncToAsyncProcedure<String>(procedure));
2573 public void forURI(Resource resource, Procedure<String> procedure) {
2574 forURI(resource, new NoneToAsyncProcedure<String>(procedure));
2578 public void forResource(String id, AsyncListener<Resource> listener) {
2579 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2584 public void forResource(String id, SyncListener<Resource> listener) {
2585 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2590 public void forResource(String id, Listener<Resource> listener) {
2591 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2596 final public void forResource(final String id,
2597 final AsyncProcedure<Resource> procedure) {
2599 assert (id != null);
2600 assert (procedure != null);
2602 processor.forResource(this, id, procedure);
2607 public void forResource(String id, SyncProcedure<Resource> procedure) {
2608 forResource(id, new SyncToAsyncProcedure<Resource>(procedure));
2612 public void forResource(String id, Procedure<Resource> procedure) {
2613 forResource(id, new NoneToAsyncProcedure<Resource>(procedure));
2617 public void forBuiltin(String id, AsyncListener<Resource> listener) {
2618 asyncRequest(new Builtin(id), listener);
2622 public void forBuiltin(String id, SyncListener<Resource> listener) {
2623 asyncRequest(new Builtin(id), listener);
2627 public void forBuiltin(String id, Listener<Resource> listener) {
2628 asyncRequest(new Builtin(id), listener);
2632 final public void forBuiltin(final String id,
2633 final AsyncProcedure<Resource> procedure) {
2635 assert (id != null);
2636 assert (procedure != null);
2638 processor.forBuiltin(this, id, procedure);
2643 public void forBuiltin(String id, SyncProcedure<Resource> procedure) {
2644 forBuiltin(id, new SyncToAsyncProcedure<Resource>(procedure));
2648 public void forBuiltin(String id, Procedure<Resource> procedure) {
2649 forBuiltin(id, new NoneToAsyncProcedure<Resource>(procedure));
2653 final public void forEachStatement(Resource subject, Resource relation,
2654 AsyncMultiProcedure<Statement> procedure) {
2656 assert (subject != null);
2657 assert (relation != null);
2658 assert (procedure != null);
2660 processor.forEachStatement(this, subject, relation, procedure);
2665 public void forEachStatement(Resource subject, Resource relation,
2666 SyncMultiProcedure<Statement> procedure) {
2667 forEachStatement(subject, relation,
2668 new SyncToAsyncMultiProcedure<Statement>(procedure));
2672 final public void forEachStatement(Resource subject, Resource relation,
2673 MultiProcedure<Statement> procedure) {
2675 assert (subject != null);
2676 assert (relation != null);
2677 assert (procedure != null);
2679 processor.forEachStatement(this, subject, relation, procedure);
2684 final public void forStatementSet(Resource subject, Resource relation,
2685 AsyncSetListener<Statement> procedure) {
2687 assert (subject != null);
2688 assert (relation != null);
2689 assert (procedure != null);
2691 processor.forStatementSet(this, subject, relation, procedure);
2696 final public void forStatementSet(Resource subject, Resource relation,
2697 SyncSetListener<Statement> procedure) {
2698 forStatementSet(subject, relation,
2699 new SyncToAsyncSetProcedure<Statement>(procedure));
2703 public void forStatementSet(Resource subject, Resource relation,
2704 SetListener<Statement> listener) {
2705 forStatementSet(subject, relation,
2706 new NoneToAsyncSetProcedure<Statement>(listener));
2710 final public void forEachAssertedStatement(final Resource subject,
2711 final Resource relation,
2712 final AsyncMultiProcedure<Statement> procedure) {
2714 assert (subject != null);
2715 assert (relation != null);
2716 assert (procedure != null);
2718 processor.forEachAssertedStatement(this, subject, relation, procedure);
2723 public void forEachAssertedStatement(Resource subject, Resource relation,
2724 SyncMultiProcedure<Statement> procedure) {
2725 forEachAssertedStatement(subject, relation,
2726 new SyncToAsyncMultiProcedure<Statement>(procedure));
2730 public void forEachAssertedStatement(Resource subject, Resource relation,
2731 MultiProcedure<Statement> procedure) {
2732 forEachAssertedStatement(subject, relation,
2733 new NoneToAsyncMultiProcedure<Statement>(procedure));
2737 public void forAssertedStatementSet(Resource subject, Resource relation,
2738 AsyncSetListener<Statement> procedure) {
2740 assert (subject != null);
2741 assert (relation != null);
2742 assert (procedure != null);
2744 processor.forAssertedStatementSet(this, subject, relation, procedure);
2749 public void forAssertedStatementSet(Resource subject, Resource relation,
2750 SyncSetListener<Statement> procedure) {
2752 assert (subject != null);
2753 assert (relation != null);
2754 assert (procedure != null);
2756 forAssertedStatementSet(subject, relation,
2757 new SyncToAsyncSetProcedure<Statement>(procedure));
2762 public void forAssertedStatementSet(Resource subject, Resource relation,
2763 SetListener<Statement> procedure) {
2765 assert (subject != null);
2766 assert (relation != null);
2767 assert (procedure != null);
2769 forAssertedStatementSet(subject, relation,
2770 new NoneToAsyncSetProcedure<Statement>(procedure));
2775 final public void forEachPredicate(final Resource subject,
2776 final AsyncMultiProcedure<Resource> procedure) {
2778 assert (subject != null);
2779 assert (procedure != null);
2781 processor.forEachPredicate(this, subject, procedure);
2786 public void forEachPredicate(Resource subject,
2787 SyncMultiProcedure<Resource> procedure) {
2788 forEachPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(
2793 final public void forEachPredicate(final Resource subject,
2794 final MultiProcedure<Resource> procedure) {
2796 assert (subject != null);
2797 assert (procedure != null);
2799 processor.forEachPredicate(this, subject, procedure);
2804 final public void forPredicateSet(final Resource subject,
2805 final AsyncSetListener<Resource> procedure) {
2807 assert (subject != null);
2808 assert (procedure != null);
2810 processor.forPredicateSet(this, subject, procedure);
2815 final public void forPredicateSet(final Resource subject,
2816 final SyncSetListener<Resource> procedure) {
2818 assert (subject != null);
2819 assert (procedure != null);
2821 forPredicateSet(subject, new SyncToAsyncSetProcedure<Resource>(
2827 final public void forPredicateSet(final Resource subject,
2828 final SetListener<Resource> procedure) {
2830 assert (subject != null);
2831 assert (procedure != null);
2833 forPredicateSet(subject, new NoneToAsyncSetProcedure<Resource>(
2839 final public void forEachPrincipalType(final Resource subject,
2840 final AsyncMultiProcedure<Resource> procedure) {
2842 assert (subject != null);
2843 assert (procedure != null);
2845 processor.forEachPrincipalType(this, subject, procedure);
2850 public void forEachPrincipalType(Resource subject,
2851 SyncMultiProcedure<Resource> procedure) {
2852 forEachPrincipalType(subject, new SyncToAsyncMultiProcedure<Resource>(
2857 final public void forEachPrincipalType(final Resource subject,
2858 final MultiProcedure<Resource> procedure) {
2860 assert (subject != null);
2861 assert (procedure != null);
2863 processor.forEachPrincipalType(this, subject, procedure);
2868 final public void forPrincipalTypeSet(final Resource subject,
2869 final AsyncSetListener<Resource> procedure) {
2871 assert (subject != null);
2872 assert (procedure != null);
2874 processor.forPrincipalTypeSet(this, subject, procedure);
2879 final public void forPrincipalTypeSet(final Resource subject,
2880 final SyncSetListener<Resource> procedure) {
2882 assert (subject != null);
2883 assert (procedure != null);
2885 forPrincipalTypeSet(subject, new SyncToAsyncSetProcedure<Resource>(
2891 final public void forPrincipalTypeSet(final Resource subject,
2892 final SetListener<Resource> procedure) {
2894 assert (subject != null);
2895 assert (procedure != null);
2897 forPrincipalTypeSet(subject, new NoneToAsyncSetProcedure<Resource>(
2903 public void forTypes(Resource subject, AsyncListener<Set<Resource>> listener) {
2904 asyncRequest(new Types(subject), listener);
2908 public void forTypes(Resource subject, SyncListener<Set<Resource>> listener) {
2909 asyncRequest(new Types(subject), listener);
2913 public void forTypes(Resource subject, Listener<Set<Resource>> listener) {
2914 asyncRequest(new Types(subject), listener);
2918 final public void forTypes(final Resource subject,
2919 final AsyncProcedure<Set<Resource>> procedure) {
2921 assert (subject != null);
2922 assert (procedure != null);
2924 processor.forTypes(this, subject, procedure);
2929 public void forTypes(Resource subject,
2930 SyncProcedure<Set<Resource>> procedure) {
2931 forTypes(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
2935 public void forTypes(Resource subject, Procedure<Set<Resource>> procedure) {
2936 forTypes(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
2940 public void forSupertypes(Resource subject,
2941 AsyncListener<Set<Resource>> listener) {
2942 asyncRequest(new Types(subject), listener);
2946 public void forSupertypes(Resource subject,
2947 SyncListener<Set<Resource>> listener) {
2948 asyncRequest(new Types(subject), listener);
2952 public void forSupertypes(Resource subject, Listener<Set<Resource>> listener) {
2953 asyncRequest(new Types(subject), listener);
2957 final public void forSupertypes(final Resource subject,
2958 final AsyncProcedure<Set<Resource>> procedure) {
2960 assert (subject != null);
2961 assert (procedure != null);
2963 processor.forSupertypes(this, subject, procedure);
2968 public void forSupertypes(Resource subject,
2969 SyncProcedure<Set<Resource>> procedure) {
2970 forSupertypes(subject, new SyncToAsyncProcedure<Set<Resource>>(
2975 public void forSupertypes(Resource subject,
2976 Procedure<Set<Resource>> procedure) {
2977 forSupertypes(subject, new NoneToAsyncProcedure<Set<Resource>>(
2982 public void forDirectSuperrelations(Resource subject,
2983 AsyncMultiProcedure<Resource> procedure) {
2985 assert (subject != null);
2986 assert (procedure != null);
2988 processor.forDirectSuperrelations(this, subject, procedure);
2993 public void forPossibleSuperrelation(Resource subject, AsyncProcedure<Resource> procedure) {
2995 assert (subject != null);
2996 assert (procedure != null);
2998 processor.forPossibleSuperrelation(this, subject, procedure);
3003 public void forSuperrelations(Resource subject,
3004 AsyncListener<Set<Resource>> listener) {
3005 asyncRequest(new Types(subject), listener);
3009 public void forSuperrelations(Resource subject,
3010 SyncListener<Set<Resource>> listener) {
3011 asyncRequest(new Types(subject), listener);
3015 public void forSuperrelations(Resource subject,
3016 Listener<Set<Resource>> listener) {
3017 asyncRequest(new Types(subject), listener);
3021 final public void forSuperrelations(final Resource subject,
3022 final AsyncProcedure<Set<Resource>> procedure) {
3024 assert (subject != null);
3025 assert (procedure != null);
3027 processor.forSuperrelations(this, subject, procedure);
3032 public void forSuperrelations(Resource subject,
3033 SyncProcedure<Set<Resource>> procedure) {
3034 forSuperrelations(subject, new SyncToAsyncProcedure<Set<Resource>>(
3039 public void forSuperrelations(Resource subject,
3040 Procedure<Set<Resource>> procedure) {
3041 forSuperrelations(subject, new NoneToAsyncProcedure<Set<Resource>>(
3046 final public void forEachObject(final Resource subject, final Resource relation, final AsyncMultiProcedure<Resource> procedure) {
3047 processor.forEachObject(this, subject, relation, procedure);
3051 public void forEachObject(Resource subject, Resource relation,
3052 SyncMultiProcedure<Resource> procedure) {
3053 forEachObject(subject, relation,
3054 new SyncToAsyncMultiProcedure<Resource>(procedure));
3058 public void forEachObject(Resource subject, Resource relation,
3059 MultiProcedure<Resource> procedure) {
3061 processor.forEachObject(this, subject, relation, procedure);
3066 final public void forEachDirectPredicate(final Resource subject, final AsyncProcedure<Set<Resource>> procedure) {
3067 processor.forEachDirectPredicate(this, subject, procedure);
3071 final public void forEachDirectPredicate(final Resource subject, final SyncProcedure<Set<Resource>> procedure) {
3072 forEachDirectPredicate(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
3076 public void forEachDirectPredicate(Resource subject, Procedure<Set<Resource>> procedure) {
3077 forEachDirectPredicate(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
3081 final public void forObjectSet(final Resource subject,
3082 final Resource relation, final AsyncSetListener<Resource> procedure) {
3084 assert (subject != null);
3085 assert (relation != null);
3086 assert (procedure != null);
3088 processor.forObjectSet(this, subject, relation, procedure);
3093 final public void forObjectSet(final Resource subject,
3094 final Resource relation, final SyncSetListener<Resource> procedure) {
3096 assert (subject != null);
3097 assert (relation != null);
3098 assert (procedure != null);
3100 forObjectSet(subject, relation, new SyncToAsyncSetProcedure<Resource>(
3106 final public void forObjectSet(final Resource subject,
3107 final Resource relation, final SetListener<Resource> procedure) {
3109 assert (subject != null);
3110 assert (relation != null);
3111 assert (procedure != null);
3113 forObjectSet(subject, relation, new NoneToAsyncSetProcedure<Resource>(
3119 final public void forEachAssertedObject(final Resource subject,
3120 final Resource relation,
3121 final AsyncMultiProcedure<Resource> procedure) {
3123 assert (subject != null);
3124 assert (relation != null);
3125 assert (procedure != null);
3127 processor.forEachAssertedObject(this, subject, relation, procedure);
3132 public void forEachAssertedObject(Resource subject, Resource relation,
3133 SyncMultiProcedure<Resource> procedure) {
3135 assert (subject != null);
3136 assert (relation != null);
3137 assert (procedure != null);
3139 forEachAssertedObject(subject, relation,
3140 new SyncToAsyncMultiProcedure<Resource>(procedure));
3145 public void forEachAssertedObject(Resource subject, Resource relation,
3146 MultiProcedure<Resource> procedure) {
3148 assert (subject != null);
3149 assert (relation != null);
3150 assert (procedure != null);
3152 forEachAssertedObject(subject, relation,
3153 new NoneToAsyncMultiProcedure<Resource>(procedure));
3158 public void forAssertedObjectSet(Resource subject, Resource relation,
3159 AsyncSetListener<Resource> procedure) {
3161 assert (subject != null);
3162 assert (relation != null);
3163 assert (procedure != null);
3165 processor.forAssertedObjectSet(this, subject, relation, procedure);
3170 public void forAssertedObjectSet(Resource subject, Resource relation,
3171 SyncSetListener<Resource> procedure) {
3173 assert (subject != null);
3174 assert (relation != null);
3175 assert (procedure != null);
3177 forAssertedObjectSet(subject, relation,
3178 new SyncToAsyncSetProcedure<Resource>(procedure));
3183 public void forAssertedObjectSet(Resource subject, Resource relation,
3184 SetListener<Resource> procedure) {
3186 assert (subject != null);
3187 assert (relation != null);
3188 assert (procedure != null);
3190 forAssertedObjectSet(subject, relation,
3191 new NoneToAsyncSetProcedure<Resource>(procedure));
3196 public void forInverse(Resource relation, AsyncListener<Resource> listener) {
3197 asyncRequest(new Inverse(relation), listener);
3201 public void forInverse(Resource relation, SyncListener<Resource> listener) {
3202 asyncRequest(new Inverse(relation), listener);
3206 public void forInverse(Resource relation, Listener<Resource> listener) {
3207 asyncRequest(new Inverse(relation), listener);
3211 final public void forInverse(final Resource relation,
3212 final AsyncProcedure<Resource> procedure) {
3214 assert (relation != null);
3215 assert (procedure != null);
3217 processor.forInverse(this, relation, new AsyncProcedure<Resource>() {
3220 public void execute(AsyncReadGraph graph, Resource result) {
3222 procedure.execute(graph, result);
3224 procedure.exception(graph, new NoInverseException(relation
3230 public void exception(AsyncReadGraph graph, Throwable throwable) {
3231 procedure.exception(graph, throwable);
3235 public String toString() {
3236 return "forInverse -> " + procedure;
3244 public void forInverse(Resource relation, SyncProcedure<Resource> procedure) {
3245 forInverse(relation, new SyncToAsyncProcedure<Resource>(procedure));
3249 public void forInverse(Resource relation, Procedure<Resource> procedure) {
3250 forInverse(relation, new NoneToAsyncProcedure<Resource>(procedure));
3254 public void forSingleObject(Resource subject, Resource relation,
3255 AsyncListener<Resource> listener) {
3256 asyncRequest(new SingleObject(subject, relation), listener);
3260 public void forSingleObject(Resource subject, Resource relation,
3261 SyncListener<Resource> listener) {
3262 asyncRequest(new SingleObject(subject, relation), listener);
3266 public void forSingleObject(Resource subject, Resource relation,
3267 Listener<Resource> listener) {
3268 asyncRequest(new SingleObject(subject, relation), listener);
3272 final public void forSingleObject(final Resource subject,
3273 final Resource relation, final AsyncProcedure<Resource> procedure) {
3275 assert (subject != null);
3276 assert (relation != null);
3277 assert (procedure != null);
3279 processor.forEachObject(this, subject, relation,
3280 new SingleOrErrorProcedure<Resource>(procedure));
3285 public void forSingleObject(Resource subject, Resource relation,
3286 SyncProcedure<Resource> procedure) {
3287 forSingleObject(subject, relation, new SyncToAsyncProcedure<Resource>(
3292 public void forSingleObject(Resource subject, Resource relation,
3293 Procedure<Resource> procedure) {
3294 forSingleObject(subject, relation, new NoneToAsyncProcedure<Resource>(
3299 public void forSingleStatement(Resource subject, Resource relation,
3300 AsyncListener<Statement> listener) {
3301 asyncRequest(new SingleStatement(subject, relation), listener);
3305 public void forSingleStatement(Resource subject, Resource relation,
3306 SyncListener<Statement> listener) {
3307 asyncRequest(new SingleStatement(subject, relation), listener);
3311 public void forSingleStatement(Resource subject, Resource relation,
3312 Listener<Statement> listener) {
3313 asyncRequest(new SingleStatement(subject, relation), listener);
3317 final public void forSingleStatement(final Resource subject,
3318 final Resource relation, final AsyncProcedure<Statement> procedure) {
3320 assert (subject != null);
3321 assert (relation != null);
3322 assert (procedure != null);
3324 processor.forEachStatement(this, subject, relation,
3325 new SingleOrErrorProcedure<Statement>(procedure));
3330 public void forSingleStatement(Resource subject, Resource relation,
3331 SyncProcedure<Statement> procedure) {
3332 forSingleStatement(subject, relation,
3333 new SyncToAsyncProcedure<Statement>(procedure));
3337 public void forSingleStatement(Resource subject, Resource relation,
3338 Procedure<Statement> procedure) {
3339 forSingleStatement(subject, relation,
3340 new NoneToAsyncProcedure<Statement>(procedure));
3344 public void forSingleType(Resource subject,
3345 AsyncListener<Resource> listener) {
3346 asyncRequest(new SingleTypeAny(subject), listener);
3350 public void forSingleType(Resource subject,
3351 SyncListener<Resource> listener) {
3352 asyncRequest(new SingleTypeAny(subject), listener);
3356 public void forSingleType(Resource subject,
3357 Listener<Resource> listener) {
3358 asyncRequest(new SingleTypeAny(subject), listener);
3362 final public void forSingleType(final Resource subject, final AsyncProcedure<Resource> procedure) {
3364 assert (subject != null);
3365 assert (procedure != null);
3367 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3369 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
3372 public void execute(AsyncReadGraph graph, final Resource principalType) {
3373 checkedProcedure.offer(graph, principalType);
3377 public void finished(AsyncReadGraph graph) {
3378 checkedProcedure.dec(graph);
3382 public void exception(AsyncReadGraph graph, Throwable t) {
3383 checkedProcedure.exception(graph, t);
3387 public String toString() {
3388 return "forSingleType -> " + procedure;
3396 public void forSingleType(Resource subject, SyncProcedure<Resource> procedure) {
3397 forSingleType(subject, new SyncToAsyncProcedure<Resource>(
3402 public void forSingleType(Resource subject, Procedure<Resource> procedure) {
3403 forSingleType(subject, new NoneToAsyncProcedure<Resource>(
3408 public void forSingleType(Resource subject, Resource relation,
3409 AsyncListener<Resource> listener) {
3410 asyncRequest(new SingleType(subject, relation), listener);
3414 public void forSingleType(Resource subject, Resource relation,
3415 SyncListener<Resource> listener) {
3416 asyncRequest(new SingleType(subject, relation), listener);
3420 public void forSingleType(Resource subject, Resource relation,
3421 Listener<Resource> listener) {
3422 asyncRequest(new SingleType(subject, relation), listener);
3426 final public void forSingleType(final Resource subject,
3427 final Resource baseType, final AsyncProcedure<Resource> procedure) {
3429 assert (subject != null);
3430 assert (procedure != null);
3432 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3434 processor.forEachPrincipalType(this, subject,
3435 new AsyncMultiProcedureAdapter<Resource>() {
3438 public void execute(AsyncReadGraph graph,
3439 final Resource principalType) {
3441 checkedProcedure.inc();
3443 if(baseType == null) {
3445 checkedProcedure.offer(graph, principalType);
3446 checkedProcedure.dec(graph);
3448 } else if(principalType.equals(baseType)) {
3450 checkedProcedure.offer(graph, principalType);
3451 checkedProcedure.dec(graph);
3455 processor.forSupertypes((ReadGraphImpl)graph, principalType,
3456 new AsyncProcedure<Set<Resource>>() {
3459 public void execute(
3460 AsyncReadGraph graph,
3461 Set<Resource> result) {
3463 if (result.contains(baseType))
3464 checkedProcedure.offer(graph,
3466 checkedProcedure.dec(graph);
3471 public void exception(
3472 AsyncReadGraph graph,
3475 .exception(graph, t);
3485 public void finished(AsyncReadGraph graph) {
3486 checkedProcedure.dec(graph);
3490 public void exception(AsyncReadGraph graph, Throwable t) {
3491 checkedProcedure.exception(graph, t);
3495 public String toString() {
3496 return "forSingleType -> " + procedure;
3504 public void forSingleType(Resource subject, Resource relation,
3505 SyncProcedure<Resource> procedure) {
3506 forSingleType(subject, relation, new SyncToAsyncProcedure<Resource>(
3511 public void forSingleType(Resource subject, Resource relation,
3512 Procedure<Resource> procedure) {
3513 forSingleType(subject, relation, new NoneToAsyncProcedure<Resource>(
3518 public <T> void forValue(Resource subject, Binding binding,
3519 AsyncListener<T> listener) {
3520 asyncRequest(new Value<T>(subject, binding), listener);
3524 public <T> void forValue(Resource subject, Binding binding,
3525 SyncListener<T> listener) {
3526 asyncRequest(new Value<T>(subject, binding), listener);
3530 public <T> void forValue(Resource subject, Binding binding,
3531 Listener<T> listener) {
3532 asyncRequest(new Value<T>(subject, binding), listener);
3536 public <T> void forValue(final Resource resource, final Binding binding,
3537 final AsyncProcedure<T> procedure) {
3539 assert (resource != null);
3540 assert (binding != null);
3541 assert (procedure != null);
3543 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
3546 public void execute(AsyncReadGraph graph, byte[] result) {
3550 if (result == null) {
3551 procedure.exception(graph,
3552 new DoesNotContainValueException(
3553 "No value for resource " + resource));
3557 Serializer serializer = binding.serializer();
3558 // Serializer serializer = Bindings.getSerializer( binding );
3559 Object obj = serializer.deserialize(result);
3560 // if (!binding.isInstance(obj))
3561 // procedure.exception(graph, new ClassCastException(
3562 // "Cannot get value " + obj + " with binding "
3565 procedure.execute(graph, (T) obj);
3567 } catch (IOException e) {
3568 procedure.exception(graph, e);
3569 } catch (BufferUnderflowException e) {
3570 procedure.exception(graph, e);
3571 } catch (Throwable t) {
3572 procedure.exception(graph, t);
3578 public void exception(AsyncReadGraph graph, Throwable t) {
3580 procedure.exception(graph, t);
3581 } catch (Throwable t2) {
3582 Logger.defaultLogError(t2);
3587 public String toString() {
3588 return "forValue -> " + procedure;
3596 public <T> void forValue(Resource subject, Binding binding,
3597 SyncProcedure<T> procedure) {
3598 forValue(subject, binding, new SyncToAsyncProcedure<T>(procedure));
3602 public <T> void forValue(Resource subject, Binding binding,
3603 Procedure<T> procedure) {
3604 forValue(subject, binding, new NoneToAsyncProcedure<T>(procedure));
3608 public <T> void forValue(Resource subject, AsyncListener<T> listener) {
3609 asyncRequest(new ValueImplied<T>(subject), listener);
3613 public <T> void forValue(Resource subject, SyncListener<T> listener) {
3614 asyncRequest(new ValueImplied<T>(subject), listener);
3618 public <T> void forValue(Resource subject, Listener<T> listener) {
3619 asyncRequest(new ValueImplied<T>(subject), listener);
3623 final public <T> void forValue(final Resource subject, final AsyncProcedure<T> procedure) {
3625 assert (subject != null);
3626 assert (procedure != null);
3628 forRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
3631 public void execute(AsyncReadGraph graph, Datatype type) {
3632 // TODO: consider trying Bindings.getBeanBinding(type);
3633 Binding binding = Bindings.getBinding(type);
3634 graph.forValue(subject, binding, procedure);
3638 public void exception(AsyncReadGraph graph, Throwable throwable) {
3639 procedure.exception(graph, new DoesNotContainValueException("Invalid data type", throwable));
3647 public <T> void forValue(Resource subject, SyncProcedure<T> procedure) {
3648 forValue(subject, new SyncToAsyncProcedure<T>(procedure));
3652 public <T> void forValue(Resource subject, Procedure<T> procedure) {
3653 forValue(subject, new NoneToAsyncProcedure<T>(procedure));
3657 public <T> void forRelatedValue(Resource subject, Resource relation,
3658 AsyncListener<T> listener) {
3659 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3663 public <T> void forRelatedValue(Resource subject, Resource relation,
3664 SyncListener<T> listener) {
3665 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3669 public <T> void forRelatedValue(Resource subject, Resource relation,
3670 Listener<T> listener) {
3671 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3675 final public <T> void forRelatedValue(final Resource subject,
3676 final Resource relation, final AsyncProcedure<T> procedure) {
3678 assert (subject != null);
3679 assert (relation != null);
3680 assert (procedure != null);
3682 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3684 processor.forEachObject(this, subject, relation,
3685 new AsyncMultiProcedureAdapter<Resource>() {
3688 public void execute(AsyncReadGraph graph,
3689 final Resource object) {
3691 checkedProcedure.inc();
3693 graph.forValue(object, new AsyncProcedure<Object>() {
3696 public void execute(AsyncReadGraph graph,
3698 checkedProcedure.offer(graph, (T) result);
3699 checkedProcedure.dec(graph);
3703 public void exception(AsyncReadGraph graph,
3705 checkedProcedure.exception(graph, t);
3709 public String toString() {
3710 return "forRelatedValue -> " + procedure;
3718 public void finished(AsyncReadGraph graph) {
3719 checkedProcedure.dec(graph);
3723 public void exception(AsyncReadGraph graph, Throwable t) {
3724 checkedProcedure.exception(graph, t);
3732 public <T> void forRelatedValue(Resource subject, Resource relation,
3733 SyncProcedure<T> procedure) {
3734 forRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
3739 public <T> void forRelatedValue(Resource subject, Resource relation,
3740 Procedure<T> procedure) {
3741 forRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
3746 public <T> void forRelatedValue(Resource subject, Resource relation,
3747 Binding binding, AsyncListener<T> listener) {
3748 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3752 public <T> void forRelatedValue(Resource subject, Resource relation,
3753 Binding binding, SyncListener<T> listener) {
3754 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3758 public <T> void forRelatedValue(Resource subject, Resource relation,
3759 Binding binding, Listener<T> listener) {
3760 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3764 final public <T> void forRelatedValue(final Resource subject,
3765 final Resource relation, final Binding binding,
3766 final AsyncProcedure<T> procedure) {
3768 assert (subject != null);
3769 assert (relation != null);
3770 assert (binding != null);
3771 assert (procedure != null);
3773 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3775 processor.forEachObject(this, subject, relation,
3776 new AsyncMultiProcedureAdapter<Resource>() {
3779 public void execute(AsyncReadGraph graph,
3780 final Resource object) {
3782 checkedProcedure.inc();
3784 graph.forValue(object, binding, new AsyncProcedure<Object>() {
3787 public void execute(AsyncReadGraph graph,
3790 checkedProcedure.offer(graph,
3792 checkedProcedure.dec(graph);
3796 public void exception(AsyncReadGraph graph,
3798 checkedProcedure.exception(graph, t);
3802 public String toString() {
3803 return "forRelatedValue -> "
3812 public void finished(AsyncReadGraph graph) {
3813 checkedProcedure.dec(graph);
3817 public void exception(AsyncReadGraph graph, Throwable t) {
3818 checkedProcedure.exception(graph, t);
3826 public <T> void forRelatedValue(Resource subject, Resource relation,
3827 Binding binding, SyncProcedure<T> procedure) {
3828 forRelatedValue(subject, relation, binding,
3829 new SyncToAsyncProcedure<T>(procedure));
3833 public <T> void forRelatedValue(Resource subject, Resource relation,
3834 Binding binding, Procedure<T> procedure) {
3835 forRelatedValue(subject, relation, binding,
3836 new NoneToAsyncProcedure<T>(procedure));
3840 public <T> void forAdapted(Resource resource, Class<T> clazz,
3841 AsyncListener<T> listener) {
3842 asyncRequest(new Adapter<T>(resource, clazz), listener);
3846 public <T> void forAdapted(Resource resource, Class<T> clazz,
3847 SyncListener<T> listener) {
3848 asyncRequest(new Adapter<T>(resource, clazz), listener);
3852 public <T> void forAdapted(Resource resource, Class<T> clazz,
3853 Listener<T> listener) {
3854 asyncRequest(new Adapter<T>(resource, clazz), listener);
3858 final public <T> void forAdapted(final Resource resource,
3859 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3861 assert (resource != null);
3862 assert (clazz != null);
3863 assert (procedure != null);
3865 final AdaptionService service = getSession().peekService(AdaptionService.class);
3866 if (service == null)
3867 procedure.exception(this, new ServiceException("No AdaptionService available"));
3869 service.adapt(this, resource, resource, Resource.class, clazz, false, procedure);
3874 public <T> void forAdapted(Resource resource, Class<T> clazz,
3875 SyncProcedure<T> procedure) {
3876 forAdapted(resource, clazz, new SyncToAsyncProcedure<T>(procedure));
3880 public <T> void forAdapted(Resource resource, Class<T> clazz,
3881 Procedure<T> procedure) {
3882 forAdapted(resource, clazz, new NoneToAsyncProcedure<T>(procedure));
3886 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3887 AsyncListener<T> listener) {
3888 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3892 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3893 SyncListener<T> listener) {
3894 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3898 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3899 Listener<T> listener) {
3900 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3904 final public <T> void forUniqueAdapted(final Resource resource,
3905 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3907 assert (resource != null);
3908 assert (clazz != null);
3909 assert (procedure != null);
3911 final AdaptionService service = getSession().peekService(AdaptionService.class);
3912 if (service == null)
3913 procedure.exception(this, new ServiceException("No AdaptionService available"));
3915 service.adaptNew(this, resource, clazz, false, procedure);
3920 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3921 SyncProcedure<T> procedure) {
3922 forUniqueAdapted(resource, clazz,
3923 new SyncToAsyncProcedure<T>(procedure));
3927 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3928 Procedure<T> procedure) {
3929 forUniqueAdapted(resource, clazz,
3930 new NoneToAsyncProcedure<T>(procedure));
3934 public void forPossibleInverse(Resource subject,
3935 AsyncListener<Resource> listener) {
3936 asyncRequest(new PossibleInverse(subject), listener);
3940 public void forPossibleInverse(Resource subject,
3941 SyncListener<Resource> listener) {
3942 asyncRequest(new PossibleInverse(subject), listener);
3946 public void forPossibleInverse(Resource subject, Listener<Resource> listener) {
3947 asyncRequest(new PossibleInverse(subject), listener);
3951 final public void forPossibleInverse(final Resource relation,
3952 final AsyncProcedure<Resource> procedure) {
3954 assert (relation != null);
3955 assert (procedure != null);
3957 processor.forInverse(this, relation, new ExceptionToNullProcedure<Resource>(procedure));
3962 public void forPossibleInverse(Resource subject,
3963 SyncProcedure<Resource> procedure) {
3964 forPossibleInverse(subject, new SyncToAsyncProcedure<Resource>(
3969 public void forPossibleInverse(Resource subject,
3970 Procedure<Resource> procedure) {
3971 forPossibleInverse(subject, new NoneToAsyncProcedure<Resource>(
3976 public void forPossibleObject(Resource subject, Resource relation,
3977 AsyncListener<Resource> listener) {
3978 asyncRequest(new PossibleObject(subject, relation), listener);
3982 public void forPossibleObject(Resource subject, Resource relation,
3983 SyncListener<Resource> listener) {
3984 asyncRequest(new PossibleObject(subject, relation), listener);
3988 public void forPossibleObject(Resource subject, Resource relation,
3989 Listener<Resource> listener) {
3990 asyncRequest(new PossibleObject(subject, relation), listener);
3994 final public void forPossibleObject(final Resource subject,
3995 final Resource relation, final AsyncProcedure<Resource> procedure) {
3997 assert (subject != null);
3998 assert (relation != null);
3999 assert (procedure != null);
4001 processor.forEachObject(this, subject, relation,
4002 new SingleOrNullProcedure<Resource>(procedure));
4007 public void forPossibleObject(Resource subject, Resource relation,
4008 SyncProcedure<Resource> procedure) {
4009 forPossibleObject(subject, relation,
4010 new SyncToAsyncProcedure<Resource>(procedure));
4014 public void forPossibleObject(Resource subject, Resource relation,
4015 Procedure<Resource> procedure) {
4016 forPossibleObject(subject, relation,
4017 new NoneToAsyncProcedure<Resource>(procedure));
4021 public void forPossibleStatement(Resource subject, Resource relation,
4022 AsyncListener<Statement> listener) {
4023 asyncRequest(new PossibleStatement(subject, relation), listener);
4027 public void forPossibleStatement(Resource subject, Resource relation,
4028 SyncListener<Statement> listener) {
4029 asyncRequest(new PossibleStatement(subject, relation), listener);
4033 public void forPossibleStatement(Resource subject, Resource relation,
4034 Listener<Statement> listener) {
4035 asyncRequest(new PossibleStatement(subject, relation), listener);
4039 final public void forPossibleStatement(final Resource subject,
4040 final Resource relation, final AsyncProcedure<Statement> procedure) {
4042 assert (subject != null);
4043 assert (relation != null);
4044 assert (procedure != null);
4046 processor.forEachStatement(this, subject, relation,
4047 new SingleFunctionalOrNullProcedure<Statement>(
4048 "forPossibleStatement", procedure));
4053 public void forPossibleStatement(Resource subject, Resource relation,
4054 SyncProcedure<Statement> procedure) {
4055 forPossibleStatement(subject, relation,
4056 new SyncToAsyncProcedure<Statement>(procedure));
4060 public void forPossibleStatement(Resource subject, Resource relation,
4061 Procedure<Statement> procedure) {
4062 forPossibleStatement(subject, relation,
4063 new NoneToAsyncProcedure<Statement>(procedure));
4067 public void forPossibleType(Resource subject, Resource relation,
4068 AsyncListener<Resource> listener) {
4069 asyncRequest(new PossibleType(subject, relation), listener);
4073 public void forPossibleType(Resource subject, Resource relation,
4074 SyncListener<Resource> listener) {
4075 asyncRequest(new PossibleType(subject, relation), listener);
4079 public void forPossibleType(Resource subject, Resource relation,
4080 Listener<Resource> listener) {
4081 asyncRequest(new PossibleType(subject, relation), listener);
4085 final public void forPossibleType(final Resource subject,
4086 final Resource baseType, final AsyncProcedure<Resource> procedure) {
4088 assert (subject != null);
4089 assert (procedure != null);
4091 final NullSingleOrNullProcedure<Resource> checkedProcedure = new NullSingleOrNullProcedure<Resource>(procedure);
4093 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
4096 public void execute(AsyncReadGraph graph,
4097 final Resource principalType) {
4099 if (baseType == null) {
4101 checkedProcedure.offer(graph, principalType);
4103 } else if (principalType.equals(baseType)) {
4105 checkedProcedure.offer(graph, principalType);
4109 checkedProcedure.inc();
4111 processor.forSupertypes((ReadGraphImpl)graph, principalType,
4112 new AsyncProcedure<Set<Resource>>() {
4115 public void execute(
4116 AsyncReadGraph graph,
4117 Set<Resource> result) {
4119 if (result.contains(baseType)) {
4120 checkedProcedure.offer(graph,
4124 checkedProcedure.dec(graph);
4129 public void exception(
4130 AsyncReadGraph graph,
4132 checkedProcedure.exception(graph, t);
4133 checkedProcedure.dec(graph);
4137 public String toString() {
4138 return "forPossibleType -> "
4149 public void finished(AsyncReadGraph graph) {
4150 checkedProcedure.dec(graph);
4154 public void exception(AsyncReadGraph graph, Throwable t) {
4155 checkedProcedure.exception(graph, t);
4156 checkedProcedure.dec(graph);
4164 public void forPossibleType(Resource subject, Resource relation,
4165 SyncProcedure<Resource> procedure) {
4166 forPossibleType(subject, relation, new SyncToAsyncProcedure<Resource>(
4171 public void forPossibleType(Resource subject, Resource relation,
4172 Procedure<Resource> procedure) {
4173 forPossibleType(subject, relation, new NoneToAsyncProcedure<Resource>(
4178 public <T> void forPossibleValue(Resource subject, AsyncListener<T> listener) {
4179 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4183 public <T> void forPossibleValue(Resource subject, SyncListener<T> listener) {
4184 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4188 public <T> void forPossibleValue(Resource subject, Listener<T> listener) {
4189 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4193 final public <T> void forPossibleValue(final Resource subject,
4194 final AsyncProcedure<T> procedure) {
4196 assert (subject != null);
4197 assert (procedure != null);
4199 forPossibleRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
4202 public void execute(AsyncReadGraph graph, final Datatype type) {
4204 procedure.execute(graph, null);
4207 // TODO: consider trying Bindings.getBeanBinding(type);
4208 Binding binding = Bindings.getBinding(type);
4209 graph.forPossibleValue(subject, binding, procedure);
4210 } catch (RuntimeBindingConstructionException e) {
4211 procedure.exception(graph, e);
4217 public void exception(AsyncReadGraph graph, Throwable t) {
4218 procedure.exception(graph, t);
4222 public String toString() {
4223 return "forPossibleValue -> " + procedure;
4231 public <T> void forPossibleValue(Resource subject,
4232 SyncProcedure<T> procedure) {
4233 forPossibleValue(subject, new SyncToAsyncProcedure<T>(procedure));
4237 public <T> void forPossibleValue(Resource subject, Procedure<T> procedure) {
4238 forPossibleValue(subject, new NoneToAsyncProcedure<T>(procedure));
4242 public <T> void forPossibleValue(Resource subject, Binding binding,
4243 AsyncListener<T> listener) {
4244 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4248 public <T> void forPossibleValue(Resource subject, Binding binding,
4249 SyncListener<T> listener) {
4250 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4254 public <T> void forPossibleValue(Resource subject, Binding binding,
4255 Listener<T> listener) {
4256 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4260 final public <T> void forPossibleValue(final Resource resource,
4261 final Binding binding, final AsyncProcedure<T> procedure) {
4263 assert (resource != null);
4264 assert (binding != null);
4265 assert (procedure != null);
4267 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
4270 public void execute(AsyncReadGraph graph, byte[] result) {
4274 if (result == null) {
4275 procedure.execute(graph, null);
4279 Serializer serializer = Bindings.getSerializer( binding );
4280 Object obj = serializer.deserialize(result);
4281 if (!binding.isInstance(obj))
4282 procedure.exception(graph, new ClassCastException(
4283 "Cannot get value " + obj + " with binding "
4286 procedure.execute(graph, (T) obj);
4288 } catch (IOException e) {
4289 procedure.exception(graph, e);
4290 } catch (BufferUnderflowException e) {
4291 procedure.exception(graph, e);
4292 } catch (Throwable t) {
4293 procedure.exception(graph, t);
4299 public void exception(AsyncReadGraph graph, Throwable t) {
4301 procedure.exception(graph, t);
4302 } catch (Throwable t2) {
4303 Logger.defaultLogError(t2);
4308 public String toString() {
4309 return "forPossibleValue -> " + procedure;
4317 public <T> void forPossibleValue(Resource subject, Binding binding,
4318 SyncProcedure<T> procedure) {
4319 forPossibleValue(subject, binding, new SyncToAsyncProcedure<T>(
4324 public <T> void forPossibleValue(Resource subject, Binding binding,
4325 Procedure<T> procedure) {
4326 forPossibleValue(subject, binding, new NoneToAsyncProcedure<T>(
4331 public <T> void forPossibleRelatedValue(Resource subject,
4332 Resource relation, AsyncListener<T> listener) {
4333 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4338 public <T> void forPossibleRelatedValue(Resource subject,
4339 Resource relation, SyncListener<T> listener) {
4340 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4345 public <T> void forPossibleRelatedValue(Resource subject,
4346 Resource relation, Listener<T> listener) {
4347 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4352 final public <T> void forPossibleRelatedValue(final Resource subject,
4353 final Resource relation, final AsyncProcedure<T> procedure) {
4355 assert (subject != null);
4356 assert (relation != null);
4357 assert (procedure != null);
4359 final DeepSingleOrNullProcedure<T> checkedProcedure = new DeepSingleOrNullProcedure<T>(procedure);
4361 processor.forEachObject(this, subject, relation,
4362 new AsyncMultiProcedureAdapter<Resource>() {
4365 public void execute(AsyncReadGraph graph,
4366 final Resource object) {
4368 checkedProcedure.inc();
4370 graph.forValue(object, new AsyncProcedure<Object>() {
4373 public void execute(AsyncReadGraph graph,
4375 checkedProcedure.offer(graph, (T) result);
4376 checkedProcedure.dec(graph);
4380 public void exception(AsyncReadGraph graph,
4382 checkedProcedure.exception(graph, t);
4383 checkedProcedure.dec(graph);
4391 public void finished(AsyncReadGraph graph) {
4393 checkedProcedure.dec(graph);
4397 public void exception(AsyncReadGraph graph, Throwable t) {
4398 checkedProcedure.exception(graph, t);
4399 checkedProcedure.dec(graph);
4403 public String toString() {
4404 return "forPossibleRelatedValue -> " + procedure;
4411 public <T> void forPossibleRelatedValue(Resource subject,
4412 Resource relation, SyncProcedure<T> procedure) {
4413 forPossibleRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
4418 public <T> void forPossibleRelatedValue(Resource subject,
4419 Resource relation, Procedure<T> procedure) {
4420 forPossibleRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
4425 public <T> void forPossibleRelatedValue(Resource subject,
4426 Resource relation, Binding binding, AsyncListener<T> listener) {
4427 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4432 public <T> void forPossibleRelatedValue(Resource subject,
4433 Resource relation, Binding binding, SyncListener<T> listener) {
4434 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4439 public <T> void forPossibleRelatedValue(Resource subject,
4440 Resource relation, Binding binding, Listener<T> listener) {
4441 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4445 final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4446 final AsyncProcedure<T> procedure) {
4448 assert (subject != null);
4449 assert (relation != null);
4450 assert (procedure != null);
4452 processor.forPossibleObject(this, subject, relation, new AsyncProcedure<Resource>() {
4455 public void execute(AsyncReadGraph graph, Resource object) {
4457 if(object == null) {
4458 procedure.execute(graph, null);
4462 processor.forPossibleValue((ReadGraphImpl)graph, object, new AsyncProcedure<byte[]>() {
4465 public void execute(AsyncReadGraph graph, byte[] bytes) {
4471 Serializer serializer = binding.serializer();
4472 Object obj = serializer.deserialize(bytes);
4473 if (!binding.isInstance(obj)) {
4474 procedure.exception(graph, new ClassCastException("Cannot get value " + obj + " with binding " + binding));
4476 procedure.execute(graph, (T) obj);
4479 } catch (Throwable t) {
4481 procedure.exception(graph, t);
4487 procedure.execute(graph, null);
4494 public void exception(AsyncReadGraph graph, Throwable t) {
4495 procedure.exception(graph, t);
4503 public void exception(AsyncReadGraph graph, Throwable throwable) {
4504 throwable.printStackTrace();
4505 procedure.exception(graph, throwable);
4513 public <T> void forPossibleRelatedValue(Resource subject,
4514 Resource relation, Binding binding, SyncProcedure<T> procedure) {
4515 forPossibleRelatedValue(subject, relation, binding,
4516 new SyncToAsyncProcedure<T>(procedure));
4520 public <T> void forPossibleRelatedValue(Resource subject,
4521 Resource relation, Binding binding, Procedure<T> procedure) {
4522 forPossibleRelatedValue(subject, relation, binding,
4523 new NoneToAsyncProcedure<T>(procedure));
4527 public void forIsInstanceOf(Resource subject, Resource relation,
4528 AsyncListener<Boolean> listener) {
4529 asyncRequest(new IsInstanceOf(subject, relation), listener);
4533 public void forIsInstanceOf(Resource subject, Resource relation,
4534 SyncListener<Boolean> listener) {
4535 asyncRequest(new IsInstanceOf(subject, relation), listener);
4539 public void forIsInstanceOf(Resource subject, Resource relation,
4540 Listener<Boolean> listener) {
4541 asyncRequest(new IsInstanceOf(subject, relation), listener);
4545 final public void forIsInstanceOf(final Resource resource,
4546 final Resource type, final AsyncProcedure<Boolean> procedure) {
4548 assert (resource != null);
4549 assert (type != null);
4550 assert (procedure != null);
4552 forTypes(resource, new AsyncProcedure<Set<Resource>>() {
4555 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4558 if (result.contains(type))
4559 procedure.execute(graph, true);
4561 procedure.execute(graph, false);
4562 } catch (Throwable t) {
4563 Logger.defaultLogError(t);
4568 public void exception(AsyncReadGraph graph, Throwable t) {
4570 procedure.exception(graph, t);
4571 } catch (Throwable t2) {
4572 Logger.defaultLogError(t2);
4577 public String toString() {
4578 return "forIsInstanceOf -> " + procedure;
4586 public void forIsInstanceOf(Resource subject, Resource relation,
4587 SyncProcedure<Boolean> procedure) {
4588 forIsInstanceOf(subject, relation, new SyncToAsyncProcedure<Boolean>(
4593 public void forIsInstanceOf(Resource subject, Resource relation,
4594 Procedure<Boolean> procedure) {
4595 forIsInstanceOf(subject, relation, new NoneToAsyncProcedure<Boolean>(
4600 public void forIsInheritedFrom(Resource subject, Resource relation,
4601 AsyncListener<Boolean> listener) {
4602 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4606 public void forIsInheritedFrom(Resource subject, Resource relation,
4607 SyncListener<Boolean> listener) {
4608 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4612 public void forIsInheritedFrom(Resource subject, Resource relation,
4613 Listener<Boolean> listener) {
4614 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4618 final public void forIsInheritedFrom(final Resource resource,
4619 final Resource type, final AsyncProcedure<Boolean> procedure) {
4621 assert (resource != null);
4622 assert (type != null);
4623 assert (procedure != null);
4625 if (resource.equals(type)) {
4627 procedure.execute(this, true);
4628 } catch (Throwable t) {
4629 Logger.defaultLogError(t);
4634 forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
4637 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4639 if (result.contains(type))
4640 procedure.execute(graph, true);
4642 procedure.execute(graph, false);
4643 } catch (Throwable t) {
4644 Logger.defaultLogError(t);
4649 public void exception(AsyncReadGraph graph, Throwable t) {
4651 procedure.exception(graph, t);
4652 } catch (Throwable t2) {
4653 Logger.defaultLogError(t2);
4658 public String toString() {
4659 return "forIsInheritedFrom -> " + procedure;
4667 public void forIsInheritedFrom(Resource subject, Resource relation,
4668 SyncProcedure<Boolean> procedure) {
4669 forIsInheritedFrom(subject, relation,
4670 new SyncToAsyncProcedure<Boolean>(procedure));
4674 public void forIsInheritedFrom(Resource subject, Resource relation,
4675 Procedure<Boolean> procedure) {
4676 forIsInheritedFrom(subject, relation,
4677 new NoneToAsyncProcedure<Boolean>(procedure));
4681 public void forIsSubrelationOf(Resource subject, Resource relation,
4682 AsyncListener<Boolean> listener) {
4683 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4687 public void forIsSubrelationOf(Resource subject, Resource relation,
4688 SyncListener<Boolean> listener) {
4689 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4693 public void forIsSubrelationOf(Resource subject, Resource relation,
4694 Listener<Boolean> listener) {
4695 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4699 final public void forIsSubrelationOf(final Resource resource,
4700 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4702 assert (resource != null);
4703 assert (relation != null);
4704 assert (procedure != null);
4706 if (resource.equals(relation)) {
4707 procedure.execute(this, true);
4711 forSuperrelations(resource, new AsyncProcedure<Set<Resource>>() {
4714 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4716 if (result.contains(relation))
4717 procedure.execute(graph, true);
4719 procedure.execute(graph, false);
4720 } catch (Throwable t) {
4721 Logger.defaultLogError(t);
4726 public void exception(AsyncReadGraph graph, Throwable t) {
4728 procedure.exception(graph, t);
4729 } catch (Throwable t2) {
4730 Logger.defaultLogError(t2);
4735 public String toString() {
4736 return "forIsSubrelationOf -> " + procedure;
4744 public void forIsSubrelationOf(Resource subject, Resource relation,
4745 SyncProcedure<Boolean> procedure) {
4746 forIsSubrelationOf(subject, relation,
4747 new SyncToAsyncProcedure<Boolean>(procedure));
4751 public void forIsSubrelationOf(Resource subject, Resource relation,
4752 Procedure<Boolean> procedure) {
4753 forIsSubrelationOf(subject, relation,
4754 new NoneToAsyncProcedure<Boolean>(procedure));
4758 public void forHasStatement(Resource subject,
4759 AsyncListener<Boolean> listener) {
4760 asyncRequest(new HasStatementSubject(subject), listener);
4764 public void forHasStatement(Resource subject, SyncListener<Boolean> listener) {
4765 asyncRequest(new HasStatementSubject(subject), listener);
4769 public void forHasStatement(Resource subject, Listener<Boolean> listener) {
4770 asyncRequest(new HasStatementSubject(subject), listener);
4774 final public void forHasStatement(final Resource subject,
4775 final AsyncProcedure<Boolean> procedure) {
4777 assert (subject != null);
4778 assert (procedure != null);
4780 processor.forHasStatement(this, subject, procedure);
4785 public void forHasStatement(Resource subject,
4786 SyncProcedure<Boolean> procedure) {
4787 forHasStatement(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4791 public void forHasStatement(Resource subject, Procedure<Boolean> procedure) {
4792 forHasStatement(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4796 public void forHasStatement(Resource subject, Resource relation,
4797 AsyncListener<Boolean> listener) {
4798 asyncRequest(new HasStatement(subject, relation), listener);
4802 public void forHasStatement(Resource subject, Resource relation,
4803 SyncListener<Boolean> listener) {
4804 asyncRequest(new HasStatement(subject, relation), listener);
4808 public void forHasStatement(Resource subject, Resource relation,
4809 Listener<Boolean> listener) {
4810 asyncRequest(new HasStatement(subject, relation), listener);
4814 final public void forHasStatement(final Resource subject,
4815 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4817 assert (subject != null);
4818 assert (relation != null);
4819 assert (procedure != null);
4821 processor.forHasStatement(this, subject, relation, procedure);
4826 public void forHasStatement(Resource subject, Resource relation,
4827 SyncProcedure<Boolean> procedure) {
4828 forHasStatement(subject, relation, new SyncToAsyncProcedure<Boolean>(
4833 public void forHasStatement(Resource subject, Resource relation,
4834 Procedure<Boolean> procedure) {
4835 forHasStatement(subject, relation, new NoneToAsyncProcedure<Boolean>(
4840 public void forHasStatement(Resource subject, Resource relation,
4841 Resource object, AsyncListener<Boolean> listener) {
4842 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4847 public void forHasStatement(Resource subject, Resource relation,
4848 Resource object, SyncListener<Boolean> listener) {
4849 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4854 public void forHasStatement(Resource subject, Resource relation,
4855 Resource object, Listener<Boolean> listener) {
4856 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4861 final public void forHasStatement(final Resource subject,
4862 final Resource relation, final Resource object,
4863 final AsyncProcedure<Boolean> procedure) {
4865 assert (subject != null);
4866 assert (relation != null);
4867 assert (object != null);
4868 assert (procedure != null);
4870 processor.forHasStatement(this, subject, relation, object, procedure);
4875 public void forHasStatement(Resource subject, Resource relation,
4876 Resource object, SyncProcedure<Boolean> procedure) {
4877 forHasStatement(subject, relation, object,
4878 new SyncToAsyncProcedure<Boolean>(procedure));
4882 public void forHasStatement(Resource subject, Resource relation,
4883 Resource object, Procedure<Boolean> procedure) {
4884 forHasStatement(subject, relation, object,
4885 new NoneToAsyncProcedure<Boolean>(procedure));
4889 public void forHasValue(Resource subject, AsyncListener<Boolean> listener) {
4890 asyncRequest(new HasValue(subject), listener);
4894 public void forHasValue(Resource subject, SyncListener<Boolean> listener) {
4895 asyncRequest(new HasValue(subject), listener);
4899 public void forHasValue(Resource subject, Listener<Boolean> listener) {
4900 asyncRequest(new HasValue(subject), listener);
4904 final public void forHasValue(final Resource subject,
4905 final AsyncProcedure<Boolean> procedure) {
4907 assert (subject != null);
4908 assert (procedure != null);
4910 processor.forValue(this, subject, new AsyncProcedure<byte[]>() {
4913 public void execute(AsyncReadGraph graph, byte[] result) {
4916 procedure.execute(graph, false);
4918 procedure.execute(graph, true);
4919 } catch (Throwable t) {
4920 Logger.defaultLogError(t);
4925 public void exception(AsyncReadGraph graph, Throwable t) {
4927 procedure.exception(graph, t);
4928 } catch (Throwable t2) {
4929 Logger.defaultLogError(t2);
4934 public String toString() {
4935 return "forHasValue -> " + procedure;
4943 public void forHasValue(Resource subject, SyncProcedure<Boolean> procedure) {
4944 forHasValue(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4948 public void forHasValue(Resource subject, Procedure<Boolean> procedure) {
4949 forHasValue(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4953 public void forOrderedSet(Resource subject,
4954 AsyncMultiListener<Resource> listener) {
4955 asyncRequest(new OrderedSet(subject), listener);
4959 public void forOrderedSet(Resource subject,
4960 SyncMultiListener<Resource> listener) {
4961 asyncRequest(new OrderedSet(subject), listener);
4965 public void forOrderedSet(Resource subject, MultiListener<Resource> listener) {
4966 asyncRequest(new OrderedSet(subject), listener);
4970 final public void forOrderedSet(final Resource subject,
4971 final AsyncMultiProcedure<Resource> procedure) {
4973 assert (subject != null);
4974 assert (procedure != null);
4976 processor.forOrderedSet(this, subject,
4977 new AsyncMultiProcedure<Resource>() {
4980 public void finished(AsyncReadGraph graph) {
4982 procedure.finished(graph);
4983 } catch (Throwable t) {
4984 Logger.defaultLogError(t);
4989 public void execute(AsyncReadGraph graph, Resource result) {
4991 procedure.execute(graph, result);
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 "forOrderedSet -> " + procedure;
5016 public void forOrderedSet(Resource subject,
5017 SyncMultiProcedure<Resource> procedure) {
5018 forOrderedSet(subject, new SyncToAsyncMultiProcedure<Resource>(
5023 public void forOrderedSet(Resource subject,
5024 MultiProcedure<Resource> procedure) {
5025 forOrderedSet(subject, new NoneToAsyncMultiProcedure<Resource>(
5030 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5031 AsyncListener<T> listener) {
5032 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5036 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5037 SyncListener<T> listener) {
5038 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5042 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5043 Listener<T> listener) {
5044 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5048 final public <T> void forPossibleAdapted(final Resource resource,
5049 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5051 assert (resource != null);
5052 assert (clazz != null);
5053 assert (procedure != null);
5055 final AdaptionService service = getSession().peekService(AdaptionService.class);
5056 if (service == null)
5057 procedure.exception(this, new ServiceException("No AdaptionService available"));
5059 service.adapt(this, resource, resource, Resource.class, clazz, true, procedure);
5063 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5064 SyncProcedure<T> procedure) {
5065 forPossibleAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5070 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5071 Procedure<T> procedure) {
5072 forPossibleAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5077 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5078 AsyncListener<T> listener) {
5079 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5083 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5084 SyncListener<T> listener) {
5085 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5089 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5090 Listener<T> listener) {
5091 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5095 final public <T> void forPossibleUniqueAdapted(final Resource resource,
5096 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5098 assert (resource != null);
5099 assert (clazz != null);
5100 assert (procedure != null);
5102 final AdaptionService service = getSession().peekService(AdaptionService.class);
5103 if (service == null)
5104 procedure.exception(this, new ServiceException("No AdaptionService available"));
5106 service.adaptNew(this, resource, clazz, true, procedure);
5111 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5112 SyncProcedure<T> procedure) {
5113 forPossibleUniqueAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5118 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5119 Procedure<T> procedure) {
5120 forPossibleUniqueAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5125 * Implementation of the interface AsyncRequestProcessor
5129 final public Session getSession() {
5130 return processor.getSession();
5134 public <T> void asyncRequest(final Read<T> request) {
5136 asyncRequest(request, new AsyncProcedure<T>() {
5139 public void execute(AsyncReadGraph graph, T result) {
5143 public void exception(AsyncReadGraph graph, Throwable t) {
5144 Logger.defaultLogError(t);
5148 public String toString() {
5149 return "asyncRequest(Read) -> " + request;
5157 public <T> void asyncRequest(Read<T> request, AsyncListener<T> procedure) {
5158 asyncRequest(request, (AsyncProcedure<T>) procedure);
5162 public <T> void asyncRequest(Read<T> request,
5163 final SyncListener<T> procedure) {
5164 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5168 public <T> void asyncRequest(Read<T> request, final Listener<T> procedure) {
5169 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5173 public <T> void asyncRequest(final Read<T> request, final AsyncProcedure<T> procedure) {
5175 assert (request != null);
5176 assert (procedure != null);
5178 processor.schedule(new SessionTask(false) {
5181 public void run(int thread) {
5183 final ListenerBase listener = getListenerBase(procedure);
5184 QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5185 } catch (DatabaseException e) {
5186 Logger.defaultLogError(e);
5194 public static ReadGraphImpl createAsync(QueryProcessor support) {
5195 return new ReadGraphImpl(null, support);
5199 public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
5200 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5204 public <T> void asyncRequest(Read<T> request, final Procedure<T> procedure) {
5205 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5209 final public <T> void asyncRequest(final AsyncRead<T> request) {
5211 assert (request != null);
5213 asyncRequest(request, new AsyncProcedure<T>() {
5216 public void execute(AsyncReadGraph graph, T result) {
5220 public void exception(AsyncReadGraph graph, Throwable t) {
5221 Logger.defaultLogError(t);
5225 public String toString() {
5226 return "asyncRequest(AsyncRead) -> " + request;
5234 public <T> void asyncRequest(AsyncRead<T> request,
5235 AsyncListener<T> procedure) {
5236 asyncRequest(request, (AsyncProcedure<T>) procedure);
5240 final public <T> void asyncRequest(AsyncRead<T> request,
5241 final SyncListener<T> procedure) {
5242 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5246 final public <T> void asyncRequest(AsyncRead<T> request,
5247 final Listener<T> procedure) {
5248 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5252 final public <T> void asyncRequest(final AsyncRead<T> request,
5253 final AsyncProcedure<T> procedure) {
5255 assert (request != null);
5256 assert (procedure != null);
5258 processor.schedule(new SessionTask(false) {
5261 public void run(int thread) {
5263 final ListenerBase listener = getListenerBase(procedure);
5264 QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5265 } catch (DatabaseException e) {
5266 Logger.defaultLogError(e);
5275 public <T> void asyncRequest(AsyncRead<T> request,
5276 SyncProcedure<T> procedure) {
5277 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5281 final public <T> void asyncRequest(final AsyncRead<T> request,
5282 final Procedure<T> procedure) {
5283 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5287 public <T> void asyncRequest(final MultiRead<T> request) {
5289 assert (request != null);
5291 asyncRequest(request, new SyncMultiProcedureAdapter<T>() {
5293 public void exception(ReadGraph graph, Throwable t) {
5294 Logger.defaultLogError(t);
5298 public String toString() {
5299 return "asyncRequest(MultiRead) -> " + request;
5306 public <T> void asyncRequest(MultiRead<T> request,
5307 SyncMultiListener<T> procedure) {
5308 asyncRequest(request, (SyncMultiProcedure<T>)procedure);
5312 public <T> void asyncRequest(MultiRead<T> request,
5313 MultiListener<T> procedure) {
5314 asyncRequest(request, new NoneToSyncMultiListener<T>(procedure));
5319 public <T> void asyncRequest(final MultiRead<T> request,
5320 final SyncMultiProcedure<T> procedure) {
5322 assert (request != null);
5323 assert (procedure != null);
5325 final ListenerBase listener = getListenerBase(procedure);
5327 if (parent != null || listener != null) {
5329 // final ReadGraphImpl newGraph = newSync();
5330 processor.query(this, request, parent, procedure,listener);
5334 // final ReadGraphImpl newGraph = newSync();
5338 request.perform(this, procedure);
5340 } catch (Throwable t) {
5343 procedure.exception(this, t);
5344 } catch (DatabaseException e) {
5345 LOGGER.error("Unexpected exception while handling exception", e);
5355 public <T> void asyncRequest(MultiRead<T> request,
5356 MultiProcedure<T> procedure) {
5357 asyncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
5361 final public <T> void asyncRequest(final AsyncMultiRead<T> request) {
5363 assert (request != null);
5365 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5367 public void exception(AsyncReadGraph graph, Throwable t) {
5368 Logger.defaultLogError(t);
5372 public String toString() {
5373 return "asyncRequest(AsyncMultiRead) -> " + request;
5380 public <T> void asyncRequest(AsyncMultiRead<T> request,
5381 AsyncMultiListener<T> procedure) {
5382 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5386 public <T> void asyncRequest(AsyncMultiRead<T> request,
5387 SyncMultiListener<T> procedure) {
5388 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5392 public <T> void asyncRequest(AsyncMultiRead<T> request,
5393 MultiListener<T> procedure) {
5394 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5398 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5399 final AsyncMultiProcedure<T> procedure) {
5401 assert (request != null);
5402 assert (procedure != null);
5404 ListenerBase listener = getListenerBase(procedure);
5406 if (parent != null || listener != null) {
5408 processor.query(this, request, parent, procedure, listener);
5414 request.perform(this, new AsyncMultiProcedure<T>() {
5417 public void execute(AsyncReadGraph graph, T result) {
5418 procedure.execute(graph, result);
5422 public void finished(AsyncReadGraph graph) {
5423 procedure.finished(graph);
5427 public void exception(AsyncReadGraph graph, Throwable t) {
5428 procedure.exception(graph, t);
5432 public String toString() {
5433 return "asyncRequest(AsyncMultiRead) -> " + procedure;
5438 } catch (Throwable t) {
5440 procedure.exception(this, new DatabaseException(t));
5448 public <T> void asyncRequest(AsyncMultiRead<T> request,
5449 SyncMultiProcedure<T> procedure) {
5450 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5454 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5455 final MultiProcedure<T> procedure) {
5456 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5460 final public <T> void asyncRequest(final ExternalRead<T> request) {
5462 assert (request != null);
5464 asyncRequest(request, new Procedure<T>() {
5467 public void execute(T result) {
5471 public void exception(Throwable t) {
5472 Logger.defaultLogError(t);
5476 public String toString() {
5477 return "asyncRequest(PrimitiveRead) -> " + request;
5485 public <T> void asyncRequest(ExternalRead<T> request,
5486 final Listener<T> procedure) {
5487 asyncRequest(request, (Procedure<T>) procedure);
5491 final public <T> void asyncRequest(final ExternalRead<T> request,
5492 final Procedure<T> procedure) {
5494 assert (request != null);
5495 assert (procedure != null);
5497 final ListenerBase listener = getListenerBase(procedure);
5499 if (parent != null || listener != null) {
5502 QueryCacheBase.resultExternalReadEntry(this, request, parent, listener, procedure);
5503 } catch (DatabaseException e) {
5504 Logger.defaultLogError(e);
5505 // This throwable has already been transferred to procedure at this point - do nothing about it
5510 request.register(this, new Listener<T>() {
5512 public void execute(T result) {
5513 procedure.execute(result);
5517 public void exception(Throwable t) {
5518 procedure.exception(t);
5522 public String toString() {
5523 return "asyncRequest(PrimitiveRead) -> " + request;
5527 public boolean isDisposed() {
5538 public void asyncRequest(final Write request) {
5540 assert (request != null);
5542 getSession().asyncRequest(request);
5544 // processor.asyncWrite(request);
5549 public <T> void asyncRequest(final WriteResult<T> request, Procedure<T> procedure) {
5550 throw new Error("Not implemented.");
5554 public void asyncRequest(Write request, Consumer<DatabaseException> callback) {
5556 assert (request != null);
5558 getSession().asyncRequest(request, callback);
5563 public void asyncRequest(final DelayedWrite request) {
5565 assert (request != null);
5567 getSession().asyncRequest(request);
5572 public <T> void asyncRequest(final DelayedWriteResult<T> request, Procedure<T> procedure) {
5573 throw new Error("Not implemented.");
5577 public void asyncRequest(DelayedWrite r,
5578 Consumer<DatabaseException> callback) {
5579 throw new Error("Not implemented.");
5583 public void asyncRequest(final WriteOnly request) {
5585 assert (request != null);
5587 getSession().asyncRequest(request);
5592 public <T> void asyncRequest(final WriteOnlyResult<T> request, Procedure<T> procedure) {
5593 throw new Error("Not implemented.");
5597 public void asyncRequest(WriteOnly r, Consumer<DatabaseException> callback) {
5598 throw new Error("Not implemented.");
5602 * Implementation of the interface ServiceLocator
5606 public <T> T getService(Class<T> api) {
5607 if(WriteSupport.class == api) {
5608 if(this instanceof WriteGraphImpl) {
5609 WriteGraphImpl impl = (WriteGraphImpl)this;
5610 return (T)impl.writeSupport;
5613 return getSession().getService(api);
5617 public <T> T peekService(Class<T> api) {
5618 return getSession().peekService(api);
5622 public boolean hasService(Class<?> api) {
5623 return getSession().hasService(api);
5627 public <T> void registerService(Class<T> api, T service) {
5628 getSession().registerService(api, service);
5632 public boolean isImmutable(Resource resource) throws DatabaseException {
5633 ResourceImpl impl = (ResourceImpl)resource;
5634 return processor.isImmutable(impl.id);
5641 protected static String INTERNAL_ERROR_STRING = "Transaction aborted due to internal client error. Contact application support.";
5644 * callerThread is the currently running thread state.syncThread is blocking for
5645 * this execution state.syncParent is the blocking request
5648 final private boolean isExternal(int thread) {
5649 return thread == Integer.MIN_VALUE;
5652 ReadGraphImpl(ReadGraphImpl graph) {
5653 this(graph.parent, graph.processor);
5656 ReadGraphImpl(CacheEntry parent, QueryProcessor support) {
5657 this.parent = parent;
5658 this.processor = support;
5661 public static ReadGraphImpl create(QueryProcessor support) {
5662 return new ReadGraphImpl(null, support);
5665 public static ReadGraphImpl newAsync(ReadGraphImpl parent) {
5666 ReadGraphImpl result = new ReadGraphImpl(parent);
5667 result.asyncBarrier = new AsyncBarrierImpl(parent.asyncBarrier);
5671 public ReadGraphImpl newRestart(ReadGraphImpl impl) {
5673 WriteGraphImpl write = processor.getSession().getService(
5674 WriteGraphImpl.class);
5680 public ReadGraphImpl withParent(CacheEntry parent) {
5681 if(parent == this.parent) return this;
5682 else return new ReadGraphImpl(parent, processor);
5685 final private ListenerBase getListenerBase(final Object procedure) {
5686 if (procedure instanceof ListenerBase)
5687 return (ListenerBase) procedure;
5692 public <T> void waitAsyncProcedure(AsyncMultiReadProcedure<T> procedure) {
5694 assert(procedure.done());
5698 public <T> void waitAsyncProcedure(AsyncReadProcedure<T> procedure) {
5700 assert(procedure.done());
5704 public boolean resumeTasks() {
5705 return processor.resumeTasks(this);
5708 Class<?> singleClass(Set<Resource> types) {
5709 Class<?> result = null;
5710 for (Resource type : types) {
5711 Class<?> clazz = processor.getBuiltinValue(type);
5712 if (clazz != null) {
5722 private String debugString(Resource r) {
5725 name = getPossibleRelatedValue(r, processor.getL0(this).HasName);
5726 } catch (ManyObjectsForFunctionalRelationException e) {
5727 Logger.defaultLogError(e);
5728 } catch (ServiceException e) {
5729 Logger.defaultLogError(e);
5731 return "[" + name + " - " + r + "]";
5735 public String toString() {
5736 return "ReadGraphImpl[thread=" + Thread.currentThread() + "]";
5740 final public int thread() {
5744 static class MultiTripleIntProcedure implements TripleIntProcedure {
5746 final private AsyncMultiProcedure<Statement> procedure;
5747 final private ReadGraphImpl impl;
5748 final private QuerySupport support;
5750 public MultiTripleIntProcedure(AsyncMultiProcedure<Statement> procedure, ReadGraphImpl impl, QuerySupport support) {
5751 this.procedure = procedure;
5753 this.support = support;
5757 public void execute(ReadGraphImpl graph, int s, int p, int o) {
5759 procedure.execute(graph, support.getStatement(s, p, o));
5760 } catch (Throwable t2) {
5761 Logger.defaultLogError(t2);
5766 public void finished(ReadGraphImpl graph) {
5768 procedure.finished(graph);
5769 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5770 } catch (Throwable t2) {
5771 Logger.defaultLogError(t2);
5776 public void exception(ReadGraphImpl graph, Throwable t) {
5778 procedure.exception(graph, t);
5779 } catch (Throwable t2) {
5780 Logger.defaultLogError(t2);
5782 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5786 public String toString() {
5787 return "forEachObject with " + procedure;
5792 // private AsyncMultiProcedure<Resource> cacheKey = null;
5793 // private MultiIntProcedure cacheResult = null;
5795 // final IntProcedure forMultiProcedure(final AsyncMultiProcedure<Resource> procedure) {
5797 // if(procedure == cacheKey) return cacheResult;
5799 // cacheResult = new MultiIntProcedure(procedure, this, processor.support);
5800 // cacheKey = procedure;
5802 // return cacheResult;
5806 // private AsyncMultiProcedure<Statement> cacheKey2 = null;
5807 // private MultiTripleIntProcedure cacheResult2 = null;
5809 // final synchronized TripleIntProcedure forMultiProcedure(final AsyncMultiProcedure<Statement> procedure) {
5811 // if(procedure == cacheKey2) return cacheResult2;
5813 // cacheResult2 = new MultiTripleIntProcedure(procedure, this, processor.support);
5814 // cacheKey2 = procedure;
5816 // return cacheResult2;
5821 public Datatype getDataType(Resource subject) throws DatabaseException {
5822 for(Resource dataTypeResource : getObjects(subject, processor.getL0(this).HasDataType))
5823 return getValue(dataTypeResource, Bindings.getBindingUnchecked(Datatype.class));
5824 throw new DoesNotContainValueException("The literal has no data type.");
5827 protected <T extends Accessor> T getAccessor4File(Resource subject)
5828 throws DatabaseException {
5831 byte[] bytes = processor.support.getValue(g, subject);
5835 BinaryVariant va = (BinaryVariant) Accessors.getAccessor(bytes, Datatypes.VARIANT);
5836 Accessor ca = va.getContentAccessor();
5838 } catch (AccessorConstructionException e) {
5839 throw new DatabaseException(e);
5845 Binding datatype_binding = Bindings.getBindingUnchecked(DataType.class);
5846 Serializer datatype_serializer = datatype_binding.serializer();
5849 BinaryMemory in = new BinaryMemory(ByteBuffer.wrap(bytes));
5850 datatype = (DataType)datatype_serializer.deserialize(in);
5851 Binding data_binding = Bindings.getBinding(datatype);
5852 Serializer data_serializer = data_binding.serializer(BinarySerializationFormat.INSTANCE);
5853 Object o = data_serializer.deserialize(in);
5855 return (T)Accessors.getAccessor(data_binding, o);
5856 } catch(AccessorConstructionException e) {
5859 } catch (Exception e) {
5860 throw new DatabaseException(e);
5863 @SuppressWarnings("unchecked")
5865 public <T extends Accessor> T getAccessor(Resource subject) throws DatabaseException {
5866 RandomAccessBinary rab = getRandomAccessBinary(subject);
5868 return (T)Accessors.getAccessor(rab, getDataType(subject));
5869 } catch(AccessorConstructionException e) {
5870 throw new DatabaseException(e);
5873 @SuppressWarnings("unchecked")
5874 protected <T extends Accessor> T createAccessor(Resource resource, Datatype datatype, Object intialValue)
5875 throws DatabaseException {
5876 RandomAccessBinary rab = createRandomAccessBinary(resource, datatype, intialValue);
5878 return (T)Accessors.getAccessor(rab, datatype);
5879 } catch(AccessorConstructionException e) {
5880 throw new DatabaseException(e);
5884 public RandomAccessBinary getRandomAccessBinary(Resource subject) throws DatabaseException {
5885 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5886 ResourceData rd = ravs.get(subject);
5890 ExternalValueSupport evs = getService(ExternalValueSupport.class);
5891 long size = evs.getValueSize(this, subject); // Throws DatabaseException if no old external value.
5893 File platform = Platform.getLocation().toFile();
5894 File tempFiles = new File(platform, "tempFiles");
5895 File dbDir = new File(tempFiles, "db");
5897 File file = new File(dbDir, "ResourceFile" + subject.getResourceId());
5898 rd = new ResourceData(new BinaryFile(file), true); // Old external value.
5899 final int N = 1<<20;
5903 int length = N < left ? N : (int)left;
5904 byte[] bytes = evs.readValue(this, subject, offset, length);
5905 offset += bytes.length;
5906 left -= bytes.length;
5907 rd.binaryFile.write(bytes);
5909 ravs.put(subject, rd);
5911 } catch (Exception e) {
5912 throw new DatabaseException("Resource " + subject + " have value but there was problem with accessing value data.", e);
5914 } catch (Exception e) {
5915 if(Development.DEVELOPMENT) {
5916 if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
5917 e.printStackTrace();
5921 Datatype datatype = getDataType(subject);
5922 Object value = getPossibleValue(subject, Bindings.getBinding(datatype));
5923 return createRandomAccessBinary(subject, datatype, value);
5925 public RandomAccessBinary createRandomAccessBinary(Resource resource, Datatype datatype, Object initialValue)
5926 throws DatabaseException {
5927 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5929 File platform = Platform.getLocation().toFile();
5930 File tempFiles = new File(platform, "tempFiles");
5931 File dbDir = new File(tempFiles, "db");
5933 File file = new File(dbDir, "ResourceFile" + resource.getResourceId());
5934 ResourceData rd = new ResourceData(new BinaryFile(file), false);
5935 Binding binding = Bindings.getBinding(datatype);
5936 if (null == initialValue) {
5937 initialValue = binding.createDefault();
5939 Serializer serializer = binding.serializer();
5940 byte[] bytes = serializer.serialize(initialValue);
5941 rd.binaryFile.write(bytes);
5942 ravs.put(resource, rd);
5944 } catch (Exception e) {
5945 if (e instanceof DatabaseException)
5946 throw (DatabaseException)e;
5948 throw new DatabaseException(e);
5952 // static class ExternalValueRequest<T> extends ResourceRead<T> {
5954 // public ExternalValueRequest(Resource resource) {
5958 // @SuppressWarnings("unchecked")
5960 // public T perform(ReadGraph graph) throws DatabaseException {
5963 // String uri = graph.getURI(resource);
5964 // if(Layer0.URIs.Functions_functionApplication.equals(uri)) return (T)functionApplication;
5966 // return (T)ReflectionUtils.getValue(uri).getValue();
5968 // } catch(ValueNotFoundException e) {
5969 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
5970 // } catch(ClassCastException e) {
5971 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
5977 @SuppressWarnings("unchecked")
5979 public <T> T getValue2(Resource r, Object context) throws DatabaseException {
5980 Layer0 L0 = processor.getL0(this);
5981 Set<Resource> types = getTypes(r);
5983 if(types.contains(L0.Literal)) {
5984 if(isImmutable(r)) {
5985 return syncRequest(new ValueImplied<T>(r));
5990 else if(types.contains(L0.ExternalValue)) {
5991 return (T)syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
5995 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
5996 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
5998 return function.apply(this, r, context);
5999 } catch(RuntimeException e) {
6000 DatabaseException dte = findPossibleRootException(e);
6001 if(dte != null) throw dte;
6002 else throw new DatabaseException(e);
6009 public Variant getVariantValue2(Resource r, Object context) throws DatabaseException {
6010 Layer0 L0 = processor.getL0(this);
6011 Set<Resource> types = getTypes(r);
6013 if(types.contains(L0.Literal)) {
6014 if(isImmutable(r)) {
6015 return syncRequest(new VariantValueImplied(r));
6017 return getVariantValue(r);
6020 else if(types.contains(L0.ExternalValue)) {
6021 Object value = syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6023 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6024 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6025 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6030 Function3<ReadGraph,Resource,Object,Object> function = requestValueFunction(r);
6031 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6033 Object value = function.apply(this, r, context);
6035 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6036 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6037 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6039 } catch(RuntimeException e) {
6040 DatabaseException dte = findPossibleRootException(e);
6041 if(dte != null) throw dte;
6042 else throw new DatabaseException(e);
6048 public <T> T getPossibleValue2(Resource subject, Object context) throws DatabaseException {
6050 return getValue2(subject, context);
6051 } catch (DatabaseException e) {
6056 static class PossibleConverterFunction<T> extends ResourceRead<Function3<ReadGraph,Resource,Object,T>> {
6058 public PossibleConverterFunction(Resource resource) {
6063 public Function3<ReadGraph,Resource,Object,T> perform(ReadGraph graph) throws DatabaseException {
6064 return compute(graph, resource);
6067 @SuppressWarnings("unchecked")
6068 public static <T> Function3<ReadGraph,Resource,Object,T> compute(ReadGraph graph, Resource resource) throws DatabaseException {
6069 Layer0 L0 = Layer0.getInstance(graph);
6070 for(Resource converter : graph.getObjects(resource, L0.ConvertsToValueWith)) {
6072 if(L0.Functions_functionApplication.equals(converter)) {
6073 return (Function3<ReadGraph,Resource,Object,T>)graph.syncRequest(new AdaptValue(resource));
6075 return graph.getValue2(converter, resource);
6077 } catch(RuntimeException e) {
6078 DatabaseException dte = findPossibleRootException(e);
6079 if(dte != null) throw dte;
6080 else throw new DatabaseException(e);
6088 <T> Function3<ReadGraph,Resource,Object,T> requestValueFunction(Resource r) throws DatabaseException {
6090 return syncRequest(new PossibleConverterFunction<T>(r), TransientCacheAsyncListener.<Function3<ReadGraph,Resource,Object,T>>instance());
6092 return syncRequest(new PossibleConverterFunction<T>(r));
6096 * Get a value associated with a graph {@link Resource}, using a possible context object and
6097 * a desired value binding. The following methods are tried in order to retreive the value:
6099 * <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>
6100 * <li>If the resource is a {@code L0.ExternalValue}, the value is acquired using
6101 * {@link ReflectionUtils#getValue(String)}.</li>
6102 * <li>If the resource is associated with a suitable value converter with relation {@code L0.ConvertsToValueWith}
6103 * (see {@link #requestValueFunction(Resource)}), the value function is called with the graph, the resource
6104 * and the context object.</li>
6107 * @param r A graph resource with which the value is associated
6108 * @param context A context object that is used for acquiring the value (only applied in case 3)
6109 * @param binding A binding for the value type (only applied in case 1)
6110 * @return The value of the graph node.
6111 * @throws DoesNotContainValueException No value is associated with the graph node.
6112 * @throws DatabaseException Other errors, such as an error in casting the value to the return type or
6113 * a runtime error in the value function.
6115 @SuppressWarnings("unchecked")
6117 public <T> T getValue2(Resource r, Object context, Binding binding) throws DatabaseException {
6118 if (binding instanceof ObjectVariantBinding)
6119 return getValue2(r, context);
6121 Layer0 L0 = processor.getL0(this);
6122 Set<Resource> types = getTypes(r);
6123 if(types.contains(L0.Literal)) {
6124 if(isImmutable(r)) {
6125 return syncRequest(new Value<T>(r, binding));
6127 return getValue(r, binding);
6129 } else if(types.contains(L0.ExternalValue)) {
6131 return (T)ReflectionUtils.getValue(getURI(r)).getValue();
6132 } catch(ValueNotFoundException e) {
6133 throw new DatabaseException(e);
6134 } catch(ClassCastException e) {
6135 throw new DatabaseException(e);
6139 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6140 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function.");
6142 Object value = function.apply(this, r, context);
6143 if(binding.isInstance(value)) return (T)value;
6144 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
6145 return (T)Bindings.adapt(value, srcBinding, binding);
6146 } catch(RuntimeException e) {
6147 DatabaseException dte = findPossibleRootException(e);
6148 if(dte != null) throw dte;
6149 else throw new DatabaseException(e);
6150 } catch (AdaptException e) {
6151 throw new DatabaseException(e);
6152 } catch (org.simantics.databoard.binding.error.BindingException e) {
6153 throw new DatabaseException(e);
6159 public <T> T getPossibleValue2(Resource subject, Object context, Binding binding) throws DatabaseException {
6161 return getValue2(subject, context, binding);
6162 } catch (DatabaseException e) {
6167 private static DatabaseException findPossibleRootException(Throwable t) {
6168 if(t == null) return null;
6169 if(t instanceof DatabaseException) return (DatabaseException)t;
6170 if(t instanceof RuntimeException || t instanceof InvocationTargetException) {
6171 return findPossibleRootException(t.getCause());
6177 public <T> T getRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6178 return getRelatedValue2(subject, relation, subject);
6182 public Variant getRelatedVariantValue2(Resource subject, Resource relation) throws DatabaseException {
6183 return getRelatedVariantValue2(subject, relation, subject);
6187 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6189 Resource object = getPossibleObject(subject, relation);
6190 if(object == null) return null;
6191 else return getValue2(object, subject);
6192 } catch (DatabaseException e) {
6198 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6199 if(Development.DEVELOPMENT) {
6200 String error = L0Validations.checkValueType(this, subject, relation);
6202 Logger.defaultLogError(new ValidationException(error));
6203 //throw new ValidationException(error);
6204 new ValidationException(error).printStackTrace();
6207 return getValue2(getSingleObject(subject, relation), context);
6211 public Variant getRelatedVariantValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6212 if(Development.DEVELOPMENT) {
6213 String error = L0Validations.checkValueType(this, subject, relation);
6215 Logger.defaultLogError(new ValidationException(error));
6216 //throw new ValidationException(error);
6217 new ValidationException(error).printStackTrace();
6220 return getVariantValue2(getSingleObject(subject, relation), context);
6224 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6226 Resource object = getPossibleObject(subject, relation);
6227 if(object == null) return null;
6228 else return getValue2(object, context);
6229 } catch (DatabaseException e) {
6235 public <T> T getRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6236 return getRelatedValue2(subject, relation, subject, binding);
6240 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6242 Resource object = getPossibleObject(subject, relation);
6243 if(object == null) return null;
6244 return getValue2(object, subject, binding);
6245 } catch (DatabaseException e) {
6251 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6252 return getValue2(getSingleObject(subject, relation), context, binding);
6256 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6258 Resource object = getPossibleObject(subject, relation);
6259 if(object == null) return null;
6260 else return getValue2(object, context, binding);
6261 } catch (DatabaseException e) {
6267 public Type getRelatedValueType(Resource subject, Resource relation) throws DatabaseException {
6268 Layer0 L0 = processor.getL0(this);
6269 Resource property = getSingleObject(subject, relation);
6270 String typeText = (String)getRelatedValue(property, L0.HasValueType, Bindings.STRING);
6272 return org.simantics.scl.compiler.types.Types.parseType(typeText);
6273 } catch (SCLTypeParseException e) {
6274 throw new DatabaseException(e);
6278 private static ThreadLocal<Boolean> syncGraph = new ThreadLocal<Boolean>() {
6279 protected Boolean initialValue() {
6285 public boolean setSynchronous(boolean value) {
6286 boolean old = getSynchronous();
6287 syncGraph.set(value);
6292 public boolean getSynchronous() {
6293 return syncGraph.get();
6296 public void ensureLoaded(int resource) {
6297 processor.querySupport.ensureLoaded(this, resource);
6300 public void ensureLoaded(int resource, int predicate) {
6301 processor.querySupport.ensureLoaded(this, resource, predicate);
6304 public byte[] getValue(int resource) {
6305 return processor.querySupport.getValue(this, resource);
6308 public int thread(int resource) {
6309 return (resource >>> 16) & processor.THREAD_MASK;
6312 public int thread(Resource resource) {
6313 return (((ResourceImpl)resource).id >>> 16) & processor.THREAD_MASK;
6316 public ResourceSupport getResourceSupport() {
6317 return processor.getResourceSupport();
6321 public Object getModificationCounter() {
6322 return processor.getSession().getModificationCounter();
6326 public boolean performPending() {
6327 return processor.performPending(processor.thread.get());