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.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.HashSet;
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.ComputationalValue;
49 import org.simantics.db.DevelopmentKeys;
50 import org.simantics.db.ExternalValueSupport;
51 import org.simantics.db.ReadGraph;
52 import org.simantics.db.RelationContext;
53 import org.simantics.db.Resource;
54 import org.simantics.db.Session;
55 import org.simantics.db.Statement;
56 import org.simantics.db.adaption.AdaptionService;
57 import org.simantics.db.common.primitiverequest.Adapter;
58 import org.simantics.db.common.primitiverequest.Builtin;
59 import org.simantics.db.common.primitiverequest.DatatypeBinding;
60 import org.simantics.db.common.primitiverequest.ForEachAssertedObject;
61 import org.simantics.db.common.primitiverequest.ForEachAssertedStatement;
62 import org.simantics.db.common.primitiverequest.HasStatement;
63 import org.simantics.db.common.primitiverequest.HasStatementSubject;
64 import org.simantics.db.common.primitiverequest.HasStatementSubjectObject;
65 import org.simantics.db.common.primitiverequest.HasValue;
66 import org.simantics.db.common.primitiverequest.Inverse;
67 import org.simantics.db.common.primitiverequest.IsInheritedFrom;
68 import org.simantics.db.common.primitiverequest.IsInstanceOf;
69 import org.simantics.db.common.primitiverequest.IsSubrelationOf;
70 import org.simantics.db.common.primitiverequest.OrderedSet;
71 import org.simantics.db.common.primitiverequest.PossibleAdapter;
72 import org.simantics.db.common.primitiverequest.PossibleInverse;
73 import org.simantics.db.common.primitiverequest.PossibleObject;
74 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;
75 import org.simantics.db.common.primitiverequest.PossibleRelatedValueImplied;
76 import org.simantics.db.common.primitiverequest.PossibleStatement;
77 import org.simantics.db.common.primitiverequest.PossibleType;
78 import org.simantics.db.common.primitiverequest.PossibleUniqueAdapter;
79 import org.simantics.db.common.primitiverequest.PossibleValue;
80 import org.simantics.db.common.primitiverequest.PossibleValueImplied;
81 import org.simantics.db.common.primitiverequest.RelatedValue;
82 import org.simantics.db.common.primitiverequest.RelatedValueImplied;
83 import org.simantics.db.common.primitiverequest.SingleObject;
84 import org.simantics.db.common.primitiverequest.SingleStatement;
85 import org.simantics.db.common.primitiverequest.SingleType;
86 import org.simantics.db.common.primitiverequest.SingleTypeAny;
87 import org.simantics.db.common.primitiverequest.Types;
88 import org.simantics.db.common.primitiverequest.UniqueAdapter;
89 import org.simantics.db.common.primitiverequest.Value;
90 import org.simantics.db.common.primitiverequest.ValueImplied;
91 import org.simantics.db.common.primitiverequest.VariantValueImplied;
92 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
93 import org.simantics.db.common.procedure.adapter.AsyncProcedureAdapter;
94 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
95 import org.simantics.db.common.procedure.adapter.SyncMultiProcedureAdapter;
96 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
97 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
98 import org.simantics.db.common.procedure.single.SyncReadProcedure;
99 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrErrorProcedure;
100 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrNullProcedure;
101 import org.simantics.db.common.procedure.single.wrapper.ExceptionToNullProcedure;
102 import org.simantics.db.common.procedure.single.wrapper.NullSingleOrNullProcedure;
103 import org.simantics.db.common.procedure.single.wrapper.SingleFunctionalOrNullProcedure;
104 import org.simantics.db.common.procedure.single.wrapper.SingleOrErrorProcedure;
105 import org.simantics.db.common.procedure.single.wrapper.SingleOrNullProcedure;
106 import org.simantics.db.common.procedure.wrapper.NoneToAsyncListener;
107 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiListener;
108 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiProcedure;
109 import org.simantics.db.common.procedure.wrapper.NoneToAsyncProcedure;
110 import org.simantics.db.common.procedure.wrapper.NoneToAsyncSetProcedure;
111 import org.simantics.db.common.procedure.wrapper.NoneToSyncMultiListener;
112 import org.simantics.db.common.procedure.wrapper.NoneToSyncMultiProcedure;
113 import org.simantics.db.common.procedure.wrapper.SyncToAsyncListener;
114 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiListener;
115 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiProcedure;
116 import org.simantics.db.common.procedure.wrapper.SyncToAsyncProcedure;
117 import org.simantics.db.common.procedure.wrapper.SyncToAsyncSetProcedure;
118 import org.simantics.db.common.request.AdaptValue;
119 import org.simantics.db.common.request.ResourceRead;
120 import org.simantics.db.common.utils.Logger;
121 import org.simantics.db.common.utils.NameUtils;
122 import org.simantics.db.common.validation.L0Validations;
123 import org.simantics.db.exception.AdaptionException;
124 import org.simantics.db.exception.ArgumentException;
125 import org.simantics.db.exception.AssumptionException;
126 import org.simantics.db.exception.BindingException;
127 import org.simantics.db.exception.DatabaseException;
128 import org.simantics.db.exception.DoesNotContainValueException;
129 import org.simantics.db.exception.EmptyResourceException;
130 import org.simantics.db.exception.InternalException;
131 import org.simantics.db.exception.InvalidLiteralException;
132 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
133 import org.simantics.db.exception.NoInverseException;
134 import org.simantics.db.exception.NoSingleResultException;
135 import org.simantics.db.exception.ResourceNotFoundException;
136 import org.simantics.db.exception.ServiceException;
137 import org.simantics.db.exception.ValidationException;
138 import org.simantics.db.impl.RelationContextImpl;
139 import org.simantics.db.impl.ResourceImpl;
140 import org.simantics.db.impl.internal.RandomAccessValueSupport;
141 import org.simantics.db.impl.internal.ResourceData;
142 import org.simantics.db.impl.procedure.ResultCallWrappedSyncQueryProcedure;
143 import org.simantics.db.impl.query.CacheEntry;
144 import org.simantics.db.impl.query.QueryCache;
145 import org.simantics.db.impl.query.QueryCacheBase;
146 import org.simantics.db.impl.query.QueryProcessor;
147 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
148 import org.simantics.db.impl.query.QuerySupport;
149 import org.simantics.db.impl.query.TripleIntProcedure;
150 import org.simantics.db.impl.support.ResourceSupport;
151 import org.simantics.db.procedure.AsyncListener;
152 import org.simantics.db.procedure.AsyncMultiListener;
153 import org.simantics.db.procedure.AsyncMultiProcedure;
154 import org.simantics.db.procedure.AsyncProcedure;
155 import org.simantics.db.procedure.AsyncSetListener;
156 import org.simantics.db.procedure.Listener;
157 import org.simantics.db.procedure.ListenerBase;
158 import org.simantics.db.procedure.MultiListener;
159 import org.simantics.db.procedure.MultiProcedure;
160 import org.simantics.db.procedure.Procedure;
161 import org.simantics.db.procedure.SetListener;
162 import org.simantics.db.procedure.StatementProcedure;
163 import org.simantics.db.procedure.SyncListener;
164 import org.simantics.db.procedure.SyncMultiListener;
165 import org.simantics.db.procedure.SyncMultiProcedure;
166 import org.simantics.db.procedure.SyncProcedure;
167 import org.simantics.db.procedure.SyncSetListener;
168 import org.simantics.db.request.AsyncMultiRead;
169 import org.simantics.db.request.AsyncRead;
170 import org.simantics.db.request.DelayedWrite;
171 import org.simantics.db.request.DelayedWriteResult;
172 import org.simantics.db.request.ExternalRead;
173 import org.simantics.db.request.MultiRead;
174 import org.simantics.db.request.Read;
175 import org.simantics.db.request.ReadInterface;
176 import org.simantics.db.request.Write;
177 import org.simantics.db.request.WriteInterface;
178 import org.simantics.db.request.WriteOnly;
179 import org.simantics.db.request.WriteOnlyResult;
180 import org.simantics.db.request.WriteResult;
181 import org.simantics.layer0.Layer0;
182 import org.simantics.scl.compiler.types.Type;
183 import org.simantics.scl.compiler.types.exceptions.SCLTypeParseException;
184 import org.simantics.scl.reflection.ReflectionUtils;
185 import org.simantics.scl.reflection.ValueNotFoundException;
186 import org.simantics.scl.runtime.function.Function3;
187 import org.simantics.utils.DataContainer;
188 import org.simantics.utils.Development;
189 import org.simantics.utils.datastructures.Pair;
190 import org.simantics.utils.datastructures.collections.CollectionUtils;
191 import org.slf4j.LoggerFactory;
193 import gnu.trove.map.hash.TObjectIntHashMap;
195 public class ReadGraphImpl implements AsyncReadGraph {
197 private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ReadGraphImpl.class);
199 final static boolean EMPTY_RESOURCE_CHECK = false;
201 final public CacheEntry parent;
202 public final ReadGraphImpl parentGraph;
203 final public QueryProcessor processor;
205 public final AsyncBarrierImpl asyncBarrier;
207 final static Binding DATA_TYPE_BINDING_INTERNAL = Bindings.getBindingUnchecked(Datatype.class);
208 final static Serializer DATA_TYPE_SERIALIZER = Bindings.getSerializerUnchecked(DATA_TYPE_BINDING_INTERNAL);
210 final public static TObjectIntHashMap<String> counters = new TObjectIntHashMap<String>();
212 public static void resetCounters() {
216 public static String listCounters(File file) throws IOException {
218 PrintStream b = new PrintStream(new BufferedOutputStream(new FileOutputStream(file)));
220 for(Pair<String,Integer> p : CollectionUtils.valueSortedEntries(counters)) {
221 b.print(-p.second + " " + p.first + "\n");
226 return "Dumped " + counters.size() + " queries.";
231 * Implementation of the interface ReadGraph
233 final public String getURI(final Resource resource) throws AssumptionException, ValidationException, ServiceException {
235 assert (resource != null);
239 return syncRequest(new org.simantics.db.common.uri.ResourceToURI(resource));
241 } catch (AssumptionException e) {
243 throw new AssumptionException(e);
245 } catch (ValidationException e) {
247 throw new ValidationException(e);
249 } catch (ServiceException e) {
251 throw new ServiceException(e);
253 } catch (DatabaseException e) {
255 throw new ServiceException(INTERNAL_ERROR_STRING, e);
261 final public String getPossibleURI(final Resource resource) throws ValidationException, ServiceException {
263 assert (resource != null);
267 return syncRequest(new org.simantics.db.common.uri.ResourceToPossibleURI(resource));
269 } catch (ValidationException e) {
271 throw new ValidationException(e);
273 } catch (ServiceException e) {
275 throw new ServiceException(e);
277 } catch (DatabaseException e) {
279 throw new ServiceException(INTERNAL_ERROR_STRING, e);
285 final public Resource getResource(final String id)
286 throws ResourceNotFoundException, ValidationException,
293 Integer rid = QueryCache.resultURIToResource(this, id, parent, null);
294 // FIXME: stupid to throw this here and catch and wrap it right away
295 if(rid == 0) throw new ResourceNotFoundException(id);
296 return processor.querySupport.getResource(rid);
298 } catch (ResourceNotFoundException e) {
300 throw new ResourceNotFoundException(id, e);
302 } catch (ValidationException e) {
304 throw new ValidationException(e);
306 } catch (ServiceException e) {
308 throw new ServiceException(e);
310 } catch (DatabaseException e) {
312 throw new ServiceException(INTERNAL_ERROR_STRING, e);
318 final public Resource getPossibleResource(final String id)
319 throws ResourceNotFoundException, ValidationException,
326 return getResource(id);
328 } catch (ResourceNotFoundException e) {
332 } catch (ValidationException e) {
334 throw new ValidationException(e);
336 } catch (ServiceException e) {
338 throw new ServiceException(e);
340 } catch (DatabaseException e) {
342 throw new ServiceException(INTERNAL_ERROR_STRING, e);
349 public Map<String, Resource> getChildren(Resource resource) throws ValidationException, ServiceException {
351 assert (resource != null);
355 int rId = processor.querySupport.getId(resource);
356 return QueryCache.resultChildMap(this, rId, parent, null);
358 } catch (ValidationException e) {
360 throw new ValidationException(e);
362 } catch (ServiceException e) {
364 throw new ServiceException(e);
366 } catch (DatabaseException e) {
368 throw new ServiceException(INTERNAL_ERROR_STRING, e);
374 final public Resource getRootLibrary() {
375 return processor.getRootLibraryResource();
378 final public Resource getBuiltin(final String id)
379 throws ResourceNotFoundException, ServiceException {
385 return syncRequest(new Builtin(id));
387 } catch (ResourceNotFoundException e) {
389 throw new ResourceNotFoundException(id, e);
391 } catch (ServiceException e) {
393 throw new ServiceException(e);
395 } catch (DatabaseException e) {
397 throw new ServiceException(INTERNAL_ERROR_STRING, e);
403 static class StatementReadProcedure extends TIntArrayListInternal implements StatementProcedure {
405 private static Throwable DONE = new Throwable();
407 Throwable exception = null;
409 final ResourceSupport support;
411 public StatementReadProcedure(ResourceSupport support) {
412 this.support = support;
416 public synchronized void execute(AsyncReadGraph graph, int s, int p, int o) {
423 public void finished(AsyncReadGraph graph) {
428 public void exception(AsyncReadGraph graph, Throwable t) {
432 public void checkAndThrow() throws DatabaseException {
433 if(exception != DONE) {
434 if (exception instanceof DatabaseException)
435 throw (DatabaseException) exception;
437 throw new DatabaseException(
438 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
443 public boolean done() {
444 return exception != null;
448 public boolean contains(Object obj) {
449 if(!(obj instanceof InternalStatement))
451 InternalStatement statement = (InternalStatement)obj;
455 for(int i=0;i<sizeInternal();i+=3)
456 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
461 @SuppressWarnings("unchecked")
463 public <T> T[] toArray(T[] a) {
464 int length = sizeInternal() / 3;
465 if(length > a.length) {
466 Class<?> arrayType = a.getClass();
467 a = (arrayType == Object[].class)
468 ? (T[]) new Object[length]
469 : (T[]) Array.newInstance(arrayType.getComponentType(), length);
472 for(int i=length;i<a.length;++i)
475 for(int i=0,j=0;i<sizeInternal();i+=3,++j)
476 a[j] = (T)new InternalStatement(support, getQuick(i), getQuick(i+1), getQuick(i+2));
481 public boolean add(Statement e) {
482 throw new UnsupportedOperationException();
486 public boolean remove(Object o) {
487 throw new UnsupportedOperationException();
491 public boolean addAll(Collection<? extends Statement> c) {
492 throw new UnsupportedOperationException();
495 class IteratorImpl implements ListIterator<Statement> {
499 public IteratorImpl(int index) {
504 public boolean hasNext() {
505 return index < sizeInternal();
509 public Statement next() {
510 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
516 public void remove() {
517 throw new Error("Not supported");
521 public boolean hasPrevious() {
526 public Statement previous() {
528 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
533 public int nextIndex() {
538 public int previousIndex() {
543 public void set(Statement e) {
544 throw new UnsupportedOperationException();
548 public void add(Statement e) {
549 throw new UnsupportedOperationException();
555 public Iterator<Statement> iterator() {
556 return new IteratorImpl(0);
561 return sizeInternal() / 3;
565 public Object[] toArray() {
566 Object[] result = new Object[sizeInternal() / 3];
567 for(int i=0,j=0;j<sizeInternal();i++,j+=3)
568 result[i] = new InternalStatement(support, getQuick(j), getQuick(j+1), getQuick(j+2));
573 public boolean addAll(int index, Collection<? extends Statement> c) {
574 throw new UnsupportedOperationException();
578 public Statement get(int index) {
580 if(index < 0 || index >= sizeInternal())
581 throw new IndexOutOfBoundsException();
582 return new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
586 public Statement set(int index, Statement element) {
587 throw new UnsupportedOperationException();
591 public void add(int index, Statement element) {
592 throw new UnsupportedOperationException();
596 public Statement remove(int index) {
597 throw new UnsupportedOperationException();
601 public int indexOf(Object obj) {
602 if(!(obj instanceof InternalStatement))
604 InternalStatement statement = (InternalStatement)obj;
608 for(int i=0;i<sizeInternal();i+=3)
609 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
615 public int lastIndexOf(Object obj) {
616 if(!(obj instanceof InternalStatement))
618 InternalStatement statement = (InternalStatement)obj;
622 for(int i=sizeInternal()-3;i>=0;i-=3)
623 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
629 public ListIterator<Statement> listIterator() {
630 return new IteratorImpl(0);
634 public ListIterator<Statement> listIterator(int index) {
635 return new IteratorImpl(index*3);
639 public List<Statement> subList(int fromIndex, int toIndex) {
640 if(fromIndex < 0 || toIndex*3 >= sizeInternal() || fromIndex > toIndex)
641 throw new IndexOutOfBoundsException();
642 return new RandomAccessSubList<Statement>(this, fromIndex, toIndex-fromIndex);
647 final public Collection<Statement> getStatements(final Resource subject,
648 final Resource relation)
649 throws ManyObjectsForFunctionalRelationException, ServiceException {
651 assert (subject != null);
652 assert (relation != null);
656 StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
657 processor.forEachStatement(this, subject, relation, procedure);
658 procedure.checkAndThrow();
661 } catch (DatabaseException e) {
663 System.err.println(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation);
665 StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
666 processor.forEachStatement(this, subject, relation, procedure);
668 return Collections.emptyList();
670 // throw new ServiceException(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation, e);
677 final public Collection<Statement> getAssertedStatements(final Resource subject, final Resource relation)
678 throws ManyObjectsForFunctionalRelationException, ServiceException {
680 assert (subject != null);
681 assert (relation != null);
685 return syncRequest(new ForEachAssertedStatement(subject, relation));
687 } catch (ManyObjectsForFunctionalRelationException e) {
689 throw new ManyObjectsForFunctionalRelationException(e);
691 } catch (ServiceException e) {
693 throw new ServiceException(e);
695 } catch (DatabaseException e) {
697 throw new ServiceException(INTERNAL_ERROR_STRING, e);
704 final public Collection<Resource> getPredicates(final Resource subject) throws ServiceException {
706 assert (subject != null);
710 return processor.getPredicates(this, subject);
712 // AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
713 // processor.forEachPredicate(this, subject, procedure);
714 // procedure.checkAndThrow();
717 } catch (ServiceException e) {
719 throw new ServiceException(e);
721 } catch (DatabaseException e) {
723 throw new ServiceException(INTERNAL_ERROR_STRING, e);
725 } catch (Throwable e) {
727 throw new ServiceException(e);
734 final public Collection<Resource> getPrincipalTypes(final Resource subject)
735 throws ServiceException {
737 assert (subject != null);
741 AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
742 processor.forEachPrincipalType(this, subject, procedure);
743 procedure.checkAndThrow();
746 } catch (ServiceException e) {
748 throw new ServiceException(e);
750 } catch (DatabaseException e) {
752 throw new ServiceException(INTERNAL_ERROR_STRING, e);
759 final public Set<Resource> getTypes(final Resource subject) throws ServiceException {
761 assert (subject != null);
765 return processor.getTypes(this, subject);
767 } catch (ServiceException e) {
769 throw new ServiceException(e);
771 } catch (DatabaseException e) {
773 throw new ServiceException(INTERNAL_ERROR_STRING, e);
775 } catch (Throwable e) {
777 throw new ServiceException(e);
784 final public Set<Resource> getSupertypes(final Resource subject)
785 throws ServiceException {
787 assert (subject != null);
791 SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
792 processor.forSupertypes(this, subject, procedure);
793 procedure.checkAndThrow();
794 return procedure.result;
796 } catch (ServiceException e) {
798 throw new ServiceException(e);
800 } catch (DatabaseException e) {
802 throw new ServiceException(INTERNAL_ERROR_STRING, e);
809 final public Set<Resource> getSuperrelations(final Resource subject)
810 throws ServiceException {
812 assert (subject != null);
816 SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
817 processor.forSuperrelations(this, subject, procedure);
818 procedure.checkAndThrow();
819 return procedure.result;
821 } catch (ServiceException e) {
823 throw new ServiceException(e);
825 } catch (DatabaseException e) {
827 throw new ServiceException(INTERNAL_ERROR_STRING, e);
834 public Resource getPossibleSuperrelation(Resource subject) throws ServiceException {
838 SyncReadProcedure<Resource> procedure = new SyncReadProcedure<Resource>();
839 processor.forPossibleSuperrelation(this, subject, procedure);
840 procedure.checkAndThrow();
841 return procedure.result;
843 } catch (ServiceException e) {
845 throw new ServiceException(e);
847 } catch (DatabaseException e) {
849 throw new ServiceException(INTERNAL_ERROR_STRING, e);
856 final public Collection<Resource> getObjects(final Resource subject, final Resource relation)
857 throws ServiceException {
859 assert (subject != null);
860 assert (relation != null);
862 if(Development.DEVELOPMENT) {
863 if(Development.isTrue(DevelopmentKeys.READGRAPH_COUNT)) {
864 counters.adjustOrPutValue("objects $" + subject.getResourceId() + " $" + relation.getResourceId(), 1, 1);
866 //if(subject.getResourceId()==xx && relation.getResourceId()==xx) new Exception().printStackTrace();
871 AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
872 processor.forEachObject(this, subject, relation, procedure);
873 procedure.checkAndThrow();
876 } catch (DatabaseException e) {
878 throw new ServiceException(INTERNAL_ERROR_STRING, e);
885 final public Collection<Resource> getAssertedObjects(
886 final Resource subject, final Resource relation)
887 throws ManyObjectsForFunctionalRelationException, ServiceException {
890 throw new ArgumentException("Subject must not be null.");
891 if (relation == null)
892 throw new ArgumentException("Relation must not be null. Subject=" + subject);
896 return syncRequest(new ForEachAssertedObject(subject, relation));
898 } catch (ManyObjectsForFunctionalRelationException e) {
900 throw new ManyObjectsForFunctionalRelationException(e);
902 } catch (ServiceException e) {
904 throw new ServiceException(e);
906 } catch (DatabaseException e) {
908 throw new ServiceException(INTERNAL_ERROR_STRING, e);
915 final public Resource getInverse(final Resource relation) throws NoInverseException, ServiceException {
917 assert (relation != null);
921 return getSingleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
923 } catch (NoSingleResultException e) {
925 throw new NoInverseException(e);
927 } catch (ServiceException e) {
929 throw new ServiceException(e);
936 final public Resource getSingleObject(final Resource subject, final Resource relation) throws NoSingleResultException, ServiceException {
938 if( subject == null) throw new IllegalArgumentException("subject can not be null");
939 if( relation == null) throw new IllegalArgumentException("relation can not be null");
942 int single = processor.getSingleObject(this, subject, relation);
944 if (EMPTY_RESOURCE_CHECK) {
945 if (!hasStatement(subject)) {
946 throw new EmptyResourceException("Resource " + debugString(subject));
949 throw new NoSingleResultException("No single object for subject " + debugString(subject)
950 + " and relation " + debugString(relation), single);
952 return processor.querySupport.getResource(single);
953 } catch (NoSingleResultException e) {
955 } catch (DatabaseException e) {
956 throw new ServiceException(e);
961 final public Statement getSingleStatement(final Resource subject, final Resource relation) throws NoSingleResultException, ManyObjectsForFunctionalRelationException, ServiceException {
962 assert (subject != null);
963 assert (relation != null);
965 Collection<Statement> statements = getStatements(subject, relation);
966 if (statements.size() == 1) {
967 return statements.iterator().next();
969 if (EMPTY_RESOURCE_CHECK)
970 if (!hasStatement(subject))
971 throw new EmptyResourceException("Resource " + debugString(subject));
972 throw new NoSingleResultException("No single statement for subject " + debugString(subject)
973 + " and relation " + debugString(relation), statements.size());
975 } catch (ServiceException e) {
976 throw new ServiceException(e);
981 final public Resource getSingleType(final Resource subject) throws NoSingleResultException, ServiceException {
982 assert (subject != null);
984 ArrayList<Resource> principalTypes = (ArrayList<Resource>)getPrincipalTypes(subject);
985 if (principalTypes.size() == 1) {
986 return principalTypes.get(0);
988 throw new NoSingleResultException("No single type for subject " + debugString(subject), principalTypes.size());
990 } catch (ServiceException e) {
991 throw new ServiceException(e);
996 final public Resource getSingleType(final Resource subject,
997 final Resource baseType) throws NoSingleResultException,
1000 assert (subject != null);
1001 assert (baseType != null);
1004 return syncRequest(new SingleType(subject, baseType));
1005 } catch (DatabaseException e) {
1006 throw new NoSingleResultException("subject=" + subject + ", baseType=" + baseType, 0, e);
1011 final public <T> T getValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
1013 assert (subject != null);
1017 Layer0 L0 = processor.getL0(this);
1018 int object = processor.getSingleObject(this, subject, L0.HasDataType);
1019 if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
1021 if(processor.isImmutable(object)) {
1022 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1023 return getValue(subject, binding);
1025 byte[] dt = processor.getValue(this, object);
1026 if(dt == null) throw new ServiceException("No data type for " + subject);
1027 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1028 Binding binding = Bindings.getBinding(datatype);
1029 return getValue(subject, binding);
1032 } catch (IOException e) {
1034 throw new ServiceException(e);
1036 } catch (DoesNotContainValueException e) {
1038 throw new DoesNotContainValueException(e, subject);
1040 } catch (ServiceException e) {
1042 throw new ServiceException(e);
1044 } catch (DatabaseException e) {
1046 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1053 final public Variant getVariantValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
1055 assert (subject != null);
1059 Layer0 L0 = processor.getL0(this);
1060 int object = processor.getSingleObject(this, subject, L0.HasDataType);
1061 if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
1063 if(processor.isImmutable(object)) {
1064 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1065 return new Variant(binding, getValue(subject, binding));
1067 byte[] dt = processor.getValue(this, object);
1068 if(dt == null) throw new ServiceException("No data type for " + subject);
1069 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1070 Binding binding = Bindings.getBinding(datatype);
1071 return new Variant(binding, getValue(subject, binding));
1074 } catch (IOException e) {
1076 throw new ServiceException(e);
1078 } catch (DoesNotContainValueException e) {
1080 throw new DoesNotContainValueException(e, subject);
1082 } catch (ServiceException e) {
1084 throw new ServiceException(e);
1086 } catch (DatabaseException e) {
1088 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1093 static final IdentityHashMap<Binding,Serializer> serializers = new IdentityHashMap<Binding,Serializer>();
1096 serializers.put(Bindings.STRING, Bindings.STRING.serializer());
1099 final protected Serializer getSerializer(Binding binding) {
1100 return binding.serializer();
1104 final public <T> T getValue(final Resource subject, final Binding binding) throws DoesNotContainValueException, BindingException,
1107 assert (subject != null);
1109 byte[] bytes = null;
1112 bytes = processor.getValue(this, subject);
1113 if (bytes == null) throw new DoesNotContainValueException("No value for resource " + subject);
1115 Serializer serializer = getSerializer(binding);
1116 return (T)serializer.deserialize(bytes);
1118 } catch (DoesNotContainValueException e) {
1120 throw new DoesNotContainValueException(e);
1122 } catch (Throwable t) {
1123 throw new ServiceException("Could not getValue for subject " + debugString(subject) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(bytes), t);
1128 final public <T> T getRelatedValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1129 DoesNotContainValueException, ServiceException {
1131 assert (subject != null);
1132 assert (relation != null);
1135 Resource object = getSingleObject(subject, relation);
1136 return getValue(object);
1137 } catch (NoSingleResultException e) {
1138 throw new NoSingleResultException("No single value found for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1139 } catch (DoesNotContainValueException e) {
1141 Layer0 L0 = processor.getL0(this);
1142 Resource object = getPossibleObject(subject, relation);
1143 if(isInstanceOf(object, L0.Value)) {
1144 if(isInstanceOf(object, L0.Literal)) {
1145 throw new DoesNotContainValueException(e);
1147 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1150 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1152 } catch (DoesNotContainValueException e2) {
1154 } catch (DatabaseException e2) {
1155 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1157 } catch (ServiceException e) {
1158 throw new ServiceException(e);
1163 final public Variant getRelatedVariantValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1164 DoesNotContainValueException, ServiceException {
1166 assert (subject != null);
1167 assert (relation != null);
1170 Resource object = getSingleObject(subject, relation);
1171 return getVariantValue(object);
1172 } catch (NoSingleResultException e) {
1173 throw new NoSingleResultException("No single object for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1174 } catch (DoesNotContainValueException e) {
1176 Layer0 L0 = processor.getL0(this);
1177 Resource object = getPossibleObject(subject, relation);
1178 if(isInstanceOf(object, L0.Value)) {
1179 if(isInstanceOf(object, L0.Literal)) {
1180 throw new DoesNotContainValueException(e);
1182 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1185 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1187 } catch (DoesNotContainValueException e2) {
1189 } catch (DatabaseException e2) {
1190 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1192 } catch (ServiceException e) {
1193 throw new ServiceException(e);
1198 final public <T> T getRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1199 throws NoSingleResultException, DoesNotContainValueException, BindingException, ServiceException {
1201 assert (subject != null);
1202 assert (relation != null);
1205 Resource object = getSingleObject(subject, relation);
1206 return getValue(object, binding);
1207 } catch (NoSingleResultException e) {
1208 String message = "";
1210 String subjectName = NameUtils.getSafeName(this, subject, true);
1211 String relationName = NameUtils.getSafeName(this, relation, true);
1212 message = "Subject: " + subjectName + ", Relation: " + relationName;
1213 } catch (DatabaseException e2) {
1216 throw new NoSingleResultException(message, e.getResultCount(), e);
1217 } catch (DoesNotContainValueException e) {
1218 throw new DoesNotContainValueException(e);
1219 } catch (ServiceException e) {
1220 throw new ServiceException(e);
1225 final public <T> T adapt(final Resource resource, final Class<T> clazz)
1226 throws AdaptionException, ValidationException, ServiceException {
1228 assert (resource != null);
1229 assert (clazz != null);
1233 return syncRequest(new Adapter<T>(resource, clazz));
1235 } catch (AdaptionException e) {
1237 throw new AdaptionException(e);
1239 } catch (ValidationException e) {
1241 throw new ValidationException(e);
1243 } catch (ServiceException e) {
1245 throw new ServiceException(e);
1247 } catch (DatabaseException e) {
1249 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1256 final public <T,C> T adaptContextual(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1257 throws AdaptionException, ValidationException, ServiceException {
1259 assert (resource != null);
1260 assert (context != null);
1262 class ContextualAdapter implements AsyncRead<T> {
1264 final private Resource resource;
1265 final private C context;
1266 final private Class<T> clazz;
1269 public int hashCode() {
1270 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1274 final public int threadHash() {
1275 return resource.getThreadHash();
1279 public boolean equals(Object object) {
1282 else if (object == null)
1284 else if (getClass() != object.getClass())
1286 ContextualAdapter r = (ContextualAdapter)object;
1287 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1291 public int getFlags() {
1295 public ContextualAdapter(Resource resource, C context, Class<T> clazz) {
1296 this.resource = resource;
1297 this.context = context;
1302 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1304 final AdaptionService service = getSession().peekService(AdaptionService.class);
1305 if (service == null)
1306 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1308 service.adapt(graph, resource, context, contextClass, clazz, false, procedure);
1313 public String toString() {
1314 return "Adapter for (" + resource + "," + context + ") as " + clazz.getName();
1321 return syncRequest(new ContextualAdapter(resource, context, clazz));
1323 } catch (AdaptionException e) {
1325 throw new AdaptionException(e);
1327 } catch (ValidationException e) {
1329 throw new ValidationException(e);
1331 } catch (ServiceException e) {
1333 throw new ServiceException(e);
1335 } catch (DatabaseException e) {
1337 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1344 final public <T> T adaptRelated(final Resource resource, final Resource relation, final Class<T> clazz)
1345 throws AdaptionException, NoSingleResultException, ValidationException, ServiceException {
1347 assert (resource != null);
1348 assert (clazz != null);
1350 Statement stm = getSingleStatement(resource, relation);
1352 Object o = adaptContextual(stm.getObject(), new RelationContextImpl(resource, stm), RelationContext.class, clazz);
1353 if (clazz.isInstance(o))
1355 throw new AdaptionException("Returned value is not expected class , got " + o.getClass().getName()+ " , expected " + clazz.getName());
1360 final public <T> T getPossibleRelatedAdapter(final Resource resource, final Resource relation, final Class<T> clazz)
1361 throws ValidationException, ServiceException {
1364 return adaptRelated(resource, relation, clazz);
1365 } catch (DatabaseException e) {
1372 final public <T,C> T getPossibleContextualAdapter(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1373 throws ValidationException, ServiceException {
1375 assert (resource != null);
1376 assert (context != null);
1378 class PossibleContextualAdapter implements AsyncRead<T> {
1380 final private Resource resource;
1381 final private C context;
1382 final private Class<T> clazz;
1385 public int hashCode() {
1386 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1390 final public int threadHash() {
1391 return resource.getThreadHash();
1395 public boolean equals(Object object) {
1398 else if (object == null)
1400 else if (getClass() != object.getClass())
1402 PossibleContextualAdapter r = (PossibleContextualAdapter)object;
1403 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1407 public int getFlags() {
1411 public PossibleContextualAdapter(Resource resource, C context, Class<T> clazz) {
1412 this.resource = resource;
1413 this.context = context;
1418 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1420 final AdaptionService service = getSession().peekService(AdaptionService.class);
1421 if (service == null)
1422 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1424 service.adapt(graph, resource, context, contextClass, clazz, true, procedure);
1429 public String toString() {
1430 return "Possible adapter for (" + resource + "," + context + ") as " + clazz.getName();
1437 return syncRequest(new PossibleContextualAdapter(resource, context, clazz));
1439 } catch (ValidationException e) {
1441 throw new ValidationException(e);
1443 } catch (ServiceException e) {
1445 throw new ServiceException(e);
1447 } catch (DatabaseException e) {
1449 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1456 final public <T> T adaptUnique(final Resource resource, final Class<T> clazz)
1457 throws AdaptionException, ValidationException, ServiceException {
1459 assert (resource != null);
1460 assert (clazz != null);
1464 return syncRequest(new UniqueAdapter<T>(resource, clazz));
1466 } catch (AdaptionException e) {
1468 throw new AdaptionException(e);
1470 } catch (ValidationException e) {
1472 throw new ValidationException(e);
1474 } catch (ServiceException e) {
1476 throw new ServiceException(e);
1478 } catch (DatabaseException e) {
1480 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1487 final public Resource getPossibleInverse(final Resource relation)
1488 throws ServiceException {
1490 assert (relation != null);
1494 return getPossibleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
1496 } catch (ServiceException e) {
1498 throw new ServiceException(e);
1500 } catch (DatabaseException e) {
1502 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1509 public Resource getPossibleObject(final Resource subject, final Resource relation)
1510 throws ManyObjectsForFunctionalRelationException, ServiceException {
1512 assert (subject != null);
1513 assert (relation != null);
1517 int result = processor.getSingleObject(this, subject, relation);
1518 if(result == 0) return null;
1520 return processor.querySupport.getResource(result);
1522 } catch (ManyObjectsForFunctionalRelationException e) {
1524 throw new ManyObjectsForFunctionalRelationException("Many objects in " + subject + " for functional relation " + relation);
1526 } catch (DatabaseException e) {
1528 throw new ServiceException(e);
1535 final public Statement getPossibleStatement(final Resource subject, final Resource relation)
1536 throws ManyObjectsForFunctionalRelationException, ServiceException {
1538 assert (subject != null);
1539 assert (relation != null);
1543 Collection<Statement> statements = getStatements(subject, relation);
1544 if(statements.size() == 1) return statements.iterator().next();
1547 } catch (ManyObjectsForFunctionalRelationException e) {
1549 throw new ManyObjectsForFunctionalRelationException("Many objects in " + subject + " for functional relation " + relation);
1551 } catch (ServiceException e) {
1553 throw new ServiceException(e);
1560 final public Resource getPossibleType(final Resource subject, final Resource baseType) throws ServiceException {
1562 assert (subject != null);
1563 assert (baseType != null);
1567 AsyncReadProcedure<Resource> procedure = new AsyncReadProcedure<Resource>();
1568 forPossibleType(subject, baseType, procedure);
1569 procedure.checkAndThrow();
1570 return procedure.result;
1572 } catch (ServiceException e) {
1574 throw new ServiceException(e);
1576 } catch (DatabaseException e) {
1578 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1585 final public <T> T getPossibleValue(final Resource subject) throws ServiceException {
1587 assert (subject != null);
1591 int object = processor.getSingleObject(this, subject, processor.getL0(this).HasDataType);
1592 if(object == 0) return null;
1594 if(processor.isImmutable(object)) {
1595 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1596 return getPossibleValue(subject, binding);
1598 byte[] dt = processor.getValue(this, object);
1599 if(dt == null) return null;
1600 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1601 Binding binding = Bindings.getBinding(datatype);
1602 return getPossibleValue(subject, binding);
1605 } catch (IOException e) {
1607 throw new ServiceException(e);
1609 } catch (ServiceException e) {
1611 throw new ServiceException(e);
1613 } catch (DatabaseException e) {
1615 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1622 final public <T> T getPossibleValue(final Resource subject, final Binding binding) throws BindingException, ServiceException {
1624 assert (subject != null);
1625 assert (binding != null);
1629 byte[] dt = processor.getValue(this, subject);
1630 if(dt == null) return null;
1631 Serializer serializer = getSerializer(binding);
1632 return (T)serializer.deserialize(dt);
1634 } catch (IOException e) {
1636 throw new ServiceException(e);
1638 } catch (BindingException e) {
1640 throw new BindingException(e);
1642 } catch (ServiceException e) {
1644 throw new ServiceException(e);
1646 } catch (DatabaseException e) {
1647 e.printStackTrace();
1648 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1654 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation)
1655 throws ManyObjectsForFunctionalRelationException, ServiceException {
1657 assert (subject != null);
1658 assert (relation != null);
1662 Resource object = getPossibleObject(subject, relation);
1663 if(object == null) return null;
1664 else return getPossibleValue(object);
1666 } catch (ManyObjectsForFunctionalRelationException e) {
1668 throw new ManyObjectsForFunctionalRelationException(e);
1670 } catch (ServiceException e) {
1672 throw new ServiceException(e);
1679 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1680 throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
1682 assert (subject != null);
1683 assert (relation != null);
1684 assert (binding != null);
1688 Resource object = getPossibleObject(subject, relation);
1689 if(object == null) return null;
1690 else return getPossibleValue(object, binding);
1692 } catch (ManyObjectsForFunctionalRelationException e) {
1694 throw new ManyObjectsForFunctionalRelationException(e);
1696 } catch (BindingException e) {
1698 throw new BindingException(e);
1700 } catch (ServiceException e) {
1702 throw new ServiceException(e);
1709 public <T> T getPossibleAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1711 assert (resource != null);
1712 assert (clazz != null);
1716 return syncRequest(new PossibleAdapter<T>(resource, clazz));
1718 } catch (ValidationException e) {
1720 throw new ValidationException(e);
1722 } catch (AdaptionException e) {
1726 } catch (DatabaseException e) {
1728 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1734 public <T> T getPossibleUniqueAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1736 assert (resource != null);
1737 assert (clazz != null);
1741 return syncRequest(new PossibleUniqueAdapter<T>(resource, clazz));
1743 } catch (AdaptionException e) {
1747 } catch (ValidationException e) {
1749 throw new ValidationException(e);
1751 } catch (DatabaseException e) {
1753 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1760 final public boolean isInstanceOf(final Resource resource, final Resource type) throws ServiceException {
1762 assert (resource != null);
1763 assert (type != null);
1765 Set<Resource> resources = getTypes(resource);
1766 // This check was necessary because some of the callers of this method got stuck when the NPE was thrown from here.
1767 if (null == resources)
1770 if(EMPTY_RESOURCE_CHECK) {
1771 if (resources.isEmpty()) {
1772 if(!hasStatement(resource)) throw new EmptyResourceException("Resource " + debugString(resource));
1776 return resources.contains(type);
1781 final public boolean isInheritedFrom(final Resource resource, final Resource type) throws ServiceException {
1783 assert (resource != null);
1784 assert (type != null);
1788 if(resource.equals(type)) return true;
1790 return getSupertypes(resource).contains(type);
1792 } catch (ServiceException e) {
1794 throw new ServiceException(e);
1801 final public boolean isSubrelationOf(final Resource resource, final Resource type) throws ServiceException {
1803 assert (resource != null);
1804 assert (type != null);
1808 if(resource.equals(type)) return true;
1810 return getSuperrelations(resource).contains(type);
1812 } catch (ServiceException e) {
1814 throw new ServiceException(e);
1821 final public boolean hasStatement(final Resource subject) throws ServiceException {
1823 assert (subject != null);
1827 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1828 processor.forHasStatement(this, subject, procedure);
1829 procedure.checkAndThrow();
1830 return procedure.result;
1832 } catch (ServiceException e) {
1834 throw new ServiceException(e);
1836 } catch (DatabaseException e) {
1838 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1845 final public boolean hasStatement(final Resource subject, final Resource relation) throws ServiceException {
1847 assert (subject != null);
1848 assert (relation != null);
1852 Collection<Resource> objects = getObjects(subject, relation);
1853 return !objects.isEmpty();
1855 } catch (ServiceException e) {
1857 throw new ServiceException(e);
1864 final public boolean hasStatement(final Resource subject, final Resource relation, final Resource object) throws ServiceException {
1866 assert (subject != null);
1867 assert (relation != null);
1868 assert (object != null);
1872 for(Resource o : getObjects(subject, relation)) {
1873 if(object.equals(o)) return true;
1878 } catch (ServiceException e) {
1880 throw new ServiceException(e);
1887 final public boolean hasValue(final Resource subject) throws ServiceException {
1889 assert (subject != null);
1893 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1894 processor.forHasValue(this, subject, procedure);
1895 procedure.checkAndThrow();
1896 return procedure.result;
1898 } catch (ServiceException e) {
1900 throw new ServiceException(e);
1902 } catch (DatabaseException e) {
1904 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1910 final AsyncProcedure<?> NONE = new AsyncProcedure<Object>() {
1913 public void execute(AsyncReadGraph graph, Object result) {
1917 public void exception(AsyncReadGraph graph, Throwable throwable) {
1923 * Implementation of the interface RequestProcessor
1927 public <T> T syncRequest(final Read<T> request) throws DatabaseException {
1928 assert (request != null);
1929 return (T)QueryCache.runnerReadEntry(this, request, parent, null, null, true);
1933 public <T> T syncRequest(Read<T> request, SyncListener<T> procedure)
1934 throws DatabaseException {
1935 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
1939 public <T> T syncRequest(Read<T> request, final Listener<T> procedure)
1940 throws DatabaseException {
1941 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
1945 public <T> T syncRequest(final Read<T> request, final AsyncProcedure<T> procedure) throws DatabaseException {
1946 assert (request != null);
1947 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
1948 return (T)QueryCache.runnerReadEntry(this, request, parent, listener, procedure, true);
1952 public <T> T syncRequest(final Read<T> request,
1953 final SyncProcedure<T> procedure) throws DatabaseException {
1954 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
1958 public <T> T syncRequest(Read<T> request, Procedure<T> procedure)
1959 throws DatabaseException {
1960 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
1963 static class AsyncReadProcedure<T> implements AsyncProcedure<T> {
1965 private static Throwable DONE = new Throwable();
1968 Throwable exception = null;
1971 public void execute(AsyncReadGraph graph, T t) {
1977 public void exception(AsyncReadGraph graph, Throwable t) {
1981 public void checkAndThrow() throws DatabaseException {
1982 if(exception != DONE) {
1983 if (exception instanceof DatabaseException)
1984 throw (DatabaseException) exception;
1986 throw new DatabaseException(
1987 "Unexpected exception in ReadGraph.syncRequest(AsyncRead)",
1992 public boolean done() {
1993 return exception != null;
1999 public <T> T syncRequest(final AsyncRead<T> request)
2000 throws DatabaseException {
2002 assert (request != null);
2003 return syncRequest(request, new AsyncProcedureAdapter<>() );
2008 public <T> T syncRequest(AsyncRead<T> request, AsyncListener<T> procedure)
2009 throws DatabaseException {
2010 return syncRequest(request, (AsyncProcedure<T>) procedure);
2014 public <T> T syncRequest(AsyncRead<T> request, SyncListener<T> procedure)
2015 throws DatabaseException {
2016 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
2020 public <T> T syncRequest(AsyncRead<T> request, Listener<T> procedure)
2021 throws DatabaseException {
2022 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
2026 final public <T> T syncRequest(final AsyncRead<T> request,
2027 final AsyncProcedure<T> procedure) throws DatabaseException {
2028 assert (request != null);
2029 ListenerBase listener = getListenerBase(procedure);
2030 return (T)QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure, true);
2034 public <T> T syncRequest(AsyncRead<T> request,
2035 final SyncProcedure<T> procedure) throws DatabaseException {
2036 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2040 final public <T> T syncRequest(final AsyncRead<T> request,
2041 final Procedure<T> procedure) throws DatabaseException {
2042 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2046 public <T> Collection<T> syncRequest(final MultiRead<T> request)
2047 throws DatabaseException {
2049 assert (request != null);
2051 final ArrayList<T> result = new ArrayList<T>();
2052 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2054 syncRequest(request, new SyncMultiProcedure<T>() {
2057 public void execute(ReadGraph graph, T t) {
2058 synchronized (result) {
2064 public void finished(ReadGraph graph) {
2068 public void exception(ReadGraph graph, Throwable t) {
2073 public String toString() {
2074 return "syncRequest(MultiRead) -> " + request;
2079 Throwable t = exception.get();
2081 if (t instanceof DatabaseException)
2082 throw (DatabaseException) t;
2084 throw new DatabaseException(
2085 "Unexpected exception in ReadGraph.syncRequest(Read)",
2094 public <T> Collection<T> syncRequest(MultiRead<T> request,
2095 SyncMultiListener<T> procedure) {
2096 return syncRequest(request, (SyncMultiProcedure<T>)procedure);
2100 public <T> Collection<T> syncRequest(MultiRead<T> request,
2101 MultiListener<T> procedure) {
2102 return syncRequest(request, new NoneToSyncMultiListener<T>(procedure));
2106 public <T> Collection<T> syncRequest(MultiRead<T> request,
2107 SyncMultiProcedure<T> procedure) {
2109 assert (request != null);
2111 ListenerBase listener = getListenerBase(procedure);
2113 final ResultCallWrappedSyncQueryProcedure<T> wrapper = new ResultCallWrappedSyncQueryProcedure<T>(procedure);
2115 if (parent != null || listener != null) {
2117 // Object syncParent = request;
2119 // final ReadGraphImpl newGraph = newSync();
2121 processor.query(this, request, parent, wrapper, listener);
2123 // newGraph.waitAsync(syncParent);
2127 // Object syncParent = request;
2129 // final ReadGraphImpl newGraph = newSync();
2132 request.perform(this, wrapper);
2133 } catch (Throwable t) {
2134 wrapper.exception(this, t);
2139 return wrapper.get();
2144 public <T> Collection<T> syncRequest(MultiRead<T> request,
2145 MultiProcedure<T> procedure) {
2146 return syncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
2149 static class AsyncMultiReadProcedure<T> extends ArrayList<T> implements AsyncMultiProcedure<T> {
2151 private static Throwable DONE = new Throwable();
2153 private static final long serialVersionUID = -6494230465108115812L;
2155 Throwable exception = null;
2158 public synchronized void execute(AsyncReadGraph graph, T t) {
2163 public void finished(AsyncReadGraph graph) {
2168 public void exception(AsyncReadGraph graph, Throwable t) {
2172 public void checkAndThrow() throws DatabaseException {
2173 if(exception != DONE) {
2174 if (exception instanceof DatabaseException)
2175 throw (DatabaseException) exception;
2177 throw new DatabaseException(
2178 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
2183 public boolean done() {
2184 return exception != null;
2190 final public <T> Collection<T> syncRequest(AsyncMultiRead<T> request)
2191 throws DatabaseException {
2193 assert (request != null);
2195 final AsyncMultiReadProcedure<T> procedure = new AsyncMultiReadProcedure<T>();
2197 syncRequest(request, procedure);
2199 procedure.checkAndThrow();
2205 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2206 AsyncMultiListener<T> procedure) {
2207 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2211 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2212 SyncMultiListener<T> procedure) {
2213 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2217 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2218 MultiListener<T> procedure) {
2219 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2222 final private <T> void syncRequest(final AsyncMultiRead<T> request,
2223 final AsyncMultiReadProcedure<T> procedure) {
2225 assert (request != null);
2226 assert (procedure != null);
2228 ListenerBase listener = getListenerBase(procedure);
2230 if (parent != null || listener != null) {
2232 // Object syncParent = request;
2234 // final ReadGraphImpl newGraph = newSync();
2236 processor.query(this, request, parent, procedure, listener);
2238 // newGraph.waitAsync(syncParent);
2239 waitAsyncProcedure(procedure);
2243 // Object syncParent = callerThread == Integer.MIN_VALUE ? null
2246 // final ReadGraphImpl newGraph = newSyncAsync(syncParent);
2251 // ReadGraphImpl sync = newSync();
2252 request.perform(this, procedure);
2253 // sync.waitAsync(null);
2254 waitAsyncProcedure(procedure);
2257 } catch (Throwable t) {
2259 waitAsyncProcedure(procedure);
2270 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2271 final AsyncMultiProcedure<T> procedure) {
2273 assert (request != null);
2274 assert (procedure != null);
2276 ListenerBase listener = getListenerBase(procedure);
2278 if (parent != null || listener != null) {
2280 // Object syncParent = request;
2282 // final ReadGraphImpl newGraph = newSync();
2284 processor.query(this, request, parent, procedure, listener);
2286 // newGraph.waitAsync(syncParent);
2290 // Object syncParent = request;
2292 // final ReadGraphImpl newGraph = newSync();
2296 request.perform(this, new AsyncMultiProcedure<T>() {
2299 public void execute(AsyncReadGraph graph, T result) {
2300 procedure.execute(graph, result);
2304 public void finished(AsyncReadGraph graph) {
2305 procedure.finished(graph);
2309 public void exception(AsyncReadGraph graph, Throwable t) {
2310 procedure.exception(graph, t);
2314 public String toString() {
2315 return "syncRequest(AsyncMultiRead) -> " + procedure;
2320 } catch (Throwable t) {
2332 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2333 final SyncMultiProcedure<T> procedure) {
2334 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2338 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2339 final MultiProcedure<T> procedure) {
2340 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2344 public <T> T syncRequest(final ExternalRead<T> request)
2345 throws DatabaseException {
2347 assert (request != null);
2349 return syncRequest(request, new Procedure<T>() {
2352 public void execute(T t) {
2356 public void exception(Throwable t) {
2360 public String toString() {
2361 return "syncRequest(AsyncRead) -> " + request;
2369 public <T> T syncRequest(ExternalRead<T> request, Listener<T> procedure) throws DatabaseException {
2370 return syncRequest(request, (Procedure<T>) procedure);
2374 final public <T> T syncRequest(final ExternalRead<T> request,
2375 final Procedure<T> procedure) throws DatabaseException {
2377 assert (request != null);
2379 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
2380 return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
2385 public void syncRequest(final Write request) throws DatabaseException {
2387 assert (request != null);
2389 throw new DatabaseException(
2390 "Write operations are not supported during read transactions!");
2395 public <T> T syncRequest(final WriteResult<T> request) throws DatabaseException {
2397 assert (request != null);
2399 throw new DatabaseException(
2400 "Write operations are not supported during read transactions!");
2405 public void syncRequest(final DelayedWrite request)
2406 throws DatabaseException {
2408 assert (request != null);
2410 throw new DatabaseException(
2411 "Write operations are not supported during read transactions!");
2416 public <T> T syncRequest(final DelayedWriteResult<T> request) throws DatabaseException {
2418 assert (request != null);
2420 throw new DatabaseException(
2421 "Write operations are not supported during read transactions!");
2426 public void syncRequest(final WriteOnly request) throws DatabaseException {
2428 assert (request != null);
2430 throw new DatabaseException(
2431 "Write operations are not supported during read transactions!");
2436 public <T> T syncRequest(final WriteOnlyResult<T> request) throws DatabaseException {
2438 assert (request != null);
2440 throw new DatabaseException(
2441 "Write operations are not supported during read transactions!");
2446 public <T> void async(ReadInterface<T> r, AsyncProcedure<T> procedure) {
2447 r.request(this, procedure);
2451 public <T> void async(ReadInterface<T> r, Procedure<T> procedure) {
2452 r.request(this, procedure);
2456 public <T> void async(ReadInterface<T> r, SyncProcedure<T> procedure) {
2457 r.request(this, procedure);
2461 public <T> void async(ReadInterface<T> r, AsyncListener<T> procedure) {
2462 r.request(this, procedure);
2466 public <T> void async(ReadInterface<T> r, Listener<T> procedure) {
2467 r.request(this, procedure);
2471 public <T> void async(ReadInterface<T> r, SyncListener<T> procedure) {
2472 r.request(this, procedure);
2476 public <T> T sync(ReadInterface<T> r) throws DatabaseException {
2477 return r.request(this);
2481 public <T> T sync(WriteInterface<T> r) throws DatabaseException {
2482 return r.request(this);
2486 public <T> void async(WriteInterface<T> r, Procedure<T> procedure) {
2487 r.request(this, procedure);
2491 public <T> void async(WriteInterface<T> r) {
2492 r.request(this, new ProcedureAdapter<T>());
2496 * Implementation of the interface AsyncReadGraph
2500 public void forURI(Resource resource, AsyncListener<String> listener) {
2501 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2506 public void forURI(Resource resource, SyncListener<String> listener) {
2507 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2512 public void forURI(Resource resource, Listener<String> listener) {
2513 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2518 final public void forURI(final Resource resource,
2519 final AsyncProcedure<String> procedure) {
2521 assert (resource != null);
2522 assert (procedure != null);
2524 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2530 public void forURI(Resource resource, SyncProcedure<String> procedure) {
2531 forURI(resource, new SyncToAsyncProcedure<String>(procedure));
2535 public void forURI(Resource resource, Procedure<String> procedure) {
2536 forURI(resource, new NoneToAsyncProcedure<String>(procedure));
2540 public void forResource(String id, AsyncListener<Resource> listener) {
2541 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2546 public void forResource(String id, SyncListener<Resource> listener) {
2547 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2552 public void forResource(String id, Listener<Resource> listener) {
2553 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2558 final public void forResource(final String id,
2559 final AsyncProcedure<Resource> procedure) {
2561 assert (id != null);
2562 assert (procedure != null);
2564 processor.forResource(this, id, procedure);
2569 public void forResource(String id, SyncProcedure<Resource> procedure) {
2570 forResource(id, new SyncToAsyncProcedure<Resource>(procedure));
2574 public void forResource(String id, Procedure<Resource> procedure) {
2575 forResource(id, new NoneToAsyncProcedure<Resource>(procedure));
2579 public void forBuiltin(String id, AsyncListener<Resource> listener) {
2580 asyncRequest(new Builtin(id), listener);
2584 public void forBuiltin(String id, SyncListener<Resource> listener) {
2585 asyncRequest(new Builtin(id), listener);
2589 public void forBuiltin(String id, Listener<Resource> listener) {
2590 asyncRequest(new Builtin(id), listener);
2594 final public void forBuiltin(final String id,
2595 final AsyncProcedure<Resource> procedure) {
2597 assert (id != null);
2598 assert (procedure != null);
2600 processor.forBuiltin(this, id, procedure);
2605 public void forBuiltin(String id, SyncProcedure<Resource> procedure) {
2606 forBuiltin(id, new SyncToAsyncProcedure<Resource>(procedure));
2610 public void forBuiltin(String id, Procedure<Resource> procedure) {
2611 forBuiltin(id, new NoneToAsyncProcedure<Resource>(procedure));
2615 final public void forEachStatement(Resource subject, Resource relation,
2616 AsyncMultiProcedure<Statement> procedure) {
2618 assert (subject != null);
2619 assert (relation != null);
2620 assert (procedure != null);
2622 processor.forEachStatement(this, subject, relation, procedure);
2627 public void forEachStatement(Resource subject, Resource relation,
2628 SyncMultiProcedure<Statement> procedure) {
2629 forEachStatement(subject, relation,
2630 new SyncToAsyncMultiProcedure<Statement>(procedure));
2634 final public void forEachStatement(Resource subject, Resource relation,
2635 MultiProcedure<Statement> procedure) {
2637 assert (subject != null);
2638 assert (relation != null);
2639 assert (procedure != null);
2641 processor.forEachStatement(this, subject, relation, procedure);
2646 final public void forStatementSet(Resource subject, Resource relation,
2647 AsyncSetListener<Statement> procedure) {
2649 assert (subject != null);
2650 assert (relation != null);
2651 assert (procedure != null);
2653 processor.forStatementSet(this, subject, relation, procedure);
2658 final public void forStatementSet(Resource subject, Resource relation,
2659 SyncSetListener<Statement> procedure) {
2660 forStatementSet(subject, relation,
2661 new SyncToAsyncSetProcedure<Statement>(procedure));
2665 public void forStatementSet(Resource subject, Resource relation,
2666 SetListener<Statement> listener) {
2667 forStatementSet(subject, relation,
2668 new NoneToAsyncSetProcedure<Statement>(listener));
2672 final public void forEachAssertedStatement(final Resource subject,
2673 final Resource relation,
2674 final AsyncMultiProcedure<Statement> procedure) {
2676 assert (subject != null);
2677 assert (relation != null);
2678 assert (procedure != null);
2680 processor.forEachAssertedStatement(this, subject, relation, procedure);
2685 public void forEachAssertedStatement(Resource subject, Resource relation,
2686 SyncMultiProcedure<Statement> procedure) {
2687 forEachAssertedStatement(subject, relation,
2688 new SyncToAsyncMultiProcedure<Statement>(procedure));
2692 public void forEachAssertedStatement(Resource subject, Resource relation,
2693 MultiProcedure<Statement> procedure) {
2694 forEachAssertedStatement(subject, relation,
2695 new NoneToAsyncMultiProcedure<Statement>(procedure));
2699 public void forAssertedStatementSet(Resource subject, Resource relation,
2700 AsyncSetListener<Statement> procedure) {
2702 assert (subject != null);
2703 assert (relation != null);
2704 assert (procedure != null);
2706 processor.forAssertedStatementSet(this, subject, relation, procedure);
2711 public void forAssertedStatementSet(Resource subject, Resource relation,
2712 SyncSetListener<Statement> procedure) {
2714 assert (subject != null);
2715 assert (relation != null);
2716 assert (procedure != null);
2718 forAssertedStatementSet(subject, relation,
2719 new SyncToAsyncSetProcedure<Statement>(procedure));
2724 public void forAssertedStatementSet(Resource subject, Resource relation,
2725 SetListener<Statement> procedure) {
2727 assert (subject != null);
2728 assert (relation != null);
2729 assert (procedure != null);
2731 forAssertedStatementSet(subject, relation,
2732 new NoneToAsyncSetProcedure<Statement>(procedure));
2737 final public void forEachPredicate(final Resource subject,
2738 final AsyncMultiProcedure<Resource> procedure) {
2740 assert (subject != null);
2741 assert (procedure != null);
2743 processor.forEachPredicate(this, subject, procedure);
2748 public void forEachPredicate(Resource subject,
2749 SyncMultiProcedure<Resource> procedure) {
2750 forEachPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(
2755 final public void forEachPredicate(final Resource subject,
2756 final MultiProcedure<Resource> procedure) {
2758 assert (subject != null);
2759 assert (procedure != null);
2761 processor.forEachPredicate(this, subject, procedure);
2766 final public void forPredicateSet(final Resource subject,
2767 final AsyncSetListener<Resource> procedure) {
2769 assert (subject != null);
2770 assert (procedure != null);
2772 processor.forPredicateSet(this, subject, procedure);
2777 final public void forPredicateSet(final Resource subject,
2778 final SyncSetListener<Resource> procedure) {
2780 assert (subject != null);
2781 assert (procedure != null);
2783 forPredicateSet(subject, new SyncToAsyncSetProcedure<Resource>(
2789 final public void forPredicateSet(final Resource subject,
2790 final SetListener<Resource> procedure) {
2792 assert (subject != null);
2793 assert (procedure != null);
2795 forPredicateSet(subject, new NoneToAsyncSetProcedure<Resource>(
2801 final public void forEachPrincipalType(final Resource subject,
2802 final AsyncMultiProcedure<Resource> procedure) {
2804 assert (subject != null);
2805 assert (procedure != null);
2807 processor.forEachPrincipalType(this, subject, procedure);
2812 public void forEachPrincipalType(Resource subject,
2813 SyncMultiProcedure<Resource> procedure) {
2814 forEachPrincipalType(subject, new SyncToAsyncMultiProcedure<Resource>(
2819 final public void forEachPrincipalType(final Resource subject,
2820 final MultiProcedure<Resource> procedure) {
2822 assert (subject != null);
2823 assert (procedure != null);
2825 processor.forEachPrincipalType(this, subject, procedure);
2830 final public void forPrincipalTypeSet(final Resource subject,
2831 final AsyncSetListener<Resource> procedure) {
2833 assert (subject != null);
2834 assert (procedure != null);
2836 processor.forPrincipalTypeSet(this, subject, procedure);
2841 final public void forPrincipalTypeSet(final Resource subject,
2842 final SyncSetListener<Resource> procedure) {
2844 assert (subject != null);
2845 assert (procedure != null);
2847 forPrincipalTypeSet(subject, new SyncToAsyncSetProcedure<Resource>(
2853 final public void forPrincipalTypeSet(final Resource subject,
2854 final SetListener<Resource> procedure) {
2856 assert (subject != null);
2857 assert (procedure != null);
2859 forPrincipalTypeSet(subject, new NoneToAsyncSetProcedure<Resource>(
2865 public void forTypes(Resource subject, AsyncListener<Set<Resource>> listener) {
2866 asyncRequest(new Types(subject), listener);
2870 public void forTypes(Resource subject, SyncListener<Set<Resource>> listener) {
2871 asyncRequest(new Types(subject), listener);
2875 public void forTypes(Resource subject, Listener<Set<Resource>> listener) {
2876 asyncRequest(new Types(subject), listener);
2880 final public void forTypes(final Resource subject,
2881 final AsyncProcedure<Set<Resource>> procedure) {
2883 assert (subject != null);
2884 assert (procedure != null);
2886 processor.forTypes(this, subject, procedure);
2891 public void forTypes(Resource subject,
2892 SyncProcedure<Set<Resource>> procedure) {
2893 forTypes(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
2897 public void forTypes(Resource subject, Procedure<Set<Resource>> procedure) {
2898 forTypes(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
2902 public void forSupertypes(Resource subject,
2903 AsyncListener<Set<Resource>> listener) {
2904 asyncRequest(new Types(subject), listener);
2908 public void forSupertypes(Resource subject,
2909 SyncListener<Set<Resource>> listener) {
2910 asyncRequest(new Types(subject), listener);
2914 public void forSupertypes(Resource subject, Listener<Set<Resource>> listener) {
2915 asyncRequest(new Types(subject), listener);
2919 final public void forSupertypes(final Resource subject,
2920 final AsyncProcedure<Set<Resource>> procedure) {
2922 assert (subject != null);
2923 assert (procedure != null);
2925 processor.forSupertypes(this, subject, procedure);
2930 public void forSupertypes(Resource subject,
2931 SyncProcedure<Set<Resource>> procedure) {
2932 forSupertypes(subject, new SyncToAsyncProcedure<Set<Resource>>(
2937 public void forSupertypes(Resource subject,
2938 Procedure<Set<Resource>> procedure) {
2939 forSupertypes(subject, new NoneToAsyncProcedure<Set<Resource>>(
2944 public void forDirectSuperrelations(Resource subject,
2945 AsyncMultiProcedure<Resource> procedure) {
2947 assert (subject != null);
2948 assert (procedure != null);
2950 processor.forDirectSuperrelations(this, subject, procedure);
2955 public void forPossibleSuperrelation(Resource subject, AsyncProcedure<Resource> procedure) {
2957 assert (subject != null);
2958 assert (procedure != null);
2960 processor.forPossibleSuperrelation(this, subject, procedure);
2965 public void forSuperrelations(Resource subject,
2966 AsyncListener<Set<Resource>> listener) {
2967 asyncRequest(new Types(subject), listener);
2971 public void forSuperrelations(Resource subject,
2972 SyncListener<Set<Resource>> listener) {
2973 asyncRequest(new Types(subject), listener);
2977 public void forSuperrelations(Resource subject,
2978 Listener<Set<Resource>> listener) {
2979 asyncRequest(new Types(subject), listener);
2983 final public void forSuperrelations(final Resource subject,
2984 final AsyncProcedure<Set<Resource>> procedure) {
2986 assert (subject != null);
2987 assert (procedure != null);
2989 processor.forSuperrelations(this, subject, procedure);
2994 public void forSuperrelations(Resource subject,
2995 SyncProcedure<Set<Resource>> procedure) {
2996 forSuperrelations(subject, new SyncToAsyncProcedure<Set<Resource>>(
3001 public void forSuperrelations(Resource subject,
3002 Procedure<Set<Resource>> procedure) {
3003 forSuperrelations(subject, new NoneToAsyncProcedure<Set<Resource>>(
3008 final public void forEachObject(final Resource subject, final Resource relation, final AsyncMultiProcedure<Resource> procedure) {
3009 processor.forEachObject(this, subject, relation, procedure);
3013 public void forEachObject(Resource subject, Resource relation,
3014 SyncMultiProcedure<Resource> procedure) {
3015 forEachObject(subject, relation,
3016 new SyncToAsyncMultiProcedure<Resource>(procedure));
3020 public void forEachObject(Resource subject, Resource relation,
3021 MultiProcedure<Resource> procedure) {
3023 processor.forEachObject(this, subject, relation, procedure);
3028 final public void forEachDirectPredicate(final Resource subject, final AsyncProcedure<Set<Resource>> procedure) {
3029 processor.forEachDirectPredicate(this, subject, procedure);
3033 final public void forEachDirectPredicate(final Resource subject, final SyncProcedure<Set<Resource>> procedure) {
3034 forEachDirectPredicate(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
3038 public void forEachDirectPredicate(Resource subject, Procedure<Set<Resource>> procedure) {
3039 forEachDirectPredicate(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
3043 final public void forObjectSet(final Resource subject,
3044 final Resource relation, final AsyncSetListener<Resource> procedure) {
3046 assert (subject != null);
3047 assert (relation != null);
3048 assert (procedure != null);
3050 processor.forObjectSet(this, subject, relation, procedure);
3055 final public void forObjectSet(final Resource subject,
3056 final Resource relation, final SyncSetListener<Resource> procedure) {
3058 assert (subject != null);
3059 assert (relation != null);
3060 assert (procedure != null);
3062 forObjectSet(subject, relation, new SyncToAsyncSetProcedure<Resource>(
3068 final public void forObjectSet(final Resource subject,
3069 final Resource relation, final SetListener<Resource> procedure) {
3071 assert (subject != null);
3072 assert (relation != null);
3073 assert (procedure != null);
3075 forObjectSet(subject, relation, new NoneToAsyncSetProcedure<Resource>(
3081 final public void forEachAssertedObject(final Resource subject,
3082 final Resource relation,
3083 final AsyncMultiProcedure<Resource> procedure) {
3085 assert (subject != null);
3086 assert (relation != null);
3087 assert (procedure != null);
3089 processor.forEachAssertedObject(this, subject, relation, procedure);
3094 public void forEachAssertedObject(Resource subject, Resource relation,
3095 SyncMultiProcedure<Resource> procedure) {
3097 assert (subject != null);
3098 assert (relation != null);
3099 assert (procedure != null);
3101 forEachAssertedObject(subject, relation,
3102 new SyncToAsyncMultiProcedure<Resource>(procedure));
3107 public void forEachAssertedObject(Resource subject, Resource relation,
3108 MultiProcedure<Resource> procedure) {
3110 assert (subject != null);
3111 assert (relation != null);
3112 assert (procedure != null);
3114 forEachAssertedObject(subject, relation,
3115 new NoneToAsyncMultiProcedure<Resource>(procedure));
3120 public void forAssertedObjectSet(Resource subject, Resource relation,
3121 AsyncSetListener<Resource> procedure) {
3123 assert (subject != null);
3124 assert (relation != null);
3125 assert (procedure != null);
3127 processor.forAssertedObjectSet(this, subject, relation, procedure);
3132 public void forAssertedObjectSet(Resource subject, Resource relation,
3133 SyncSetListener<Resource> procedure) {
3135 assert (subject != null);
3136 assert (relation != null);
3137 assert (procedure != null);
3139 forAssertedObjectSet(subject, relation,
3140 new SyncToAsyncSetProcedure<Resource>(procedure));
3145 public void forAssertedObjectSet(Resource subject, Resource relation,
3146 SetListener<Resource> procedure) {
3148 assert (subject != null);
3149 assert (relation != null);
3150 assert (procedure != null);
3152 forAssertedObjectSet(subject, relation,
3153 new NoneToAsyncSetProcedure<Resource>(procedure));
3158 public void forInverse(Resource relation, AsyncListener<Resource> listener) {
3159 asyncRequest(new Inverse(relation), listener);
3163 public void forInverse(Resource relation, SyncListener<Resource> listener) {
3164 asyncRequest(new Inverse(relation), listener);
3168 public void forInverse(Resource relation, Listener<Resource> listener) {
3169 asyncRequest(new Inverse(relation), listener);
3173 final public void forInverse(final Resource relation,
3174 final AsyncProcedure<Resource> procedure) {
3176 assert (relation != null);
3177 assert (procedure != null);
3179 processor.forInverse(this, relation, new AsyncProcedure<Resource>() {
3182 public void execute(AsyncReadGraph graph, Resource result) {
3184 procedure.execute(graph, result);
3186 procedure.exception(graph, new NoInverseException(relation
3192 public void exception(AsyncReadGraph graph, Throwable throwable) {
3193 procedure.exception(graph, throwable);
3197 public String toString() {
3198 return "forInverse -> " + procedure;
3206 public void forInverse(Resource relation, SyncProcedure<Resource> procedure) {
3207 forInverse(relation, new SyncToAsyncProcedure<Resource>(procedure));
3211 public void forInverse(Resource relation, Procedure<Resource> procedure) {
3212 forInverse(relation, new NoneToAsyncProcedure<Resource>(procedure));
3216 public void forSingleObject(Resource subject, Resource relation,
3217 AsyncListener<Resource> listener) {
3218 asyncRequest(new SingleObject(subject, relation), listener);
3222 public void forSingleObject(Resource subject, Resource relation,
3223 SyncListener<Resource> listener) {
3224 asyncRequest(new SingleObject(subject, relation), listener);
3228 public void forSingleObject(Resource subject, Resource relation,
3229 Listener<Resource> listener) {
3230 asyncRequest(new SingleObject(subject, relation), listener);
3234 final public void forSingleObject(final Resource subject,
3235 final Resource relation, final AsyncProcedure<Resource> procedure) {
3237 assert (subject != null);
3238 assert (relation != null);
3239 assert (procedure != null);
3241 processor.forEachObject(this, subject, relation,
3242 new SingleOrErrorProcedure<Resource>(procedure));
3247 public void forSingleObject(Resource subject, Resource relation,
3248 SyncProcedure<Resource> procedure) {
3249 forSingleObject(subject, relation, new SyncToAsyncProcedure<Resource>(
3254 public void forSingleObject(Resource subject, Resource relation,
3255 Procedure<Resource> procedure) {
3256 forSingleObject(subject, relation, new NoneToAsyncProcedure<Resource>(
3261 public void forSingleStatement(Resource subject, Resource relation,
3262 AsyncListener<Statement> listener) {
3263 asyncRequest(new SingleStatement(subject, relation), listener);
3267 public void forSingleStatement(Resource subject, Resource relation,
3268 SyncListener<Statement> listener) {
3269 asyncRequest(new SingleStatement(subject, relation), listener);
3273 public void forSingleStatement(Resource subject, Resource relation,
3274 Listener<Statement> listener) {
3275 asyncRequest(new SingleStatement(subject, relation), listener);
3279 final public void forSingleStatement(final Resource subject,
3280 final Resource relation, final AsyncProcedure<Statement> procedure) {
3282 assert (subject != null);
3283 assert (relation != null);
3284 assert (procedure != null);
3286 processor.forEachStatement(this, subject, relation,
3287 new SingleOrErrorProcedure<Statement>(procedure));
3292 public void forSingleStatement(Resource subject, Resource relation,
3293 SyncProcedure<Statement> procedure) {
3294 forSingleStatement(subject, relation,
3295 new SyncToAsyncProcedure<Statement>(procedure));
3299 public void forSingleStatement(Resource subject, Resource relation,
3300 Procedure<Statement> procedure) {
3301 forSingleStatement(subject, relation,
3302 new NoneToAsyncProcedure<Statement>(procedure));
3306 public void forSingleType(Resource subject,
3307 AsyncListener<Resource> listener) {
3308 asyncRequest(new SingleTypeAny(subject), listener);
3312 public void forSingleType(Resource subject,
3313 SyncListener<Resource> listener) {
3314 asyncRequest(new SingleTypeAny(subject), listener);
3318 public void forSingleType(Resource subject,
3319 Listener<Resource> listener) {
3320 asyncRequest(new SingleTypeAny(subject), listener);
3324 final public void forSingleType(final Resource subject, final AsyncProcedure<Resource> procedure) {
3326 assert (subject != null);
3327 assert (procedure != null);
3329 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3331 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
3334 public void execute(AsyncReadGraph graph, final Resource principalType) {
3335 checkedProcedure.offer(graph, principalType);
3339 public void finished(AsyncReadGraph graph) {
3340 checkedProcedure.dec(graph);
3344 public void exception(AsyncReadGraph graph, Throwable t) {
3345 checkedProcedure.exception(graph, t);
3349 public String toString() {
3350 return "forSingleType -> " + procedure;
3358 public void forSingleType(Resource subject, SyncProcedure<Resource> procedure) {
3359 forSingleType(subject, new SyncToAsyncProcedure<Resource>(
3364 public void forSingleType(Resource subject, Procedure<Resource> procedure) {
3365 forSingleType(subject, new NoneToAsyncProcedure<Resource>(
3370 public void forSingleType(Resource subject, Resource relation,
3371 AsyncListener<Resource> listener) {
3372 asyncRequest(new SingleType(subject, relation), listener);
3376 public void forSingleType(Resource subject, Resource relation,
3377 SyncListener<Resource> listener) {
3378 asyncRequest(new SingleType(subject, relation), listener);
3382 public void forSingleType(Resource subject, Resource relation,
3383 Listener<Resource> listener) {
3384 asyncRequest(new SingleType(subject, relation), listener);
3388 final public void forSingleType(final Resource subject,
3389 final Resource baseType, final AsyncProcedure<Resource> procedure) {
3391 assert (subject != null);
3392 assert (procedure != null);
3394 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3396 processor.forEachPrincipalType(this, subject,
3397 new AsyncMultiProcedureAdapter<Resource>() {
3400 public void execute(AsyncReadGraph graph,
3401 final Resource principalType) {
3403 checkedProcedure.inc();
3405 if(baseType == null) {
3407 checkedProcedure.offer(graph, principalType);
3408 checkedProcedure.dec(graph);
3410 } else if(principalType.equals(baseType)) {
3412 checkedProcedure.offer(graph, principalType);
3413 checkedProcedure.dec(graph);
3417 processor.forSupertypes((ReadGraphImpl)graph, principalType,
3418 new AsyncProcedure<Set<Resource>>() {
3421 public void execute(
3422 AsyncReadGraph graph,
3423 Set<Resource> result) {
3425 if (result.contains(baseType))
3426 checkedProcedure.offer(graph,
3428 checkedProcedure.dec(graph);
3433 public void exception(
3434 AsyncReadGraph graph,
3437 .exception(graph, t);
3447 public void finished(AsyncReadGraph graph) {
3448 checkedProcedure.dec(graph);
3452 public void exception(AsyncReadGraph graph, Throwable t) {
3453 checkedProcedure.exception(graph, t);
3457 public String toString() {
3458 return "forSingleType -> " + procedure;
3466 public void forSingleType(Resource subject, Resource relation,
3467 SyncProcedure<Resource> procedure) {
3468 forSingleType(subject, relation, new SyncToAsyncProcedure<Resource>(
3473 public void forSingleType(Resource subject, Resource relation,
3474 Procedure<Resource> procedure) {
3475 forSingleType(subject, relation, new NoneToAsyncProcedure<Resource>(
3480 public <T> void forValue(Resource subject, Binding binding,
3481 AsyncListener<T> listener) {
3482 asyncRequest(new Value<T>(subject, binding), listener);
3486 public <T> void forValue(Resource subject, Binding binding,
3487 SyncListener<T> listener) {
3488 asyncRequest(new Value<T>(subject, binding), listener);
3492 public <T> void forValue(Resource subject, Binding binding,
3493 Listener<T> listener) {
3494 asyncRequest(new Value<T>(subject, binding), listener);
3498 public <T> void forValue(final Resource resource, final Binding binding,
3499 final AsyncProcedure<T> procedure) {
3501 assert (resource != null);
3502 assert (binding != null);
3503 assert (procedure != null);
3505 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
3508 public void execute(AsyncReadGraph graph, byte[] result) {
3512 if (result == null) {
3513 procedure.exception(graph,
3514 new DoesNotContainValueException(
3515 "No value for resource " + resource));
3519 Serializer serializer = binding.serializer();
3520 // Serializer serializer = Bindings.getSerializer( binding );
3521 Object obj = serializer.deserialize(result);
3522 // if (!binding.isInstance(obj))
3523 // procedure.exception(graph, new ClassCastException(
3524 // "Cannot get value " + obj + " with binding "
3527 procedure.execute(graph, (T) obj);
3529 } catch (Throwable t) {
3530 procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
3535 public void exception(AsyncReadGraph graph, Throwable t) {
3537 procedure.exception(graph, t);
3538 } catch (Throwable t2) {
3539 Logger.defaultLogError(t2);
3544 public String toString() {
3545 return "forValue -> " + procedure;
3552 private static String safeArrayToString(byte[] a) {
3555 int iMax = a.length - 1;
3559 StringBuilder b = new StringBuilder();
3561 for (int i = 0; i < 100; i++) { // limit to first 100 items
3564 return b.append(']').toString();
3567 return b.append(", ... (" + a.length + ")]").toString();
3571 public <T> void forValue(Resource subject, Binding binding,
3572 SyncProcedure<T> procedure) {
3573 forValue(subject, binding, new SyncToAsyncProcedure<T>(procedure));
3577 public <T> void forValue(Resource subject, Binding binding,
3578 Procedure<T> procedure) {
3579 forValue(subject, binding, new NoneToAsyncProcedure<T>(procedure));
3583 public <T> void forValue(Resource subject, AsyncListener<T> listener) {
3584 asyncRequest(new ValueImplied<T>(subject), listener);
3588 public <T> void forValue(Resource subject, SyncListener<T> listener) {
3589 asyncRequest(new ValueImplied<T>(subject), listener);
3593 public <T> void forValue(Resource subject, Listener<T> listener) {
3594 asyncRequest(new ValueImplied<T>(subject), listener);
3598 final public <T> void forValue(final Resource subject, final AsyncProcedure<T> procedure) {
3600 assert (subject != null);
3601 assert (procedure != null);
3603 forRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
3606 public void execute(AsyncReadGraph graph, Datatype type) {
3607 // TODO: consider trying Bindings.getBeanBinding(type);
3608 Binding binding = Bindings.getBinding(type);
3609 graph.forValue(subject, binding, procedure);
3613 public void exception(AsyncReadGraph graph, Throwable throwable) {
3614 procedure.exception(graph, new DoesNotContainValueException("Invalid data type", throwable));
3622 public <T> void forValue(Resource subject, SyncProcedure<T> procedure) {
3623 forValue(subject, new SyncToAsyncProcedure<T>(procedure));
3627 public <T> void forValue(Resource subject, Procedure<T> procedure) {
3628 forValue(subject, new NoneToAsyncProcedure<T>(procedure));
3632 public <T> void forRelatedValue(Resource subject, Resource relation,
3633 AsyncListener<T> listener) {
3634 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3638 public <T> void forRelatedValue(Resource subject, Resource relation,
3639 SyncListener<T> listener) {
3640 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3644 public <T> void forRelatedValue(Resource subject, Resource relation,
3645 Listener<T> listener) {
3646 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3650 final public <T> void forRelatedValue(final Resource subject,
3651 final Resource relation, final AsyncProcedure<T> procedure) {
3653 assert (subject != null);
3654 assert (relation != null);
3655 assert (procedure != null);
3657 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3659 processor.forEachObject(this, subject, relation,
3660 new AsyncMultiProcedureAdapter<Resource>() {
3663 public void execute(AsyncReadGraph graph,
3664 final Resource object) {
3666 checkedProcedure.inc();
3668 graph.forValue(object, new AsyncProcedure<Object>() {
3671 public void execute(AsyncReadGraph graph,
3673 checkedProcedure.offer(graph, (T) result);
3674 checkedProcedure.dec(graph);
3678 public void exception(AsyncReadGraph graph,
3680 checkedProcedure.exception(graph, t);
3684 public String toString() {
3685 return "forRelatedValue -> " + procedure;
3693 public void finished(AsyncReadGraph graph) {
3694 checkedProcedure.dec(graph);
3698 public void exception(AsyncReadGraph graph, Throwable t) {
3699 checkedProcedure.exception(graph, t);
3707 public <T> void forRelatedValue(Resource subject, Resource relation,
3708 SyncProcedure<T> procedure) {
3709 forRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
3714 public <T> void forRelatedValue(Resource subject, Resource relation,
3715 Procedure<T> procedure) {
3716 forRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
3721 public <T> void forRelatedValue(Resource subject, Resource relation,
3722 Binding binding, AsyncListener<T> listener) {
3723 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3727 public <T> void forRelatedValue(Resource subject, Resource relation,
3728 Binding binding, SyncListener<T> listener) {
3729 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3733 public <T> void forRelatedValue(Resource subject, Resource relation,
3734 Binding binding, Listener<T> listener) {
3735 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3739 final public <T> void forRelatedValue(final Resource subject,
3740 final Resource relation, final Binding binding,
3741 final AsyncProcedure<T> procedure) {
3743 assert (subject != null);
3744 assert (relation != null);
3745 assert (binding != null);
3746 assert (procedure != null);
3748 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3750 processor.forEachObject(this, subject, relation,
3751 new AsyncMultiProcedureAdapter<Resource>() {
3754 public void execute(AsyncReadGraph graph,
3755 final Resource object) {
3757 checkedProcedure.inc();
3759 graph.forValue(object, binding, new AsyncProcedure<Object>() {
3762 public void execute(AsyncReadGraph graph,
3765 checkedProcedure.offer(graph,
3767 checkedProcedure.dec(graph);
3771 public void exception(AsyncReadGraph graph,
3773 checkedProcedure.exception(graph, t);
3777 public String toString() {
3778 return "forRelatedValue -> "
3787 public void finished(AsyncReadGraph graph) {
3788 checkedProcedure.dec(graph);
3792 public void exception(AsyncReadGraph graph, Throwable t) {
3793 checkedProcedure.exception(graph, t);
3801 public <T> void forRelatedValue(Resource subject, Resource relation,
3802 Binding binding, SyncProcedure<T> procedure) {
3803 forRelatedValue(subject, relation, binding,
3804 new SyncToAsyncProcedure<T>(procedure));
3808 public <T> void forRelatedValue(Resource subject, Resource relation,
3809 Binding binding, Procedure<T> procedure) {
3810 forRelatedValue(subject, relation, binding,
3811 new NoneToAsyncProcedure<T>(procedure));
3815 public <T> void forAdapted(Resource resource, Class<T> clazz,
3816 AsyncListener<T> listener) {
3817 asyncRequest(new Adapter<T>(resource, clazz), listener);
3821 public <T> void forAdapted(Resource resource, Class<T> clazz,
3822 SyncListener<T> listener) {
3823 asyncRequest(new Adapter<T>(resource, clazz), listener);
3827 public <T> void forAdapted(Resource resource, Class<T> clazz,
3828 Listener<T> listener) {
3829 asyncRequest(new Adapter<T>(resource, clazz), listener);
3833 final public <T> void forAdapted(final Resource resource,
3834 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3836 assert (resource != null);
3837 assert (clazz != null);
3838 assert (procedure != null);
3840 final AdaptionService service = getSession().peekService(AdaptionService.class);
3841 if (service == null)
3842 procedure.exception(this, new ServiceException("No AdaptionService available"));
3844 service.adapt(this, resource, resource, Resource.class, clazz, false, procedure);
3849 public <T> void forAdapted(Resource resource, Class<T> clazz,
3850 SyncProcedure<T> procedure) {
3851 forAdapted(resource, clazz, new SyncToAsyncProcedure<T>(procedure));
3855 public <T> void forAdapted(Resource resource, Class<T> clazz,
3856 Procedure<T> procedure) {
3857 forAdapted(resource, clazz, new NoneToAsyncProcedure<T>(procedure));
3861 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3862 AsyncListener<T> listener) {
3863 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3867 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3868 SyncListener<T> listener) {
3869 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3873 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3874 Listener<T> listener) {
3875 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3879 final public <T> void forUniqueAdapted(final Resource resource,
3880 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3882 assert (resource != null);
3883 assert (clazz != null);
3884 assert (procedure != null);
3886 final AdaptionService service = getSession().peekService(AdaptionService.class);
3887 if (service == null)
3888 procedure.exception(this, new ServiceException("No AdaptionService available"));
3890 service.adaptNew(this, resource, clazz, false, procedure);
3895 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3896 SyncProcedure<T> procedure) {
3897 forUniqueAdapted(resource, clazz,
3898 new SyncToAsyncProcedure<T>(procedure));
3902 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3903 Procedure<T> procedure) {
3904 forUniqueAdapted(resource, clazz,
3905 new NoneToAsyncProcedure<T>(procedure));
3909 public void forPossibleInverse(Resource subject,
3910 AsyncListener<Resource> listener) {
3911 asyncRequest(new PossibleInverse(subject), listener);
3915 public void forPossibleInverse(Resource subject,
3916 SyncListener<Resource> listener) {
3917 asyncRequest(new PossibleInverse(subject), listener);
3921 public void forPossibleInverse(Resource subject, Listener<Resource> listener) {
3922 asyncRequest(new PossibleInverse(subject), listener);
3926 final public void forPossibleInverse(final Resource relation,
3927 final AsyncProcedure<Resource> procedure) {
3929 assert (relation != null);
3930 assert (procedure != null);
3932 processor.forInverse(this, relation, new ExceptionToNullProcedure<Resource>(procedure));
3937 public void forPossibleInverse(Resource subject,
3938 SyncProcedure<Resource> procedure) {
3939 forPossibleInverse(subject, new SyncToAsyncProcedure<Resource>(
3944 public void forPossibleInverse(Resource subject,
3945 Procedure<Resource> procedure) {
3946 forPossibleInverse(subject, new NoneToAsyncProcedure<Resource>(
3951 public void forPossibleObject(Resource subject, Resource relation,
3952 AsyncListener<Resource> listener) {
3953 asyncRequest(new PossibleObject(subject, relation), listener);
3957 public void forPossibleObject(Resource subject, Resource relation,
3958 SyncListener<Resource> listener) {
3959 asyncRequest(new PossibleObject(subject, relation), listener);
3963 public void forPossibleObject(Resource subject, Resource relation,
3964 Listener<Resource> listener) {
3965 asyncRequest(new PossibleObject(subject, relation), listener);
3969 final public void forPossibleObject(final Resource subject,
3970 final Resource relation, final AsyncProcedure<Resource> procedure) {
3972 assert (subject != null);
3973 assert (relation != null);
3974 assert (procedure != null);
3976 processor.forEachObject(this, subject, relation,
3977 new SingleOrNullProcedure<Resource>(procedure));
3982 public void forPossibleObject(Resource subject, Resource relation,
3983 SyncProcedure<Resource> procedure) {
3984 forPossibleObject(subject, relation,
3985 new SyncToAsyncProcedure<Resource>(procedure));
3989 public void forPossibleObject(Resource subject, Resource relation,
3990 Procedure<Resource> procedure) {
3991 forPossibleObject(subject, relation,
3992 new NoneToAsyncProcedure<Resource>(procedure));
3996 public void forPossibleStatement(Resource subject, Resource relation,
3997 AsyncListener<Statement> listener) {
3998 asyncRequest(new PossibleStatement(subject, relation), listener);
4002 public void forPossibleStatement(Resource subject, Resource relation,
4003 SyncListener<Statement> listener) {
4004 asyncRequest(new PossibleStatement(subject, relation), listener);
4008 public void forPossibleStatement(Resource subject, Resource relation,
4009 Listener<Statement> listener) {
4010 asyncRequest(new PossibleStatement(subject, relation), listener);
4014 final public void forPossibleStatement(final Resource subject,
4015 final Resource relation, final AsyncProcedure<Statement> procedure) {
4017 assert (subject != null);
4018 assert (relation != null);
4019 assert (procedure != null);
4021 processor.forEachStatement(this, subject, relation,
4022 new SingleFunctionalOrNullProcedure<Statement>(
4023 "forPossibleStatement", procedure));
4028 public void forPossibleStatement(Resource subject, Resource relation,
4029 SyncProcedure<Statement> procedure) {
4030 forPossibleStatement(subject, relation,
4031 new SyncToAsyncProcedure<Statement>(procedure));
4035 public void forPossibleStatement(Resource subject, Resource relation,
4036 Procedure<Statement> procedure) {
4037 forPossibleStatement(subject, relation,
4038 new NoneToAsyncProcedure<Statement>(procedure));
4042 public void forPossibleType(Resource subject, Resource relation,
4043 AsyncListener<Resource> listener) {
4044 asyncRequest(new PossibleType(subject, relation), listener);
4048 public void forPossibleType(Resource subject, Resource relation,
4049 SyncListener<Resource> listener) {
4050 asyncRequest(new PossibleType(subject, relation), listener);
4054 public void forPossibleType(Resource subject, Resource relation,
4055 Listener<Resource> listener) {
4056 asyncRequest(new PossibleType(subject, relation), listener);
4060 final public void forPossibleType(final Resource subject,
4061 final Resource baseType, final AsyncProcedure<Resource> procedure) {
4063 assert (subject != null);
4064 assert (procedure != null);
4066 final NullSingleOrNullProcedure<Resource> checkedProcedure = new NullSingleOrNullProcedure<Resource>(procedure);
4068 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
4071 public void execute(AsyncReadGraph graph,
4072 final Resource principalType) {
4074 if (baseType == null) {
4076 checkedProcedure.offer(graph, principalType);
4078 } else if (principalType.equals(baseType)) {
4080 checkedProcedure.offer(graph, principalType);
4084 checkedProcedure.inc();
4086 processor.forSupertypes((ReadGraphImpl)graph, principalType,
4087 new AsyncProcedure<Set<Resource>>() {
4090 public void execute(
4091 AsyncReadGraph graph,
4092 Set<Resource> result) {
4094 if (result.contains(baseType)) {
4095 checkedProcedure.offer(graph,
4099 checkedProcedure.dec(graph);
4104 public void exception(
4105 AsyncReadGraph graph,
4107 checkedProcedure.exception(graph, t);
4108 checkedProcedure.dec(graph);
4112 public String toString() {
4113 return "forPossibleType -> "
4124 public void finished(AsyncReadGraph graph) {
4125 checkedProcedure.dec(graph);
4129 public void exception(AsyncReadGraph graph, Throwable t) {
4130 checkedProcedure.exception(graph, t);
4131 checkedProcedure.dec(graph);
4139 public void forPossibleType(Resource subject, Resource relation,
4140 SyncProcedure<Resource> procedure) {
4141 forPossibleType(subject, relation, new SyncToAsyncProcedure<Resource>(
4146 public void forPossibleType(Resource subject, Resource relation,
4147 Procedure<Resource> procedure) {
4148 forPossibleType(subject, relation, new NoneToAsyncProcedure<Resource>(
4153 public <T> void forPossibleValue(Resource subject, AsyncListener<T> listener) {
4154 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4158 public <T> void forPossibleValue(Resource subject, SyncListener<T> listener) {
4159 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4163 public <T> void forPossibleValue(Resource subject, Listener<T> listener) {
4164 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4168 final public <T> void forPossibleValue(final Resource subject,
4169 final AsyncProcedure<T> procedure) {
4171 assert (subject != null);
4172 assert (procedure != null);
4174 forPossibleRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
4177 public void execute(AsyncReadGraph graph, final Datatype type) {
4179 procedure.execute(graph, null);
4182 // TODO: consider trying Bindings.getBeanBinding(type);
4183 Binding binding = Bindings.getBinding(type);
4184 graph.forPossibleValue(subject, binding, procedure);
4185 } catch (RuntimeBindingConstructionException e) {
4186 procedure.exception(graph, e);
4192 public void exception(AsyncReadGraph graph, Throwable t) {
4193 procedure.exception(graph, t);
4197 public String toString() {
4198 return "forPossibleValue -> " + procedure;
4206 public <T> void forPossibleValue(Resource subject,
4207 SyncProcedure<T> procedure) {
4208 forPossibleValue(subject, new SyncToAsyncProcedure<T>(procedure));
4212 public <T> void forPossibleValue(Resource subject, Procedure<T> procedure) {
4213 forPossibleValue(subject, new NoneToAsyncProcedure<T>(procedure));
4217 public <T> void forPossibleValue(Resource subject, Binding binding,
4218 AsyncListener<T> listener) {
4219 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4223 public <T> void forPossibleValue(Resource subject, Binding binding,
4224 SyncListener<T> listener) {
4225 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4229 public <T> void forPossibleValue(Resource subject, Binding binding,
4230 Listener<T> listener) {
4231 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4235 final public <T> void forPossibleValue(final Resource resource,
4236 final Binding binding, final AsyncProcedure<T> procedure) {
4238 assert (resource != null);
4239 assert (binding != null);
4240 assert (procedure != null);
4242 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
4245 public void execute(AsyncReadGraph graph, byte[] result) {
4249 if (result == null) {
4250 procedure.execute(graph, null);
4254 Serializer serializer = Bindings.getSerializer( binding );
4255 Object obj = serializer.deserialize(result);
4256 if (!binding.isInstance(obj))
4257 procedure.exception(graph, new ClassCastException(
4258 "Cannot get value " + obj + " with binding "
4261 procedure.execute(graph, (T) obj);
4263 } catch (Throwable t) {
4264 procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
4269 public void exception(AsyncReadGraph graph, Throwable t) {
4271 procedure.exception(graph, t);
4272 } catch (Throwable t2) {
4273 Logger.defaultLogError(t2);
4278 public String toString() {
4279 return "forPossibleValue -> " + procedure;
4287 public <T> void forPossibleValue(Resource subject, Binding binding,
4288 SyncProcedure<T> procedure) {
4289 forPossibleValue(subject, binding, new SyncToAsyncProcedure<T>(
4294 public <T> void forPossibleValue(Resource subject, Binding binding,
4295 Procedure<T> procedure) {
4296 forPossibleValue(subject, binding, new NoneToAsyncProcedure<T>(
4301 public <T> void forPossibleRelatedValue(Resource subject,
4302 Resource relation, AsyncListener<T> listener) {
4303 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4308 public <T> void forPossibleRelatedValue(Resource subject,
4309 Resource relation, SyncListener<T> listener) {
4310 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4315 public <T> void forPossibleRelatedValue(Resource subject,
4316 Resource relation, Listener<T> listener) {
4317 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4322 final public <T> void forPossibleRelatedValue(final Resource subject,
4323 final Resource relation, final AsyncProcedure<T> procedure) {
4325 assert (subject != null);
4326 assert (relation != null);
4327 assert (procedure != null);
4329 final DeepSingleOrNullProcedure<T> checkedProcedure = new DeepSingleOrNullProcedure<T>(procedure);
4331 processor.forEachObject(this, subject, relation,
4332 new AsyncMultiProcedureAdapter<Resource>() {
4335 public void execute(AsyncReadGraph graph,
4336 final Resource object) {
4338 checkedProcedure.inc();
4340 graph.forValue(object, new AsyncProcedure<Object>() {
4343 public void execute(AsyncReadGraph graph,
4345 checkedProcedure.offer(graph, (T) result);
4346 checkedProcedure.dec(graph);
4350 public void exception(AsyncReadGraph graph,
4352 checkedProcedure.exception(graph, t);
4353 checkedProcedure.dec(graph);
4361 public void finished(AsyncReadGraph graph) {
4363 checkedProcedure.dec(graph);
4367 public void exception(AsyncReadGraph graph, Throwable t) {
4368 checkedProcedure.exception(graph, t);
4369 checkedProcedure.dec(graph);
4373 public String toString() {
4374 return "forPossibleRelatedValue -> " + procedure;
4381 public <T> void forPossibleRelatedValue(Resource subject,
4382 Resource relation, SyncProcedure<T> procedure) {
4383 forPossibleRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
4388 public <T> void forPossibleRelatedValue(Resource subject,
4389 Resource relation, Procedure<T> procedure) {
4390 forPossibleRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
4395 public <T> void forPossibleRelatedValue(Resource subject,
4396 Resource relation, Binding binding, AsyncListener<T> listener) {
4397 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4402 public <T> void forPossibleRelatedValue(Resource subject,
4403 Resource relation, Binding binding, SyncListener<T> listener) {
4404 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4409 public <T> void forPossibleRelatedValue(Resource subject,
4410 Resource relation, Binding binding, Listener<T> listener) {
4411 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4415 final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4416 final AsyncProcedure<T> procedure) {
4418 assert (subject != null);
4419 assert (relation != null);
4420 assert (procedure != null);
4422 processor.forPossibleObject(this, subject, relation, new AsyncProcedure<Resource>() {
4425 public void execute(AsyncReadGraph graph, Resource object) {
4427 if(object == null) {
4428 procedure.execute(graph, null);
4432 processor.forPossibleValue((ReadGraphImpl)graph, object, new AsyncProcedure<byte[]>() {
4435 public void execute(AsyncReadGraph graph, byte[] bytes) {
4441 Serializer serializer = binding.serializer();
4442 Object obj = serializer.deserialize(bytes);
4443 if (!binding.isInstance(obj)) {
4444 procedure.exception(graph, new ClassCastException("Cannot get value " + obj + " with binding " + binding));
4446 procedure.execute(graph, (T) obj);
4449 } catch (Throwable t) {
4451 procedure.exception(graph, t);
4457 procedure.execute(graph, null);
4464 public void exception(AsyncReadGraph graph, Throwable t) {
4465 procedure.exception(graph, t);
4473 public void exception(AsyncReadGraph graph, Throwable throwable) {
4474 throwable.printStackTrace();
4475 procedure.exception(graph, throwable);
4483 public <T> void forPossibleRelatedValue(Resource subject,
4484 Resource relation, Binding binding, SyncProcedure<T> procedure) {
4485 forPossibleRelatedValue(subject, relation, binding,
4486 new SyncToAsyncProcedure<T>(procedure));
4490 public <T> void forPossibleRelatedValue(Resource subject,
4491 Resource relation, Binding binding, Procedure<T> procedure) {
4492 forPossibleRelatedValue(subject, relation, binding,
4493 new NoneToAsyncProcedure<T>(procedure));
4497 public void forIsInstanceOf(Resource subject, Resource relation,
4498 AsyncListener<Boolean> listener) {
4499 asyncRequest(new IsInstanceOf(subject, relation), listener);
4503 public void forIsInstanceOf(Resource subject, Resource relation,
4504 SyncListener<Boolean> listener) {
4505 asyncRequest(new IsInstanceOf(subject, relation), listener);
4509 public void forIsInstanceOf(Resource subject, Resource relation,
4510 Listener<Boolean> listener) {
4511 asyncRequest(new IsInstanceOf(subject, relation), listener);
4515 final public void forIsInstanceOf(final Resource resource,
4516 final Resource type, final AsyncProcedure<Boolean> procedure) {
4518 assert (resource != null);
4519 assert (type != null);
4520 assert (procedure != null);
4522 forTypes(resource, new AsyncProcedure<Set<Resource>>() {
4525 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4528 if (result.contains(type))
4529 procedure.execute(graph, true);
4531 procedure.execute(graph, false);
4532 } catch (Throwable t) {
4533 Logger.defaultLogError(t);
4538 public void exception(AsyncReadGraph graph, Throwable t) {
4540 procedure.exception(graph, t);
4541 } catch (Throwable t2) {
4542 Logger.defaultLogError(t2);
4547 public String toString() {
4548 return "forIsInstanceOf -> " + procedure;
4556 public void forIsInstanceOf(Resource subject, Resource relation,
4557 SyncProcedure<Boolean> procedure) {
4558 forIsInstanceOf(subject, relation, new SyncToAsyncProcedure<Boolean>(
4563 public void forIsInstanceOf(Resource subject, Resource relation,
4564 Procedure<Boolean> procedure) {
4565 forIsInstanceOf(subject, relation, new NoneToAsyncProcedure<Boolean>(
4570 public void forIsInheritedFrom(Resource subject, Resource relation,
4571 AsyncListener<Boolean> listener) {
4572 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4576 public void forIsInheritedFrom(Resource subject, Resource relation,
4577 SyncListener<Boolean> listener) {
4578 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4582 public void forIsInheritedFrom(Resource subject, Resource relation,
4583 Listener<Boolean> listener) {
4584 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4588 final public void forIsInheritedFrom(final Resource resource,
4589 final Resource type, final AsyncProcedure<Boolean> procedure) {
4591 assert (resource != null);
4592 assert (type != null);
4593 assert (procedure != null);
4595 if (resource.equals(type)) {
4597 procedure.execute(this, true);
4598 } catch (Throwable t) {
4599 Logger.defaultLogError(t);
4604 forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
4607 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4609 if (result.contains(type))
4610 procedure.execute(graph, true);
4612 procedure.execute(graph, false);
4613 } catch (Throwable t) {
4614 Logger.defaultLogError(t);
4619 public void exception(AsyncReadGraph graph, Throwable t) {
4621 procedure.exception(graph, t);
4622 } catch (Throwable t2) {
4623 Logger.defaultLogError(t2);
4628 public String toString() {
4629 return "forIsInheritedFrom -> " + procedure;
4637 public void forIsInheritedFrom(Resource subject, Resource relation,
4638 SyncProcedure<Boolean> procedure) {
4639 forIsInheritedFrom(subject, relation,
4640 new SyncToAsyncProcedure<Boolean>(procedure));
4644 public void forIsInheritedFrom(Resource subject, Resource relation,
4645 Procedure<Boolean> procedure) {
4646 forIsInheritedFrom(subject, relation,
4647 new NoneToAsyncProcedure<Boolean>(procedure));
4651 public void forIsSubrelationOf(Resource subject, Resource relation,
4652 AsyncListener<Boolean> listener) {
4653 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4657 public void forIsSubrelationOf(Resource subject, Resource relation,
4658 SyncListener<Boolean> listener) {
4659 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4663 public void forIsSubrelationOf(Resource subject, Resource relation,
4664 Listener<Boolean> listener) {
4665 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4669 final public void forIsSubrelationOf(final Resource resource,
4670 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4672 assert (resource != null);
4673 assert (relation != null);
4674 assert (procedure != null);
4676 if (resource.equals(relation)) {
4677 procedure.execute(this, true);
4681 forSuperrelations(resource, new AsyncProcedure<Set<Resource>>() {
4684 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4686 if (result.contains(relation))
4687 procedure.execute(graph, true);
4689 procedure.execute(graph, false);
4690 } catch (Throwable t) {
4691 Logger.defaultLogError(t);
4696 public void exception(AsyncReadGraph graph, Throwable t) {
4698 procedure.exception(graph, t);
4699 } catch (Throwable t2) {
4700 Logger.defaultLogError(t2);
4705 public String toString() {
4706 return "forIsSubrelationOf -> " + procedure;
4714 public void forIsSubrelationOf(Resource subject, Resource relation,
4715 SyncProcedure<Boolean> procedure) {
4716 forIsSubrelationOf(subject, relation,
4717 new SyncToAsyncProcedure<Boolean>(procedure));
4721 public void forIsSubrelationOf(Resource subject, Resource relation,
4722 Procedure<Boolean> procedure) {
4723 forIsSubrelationOf(subject, relation,
4724 new NoneToAsyncProcedure<Boolean>(procedure));
4728 public void forHasStatement(Resource subject,
4729 AsyncListener<Boolean> listener) {
4730 asyncRequest(new HasStatementSubject(subject), listener);
4734 public void forHasStatement(Resource subject, SyncListener<Boolean> listener) {
4735 asyncRequest(new HasStatementSubject(subject), listener);
4739 public void forHasStatement(Resource subject, Listener<Boolean> listener) {
4740 asyncRequest(new HasStatementSubject(subject), listener);
4744 final public void forHasStatement(final Resource subject,
4745 final AsyncProcedure<Boolean> procedure) {
4747 assert (subject != null);
4748 assert (procedure != null);
4750 processor.forHasStatement(this, subject, procedure);
4755 public void forHasStatement(Resource subject,
4756 SyncProcedure<Boolean> procedure) {
4757 forHasStatement(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4761 public void forHasStatement(Resource subject, Procedure<Boolean> procedure) {
4762 forHasStatement(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4766 public void forHasStatement(Resource subject, Resource relation,
4767 AsyncListener<Boolean> listener) {
4768 asyncRequest(new HasStatement(subject, relation), listener);
4772 public void forHasStatement(Resource subject, Resource relation,
4773 SyncListener<Boolean> listener) {
4774 asyncRequest(new HasStatement(subject, relation), listener);
4778 public void forHasStatement(Resource subject, Resource relation,
4779 Listener<Boolean> listener) {
4780 asyncRequest(new HasStatement(subject, relation), listener);
4784 final public void forHasStatement(final Resource subject,
4785 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4787 assert (subject != null);
4788 assert (relation != null);
4789 assert (procedure != null);
4791 processor.forHasStatement(this, subject, relation, procedure);
4796 public void forHasStatement(Resource subject, Resource relation,
4797 SyncProcedure<Boolean> procedure) {
4798 forHasStatement(subject, relation, new SyncToAsyncProcedure<Boolean>(
4803 public void forHasStatement(Resource subject, Resource relation,
4804 Procedure<Boolean> procedure) {
4805 forHasStatement(subject, relation, new NoneToAsyncProcedure<Boolean>(
4810 public void forHasStatement(Resource subject, Resource relation,
4811 Resource object, AsyncListener<Boolean> listener) {
4812 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4817 public void forHasStatement(Resource subject, Resource relation,
4818 Resource object, SyncListener<Boolean> listener) {
4819 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4824 public void forHasStatement(Resource subject, Resource relation,
4825 Resource object, Listener<Boolean> listener) {
4826 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4831 final public void forHasStatement(final Resource subject,
4832 final Resource relation, final Resource object,
4833 final AsyncProcedure<Boolean> procedure) {
4835 assert (subject != null);
4836 assert (relation != null);
4837 assert (object != null);
4838 assert (procedure != null);
4840 processor.forHasStatement(this, subject, relation, object, procedure);
4845 public void forHasStatement(Resource subject, Resource relation,
4846 Resource object, SyncProcedure<Boolean> procedure) {
4847 forHasStatement(subject, relation, object,
4848 new SyncToAsyncProcedure<Boolean>(procedure));
4852 public void forHasStatement(Resource subject, Resource relation,
4853 Resource object, Procedure<Boolean> procedure) {
4854 forHasStatement(subject, relation, object,
4855 new NoneToAsyncProcedure<Boolean>(procedure));
4859 public void forHasValue(Resource subject, AsyncListener<Boolean> listener) {
4860 asyncRequest(new HasValue(subject), listener);
4864 public void forHasValue(Resource subject, SyncListener<Boolean> listener) {
4865 asyncRequest(new HasValue(subject), listener);
4869 public void forHasValue(Resource subject, Listener<Boolean> listener) {
4870 asyncRequest(new HasValue(subject), listener);
4874 final public void forHasValue(final Resource subject,
4875 final AsyncProcedure<Boolean> procedure) {
4877 assert (subject != null);
4878 assert (procedure != null);
4880 processor.forValue(this, subject, new AsyncProcedure<byte[]>() {
4883 public void execute(AsyncReadGraph graph, byte[] result) {
4886 procedure.execute(graph, false);
4888 procedure.execute(graph, true);
4889 } catch (Throwable t) {
4890 Logger.defaultLogError(t);
4895 public void exception(AsyncReadGraph graph, Throwable t) {
4897 procedure.exception(graph, t);
4898 } catch (Throwable t2) {
4899 Logger.defaultLogError(t2);
4904 public String toString() {
4905 return "forHasValue -> " + procedure;
4913 public void forHasValue(Resource subject, SyncProcedure<Boolean> procedure) {
4914 forHasValue(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4918 public void forHasValue(Resource subject, Procedure<Boolean> procedure) {
4919 forHasValue(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4923 public void forOrderedSet(Resource subject,
4924 AsyncMultiListener<Resource> listener) {
4925 asyncRequest(new OrderedSet(subject), listener);
4929 public void forOrderedSet(Resource subject,
4930 SyncMultiListener<Resource> listener) {
4931 asyncRequest(new OrderedSet(subject), listener);
4935 public void forOrderedSet(Resource subject, MultiListener<Resource> listener) {
4936 asyncRequest(new OrderedSet(subject), listener);
4940 final public void forOrderedSet(final Resource subject,
4941 final AsyncMultiProcedure<Resource> procedure) {
4943 assert (subject != null);
4944 assert (procedure != null);
4946 processor.forOrderedSet(this, subject,
4947 new AsyncMultiProcedure<Resource>() {
4950 public void finished(AsyncReadGraph graph) {
4952 procedure.finished(graph);
4953 } catch (Throwable t) {
4954 Logger.defaultLogError(t);
4959 public void execute(AsyncReadGraph graph, Resource result) {
4961 procedure.execute(graph, result);
4962 } catch (Throwable t) {
4963 Logger.defaultLogError(t);
4968 public void exception(AsyncReadGraph graph, Throwable t) {
4970 procedure.exception(graph, t);
4971 } catch (Throwable t2) {
4972 Logger.defaultLogError(t2);
4977 public String toString() {
4978 return "forOrderedSet -> " + procedure;
4986 public void forOrderedSet(Resource subject,
4987 SyncMultiProcedure<Resource> procedure) {
4988 forOrderedSet(subject, new SyncToAsyncMultiProcedure<Resource>(
4993 public void forOrderedSet(Resource subject,
4994 MultiProcedure<Resource> procedure) {
4995 forOrderedSet(subject, new NoneToAsyncMultiProcedure<Resource>(
5000 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5001 AsyncListener<T> listener) {
5002 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5006 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5007 SyncListener<T> listener) {
5008 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5012 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5013 Listener<T> listener) {
5014 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5018 final public <T> void forPossibleAdapted(final Resource resource,
5019 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5021 assert (resource != null);
5022 assert (clazz != null);
5023 assert (procedure != null);
5025 final AdaptionService service = getSession().peekService(AdaptionService.class);
5026 if (service == null)
5027 procedure.exception(this, new ServiceException("No AdaptionService available"));
5029 service.adapt(this, resource, resource, Resource.class, clazz, true, procedure);
5033 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5034 SyncProcedure<T> procedure) {
5035 forPossibleAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5040 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5041 Procedure<T> procedure) {
5042 forPossibleAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5047 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5048 AsyncListener<T> listener) {
5049 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5053 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5054 SyncListener<T> listener) {
5055 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5059 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5060 Listener<T> listener) {
5061 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5065 final public <T> void forPossibleUniqueAdapted(final Resource resource,
5066 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5068 assert (resource != null);
5069 assert (clazz != null);
5070 assert (procedure != null);
5072 final AdaptionService service = getSession().peekService(AdaptionService.class);
5073 if (service == null)
5074 procedure.exception(this, new ServiceException("No AdaptionService available"));
5076 service.adaptNew(this, resource, clazz, true, procedure);
5081 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5082 SyncProcedure<T> procedure) {
5083 forPossibleUniqueAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5088 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5089 Procedure<T> procedure) {
5090 forPossibleUniqueAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5095 * Implementation of the interface AsyncRequestProcessor
5099 final public Session getSession() {
5100 return processor.getSession();
5104 public <T> void asyncRequest(final Read<T> request) {
5106 asyncRequest(request, new AsyncProcedure<T>() {
5109 public void execute(AsyncReadGraph graph, T result) {
5113 public void exception(AsyncReadGraph graph, Throwable t) {
5114 Logger.defaultLogError(t);
5118 public String toString() {
5119 return "asyncRequest(Read) -> " + request;
5127 public <T> void asyncRequest(Read<T> request, AsyncListener<T> procedure) {
5128 asyncRequest(request, (AsyncProcedure<T>) procedure);
5132 public <T> void asyncRequest(Read<T> request,
5133 final SyncListener<T> procedure) {
5134 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5138 public <T> void asyncRequest(Read<T> request, final Listener<T> procedure) {
5139 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5143 public <T> void asyncRequest(final Read<T> request, final AsyncProcedure<T> procedure) {
5145 assert (request != null);
5146 assert (procedure != null);
5148 AsyncBarrierImpl barrier = asyncBarrier;
5152 processor.scheduleNow(new SessionTask(this) {
5155 public void run0(int thread) {
5157 final ListenerBase listener = getListenerBase(procedure);
5158 QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5159 } catch (DatabaseException e) {
5160 Logger.defaultLogError(e);
5171 public static ReadGraphImpl createAsync(QueryProcessor support) {
5172 return new ReadGraphImpl(null, null, support);
5176 public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
5177 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5181 public <T> void asyncRequest(Read<T> request, final Procedure<T> procedure) {
5182 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5186 final public <T> void asyncRequest(final AsyncRead<T> request) {
5188 assert (request != null);
5190 asyncRequest(request, new AsyncProcedure<T>() {
5193 public void execute(AsyncReadGraph graph, T result) {
5197 public void exception(AsyncReadGraph graph, Throwable t) {
5198 Logger.defaultLogError(t);
5202 public String toString() {
5203 return "asyncRequest(AsyncRead) -> " + request;
5211 public <T> void asyncRequest(AsyncRead<T> request,
5212 AsyncListener<T> procedure) {
5213 asyncRequest(request, (AsyncProcedure<T>) procedure);
5217 final public <T> void asyncRequest(AsyncRead<T> request,
5218 final SyncListener<T> procedure) {
5219 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5223 final public <T> void asyncRequest(AsyncRead<T> request,
5224 final Listener<T> procedure) {
5225 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5229 final public <T> void asyncRequest(final AsyncRead<T> request,
5230 final AsyncProcedure<T> procedure) {
5232 assert (request != null);
5233 assert (procedure != null);
5235 AsyncBarrierImpl barrier = asyncBarrier;
5239 processor.scheduleNow(new SessionTask(this) {
5242 public void run0(int thread) {
5248 final ListenerBase listener = getListenerBase(procedure);
5249 QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, new AsyncProcedure<T>() {
5252 public void execute(AsyncReadGraph graph, T result) {
5253 procedure.execute(graph, result);
5259 public void exception(AsyncReadGraph graph, Throwable throwable) {
5260 procedure.exception(graph, throwable);
5266 } catch (DatabaseException e) {
5267 LOGGER.error("Error while executing async request", e);
5279 public <T> void asyncRequest(AsyncRead<T> request,
5280 SyncProcedure<T> procedure) {
5281 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5285 final public <T> void asyncRequest(final AsyncRead<T> request,
5286 final Procedure<T> procedure) {
5287 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5291 public <T> void asyncRequest(final MultiRead<T> request) {
5293 assert (request != null);
5295 asyncRequest(request, new SyncMultiProcedureAdapter<T>() {
5297 public void exception(ReadGraph graph, Throwable t) {
5298 Logger.defaultLogError(t);
5302 public String toString() {
5303 return "asyncRequest(MultiRead) -> " + request;
5310 public <T> void asyncRequest(MultiRead<T> request,
5311 SyncMultiListener<T> procedure) {
5312 asyncRequest(request, (SyncMultiProcedure<T>)procedure);
5316 public <T> void asyncRequest(MultiRead<T> request,
5317 MultiListener<T> procedure) {
5318 asyncRequest(request, new NoneToSyncMultiListener<T>(procedure));
5323 public <T> void asyncRequest(final MultiRead<T> request,
5324 final SyncMultiProcedure<T> procedure) {
5326 assert (request != null);
5327 assert (procedure != null);
5329 final ListenerBase listener = getListenerBase(procedure);
5331 if (parent != null || listener != null) {
5333 // final ReadGraphImpl newGraph = newSync();
5334 processor.query(this, request, parent, procedure,listener);
5338 // final ReadGraphImpl newGraph = newSync();
5342 request.perform(this, procedure);
5344 } catch (Throwable t) {
5347 procedure.exception(this, t);
5348 } catch (DatabaseException e) {
5349 LOGGER.error("Unexpected exception while handling exception", e);
5359 public <T> void asyncRequest(MultiRead<T> request,
5360 MultiProcedure<T> procedure) {
5361 asyncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
5365 final public <T> void asyncRequest(final AsyncMultiRead<T> request) {
5367 assert (request != null);
5369 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5371 public void exception(AsyncReadGraph graph, Throwable t) {
5372 Logger.defaultLogError(t);
5376 public String toString() {
5377 return "asyncRequest(AsyncMultiRead) -> " + request;
5384 public <T> void asyncRequest(AsyncMultiRead<T> request,
5385 AsyncMultiListener<T> procedure) {
5386 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5390 public <T> void asyncRequest(AsyncMultiRead<T> request,
5391 SyncMultiListener<T> procedure) {
5392 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5396 public <T> void asyncRequest(AsyncMultiRead<T> request,
5397 MultiListener<T> procedure) {
5398 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5402 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5403 final AsyncMultiProcedure<T> procedure) {
5405 assert (request != null);
5406 assert (procedure != null);
5408 ListenerBase listener = getListenerBase(procedure);
5410 if (parent != null || listener != null) {
5412 processor.query(this, request, parent, procedure, listener);
5418 request.perform(this, new AsyncMultiProcedure<T>() {
5421 public void execute(AsyncReadGraph graph, T result) {
5422 procedure.execute(graph, result);
5426 public void finished(AsyncReadGraph graph) {
5427 procedure.finished(graph);
5431 public void exception(AsyncReadGraph graph, Throwable t) {
5432 procedure.exception(graph, t);
5436 public String toString() {
5437 return "asyncRequest(AsyncMultiRead) -> " + procedure;
5442 } catch (Throwable t) {
5444 procedure.exception(this, new DatabaseException(t));
5452 public <T> void asyncRequest(AsyncMultiRead<T> request,
5453 SyncMultiProcedure<T> procedure) {
5454 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5458 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5459 final MultiProcedure<T> procedure) {
5460 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5464 final public <T> void asyncRequest(final ExternalRead<T> request) {
5466 assert (request != null);
5468 asyncRequest(request, new Procedure<T>() {
5471 public void execute(T result) {
5475 public void exception(Throwable t) {
5476 Logger.defaultLogError(t);
5480 public String toString() {
5481 return "asyncRequest(PrimitiveRead) -> " + request;
5489 public <T> void asyncRequest(ExternalRead<T> request,
5490 final Listener<T> procedure) {
5491 asyncRequest(request, (Procedure<T>) procedure);
5495 final public <T> void asyncRequest(final ExternalRead<T> request,
5496 final Procedure<T> procedure) {
5498 assert (request != null);
5499 assert (procedure != null);
5501 final ListenerBase listener = getListenerBase(procedure);
5503 if (parent != null || listener != null) {
5506 QueryCacheBase.resultExternalReadEntry(this, request, parent, listener, procedure);
5507 } catch (DatabaseException e) {
5508 Logger.defaultLogError(e);
5509 // This throwable has already been transferred to procedure at this point - do nothing about it
5514 request.register(this, new Listener<T>() {
5516 public void execute(T result) {
5517 procedure.execute(result);
5521 public void exception(Throwable t) {
5522 procedure.exception(t);
5526 public String toString() {
5527 return "asyncRequest(PrimitiveRead) -> " + request;
5531 public boolean isDisposed() {
5542 public void asyncRequest(final Write request) {
5544 assert (request != null);
5546 getSession().asyncRequest(request);
5548 // processor.asyncWrite(request);
5553 public <T> void asyncRequest(final WriteResult<T> request, Procedure<T> procedure) {
5554 throw new Error("Not implemented.");
5558 public void asyncRequest(Write request, Consumer<DatabaseException> callback) {
5560 assert (request != null);
5562 getSession().asyncRequest(request, callback);
5567 public void asyncRequest(final DelayedWrite request) {
5569 assert (request != null);
5571 getSession().asyncRequest(request);
5576 public <T> void asyncRequest(final DelayedWriteResult<T> request, Procedure<T> procedure) {
5577 throw new Error("Not implemented.");
5581 public void asyncRequest(DelayedWrite r,
5582 Consumer<DatabaseException> callback) {
5583 throw new Error("Not implemented.");
5587 public void asyncRequest(final WriteOnly request) {
5589 assert (request != null);
5591 getSession().asyncRequest(request);
5596 public <T> void asyncRequest(final WriteOnlyResult<T> request, Procedure<T> procedure) {
5597 throw new Error("Not implemented.");
5601 public void asyncRequest(WriteOnly r, Consumer<DatabaseException> callback) {
5602 throw new Error("Not implemented.");
5606 * Implementation of the interface ServiceLocator
5610 public <T> T getService(Class<T> api) {
5611 if(WriteSupport.class == api) {
5612 if(this instanceof WriteGraphImpl) {
5613 WriteGraphImpl impl = (WriteGraphImpl)this;
5614 return (T)impl.writeSupport;
5617 return getSession().getService(api);
5621 public <T> T peekService(Class<T> api) {
5622 return getSession().peekService(api);
5626 public boolean hasService(Class<?> api) {
5627 return getSession().hasService(api);
5631 public <T> void registerService(Class<T> api, T service) {
5632 getSession().registerService(api, service);
5636 public boolean isImmutable(Resource resource) throws DatabaseException {
5637 ResourceImpl impl = (ResourceImpl)resource;
5638 return processor.isImmutable(impl.id);
5645 protected static String INTERNAL_ERROR_STRING = "Transaction aborted due to internal client error.";
5648 * callerThread is the currently running thread state.syncThread is blocking for
5649 * this execution state.syncParent is the blocking request
5652 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support) {
5653 this.parentGraph = parentGraph;
5654 this.parent = parent;
5655 this.processor = support;
5656 this.asyncBarrier = prepareBarrier(parentGraph, parent, null, false);
5659 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support, AsyncBarrierImpl asyncBarrier) {
5660 this.parentGraph = parentGraph;
5661 this.parent = parent;
5662 this.processor = support;
5663 this.asyncBarrier = asyncBarrier;
5666 ReadGraphImpl(ReadGraphImpl graph, CacheEntry parent) {
5667 this(graph, parent, graph.processor);
5670 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, Runnable callback, boolean needsToBlock) {
5671 this(parentGraph, parent, parentGraph.processor, prepareBarrier(parentGraph, parent, callback, needsToBlock));
5674 static AsyncBarrierImpl prepareBarrier(ReadGraphImpl parentGraph, CacheEntry parent, Runnable callback, boolean needsToBlock) {
5675 return new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent, callback, needsToBlock);
5678 ReadGraphImpl(ReadGraphImpl graph) {
5679 this(graph, graph.parent);
5682 public ReadGraphImpl withParent(CacheEntry parent, Runnable callback, boolean needsToBlock) {
5683 return new ReadGraphImpl(this, parent, callback, needsToBlock);
5686 public ReadGraphImpl forRecompute(CacheEntry parent) {
5687 return new ReadGraphImpl(null, parent, processor);
5690 public static ReadGraphImpl create(QueryProcessor support) {
5691 ReadGraphImpl result = new ReadGraphImpl(null, null, support);
5695 public ReadGraphImpl newRestart(ReadGraphImpl impl) {
5697 WriteGraphImpl write = processor.getSession().getService(
5698 WriteGraphImpl.class);
5704 final private ListenerBase getListenerBase(final Object procedure) {
5705 if (procedure instanceof ListenerBase)
5706 return (ListenerBase) procedure;
5711 public <T> void waitAsyncProcedure(AsyncMultiReadProcedure<T> procedure) {
5713 assert(procedure.done());
5717 public <T> void waitAsyncProcedure(AsyncReadProcedure<T> procedure) {
5719 assert(procedure.done());
5723 public boolean resumeTasks() {
5724 return processor.resumeTasks(this);
5727 Class<?> singleClass(Set<Resource> types) {
5728 Class<?> result = null;
5729 for (Resource type : types) {
5730 Class<?> clazz = processor.getBuiltinValue(type);
5731 if (clazz != null) {
5741 private String debugString(Resource r) {
5744 name = getPossibleRelatedValue(r, processor.getL0(this).HasName);
5745 } catch (ManyObjectsForFunctionalRelationException e) {
5746 Logger.defaultLogError(e);
5747 } catch (ServiceException e) {
5748 Logger.defaultLogError(e);
5750 return "[" + name + " - " + r + "]";
5754 public String toString() {
5755 return "ReadGraphImpl[thread=" + Thread.currentThread() + "] [parent=" + String.valueOf(parent) + "] [asyncBarrier=" + String.valueOf(asyncBarrier)+ "]";
5759 final public int thread() {
5763 static class MultiTripleIntProcedure implements TripleIntProcedure {
5765 final private AsyncMultiProcedure<Statement> procedure;
5766 final private ReadGraphImpl impl;
5767 final private QuerySupport support;
5769 public MultiTripleIntProcedure(AsyncMultiProcedure<Statement> procedure, ReadGraphImpl impl, QuerySupport support) {
5770 this.procedure = procedure;
5772 this.support = support;
5776 public void execute(ReadGraphImpl graph, int s, int p, int o) {
5778 procedure.execute(graph, support.getStatement(s, p, o));
5779 } catch (Throwable t2) {
5780 Logger.defaultLogError(t2);
5785 public void finished(ReadGraphImpl graph) {
5787 procedure.finished(graph);
5788 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5789 } catch (Throwable t2) {
5790 Logger.defaultLogError(t2);
5795 public void exception(ReadGraphImpl graph, Throwable t) {
5797 procedure.exception(graph, t);
5798 } catch (Throwable t2) {
5799 Logger.defaultLogError(t2);
5801 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5805 public String toString() {
5806 return "forEachObject with " + procedure;
5811 // private AsyncMultiProcedure<Resource> cacheKey = null;
5812 // private MultiIntProcedure cacheResult = null;
5814 // final IntProcedure forMultiProcedure(final AsyncMultiProcedure<Resource> procedure) {
5816 // if(procedure == cacheKey) return cacheResult;
5818 // cacheResult = new MultiIntProcedure(procedure, this, processor.support);
5819 // cacheKey = procedure;
5821 // return cacheResult;
5825 // private AsyncMultiProcedure<Statement> cacheKey2 = null;
5826 // private MultiTripleIntProcedure cacheResult2 = null;
5828 // final synchronized TripleIntProcedure forMultiProcedure(final AsyncMultiProcedure<Statement> procedure) {
5830 // if(procedure == cacheKey2) return cacheResult2;
5832 // cacheResult2 = new MultiTripleIntProcedure(procedure, this, processor.support);
5833 // cacheKey2 = procedure;
5835 // return cacheResult2;
5840 public Datatype getDataType(Resource subject) throws DatabaseException {
5841 for(Resource dataTypeResource : getObjects(subject, processor.getL0(this).HasDataType))
5842 return getValue(dataTypeResource, Bindings.getBindingUnchecked(Datatype.class));
5843 throw new DoesNotContainValueException("The literal has no data type.");
5846 protected <T extends Accessor> T getAccessor4File(Resource subject)
5847 throws DatabaseException {
5850 byte[] bytes = processor.support.getValue(g, subject);
5854 BinaryVariant va = (BinaryVariant) Accessors.getAccessor(bytes, Datatypes.VARIANT);
5855 Accessor ca = va.getContentAccessor();
5857 } catch (AccessorConstructionException e) {
5858 throw new DatabaseException(e);
5864 Binding datatype_binding = Bindings.getBindingUnchecked(DataType.class);
5865 Serializer datatype_serializer = datatype_binding.serializer();
5868 BinaryMemory in = new BinaryMemory(ByteBuffer.wrap(bytes));
5869 datatype = (DataType)datatype_serializer.deserialize(in);
5870 Binding data_binding = Bindings.getBinding(datatype);
5871 Serializer data_serializer = data_binding.serializer(BinarySerializationFormat.INSTANCE);
5872 Object o = data_serializer.deserialize(in);
5874 return (T)Accessors.getAccessor(data_binding, o);
5875 } catch(AccessorConstructionException e) {
5878 } catch (Exception e) {
5879 throw new DatabaseException(e);
5882 @SuppressWarnings("unchecked")
5884 public <T extends Accessor> T getAccessor(Resource subject) throws DatabaseException {
5885 RandomAccessBinary rab = getRandomAccessBinary(subject);
5887 return (T)Accessors.getAccessor(rab, getDataType(subject));
5888 } catch(AccessorConstructionException e) {
5889 throw new DatabaseException(e);
5892 @SuppressWarnings("unchecked")
5893 protected <T extends Accessor> T createAccessor(Resource resource, Datatype datatype, Object intialValue)
5894 throws DatabaseException {
5895 RandomAccessBinary rab = createRandomAccessBinary(resource, datatype, intialValue);
5897 return (T)Accessors.getAccessor(rab, datatype);
5898 } catch(AccessorConstructionException e) {
5899 throw new DatabaseException(e);
5903 public RandomAccessBinary getRandomAccessBinary(Resource subject) throws DatabaseException {
5904 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5905 ResourceData rd = ravs.get(subject);
5909 ExternalValueSupport evs = getService(ExternalValueSupport.class);
5910 long size = evs.getValueSize(this, subject); // Throws DatabaseException if no old external value.
5912 File platform = Platform.getLocation().toFile();
5913 File tempFiles = new File(platform, "tempFiles");
5914 File dbDir = new File(tempFiles, "db");
5916 File file = new File(dbDir, "ResourceFile" + subject.getResourceId());
5917 rd = new ResourceData(new BinaryFile(file), true); // Old external value.
5918 final int N = 1<<20;
5922 int length = N < left ? N : (int)left;
5923 byte[] bytes = evs.readValue(this, subject, offset, length);
5924 offset += bytes.length;
5925 left -= bytes.length;
5926 rd.binaryFile.write(bytes);
5928 ravs.put(subject, rd);
5930 } catch (Exception e) {
5931 throw new DatabaseException("Resource " + subject + " have value but there was problem with accessing value data.", e);
5933 } catch (Exception e) {
5934 if(Development.DEVELOPMENT) {
5935 if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
5936 e.printStackTrace();
5940 Datatype datatype = getDataType(subject);
5941 Object value = getPossibleValue(subject, Bindings.getBinding(datatype));
5942 return createRandomAccessBinary(subject, datatype, value);
5944 public RandomAccessBinary createRandomAccessBinary(Resource resource, Datatype datatype, Object initialValue)
5945 throws DatabaseException {
5946 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5948 File platform = Platform.getLocation().toFile();
5949 File tempFiles = new File(platform, "tempFiles");
5950 File dbDir = new File(tempFiles, "db");
5952 File file = new File(dbDir, "ResourceFile" + resource.getResourceId());
5953 ResourceData rd = new ResourceData(new BinaryFile(file), false);
5954 Binding binding = Bindings.getBinding(datatype);
5955 if (null == initialValue) {
5956 initialValue = binding.createDefault();
5958 Serializer serializer = binding.serializer();
5959 byte[] bytes = serializer.serialize(initialValue);
5960 // In case the file has been previously accessed and was larger we set the correct size now
5961 rd.binaryFile.setLength(bytes.length);
5962 rd.binaryFile.write(bytes);
5963 ravs.put(resource, rd);
5965 } catch (Exception e) {
5966 if (e instanceof DatabaseException)
5967 throw (DatabaseException)e;
5969 throw new DatabaseException(e);
5973 // static class ExternalValueRequest<T> extends ResourceRead<T> {
5975 // public ExternalValueRequest(Resource resource) {
5979 // @SuppressWarnings("unchecked")
5981 // public T perform(ReadGraph graph) throws DatabaseException {
5984 // String uri = graph.getURI(resource);
5985 // if(Layer0.URIs.Functions_functionApplication.equals(uri)) return (T)functionApplication;
5987 // return (T)ReflectionUtils.getValue(uri).getValue();
5989 // } catch(ValueNotFoundException e) {
5990 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
5991 // } catch(ClassCastException e) {
5992 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
5998 @SuppressWarnings("unchecked")
6000 public <T> T getValue2(Resource r, Object context) throws DatabaseException {
6001 Layer0 L0 = processor.getL0(this);
6002 Set<Resource> types = getTypes(r);
6004 if(types.contains(L0.Literal)) {
6005 if(isImmutable(r)) {
6006 return syncRequest(new ValueImplied<T>(r));
6011 else if(types.contains(L0.ExternalValue)) {
6012 return (T)syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6016 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6017 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6019 return function.apply(this, r, context);
6020 } catch(RuntimeException e) {
6021 DatabaseException dte = findPossibleRootException(e);
6022 if(dte != null) throw dte;
6023 else throw new DatabaseException(e);
6030 public Variant getVariantValue2(Resource r, Object context) throws DatabaseException {
6031 Layer0 L0 = processor.getL0(this);
6032 Set<Resource> types = getTypes(r);
6034 if(types.contains(L0.Literal)) {
6035 if(isImmutable(r)) {
6036 return syncRequest(new VariantValueImplied(r));
6038 return getVariantValue(r);
6041 else if(types.contains(L0.ExternalValue)) {
6042 Object value = syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6044 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6045 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6046 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6051 Function3<ReadGraph,Resource,Object,Object> function = requestValueFunction(r);
6052 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6054 Object value = function.apply(this, r, context);
6056 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6057 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6058 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6060 } catch(RuntimeException e) {
6061 DatabaseException dte = findPossibleRootException(e);
6062 if(dte != null) throw dte;
6063 else throw new DatabaseException(e);
6069 public <T> T getPossibleValue2(Resource subject, Object context) throws DatabaseException {
6071 return getValue2(subject, context);
6072 } catch (DatabaseException e) {
6077 static class PossibleConverterFunction<T> extends ResourceRead<Function3<ReadGraph,Resource,Object,T>> {
6079 public PossibleConverterFunction(Resource resource) {
6084 public Function3<ReadGraph,Resource,Object,T> perform(ReadGraph graph) throws DatabaseException {
6085 return compute(graph, resource);
6088 @SuppressWarnings("unchecked")
6089 public static <T> Function3<ReadGraph,Resource,Object,T> compute(ReadGraph graph, Resource resource) throws DatabaseException {
6090 Layer0 L0 = Layer0.getInstance(graph);
6091 for(Resource converter : graph.getObjects(resource, L0.ConvertsToValueWith)) {
6093 if(L0.Functions_functionApplication.equals(converter)) {
6094 return (Function3<ReadGraph,Resource,Object,T>)graph.syncRequest(new AdaptValue(resource));
6096 return graph.getValue2(converter, resource);
6098 } catch(RuntimeException e) {
6099 DatabaseException dte = findPossibleRootException(e);
6100 if(dte != null) throw dte;
6101 else throw new DatabaseException(e);
6109 <T> Function3<ReadGraph,Resource,Object,T> requestValueFunction(Resource r) throws DatabaseException {
6111 return syncRequest(new PossibleConverterFunction<T>(r), TransientCacheAsyncListener.<Function3<ReadGraph,Resource,Object,T>>instance());
6113 return syncRequest(new PossibleConverterFunction<T>(r));
6117 * Get a value associated with a graph {@link Resource}, using a possible context object and
6118 * a desired value binding. The following methods are tried in order to retreive the value:
6120 * <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>
6121 * <li>If the resource is a {@code L0.ExternalValue}, the value is acquired using
6122 * {@link ReflectionUtils#getValue(String)}.</li>
6123 * <li>If the resource is associated with a suitable value converter with relation {@code L0.ConvertsToValueWith}
6124 * (see {@link #requestValueFunction(Resource)}), the value function is called with the graph, the resource
6125 * and the context object.</li>
6128 * @param r A graph resource with which the value is associated
6129 * @param context A context object that is used for acquiring the value (only applied in case 3)
6130 * @param binding A binding for the value type (only applied in case 1)
6131 * @return The value of the graph node.
6132 * @throws DoesNotContainValueException No value is associated with the graph node.
6133 * @throws DatabaseException Other errors, such as an error in casting the value to the return type or
6134 * a runtime error in the value function.
6136 @SuppressWarnings("unchecked")
6138 public <T> T getValue2(Resource r, Object context, Binding binding) throws DatabaseException {
6139 if (binding instanceof ObjectVariantBinding)
6140 return getValue2(r, context);
6142 Layer0 L0 = processor.getL0(this);
6143 Set<Resource> types = getTypes(r);
6144 if(types.contains(L0.Literal)) {
6145 if(isImmutable(r)) {
6146 return syncRequest(new Value<T>(r, binding));
6148 return getValue(r, binding);
6150 } else if(types.contains(L0.ExternalValue)) {
6151 ComputationalValue cv = syncRequest(new PossibleAdapter<ComputationalValue>(r, ComputationalValue.class), TransientCacheAsyncListener.instance());
6153 return cv.getValue(this, r);
6155 // This should not even be possible since L0 defines an adapter for all values
6157 return (T)ReflectionUtils.getValue(getURI(r)).getValue();
6158 } catch(ValueNotFoundException e) {
6159 throw new DatabaseException(e);
6160 } catch(ClassCastException e) {
6161 throw new DatabaseException(e);
6166 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6167 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function.");
6169 Object value = function.apply(this, r, context);
6170 if(binding.isInstance(value)) return (T)value;
6171 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
6172 return (T)Bindings.adapt(value, srcBinding, binding);
6173 } catch(RuntimeException e) {
6174 DatabaseException dte = findPossibleRootException(e);
6175 if(dte != null) throw dte;
6176 else throw new DatabaseException(e);
6177 } catch (AdaptException e) {
6178 throw new DatabaseException(e);
6179 } catch (org.simantics.databoard.binding.error.BindingException e) {
6180 throw new DatabaseException(e);
6186 public <T> T getPossibleValue2(Resource subject, Object context, Binding binding) throws DatabaseException {
6188 return getValue2(subject, context, binding);
6189 } catch (DatabaseException e) {
6194 private static DatabaseException findPossibleRootException(Throwable t) {
6195 if(t == null) return null;
6196 if(t instanceof DatabaseException) return (DatabaseException)t;
6197 if(t instanceof RuntimeException || t instanceof InvocationTargetException) {
6198 return findPossibleRootException(t.getCause());
6204 public <T> T getRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6205 return getRelatedValue2(subject, relation, subject);
6209 public Variant getRelatedVariantValue2(Resource subject, Resource relation) throws DatabaseException {
6210 return getRelatedVariantValue2(subject, relation, subject);
6214 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6216 Resource object = getPossibleObject(subject, relation);
6217 if(object == null) return null;
6218 else return getValue2(object, subject);
6219 } catch (DatabaseException e) {
6225 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6226 if(Development.DEVELOPMENT) {
6227 if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
6228 String error = L0Validations.checkValueType(this, subject, relation);
6230 Logger.defaultLogError(new ValidationException(error));
6231 throw new ValidationException(error);
6235 return getValue2(getSingleObject(subject, relation), context);
6239 public Variant getRelatedVariantValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6240 if(Development.DEVELOPMENT) {
6241 if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
6242 String error = L0Validations.checkValueType(this, subject, relation);
6244 Logger.defaultLogError(new ValidationException(error));
6245 throw new ValidationException(error);
6249 return getVariantValue2(getSingleObject(subject, relation), context);
6253 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6255 Resource object = getPossibleObject(subject, relation);
6256 if(object == null) return null;
6257 else return getValue2(object, context);
6258 } catch (DatabaseException e) {
6264 public <T> T getRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6265 return getRelatedValue2(subject, relation, subject, binding);
6269 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6271 Resource object = getPossibleObject(subject, relation);
6272 if(object == null) return null;
6273 return getValue2(object, subject, binding);
6274 } catch (DatabaseException e) {
6280 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6281 return getValue2(getSingleObject(subject, relation), context, binding);
6285 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6287 Resource object = getPossibleObject(subject, relation);
6288 if(object == null) return null;
6289 else return getValue2(object, context, binding);
6290 } catch (DatabaseException e) {
6296 public Type getRelatedValueType(Resource subject, Resource relation) throws DatabaseException {
6297 Layer0 L0 = processor.getL0(this);
6298 Resource property = getSingleObject(subject, relation);
6299 String typeText = (String)getRelatedValue(property, L0.HasValueType, Bindings.STRING);
6301 return org.simantics.scl.compiler.types.Types.parseType(typeText);
6302 } catch (SCLTypeParseException e) {
6303 throw new DatabaseException(e);
6307 private static ThreadLocal<Boolean> syncGraph = new ThreadLocal<Boolean>() {
6308 protected Boolean initialValue() {
6314 public boolean setSynchronous(boolean value) {
6315 boolean old = getSynchronous();
6316 syncGraph.set(value);
6321 public boolean getSynchronous() {
6322 return syncGraph.get();
6325 public void ensureLoaded(int resource) {
6326 processor.querySupport.ensureLoaded(this, resource);
6329 public void ensureLoaded(int resource, int predicate) {
6330 processor.querySupport.ensureLoaded(this, resource, predicate);
6333 public byte[] getValue(int resource) {
6334 return processor.querySupport.getValue(this, resource);
6337 public int thread(int resource) {
6338 return (resource >>> 16) & processor.THREAD_MASK;
6341 public int thread(Resource resource) {
6342 return (((ResourceImpl)resource).id >>> 16) & processor.THREAD_MASK;
6345 public ResourceSupport getResourceSupport() {
6346 return processor.getResourceSupport();
6350 public Object getModificationCounter() {
6351 return processor.getSession().getModificationCounter();
6355 public boolean performPending() {
6356 return processor.performPending(this);
6359 public Set<ReadGraphImpl> ancestorSet() {
6360 HashSet<ReadGraphImpl> result = new HashSet<>();
6361 ReadGraphImpl g = this;
6369 public int getLevel() {
6370 return getLevelStatic(this);
6373 private static int getLevelStatic(ReadGraphImpl impl) {
6374 if(impl == null) return 0;
6375 else return 1 + getLevelStatic(impl.parentGraph);
6378 public boolean isParent(ReadGraphImpl impl) {
6379 if(impl == null) return false;
6380 if(this == impl) return true;
6381 return isParent(impl.parentGraph);
6384 public ReadGraphImpl getTopLevelGraph() {
6385 return getTopLevelGraphStatic(this);
6388 private static ReadGraphImpl getTopLevelGraphStatic(ReadGraphImpl impl) {
6389 if(impl.parentGraph == null) return impl;
6390 else return getTopLevelGraphStatic(impl.parentGraph);
6393 @SuppressWarnings("unchecked")
6396 return (T) processor.getL0();