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 return adaptContextual(stm.getObject(), new RelationContextImpl(resource, stm), RelationContext.class, clazz);
1357 final public <T> T getPossibleRelatedAdapter(final Resource resource, final Resource relation, final Class<T> clazz)
1358 throws ValidationException, ServiceException {
1361 return adaptRelated(resource, relation, clazz);
1362 } catch (DatabaseException e) {
1369 final public <T,C> T getPossibleContextualAdapter(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1370 throws ValidationException, ServiceException {
1372 assert (resource != null);
1373 assert (context != null);
1375 class PossibleContextualAdapter implements AsyncRead<T> {
1377 final private Resource resource;
1378 final private C context;
1379 final private Class<T> clazz;
1382 public int hashCode() {
1383 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1387 final public int threadHash() {
1388 return resource.getThreadHash();
1392 public boolean equals(Object object) {
1395 else if (object == null)
1397 else if (getClass() != object.getClass())
1399 PossibleContextualAdapter r = (PossibleContextualAdapter)object;
1400 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1404 public int getFlags() {
1408 public PossibleContextualAdapter(Resource resource, C context, Class<T> clazz) {
1409 this.resource = resource;
1410 this.context = context;
1415 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1417 final AdaptionService service = getSession().peekService(AdaptionService.class);
1418 if (service == null)
1419 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1421 service.adapt(graph, resource, context, contextClass, clazz, true, procedure);
1426 public String toString() {
1427 return "Possible adapter for (" + resource + "," + context + ") as " + clazz.getName();
1434 return syncRequest(new PossibleContextualAdapter(resource, context, clazz));
1436 } catch (ValidationException e) {
1438 throw new ValidationException(e);
1440 } catch (ServiceException e) {
1442 throw new ServiceException(e);
1444 } catch (DatabaseException e) {
1446 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1453 final public <T> T adaptUnique(final Resource resource, final Class<T> clazz)
1454 throws AdaptionException, ValidationException, ServiceException {
1456 assert (resource != null);
1457 assert (clazz != null);
1461 return syncRequest(new UniqueAdapter<T>(resource, clazz));
1463 } catch (AdaptionException e) {
1465 throw new AdaptionException(e);
1467 } catch (ValidationException e) {
1469 throw new ValidationException(e);
1471 } catch (ServiceException e) {
1473 throw new ServiceException(e);
1475 } catch (DatabaseException e) {
1477 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1484 final public Resource getPossibleInverse(final Resource relation)
1485 throws ServiceException {
1487 assert (relation != null);
1491 return getPossibleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
1493 } catch (ServiceException e) {
1495 throw new ServiceException(e);
1497 } catch (DatabaseException e) {
1499 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1506 public Resource getPossibleObject(final Resource subject, final Resource relation)
1507 throws ManyObjectsForFunctionalRelationException, ServiceException {
1509 assert (subject != null);
1510 assert (relation != null);
1514 int result = processor.getSingleObject(this, subject, relation);
1515 if(result == 0) return null;
1517 return processor.querySupport.getResource(result);
1519 } catch (ManyObjectsForFunctionalRelationException e) {
1521 throw new ManyObjectsForFunctionalRelationException("subject=" + subject + ", relation=" + relation, e);
1523 } catch (DatabaseException e) {
1525 throw new ServiceException(e);
1532 final public Statement getPossibleStatement(final Resource subject, final Resource relation)
1533 throws ManyObjectsForFunctionalRelationException, ServiceException {
1535 assert (subject != null);
1536 assert (relation != null);
1540 Collection<Statement> statements = getStatements(subject, relation);
1541 if(statements.size() == 1) return statements.iterator().next();
1544 } catch (ManyObjectsForFunctionalRelationException e) {
1546 throw new ManyObjectsForFunctionalRelationException("Many objects in " + subject + " for functional relation " + relation);
1548 } catch (ServiceException e) {
1550 throw new ServiceException(e);
1557 final public Resource getPossibleType(final Resource subject, final Resource baseType) throws ServiceException {
1559 assert (subject != null);
1560 assert (baseType != null);
1564 AsyncReadProcedure<Resource> procedure = new AsyncReadProcedure<Resource>();
1565 forPossibleType(subject, baseType, procedure);
1566 procedure.checkAndThrow();
1567 return procedure.result;
1569 } catch (ServiceException e) {
1571 throw new ServiceException(e);
1573 } catch (DatabaseException e) {
1575 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1582 final public <T> T getPossibleValue(final Resource subject) throws ServiceException {
1584 assert (subject != null);
1588 int object = processor.getSingleObject(this, subject, processor.getL0(this).HasDataType);
1589 if(object == 0) return null;
1591 if(processor.isImmutable(object)) {
1592 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1593 return getPossibleValue(subject, binding);
1595 byte[] dt = processor.getValue(this, object);
1596 if(dt == null) return null;
1597 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1598 Binding binding = Bindings.getBinding(datatype);
1599 return getPossibleValue(subject, binding);
1602 } catch (IOException e) {
1604 throw new ServiceException(e);
1606 } catch (ServiceException e) {
1608 throw new ServiceException(e);
1610 } catch (DatabaseException e) {
1612 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1619 final public <T> T getPossibleValue(final Resource subject, final Binding binding) throws BindingException, ServiceException {
1621 assert (subject != null);
1622 assert (binding != null);
1626 byte[] dt = processor.getValue(this, subject);
1627 if(dt == null) return null;
1628 Serializer serializer = getSerializer(binding);
1629 return (T)serializer.deserialize(dt);
1631 } catch (IOException e) {
1633 throw new ServiceException(e);
1635 } catch (BindingException e) {
1637 throw new BindingException(e);
1639 } catch (ServiceException e) {
1641 throw new ServiceException(e);
1643 } catch (DatabaseException e) {
1644 e.printStackTrace();
1645 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1651 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation)
1652 throws ManyObjectsForFunctionalRelationException, ServiceException {
1654 assert (subject != null);
1655 assert (relation != null);
1659 Resource object = getPossibleObject(subject, relation);
1660 if(object == null) return null;
1661 else return getPossibleValue(object);
1663 } catch (ManyObjectsForFunctionalRelationException e) {
1665 throw new ManyObjectsForFunctionalRelationException(e);
1667 } catch (ServiceException e) {
1669 throw new ServiceException(e);
1676 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1677 throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
1679 assert (subject != null);
1680 assert (relation != null);
1681 assert (binding != null);
1685 Resource object = getPossibleObject(subject, relation);
1686 if(object == null) return null;
1687 else return getPossibleValue(object, binding);
1689 } catch (ManyObjectsForFunctionalRelationException e) {
1691 throw new ManyObjectsForFunctionalRelationException(e);
1693 } catch (BindingException e) {
1695 throw new BindingException(e);
1697 } catch (ServiceException e) {
1699 throw new ServiceException(e);
1706 public <T> T getPossibleAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1708 assert (resource != null);
1709 assert (clazz != null);
1713 return syncRequest(new PossibleAdapter<T>(resource, clazz));
1715 } catch (ValidationException e) {
1717 throw new ValidationException(e);
1719 } catch (AdaptionException e) {
1723 } catch (DatabaseException e) {
1725 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1731 public <T> T getPossibleUniqueAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1733 assert (resource != null);
1734 assert (clazz != null);
1738 return syncRequest(new PossibleUniqueAdapter<T>(resource, clazz));
1740 } catch (AdaptionException e) {
1744 } catch (ValidationException e) {
1746 throw new ValidationException(e);
1748 } catch (DatabaseException e) {
1750 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1757 final public boolean isInstanceOf(final Resource resource, final Resource type) throws ServiceException {
1759 assert (resource != null);
1760 assert (type != null);
1762 Set<Resource> resources = getTypes(resource);
1763 // This check was necessary because some of the callers of this method got stuck when the NPE was thrown from here.
1764 if (null == resources)
1767 if(EMPTY_RESOURCE_CHECK) {
1768 if (resources.isEmpty()) {
1769 if(!hasStatement(resource)) throw new EmptyResourceException("Resource " + debugString(resource));
1773 return resources.contains(type);
1778 final public boolean isInheritedFrom(final Resource resource, final Resource type) throws ServiceException {
1780 assert (resource != null);
1781 assert (type != null);
1785 if(resource.equals(type)) return true;
1787 return getSupertypes(resource).contains(type);
1789 } catch (ServiceException e) {
1791 throw new ServiceException(e);
1798 final public boolean isSubrelationOf(final Resource resource, final Resource type) throws ServiceException {
1800 assert (resource != null);
1801 assert (type != null);
1805 if(resource.equals(type)) return true;
1807 return getSuperrelations(resource).contains(type);
1809 } catch (ServiceException e) {
1811 throw new ServiceException(e);
1818 final public boolean hasStatement(final Resource subject) throws ServiceException {
1820 assert (subject != null);
1824 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1825 processor.forHasStatement(this, subject, procedure);
1826 procedure.checkAndThrow();
1827 return procedure.result;
1829 } catch (ServiceException e) {
1831 throw new ServiceException(e);
1833 } catch (DatabaseException e) {
1835 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1842 final public boolean hasStatement(final Resource subject, final Resource relation) throws ServiceException {
1844 assert (subject != null);
1845 assert (relation != null);
1849 Collection<Resource> objects = getObjects(subject, relation);
1850 return !objects.isEmpty();
1852 } catch (ServiceException e) {
1854 throw new ServiceException(e);
1861 final public boolean hasStatement(final Resource subject, final Resource relation, final Resource object) throws ServiceException {
1863 assert (subject != null);
1864 assert (relation != null);
1865 assert (object != null);
1869 for(Resource o : getObjects(subject, relation)) {
1870 if(object.equals(o)) return true;
1875 } catch (ServiceException e) {
1877 throw new ServiceException(e);
1884 final public boolean hasValue(final Resource subject) throws ServiceException {
1886 assert (subject != null);
1890 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1891 processor.forHasValue(this, subject, procedure);
1892 procedure.checkAndThrow();
1893 return procedure.result;
1895 } catch (ServiceException e) {
1897 throw new ServiceException(e);
1899 } catch (DatabaseException e) {
1901 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1907 final AsyncProcedure<?> NONE = new AsyncProcedure<Object>() {
1910 public void execute(AsyncReadGraph graph, Object result) {
1914 public void exception(AsyncReadGraph graph, Throwable throwable) {
1920 * Implementation of the interface RequestProcessor
1924 public <T> T syncRequest(final Read<T> request) throws DatabaseException {
1925 assert (request != null);
1926 return (T)QueryCache.runnerReadEntry(this, request, parent, null, null, true);
1930 public <T> T syncRequest(Read<T> request, SyncListener<T> procedure)
1931 throws DatabaseException {
1932 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
1936 public <T> T syncRequest(Read<T> request, final Listener<T> procedure)
1937 throws DatabaseException {
1938 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
1942 public <T> T syncRequest(final Read<T> request, final AsyncProcedure<T> procedure) throws DatabaseException {
1943 assert (request != null);
1944 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
1945 return (T)QueryCache.runnerReadEntry(this, request, parent, listener, procedure, true);
1949 public <T> T syncRequest(final Read<T> request,
1950 final SyncProcedure<T> procedure) throws DatabaseException {
1951 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
1955 public <T> T syncRequest(Read<T> request, Procedure<T> procedure)
1956 throws DatabaseException {
1957 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
1960 static class AsyncReadProcedure<T> implements AsyncProcedure<T> {
1962 private static Throwable DONE = new Throwable();
1965 Throwable exception = null;
1968 public void execute(AsyncReadGraph graph, T t) {
1974 public void exception(AsyncReadGraph graph, Throwable t) {
1978 public void checkAndThrow() throws DatabaseException {
1979 if(exception != DONE) {
1980 if (exception instanceof DatabaseException)
1981 throw (DatabaseException) exception;
1983 throw new DatabaseException(
1984 "Unexpected exception in ReadGraph.syncRequest(AsyncRead)",
1989 public boolean done() {
1990 return exception != null;
1996 public <T> T syncRequest(final AsyncRead<T> request)
1997 throws DatabaseException {
1999 assert (request != null);
2000 return syncRequest(request, new AsyncProcedureAdapter<>() );
2005 public <T> T syncRequest(AsyncRead<T> request, AsyncListener<T> procedure)
2006 throws DatabaseException {
2007 return syncRequest(request, (AsyncProcedure<T>) procedure);
2011 public <T> T syncRequest(AsyncRead<T> request, SyncListener<T> procedure)
2012 throws DatabaseException {
2013 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
2017 public <T> T syncRequest(AsyncRead<T> request, Listener<T> procedure)
2018 throws DatabaseException {
2019 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
2023 final public <T> T syncRequest(final AsyncRead<T> request,
2024 final AsyncProcedure<T> procedure) throws DatabaseException {
2025 assert (request != null);
2026 ListenerBase listener = getListenerBase(procedure);
2027 return (T)QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure, true);
2031 public <T> T syncRequest(AsyncRead<T> request,
2032 final SyncProcedure<T> procedure) throws DatabaseException {
2033 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2037 final public <T> T syncRequest(final AsyncRead<T> request,
2038 final Procedure<T> procedure) throws DatabaseException {
2039 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2043 public <T> Collection<T> syncRequest(final MultiRead<T> request)
2044 throws DatabaseException {
2046 assert (request != null);
2048 final ArrayList<T> result = new ArrayList<T>();
2049 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2051 syncRequest(request, new SyncMultiProcedure<T>() {
2054 public void execute(ReadGraph graph, T t) {
2055 synchronized (result) {
2061 public void finished(ReadGraph graph) {
2065 public void exception(ReadGraph graph, Throwable t) {
2070 public String toString() {
2071 return "syncRequest(MultiRead) -> " + request;
2076 Throwable t = exception.get();
2078 if (t instanceof DatabaseException)
2079 throw (DatabaseException) t;
2081 throw new DatabaseException(
2082 "Unexpected exception in ReadGraph.syncRequest(Read)",
2091 public <T> Collection<T> syncRequest(MultiRead<T> request,
2092 SyncMultiListener<T> procedure) {
2093 return syncRequest(request, (SyncMultiProcedure<T>)procedure);
2097 public <T> Collection<T> syncRequest(MultiRead<T> request,
2098 MultiListener<T> procedure) {
2099 return syncRequest(request, new NoneToSyncMultiListener<T>(procedure));
2103 public <T> Collection<T> syncRequest(MultiRead<T> request,
2104 SyncMultiProcedure<T> procedure) {
2106 assert (request != null);
2108 ListenerBase listener = getListenerBase(procedure);
2110 final ResultCallWrappedSyncQueryProcedure<T> wrapper = new ResultCallWrappedSyncQueryProcedure<T>(procedure);
2112 if (parent != null || listener != null) {
2114 // Object syncParent = request;
2116 // final ReadGraphImpl newGraph = newSync();
2118 processor.query(this, request, parent, wrapper, listener);
2120 // newGraph.waitAsync(syncParent);
2124 // Object syncParent = request;
2126 // final ReadGraphImpl newGraph = newSync();
2129 request.perform(this, wrapper);
2130 } catch (Throwable t) {
2131 wrapper.exception(this, t);
2136 return wrapper.get();
2141 public <T> Collection<T> syncRequest(MultiRead<T> request,
2142 MultiProcedure<T> procedure) {
2143 return syncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
2146 static class AsyncMultiReadProcedure<T> extends ArrayList<T> implements AsyncMultiProcedure<T> {
2148 private static Throwable DONE = new Throwable();
2150 private static final long serialVersionUID = -6494230465108115812L;
2152 Throwable exception = null;
2155 public synchronized void execute(AsyncReadGraph graph, T t) {
2160 public void finished(AsyncReadGraph graph) {
2165 public void exception(AsyncReadGraph graph, Throwable t) {
2169 public void checkAndThrow() throws DatabaseException {
2170 if(exception != DONE) {
2171 if (exception instanceof DatabaseException)
2172 throw (DatabaseException) exception;
2174 throw new DatabaseException(
2175 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
2180 public boolean done() {
2181 return exception != null;
2187 final public <T> Collection<T> syncRequest(AsyncMultiRead<T> request)
2188 throws DatabaseException {
2190 assert (request != null);
2192 final AsyncMultiReadProcedure<T> procedure = new AsyncMultiReadProcedure<T>();
2194 syncRequest(request, procedure);
2196 procedure.checkAndThrow();
2202 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2203 AsyncMultiListener<T> procedure) {
2204 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2208 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2209 SyncMultiListener<T> procedure) {
2210 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2214 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2215 MultiListener<T> procedure) {
2216 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2219 final private <T> void syncRequest(final AsyncMultiRead<T> request,
2220 final AsyncMultiReadProcedure<T> procedure) {
2222 assert (request != null);
2223 assert (procedure != null);
2225 ListenerBase listener = getListenerBase(procedure);
2227 if (parent != null || listener != null) {
2229 // Object syncParent = request;
2231 // final ReadGraphImpl newGraph = newSync();
2233 processor.query(this, request, parent, procedure, listener);
2235 // newGraph.waitAsync(syncParent);
2236 waitAsyncProcedure(procedure);
2240 // Object syncParent = callerThread == Integer.MIN_VALUE ? null
2243 // final ReadGraphImpl newGraph = newSyncAsync(syncParent);
2248 // ReadGraphImpl sync = newSync();
2249 request.perform(this, procedure);
2250 // sync.waitAsync(null);
2251 waitAsyncProcedure(procedure);
2254 } catch (Throwable t) {
2256 waitAsyncProcedure(procedure);
2267 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2268 final AsyncMultiProcedure<T> procedure) {
2270 assert (request != null);
2271 assert (procedure != null);
2273 ListenerBase listener = getListenerBase(procedure);
2275 if (parent != null || listener != null) {
2277 // Object syncParent = request;
2279 // final ReadGraphImpl newGraph = newSync();
2281 processor.query(this, request, parent, procedure, listener);
2283 // newGraph.waitAsync(syncParent);
2287 // Object syncParent = request;
2289 // final ReadGraphImpl newGraph = newSync();
2293 request.perform(this, new AsyncMultiProcedure<T>() {
2296 public void execute(AsyncReadGraph graph, T result) {
2297 procedure.execute(graph, result);
2301 public void finished(AsyncReadGraph graph) {
2302 procedure.finished(graph);
2306 public void exception(AsyncReadGraph graph, Throwable t) {
2307 procedure.exception(graph, t);
2311 public String toString() {
2312 return "syncRequest(AsyncMultiRead) -> " + procedure;
2317 } catch (Throwable t) {
2329 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2330 final SyncMultiProcedure<T> procedure) {
2331 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2335 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2336 final MultiProcedure<T> procedure) {
2337 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2341 public <T> T syncRequest(final ExternalRead<T> request)
2342 throws DatabaseException {
2344 assert (request != null);
2346 return syncRequest(request, new Procedure<T>() {
2349 public void execute(T t) {
2353 public void exception(Throwable t) {
2357 public String toString() {
2358 return "syncRequest(AsyncRead) -> " + request;
2366 public <T> T syncRequest(ExternalRead<T> request, Listener<T> procedure) throws DatabaseException {
2367 return syncRequest(request, (Procedure<T>) procedure);
2371 final public <T> T syncRequest(final ExternalRead<T> request,
2372 final Procedure<T> procedure) throws DatabaseException {
2374 assert (request != null);
2376 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
2377 return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
2382 public void syncRequest(final Write request) throws DatabaseException {
2384 assert (request != null);
2386 throw new DatabaseException(
2387 "Write operations are not supported during read transactions!");
2392 public <T> T syncRequest(final WriteResult<T> request) throws DatabaseException {
2394 assert (request != null);
2396 throw new DatabaseException(
2397 "Write operations are not supported during read transactions!");
2402 public void syncRequest(final DelayedWrite request)
2403 throws DatabaseException {
2405 assert (request != null);
2407 throw new DatabaseException(
2408 "Write operations are not supported during read transactions!");
2413 public <T> T syncRequest(final DelayedWriteResult<T> request) throws DatabaseException {
2415 assert (request != null);
2417 throw new DatabaseException(
2418 "Write operations are not supported during read transactions!");
2423 public void syncRequest(final WriteOnly request) throws DatabaseException {
2425 assert (request != null);
2427 throw new DatabaseException(
2428 "Write operations are not supported during read transactions!");
2433 public <T> T syncRequest(final WriteOnlyResult<T> request) throws DatabaseException {
2435 assert (request != null);
2437 throw new DatabaseException(
2438 "Write operations are not supported during read transactions!");
2443 public <T> void async(ReadInterface<T> r, AsyncProcedure<T> procedure) {
2444 r.request(this, procedure);
2448 public <T> void async(ReadInterface<T> r, Procedure<T> procedure) {
2449 r.request(this, procedure);
2453 public <T> void async(ReadInterface<T> r, SyncProcedure<T> procedure) {
2454 r.request(this, procedure);
2458 public <T> void async(ReadInterface<T> r, AsyncListener<T> procedure) {
2459 r.request(this, procedure);
2463 public <T> void async(ReadInterface<T> r, Listener<T> procedure) {
2464 r.request(this, procedure);
2468 public <T> void async(ReadInterface<T> r, SyncListener<T> procedure) {
2469 r.request(this, procedure);
2473 public <T> T sync(ReadInterface<T> r) throws DatabaseException {
2474 return r.request(this);
2478 public <T> T sync(WriteInterface<T> r) throws DatabaseException {
2479 return r.request(this);
2483 public <T> void async(WriteInterface<T> r, Procedure<T> procedure) {
2484 r.request(this, procedure);
2488 public <T> void async(WriteInterface<T> r) {
2489 r.request(this, new ProcedureAdapter<T>());
2493 * Implementation of the interface AsyncReadGraph
2497 public void forURI(Resource resource, AsyncListener<String> listener) {
2498 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2503 public void forURI(Resource resource, SyncListener<String> listener) {
2504 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2509 public void forURI(Resource resource, Listener<String> listener) {
2510 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2515 final public void forURI(final Resource resource,
2516 final AsyncProcedure<String> procedure) {
2518 assert (resource != null);
2519 assert (procedure != null);
2521 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2527 public void forURI(Resource resource, SyncProcedure<String> procedure) {
2528 forURI(resource, new SyncToAsyncProcedure<String>(procedure));
2532 public void forURI(Resource resource, Procedure<String> procedure) {
2533 forURI(resource, new NoneToAsyncProcedure<String>(procedure));
2537 public void forResource(String id, AsyncListener<Resource> listener) {
2538 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2543 public void forResource(String id, SyncListener<Resource> listener) {
2544 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2549 public void forResource(String id, Listener<Resource> listener) {
2550 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2555 final public void forResource(final String id,
2556 final AsyncProcedure<Resource> procedure) {
2558 assert (id != null);
2559 assert (procedure != null);
2561 processor.forResource(this, id, procedure);
2566 public void forResource(String id, SyncProcedure<Resource> procedure) {
2567 forResource(id, new SyncToAsyncProcedure<Resource>(procedure));
2571 public void forResource(String id, Procedure<Resource> procedure) {
2572 forResource(id, new NoneToAsyncProcedure<Resource>(procedure));
2576 public void forBuiltin(String id, AsyncListener<Resource> listener) {
2577 asyncRequest(new Builtin(id), listener);
2581 public void forBuiltin(String id, SyncListener<Resource> listener) {
2582 asyncRequest(new Builtin(id), listener);
2586 public void forBuiltin(String id, Listener<Resource> listener) {
2587 asyncRequest(new Builtin(id), listener);
2591 final public void forBuiltin(final String id,
2592 final AsyncProcedure<Resource> procedure) {
2594 assert (id != null);
2595 assert (procedure != null);
2597 processor.forBuiltin(this, id, procedure);
2602 public void forBuiltin(String id, SyncProcedure<Resource> procedure) {
2603 forBuiltin(id, new SyncToAsyncProcedure<Resource>(procedure));
2607 public void forBuiltin(String id, Procedure<Resource> procedure) {
2608 forBuiltin(id, new NoneToAsyncProcedure<Resource>(procedure));
2612 final public void forEachStatement(Resource subject, Resource relation,
2613 AsyncMultiProcedure<Statement> procedure) {
2615 assert (subject != null);
2616 assert (relation != null);
2617 assert (procedure != null);
2619 processor.forEachStatement(this, subject, relation, procedure);
2624 public void forEachStatement(Resource subject, Resource relation,
2625 SyncMultiProcedure<Statement> procedure) {
2626 forEachStatement(subject, relation,
2627 new SyncToAsyncMultiProcedure<Statement>(procedure));
2631 final public void forEachStatement(Resource subject, Resource relation,
2632 MultiProcedure<Statement> procedure) {
2634 assert (subject != null);
2635 assert (relation != null);
2636 assert (procedure != null);
2638 processor.forEachStatement(this, subject, relation, procedure);
2643 final public void forStatementSet(Resource subject, Resource relation,
2644 AsyncSetListener<Statement> procedure) {
2646 assert (subject != null);
2647 assert (relation != null);
2648 assert (procedure != null);
2650 processor.forStatementSet(this, subject, relation, procedure);
2655 final public void forStatementSet(Resource subject, Resource relation,
2656 SyncSetListener<Statement> procedure) {
2657 forStatementSet(subject, relation,
2658 new SyncToAsyncSetProcedure<Statement>(procedure));
2662 public void forStatementSet(Resource subject, Resource relation,
2663 SetListener<Statement> listener) {
2664 forStatementSet(subject, relation,
2665 new NoneToAsyncSetProcedure<Statement>(listener));
2669 final public void forEachAssertedStatement(final Resource subject,
2670 final Resource relation,
2671 final AsyncMultiProcedure<Statement> procedure) {
2673 assert (subject != null);
2674 assert (relation != null);
2675 assert (procedure != null);
2677 processor.forEachAssertedStatement(this, subject, relation, procedure);
2682 public void forEachAssertedStatement(Resource subject, Resource relation,
2683 SyncMultiProcedure<Statement> procedure) {
2684 forEachAssertedStatement(subject, relation,
2685 new SyncToAsyncMultiProcedure<Statement>(procedure));
2689 public void forEachAssertedStatement(Resource subject, Resource relation,
2690 MultiProcedure<Statement> procedure) {
2691 forEachAssertedStatement(subject, relation,
2692 new NoneToAsyncMultiProcedure<Statement>(procedure));
2696 public void forAssertedStatementSet(Resource subject, Resource relation,
2697 AsyncSetListener<Statement> procedure) {
2699 assert (subject != null);
2700 assert (relation != null);
2701 assert (procedure != null);
2703 processor.forAssertedStatementSet(this, subject, relation, procedure);
2708 public void forAssertedStatementSet(Resource subject, Resource relation,
2709 SyncSetListener<Statement> procedure) {
2711 assert (subject != null);
2712 assert (relation != null);
2713 assert (procedure != null);
2715 forAssertedStatementSet(subject, relation,
2716 new SyncToAsyncSetProcedure<Statement>(procedure));
2721 public void forAssertedStatementSet(Resource subject, Resource relation,
2722 SetListener<Statement> procedure) {
2724 assert (subject != null);
2725 assert (relation != null);
2726 assert (procedure != null);
2728 forAssertedStatementSet(subject, relation,
2729 new NoneToAsyncSetProcedure<Statement>(procedure));
2734 final public void forEachPredicate(final Resource subject,
2735 final AsyncMultiProcedure<Resource> procedure) {
2737 assert (subject != null);
2738 assert (procedure != null);
2740 processor.forEachPredicate(this, subject, procedure);
2745 public void forEachPredicate(Resource subject,
2746 SyncMultiProcedure<Resource> procedure) {
2747 forEachPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(
2752 final public void forEachPredicate(final Resource subject,
2753 final MultiProcedure<Resource> procedure) {
2755 assert (subject != null);
2756 assert (procedure != null);
2758 processor.forEachPredicate(this, subject, procedure);
2763 final public void forPredicateSet(final Resource subject,
2764 final AsyncSetListener<Resource> procedure) {
2766 assert (subject != null);
2767 assert (procedure != null);
2769 processor.forPredicateSet(this, subject, procedure);
2774 final public void forPredicateSet(final Resource subject,
2775 final SyncSetListener<Resource> procedure) {
2777 assert (subject != null);
2778 assert (procedure != null);
2780 forPredicateSet(subject, new SyncToAsyncSetProcedure<Resource>(
2786 final public void forPredicateSet(final Resource subject,
2787 final SetListener<Resource> procedure) {
2789 assert (subject != null);
2790 assert (procedure != null);
2792 forPredicateSet(subject, new NoneToAsyncSetProcedure<Resource>(
2798 final public void forEachPrincipalType(final Resource subject,
2799 final AsyncMultiProcedure<Resource> procedure) {
2801 assert (subject != null);
2802 assert (procedure != null);
2804 processor.forEachPrincipalType(this, subject, procedure);
2809 public void forEachPrincipalType(Resource subject,
2810 SyncMultiProcedure<Resource> procedure) {
2811 forEachPrincipalType(subject, new SyncToAsyncMultiProcedure<Resource>(
2816 final public void forEachPrincipalType(final Resource subject,
2817 final MultiProcedure<Resource> procedure) {
2819 assert (subject != null);
2820 assert (procedure != null);
2822 processor.forEachPrincipalType(this, subject, procedure);
2827 final public void forPrincipalTypeSet(final Resource subject,
2828 final AsyncSetListener<Resource> procedure) {
2830 assert (subject != null);
2831 assert (procedure != null);
2833 processor.forPrincipalTypeSet(this, subject, procedure);
2838 final public void forPrincipalTypeSet(final Resource subject,
2839 final SyncSetListener<Resource> procedure) {
2841 assert (subject != null);
2842 assert (procedure != null);
2844 forPrincipalTypeSet(subject, new SyncToAsyncSetProcedure<Resource>(
2850 final public void forPrincipalTypeSet(final Resource subject,
2851 final SetListener<Resource> procedure) {
2853 assert (subject != null);
2854 assert (procedure != null);
2856 forPrincipalTypeSet(subject, new NoneToAsyncSetProcedure<Resource>(
2862 public void forTypes(Resource subject, AsyncListener<Set<Resource>> listener) {
2863 asyncRequest(new Types(subject), listener);
2867 public void forTypes(Resource subject, SyncListener<Set<Resource>> listener) {
2868 asyncRequest(new Types(subject), listener);
2872 public void forTypes(Resource subject, Listener<Set<Resource>> listener) {
2873 asyncRequest(new Types(subject), listener);
2877 final public void forTypes(final Resource subject,
2878 final AsyncProcedure<Set<Resource>> procedure) {
2880 assert (subject != null);
2881 assert (procedure != null);
2883 processor.forTypes(this, subject, procedure);
2888 public void forTypes(Resource subject,
2889 SyncProcedure<Set<Resource>> procedure) {
2890 forTypes(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
2894 public void forTypes(Resource subject, Procedure<Set<Resource>> procedure) {
2895 forTypes(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
2899 public void forSupertypes(Resource subject,
2900 AsyncListener<Set<Resource>> listener) {
2901 asyncRequest(new Types(subject), listener);
2905 public void forSupertypes(Resource subject,
2906 SyncListener<Set<Resource>> listener) {
2907 asyncRequest(new Types(subject), listener);
2911 public void forSupertypes(Resource subject, Listener<Set<Resource>> listener) {
2912 asyncRequest(new Types(subject), listener);
2916 final public void forSupertypes(final Resource subject,
2917 final AsyncProcedure<Set<Resource>> procedure) {
2919 assert (subject != null);
2920 assert (procedure != null);
2922 processor.forSupertypes(this, subject, procedure);
2927 public void forSupertypes(Resource subject,
2928 SyncProcedure<Set<Resource>> procedure) {
2929 forSupertypes(subject, new SyncToAsyncProcedure<Set<Resource>>(
2934 public void forSupertypes(Resource subject,
2935 Procedure<Set<Resource>> procedure) {
2936 forSupertypes(subject, new NoneToAsyncProcedure<Set<Resource>>(
2941 public void forDirectSuperrelations(Resource subject,
2942 AsyncMultiProcedure<Resource> procedure) {
2944 assert (subject != null);
2945 assert (procedure != null);
2947 processor.forDirectSuperrelations(this, subject, procedure);
2952 public void forPossibleSuperrelation(Resource subject, AsyncProcedure<Resource> procedure) {
2954 assert (subject != null);
2955 assert (procedure != null);
2957 processor.forPossibleSuperrelation(this, subject, procedure);
2962 public void forSuperrelations(Resource subject,
2963 AsyncListener<Set<Resource>> listener) {
2964 asyncRequest(new Types(subject), listener);
2968 public void forSuperrelations(Resource subject,
2969 SyncListener<Set<Resource>> listener) {
2970 asyncRequest(new Types(subject), listener);
2974 public void forSuperrelations(Resource subject,
2975 Listener<Set<Resource>> listener) {
2976 asyncRequest(new Types(subject), listener);
2980 final public void forSuperrelations(final Resource subject,
2981 final AsyncProcedure<Set<Resource>> procedure) {
2983 assert (subject != null);
2984 assert (procedure != null);
2986 processor.forSuperrelations(this, subject, procedure);
2991 public void forSuperrelations(Resource subject,
2992 SyncProcedure<Set<Resource>> procedure) {
2993 forSuperrelations(subject, new SyncToAsyncProcedure<Set<Resource>>(
2998 public void forSuperrelations(Resource subject,
2999 Procedure<Set<Resource>> procedure) {
3000 forSuperrelations(subject, new NoneToAsyncProcedure<Set<Resource>>(
3005 final public void forEachObject(final Resource subject, final Resource relation, final AsyncMultiProcedure<Resource> procedure) {
3006 processor.forEachObject(this, subject, relation, procedure);
3010 public void forEachObject(Resource subject, Resource relation,
3011 SyncMultiProcedure<Resource> procedure) {
3012 forEachObject(subject, relation,
3013 new SyncToAsyncMultiProcedure<Resource>(procedure));
3017 public void forEachObject(Resource subject, Resource relation,
3018 MultiProcedure<Resource> procedure) {
3020 processor.forEachObject(this, subject, relation, procedure);
3025 final public void forEachDirectPredicate(final Resource subject, final AsyncProcedure<Set<Resource>> procedure) {
3026 processor.forEachDirectPredicate(this, subject, procedure);
3030 final public void forEachDirectPredicate(final Resource subject, final SyncProcedure<Set<Resource>> procedure) {
3031 forEachDirectPredicate(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
3035 public void forEachDirectPredicate(Resource subject, Procedure<Set<Resource>> procedure) {
3036 forEachDirectPredicate(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
3040 final public void forObjectSet(final Resource subject,
3041 final Resource relation, final AsyncSetListener<Resource> procedure) {
3043 assert (subject != null);
3044 assert (relation != null);
3045 assert (procedure != null);
3047 processor.forObjectSet(this, subject, relation, procedure);
3052 final public void forObjectSet(final Resource subject,
3053 final Resource relation, final SyncSetListener<Resource> procedure) {
3055 assert (subject != null);
3056 assert (relation != null);
3057 assert (procedure != null);
3059 forObjectSet(subject, relation, new SyncToAsyncSetProcedure<Resource>(
3065 final public void forObjectSet(final Resource subject,
3066 final Resource relation, final SetListener<Resource> procedure) {
3068 assert (subject != null);
3069 assert (relation != null);
3070 assert (procedure != null);
3072 forObjectSet(subject, relation, new NoneToAsyncSetProcedure<Resource>(
3078 final public void forEachAssertedObject(final Resource subject,
3079 final Resource relation,
3080 final AsyncMultiProcedure<Resource> procedure) {
3082 assert (subject != null);
3083 assert (relation != null);
3084 assert (procedure != null);
3086 processor.forEachAssertedObject(this, subject, relation, procedure);
3091 public void forEachAssertedObject(Resource subject, Resource relation,
3092 SyncMultiProcedure<Resource> procedure) {
3094 assert (subject != null);
3095 assert (relation != null);
3096 assert (procedure != null);
3098 forEachAssertedObject(subject, relation,
3099 new SyncToAsyncMultiProcedure<Resource>(procedure));
3104 public void forEachAssertedObject(Resource subject, Resource relation,
3105 MultiProcedure<Resource> procedure) {
3107 assert (subject != null);
3108 assert (relation != null);
3109 assert (procedure != null);
3111 forEachAssertedObject(subject, relation,
3112 new NoneToAsyncMultiProcedure<Resource>(procedure));
3117 public void forAssertedObjectSet(Resource subject, Resource relation,
3118 AsyncSetListener<Resource> procedure) {
3120 assert (subject != null);
3121 assert (relation != null);
3122 assert (procedure != null);
3124 processor.forAssertedObjectSet(this, subject, relation, procedure);
3129 public void forAssertedObjectSet(Resource subject, Resource relation,
3130 SyncSetListener<Resource> procedure) {
3132 assert (subject != null);
3133 assert (relation != null);
3134 assert (procedure != null);
3136 forAssertedObjectSet(subject, relation,
3137 new SyncToAsyncSetProcedure<Resource>(procedure));
3142 public void forAssertedObjectSet(Resource subject, Resource relation,
3143 SetListener<Resource> procedure) {
3145 assert (subject != null);
3146 assert (relation != null);
3147 assert (procedure != null);
3149 forAssertedObjectSet(subject, relation,
3150 new NoneToAsyncSetProcedure<Resource>(procedure));
3155 public void forInverse(Resource relation, AsyncListener<Resource> listener) {
3156 asyncRequest(new Inverse(relation), listener);
3160 public void forInverse(Resource relation, SyncListener<Resource> listener) {
3161 asyncRequest(new Inverse(relation), listener);
3165 public void forInverse(Resource relation, Listener<Resource> listener) {
3166 asyncRequest(new Inverse(relation), listener);
3170 final public void forInverse(final Resource relation,
3171 final AsyncProcedure<Resource> procedure) {
3173 assert (relation != null);
3174 assert (procedure != null);
3176 processor.forInverse(this, relation, new AsyncProcedure<Resource>() {
3179 public void execute(AsyncReadGraph graph, Resource result) {
3181 procedure.execute(graph, result);
3183 procedure.exception(graph, new NoInverseException(relation
3189 public void exception(AsyncReadGraph graph, Throwable throwable) {
3190 procedure.exception(graph, throwable);
3194 public String toString() {
3195 return "forInverse -> " + procedure;
3203 public void forInverse(Resource relation, SyncProcedure<Resource> procedure) {
3204 forInverse(relation, new SyncToAsyncProcedure<Resource>(procedure));
3208 public void forInverse(Resource relation, Procedure<Resource> procedure) {
3209 forInverse(relation, new NoneToAsyncProcedure<Resource>(procedure));
3213 public void forSingleObject(Resource subject, Resource relation,
3214 AsyncListener<Resource> listener) {
3215 asyncRequest(new SingleObject(subject, relation), listener);
3219 public void forSingleObject(Resource subject, Resource relation,
3220 SyncListener<Resource> listener) {
3221 asyncRequest(new SingleObject(subject, relation), listener);
3225 public void forSingleObject(Resource subject, Resource relation,
3226 Listener<Resource> listener) {
3227 asyncRequest(new SingleObject(subject, relation), listener);
3231 final public void forSingleObject(final Resource subject,
3232 final Resource relation, final AsyncProcedure<Resource> procedure) {
3234 assert (subject != null);
3235 assert (relation != null);
3236 assert (procedure != null);
3238 processor.forEachObject(this, subject, relation,
3239 new SingleOrErrorProcedure<Resource>(procedure));
3244 public void forSingleObject(Resource subject, Resource relation,
3245 SyncProcedure<Resource> procedure) {
3246 forSingleObject(subject, relation, new SyncToAsyncProcedure<Resource>(
3251 public void forSingleObject(Resource subject, Resource relation,
3252 Procedure<Resource> procedure) {
3253 forSingleObject(subject, relation, new NoneToAsyncProcedure<Resource>(
3258 public void forSingleStatement(Resource subject, Resource relation,
3259 AsyncListener<Statement> listener) {
3260 asyncRequest(new SingleStatement(subject, relation), listener);
3264 public void forSingleStatement(Resource subject, Resource relation,
3265 SyncListener<Statement> listener) {
3266 asyncRequest(new SingleStatement(subject, relation), listener);
3270 public void forSingleStatement(Resource subject, Resource relation,
3271 Listener<Statement> listener) {
3272 asyncRequest(new SingleStatement(subject, relation), listener);
3276 final public void forSingleStatement(final Resource subject,
3277 final Resource relation, final AsyncProcedure<Statement> procedure) {
3279 assert (subject != null);
3280 assert (relation != null);
3281 assert (procedure != null);
3283 processor.forEachStatement(this, subject, relation,
3284 new SingleOrErrorProcedure<Statement>(procedure));
3289 public void forSingleStatement(Resource subject, Resource relation,
3290 SyncProcedure<Statement> procedure) {
3291 forSingleStatement(subject, relation,
3292 new SyncToAsyncProcedure<Statement>(procedure));
3296 public void forSingleStatement(Resource subject, Resource relation,
3297 Procedure<Statement> procedure) {
3298 forSingleStatement(subject, relation,
3299 new NoneToAsyncProcedure<Statement>(procedure));
3303 public void forSingleType(Resource subject,
3304 AsyncListener<Resource> listener) {
3305 asyncRequest(new SingleTypeAny(subject), listener);
3309 public void forSingleType(Resource subject,
3310 SyncListener<Resource> listener) {
3311 asyncRequest(new SingleTypeAny(subject), listener);
3315 public void forSingleType(Resource subject,
3316 Listener<Resource> listener) {
3317 asyncRequest(new SingleTypeAny(subject), listener);
3321 final public void forSingleType(final Resource subject, final AsyncProcedure<Resource> procedure) {
3323 assert (subject != null);
3324 assert (procedure != null);
3326 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3328 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
3331 public void execute(AsyncReadGraph graph, final Resource principalType) {
3332 checkedProcedure.offer(graph, principalType);
3336 public void finished(AsyncReadGraph graph) {
3337 checkedProcedure.dec(graph);
3341 public void exception(AsyncReadGraph graph, Throwable t) {
3342 checkedProcedure.exception(graph, t);
3346 public String toString() {
3347 return "forSingleType -> " + procedure;
3355 public void forSingleType(Resource subject, SyncProcedure<Resource> procedure) {
3356 forSingleType(subject, new SyncToAsyncProcedure<Resource>(
3361 public void forSingleType(Resource subject, Procedure<Resource> procedure) {
3362 forSingleType(subject, new NoneToAsyncProcedure<Resource>(
3367 public void forSingleType(Resource subject, Resource relation,
3368 AsyncListener<Resource> listener) {
3369 asyncRequest(new SingleType(subject, relation), listener);
3373 public void forSingleType(Resource subject, Resource relation,
3374 SyncListener<Resource> listener) {
3375 asyncRequest(new SingleType(subject, relation), listener);
3379 public void forSingleType(Resource subject, Resource relation,
3380 Listener<Resource> listener) {
3381 asyncRequest(new SingleType(subject, relation), listener);
3385 final public void forSingleType(final Resource subject,
3386 final Resource baseType, final AsyncProcedure<Resource> procedure) {
3388 assert (subject != null);
3389 assert (procedure != null);
3391 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3393 processor.forEachPrincipalType(this, subject,
3394 new AsyncMultiProcedureAdapter<Resource>() {
3397 public void execute(AsyncReadGraph graph,
3398 final Resource principalType) {
3400 checkedProcedure.inc();
3402 if(baseType == null) {
3404 checkedProcedure.offer(graph, principalType);
3405 checkedProcedure.dec(graph);
3407 } else if(principalType.equals(baseType)) {
3409 checkedProcedure.offer(graph, principalType);
3410 checkedProcedure.dec(graph);
3414 processor.forSupertypes((ReadGraphImpl)graph, principalType,
3415 new AsyncProcedure<Set<Resource>>() {
3418 public void execute(
3419 AsyncReadGraph graph,
3420 Set<Resource> result) {
3422 if (result.contains(baseType))
3423 checkedProcedure.offer(graph,
3425 checkedProcedure.dec(graph);
3430 public void exception(
3431 AsyncReadGraph graph,
3434 .exception(graph, t);
3444 public void finished(AsyncReadGraph graph) {
3445 checkedProcedure.dec(graph);
3449 public void exception(AsyncReadGraph graph, Throwable t) {
3450 checkedProcedure.exception(graph, t);
3454 public String toString() {
3455 return "forSingleType -> " + procedure;
3463 public void forSingleType(Resource subject, Resource relation,
3464 SyncProcedure<Resource> procedure) {
3465 forSingleType(subject, relation, new SyncToAsyncProcedure<Resource>(
3470 public void forSingleType(Resource subject, Resource relation,
3471 Procedure<Resource> procedure) {
3472 forSingleType(subject, relation, new NoneToAsyncProcedure<Resource>(
3477 public <T> void forValue(Resource subject, Binding binding,
3478 AsyncListener<T> listener) {
3479 asyncRequest(new Value<T>(subject, binding), listener);
3483 public <T> void forValue(Resource subject, Binding binding,
3484 SyncListener<T> listener) {
3485 asyncRequest(new Value<T>(subject, binding), listener);
3489 public <T> void forValue(Resource subject, Binding binding,
3490 Listener<T> listener) {
3491 asyncRequest(new Value<T>(subject, binding), listener);
3495 public <T> void forValue(final Resource resource, final Binding binding,
3496 final AsyncProcedure<T> procedure) {
3498 assert (resource != null);
3499 assert (binding != null);
3500 assert (procedure != null);
3502 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
3505 public void execute(AsyncReadGraph graph, byte[] result) {
3509 if (result == null) {
3510 procedure.exception(graph,
3511 new DoesNotContainValueException(
3512 "No value for resource " + resource));
3516 Serializer serializer = binding.serializer();
3517 // Serializer serializer = Bindings.getSerializer( binding );
3518 Object obj = serializer.deserialize(result);
3519 // if (!binding.isInstance(obj))
3520 // procedure.exception(graph, new ClassCastException(
3521 // "Cannot get value " + obj + " with binding "
3524 procedure.execute(graph, (T) obj);
3526 } catch (Throwable t) {
3527 procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
3532 public void exception(AsyncReadGraph graph, Throwable t) {
3534 procedure.exception(graph, t);
3535 } catch (Throwable t2) {
3536 Logger.defaultLogError(t2);
3541 public String toString() {
3542 return "forValue -> " + procedure;
3549 private static String safeArrayToString(byte[] a) {
3552 int iMax = a.length - 1;
3556 StringBuilder b = new StringBuilder();
3558 for (int i = 0; i < 100; i++) { // limit to first 100 items
3561 return b.append(']').toString();
3564 return b.append(", ... (" + a.length + ")]").toString();
3568 public <T> void forValue(Resource subject, Binding binding,
3569 SyncProcedure<T> procedure) {
3570 forValue(subject, binding, new SyncToAsyncProcedure<T>(procedure));
3574 public <T> void forValue(Resource subject, Binding binding,
3575 Procedure<T> procedure) {
3576 forValue(subject, binding, new NoneToAsyncProcedure<T>(procedure));
3580 public <T> void forValue(Resource subject, AsyncListener<T> listener) {
3581 asyncRequest(new ValueImplied<T>(subject), listener);
3585 public <T> void forValue(Resource subject, SyncListener<T> listener) {
3586 asyncRequest(new ValueImplied<T>(subject), listener);
3590 public <T> void forValue(Resource subject, Listener<T> listener) {
3591 asyncRequest(new ValueImplied<T>(subject), listener);
3595 final public <T> void forValue(final Resource subject, final AsyncProcedure<T> procedure) {
3597 assert (subject != null);
3598 assert (procedure != null);
3600 forRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
3603 public void execute(AsyncReadGraph graph, Datatype type) {
3604 // TODO: consider trying Bindings.getBeanBinding(type);
3605 Binding binding = Bindings.getBinding(type);
3606 graph.forValue(subject, binding, procedure);
3610 public void exception(AsyncReadGraph graph, Throwable throwable) {
3611 procedure.exception(graph, new DoesNotContainValueException("Invalid data type", throwable));
3619 public <T> void forValue(Resource subject, SyncProcedure<T> procedure) {
3620 forValue(subject, new SyncToAsyncProcedure<T>(procedure));
3624 public <T> void forValue(Resource subject, Procedure<T> procedure) {
3625 forValue(subject, new NoneToAsyncProcedure<T>(procedure));
3629 public <T> void forRelatedValue(Resource subject, Resource relation,
3630 AsyncListener<T> listener) {
3631 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3635 public <T> void forRelatedValue(Resource subject, Resource relation,
3636 SyncListener<T> listener) {
3637 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3641 public <T> void forRelatedValue(Resource subject, Resource relation,
3642 Listener<T> listener) {
3643 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3647 final public <T> void forRelatedValue(final Resource subject,
3648 final Resource relation, final AsyncProcedure<T> procedure) {
3650 assert (subject != null);
3651 assert (relation != null);
3652 assert (procedure != null);
3654 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3656 processor.forEachObject(this, subject, relation,
3657 new AsyncMultiProcedureAdapter<Resource>() {
3660 public void execute(AsyncReadGraph graph,
3661 final Resource object) {
3663 checkedProcedure.inc();
3665 graph.forValue(object, new AsyncProcedure<Object>() {
3668 public void execute(AsyncReadGraph graph,
3670 checkedProcedure.offer(graph, (T) result);
3671 checkedProcedure.dec(graph);
3675 public void exception(AsyncReadGraph graph,
3677 checkedProcedure.exception(graph, t);
3681 public String toString() {
3682 return "forRelatedValue -> " + procedure;
3690 public void finished(AsyncReadGraph graph) {
3691 checkedProcedure.dec(graph);
3695 public void exception(AsyncReadGraph graph, Throwable t) {
3696 checkedProcedure.exception(graph, t);
3704 public <T> void forRelatedValue(Resource subject, Resource relation,
3705 SyncProcedure<T> procedure) {
3706 forRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
3711 public <T> void forRelatedValue(Resource subject, Resource relation,
3712 Procedure<T> procedure) {
3713 forRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
3718 public <T> void forRelatedValue(Resource subject, Resource relation,
3719 Binding binding, AsyncListener<T> listener) {
3720 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3724 public <T> void forRelatedValue(Resource subject, Resource relation,
3725 Binding binding, SyncListener<T> listener) {
3726 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3730 public <T> void forRelatedValue(Resource subject, Resource relation,
3731 Binding binding, Listener<T> listener) {
3732 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3736 final public <T> void forRelatedValue(final Resource subject,
3737 final Resource relation, final Binding binding,
3738 final AsyncProcedure<T> procedure) {
3740 assert (subject != null);
3741 assert (relation != null);
3742 assert (binding != null);
3743 assert (procedure != null);
3745 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3747 processor.forEachObject(this, subject, relation,
3748 new AsyncMultiProcedureAdapter<Resource>() {
3751 public void execute(AsyncReadGraph graph,
3752 final Resource object) {
3754 checkedProcedure.inc();
3756 graph.forValue(object, binding, new AsyncProcedure<Object>() {
3759 public void execute(AsyncReadGraph graph,
3762 checkedProcedure.offer(graph,
3764 checkedProcedure.dec(graph);
3768 public void exception(AsyncReadGraph graph,
3770 checkedProcedure.exception(graph, t);
3774 public String toString() {
3775 return "forRelatedValue -> "
3784 public void finished(AsyncReadGraph graph) {
3785 checkedProcedure.dec(graph);
3789 public void exception(AsyncReadGraph graph, Throwable t) {
3790 checkedProcedure.exception(graph, t);
3798 public <T> void forRelatedValue(Resource subject, Resource relation,
3799 Binding binding, SyncProcedure<T> procedure) {
3800 forRelatedValue(subject, relation, binding,
3801 new SyncToAsyncProcedure<T>(procedure));
3805 public <T> void forRelatedValue(Resource subject, Resource relation,
3806 Binding binding, Procedure<T> procedure) {
3807 forRelatedValue(subject, relation, binding,
3808 new NoneToAsyncProcedure<T>(procedure));
3812 public <T> void forAdapted(Resource resource, Class<T> clazz,
3813 AsyncListener<T> listener) {
3814 asyncRequest(new Adapter<T>(resource, clazz), listener);
3818 public <T> void forAdapted(Resource resource, Class<T> clazz,
3819 SyncListener<T> listener) {
3820 asyncRequest(new Adapter<T>(resource, clazz), listener);
3824 public <T> void forAdapted(Resource resource, Class<T> clazz,
3825 Listener<T> listener) {
3826 asyncRequest(new Adapter<T>(resource, clazz), listener);
3830 final public <T> void forAdapted(final Resource resource,
3831 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3833 assert (resource != null);
3834 assert (clazz != null);
3835 assert (procedure != null);
3837 final AdaptionService service = getSession().peekService(AdaptionService.class);
3838 if (service == null)
3839 procedure.exception(this, new ServiceException("No AdaptionService available"));
3841 service.adapt(this, resource, resource, Resource.class, clazz, false, procedure);
3846 public <T> void forAdapted(Resource resource, Class<T> clazz,
3847 SyncProcedure<T> procedure) {
3848 forAdapted(resource, clazz, new SyncToAsyncProcedure<T>(procedure));
3852 public <T> void forAdapted(Resource resource, Class<T> clazz,
3853 Procedure<T> procedure) {
3854 forAdapted(resource, clazz, new NoneToAsyncProcedure<T>(procedure));
3858 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3859 AsyncListener<T> listener) {
3860 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3864 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3865 SyncListener<T> listener) {
3866 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3870 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3871 Listener<T> listener) {
3872 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3876 final public <T> void forUniqueAdapted(final Resource resource,
3877 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3879 assert (resource != null);
3880 assert (clazz != null);
3881 assert (procedure != null);
3883 final AdaptionService service = getSession().peekService(AdaptionService.class);
3884 if (service == null)
3885 procedure.exception(this, new ServiceException("No AdaptionService available"));
3887 service.adaptNew(this, resource, clazz, false, procedure);
3892 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3893 SyncProcedure<T> procedure) {
3894 forUniqueAdapted(resource, clazz,
3895 new SyncToAsyncProcedure<T>(procedure));
3899 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3900 Procedure<T> procedure) {
3901 forUniqueAdapted(resource, clazz,
3902 new NoneToAsyncProcedure<T>(procedure));
3906 public void forPossibleInverse(Resource subject,
3907 AsyncListener<Resource> listener) {
3908 asyncRequest(new PossibleInverse(subject), listener);
3912 public void forPossibleInverse(Resource subject,
3913 SyncListener<Resource> listener) {
3914 asyncRequest(new PossibleInverse(subject), listener);
3918 public void forPossibleInverse(Resource subject, Listener<Resource> listener) {
3919 asyncRequest(new PossibleInverse(subject), listener);
3923 final public void forPossibleInverse(final Resource relation,
3924 final AsyncProcedure<Resource> procedure) {
3926 assert (relation != null);
3927 assert (procedure != null);
3929 processor.forInverse(this, relation, new ExceptionToNullProcedure<Resource>(procedure));
3934 public void forPossibleInverse(Resource subject,
3935 SyncProcedure<Resource> procedure) {
3936 forPossibleInverse(subject, new SyncToAsyncProcedure<Resource>(
3941 public void forPossibleInverse(Resource subject,
3942 Procedure<Resource> procedure) {
3943 forPossibleInverse(subject, new NoneToAsyncProcedure<Resource>(
3948 public void forPossibleObject(Resource subject, Resource relation,
3949 AsyncListener<Resource> listener) {
3950 asyncRequest(new PossibleObject(subject, relation), listener);
3954 public void forPossibleObject(Resource subject, Resource relation,
3955 SyncListener<Resource> listener) {
3956 asyncRequest(new PossibleObject(subject, relation), listener);
3960 public void forPossibleObject(Resource subject, Resource relation,
3961 Listener<Resource> listener) {
3962 asyncRequest(new PossibleObject(subject, relation), listener);
3966 final public void forPossibleObject(final Resource subject,
3967 final Resource relation, final AsyncProcedure<Resource> procedure) {
3969 assert (subject != null);
3970 assert (relation != null);
3971 assert (procedure != null);
3973 processor.forEachObject(this, subject, relation,
3974 new SingleOrNullProcedure<Resource>(procedure));
3979 public void forPossibleObject(Resource subject, Resource relation,
3980 SyncProcedure<Resource> procedure) {
3981 forPossibleObject(subject, relation,
3982 new SyncToAsyncProcedure<Resource>(procedure));
3986 public void forPossibleObject(Resource subject, Resource relation,
3987 Procedure<Resource> procedure) {
3988 forPossibleObject(subject, relation,
3989 new NoneToAsyncProcedure<Resource>(procedure));
3993 public void forPossibleStatement(Resource subject, Resource relation,
3994 AsyncListener<Statement> listener) {
3995 asyncRequest(new PossibleStatement(subject, relation), listener);
3999 public void forPossibleStatement(Resource subject, Resource relation,
4000 SyncListener<Statement> listener) {
4001 asyncRequest(new PossibleStatement(subject, relation), listener);
4005 public void forPossibleStatement(Resource subject, Resource relation,
4006 Listener<Statement> listener) {
4007 asyncRequest(new PossibleStatement(subject, relation), listener);
4011 final public void forPossibleStatement(final Resource subject,
4012 final Resource relation, final AsyncProcedure<Statement> procedure) {
4014 assert (subject != null);
4015 assert (relation != null);
4016 assert (procedure != null);
4018 processor.forEachStatement(this, subject, relation,
4019 new SingleFunctionalOrNullProcedure<Statement>(
4020 "forPossibleStatement", procedure));
4025 public void forPossibleStatement(Resource subject, Resource relation,
4026 SyncProcedure<Statement> procedure) {
4027 forPossibleStatement(subject, relation,
4028 new SyncToAsyncProcedure<Statement>(procedure));
4032 public void forPossibleStatement(Resource subject, Resource relation,
4033 Procedure<Statement> procedure) {
4034 forPossibleStatement(subject, relation,
4035 new NoneToAsyncProcedure<Statement>(procedure));
4039 public void forPossibleType(Resource subject, Resource relation,
4040 AsyncListener<Resource> listener) {
4041 asyncRequest(new PossibleType(subject, relation), listener);
4045 public void forPossibleType(Resource subject, Resource relation,
4046 SyncListener<Resource> listener) {
4047 asyncRequest(new PossibleType(subject, relation), listener);
4051 public void forPossibleType(Resource subject, Resource relation,
4052 Listener<Resource> listener) {
4053 asyncRequest(new PossibleType(subject, relation), listener);
4057 final public void forPossibleType(final Resource subject,
4058 final Resource baseType, final AsyncProcedure<Resource> procedure) {
4060 assert (subject != null);
4061 assert (procedure != null);
4063 final NullSingleOrNullProcedure<Resource> checkedProcedure = new NullSingleOrNullProcedure<Resource>(procedure);
4065 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
4068 public void execute(AsyncReadGraph graph,
4069 final Resource principalType) {
4071 if (baseType == null) {
4073 checkedProcedure.offer(graph, principalType);
4075 } else if (principalType.equals(baseType)) {
4077 checkedProcedure.offer(graph, principalType);
4081 checkedProcedure.inc();
4083 processor.forSupertypes((ReadGraphImpl)graph, principalType,
4084 new AsyncProcedure<Set<Resource>>() {
4087 public void execute(
4088 AsyncReadGraph graph,
4089 Set<Resource> result) {
4091 if (result.contains(baseType)) {
4092 checkedProcedure.offer(graph,
4096 checkedProcedure.dec(graph);
4101 public void exception(
4102 AsyncReadGraph graph,
4104 checkedProcedure.exception(graph, t);
4105 checkedProcedure.dec(graph);
4109 public String toString() {
4110 return "forPossibleType -> "
4121 public void finished(AsyncReadGraph graph) {
4122 checkedProcedure.dec(graph);
4126 public void exception(AsyncReadGraph graph, Throwable t) {
4127 checkedProcedure.exception(graph, t);
4128 checkedProcedure.dec(graph);
4136 public void forPossibleType(Resource subject, Resource relation,
4137 SyncProcedure<Resource> procedure) {
4138 forPossibleType(subject, relation, new SyncToAsyncProcedure<Resource>(
4143 public void forPossibleType(Resource subject, Resource relation,
4144 Procedure<Resource> procedure) {
4145 forPossibleType(subject, relation, new NoneToAsyncProcedure<Resource>(
4150 public <T> void forPossibleValue(Resource subject, AsyncListener<T> listener) {
4151 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4155 public <T> void forPossibleValue(Resource subject, SyncListener<T> listener) {
4156 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4160 public <T> void forPossibleValue(Resource subject, Listener<T> listener) {
4161 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4165 final public <T> void forPossibleValue(final Resource subject,
4166 final AsyncProcedure<T> procedure) {
4168 assert (subject != null);
4169 assert (procedure != null);
4171 forPossibleRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
4174 public void execute(AsyncReadGraph graph, final Datatype type) {
4176 procedure.execute(graph, null);
4179 // TODO: consider trying Bindings.getBeanBinding(type);
4180 Binding binding = Bindings.getBinding(type);
4181 graph.forPossibleValue(subject, binding, procedure);
4182 } catch (RuntimeBindingConstructionException e) {
4183 procedure.exception(graph, e);
4189 public void exception(AsyncReadGraph graph, Throwable t) {
4190 procedure.exception(graph, t);
4194 public String toString() {
4195 return "forPossibleValue -> " + procedure;
4203 public <T> void forPossibleValue(Resource subject,
4204 SyncProcedure<T> procedure) {
4205 forPossibleValue(subject, new SyncToAsyncProcedure<T>(procedure));
4209 public <T> void forPossibleValue(Resource subject, Procedure<T> procedure) {
4210 forPossibleValue(subject, new NoneToAsyncProcedure<T>(procedure));
4214 public <T> void forPossibleValue(Resource subject, Binding binding,
4215 AsyncListener<T> listener) {
4216 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4220 public <T> void forPossibleValue(Resource subject, Binding binding,
4221 SyncListener<T> listener) {
4222 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4226 public <T> void forPossibleValue(Resource subject, Binding binding,
4227 Listener<T> listener) {
4228 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4232 final public <T> void forPossibleValue(final Resource resource,
4233 final Binding binding, final AsyncProcedure<T> procedure) {
4235 assert (resource != null);
4236 assert (binding != null);
4237 assert (procedure != null);
4239 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
4242 public void execute(AsyncReadGraph graph, byte[] result) {
4246 if (result == null) {
4247 procedure.execute(graph, null);
4251 Serializer serializer = Bindings.getSerializer( binding );
4252 Object obj = serializer.deserialize(result);
4253 if (!binding.isInstance(obj))
4254 procedure.exception(graph, new ClassCastException(
4255 "Cannot get value " + obj + " with binding "
4258 procedure.execute(graph, (T) obj);
4260 } catch (Throwable t) {
4261 procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
4266 public void exception(AsyncReadGraph graph, Throwable t) {
4268 procedure.exception(graph, t);
4269 } catch (Throwable t2) {
4270 Logger.defaultLogError(t2);
4275 public String toString() {
4276 return "forPossibleValue -> " + procedure;
4284 public <T> void forPossibleValue(Resource subject, Binding binding,
4285 SyncProcedure<T> procedure) {
4286 forPossibleValue(subject, binding, new SyncToAsyncProcedure<T>(
4291 public <T> void forPossibleValue(Resource subject, Binding binding,
4292 Procedure<T> procedure) {
4293 forPossibleValue(subject, binding, new NoneToAsyncProcedure<T>(
4298 public <T> void forPossibleRelatedValue(Resource subject,
4299 Resource relation, AsyncListener<T> listener) {
4300 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4305 public <T> void forPossibleRelatedValue(Resource subject,
4306 Resource relation, SyncListener<T> listener) {
4307 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4312 public <T> void forPossibleRelatedValue(Resource subject,
4313 Resource relation, Listener<T> listener) {
4314 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4319 final public <T> void forPossibleRelatedValue(final Resource subject,
4320 final Resource relation, final AsyncProcedure<T> procedure) {
4322 assert (subject != null);
4323 assert (relation != null);
4324 assert (procedure != null);
4326 final DeepSingleOrNullProcedure<T> checkedProcedure = new DeepSingleOrNullProcedure<T>(procedure);
4328 processor.forEachObject(this, subject, relation,
4329 new AsyncMultiProcedureAdapter<Resource>() {
4332 public void execute(AsyncReadGraph graph,
4333 final Resource object) {
4335 checkedProcedure.inc();
4337 graph.forValue(object, new AsyncProcedure<Object>() {
4340 public void execute(AsyncReadGraph graph,
4342 checkedProcedure.offer(graph, (T) result);
4343 checkedProcedure.dec(graph);
4347 public void exception(AsyncReadGraph graph,
4349 checkedProcedure.exception(graph, t);
4350 checkedProcedure.dec(graph);
4358 public void finished(AsyncReadGraph graph) {
4360 checkedProcedure.dec(graph);
4364 public void exception(AsyncReadGraph graph, Throwable t) {
4365 checkedProcedure.exception(graph, t);
4366 checkedProcedure.dec(graph);
4370 public String toString() {
4371 return "forPossibleRelatedValue -> " + procedure;
4378 public <T> void forPossibleRelatedValue(Resource subject,
4379 Resource relation, SyncProcedure<T> procedure) {
4380 forPossibleRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
4385 public <T> void forPossibleRelatedValue(Resource subject,
4386 Resource relation, Procedure<T> procedure) {
4387 forPossibleRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
4392 public <T> void forPossibleRelatedValue(Resource subject,
4393 Resource relation, Binding binding, AsyncListener<T> listener) {
4394 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4399 public <T> void forPossibleRelatedValue(Resource subject,
4400 Resource relation, Binding binding, SyncListener<T> listener) {
4401 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4406 public <T> void forPossibleRelatedValue(Resource subject,
4407 Resource relation, Binding binding, Listener<T> listener) {
4408 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4412 final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4413 final AsyncProcedure<T> procedure) {
4415 assert (subject != null);
4416 assert (relation != null);
4417 assert (procedure != null);
4419 processor.forPossibleObject(this, subject, relation, new AsyncProcedure<Resource>() {
4422 public void execute(AsyncReadGraph graph, Resource object) {
4424 if(object == null) {
4425 procedure.execute(graph, null);
4429 processor.forPossibleValue((ReadGraphImpl)graph, object, new AsyncProcedure<byte[]>() {
4432 public void execute(AsyncReadGraph graph, byte[] bytes) {
4438 Serializer serializer = binding.serializer();
4439 Object obj = serializer.deserialize(bytes);
4440 if (!binding.isInstance(obj)) {
4441 procedure.exception(graph, new ClassCastException("Cannot get value " + obj + " with binding " + binding));
4443 procedure.execute(graph, (T) obj);
4446 } catch (Throwable t) {
4448 procedure.exception(graph, t);
4454 procedure.execute(graph, null);
4461 public void exception(AsyncReadGraph graph, Throwable t) {
4462 procedure.exception(graph, t);
4470 public void exception(AsyncReadGraph graph, Throwable throwable) {
4471 throwable.printStackTrace();
4472 procedure.exception(graph, throwable);
4480 public <T> void forPossibleRelatedValue(Resource subject,
4481 Resource relation, Binding binding, SyncProcedure<T> procedure) {
4482 forPossibleRelatedValue(subject, relation, binding,
4483 new SyncToAsyncProcedure<T>(procedure));
4487 public <T> void forPossibleRelatedValue(Resource subject,
4488 Resource relation, Binding binding, Procedure<T> procedure) {
4489 forPossibleRelatedValue(subject, relation, binding,
4490 new NoneToAsyncProcedure<T>(procedure));
4494 public void forIsInstanceOf(Resource subject, Resource relation,
4495 AsyncListener<Boolean> listener) {
4496 asyncRequest(new IsInstanceOf(subject, relation), listener);
4500 public void forIsInstanceOf(Resource subject, Resource relation,
4501 SyncListener<Boolean> listener) {
4502 asyncRequest(new IsInstanceOf(subject, relation), listener);
4506 public void forIsInstanceOf(Resource subject, Resource relation,
4507 Listener<Boolean> listener) {
4508 asyncRequest(new IsInstanceOf(subject, relation), listener);
4512 final public void forIsInstanceOf(final Resource resource,
4513 final Resource type, final AsyncProcedure<Boolean> procedure) {
4515 assert (resource != null);
4516 assert (type != null);
4517 assert (procedure != null);
4519 forTypes(resource, new AsyncProcedure<Set<Resource>>() {
4522 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4525 if (result.contains(type))
4526 procedure.execute(graph, true);
4528 procedure.execute(graph, false);
4529 } catch (Throwable t) {
4530 Logger.defaultLogError(t);
4535 public void exception(AsyncReadGraph graph, Throwable t) {
4537 procedure.exception(graph, t);
4538 } catch (Throwable t2) {
4539 Logger.defaultLogError(t2);
4544 public String toString() {
4545 return "forIsInstanceOf -> " + procedure;
4553 public void forIsInstanceOf(Resource subject, Resource relation,
4554 SyncProcedure<Boolean> procedure) {
4555 forIsInstanceOf(subject, relation, new SyncToAsyncProcedure<Boolean>(
4560 public void forIsInstanceOf(Resource subject, Resource relation,
4561 Procedure<Boolean> procedure) {
4562 forIsInstanceOf(subject, relation, new NoneToAsyncProcedure<Boolean>(
4567 public void forIsInheritedFrom(Resource subject, Resource relation,
4568 AsyncListener<Boolean> listener) {
4569 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4573 public void forIsInheritedFrom(Resource subject, Resource relation,
4574 SyncListener<Boolean> listener) {
4575 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4579 public void forIsInheritedFrom(Resource subject, Resource relation,
4580 Listener<Boolean> listener) {
4581 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4585 final public void forIsInheritedFrom(final Resource resource,
4586 final Resource type, final AsyncProcedure<Boolean> procedure) {
4588 assert (resource != null);
4589 assert (type != null);
4590 assert (procedure != null);
4592 if (resource.equals(type)) {
4594 procedure.execute(this, true);
4595 } catch (Throwable t) {
4596 Logger.defaultLogError(t);
4601 forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
4604 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4606 if (result.contains(type))
4607 procedure.execute(graph, true);
4609 procedure.execute(graph, false);
4610 } catch (Throwable t) {
4611 Logger.defaultLogError(t);
4616 public void exception(AsyncReadGraph graph, Throwable t) {
4618 procedure.exception(graph, t);
4619 } catch (Throwable t2) {
4620 Logger.defaultLogError(t2);
4625 public String toString() {
4626 return "forIsInheritedFrom -> " + procedure;
4634 public void forIsInheritedFrom(Resource subject, Resource relation,
4635 SyncProcedure<Boolean> procedure) {
4636 forIsInheritedFrom(subject, relation,
4637 new SyncToAsyncProcedure<Boolean>(procedure));
4641 public void forIsInheritedFrom(Resource subject, Resource relation,
4642 Procedure<Boolean> procedure) {
4643 forIsInheritedFrom(subject, relation,
4644 new NoneToAsyncProcedure<Boolean>(procedure));
4648 public void forIsSubrelationOf(Resource subject, Resource relation,
4649 AsyncListener<Boolean> listener) {
4650 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4654 public void forIsSubrelationOf(Resource subject, Resource relation,
4655 SyncListener<Boolean> listener) {
4656 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4660 public void forIsSubrelationOf(Resource subject, Resource relation,
4661 Listener<Boolean> listener) {
4662 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4666 final public void forIsSubrelationOf(final Resource resource,
4667 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4669 assert (resource != null);
4670 assert (relation != null);
4671 assert (procedure != null);
4673 if (resource.equals(relation)) {
4674 procedure.execute(this, true);
4678 forSuperrelations(resource, new AsyncProcedure<Set<Resource>>() {
4681 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4683 if (result.contains(relation))
4684 procedure.execute(graph, true);
4686 procedure.execute(graph, false);
4687 } catch (Throwable t) {
4688 Logger.defaultLogError(t);
4693 public void exception(AsyncReadGraph graph, Throwable t) {
4695 procedure.exception(graph, t);
4696 } catch (Throwable t2) {
4697 Logger.defaultLogError(t2);
4702 public String toString() {
4703 return "forIsSubrelationOf -> " + procedure;
4711 public void forIsSubrelationOf(Resource subject, Resource relation,
4712 SyncProcedure<Boolean> procedure) {
4713 forIsSubrelationOf(subject, relation,
4714 new SyncToAsyncProcedure<Boolean>(procedure));
4718 public void forIsSubrelationOf(Resource subject, Resource relation,
4719 Procedure<Boolean> procedure) {
4720 forIsSubrelationOf(subject, relation,
4721 new NoneToAsyncProcedure<Boolean>(procedure));
4725 public void forHasStatement(Resource subject,
4726 AsyncListener<Boolean> listener) {
4727 asyncRequest(new HasStatementSubject(subject), listener);
4731 public void forHasStatement(Resource subject, SyncListener<Boolean> listener) {
4732 asyncRequest(new HasStatementSubject(subject), listener);
4736 public void forHasStatement(Resource subject, Listener<Boolean> listener) {
4737 asyncRequest(new HasStatementSubject(subject), listener);
4741 final public void forHasStatement(final Resource subject,
4742 final AsyncProcedure<Boolean> procedure) {
4744 assert (subject != null);
4745 assert (procedure != null);
4747 processor.forHasStatement(this, subject, procedure);
4752 public void forHasStatement(Resource subject,
4753 SyncProcedure<Boolean> procedure) {
4754 forHasStatement(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4758 public void forHasStatement(Resource subject, Procedure<Boolean> procedure) {
4759 forHasStatement(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4763 public void forHasStatement(Resource subject, Resource relation,
4764 AsyncListener<Boolean> listener) {
4765 asyncRequest(new HasStatement(subject, relation), listener);
4769 public void forHasStatement(Resource subject, Resource relation,
4770 SyncListener<Boolean> listener) {
4771 asyncRequest(new HasStatement(subject, relation), listener);
4775 public void forHasStatement(Resource subject, Resource relation,
4776 Listener<Boolean> listener) {
4777 asyncRequest(new HasStatement(subject, relation), listener);
4781 final public void forHasStatement(final Resource subject,
4782 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4784 assert (subject != null);
4785 assert (relation != null);
4786 assert (procedure != null);
4788 processor.forHasStatement(this, subject, relation, procedure);
4793 public void forHasStatement(Resource subject, Resource relation,
4794 SyncProcedure<Boolean> procedure) {
4795 forHasStatement(subject, relation, new SyncToAsyncProcedure<Boolean>(
4800 public void forHasStatement(Resource subject, Resource relation,
4801 Procedure<Boolean> procedure) {
4802 forHasStatement(subject, relation, new NoneToAsyncProcedure<Boolean>(
4807 public void forHasStatement(Resource subject, Resource relation,
4808 Resource object, AsyncListener<Boolean> listener) {
4809 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4814 public void forHasStatement(Resource subject, Resource relation,
4815 Resource object, SyncListener<Boolean> listener) {
4816 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4821 public void forHasStatement(Resource subject, Resource relation,
4822 Resource object, Listener<Boolean> listener) {
4823 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4828 final public void forHasStatement(final Resource subject,
4829 final Resource relation, final Resource object,
4830 final AsyncProcedure<Boolean> procedure) {
4832 assert (subject != null);
4833 assert (relation != null);
4834 assert (object != null);
4835 assert (procedure != null);
4837 processor.forHasStatement(this, subject, relation, object, procedure);
4842 public void forHasStatement(Resource subject, Resource relation,
4843 Resource object, SyncProcedure<Boolean> procedure) {
4844 forHasStatement(subject, relation, object,
4845 new SyncToAsyncProcedure<Boolean>(procedure));
4849 public void forHasStatement(Resource subject, Resource relation,
4850 Resource object, Procedure<Boolean> procedure) {
4851 forHasStatement(subject, relation, object,
4852 new NoneToAsyncProcedure<Boolean>(procedure));
4856 public void forHasValue(Resource subject, AsyncListener<Boolean> listener) {
4857 asyncRequest(new HasValue(subject), listener);
4861 public void forHasValue(Resource subject, SyncListener<Boolean> listener) {
4862 asyncRequest(new HasValue(subject), listener);
4866 public void forHasValue(Resource subject, Listener<Boolean> listener) {
4867 asyncRequest(new HasValue(subject), listener);
4871 final public void forHasValue(final Resource subject,
4872 final AsyncProcedure<Boolean> procedure) {
4874 assert (subject != null);
4875 assert (procedure != null);
4877 processor.forValue(this, subject, new AsyncProcedure<byte[]>() {
4880 public void execute(AsyncReadGraph graph, byte[] result) {
4883 procedure.execute(graph, false);
4885 procedure.execute(graph, true);
4886 } catch (Throwable t) {
4887 Logger.defaultLogError(t);
4892 public void exception(AsyncReadGraph graph, Throwable t) {
4894 procedure.exception(graph, t);
4895 } catch (Throwable t2) {
4896 Logger.defaultLogError(t2);
4901 public String toString() {
4902 return "forHasValue -> " + procedure;
4910 public void forHasValue(Resource subject, SyncProcedure<Boolean> procedure) {
4911 forHasValue(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4915 public void forHasValue(Resource subject, Procedure<Boolean> procedure) {
4916 forHasValue(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4920 public void forOrderedSet(Resource subject,
4921 AsyncMultiListener<Resource> listener) {
4922 asyncRequest(new OrderedSet(subject), listener);
4926 public void forOrderedSet(Resource subject,
4927 SyncMultiListener<Resource> listener) {
4928 asyncRequest(new OrderedSet(subject), listener);
4932 public void forOrderedSet(Resource subject, MultiListener<Resource> listener) {
4933 asyncRequest(new OrderedSet(subject), listener);
4937 final public void forOrderedSet(final Resource subject,
4938 final AsyncMultiProcedure<Resource> procedure) {
4940 assert (subject != null);
4941 assert (procedure != null);
4943 processor.forOrderedSet(this, subject,
4944 new AsyncMultiProcedure<Resource>() {
4947 public void finished(AsyncReadGraph graph) {
4949 procedure.finished(graph);
4950 } catch (Throwable t) {
4951 Logger.defaultLogError(t);
4956 public void execute(AsyncReadGraph graph, Resource result) {
4958 procedure.execute(graph, result);
4959 } catch (Throwable t) {
4960 Logger.defaultLogError(t);
4965 public void exception(AsyncReadGraph graph, Throwable t) {
4967 procedure.exception(graph, t);
4968 } catch (Throwable t2) {
4969 Logger.defaultLogError(t2);
4974 public String toString() {
4975 return "forOrderedSet -> " + procedure;
4983 public void forOrderedSet(Resource subject,
4984 SyncMultiProcedure<Resource> procedure) {
4985 forOrderedSet(subject, new SyncToAsyncMultiProcedure<Resource>(
4990 public void forOrderedSet(Resource subject,
4991 MultiProcedure<Resource> procedure) {
4992 forOrderedSet(subject, new NoneToAsyncMultiProcedure<Resource>(
4997 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
4998 AsyncListener<T> listener) {
4999 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5003 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5004 SyncListener<T> listener) {
5005 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5009 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5010 Listener<T> listener) {
5011 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5015 final public <T> void forPossibleAdapted(final Resource resource,
5016 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5018 assert (resource != null);
5019 assert (clazz != null);
5020 assert (procedure != null);
5022 final AdaptionService service = getSession().peekService(AdaptionService.class);
5023 if (service == null)
5024 procedure.exception(this, new ServiceException("No AdaptionService available"));
5026 service.adapt(this, resource, resource, Resource.class, clazz, true, procedure);
5030 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5031 SyncProcedure<T> procedure) {
5032 forPossibleAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5037 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5038 Procedure<T> procedure) {
5039 forPossibleAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5044 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5045 AsyncListener<T> listener) {
5046 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5050 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5051 SyncListener<T> listener) {
5052 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5056 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5057 Listener<T> listener) {
5058 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5062 final public <T> void forPossibleUniqueAdapted(final Resource resource,
5063 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5065 assert (resource != null);
5066 assert (clazz != null);
5067 assert (procedure != null);
5069 final AdaptionService service = getSession().peekService(AdaptionService.class);
5070 if (service == null)
5071 procedure.exception(this, new ServiceException("No AdaptionService available"));
5073 service.adaptNew(this, resource, clazz, true, procedure);
5078 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5079 SyncProcedure<T> procedure) {
5080 forPossibleUniqueAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5085 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5086 Procedure<T> procedure) {
5087 forPossibleUniqueAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5092 * Implementation of the interface AsyncRequestProcessor
5096 final public Session getSession() {
5097 return processor.getSession();
5101 public <T> void asyncRequest(final Read<T> request) {
5103 asyncRequest(request, new AsyncProcedure<T>() {
5106 public void execute(AsyncReadGraph graph, T result) {
5110 public void exception(AsyncReadGraph graph, Throwable t) {
5111 Logger.defaultLogError(t);
5115 public String toString() {
5116 return "asyncRequest(Read) -> " + request;
5124 public <T> void asyncRequest(Read<T> request, AsyncListener<T> procedure) {
5125 asyncRequest(request, (AsyncProcedure<T>) procedure);
5129 public <T> void asyncRequest(Read<T> request,
5130 final SyncListener<T> procedure) {
5131 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5135 public <T> void asyncRequest(Read<T> request, final Listener<T> procedure) {
5136 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5140 public <T> void asyncRequest(final Read<T> request, final AsyncProcedure<T> procedure) {
5142 assert (request != null);
5143 assert (procedure != null);
5145 AsyncBarrierImpl barrier = asyncBarrier;
5149 processor.scheduleNow(new SessionTask(this) {
5152 public void run0(int thread) {
5154 final ListenerBase listener = getListenerBase(procedure);
5155 QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5156 } catch (DatabaseException e) {
5157 Logger.defaultLogError(e);
5168 public static ReadGraphImpl createAsync(QueryProcessor support) {
5169 return new ReadGraphImpl(null, null, support);
5173 public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
5174 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5178 public <T> void asyncRequest(Read<T> request, final Procedure<T> procedure) {
5179 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5183 final public <T> void asyncRequest(final AsyncRead<T> request) {
5185 assert (request != null);
5187 asyncRequest(request, new AsyncProcedure<T>() {
5190 public void execute(AsyncReadGraph graph, T result) {
5194 public void exception(AsyncReadGraph graph, Throwable t) {
5195 Logger.defaultLogError(t);
5199 public String toString() {
5200 return "asyncRequest(AsyncRead) -> " + request;
5208 public <T> void asyncRequest(AsyncRead<T> request,
5209 AsyncListener<T> procedure) {
5210 asyncRequest(request, (AsyncProcedure<T>) procedure);
5214 final public <T> void asyncRequest(AsyncRead<T> request,
5215 final SyncListener<T> procedure) {
5216 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5220 final public <T> void asyncRequest(AsyncRead<T> request,
5221 final Listener<T> procedure) {
5222 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5226 final public <T> void asyncRequest(final AsyncRead<T> request,
5227 final AsyncProcedure<T> procedure) {
5229 assert (request != null);
5230 assert (procedure != null);
5232 AsyncBarrierImpl barrier = asyncBarrier;
5236 processor.scheduleNow(new SessionTask(this) {
5239 public void run0(int thread) {
5245 final ListenerBase listener = getListenerBase(procedure);
5246 QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, new AsyncProcedure<T>() {
5249 public void execute(AsyncReadGraph graph, T result) {
5250 procedure.execute(graph, result);
5256 public void exception(AsyncReadGraph graph, Throwable throwable) {
5257 procedure.exception(graph, throwable);
5263 } catch (DatabaseException e) {
5264 LOGGER.error("Error while executing async request", e);
5276 public <T> void asyncRequest(AsyncRead<T> request,
5277 SyncProcedure<T> procedure) {
5278 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5282 final public <T> void asyncRequest(final AsyncRead<T> request,
5283 final Procedure<T> procedure) {
5284 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5288 public <T> void asyncRequest(final MultiRead<T> request) {
5290 assert (request != null);
5292 asyncRequest(request, new SyncMultiProcedureAdapter<T>() {
5294 public void exception(ReadGraph graph, Throwable t) {
5295 Logger.defaultLogError(t);
5299 public String toString() {
5300 return "asyncRequest(MultiRead) -> " + request;
5307 public <T> void asyncRequest(MultiRead<T> request,
5308 SyncMultiListener<T> procedure) {
5309 asyncRequest(request, (SyncMultiProcedure<T>)procedure);
5313 public <T> void asyncRequest(MultiRead<T> request,
5314 MultiListener<T> procedure) {
5315 asyncRequest(request, new NoneToSyncMultiListener<T>(procedure));
5320 public <T> void asyncRequest(final MultiRead<T> request,
5321 final SyncMultiProcedure<T> procedure) {
5323 assert (request != null);
5324 assert (procedure != null);
5326 final ListenerBase listener = getListenerBase(procedure);
5328 if (parent != null || listener != null) {
5330 // final ReadGraphImpl newGraph = newSync();
5331 processor.query(this, request, parent, procedure,listener);
5335 // final ReadGraphImpl newGraph = newSync();
5339 request.perform(this, procedure);
5341 } catch (Throwable t) {
5344 procedure.exception(this, t);
5345 } catch (DatabaseException e) {
5346 LOGGER.error("Unexpected exception while handling exception", e);
5356 public <T> void asyncRequest(MultiRead<T> request,
5357 MultiProcedure<T> procedure) {
5358 asyncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
5362 final public <T> void asyncRequest(final AsyncMultiRead<T> request) {
5364 assert (request != null);
5366 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5368 public void exception(AsyncReadGraph graph, Throwable t) {
5369 Logger.defaultLogError(t);
5373 public String toString() {
5374 return "asyncRequest(AsyncMultiRead) -> " + request;
5381 public <T> void asyncRequest(AsyncMultiRead<T> request,
5382 AsyncMultiListener<T> procedure) {
5383 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5387 public <T> void asyncRequest(AsyncMultiRead<T> request,
5388 SyncMultiListener<T> procedure) {
5389 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5393 public <T> void asyncRequest(AsyncMultiRead<T> request,
5394 MultiListener<T> procedure) {
5395 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5399 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5400 final AsyncMultiProcedure<T> procedure) {
5402 assert (request != null);
5403 assert (procedure != null);
5405 ListenerBase listener = getListenerBase(procedure);
5407 if (parent != null || listener != null) {
5409 processor.query(this, request, parent, procedure, listener);
5415 request.perform(this, new AsyncMultiProcedure<T>() {
5418 public void execute(AsyncReadGraph graph, T result) {
5419 procedure.execute(graph, result);
5423 public void finished(AsyncReadGraph graph) {
5424 procedure.finished(graph);
5428 public void exception(AsyncReadGraph graph, Throwable t) {
5429 procedure.exception(graph, t);
5433 public String toString() {
5434 return "asyncRequest(AsyncMultiRead) -> " + procedure;
5439 } catch (Throwable t) {
5441 procedure.exception(this, new DatabaseException(t));
5449 public <T> void asyncRequest(AsyncMultiRead<T> request,
5450 SyncMultiProcedure<T> procedure) {
5451 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5455 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5456 final MultiProcedure<T> procedure) {
5457 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5461 final public <T> void asyncRequest(final ExternalRead<T> request) {
5463 assert (request != null);
5465 asyncRequest(request, new Procedure<T>() {
5468 public void execute(T result) {
5472 public void exception(Throwable t) {
5473 Logger.defaultLogError(t);
5477 public String toString() {
5478 return "asyncRequest(PrimitiveRead) -> " + request;
5486 public <T> void asyncRequest(ExternalRead<T> request,
5487 final Listener<T> procedure) {
5488 asyncRequest(request, (Procedure<T>) procedure);
5492 final public <T> void asyncRequest(final ExternalRead<T> request,
5493 final Procedure<T> procedure) {
5495 assert (request != null);
5496 assert (procedure != null);
5498 final ListenerBase listener = getListenerBase(procedure);
5500 if (parent != null || listener != null) {
5503 QueryCacheBase.resultExternalReadEntry(this, request, parent, listener, procedure);
5504 } catch (DatabaseException e) {
5505 Logger.defaultLogError(e);
5506 // This throwable has already been transferred to procedure at this point - do nothing about it
5511 request.register(this, new Listener<T>() {
5513 public void execute(T result) {
5514 procedure.execute(result);
5518 public void exception(Throwable t) {
5519 procedure.exception(t);
5523 public String toString() {
5524 return "asyncRequest(PrimitiveRead) -> " + request;
5528 public boolean isDisposed() {
5539 public void asyncRequest(final Write request) {
5541 assert (request != null);
5543 getSession().asyncRequest(request);
5545 // processor.asyncWrite(request);
5550 public <T> void asyncRequest(final WriteResult<T> request, Procedure<T> procedure) {
5551 throw new Error("Not implemented.");
5555 public void asyncRequest(Write request, Consumer<DatabaseException> callback) {
5557 assert (request != null);
5559 getSession().asyncRequest(request, callback);
5564 public void asyncRequest(final DelayedWrite request) {
5566 assert (request != null);
5568 getSession().asyncRequest(request);
5573 public <T> void asyncRequest(final DelayedWriteResult<T> request, Procedure<T> procedure) {
5574 throw new Error("Not implemented.");
5578 public void asyncRequest(DelayedWrite r,
5579 Consumer<DatabaseException> callback) {
5580 throw new Error("Not implemented.");
5584 public void asyncRequest(final WriteOnly request) {
5586 assert (request != null);
5588 getSession().asyncRequest(request);
5593 public <T> void asyncRequest(final WriteOnlyResult<T> request, Procedure<T> procedure) {
5594 throw new Error("Not implemented.");
5598 public void asyncRequest(WriteOnly r, Consumer<DatabaseException> callback) {
5599 throw new Error("Not implemented.");
5603 * Implementation of the interface ServiceLocator
5607 public <T> T getService(Class<T> api) {
5608 if(WriteSupport.class == api) {
5609 if(this instanceof WriteGraphImpl) {
5610 WriteGraphImpl impl = (WriteGraphImpl)this;
5611 return (T)impl.writeSupport;
5614 return getSession().getService(api);
5618 public <T> T peekService(Class<T> api) {
5619 return getSession().peekService(api);
5623 public boolean hasService(Class<?> api) {
5624 return getSession().hasService(api);
5628 public <T> void registerService(Class<T> api, T service) {
5629 getSession().registerService(api, service);
5633 public boolean isImmutable(Resource resource) throws DatabaseException {
5634 ResourceImpl impl = (ResourceImpl)resource;
5635 return processor.isImmutable(impl.id);
5642 protected static String INTERNAL_ERROR_STRING = "Transaction aborted due to internal client error.";
5645 * callerThread is the currently running thread state.syncThread is blocking for
5646 * this execution state.syncParent is the blocking request
5649 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support) {
5650 this.parentGraph = parentGraph;
5651 this.parent = parent;
5652 this.processor = support;
5653 this.asyncBarrier = prepareBarrier(parentGraph, parent, null, false);
5656 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support, AsyncBarrierImpl asyncBarrier) {
5657 this.parentGraph = parentGraph;
5658 this.parent = parent;
5659 this.processor = support;
5660 this.asyncBarrier = asyncBarrier;
5663 ReadGraphImpl(ReadGraphImpl graph, CacheEntry parent) {
5664 this(graph, parent, graph.processor);
5667 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, Runnable callback, boolean needsToBlock) {
5668 this(parentGraph, parent, parentGraph.processor, prepareBarrier(parentGraph, parent, callback, needsToBlock));
5671 static AsyncBarrierImpl prepareBarrier(ReadGraphImpl parentGraph, CacheEntry parent, Runnable callback, boolean needsToBlock) {
5672 return new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent, callback, needsToBlock);
5675 ReadGraphImpl(ReadGraphImpl graph) {
5676 this(graph, graph.parent);
5679 public ReadGraphImpl withParent(CacheEntry parent, Runnable callback, boolean needsToBlock) {
5680 return new ReadGraphImpl(this, parent, callback, needsToBlock);
5683 public ReadGraphImpl forRecompute(CacheEntry parent) {
5684 return new ReadGraphImpl(null, parent, processor);
5687 public static ReadGraphImpl create(QueryProcessor support) {
5688 ReadGraphImpl result = new ReadGraphImpl(null, null, support);
5692 public ReadGraphImpl newRestart(ReadGraphImpl impl) {
5694 WriteGraphImpl write = processor.getSession().getService(
5695 WriteGraphImpl.class);
5701 final private ListenerBase getListenerBase(final Object procedure) {
5702 if (procedure instanceof ListenerBase)
5703 return (ListenerBase) procedure;
5708 public <T> void waitAsyncProcedure(AsyncMultiReadProcedure<T> procedure) {
5710 assert(procedure.done());
5714 public <T> void waitAsyncProcedure(AsyncReadProcedure<T> procedure) {
5716 assert(procedure.done());
5720 public boolean resumeTasks() {
5721 return processor.resumeTasks(this);
5724 Class<?> singleClass(Set<Resource> types) {
5725 Class<?> result = null;
5726 for (Resource type : types) {
5727 Class<?> clazz = processor.getBuiltinValue(type);
5728 if (clazz != null) {
5738 private String debugString(Resource r) {
5741 name = getPossibleRelatedValue(r, processor.getL0(this).HasName);
5742 } catch (ManyObjectsForFunctionalRelationException e) {
5743 Logger.defaultLogError(e);
5744 } catch (ServiceException e) {
5745 Logger.defaultLogError(e);
5747 return "[" + name + " - " + r + "]";
5751 public String toString() {
5752 return "ReadGraphImpl[thread=" + Thread.currentThread() + "]";
5756 final public int thread() {
5760 static class MultiTripleIntProcedure implements TripleIntProcedure {
5762 final private AsyncMultiProcedure<Statement> procedure;
5763 final private ReadGraphImpl impl;
5764 final private QuerySupport support;
5766 public MultiTripleIntProcedure(AsyncMultiProcedure<Statement> procedure, ReadGraphImpl impl, QuerySupport support) {
5767 this.procedure = procedure;
5769 this.support = support;
5773 public void execute(ReadGraphImpl graph, int s, int p, int o) {
5775 procedure.execute(graph, support.getStatement(s, p, o));
5776 } catch (Throwable t2) {
5777 Logger.defaultLogError(t2);
5782 public void finished(ReadGraphImpl graph) {
5784 procedure.finished(graph);
5785 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5786 } catch (Throwable t2) {
5787 Logger.defaultLogError(t2);
5792 public void exception(ReadGraphImpl graph, Throwable t) {
5794 procedure.exception(graph, t);
5795 } catch (Throwable t2) {
5796 Logger.defaultLogError(t2);
5798 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5802 public String toString() {
5803 return "forEachObject with " + procedure;
5808 // private AsyncMultiProcedure<Resource> cacheKey = null;
5809 // private MultiIntProcedure cacheResult = null;
5811 // final IntProcedure forMultiProcedure(final AsyncMultiProcedure<Resource> procedure) {
5813 // if(procedure == cacheKey) return cacheResult;
5815 // cacheResult = new MultiIntProcedure(procedure, this, processor.support);
5816 // cacheKey = procedure;
5818 // return cacheResult;
5822 // private AsyncMultiProcedure<Statement> cacheKey2 = null;
5823 // private MultiTripleIntProcedure cacheResult2 = null;
5825 // final synchronized TripleIntProcedure forMultiProcedure(final AsyncMultiProcedure<Statement> procedure) {
5827 // if(procedure == cacheKey2) return cacheResult2;
5829 // cacheResult2 = new MultiTripleIntProcedure(procedure, this, processor.support);
5830 // cacheKey2 = procedure;
5832 // return cacheResult2;
5837 public Datatype getDataType(Resource subject) throws DatabaseException {
5838 for(Resource dataTypeResource : getObjects(subject, processor.getL0(this).HasDataType))
5839 return getValue(dataTypeResource, Bindings.getBindingUnchecked(Datatype.class));
5840 throw new DoesNotContainValueException("The literal has no data type.");
5843 protected <T extends Accessor> T getAccessor4File(Resource subject)
5844 throws DatabaseException {
5847 byte[] bytes = processor.support.getValue(g, subject);
5851 BinaryVariant va = (BinaryVariant) Accessors.getAccessor(bytes, Datatypes.VARIANT);
5852 Accessor ca = va.getContentAccessor();
5854 } catch (AccessorConstructionException e) {
5855 throw new DatabaseException(e);
5861 Binding datatype_binding = Bindings.getBindingUnchecked(DataType.class);
5862 Serializer datatype_serializer = datatype_binding.serializer();
5865 BinaryMemory in = new BinaryMemory(ByteBuffer.wrap(bytes));
5866 datatype = (DataType)datatype_serializer.deserialize(in);
5867 Binding data_binding = Bindings.getBinding(datatype);
5868 Serializer data_serializer = data_binding.serializer(BinarySerializationFormat.INSTANCE);
5869 Object o = data_serializer.deserialize(in);
5871 return (T)Accessors.getAccessor(data_binding, o);
5872 } catch(AccessorConstructionException e) {
5875 } catch (Exception e) {
5876 throw new DatabaseException(e);
5879 @SuppressWarnings("unchecked")
5881 public <T extends Accessor> T getAccessor(Resource subject) throws DatabaseException {
5882 RandomAccessBinary rab = getRandomAccessBinary(subject);
5884 return (T)Accessors.getAccessor(rab, getDataType(subject));
5885 } catch(AccessorConstructionException e) {
5886 throw new DatabaseException(e);
5889 @SuppressWarnings("unchecked")
5890 protected <T extends Accessor> T createAccessor(Resource resource, Datatype datatype, Object intialValue)
5891 throws DatabaseException {
5892 RandomAccessBinary rab = createRandomAccessBinary(resource, datatype, intialValue);
5894 return (T)Accessors.getAccessor(rab, datatype);
5895 } catch(AccessorConstructionException e) {
5896 throw new DatabaseException(e);
5900 public RandomAccessBinary getRandomAccessBinary(Resource subject) throws DatabaseException {
5901 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5902 ResourceData rd = ravs.get(subject);
5906 ExternalValueSupport evs = getService(ExternalValueSupport.class);
5907 long size = evs.getValueSize(this, subject); // Throws DatabaseException if no old external value.
5909 File platform = Platform.getLocation().toFile();
5910 File tempFiles = new File(platform, "tempFiles");
5911 File dbDir = new File(tempFiles, "db");
5913 File file = new File(dbDir, "ResourceFile" + subject.getResourceId());
5914 rd = new ResourceData(new BinaryFile(file), true); // Old external value.
5915 final int N = 1<<20;
5919 int length = N < left ? N : (int)left;
5920 byte[] bytes = evs.readValue(this, subject, offset, length);
5921 offset += bytes.length;
5922 left -= bytes.length;
5923 rd.binaryFile.write(bytes);
5925 ravs.put(subject, rd);
5927 } catch (Exception e) {
5928 throw new DatabaseException("Resource " + subject + " have value but there was problem with accessing value data.", e);
5930 } catch (Exception e) {
5931 if(Development.DEVELOPMENT) {
5932 if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
5933 e.printStackTrace();
5937 Datatype datatype = getDataType(subject);
5938 Object value = getPossibleValue(subject, Bindings.getBinding(datatype));
5939 return createRandomAccessBinary(subject, datatype, value);
5941 public RandomAccessBinary createRandomAccessBinary(Resource resource, Datatype datatype, Object initialValue)
5942 throws DatabaseException {
5943 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5945 File platform = Platform.getLocation().toFile();
5946 File tempFiles = new File(platform, "tempFiles");
5947 File dbDir = new File(tempFiles, "db");
5949 File file = new File(dbDir, "ResourceFile" + resource.getResourceId());
5950 ResourceData rd = new ResourceData(new BinaryFile(file), false);
5951 Binding binding = Bindings.getBinding(datatype);
5952 if (null == initialValue) {
5953 initialValue = binding.createDefault();
5955 Serializer serializer = binding.serializer();
5956 byte[] bytes = serializer.serialize(initialValue);
5957 // In case the file has been previously accessed and was larger we set the correct size now
5958 rd.binaryFile.setLength(bytes.length);
5959 rd.binaryFile.write(bytes);
5960 ravs.put(resource, rd);
5962 } catch (Exception e) {
5963 if (e instanceof DatabaseException)
5964 throw (DatabaseException)e;
5966 throw new DatabaseException(e);
5970 // static class ExternalValueRequest<T> extends ResourceRead<T> {
5972 // public ExternalValueRequest(Resource resource) {
5976 // @SuppressWarnings("unchecked")
5978 // public T perform(ReadGraph graph) throws DatabaseException {
5981 // String uri = graph.getURI(resource);
5982 // if(Layer0.URIs.Functions_functionApplication.equals(uri)) return (T)functionApplication;
5984 // return (T)ReflectionUtils.getValue(uri).getValue();
5986 // } catch(ValueNotFoundException e) {
5987 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
5988 // } catch(ClassCastException e) {
5989 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
5995 @SuppressWarnings("unchecked")
5997 public <T> T getValue2(Resource r, Object context) throws DatabaseException {
5998 Layer0 L0 = processor.getL0(this);
5999 Set<Resource> types = getTypes(r);
6001 if(types.contains(L0.Literal)) {
6002 if(isImmutable(r)) {
6003 return syncRequest(new ValueImplied<T>(r));
6008 else if(types.contains(L0.ExternalValue)) {
6009 return (T)syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6013 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6014 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6016 return function.apply(this, r, context);
6017 } catch(RuntimeException e) {
6018 DatabaseException dte = findPossibleRootException(e);
6019 if(dte != null) throw dte;
6020 else throw new DatabaseException(e);
6027 public Variant getVariantValue2(Resource r, Object context) throws DatabaseException {
6028 Layer0 L0 = processor.getL0(this);
6029 Set<Resource> types = getTypes(r);
6031 if(types.contains(L0.Literal)) {
6032 if(isImmutable(r)) {
6033 return syncRequest(new VariantValueImplied(r));
6035 return getVariantValue(r);
6038 else if(types.contains(L0.ExternalValue)) {
6039 Object value = syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6041 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6042 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6043 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6048 Function3<ReadGraph,Resource,Object,Object> function = requestValueFunction(r);
6049 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6051 Object value = function.apply(this, r, context);
6053 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6054 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6055 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6057 } catch(RuntimeException e) {
6058 DatabaseException dte = findPossibleRootException(e);
6059 if(dte != null) throw dte;
6060 else throw new DatabaseException(e);
6066 public <T> T getPossibleValue2(Resource subject, Object context) throws DatabaseException {
6068 return getValue2(subject, context);
6069 } catch (DatabaseException e) {
6074 static class PossibleConverterFunction<T> extends ResourceRead<Function3<ReadGraph,Resource,Object,T>> {
6076 public PossibleConverterFunction(Resource resource) {
6081 public Function3<ReadGraph,Resource,Object,T> perform(ReadGraph graph) throws DatabaseException {
6082 return compute(graph, resource);
6085 @SuppressWarnings("unchecked")
6086 public static <T> Function3<ReadGraph,Resource,Object,T> compute(ReadGraph graph, Resource resource) throws DatabaseException {
6087 Layer0 L0 = Layer0.getInstance(graph);
6088 for(Resource converter : graph.getObjects(resource, L0.ConvertsToValueWith)) {
6090 if(L0.Functions_functionApplication.equals(converter)) {
6091 return (Function3<ReadGraph,Resource,Object,T>)graph.syncRequest(new AdaptValue(resource));
6093 return graph.getValue2(converter, resource);
6095 } catch(RuntimeException e) {
6096 DatabaseException dte = findPossibleRootException(e);
6097 if(dte != null) throw dte;
6098 else throw new DatabaseException(e);
6106 <T> Function3<ReadGraph,Resource,Object,T> requestValueFunction(Resource r) throws DatabaseException {
6108 return syncRequest(new PossibleConverterFunction<T>(r), TransientCacheAsyncListener.<Function3<ReadGraph,Resource,Object,T>>instance());
6110 return syncRequest(new PossibleConverterFunction<T>(r));
6114 * Get a value associated with a graph {@link Resource}, using a possible context object and
6115 * a desired value binding. The following methods are tried in order to retreive the value:
6117 * <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>
6118 * <li>If the resource is a {@code L0.ExternalValue}, the value is acquired using
6119 * {@link ReflectionUtils#getValue(String)}.</li>
6120 * <li>If the resource is associated with a suitable value converter with relation {@code L0.ConvertsToValueWith}
6121 * (see {@link #requestValueFunction(Resource)}), the value function is called with the graph, the resource
6122 * and the context object.</li>
6125 * @param r A graph resource with which the value is associated
6126 * @param context A context object that is used for acquiring the value (only applied in case 3)
6127 * @param binding A binding for the value type (only applied in case 1)
6128 * @return The value of the graph node.
6129 * @throws DoesNotContainValueException No value is associated with the graph node.
6130 * @throws DatabaseException Other errors, such as an error in casting the value to the return type or
6131 * a runtime error in the value function.
6133 @SuppressWarnings("unchecked")
6135 public <T> T getValue2(Resource r, Object context, Binding binding) throws DatabaseException {
6136 if (binding instanceof ObjectVariantBinding)
6137 return getValue2(r, context);
6139 Layer0 L0 = processor.getL0(this);
6140 Set<Resource> types = getTypes(r);
6141 if(types.contains(L0.Literal)) {
6142 if(isImmutable(r)) {
6143 return syncRequest(new Value<T>(r, binding));
6145 return getValue(r, binding);
6147 } else if(types.contains(L0.ExternalValue)) {
6148 ComputationalValue cv = syncRequest(new PossibleAdapter<ComputationalValue>(r, ComputationalValue.class), TransientCacheAsyncListener.instance());
6150 return cv.getValue(this, r);
6152 // This should not even be possible since L0 defines an adapter for all values
6154 return (T)ReflectionUtils.getValue(getURI(r)).getValue();
6155 } catch(ValueNotFoundException e) {
6156 throw new DatabaseException(e);
6157 } catch(ClassCastException e) {
6158 throw new DatabaseException(e);
6163 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6164 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function.");
6166 Object value = function.apply(this, r, context);
6167 if(binding.isInstance(value)) return (T)value;
6168 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
6169 return (T)Bindings.adapt(value, srcBinding, binding);
6170 } catch(RuntimeException e) {
6171 DatabaseException dte = findPossibleRootException(e);
6172 if(dte != null) throw dte;
6173 else throw new DatabaseException(e);
6174 } catch (AdaptException e) {
6175 throw new DatabaseException(e);
6176 } catch (org.simantics.databoard.binding.error.BindingException e) {
6177 throw new DatabaseException(e);
6183 public <T> T getPossibleValue2(Resource subject, Object context, Binding binding) throws DatabaseException {
6185 return getValue2(subject, context, binding);
6186 } catch (DatabaseException e) {
6191 private static DatabaseException findPossibleRootException(Throwable t) {
6192 if(t == null) return null;
6193 if(t instanceof DatabaseException) return (DatabaseException)t;
6194 if(t instanceof RuntimeException || t instanceof InvocationTargetException) {
6195 return findPossibleRootException(t.getCause());
6201 public <T> T getRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6202 return getRelatedValue2(subject, relation, subject);
6206 public Variant getRelatedVariantValue2(Resource subject, Resource relation) throws DatabaseException {
6207 return getRelatedVariantValue2(subject, relation, subject);
6211 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6213 Resource object = getPossibleObject(subject, relation);
6214 if(object == null) return null;
6215 else return getValue2(object, subject);
6216 } catch (DatabaseException e) {
6222 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6223 if(Development.DEVELOPMENT) {
6224 if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
6225 String error = L0Validations.checkValueType(this, subject, relation);
6227 Logger.defaultLogError(new ValidationException(error));
6228 throw new ValidationException(error);
6232 return getValue2(getSingleObject(subject, relation), context);
6236 public Variant getRelatedVariantValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6237 if(Development.DEVELOPMENT) {
6238 if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
6239 String error = L0Validations.checkValueType(this, subject, relation);
6241 Logger.defaultLogError(new ValidationException(error));
6242 throw new ValidationException(error);
6246 return getVariantValue2(getSingleObject(subject, relation), context);
6250 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6252 Resource object = getPossibleObject(subject, relation);
6253 if(object == null) return null;
6254 else return getValue2(object, context);
6255 } catch (DatabaseException e) {
6261 public <T> T getRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6262 return getRelatedValue2(subject, relation, subject, binding);
6266 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6268 Resource object = getPossibleObject(subject, relation);
6269 if(object == null) return null;
6270 return getValue2(object, subject, binding);
6271 } catch (DatabaseException e) {
6277 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6278 return getValue2(getSingleObject(subject, relation), context, binding);
6282 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6284 Resource object = getPossibleObject(subject, relation);
6285 if(object == null) return null;
6286 else return getValue2(object, context, binding);
6287 } catch (DatabaseException e) {
6293 public Type getRelatedValueType(Resource subject, Resource relation) throws DatabaseException {
6294 Layer0 L0 = processor.getL0(this);
6295 Resource property = getSingleObject(subject, relation);
6296 String typeText = (String)getRelatedValue(property, L0.HasValueType, Bindings.STRING);
6298 return org.simantics.scl.compiler.types.Types.parseType(typeText);
6299 } catch (SCLTypeParseException e) {
6300 throw new DatabaseException(e);
6304 private static ThreadLocal<Boolean> syncGraph = new ThreadLocal<Boolean>() {
6305 protected Boolean initialValue() {
6311 public boolean setSynchronous(boolean value) {
6312 boolean old = getSynchronous();
6313 syncGraph.set(value);
6318 public boolean getSynchronous() {
6319 return syncGraph.get();
6322 public void ensureLoaded(int resource) {
6323 processor.querySupport.ensureLoaded(this, resource);
6326 public void ensureLoaded(int resource, int predicate) {
6327 processor.querySupport.ensureLoaded(this, resource, predicate);
6330 public byte[] getValue(int resource) {
6331 return processor.querySupport.getValue(this, resource);
6334 public int thread(int resource) {
6335 return (resource >>> 16) & processor.THREAD_MASK;
6338 public int thread(Resource resource) {
6339 return (((ResourceImpl)resource).id >>> 16) & processor.THREAD_MASK;
6342 public ResourceSupport getResourceSupport() {
6343 return processor.getResourceSupport();
6347 public Object getModificationCounter() {
6348 return processor.getSession().getModificationCounter();
6352 public boolean performPending() {
6353 return processor.performPending(this);
6356 public Set<ReadGraphImpl> ancestorSet() {
6357 HashSet<ReadGraphImpl> result = new HashSet<>();
6358 ReadGraphImpl g = this;
6366 public int getLevel() {
6367 return getLevelStatic(this);
6370 private static int getLevelStatic(ReadGraphImpl impl) {
6371 if(impl == null) return 0;
6372 else return 1 + getLevelStatic(impl.parentGraph);
6375 public boolean isParent(ReadGraphImpl impl) {
6376 if(impl == null) return false;
6377 if(this == impl) return true;
6378 return isParent(impl.parentGraph);
6381 public ReadGraphImpl getTopLevelGraph() {
6382 return getTopLevelGraphStatic(this);
6385 private static ReadGraphImpl getTopLevelGraphStatic(ReadGraphImpl impl) {
6386 if(impl.parentGraph == null) return impl;
6387 else return getTopLevelGraphStatic(impl.parentGraph);
6390 @SuppressWarnings("unchecked")
6393 return (T) processor.getL0();