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.DevelopmentKeys;
49 import org.simantics.db.ExternalValueSupport;
50 import org.simantics.db.ReadGraph;
51 import org.simantics.db.RelationContext;
52 import org.simantics.db.Resource;
53 import org.simantics.db.Session;
54 import org.simantics.db.Statement;
55 import org.simantics.db.adaption.AdaptionService;
56 import org.simantics.db.common.primitiverequest.Adapter;
57 import org.simantics.db.common.primitiverequest.Builtin;
58 import org.simantics.db.common.primitiverequest.DatatypeBinding;
59 import org.simantics.db.common.primitiverequest.ForEachAssertedObject;
60 import org.simantics.db.common.primitiverequest.ForEachAssertedStatement;
61 import org.simantics.db.common.primitiverequest.HasStatement;
62 import org.simantics.db.common.primitiverequest.HasStatementSubject;
63 import org.simantics.db.common.primitiverequest.HasStatementSubjectObject;
64 import org.simantics.db.common.primitiverequest.HasValue;
65 import org.simantics.db.common.primitiverequest.Inverse;
66 import org.simantics.db.common.primitiverequest.IsInheritedFrom;
67 import org.simantics.db.common.primitiverequest.IsInstanceOf;
68 import org.simantics.db.common.primitiverequest.IsSubrelationOf;
69 import org.simantics.db.common.primitiverequest.OrderedSet;
70 import org.simantics.db.common.primitiverequest.PossibleAdapter;
71 import org.simantics.db.common.primitiverequest.PossibleInverse;
72 import org.simantics.db.common.primitiverequest.PossibleObject;
73 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;
74 import org.simantics.db.common.primitiverequest.PossibleRelatedValueImplied;
75 import org.simantics.db.common.primitiverequest.PossibleStatement;
76 import org.simantics.db.common.primitiverequest.PossibleType;
77 import org.simantics.db.common.primitiverequest.PossibleUniqueAdapter;
78 import org.simantics.db.common.primitiverequest.PossibleValue;
79 import org.simantics.db.common.primitiverequest.PossibleValueImplied;
80 import org.simantics.db.common.primitiverequest.RelatedValue;
81 import org.simantics.db.common.primitiverequest.RelatedValueImplied;
82 import org.simantics.db.common.primitiverequest.SingleObject;
83 import org.simantics.db.common.primitiverequest.SingleStatement;
84 import org.simantics.db.common.primitiverequest.SingleType;
85 import org.simantics.db.common.primitiverequest.SingleTypeAny;
86 import org.simantics.db.common.primitiverequest.Types;
87 import org.simantics.db.common.primitiverequest.UniqueAdapter;
88 import org.simantics.db.common.primitiverequest.Value;
89 import org.simantics.db.common.primitiverequest.ValueImplied;
90 import org.simantics.db.common.primitiverequest.VariantValueImplied;
91 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
92 import org.simantics.db.common.procedure.adapter.AsyncProcedureAdapter;
93 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
94 import org.simantics.db.common.procedure.adapter.SyncMultiProcedureAdapter;
95 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
96 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
97 import org.simantics.db.common.procedure.single.SyncReadProcedure;
98 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrErrorProcedure;
99 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrNullProcedure;
100 import org.simantics.db.common.procedure.single.wrapper.ExceptionToNullProcedure;
101 import org.simantics.db.common.procedure.single.wrapper.NullSingleOrNullProcedure;
102 import org.simantics.db.common.procedure.single.wrapper.SingleFunctionalOrNullProcedure;
103 import org.simantics.db.common.procedure.single.wrapper.SingleOrErrorProcedure;
104 import org.simantics.db.common.procedure.single.wrapper.SingleOrNullProcedure;
105 import org.simantics.db.common.procedure.wrapper.NoneToAsyncListener;
106 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiListener;
107 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiProcedure;
108 import org.simantics.db.common.procedure.wrapper.NoneToAsyncProcedure;
109 import org.simantics.db.common.procedure.wrapper.NoneToAsyncSetProcedure;
110 import org.simantics.db.common.procedure.wrapper.NoneToSyncMultiListener;
111 import org.simantics.db.common.procedure.wrapper.NoneToSyncMultiProcedure;
112 import org.simantics.db.common.procedure.wrapper.SyncToAsyncListener;
113 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiListener;
114 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiProcedure;
115 import org.simantics.db.common.procedure.wrapper.SyncToAsyncProcedure;
116 import org.simantics.db.common.procedure.wrapper.SyncToAsyncSetProcedure;
117 import org.simantics.db.common.request.AdaptValue;
118 import org.simantics.db.common.request.ResourceRead;
119 import org.simantics.db.common.utils.Logger;
120 import org.simantics.db.common.utils.NameUtils;
121 import org.simantics.db.common.validation.L0Validations;
122 import org.simantics.db.exception.AdaptionException;
123 import org.simantics.db.exception.ArgumentException;
124 import org.simantics.db.exception.AssumptionException;
125 import org.simantics.db.exception.BindingException;
126 import org.simantics.db.exception.DatabaseException;
127 import org.simantics.db.exception.DoesNotContainValueException;
128 import org.simantics.db.exception.EmptyResourceException;
129 import org.simantics.db.exception.InternalException;
130 import org.simantics.db.exception.InvalidLiteralException;
131 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
132 import org.simantics.db.exception.NoInverseException;
133 import org.simantics.db.exception.NoSingleResultException;
134 import org.simantics.db.exception.ResourceNotFoundException;
135 import org.simantics.db.exception.ServiceException;
136 import org.simantics.db.exception.ValidationException;
137 import org.simantics.db.impl.RelationContextImpl;
138 import org.simantics.db.impl.ResourceImpl;
139 import org.simantics.db.impl.internal.RandomAccessValueSupport;
140 import org.simantics.db.impl.internal.ResourceData;
141 import org.simantics.db.impl.procedure.ResultCallWrappedSyncQueryProcedure;
142 import org.simantics.db.impl.query.CacheEntry;
143 import org.simantics.db.impl.query.QueryCache;
144 import org.simantics.db.impl.query.QueryCacheBase;
145 import org.simantics.db.impl.query.QueryProcessor;
146 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
147 import org.simantics.db.impl.query.QuerySupport;
148 import org.simantics.db.impl.query.TripleIntProcedure;
149 import org.simantics.db.impl.support.ResourceSupport;
150 import org.simantics.db.procedure.AsyncListener;
151 import org.simantics.db.procedure.AsyncMultiListener;
152 import org.simantics.db.procedure.AsyncMultiProcedure;
153 import org.simantics.db.procedure.AsyncProcedure;
154 import org.simantics.db.procedure.AsyncSetListener;
155 import org.simantics.db.procedure.Listener;
156 import org.simantics.db.procedure.ListenerBase;
157 import org.simantics.db.procedure.MultiListener;
158 import org.simantics.db.procedure.MultiProcedure;
159 import org.simantics.db.procedure.Procedure;
160 import org.simantics.db.procedure.SetListener;
161 import org.simantics.db.procedure.StatementProcedure;
162 import org.simantics.db.procedure.SyncListener;
163 import org.simantics.db.procedure.SyncMultiListener;
164 import org.simantics.db.procedure.SyncMultiProcedure;
165 import org.simantics.db.procedure.SyncProcedure;
166 import org.simantics.db.procedure.SyncSetListener;
167 import org.simantics.db.request.AsyncMultiRead;
168 import org.simantics.db.request.AsyncRead;
169 import org.simantics.db.request.DelayedWrite;
170 import org.simantics.db.request.DelayedWriteResult;
171 import org.simantics.db.request.ExternalRead;
172 import org.simantics.db.request.MultiRead;
173 import org.simantics.db.request.Read;
174 import org.simantics.db.request.ReadInterface;
175 import org.simantics.db.request.Write;
176 import org.simantics.db.request.WriteInterface;
177 import org.simantics.db.request.WriteOnly;
178 import org.simantics.db.request.WriteOnlyResult;
179 import org.simantics.db.request.WriteResult;
180 import org.simantics.layer0.Layer0;
181 import org.simantics.scl.compiler.types.Type;
182 import org.simantics.scl.compiler.types.exceptions.SCLTypeParseException;
183 import org.simantics.scl.reflection.ReflectionUtils;
184 import org.simantics.scl.reflection.ValueNotFoundException;
185 import org.simantics.scl.runtime.function.Function3;
186 import org.simantics.utils.DataContainer;
187 import org.simantics.utils.Development;
188 import org.simantics.utils.datastructures.Pair;
189 import org.simantics.utils.datastructures.collections.CollectionUtils;
190 import org.slf4j.LoggerFactory;
192 import gnu.trove.map.hash.TObjectIntHashMap;
194 public class ReadGraphImpl implements AsyncReadGraph {
196 private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ReadGraphImpl.class);
198 final static boolean EMPTY_RESOURCE_CHECK = false;
200 final public CacheEntry parent;
201 public final ReadGraphImpl parentGraph;
202 final public QueryProcessor processor;
204 public final AsyncBarrierImpl asyncBarrier;
206 final static Binding DATA_TYPE_BINDING_INTERNAL = Bindings.getBindingUnchecked(Datatype.class);
207 final static Serializer DATA_TYPE_SERIALIZER = Bindings.getSerializerUnchecked(DATA_TYPE_BINDING_INTERNAL);
209 final public static TObjectIntHashMap<String> counters = new TObjectIntHashMap<String>();
211 public static void resetCounters() {
215 public static String listCounters(File file) throws IOException {
217 PrintStream b = new PrintStream(new BufferedOutputStream(new FileOutputStream(file)));
219 for(Pair<String,Integer> p : CollectionUtils.valueSortedEntries(counters)) {
220 b.print(-p.second + " " + p.first + "\n");
225 return "Dumped " + counters.size() + " queries.";
230 * Implementation of the interface ReadGraph
232 final public String getURI(final Resource resource) throws AssumptionException, ValidationException, ServiceException {
234 assert (resource != null);
238 return syncRequest(new org.simantics.db.common.uri.ResourceToURI(resource));
240 } catch (AssumptionException e) {
242 throw new AssumptionException(e);
244 } catch (ValidationException e) {
246 throw new ValidationException(e);
248 } catch (ServiceException e) {
250 throw new ServiceException(e);
252 } catch (DatabaseException e) {
254 throw new ServiceException(INTERNAL_ERROR_STRING, e);
260 final public String getPossibleURI(final Resource resource) throws ValidationException, ServiceException {
262 assert (resource != null);
266 return syncRequest(new org.simantics.db.common.uri.ResourceToPossibleURI(resource));
268 } catch (ValidationException e) {
270 throw new ValidationException(e);
272 } catch (ServiceException e) {
274 throw new ServiceException(e);
276 } catch (DatabaseException e) {
278 throw new ServiceException(INTERNAL_ERROR_STRING, e);
284 final public Resource getResource(final String id)
285 throws ResourceNotFoundException, ValidationException,
292 Integer rid = QueryCache.resultURIToResource(this, id, parent, null);
293 // FIXME: stupid to throw this here and catch and wrap it right away
294 if(rid == 0) throw new ResourceNotFoundException(id);
295 return processor.querySupport.getResource(rid);
297 } catch (ResourceNotFoundException e) {
299 throw new ResourceNotFoundException(id, e);
301 } catch (ValidationException e) {
303 throw new ValidationException(e);
305 } catch (ServiceException e) {
307 throw new ServiceException(e);
309 } catch (DatabaseException e) {
311 throw new ServiceException(INTERNAL_ERROR_STRING, e);
317 final public Resource getPossibleResource(final String id)
318 throws ResourceNotFoundException, ValidationException,
325 return getResource(id);
327 } catch (ResourceNotFoundException e) {
331 } catch (ValidationException e) {
333 throw new ValidationException(e);
335 } catch (ServiceException e) {
337 throw new ServiceException(e);
339 } catch (DatabaseException e) {
341 throw new ServiceException(INTERNAL_ERROR_STRING, e);
348 public Map<String, Resource> getChildren(Resource resource) throws ValidationException, ServiceException {
350 assert (resource != null);
354 int rId = processor.querySupport.getId(resource);
355 return QueryCache.resultChildMap(this, rId, parent, null);
357 } catch (ValidationException e) {
359 throw new ValidationException(e);
361 } catch (ServiceException e) {
363 throw new ServiceException(e);
365 } catch (DatabaseException e) {
367 throw new ServiceException(INTERNAL_ERROR_STRING, e);
373 final public Resource getRootLibrary() {
374 return processor.getRootLibraryResource();
377 final public Resource getBuiltin(final String id)
378 throws ResourceNotFoundException, ServiceException {
384 return syncRequest(new Builtin(id));
386 } catch (ResourceNotFoundException e) {
388 throw new ResourceNotFoundException(id, e);
390 } catch (ServiceException e) {
392 throw new ServiceException(e);
394 } catch (DatabaseException e) {
396 throw new ServiceException(INTERNAL_ERROR_STRING, e);
402 static class StatementReadProcedure extends TIntArrayListInternal implements StatementProcedure {
404 private static Throwable DONE = new Throwable();
406 Throwable exception = null;
408 final ResourceSupport support;
410 public StatementReadProcedure(ResourceSupport support) {
411 this.support = support;
415 public synchronized void execute(AsyncReadGraph graph, int s, int p, int o) {
422 public void finished(AsyncReadGraph graph) {
427 public void exception(AsyncReadGraph graph, Throwable t) {
431 public void checkAndThrow() throws DatabaseException {
432 if(exception != DONE) {
433 if (exception instanceof DatabaseException)
434 throw (DatabaseException) exception;
436 throw new DatabaseException(
437 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
442 public boolean done() {
443 return exception != null;
447 public boolean contains(Object obj) {
448 if(!(obj instanceof InternalStatement))
450 InternalStatement statement = (InternalStatement)obj;
454 for(int i=0;i<sizeInternal();i+=3)
455 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
460 @SuppressWarnings("unchecked")
462 public <T> T[] toArray(T[] a) {
463 int length = sizeInternal() / 3;
464 if(length > a.length) {
465 Class<?> arrayType = a.getClass();
466 a = (arrayType == Object[].class)
467 ? (T[]) new Object[length]
468 : (T[]) Array.newInstance(arrayType.getComponentType(), length);
471 for(int i=length;i<a.length;++i)
474 for(int i=0,j=0;i<sizeInternal();i+=3,++j)
475 a[j] = (T)new InternalStatement(support, getQuick(i), getQuick(i+1), getQuick(i+2));
480 public boolean add(Statement e) {
481 throw new UnsupportedOperationException();
485 public boolean remove(Object o) {
486 throw new UnsupportedOperationException();
490 public boolean addAll(Collection<? extends Statement> c) {
491 throw new UnsupportedOperationException();
494 class IteratorImpl implements ListIterator<Statement> {
498 public IteratorImpl(int index) {
503 public boolean hasNext() {
504 return index < sizeInternal();
508 public Statement next() {
509 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
515 public void remove() {
516 throw new Error("Not supported");
520 public boolean hasPrevious() {
525 public Statement previous() {
527 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
532 public int nextIndex() {
537 public int previousIndex() {
542 public void set(Statement e) {
543 throw new UnsupportedOperationException();
547 public void add(Statement e) {
548 throw new UnsupportedOperationException();
554 public Iterator<Statement> iterator() {
555 return new IteratorImpl(0);
560 return sizeInternal() / 3;
564 public Object[] toArray() {
565 Object[] result = new Object[sizeInternal() / 3];
566 for(int i=0,j=0;j<sizeInternal();i++,j+=3)
567 result[i] = new InternalStatement(support, getQuick(j), getQuick(j+1), getQuick(j+2));
572 public boolean addAll(int index, Collection<? extends Statement> c) {
573 throw new UnsupportedOperationException();
577 public Statement get(int index) {
579 if(index < 0 || index >= sizeInternal())
580 throw new IndexOutOfBoundsException();
581 return new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
585 public Statement set(int index, Statement element) {
586 throw new UnsupportedOperationException();
590 public void add(int index, Statement element) {
591 throw new UnsupportedOperationException();
595 public Statement remove(int index) {
596 throw new UnsupportedOperationException();
600 public int indexOf(Object obj) {
601 if(!(obj instanceof InternalStatement))
603 InternalStatement statement = (InternalStatement)obj;
607 for(int i=0;i<sizeInternal();i+=3)
608 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
614 public int lastIndexOf(Object obj) {
615 if(!(obj instanceof InternalStatement))
617 InternalStatement statement = (InternalStatement)obj;
621 for(int i=sizeInternal()-3;i>=0;i-=3)
622 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
628 public ListIterator<Statement> listIterator() {
629 return new IteratorImpl(0);
633 public ListIterator<Statement> listIterator(int index) {
634 return new IteratorImpl(index*3);
638 public List<Statement> subList(int fromIndex, int toIndex) {
639 if(fromIndex < 0 || toIndex*3 >= sizeInternal() || fromIndex > toIndex)
640 throw new IndexOutOfBoundsException();
641 return new RandomAccessSubList<Statement>(this, fromIndex, toIndex-fromIndex);
646 final public Collection<Statement> getStatements(final Resource subject,
647 final Resource relation)
648 throws ManyObjectsForFunctionalRelationException, ServiceException {
650 assert (subject != null);
651 assert (relation != null);
655 StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
656 processor.forEachStatement(this, subject, relation, procedure);
657 procedure.checkAndThrow();
660 } catch (DatabaseException e) {
662 System.err.println(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation);
664 StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
665 processor.forEachStatement(this, subject, relation, procedure);
667 return Collections.emptyList();
669 // throw new ServiceException(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation, e);
676 final public Collection<Statement> getAssertedStatements(final Resource subject, final Resource relation)
677 throws ManyObjectsForFunctionalRelationException, ServiceException {
679 assert (subject != null);
680 assert (relation != null);
684 return syncRequest(new ForEachAssertedStatement(subject, relation));
686 } catch (ManyObjectsForFunctionalRelationException e) {
688 throw new ManyObjectsForFunctionalRelationException(e);
690 } catch (ServiceException e) {
692 throw new ServiceException(e);
694 } catch (DatabaseException e) {
696 throw new ServiceException(INTERNAL_ERROR_STRING, e);
703 final public Collection<Resource> getPredicates(final Resource subject) throws ServiceException {
705 assert (subject != null);
709 return processor.getPredicates(this, subject);
711 // AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
712 // processor.forEachPredicate(this, subject, procedure);
713 // procedure.checkAndThrow();
716 } catch (ServiceException e) {
718 throw new ServiceException(e);
720 } catch (DatabaseException e) {
722 throw new ServiceException(INTERNAL_ERROR_STRING, e);
724 } catch (Throwable e) {
726 throw new ServiceException(e);
733 final public Collection<Resource> getPrincipalTypes(final Resource subject)
734 throws ServiceException {
736 assert (subject != null);
740 AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
741 processor.forEachPrincipalType(this, subject, procedure);
742 procedure.checkAndThrow();
745 } catch (ServiceException e) {
747 throw new ServiceException(e);
749 } catch (DatabaseException e) {
751 throw new ServiceException(INTERNAL_ERROR_STRING, e);
758 final public Set<Resource> getTypes(final Resource subject) throws ServiceException {
760 assert (subject != null);
764 return processor.getTypes(this, subject);
766 } catch (ServiceException e) {
768 throw new ServiceException(e);
770 } catch (DatabaseException e) {
772 throw new ServiceException(INTERNAL_ERROR_STRING, e);
774 } catch (Throwable e) {
776 throw new ServiceException(e);
783 final public Set<Resource> getSupertypes(final Resource subject)
784 throws ServiceException {
786 assert (subject != null);
790 SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
791 processor.forSupertypes(this, subject, procedure);
792 procedure.checkAndThrow();
793 return procedure.result;
795 } catch (ServiceException e) {
797 throw new ServiceException(e);
799 } catch (DatabaseException e) {
801 throw new ServiceException(INTERNAL_ERROR_STRING, e);
808 final public Set<Resource> getSuperrelations(final Resource subject)
809 throws ServiceException {
811 assert (subject != null);
815 SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
816 processor.forSuperrelations(this, subject, procedure);
817 procedure.checkAndThrow();
818 return procedure.result;
820 } catch (ServiceException e) {
822 throw new ServiceException(e);
824 } catch (DatabaseException e) {
826 throw new ServiceException(INTERNAL_ERROR_STRING, e);
833 public Resource getPossibleSuperrelation(Resource subject) throws ServiceException {
837 SyncReadProcedure<Resource> procedure = new SyncReadProcedure<Resource>();
838 processor.forPossibleSuperrelation(this, subject, procedure);
839 procedure.checkAndThrow();
840 return procedure.result;
842 } catch (ServiceException e) {
844 throw new ServiceException(e);
846 } catch (DatabaseException e) {
848 throw new ServiceException(INTERNAL_ERROR_STRING, e);
855 final public Collection<Resource> getObjects(final Resource subject, final Resource relation)
856 throws ServiceException {
858 assert (subject != null);
859 assert (relation != null);
861 if(Development.DEVELOPMENT) {
862 if(Development.isTrue(DevelopmentKeys.READGRAPH_COUNT)) {
863 counters.adjustOrPutValue("objects $" + subject.getResourceId() + " $" + relation.getResourceId(), 1, 1);
865 //if(subject.getResourceId()==xx && relation.getResourceId()==xx) new Exception().printStackTrace();
870 AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
871 processor.forEachObject(this, subject, relation, procedure);
872 procedure.checkAndThrow();
875 } catch (DatabaseException e) {
877 throw new ServiceException(INTERNAL_ERROR_STRING, e);
884 final public Collection<Resource> getAssertedObjects(
885 final Resource subject, final Resource relation)
886 throws ManyObjectsForFunctionalRelationException, ServiceException {
889 throw new ArgumentException("Subject must not be null.");
890 if (relation == null)
891 throw new ArgumentException("Relation must not be null. Subject=" + subject);
895 return syncRequest(new ForEachAssertedObject(subject, relation));
897 } catch (ManyObjectsForFunctionalRelationException e) {
899 throw new ManyObjectsForFunctionalRelationException(e);
901 } catch (ServiceException e) {
903 throw new ServiceException(e);
905 } catch (DatabaseException e) {
907 throw new ServiceException(INTERNAL_ERROR_STRING, e);
914 final public Resource getInverse(final Resource relation) throws NoInverseException, ServiceException {
916 assert (relation != null);
920 return getSingleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
922 } catch (NoSingleResultException e) {
924 throw new NoInverseException(e);
926 } catch (ServiceException e) {
928 throw new ServiceException(e);
935 final public Resource getSingleObject(final Resource subject, final Resource relation) throws NoSingleResultException, ServiceException {
937 if( subject == null) throw new IllegalArgumentException("subject can not be null");
938 if( relation == null) throw new IllegalArgumentException("relation can not be null");
941 int single = processor.getSingleObject(this, subject, relation);
943 if (EMPTY_RESOURCE_CHECK) {
944 if (!hasStatement(subject)) {
945 throw new EmptyResourceException("Resource " + debugString(subject));
948 throw new NoSingleResultException("No single object for subject " + debugString(subject)
949 + " and relation " + debugString(relation), single);
951 return processor.querySupport.getResource(single);
952 } catch (NoSingleResultException e) {
954 } catch (DatabaseException e) {
955 throw new ServiceException(e);
960 final public Statement getSingleStatement(final Resource subject, final Resource relation) throws NoSingleResultException, ManyObjectsForFunctionalRelationException, ServiceException {
961 assert (subject != null);
962 assert (relation != null);
964 Collection<Statement> statements = getStatements(subject, relation);
965 if (statements.size() == 1) {
966 return statements.iterator().next();
968 if (EMPTY_RESOURCE_CHECK)
969 if (!hasStatement(subject))
970 throw new EmptyResourceException("Resource " + debugString(subject));
971 throw new NoSingleResultException("No single statement for subject " + debugString(subject)
972 + " and relation " + debugString(relation), statements.size());
974 } catch (ServiceException e) {
975 throw new ServiceException(e);
980 final public Resource getSingleType(final Resource subject) throws NoSingleResultException, ServiceException {
981 assert (subject != null);
983 ArrayList<Resource> principalTypes = (ArrayList<Resource>)getPrincipalTypes(subject);
984 if (principalTypes.size() == 1) {
985 return principalTypes.get(0);
987 throw new NoSingleResultException("No single type for subject " + debugString(subject), principalTypes.size());
989 } catch (ServiceException e) {
990 throw new ServiceException(e);
995 final public Resource getSingleType(final Resource subject,
996 final Resource baseType) throws NoSingleResultException,
999 assert (subject != null);
1000 assert (baseType != null);
1003 return syncRequest(new SingleType(subject, baseType));
1004 } catch (DatabaseException e) {
1005 throw new NoSingleResultException("subject=" + subject + ", baseType=" + baseType, 0, e);
1010 final public <T> T getValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
1012 assert (subject != null);
1016 Layer0 L0 = processor.getL0(this);
1017 int object = processor.getSingleObject(this, subject, L0.HasDataType);
1018 if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
1020 if(processor.isImmutable(object)) {
1021 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1022 return getValue(subject, binding);
1024 byte[] dt = processor.getValue(this, object);
1025 if(dt == null) throw new ServiceException("No data type for " + subject);
1026 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1027 Binding binding = Bindings.getBinding(datatype);
1028 return getValue(subject, binding);
1031 } catch (IOException e) {
1033 throw new ServiceException(e);
1035 } catch (DoesNotContainValueException e) {
1037 throw new DoesNotContainValueException(e, subject);
1039 } catch (ServiceException e) {
1041 throw new ServiceException(e);
1043 } catch (DatabaseException e) {
1045 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1052 final public Variant getVariantValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
1054 assert (subject != null);
1058 Layer0 L0 = processor.getL0(this);
1059 int object = processor.getSingleObject(this, subject, L0.HasDataType);
1060 if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
1062 if(processor.isImmutable(object)) {
1063 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1064 return new Variant(binding, getValue(subject, binding));
1066 byte[] dt = processor.getValue(this, object);
1067 if(dt == null) throw new ServiceException("No data type for " + subject);
1068 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1069 Binding binding = Bindings.getBinding(datatype);
1070 return new Variant(binding, getValue(subject, binding));
1073 } catch (IOException e) {
1075 throw new ServiceException(e);
1077 } catch (DoesNotContainValueException e) {
1079 throw new DoesNotContainValueException(e, subject);
1081 } catch (ServiceException e) {
1083 throw new ServiceException(e);
1085 } catch (DatabaseException e) {
1087 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1092 static final IdentityHashMap<Binding,Serializer> serializers = new IdentityHashMap<Binding,Serializer>();
1095 serializers.put(Bindings.STRING, Bindings.STRING.serializer());
1098 final protected Serializer getSerializer(Binding binding) {
1099 return binding.serializer();
1103 final public <T> T getValue(final Resource subject, final Binding binding) throws DoesNotContainValueException, BindingException,
1106 assert (subject != null);
1108 byte[] bytes = null;
1111 bytes = processor.getValue(this, subject);
1112 if (bytes == null) throw new DoesNotContainValueException("No value for resource " + subject);
1114 Serializer serializer = getSerializer(binding);
1115 return (T)serializer.deserialize(bytes);
1117 } catch (DoesNotContainValueException e) {
1119 throw new DoesNotContainValueException(e);
1121 } catch (Throwable t) {
1122 throw new ServiceException("Could not getValue for subject " + debugString(subject) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(bytes), t);
1127 final public <T> T getRelatedValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1128 DoesNotContainValueException, ServiceException {
1130 assert (subject != null);
1131 assert (relation != null);
1134 Resource object = getSingleObject(subject, relation);
1135 return getValue(object);
1136 } catch (NoSingleResultException e) {
1137 throw new NoSingleResultException("No single value found for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1138 } catch (DoesNotContainValueException e) {
1140 Layer0 L0 = processor.getL0(this);
1141 Resource object = getPossibleObject(subject, relation);
1142 if(isInstanceOf(object, L0.Value)) {
1143 if(isInstanceOf(object, L0.Literal)) {
1144 throw new DoesNotContainValueException(e);
1146 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1149 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1151 } catch (DoesNotContainValueException e2) {
1153 } catch (DatabaseException e2) {
1154 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1156 } catch (ServiceException e) {
1157 throw new ServiceException(e);
1162 final public Variant getRelatedVariantValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1163 DoesNotContainValueException, ServiceException {
1165 assert (subject != null);
1166 assert (relation != null);
1169 Resource object = getSingleObject(subject, relation);
1170 return getVariantValue(object);
1171 } catch (NoSingleResultException e) {
1172 throw new NoSingleResultException("No single object for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1173 } catch (DoesNotContainValueException e) {
1175 Layer0 L0 = processor.getL0(this);
1176 Resource object = getPossibleObject(subject, relation);
1177 if(isInstanceOf(object, L0.Value)) {
1178 if(isInstanceOf(object, L0.Literal)) {
1179 throw new DoesNotContainValueException(e);
1181 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1184 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1186 } catch (DoesNotContainValueException e2) {
1188 } catch (DatabaseException e2) {
1189 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1191 } catch (ServiceException e) {
1192 throw new ServiceException(e);
1197 final public <T> T getRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1198 throws NoSingleResultException, DoesNotContainValueException, BindingException, ServiceException {
1200 assert (subject != null);
1201 assert (relation != null);
1204 Resource object = getSingleObject(subject, relation);
1205 return getValue(object, binding);
1206 } catch (NoSingleResultException e) {
1207 String message = "";
1209 String subjectName = NameUtils.getSafeName(this, subject, true);
1210 String relationName = NameUtils.getSafeName(this, relation, true);
1211 message = "Subject: " + subjectName + ", Relation: " + relationName;
1212 } catch (DatabaseException e2) {
1215 throw new NoSingleResultException(message, e.getResultCount(), e);
1216 } catch (DoesNotContainValueException e) {
1217 throw new DoesNotContainValueException(e);
1218 } catch (ServiceException e) {
1219 throw new ServiceException(e);
1224 final public <T> T adapt(final Resource resource, final Class<T> clazz)
1225 throws AdaptionException, ValidationException, ServiceException {
1227 assert (resource != null);
1228 assert (clazz != null);
1232 return syncRequest(new Adapter<T>(resource, clazz));
1234 } catch (AdaptionException e) {
1236 throw new AdaptionException(e);
1238 } catch (ValidationException e) {
1240 throw new ValidationException(e);
1242 } catch (ServiceException e) {
1244 throw new ServiceException(e);
1246 } catch (DatabaseException e) {
1248 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1255 final public <T,C> T adaptContextual(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1256 throws AdaptionException, ValidationException, ServiceException {
1258 assert (resource != null);
1259 assert (context != null);
1261 class ContextualAdapter implements AsyncRead<T> {
1263 final private Resource resource;
1264 final private C context;
1265 final private Class<T> clazz;
1268 public int hashCode() {
1269 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1273 final public int threadHash() {
1274 return resource.getThreadHash();
1278 public boolean equals(Object object) {
1281 else if (object == null)
1283 else if (getClass() != object.getClass())
1285 ContextualAdapter r = (ContextualAdapter)object;
1286 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1290 public int getFlags() {
1294 public ContextualAdapter(Resource resource, C context, Class<T> clazz) {
1295 this.resource = resource;
1296 this.context = context;
1301 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1303 final AdaptionService service = getSession().peekService(AdaptionService.class);
1304 if (service == null)
1305 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1307 service.adapt(graph, resource, context, contextClass, clazz, false, procedure);
1312 public String toString() {
1313 return "Adapter for (" + resource + "," + context + ") as " + clazz.getName();
1320 return syncRequest(new ContextualAdapter(resource, context, clazz));
1322 } catch (AdaptionException e) {
1324 throw new AdaptionException(e);
1326 } catch (ValidationException e) {
1328 throw new ValidationException(e);
1330 } catch (ServiceException e) {
1332 throw new ServiceException(e);
1334 } catch (DatabaseException e) {
1336 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1343 final public <T> T adaptRelated(final Resource resource, final Resource relation, final Class<T> clazz)
1344 throws AdaptionException, NoSingleResultException, ValidationException, ServiceException {
1346 assert (resource != null);
1347 assert (clazz != null);
1349 Statement stm = getSingleStatement(resource, relation);
1351 return adaptContextual(stm.getObject(), new RelationContextImpl(resource, stm), RelationContext.class, clazz);
1356 final public <T> T getPossibleRelatedAdapter(final Resource resource, final Resource relation, final Class<T> clazz)
1357 throws ValidationException, ServiceException {
1360 return adaptRelated(resource, relation, clazz);
1361 } catch (DatabaseException e) {
1368 final public <T,C> T getPossibleContextualAdapter(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1369 throws ValidationException, ServiceException {
1371 assert (resource != null);
1372 assert (context != null);
1374 class PossibleContextualAdapter implements AsyncRead<T> {
1376 final private Resource resource;
1377 final private C context;
1378 final private Class<T> clazz;
1381 public int hashCode() {
1382 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1386 final public int threadHash() {
1387 return resource.getThreadHash();
1391 public boolean equals(Object object) {
1394 else if (object == null)
1396 else if (getClass() != object.getClass())
1398 PossibleContextualAdapter r = (PossibleContextualAdapter)object;
1399 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1403 public int getFlags() {
1407 public PossibleContextualAdapter(Resource resource, C context, Class<T> clazz) {
1408 this.resource = resource;
1409 this.context = context;
1414 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1416 final AdaptionService service = getSession().peekService(AdaptionService.class);
1417 if (service == null)
1418 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1420 service.adapt(graph, resource, context, contextClass, clazz, true, procedure);
1425 public String toString() {
1426 return "Possible adapter for (" + resource + "," + context + ") as " + clazz.getName();
1433 return syncRequest(new PossibleContextualAdapter(resource, context, clazz));
1435 } catch (ValidationException e) {
1437 throw new ValidationException(e);
1439 } catch (ServiceException e) {
1441 throw new ServiceException(e);
1443 } catch (DatabaseException e) {
1445 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1452 final public <T> T adaptUnique(final Resource resource, final Class<T> clazz)
1453 throws AdaptionException, ValidationException, ServiceException {
1455 assert (resource != null);
1456 assert (clazz != null);
1460 return syncRequest(new UniqueAdapter<T>(resource, clazz));
1462 } catch (AdaptionException e) {
1464 throw new AdaptionException(e);
1466 } catch (ValidationException e) {
1468 throw new ValidationException(e);
1470 } catch (ServiceException e) {
1472 throw new ServiceException(e);
1474 } catch (DatabaseException e) {
1476 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1483 final public Resource getPossibleInverse(final Resource relation)
1484 throws ServiceException {
1486 assert (relation != null);
1490 return getPossibleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
1492 } catch (ServiceException e) {
1494 throw new ServiceException(e);
1496 } catch (DatabaseException e) {
1498 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1505 public Resource getPossibleObject(final Resource subject, final Resource relation)
1506 throws ManyObjectsForFunctionalRelationException, ServiceException {
1508 assert (subject != null);
1509 assert (relation != null);
1513 int result = processor.getSingleObject(this, subject, relation);
1514 if(result == 0) return null;
1516 return processor.querySupport.getResource(result);
1518 } catch (ManyObjectsForFunctionalRelationException e) {
1520 throw new ManyObjectsForFunctionalRelationException("subject=" + subject + ", relation=" + relation, e);
1522 } catch (DatabaseException e) {
1524 throw new ServiceException(e);
1531 final public Statement getPossibleStatement(final Resource subject, final Resource relation)
1532 throws ManyObjectsForFunctionalRelationException, ServiceException {
1534 assert (subject != null);
1535 assert (relation != null);
1539 Collection<Statement> statements = getStatements(subject, relation);
1540 if(statements.size() == 1) return statements.iterator().next();
1543 } catch (ManyObjectsForFunctionalRelationException e) {
1545 throw new ManyObjectsForFunctionalRelationException("Many objects in " + subject + " for functional relation " + relation);
1547 } catch (ServiceException e) {
1549 throw new ServiceException(e);
1556 final public Resource getPossibleType(final Resource subject, final Resource baseType) throws ServiceException {
1558 assert (subject != null);
1559 assert (baseType != null);
1563 AsyncReadProcedure<Resource> procedure = new AsyncReadProcedure<Resource>();
1564 forPossibleType(subject, baseType, procedure);
1565 procedure.checkAndThrow();
1566 return procedure.result;
1568 } catch (ServiceException e) {
1570 throw new ServiceException(e);
1572 } catch (DatabaseException e) {
1574 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1581 final public <T> T getPossibleValue(final Resource subject) throws ServiceException {
1583 assert (subject != null);
1587 int object = processor.getSingleObject(this, subject, processor.getL0(this).HasDataType);
1588 if(object == 0) return null;
1590 if(processor.isImmutable(object)) {
1591 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1592 return getPossibleValue(subject, binding);
1594 byte[] dt = processor.getValue(this, object);
1595 if(dt == null) return null;
1596 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1597 Binding binding = Bindings.getBinding(datatype);
1598 return getPossibleValue(subject, binding);
1601 } catch (IOException e) {
1603 throw new ServiceException(e);
1605 } catch (ServiceException e) {
1607 throw new ServiceException(e);
1609 } catch (DatabaseException e) {
1611 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1618 final public <T> T getPossibleValue(final Resource subject, final Binding binding) throws BindingException, ServiceException {
1620 assert (subject != null);
1621 assert (binding != null);
1625 byte[] dt = processor.getValue(this, subject);
1626 if(dt == null) return null;
1627 Serializer serializer = getSerializer(binding);
1628 return (T)serializer.deserialize(dt);
1630 } catch (IOException e) {
1632 throw new ServiceException(e);
1634 } catch (BindingException e) {
1636 throw new BindingException(e);
1638 } catch (ServiceException e) {
1640 throw new ServiceException(e);
1642 } catch (DatabaseException e) {
1643 e.printStackTrace();
1644 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1650 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation)
1651 throws ManyObjectsForFunctionalRelationException, ServiceException {
1653 assert (subject != null);
1654 assert (relation != null);
1658 Resource object = getPossibleObject(subject, relation);
1659 if(object == null) return null;
1660 else return getPossibleValue(object);
1662 } catch (ManyObjectsForFunctionalRelationException e) {
1664 throw new ManyObjectsForFunctionalRelationException(e);
1666 } catch (ServiceException e) {
1668 throw new ServiceException(e);
1675 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1676 throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
1678 assert (subject != null);
1679 assert (relation != null);
1680 assert (binding != null);
1684 Resource object = getPossibleObject(subject, relation);
1685 if(object == null) return null;
1686 else return getPossibleValue(object, binding);
1688 } catch (ManyObjectsForFunctionalRelationException e) {
1690 throw new ManyObjectsForFunctionalRelationException(e);
1692 } catch (BindingException e) {
1694 throw new BindingException(e);
1696 } catch (ServiceException e) {
1698 throw new ServiceException(e);
1705 public <T> T getPossibleAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1707 assert (resource != null);
1708 assert (clazz != null);
1712 return syncRequest(new PossibleAdapter<T>(resource, clazz));
1714 } catch (ValidationException e) {
1716 throw new ValidationException(e);
1718 } catch (AdaptionException e) {
1722 } catch (DatabaseException e) {
1724 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1730 public <T> T getPossibleUniqueAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1732 assert (resource != null);
1733 assert (clazz != null);
1737 return syncRequest(new PossibleUniqueAdapter<T>(resource, clazz));
1739 } catch (AdaptionException e) {
1743 } catch (ValidationException e) {
1745 throw new ValidationException(e);
1747 } catch (DatabaseException e) {
1749 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1756 final public boolean isInstanceOf(final Resource resource, final Resource type) throws ServiceException {
1758 assert (resource != null);
1759 assert (type != null);
1761 Set<Resource> resources = getTypes(resource);
1762 // This check was necessary because some of the callers of this method got stuck when the NPE was thrown from here.
1763 if (null == resources)
1766 if(EMPTY_RESOURCE_CHECK) {
1767 if (resources.isEmpty()) {
1768 if(!hasStatement(resource)) throw new EmptyResourceException("Resource " + debugString(resource));
1772 return resources.contains(type);
1777 final public boolean isInheritedFrom(final Resource resource, final Resource type) throws ServiceException {
1779 assert (resource != null);
1780 assert (type != null);
1784 if(resource.equals(type)) return true;
1786 return getSupertypes(resource).contains(type);
1788 } catch (ServiceException e) {
1790 throw new ServiceException(e);
1797 final public boolean isSubrelationOf(final Resource resource, final Resource type) throws ServiceException {
1799 assert (resource != null);
1800 assert (type != null);
1804 if(resource.equals(type)) return true;
1806 return getSuperrelations(resource).contains(type);
1808 } catch (ServiceException e) {
1810 throw new ServiceException(e);
1817 final public boolean hasStatement(final Resource subject) throws ServiceException {
1819 assert (subject != null);
1823 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1824 processor.forHasStatement(this, subject, procedure);
1825 procedure.checkAndThrow();
1826 return procedure.result;
1828 } catch (ServiceException e) {
1830 throw new ServiceException(e);
1832 } catch (DatabaseException e) {
1834 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1841 final public boolean hasStatement(final Resource subject, final Resource relation) throws ServiceException {
1843 assert (subject != null);
1844 assert (relation != null);
1848 Collection<Resource> objects = getObjects(subject, relation);
1849 return !objects.isEmpty();
1851 } catch (ServiceException e) {
1853 throw new ServiceException(e);
1860 final public boolean hasStatement(final Resource subject, final Resource relation, final Resource object) throws ServiceException {
1862 assert (subject != null);
1863 assert (relation != null);
1864 assert (object != null);
1868 for(Resource o : getObjects(subject, relation)) {
1869 if(object.equals(o)) return true;
1874 } catch (ServiceException e) {
1876 throw new ServiceException(e);
1883 final public boolean hasValue(final Resource subject) throws ServiceException {
1885 assert (subject != null);
1889 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1890 processor.forHasValue(this, subject, procedure);
1891 procedure.checkAndThrow();
1892 return procedure.result;
1894 } catch (ServiceException e) {
1896 throw new ServiceException(e);
1898 } catch (DatabaseException e) {
1900 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1906 final AsyncProcedure<?> NONE = new AsyncProcedure<Object>() {
1909 public void execute(AsyncReadGraph graph, Object result) {
1913 public void exception(AsyncReadGraph graph, Throwable throwable) {
1919 * Implementation of the interface RequestProcessor
1923 public <T> T syncRequest(final Read<T> request) throws DatabaseException {
1924 assert (request != null);
1925 return (T)QueryCache.runnerReadEntry(this, request, parent, null, null, true);
1929 public <T> T syncRequest(Read<T> request, SyncListener<T> procedure)
1930 throws DatabaseException {
1931 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
1935 public <T> T syncRequest(Read<T> request, final Listener<T> procedure)
1936 throws DatabaseException {
1937 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
1941 public <T> T syncRequest(final Read<T> request, final AsyncProcedure<T> procedure) throws DatabaseException {
1943 assert (request != null);
1945 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
1947 return QueryCache.resultReadEntry(this, request, parent, listener, procedure);
1952 public <T> T syncRequest(final Read<T> request,
1953 final SyncProcedure<T> procedure) throws DatabaseException {
1954 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
1958 public <T> T syncRequest(Read<T> request, Procedure<T> procedure)
1959 throws DatabaseException {
1960 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
1963 static class AsyncReadProcedure<T> implements AsyncProcedure<T> {
1965 private static Throwable DONE = new Throwable();
1968 Throwable exception = null;
1971 public void execute(AsyncReadGraph graph, T t) {
1977 public void exception(AsyncReadGraph graph, Throwable t) {
1981 public void checkAndThrow() throws DatabaseException {
1982 if(exception != DONE) {
1983 if (exception instanceof DatabaseException)
1984 throw (DatabaseException) exception;
1986 throw new DatabaseException(
1987 "Unexpected exception in ReadGraph.syncRequest(AsyncRead)",
1992 public boolean done() {
1993 return exception != null;
1999 public <T> T syncRequest(final AsyncRead<T> request)
2000 throws DatabaseException {
2002 assert (request != null);
2003 return syncRequest(request, new AsyncProcedureAdapter<>() );
2008 public <T> T syncRequest(AsyncRead<T> request, AsyncListener<T> procedure)
2009 throws DatabaseException {
2010 return syncRequest(request, (AsyncProcedure<T>) procedure);
2014 public <T> T syncRequest(AsyncRead<T> request, SyncListener<T> procedure)
2015 throws DatabaseException {
2016 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
2020 public <T> T syncRequest(AsyncRead<T> request, Listener<T> procedure)
2021 throws DatabaseException {
2022 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
2026 final public <T> T syncRequest(final AsyncRead<T> request,
2027 final AsyncProcedure<T> procedure) throws DatabaseException {
2029 assert (request != null);
2031 ListenerBase listener = getListenerBase(procedure);
2033 // BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, procedure, request);
2034 return (T)QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure, true);
2040 public <T> T syncRequest(AsyncRead<T> request,
2041 final SyncProcedure<T> procedure) throws DatabaseException {
2042 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2046 final public <T> T syncRequest(final AsyncRead<T> request,
2047 final Procedure<T> procedure) throws DatabaseException {
2048 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2052 public <T> Collection<T> syncRequest(final MultiRead<T> request)
2053 throws DatabaseException {
2055 assert (request != null);
2057 final ArrayList<T> result = new ArrayList<T>();
2058 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2060 syncRequest(request, new SyncMultiProcedure<T>() {
2063 public void execute(ReadGraph graph, T t) {
2064 synchronized (result) {
2070 public void finished(ReadGraph graph) {
2074 public void exception(ReadGraph graph, Throwable t) {
2079 public String toString() {
2080 return "syncRequest(MultiRead) -> " + request;
2085 Throwable t = exception.get();
2087 if (t instanceof DatabaseException)
2088 throw (DatabaseException) t;
2090 throw new DatabaseException(
2091 "Unexpected exception in ReadGraph.syncRequest(Read)",
2100 public <T> Collection<T> syncRequest(MultiRead<T> request,
2101 SyncMultiListener<T> procedure) {
2102 return syncRequest(request, (SyncMultiProcedure<T>)procedure);
2106 public <T> Collection<T> syncRequest(MultiRead<T> request,
2107 MultiListener<T> procedure) {
2108 return syncRequest(request, new NoneToSyncMultiListener<T>(procedure));
2112 public <T> Collection<T> syncRequest(MultiRead<T> request,
2113 SyncMultiProcedure<T> procedure) {
2115 assert (request != null);
2117 ListenerBase listener = getListenerBase(procedure);
2119 final ResultCallWrappedSyncQueryProcedure<T> wrapper = new ResultCallWrappedSyncQueryProcedure<T>(procedure);
2121 if (parent != null || listener != null) {
2123 // Object syncParent = request;
2125 // final ReadGraphImpl newGraph = newSync();
2127 processor.query(this, request, parent, wrapper, listener);
2129 // newGraph.waitAsync(syncParent);
2133 // Object syncParent = request;
2135 // final ReadGraphImpl newGraph = newSync();
2138 request.perform(this, wrapper);
2139 } catch (Throwable t) {
2140 wrapper.exception(this, t);
2145 return wrapper.get();
2150 public <T> Collection<T> syncRequest(MultiRead<T> request,
2151 MultiProcedure<T> procedure) {
2152 return syncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
2155 static class AsyncMultiReadProcedure<T> extends ArrayList<T> implements AsyncMultiProcedure<T> {
2157 private static Throwable DONE = new Throwable();
2159 private static final long serialVersionUID = -6494230465108115812L;
2161 Throwable exception = null;
2164 public synchronized void execute(AsyncReadGraph graph, T t) {
2169 public void finished(AsyncReadGraph graph) {
2174 public void exception(AsyncReadGraph graph, Throwable t) {
2178 public void checkAndThrow() throws DatabaseException {
2179 if(exception != DONE) {
2180 if (exception instanceof DatabaseException)
2181 throw (DatabaseException) exception;
2183 throw new DatabaseException(
2184 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
2189 public boolean done() {
2190 return exception != null;
2196 final public <T> Collection<T> syncRequest(AsyncMultiRead<T> request)
2197 throws DatabaseException {
2199 assert (request != null);
2201 final AsyncMultiReadProcedure<T> procedure = new AsyncMultiReadProcedure<T>();
2203 syncRequest(request, procedure);
2205 procedure.checkAndThrow();
2211 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2212 AsyncMultiListener<T> procedure) {
2213 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2217 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2218 SyncMultiListener<T> procedure) {
2219 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2223 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2224 MultiListener<T> procedure) {
2225 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2228 final private <T> void syncRequest(final AsyncMultiRead<T> request,
2229 final AsyncMultiReadProcedure<T> procedure) {
2231 assert (request != null);
2232 assert (procedure != null);
2234 ListenerBase listener = getListenerBase(procedure);
2236 if (parent != null || listener != null) {
2238 // Object syncParent = request;
2240 // final ReadGraphImpl newGraph = newSync();
2242 processor.query(this, request, parent, procedure, listener);
2244 // newGraph.waitAsync(syncParent);
2245 waitAsyncProcedure(procedure);
2249 // Object syncParent = callerThread == Integer.MIN_VALUE ? null
2252 // final ReadGraphImpl newGraph = newSyncAsync(syncParent);
2257 // ReadGraphImpl sync = newSync();
2258 request.perform(this, procedure);
2259 // sync.waitAsync(null);
2260 waitAsyncProcedure(procedure);
2263 } catch (Throwable t) {
2265 waitAsyncProcedure(procedure);
2276 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2277 final AsyncMultiProcedure<T> procedure) {
2279 assert (request != null);
2280 assert (procedure != null);
2282 ListenerBase listener = getListenerBase(procedure);
2284 if (parent != null || listener != null) {
2286 // Object syncParent = request;
2288 // final ReadGraphImpl newGraph = newSync();
2290 processor.query(this, request, parent, procedure, listener);
2292 // newGraph.waitAsync(syncParent);
2296 // Object syncParent = request;
2298 // final ReadGraphImpl newGraph = newSync();
2302 request.perform(this, new AsyncMultiProcedure<T>() {
2305 public void execute(AsyncReadGraph graph, T result) {
2306 procedure.execute(graph, result);
2310 public void finished(AsyncReadGraph graph) {
2311 procedure.finished(graph);
2315 public void exception(AsyncReadGraph graph, Throwable t) {
2316 procedure.exception(graph, t);
2320 public String toString() {
2321 return "syncRequest(AsyncMultiRead) -> " + procedure;
2326 } catch (Throwable t) {
2338 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2339 final SyncMultiProcedure<T> procedure) {
2340 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2344 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2345 final MultiProcedure<T> procedure) {
2346 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2350 public <T> T syncRequest(final ExternalRead<T> request)
2351 throws DatabaseException {
2353 assert (request != null);
2355 return syncRequest(request, new Procedure<T>() {
2358 public void execute(T t) {
2362 public void exception(Throwable t) {
2366 public String toString() {
2367 return "syncRequest(AsyncRead) -> " + request;
2375 public <T> T syncRequest(ExternalRead<T> request, Listener<T> procedure) throws DatabaseException {
2376 return syncRequest(request, (Procedure<T>) procedure);
2380 final public <T> T syncRequest(final ExternalRead<T> request,
2381 final Procedure<T> procedure) throws DatabaseException {
2383 assert (request != null);
2385 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
2386 return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
2391 public void syncRequest(final Write request) throws DatabaseException {
2393 assert (request != null);
2395 throw new DatabaseException(
2396 "Write operations are not supported during read transactions!");
2401 public <T> T syncRequest(final WriteResult<T> request) throws DatabaseException {
2403 assert (request != null);
2405 throw new DatabaseException(
2406 "Write operations are not supported during read transactions!");
2411 public void syncRequest(final DelayedWrite request)
2412 throws DatabaseException {
2414 assert (request != null);
2416 throw new DatabaseException(
2417 "Write operations are not supported during read transactions!");
2422 public <T> T syncRequest(final DelayedWriteResult<T> request) throws DatabaseException {
2424 assert (request != null);
2426 throw new DatabaseException(
2427 "Write operations are not supported during read transactions!");
2432 public void syncRequest(final WriteOnly request) throws DatabaseException {
2434 assert (request != null);
2436 throw new DatabaseException(
2437 "Write operations are not supported during read transactions!");
2442 public <T> T syncRequest(final WriteOnlyResult<T> request) throws DatabaseException {
2444 assert (request != null);
2446 throw new DatabaseException(
2447 "Write operations are not supported during read transactions!");
2452 public <T> void async(ReadInterface<T> r, AsyncProcedure<T> procedure) {
2453 r.request(this, procedure);
2457 public <T> void async(ReadInterface<T> r, Procedure<T> procedure) {
2458 r.request(this, procedure);
2462 public <T> void async(ReadInterface<T> r, SyncProcedure<T> procedure) {
2463 r.request(this, procedure);
2467 public <T> void async(ReadInterface<T> r, AsyncListener<T> procedure) {
2468 r.request(this, procedure);
2472 public <T> void async(ReadInterface<T> r, Listener<T> procedure) {
2473 r.request(this, procedure);
2477 public <T> void async(ReadInterface<T> r, SyncListener<T> procedure) {
2478 r.request(this, procedure);
2482 public <T> T sync(ReadInterface<T> r) throws DatabaseException {
2483 return r.request(this);
2487 public <T> T sync(WriteInterface<T> r) throws DatabaseException {
2488 return r.request(this);
2492 public <T> void async(WriteInterface<T> r, Procedure<T> procedure) {
2493 r.request(this, procedure);
2497 public <T> void async(WriteInterface<T> r) {
2498 r.request(this, new ProcedureAdapter<T>());
2502 * Implementation of the interface AsyncReadGraph
2506 public void forURI(Resource resource, AsyncListener<String> listener) {
2507 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2512 public void forURI(Resource resource, SyncListener<String> listener) {
2513 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2518 public void forURI(Resource resource, Listener<String> listener) {
2519 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2524 final public void forURI(final Resource resource,
2525 final AsyncProcedure<String> procedure) {
2527 assert (resource != null);
2528 assert (procedure != null);
2530 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2536 public void forURI(Resource resource, SyncProcedure<String> procedure) {
2537 forURI(resource, new SyncToAsyncProcedure<String>(procedure));
2541 public void forURI(Resource resource, Procedure<String> procedure) {
2542 forURI(resource, new NoneToAsyncProcedure<String>(procedure));
2546 public void forResource(String id, AsyncListener<Resource> listener) {
2547 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2552 public void forResource(String id, SyncListener<Resource> listener) {
2553 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2558 public void forResource(String id, Listener<Resource> listener) {
2559 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2564 final public void forResource(final String id,
2565 final AsyncProcedure<Resource> procedure) {
2567 assert (id != null);
2568 assert (procedure != null);
2570 processor.forResource(this, id, procedure);
2575 public void forResource(String id, SyncProcedure<Resource> procedure) {
2576 forResource(id, new SyncToAsyncProcedure<Resource>(procedure));
2580 public void forResource(String id, Procedure<Resource> procedure) {
2581 forResource(id, new NoneToAsyncProcedure<Resource>(procedure));
2585 public void forBuiltin(String id, AsyncListener<Resource> listener) {
2586 asyncRequest(new Builtin(id), listener);
2590 public void forBuiltin(String id, SyncListener<Resource> listener) {
2591 asyncRequest(new Builtin(id), listener);
2595 public void forBuiltin(String id, Listener<Resource> listener) {
2596 asyncRequest(new Builtin(id), listener);
2600 final public void forBuiltin(final String id,
2601 final AsyncProcedure<Resource> procedure) {
2603 assert (id != null);
2604 assert (procedure != null);
2606 processor.forBuiltin(this, id, procedure);
2611 public void forBuiltin(String id, SyncProcedure<Resource> procedure) {
2612 forBuiltin(id, new SyncToAsyncProcedure<Resource>(procedure));
2616 public void forBuiltin(String id, Procedure<Resource> procedure) {
2617 forBuiltin(id, new NoneToAsyncProcedure<Resource>(procedure));
2621 final public void forEachStatement(Resource subject, Resource relation,
2622 AsyncMultiProcedure<Statement> procedure) {
2624 assert (subject != null);
2625 assert (relation != null);
2626 assert (procedure != null);
2628 processor.forEachStatement(this, subject, relation, procedure);
2633 public void forEachStatement(Resource subject, Resource relation,
2634 SyncMultiProcedure<Statement> procedure) {
2635 forEachStatement(subject, relation,
2636 new SyncToAsyncMultiProcedure<Statement>(procedure));
2640 final public void forEachStatement(Resource subject, Resource relation,
2641 MultiProcedure<Statement> procedure) {
2643 assert (subject != null);
2644 assert (relation != null);
2645 assert (procedure != null);
2647 processor.forEachStatement(this, subject, relation, procedure);
2652 final public void forStatementSet(Resource subject, Resource relation,
2653 AsyncSetListener<Statement> procedure) {
2655 assert (subject != null);
2656 assert (relation != null);
2657 assert (procedure != null);
2659 processor.forStatementSet(this, subject, relation, procedure);
2664 final public void forStatementSet(Resource subject, Resource relation,
2665 SyncSetListener<Statement> procedure) {
2666 forStatementSet(subject, relation,
2667 new SyncToAsyncSetProcedure<Statement>(procedure));
2671 public void forStatementSet(Resource subject, Resource relation,
2672 SetListener<Statement> listener) {
2673 forStatementSet(subject, relation,
2674 new NoneToAsyncSetProcedure<Statement>(listener));
2678 final public void forEachAssertedStatement(final Resource subject,
2679 final Resource relation,
2680 final AsyncMultiProcedure<Statement> procedure) {
2682 assert (subject != null);
2683 assert (relation != null);
2684 assert (procedure != null);
2686 processor.forEachAssertedStatement(this, subject, relation, procedure);
2691 public void forEachAssertedStatement(Resource subject, Resource relation,
2692 SyncMultiProcedure<Statement> procedure) {
2693 forEachAssertedStatement(subject, relation,
2694 new SyncToAsyncMultiProcedure<Statement>(procedure));
2698 public void forEachAssertedStatement(Resource subject, Resource relation,
2699 MultiProcedure<Statement> procedure) {
2700 forEachAssertedStatement(subject, relation,
2701 new NoneToAsyncMultiProcedure<Statement>(procedure));
2705 public void forAssertedStatementSet(Resource subject, Resource relation,
2706 AsyncSetListener<Statement> procedure) {
2708 assert (subject != null);
2709 assert (relation != null);
2710 assert (procedure != null);
2712 processor.forAssertedStatementSet(this, subject, relation, procedure);
2717 public void forAssertedStatementSet(Resource subject, Resource relation,
2718 SyncSetListener<Statement> procedure) {
2720 assert (subject != null);
2721 assert (relation != null);
2722 assert (procedure != null);
2724 forAssertedStatementSet(subject, relation,
2725 new SyncToAsyncSetProcedure<Statement>(procedure));
2730 public void forAssertedStatementSet(Resource subject, Resource relation,
2731 SetListener<Statement> procedure) {
2733 assert (subject != null);
2734 assert (relation != null);
2735 assert (procedure != null);
2737 forAssertedStatementSet(subject, relation,
2738 new NoneToAsyncSetProcedure<Statement>(procedure));
2743 final public void forEachPredicate(final Resource subject,
2744 final AsyncMultiProcedure<Resource> procedure) {
2746 assert (subject != null);
2747 assert (procedure != null);
2749 processor.forEachPredicate(this, subject, procedure);
2754 public void forEachPredicate(Resource subject,
2755 SyncMultiProcedure<Resource> procedure) {
2756 forEachPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(
2761 final public void forEachPredicate(final Resource subject,
2762 final MultiProcedure<Resource> procedure) {
2764 assert (subject != null);
2765 assert (procedure != null);
2767 processor.forEachPredicate(this, subject, procedure);
2772 final public void forPredicateSet(final Resource subject,
2773 final AsyncSetListener<Resource> procedure) {
2775 assert (subject != null);
2776 assert (procedure != null);
2778 processor.forPredicateSet(this, subject, procedure);
2783 final public void forPredicateSet(final Resource subject,
2784 final SyncSetListener<Resource> procedure) {
2786 assert (subject != null);
2787 assert (procedure != null);
2789 forPredicateSet(subject, new SyncToAsyncSetProcedure<Resource>(
2795 final public void forPredicateSet(final Resource subject,
2796 final SetListener<Resource> procedure) {
2798 assert (subject != null);
2799 assert (procedure != null);
2801 forPredicateSet(subject, new NoneToAsyncSetProcedure<Resource>(
2807 final public void forEachPrincipalType(final Resource subject,
2808 final AsyncMultiProcedure<Resource> procedure) {
2810 assert (subject != null);
2811 assert (procedure != null);
2813 processor.forEachPrincipalType(this, subject, procedure);
2818 public void forEachPrincipalType(Resource subject,
2819 SyncMultiProcedure<Resource> procedure) {
2820 forEachPrincipalType(subject, new SyncToAsyncMultiProcedure<Resource>(
2825 final public void forEachPrincipalType(final Resource subject,
2826 final MultiProcedure<Resource> procedure) {
2828 assert (subject != null);
2829 assert (procedure != null);
2831 processor.forEachPrincipalType(this, subject, procedure);
2836 final public void forPrincipalTypeSet(final Resource subject,
2837 final AsyncSetListener<Resource> procedure) {
2839 assert (subject != null);
2840 assert (procedure != null);
2842 processor.forPrincipalTypeSet(this, subject, procedure);
2847 final public void forPrincipalTypeSet(final Resource subject,
2848 final SyncSetListener<Resource> procedure) {
2850 assert (subject != null);
2851 assert (procedure != null);
2853 forPrincipalTypeSet(subject, new SyncToAsyncSetProcedure<Resource>(
2859 final public void forPrincipalTypeSet(final Resource subject,
2860 final SetListener<Resource> procedure) {
2862 assert (subject != null);
2863 assert (procedure != null);
2865 forPrincipalTypeSet(subject, new NoneToAsyncSetProcedure<Resource>(
2871 public void forTypes(Resource subject, AsyncListener<Set<Resource>> listener) {
2872 asyncRequest(new Types(subject), listener);
2876 public void forTypes(Resource subject, SyncListener<Set<Resource>> listener) {
2877 asyncRequest(new Types(subject), listener);
2881 public void forTypes(Resource subject, Listener<Set<Resource>> listener) {
2882 asyncRequest(new Types(subject), listener);
2886 final public void forTypes(final Resource subject,
2887 final AsyncProcedure<Set<Resource>> procedure) {
2889 assert (subject != null);
2890 assert (procedure != null);
2892 processor.forTypes(this, subject, procedure);
2897 public void forTypes(Resource subject,
2898 SyncProcedure<Set<Resource>> procedure) {
2899 forTypes(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
2903 public void forTypes(Resource subject, Procedure<Set<Resource>> procedure) {
2904 forTypes(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
2908 public void forSupertypes(Resource subject,
2909 AsyncListener<Set<Resource>> listener) {
2910 asyncRequest(new Types(subject), listener);
2914 public void forSupertypes(Resource subject,
2915 SyncListener<Set<Resource>> listener) {
2916 asyncRequest(new Types(subject), listener);
2920 public void forSupertypes(Resource subject, Listener<Set<Resource>> listener) {
2921 asyncRequest(new Types(subject), listener);
2925 final public void forSupertypes(final Resource subject,
2926 final AsyncProcedure<Set<Resource>> procedure) {
2928 assert (subject != null);
2929 assert (procedure != null);
2931 processor.forSupertypes(this, subject, procedure);
2936 public void forSupertypes(Resource subject,
2937 SyncProcedure<Set<Resource>> procedure) {
2938 forSupertypes(subject, new SyncToAsyncProcedure<Set<Resource>>(
2943 public void forSupertypes(Resource subject,
2944 Procedure<Set<Resource>> procedure) {
2945 forSupertypes(subject, new NoneToAsyncProcedure<Set<Resource>>(
2950 public void forDirectSuperrelations(Resource subject,
2951 AsyncMultiProcedure<Resource> procedure) {
2953 assert (subject != null);
2954 assert (procedure != null);
2956 processor.forDirectSuperrelations(this, subject, procedure);
2961 public void forPossibleSuperrelation(Resource subject, AsyncProcedure<Resource> procedure) {
2963 assert (subject != null);
2964 assert (procedure != null);
2966 processor.forPossibleSuperrelation(this, subject, procedure);
2971 public void forSuperrelations(Resource subject,
2972 AsyncListener<Set<Resource>> listener) {
2973 asyncRequest(new Types(subject), listener);
2977 public void forSuperrelations(Resource subject,
2978 SyncListener<Set<Resource>> listener) {
2979 asyncRequest(new Types(subject), listener);
2983 public void forSuperrelations(Resource subject,
2984 Listener<Set<Resource>> listener) {
2985 asyncRequest(new Types(subject), listener);
2989 final public void forSuperrelations(final Resource subject,
2990 final AsyncProcedure<Set<Resource>> procedure) {
2992 assert (subject != null);
2993 assert (procedure != null);
2995 processor.forSuperrelations(this, subject, procedure);
3000 public void forSuperrelations(Resource subject,
3001 SyncProcedure<Set<Resource>> procedure) {
3002 forSuperrelations(subject, new SyncToAsyncProcedure<Set<Resource>>(
3007 public void forSuperrelations(Resource subject,
3008 Procedure<Set<Resource>> procedure) {
3009 forSuperrelations(subject, new NoneToAsyncProcedure<Set<Resource>>(
3014 final public void forEachObject(final Resource subject, final Resource relation, final AsyncMultiProcedure<Resource> procedure) {
3015 processor.forEachObject(this, subject, relation, procedure);
3019 public void forEachObject(Resource subject, Resource relation,
3020 SyncMultiProcedure<Resource> procedure) {
3021 forEachObject(subject, relation,
3022 new SyncToAsyncMultiProcedure<Resource>(procedure));
3026 public void forEachObject(Resource subject, Resource relation,
3027 MultiProcedure<Resource> procedure) {
3029 processor.forEachObject(this, subject, relation, procedure);
3034 final public void forEachDirectPredicate(final Resource subject, final AsyncProcedure<Set<Resource>> procedure) {
3035 processor.forEachDirectPredicate(this, subject, procedure);
3039 final public void forEachDirectPredicate(final Resource subject, final SyncProcedure<Set<Resource>> procedure) {
3040 forEachDirectPredicate(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
3044 public void forEachDirectPredicate(Resource subject, Procedure<Set<Resource>> procedure) {
3045 forEachDirectPredicate(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
3049 final public void forObjectSet(final Resource subject,
3050 final Resource relation, final AsyncSetListener<Resource> procedure) {
3052 assert (subject != null);
3053 assert (relation != null);
3054 assert (procedure != null);
3056 processor.forObjectSet(this, subject, relation, procedure);
3061 final public void forObjectSet(final Resource subject,
3062 final Resource relation, final SyncSetListener<Resource> procedure) {
3064 assert (subject != null);
3065 assert (relation != null);
3066 assert (procedure != null);
3068 forObjectSet(subject, relation, new SyncToAsyncSetProcedure<Resource>(
3074 final public void forObjectSet(final Resource subject,
3075 final Resource relation, final SetListener<Resource> procedure) {
3077 assert (subject != null);
3078 assert (relation != null);
3079 assert (procedure != null);
3081 forObjectSet(subject, relation, new NoneToAsyncSetProcedure<Resource>(
3087 final public void forEachAssertedObject(final Resource subject,
3088 final Resource relation,
3089 final AsyncMultiProcedure<Resource> procedure) {
3091 assert (subject != null);
3092 assert (relation != null);
3093 assert (procedure != null);
3095 processor.forEachAssertedObject(this, subject, relation, procedure);
3100 public void forEachAssertedObject(Resource subject, Resource relation,
3101 SyncMultiProcedure<Resource> procedure) {
3103 assert (subject != null);
3104 assert (relation != null);
3105 assert (procedure != null);
3107 forEachAssertedObject(subject, relation,
3108 new SyncToAsyncMultiProcedure<Resource>(procedure));
3113 public void forEachAssertedObject(Resource subject, Resource relation,
3114 MultiProcedure<Resource> procedure) {
3116 assert (subject != null);
3117 assert (relation != null);
3118 assert (procedure != null);
3120 forEachAssertedObject(subject, relation,
3121 new NoneToAsyncMultiProcedure<Resource>(procedure));
3126 public void forAssertedObjectSet(Resource subject, Resource relation,
3127 AsyncSetListener<Resource> procedure) {
3129 assert (subject != null);
3130 assert (relation != null);
3131 assert (procedure != null);
3133 processor.forAssertedObjectSet(this, subject, relation, procedure);
3138 public void forAssertedObjectSet(Resource subject, Resource relation,
3139 SyncSetListener<Resource> procedure) {
3141 assert (subject != null);
3142 assert (relation != null);
3143 assert (procedure != null);
3145 forAssertedObjectSet(subject, relation,
3146 new SyncToAsyncSetProcedure<Resource>(procedure));
3151 public void forAssertedObjectSet(Resource subject, Resource relation,
3152 SetListener<Resource> procedure) {
3154 assert (subject != null);
3155 assert (relation != null);
3156 assert (procedure != null);
3158 forAssertedObjectSet(subject, relation,
3159 new NoneToAsyncSetProcedure<Resource>(procedure));
3164 public void forInverse(Resource relation, AsyncListener<Resource> listener) {
3165 asyncRequest(new Inverse(relation), listener);
3169 public void forInverse(Resource relation, SyncListener<Resource> listener) {
3170 asyncRequest(new Inverse(relation), listener);
3174 public void forInverse(Resource relation, Listener<Resource> listener) {
3175 asyncRequest(new Inverse(relation), listener);
3179 final public void forInverse(final Resource relation,
3180 final AsyncProcedure<Resource> procedure) {
3182 assert (relation != null);
3183 assert (procedure != null);
3185 processor.forInverse(this, relation, new AsyncProcedure<Resource>() {
3188 public void execute(AsyncReadGraph graph, Resource result) {
3190 procedure.execute(graph, result);
3192 procedure.exception(graph, new NoInverseException(relation
3198 public void exception(AsyncReadGraph graph, Throwable throwable) {
3199 procedure.exception(graph, throwable);
3203 public String toString() {
3204 return "forInverse -> " + procedure;
3212 public void forInverse(Resource relation, SyncProcedure<Resource> procedure) {
3213 forInverse(relation, new SyncToAsyncProcedure<Resource>(procedure));
3217 public void forInverse(Resource relation, Procedure<Resource> procedure) {
3218 forInverse(relation, new NoneToAsyncProcedure<Resource>(procedure));
3222 public void forSingleObject(Resource subject, Resource relation,
3223 AsyncListener<Resource> listener) {
3224 asyncRequest(new SingleObject(subject, relation), listener);
3228 public void forSingleObject(Resource subject, Resource relation,
3229 SyncListener<Resource> listener) {
3230 asyncRequest(new SingleObject(subject, relation), listener);
3234 public void forSingleObject(Resource subject, Resource relation,
3235 Listener<Resource> listener) {
3236 asyncRequest(new SingleObject(subject, relation), listener);
3240 final public void forSingleObject(final Resource subject,
3241 final Resource relation, final AsyncProcedure<Resource> procedure) {
3243 assert (subject != null);
3244 assert (relation != null);
3245 assert (procedure != null);
3247 processor.forEachObject(this, subject, relation,
3248 new SingleOrErrorProcedure<Resource>(procedure));
3253 public void forSingleObject(Resource subject, Resource relation,
3254 SyncProcedure<Resource> procedure) {
3255 forSingleObject(subject, relation, new SyncToAsyncProcedure<Resource>(
3260 public void forSingleObject(Resource subject, Resource relation,
3261 Procedure<Resource> procedure) {
3262 forSingleObject(subject, relation, new NoneToAsyncProcedure<Resource>(
3267 public void forSingleStatement(Resource subject, Resource relation,
3268 AsyncListener<Statement> listener) {
3269 asyncRequest(new SingleStatement(subject, relation), listener);
3273 public void forSingleStatement(Resource subject, Resource relation,
3274 SyncListener<Statement> listener) {
3275 asyncRequest(new SingleStatement(subject, relation), listener);
3279 public void forSingleStatement(Resource subject, Resource relation,
3280 Listener<Statement> listener) {
3281 asyncRequest(new SingleStatement(subject, relation), listener);
3285 final public void forSingleStatement(final Resource subject,
3286 final Resource relation, final AsyncProcedure<Statement> procedure) {
3288 assert (subject != null);
3289 assert (relation != null);
3290 assert (procedure != null);
3292 processor.forEachStatement(this, subject, relation,
3293 new SingleOrErrorProcedure<Statement>(procedure));
3298 public void forSingleStatement(Resource subject, Resource relation,
3299 SyncProcedure<Statement> procedure) {
3300 forSingleStatement(subject, relation,
3301 new SyncToAsyncProcedure<Statement>(procedure));
3305 public void forSingleStatement(Resource subject, Resource relation,
3306 Procedure<Statement> procedure) {
3307 forSingleStatement(subject, relation,
3308 new NoneToAsyncProcedure<Statement>(procedure));
3312 public void forSingleType(Resource subject,
3313 AsyncListener<Resource> listener) {
3314 asyncRequest(new SingleTypeAny(subject), listener);
3318 public void forSingleType(Resource subject,
3319 SyncListener<Resource> listener) {
3320 asyncRequest(new SingleTypeAny(subject), listener);
3324 public void forSingleType(Resource subject,
3325 Listener<Resource> listener) {
3326 asyncRequest(new SingleTypeAny(subject), listener);
3330 final public void forSingleType(final Resource subject, final AsyncProcedure<Resource> procedure) {
3332 assert (subject != null);
3333 assert (procedure != null);
3335 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3337 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
3340 public void execute(AsyncReadGraph graph, final Resource principalType) {
3341 checkedProcedure.offer(graph, principalType);
3345 public void finished(AsyncReadGraph graph) {
3346 checkedProcedure.dec(graph);
3350 public void exception(AsyncReadGraph graph, Throwable t) {
3351 checkedProcedure.exception(graph, t);
3355 public String toString() {
3356 return "forSingleType -> " + procedure;
3364 public void forSingleType(Resource subject, SyncProcedure<Resource> procedure) {
3365 forSingleType(subject, new SyncToAsyncProcedure<Resource>(
3370 public void forSingleType(Resource subject, Procedure<Resource> procedure) {
3371 forSingleType(subject, new NoneToAsyncProcedure<Resource>(
3376 public void forSingleType(Resource subject, Resource relation,
3377 AsyncListener<Resource> listener) {
3378 asyncRequest(new SingleType(subject, relation), listener);
3382 public void forSingleType(Resource subject, Resource relation,
3383 SyncListener<Resource> listener) {
3384 asyncRequest(new SingleType(subject, relation), listener);
3388 public void forSingleType(Resource subject, Resource relation,
3389 Listener<Resource> listener) {
3390 asyncRequest(new SingleType(subject, relation), listener);
3394 final public void forSingleType(final Resource subject,
3395 final Resource baseType, final AsyncProcedure<Resource> procedure) {
3397 assert (subject != null);
3398 assert (procedure != null);
3400 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3402 processor.forEachPrincipalType(this, subject,
3403 new AsyncMultiProcedureAdapter<Resource>() {
3406 public void execute(AsyncReadGraph graph,
3407 final Resource principalType) {
3409 checkedProcedure.inc();
3411 if(baseType == null) {
3413 checkedProcedure.offer(graph, principalType);
3414 checkedProcedure.dec(graph);
3416 } else if(principalType.equals(baseType)) {
3418 checkedProcedure.offer(graph, principalType);
3419 checkedProcedure.dec(graph);
3423 processor.forSupertypes((ReadGraphImpl)graph, principalType,
3424 new AsyncProcedure<Set<Resource>>() {
3427 public void execute(
3428 AsyncReadGraph graph,
3429 Set<Resource> result) {
3431 if (result.contains(baseType))
3432 checkedProcedure.offer(graph,
3434 checkedProcedure.dec(graph);
3439 public void exception(
3440 AsyncReadGraph graph,
3443 .exception(graph, t);
3453 public void finished(AsyncReadGraph graph) {
3454 checkedProcedure.dec(graph);
3458 public void exception(AsyncReadGraph graph, Throwable t) {
3459 checkedProcedure.exception(graph, t);
3463 public String toString() {
3464 return "forSingleType -> " + procedure;
3472 public void forSingleType(Resource subject, Resource relation,
3473 SyncProcedure<Resource> procedure) {
3474 forSingleType(subject, relation, new SyncToAsyncProcedure<Resource>(
3479 public void forSingleType(Resource subject, Resource relation,
3480 Procedure<Resource> procedure) {
3481 forSingleType(subject, relation, new NoneToAsyncProcedure<Resource>(
3486 public <T> void forValue(Resource subject, Binding binding,
3487 AsyncListener<T> listener) {
3488 asyncRequest(new Value<T>(subject, binding), listener);
3492 public <T> void forValue(Resource subject, Binding binding,
3493 SyncListener<T> listener) {
3494 asyncRequest(new Value<T>(subject, binding), listener);
3498 public <T> void forValue(Resource subject, Binding binding,
3499 Listener<T> listener) {
3500 asyncRequest(new Value<T>(subject, binding), listener);
3504 public <T> void forValue(final Resource resource, final Binding binding,
3505 final AsyncProcedure<T> procedure) {
3507 assert (resource != null);
3508 assert (binding != null);
3509 assert (procedure != null);
3511 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
3514 public void execute(AsyncReadGraph graph, byte[] result) {
3518 if (result == null) {
3519 procedure.exception(graph,
3520 new DoesNotContainValueException(
3521 "No value for resource " + resource));
3525 Serializer serializer = binding.serializer();
3526 // Serializer serializer = Bindings.getSerializer( binding );
3527 Object obj = serializer.deserialize(result);
3528 // if (!binding.isInstance(obj))
3529 // procedure.exception(graph, new ClassCastException(
3530 // "Cannot get value " + obj + " with binding "
3533 procedure.execute(graph, (T) obj);
3535 } catch (Throwable t) {
3536 procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
3541 public void exception(AsyncReadGraph graph, Throwable t) {
3543 procedure.exception(graph, t);
3544 } catch (Throwable t2) {
3545 Logger.defaultLogError(t2);
3550 public String toString() {
3551 return "forValue -> " + procedure;
3558 private static String safeArrayToString(byte[] a) {
3561 int iMax = a.length - 1;
3565 StringBuilder b = new StringBuilder();
3567 for (int i = 0; i < 100; i++) { // limit to first 100 items
3570 return b.append(']').toString();
3573 return b.append(", ... (" + a.length + ")]").toString();
3577 public <T> void forValue(Resource subject, Binding binding,
3578 SyncProcedure<T> procedure) {
3579 forValue(subject, binding, new SyncToAsyncProcedure<T>(procedure));
3583 public <T> void forValue(Resource subject, Binding binding,
3584 Procedure<T> procedure) {
3585 forValue(subject, binding, new NoneToAsyncProcedure<T>(procedure));
3589 public <T> void forValue(Resource subject, AsyncListener<T> listener) {
3590 asyncRequest(new ValueImplied<T>(subject), listener);
3594 public <T> void forValue(Resource subject, SyncListener<T> listener) {
3595 asyncRequest(new ValueImplied<T>(subject), listener);
3599 public <T> void forValue(Resource subject, Listener<T> listener) {
3600 asyncRequest(new ValueImplied<T>(subject), listener);
3604 final public <T> void forValue(final Resource subject, final AsyncProcedure<T> procedure) {
3606 assert (subject != null);
3607 assert (procedure != null);
3609 forRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
3612 public void execute(AsyncReadGraph graph, Datatype type) {
3613 // TODO: consider trying Bindings.getBeanBinding(type);
3614 Binding binding = Bindings.getBinding(type);
3615 graph.forValue(subject, binding, procedure);
3619 public void exception(AsyncReadGraph graph, Throwable throwable) {
3620 procedure.exception(graph, new DoesNotContainValueException("Invalid data type", throwable));
3628 public <T> void forValue(Resource subject, SyncProcedure<T> procedure) {
3629 forValue(subject, new SyncToAsyncProcedure<T>(procedure));
3633 public <T> void forValue(Resource subject, Procedure<T> procedure) {
3634 forValue(subject, new NoneToAsyncProcedure<T>(procedure));
3638 public <T> void forRelatedValue(Resource subject, Resource relation,
3639 AsyncListener<T> listener) {
3640 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3644 public <T> void forRelatedValue(Resource subject, Resource relation,
3645 SyncListener<T> listener) {
3646 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3650 public <T> void forRelatedValue(Resource subject, Resource relation,
3651 Listener<T> listener) {
3652 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3656 final public <T> void forRelatedValue(final Resource subject,
3657 final Resource relation, final AsyncProcedure<T> procedure) {
3659 assert (subject != null);
3660 assert (relation != null);
3661 assert (procedure != null);
3663 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3665 processor.forEachObject(this, subject, relation,
3666 new AsyncMultiProcedureAdapter<Resource>() {
3669 public void execute(AsyncReadGraph graph,
3670 final Resource object) {
3672 checkedProcedure.inc();
3674 graph.forValue(object, new AsyncProcedure<Object>() {
3677 public void execute(AsyncReadGraph graph,
3679 checkedProcedure.offer(graph, (T) result);
3680 checkedProcedure.dec(graph);
3684 public void exception(AsyncReadGraph graph,
3686 checkedProcedure.exception(graph, t);
3690 public String toString() {
3691 return "forRelatedValue -> " + procedure;
3699 public void finished(AsyncReadGraph graph) {
3700 checkedProcedure.dec(graph);
3704 public void exception(AsyncReadGraph graph, Throwable t) {
3705 checkedProcedure.exception(graph, t);
3713 public <T> void forRelatedValue(Resource subject, Resource relation,
3714 SyncProcedure<T> procedure) {
3715 forRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
3720 public <T> void forRelatedValue(Resource subject, Resource relation,
3721 Procedure<T> procedure) {
3722 forRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
3727 public <T> void forRelatedValue(Resource subject, Resource relation,
3728 Binding binding, AsyncListener<T> listener) {
3729 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3733 public <T> void forRelatedValue(Resource subject, Resource relation,
3734 Binding binding, SyncListener<T> listener) {
3735 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3739 public <T> void forRelatedValue(Resource subject, Resource relation,
3740 Binding binding, Listener<T> listener) {
3741 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3745 final public <T> void forRelatedValue(final Resource subject,
3746 final Resource relation, final Binding binding,
3747 final AsyncProcedure<T> procedure) {
3749 assert (subject != null);
3750 assert (relation != null);
3751 assert (binding != null);
3752 assert (procedure != null);
3754 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3756 processor.forEachObject(this, subject, relation,
3757 new AsyncMultiProcedureAdapter<Resource>() {
3760 public void execute(AsyncReadGraph graph,
3761 final Resource object) {
3763 checkedProcedure.inc();
3765 graph.forValue(object, binding, new AsyncProcedure<Object>() {
3768 public void execute(AsyncReadGraph graph,
3771 checkedProcedure.offer(graph,
3773 checkedProcedure.dec(graph);
3777 public void exception(AsyncReadGraph graph,
3779 checkedProcedure.exception(graph, t);
3783 public String toString() {
3784 return "forRelatedValue -> "
3793 public void finished(AsyncReadGraph graph) {
3794 checkedProcedure.dec(graph);
3798 public void exception(AsyncReadGraph graph, Throwable t) {
3799 checkedProcedure.exception(graph, t);
3807 public <T> void forRelatedValue(Resource subject, Resource relation,
3808 Binding binding, SyncProcedure<T> procedure) {
3809 forRelatedValue(subject, relation, binding,
3810 new SyncToAsyncProcedure<T>(procedure));
3814 public <T> void forRelatedValue(Resource subject, Resource relation,
3815 Binding binding, Procedure<T> procedure) {
3816 forRelatedValue(subject, relation, binding,
3817 new NoneToAsyncProcedure<T>(procedure));
3821 public <T> void forAdapted(Resource resource, Class<T> clazz,
3822 AsyncListener<T> listener) {
3823 asyncRequest(new Adapter<T>(resource, clazz), listener);
3827 public <T> void forAdapted(Resource resource, Class<T> clazz,
3828 SyncListener<T> listener) {
3829 asyncRequest(new Adapter<T>(resource, clazz), listener);
3833 public <T> void forAdapted(Resource resource, Class<T> clazz,
3834 Listener<T> listener) {
3835 asyncRequest(new Adapter<T>(resource, clazz), listener);
3839 final public <T> void forAdapted(final Resource resource,
3840 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3842 assert (resource != null);
3843 assert (clazz != null);
3844 assert (procedure != null);
3846 final AdaptionService service = getSession().peekService(AdaptionService.class);
3847 if (service == null)
3848 procedure.exception(this, new ServiceException("No AdaptionService available"));
3850 service.adapt(this, resource, resource, Resource.class, clazz, false, procedure);
3855 public <T> void forAdapted(Resource resource, Class<T> clazz,
3856 SyncProcedure<T> procedure) {
3857 forAdapted(resource, clazz, new SyncToAsyncProcedure<T>(procedure));
3861 public <T> void forAdapted(Resource resource, Class<T> clazz,
3862 Procedure<T> procedure) {
3863 forAdapted(resource, clazz, new NoneToAsyncProcedure<T>(procedure));
3867 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3868 AsyncListener<T> listener) {
3869 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3873 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3874 SyncListener<T> listener) {
3875 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3879 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3880 Listener<T> listener) {
3881 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3885 final public <T> void forUniqueAdapted(final Resource resource,
3886 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3888 assert (resource != null);
3889 assert (clazz != null);
3890 assert (procedure != null);
3892 final AdaptionService service = getSession().peekService(AdaptionService.class);
3893 if (service == null)
3894 procedure.exception(this, new ServiceException("No AdaptionService available"));
3896 service.adaptNew(this, resource, clazz, false, procedure);
3901 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3902 SyncProcedure<T> procedure) {
3903 forUniqueAdapted(resource, clazz,
3904 new SyncToAsyncProcedure<T>(procedure));
3908 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3909 Procedure<T> procedure) {
3910 forUniqueAdapted(resource, clazz,
3911 new NoneToAsyncProcedure<T>(procedure));
3915 public void forPossibleInverse(Resource subject,
3916 AsyncListener<Resource> listener) {
3917 asyncRequest(new PossibleInverse(subject), listener);
3921 public void forPossibleInverse(Resource subject,
3922 SyncListener<Resource> listener) {
3923 asyncRequest(new PossibleInverse(subject), listener);
3927 public void forPossibleInverse(Resource subject, Listener<Resource> listener) {
3928 asyncRequest(new PossibleInverse(subject), listener);
3932 final public void forPossibleInverse(final Resource relation,
3933 final AsyncProcedure<Resource> procedure) {
3935 assert (relation != null);
3936 assert (procedure != null);
3938 processor.forInverse(this, relation, new ExceptionToNullProcedure<Resource>(procedure));
3943 public void forPossibleInverse(Resource subject,
3944 SyncProcedure<Resource> procedure) {
3945 forPossibleInverse(subject, new SyncToAsyncProcedure<Resource>(
3950 public void forPossibleInverse(Resource subject,
3951 Procedure<Resource> procedure) {
3952 forPossibleInverse(subject, new NoneToAsyncProcedure<Resource>(
3957 public void forPossibleObject(Resource subject, Resource relation,
3958 AsyncListener<Resource> listener) {
3959 asyncRequest(new PossibleObject(subject, relation), listener);
3963 public void forPossibleObject(Resource subject, Resource relation,
3964 SyncListener<Resource> listener) {
3965 asyncRequest(new PossibleObject(subject, relation), listener);
3969 public void forPossibleObject(Resource subject, Resource relation,
3970 Listener<Resource> listener) {
3971 asyncRequest(new PossibleObject(subject, relation), listener);
3975 final public void forPossibleObject(final Resource subject,
3976 final Resource relation, final AsyncProcedure<Resource> procedure) {
3978 assert (subject != null);
3979 assert (relation != null);
3980 assert (procedure != null);
3982 processor.forEachObject(this, subject, relation,
3983 new SingleOrNullProcedure<Resource>(procedure));
3988 public void forPossibleObject(Resource subject, Resource relation,
3989 SyncProcedure<Resource> procedure) {
3990 forPossibleObject(subject, relation,
3991 new SyncToAsyncProcedure<Resource>(procedure));
3995 public void forPossibleObject(Resource subject, Resource relation,
3996 Procedure<Resource> procedure) {
3997 forPossibleObject(subject, relation,
3998 new NoneToAsyncProcedure<Resource>(procedure));
4002 public void forPossibleStatement(Resource subject, Resource relation,
4003 AsyncListener<Statement> listener) {
4004 asyncRequest(new PossibleStatement(subject, relation), listener);
4008 public void forPossibleStatement(Resource subject, Resource relation,
4009 SyncListener<Statement> listener) {
4010 asyncRequest(new PossibleStatement(subject, relation), listener);
4014 public void forPossibleStatement(Resource subject, Resource relation,
4015 Listener<Statement> listener) {
4016 asyncRequest(new PossibleStatement(subject, relation), listener);
4020 final public void forPossibleStatement(final Resource subject,
4021 final Resource relation, final AsyncProcedure<Statement> procedure) {
4023 assert (subject != null);
4024 assert (relation != null);
4025 assert (procedure != null);
4027 processor.forEachStatement(this, subject, relation,
4028 new SingleFunctionalOrNullProcedure<Statement>(
4029 "forPossibleStatement", procedure));
4034 public void forPossibleStatement(Resource subject, Resource relation,
4035 SyncProcedure<Statement> procedure) {
4036 forPossibleStatement(subject, relation,
4037 new SyncToAsyncProcedure<Statement>(procedure));
4041 public void forPossibleStatement(Resource subject, Resource relation,
4042 Procedure<Statement> procedure) {
4043 forPossibleStatement(subject, relation,
4044 new NoneToAsyncProcedure<Statement>(procedure));
4048 public void forPossibleType(Resource subject, Resource relation,
4049 AsyncListener<Resource> listener) {
4050 asyncRequest(new PossibleType(subject, relation), listener);
4054 public void forPossibleType(Resource subject, Resource relation,
4055 SyncListener<Resource> listener) {
4056 asyncRequest(new PossibleType(subject, relation), listener);
4060 public void forPossibleType(Resource subject, Resource relation,
4061 Listener<Resource> listener) {
4062 asyncRequest(new PossibleType(subject, relation), listener);
4066 final public void forPossibleType(final Resource subject,
4067 final Resource baseType, final AsyncProcedure<Resource> procedure) {
4069 assert (subject != null);
4070 assert (procedure != null);
4072 final NullSingleOrNullProcedure<Resource> checkedProcedure = new NullSingleOrNullProcedure<Resource>(procedure);
4074 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
4077 public void execute(AsyncReadGraph graph,
4078 final Resource principalType) {
4080 if (baseType == null) {
4082 checkedProcedure.offer(graph, principalType);
4084 } else if (principalType.equals(baseType)) {
4086 checkedProcedure.offer(graph, principalType);
4090 checkedProcedure.inc();
4092 processor.forSupertypes((ReadGraphImpl)graph, principalType,
4093 new AsyncProcedure<Set<Resource>>() {
4096 public void execute(
4097 AsyncReadGraph graph,
4098 Set<Resource> result) {
4100 if (result.contains(baseType)) {
4101 checkedProcedure.offer(graph,
4105 checkedProcedure.dec(graph);
4110 public void exception(
4111 AsyncReadGraph graph,
4113 checkedProcedure.exception(graph, t);
4114 checkedProcedure.dec(graph);
4118 public String toString() {
4119 return "forPossibleType -> "
4130 public void finished(AsyncReadGraph graph) {
4131 checkedProcedure.dec(graph);
4135 public void exception(AsyncReadGraph graph, Throwable t) {
4136 checkedProcedure.exception(graph, t);
4137 checkedProcedure.dec(graph);
4145 public void forPossibleType(Resource subject, Resource relation,
4146 SyncProcedure<Resource> procedure) {
4147 forPossibleType(subject, relation, new SyncToAsyncProcedure<Resource>(
4152 public void forPossibleType(Resource subject, Resource relation,
4153 Procedure<Resource> procedure) {
4154 forPossibleType(subject, relation, new NoneToAsyncProcedure<Resource>(
4159 public <T> void forPossibleValue(Resource subject, AsyncListener<T> listener) {
4160 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4164 public <T> void forPossibleValue(Resource subject, SyncListener<T> listener) {
4165 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4169 public <T> void forPossibleValue(Resource subject, Listener<T> listener) {
4170 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4174 final public <T> void forPossibleValue(final Resource subject,
4175 final AsyncProcedure<T> procedure) {
4177 assert (subject != null);
4178 assert (procedure != null);
4180 forPossibleRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
4183 public void execute(AsyncReadGraph graph, final Datatype type) {
4185 procedure.execute(graph, null);
4188 // TODO: consider trying Bindings.getBeanBinding(type);
4189 Binding binding = Bindings.getBinding(type);
4190 graph.forPossibleValue(subject, binding, procedure);
4191 } catch (RuntimeBindingConstructionException e) {
4192 procedure.exception(graph, e);
4198 public void exception(AsyncReadGraph graph, Throwable t) {
4199 procedure.exception(graph, t);
4203 public String toString() {
4204 return "forPossibleValue -> " + procedure;
4212 public <T> void forPossibleValue(Resource subject,
4213 SyncProcedure<T> procedure) {
4214 forPossibleValue(subject, new SyncToAsyncProcedure<T>(procedure));
4218 public <T> void forPossibleValue(Resource subject, Procedure<T> procedure) {
4219 forPossibleValue(subject, new NoneToAsyncProcedure<T>(procedure));
4223 public <T> void forPossibleValue(Resource subject, Binding binding,
4224 AsyncListener<T> listener) {
4225 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4229 public <T> void forPossibleValue(Resource subject, Binding binding,
4230 SyncListener<T> listener) {
4231 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4235 public <T> void forPossibleValue(Resource subject, Binding binding,
4236 Listener<T> listener) {
4237 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4241 final public <T> void forPossibleValue(final Resource resource,
4242 final Binding binding, final AsyncProcedure<T> procedure) {
4244 assert (resource != null);
4245 assert (binding != null);
4246 assert (procedure != null);
4248 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
4251 public void execute(AsyncReadGraph graph, byte[] result) {
4255 if (result == null) {
4256 procedure.execute(graph, null);
4260 Serializer serializer = Bindings.getSerializer( binding );
4261 Object obj = serializer.deserialize(result);
4262 if (!binding.isInstance(obj))
4263 procedure.exception(graph, new ClassCastException(
4264 "Cannot get value " + obj + " with binding "
4267 procedure.execute(graph, (T) obj);
4269 } catch (Throwable t) {
4270 procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
4275 public void exception(AsyncReadGraph graph, Throwable t) {
4277 procedure.exception(graph, t);
4278 } catch (Throwable t2) {
4279 Logger.defaultLogError(t2);
4284 public String toString() {
4285 return "forPossibleValue -> " + procedure;
4293 public <T> void forPossibleValue(Resource subject, Binding binding,
4294 SyncProcedure<T> procedure) {
4295 forPossibleValue(subject, binding, new SyncToAsyncProcedure<T>(
4300 public <T> void forPossibleValue(Resource subject, Binding binding,
4301 Procedure<T> procedure) {
4302 forPossibleValue(subject, binding, new NoneToAsyncProcedure<T>(
4307 public <T> void forPossibleRelatedValue(Resource subject,
4308 Resource relation, AsyncListener<T> listener) {
4309 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4314 public <T> void forPossibleRelatedValue(Resource subject,
4315 Resource relation, SyncListener<T> listener) {
4316 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4321 public <T> void forPossibleRelatedValue(Resource subject,
4322 Resource relation, Listener<T> listener) {
4323 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4328 final public <T> void forPossibleRelatedValue(final Resource subject,
4329 final Resource relation, final AsyncProcedure<T> procedure) {
4331 assert (subject != null);
4332 assert (relation != null);
4333 assert (procedure != null);
4335 final DeepSingleOrNullProcedure<T> checkedProcedure = new DeepSingleOrNullProcedure<T>(procedure);
4337 processor.forEachObject(this, subject, relation,
4338 new AsyncMultiProcedureAdapter<Resource>() {
4341 public void execute(AsyncReadGraph graph,
4342 final Resource object) {
4344 checkedProcedure.inc();
4346 graph.forValue(object, new AsyncProcedure<Object>() {
4349 public void execute(AsyncReadGraph graph,
4351 checkedProcedure.offer(graph, (T) result);
4352 checkedProcedure.dec(graph);
4356 public void exception(AsyncReadGraph graph,
4358 checkedProcedure.exception(graph, t);
4359 checkedProcedure.dec(graph);
4367 public void finished(AsyncReadGraph graph) {
4369 checkedProcedure.dec(graph);
4373 public void exception(AsyncReadGraph graph, Throwable t) {
4374 checkedProcedure.exception(graph, t);
4375 checkedProcedure.dec(graph);
4379 public String toString() {
4380 return "forPossibleRelatedValue -> " + procedure;
4387 public <T> void forPossibleRelatedValue(Resource subject,
4388 Resource relation, SyncProcedure<T> procedure) {
4389 forPossibleRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
4394 public <T> void forPossibleRelatedValue(Resource subject,
4395 Resource relation, Procedure<T> procedure) {
4396 forPossibleRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
4401 public <T> void forPossibleRelatedValue(Resource subject,
4402 Resource relation, Binding binding, AsyncListener<T> listener) {
4403 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4408 public <T> void forPossibleRelatedValue(Resource subject,
4409 Resource relation, Binding binding, SyncListener<T> listener) {
4410 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4415 public <T> void forPossibleRelatedValue(Resource subject,
4416 Resource relation, Binding binding, Listener<T> listener) {
4417 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4421 final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4422 final AsyncProcedure<T> procedure) {
4424 assert (subject != null);
4425 assert (relation != null);
4426 assert (procedure != null);
4428 processor.forPossibleObject(this, subject, relation, new AsyncProcedure<Resource>() {
4431 public void execute(AsyncReadGraph graph, Resource object) {
4433 if(object == null) {
4434 procedure.execute(graph, null);
4438 processor.forPossibleValue((ReadGraphImpl)graph, object, new AsyncProcedure<byte[]>() {
4441 public void execute(AsyncReadGraph graph, byte[] bytes) {
4447 Serializer serializer = binding.serializer();
4448 Object obj = serializer.deserialize(bytes);
4449 if (!binding.isInstance(obj)) {
4450 procedure.exception(graph, new ClassCastException("Cannot get value " + obj + " with binding " + binding));
4452 procedure.execute(graph, (T) obj);
4455 } catch (Throwable t) {
4457 procedure.exception(graph, t);
4463 procedure.execute(graph, null);
4470 public void exception(AsyncReadGraph graph, Throwable t) {
4471 procedure.exception(graph, t);
4479 public void exception(AsyncReadGraph graph, Throwable throwable) {
4480 throwable.printStackTrace();
4481 procedure.exception(graph, throwable);
4489 public <T> void forPossibleRelatedValue(Resource subject,
4490 Resource relation, Binding binding, SyncProcedure<T> procedure) {
4491 forPossibleRelatedValue(subject, relation, binding,
4492 new SyncToAsyncProcedure<T>(procedure));
4496 public <T> void forPossibleRelatedValue(Resource subject,
4497 Resource relation, Binding binding, Procedure<T> procedure) {
4498 forPossibleRelatedValue(subject, relation, binding,
4499 new NoneToAsyncProcedure<T>(procedure));
4503 public void forIsInstanceOf(Resource subject, Resource relation,
4504 AsyncListener<Boolean> listener) {
4505 asyncRequest(new IsInstanceOf(subject, relation), listener);
4509 public void forIsInstanceOf(Resource subject, Resource relation,
4510 SyncListener<Boolean> listener) {
4511 asyncRequest(new IsInstanceOf(subject, relation), listener);
4515 public void forIsInstanceOf(Resource subject, Resource relation,
4516 Listener<Boolean> listener) {
4517 asyncRequest(new IsInstanceOf(subject, relation), listener);
4521 final public void forIsInstanceOf(final Resource resource,
4522 final Resource type, final AsyncProcedure<Boolean> procedure) {
4524 assert (resource != null);
4525 assert (type != null);
4526 assert (procedure != null);
4528 forTypes(resource, new AsyncProcedure<Set<Resource>>() {
4531 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4534 if (result.contains(type))
4535 procedure.execute(graph, true);
4537 procedure.execute(graph, false);
4538 } catch (Throwable t) {
4539 Logger.defaultLogError(t);
4544 public void exception(AsyncReadGraph graph, Throwable t) {
4546 procedure.exception(graph, t);
4547 } catch (Throwable t2) {
4548 Logger.defaultLogError(t2);
4553 public String toString() {
4554 return "forIsInstanceOf -> " + procedure;
4562 public void forIsInstanceOf(Resource subject, Resource relation,
4563 SyncProcedure<Boolean> procedure) {
4564 forIsInstanceOf(subject, relation, new SyncToAsyncProcedure<Boolean>(
4569 public void forIsInstanceOf(Resource subject, Resource relation,
4570 Procedure<Boolean> procedure) {
4571 forIsInstanceOf(subject, relation, new NoneToAsyncProcedure<Boolean>(
4576 public void forIsInheritedFrom(Resource subject, Resource relation,
4577 AsyncListener<Boolean> listener) {
4578 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4582 public void forIsInheritedFrom(Resource subject, Resource relation,
4583 SyncListener<Boolean> listener) {
4584 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4588 public void forIsInheritedFrom(Resource subject, Resource relation,
4589 Listener<Boolean> listener) {
4590 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4594 final public void forIsInheritedFrom(final Resource resource,
4595 final Resource type, final AsyncProcedure<Boolean> procedure) {
4597 assert (resource != null);
4598 assert (type != null);
4599 assert (procedure != null);
4601 if (resource.equals(type)) {
4603 procedure.execute(this, true);
4604 } catch (Throwable t) {
4605 Logger.defaultLogError(t);
4610 forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
4613 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4615 if (result.contains(type))
4616 procedure.execute(graph, true);
4618 procedure.execute(graph, false);
4619 } catch (Throwable t) {
4620 Logger.defaultLogError(t);
4625 public void exception(AsyncReadGraph graph, Throwable t) {
4627 procedure.exception(graph, t);
4628 } catch (Throwable t2) {
4629 Logger.defaultLogError(t2);
4634 public String toString() {
4635 return "forIsInheritedFrom -> " + procedure;
4643 public void forIsInheritedFrom(Resource subject, Resource relation,
4644 SyncProcedure<Boolean> procedure) {
4645 forIsInheritedFrom(subject, relation,
4646 new SyncToAsyncProcedure<Boolean>(procedure));
4650 public void forIsInheritedFrom(Resource subject, Resource relation,
4651 Procedure<Boolean> procedure) {
4652 forIsInheritedFrom(subject, relation,
4653 new NoneToAsyncProcedure<Boolean>(procedure));
4657 public void forIsSubrelationOf(Resource subject, Resource relation,
4658 AsyncListener<Boolean> listener) {
4659 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4663 public void forIsSubrelationOf(Resource subject, Resource relation,
4664 SyncListener<Boolean> listener) {
4665 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4669 public void forIsSubrelationOf(Resource subject, Resource relation,
4670 Listener<Boolean> listener) {
4671 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4675 final public void forIsSubrelationOf(final Resource resource,
4676 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4678 assert (resource != null);
4679 assert (relation != null);
4680 assert (procedure != null);
4682 if (resource.equals(relation)) {
4683 procedure.execute(this, true);
4687 forSuperrelations(resource, new AsyncProcedure<Set<Resource>>() {
4690 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4692 if (result.contains(relation))
4693 procedure.execute(graph, true);
4695 procedure.execute(graph, false);
4696 } catch (Throwable t) {
4697 Logger.defaultLogError(t);
4702 public void exception(AsyncReadGraph graph, Throwable t) {
4704 procedure.exception(graph, t);
4705 } catch (Throwable t2) {
4706 Logger.defaultLogError(t2);
4711 public String toString() {
4712 return "forIsSubrelationOf -> " + procedure;
4720 public void forIsSubrelationOf(Resource subject, Resource relation,
4721 SyncProcedure<Boolean> procedure) {
4722 forIsSubrelationOf(subject, relation,
4723 new SyncToAsyncProcedure<Boolean>(procedure));
4727 public void forIsSubrelationOf(Resource subject, Resource relation,
4728 Procedure<Boolean> procedure) {
4729 forIsSubrelationOf(subject, relation,
4730 new NoneToAsyncProcedure<Boolean>(procedure));
4734 public void forHasStatement(Resource subject,
4735 AsyncListener<Boolean> listener) {
4736 asyncRequest(new HasStatementSubject(subject), listener);
4740 public void forHasStatement(Resource subject, SyncListener<Boolean> listener) {
4741 asyncRequest(new HasStatementSubject(subject), listener);
4745 public void forHasStatement(Resource subject, Listener<Boolean> listener) {
4746 asyncRequest(new HasStatementSubject(subject), listener);
4750 final public void forHasStatement(final Resource subject,
4751 final AsyncProcedure<Boolean> procedure) {
4753 assert (subject != null);
4754 assert (procedure != null);
4756 processor.forHasStatement(this, subject, procedure);
4761 public void forHasStatement(Resource subject,
4762 SyncProcedure<Boolean> procedure) {
4763 forHasStatement(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4767 public void forHasStatement(Resource subject, Procedure<Boolean> procedure) {
4768 forHasStatement(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4772 public void forHasStatement(Resource subject, Resource relation,
4773 AsyncListener<Boolean> listener) {
4774 asyncRequest(new HasStatement(subject, relation), listener);
4778 public void forHasStatement(Resource subject, Resource relation,
4779 SyncListener<Boolean> listener) {
4780 asyncRequest(new HasStatement(subject, relation), listener);
4784 public void forHasStatement(Resource subject, Resource relation,
4785 Listener<Boolean> listener) {
4786 asyncRequest(new HasStatement(subject, relation), listener);
4790 final public void forHasStatement(final Resource subject,
4791 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4793 assert (subject != null);
4794 assert (relation != null);
4795 assert (procedure != null);
4797 processor.forHasStatement(this, subject, relation, procedure);
4802 public void forHasStatement(Resource subject, Resource relation,
4803 SyncProcedure<Boolean> procedure) {
4804 forHasStatement(subject, relation, new SyncToAsyncProcedure<Boolean>(
4809 public void forHasStatement(Resource subject, Resource relation,
4810 Procedure<Boolean> procedure) {
4811 forHasStatement(subject, relation, new NoneToAsyncProcedure<Boolean>(
4816 public void forHasStatement(Resource subject, Resource relation,
4817 Resource object, AsyncListener<Boolean> listener) {
4818 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4823 public void forHasStatement(Resource subject, Resource relation,
4824 Resource object, SyncListener<Boolean> listener) {
4825 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4830 public void forHasStatement(Resource subject, Resource relation,
4831 Resource object, Listener<Boolean> listener) {
4832 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4837 final public void forHasStatement(final Resource subject,
4838 final Resource relation, final Resource object,
4839 final AsyncProcedure<Boolean> procedure) {
4841 assert (subject != null);
4842 assert (relation != null);
4843 assert (object != null);
4844 assert (procedure != null);
4846 processor.forHasStatement(this, subject, relation, object, procedure);
4851 public void forHasStatement(Resource subject, Resource relation,
4852 Resource object, SyncProcedure<Boolean> procedure) {
4853 forHasStatement(subject, relation, object,
4854 new SyncToAsyncProcedure<Boolean>(procedure));
4858 public void forHasStatement(Resource subject, Resource relation,
4859 Resource object, Procedure<Boolean> procedure) {
4860 forHasStatement(subject, relation, object,
4861 new NoneToAsyncProcedure<Boolean>(procedure));
4865 public void forHasValue(Resource subject, AsyncListener<Boolean> listener) {
4866 asyncRequest(new HasValue(subject), listener);
4870 public void forHasValue(Resource subject, SyncListener<Boolean> listener) {
4871 asyncRequest(new HasValue(subject), listener);
4875 public void forHasValue(Resource subject, Listener<Boolean> listener) {
4876 asyncRequest(new HasValue(subject), listener);
4880 final public void forHasValue(final Resource subject,
4881 final AsyncProcedure<Boolean> procedure) {
4883 assert (subject != null);
4884 assert (procedure != null);
4886 processor.forValue(this, subject, new AsyncProcedure<byte[]>() {
4889 public void execute(AsyncReadGraph graph, byte[] result) {
4892 procedure.execute(graph, false);
4894 procedure.execute(graph, true);
4895 } catch (Throwable t) {
4896 Logger.defaultLogError(t);
4901 public void exception(AsyncReadGraph graph, Throwable t) {
4903 procedure.exception(graph, t);
4904 } catch (Throwable t2) {
4905 Logger.defaultLogError(t2);
4910 public String toString() {
4911 return "forHasValue -> " + procedure;
4919 public void forHasValue(Resource subject, SyncProcedure<Boolean> procedure) {
4920 forHasValue(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4924 public void forHasValue(Resource subject, Procedure<Boolean> procedure) {
4925 forHasValue(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4929 public void forOrderedSet(Resource subject,
4930 AsyncMultiListener<Resource> listener) {
4931 asyncRequest(new OrderedSet(subject), listener);
4935 public void forOrderedSet(Resource subject,
4936 SyncMultiListener<Resource> listener) {
4937 asyncRequest(new OrderedSet(subject), listener);
4941 public void forOrderedSet(Resource subject, MultiListener<Resource> listener) {
4942 asyncRequest(new OrderedSet(subject), listener);
4946 final public void forOrderedSet(final Resource subject,
4947 final AsyncMultiProcedure<Resource> procedure) {
4949 assert (subject != null);
4950 assert (procedure != null);
4952 processor.forOrderedSet(this, subject,
4953 new AsyncMultiProcedure<Resource>() {
4956 public void finished(AsyncReadGraph graph) {
4958 procedure.finished(graph);
4959 } catch (Throwable t) {
4960 Logger.defaultLogError(t);
4965 public void execute(AsyncReadGraph graph, Resource result) {
4967 procedure.execute(graph, result);
4968 } catch (Throwable t) {
4969 Logger.defaultLogError(t);
4974 public void exception(AsyncReadGraph graph, Throwable t) {
4976 procedure.exception(graph, t);
4977 } catch (Throwable t2) {
4978 Logger.defaultLogError(t2);
4983 public String toString() {
4984 return "forOrderedSet -> " + procedure;
4992 public void forOrderedSet(Resource subject,
4993 SyncMultiProcedure<Resource> procedure) {
4994 forOrderedSet(subject, new SyncToAsyncMultiProcedure<Resource>(
4999 public void forOrderedSet(Resource subject,
5000 MultiProcedure<Resource> procedure) {
5001 forOrderedSet(subject, new NoneToAsyncMultiProcedure<Resource>(
5006 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5007 AsyncListener<T> listener) {
5008 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5012 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5013 SyncListener<T> listener) {
5014 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5018 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5019 Listener<T> listener) {
5020 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5024 final public <T> void forPossibleAdapted(final Resource resource,
5025 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5027 assert (resource != null);
5028 assert (clazz != null);
5029 assert (procedure != null);
5031 final AdaptionService service = getSession().peekService(AdaptionService.class);
5032 if (service == null)
5033 procedure.exception(this, new ServiceException("No AdaptionService available"));
5035 service.adapt(this, resource, resource, Resource.class, clazz, true, procedure);
5039 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5040 SyncProcedure<T> procedure) {
5041 forPossibleAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5046 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5047 Procedure<T> procedure) {
5048 forPossibleAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5053 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5054 AsyncListener<T> listener) {
5055 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5059 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5060 SyncListener<T> listener) {
5061 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5065 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5066 Listener<T> listener) {
5067 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5071 final public <T> void forPossibleUniqueAdapted(final Resource resource,
5072 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5074 assert (resource != null);
5075 assert (clazz != null);
5076 assert (procedure != null);
5078 final AdaptionService service = getSession().peekService(AdaptionService.class);
5079 if (service == null)
5080 procedure.exception(this, new ServiceException("No AdaptionService available"));
5082 service.adaptNew(this, resource, clazz, true, procedure);
5087 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5088 SyncProcedure<T> procedure) {
5089 forPossibleUniqueAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5094 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5095 Procedure<T> procedure) {
5096 forPossibleUniqueAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5101 * Implementation of the interface AsyncRequestProcessor
5105 final public Session getSession() {
5106 return processor.getSession();
5110 public <T> void asyncRequest(final Read<T> request) {
5112 asyncRequest(request, new AsyncProcedure<T>() {
5115 public void execute(AsyncReadGraph graph, T result) {
5119 public void exception(AsyncReadGraph graph, Throwable t) {
5120 Logger.defaultLogError(t);
5124 public String toString() {
5125 return "asyncRequest(Read) -> " + request;
5133 public <T> void asyncRequest(Read<T> request, AsyncListener<T> procedure) {
5134 asyncRequest(request, (AsyncProcedure<T>) procedure);
5138 public <T> void asyncRequest(Read<T> request,
5139 final SyncListener<T> procedure) {
5140 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5144 public <T> void asyncRequest(Read<T> request, final Listener<T> procedure) {
5145 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5149 public <T> void asyncRequest(final Read<T> request, final AsyncProcedure<T> procedure) {
5151 assert (request != null);
5152 assert (procedure != null);
5154 processor.schedule(new SessionTask(this) {
5157 public void run0(int thread) {
5159 final ListenerBase listener = getListenerBase(procedure);
5160 QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5161 } catch (DatabaseException e) {
5162 Logger.defaultLogError(e);
5170 public static ReadGraphImpl createAsync(QueryProcessor support) {
5171 return new ReadGraphImpl(null, null, support);
5175 public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
5176 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5180 public <T> void asyncRequest(Read<T> request, final Procedure<T> procedure) {
5181 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5185 final public <T> void asyncRequest(final AsyncRead<T> request) {
5187 assert (request != null);
5189 asyncRequest(request, new AsyncProcedure<T>() {
5192 public void execute(AsyncReadGraph graph, T result) {
5196 public void exception(AsyncReadGraph graph, Throwable t) {
5197 Logger.defaultLogError(t);
5201 public String toString() {
5202 return "asyncRequest(AsyncRead) -> " + request;
5210 public <T> void asyncRequest(AsyncRead<T> request,
5211 AsyncListener<T> procedure) {
5212 asyncRequest(request, (AsyncProcedure<T>) procedure);
5216 final public <T> void asyncRequest(AsyncRead<T> request,
5217 final SyncListener<T> procedure) {
5218 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5222 final public <T> void asyncRequest(AsyncRead<T> request,
5223 final Listener<T> procedure) {
5224 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5228 final public <T> void asyncRequest(final AsyncRead<T> request,
5229 final AsyncProcedure<T> procedure) {
5231 assert (request != null);
5232 assert (procedure != null);
5234 processor.schedule(new SessionTask(this) {
5237 public void run0(int thread) {
5239 final ListenerBase listener = getListenerBase(procedure);
5240 QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5241 } catch (DatabaseException e) {
5242 Logger.defaultLogError(e);
5251 public <T> void asyncRequest(AsyncRead<T> request,
5252 SyncProcedure<T> procedure) {
5253 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5257 final public <T> void asyncRequest(final AsyncRead<T> request,
5258 final Procedure<T> procedure) {
5259 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5263 public <T> void asyncRequest(final MultiRead<T> request) {
5265 assert (request != null);
5267 asyncRequest(request, new SyncMultiProcedureAdapter<T>() {
5269 public void exception(ReadGraph graph, Throwable t) {
5270 Logger.defaultLogError(t);
5274 public String toString() {
5275 return "asyncRequest(MultiRead) -> " + request;
5282 public <T> void asyncRequest(MultiRead<T> request,
5283 SyncMultiListener<T> procedure) {
5284 asyncRequest(request, (SyncMultiProcedure<T>)procedure);
5288 public <T> void asyncRequest(MultiRead<T> request,
5289 MultiListener<T> procedure) {
5290 asyncRequest(request, new NoneToSyncMultiListener<T>(procedure));
5295 public <T> void asyncRequest(final MultiRead<T> request,
5296 final SyncMultiProcedure<T> procedure) {
5298 assert (request != null);
5299 assert (procedure != null);
5301 final ListenerBase listener = getListenerBase(procedure);
5303 if (parent != null || listener != null) {
5305 // final ReadGraphImpl newGraph = newSync();
5306 processor.query(this, request, parent, procedure,listener);
5310 // final ReadGraphImpl newGraph = newSync();
5314 request.perform(this, procedure);
5316 } catch (Throwable t) {
5319 procedure.exception(this, t);
5320 } catch (DatabaseException e) {
5321 LOGGER.error("Unexpected exception while handling exception", e);
5331 public <T> void asyncRequest(MultiRead<T> request,
5332 MultiProcedure<T> procedure) {
5333 asyncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
5337 final public <T> void asyncRequest(final AsyncMultiRead<T> request) {
5339 assert (request != null);
5341 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5343 public void exception(AsyncReadGraph graph, Throwable t) {
5344 Logger.defaultLogError(t);
5348 public String toString() {
5349 return "asyncRequest(AsyncMultiRead) -> " + request;
5356 public <T> void asyncRequest(AsyncMultiRead<T> request,
5357 AsyncMultiListener<T> procedure) {
5358 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5362 public <T> void asyncRequest(AsyncMultiRead<T> request,
5363 SyncMultiListener<T> procedure) {
5364 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5368 public <T> void asyncRequest(AsyncMultiRead<T> request,
5369 MultiListener<T> procedure) {
5370 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5374 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5375 final AsyncMultiProcedure<T> procedure) {
5377 assert (request != null);
5378 assert (procedure != null);
5380 ListenerBase listener = getListenerBase(procedure);
5382 if (parent != null || listener != null) {
5384 processor.query(this, request, parent, procedure, listener);
5390 request.perform(this, new AsyncMultiProcedure<T>() {
5393 public void execute(AsyncReadGraph graph, T result) {
5394 procedure.execute(graph, result);
5398 public void finished(AsyncReadGraph graph) {
5399 procedure.finished(graph);
5403 public void exception(AsyncReadGraph graph, Throwable t) {
5404 procedure.exception(graph, t);
5408 public String toString() {
5409 return "asyncRequest(AsyncMultiRead) -> " + procedure;
5414 } catch (Throwable t) {
5416 procedure.exception(this, new DatabaseException(t));
5424 public <T> void asyncRequest(AsyncMultiRead<T> request,
5425 SyncMultiProcedure<T> procedure) {
5426 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5430 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5431 final MultiProcedure<T> procedure) {
5432 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5436 final public <T> void asyncRequest(final ExternalRead<T> request) {
5438 assert (request != null);
5440 asyncRequest(request, new Procedure<T>() {
5443 public void execute(T result) {
5447 public void exception(Throwable t) {
5448 Logger.defaultLogError(t);
5452 public String toString() {
5453 return "asyncRequest(PrimitiveRead) -> " + request;
5461 public <T> void asyncRequest(ExternalRead<T> request,
5462 final Listener<T> procedure) {
5463 asyncRequest(request, (Procedure<T>) procedure);
5467 final public <T> void asyncRequest(final ExternalRead<T> request,
5468 final Procedure<T> procedure) {
5470 assert (request != null);
5471 assert (procedure != null);
5473 final ListenerBase listener = getListenerBase(procedure);
5475 if (parent != null || listener != null) {
5478 QueryCacheBase.resultExternalReadEntry(this, request, parent, listener, procedure);
5479 } catch (DatabaseException e) {
5480 Logger.defaultLogError(e);
5481 // This throwable has already been transferred to procedure at this point - do nothing about it
5486 request.register(this, new Listener<T>() {
5488 public void execute(T result) {
5489 procedure.execute(result);
5493 public void exception(Throwable t) {
5494 procedure.exception(t);
5498 public String toString() {
5499 return "asyncRequest(PrimitiveRead) -> " + request;
5503 public boolean isDisposed() {
5514 public void asyncRequest(final Write request) {
5516 assert (request != null);
5518 getSession().asyncRequest(request);
5520 // processor.asyncWrite(request);
5525 public <T> void asyncRequest(final WriteResult<T> request, Procedure<T> procedure) {
5526 throw new Error("Not implemented.");
5530 public void asyncRequest(Write request, Consumer<DatabaseException> callback) {
5532 assert (request != null);
5534 getSession().asyncRequest(request, callback);
5539 public void asyncRequest(final DelayedWrite request) {
5541 assert (request != null);
5543 getSession().asyncRequest(request);
5548 public <T> void asyncRequest(final DelayedWriteResult<T> request, Procedure<T> procedure) {
5549 throw new Error("Not implemented.");
5553 public void asyncRequest(DelayedWrite r,
5554 Consumer<DatabaseException> callback) {
5555 throw new Error("Not implemented.");
5559 public void asyncRequest(final WriteOnly request) {
5561 assert (request != null);
5563 getSession().asyncRequest(request);
5568 public <T> void asyncRequest(final WriteOnlyResult<T> request, Procedure<T> procedure) {
5569 throw new Error("Not implemented.");
5573 public void asyncRequest(WriteOnly r, Consumer<DatabaseException> callback) {
5574 throw new Error("Not implemented.");
5578 * Implementation of the interface ServiceLocator
5582 public <T> T getService(Class<T> api) {
5583 if(WriteSupport.class == api) {
5584 if(this instanceof WriteGraphImpl) {
5585 WriteGraphImpl impl = (WriteGraphImpl)this;
5586 return (T)impl.writeSupport;
5589 return getSession().getService(api);
5593 public <T> T peekService(Class<T> api) {
5594 return getSession().peekService(api);
5598 public boolean hasService(Class<?> api) {
5599 return getSession().hasService(api);
5603 public <T> void registerService(Class<T> api, T service) {
5604 getSession().registerService(api, service);
5608 public boolean isImmutable(Resource resource) throws DatabaseException {
5609 ResourceImpl impl = (ResourceImpl)resource;
5610 return processor.isImmutable(impl.id);
5617 protected static String INTERNAL_ERROR_STRING = "Transaction aborted due to internal client error.";
5620 * callerThread is the currently running thread state.syncThread is blocking for
5621 * this execution state.syncParent is the blocking request
5624 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support) {
5625 this.parentGraph = parentGraph;
5626 this.parent = parent;
5627 this.processor = support;
5628 this.asyncBarrier = new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent);
5631 ReadGraphImpl(ReadGraphImpl graph, CacheEntry parent) {
5632 this(graph, parent, graph.processor);
5635 ReadGraphImpl(ReadGraphImpl graph) {
5636 this(graph, graph.parent);
5639 public ReadGraphImpl withParent(CacheEntry parent) {
5640 return new ReadGraphImpl(this, parent);
5643 public ReadGraphImpl forRecompute(CacheEntry parent) {
5644 return new ReadGraphImpl(null, parent, processor);
5647 public static ReadGraphImpl create(QueryProcessor support) {
5648 return new ReadGraphImpl(null, null, support);
5651 public ReadGraphImpl newRestart(ReadGraphImpl impl) {
5653 WriteGraphImpl write = processor.getSession().getService(
5654 WriteGraphImpl.class);
5660 final private ListenerBase getListenerBase(final Object procedure) {
5661 if (procedure instanceof ListenerBase)
5662 return (ListenerBase) procedure;
5667 public <T> void waitAsyncProcedure(AsyncMultiReadProcedure<T> procedure) {
5669 assert(procedure.done());
5673 public <T> void waitAsyncProcedure(AsyncReadProcedure<T> procedure) {
5675 assert(procedure.done());
5679 public boolean resumeTasks() {
5680 return processor.resumeTasks(this);
5683 Class<?> singleClass(Set<Resource> types) {
5684 Class<?> result = null;
5685 for (Resource type : types) {
5686 Class<?> clazz = processor.getBuiltinValue(type);
5687 if (clazz != null) {
5697 private String debugString(Resource r) {
5700 name = getPossibleRelatedValue(r, processor.getL0(this).HasName);
5701 } catch (ManyObjectsForFunctionalRelationException e) {
5702 Logger.defaultLogError(e);
5703 } catch (ServiceException e) {
5704 Logger.defaultLogError(e);
5706 return "[" + name + " - " + r + "]";
5710 public String toString() {
5711 return "ReadGraphImpl[thread=" + Thread.currentThread() + "]";
5715 final public int thread() {
5719 static class MultiTripleIntProcedure implements TripleIntProcedure {
5721 final private AsyncMultiProcedure<Statement> procedure;
5722 final private ReadGraphImpl impl;
5723 final private QuerySupport support;
5725 public MultiTripleIntProcedure(AsyncMultiProcedure<Statement> procedure, ReadGraphImpl impl, QuerySupport support) {
5726 this.procedure = procedure;
5728 this.support = support;
5732 public void execute(ReadGraphImpl graph, int s, int p, int o) {
5734 procedure.execute(graph, support.getStatement(s, p, o));
5735 } catch (Throwable t2) {
5736 Logger.defaultLogError(t2);
5741 public void finished(ReadGraphImpl graph) {
5743 procedure.finished(graph);
5744 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5745 } catch (Throwable t2) {
5746 Logger.defaultLogError(t2);
5751 public void exception(ReadGraphImpl graph, Throwable t) {
5753 procedure.exception(graph, t);
5754 } catch (Throwable t2) {
5755 Logger.defaultLogError(t2);
5757 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5761 public String toString() {
5762 return "forEachObject with " + procedure;
5767 // private AsyncMultiProcedure<Resource> cacheKey = null;
5768 // private MultiIntProcedure cacheResult = null;
5770 // final IntProcedure forMultiProcedure(final AsyncMultiProcedure<Resource> procedure) {
5772 // if(procedure == cacheKey) return cacheResult;
5774 // cacheResult = new MultiIntProcedure(procedure, this, processor.support);
5775 // cacheKey = procedure;
5777 // return cacheResult;
5781 // private AsyncMultiProcedure<Statement> cacheKey2 = null;
5782 // private MultiTripleIntProcedure cacheResult2 = null;
5784 // final synchronized TripleIntProcedure forMultiProcedure(final AsyncMultiProcedure<Statement> procedure) {
5786 // if(procedure == cacheKey2) return cacheResult2;
5788 // cacheResult2 = new MultiTripleIntProcedure(procedure, this, processor.support);
5789 // cacheKey2 = procedure;
5791 // return cacheResult2;
5796 public Datatype getDataType(Resource subject) throws DatabaseException {
5797 for(Resource dataTypeResource : getObjects(subject, processor.getL0(this).HasDataType))
5798 return getValue(dataTypeResource, Bindings.getBindingUnchecked(Datatype.class));
5799 throw new DoesNotContainValueException("The literal has no data type.");
5802 protected <T extends Accessor> T getAccessor4File(Resource subject)
5803 throws DatabaseException {
5806 byte[] bytes = processor.support.getValue(g, subject);
5810 BinaryVariant va = (BinaryVariant) Accessors.getAccessor(bytes, Datatypes.VARIANT);
5811 Accessor ca = va.getContentAccessor();
5813 } catch (AccessorConstructionException e) {
5814 throw new DatabaseException(e);
5820 Binding datatype_binding = Bindings.getBindingUnchecked(DataType.class);
5821 Serializer datatype_serializer = datatype_binding.serializer();
5824 BinaryMemory in = new BinaryMemory(ByteBuffer.wrap(bytes));
5825 datatype = (DataType)datatype_serializer.deserialize(in);
5826 Binding data_binding = Bindings.getBinding(datatype);
5827 Serializer data_serializer = data_binding.serializer(BinarySerializationFormat.INSTANCE);
5828 Object o = data_serializer.deserialize(in);
5830 return (T)Accessors.getAccessor(data_binding, o);
5831 } catch(AccessorConstructionException e) {
5834 } catch (Exception e) {
5835 throw new DatabaseException(e);
5838 @SuppressWarnings("unchecked")
5840 public <T extends Accessor> T getAccessor(Resource subject) throws DatabaseException {
5841 RandomAccessBinary rab = getRandomAccessBinary(subject);
5843 return (T)Accessors.getAccessor(rab, getDataType(subject));
5844 } catch(AccessorConstructionException e) {
5845 throw new DatabaseException(e);
5848 @SuppressWarnings("unchecked")
5849 protected <T extends Accessor> T createAccessor(Resource resource, Datatype datatype, Object intialValue)
5850 throws DatabaseException {
5851 RandomAccessBinary rab = createRandomAccessBinary(resource, datatype, intialValue);
5853 return (T)Accessors.getAccessor(rab, datatype);
5854 } catch(AccessorConstructionException e) {
5855 throw new DatabaseException(e);
5859 public RandomAccessBinary getRandomAccessBinary(Resource subject) throws DatabaseException {
5860 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5861 ResourceData rd = ravs.get(subject);
5865 ExternalValueSupport evs = getService(ExternalValueSupport.class);
5866 long size = evs.getValueSize(this, subject); // Throws DatabaseException if no old external value.
5868 File platform = Platform.getLocation().toFile();
5869 File tempFiles = new File(platform, "tempFiles");
5870 File dbDir = new File(tempFiles, "db");
5872 File file = new File(dbDir, "ResourceFile" + subject.getResourceId());
5873 rd = new ResourceData(new BinaryFile(file), true); // Old external value.
5874 final int N = 1<<20;
5878 int length = N < left ? N : (int)left;
5879 byte[] bytes = evs.readValue(this, subject, offset, length);
5880 offset += bytes.length;
5881 left -= bytes.length;
5882 rd.binaryFile.write(bytes);
5884 ravs.put(subject, rd);
5886 } catch (Exception e) {
5887 throw new DatabaseException("Resource " + subject + " have value but there was problem with accessing value data.", e);
5889 } catch (Exception e) {
5890 if(Development.DEVELOPMENT) {
5891 if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
5892 e.printStackTrace();
5896 Datatype datatype = getDataType(subject);
5897 Object value = getPossibleValue(subject, Bindings.getBinding(datatype));
5898 return createRandomAccessBinary(subject, datatype, value);
5900 public RandomAccessBinary createRandomAccessBinary(Resource resource, Datatype datatype, Object initialValue)
5901 throws DatabaseException {
5902 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5904 File platform = Platform.getLocation().toFile();
5905 File tempFiles = new File(platform, "tempFiles");
5906 File dbDir = new File(tempFiles, "db");
5908 File file = new File(dbDir, "ResourceFile" + resource.getResourceId());
5909 ResourceData rd = new ResourceData(new BinaryFile(file), false);
5910 Binding binding = Bindings.getBinding(datatype);
5911 if (null == initialValue) {
5912 initialValue = binding.createDefault();
5914 Serializer serializer = binding.serializer();
5915 byte[] bytes = serializer.serialize(initialValue);
5916 rd.binaryFile.write(bytes);
5917 ravs.put(resource, rd);
5919 } catch (Exception e) {
5920 if (e instanceof DatabaseException)
5921 throw (DatabaseException)e;
5923 throw new DatabaseException(e);
5927 // static class ExternalValueRequest<T> extends ResourceRead<T> {
5929 // public ExternalValueRequest(Resource resource) {
5933 // @SuppressWarnings("unchecked")
5935 // public T perform(ReadGraph graph) throws DatabaseException {
5938 // String uri = graph.getURI(resource);
5939 // if(Layer0.URIs.Functions_functionApplication.equals(uri)) return (T)functionApplication;
5941 // return (T)ReflectionUtils.getValue(uri).getValue();
5943 // } catch(ValueNotFoundException e) {
5944 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
5945 // } catch(ClassCastException e) {
5946 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
5952 @SuppressWarnings("unchecked")
5954 public <T> T getValue2(Resource r, Object context) throws DatabaseException {
5955 Layer0 L0 = processor.getL0(this);
5956 Set<Resource> types = getTypes(r);
5958 if(types.contains(L0.Literal)) {
5959 if(isImmutable(r)) {
5960 return syncRequest(new ValueImplied<T>(r));
5965 else if(types.contains(L0.ExternalValue)) {
5966 return (T)syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
5970 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
5971 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
5973 return function.apply(this, r, context);
5974 } catch(RuntimeException e) {
5975 DatabaseException dte = findPossibleRootException(e);
5976 if(dte != null) throw dte;
5977 else throw new DatabaseException(e);
5984 public Variant getVariantValue2(Resource r, Object context) throws DatabaseException {
5985 Layer0 L0 = processor.getL0(this);
5986 Set<Resource> types = getTypes(r);
5988 if(types.contains(L0.Literal)) {
5989 if(isImmutable(r)) {
5990 return syncRequest(new VariantValueImplied(r));
5992 return getVariantValue(r);
5995 else if(types.contains(L0.ExternalValue)) {
5996 Object value = syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
5998 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
5999 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6000 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6005 Function3<ReadGraph,Resource,Object,Object> function = requestValueFunction(r);
6006 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6008 Object value = function.apply(this, r, context);
6010 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6011 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6012 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6014 } catch(RuntimeException e) {
6015 DatabaseException dte = findPossibleRootException(e);
6016 if(dte != null) throw dte;
6017 else throw new DatabaseException(e);
6023 public <T> T getPossibleValue2(Resource subject, Object context) throws DatabaseException {
6025 return getValue2(subject, context);
6026 } catch (DatabaseException e) {
6031 static class PossibleConverterFunction<T> extends ResourceRead<Function3<ReadGraph,Resource,Object,T>> {
6033 public PossibleConverterFunction(Resource resource) {
6038 public Function3<ReadGraph,Resource,Object,T> perform(ReadGraph graph) throws DatabaseException {
6039 return compute(graph, resource);
6042 @SuppressWarnings("unchecked")
6043 public static <T> Function3<ReadGraph,Resource,Object,T> compute(ReadGraph graph, Resource resource) throws DatabaseException {
6044 Layer0 L0 = Layer0.getInstance(graph);
6045 for(Resource converter : graph.getObjects(resource, L0.ConvertsToValueWith)) {
6047 if(L0.Functions_functionApplication.equals(converter)) {
6048 return (Function3<ReadGraph,Resource,Object,T>)graph.syncRequest(new AdaptValue(resource));
6050 return graph.getValue2(converter, resource);
6052 } catch(RuntimeException e) {
6053 DatabaseException dte = findPossibleRootException(e);
6054 if(dte != null) throw dte;
6055 else throw new DatabaseException(e);
6063 <T> Function3<ReadGraph,Resource,Object,T> requestValueFunction(Resource r) throws DatabaseException {
6065 return syncRequest(new PossibleConverterFunction<T>(r), TransientCacheAsyncListener.<Function3<ReadGraph,Resource,Object,T>>instance());
6067 return syncRequest(new PossibleConverterFunction<T>(r));
6071 * Get a value associated with a graph {@link Resource}, using a possible context object and
6072 * a desired value binding. The following methods are tried in order to retreive the value:
6074 * <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>
6075 * <li>If the resource is a {@code L0.ExternalValue}, the value is acquired using
6076 * {@link ReflectionUtils#getValue(String)}.</li>
6077 * <li>If the resource is associated with a suitable value converter with relation {@code L0.ConvertsToValueWith}
6078 * (see {@link #requestValueFunction(Resource)}), the value function is called with the graph, the resource
6079 * and the context object.</li>
6082 * @param r A graph resource with which the value is associated
6083 * @param context A context object that is used for acquiring the value (only applied in case 3)
6084 * @param binding A binding for the value type (only applied in case 1)
6085 * @return The value of the graph node.
6086 * @throws DoesNotContainValueException No value is associated with the graph node.
6087 * @throws DatabaseException Other errors, such as an error in casting the value to the return type or
6088 * a runtime error in the value function.
6090 @SuppressWarnings("unchecked")
6092 public <T> T getValue2(Resource r, Object context, Binding binding) throws DatabaseException {
6093 if (binding instanceof ObjectVariantBinding)
6094 return getValue2(r, context);
6096 Layer0 L0 = processor.getL0(this);
6097 Set<Resource> types = getTypes(r);
6098 if(types.contains(L0.Literal)) {
6099 if(isImmutable(r)) {
6100 return syncRequest(new Value<T>(r, binding));
6102 return getValue(r, binding);
6104 } else if(types.contains(L0.ExternalValue)) {
6106 return (T)ReflectionUtils.getValue(getURI(r)).getValue();
6107 } catch(ValueNotFoundException e) {
6108 throw new DatabaseException(e);
6109 } catch(ClassCastException e) {
6110 throw new DatabaseException(e);
6114 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6115 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function.");
6117 Object value = function.apply(this, r, context);
6118 if(binding.isInstance(value)) return (T)value;
6119 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
6120 return (T)Bindings.adapt(value, srcBinding, binding);
6121 } catch(RuntimeException e) {
6122 DatabaseException dte = findPossibleRootException(e);
6123 if(dte != null) throw dte;
6124 else throw new DatabaseException(e);
6125 } catch (AdaptException e) {
6126 throw new DatabaseException(e);
6127 } catch (org.simantics.databoard.binding.error.BindingException e) {
6128 throw new DatabaseException(e);
6134 public <T> T getPossibleValue2(Resource subject, Object context, Binding binding) throws DatabaseException {
6136 return getValue2(subject, context, binding);
6137 } catch (DatabaseException e) {
6142 private static DatabaseException findPossibleRootException(Throwable t) {
6143 if(t == null) return null;
6144 if(t instanceof DatabaseException) return (DatabaseException)t;
6145 if(t instanceof RuntimeException || t instanceof InvocationTargetException) {
6146 return findPossibleRootException(t.getCause());
6152 public <T> T getRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6153 return getRelatedValue2(subject, relation, subject);
6157 public Variant getRelatedVariantValue2(Resource subject, Resource relation) throws DatabaseException {
6158 return getRelatedVariantValue2(subject, relation, subject);
6162 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6164 Resource object = getPossibleObject(subject, relation);
6165 if(object == null) return null;
6166 else return getValue2(object, subject);
6167 } catch (DatabaseException e) {
6173 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6174 if(Development.DEVELOPMENT) {
6175 String error = L0Validations.checkValueType(this, subject, relation);
6177 Logger.defaultLogError(new ValidationException(error));
6178 //throw new ValidationException(error);
6179 new ValidationException(error).printStackTrace();
6182 return getValue2(getSingleObject(subject, relation), context);
6186 public Variant getRelatedVariantValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6187 if(Development.DEVELOPMENT) {
6188 String error = L0Validations.checkValueType(this, subject, relation);
6190 Logger.defaultLogError(new ValidationException(error));
6191 //throw new ValidationException(error);
6192 new ValidationException(error).printStackTrace();
6195 return getVariantValue2(getSingleObject(subject, relation), context);
6199 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6201 Resource object = getPossibleObject(subject, relation);
6202 if(object == null) return null;
6203 else return getValue2(object, context);
6204 } catch (DatabaseException e) {
6210 public <T> T getRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6211 return getRelatedValue2(subject, relation, subject, binding);
6215 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6217 Resource object = getPossibleObject(subject, relation);
6218 if(object == null) return null;
6219 return getValue2(object, subject, binding);
6220 } catch (DatabaseException e) {
6226 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6227 return getValue2(getSingleObject(subject, relation), context, binding);
6231 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6233 Resource object = getPossibleObject(subject, relation);
6234 if(object == null) return null;
6235 else return getValue2(object, context, binding);
6236 } catch (DatabaseException e) {
6242 public Type getRelatedValueType(Resource subject, Resource relation) throws DatabaseException {
6243 Layer0 L0 = processor.getL0(this);
6244 Resource property = getSingleObject(subject, relation);
6245 String typeText = (String)getRelatedValue(property, L0.HasValueType, Bindings.STRING);
6247 return org.simantics.scl.compiler.types.Types.parseType(typeText);
6248 } catch (SCLTypeParseException e) {
6249 throw new DatabaseException(e);
6253 private static ThreadLocal<Boolean> syncGraph = new ThreadLocal<Boolean>() {
6254 protected Boolean initialValue() {
6260 public boolean setSynchronous(boolean value) {
6261 boolean old = getSynchronous();
6262 syncGraph.set(value);
6267 public boolean getSynchronous() {
6268 return syncGraph.get();
6271 public void ensureLoaded(int resource) {
6272 processor.querySupport.ensureLoaded(this, resource);
6275 public void ensureLoaded(int resource, int predicate) {
6276 processor.querySupport.ensureLoaded(this, resource, predicate);
6279 public byte[] getValue(int resource) {
6280 return processor.querySupport.getValue(this, resource);
6283 public int thread(int resource) {
6284 return (resource >>> 16) & processor.THREAD_MASK;
6287 public int thread(Resource resource) {
6288 return (((ResourceImpl)resource).id >>> 16) & processor.THREAD_MASK;
6291 public ResourceSupport getResourceSupport() {
6292 return processor.getResourceSupport();
6296 public Object getModificationCounter() {
6297 return processor.getSession().getModificationCounter();
6301 public boolean performPending() {
6302 return processor.performPending(this);
6305 public Set<ReadGraphImpl> ancestorSet() {
6306 HashSet<ReadGraphImpl> result = new HashSet<>();
6307 ReadGraphImpl g = this;