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 {
1944 assert (request != null);
1946 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
1948 return QueryCache.resultReadEntry(this, request, parent, listener, procedure);
1953 public <T> T syncRequest(final Read<T> request,
1954 final SyncProcedure<T> procedure) throws DatabaseException {
1955 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
1959 public <T> T syncRequest(Read<T> request, Procedure<T> procedure)
1960 throws DatabaseException {
1961 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
1964 static class AsyncReadProcedure<T> implements AsyncProcedure<T> {
1966 private static Throwable DONE = new Throwable();
1969 Throwable exception = null;
1972 public void execute(AsyncReadGraph graph, T t) {
1978 public void exception(AsyncReadGraph graph, Throwable t) {
1982 public void checkAndThrow() throws DatabaseException {
1983 if(exception != DONE) {
1984 if (exception instanceof DatabaseException)
1985 throw (DatabaseException) exception;
1987 throw new DatabaseException(
1988 "Unexpected exception in ReadGraph.syncRequest(AsyncRead)",
1993 public boolean done() {
1994 return exception != null;
2000 public <T> T syncRequest(final AsyncRead<T> request)
2001 throws DatabaseException {
2003 assert (request != null);
2004 return syncRequest(request, new AsyncProcedureAdapter<>() );
2009 public <T> T syncRequest(AsyncRead<T> request, AsyncListener<T> procedure)
2010 throws DatabaseException {
2011 return syncRequest(request, (AsyncProcedure<T>) procedure);
2015 public <T> T syncRequest(AsyncRead<T> request, SyncListener<T> procedure)
2016 throws DatabaseException {
2017 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
2021 public <T> T syncRequest(AsyncRead<T> request, Listener<T> procedure)
2022 throws DatabaseException {
2023 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
2027 final public <T> T syncRequest(final AsyncRead<T> request,
2028 final AsyncProcedure<T> procedure) throws DatabaseException {
2030 assert (request != null);
2032 ListenerBase listener = getListenerBase(procedure);
2034 // BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, procedure, request);
2035 return (T)QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure, true);
2041 public <T> T syncRequest(AsyncRead<T> request,
2042 final SyncProcedure<T> procedure) throws DatabaseException {
2043 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2047 final public <T> T syncRequest(final AsyncRead<T> request,
2048 final Procedure<T> procedure) throws DatabaseException {
2049 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2053 public <T> Collection<T> syncRequest(final MultiRead<T> request)
2054 throws DatabaseException {
2056 assert (request != null);
2058 final ArrayList<T> result = new ArrayList<T>();
2059 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2061 syncRequest(request, new SyncMultiProcedure<T>() {
2064 public void execute(ReadGraph graph, T t) {
2065 synchronized (result) {
2071 public void finished(ReadGraph graph) {
2075 public void exception(ReadGraph graph, Throwable t) {
2080 public String toString() {
2081 return "syncRequest(MultiRead) -> " + request;
2086 Throwable t = exception.get();
2088 if (t instanceof DatabaseException)
2089 throw (DatabaseException) t;
2091 throw new DatabaseException(
2092 "Unexpected exception in ReadGraph.syncRequest(Read)",
2101 public <T> Collection<T> syncRequest(MultiRead<T> request,
2102 SyncMultiListener<T> procedure) {
2103 return syncRequest(request, (SyncMultiProcedure<T>)procedure);
2107 public <T> Collection<T> syncRequest(MultiRead<T> request,
2108 MultiListener<T> procedure) {
2109 return syncRequest(request, new NoneToSyncMultiListener<T>(procedure));
2113 public <T> Collection<T> syncRequest(MultiRead<T> request,
2114 SyncMultiProcedure<T> procedure) {
2116 assert (request != null);
2118 ListenerBase listener = getListenerBase(procedure);
2120 final ResultCallWrappedSyncQueryProcedure<T> wrapper = new ResultCallWrappedSyncQueryProcedure<T>(procedure);
2122 if (parent != null || listener != null) {
2124 // Object syncParent = request;
2126 // final ReadGraphImpl newGraph = newSync();
2128 processor.query(this, request, parent, wrapper, listener);
2130 // newGraph.waitAsync(syncParent);
2134 // Object syncParent = request;
2136 // final ReadGraphImpl newGraph = newSync();
2139 request.perform(this, wrapper);
2140 } catch (Throwable t) {
2141 wrapper.exception(this, t);
2146 return wrapper.get();
2151 public <T> Collection<T> syncRequest(MultiRead<T> request,
2152 MultiProcedure<T> procedure) {
2153 return syncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
2156 static class AsyncMultiReadProcedure<T> extends ArrayList<T> implements AsyncMultiProcedure<T> {
2158 private static Throwable DONE = new Throwable();
2160 private static final long serialVersionUID = -6494230465108115812L;
2162 Throwable exception = null;
2165 public synchronized void execute(AsyncReadGraph graph, T t) {
2170 public void finished(AsyncReadGraph graph) {
2175 public void exception(AsyncReadGraph graph, Throwable t) {
2179 public void checkAndThrow() throws DatabaseException {
2180 if(exception != DONE) {
2181 if (exception instanceof DatabaseException)
2182 throw (DatabaseException) exception;
2184 throw new DatabaseException(
2185 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
2190 public boolean done() {
2191 return exception != null;
2197 final public <T> Collection<T> syncRequest(AsyncMultiRead<T> request)
2198 throws DatabaseException {
2200 assert (request != null);
2202 final AsyncMultiReadProcedure<T> procedure = new AsyncMultiReadProcedure<T>();
2204 syncRequest(request, procedure);
2206 procedure.checkAndThrow();
2212 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2213 AsyncMultiListener<T> procedure) {
2214 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2218 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2219 SyncMultiListener<T> procedure) {
2220 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2224 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2225 MultiListener<T> procedure) {
2226 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2229 final private <T> void syncRequest(final AsyncMultiRead<T> request,
2230 final AsyncMultiReadProcedure<T> procedure) {
2232 assert (request != null);
2233 assert (procedure != null);
2235 ListenerBase listener = getListenerBase(procedure);
2237 if (parent != null || listener != null) {
2239 // Object syncParent = request;
2241 // final ReadGraphImpl newGraph = newSync();
2243 processor.query(this, request, parent, procedure, listener);
2245 // newGraph.waitAsync(syncParent);
2246 waitAsyncProcedure(procedure);
2250 // Object syncParent = callerThread == Integer.MIN_VALUE ? null
2253 // final ReadGraphImpl newGraph = newSyncAsync(syncParent);
2258 // ReadGraphImpl sync = newSync();
2259 request.perform(this, procedure);
2260 // sync.waitAsync(null);
2261 waitAsyncProcedure(procedure);
2264 } catch (Throwable t) {
2266 waitAsyncProcedure(procedure);
2277 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2278 final AsyncMultiProcedure<T> procedure) {
2280 assert (request != null);
2281 assert (procedure != null);
2283 ListenerBase listener = getListenerBase(procedure);
2285 if (parent != null || listener != null) {
2287 // Object syncParent = request;
2289 // final ReadGraphImpl newGraph = newSync();
2291 processor.query(this, request, parent, procedure, listener);
2293 // newGraph.waitAsync(syncParent);
2297 // Object syncParent = request;
2299 // final ReadGraphImpl newGraph = newSync();
2303 request.perform(this, new AsyncMultiProcedure<T>() {
2306 public void execute(AsyncReadGraph graph, T result) {
2307 procedure.execute(graph, result);
2311 public void finished(AsyncReadGraph graph) {
2312 procedure.finished(graph);
2316 public void exception(AsyncReadGraph graph, Throwable t) {
2317 procedure.exception(graph, t);
2321 public String toString() {
2322 return "syncRequest(AsyncMultiRead) -> " + procedure;
2327 } catch (Throwable t) {
2339 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2340 final SyncMultiProcedure<T> procedure) {
2341 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2345 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2346 final MultiProcedure<T> procedure) {
2347 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2351 public <T> T syncRequest(final ExternalRead<T> request)
2352 throws DatabaseException {
2354 assert (request != null);
2356 return syncRequest(request, new Procedure<T>() {
2359 public void execute(T t) {
2363 public void exception(Throwable t) {
2367 public String toString() {
2368 return "syncRequest(AsyncRead) -> " + request;
2376 public <T> T syncRequest(ExternalRead<T> request, Listener<T> procedure) throws DatabaseException {
2377 return syncRequest(request, (Procedure<T>) procedure);
2381 final public <T> T syncRequest(final ExternalRead<T> request,
2382 final Procedure<T> procedure) throws DatabaseException {
2384 assert (request != null);
2386 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
2387 return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
2392 public void syncRequest(final Write request) throws DatabaseException {
2394 assert (request != null);
2396 throw new DatabaseException(
2397 "Write operations are not supported during read transactions!");
2402 public <T> T syncRequest(final WriteResult<T> request) throws DatabaseException {
2404 assert (request != null);
2406 throw new DatabaseException(
2407 "Write operations are not supported during read transactions!");
2412 public void syncRequest(final DelayedWrite request)
2413 throws DatabaseException {
2415 assert (request != null);
2417 throw new DatabaseException(
2418 "Write operations are not supported during read transactions!");
2423 public <T> T syncRequest(final DelayedWriteResult<T> request) throws DatabaseException {
2425 assert (request != null);
2427 throw new DatabaseException(
2428 "Write operations are not supported during read transactions!");
2433 public void syncRequest(final WriteOnly request) throws DatabaseException {
2435 assert (request != null);
2437 throw new DatabaseException(
2438 "Write operations are not supported during read transactions!");
2443 public <T> T syncRequest(final WriteOnlyResult<T> request) throws DatabaseException {
2445 assert (request != null);
2447 throw new DatabaseException(
2448 "Write operations are not supported during read transactions!");
2453 public <T> void async(ReadInterface<T> r, AsyncProcedure<T> procedure) {
2454 r.request(this, procedure);
2458 public <T> void async(ReadInterface<T> r, Procedure<T> procedure) {
2459 r.request(this, procedure);
2463 public <T> void async(ReadInterface<T> r, SyncProcedure<T> procedure) {
2464 r.request(this, procedure);
2468 public <T> void async(ReadInterface<T> r, AsyncListener<T> procedure) {
2469 r.request(this, procedure);
2473 public <T> void async(ReadInterface<T> r, Listener<T> procedure) {
2474 r.request(this, procedure);
2478 public <T> void async(ReadInterface<T> r, SyncListener<T> procedure) {
2479 r.request(this, procedure);
2483 public <T> T sync(ReadInterface<T> r) throws DatabaseException {
2484 return r.request(this);
2488 public <T> T sync(WriteInterface<T> r) throws DatabaseException {
2489 return r.request(this);
2493 public <T> void async(WriteInterface<T> r, Procedure<T> procedure) {
2494 r.request(this, procedure);
2498 public <T> void async(WriteInterface<T> r) {
2499 r.request(this, new ProcedureAdapter<T>());
2503 * Implementation of the interface AsyncReadGraph
2507 public void forURI(Resource resource, AsyncListener<String> listener) {
2508 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2513 public void forURI(Resource resource, SyncListener<String> listener) {
2514 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2519 public void forURI(Resource resource, Listener<String> listener) {
2520 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2525 final public void forURI(final Resource resource,
2526 final AsyncProcedure<String> procedure) {
2528 assert (resource != null);
2529 assert (procedure != null);
2531 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2537 public void forURI(Resource resource, SyncProcedure<String> procedure) {
2538 forURI(resource, new SyncToAsyncProcedure<String>(procedure));
2542 public void forURI(Resource resource, Procedure<String> procedure) {
2543 forURI(resource, new NoneToAsyncProcedure<String>(procedure));
2547 public void forResource(String id, AsyncListener<Resource> listener) {
2548 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2553 public void forResource(String id, SyncListener<Resource> listener) {
2554 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2559 public void forResource(String id, Listener<Resource> listener) {
2560 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2565 final public void forResource(final String id,
2566 final AsyncProcedure<Resource> procedure) {
2568 assert (id != null);
2569 assert (procedure != null);
2571 processor.forResource(this, id, procedure);
2576 public void forResource(String id, SyncProcedure<Resource> procedure) {
2577 forResource(id, new SyncToAsyncProcedure<Resource>(procedure));
2581 public void forResource(String id, Procedure<Resource> procedure) {
2582 forResource(id, new NoneToAsyncProcedure<Resource>(procedure));
2586 public void forBuiltin(String id, AsyncListener<Resource> listener) {
2587 asyncRequest(new Builtin(id), listener);
2591 public void forBuiltin(String id, SyncListener<Resource> listener) {
2592 asyncRequest(new Builtin(id), listener);
2596 public void forBuiltin(String id, Listener<Resource> listener) {
2597 asyncRequest(new Builtin(id), listener);
2601 final public void forBuiltin(final String id,
2602 final AsyncProcedure<Resource> procedure) {
2604 assert (id != null);
2605 assert (procedure != null);
2607 processor.forBuiltin(this, id, procedure);
2612 public void forBuiltin(String id, SyncProcedure<Resource> procedure) {
2613 forBuiltin(id, new SyncToAsyncProcedure<Resource>(procedure));
2617 public void forBuiltin(String id, Procedure<Resource> procedure) {
2618 forBuiltin(id, new NoneToAsyncProcedure<Resource>(procedure));
2622 final public void forEachStatement(Resource subject, Resource relation,
2623 AsyncMultiProcedure<Statement> procedure) {
2625 assert (subject != null);
2626 assert (relation != null);
2627 assert (procedure != null);
2629 processor.forEachStatement(this, subject, relation, procedure);
2634 public void forEachStatement(Resource subject, Resource relation,
2635 SyncMultiProcedure<Statement> procedure) {
2636 forEachStatement(subject, relation,
2637 new SyncToAsyncMultiProcedure<Statement>(procedure));
2641 final public void forEachStatement(Resource subject, Resource relation,
2642 MultiProcedure<Statement> procedure) {
2644 assert (subject != null);
2645 assert (relation != null);
2646 assert (procedure != null);
2648 processor.forEachStatement(this, subject, relation, procedure);
2653 final public void forStatementSet(Resource subject, Resource relation,
2654 AsyncSetListener<Statement> procedure) {
2656 assert (subject != null);
2657 assert (relation != null);
2658 assert (procedure != null);
2660 processor.forStatementSet(this, subject, relation, procedure);
2665 final public void forStatementSet(Resource subject, Resource relation,
2666 SyncSetListener<Statement> procedure) {
2667 forStatementSet(subject, relation,
2668 new SyncToAsyncSetProcedure<Statement>(procedure));
2672 public void forStatementSet(Resource subject, Resource relation,
2673 SetListener<Statement> listener) {
2674 forStatementSet(subject, relation,
2675 new NoneToAsyncSetProcedure<Statement>(listener));
2679 final public void forEachAssertedStatement(final Resource subject,
2680 final Resource relation,
2681 final AsyncMultiProcedure<Statement> procedure) {
2683 assert (subject != null);
2684 assert (relation != null);
2685 assert (procedure != null);
2687 processor.forEachAssertedStatement(this, subject, relation, procedure);
2692 public void forEachAssertedStatement(Resource subject, Resource relation,
2693 SyncMultiProcedure<Statement> procedure) {
2694 forEachAssertedStatement(subject, relation,
2695 new SyncToAsyncMultiProcedure<Statement>(procedure));
2699 public void forEachAssertedStatement(Resource subject, Resource relation,
2700 MultiProcedure<Statement> procedure) {
2701 forEachAssertedStatement(subject, relation,
2702 new NoneToAsyncMultiProcedure<Statement>(procedure));
2706 public void forAssertedStatementSet(Resource subject, Resource relation,
2707 AsyncSetListener<Statement> procedure) {
2709 assert (subject != null);
2710 assert (relation != null);
2711 assert (procedure != null);
2713 processor.forAssertedStatementSet(this, subject, relation, procedure);
2718 public void forAssertedStatementSet(Resource subject, Resource relation,
2719 SyncSetListener<Statement> procedure) {
2721 assert (subject != null);
2722 assert (relation != null);
2723 assert (procedure != null);
2725 forAssertedStatementSet(subject, relation,
2726 new SyncToAsyncSetProcedure<Statement>(procedure));
2731 public void forAssertedStatementSet(Resource subject, Resource relation,
2732 SetListener<Statement> procedure) {
2734 assert (subject != null);
2735 assert (relation != null);
2736 assert (procedure != null);
2738 forAssertedStatementSet(subject, relation,
2739 new NoneToAsyncSetProcedure<Statement>(procedure));
2744 final public void forEachPredicate(final Resource subject,
2745 final AsyncMultiProcedure<Resource> procedure) {
2747 assert (subject != null);
2748 assert (procedure != null);
2750 processor.forEachPredicate(this, subject, procedure);
2755 public void forEachPredicate(Resource subject,
2756 SyncMultiProcedure<Resource> procedure) {
2757 forEachPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(
2762 final public void forEachPredicate(final Resource subject,
2763 final MultiProcedure<Resource> procedure) {
2765 assert (subject != null);
2766 assert (procedure != null);
2768 processor.forEachPredicate(this, subject, procedure);
2773 final public void forPredicateSet(final Resource subject,
2774 final AsyncSetListener<Resource> procedure) {
2776 assert (subject != null);
2777 assert (procedure != null);
2779 processor.forPredicateSet(this, subject, procedure);
2784 final public void forPredicateSet(final Resource subject,
2785 final SyncSetListener<Resource> procedure) {
2787 assert (subject != null);
2788 assert (procedure != null);
2790 forPredicateSet(subject, new SyncToAsyncSetProcedure<Resource>(
2796 final public void forPredicateSet(final Resource subject,
2797 final SetListener<Resource> procedure) {
2799 assert (subject != null);
2800 assert (procedure != null);
2802 forPredicateSet(subject, new NoneToAsyncSetProcedure<Resource>(
2808 final public void forEachPrincipalType(final Resource subject,
2809 final AsyncMultiProcedure<Resource> procedure) {
2811 assert (subject != null);
2812 assert (procedure != null);
2814 processor.forEachPrincipalType(this, subject, procedure);
2819 public void forEachPrincipalType(Resource subject,
2820 SyncMultiProcedure<Resource> procedure) {
2821 forEachPrincipalType(subject, new SyncToAsyncMultiProcedure<Resource>(
2826 final public void forEachPrincipalType(final Resource subject,
2827 final MultiProcedure<Resource> procedure) {
2829 assert (subject != null);
2830 assert (procedure != null);
2832 processor.forEachPrincipalType(this, subject, procedure);
2837 final public void forPrincipalTypeSet(final Resource subject,
2838 final AsyncSetListener<Resource> procedure) {
2840 assert (subject != null);
2841 assert (procedure != null);
2843 processor.forPrincipalTypeSet(this, subject, procedure);
2848 final public void forPrincipalTypeSet(final Resource subject,
2849 final SyncSetListener<Resource> procedure) {
2851 assert (subject != null);
2852 assert (procedure != null);
2854 forPrincipalTypeSet(subject, new SyncToAsyncSetProcedure<Resource>(
2860 final public void forPrincipalTypeSet(final Resource subject,
2861 final SetListener<Resource> procedure) {
2863 assert (subject != null);
2864 assert (procedure != null);
2866 forPrincipalTypeSet(subject, new NoneToAsyncSetProcedure<Resource>(
2872 public void forTypes(Resource subject, AsyncListener<Set<Resource>> listener) {
2873 asyncRequest(new Types(subject), listener);
2877 public void forTypes(Resource subject, SyncListener<Set<Resource>> listener) {
2878 asyncRequest(new Types(subject), listener);
2882 public void forTypes(Resource subject, Listener<Set<Resource>> listener) {
2883 asyncRequest(new Types(subject), listener);
2887 final public void forTypes(final Resource subject,
2888 final AsyncProcedure<Set<Resource>> procedure) {
2890 assert (subject != null);
2891 assert (procedure != null);
2893 processor.forTypes(this, subject, procedure);
2898 public void forTypes(Resource subject,
2899 SyncProcedure<Set<Resource>> procedure) {
2900 forTypes(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
2904 public void forTypes(Resource subject, Procedure<Set<Resource>> procedure) {
2905 forTypes(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
2909 public void forSupertypes(Resource subject,
2910 AsyncListener<Set<Resource>> listener) {
2911 asyncRequest(new Types(subject), listener);
2915 public void forSupertypes(Resource subject,
2916 SyncListener<Set<Resource>> listener) {
2917 asyncRequest(new Types(subject), listener);
2921 public void forSupertypes(Resource subject, Listener<Set<Resource>> listener) {
2922 asyncRequest(new Types(subject), listener);
2926 final public void forSupertypes(final Resource subject,
2927 final AsyncProcedure<Set<Resource>> procedure) {
2929 assert (subject != null);
2930 assert (procedure != null);
2932 processor.forSupertypes(this, subject, procedure);
2937 public void forSupertypes(Resource subject,
2938 SyncProcedure<Set<Resource>> procedure) {
2939 forSupertypes(subject, new SyncToAsyncProcedure<Set<Resource>>(
2944 public void forSupertypes(Resource subject,
2945 Procedure<Set<Resource>> procedure) {
2946 forSupertypes(subject, new NoneToAsyncProcedure<Set<Resource>>(
2951 public void forDirectSuperrelations(Resource subject,
2952 AsyncMultiProcedure<Resource> procedure) {
2954 assert (subject != null);
2955 assert (procedure != null);
2957 processor.forDirectSuperrelations(this, subject, procedure);
2962 public void forPossibleSuperrelation(Resource subject, AsyncProcedure<Resource> procedure) {
2964 assert (subject != null);
2965 assert (procedure != null);
2967 processor.forPossibleSuperrelation(this, subject, procedure);
2972 public void forSuperrelations(Resource subject,
2973 AsyncListener<Set<Resource>> listener) {
2974 asyncRequest(new Types(subject), listener);
2978 public void forSuperrelations(Resource subject,
2979 SyncListener<Set<Resource>> listener) {
2980 asyncRequest(new Types(subject), listener);
2984 public void forSuperrelations(Resource subject,
2985 Listener<Set<Resource>> listener) {
2986 asyncRequest(new Types(subject), listener);
2990 final public void forSuperrelations(final Resource subject,
2991 final AsyncProcedure<Set<Resource>> procedure) {
2993 assert (subject != null);
2994 assert (procedure != null);
2996 processor.forSuperrelations(this, subject, procedure);
3001 public void forSuperrelations(Resource subject,
3002 SyncProcedure<Set<Resource>> procedure) {
3003 forSuperrelations(subject, new SyncToAsyncProcedure<Set<Resource>>(
3008 public void forSuperrelations(Resource subject,
3009 Procedure<Set<Resource>> procedure) {
3010 forSuperrelations(subject, new NoneToAsyncProcedure<Set<Resource>>(
3015 final public void forEachObject(final Resource subject, final Resource relation, final AsyncMultiProcedure<Resource> procedure) {
3016 processor.forEachObject(this, subject, relation, procedure);
3020 public void forEachObject(Resource subject, Resource relation,
3021 SyncMultiProcedure<Resource> procedure) {
3022 forEachObject(subject, relation,
3023 new SyncToAsyncMultiProcedure<Resource>(procedure));
3027 public void forEachObject(Resource subject, Resource relation,
3028 MultiProcedure<Resource> procedure) {
3030 processor.forEachObject(this, subject, relation, procedure);
3035 final public void forEachDirectPredicate(final Resource subject, final AsyncProcedure<Set<Resource>> procedure) {
3036 processor.forEachDirectPredicate(this, subject, procedure);
3040 final public void forEachDirectPredicate(final Resource subject, final SyncProcedure<Set<Resource>> procedure) {
3041 forEachDirectPredicate(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
3045 public void forEachDirectPredicate(Resource subject, Procedure<Set<Resource>> procedure) {
3046 forEachDirectPredicate(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
3050 final public void forObjectSet(final Resource subject,
3051 final Resource relation, final AsyncSetListener<Resource> procedure) {
3053 assert (subject != null);
3054 assert (relation != null);
3055 assert (procedure != null);
3057 processor.forObjectSet(this, subject, relation, procedure);
3062 final public void forObjectSet(final Resource subject,
3063 final Resource relation, final SyncSetListener<Resource> procedure) {
3065 assert (subject != null);
3066 assert (relation != null);
3067 assert (procedure != null);
3069 forObjectSet(subject, relation, new SyncToAsyncSetProcedure<Resource>(
3075 final public void forObjectSet(final Resource subject,
3076 final Resource relation, final SetListener<Resource> procedure) {
3078 assert (subject != null);
3079 assert (relation != null);
3080 assert (procedure != null);
3082 forObjectSet(subject, relation, new NoneToAsyncSetProcedure<Resource>(
3088 final public void forEachAssertedObject(final Resource subject,
3089 final Resource relation,
3090 final AsyncMultiProcedure<Resource> procedure) {
3092 assert (subject != null);
3093 assert (relation != null);
3094 assert (procedure != null);
3096 processor.forEachAssertedObject(this, subject, relation, procedure);
3101 public void forEachAssertedObject(Resource subject, Resource relation,
3102 SyncMultiProcedure<Resource> procedure) {
3104 assert (subject != null);
3105 assert (relation != null);
3106 assert (procedure != null);
3108 forEachAssertedObject(subject, relation,
3109 new SyncToAsyncMultiProcedure<Resource>(procedure));
3114 public void forEachAssertedObject(Resource subject, Resource relation,
3115 MultiProcedure<Resource> procedure) {
3117 assert (subject != null);
3118 assert (relation != null);
3119 assert (procedure != null);
3121 forEachAssertedObject(subject, relation,
3122 new NoneToAsyncMultiProcedure<Resource>(procedure));
3127 public void forAssertedObjectSet(Resource subject, Resource relation,
3128 AsyncSetListener<Resource> procedure) {
3130 assert (subject != null);
3131 assert (relation != null);
3132 assert (procedure != null);
3134 processor.forAssertedObjectSet(this, subject, relation, procedure);
3139 public void forAssertedObjectSet(Resource subject, Resource relation,
3140 SyncSetListener<Resource> procedure) {
3142 assert (subject != null);
3143 assert (relation != null);
3144 assert (procedure != null);
3146 forAssertedObjectSet(subject, relation,
3147 new SyncToAsyncSetProcedure<Resource>(procedure));
3152 public void forAssertedObjectSet(Resource subject, Resource relation,
3153 SetListener<Resource> procedure) {
3155 assert (subject != null);
3156 assert (relation != null);
3157 assert (procedure != null);
3159 forAssertedObjectSet(subject, relation,
3160 new NoneToAsyncSetProcedure<Resource>(procedure));
3165 public void forInverse(Resource relation, AsyncListener<Resource> listener) {
3166 asyncRequest(new Inverse(relation), listener);
3170 public void forInverse(Resource relation, SyncListener<Resource> listener) {
3171 asyncRequest(new Inverse(relation), listener);
3175 public void forInverse(Resource relation, Listener<Resource> listener) {
3176 asyncRequest(new Inverse(relation), listener);
3180 final public void forInverse(final Resource relation,
3181 final AsyncProcedure<Resource> procedure) {
3183 assert (relation != null);
3184 assert (procedure != null);
3186 processor.forInverse(this, relation, new AsyncProcedure<Resource>() {
3189 public void execute(AsyncReadGraph graph, Resource result) {
3191 procedure.execute(graph, result);
3193 procedure.exception(graph, new NoInverseException(relation
3199 public void exception(AsyncReadGraph graph, Throwable throwable) {
3200 procedure.exception(graph, throwable);
3204 public String toString() {
3205 return "forInverse -> " + procedure;
3213 public void forInverse(Resource relation, SyncProcedure<Resource> procedure) {
3214 forInverse(relation, new SyncToAsyncProcedure<Resource>(procedure));
3218 public void forInverse(Resource relation, Procedure<Resource> procedure) {
3219 forInverse(relation, new NoneToAsyncProcedure<Resource>(procedure));
3223 public void forSingleObject(Resource subject, Resource relation,
3224 AsyncListener<Resource> listener) {
3225 asyncRequest(new SingleObject(subject, relation), listener);
3229 public void forSingleObject(Resource subject, Resource relation,
3230 SyncListener<Resource> listener) {
3231 asyncRequest(new SingleObject(subject, relation), listener);
3235 public void forSingleObject(Resource subject, Resource relation,
3236 Listener<Resource> listener) {
3237 asyncRequest(new SingleObject(subject, relation), listener);
3241 final public void forSingleObject(final Resource subject,
3242 final Resource relation, final AsyncProcedure<Resource> procedure) {
3244 assert (subject != null);
3245 assert (relation != null);
3246 assert (procedure != null);
3248 processor.forEachObject(this, subject, relation,
3249 new SingleOrErrorProcedure<Resource>(procedure));
3254 public void forSingleObject(Resource subject, Resource relation,
3255 SyncProcedure<Resource> procedure) {
3256 forSingleObject(subject, relation, new SyncToAsyncProcedure<Resource>(
3261 public void forSingleObject(Resource subject, Resource relation,
3262 Procedure<Resource> procedure) {
3263 forSingleObject(subject, relation, new NoneToAsyncProcedure<Resource>(
3268 public void forSingleStatement(Resource subject, Resource relation,
3269 AsyncListener<Statement> listener) {
3270 asyncRequest(new SingleStatement(subject, relation), listener);
3274 public void forSingleStatement(Resource subject, Resource relation,
3275 SyncListener<Statement> listener) {
3276 asyncRequest(new SingleStatement(subject, relation), listener);
3280 public void forSingleStatement(Resource subject, Resource relation,
3281 Listener<Statement> listener) {
3282 asyncRequest(new SingleStatement(subject, relation), listener);
3286 final public void forSingleStatement(final Resource subject,
3287 final Resource relation, final AsyncProcedure<Statement> procedure) {
3289 assert (subject != null);
3290 assert (relation != null);
3291 assert (procedure != null);
3293 processor.forEachStatement(this, subject, relation,
3294 new SingleOrErrorProcedure<Statement>(procedure));
3299 public void forSingleStatement(Resource subject, Resource relation,
3300 SyncProcedure<Statement> procedure) {
3301 forSingleStatement(subject, relation,
3302 new SyncToAsyncProcedure<Statement>(procedure));
3306 public void forSingleStatement(Resource subject, Resource relation,
3307 Procedure<Statement> procedure) {
3308 forSingleStatement(subject, relation,
3309 new NoneToAsyncProcedure<Statement>(procedure));
3313 public void forSingleType(Resource subject,
3314 AsyncListener<Resource> listener) {
3315 asyncRequest(new SingleTypeAny(subject), listener);
3319 public void forSingleType(Resource subject,
3320 SyncListener<Resource> listener) {
3321 asyncRequest(new SingleTypeAny(subject), listener);
3325 public void forSingleType(Resource subject,
3326 Listener<Resource> listener) {
3327 asyncRequest(new SingleTypeAny(subject), listener);
3331 final public void forSingleType(final Resource subject, final AsyncProcedure<Resource> procedure) {
3333 assert (subject != null);
3334 assert (procedure != null);
3336 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3338 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
3341 public void execute(AsyncReadGraph graph, final Resource principalType) {
3342 checkedProcedure.offer(graph, principalType);
3346 public void finished(AsyncReadGraph graph) {
3347 checkedProcedure.dec(graph);
3351 public void exception(AsyncReadGraph graph, Throwable t) {
3352 checkedProcedure.exception(graph, t);
3356 public String toString() {
3357 return "forSingleType -> " + procedure;
3365 public void forSingleType(Resource subject, SyncProcedure<Resource> procedure) {
3366 forSingleType(subject, new SyncToAsyncProcedure<Resource>(
3371 public void forSingleType(Resource subject, Procedure<Resource> procedure) {
3372 forSingleType(subject, new NoneToAsyncProcedure<Resource>(
3377 public void forSingleType(Resource subject, Resource relation,
3378 AsyncListener<Resource> listener) {
3379 asyncRequest(new SingleType(subject, relation), listener);
3383 public void forSingleType(Resource subject, Resource relation,
3384 SyncListener<Resource> listener) {
3385 asyncRequest(new SingleType(subject, relation), listener);
3389 public void forSingleType(Resource subject, Resource relation,
3390 Listener<Resource> listener) {
3391 asyncRequest(new SingleType(subject, relation), listener);
3395 final public void forSingleType(final Resource subject,
3396 final Resource baseType, final AsyncProcedure<Resource> procedure) {
3398 assert (subject != null);
3399 assert (procedure != null);
3401 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3403 processor.forEachPrincipalType(this, subject,
3404 new AsyncMultiProcedureAdapter<Resource>() {
3407 public void execute(AsyncReadGraph graph,
3408 final Resource principalType) {
3410 checkedProcedure.inc();
3412 if(baseType == null) {
3414 checkedProcedure.offer(graph, principalType);
3415 checkedProcedure.dec(graph);
3417 } else if(principalType.equals(baseType)) {
3419 checkedProcedure.offer(graph, principalType);
3420 checkedProcedure.dec(graph);
3424 processor.forSupertypes((ReadGraphImpl)graph, principalType,
3425 new AsyncProcedure<Set<Resource>>() {
3428 public void execute(
3429 AsyncReadGraph graph,
3430 Set<Resource> result) {
3432 if (result.contains(baseType))
3433 checkedProcedure.offer(graph,
3435 checkedProcedure.dec(graph);
3440 public void exception(
3441 AsyncReadGraph graph,
3444 .exception(graph, t);
3454 public void finished(AsyncReadGraph graph) {
3455 checkedProcedure.dec(graph);
3459 public void exception(AsyncReadGraph graph, Throwable t) {
3460 checkedProcedure.exception(graph, t);
3464 public String toString() {
3465 return "forSingleType -> " + procedure;
3473 public void forSingleType(Resource subject, Resource relation,
3474 SyncProcedure<Resource> procedure) {
3475 forSingleType(subject, relation, new SyncToAsyncProcedure<Resource>(
3480 public void forSingleType(Resource subject, Resource relation,
3481 Procedure<Resource> procedure) {
3482 forSingleType(subject, relation, new NoneToAsyncProcedure<Resource>(
3487 public <T> void forValue(Resource subject, Binding binding,
3488 AsyncListener<T> listener) {
3489 asyncRequest(new Value<T>(subject, binding), listener);
3493 public <T> void forValue(Resource subject, Binding binding,
3494 SyncListener<T> listener) {
3495 asyncRequest(new Value<T>(subject, binding), listener);
3499 public <T> void forValue(Resource subject, Binding binding,
3500 Listener<T> listener) {
3501 asyncRequest(new Value<T>(subject, binding), listener);
3505 public <T> void forValue(final Resource resource, final Binding binding,
3506 final AsyncProcedure<T> procedure) {
3508 assert (resource != null);
3509 assert (binding != null);
3510 assert (procedure != null);
3512 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
3515 public void execute(AsyncReadGraph graph, byte[] result) {
3519 if (result == null) {
3520 procedure.exception(graph,
3521 new DoesNotContainValueException(
3522 "No value for resource " + resource));
3526 Serializer serializer = binding.serializer();
3527 // Serializer serializer = Bindings.getSerializer( binding );
3528 Object obj = serializer.deserialize(result);
3529 // if (!binding.isInstance(obj))
3530 // procedure.exception(graph, new ClassCastException(
3531 // "Cannot get value " + obj + " with binding "
3534 procedure.execute(graph, (T) obj);
3536 } catch (Throwable t) {
3537 procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
3542 public void exception(AsyncReadGraph graph, Throwable t) {
3544 procedure.exception(graph, t);
3545 } catch (Throwable t2) {
3546 Logger.defaultLogError(t2);
3551 public String toString() {
3552 return "forValue -> " + procedure;
3559 private static String safeArrayToString(byte[] a) {
3562 int iMax = a.length - 1;
3566 StringBuilder b = new StringBuilder();
3568 for (int i = 0; i < 100; i++) { // limit to first 100 items
3571 return b.append(']').toString();
3574 return b.append(", ... (" + a.length + ")]").toString();
3578 public <T> void forValue(Resource subject, Binding binding,
3579 SyncProcedure<T> procedure) {
3580 forValue(subject, binding, new SyncToAsyncProcedure<T>(procedure));
3584 public <T> void forValue(Resource subject, Binding binding,
3585 Procedure<T> procedure) {
3586 forValue(subject, binding, new NoneToAsyncProcedure<T>(procedure));
3590 public <T> void forValue(Resource subject, AsyncListener<T> listener) {
3591 asyncRequest(new ValueImplied<T>(subject), listener);
3595 public <T> void forValue(Resource subject, SyncListener<T> listener) {
3596 asyncRequest(new ValueImplied<T>(subject), listener);
3600 public <T> void forValue(Resource subject, Listener<T> listener) {
3601 asyncRequest(new ValueImplied<T>(subject), listener);
3605 final public <T> void forValue(final Resource subject, final AsyncProcedure<T> procedure) {
3607 assert (subject != null);
3608 assert (procedure != null);
3610 forRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
3613 public void execute(AsyncReadGraph graph, Datatype type) {
3614 // TODO: consider trying Bindings.getBeanBinding(type);
3615 Binding binding = Bindings.getBinding(type);
3616 graph.forValue(subject, binding, procedure);
3620 public void exception(AsyncReadGraph graph, Throwable throwable) {
3621 procedure.exception(graph, new DoesNotContainValueException("Invalid data type", throwable));
3629 public <T> void forValue(Resource subject, SyncProcedure<T> procedure) {
3630 forValue(subject, new SyncToAsyncProcedure<T>(procedure));
3634 public <T> void forValue(Resource subject, Procedure<T> procedure) {
3635 forValue(subject, new NoneToAsyncProcedure<T>(procedure));
3639 public <T> void forRelatedValue(Resource subject, Resource relation,
3640 AsyncListener<T> listener) {
3641 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3645 public <T> void forRelatedValue(Resource subject, Resource relation,
3646 SyncListener<T> listener) {
3647 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3651 public <T> void forRelatedValue(Resource subject, Resource relation,
3652 Listener<T> listener) {
3653 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3657 final public <T> void forRelatedValue(final Resource subject,
3658 final Resource relation, final AsyncProcedure<T> procedure) {
3660 assert (subject != null);
3661 assert (relation != null);
3662 assert (procedure != null);
3664 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3666 processor.forEachObject(this, subject, relation,
3667 new AsyncMultiProcedureAdapter<Resource>() {
3670 public void execute(AsyncReadGraph graph,
3671 final Resource object) {
3673 checkedProcedure.inc();
3675 graph.forValue(object, new AsyncProcedure<Object>() {
3678 public void execute(AsyncReadGraph graph,
3680 checkedProcedure.offer(graph, (T) result);
3681 checkedProcedure.dec(graph);
3685 public void exception(AsyncReadGraph graph,
3687 checkedProcedure.exception(graph, t);
3691 public String toString() {
3692 return "forRelatedValue -> " + procedure;
3700 public void finished(AsyncReadGraph graph) {
3701 checkedProcedure.dec(graph);
3705 public void exception(AsyncReadGraph graph, Throwable t) {
3706 checkedProcedure.exception(graph, t);
3714 public <T> void forRelatedValue(Resource subject, Resource relation,
3715 SyncProcedure<T> procedure) {
3716 forRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
3721 public <T> void forRelatedValue(Resource subject, Resource relation,
3722 Procedure<T> procedure) {
3723 forRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
3728 public <T> void forRelatedValue(Resource subject, Resource relation,
3729 Binding binding, AsyncListener<T> listener) {
3730 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3734 public <T> void forRelatedValue(Resource subject, Resource relation,
3735 Binding binding, SyncListener<T> listener) {
3736 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3740 public <T> void forRelatedValue(Resource subject, Resource relation,
3741 Binding binding, Listener<T> listener) {
3742 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3746 final public <T> void forRelatedValue(final Resource subject,
3747 final Resource relation, final Binding binding,
3748 final AsyncProcedure<T> procedure) {
3750 assert (subject != null);
3751 assert (relation != null);
3752 assert (binding != null);
3753 assert (procedure != null);
3755 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3757 processor.forEachObject(this, subject, relation,
3758 new AsyncMultiProcedureAdapter<Resource>() {
3761 public void execute(AsyncReadGraph graph,
3762 final Resource object) {
3764 checkedProcedure.inc();
3766 graph.forValue(object, binding, new AsyncProcedure<Object>() {
3769 public void execute(AsyncReadGraph graph,
3772 checkedProcedure.offer(graph,
3774 checkedProcedure.dec(graph);
3778 public void exception(AsyncReadGraph graph,
3780 checkedProcedure.exception(graph, t);
3784 public String toString() {
3785 return "forRelatedValue -> "
3794 public void finished(AsyncReadGraph graph) {
3795 checkedProcedure.dec(graph);
3799 public void exception(AsyncReadGraph graph, Throwable t) {
3800 checkedProcedure.exception(graph, t);
3808 public <T> void forRelatedValue(Resource subject, Resource relation,
3809 Binding binding, SyncProcedure<T> procedure) {
3810 forRelatedValue(subject, relation, binding,
3811 new SyncToAsyncProcedure<T>(procedure));
3815 public <T> void forRelatedValue(Resource subject, Resource relation,
3816 Binding binding, Procedure<T> procedure) {
3817 forRelatedValue(subject, relation, binding,
3818 new NoneToAsyncProcedure<T>(procedure));
3822 public <T> void forAdapted(Resource resource, Class<T> clazz,
3823 AsyncListener<T> listener) {
3824 asyncRequest(new Adapter<T>(resource, clazz), listener);
3828 public <T> void forAdapted(Resource resource, Class<T> clazz,
3829 SyncListener<T> listener) {
3830 asyncRequest(new Adapter<T>(resource, clazz), listener);
3834 public <T> void forAdapted(Resource resource, Class<T> clazz,
3835 Listener<T> listener) {
3836 asyncRequest(new Adapter<T>(resource, clazz), listener);
3840 final public <T> void forAdapted(final Resource resource,
3841 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3843 assert (resource != null);
3844 assert (clazz != null);
3845 assert (procedure != null);
3847 final AdaptionService service = getSession().peekService(AdaptionService.class);
3848 if (service == null)
3849 procedure.exception(this, new ServiceException("No AdaptionService available"));
3851 service.adapt(this, resource, resource, Resource.class, clazz, false, procedure);
3856 public <T> void forAdapted(Resource resource, Class<T> clazz,
3857 SyncProcedure<T> procedure) {
3858 forAdapted(resource, clazz, new SyncToAsyncProcedure<T>(procedure));
3862 public <T> void forAdapted(Resource resource, Class<T> clazz,
3863 Procedure<T> procedure) {
3864 forAdapted(resource, clazz, new NoneToAsyncProcedure<T>(procedure));
3868 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3869 AsyncListener<T> listener) {
3870 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3874 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3875 SyncListener<T> listener) {
3876 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3880 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3881 Listener<T> listener) {
3882 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3886 final public <T> void forUniqueAdapted(final Resource resource,
3887 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3889 assert (resource != null);
3890 assert (clazz != null);
3891 assert (procedure != null);
3893 final AdaptionService service = getSession().peekService(AdaptionService.class);
3894 if (service == null)
3895 procedure.exception(this, new ServiceException("No AdaptionService available"));
3897 service.adaptNew(this, resource, clazz, false, procedure);
3902 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3903 SyncProcedure<T> procedure) {
3904 forUniqueAdapted(resource, clazz,
3905 new SyncToAsyncProcedure<T>(procedure));
3909 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3910 Procedure<T> procedure) {
3911 forUniqueAdapted(resource, clazz,
3912 new NoneToAsyncProcedure<T>(procedure));
3916 public void forPossibleInverse(Resource subject,
3917 AsyncListener<Resource> listener) {
3918 asyncRequest(new PossibleInverse(subject), listener);
3922 public void forPossibleInverse(Resource subject,
3923 SyncListener<Resource> listener) {
3924 asyncRequest(new PossibleInverse(subject), listener);
3928 public void forPossibleInverse(Resource subject, Listener<Resource> listener) {
3929 asyncRequest(new PossibleInverse(subject), listener);
3933 final public void forPossibleInverse(final Resource relation,
3934 final AsyncProcedure<Resource> procedure) {
3936 assert (relation != null);
3937 assert (procedure != null);
3939 processor.forInverse(this, relation, new ExceptionToNullProcedure<Resource>(procedure));
3944 public void forPossibleInverse(Resource subject,
3945 SyncProcedure<Resource> procedure) {
3946 forPossibleInverse(subject, new SyncToAsyncProcedure<Resource>(
3951 public void forPossibleInverse(Resource subject,
3952 Procedure<Resource> procedure) {
3953 forPossibleInverse(subject, new NoneToAsyncProcedure<Resource>(
3958 public void forPossibleObject(Resource subject, Resource relation,
3959 AsyncListener<Resource> listener) {
3960 asyncRequest(new PossibleObject(subject, relation), listener);
3964 public void forPossibleObject(Resource subject, Resource relation,
3965 SyncListener<Resource> listener) {
3966 asyncRequest(new PossibleObject(subject, relation), listener);
3970 public void forPossibleObject(Resource subject, Resource relation,
3971 Listener<Resource> listener) {
3972 asyncRequest(new PossibleObject(subject, relation), listener);
3976 final public void forPossibleObject(final Resource subject,
3977 final Resource relation, final AsyncProcedure<Resource> procedure) {
3979 assert (subject != null);
3980 assert (relation != null);
3981 assert (procedure != null);
3983 processor.forEachObject(this, subject, relation,
3984 new SingleOrNullProcedure<Resource>(procedure));
3989 public void forPossibleObject(Resource subject, Resource relation,
3990 SyncProcedure<Resource> procedure) {
3991 forPossibleObject(subject, relation,
3992 new SyncToAsyncProcedure<Resource>(procedure));
3996 public void forPossibleObject(Resource subject, Resource relation,
3997 Procedure<Resource> procedure) {
3998 forPossibleObject(subject, relation,
3999 new NoneToAsyncProcedure<Resource>(procedure));
4003 public void forPossibleStatement(Resource subject, Resource relation,
4004 AsyncListener<Statement> listener) {
4005 asyncRequest(new PossibleStatement(subject, relation), listener);
4009 public void forPossibleStatement(Resource subject, Resource relation,
4010 SyncListener<Statement> listener) {
4011 asyncRequest(new PossibleStatement(subject, relation), listener);
4015 public void forPossibleStatement(Resource subject, Resource relation,
4016 Listener<Statement> listener) {
4017 asyncRequest(new PossibleStatement(subject, relation), listener);
4021 final public void forPossibleStatement(final Resource subject,
4022 final Resource relation, final AsyncProcedure<Statement> procedure) {
4024 assert (subject != null);
4025 assert (relation != null);
4026 assert (procedure != null);
4028 processor.forEachStatement(this, subject, relation,
4029 new SingleFunctionalOrNullProcedure<Statement>(
4030 "forPossibleStatement", procedure));
4035 public void forPossibleStatement(Resource subject, Resource relation,
4036 SyncProcedure<Statement> procedure) {
4037 forPossibleStatement(subject, relation,
4038 new SyncToAsyncProcedure<Statement>(procedure));
4042 public void forPossibleStatement(Resource subject, Resource relation,
4043 Procedure<Statement> procedure) {
4044 forPossibleStatement(subject, relation,
4045 new NoneToAsyncProcedure<Statement>(procedure));
4049 public void forPossibleType(Resource subject, Resource relation,
4050 AsyncListener<Resource> listener) {
4051 asyncRequest(new PossibleType(subject, relation), listener);
4055 public void forPossibleType(Resource subject, Resource relation,
4056 SyncListener<Resource> listener) {
4057 asyncRequest(new PossibleType(subject, relation), listener);
4061 public void forPossibleType(Resource subject, Resource relation,
4062 Listener<Resource> listener) {
4063 asyncRequest(new PossibleType(subject, relation), listener);
4067 final public void forPossibleType(final Resource subject,
4068 final Resource baseType, final AsyncProcedure<Resource> procedure) {
4070 assert (subject != null);
4071 assert (procedure != null);
4073 final NullSingleOrNullProcedure<Resource> checkedProcedure = new NullSingleOrNullProcedure<Resource>(procedure);
4075 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
4078 public void execute(AsyncReadGraph graph,
4079 final Resource principalType) {
4081 if (baseType == null) {
4083 checkedProcedure.offer(graph, principalType);
4085 } else if (principalType.equals(baseType)) {
4087 checkedProcedure.offer(graph, principalType);
4091 checkedProcedure.inc();
4093 processor.forSupertypes((ReadGraphImpl)graph, principalType,
4094 new AsyncProcedure<Set<Resource>>() {
4097 public void execute(
4098 AsyncReadGraph graph,
4099 Set<Resource> result) {
4101 if (result.contains(baseType)) {
4102 checkedProcedure.offer(graph,
4106 checkedProcedure.dec(graph);
4111 public void exception(
4112 AsyncReadGraph graph,
4114 checkedProcedure.exception(graph, t);
4115 checkedProcedure.dec(graph);
4119 public String toString() {
4120 return "forPossibleType -> "
4131 public void finished(AsyncReadGraph graph) {
4132 checkedProcedure.dec(graph);
4136 public void exception(AsyncReadGraph graph, Throwable t) {
4137 checkedProcedure.exception(graph, t);
4138 checkedProcedure.dec(graph);
4146 public void forPossibleType(Resource subject, Resource relation,
4147 SyncProcedure<Resource> procedure) {
4148 forPossibleType(subject, relation, new SyncToAsyncProcedure<Resource>(
4153 public void forPossibleType(Resource subject, Resource relation,
4154 Procedure<Resource> procedure) {
4155 forPossibleType(subject, relation, new NoneToAsyncProcedure<Resource>(
4160 public <T> void forPossibleValue(Resource subject, AsyncListener<T> listener) {
4161 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4165 public <T> void forPossibleValue(Resource subject, SyncListener<T> listener) {
4166 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4170 public <T> void forPossibleValue(Resource subject, Listener<T> listener) {
4171 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4175 final public <T> void forPossibleValue(final Resource subject,
4176 final AsyncProcedure<T> procedure) {
4178 assert (subject != null);
4179 assert (procedure != null);
4181 forPossibleRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
4184 public void execute(AsyncReadGraph graph, final Datatype type) {
4186 procedure.execute(graph, null);
4189 // TODO: consider trying Bindings.getBeanBinding(type);
4190 Binding binding = Bindings.getBinding(type);
4191 graph.forPossibleValue(subject, binding, procedure);
4192 } catch (RuntimeBindingConstructionException e) {
4193 procedure.exception(graph, e);
4199 public void exception(AsyncReadGraph graph, Throwable t) {
4200 procedure.exception(graph, t);
4204 public String toString() {
4205 return "forPossibleValue -> " + procedure;
4213 public <T> void forPossibleValue(Resource subject,
4214 SyncProcedure<T> procedure) {
4215 forPossibleValue(subject, new SyncToAsyncProcedure<T>(procedure));
4219 public <T> void forPossibleValue(Resource subject, Procedure<T> procedure) {
4220 forPossibleValue(subject, new NoneToAsyncProcedure<T>(procedure));
4224 public <T> void forPossibleValue(Resource subject, Binding binding,
4225 AsyncListener<T> listener) {
4226 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4230 public <T> void forPossibleValue(Resource subject, Binding binding,
4231 SyncListener<T> listener) {
4232 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4236 public <T> void forPossibleValue(Resource subject, Binding binding,
4237 Listener<T> listener) {
4238 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4242 final public <T> void forPossibleValue(final Resource resource,
4243 final Binding binding, final AsyncProcedure<T> procedure) {
4245 assert (resource != null);
4246 assert (binding != null);
4247 assert (procedure != null);
4249 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
4252 public void execute(AsyncReadGraph graph, byte[] result) {
4256 if (result == null) {
4257 procedure.execute(graph, null);
4261 Serializer serializer = Bindings.getSerializer( binding );
4262 Object obj = serializer.deserialize(result);
4263 if (!binding.isInstance(obj))
4264 procedure.exception(graph, new ClassCastException(
4265 "Cannot get value " + obj + " with binding "
4268 procedure.execute(graph, (T) obj);
4270 } catch (Throwable t) {
4271 procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
4276 public void exception(AsyncReadGraph graph, Throwable t) {
4278 procedure.exception(graph, t);
4279 } catch (Throwable t2) {
4280 Logger.defaultLogError(t2);
4285 public String toString() {
4286 return "forPossibleValue -> " + procedure;
4294 public <T> void forPossibleValue(Resource subject, Binding binding,
4295 SyncProcedure<T> procedure) {
4296 forPossibleValue(subject, binding, new SyncToAsyncProcedure<T>(
4301 public <T> void forPossibleValue(Resource subject, Binding binding,
4302 Procedure<T> procedure) {
4303 forPossibleValue(subject, binding, new NoneToAsyncProcedure<T>(
4308 public <T> void forPossibleRelatedValue(Resource subject,
4309 Resource relation, AsyncListener<T> listener) {
4310 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4315 public <T> void forPossibleRelatedValue(Resource subject,
4316 Resource relation, SyncListener<T> listener) {
4317 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4322 public <T> void forPossibleRelatedValue(Resource subject,
4323 Resource relation, Listener<T> listener) {
4324 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4329 final public <T> void forPossibleRelatedValue(final Resource subject,
4330 final Resource relation, final AsyncProcedure<T> procedure) {
4332 assert (subject != null);
4333 assert (relation != null);
4334 assert (procedure != null);
4336 final DeepSingleOrNullProcedure<T> checkedProcedure = new DeepSingleOrNullProcedure<T>(procedure);
4338 processor.forEachObject(this, subject, relation,
4339 new AsyncMultiProcedureAdapter<Resource>() {
4342 public void execute(AsyncReadGraph graph,
4343 final Resource object) {
4345 checkedProcedure.inc();
4347 graph.forValue(object, new AsyncProcedure<Object>() {
4350 public void execute(AsyncReadGraph graph,
4352 checkedProcedure.offer(graph, (T) result);
4353 checkedProcedure.dec(graph);
4357 public void exception(AsyncReadGraph graph,
4359 checkedProcedure.exception(graph, t);
4360 checkedProcedure.dec(graph);
4368 public void finished(AsyncReadGraph graph) {
4370 checkedProcedure.dec(graph);
4374 public void exception(AsyncReadGraph graph, Throwable t) {
4375 checkedProcedure.exception(graph, t);
4376 checkedProcedure.dec(graph);
4380 public String toString() {
4381 return "forPossibleRelatedValue -> " + procedure;
4388 public <T> void forPossibleRelatedValue(Resource subject,
4389 Resource relation, SyncProcedure<T> procedure) {
4390 forPossibleRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
4395 public <T> void forPossibleRelatedValue(Resource subject,
4396 Resource relation, Procedure<T> procedure) {
4397 forPossibleRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
4402 public <T> void forPossibleRelatedValue(Resource subject,
4403 Resource relation, Binding binding, AsyncListener<T> listener) {
4404 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4409 public <T> void forPossibleRelatedValue(Resource subject,
4410 Resource relation, Binding binding, SyncListener<T> listener) {
4411 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4416 public <T> void forPossibleRelatedValue(Resource subject,
4417 Resource relation, Binding binding, Listener<T> listener) {
4418 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4422 final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4423 final AsyncProcedure<T> procedure) {
4425 assert (subject != null);
4426 assert (relation != null);
4427 assert (procedure != null);
4429 processor.forPossibleObject(this, subject, relation, new AsyncProcedure<Resource>() {
4432 public void execute(AsyncReadGraph graph, Resource object) {
4434 if(object == null) {
4435 procedure.execute(graph, null);
4439 processor.forPossibleValue((ReadGraphImpl)graph, object, new AsyncProcedure<byte[]>() {
4442 public void execute(AsyncReadGraph graph, byte[] bytes) {
4448 Serializer serializer = binding.serializer();
4449 Object obj = serializer.deserialize(bytes);
4450 if (!binding.isInstance(obj)) {
4451 procedure.exception(graph, new ClassCastException("Cannot get value " + obj + " with binding " + binding));
4453 procedure.execute(graph, (T) obj);
4456 } catch (Throwable t) {
4458 procedure.exception(graph, t);
4464 procedure.execute(graph, null);
4471 public void exception(AsyncReadGraph graph, Throwable t) {
4472 procedure.exception(graph, t);
4480 public void exception(AsyncReadGraph graph, Throwable throwable) {
4481 throwable.printStackTrace();
4482 procedure.exception(graph, throwable);
4490 public <T> void forPossibleRelatedValue(Resource subject,
4491 Resource relation, Binding binding, SyncProcedure<T> procedure) {
4492 forPossibleRelatedValue(subject, relation, binding,
4493 new SyncToAsyncProcedure<T>(procedure));
4497 public <T> void forPossibleRelatedValue(Resource subject,
4498 Resource relation, Binding binding, Procedure<T> procedure) {
4499 forPossibleRelatedValue(subject, relation, binding,
4500 new NoneToAsyncProcedure<T>(procedure));
4504 public void forIsInstanceOf(Resource subject, Resource relation,
4505 AsyncListener<Boolean> listener) {
4506 asyncRequest(new IsInstanceOf(subject, relation), listener);
4510 public void forIsInstanceOf(Resource subject, Resource relation,
4511 SyncListener<Boolean> listener) {
4512 asyncRequest(new IsInstanceOf(subject, relation), listener);
4516 public void forIsInstanceOf(Resource subject, Resource relation,
4517 Listener<Boolean> listener) {
4518 asyncRequest(new IsInstanceOf(subject, relation), listener);
4522 final public void forIsInstanceOf(final Resource resource,
4523 final Resource type, final AsyncProcedure<Boolean> procedure) {
4525 assert (resource != null);
4526 assert (type != null);
4527 assert (procedure != null);
4529 forTypes(resource, new AsyncProcedure<Set<Resource>>() {
4532 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4535 if (result.contains(type))
4536 procedure.execute(graph, true);
4538 procedure.execute(graph, false);
4539 } catch (Throwable t) {
4540 Logger.defaultLogError(t);
4545 public void exception(AsyncReadGraph graph, Throwable t) {
4547 procedure.exception(graph, t);
4548 } catch (Throwable t2) {
4549 Logger.defaultLogError(t2);
4554 public String toString() {
4555 return "forIsInstanceOf -> " + procedure;
4563 public void forIsInstanceOf(Resource subject, Resource relation,
4564 SyncProcedure<Boolean> procedure) {
4565 forIsInstanceOf(subject, relation, new SyncToAsyncProcedure<Boolean>(
4570 public void forIsInstanceOf(Resource subject, Resource relation,
4571 Procedure<Boolean> procedure) {
4572 forIsInstanceOf(subject, relation, new NoneToAsyncProcedure<Boolean>(
4577 public void forIsInheritedFrom(Resource subject, Resource relation,
4578 AsyncListener<Boolean> listener) {
4579 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4583 public void forIsInheritedFrom(Resource subject, Resource relation,
4584 SyncListener<Boolean> listener) {
4585 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4589 public void forIsInheritedFrom(Resource subject, Resource relation,
4590 Listener<Boolean> listener) {
4591 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4595 final public void forIsInheritedFrom(final Resource resource,
4596 final Resource type, final AsyncProcedure<Boolean> procedure) {
4598 assert (resource != null);
4599 assert (type != null);
4600 assert (procedure != null);
4602 if (resource.equals(type)) {
4604 procedure.execute(this, true);
4605 } catch (Throwable t) {
4606 Logger.defaultLogError(t);
4611 forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
4614 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4616 if (result.contains(type))
4617 procedure.execute(graph, true);
4619 procedure.execute(graph, false);
4620 } catch (Throwable t) {
4621 Logger.defaultLogError(t);
4626 public void exception(AsyncReadGraph graph, Throwable t) {
4628 procedure.exception(graph, t);
4629 } catch (Throwable t2) {
4630 Logger.defaultLogError(t2);
4635 public String toString() {
4636 return "forIsInheritedFrom -> " + procedure;
4644 public void forIsInheritedFrom(Resource subject, Resource relation,
4645 SyncProcedure<Boolean> procedure) {
4646 forIsInheritedFrom(subject, relation,
4647 new SyncToAsyncProcedure<Boolean>(procedure));
4651 public void forIsInheritedFrom(Resource subject, Resource relation,
4652 Procedure<Boolean> procedure) {
4653 forIsInheritedFrom(subject, relation,
4654 new NoneToAsyncProcedure<Boolean>(procedure));
4658 public void forIsSubrelationOf(Resource subject, Resource relation,
4659 AsyncListener<Boolean> listener) {
4660 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4664 public void forIsSubrelationOf(Resource subject, Resource relation,
4665 SyncListener<Boolean> listener) {
4666 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4670 public void forIsSubrelationOf(Resource subject, Resource relation,
4671 Listener<Boolean> listener) {
4672 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4676 final public void forIsSubrelationOf(final Resource resource,
4677 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4679 assert (resource != null);
4680 assert (relation != null);
4681 assert (procedure != null);
4683 if (resource.equals(relation)) {
4684 procedure.execute(this, true);
4688 forSuperrelations(resource, new AsyncProcedure<Set<Resource>>() {
4691 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4693 if (result.contains(relation))
4694 procedure.execute(graph, true);
4696 procedure.execute(graph, false);
4697 } catch (Throwable t) {
4698 Logger.defaultLogError(t);
4703 public void exception(AsyncReadGraph graph, Throwable t) {
4705 procedure.exception(graph, t);
4706 } catch (Throwable t2) {
4707 Logger.defaultLogError(t2);
4712 public String toString() {
4713 return "forIsSubrelationOf -> " + procedure;
4721 public void forIsSubrelationOf(Resource subject, Resource relation,
4722 SyncProcedure<Boolean> procedure) {
4723 forIsSubrelationOf(subject, relation,
4724 new SyncToAsyncProcedure<Boolean>(procedure));
4728 public void forIsSubrelationOf(Resource subject, Resource relation,
4729 Procedure<Boolean> procedure) {
4730 forIsSubrelationOf(subject, relation,
4731 new NoneToAsyncProcedure<Boolean>(procedure));
4735 public void forHasStatement(Resource subject,
4736 AsyncListener<Boolean> listener) {
4737 asyncRequest(new HasStatementSubject(subject), listener);
4741 public void forHasStatement(Resource subject, SyncListener<Boolean> listener) {
4742 asyncRequest(new HasStatementSubject(subject), listener);
4746 public void forHasStatement(Resource subject, Listener<Boolean> listener) {
4747 asyncRequest(new HasStatementSubject(subject), listener);
4751 final public void forHasStatement(final Resource subject,
4752 final AsyncProcedure<Boolean> procedure) {
4754 assert (subject != null);
4755 assert (procedure != null);
4757 processor.forHasStatement(this, subject, procedure);
4762 public void forHasStatement(Resource subject,
4763 SyncProcedure<Boolean> procedure) {
4764 forHasStatement(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4768 public void forHasStatement(Resource subject, Procedure<Boolean> procedure) {
4769 forHasStatement(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4773 public void forHasStatement(Resource subject, Resource relation,
4774 AsyncListener<Boolean> listener) {
4775 asyncRequest(new HasStatement(subject, relation), listener);
4779 public void forHasStatement(Resource subject, Resource relation,
4780 SyncListener<Boolean> listener) {
4781 asyncRequest(new HasStatement(subject, relation), listener);
4785 public void forHasStatement(Resource subject, Resource relation,
4786 Listener<Boolean> listener) {
4787 asyncRequest(new HasStatement(subject, relation), listener);
4791 final public void forHasStatement(final Resource subject,
4792 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4794 assert (subject != null);
4795 assert (relation != null);
4796 assert (procedure != null);
4798 processor.forHasStatement(this, subject, relation, procedure);
4803 public void forHasStatement(Resource subject, Resource relation,
4804 SyncProcedure<Boolean> procedure) {
4805 forHasStatement(subject, relation, new SyncToAsyncProcedure<Boolean>(
4810 public void forHasStatement(Resource subject, Resource relation,
4811 Procedure<Boolean> procedure) {
4812 forHasStatement(subject, relation, new NoneToAsyncProcedure<Boolean>(
4817 public void forHasStatement(Resource subject, Resource relation,
4818 Resource object, AsyncListener<Boolean> listener) {
4819 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4824 public void forHasStatement(Resource subject, Resource relation,
4825 Resource object, SyncListener<Boolean> listener) {
4826 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4831 public void forHasStatement(Resource subject, Resource relation,
4832 Resource object, Listener<Boolean> listener) {
4833 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4838 final public void forHasStatement(final Resource subject,
4839 final Resource relation, final Resource object,
4840 final AsyncProcedure<Boolean> procedure) {
4842 assert (subject != null);
4843 assert (relation != null);
4844 assert (object != null);
4845 assert (procedure != null);
4847 processor.forHasStatement(this, subject, relation, object, procedure);
4852 public void forHasStatement(Resource subject, Resource relation,
4853 Resource object, SyncProcedure<Boolean> procedure) {
4854 forHasStatement(subject, relation, object,
4855 new SyncToAsyncProcedure<Boolean>(procedure));
4859 public void forHasStatement(Resource subject, Resource relation,
4860 Resource object, Procedure<Boolean> procedure) {
4861 forHasStatement(subject, relation, object,
4862 new NoneToAsyncProcedure<Boolean>(procedure));
4866 public void forHasValue(Resource subject, AsyncListener<Boolean> listener) {
4867 asyncRequest(new HasValue(subject), listener);
4871 public void forHasValue(Resource subject, SyncListener<Boolean> listener) {
4872 asyncRequest(new HasValue(subject), listener);
4876 public void forHasValue(Resource subject, Listener<Boolean> listener) {
4877 asyncRequest(new HasValue(subject), listener);
4881 final public void forHasValue(final Resource subject,
4882 final AsyncProcedure<Boolean> procedure) {
4884 assert (subject != null);
4885 assert (procedure != null);
4887 processor.forValue(this, subject, new AsyncProcedure<byte[]>() {
4890 public void execute(AsyncReadGraph graph, byte[] result) {
4893 procedure.execute(graph, false);
4895 procedure.execute(graph, true);
4896 } catch (Throwable t) {
4897 Logger.defaultLogError(t);
4902 public void exception(AsyncReadGraph graph, Throwable t) {
4904 procedure.exception(graph, t);
4905 } catch (Throwable t2) {
4906 Logger.defaultLogError(t2);
4911 public String toString() {
4912 return "forHasValue -> " + procedure;
4920 public void forHasValue(Resource subject, SyncProcedure<Boolean> procedure) {
4921 forHasValue(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4925 public void forHasValue(Resource subject, Procedure<Boolean> procedure) {
4926 forHasValue(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4930 public void forOrderedSet(Resource subject,
4931 AsyncMultiListener<Resource> listener) {
4932 asyncRequest(new OrderedSet(subject), listener);
4936 public void forOrderedSet(Resource subject,
4937 SyncMultiListener<Resource> listener) {
4938 asyncRequest(new OrderedSet(subject), listener);
4942 public void forOrderedSet(Resource subject, MultiListener<Resource> listener) {
4943 asyncRequest(new OrderedSet(subject), listener);
4947 final public void forOrderedSet(final Resource subject,
4948 final AsyncMultiProcedure<Resource> procedure) {
4950 assert (subject != null);
4951 assert (procedure != null);
4953 processor.forOrderedSet(this, subject,
4954 new AsyncMultiProcedure<Resource>() {
4957 public void finished(AsyncReadGraph graph) {
4959 procedure.finished(graph);
4960 } catch (Throwable t) {
4961 Logger.defaultLogError(t);
4966 public void execute(AsyncReadGraph graph, Resource result) {
4968 procedure.execute(graph, result);
4969 } catch (Throwable t) {
4970 Logger.defaultLogError(t);
4975 public void exception(AsyncReadGraph graph, Throwable t) {
4977 procedure.exception(graph, t);
4978 } catch (Throwable t2) {
4979 Logger.defaultLogError(t2);
4984 public String toString() {
4985 return "forOrderedSet -> " + procedure;
4993 public void forOrderedSet(Resource subject,
4994 SyncMultiProcedure<Resource> procedure) {
4995 forOrderedSet(subject, new SyncToAsyncMultiProcedure<Resource>(
5000 public void forOrderedSet(Resource subject,
5001 MultiProcedure<Resource> procedure) {
5002 forOrderedSet(subject, new NoneToAsyncMultiProcedure<Resource>(
5007 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5008 AsyncListener<T> listener) {
5009 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5013 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5014 SyncListener<T> listener) {
5015 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5019 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5020 Listener<T> listener) {
5021 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5025 final public <T> void forPossibleAdapted(final Resource resource,
5026 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5028 assert (resource != null);
5029 assert (clazz != null);
5030 assert (procedure != null);
5032 final AdaptionService service = getSession().peekService(AdaptionService.class);
5033 if (service == null)
5034 procedure.exception(this, new ServiceException("No AdaptionService available"));
5036 service.adapt(this, resource, resource, Resource.class, clazz, true, procedure);
5040 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5041 SyncProcedure<T> procedure) {
5042 forPossibleAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5047 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5048 Procedure<T> procedure) {
5049 forPossibleAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5054 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5055 AsyncListener<T> listener) {
5056 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5060 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5061 SyncListener<T> listener) {
5062 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5066 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5067 Listener<T> listener) {
5068 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5072 final public <T> void forPossibleUniqueAdapted(final Resource resource,
5073 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5075 assert (resource != null);
5076 assert (clazz != null);
5077 assert (procedure != null);
5079 final AdaptionService service = getSession().peekService(AdaptionService.class);
5080 if (service == null)
5081 procedure.exception(this, new ServiceException("No AdaptionService available"));
5083 service.adaptNew(this, resource, clazz, true, procedure);
5088 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5089 SyncProcedure<T> procedure) {
5090 forPossibleUniqueAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5095 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5096 Procedure<T> procedure) {
5097 forPossibleUniqueAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5102 * Implementation of the interface AsyncRequestProcessor
5106 final public Session getSession() {
5107 return processor.getSession();
5111 public <T> void asyncRequest(final Read<T> request) {
5113 asyncRequest(request, new AsyncProcedure<T>() {
5116 public void execute(AsyncReadGraph graph, T result) {
5120 public void exception(AsyncReadGraph graph, Throwable t) {
5121 Logger.defaultLogError(t);
5125 public String toString() {
5126 return "asyncRequest(Read) -> " + request;
5134 public <T> void asyncRequest(Read<T> request, AsyncListener<T> procedure) {
5135 asyncRequest(request, (AsyncProcedure<T>) procedure);
5139 public <T> void asyncRequest(Read<T> request,
5140 final SyncListener<T> procedure) {
5141 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5145 public <T> void asyncRequest(Read<T> request, final Listener<T> procedure) {
5146 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5150 public <T> void asyncRequest(final Read<T> request, final AsyncProcedure<T> procedure) {
5152 assert (request != null);
5153 assert (procedure != null);
5155 processor.schedule(new SessionTask(this) {
5158 public void run0(int thread) {
5160 final ListenerBase listener = getListenerBase(procedure);
5161 QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5162 } catch (DatabaseException e) {
5163 Logger.defaultLogError(e);
5171 public static ReadGraphImpl createAsync(QueryProcessor support) {
5172 return new ReadGraphImpl(null, null, support);
5176 public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
5177 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5181 public <T> void asyncRequest(Read<T> request, final Procedure<T> procedure) {
5182 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5186 final public <T> void asyncRequest(final AsyncRead<T> request) {
5188 assert (request != null);
5190 asyncRequest(request, new AsyncProcedure<T>() {
5193 public void execute(AsyncReadGraph graph, T result) {
5197 public void exception(AsyncReadGraph graph, Throwable t) {
5198 Logger.defaultLogError(t);
5202 public String toString() {
5203 return "asyncRequest(AsyncRead) -> " + request;
5211 public <T> void asyncRequest(AsyncRead<T> request,
5212 AsyncListener<T> procedure) {
5213 asyncRequest(request, (AsyncProcedure<T>) procedure);
5217 final public <T> void asyncRequest(AsyncRead<T> request,
5218 final SyncListener<T> procedure) {
5219 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5223 final public <T> void asyncRequest(AsyncRead<T> request,
5224 final Listener<T> procedure) {
5225 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5229 final public <T> void asyncRequest(final AsyncRead<T> request,
5230 final AsyncProcedure<T> procedure) {
5232 assert (request != null);
5233 assert (procedure != null);
5235 processor.schedule(new SessionTask(this) {
5238 public void run0(int thread) {
5240 final ListenerBase listener = getListenerBase(procedure);
5241 QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5242 } catch (DatabaseException e) {
5243 Logger.defaultLogError(e);
5252 public <T> void asyncRequest(AsyncRead<T> request,
5253 SyncProcedure<T> procedure) {
5254 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5258 final public <T> void asyncRequest(final AsyncRead<T> request,
5259 final Procedure<T> procedure) {
5260 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5264 public <T> void asyncRequest(final MultiRead<T> request) {
5266 assert (request != null);
5268 asyncRequest(request, new SyncMultiProcedureAdapter<T>() {
5270 public void exception(ReadGraph graph, Throwable t) {
5271 Logger.defaultLogError(t);
5275 public String toString() {
5276 return "asyncRequest(MultiRead) -> " + request;
5283 public <T> void asyncRequest(MultiRead<T> request,
5284 SyncMultiListener<T> procedure) {
5285 asyncRequest(request, (SyncMultiProcedure<T>)procedure);
5289 public <T> void asyncRequest(MultiRead<T> request,
5290 MultiListener<T> procedure) {
5291 asyncRequest(request, new NoneToSyncMultiListener<T>(procedure));
5296 public <T> void asyncRequest(final MultiRead<T> request,
5297 final SyncMultiProcedure<T> procedure) {
5299 assert (request != null);
5300 assert (procedure != null);
5302 final ListenerBase listener = getListenerBase(procedure);
5304 if (parent != null || listener != null) {
5306 // final ReadGraphImpl newGraph = newSync();
5307 processor.query(this, request, parent, procedure,listener);
5311 // final ReadGraphImpl newGraph = newSync();
5315 request.perform(this, procedure);
5317 } catch (Throwable t) {
5320 procedure.exception(this, t);
5321 } catch (DatabaseException e) {
5322 LOGGER.error("Unexpected exception while handling exception", e);
5332 public <T> void asyncRequest(MultiRead<T> request,
5333 MultiProcedure<T> procedure) {
5334 asyncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
5338 final public <T> void asyncRequest(final AsyncMultiRead<T> request) {
5340 assert (request != null);
5342 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5344 public void exception(AsyncReadGraph graph, Throwable t) {
5345 Logger.defaultLogError(t);
5349 public String toString() {
5350 return "asyncRequest(AsyncMultiRead) -> " + request;
5357 public <T> void asyncRequest(AsyncMultiRead<T> request,
5358 AsyncMultiListener<T> procedure) {
5359 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5363 public <T> void asyncRequest(AsyncMultiRead<T> request,
5364 SyncMultiListener<T> procedure) {
5365 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5369 public <T> void asyncRequest(AsyncMultiRead<T> request,
5370 MultiListener<T> procedure) {
5371 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5375 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5376 final AsyncMultiProcedure<T> procedure) {
5378 assert (request != null);
5379 assert (procedure != null);
5381 ListenerBase listener = getListenerBase(procedure);
5383 if (parent != null || listener != null) {
5385 processor.query(this, request, parent, procedure, listener);
5391 request.perform(this, new AsyncMultiProcedure<T>() {
5394 public void execute(AsyncReadGraph graph, T result) {
5395 procedure.execute(graph, result);
5399 public void finished(AsyncReadGraph graph) {
5400 procedure.finished(graph);
5404 public void exception(AsyncReadGraph graph, Throwable t) {
5405 procedure.exception(graph, t);
5409 public String toString() {
5410 return "asyncRequest(AsyncMultiRead) -> " + procedure;
5415 } catch (Throwable t) {
5417 procedure.exception(this, new DatabaseException(t));
5425 public <T> void asyncRequest(AsyncMultiRead<T> request,
5426 SyncMultiProcedure<T> procedure) {
5427 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5431 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5432 final MultiProcedure<T> procedure) {
5433 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5437 final public <T> void asyncRequest(final ExternalRead<T> request) {
5439 assert (request != null);
5441 asyncRequest(request, new Procedure<T>() {
5444 public void execute(T result) {
5448 public void exception(Throwable t) {
5449 Logger.defaultLogError(t);
5453 public String toString() {
5454 return "asyncRequest(PrimitiveRead) -> " + request;
5462 public <T> void asyncRequest(ExternalRead<T> request,
5463 final Listener<T> procedure) {
5464 asyncRequest(request, (Procedure<T>) procedure);
5468 final public <T> void asyncRequest(final ExternalRead<T> request,
5469 final Procedure<T> procedure) {
5471 assert (request != null);
5472 assert (procedure != null);
5474 final ListenerBase listener = getListenerBase(procedure);
5476 if (parent != null || listener != null) {
5479 QueryCacheBase.resultExternalReadEntry(this, request, parent, listener, procedure);
5480 } catch (DatabaseException e) {
5481 Logger.defaultLogError(e);
5482 // This throwable has already been transferred to procedure at this point - do nothing about it
5487 request.register(this, new Listener<T>() {
5489 public void execute(T result) {
5490 procedure.execute(result);
5494 public void exception(Throwable t) {
5495 procedure.exception(t);
5499 public String toString() {
5500 return "asyncRequest(PrimitiveRead) -> " + request;
5504 public boolean isDisposed() {
5515 public void asyncRequest(final Write request) {
5517 assert (request != null);
5519 getSession().asyncRequest(request);
5521 // processor.asyncWrite(request);
5526 public <T> void asyncRequest(final WriteResult<T> request, Procedure<T> procedure) {
5527 throw new Error("Not implemented.");
5531 public void asyncRequest(Write request, Consumer<DatabaseException> callback) {
5533 assert (request != null);
5535 getSession().asyncRequest(request, callback);
5540 public void asyncRequest(final DelayedWrite request) {
5542 assert (request != null);
5544 getSession().asyncRequest(request);
5549 public <T> void asyncRequest(final DelayedWriteResult<T> request, Procedure<T> procedure) {
5550 throw new Error("Not implemented.");
5554 public void asyncRequest(DelayedWrite r,
5555 Consumer<DatabaseException> callback) {
5556 throw new Error("Not implemented.");
5560 public void asyncRequest(final WriteOnly request) {
5562 assert (request != null);
5564 getSession().asyncRequest(request);
5569 public <T> void asyncRequest(final WriteOnlyResult<T> request, Procedure<T> procedure) {
5570 throw new Error("Not implemented.");
5574 public void asyncRequest(WriteOnly r, Consumer<DatabaseException> callback) {
5575 throw new Error("Not implemented.");
5579 * Implementation of the interface ServiceLocator
5583 public <T> T getService(Class<T> api) {
5584 if(WriteSupport.class == api) {
5585 if(this instanceof WriteGraphImpl) {
5586 WriteGraphImpl impl = (WriteGraphImpl)this;
5587 return (T)impl.writeSupport;
5590 return getSession().getService(api);
5594 public <T> T peekService(Class<T> api) {
5595 return getSession().peekService(api);
5599 public boolean hasService(Class<?> api) {
5600 return getSession().hasService(api);
5604 public <T> void registerService(Class<T> api, T service) {
5605 getSession().registerService(api, service);
5609 public boolean isImmutable(Resource resource) throws DatabaseException {
5610 ResourceImpl impl = (ResourceImpl)resource;
5611 return processor.isImmutable(impl.id);
5618 protected static String INTERNAL_ERROR_STRING = "Transaction aborted due to internal client error.";
5621 * callerThread is the currently running thread state.syncThread is blocking for
5622 * this execution state.syncParent is the blocking request
5625 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support) {
5626 this.parentGraph = parentGraph;
5627 this.parent = parent;
5628 this.processor = support;
5629 this.asyncBarrier = new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent);
5632 ReadGraphImpl(ReadGraphImpl graph, CacheEntry parent) {
5633 this(graph, parent, graph.processor);
5636 ReadGraphImpl(ReadGraphImpl graph) {
5637 this(graph, graph.parent);
5640 public ReadGraphImpl withParent(CacheEntry parent) {
5641 return new ReadGraphImpl(this, parent);
5644 public ReadGraphImpl forRecompute(CacheEntry parent) {
5645 return new ReadGraphImpl(null, parent, processor);
5648 public static ReadGraphImpl create(QueryProcessor support) {
5649 return new ReadGraphImpl(null, null, support);
5652 public ReadGraphImpl newRestart(ReadGraphImpl impl) {
5654 WriteGraphImpl write = processor.getSession().getService(
5655 WriteGraphImpl.class);
5661 final private ListenerBase getListenerBase(final Object procedure) {
5662 if (procedure instanceof ListenerBase)
5663 return (ListenerBase) procedure;
5668 public <T> void waitAsyncProcedure(AsyncMultiReadProcedure<T> procedure) {
5670 assert(procedure.done());
5674 public <T> void waitAsyncProcedure(AsyncReadProcedure<T> procedure) {
5676 assert(procedure.done());
5680 public boolean resumeTasks() {
5681 return processor.resumeTasks(this);
5684 Class<?> singleClass(Set<Resource> types) {
5685 Class<?> result = null;
5686 for (Resource type : types) {
5687 Class<?> clazz = processor.getBuiltinValue(type);
5688 if (clazz != null) {
5698 private String debugString(Resource r) {
5701 name = getPossibleRelatedValue(r, processor.getL0(this).HasName);
5702 } catch (ManyObjectsForFunctionalRelationException e) {
5703 Logger.defaultLogError(e);
5704 } catch (ServiceException e) {
5705 Logger.defaultLogError(e);
5707 return "[" + name + " - " + r + "]";
5711 public String toString() {
5712 return "ReadGraphImpl[thread=" + Thread.currentThread() + "]";
5716 final public int thread() {
5720 static class MultiTripleIntProcedure implements TripleIntProcedure {
5722 final private AsyncMultiProcedure<Statement> procedure;
5723 final private ReadGraphImpl impl;
5724 final private QuerySupport support;
5726 public MultiTripleIntProcedure(AsyncMultiProcedure<Statement> procedure, ReadGraphImpl impl, QuerySupport support) {
5727 this.procedure = procedure;
5729 this.support = support;
5733 public void execute(ReadGraphImpl graph, int s, int p, int o) {
5735 procedure.execute(graph, support.getStatement(s, p, o));
5736 } catch (Throwable t2) {
5737 Logger.defaultLogError(t2);
5742 public void finished(ReadGraphImpl graph) {
5744 procedure.finished(graph);
5745 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5746 } catch (Throwable t2) {
5747 Logger.defaultLogError(t2);
5752 public void exception(ReadGraphImpl graph, Throwable t) {
5754 procedure.exception(graph, t);
5755 } catch (Throwable t2) {
5756 Logger.defaultLogError(t2);
5758 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5762 public String toString() {
5763 return "forEachObject with " + procedure;
5768 // private AsyncMultiProcedure<Resource> cacheKey = null;
5769 // private MultiIntProcedure cacheResult = null;
5771 // final IntProcedure forMultiProcedure(final AsyncMultiProcedure<Resource> procedure) {
5773 // if(procedure == cacheKey) return cacheResult;
5775 // cacheResult = new MultiIntProcedure(procedure, this, processor.support);
5776 // cacheKey = procedure;
5778 // return cacheResult;
5782 // private AsyncMultiProcedure<Statement> cacheKey2 = null;
5783 // private MultiTripleIntProcedure cacheResult2 = null;
5785 // final synchronized TripleIntProcedure forMultiProcedure(final AsyncMultiProcedure<Statement> procedure) {
5787 // if(procedure == cacheKey2) return cacheResult2;
5789 // cacheResult2 = new MultiTripleIntProcedure(procedure, this, processor.support);
5790 // cacheKey2 = procedure;
5792 // return cacheResult2;
5797 public Datatype getDataType(Resource subject) throws DatabaseException {
5798 for(Resource dataTypeResource : getObjects(subject, processor.getL0(this).HasDataType))
5799 return getValue(dataTypeResource, Bindings.getBindingUnchecked(Datatype.class));
5800 throw new DoesNotContainValueException("The literal has no data type.");
5803 protected <T extends Accessor> T getAccessor4File(Resource subject)
5804 throws DatabaseException {
5807 byte[] bytes = processor.support.getValue(g, subject);
5811 BinaryVariant va = (BinaryVariant) Accessors.getAccessor(bytes, Datatypes.VARIANT);
5812 Accessor ca = va.getContentAccessor();
5814 } catch (AccessorConstructionException e) {
5815 throw new DatabaseException(e);
5821 Binding datatype_binding = Bindings.getBindingUnchecked(DataType.class);
5822 Serializer datatype_serializer = datatype_binding.serializer();
5825 BinaryMemory in = new BinaryMemory(ByteBuffer.wrap(bytes));
5826 datatype = (DataType)datatype_serializer.deserialize(in);
5827 Binding data_binding = Bindings.getBinding(datatype);
5828 Serializer data_serializer = data_binding.serializer(BinarySerializationFormat.INSTANCE);
5829 Object o = data_serializer.deserialize(in);
5831 return (T)Accessors.getAccessor(data_binding, o);
5832 } catch(AccessorConstructionException e) {
5835 } catch (Exception e) {
5836 throw new DatabaseException(e);
5839 @SuppressWarnings("unchecked")
5841 public <T extends Accessor> T getAccessor(Resource subject) throws DatabaseException {
5842 RandomAccessBinary rab = getRandomAccessBinary(subject);
5844 return (T)Accessors.getAccessor(rab, getDataType(subject));
5845 } catch(AccessorConstructionException e) {
5846 throw new DatabaseException(e);
5849 @SuppressWarnings("unchecked")
5850 protected <T extends Accessor> T createAccessor(Resource resource, Datatype datatype, Object intialValue)
5851 throws DatabaseException {
5852 RandomAccessBinary rab = createRandomAccessBinary(resource, datatype, intialValue);
5854 return (T)Accessors.getAccessor(rab, datatype);
5855 } catch(AccessorConstructionException e) {
5856 throw new DatabaseException(e);
5860 public RandomAccessBinary getRandomAccessBinary(Resource subject) throws DatabaseException {
5861 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5862 ResourceData rd = ravs.get(subject);
5866 ExternalValueSupport evs = getService(ExternalValueSupport.class);
5867 long size = evs.getValueSize(this, subject); // Throws DatabaseException if no old external value.
5869 File platform = Platform.getLocation().toFile();
5870 File tempFiles = new File(platform, "tempFiles");
5871 File dbDir = new File(tempFiles, "db");
5873 File file = new File(dbDir, "ResourceFile" + subject.getResourceId());
5874 rd = new ResourceData(new BinaryFile(file), true); // Old external value.
5875 final int N = 1<<20;
5879 int length = N < left ? N : (int)left;
5880 byte[] bytes = evs.readValue(this, subject, offset, length);
5881 offset += bytes.length;
5882 left -= bytes.length;
5883 rd.binaryFile.write(bytes);
5885 ravs.put(subject, rd);
5887 } catch (Exception e) {
5888 throw new DatabaseException("Resource " + subject + " have value but there was problem with accessing value data.", e);
5890 } catch (Exception e) {
5891 if(Development.DEVELOPMENT) {
5892 if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
5893 e.printStackTrace();
5897 Datatype datatype = getDataType(subject);
5898 Object value = getPossibleValue(subject, Bindings.getBinding(datatype));
5899 return createRandomAccessBinary(subject, datatype, value);
5901 public RandomAccessBinary createRandomAccessBinary(Resource resource, Datatype datatype, Object initialValue)
5902 throws DatabaseException {
5903 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5905 File platform = Platform.getLocation().toFile();
5906 File tempFiles = new File(platform, "tempFiles");
5907 File dbDir = new File(tempFiles, "db");
5909 File file = new File(dbDir, "ResourceFile" + resource.getResourceId());
5910 ResourceData rd = new ResourceData(new BinaryFile(file), false);
5911 Binding binding = Bindings.getBinding(datatype);
5912 if (null == initialValue) {
5913 initialValue = binding.createDefault();
5915 Serializer serializer = binding.serializer();
5916 byte[] bytes = serializer.serialize(initialValue);
5917 // In case the file has been previously accessed and was larger we set the correct size now
5918 rd.binaryFile.setLength(bytes.length);
5919 rd.binaryFile.write(bytes);
5920 ravs.put(resource, rd);
5922 } catch (Exception e) {
5923 if (e instanceof DatabaseException)
5924 throw (DatabaseException)e;
5926 throw new DatabaseException(e);
5930 // static class ExternalValueRequest<T> extends ResourceRead<T> {
5932 // public ExternalValueRequest(Resource resource) {
5936 // @SuppressWarnings("unchecked")
5938 // public T perform(ReadGraph graph) throws DatabaseException {
5941 // String uri = graph.getURI(resource);
5942 // if(Layer0.URIs.Functions_functionApplication.equals(uri)) return (T)functionApplication;
5944 // return (T)ReflectionUtils.getValue(uri).getValue();
5946 // } catch(ValueNotFoundException e) {
5947 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
5948 // } catch(ClassCastException e) {
5949 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
5955 @SuppressWarnings("unchecked")
5957 public <T> T getValue2(Resource r, Object context) throws DatabaseException {
5958 Layer0 L0 = processor.getL0(this);
5959 Set<Resource> types = getTypes(r);
5961 if(types.contains(L0.Literal)) {
5962 if(isImmutable(r)) {
5963 return syncRequest(new ValueImplied<T>(r));
5968 else if(types.contains(L0.ExternalValue)) {
5969 return (T)syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
5973 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
5974 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
5976 return function.apply(this, r, context);
5977 } catch(RuntimeException e) {
5978 DatabaseException dte = findPossibleRootException(e);
5979 if(dte != null) throw dte;
5980 else throw new DatabaseException(e);
5987 public Variant getVariantValue2(Resource r, Object context) throws DatabaseException {
5988 Layer0 L0 = processor.getL0(this);
5989 Set<Resource> types = getTypes(r);
5991 if(types.contains(L0.Literal)) {
5992 if(isImmutable(r)) {
5993 return syncRequest(new VariantValueImplied(r));
5995 return getVariantValue(r);
5998 else if(types.contains(L0.ExternalValue)) {
5999 Object value = syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6001 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6002 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6003 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6008 Function3<ReadGraph,Resource,Object,Object> function = requestValueFunction(r);
6009 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6011 Object value = function.apply(this, r, context);
6013 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6014 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6015 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6017 } catch(RuntimeException e) {
6018 DatabaseException dte = findPossibleRootException(e);
6019 if(dte != null) throw dte;
6020 else throw new DatabaseException(e);
6026 public <T> T getPossibleValue2(Resource subject, Object context) throws DatabaseException {
6028 return getValue2(subject, context);
6029 } catch (DatabaseException e) {
6034 static class PossibleConverterFunction<T> extends ResourceRead<Function3<ReadGraph,Resource,Object,T>> {
6036 public PossibleConverterFunction(Resource resource) {
6041 public Function3<ReadGraph,Resource,Object,T> perform(ReadGraph graph) throws DatabaseException {
6042 return compute(graph, resource);
6045 @SuppressWarnings("unchecked")
6046 public static <T> Function3<ReadGraph,Resource,Object,T> compute(ReadGraph graph, Resource resource) throws DatabaseException {
6047 Layer0 L0 = Layer0.getInstance(graph);
6048 for(Resource converter : graph.getObjects(resource, L0.ConvertsToValueWith)) {
6050 if(L0.Functions_functionApplication.equals(converter)) {
6051 return (Function3<ReadGraph,Resource,Object,T>)graph.syncRequest(new AdaptValue(resource));
6053 return graph.getValue2(converter, resource);
6055 } catch(RuntimeException e) {
6056 DatabaseException dte = findPossibleRootException(e);
6057 if(dte != null) throw dte;
6058 else throw new DatabaseException(e);
6066 <T> Function3<ReadGraph,Resource,Object,T> requestValueFunction(Resource r) throws DatabaseException {
6068 return syncRequest(new PossibleConverterFunction<T>(r), TransientCacheAsyncListener.<Function3<ReadGraph,Resource,Object,T>>instance());
6070 return syncRequest(new PossibleConverterFunction<T>(r));
6074 * Get a value associated with a graph {@link Resource}, using a possible context object and
6075 * a desired value binding. The following methods are tried in order to retreive the value:
6077 * <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>
6078 * <li>If the resource is a {@code L0.ExternalValue}, the value is acquired using
6079 * {@link ReflectionUtils#getValue(String)}.</li>
6080 * <li>If the resource is associated with a suitable value converter with relation {@code L0.ConvertsToValueWith}
6081 * (see {@link #requestValueFunction(Resource)}), the value function is called with the graph, the resource
6082 * and the context object.</li>
6085 * @param r A graph resource with which the value is associated
6086 * @param context A context object that is used for acquiring the value (only applied in case 3)
6087 * @param binding A binding for the value type (only applied in case 1)
6088 * @return The value of the graph node.
6089 * @throws DoesNotContainValueException No value is associated with the graph node.
6090 * @throws DatabaseException Other errors, such as an error in casting the value to the return type or
6091 * a runtime error in the value function.
6093 @SuppressWarnings("unchecked")
6095 public <T> T getValue2(Resource r, Object context, Binding binding) throws DatabaseException {
6096 if (binding instanceof ObjectVariantBinding)
6097 return getValue2(r, context);
6099 Layer0 L0 = processor.getL0(this);
6100 Set<Resource> types = getTypes(r);
6101 if(types.contains(L0.Literal)) {
6102 if(isImmutable(r)) {
6103 return syncRequest(new Value<T>(r, binding));
6105 return getValue(r, binding);
6107 } else if(types.contains(L0.ExternalValue)) {
6108 ComputationalValue cv = syncRequest(new PossibleAdapter<ComputationalValue>(r, ComputationalValue.class), TransientCacheAsyncListener.instance());
6110 return cv.getValue(this, r);
6112 // This should not even be possible since L0 defines an adapter for all values
6114 return (T)ReflectionUtils.getValue(getURI(r)).getValue();
6115 } catch(ValueNotFoundException e) {
6116 throw new DatabaseException(e);
6117 } catch(ClassCastException e) {
6118 throw new DatabaseException(e);
6123 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6124 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function.");
6126 Object value = function.apply(this, r, context);
6127 if(binding.isInstance(value)) return (T)value;
6128 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
6129 return (T)Bindings.adapt(value, srcBinding, binding);
6130 } catch(RuntimeException e) {
6131 DatabaseException dte = findPossibleRootException(e);
6132 if(dte != null) throw dte;
6133 else throw new DatabaseException(e);
6134 } catch (AdaptException e) {
6135 throw new DatabaseException(e);
6136 } catch (org.simantics.databoard.binding.error.BindingException e) {
6137 throw new DatabaseException(e);
6143 public <T> T getPossibleValue2(Resource subject, Object context, Binding binding) throws DatabaseException {
6145 return getValue2(subject, context, binding);
6146 } catch (DatabaseException e) {
6151 private static DatabaseException findPossibleRootException(Throwable t) {
6152 if(t == null) return null;
6153 if(t instanceof DatabaseException) return (DatabaseException)t;
6154 if(t instanceof RuntimeException || t instanceof InvocationTargetException) {
6155 return findPossibleRootException(t.getCause());
6161 public <T> T getRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6162 return getRelatedValue2(subject, relation, subject);
6166 public Variant getRelatedVariantValue2(Resource subject, Resource relation) throws DatabaseException {
6167 return getRelatedVariantValue2(subject, relation, subject);
6171 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6173 Resource object = getPossibleObject(subject, relation);
6174 if(object == null) return null;
6175 else return getValue2(object, subject);
6176 } catch (DatabaseException e) {
6182 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6183 if(Development.DEVELOPMENT) {
6184 if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
6185 String error = L0Validations.checkValueType(this, subject, relation);
6187 Logger.defaultLogError(new ValidationException(error));
6188 throw new ValidationException(error);
6192 return getValue2(getSingleObject(subject, relation), context);
6196 public Variant getRelatedVariantValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6197 if(Development.DEVELOPMENT) {
6198 if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
6199 String error = L0Validations.checkValueType(this, subject, relation);
6201 Logger.defaultLogError(new ValidationException(error));
6202 throw new ValidationException(error);
6206 return getVariantValue2(getSingleObject(subject, relation), context);
6210 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6212 Resource object = getPossibleObject(subject, relation);
6213 if(object == null) return null;
6214 else return getValue2(object, context);
6215 } catch (DatabaseException e) {
6221 public <T> T getRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6222 return getRelatedValue2(subject, relation, subject, binding);
6226 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6228 Resource object = getPossibleObject(subject, relation);
6229 if(object == null) return null;
6230 return getValue2(object, subject, binding);
6231 } catch (DatabaseException e) {
6237 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6238 return getValue2(getSingleObject(subject, relation), context, binding);
6242 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6244 Resource object = getPossibleObject(subject, relation);
6245 if(object == null) return null;
6246 else return getValue2(object, context, binding);
6247 } catch (DatabaseException e) {
6253 public Type getRelatedValueType(Resource subject, Resource relation) throws DatabaseException {
6254 Layer0 L0 = processor.getL0(this);
6255 Resource property = getSingleObject(subject, relation);
6256 String typeText = (String)getRelatedValue(property, L0.HasValueType, Bindings.STRING);
6258 return org.simantics.scl.compiler.types.Types.parseType(typeText);
6259 } catch (SCLTypeParseException e) {
6260 throw new DatabaseException(e);
6264 private static ThreadLocal<Boolean> syncGraph = new ThreadLocal<Boolean>() {
6265 protected Boolean initialValue() {
6271 public boolean setSynchronous(boolean value) {
6272 boolean old = getSynchronous();
6273 syncGraph.set(value);
6278 public boolean getSynchronous() {
6279 return syncGraph.get();
6282 public void ensureLoaded(int resource) {
6283 processor.querySupport.ensureLoaded(this, resource);
6286 public void ensureLoaded(int resource, int predicate) {
6287 processor.querySupport.ensureLoaded(this, resource, predicate);
6290 public byte[] getValue(int resource) {
6291 return processor.querySupport.getValue(this, resource);
6294 public int thread(int resource) {
6295 return (resource >>> 16) & processor.THREAD_MASK;
6298 public int thread(Resource resource) {
6299 return (((ResourceImpl)resource).id >>> 16) & processor.THREAD_MASK;
6302 public ResourceSupport getResourceSupport() {
6303 return processor.getResourceSupport();
6307 public Object getModificationCounter() {
6308 return processor.getSession().getModificationCounter();
6312 public boolean performPending() {
6313 return processor.performPending(this);
6316 public Set<ReadGraphImpl> ancestorSet() {
6317 HashSet<ReadGraphImpl> result = new HashSet<>();
6318 ReadGraphImpl g = this;