1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl.graph;
14 import java.io.BufferedOutputStream;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.io.PrintStream;
19 import java.lang.reflect.Array;
20 import java.lang.reflect.InvocationTargetException;
21 import java.nio.BufferUnderflowException;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.IdentityHashMap;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.ListIterator;
32 import java.util.function.Consumer;
34 import org.eclipse.core.runtime.Platform;
35 import org.simantics.databoard.Accessors;
36 import org.simantics.databoard.Bindings;
37 import org.simantics.databoard.accessor.Accessor;
38 import org.simantics.databoard.accessor.error.AccessorConstructionException;
39 import org.simantics.databoard.adapter.AdaptException;
40 import org.simantics.databoard.binding.Binding;
41 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;
42 import org.simantics.databoard.binding.impl.ObjectVariantBinding;
43 import org.simantics.databoard.binding.mutable.Variant;
44 import org.simantics.databoard.serialization.Serializer;
45 import org.simantics.databoard.type.Datatype;
46 import org.simantics.databoard.util.binary.BinaryFile;
47 import org.simantics.databoard.util.binary.RandomAccessBinary;
48 import org.simantics.db.AsyncReadGraph;
49 import org.simantics.db.DevelopmentKeys;
50 import org.simantics.db.ExternalValueSupport;
51 import org.simantics.db.ReadGraph;
52 import org.simantics.db.RelationContext;
53 import org.simantics.db.Resource;
54 import org.simantics.db.Session;
55 import org.simantics.db.Statement;
56 import org.simantics.db.adaption.AdaptionService;
57 import org.simantics.db.common.primitiverequest.Adapter;
58 import org.simantics.db.common.primitiverequest.Builtin;
59 import org.simantics.db.common.primitiverequest.DatatypeBinding;
60 import org.simantics.db.common.primitiverequest.ForEachAssertedObject;
61 import org.simantics.db.common.primitiverequest.ForEachAssertedStatement;
62 import org.simantics.db.common.primitiverequest.HasStatement;
63 import org.simantics.db.common.primitiverequest.HasStatementSubject;
64 import org.simantics.db.common.primitiverequest.HasStatementSubjectObject;
65 import org.simantics.db.common.primitiverequest.HasValue;
66 import org.simantics.db.common.primitiverequest.Inverse;
67 import org.simantics.db.common.primitiverequest.IsInheritedFrom;
68 import org.simantics.db.common.primitiverequest.IsInstanceOf;
69 import org.simantics.db.common.primitiverequest.IsSubrelationOf;
70 import org.simantics.db.common.primitiverequest.OrderedSet;
71 import org.simantics.db.common.primitiverequest.PossibleAdapter;
72 import org.simantics.db.common.primitiverequest.PossibleInverse;
73 import org.simantics.db.common.primitiverequest.PossibleObject;
74 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;
75 import org.simantics.db.common.primitiverequest.PossibleRelatedValueImplied;
76 import org.simantics.db.common.primitiverequest.PossibleStatement;
77 import org.simantics.db.common.primitiverequest.PossibleType;
78 import org.simantics.db.common.primitiverequest.PossibleUniqueAdapter;
79 import org.simantics.db.common.primitiverequest.PossibleValue;
80 import org.simantics.db.common.primitiverequest.PossibleValueImplied;
81 import org.simantics.db.common.primitiverequest.RelatedValue;
82 import org.simantics.db.common.primitiverequest.RelatedValueImplied;
83 import org.simantics.db.common.primitiverequest.SingleObject;
84 import org.simantics.db.common.primitiverequest.SingleStatement;
85 import org.simantics.db.common.primitiverequest.SingleType;
86 import org.simantics.db.common.primitiverequest.SingleTypeAny;
87 import org.simantics.db.common.primitiverequest.Types;
88 import org.simantics.db.common.primitiverequest.UniqueAdapter;
89 import org.simantics.db.common.primitiverequest.Value;
90 import org.simantics.db.common.primitiverequest.ValueImplied;
91 import org.simantics.db.common.primitiverequest.VariantValueImplied;
92 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
93 import org.simantics.db.common.procedure.adapter.AsyncProcedureAdapter;
94 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
95 import org.simantics.db.common.procedure.adapter.SyncMultiProcedureAdapter;
96 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
97 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
98 import org.simantics.db.common.procedure.single.SyncReadProcedure;
99 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrErrorProcedure;
100 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrNullProcedure;
101 import org.simantics.db.common.procedure.single.wrapper.ExceptionToNullProcedure;
102 import org.simantics.db.common.procedure.single.wrapper.NullSingleOrNullProcedure;
103 import org.simantics.db.common.procedure.single.wrapper.SingleFunctionalOrNullProcedure;
104 import org.simantics.db.common.procedure.single.wrapper.SingleOrErrorProcedure;
105 import org.simantics.db.common.procedure.single.wrapper.SingleOrNullProcedure;
106 import org.simantics.db.common.procedure.wrapper.NoneToAsyncListener;
107 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiListener;
108 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiProcedure;
109 import org.simantics.db.common.procedure.wrapper.NoneToAsyncProcedure;
110 import org.simantics.db.common.procedure.wrapper.NoneToAsyncSetProcedure;
111 import org.simantics.db.common.procedure.wrapper.NoneToSyncMultiListener;
112 import org.simantics.db.common.procedure.wrapper.NoneToSyncMultiProcedure;
113 import org.simantics.db.common.procedure.wrapper.SyncToAsyncListener;
114 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiListener;
115 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiProcedure;
116 import org.simantics.db.common.procedure.wrapper.SyncToAsyncProcedure;
117 import org.simantics.db.common.procedure.wrapper.SyncToAsyncSetProcedure;
118 import org.simantics.db.common.request.AdaptValue;
119 import org.simantics.db.common.request.ResourceRead;
120 import org.simantics.db.common.utils.Logger;
121 import org.simantics.db.common.utils.NameUtils;
122 import org.simantics.db.common.validation.L0Validations;
123 import org.simantics.db.exception.AdaptionException;
124 import org.simantics.db.exception.ArgumentException;
125 import org.simantics.db.exception.AssumptionException;
126 import org.simantics.db.exception.BindingException;
127 import org.simantics.db.exception.DatabaseException;
128 import org.simantics.db.exception.DoesNotContainValueException;
129 import org.simantics.db.exception.EmptyResourceException;
130 import org.simantics.db.exception.InternalException;
131 import org.simantics.db.exception.InvalidLiteralException;
132 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
133 import org.simantics.db.exception.NoInverseException;
134 import org.simantics.db.exception.NoSingleResultException;
135 import org.simantics.db.exception.ResourceNotFoundException;
136 import org.simantics.db.exception.ServiceException;
137 import org.simantics.db.exception.ValidationException;
138 import org.simantics.db.impl.BlockingAsyncProcedure;
139 import org.simantics.db.impl.RelationContextImpl;
140 import org.simantics.db.impl.ResourceImpl;
141 import org.simantics.db.impl.internal.RandomAccessValueSupport;
142 import org.simantics.db.impl.internal.ResourceData;
143 import org.simantics.db.impl.procedure.ResultCallWrappedSyncQueryProcedure;
144 import org.simantics.db.impl.query.CacheEntry;
145 import org.simantics.db.impl.query.QueryCache;
146 import org.simantics.db.impl.query.QueryCacheBase;
147 import org.simantics.db.impl.query.QueryProcessor;
148 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
149 import org.simantics.db.impl.query.QuerySupport;
150 import org.simantics.db.impl.query.TripleIntProcedure;
151 import org.simantics.db.impl.support.ResourceSupport;
152 import org.simantics.db.procedure.AsyncListener;
153 import org.simantics.db.procedure.AsyncMultiListener;
154 import org.simantics.db.procedure.AsyncMultiProcedure;
155 import org.simantics.db.procedure.AsyncProcedure;
156 import org.simantics.db.procedure.AsyncSetListener;
157 import org.simantics.db.procedure.Listener;
158 import org.simantics.db.procedure.ListenerBase;
159 import org.simantics.db.procedure.MultiListener;
160 import org.simantics.db.procedure.MultiProcedure;
161 import org.simantics.db.procedure.Procedure;
162 import org.simantics.db.procedure.SetListener;
163 import org.simantics.db.procedure.StatementProcedure;
164 import org.simantics.db.procedure.SyncListener;
165 import org.simantics.db.procedure.SyncMultiListener;
166 import org.simantics.db.procedure.SyncMultiProcedure;
167 import org.simantics.db.procedure.SyncProcedure;
168 import org.simantics.db.procedure.SyncSetListener;
169 import org.simantics.db.request.AsyncMultiRead;
170 import org.simantics.db.request.AsyncRead;
171 import org.simantics.db.request.DelayedWrite;
172 import org.simantics.db.request.DelayedWriteResult;
173 import org.simantics.db.request.ExternalRead;
174 import org.simantics.db.request.MultiRead;
175 import org.simantics.db.request.Read;
176 import org.simantics.db.request.ReadInterface;
177 import org.simantics.db.request.Write;
178 import org.simantics.db.request.WriteInterface;
179 import org.simantics.db.request.WriteOnly;
180 import org.simantics.db.request.WriteOnlyResult;
181 import org.simantics.db.request.WriteResult;
182 import org.simantics.layer0.Layer0;
183 import org.simantics.scl.compiler.types.Type;
184 import org.simantics.scl.compiler.types.exceptions.SCLTypeParseException;
185 import org.simantics.scl.reflection.ReflectionUtils;
186 import org.simantics.scl.reflection.ValueNotFoundException;
187 import org.simantics.scl.runtime.function.Function3;
188 import org.simantics.utils.DataContainer;
189 import org.simantics.utils.Development;
190 import org.simantics.utils.datastructures.Pair;
191 import org.simantics.utils.datastructures.collections.CollectionUtils;
192 import org.slf4j.LoggerFactory;
194 import gnu.trove.map.hash.TObjectIntHashMap;
196 public class ReadGraphImpl implements AsyncReadGraph {
198 private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ReadGraphImpl.class);
200 final static boolean EMPTY_RESOURCE_CHECK = false;
202 final public CacheEntry parent;
203 public final ReadGraphImpl parentGraph;
204 final public QueryProcessor processor;
206 public final AsyncBarrierImpl asyncBarrier;
208 final static Binding DATA_TYPE_BINDING_INTERNAL = Bindings.getBindingUnchecked(Datatype.class);
209 final static Serializer DATA_TYPE_SERIALIZER = Bindings.getSerializerUnchecked(DATA_TYPE_BINDING_INTERNAL);
211 final public static TObjectIntHashMap<String> counters = new TObjectIntHashMap<String>();
213 public static void resetCounters() {
217 public static String listCounters(File file) throws IOException {
219 PrintStream b = new PrintStream(new BufferedOutputStream(new FileOutputStream(file)));
221 for(Pair<String,Integer> p : CollectionUtils.valueSortedEntries(counters)) {
222 b.print(-p.second + " " + p.first + "\n");
227 return "Dumped " + counters.size() + " queries.";
232 * Implementation of the interface ReadGraph
234 final public String getURI(final Resource resource) throws AssumptionException, ValidationException, ServiceException {
236 assert (resource != null);
240 return syncRequest(new org.simantics.db.common.uri.ResourceToURI(resource));
242 } catch (AssumptionException e) {
244 throw new AssumptionException(e);
246 } catch (ValidationException e) {
248 throw new ValidationException(e);
250 } catch (ServiceException e) {
252 throw new ServiceException(e);
254 } catch (DatabaseException e) {
256 throw new ServiceException(INTERNAL_ERROR_STRING, e);
262 final public String getPossibleURI(final Resource resource) throws ValidationException, ServiceException {
264 assert (resource != null);
268 return syncRequest(new org.simantics.db.common.uri.ResourceToPossibleURI(resource));
270 } catch (ValidationException e) {
272 throw new ValidationException(e);
274 } catch (ServiceException e) {
276 throw new ServiceException(e);
278 } catch (DatabaseException e) {
280 throw new ServiceException(INTERNAL_ERROR_STRING, e);
286 final public Resource getResource(final String id)
287 throws ResourceNotFoundException, ValidationException,
294 Integer rid = QueryCache.resultURIToResource(this, id, parent, null);
295 // FIXME: stupid to throw this here and catch and wrap it right away
296 if(rid == 0) throw new ResourceNotFoundException(id);
297 return processor.querySupport.getResource(rid);
299 } catch (ResourceNotFoundException e) {
301 throw new ResourceNotFoundException(id, e);
303 } catch (ValidationException e) {
305 throw new ValidationException(e);
307 } catch (ServiceException e) {
309 throw new ServiceException(e);
311 } catch (DatabaseException e) {
313 throw new ServiceException(INTERNAL_ERROR_STRING, e);
319 final public Resource getPossibleResource(final String id)
320 throws ResourceNotFoundException, ValidationException,
327 return getResource(id);
329 } catch (ResourceNotFoundException e) {
333 } catch (ValidationException e) {
335 throw new ValidationException(e);
337 } catch (ServiceException e) {
339 throw new ServiceException(e);
341 } catch (DatabaseException e) {
343 throw new ServiceException(INTERNAL_ERROR_STRING, e);
350 public Map<String, Resource> getChildren(Resource resource) throws ValidationException, ServiceException {
352 assert (resource != null);
356 int rId = processor.querySupport.getId(resource);
357 return QueryCache.resultChildMap(this, rId, parent, null);
359 } catch (ValidationException e) {
361 throw new ValidationException(e);
363 } catch (ServiceException e) {
365 throw new ServiceException(e);
367 } catch (DatabaseException e) {
369 throw new ServiceException(INTERNAL_ERROR_STRING, e);
375 final public Resource getRootLibrary() {
376 return processor.getRootLibraryResource();
379 final public Resource getBuiltin(final String id)
380 throws ResourceNotFoundException, ServiceException {
386 return syncRequest(new Builtin(id));
388 } catch (ResourceNotFoundException e) {
390 throw new ResourceNotFoundException(id, e);
392 } catch (ServiceException e) {
394 throw new ServiceException(e);
396 } catch (DatabaseException e) {
398 throw new ServiceException(INTERNAL_ERROR_STRING, e);
404 static class StatementReadProcedure extends TIntArrayListInternal implements StatementProcedure {
406 private static Throwable DONE = new Throwable();
408 Throwable exception = null;
410 final ResourceSupport support;
412 public StatementReadProcedure(ResourceSupport support) {
413 this.support = support;
417 public synchronized void execute(AsyncReadGraph graph, int s, int p, int o) {
424 public void finished(AsyncReadGraph graph) {
429 public void exception(AsyncReadGraph graph, Throwable t) {
433 public void checkAndThrow() throws DatabaseException {
434 if(exception != DONE) {
435 if (exception instanceof DatabaseException)
436 throw (DatabaseException) exception;
438 throw new DatabaseException(
439 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
444 public boolean done() {
445 return exception != null;
449 public boolean contains(Object obj) {
450 if(!(obj instanceof InternalStatement))
452 InternalStatement statement = (InternalStatement)obj;
456 for(int i=0;i<sizeInternal();i+=3)
457 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
462 @SuppressWarnings("unchecked")
464 public <T> T[] toArray(T[] a) {
465 int length = sizeInternal() / 3;
466 if(length > a.length) {
467 Class<?> arrayType = a.getClass();
468 a = (arrayType == Object[].class)
469 ? (T[]) new Object[length]
470 : (T[]) Array.newInstance(arrayType.getComponentType(), length);
473 for(int i=length;i<a.length;++i)
476 for(int i=0,j=0;i<sizeInternal();i+=3,++j)
477 a[j] = (T)new InternalStatement(support, getQuick(i), getQuick(i+1), getQuick(i+2));
482 public boolean add(Statement e) {
483 throw new UnsupportedOperationException();
487 public boolean remove(Object o) {
488 throw new UnsupportedOperationException();
492 public boolean addAll(Collection<? extends Statement> c) {
493 throw new UnsupportedOperationException();
496 class IteratorImpl implements ListIterator<Statement> {
500 public IteratorImpl(int index) {
505 public boolean hasNext() {
506 return index < sizeInternal();
510 public Statement next() {
511 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
517 public void remove() {
518 throw new Error("Not supported");
522 public boolean hasPrevious() {
527 public Statement previous() {
529 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
534 public int nextIndex() {
539 public int previousIndex() {
544 public void set(Statement e) {
545 throw new UnsupportedOperationException();
549 public void add(Statement e) {
550 throw new UnsupportedOperationException();
556 public Iterator<Statement> iterator() {
557 return new IteratorImpl(0);
562 return sizeInternal() / 3;
566 public Object[] toArray() {
567 Object[] result = new Object[sizeInternal() / 3];
568 for(int i=0,j=0;j<sizeInternal();i++,j+=3)
569 result[i] = new InternalStatement(support, getQuick(j), getQuick(j+1), getQuick(j+2));
574 public boolean addAll(int index, Collection<? extends Statement> c) {
575 throw new UnsupportedOperationException();
579 public Statement get(int index) {
581 if(index < 0 || index >= sizeInternal())
582 throw new IndexOutOfBoundsException();
583 return new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
587 public Statement set(int index, Statement element) {
588 throw new UnsupportedOperationException();
592 public void add(int index, Statement element) {
593 throw new UnsupportedOperationException();
597 public Statement remove(int index) {
598 throw new UnsupportedOperationException();
602 public int indexOf(Object obj) {
603 if(!(obj instanceof InternalStatement))
605 InternalStatement statement = (InternalStatement)obj;
609 for(int i=0;i<sizeInternal();i+=3)
610 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
616 public int lastIndexOf(Object obj) {
617 if(!(obj instanceof InternalStatement))
619 InternalStatement statement = (InternalStatement)obj;
623 for(int i=sizeInternal()-3;i>=0;i-=3)
624 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
630 public ListIterator<Statement> listIterator() {
631 return new IteratorImpl(0);
635 public ListIterator<Statement> listIterator(int index) {
636 return new IteratorImpl(index*3);
640 public List<Statement> subList(int fromIndex, int toIndex) {
641 if(fromIndex < 0 || toIndex*3 >= sizeInternal() || fromIndex > toIndex)
642 throw new IndexOutOfBoundsException();
643 return new RandomAccessSubList<Statement>(this, fromIndex, toIndex-fromIndex);
648 final public Collection<Statement> getStatements(final Resource subject,
649 final Resource relation)
650 throws ManyObjectsForFunctionalRelationException, ServiceException {
652 assert (subject != null);
653 assert (relation != null);
657 StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
658 processor.forEachStatement(this, subject, relation, procedure);
659 procedure.checkAndThrow();
662 } catch (DatabaseException e) {
664 System.err.println(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation);
666 StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
667 processor.forEachStatement(this, subject, relation, procedure);
669 return Collections.emptyList();
671 // throw new ServiceException(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation, e);
678 final public Collection<Statement> getAssertedStatements(final Resource subject, final Resource relation)
679 throws ManyObjectsForFunctionalRelationException, ServiceException {
681 assert (subject != null);
682 assert (relation != null);
686 return syncRequest(new ForEachAssertedStatement(subject, relation));
688 } catch (ManyObjectsForFunctionalRelationException e) {
690 throw new ManyObjectsForFunctionalRelationException(e);
692 } catch (ServiceException e) {
694 throw new ServiceException(e);
696 } catch (DatabaseException e) {
698 throw new ServiceException(INTERNAL_ERROR_STRING, e);
705 final public Collection<Resource> getPredicates(final Resource subject) throws ServiceException {
707 assert (subject != null);
711 return processor.getPredicates(this, subject);
713 // AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
714 // processor.forEachPredicate(this, subject, procedure);
715 // procedure.checkAndThrow();
718 } catch (ServiceException e) {
720 throw new ServiceException(e);
722 } catch (DatabaseException e) {
724 throw new ServiceException(INTERNAL_ERROR_STRING, e);
726 } catch (Throwable e) {
728 throw new ServiceException(e);
735 final public Collection<Resource> getPrincipalTypes(final Resource subject)
736 throws ServiceException {
738 assert (subject != null);
742 AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
743 processor.forEachPrincipalType(this, subject, procedure);
744 procedure.checkAndThrow();
747 } catch (ServiceException e) {
749 throw new ServiceException(e);
751 } catch (DatabaseException e) {
753 throw new ServiceException(INTERNAL_ERROR_STRING, e);
760 final public Set<Resource> getTypes(final Resource subject) throws ServiceException {
762 assert (subject != null);
766 return processor.getTypes(this, subject);
768 } catch (ServiceException e) {
770 throw new ServiceException(e);
772 } catch (DatabaseException e) {
774 throw new ServiceException(INTERNAL_ERROR_STRING, e);
776 } catch (Throwable e) {
778 throw new ServiceException(e);
785 final public Set<Resource> getSupertypes(final Resource subject)
786 throws ServiceException {
788 assert (subject != null);
792 SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
793 processor.forSupertypes(this, subject, procedure);
794 procedure.checkAndThrow();
795 return procedure.result;
797 } catch (ServiceException e) {
799 throw new ServiceException(e);
801 } catch (DatabaseException e) {
803 throw new ServiceException(INTERNAL_ERROR_STRING, e);
810 final public Set<Resource> getSuperrelations(final Resource subject)
811 throws ServiceException {
813 assert (subject != null);
817 SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
818 processor.forSuperrelations(this, subject, procedure);
819 procedure.checkAndThrow();
820 return procedure.result;
822 } catch (ServiceException e) {
824 throw new ServiceException(e);
826 } catch (DatabaseException e) {
828 throw new ServiceException(INTERNAL_ERROR_STRING, e);
835 public Resource getPossibleSuperrelation(Resource subject) throws ServiceException {
839 SyncReadProcedure<Resource> procedure = new SyncReadProcedure<Resource>();
840 processor.forPossibleSuperrelation(this, subject, procedure);
841 procedure.checkAndThrow();
842 return procedure.result;
844 } catch (ServiceException e) {
846 throw new ServiceException(e);
848 } catch (DatabaseException e) {
850 throw new ServiceException(INTERNAL_ERROR_STRING, e);
857 final public Collection<Resource> getObjects(final Resource subject, final Resource relation)
858 throws ServiceException {
860 assert (subject != null);
861 assert (relation != null);
863 if(Development.DEVELOPMENT) {
864 if(Development.isTrue(DevelopmentKeys.READGRAPH_COUNT)) {
865 counters.adjustOrPutValue("objects $" + subject.getResourceId() + " $" + relation.getResourceId(), 1, 1);
867 //if(subject.getResourceId()==xx && relation.getResourceId()==xx) new Exception().printStackTrace();
872 AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
873 processor.forEachObject(this, subject, relation, procedure);
874 procedure.checkAndThrow();
877 } catch (DatabaseException e) {
879 throw new ServiceException(INTERNAL_ERROR_STRING, e);
886 final public Collection<Resource> getAssertedObjects(
887 final Resource subject, final Resource relation)
888 throws ManyObjectsForFunctionalRelationException, ServiceException {
891 throw new ArgumentException("Subject must not be null.");
892 if (relation == null)
893 throw new ArgumentException("Relation must not be null. Subject=" + subject);
897 return syncRequest(new ForEachAssertedObject(subject, relation));
899 } catch (ManyObjectsForFunctionalRelationException e) {
901 throw new ManyObjectsForFunctionalRelationException(e);
903 } catch (ServiceException e) {
905 throw new ServiceException(e);
907 } catch (DatabaseException e) {
909 throw new ServiceException(INTERNAL_ERROR_STRING, e);
916 final public Resource getInverse(final Resource relation) throws NoInverseException, ServiceException {
918 assert (relation != null);
922 return getSingleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
924 } catch (NoSingleResultException e) {
926 throw new NoInverseException(e);
928 } catch (ServiceException e) {
930 throw new ServiceException(e);
937 final public Resource getSingleObject(final Resource subject, final Resource relation) throws NoSingleResultException, ServiceException {
939 if( subject == null) throw new IllegalArgumentException("subject can not be null");
940 if( relation == null) throw new IllegalArgumentException("relation can not be null");
943 int single = processor.getSingleObject(this, subject, relation);
945 if (EMPTY_RESOURCE_CHECK) {
946 if (!hasStatement(subject)) {
947 throw new EmptyResourceException("Resource " + debugString(subject));
950 throw new NoSingleResultException("No single object for subject " + debugString(subject)
951 + " and relation " + debugString(relation), single);
953 return processor.querySupport.getResource(single);
954 } catch (NoSingleResultException e) {
956 } catch (DatabaseException e) {
957 throw new ServiceException(e);
962 final public Statement getSingleStatement(final Resource subject, final Resource relation) throws NoSingleResultException, ManyObjectsForFunctionalRelationException, ServiceException {
963 assert (subject != null);
964 assert (relation != null);
966 Collection<Statement> statements = getStatements(subject, relation);
967 if (statements.size() == 1) {
968 return statements.iterator().next();
970 if (EMPTY_RESOURCE_CHECK)
971 if (!hasStatement(subject))
972 throw new EmptyResourceException("Resource " + debugString(subject));
973 throw new NoSingleResultException("No single statement for subject " + debugString(subject)
974 + " and relation " + debugString(relation), statements.size());
976 } catch (ServiceException e) {
977 throw new ServiceException(e);
982 final public Resource getSingleType(final Resource subject) throws NoSingleResultException, ServiceException {
983 assert (subject != null);
985 ArrayList<Resource> principalTypes = (ArrayList<Resource>)getPrincipalTypes(subject);
986 if (principalTypes.size() == 1) {
987 return principalTypes.get(0);
989 throw new NoSingleResultException("No single type for subject " + debugString(subject), principalTypes.size());
991 } catch (ServiceException e) {
992 throw new ServiceException(e);
997 final public Resource getSingleType(final Resource subject,
998 final Resource baseType) throws NoSingleResultException,
1001 assert (subject != null);
1002 assert (baseType != null);
1005 return syncRequest(new SingleType(subject, baseType));
1006 } catch (DatabaseException e) {
1007 throw new NoSingleResultException("subject=" + subject + ", baseType=" + baseType, 0, e);
1012 final public <T> T getValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
1014 assert (subject != null);
1018 Layer0 L0 = processor.getL0(this);
1019 int object = processor.getSingleObject(this, subject, L0.HasDataType);
1020 if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
1022 if(processor.isImmutable(object)) {
1023 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1024 return getValue(subject, binding);
1026 byte[] dt = processor.getValue(this, object);
1027 if(dt == null) throw new ServiceException("No data type for " + subject);
1028 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1029 Binding binding = Bindings.getBinding(datatype);
1030 return getValue(subject, binding);
1033 } catch (IOException e) {
1035 throw new ServiceException(e);
1037 } catch (DoesNotContainValueException e) {
1039 throw new DoesNotContainValueException(e, subject);
1041 } catch (ServiceException e) {
1043 throw new ServiceException(e);
1045 } catch (DatabaseException e) {
1047 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1054 final public Variant getVariantValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
1056 assert (subject != null);
1060 Layer0 L0 = processor.getL0(this);
1061 int object = processor.getSingleObject(this, subject, L0.HasDataType);
1062 if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
1064 if(processor.isImmutable(object)) {
1065 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1066 return new Variant(binding, getValue(subject, binding));
1068 byte[] dt = processor.getValue(this, object);
1069 if(dt == null) throw new ServiceException("No data type for " + subject);
1070 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1071 Binding binding = Bindings.getBinding(datatype);
1072 return new Variant(binding, getValue(subject, binding));
1075 } catch (IOException e) {
1077 throw new ServiceException(e);
1079 } catch (DoesNotContainValueException e) {
1081 throw new DoesNotContainValueException(e, subject);
1083 } catch (ServiceException e) {
1085 throw new ServiceException(e);
1087 } catch (DatabaseException e) {
1089 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1094 static final IdentityHashMap<Binding,Serializer> serializers = new IdentityHashMap<Binding,Serializer>();
1097 serializers.put(Bindings.STRING, Bindings.STRING.serializer());
1100 final protected Serializer getSerializer(Binding binding) {
1101 return binding.serializer();
1105 final public <T> T getValue(final Resource subject, final Binding binding) throws DoesNotContainValueException, BindingException,
1108 assert (subject != null);
1112 byte[] bytes = processor.getValue(this, subject);
1113 if (bytes == null) throw new DoesNotContainValueException("No value for resource " + subject);
1115 Serializer serializer = getSerializer(binding);
1116 return (T)serializer.deserialize(bytes);
1118 } catch (DoesNotContainValueException e) {
1120 throw new DoesNotContainValueException(e);
1122 } catch (IOException e) {
1124 throw new ServiceException(e);
1126 } catch (DatabaseException e) {
1128 throw new ServiceException(e);
1130 } catch (BufferUnderflowException e) {
1131 // This is sometimes thrown when deserialize fails because wrong format.
1132 // For callers of this method this is just an service exception.
1133 throw new ServiceException(e);
1139 final public <T> T getRelatedValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1140 DoesNotContainValueException, ServiceException {
1142 assert (subject != null);
1143 assert (relation != null);
1146 Resource object = getSingleObject(subject, relation);
1147 return getValue(object);
1148 } catch (NoSingleResultException e) {
1149 throw new NoSingleResultException("No single value found for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1150 } catch (DoesNotContainValueException e) {
1152 Layer0 L0 = processor.getL0(this);
1153 Resource object = getPossibleObject(subject, relation);
1154 if(isInstanceOf(object, L0.Value)) {
1155 if(isInstanceOf(object, L0.Literal)) {
1156 throw new DoesNotContainValueException(e);
1158 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1161 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1163 } catch (DoesNotContainValueException e2) {
1165 } catch (DatabaseException e2) {
1166 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1168 } catch (ServiceException e) {
1169 throw new ServiceException(e);
1174 final public Variant getRelatedVariantValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1175 DoesNotContainValueException, ServiceException {
1177 assert (subject != null);
1178 assert (relation != null);
1181 Resource object = getSingleObject(subject, relation);
1182 return getVariantValue(object);
1183 } catch (NoSingleResultException e) {
1184 throw new NoSingleResultException("No single object for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1185 } catch (DoesNotContainValueException e) {
1187 Layer0 L0 = processor.getL0(this);
1188 Resource object = getPossibleObject(subject, relation);
1189 if(isInstanceOf(object, L0.Value)) {
1190 if(isInstanceOf(object, L0.Literal)) {
1191 throw new DoesNotContainValueException(e);
1193 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1196 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1198 } catch (DoesNotContainValueException e2) {
1200 } catch (DatabaseException e2) {
1201 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1203 } catch (ServiceException e) {
1204 throw new ServiceException(e);
1209 final public <T> T getRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1210 throws NoSingleResultException, DoesNotContainValueException, BindingException, ServiceException {
1212 assert (subject != null);
1213 assert (relation != null);
1216 Resource object = getSingleObject(subject, relation);
1217 return getValue(object, binding);
1218 } catch (NoSingleResultException e) {
1219 String message = "";
1221 String subjectName = NameUtils.getSafeName(this, subject, true);
1222 String relationName = NameUtils.getSafeName(this, relation, true);
1223 message = "Subject: " + subjectName + ", Relation: " + relationName;
1224 } catch (DatabaseException e2) {
1227 throw new NoSingleResultException(message, e.getResultCount(), e);
1228 } catch (DoesNotContainValueException e) {
1229 throw new DoesNotContainValueException(e);
1230 } catch (ServiceException e) {
1231 throw new ServiceException(e);
1236 final public <T> T adapt(final Resource resource, final Class<T> clazz)
1237 throws AdaptionException, ValidationException, ServiceException {
1239 assert (resource != null);
1240 assert (clazz != null);
1244 return syncRequest(new Adapter<T>(resource, clazz));
1246 } catch (AdaptionException e) {
1248 throw new AdaptionException(e);
1250 } catch (ValidationException e) {
1252 throw new ValidationException(e);
1254 } catch (ServiceException e) {
1256 throw new ServiceException(e);
1258 } catch (DatabaseException e) {
1260 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1267 final public <T,C> T adaptContextual(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1268 throws AdaptionException, ValidationException, ServiceException {
1270 assert (resource != null);
1271 assert (context != null);
1273 class ContextualAdapter implements AsyncRead<T> {
1275 final private Resource resource;
1276 final private C context;
1277 final private Class<T> clazz;
1280 public int hashCode() {
1281 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1285 final public int threadHash() {
1286 return resource.getThreadHash();
1290 public boolean equals(Object object) {
1293 else if (object == null)
1295 else if (getClass() != object.getClass())
1297 ContextualAdapter r = (ContextualAdapter)object;
1298 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1302 public int getFlags() {
1306 public ContextualAdapter(Resource resource, C context, Class<T> clazz) {
1307 this.resource = resource;
1308 this.context = context;
1313 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1315 final AdaptionService service = getSession().peekService(AdaptionService.class);
1316 if (service == null)
1317 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1319 service.adapt(graph, resource, context, contextClass, clazz, false, procedure);
1324 public String toString() {
1325 return "Adapter for (" + resource + "," + context + ") as " + clazz.getName();
1332 return syncRequest(new ContextualAdapter(resource, context, clazz));
1334 } catch (AdaptionException e) {
1336 throw new AdaptionException(e);
1338 } catch (ValidationException e) {
1340 throw new ValidationException(e);
1342 } catch (ServiceException e) {
1344 throw new ServiceException(e);
1346 } catch (DatabaseException e) {
1348 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1355 final public <T> T adaptRelated(final Resource resource, final Resource relation, final Class<T> clazz)
1356 throws AdaptionException, NoSingleResultException, ValidationException, ServiceException {
1358 assert (resource != null);
1359 assert (clazz != null);
1361 Statement stm = getSingleStatement(resource, relation);
1363 return adaptContextual(stm.getObject(), new RelationContextImpl(resource, stm), RelationContext.class, clazz);
1368 final public <T> T getPossibleRelatedAdapter(final Resource resource, final Resource relation, final Class<T> clazz)
1369 throws ValidationException, ServiceException {
1372 return adaptRelated(resource, relation, clazz);
1373 } catch (DatabaseException e) {
1380 final public <T,C> T getPossibleContextualAdapter(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1381 throws ValidationException, ServiceException {
1383 assert (resource != null);
1384 assert (context != null);
1386 class PossibleContextualAdapter implements AsyncRead<T> {
1388 final private Resource resource;
1389 final private C context;
1390 final private Class<T> clazz;
1393 public int hashCode() {
1394 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1398 final public int threadHash() {
1399 return resource.getThreadHash();
1403 public boolean equals(Object object) {
1406 else if (object == null)
1408 else if (getClass() != object.getClass())
1410 PossibleContextualAdapter r = (PossibleContextualAdapter)object;
1411 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1415 public int getFlags() {
1419 public PossibleContextualAdapter(Resource resource, C context, Class<T> clazz) {
1420 this.resource = resource;
1421 this.context = context;
1426 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1428 final AdaptionService service = getSession().peekService(AdaptionService.class);
1429 if (service == null)
1430 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1432 service.adapt(graph, resource, context, contextClass, clazz, true, procedure);
1437 public String toString() {
1438 return "Possible adapter for (" + resource + "," + context + ") as " + clazz.getName();
1445 return syncRequest(new PossibleContextualAdapter(resource, context, clazz));
1447 } catch (ValidationException e) {
1449 throw new ValidationException(e);
1451 } catch (ServiceException e) {
1453 throw new ServiceException(e);
1455 } catch (DatabaseException e) {
1457 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1464 final public <T> T adaptUnique(final Resource resource, final Class<T> clazz)
1465 throws AdaptionException, ValidationException, ServiceException {
1467 assert (resource != null);
1468 assert (clazz != null);
1472 return syncRequest(new UniqueAdapter<T>(resource, clazz));
1474 } catch (AdaptionException e) {
1476 throw new AdaptionException(e);
1478 } catch (ValidationException e) {
1480 throw new ValidationException(e);
1482 } catch (ServiceException e) {
1484 throw new ServiceException(e);
1486 } catch (DatabaseException e) {
1488 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1495 final public Resource getPossibleInverse(final Resource relation)
1496 throws ServiceException {
1498 assert (relation != null);
1502 return getPossibleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
1504 } catch (ServiceException e) {
1506 throw new ServiceException(e);
1508 } catch (DatabaseException e) {
1510 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1517 public Resource getPossibleObject(final Resource subject, final Resource relation)
1518 throws ManyObjectsForFunctionalRelationException, ServiceException {
1520 assert (subject != null);
1521 assert (relation != null);
1525 int result = processor.getSingleObject(this, subject, relation);
1526 if(result == 0) return null;
1528 return processor.querySupport.getResource(result);
1530 } catch (ManyObjectsForFunctionalRelationException e) {
1532 throw new ManyObjectsForFunctionalRelationException("subject=" + subject + ", relation=" + relation, e);
1534 } catch (DatabaseException e) {
1536 throw new ServiceException(e);
1543 final public Statement getPossibleStatement(final Resource subject, final Resource relation)
1544 throws ManyObjectsForFunctionalRelationException, ServiceException {
1546 assert (subject != null);
1547 assert (relation != null);
1551 Collection<Statement> statements = getStatements(subject, relation);
1552 if(statements.size() == 1) return statements.iterator().next();
1555 } catch (ManyObjectsForFunctionalRelationException e) {
1557 throw new ManyObjectsForFunctionalRelationException("Many objects in " + subject + " for functional relation " + relation);
1559 } catch (ServiceException e) {
1561 throw new ServiceException(e);
1568 final public Resource getPossibleType(final Resource subject, final Resource baseType) throws ServiceException {
1570 assert (subject != null);
1571 assert (baseType != null);
1575 AsyncReadProcedure<Resource> procedure = new AsyncReadProcedure<Resource>();
1576 forPossibleType(subject, baseType, procedure);
1577 procedure.checkAndThrow();
1578 return procedure.result;
1580 } catch (ServiceException e) {
1582 throw new ServiceException(e);
1584 } catch (DatabaseException e) {
1586 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1593 final public <T> T getPossibleValue(final Resource subject) throws ServiceException {
1595 assert (subject != null);
1599 int object = processor.getSingleObject(this, subject, processor.getL0(this).HasDataType);
1600 if(object == 0) return null;
1602 if(processor.isImmutable(object)) {
1603 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1604 return getPossibleValue(subject, binding);
1606 byte[] dt = processor.getValue(this, object);
1607 if(dt == null) return null;
1608 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1609 Binding binding = Bindings.getBinding(datatype);
1610 return getPossibleValue(subject, binding);
1613 } catch (IOException e) {
1615 throw new ServiceException(e);
1617 } catch (ServiceException e) {
1619 throw new ServiceException(e);
1621 } catch (DatabaseException e) {
1623 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1630 final public <T> T getPossibleValue(final Resource subject, final Binding binding) throws BindingException, ServiceException {
1632 assert (subject != null);
1633 assert (binding != null);
1637 byte[] dt = processor.getValue(this, subject);
1638 if(dt == null) return null;
1639 Serializer serializer = getSerializer(binding);
1640 return (T)serializer.deserialize(dt);
1642 } catch (IOException e) {
1644 throw new ServiceException(e);
1646 } catch (BindingException e) {
1648 throw new BindingException(e);
1650 } catch (ServiceException e) {
1652 throw new ServiceException(e);
1654 } catch (DatabaseException e) {
1655 e.printStackTrace();
1656 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1662 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation)
1663 throws ManyObjectsForFunctionalRelationException, ServiceException {
1665 assert (subject != null);
1666 assert (relation != null);
1670 Resource object = getPossibleObject(subject, relation);
1671 if(object == null) return null;
1672 else return getPossibleValue(object);
1674 } catch (ManyObjectsForFunctionalRelationException e) {
1676 throw new ManyObjectsForFunctionalRelationException(e);
1678 } catch (ServiceException e) {
1680 throw new ServiceException(e);
1687 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1688 throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
1690 assert (subject != null);
1691 assert (relation != null);
1692 assert (binding != null);
1696 Resource object = getPossibleObject(subject, relation);
1697 if(object == null) return null;
1698 else return getPossibleValue(object, binding);
1700 } catch (ManyObjectsForFunctionalRelationException e) {
1702 throw new ManyObjectsForFunctionalRelationException(e);
1704 } catch (BindingException e) {
1706 throw new BindingException(e);
1708 } catch (ServiceException e) {
1710 throw new ServiceException(e);
1717 public <T> T getPossibleAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1719 assert (resource != null);
1720 assert (clazz != null);
1724 return syncRequest(new PossibleAdapter<T>(resource, clazz));
1726 } catch (ValidationException e) {
1728 throw new ValidationException(e);
1730 } catch (AdaptionException e) {
1734 } catch (DatabaseException e) {
1736 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1742 public <T> T getPossibleUniqueAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1744 assert (resource != null);
1745 assert (clazz != null);
1749 return syncRequest(new PossibleUniqueAdapter<T>(resource, clazz));
1751 } catch (AdaptionException e) {
1755 } catch (ValidationException e) {
1757 throw new ValidationException(e);
1759 } catch (DatabaseException e) {
1761 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1768 final public boolean isInstanceOf(final Resource resource, final Resource type) throws ServiceException {
1770 assert (resource != null);
1771 assert (type != null);
1773 Set<Resource> resources = getTypes(resource);
1774 // This check was necessary because some of the callers of this method got stuck when the NPE was thrown from here.
1775 if (null == resources)
1778 if(EMPTY_RESOURCE_CHECK) {
1779 if (resources.isEmpty()) {
1780 if(!hasStatement(resource)) throw new EmptyResourceException("Resource " + debugString(resource));
1784 return resources.contains(type);
1789 final public boolean isInheritedFrom(final Resource resource, final Resource type) throws ServiceException {
1791 assert (resource != null);
1792 assert (type != null);
1796 if(resource.equals(type)) return true;
1798 return getSupertypes(resource).contains(type);
1800 } catch (ServiceException e) {
1802 throw new ServiceException(e);
1809 final public boolean isSubrelationOf(final Resource resource, final Resource type) throws ServiceException {
1811 assert (resource != null);
1812 assert (type != null);
1816 if(resource.equals(type)) return true;
1818 return getSuperrelations(resource).contains(type);
1820 } catch (ServiceException e) {
1822 throw new ServiceException(e);
1829 final public boolean hasStatement(final Resource subject) throws ServiceException {
1831 assert (subject != null);
1835 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1836 processor.forHasStatement(this, subject, procedure);
1837 procedure.checkAndThrow();
1838 return procedure.result;
1840 } catch (ServiceException e) {
1842 throw new ServiceException(e);
1844 } catch (DatabaseException e) {
1846 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1853 final public boolean hasStatement(final Resource subject, final Resource relation) throws ServiceException {
1855 assert (subject != null);
1856 assert (relation != null);
1860 Collection<Resource> objects = getObjects(subject, relation);
1861 return !objects.isEmpty();
1863 } catch (ServiceException e) {
1865 throw new ServiceException(e);
1872 final public boolean hasStatement(final Resource subject, final Resource relation, final Resource object) throws ServiceException {
1874 assert (subject != null);
1875 assert (relation != null);
1876 assert (object != null);
1880 for(Resource o : getObjects(subject, relation)) {
1881 if(object.equals(o)) return true;
1886 } catch (ServiceException e) {
1888 throw new ServiceException(e);
1895 final public boolean hasValue(final Resource subject) throws ServiceException {
1897 assert (subject != null);
1901 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1902 processor.forHasValue(this, subject, procedure);
1903 procedure.checkAndThrow();
1904 return procedure.result;
1906 } catch (ServiceException e) {
1908 throw new ServiceException(e);
1910 } catch (DatabaseException e) {
1912 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1918 final AsyncProcedure<?> NONE = new AsyncProcedure<Object>() {
1921 public void execute(AsyncReadGraph graph, Object result) {
1925 public void exception(AsyncReadGraph graph, Throwable throwable) {
1931 * Implementation of the interface RequestProcessor
1935 public <T> T syncRequest(final Read<T> request) throws DatabaseException {
1936 assert (request != null);
1937 return (T)QueryCache.runnerReadEntry(this, request, parent, null, null, true);
1941 public <T> T syncRequest(Read<T> request, SyncListener<T> procedure)
1942 throws DatabaseException {
1943 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
1947 public <T> T syncRequest(Read<T> request, final Listener<T> procedure)
1948 throws DatabaseException {
1949 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
1953 public <T> T syncRequest(final Read<T> request, final AsyncProcedure<T> procedure) throws DatabaseException {
1955 assert (request != null);
1957 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
1959 return QueryCache.resultReadEntry(this, request, parent, listener, procedure);
1964 public <T> T syncRequest(final Read<T> request,
1965 final SyncProcedure<T> procedure) throws DatabaseException {
1966 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
1970 public <T> T syncRequest(Read<T> request, Procedure<T> procedure)
1971 throws DatabaseException {
1972 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
1975 static class AsyncReadProcedure<T> implements AsyncProcedure<T> {
1977 private static Throwable DONE = new Throwable();
1980 Throwable exception = null;
1983 public void execute(AsyncReadGraph graph, T t) {
1989 public void exception(AsyncReadGraph graph, Throwable t) {
1993 public void checkAndThrow() throws DatabaseException {
1994 if(exception != DONE) {
1995 if (exception instanceof DatabaseException)
1996 throw (DatabaseException) exception;
1998 throw new DatabaseException(
1999 "Unexpected exception in ReadGraph.syncRequest(AsyncRead)",
2004 public boolean done() {
2005 return exception != null;
2011 public <T> T syncRequest(final AsyncRead<T> request)
2012 throws DatabaseException {
2014 assert (request != null);
2015 return syncRequest(request, new AsyncProcedureAdapter<>() );
2020 public <T> T syncRequest(AsyncRead<T> request, AsyncListener<T> procedure)
2021 throws DatabaseException {
2022 return syncRequest(request, (AsyncProcedure<T>) procedure);
2026 public <T> T syncRequest(AsyncRead<T> request, SyncListener<T> procedure)
2027 throws DatabaseException {
2028 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
2032 public <T> T syncRequest(AsyncRead<T> request, Listener<T> procedure)
2033 throws DatabaseException {
2034 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
2038 final public <T> T syncRequest(final AsyncRead<T> request,
2039 final AsyncProcedure<T> procedure) throws DatabaseException {
2041 assert (request != null);
2043 ListenerBase listener = getListenerBase(procedure);
2045 // BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, procedure, request);
2046 return (T)QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure, true);
2051 final private <T> void syncRequest(final AsyncRead<T> request, final AsyncReadProcedure<T> procedure) throws DatabaseException {
2053 assert (request != null);
2055 ListenerBase listener = getListenerBase(procedure);
2056 assert(listener == null);
2058 BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, procedure, request);
2060 QueryCache.runnerAsyncReadEntry(this, request, parent, listener, ap, true);
2067 public <T> T syncRequest(AsyncRead<T> request,
2068 final SyncProcedure<T> procedure) throws DatabaseException {
2069 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2073 final public <T> T syncRequest(final AsyncRead<T> request,
2074 final Procedure<T> procedure) throws DatabaseException {
2075 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2079 public <T> Collection<T> syncRequest(final MultiRead<T> request)
2080 throws DatabaseException {
2082 assert (request != null);
2084 final ArrayList<T> result = new ArrayList<T>();
2085 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2087 syncRequest(request, new SyncMultiProcedure<T>() {
2090 public void execute(ReadGraph graph, T t) {
2091 synchronized (result) {
2097 public void finished(ReadGraph graph) {
2101 public void exception(ReadGraph graph, Throwable t) {
2106 public String toString() {
2107 return "syncRequest(MultiRead) -> " + request;
2112 Throwable t = exception.get();
2114 if (t instanceof DatabaseException)
2115 throw (DatabaseException) t;
2117 throw new DatabaseException(
2118 "Unexpected exception in ReadGraph.syncRequest(Read)",
2127 public <T> Collection<T> syncRequest(MultiRead<T> request,
2128 SyncMultiListener<T> procedure) {
2129 return syncRequest(request, (SyncMultiProcedure<T>)procedure);
2133 public <T> Collection<T> syncRequest(MultiRead<T> request,
2134 MultiListener<T> procedure) {
2135 return syncRequest(request, new NoneToSyncMultiListener<T>(procedure));
2139 public <T> Collection<T> syncRequest(MultiRead<T> request,
2140 SyncMultiProcedure<T> procedure) {
2142 assert (request != null);
2144 ListenerBase listener = getListenerBase(procedure);
2146 final ResultCallWrappedSyncQueryProcedure<T> wrapper = new ResultCallWrappedSyncQueryProcedure<T>(procedure);
2148 if (parent != null || listener != null) {
2150 // Object syncParent = request;
2152 // final ReadGraphImpl newGraph = newSync();
2154 processor.query(this, request, parent, wrapper, listener);
2156 // newGraph.waitAsync(syncParent);
2160 // Object syncParent = request;
2162 // final ReadGraphImpl newGraph = newSync();
2165 request.perform(this, wrapper);
2166 } catch (Throwable t) {
2167 wrapper.exception(this, t);
2172 return wrapper.get();
2177 public <T> Collection<T> syncRequest(MultiRead<T> request,
2178 MultiProcedure<T> procedure) {
2179 return syncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
2182 static class AsyncMultiReadProcedure<T> extends ArrayList<T> implements AsyncMultiProcedure<T> {
2184 private static Throwable DONE = new Throwable();
2186 private static final long serialVersionUID = -6494230465108115812L;
2188 Throwable exception = null;
2191 public synchronized void execute(AsyncReadGraph graph, T t) {
2196 public void finished(AsyncReadGraph graph) {
2201 public void exception(AsyncReadGraph graph, Throwable t) {
2205 public void checkAndThrow() throws DatabaseException {
2206 if(exception != DONE) {
2207 if (exception instanceof DatabaseException)
2208 throw (DatabaseException) exception;
2210 throw new DatabaseException(
2211 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
2216 public boolean done() {
2217 return exception != null;
2223 final public <T> Collection<T> syncRequest(AsyncMultiRead<T> request)
2224 throws DatabaseException {
2226 assert (request != null);
2228 final AsyncMultiReadProcedure<T> procedure = new AsyncMultiReadProcedure<T>();
2230 syncRequest(request, procedure);
2232 procedure.checkAndThrow();
2238 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2239 AsyncMultiListener<T> procedure) {
2240 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2244 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2245 SyncMultiListener<T> procedure) {
2246 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2250 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2251 MultiListener<T> procedure) {
2252 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2255 final private <T> void syncRequest(final AsyncMultiRead<T> request,
2256 final AsyncMultiReadProcedure<T> procedure) {
2258 assert (request != null);
2259 assert (procedure != null);
2261 ListenerBase listener = getListenerBase(procedure);
2263 if (parent != null || listener != null) {
2265 // Object syncParent = request;
2267 // final ReadGraphImpl newGraph = newSync();
2269 processor.query(this, request, parent, procedure, listener);
2271 // newGraph.waitAsync(syncParent);
2272 waitAsyncProcedure(procedure);
2276 // Object syncParent = callerThread == Integer.MIN_VALUE ? null
2279 // final ReadGraphImpl newGraph = newSyncAsync(syncParent);
2284 // ReadGraphImpl sync = newSync();
2285 request.perform(this, procedure);
2286 // sync.waitAsync(null);
2287 waitAsyncProcedure(procedure);
2290 } catch (Throwable t) {
2292 waitAsyncProcedure(procedure);
2303 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2304 final AsyncMultiProcedure<T> procedure) {
2306 assert (request != null);
2307 assert (procedure != null);
2309 ListenerBase listener = getListenerBase(procedure);
2311 if (parent != null || listener != null) {
2313 // Object syncParent = request;
2315 // final ReadGraphImpl newGraph = newSync();
2317 processor.query(this, request, parent, procedure, listener);
2319 // newGraph.waitAsync(syncParent);
2323 // Object syncParent = request;
2325 // final ReadGraphImpl newGraph = newSync();
2329 request.perform(this, new AsyncMultiProcedure<T>() {
2332 public void execute(AsyncReadGraph graph, T result) {
2333 procedure.execute(graph, result);
2337 public void finished(AsyncReadGraph graph) {
2338 procedure.finished(graph);
2342 public void exception(AsyncReadGraph graph, Throwable t) {
2343 procedure.exception(graph, t);
2347 public String toString() {
2348 return "syncRequest(AsyncMultiRead) -> " + procedure;
2353 } catch (Throwable t) {
2365 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2366 final SyncMultiProcedure<T> procedure) {
2367 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2371 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2372 final MultiProcedure<T> procedure) {
2373 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2377 public <T> T syncRequest(final ExternalRead<T> request)
2378 throws DatabaseException {
2380 assert (request != null);
2382 return syncRequest(request, new Procedure<T>() {
2385 public void execute(T t) {
2389 public void exception(Throwable t) {
2393 public String toString() {
2394 return "syncRequest(AsyncRead) -> " + request;
2402 public <T> T syncRequest(ExternalRead<T> request, Listener<T> procedure) throws DatabaseException {
2403 return syncRequest(request, (Procedure<T>) procedure);
2407 final public <T> T syncRequest(final ExternalRead<T> request,
2408 final Procedure<T> procedure) throws DatabaseException {
2410 assert (request != null);
2412 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
2413 return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
2418 public void syncRequest(final Write request) throws DatabaseException {
2420 assert (request != null);
2422 throw new DatabaseException(
2423 "Write operations are not supported during read transactions!");
2428 public <T> T syncRequest(final WriteResult<T> request) throws DatabaseException {
2430 assert (request != null);
2432 throw new DatabaseException(
2433 "Write operations are not supported during read transactions!");
2438 public void syncRequest(final DelayedWrite request)
2439 throws DatabaseException {
2441 assert (request != null);
2443 throw new DatabaseException(
2444 "Write operations are not supported during read transactions!");
2449 public <T> T syncRequest(final DelayedWriteResult<T> request) throws DatabaseException {
2451 assert (request != null);
2453 throw new DatabaseException(
2454 "Write operations are not supported during read transactions!");
2459 public void syncRequest(final WriteOnly request) throws DatabaseException {
2461 assert (request != null);
2463 throw new DatabaseException(
2464 "Write operations are not supported during read transactions!");
2469 public <T> T syncRequest(final WriteOnlyResult<T> request) throws DatabaseException {
2471 assert (request != null);
2473 throw new DatabaseException(
2474 "Write operations are not supported during read transactions!");
2479 public <T> void async(ReadInterface<T> r, AsyncProcedure<T> procedure) {
2480 r.request(this, procedure);
2484 public <T> void async(ReadInterface<T> r, Procedure<T> procedure) {
2485 r.request(this, procedure);
2489 public <T> void async(ReadInterface<T> r, SyncProcedure<T> procedure) {
2490 r.request(this, procedure);
2494 public <T> void async(ReadInterface<T> r, AsyncListener<T> procedure) {
2495 r.request(this, procedure);
2499 public <T> void async(ReadInterface<T> r, Listener<T> procedure) {
2500 r.request(this, procedure);
2504 public <T> void async(ReadInterface<T> r, SyncListener<T> procedure) {
2505 r.request(this, procedure);
2509 public <T> T sync(ReadInterface<T> r) throws DatabaseException {
2510 return r.request(this);
2514 public <T> T sync(WriteInterface<T> r) throws DatabaseException {
2515 return r.request(this);
2519 public <T> void async(WriteInterface<T> r, Procedure<T> procedure) {
2520 r.request(this, procedure);
2524 public <T> void async(WriteInterface<T> r) {
2525 r.request(this, new ProcedureAdapter<T>());
2529 * Implementation of the interface AsyncReadGraph
2533 public void forURI(Resource resource, AsyncListener<String> listener) {
2534 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2539 public void forURI(Resource resource, SyncListener<String> listener) {
2540 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2545 public void forURI(Resource resource, Listener<String> listener) {
2546 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2551 final public void forURI(final Resource resource,
2552 final AsyncProcedure<String> procedure) {
2554 assert (resource != null);
2555 assert (procedure != null);
2557 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2563 public void forURI(Resource resource, SyncProcedure<String> procedure) {
2564 forURI(resource, new SyncToAsyncProcedure<String>(procedure));
2568 public void forURI(Resource resource, Procedure<String> procedure) {
2569 forURI(resource, new NoneToAsyncProcedure<String>(procedure));
2573 public void forResource(String id, AsyncListener<Resource> listener) {
2574 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2579 public void forResource(String id, SyncListener<Resource> listener) {
2580 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2585 public void forResource(String id, Listener<Resource> listener) {
2586 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2591 final public void forResource(final String id,
2592 final AsyncProcedure<Resource> procedure) {
2594 assert (id != null);
2595 assert (procedure != null);
2597 processor.forResource(this, id, procedure);
2602 public void forResource(String id, SyncProcedure<Resource> procedure) {
2603 forResource(id, new SyncToAsyncProcedure<Resource>(procedure));
2607 public void forResource(String id, Procedure<Resource> procedure) {
2608 forResource(id, new NoneToAsyncProcedure<Resource>(procedure));
2612 public void forBuiltin(String id, AsyncListener<Resource> listener) {
2613 asyncRequest(new Builtin(id), listener);
2617 public void forBuiltin(String id, SyncListener<Resource> listener) {
2618 asyncRequest(new Builtin(id), listener);
2622 public void forBuiltin(String id, Listener<Resource> listener) {
2623 asyncRequest(new Builtin(id), listener);
2627 final public void forBuiltin(final String id,
2628 final AsyncProcedure<Resource> procedure) {
2630 assert (id != null);
2631 assert (procedure != null);
2633 processor.forBuiltin(this, id, procedure);
2638 public void forBuiltin(String id, SyncProcedure<Resource> procedure) {
2639 forBuiltin(id, new SyncToAsyncProcedure<Resource>(procedure));
2643 public void forBuiltin(String id, Procedure<Resource> procedure) {
2644 forBuiltin(id, new NoneToAsyncProcedure<Resource>(procedure));
2648 final public void forEachStatement(Resource subject, Resource relation,
2649 AsyncMultiProcedure<Statement> procedure) {
2651 assert (subject != null);
2652 assert (relation != null);
2653 assert (procedure != null);
2655 processor.forEachStatement(this, subject, relation, procedure);
2660 public void forEachStatement(Resource subject, Resource relation,
2661 SyncMultiProcedure<Statement> procedure) {
2662 forEachStatement(subject, relation,
2663 new SyncToAsyncMultiProcedure<Statement>(procedure));
2667 final public void forEachStatement(Resource subject, Resource relation,
2668 MultiProcedure<Statement> procedure) {
2670 assert (subject != null);
2671 assert (relation != null);
2672 assert (procedure != null);
2674 processor.forEachStatement(this, subject, relation, procedure);
2679 final public void forStatementSet(Resource subject, Resource relation,
2680 AsyncSetListener<Statement> procedure) {
2682 assert (subject != null);
2683 assert (relation != null);
2684 assert (procedure != null);
2686 processor.forStatementSet(this, subject, relation, procedure);
2691 final public void forStatementSet(Resource subject, Resource relation,
2692 SyncSetListener<Statement> procedure) {
2693 forStatementSet(subject, relation,
2694 new SyncToAsyncSetProcedure<Statement>(procedure));
2698 public void forStatementSet(Resource subject, Resource relation,
2699 SetListener<Statement> listener) {
2700 forStatementSet(subject, relation,
2701 new NoneToAsyncSetProcedure<Statement>(listener));
2705 final public void forEachAssertedStatement(final Resource subject,
2706 final Resource relation,
2707 final AsyncMultiProcedure<Statement> procedure) {
2709 assert (subject != null);
2710 assert (relation != null);
2711 assert (procedure != null);
2713 processor.forEachAssertedStatement(this, subject, relation, procedure);
2718 public void forEachAssertedStatement(Resource subject, Resource relation,
2719 SyncMultiProcedure<Statement> procedure) {
2720 forEachAssertedStatement(subject, relation,
2721 new SyncToAsyncMultiProcedure<Statement>(procedure));
2725 public void forEachAssertedStatement(Resource subject, Resource relation,
2726 MultiProcedure<Statement> procedure) {
2727 forEachAssertedStatement(subject, relation,
2728 new NoneToAsyncMultiProcedure<Statement>(procedure));
2732 public void forAssertedStatementSet(Resource subject, Resource relation,
2733 AsyncSetListener<Statement> procedure) {
2735 assert (subject != null);
2736 assert (relation != null);
2737 assert (procedure != null);
2739 processor.forAssertedStatementSet(this, subject, relation, procedure);
2744 public void forAssertedStatementSet(Resource subject, Resource relation,
2745 SyncSetListener<Statement> procedure) {
2747 assert (subject != null);
2748 assert (relation != null);
2749 assert (procedure != null);
2751 forAssertedStatementSet(subject, relation,
2752 new SyncToAsyncSetProcedure<Statement>(procedure));
2757 public void forAssertedStatementSet(Resource subject, Resource relation,
2758 SetListener<Statement> procedure) {
2760 assert (subject != null);
2761 assert (relation != null);
2762 assert (procedure != null);
2764 forAssertedStatementSet(subject, relation,
2765 new NoneToAsyncSetProcedure<Statement>(procedure));
2770 final public void forEachPredicate(final Resource subject,
2771 final AsyncMultiProcedure<Resource> procedure) {
2773 assert (subject != null);
2774 assert (procedure != null);
2776 processor.forEachPredicate(this, subject, procedure);
2781 public void forEachPredicate(Resource subject,
2782 SyncMultiProcedure<Resource> procedure) {
2783 forEachPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(
2788 final public void forEachPredicate(final Resource subject,
2789 final MultiProcedure<Resource> procedure) {
2791 assert (subject != null);
2792 assert (procedure != null);
2794 processor.forEachPredicate(this, subject, procedure);
2799 final public void forPredicateSet(final Resource subject,
2800 final AsyncSetListener<Resource> procedure) {
2802 assert (subject != null);
2803 assert (procedure != null);
2805 processor.forPredicateSet(this, subject, procedure);
2810 final public void forPredicateSet(final Resource subject,
2811 final SyncSetListener<Resource> procedure) {
2813 assert (subject != null);
2814 assert (procedure != null);
2816 forPredicateSet(subject, new SyncToAsyncSetProcedure<Resource>(
2822 final public void forPredicateSet(final Resource subject,
2823 final SetListener<Resource> procedure) {
2825 assert (subject != null);
2826 assert (procedure != null);
2828 forPredicateSet(subject, new NoneToAsyncSetProcedure<Resource>(
2834 final public void forEachPrincipalType(final Resource subject,
2835 final AsyncMultiProcedure<Resource> procedure) {
2837 assert (subject != null);
2838 assert (procedure != null);
2840 processor.forEachPrincipalType(this, subject, procedure);
2845 public void forEachPrincipalType(Resource subject,
2846 SyncMultiProcedure<Resource> procedure) {
2847 forEachPrincipalType(subject, new SyncToAsyncMultiProcedure<Resource>(
2852 final public void forEachPrincipalType(final Resource subject,
2853 final MultiProcedure<Resource> procedure) {
2855 assert (subject != null);
2856 assert (procedure != null);
2858 processor.forEachPrincipalType(this, subject, procedure);
2863 final public void forPrincipalTypeSet(final Resource subject,
2864 final AsyncSetListener<Resource> procedure) {
2866 assert (subject != null);
2867 assert (procedure != null);
2869 processor.forPrincipalTypeSet(this, subject, procedure);
2874 final public void forPrincipalTypeSet(final Resource subject,
2875 final SyncSetListener<Resource> procedure) {
2877 assert (subject != null);
2878 assert (procedure != null);
2880 forPrincipalTypeSet(subject, new SyncToAsyncSetProcedure<Resource>(
2886 final public void forPrincipalTypeSet(final Resource subject,
2887 final SetListener<Resource> procedure) {
2889 assert (subject != null);
2890 assert (procedure != null);
2892 forPrincipalTypeSet(subject, new NoneToAsyncSetProcedure<Resource>(
2898 public void forTypes(Resource subject, AsyncListener<Set<Resource>> listener) {
2899 asyncRequest(new Types(subject), listener);
2903 public void forTypes(Resource subject, SyncListener<Set<Resource>> listener) {
2904 asyncRequest(new Types(subject), listener);
2908 public void forTypes(Resource subject, Listener<Set<Resource>> listener) {
2909 asyncRequest(new Types(subject), listener);
2913 final public void forTypes(final Resource subject,
2914 final AsyncProcedure<Set<Resource>> procedure) {
2916 assert (subject != null);
2917 assert (procedure != null);
2919 processor.forTypes(this, subject, procedure);
2924 public void forTypes(Resource subject,
2925 SyncProcedure<Set<Resource>> procedure) {
2926 forTypes(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
2930 public void forTypes(Resource subject, Procedure<Set<Resource>> procedure) {
2931 forTypes(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
2935 public void forSupertypes(Resource subject,
2936 AsyncListener<Set<Resource>> listener) {
2937 asyncRequest(new Types(subject), listener);
2941 public void forSupertypes(Resource subject,
2942 SyncListener<Set<Resource>> listener) {
2943 asyncRequest(new Types(subject), listener);
2947 public void forSupertypes(Resource subject, Listener<Set<Resource>> listener) {
2948 asyncRequest(new Types(subject), listener);
2952 final public void forSupertypes(final Resource subject,
2953 final AsyncProcedure<Set<Resource>> procedure) {
2955 assert (subject != null);
2956 assert (procedure != null);
2958 processor.forSupertypes(this, subject, procedure);
2963 public void forSupertypes(Resource subject,
2964 SyncProcedure<Set<Resource>> procedure) {
2965 forSupertypes(subject, new SyncToAsyncProcedure<Set<Resource>>(
2970 public void forSupertypes(Resource subject,
2971 Procedure<Set<Resource>> procedure) {
2972 forSupertypes(subject, new NoneToAsyncProcedure<Set<Resource>>(
2977 public void forDirectSuperrelations(Resource subject,
2978 AsyncMultiProcedure<Resource> procedure) {
2980 assert (subject != null);
2981 assert (procedure != null);
2983 processor.forDirectSuperrelations(this, subject, procedure);
2988 public void forPossibleSuperrelation(Resource subject, AsyncProcedure<Resource> procedure) {
2990 assert (subject != null);
2991 assert (procedure != null);
2993 processor.forPossibleSuperrelation(this, subject, procedure);
2998 public void forSuperrelations(Resource subject,
2999 AsyncListener<Set<Resource>> listener) {
3000 asyncRequest(new Types(subject), listener);
3004 public void forSuperrelations(Resource subject,
3005 SyncListener<Set<Resource>> listener) {
3006 asyncRequest(new Types(subject), listener);
3010 public void forSuperrelations(Resource subject,
3011 Listener<Set<Resource>> listener) {
3012 asyncRequest(new Types(subject), listener);
3016 final public void forSuperrelations(final Resource subject,
3017 final AsyncProcedure<Set<Resource>> procedure) {
3019 assert (subject != null);
3020 assert (procedure != null);
3022 processor.forSuperrelations(this, subject, procedure);
3027 public void forSuperrelations(Resource subject,
3028 SyncProcedure<Set<Resource>> procedure) {
3029 forSuperrelations(subject, new SyncToAsyncProcedure<Set<Resource>>(
3034 public void forSuperrelations(Resource subject,
3035 Procedure<Set<Resource>> procedure) {
3036 forSuperrelations(subject, new NoneToAsyncProcedure<Set<Resource>>(
3041 final public void forEachObject(final Resource subject, final Resource relation, final AsyncMultiProcedure<Resource> procedure) {
3042 processor.forEachObject(this, subject, relation, procedure);
3046 public void forEachObject(Resource subject, Resource relation,
3047 SyncMultiProcedure<Resource> procedure) {
3048 forEachObject(subject, relation,
3049 new SyncToAsyncMultiProcedure<Resource>(procedure));
3053 public void forEachObject(Resource subject, Resource relation,
3054 MultiProcedure<Resource> procedure) {
3056 processor.forEachObject(this, subject, relation, procedure);
3061 final public void forEachDirectPredicate(final Resource subject, final AsyncProcedure<Set<Resource>> procedure) {
3062 processor.forEachDirectPredicate(this, subject, procedure);
3066 final public void forEachDirectPredicate(final Resource subject, final SyncProcedure<Set<Resource>> procedure) {
3067 forEachDirectPredicate(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
3071 public void forEachDirectPredicate(Resource subject, Procedure<Set<Resource>> procedure) {
3072 forEachDirectPredicate(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
3076 final public void forObjectSet(final Resource subject,
3077 final Resource relation, final AsyncSetListener<Resource> procedure) {
3079 assert (subject != null);
3080 assert (relation != null);
3081 assert (procedure != null);
3083 processor.forObjectSet(this, subject, relation, procedure);
3088 final public void forObjectSet(final Resource subject,
3089 final Resource relation, final SyncSetListener<Resource> procedure) {
3091 assert (subject != null);
3092 assert (relation != null);
3093 assert (procedure != null);
3095 forObjectSet(subject, relation, new SyncToAsyncSetProcedure<Resource>(
3101 final public void forObjectSet(final Resource subject,
3102 final Resource relation, final SetListener<Resource> procedure) {
3104 assert (subject != null);
3105 assert (relation != null);
3106 assert (procedure != null);
3108 forObjectSet(subject, relation, new NoneToAsyncSetProcedure<Resource>(
3114 final public void forEachAssertedObject(final Resource subject,
3115 final Resource relation,
3116 final AsyncMultiProcedure<Resource> procedure) {
3118 assert (subject != null);
3119 assert (relation != null);
3120 assert (procedure != null);
3122 processor.forEachAssertedObject(this, subject, relation, procedure);
3127 public void forEachAssertedObject(Resource subject, Resource relation,
3128 SyncMultiProcedure<Resource> procedure) {
3130 assert (subject != null);
3131 assert (relation != null);
3132 assert (procedure != null);
3134 forEachAssertedObject(subject, relation,
3135 new SyncToAsyncMultiProcedure<Resource>(procedure));
3140 public void forEachAssertedObject(Resource subject, Resource relation,
3141 MultiProcedure<Resource> procedure) {
3143 assert (subject != null);
3144 assert (relation != null);
3145 assert (procedure != null);
3147 forEachAssertedObject(subject, relation,
3148 new NoneToAsyncMultiProcedure<Resource>(procedure));
3153 public void forAssertedObjectSet(Resource subject, Resource relation,
3154 AsyncSetListener<Resource> procedure) {
3156 assert (subject != null);
3157 assert (relation != null);
3158 assert (procedure != null);
3160 processor.forAssertedObjectSet(this, subject, relation, procedure);
3165 public void forAssertedObjectSet(Resource subject, Resource relation,
3166 SyncSetListener<Resource> procedure) {
3168 assert (subject != null);
3169 assert (relation != null);
3170 assert (procedure != null);
3172 forAssertedObjectSet(subject, relation,
3173 new SyncToAsyncSetProcedure<Resource>(procedure));
3178 public void forAssertedObjectSet(Resource subject, Resource relation,
3179 SetListener<Resource> procedure) {
3181 assert (subject != null);
3182 assert (relation != null);
3183 assert (procedure != null);
3185 forAssertedObjectSet(subject, relation,
3186 new NoneToAsyncSetProcedure<Resource>(procedure));
3191 public void forInverse(Resource relation, AsyncListener<Resource> listener) {
3192 asyncRequest(new Inverse(relation), listener);
3196 public void forInverse(Resource relation, SyncListener<Resource> listener) {
3197 asyncRequest(new Inverse(relation), listener);
3201 public void forInverse(Resource relation, Listener<Resource> listener) {
3202 asyncRequest(new Inverse(relation), listener);
3206 final public void forInverse(final Resource relation,
3207 final AsyncProcedure<Resource> procedure) {
3209 assert (relation != null);
3210 assert (procedure != null);
3212 processor.forInverse(this, relation, new AsyncProcedure<Resource>() {
3215 public void execute(AsyncReadGraph graph, Resource result) {
3217 procedure.execute(graph, result);
3219 procedure.exception(graph, new NoInverseException(relation
3225 public void exception(AsyncReadGraph graph, Throwable throwable) {
3226 procedure.exception(graph, throwable);
3230 public String toString() {
3231 return "forInverse -> " + procedure;
3239 public void forInverse(Resource relation, SyncProcedure<Resource> procedure) {
3240 forInverse(relation, new SyncToAsyncProcedure<Resource>(procedure));
3244 public void forInverse(Resource relation, Procedure<Resource> procedure) {
3245 forInverse(relation, new NoneToAsyncProcedure<Resource>(procedure));
3249 public void forSingleObject(Resource subject, Resource relation,
3250 AsyncListener<Resource> listener) {
3251 asyncRequest(new SingleObject(subject, relation), listener);
3255 public void forSingleObject(Resource subject, Resource relation,
3256 SyncListener<Resource> listener) {
3257 asyncRequest(new SingleObject(subject, relation), listener);
3261 public void forSingleObject(Resource subject, Resource relation,
3262 Listener<Resource> listener) {
3263 asyncRequest(new SingleObject(subject, relation), listener);
3267 final public void forSingleObject(final Resource subject,
3268 final Resource relation, final AsyncProcedure<Resource> procedure) {
3270 assert (subject != null);
3271 assert (relation != null);
3272 assert (procedure != null);
3274 processor.forEachObject(this, subject, relation,
3275 new SingleOrErrorProcedure<Resource>(procedure));
3280 public void forSingleObject(Resource subject, Resource relation,
3281 SyncProcedure<Resource> procedure) {
3282 forSingleObject(subject, relation, new SyncToAsyncProcedure<Resource>(
3287 public void forSingleObject(Resource subject, Resource relation,
3288 Procedure<Resource> procedure) {
3289 forSingleObject(subject, relation, new NoneToAsyncProcedure<Resource>(
3294 public void forSingleStatement(Resource subject, Resource relation,
3295 AsyncListener<Statement> listener) {
3296 asyncRequest(new SingleStatement(subject, relation), listener);
3300 public void forSingleStatement(Resource subject, Resource relation,
3301 SyncListener<Statement> listener) {
3302 asyncRequest(new SingleStatement(subject, relation), listener);
3306 public void forSingleStatement(Resource subject, Resource relation,
3307 Listener<Statement> listener) {
3308 asyncRequest(new SingleStatement(subject, relation), listener);
3312 final public void forSingleStatement(final Resource subject,
3313 final Resource relation, final AsyncProcedure<Statement> procedure) {
3315 assert (subject != null);
3316 assert (relation != null);
3317 assert (procedure != null);
3319 processor.forEachStatement(this, subject, relation,
3320 new SingleOrErrorProcedure<Statement>(procedure));
3325 public void forSingleStatement(Resource subject, Resource relation,
3326 SyncProcedure<Statement> procedure) {
3327 forSingleStatement(subject, relation,
3328 new SyncToAsyncProcedure<Statement>(procedure));
3332 public void forSingleStatement(Resource subject, Resource relation,
3333 Procedure<Statement> procedure) {
3334 forSingleStatement(subject, relation,
3335 new NoneToAsyncProcedure<Statement>(procedure));
3339 public void forSingleType(Resource subject,
3340 AsyncListener<Resource> listener) {
3341 asyncRequest(new SingleTypeAny(subject), listener);
3345 public void forSingleType(Resource subject,
3346 SyncListener<Resource> listener) {
3347 asyncRequest(new SingleTypeAny(subject), listener);
3351 public void forSingleType(Resource subject,
3352 Listener<Resource> listener) {
3353 asyncRequest(new SingleTypeAny(subject), listener);
3357 final public void forSingleType(final Resource subject, final AsyncProcedure<Resource> procedure) {
3359 assert (subject != null);
3360 assert (procedure != null);
3362 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3364 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
3367 public void execute(AsyncReadGraph graph, final Resource principalType) {
3368 checkedProcedure.offer(graph, principalType);
3372 public void finished(AsyncReadGraph graph) {
3373 checkedProcedure.dec(graph);
3377 public void exception(AsyncReadGraph graph, Throwable t) {
3378 checkedProcedure.exception(graph, t);
3382 public String toString() {
3383 return "forSingleType -> " + procedure;
3391 public void forSingleType(Resource subject, SyncProcedure<Resource> procedure) {
3392 forSingleType(subject, new SyncToAsyncProcedure<Resource>(
3397 public void forSingleType(Resource subject, Procedure<Resource> procedure) {
3398 forSingleType(subject, new NoneToAsyncProcedure<Resource>(
3403 public void forSingleType(Resource subject, Resource relation,
3404 AsyncListener<Resource> listener) {
3405 asyncRequest(new SingleType(subject, relation), listener);
3409 public void forSingleType(Resource subject, Resource relation,
3410 SyncListener<Resource> listener) {
3411 asyncRequest(new SingleType(subject, relation), listener);
3415 public void forSingleType(Resource subject, Resource relation,
3416 Listener<Resource> listener) {
3417 asyncRequest(new SingleType(subject, relation), listener);
3421 final public void forSingleType(final Resource subject,
3422 final Resource baseType, final AsyncProcedure<Resource> procedure) {
3424 assert (subject != null);
3425 assert (procedure != null);
3427 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3429 processor.forEachPrincipalType(this, subject,
3430 new AsyncMultiProcedureAdapter<Resource>() {
3433 public void execute(AsyncReadGraph graph,
3434 final Resource principalType) {
3436 checkedProcedure.inc();
3438 if(baseType == null) {
3440 checkedProcedure.offer(graph, principalType);
3441 checkedProcedure.dec(graph);
3443 } else if(principalType.equals(baseType)) {
3445 checkedProcedure.offer(graph, principalType);
3446 checkedProcedure.dec(graph);
3450 processor.forSupertypes((ReadGraphImpl)graph, principalType,
3451 new AsyncProcedure<Set<Resource>>() {
3454 public void execute(
3455 AsyncReadGraph graph,
3456 Set<Resource> result) {
3458 if (result.contains(baseType))
3459 checkedProcedure.offer(graph,
3461 checkedProcedure.dec(graph);
3466 public void exception(
3467 AsyncReadGraph graph,
3470 .exception(graph, t);
3480 public void finished(AsyncReadGraph graph) {
3481 checkedProcedure.dec(graph);
3485 public void exception(AsyncReadGraph graph, Throwable t) {
3486 checkedProcedure.exception(graph, t);
3490 public String toString() {
3491 return "forSingleType -> " + procedure;
3499 public void forSingleType(Resource subject, Resource relation,
3500 SyncProcedure<Resource> procedure) {
3501 forSingleType(subject, relation, new SyncToAsyncProcedure<Resource>(
3506 public void forSingleType(Resource subject, Resource relation,
3507 Procedure<Resource> procedure) {
3508 forSingleType(subject, relation, new NoneToAsyncProcedure<Resource>(
3513 public <T> void forValue(Resource subject, Binding binding,
3514 AsyncListener<T> listener) {
3515 asyncRequest(new Value<T>(subject, binding), listener);
3519 public <T> void forValue(Resource subject, Binding binding,
3520 SyncListener<T> listener) {
3521 asyncRequest(new Value<T>(subject, binding), listener);
3525 public <T> void forValue(Resource subject, Binding binding,
3526 Listener<T> listener) {
3527 asyncRequest(new Value<T>(subject, binding), listener);
3531 public <T> void forValue(final Resource resource, final Binding binding,
3532 final AsyncProcedure<T> procedure) {
3534 assert (resource != null);
3535 assert (binding != null);
3536 assert (procedure != null);
3538 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
3541 public void execute(AsyncReadGraph graph, byte[] result) {
3545 if (result == null) {
3546 procedure.exception(graph,
3547 new DoesNotContainValueException(
3548 "No value for resource " + resource));
3552 Serializer serializer = binding.serializer();
3553 // Serializer serializer = Bindings.getSerializer( binding );
3554 Object obj = serializer.deserialize(result);
3555 // if (!binding.isInstance(obj))
3556 // procedure.exception(graph, new ClassCastException(
3557 // "Cannot get value " + obj + " with binding "
3560 procedure.execute(graph, (T) obj);
3562 } catch (IOException e) {
3563 procedure.exception(graph, e);
3564 } catch (BufferUnderflowException e) {
3565 procedure.exception(graph, e);
3566 } catch (Throwable t) {
3567 procedure.exception(graph, t);
3573 public void exception(AsyncReadGraph graph, Throwable t) {
3575 procedure.exception(graph, t);
3576 } catch (Throwable t2) {
3577 Logger.defaultLogError(t2);
3582 public String toString() {
3583 return "forValue -> " + procedure;
3591 public <T> void forValue(Resource subject, Binding binding,
3592 SyncProcedure<T> procedure) {
3593 forValue(subject, binding, new SyncToAsyncProcedure<T>(procedure));
3597 public <T> void forValue(Resource subject, Binding binding,
3598 Procedure<T> procedure) {
3599 forValue(subject, binding, new NoneToAsyncProcedure<T>(procedure));
3603 public <T> void forValue(Resource subject, AsyncListener<T> listener) {
3604 asyncRequest(new ValueImplied<T>(subject), listener);
3608 public <T> void forValue(Resource subject, SyncListener<T> listener) {
3609 asyncRequest(new ValueImplied<T>(subject), listener);
3613 public <T> void forValue(Resource subject, Listener<T> listener) {
3614 asyncRequest(new ValueImplied<T>(subject), listener);
3618 final public <T> void forValue(final Resource subject, final AsyncProcedure<T> procedure) {
3620 assert (subject != null);
3621 assert (procedure != null);
3623 forRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
3626 public void execute(AsyncReadGraph graph, Datatype type) {
3627 // TODO: consider trying Bindings.getBeanBinding(type);
3628 Binding binding = Bindings.getBinding(type);
3629 graph.forValue(subject, binding, procedure);
3633 public void exception(AsyncReadGraph graph, Throwable throwable) {
3634 procedure.exception(graph, new DoesNotContainValueException("Invalid data type", throwable));
3642 public <T> void forValue(Resource subject, SyncProcedure<T> procedure) {
3643 forValue(subject, new SyncToAsyncProcedure<T>(procedure));
3647 public <T> void forValue(Resource subject, Procedure<T> procedure) {
3648 forValue(subject, new NoneToAsyncProcedure<T>(procedure));
3652 public <T> void forRelatedValue(Resource subject, Resource relation,
3653 AsyncListener<T> listener) {
3654 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3658 public <T> void forRelatedValue(Resource subject, Resource relation,
3659 SyncListener<T> listener) {
3660 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3664 public <T> void forRelatedValue(Resource subject, Resource relation,
3665 Listener<T> listener) {
3666 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3670 final public <T> void forRelatedValue(final Resource subject,
3671 final Resource relation, final AsyncProcedure<T> procedure) {
3673 assert (subject != null);
3674 assert (relation != null);
3675 assert (procedure != null);
3677 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3679 processor.forEachObject(this, subject, relation,
3680 new AsyncMultiProcedureAdapter<Resource>() {
3683 public void execute(AsyncReadGraph graph,
3684 final Resource object) {
3686 checkedProcedure.inc();
3688 graph.forValue(object, new AsyncProcedure<Object>() {
3691 public void execute(AsyncReadGraph graph,
3693 checkedProcedure.offer(graph, (T) result);
3694 checkedProcedure.dec(graph);
3698 public void exception(AsyncReadGraph graph,
3700 checkedProcedure.exception(graph, t);
3704 public String toString() {
3705 return "forRelatedValue -> " + procedure;
3713 public void finished(AsyncReadGraph graph) {
3714 checkedProcedure.dec(graph);
3718 public void exception(AsyncReadGraph graph, Throwable t) {
3719 checkedProcedure.exception(graph, t);
3727 public <T> void forRelatedValue(Resource subject, Resource relation,
3728 SyncProcedure<T> procedure) {
3729 forRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
3734 public <T> void forRelatedValue(Resource subject, Resource relation,
3735 Procedure<T> procedure) {
3736 forRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
3741 public <T> void forRelatedValue(Resource subject, Resource relation,
3742 Binding binding, AsyncListener<T> listener) {
3743 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3747 public <T> void forRelatedValue(Resource subject, Resource relation,
3748 Binding binding, SyncListener<T> listener) {
3749 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3753 public <T> void forRelatedValue(Resource subject, Resource relation,
3754 Binding binding, Listener<T> listener) {
3755 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3759 final public <T> void forRelatedValue(final Resource subject,
3760 final Resource relation, final Binding binding,
3761 final AsyncProcedure<T> procedure) {
3763 assert (subject != null);
3764 assert (relation != null);
3765 assert (binding != null);
3766 assert (procedure != null);
3768 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3770 processor.forEachObject(this, subject, relation,
3771 new AsyncMultiProcedureAdapter<Resource>() {
3774 public void execute(AsyncReadGraph graph,
3775 final Resource object) {
3777 checkedProcedure.inc();
3779 graph.forValue(object, binding, new AsyncProcedure<Object>() {
3782 public void execute(AsyncReadGraph graph,
3785 checkedProcedure.offer(graph,
3787 checkedProcedure.dec(graph);
3791 public void exception(AsyncReadGraph graph,
3793 checkedProcedure.exception(graph, t);
3797 public String toString() {
3798 return "forRelatedValue -> "
3807 public void finished(AsyncReadGraph graph) {
3808 checkedProcedure.dec(graph);
3812 public void exception(AsyncReadGraph graph, Throwable t) {
3813 checkedProcedure.exception(graph, t);
3821 public <T> void forRelatedValue(Resource subject, Resource relation,
3822 Binding binding, SyncProcedure<T> procedure) {
3823 forRelatedValue(subject, relation, binding,
3824 new SyncToAsyncProcedure<T>(procedure));
3828 public <T> void forRelatedValue(Resource subject, Resource relation,
3829 Binding binding, Procedure<T> procedure) {
3830 forRelatedValue(subject, relation, binding,
3831 new NoneToAsyncProcedure<T>(procedure));
3835 public <T> void forAdapted(Resource resource, Class<T> clazz,
3836 AsyncListener<T> listener) {
3837 asyncRequest(new Adapter<T>(resource, clazz), listener);
3841 public <T> void forAdapted(Resource resource, Class<T> clazz,
3842 SyncListener<T> listener) {
3843 asyncRequest(new Adapter<T>(resource, clazz), listener);
3847 public <T> void forAdapted(Resource resource, Class<T> clazz,
3848 Listener<T> listener) {
3849 asyncRequest(new Adapter<T>(resource, clazz), listener);
3853 final public <T> void forAdapted(final Resource resource,
3854 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3856 assert (resource != null);
3857 assert (clazz != null);
3858 assert (procedure != null);
3860 final AdaptionService service = getSession().peekService(AdaptionService.class);
3861 if (service == null)
3862 procedure.exception(this, new ServiceException("No AdaptionService available"));
3864 service.adapt(this, resource, resource, Resource.class, clazz, false, procedure);
3869 public <T> void forAdapted(Resource resource, Class<T> clazz,
3870 SyncProcedure<T> procedure) {
3871 forAdapted(resource, clazz, new SyncToAsyncProcedure<T>(procedure));
3875 public <T> void forAdapted(Resource resource, Class<T> clazz,
3876 Procedure<T> procedure) {
3877 forAdapted(resource, clazz, new NoneToAsyncProcedure<T>(procedure));
3881 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3882 AsyncListener<T> listener) {
3883 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3887 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3888 SyncListener<T> listener) {
3889 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3893 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3894 Listener<T> listener) {
3895 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3899 final public <T> void forUniqueAdapted(final Resource resource,
3900 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3902 assert (resource != null);
3903 assert (clazz != null);
3904 assert (procedure != null);
3906 final AdaptionService service = getSession().peekService(AdaptionService.class);
3907 if (service == null)
3908 procedure.exception(this, new ServiceException("No AdaptionService available"));
3910 service.adaptNew(this, resource, clazz, false, procedure);
3915 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3916 SyncProcedure<T> procedure) {
3917 forUniqueAdapted(resource, clazz,
3918 new SyncToAsyncProcedure<T>(procedure));
3922 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3923 Procedure<T> procedure) {
3924 forUniqueAdapted(resource, clazz,
3925 new NoneToAsyncProcedure<T>(procedure));
3929 public void forPossibleInverse(Resource subject,
3930 AsyncListener<Resource> listener) {
3931 asyncRequest(new PossibleInverse(subject), listener);
3935 public void forPossibleInverse(Resource subject,
3936 SyncListener<Resource> listener) {
3937 asyncRequest(new PossibleInverse(subject), listener);
3941 public void forPossibleInverse(Resource subject, Listener<Resource> listener) {
3942 asyncRequest(new PossibleInverse(subject), listener);
3946 final public void forPossibleInverse(final Resource relation,
3947 final AsyncProcedure<Resource> procedure) {
3949 assert (relation != null);
3950 assert (procedure != null);
3952 processor.forInverse(this, relation, new ExceptionToNullProcedure<Resource>(procedure));
3957 public void forPossibleInverse(Resource subject,
3958 SyncProcedure<Resource> procedure) {
3959 forPossibleInverse(subject, new SyncToAsyncProcedure<Resource>(
3964 public void forPossibleInverse(Resource subject,
3965 Procedure<Resource> procedure) {
3966 forPossibleInverse(subject, new NoneToAsyncProcedure<Resource>(
3971 public void forPossibleObject(Resource subject, Resource relation,
3972 AsyncListener<Resource> listener) {
3973 asyncRequest(new PossibleObject(subject, relation), listener);
3977 public void forPossibleObject(Resource subject, Resource relation,
3978 SyncListener<Resource> listener) {
3979 asyncRequest(new PossibleObject(subject, relation), listener);
3983 public void forPossibleObject(Resource subject, Resource relation,
3984 Listener<Resource> listener) {
3985 asyncRequest(new PossibleObject(subject, relation), listener);
3989 final public void forPossibleObject(final Resource subject,
3990 final Resource relation, final AsyncProcedure<Resource> procedure) {
3992 assert (subject != null);
3993 assert (relation != null);
3994 assert (procedure != null);
3996 processor.forEachObject(this, subject, relation,
3997 new SingleOrNullProcedure<Resource>(procedure));
4002 public void forPossibleObject(Resource subject, Resource relation,
4003 SyncProcedure<Resource> procedure) {
4004 forPossibleObject(subject, relation,
4005 new SyncToAsyncProcedure<Resource>(procedure));
4009 public void forPossibleObject(Resource subject, Resource relation,
4010 Procedure<Resource> procedure) {
4011 forPossibleObject(subject, relation,
4012 new NoneToAsyncProcedure<Resource>(procedure));
4016 public void forPossibleStatement(Resource subject, Resource relation,
4017 AsyncListener<Statement> listener) {
4018 asyncRequest(new PossibleStatement(subject, relation), listener);
4022 public void forPossibleStatement(Resource subject, Resource relation,
4023 SyncListener<Statement> listener) {
4024 asyncRequest(new PossibleStatement(subject, relation), listener);
4028 public void forPossibleStatement(Resource subject, Resource relation,
4029 Listener<Statement> listener) {
4030 asyncRequest(new PossibleStatement(subject, relation), listener);
4034 final public void forPossibleStatement(final Resource subject,
4035 final Resource relation, final AsyncProcedure<Statement> procedure) {
4037 assert (subject != null);
4038 assert (relation != null);
4039 assert (procedure != null);
4041 processor.forEachStatement(this, subject, relation,
4042 new SingleFunctionalOrNullProcedure<Statement>(
4043 "forPossibleStatement", procedure));
4048 public void forPossibleStatement(Resource subject, Resource relation,
4049 SyncProcedure<Statement> procedure) {
4050 forPossibleStatement(subject, relation,
4051 new SyncToAsyncProcedure<Statement>(procedure));
4055 public void forPossibleStatement(Resource subject, Resource relation,
4056 Procedure<Statement> procedure) {
4057 forPossibleStatement(subject, relation,
4058 new NoneToAsyncProcedure<Statement>(procedure));
4062 public void forPossibleType(Resource subject, Resource relation,
4063 AsyncListener<Resource> listener) {
4064 asyncRequest(new PossibleType(subject, relation), listener);
4068 public void forPossibleType(Resource subject, Resource relation,
4069 SyncListener<Resource> listener) {
4070 asyncRequest(new PossibleType(subject, relation), listener);
4074 public void forPossibleType(Resource subject, Resource relation,
4075 Listener<Resource> listener) {
4076 asyncRequest(new PossibleType(subject, relation), listener);
4080 final public void forPossibleType(final Resource subject,
4081 final Resource baseType, final AsyncProcedure<Resource> procedure) {
4083 assert (subject != null);
4084 assert (procedure != null);
4086 final NullSingleOrNullProcedure<Resource> checkedProcedure = new NullSingleOrNullProcedure<Resource>(procedure);
4088 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
4091 public void execute(AsyncReadGraph graph,
4092 final Resource principalType) {
4094 if (baseType == null) {
4096 checkedProcedure.offer(graph, principalType);
4098 } else if (principalType.equals(baseType)) {
4100 checkedProcedure.offer(graph, principalType);
4104 checkedProcedure.inc();
4106 processor.forSupertypes((ReadGraphImpl)graph, principalType,
4107 new AsyncProcedure<Set<Resource>>() {
4110 public void execute(
4111 AsyncReadGraph graph,
4112 Set<Resource> result) {
4114 if (result.contains(baseType)) {
4115 checkedProcedure.offer(graph,
4119 checkedProcedure.dec(graph);
4124 public void exception(
4125 AsyncReadGraph graph,
4127 checkedProcedure.exception(graph, t);
4128 checkedProcedure.dec(graph);
4132 public String toString() {
4133 return "forPossibleType -> "
4144 public void finished(AsyncReadGraph graph) {
4145 checkedProcedure.dec(graph);
4149 public void exception(AsyncReadGraph graph, Throwable t) {
4150 checkedProcedure.exception(graph, t);
4151 checkedProcedure.dec(graph);
4159 public void forPossibleType(Resource subject, Resource relation,
4160 SyncProcedure<Resource> procedure) {
4161 forPossibleType(subject, relation, new SyncToAsyncProcedure<Resource>(
4166 public void forPossibleType(Resource subject, Resource relation,
4167 Procedure<Resource> procedure) {
4168 forPossibleType(subject, relation, new NoneToAsyncProcedure<Resource>(
4173 public <T> void forPossibleValue(Resource subject, AsyncListener<T> listener) {
4174 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4178 public <T> void forPossibleValue(Resource subject, SyncListener<T> listener) {
4179 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4183 public <T> void forPossibleValue(Resource subject, Listener<T> listener) {
4184 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4188 final public <T> void forPossibleValue(final Resource subject,
4189 final AsyncProcedure<T> procedure) {
4191 assert (subject != null);
4192 assert (procedure != null);
4194 forPossibleRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
4197 public void execute(AsyncReadGraph graph, final Datatype type) {
4199 procedure.execute(graph, null);
4202 // TODO: consider trying Bindings.getBeanBinding(type);
4203 Binding binding = Bindings.getBinding(type);
4204 graph.forPossibleValue(subject, binding, procedure);
4205 } catch (RuntimeBindingConstructionException e) {
4206 procedure.exception(graph, e);
4212 public void exception(AsyncReadGraph graph, Throwable t) {
4213 procedure.exception(graph, t);
4217 public String toString() {
4218 return "forPossibleValue -> " + procedure;
4226 public <T> void forPossibleValue(Resource subject,
4227 SyncProcedure<T> procedure) {
4228 forPossibleValue(subject, new SyncToAsyncProcedure<T>(procedure));
4232 public <T> void forPossibleValue(Resource subject, Procedure<T> procedure) {
4233 forPossibleValue(subject, new NoneToAsyncProcedure<T>(procedure));
4237 public <T> void forPossibleValue(Resource subject, Binding binding,
4238 AsyncListener<T> listener) {
4239 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4243 public <T> void forPossibleValue(Resource subject, Binding binding,
4244 SyncListener<T> listener) {
4245 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4249 public <T> void forPossibleValue(Resource subject, Binding binding,
4250 Listener<T> listener) {
4251 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4255 final public <T> void forPossibleValue(final Resource resource,
4256 final Binding binding, final AsyncProcedure<T> procedure) {
4258 assert (resource != null);
4259 assert (binding != null);
4260 assert (procedure != null);
4262 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
4265 public void execute(AsyncReadGraph graph, byte[] result) {
4269 if (result == null) {
4270 procedure.execute(graph, null);
4274 Serializer serializer = Bindings.getSerializer( binding );
4275 Object obj = serializer.deserialize(result);
4276 if (!binding.isInstance(obj))
4277 procedure.exception(graph, new ClassCastException(
4278 "Cannot get value " + obj + " with binding "
4281 procedure.execute(graph, (T) obj);
4283 } catch (IOException e) {
4284 procedure.exception(graph, e);
4285 } catch (BufferUnderflowException e) {
4286 procedure.exception(graph, e);
4287 } catch (Throwable t) {
4288 procedure.exception(graph, t);
4294 public void exception(AsyncReadGraph graph, Throwable t) {
4296 procedure.exception(graph, t);
4297 } catch (Throwable t2) {
4298 Logger.defaultLogError(t2);
4303 public String toString() {
4304 return "forPossibleValue -> " + procedure;
4312 public <T> void forPossibleValue(Resource subject, Binding binding,
4313 SyncProcedure<T> procedure) {
4314 forPossibleValue(subject, binding, new SyncToAsyncProcedure<T>(
4319 public <T> void forPossibleValue(Resource subject, Binding binding,
4320 Procedure<T> procedure) {
4321 forPossibleValue(subject, binding, new NoneToAsyncProcedure<T>(
4326 public <T> void forPossibleRelatedValue(Resource subject,
4327 Resource relation, AsyncListener<T> listener) {
4328 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4333 public <T> void forPossibleRelatedValue(Resource subject,
4334 Resource relation, SyncListener<T> listener) {
4335 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4340 public <T> void forPossibleRelatedValue(Resource subject,
4341 Resource relation, Listener<T> listener) {
4342 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4347 final public <T> void forPossibleRelatedValue(final Resource subject,
4348 final Resource relation, final AsyncProcedure<T> procedure) {
4350 assert (subject != null);
4351 assert (relation != null);
4352 assert (procedure != null);
4354 final DeepSingleOrNullProcedure<T> checkedProcedure = new DeepSingleOrNullProcedure<T>(procedure);
4356 processor.forEachObject(this, subject, relation,
4357 new AsyncMultiProcedureAdapter<Resource>() {
4360 public void execute(AsyncReadGraph graph,
4361 final Resource object) {
4363 checkedProcedure.inc();
4365 graph.forValue(object, new AsyncProcedure<Object>() {
4368 public void execute(AsyncReadGraph graph,
4370 checkedProcedure.offer(graph, (T) result);
4371 checkedProcedure.dec(graph);
4375 public void exception(AsyncReadGraph graph,
4377 checkedProcedure.exception(graph, t);
4378 checkedProcedure.dec(graph);
4386 public void finished(AsyncReadGraph graph) {
4388 checkedProcedure.dec(graph);
4392 public void exception(AsyncReadGraph graph, Throwable t) {
4393 checkedProcedure.exception(graph, t);
4394 checkedProcedure.dec(graph);
4398 public String toString() {
4399 return "forPossibleRelatedValue -> " + procedure;
4406 public <T> void forPossibleRelatedValue(Resource subject,
4407 Resource relation, SyncProcedure<T> procedure) {
4408 forPossibleRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
4413 public <T> void forPossibleRelatedValue(Resource subject,
4414 Resource relation, Procedure<T> procedure) {
4415 forPossibleRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
4420 public <T> void forPossibleRelatedValue(Resource subject,
4421 Resource relation, Binding binding, AsyncListener<T> listener) {
4422 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4427 public <T> void forPossibleRelatedValue(Resource subject,
4428 Resource relation, Binding binding, SyncListener<T> listener) {
4429 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4434 public <T> void forPossibleRelatedValue(Resource subject,
4435 Resource relation, Binding binding, Listener<T> listener) {
4436 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4440 final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4441 final AsyncProcedure<T> procedure) {
4443 assert (subject != null);
4444 assert (relation != null);
4445 assert (procedure != null);
4447 processor.forPossibleObject(this, subject, relation, new AsyncProcedure<Resource>() {
4450 public void execute(AsyncReadGraph graph, Resource object) {
4452 if(object == null) {
4453 procedure.execute(graph, null);
4457 processor.forPossibleValue((ReadGraphImpl)graph, object, new AsyncProcedure<byte[]>() {
4460 public void execute(AsyncReadGraph graph, byte[] bytes) {
4466 Serializer serializer = binding.serializer();
4467 Object obj = serializer.deserialize(bytes);
4468 if (!binding.isInstance(obj)) {
4469 procedure.exception(graph, new ClassCastException("Cannot get value " + obj + " with binding " + binding));
4471 procedure.execute(graph, (T) obj);
4474 } catch (Throwable t) {
4476 procedure.exception(graph, t);
4482 procedure.execute(graph, null);
4489 public void exception(AsyncReadGraph graph, Throwable t) {
4490 procedure.exception(graph, t);
4498 public void exception(AsyncReadGraph graph, Throwable throwable) {
4499 throwable.printStackTrace();
4500 procedure.exception(graph, throwable);
4508 public <T> void forPossibleRelatedValue(Resource subject,
4509 Resource relation, Binding binding, SyncProcedure<T> procedure) {
4510 forPossibleRelatedValue(subject, relation, binding,
4511 new SyncToAsyncProcedure<T>(procedure));
4515 public <T> void forPossibleRelatedValue(Resource subject,
4516 Resource relation, Binding binding, Procedure<T> procedure) {
4517 forPossibleRelatedValue(subject, relation, binding,
4518 new NoneToAsyncProcedure<T>(procedure));
4522 public void forIsInstanceOf(Resource subject, Resource relation,
4523 AsyncListener<Boolean> listener) {
4524 asyncRequest(new IsInstanceOf(subject, relation), listener);
4528 public void forIsInstanceOf(Resource subject, Resource relation,
4529 SyncListener<Boolean> listener) {
4530 asyncRequest(new IsInstanceOf(subject, relation), listener);
4534 public void forIsInstanceOf(Resource subject, Resource relation,
4535 Listener<Boolean> listener) {
4536 asyncRequest(new IsInstanceOf(subject, relation), listener);
4540 final public void forIsInstanceOf(final Resource resource,
4541 final Resource type, final AsyncProcedure<Boolean> procedure) {
4543 assert (resource != null);
4544 assert (type != null);
4545 assert (procedure != null);
4547 forTypes(resource, new AsyncProcedure<Set<Resource>>() {
4550 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4553 if (result.contains(type))
4554 procedure.execute(graph, true);
4556 procedure.execute(graph, false);
4557 } catch (Throwable t) {
4558 Logger.defaultLogError(t);
4563 public void exception(AsyncReadGraph graph, Throwable t) {
4565 procedure.exception(graph, t);
4566 } catch (Throwable t2) {
4567 Logger.defaultLogError(t2);
4572 public String toString() {
4573 return "forIsInstanceOf -> " + procedure;
4581 public void forIsInstanceOf(Resource subject, Resource relation,
4582 SyncProcedure<Boolean> procedure) {
4583 forIsInstanceOf(subject, relation, new SyncToAsyncProcedure<Boolean>(
4588 public void forIsInstanceOf(Resource subject, Resource relation,
4589 Procedure<Boolean> procedure) {
4590 forIsInstanceOf(subject, relation, new NoneToAsyncProcedure<Boolean>(
4595 public void forIsInheritedFrom(Resource subject, Resource relation,
4596 AsyncListener<Boolean> listener) {
4597 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4601 public void forIsInheritedFrom(Resource subject, Resource relation,
4602 SyncListener<Boolean> listener) {
4603 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4607 public void forIsInheritedFrom(Resource subject, Resource relation,
4608 Listener<Boolean> listener) {
4609 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4613 final public void forIsInheritedFrom(final Resource resource,
4614 final Resource type, final AsyncProcedure<Boolean> procedure) {
4616 assert (resource != null);
4617 assert (type != null);
4618 assert (procedure != null);
4620 if (resource.equals(type)) {
4622 procedure.execute(this, true);
4623 } catch (Throwable t) {
4624 Logger.defaultLogError(t);
4629 forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
4632 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4634 if (result.contains(type))
4635 procedure.execute(graph, true);
4637 procedure.execute(graph, false);
4638 } catch (Throwable t) {
4639 Logger.defaultLogError(t);
4644 public void exception(AsyncReadGraph graph, Throwable t) {
4646 procedure.exception(graph, t);
4647 } catch (Throwable t2) {
4648 Logger.defaultLogError(t2);
4653 public String toString() {
4654 return "forIsInheritedFrom -> " + procedure;
4662 public void forIsInheritedFrom(Resource subject, Resource relation,
4663 SyncProcedure<Boolean> procedure) {
4664 forIsInheritedFrom(subject, relation,
4665 new SyncToAsyncProcedure<Boolean>(procedure));
4669 public void forIsInheritedFrom(Resource subject, Resource relation,
4670 Procedure<Boolean> procedure) {
4671 forIsInheritedFrom(subject, relation,
4672 new NoneToAsyncProcedure<Boolean>(procedure));
4676 public void forIsSubrelationOf(Resource subject, Resource relation,
4677 AsyncListener<Boolean> listener) {
4678 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4682 public void forIsSubrelationOf(Resource subject, Resource relation,
4683 SyncListener<Boolean> listener) {
4684 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4688 public void forIsSubrelationOf(Resource subject, Resource relation,
4689 Listener<Boolean> listener) {
4690 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4694 final public void forIsSubrelationOf(final Resource resource,
4695 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4697 assert (resource != null);
4698 assert (relation != null);
4699 assert (procedure != null);
4701 if (resource.equals(relation)) {
4702 procedure.execute(this, true);
4706 forSuperrelations(resource, new AsyncProcedure<Set<Resource>>() {
4709 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4711 if (result.contains(relation))
4712 procedure.execute(graph, true);
4714 procedure.execute(graph, false);
4715 } catch (Throwable t) {
4716 Logger.defaultLogError(t);
4721 public void exception(AsyncReadGraph graph, Throwable t) {
4723 procedure.exception(graph, t);
4724 } catch (Throwable t2) {
4725 Logger.defaultLogError(t2);
4730 public String toString() {
4731 return "forIsSubrelationOf -> " + procedure;
4739 public void forIsSubrelationOf(Resource subject, Resource relation,
4740 SyncProcedure<Boolean> procedure) {
4741 forIsSubrelationOf(subject, relation,
4742 new SyncToAsyncProcedure<Boolean>(procedure));
4746 public void forIsSubrelationOf(Resource subject, Resource relation,
4747 Procedure<Boolean> procedure) {
4748 forIsSubrelationOf(subject, relation,
4749 new NoneToAsyncProcedure<Boolean>(procedure));
4753 public void forHasStatement(Resource subject,
4754 AsyncListener<Boolean> listener) {
4755 asyncRequest(new HasStatementSubject(subject), listener);
4759 public void forHasStatement(Resource subject, SyncListener<Boolean> listener) {
4760 asyncRequest(new HasStatementSubject(subject), listener);
4764 public void forHasStatement(Resource subject, Listener<Boolean> listener) {
4765 asyncRequest(new HasStatementSubject(subject), listener);
4769 final public void forHasStatement(final Resource subject,
4770 final AsyncProcedure<Boolean> procedure) {
4772 assert (subject != null);
4773 assert (procedure != null);
4775 processor.forHasStatement(this, subject, procedure);
4780 public void forHasStatement(Resource subject,
4781 SyncProcedure<Boolean> procedure) {
4782 forHasStatement(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4786 public void forHasStatement(Resource subject, Procedure<Boolean> procedure) {
4787 forHasStatement(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4791 public void forHasStatement(Resource subject, Resource relation,
4792 AsyncListener<Boolean> listener) {
4793 asyncRequest(new HasStatement(subject, relation), listener);
4797 public void forHasStatement(Resource subject, Resource relation,
4798 SyncListener<Boolean> listener) {
4799 asyncRequest(new HasStatement(subject, relation), listener);
4803 public void forHasStatement(Resource subject, Resource relation,
4804 Listener<Boolean> listener) {
4805 asyncRequest(new HasStatement(subject, relation), listener);
4809 final public void forHasStatement(final Resource subject,
4810 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4812 assert (subject != null);
4813 assert (relation != null);
4814 assert (procedure != null);
4816 processor.forHasStatement(this, subject, relation, procedure);
4821 public void forHasStatement(Resource subject, Resource relation,
4822 SyncProcedure<Boolean> procedure) {
4823 forHasStatement(subject, relation, new SyncToAsyncProcedure<Boolean>(
4828 public void forHasStatement(Resource subject, Resource relation,
4829 Procedure<Boolean> procedure) {
4830 forHasStatement(subject, relation, new NoneToAsyncProcedure<Boolean>(
4835 public void forHasStatement(Resource subject, Resource relation,
4836 Resource object, AsyncListener<Boolean> listener) {
4837 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4842 public void forHasStatement(Resource subject, Resource relation,
4843 Resource object, SyncListener<Boolean> listener) {
4844 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4849 public void forHasStatement(Resource subject, Resource relation,
4850 Resource object, Listener<Boolean> listener) {
4851 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4856 final public void forHasStatement(final Resource subject,
4857 final Resource relation, final Resource object,
4858 final AsyncProcedure<Boolean> procedure) {
4860 assert (subject != null);
4861 assert (relation != null);
4862 assert (object != null);
4863 assert (procedure != null);
4865 processor.forHasStatement(this, subject, relation, object, procedure);
4870 public void forHasStatement(Resource subject, Resource relation,
4871 Resource object, SyncProcedure<Boolean> procedure) {
4872 forHasStatement(subject, relation, object,
4873 new SyncToAsyncProcedure<Boolean>(procedure));
4877 public void forHasStatement(Resource subject, Resource relation,
4878 Resource object, Procedure<Boolean> procedure) {
4879 forHasStatement(subject, relation, object,
4880 new NoneToAsyncProcedure<Boolean>(procedure));
4884 public void forHasValue(Resource subject, AsyncListener<Boolean> listener) {
4885 asyncRequest(new HasValue(subject), listener);
4889 public void forHasValue(Resource subject, SyncListener<Boolean> listener) {
4890 asyncRequest(new HasValue(subject), listener);
4894 public void forHasValue(Resource subject, Listener<Boolean> listener) {
4895 asyncRequest(new HasValue(subject), listener);
4899 final public void forHasValue(final Resource subject,
4900 final AsyncProcedure<Boolean> procedure) {
4902 assert (subject != null);
4903 assert (procedure != null);
4905 processor.forValue(this, subject, new AsyncProcedure<byte[]>() {
4908 public void execute(AsyncReadGraph graph, byte[] result) {
4911 procedure.execute(graph, false);
4913 procedure.execute(graph, true);
4914 } catch (Throwable t) {
4915 Logger.defaultLogError(t);
4920 public void exception(AsyncReadGraph graph, Throwable t) {
4922 procedure.exception(graph, t);
4923 } catch (Throwable t2) {
4924 Logger.defaultLogError(t2);
4929 public String toString() {
4930 return "forHasValue -> " + procedure;
4938 public void forHasValue(Resource subject, SyncProcedure<Boolean> procedure) {
4939 forHasValue(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4943 public void forHasValue(Resource subject, Procedure<Boolean> procedure) {
4944 forHasValue(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4948 public void forOrderedSet(Resource subject,
4949 AsyncMultiListener<Resource> listener) {
4950 asyncRequest(new OrderedSet(subject), listener);
4954 public void forOrderedSet(Resource subject,
4955 SyncMultiListener<Resource> listener) {
4956 asyncRequest(new OrderedSet(subject), listener);
4960 public void forOrderedSet(Resource subject, MultiListener<Resource> listener) {
4961 asyncRequest(new OrderedSet(subject), listener);
4965 final public void forOrderedSet(final Resource subject,
4966 final AsyncMultiProcedure<Resource> procedure) {
4968 assert (subject != null);
4969 assert (procedure != null);
4971 processor.forOrderedSet(this, subject,
4972 new AsyncMultiProcedure<Resource>() {
4975 public void finished(AsyncReadGraph graph) {
4977 procedure.finished(graph);
4978 } catch (Throwable t) {
4979 Logger.defaultLogError(t);
4984 public void execute(AsyncReadGraph graph, Resource result) {
4986 procedure.execute(graph, result);
4987 } catch (Throwable t) {
4988 Logger.defaultLogError(t);
4993 public void exception(AsyncReadGraph graph, Throwable t) {
4995 procedure.exception(graph, t);
4996 } catch (Throwable t2) {
4997 Logger.defaultLogError(t2);
5002 public String toString() {
5003 return "forOrderedSet -> " + procedure;
5011 public void forOrderedSet(Resource subject,
5012 SyncMultiProcedure<Resource> procedure) {
5013 forOrderedSet(subject, new SyncToAsyncMultiProcedure<Resource>(
5018 public void forOrderedSet(Resource subject,
5019 MultiProcedure<Resource> procedure) {
5020 forOrderedSet(subject, new NoneToAsyncMultiProcedure<Resource>(
5025 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5026 AsyncListener<T> listener) {
5027 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5031 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5032 SyncListener<T> listener) {
5033 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5037 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5038 Listener<T> listener) {
5039 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5043 final public <T> void forPossibleAdapted(final Resource resource,
5044 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5046 assert (resource != null);
5047 assert (clazz != null);
5048 assert (procedure != null);
5050 final AdaptionService service = getSession().peekService(AdaptionService.class);
5051 if (service == null)
5052 procedure.exception(this, new ServiceException("No AdaptionService available"));
5054 service.adapt(this, resource, resource, Resource.class, clazz, true, procedure);
5058 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5059 SyncProcedure<T> procedure) {
5060 forPossibleAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5065 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5066 Procedure<T> procedure) {
5067 forPossibleAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5072 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5073 AsyncListener<T> listener) {
5074 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5078 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5079 SyncListener<T> listener) {
5080 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5084 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5085 Listener<T> listener) {
5086 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5090 final public <T> void forPossibleUniqueAdapted(final Resource resource,
5091 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5093 assert (resource != null);
5094 assert (clazz != null);
5095 assert (procedure != null);
5097 final AdaptionService service = getSession().peekService(AdaptionService.class);
5098 if (service == null)
5099 procedure.exception(this, new ServiceException("No AdaptionService available"));
5101 service.adaptNew(this, resource, clazz, true, procedure);
5106 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5107 SyncProcedure<T> procedure) {
5108 forPossibleUniqueAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5113 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5114 Procedure<T> procedure) {
5115 forPossibleUniqueAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5120 * Implementation of the interface AsyncRequestProcessor
5124 final public Session getSession() {
5125 return processor.getSession();
5129 public <T> void asyncRequest(final Read<T> request) {
5131 asyncRequest(request, new AsyncProcedure<T>() {
5134 public void execute(AsyncReadGraph graph, T result) {
5138 public void exception(AsyncReadGraph graph, Throwable t) {
5139 Logger.defaultLogError(t);
5143 public String toString() {
5144 return "asyncRequest(Read) -> " + request;
5152 public <T> void asyncRequest(Read<T> request, AsyncListener<T> procedure) {
5153 asyncRequest(request, (AsyncProcedure<T>) procedure);
5157 public <T> void asyncRequest(Read<T> request,
5158 final SyncListener<T> procedure) {
5159 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5163 public <T> void asyncRequest(Read<T> request, final Listener<T> procedure) {
5164 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5168 public <T> void asyncRequest(final Read<T> request, final AsyncProcedure<T> procedure) {
5170 assert (request != null);
5171 assert (procedure != null);
5173 processor.schedule(new SessionTask(this) {
5176 public void run(int thread) {
5178 final ListenerBase listener = getListenerBase(procedure);
5179 QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5180 } catch (DatabaseException e) {
5181 Logger.defaultLogError(e);
5189 public static ReadGraphImpl createAsync(QueryProcessor support) {
5190 return new ReadGraphImpl(null, null, support);
5194 public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
5195 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5199 public <T> void asyncRequest(Read<T> request, final Procedure<T> procedure) {
5200 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5204 final public <T> void asyncRequest(final AsyncRead<T> request) {
5206 assert (request != null);
5208 asyncRequest(request, new AsyncProcedure<T>() {
5211 public void execute(AsyncReadGraph graph, T result) {
5215 public void exception(AsyncReadGraph graph, Throwable t) {
5216 Logger.defaultLogError(t);
5220 public String toString() {
5221 return "asyncRequest(AsyncRead) -> " + request;
5229 public <T> void asyncRequest(AsyncRead<T> request,
5230 AsyncListener<T> procedure) {
5231 asyncRequest(request, (AsyncProcedure<T>) procedure);
5235 final public <T> void asyncRequest(AsyncRead<T> request,
5236 final SyncListener<T> procedure) {
5237 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5241 final public <T> void asyncRequest(AsyncRead<T> request,
5242 final Listener<T> procedure) {
5243 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5247 final public <T> void asyncRequest(final AsyncRead<T> request,
5248 final AsyncProcedure<T> procedure) {
5250 assert (request != null);
5251 assert (procedure != null);
5253 processor.schedule(new SessionTask(this) {
5256 public void run(int thread) {
5258 final ListenerBase listener = getListenerBase(procedure);
5259 QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5260 } catch (DatabaseException e) {
5261 Logger.defaultLogError(e);
5270 public <T> void asyncRequest(AsyncRead<T> request,
5271 SyncProcedure<T> procedure) {
5272 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5276 final public <T> void asyncRequest(final AsyncRead<T> request,
5277 final Procedure<T> procedure) {
5278 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5282 public <T> void asyncRequest(final MultiRead<T> request) {
5284 assert (request != null);
5286 asyncRequest(request, new SyncMultiProcedureAdapter<T>() {
5288 public void exception(ReadGraph graph, Throwable t) {
5289 Logger.defaultLogError(t);
5293 public String toString() {
5294 return "asyncRequest(MultiRead) -> " + request;
5301 public <T> void asyncRequest(MultiRead<T> request,
5302 SyncMultiListener<T> procedure) {
5303 asyncRequest(request, (SyncMultiProcedure<T>)procedure);
5307 public <T> void asyncRequest(MultiRead<T> request,
5308 MultiListener<T> procedure) {
5309 asyncRequest(request, new NoneToSyncMultiListener<T>(procedure));
5314 public <T> void asyncRequest(final MultiRead<T> request,
5315 final SyncMultiProcedure<T> procedure) {
5317 assert (request != null);
5318 assert (procedure != null);
5320 final ListenerBase listener = getListenerBase(procedure);
5322 if (parent != null || listener != null) {
5324 // final ReadGraphImpl newGraph = newSync();
5325 processor.query(this, request, parent, procedure,listener);
5329 // final ReadGraphImpl newGraph = newSync();
5333 request.perform(this, procedure);
5335 } catch (Throwable t) {
5338 procedure.exception(this, t);
5339 } catch (DatabaseException e) {
5340 LOGGER.error("Unexpected exception while handling exception", e);
5350 public <T> void asyncRequest(MultiRead<T> request,
5351 MultiProcedure<T> procedure) {
5352 asyncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
5356 final public <T> void asyncRequest(final AsyncMultiRead<T> request) {
5358 assert (request != null);
5360 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5362 public void exception(AsyncReadGraph graph, Throwable t) {
5363 Logger.defaultLogError(t);
5367 public String toString() {
5368 return "asyncRequest(AsyncMultiRead) -> " + request;
5375 public <T> void asyncRequest(AsyncMultiRead<T> request,
5376 AsyncMultiListener<T> procedure) {
5377 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5381 public <T> void asyncRequest(AsyncMultiRead<T> request,
5382 SyncMultiListener<T> procedure) {
5383 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5387 public <T> void asyncRequest(AsyncMultiRead<T> request,
5388 MultiListener<T> procedure) {
5389 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5393 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5394 final AsyncMultiProcedure<T> procedure) {
5396 assert (request != null);
5397 assert (procedure != null);
5399 ListenerBase listener = getListenerBase(procedure);
5401 if (parent != null || listener != null) {
5403 processor.query(this, request, parent, procedure, listener);
5409 request.perform(this, new AsyncMultiProcedure<T>() {
5412 public void execute(AsyncReadGraph graph, T result) {
5413 procedure.execute(graph, result);
5417 public void finished(AsyncReadGraph graph) {
5418 procedure.finished(graph);
5422 public void exception(AsyncReadGraph graph, Throwable t) {
5423 procedure.exception(graph, t);
5427 public String toString() {
5428 return "asyncRequest(AsyncMultiRead) -> " + procedure;
5433 } catch (Throwable t) {
5435 procedure.exception(this, new DatabaseException(t));
5443 public <T> void asyncRequest(AsyncMultiRead<T> request,
5444 SyncMultiProcedure<T> procedure) {
5445 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5449 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5450 final MultiProcedure<T> procedure) {
5451 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5455 final public <T> void asyncRequest(final ExternalRead<T> request) {
5457 assert (request != null);
5459 asyncRequest(request, new Procedure<T>() {
5462 public void execute(T result) {
5466 public void exception(Throwable t) {
5467 Logger.defaultLogError(t);
5471 public String toString() {
5472 return "asyncRequest(PrimitiveRead) -> " + request;
5480 public <T> void asyncRequest(ExternalRead<T> request,
5481 final Listener<T> procedure) {
5482 asyncRequest(request, (Procedure<T>) procedure);
5486 final public <T> void asyncRequest(final ExternalRead<T> request,
5487 final Procedure<T> procedure) {
5489 assert (request != null);
5490 assert (procedure != null);
5492 final ListenerBase listener = getListenerBase(procedure);
5494 if (parent != null || listener != null) {
5497 QueryCacheBase.resultExternalReadEntry(this, request, parent, listener, procedure);
5498 } catch (DatabaseException e) {
5499 Logger.defaultLogError(e);
5500 // This throwable has already been transferred to procedure at this point - do nothing about it
5505 request.register(this, new Listener<T>() {
5507 public void execute(T result) {
5508 procedure.execute(result);
5512 public void exception(Throwable t) {
5513 procedure.exception(t);
5517 public String toString() {
5518 return "asyncRequest(PrimitiveRead) -> " + request;
5522 public boolean isDisposed() {
5533 public void asyncRequest(final Write request) {
5535 assert (request != null);
5537 getSession().asyncRequest(request);
5539 // processor.asyncWrite(request);
5544 public <T> void asyncRequest(final WriteResult<T> request, Procedure<T> procedure) {
5545 throw new Error("Not implemented.");
5549 public void asyncRequest(Write request, Consumer<DatabaseException> callback) {
5551 assert (request != null);
5553 getSession().asyncRequest(request, callback);
5558 public void asyncRequest(final DelayedWrite request) {
5560 assert (request != null);
5562 getSession().asyncRequest(request);
5567 public <T> void asyncRequest(final DelayedWriteResult<T> request, Procedure<T> procedure) {
5568 throw new Error("Not implemented.");
5572 public void asyncRequest(DelayedWrite r,
5573 Consumer<DatabaseException> callback) {
5574 throw new Error("Not implemented.");
5578 public void asyncRequest(final WriteOnly request) {
5580 assert (request != null);
5582 getSession().asyncRequest(request);
5587 public <T> void asyncRequest(final WriteOnlyResult<T> request, Procedure<T> procedure) {
5588 throw new Error("Not implemented.");
5592 public void asyncRequest(WriteOnly r, Consumer<DatabaseException> callback) {
5593 throw new Error("Not implemented.");
5597 * Implementation of the interface ServiceLocator
5601 public <T> T getService(Class<T> api) {
5602 if(WriteSupport.class == api) {
5603 if(this instanceof WriteGraphImpl) {
5604 WriteGraphImpl impl = (WriteGraphImpl)this;
5605 return (T)impl.writeSupport;
5608 return getSession().getService(api);
5612 public <T> T peekService(Class<T> api) {
5613 return getSession().peekService(api);
5617 public boolean hasService(Class<?> api) {
5618 return getSession().hasService(api);
5622 public <T> void registerService(Class<T> api, T service) {
5623 getSession().registerService(api, service);
5627 public boolean isImmutable(Resource resource) throws DatabaseException {
5628 ResourceImpl impl = (ResourceImpl)resource;
5629 return processor.isImmutable(impl.id);
5636 protected static String INTERNAL_ERROR_STRING = "Transaction aborted due to internal client error.";
5639 * callerThread is the currently running thread state.syncThread is blocking for
5640 * this execution state.syncParent is the blocking request
5643 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support) {
5644 this.parentGraph = parentGraph;
5645 this.parent = parent;
5646 this.processor = support;
5647 this.asyncBarrier = new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null);
5650 ReadGraphImpl(ReadGraphImpl graph, CacheEntry parent) {
5651 this(graph, parent, graph.processor);
5654 ReadGraphImpl(ReadGraphImpl graph) {
5655 this(graph, graph.parent);
5658 public ReadGraphImpl withParent(CacheEntry parent) {
5659 return new ReadGraphImpl(this, parent);
5662 public ReadGraphImpl forRecompute(CacheEntry parent) {
5663 return new ReadGraphImpl(null, parent, processor);
5666 public static ReadGraphImpl create(QueryProcessor support) {
5667 return new ReadGraphImpl(null, null, support);
5670 public ReadGraphImpl newRestart(ReadGraphImpl impl) {
5672 WriteGraphImpl write = processor.getSession().getService(
5673 WriteGraphImpl.class);
5679 final private ListenerBase getListenerBase(final Object procedure) {
5680 if (procedure instanceof ListenerBase)
5681 return (ListenerBase) procedure;
5686 public <T> void waitAsyncProcedure(AsyncMultiReadProcedure<T> procedure) {
5688 assert(procedure.done());
5692 public <T> void waitAsyncProcedure(AsyncReadProcedure<T> procedure) {
5694 assert(procedure.done());
5698 public boolean resumeTasks() {
5699 return processor.resumeTasks(this);
5702 Class<?> singleClass(Set<Resource> types) {
5703 Class<?> result = null;
5704 for (Resource type : types) {
5705 Class<?> clazz = processor.getBuiltinValue(type);
5706 if (clazz != null) {
5716 private String debugString(Resource r) {
5719 name = getPossibleRelatedValue(r, processor.getL0(this).HasName);
5720 } catch (ManyObjectsForFunctionalRelationException e) {
5721 Logger.defaultLogError(e);
5722 } catch (ServiceException e) {
5723 Logger.defaultLogError(e);
5725 return "[" + name + " - " + r + "]";
5729 public String toString() {
5730 return "ReadGraphImpl[thread=" + Thread.currentThread() + "]";
5734 final public int thread() {
5738 static class MultiTripleIntProcedure implements TripleIntProcedure {
5740 final private AsyncMultiProcedure<Statement> procedure;
5741 final private ReadGraphImpl impl;
5742 final private QuerySupport support;
5744 public MultiTripleIntProcedure(AsyncMultiProcedure<Statement> procedure, ReadGraphImpl impl, QuerySupport support) {
5745 this.procedure = procedure;
5747 this.support = support;
5751 public void execute(ReadGraphImpl graph, int s, int p, int o) {
5753 procedure.execute(graph, support.getStatement(s, p, o));
5754 } catch (Throwable t2) {
5755 Logger.defaultLogError(t2);
5760 public void finished(ReadGraphImpl graph) {
5762 procedure.finished(graph);
5763 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5764 } catch (Throwable t2) {
5765 Logger.defaultLogError(t2);
5770 public void exception(ReadGraphImpl graph, Throwable t) {
5772 procedure.exception(graph, t);
5773 } catch (Throwable t2) {
5774 Logger.defaultLogError(t2);
5776 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5780 public String toString() {
5781 return "forEachObject with " + procedure;
5786 // private AsyncMultiProcedure<Resource> cacheKey = null;
5787 // private MultiIntProcedure cacheResult = null;
5789 // final IntProcedure forMultiProcedure(final AsyncMultiProcedure<Resource> procedure) {
5791 // if(procedure == cacheKey) return cacheResult;
5793 // cacheResult = new MultiIntProcedure(procedure, this, processor.support);
5794 // cacheKey = procedure;
5796 // return cacheResult;
5800 // private AsyncMultiProcedure<Statement> cacheKey2 = null;
5801 // private MultiTripleIntProcedure cacheResult2 = null;
5803 // final synchronized TripleIntProcedure forMultiProcedure(final AsyncMultiProcedure<Statement> procedure) {
5805 // if(procedure == cacheKey2) return cacheResult2;
5807 // cacheResult2 = new MultiTripleIntProcedure(procedure, this, processor.support);
5808 // cacheKey2 = procedure;
5810 // return cacheResult2;
5815 public Datatype getDataType(Resource subject) throws DatabaseException {
5816 for(Resource dataTypeResource : getObjects(subject, processor.getL0(this).HasDataType))
5817 return getValue(dataTypeResource, Bindings.getBindingUnchecked(Datatype.class));
5818 throw new DoesNotContainValueException("The literal has no data type.");
5821 protected <T extends Accessor> T getAccessor4File(Resource subject)
5822 throws DatabaseException {
5825 byte[] bytes = processor.support.getValue(g, subject);
5829 BinaryVariant va = (BinaryVariant) Accessors.getAccessor(bytes, Datatypes.VARIANT);
5830 Accessor ca = va.getContentAccessor();
5832 } catch (AccessorConstructionException e) {
5833 throw new DatabaseException(e);
5839 Binding datatype_binding = Bindings.getBindingUnchecked(DataType.class);
5840 Serializer datatype_serializer = datatype_binding.serializer();
5843 BinaryMemory in = new BinaryMemory(ByteBuffer.wrap(bytes));
5844 datatype = (DataType)datatype_serializer.deserialize(in);
5845 Binding data_binding = Bindings.getBinding(datatype);
5846 Serializer data_serializer = data_binding.serializer(BinarySerializationFormat.INSTANCE);
5847 Object o = data_serializer.deserialize(in);
5849 return (T)Accessors.getAccessor(data_binding, o);
5850 } catch(AccessorConstructionException e) {
5853 } catch (Exception e) {
5854 throw new DatabaseException(e);
5857 @SuppressWarnings("unchecked")
5859 public <T extends Accessor> T getAccessor(Resource subject) throws DatabaseException {
5860 RandomAccessBinary rab = getRandomAccessBinary(subject);
5862 return (T)Accessors.getAccessor(rab, getDataType(subject));
5863 } catch(AccessorConstructionException e) {
5864 throw new DatabaseException(e);
5867 @SuppressWarnings("unchecked")
5868 protected <T extends Accessor> T createAccessor(Resource resource, Datatype datatype, Object intialValue)
5869 throws DatabaseException {
5870 RandomAccessBinary rab = createRandomAccessBinary(resource, datatype, intialValue);
5872 return (T)Accessors.getAccessor(rab, datatype);
5873 } catch(AccessorConstructionException e) {
5874 throw new DatabaseException(e);
5878 public RandomAccessBinary getRandomAccessBinary(Resource subject) throws DatabaseException {
5879 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5880 ResourceData rd = ravs.get(subject);
5884 ExternalValueSupport evs = getService(ExternalValueSupport.class);
5885 long size = evs.getValueSize(this, subject); // Throws DatabaseException if no old external value.
5887 File platform = Platform.getLocation().toFile();
5888 File tempFiles = new File(platform, "tempFiles");
5889 File dbDir = new File(tempFiles, "db");
5891 File file = new File(dbDir, "ResourceFile" + subject.getResourceId());
5892 rd = new ResourceData(new BinaryFile(file), true); // Old external value.
5893 final int N = 1<<20;
5897 int length = N < left ? N : (int)left;
5898 byte[] bytes = evs.readValue(this, subject, offset, length);
5899 offset += bytes.length;
5900 left -= bytes.length;
5901 rd.binaryFile.write(bytes);
5903 ravs.put(subject, rd);
5905 } catch (Exception e) {
5906 throw new DatabaseException("Resource " + subject + " have value but there was problem with accessing value data.", e);
5908 } catch (Exception e) {
5909 if(Development.DEVELOPMENT) {
5910 if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
5911 e.printStackTrace();
5915 Datatype datatype = getDataType(subject);
5916 Object value = getPossibleValue(subject, Bindings.getBinding(datatype));
5917 return createRandomAccessBinary(subject, datatype, value);
5919 public RandomAccessBinary createRandomAccessBinary(Resource resource, Datatype datatype, Object initialValue)
5920 throws DatabaseException {
5921 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5923 File platform = Platform.getLocation().toFile();
5924 File tempFiles = new File(platform, "tempFiles");
5925 File dbDir = new File(tempFiles, "db");
5927 File file = new File(dbDir, "ResourceFile" + resource.getResourceId());
5928 ResourceData rd = new ResourceData(new BinaryFile(file), false);
5929 Binding binding = Bindings.getBinding(datatype);
5930 if (null == initialValue) {
5931 initialValue = binding.createDefault();
5933 Serializer serializer = binding.serializer();
5934 byte[] bytes = serializer.serialize(initialValue);
5935 rd.binaryFile.write(bytes);
5936 ravs.put(resource, rd);
5938 } catch (Exception e) {
5939 if (e instanceof DatabaseException)
5940 throw (DatabaseException)e;
5942 throw new DatabaseException(e);
5946 // static class ExternalValueRequest<T> extends ResourceRead<T> {
5948 // public ExternalValueRequest(Resource resource) {
5952 // @SuppressWarnings("unchecked")
5954 // public T perform(ReadGraph graph) throws DatabaseException {
5957 // String uri = graph.getURI(resource);
5958 // if(Layer0.URIs.Functions_functionApplication.equals(uri)) return (T)functionApplication;
5960 // return (T)ReflectionUtils.getValue(uri).getValue();
5962 // } catch(ValueNotFoundException e) {
5963 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
5964 // } catch(ClassCastException e) {
5965 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
5971 @SuppressWarnings("unchecked")
5973 public <T> T getValue2(Resource r, Object context) throws DatabaseException {
5974 Layer0 L0 = processor.getL0(this);
5975 Set<Resource> types = getTypes(r);
5977 if(types.contains(L0.Literal)) {
5978 if(isImmutable(r)) {
5979 return syncRequest(new ValueImplied<T>(r));
5984 else if(types.contains(L0.ExternalValue)) {
5985 return (T)syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
5989 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
5990 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
5992 return function.apply(this, r, context);
5993 } catch(RuntimeException e) {
5994 DatabaseException dte = findPossibleRootException(e);
5995 if(dte != null) throw dte;
5996 else throw new DatabaseException(e);
6003 public Variant getVariantValue2(Resource r, Object context) throws DatabaseException {
6004 Layer0 L0 = processor.getL0(this);
6005 Set<Resource> types = getTypes(r);
6007 if(types.contains(L0.Literal)) {
6008 if(isImmutable(r)) {
6009 return syncRequest(new VariantValueImplied(r));
6011 return getVariantValue(r);
6014 else if(types.contains(L0.ExternalValue)) {
6015 Object value = syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6017 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6018 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6019 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6024 Function3<ReadGraph,Resource,Object,Object> function = requestValueFunction(r);
6025 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6027 Object value = function.apply(this, r, context);
6029 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6030 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6031 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6033 } catch(RuntimeException e) {
6034 DatabaseException dte = findPossibleRootException(e);
6035 if(dte != null) throw dte;
6036 else throw new DatabaseException(e);
6042 public <T> T getPossibleValue2(Resource subject, Object context) throws DatabaseException {
6044 return getValue2(subject, context);
6045 } catch (DatabaseException e) {
6050 static class PossibleConverterFunction<T> extends ResourceRead<Function3<ReadGraph,Resource,Object,T>> {
6052 public PossibleConverterFunction(Resource resource) {
6057 public Function3<ReadGraph,Resource,Object,T> perform(ReadGraph graph) throws DatabaseException {
6058 return compute(graph, resource);
6061 @SuppressWarnings("unchecked")
6062 public static <T> Function3<ReadGraph,Resource,Object,T> compute(ReadGraph graph, Resource resource) throws DatabaseException {
6063 Layer0 L0 = Layer0.getInstance(graph);
6064 for(Resource converter : graph.getObjects(resource, L0.ConvertsToValueWith)) {
6066 if(L0.Functions_functionApplication.equals(converter)) {
6067 return (Function3<ReadGraph,Resource,Object,T>)graph.syncRequest(new AdaptValue(resource));
6069 return graph.getValue2(converter, resource);
6071 } catch(RuntimeException e) {
6072 DatabaseException dte = findPossibleRootException(e);
6073 if(dte != null) throw dte;
6074 else throw new DatabaseException(e);
6082 <T> Function3<ReadGraph,Resource,Object,T> requestValueFunction(Resource r) throws DatabaseException {
6084 return syncRequest(new PossibleConverterFunction<T>(r), TransientCacheAsyncListener.<Function3<ReadGraph,Resource,Object,T>>instance());
6086 return syncRequest(new PossibleConverterFunction<T>(r));
6090 * Get a value associated with a graph {@link Resource}, using a possible context object and
6091 * a desired value binding. The following methods are tried in order to retreive the value:
6093 * <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>
6094 * <li>If the resource is a {@code L0.ExternalValue}, the value is acquired using
6095 * {@link ReflectionUtils#getValue(String)}.</li>
6096 * <li>If the resource is associated with a suitable value converter with relation {@code L0.ConvertsToValueWith}
6097 * (see {@link #requestValueFunction(Resource)}), the value function is called with the graph, the resource
6098 * and the context object.</li>
6101 * @param r A graph resource with which the value is associated
6102 * @param context A context object that is used for acquiring the value (only applied in case 3)
6103 * @param binding A binding for the value type (only applied in case 1)
6104 * @return The value of the graph node.
6105 * @throws DoesNotContainValueException No value is associated with the graph node.
6106 * @throws DatabaseException Other errors, such as an error in casting the value to the return type or
6107 * a runtime error in the value function.
6109 @SuppressWarnings("unchecked")
6111 public <T> T getValue2(Resource r, Object context, Binding binding) throws DatabaseException {
6112 if (binding instanceof ObjectVariantBinding)
6113 return getValue2(r, context);
6115 Layer0 L0 = processor.getL0(this);
6116 Set<Resource> types = getTypes(r);
6117 if(types.contains(L0.Literal)) {
6118 if(isImmutable(r)) {
6119 return syncRequest(new Value<T>(r, binding));
6121 return getValue(r, binding);
6123 } else if(types.contains(L0.ExternalValue)) {
6125 return (T)ReflectionUtils.getValue(getURI(r)).getValue();
6126 } catch(ValueNotFoundException e) {
6127 throw new DatabaseException(e);
6128 } catch(ClassCastException e) {
6129 throw new DatabaseException(e);
6133 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6134 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function.");
6136 Object value = function.apply(this, r, context);
6137 if(binding.isInstance(value)) return (T)value;
6138 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
6139 return (T)Bindings.adapt(value, srcBinding, binding);
6140 } catch(RuntimeException e) {
6141 DatabaseException dte = findPossibleRootException(e);
6142 if(dte != null) throw dte;
6143 else throw new DatabaseException(e);
6144 } catch (AdaptException e) {
6145 throw new DatabaseException(e);
6146 } catch (org.simantics.databoard.binding.error.BindingException e) {
6147 throw new DatabaseException(e);
6153 public <T> T getPossibleValue2(Resource subject, Object context, Binding binding) throws DatabaseException {
6155 return getValue2(subject, context, binding);
6156 } catch (DatabaseException e) {
6161 private static DatabaseException findPossibleRootException(Throwable t) {
6162 if(t == null) return null;
6163 if(t instanceof DatabaseException) return (DatabaseException)t;
6164 if(t instanceof RuntimeException || t instanceof InvocationTargetException) {
6165 return findPossibleRootException(t.getCause());
6171 public <T> T getRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6172 return getRelatedValue2(subject, relation, subject);
6176 public Variant getRelatedVariantValue2(Resource subject, Resource relation) throws DatabaseException {
6177 return getRelatedVariantValue2(subject, relation, subject);
6181 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6183 Resource object = getPossibleObject(subject, relation);
6184 if(object == null) return null;
6185 else return getValue2(object, subject);
6186 } catch (DatabaseException e) {
6192 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6193 if(Development.DEVELOPMENT) {
6194 String error = L0Validations.checkValueType(this, subject, relation);
6196 Logger.defaultLogError(new ValidationException(error));
6197 //throw new ValidationException(error);
6198 new ValidationException(error).printStackTrace();
6201 return getValue2(getSingleObject(subject, relation), context);
6205 public Variant getRelatedVariantValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6206 if(Development.DEVELOPMENT) {
6207 String error = L0Validations.checkValueType(this, subject, relation);
6209 Logger.defaultLogError(new ValidationException(error));
6210 //throw new ValidationException(error);
6211 new ValidationException(error).printStackTrace();
6214 return getVariantValue2(getSingleObject(subject, relation), context);
6218 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6220 Resource object = getPossibleObject(subject, relation);
6221 if(object == null) return null;
6222 else return getValue2(object, context);
6223 } catch (DatabaseException e) {
6229 public <T> T getRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6230 return getRelatedValue2(subject, relation, subject, binding);
6234 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6236 Resource object = getPossibleObject(subject, relation);
6237 if(object == null) return null;
6238 return getValue2(object, subject, binding);
6239 } catch (DatabaseException e) {
6245 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6246 return getValue2(getSingleObject(subject, relation), context, binding);
6250 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6252 Resource object = getPossibleObject(subject, relation);
6253 if(object == null) return null;
6254 else return getValue2(object, context, binding);
6255 } catch (DatabaseException e) {
6261 public Type getRelatedValueType(Resource subject, Resource relation) throws DatabaseException {
6262 Layer0 L0 = processor.getL0(this);
6263 Resource property = getSingleObject(subject, relation);
6264 String typeText = (String)getRelatedValue(property, L0.HasValueType, Bindings.STRING);
6266 return org.simantics.scl.compiler.types.Types.parseType(typeText);
6267 } catch (SCLTypeParseException e) {
6268 throw new DatabaseException(e);
6272 private static ThreadLocal<Boolean> syncGraph = new ThreadLocal<Boolean>() {
6273 protected Boolean initialValue() {
6279 public boolean setSynchronous(boolean value) {
6280 boolean old = getSynchronous();
6281 syncGraph.set(value);
6286 public boolean getSynchronous() {
6287 return syncGraph.get();
6290 public void ensureLoaded(int resource) {
6291 processor.querySupport.ensureLoaded(this, resource);
6294 public void ensureLoaded(int resource, int predicate) {
6295 processor.querySupport.ensureLoaded(this, resource, predicate);
6298 public byte[] getValue(int resource) {
6299 return processor.querySupport.getValue(this, resource);
6302 public int thread(int resource) {
6303 return (resource >>> 16) & processor.THREAD_MASK;
6306 public int thread(Resource resource) {
6307 return (((ResourceImpl)resource).id >>> 16) & processor.THREAD_MASK;
6310 public ResourceSupport getResourceSupport() {
6311 return processor.getResourceSupport();
6315 public Object getModificationCounter() {
6316 return processor.getSession().getModificationCounter();
6320 public boolean performPending() {
6321 return processor.performPending(this);
6324 public Set<ReadGraphImpl> ancestorSet() {
6325 HashSet<ReadGraphImpl> result = new HashSet<>();
6326 ReadGraphImpl g = this;