1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl.graph;
14 import java.io.BufferedOutputStream;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.io.PrintStream;
19 import java.lang.reflect.Array;
20 import java.lang.reflect.InvocationTargetException;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.HashSet;
25 import java.util.IdentityHashMap;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.ListIterator;
31 import java.util.function.Consumer;
33 import org.eclipse.core.runtime.Platform;
34 import org.simantics.databoard.Accessors;
35 import org.simantics.databoard.Bindings;
36 import org.simantics.databoard.accessor.Accessor;
37 import org.simantics.databoard.accessor.error.AccessorConstructionException;
38 import org.simantics.databoard.adapter.AdaptException;
39 import org.simantics.databoard.binding.Binding;
40 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;
41 import org.simantics.databoard.binding.impl.ObjectVariantBinding;
42 import org.simantics.databoard.binding.mutable.Variant;
43 import org.simantics.databoard.serialization.Serializer;
44 import org.simantics.databoard.type.Datatype;
45 import org.simantics.databoard.util.binary.BinaryFile;
46 import org.simantics.databoard.util.binary.RandomAccessBinary;
47 import org.simantics.db.AsyncReadGraph;
48 import org.simantics.db.ComputationalValue;
49 import org.simantics.db.DevelopmentKeys;
50 import org.simantics.db.ExternalValueSupport;
51 import org.simantics.db.ReadGraph;
52 import org.simantics.db.RelationContext;
53 import org.simantics.db.RelationInfo;
54 import org.simantics.db.Resource;
55 import org.simantics.db.Session;
56 import org.simantics.db.Statement;
57 import org.simantics.db.adaption.AdaptionService;
58 import org.simantics.db.common.primitiverequest.Adapter;
59 import org.simantics.db.common.primitiverequest.Builtin;
60 import org.simantics.db.common.primitiverequest.DatatypeBinding;
61 import org.simantics.db.common.primitiverequest.ForEachAssertedObject;
62 import org.simantics.db.common.primitiverequest.ForEachAssertedStatement;
63 import org.simantics.db.common.primitiverequest.HasStatement;
64 import org.simantics.db.common.primitiverequest.HasStatementSubject;
65 import org.simantics.db.common.primitiverequest.HasStatementSubjectObject;
66 import org.simantics.db.common.primitiverequest.HasValue;
67 import org.simantics.db.common.primitiverequest.Inverse;
68 import org.simantics.db.common.primitiverequest.IsInheritedFrom;
69 import org.simantics.db.common.primitiverequest.IsInstanceOf;
70 import org.simantics.db.common.primitiverequest.IsSubrelationOf;
71 import org.simantics.db.common.primitiverequest.OrderedSet;
72 import org.simantics.db.common.primitiverequest.PossibleAdapter;
73 import org.simantics.db.common.primitiverequest.PossibleInverse;
74 import org.simantics.db.common.primitiverequest.PossibleObject;
75 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;
76 import org.simantics.db.common.primitiverequest.PossibleRelatedValueImplied;
77 import org.simantics.db.common.primitiverequest.PossibleStatement;
78 import org.simantics.db.common.primitiverequest.PossibleType;
79 import org.simantics.db.common.primitiverequest.PossibleUniqueAdapter;
80 import org.simantics.db.common.primitiverequest.PossibleValue;
81 import org.simantics.db.common.primitiverequest.PossibleValueImplied;
82 import org.simantics.db.common.primitiverequest.RelatedValue;
83 import org.simantics.db.common.primitiverequest.RelatedValueImplied;
84 import org.simantics.db.common.primitiverequest.SingleObject;
85 import org.simantics.db.common.primitiverequest.SingleStatement;
86 import org.simantics.db.common.primitiverequest.SingleType;
87 import org.simantics.db.common.primitiverequest.SingleTypeAny;
88 import org.simantics.db.common.primitiverequest.Types;
89 import org.simantics.db.common.primitiverequest.UniqueAdapter;
90 import org.simantics.db.common.primitiverequest.Value;
91 import org.simantics.db.common.primitiverequest.ValueImplied;
92 import org.simantics.db.common.primitiverequest.VariantValueImplied;
93 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
94 import org.simantics.db.common.procedure.adapter.AsyncProcedureAdapter;
95 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
96 import org.simantics.db.common.procedure.adapter.SyncMultiProcedureAdapter;
97 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
98 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
99 import org.simantics.db.common.procedure.single.SyncReadProcedure;
100 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrErrorProcedure;
101 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrNullProcedure;
102 import org.simantics.db.common.procedure.single.wrapper.ExceptionToNullProcedure;
103 import org.simantics.db.common.procedure.single.wrapper.NullSingleOrNullProcedure;
104 import org.simantics.db.common.procedure.single.wrapper.SingleFunctionalOrNullProcedure;
105 import org.simantics.db.common.procedure.single.wrapper.SingleOrErrorProcedure;
106 import org.simantics.db.common.procedure.single.wrapper.SingleOrNullProcedure;
107 import org.simantics.db.common.procedure.wrapper.NoneToAsyncListener;
108 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiListener;
109 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiProcedure;
110 import org.simantics.db.common.procedure.wrapper.NoneToAsyncProcedure;
111 import org.simantics.db.common.procedure.wrapper.NoneToAsyncSetProcedure;
112 import org.simantics.db.common.procedure.wrapper.NoneToSyncMultiListener;
113 import org.simantics.db.common.procedure.wrapper.NoneToSyncMultiProcedure;
114 import org.simantics.db.common.procedure.wrapper.SyncToAsyncListener;
115 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiListener;
116 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiProcedure;
117 import org.simantics.db.common.procedure.wrapper.SyncToAsyncProcedure;
118 import org.simantics.db.common.procedure.wrapper.SyncToAsyncSetProcedure;
119 import org.simantics.db.common.request.AdaptValue;
120 import org.simantics.db.common.request.ResourceRead;
121 import org.simantics.db.common.utils.Logger;
122 import org.simantics.db.common.utils.NameUtils;
123 import org.simantics.db.common.validation.L0Validations;
124 import org.simantics.db.exception.AdaptionException;
125 import org.simantics.db.exception.ArgumentException;
126 import org.simantics.db.exception.AssumptionException;
127 import org.simantics.db.exception.BindingException;
128 import org.simantics.db.exception.DatabaseException;
129 import org.simantics.db.exception.DoesNotContainValueException;
130 import org.simantics.db.exception.EmptyResourceException;
131 import org.simantics.db.exception.InternalException;
132 import org.simantics.db.exception.InvalidLiteralException;
133 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
134 import org.simantics.db.exception.NoInverseException;
135 import org.simantics.db.exception.NoSingleResultException;
136 import org.simantics.db.exception.ResourceNotFoundException;
137 import org.simantics.db.exception.ServiceException;
138 import org.simantics.db.exception.ValidationException;
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);
1110 byte[] bytes = null;
1113 bytes = processor.getValue(this, subject);
1114 if (bytes == null) throw new DoesNotContainValueException("No value for resource " + subject);
1116 Serializer serializer = getSerializer(binding);
1117 return (T)serializer.deserialize(bytes);
1119 } catch (DoesNotContainValueException e) {
1121 throw new DoesNotContainValueException(e);
1123 } catch (Throwable t) {
1124 throw new ServiceException("Could not getValue for subject " + debugString(subject) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(bytes), t);
1129 final public <T> T getRelatedValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1130 DoesNotContainValueException, ServiceException {
1132 assert (subject != null);
1133 assert (relation != null);
1136 Resource object = getSingleObject(subject, relation);
1137 return getValue(object);
1138 } catch (NoSingleResultException e) {
1139 throw new NoSingleResultException("No single value found for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1140 } catch (DoesNotContainValueException e) {
1142 Layer0 L0 = processor.getL0(this);
1143 Resource object = getPossibleObject(subject, relation);
1144 if(isInstanceOf(object, L0.Value)) {
1145 if(isInstanceOf(object, L0.Literal)) {
1146 throw new DoesNotContainValueException(e);
1148 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1151 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1153 } catch (DoesNotContainValueException e2) {
1155 } catch (DatabaseException e2) {
1156 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1158 } catch (ServiceException e) {
1159 throw new ServiceException(e);
1164 final public Variant getRelatedVariantValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1165 DoesNotContainValueException, ServiceException {
1167 assert (subject != null);
1168 assert (relation != null);
1171 Resource object = getSingleObject(subject, relation);
1172 return getVariantValue(object);
1173 } catch (NoSingleResultException e) {
1174 throw new NoSingleResultException("No single object for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1175 } catch (DoesNotContainValueException e) {
1177 Layer0 L0 = processor.getL0(this);
1178 Resource object = getPossibleObject(subject, relation);
1179 if(isInstanceOf(object, L0.Value)) {
1180 if(isInstanceOf(object, L0.Literal)) {
1181 throw new DoesNotContainValueException(e);
1183 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1186 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1188 } catch (DoesNotContainValueException e2) {
1190 } catch (DatabaseException e2) {
1191 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1193 } catch (ServiceException e) {
1194 throw new ServiceException(e);
1199 final public <T> T getRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1200 throws NoSingleResultException, DoesNotContainValueException, BindingException, ServiceException {
1202 assert (subject != null);
1203 assert (relation != null);
1206 Resource object = getSingleObject(subject, relation);
1207 return getValue(object, binding);
1208 } catch (NoSingleResultException e) {
1209 String message = "";
1211 String subjectName = NameUtils.getSafeName(this, subject, true);
1212 String relationName = NameUtils.getSafeName(this, relation, true);
1213 message = "Subject: " + subjectName + ", Relation: " + relationName;
1214 } catch (DatabaseException e2) {
1217 throw new NoSingleResultException(message, e.getResultCount(), e);
1218 } catch (DoesNotContainValueException e) {
1219 throw new DoesNotContainValueException(e);
1220 } catch (ServiceException e) {
1221 throw new ServiceException(e);
1226 final public <T> T adapt(final Resource resource, final Class<T> clazz)
1227 throws AdaptionException, ValidationException, ServiceException {
1229 assert (resource != null);
1230 assert (clazz != null);
1234 return syncRequest(new Adapter<T>(resource, clazz));
1236 } catch (AdaptionException e) {
1238 throw new AdaptionException(e);
1240 } catch (ValidationException e) {
1242 throw new ValidationException(e);
1244 } catch (ServiceException e) {
1246 throw new ServiceException(e);
1248 } catch (DatabaseException e) {
1250 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1257 final public <T,C> T adaptContextual(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1258 throws AdaptionException, ValidationException, ServiceException {
1260 assert (resource != null);
1261 assert (context != null);
1263 class ContextualAdapter implements AsyncRead<T> {
1265 final private Resource resource;
1266 final private C context;
1267 final private Class<T> clazz;
1270 public int hashCode() {
1271 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1275 final public int threadHash() {
1276 return resource.getThreadHash();
1280 public boolean equals(Object object) {
1283 else if (object == null)
1285 else if (getClass() != object.getClass())
1287 ContextualAdapter r = (ContextualAdapter)object;
1288 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1292 public int getFlags() {
1296 public ContextualAdapter(Resource resource, C context, Class<T> clazz) {
1297 this.resource = resource;
1298 this.context = context;
1303 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1305 final AdaptionService service = getSession().peekService(AdaptionService.class);
1306 if (service == null)
1307 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1309 service.adapt(graph, resource, context, contextClass, clazz, false, procedure);
1314 public String toString() {
1315 return "Adapter for (" + resource + "," + context + ") as " + clazz.getName();
1322 return syncRequest(new ContextualAdapter(resource, context, clazz));
1324 } catch (AdaptionException e) {
1326 throw new AdaptionException(e);
1328 } catch (ValidationException e) {
1330 throw new ValidationException(e);
1332 } catch (ServiceException e) {
1334 throw new ServiceException(e);
1336 } catch (DatabaseException e) {
1338 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1345 final public <T> T adaptRelated(final Resource resource, final Resource relation, final Class<T> clazz)
1346 throws AdaptionException, NoSingleResultException, ValidationException, ServiceException {
1348 assert (resource != null);
1349 assert (clazz != null);
1351 Statement stm = getSingleStatement(resource, relation);
1353 Object o = adaptContextual(stm.getObject(), new RelationContextImpl(resource, stm), RelationContext.class, clazz);
1354 if (clazz.isInstance(o))
1356 throw new AdaptionException("Returned value is not expected class , got " + o.getClass().getName()+ " , expected " + clazz.getName());
1361 final public <T> T getPossibleRelatedAdapter(final Resource resource, final Resource relation, final Class<T> clazz)
1362 throws ValidationException, ServiceException {
1365 return adaptRelated(resource, relation, clazz);
1366 } catch (DatabaseException e) {
1373 final public <T,C> T getPossibleContextualAdapter(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1374 throws ValidationException, ServiceException {
1376 assert (resource != null);
1377 assert (context != null);
1379 class PossibleContextualAdapter implements AsyncRead<T> {
1381 final private Resource resource;
1382 final private C context;
1383 final private Class<T> clazz;
1386 public int hashCode() {
1387 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1391 final public int threadHash() {
1392 return resource.getThreadHash();
1396 public boolean equals(Object object) {
1399 else if (object == null)
1401 else if (getClass() != object.getClass())
1403 PossibleContextualAdapter r = (PossibleContextualAdapter)object;
1404 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1408 public int getFlags() {
1412 public PossibleContextualAdapter(Resource resource, C context, Class<T> clazz) {
1413 this.resource = resource;
1414 this.context = context;
1419 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1421 final AdaptionService service = getSession().peekService(AdaptionService.class);
1422 if (service == null)
1423 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1425 service.adapt(graph, resource, context, contextClass, clazz, true, procedure);
1430 public String toString() {
1431 return "Possible adapter for (" + resource + "," + context + ") as " + clazz.getName();
1438 return syncRequest(new PossibleContextualAdapter(resource, context, clazz));
1440 } catch (ValidationException e) {
1442 throw new ValidationException(e);
1444 } catch (ServiceException e) {
1446 throw new ServiceException(e);
1448 } catch (DatabaseException e) {
1450 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1457 final public <T> T adaptUnique(final Resource resource, final Class<T> clazz)
1458 throws AdaptionException, ValidationException, ServiceException {
1460 assert (resource != null);
1461 assert (clazz != null);
1465 return syncRequest(new UniqueAdapter<T>(resource, clazz));
1467 } catch (AdaptionException e) {
1469 throw new AdaptionException(e);
1471 } catch (ValidationException e) {
1473 throw new ValidationException(e);
1475 } catch (ServiceException e) {
1477 throw new ServiceException(e);
1479 } catch (DatabaseException e) {
1481 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1488 final public Resource getPossibleInverse(final Resource relation)
1489 throws ServiceException {
1491 assert (relation != null);
1495 return getPossibleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
1497 } catch (ServiceException e) {
1499 throw new ServiceException(e);
1501 } catch (DatabaseException e) {
1503 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1510 public Resource getPossibleObject(final Resource subject, final Resource relation)
1511 throws ManyObjectsForFunctionalRelationException, ServiceException {
1513 assert (subject != null);
1514 assert (relation != null);
1518 int result = processor.getSingleObject(this, subject, relation);
1519 if(result == 0) return null;
1521 return processor.querySupport.getResource(result);
1523 } catch (ManyObjectsForFunctionalRelationException e) {
1525 throw new ManyObjectsForFunctionalRelationException("Many objects in " + subject + " for functional relation " + relation);
1527 } catch (DatabaseException e) {
1529 throw new ServiceException(e);
1536 final public Statement getPossibleStatement(final Resource subject, final Resource relation)
1537 throws ManyObjectsForFunctionalRelationException, ServiceException {
1539 assert (subject != null);
1540 assert (relation != null);
1544 Collection<Statement> statements = getStatements(subject, relation);
1545 if(statements.size() == 1) return statements.iterator().next();
1548 } catch (ManyObjectsForFunctionalRelationException e) {
1550 throw new ManyObjectsForFunctionalRelationException("Many objects in " + subject + " for functional relation " + relation);
1552 } catch (ServiceException e) {
1554 throw new ServiceException(e);
1561 final public Resource getPossibleType(final Resource subject, final Resource baseType) throws ServiceException {
1563 assert (subject != null);
1564 assert (baseType != null);
1568 AsyncReadProcedure<Resource> procedure = new AsyncReadProcedure<Resource>();
1569 forPossibleType(subject, baseType, procedure);
1570 procedure.checkAndThrow();
1571 return procedure.result;
1573 } catch (ServiceException e) {
1575 throw new ServiceException(e);
1577 } catch (DatabaseException e) {
1579 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1586 final public <T> T getPossibleValue(final Resource subject) throws ServiceException {
1588 assert (subject != null);
1592 int object = processor.getSingleObject(this, subject, processor.getL0(this).HasDataType);
1593 if(object == 0) return null;
1595 if(processor.isImmutable(object)) {
1596 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1597 return getPossibleValue(subject, binding);
1599 byte[] dt = processor.getValue(this, object);
1600 if(dt == null) return null;
1601 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1602 Binding binding = Bindings.getBinding(datatype);
1603 return getPossibleValue(subject, binding);
1606 } catch (IOException e) {
1608 throw new ServiceException(e);
1610 } catch (ServiceException e) {
1612 throw new ServiceException(e);
1614 } catch (DatabaseException e) {
1616 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1623 final public <T> T getPossibleValue(final Resource subject, final Binding binding) throws BindingException, ServiceException {
1625 assert (subject != null);
1626 assert (binding != null);
1630 byte[] dt = processor.getValue(this, subject);
1631 if(dt == null) return null;
1632 Serializer serializer = getSerializer(binding);
1633 return (T)serializer.deserialize(dt);
1635 } catch (IOException e) {
1637 throw new ServiceException(e);
1639 } catch (BindingException e) {
1641 throw new BindingException(e);
1643 } catch (ServiceException e) {
1645 throw new ServiceException(e);
1647 } catch (DatabaseException e) {
1648 e.printStackTrace();
1649 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1655 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation)
1656 throws ManyObjectsForFunctionalRelationException, ServiceException {
1658 assert (subject != null);
1659 assert (relation != null);
1663 Resource object = getPossibleObject(subject, relation);
1664 if(object == null) return null;
1665 else return getPossibleValue(object);
1667 } catch (ManyObjectsForFunctionalRelationException e) {
1669 throw new ManyObjectsForFunctionalRelationException(e);
1671 } catch (ServiceException e) {
1673 throw new ServiceException(e);
1680 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1681 throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
1683 assert (subject != null);
1684 assert (relation != null);
1685 assert (binding != null);
1689 Resource object = getPossibleObject(subject, relation);
1690 if(object == null) return null;
1691 else return getPossibleValue(object, binding);
1693 } catch (ManyObjectsForFunctionalRelationException e) {
1695 throw new ManyObjectsForFunctionalRelationException(e);
1697 } catch (BindingException e) {
1699 throw new BindingException(e);
1701 } catch (ServiceException e) {
1703 throw new ServiceException(e);
1710 public <T> T getPossibleAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1712 assert (resource != null);
1713 assert (clazz != null);
1717 return syncRequest(new PossibleAdapter<T>(resource, clazz));
1719 } catch (ValidationException e) {
1721 throw new ValidationException(e);
1723 } catch (AdaptionException e) {
1727 } catch (DatabaseException e) {
1729 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1735 public <T> T getPossibleUniqueAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1737 assert (resource != null);
1738 assert (clazz != null);
1742 return syncRequest(new PossibleUniqueAdapter<T>(resource, clazz));
1744 } catch (AdaptionException e) {
1748 } catch (ValidationException e) {
1750 throw new ValidationException(e);
1752 } catch (DatabaseException e) {
1754 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1761 final public boolean isInstanceOf(final Resource resource, final Resource type) throws ServiceException {
1763 assert (resource != null);
1764 assert (type != null);
1766 Set<Resource> resources = getTypes(resource);
1767 // This check was necessary because some of the callers of this method got stuck when the NPE was thrown from here.
1768 if (null == resources)
1771 if(EMPTY_RESOURCE_CHECK) {
1772 if (resources.isEmpty()) {
1773 if(!hasStatement(resource)) throw new EmptyResourceException("Resource " + debugString(resource));
1777 return resources.contains(type);
1782 final public boolean isInheritedFrom(final Resource resource, final Resource type) throws ServiceException {
1784 assert (resource != null);
1785 assert (type != null);
1789 if(resource.equals(type)) return true;
1791 return getSupertypes(resource).contains(type);
1793 } catch (ServiceException e) {
1795 throw new ServiceException(e);
1802 final public boolean isSubrelationOf(final Resource resource, final Resource type) throws ServiceException {
1804 assert (resource != null);
1805 assert (type != null);
1809 if(resource.equals(type)) return true;
1811 return getSuperrelations(resource).contains(type);
1813 } catch (ServiceException e) {
1815 throw new ServiceException(e);
1822 final public boolean hasStatement(final Resource subject) throws ServiceException {
1824 assert (subject != null);
1828 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1829 processor.forHasStatement(this, subject, procedure);
1830 procedure.checkAndThrow();
1831 return procedure.result;
1833 } catch (ServiceException e) {
1835 throw new ServiceException(e);
1837 } catch (DatabaseException e) {
1839 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1846 final public boolean hasStatement(final Resource subject, final Resource relation) throws ServiceException {
1848 assert (subject != null);
1849 assert (relation != null);
1853 RelationInfo rinfo = processor.getRelationInfo(this, relation);
1854 Collection<Resource> predicates = getPredicates(subject);
1856 if (rinfo.isFinal) {
1858 return predicates.contains(relation);
1860 } else if (rinfo.isFunctional) {
1863 return processor.getSingleObject(this, subject, relation) != 0;
1864 } catch (ManyObjectsForFunctionalRelationException e) {
1866 } catch (DatabaseException e) {
1867 throw new ServiceException(e);
1872 for (Resource predicate : getPredicates(subject)) {
1873 if (isSubrelationOf(predicate, relation))
1881 } catch (ServiceException e) {
1883 throw new ServiceException(e);
1885 } catch (DatabaseException e) {
1887 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1894 final public boolean hasStatement(final Resource subject, final Resource relation, final Resource object) throws ServiceException {
1896 assert (subject != null);
1897 assert (relation != null);
1898 assert (object != null);
1902 for(Resource o : getObjects(subject, relation)) {
1903 if(object.equals(o)) return true;
1908 } catch (ServiceException e) {
1910 throw new ServiceException(e);
1917 final public boolean hasValue(final Resource subject) throws ServiceException {
1919 assert (subject != null);
1923 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1924 processor.forHasValue(this, subject, procedure);
1925 procedure.checkAndThrow();
1926 return procedure.result;
1928 } catch (ServiceException e) {
1930 throw new ServiceException(e);
1932 } catch (DatabaseException e) {
1934 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1940 final AsyncProcedure<?> NONE = new AsyncProcedure<Object>() {
1943 public void execute(AsyncReadGraph graph, Object result) {
1947 public void exception(AsyncReadGraph graph, Throwable throwable) {
1953 * Implementation of the interface RequestProcessor
1957 public <T> T syncRequest(final Read<T> request) throws DatabaseException {
1958 assert (request != null);
1959 return (T)QueryCache.runnerReadEntry(this, request, parent, null, null, true);
1963 public <T> T syncRequest(Read<T> request, SyncListener<T> procedure)
1964 throws DatabaseException {
1965 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
1969 public <T> T syncRequest(Read<T> request, final Listener<T> procedure)
1970 throws DatabaseException {
1971 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
1975 public <T> T syncRequest(final Read<T> request, final AsyncProcedure<T> procedure) throws DatabaseException {
1976 assert (request != null);
1977 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
1978 return (T)QueryCache.runnerReadEntry(this, request, parent, listener, procedure, true);
1982 public <T> T syncRequest(final Read<T> request,
1983 final SyncProcedure<T> procedure) throws DatabaseException {
1984 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
1988 public <T> T syncRequest(Read<T> request, Procedure<T> procedure)
1989 throws DatabaseException {
1990 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
1993 static class AsyncReadProcedure<T> implements AsyncProcedure<T> {
1995 private static Throwable DONE = new Throwable();
1998 Throwable exception = null;
2001 public void execute(AsyncReadGraph graph, T t) {
2007 public void exception(AsyncReadGraph graph, Throwable t) {
2011 public void checkAndThrow() throws DatabaseException {
2012 if(exception != DONE) {
2013 if (exception instanceof DatabaseException)
2014 throw (DatabaseException) exception;
2016 throw new DatabaseException(
2017 "Unexpected exception in ReadGraph.syncRequest(AsyncRead)",
2022 public boolean done() {
2023 return exception != null;
2029 public <T> T syncRequest(final AsyncRead<T> request)
2030 throws DatabaseException {
2032 assert (request != null);
2033 return syncRequest(request, new AsyncProcedureAdapter<>() );
2038 public <T> T syncRequest(AsyncRead<T> request, AsyncListener<T> procedure)
2039 throws DatabaseException {
2040 return syncRequest(request, (AsyncProcedure<T>) procedure);
2044 public <T> T syncRequest(AsyncRead<T> request, SyncListener<T> procedure)
2045 throws DatabaseException {
2046 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
2050 public <T> T syncRequest(AsyncRead<T> request, Listener<T> procedure)
2051 throws DatabaseException {
2052 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
2056 final public <T> T syncRequest(final AsyncRead<T> request,
2057 final AsyncProcedure<T> procedure) throws DatabaseException {
2058 assert (request != null);
2059 ListenerBase listener = getListenerBase(procedure);
2060 return (T)QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure, true);
2064 public <T> T syncRequest(AsyncRead<T> request,
2065 final SyncProcedure<T> procedure) throws DatabaseException {
2066 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2070 final public <T> T syncRequest(final AsyncRead<T> request,
2071 final Procedure<T> procedure) throws DatabaseException {
2072 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2076 public <T> Collection<T> syncRequest(final MultiRead<T> request)
2077 throws DatabaseException {
2079 assert (request != null);
2081 final ArrayList<T> result = new ArrayList<T>();
2082 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2084 syncRequest(request, new SyncMultiProcedure<T>() {
2087 public void execute(ReadGraph graph, T t) {
2088 synchronized (result) {
2094 public void finished(ReadGraph graph) {
2098 public void exception(ReadGraph graph, Throwable t) {
2103 public String toString() {
2104 return "syncRequest(MultiRead) -> " + request;
2109 Throwable t = exception.get();
2111 if (t instanceof DatabaseException)
2112 throw (DatabaseException) t;
2114 throw new DatabaseException(
2115 "Unexpected exception in ReadGraph.syncRequest(Read)",
2124 public <T> Collection<T> syncRequest(MultiRead<T> request,
2125 SyncMultiListener<T> procedure) {
2126 return syncRequest(request, (SyncMultiProcedure<T>)procedure);
2130 public <T> Collection<T> syncRequest(MultiRead<T> request,
2131 MultiListener<T> procedure) {
2132 return syncRequest(request, new NoneToSyncMultiListener<T>(procedure));
2136 public <T> Collection<T> syncRequest(MultiRead<T> request,
2137 SyncMultiProcedure<T> procedure) {
2139 assert (request != null);
2141 ListenerBase listener = getListenerBase(procedure);
2143 final ResultCallWrappedSyncQueryProcedure<T> wrapper = new ResultCallWrappedSyncQueryProcedure<T>(procedure);
2145 if (parent != null || listener != null) {
2147 // Object syncParent = request;
2149 // final ReadGraphImpl newGraph = newSync();
2151 processor.query(this, request, parent, wrapper, listener);
2153 // newGraph.waitAsync(syncParent);
2157 // Object syncParent = request;
2159 // final ReadGraphImpl newGraph = newSync();
2162 request.perform(this, wrapper);
2163 } catch (Throwable t) {
2164 wrapper.exception(this, t);
2169 return wrapper.get();
2174 public <T> Collection<T> syncRequest(MultiRead<T> request,
2175 MultiProcedure<T> procedure) {
2176 return syncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
2179 static class AsyncMultiReadProcedure<T> extends ArrayList<T> implements AsyncMultiProcedure<T> {
2181 private static Throwable DONE = new Throwable();
2183 private static final long serialVersionUID = -6494230465108115812L;
2185 Throwable exception = null;
2188 public synchronized void execute(AsyncReadGraph graph, T t) {
2193 public void finished(AsyncReadGraph graph) {
2198 public void exception(AsyncReadGraph graph, Throwable t) {
2202 public void checkAndThrow() throws DatabaseException {
2203 if(exception != DONE) {
2204 if (exception instanceof DatabaseException)
2205 throw (DatabaseException) exception;
2207 throw new DatabaseException(
2208 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
2213 public boolean done() {
2214 return exception != null;
2220 final public <T> Collection<T> syncRequest(AsyncMultiRead<T> request)
2221 throws DatabaseException {
2223 assert (request != null);
2225 final AsyncMultiReadProcedure<T> procedure = new AsyncMultiReadProcedure<T>();
2227 syncRequest(request, procedure);
2229 procedure.checkAndThrow();
2235 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2236 AsyncMultiListener<T> procedure) {
2237 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2241 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2242 SyncMultiListener<T> procedure) {
2243 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2247 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2248 MultiListener<T> procedure) {
2249 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2252 final private <T> void syncRequest(final AsyncMultiRead<T> request,
2253 final AsyncMultiReadProcedure<T> procedure) {
2255 assert (request != null);
2256 assert (procedure != null);
2258 ListenerBase listener = getListenerBase(procedure);
2260 if (parent != null || listener != null) {
2262 // Object syncParent = request;
2264 // final ReadGraphImpl newGraph = newSync();
2266 processor.query(this, request, parent, procedure, listener);
2268 // newGraph.waitAsync(syncParent);
2269 waitAsyncProcedure(procedure);
2273 // Object syncParent = callerThread == Integer.MIN_VALUE ? null
2276 // final ReadGraphImpl newGraph = newSyncAsync(syncParent);
2281 // ReadGraphImpl sync = newSync();
2282 request.perform(this, procedure);
2283 // sync.waitAsync(null);
2284 waitAsyncProcedure(procedure);
2287 } catch (Throwable t) {
2289 waitAsyncProcedure(procedure);
2300 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2301 final AsyncMultiProcedure<T> procedure) {
2303 assert (request != null);
2304 assert (procedure != null);
2306 ListenerBase listener = getListenerBase(procedure);
2308 if (parent != null || listener != null) {
2310 // Object syncParent = request;
2312 // final ReadGraphImpl newGraph = newSync();
2314 processor.query(this, request, parent, procedure, listener);
2316 // newGraph.waitAsync(syncParent);
2320 // Object syncParent = request;
2322 // final ReadGraphImpl newGraph = newSync();
2326 request.perform(this, new AsyncMultiProcedure<T>() {
2329 public void execute(AsyncReadGraph graph, T result) {
2330 procedure.execute(graph, result);
2334 public void finished(AsyncReadGraph graph) {
2335 procedure.finished(graph);
2339 public void exception(AsyncReadGraph graph, Throwable t) {
2340 procedure.exception(graph, t);
2344 public String toString() {
2345 return "syncRequest(AsyncMultiRead) -> " + procedure;
2350 } catch (Throwable t) {
2362 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2363 final SyncMultiProcedure<T> procedure) {
2364 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2368 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2369 final MultiProcedure<T> procedure) {
2370 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2374 public <T> T syncRequest(final ExternalRead<T> request)
2375 throws DatabaseException {
2377 assert (request != null);
2379 return syncRequest(request, new Procedure<T>() {
2382 public void execute(T t) {
2386 public void exception(Throwable t) {
2390 public String toString() {
2391 return "syncRequest(AsyncRead) -> " + request;
2399 public <T> T syncRequest(ExternalRead<T> request, Listener<T> procedure) throws DatabaseException {
2400 return syncRequest(request, (Procedure<T>) procedure);
2404 final public <T> T syncRequest(final ExternalRead<T> request,
2405 final Procedure<T> procedure) throws DatabaseException {
2407 assert (request != null);
2409 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
2410 return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
2415 public void syncRequest(final Write request) throws DatabaseException {
2417 assert (request != null);
2419 throw new DatabaseException(
2420 "Write operations are not supported during read transactions!");
2425 public <T> T syncRequest(final WriteResult<T> request) throws DatabaseException {
2427 assert (request != null);
2429 throw new DatabaseException(
2430 "Write operations are not supported during read transactions!");
2435 public void syncRequest(final DelayedWrite request)
2436 throws DatabaseException {
2438 assert (request != null);
2440 throw new DatabaseException(
2441 "Write operations are not supported during read transactions!");
2446 public <T> T syncRequest(final DelayedWriteResult<T> request) throws DatabaseException {
2448 assert (request != null);
2450 throw new DatabaseException(
2451 "Write operations are not supported during read transactions!");
2456 public void syncRequest(final WriteOnly request) throws DatabaseException {
2458 assert (request != null);
2460 throw new DatabaseException(
2461 "Write operations are not supported during read transactions!");
2466 public <T> T syncRequest(final WriteOnlyResult<T> request) throws DatabaseException {
2468 assert (request != null);
2470 throw new DatabaseException(
2471 "Write operations are not supported during read transactions!");
2476 public <T> void async(ReadInterface<T> r, AsyncProcedure<T> procedure) {
2477 r.request(this, procedure);
2481 public <T> void async(ReadInterface<T> r, Procedure<T> procedure) {
2482 r.request(this, procedure);
2486 public <T> void async(ReadInterface<T> r, SyncProcedure<T> procedure) {
2487 r.request(this, procedure);
2491 public <T> void async(ReadInterface<T> r, AsyncListener<T> procedure) {
2492 r.request(this, procedure);
2496 public <T> void async(ReadInterface<T> r, Listener<T> procedure) {
2497 r.request(this, procedure);
2501 public <T> void async(ReadInterface<T> r, SyncListener<T> procedure) {
2502 r.request(this, procedure);
2506 public <T> T sync(ReadInterface<T> r) throws DatabaseException {
2507 return r.request(this);
2511 public <T> T sync(WriteInterface<T> r) throws DatabaseException {
2512 return r.request(this);
2516 public <T> void async(WriteInterface<T> r, Procedure<T> procedure) {
2517 r.request(this, procedure);
2521 public <T> void async(WriteInterface<T> r) {
2522 r.request(this, new ProcedureAdapter<T>());
2526 * Implementation of the interface AsyncReadGraph
2530 public void forURI(Resource resource, AsyncListener<String> listener) {
2531 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2536 public void forURI(Resource resource, SyncListener<String> listener) {
2537 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2542 public void forURI(Resource resource, Listener<String> listener) {
2543 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2548 final public void forURI(final Resource resource,
2549 final AsyncProcedure<String> procedure) {
2551 assert (resource != null);
2552 assert (procedure != null);
2554 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2560 public void forURI(Resource resource, SyncProcedure<String> procedure) {
2561 forURI(resource, new SyncToAsyncProcedure<String>(procedure));
2565 public void forURI(Resource resource, Procedure<String> procedure) {
2566 forURI(resource, new NoneToAsyncProcedure<String>(procedure));
2570 public void forResource(String id, AsyncListener<Resource> listener) {
2571 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2576 public void forResource(String id, SyncListener<Resource> listener) {
2577 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2582 public void forResource(String id, Listener<Resource> listener) {
2583 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2588 final public void forResource(final String id,
2589 final AsyncProcedure<Resource> procedure) {
2591 assert (id != null);
2592 assert (procedure != null);
2594 processor.forResource(this, id, procedure);
2599 public void forResource(String id, SyncProcedure<Resource> procedure) {
2600 forResource(id, new SyncToAsyncProcedure<Resource>(procedure));
2604 public void forResource(String id, Procedure<Resource> procedure) {
2605 forResource(id, new NoneToAsyncProcedure<Resource>(procedure));
2609 public void forBuiltin(String id, AsyncListener<Resource> listener) {
2610 asyncRequest(new Builtin(id), listener);
2614 public void forBuiltin(String id, SyncListener<Resource> listener) {
2615 asyncRequest(new Builtin(id), listener);
2619 public void forBuiltin(String id, Listener<Resource> listener) {
2620 asyncRequest(new Builtin(id), listener);
2624 final public void forBuiltin(final String id,
2625 final AsyncProcedure<Resource> procedure) {
2627 assert (id != null);
2628 assert (procedure != null);
2630 processor.forBuiltin(this, id, procedure);
2635 public void forBuiltin(String id, SyncProcedure<Resource> procedure) {
2636 forBuiltin(id, new SyncToAsyncProcedure<Resource>(procedure));
2640 public void forBuiltin(String id, Procedure<Resource> procedure) {
2641 forBuiltin(id, new NoneToAsyncProcedure<Resource>(procedure));
2645 final public void forEachStatement(Resource subject, Resource relation,
2646 AsyncMultiProcedure<Statement> procedure) {
2648 assert (subject != null);
2649 assert (relation != null);
2650 assert (procedure != null);
2652 processor.forEachStatement(this, subject, relation, procedure);
2657 public void forEachStatement(Resource subject, Resource relation,
2658 SyncMultiProcedure<Statement> procedure) {
2659 forEachStatement(subject, relation,
2660 new SyncToAsyncMultiProcedure<Statement>(procedure));
2664 final public void forEachStatement(Resource subject, Resource relation,
2665 MultiProcedure<Statement> procedure) {
2667 assert (subject != null);
2668 assert (relation != null);
2669 assert (procedure != null);
2671 processor.forEachStatement(this, subject, relation, procedure);
2676 final public void forStatementSet(Resource subject, Resource relation,
2677 AsyncSetListener<Statement> procedure) {
2679 assert (subject != null);
2680 assert (relation != null);
2681 assert (procedure != null);
2683 processor.forStatementSet(this, subject, relation, procedure);
2688 final public void forStatementSet(Resource subject, Resource relation,
2689 SyncSetListener<Statement> procedure) {
2690 forStatementSet(subject, relation,
2691 new SyncToAsyncSetProcedure<Statement>(procedure));
2695 public void forStatementSet(Resource subject, Resource relation,
2696 SetListener<Statement> listener) {
2697 forStatementSet(subject, relation,
2698 new NoneToAsyncSetProcedure<Statement>(listener));
2702 final public void forEachAssertedStatement(final Resource subject,
2703 final Resource relation,
2704 final AsyncMultiProcedure<Statement> procedure) {
2706 assert (subject != null);
2707 assert (relation != null);
2708 assert (procedure != null);
2710 processor.forEachAssertedStatement(this, subject, relation, procedure);
2715 public void forEachAssertedStatement(Resource subject, Resource relation,
2716 SyncMultiProcedure<Statement> procedure) {
2717 forEachAssertedStatement(subject, relation,
2718 new SyncToAsyncMultiProcedure<Statement>(procedure));
2722 public void forEachAssertedStatement(Resource subject, Resource relation,
2723 MultiProcedure<Statement> procedure) {
2724 forEachAssertedStatement(subject, relation,
2725 new NoneToAsyncMultiProcedure<Statement>(procedure));
2729 public void forAssertedStatementSet(Resource subject, Resource relation,
2730 AsyncSetListener<Statement> procedure) {
2732 assert (subject != null);
2733 assert (relation != null);
2734 assert (procedure != null);
2736 processor.forAssertedStatementSet(this, subject, relation, procedure);
2741 public void forAssertedStatementSet(Resource subject, Resource relation,
2742 SyncSetListener<Statement> procedure) {
2744 assert (subject != null);
2745 assert (relation != null);
2746 assert (procedure != null);
2748 forAssertedStatementSet(subject, relation,
2749 new SyncToAsyncSetProcedure<Statement>(procedure));
2754 public void forAssertedStatementSet(Resource subject, Resource relation,
2755 SetListener<Statement> procedure) {
2757 assert (subject != null);
2758 assert (relation != null);
2759 assert (procedure != null);
2761 forAssertedStatementSet(subject, relation,
2762 new NoneToAsyncSetProcedure<Statement>(procedure));
2767 final public void forEachPredicate(final Resource subject,
2768 final AsyncMultiProcedure<Resource> procedure) {
2770 assert (subject != null);
2771 assert (procedure != null);
2773 processor.forEachPredicate(this, subject, procedure);
2778 public void forEachPredicate(Resource subject,
2779 SyncMultiProcedure<Resource> procedure) {
2780 forEachPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(
2785 final public void forEachPredicate(final Resource subject,
2786 final MultiProcedure<Resource> procedure) {
2788 assert (subject != null);
2789 assert (procedure != null);
2791 processor.forEachPredicate(this, subject, procedure);
2796 final public void forPredicateSet(final Resource subject,
2797 final AsyncSetListener<Resource> procedure) {
2799 assert (subject != null);
2800 assert (procedure != null);
2802 processor.forPredicateSet(this, subject, procedure);
2807 final public void forPredicateSet(final Resource subject,
2808 final SyncSetListener<Resource> procedure) {
2810 assert (subject != null);
2811 assert (procedure != null);
2813 forPredicateSet(subject, new SyncToAsyncSetProcedure<Resource>(
2819 final public void forPredicateSet(final Resource subject,
2820 final SetListener<Resource> procedure) {
2822 assert (subject != null);
2823 assert (procedure != null);
2825 forPredicateSet(subject, new NoneToAsyncSetProcedure<Resource>(
2831 final public void forEachPrincipalType(final Resource subject,
2832 final AsyncMultiProcedure<Resource> procedure) {
2834 assert (subject != null);
2835 assert (procedure != null);
2837 processor.forEachPrincipalType(this, subject, procedure);
2842 public void forEachPrincipalType(Resource subject,
2843 SyncMultiProcedure<Resource> procedure) {
2844 forEachPrincipalType(subject, new SyncToAsyncMultiProcedure<Resource>(
2849 final public void forEachPrincipalType(final Resource subject,
2850 final MultiProcedure<Resource> procedure) {
2852 assert (subject != null);
2853 assert (procedure != null);
2855 processor.forEachPrincipalType(this, subject, procedure);
2860 final public void forPrincipalTypeSet(final Resource subject,
2861 final AsyncSetListener<Resource> procedure) {
2863 assert (subject != null);
2864 assert (procedure != null);
2866 processor.forPrincipalTypeSet(this, subject, procedure);
2871 final public void forPrincipalTypeSet(final Resource subject,
2872 final SyncSetListener<Resource> procedure) {
2874 assert (subject != null);
2875 assert (procedure != null);
2877 forPrincipalTypeSet(subject, new SyncToAsyncSetProcedure<Resource>(
2883 final public void forPrincipalTypeSet(final Resource subject,
2884 final SetListener<Resource> procedure) {
2886 assert (subject != null);
2887 assert (procedure != null);
2889 forPrincipalTypeSet(subject, new NoneToAsyncSetProcedure<Resource>(
2895 public void forTypes(Resource subject, AsyncListener<Set<Resource>> listener) {
2896 asyncRequest(new Types(subject), listener);
2900 public void forTypes(Resource subject, SyncListener<Set<Resource>> listener) {
2901 asyncRequest(new Types(subject), listener);
2905 public void forTypes(Resource subject, Listener<Set<Resource>> listener) {
2906 asyncRequest(new Types(subject), listener);
2910 final public void forTypes(final Resource subject,
2911 final AsyncProcedure<Set<Resource>> procedure) {
2913 assert (subject != null);
2914 assert (procedure != null);
2916 processor.forTypes(this, subject, procedure);
2921 public void forTypes(Resource subject,
2922 SyncProcedure<Set<Resource>> procedure) {
2923 forTypes(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
2927 public void forTypes(Resource subject, Procedure<Set<Resource>> procedure) {
2928 forTypes(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
2932 public void forSupertypes(Resource subject,
2933 AsyncListener<Set<Resource>> listener) {
2934 asyncRequest(new Types(subject), listener);
2938 public void forSupertypes(Resource subject,
2939 SyncListener<Set<Resource>> listener) {
2940 asyncRequest(new Types(subject), listener);
2944 public void forSupertypes(Resource subject, Listener<Set<Resource>> listener) {
2945 asyncRequest(new Types(subject), listener);
2949 final public void forSupertypes(final Resource subject,
2950 final AsyncProcedure<Set<Resource>> procedure) {
2952 assert (subject != null);
2953 assert (procedure != null);
2955 processor.forSupertypes(this, subject, procedure);
2960 public void forSupertypes(Resource subject,
2961 SyncProcedure<Set<Resource>> procedure) {
2962 forSupertypes(subject, new SyncToAsyncProcedure<Set<Resource>>(
2967 public void forSupertypes(Resource subject,
2968 Procedure<Set<Resource>> procedure) {
2969 forSupertypes(subject, new NoneToAsyncProcedure<Set<Resource>>(
2974 public void forDirectSuperrelations(Resource subject,
2975 AsyncMultiProcedure<Resource> procedure) {
2977 assert (subject != null);
2978 assert (procedure != null);
2980 processor.forDirectSuperrelations(this, subject, procedure);
2985 public void forPossibleSuperrelation(Resource subject, AsyncProcedure<Resource> procedure) {
2987 assert (subject != null);
2988 assert (procedure != null);
2990 processor.forPossibleSuperrelation(this, subject, procedure);
2995 public void forSuperrelations(Resource subject,
2996 AsyncListener<Set<Resource>> listener) {
2997 asyncRequest(new Types(subject), listener);
3001 public void forSuperrelations(Resource subject,
3002 SyncListener<Set<Resource>> listener) {
3003 asyncRequest(new Types(subject), listener);
3007 public void forSuperrelations(Resource subject,
3008 Listener<Set<Resource>> listener) {
3009 asyncRequest(new Types(subject), listener);
3013 final public void forSuperrelations(final Resource subject,
3014 final AsyncProcedure<Set<Resource>> procedure) {
3016 assert (subject != null);
3017 assert (procedure != null);
3019 processor.forSuperrelations(this, subject, procedure);
3024 public void forSuperrelations(Resource subject,
3025 SyncProcedure<Set<Resource>> procedure) {
3026 forSuperrelations(subject, new SyncToAsyncProcedure<Set<Resource>>(
3031 public void forSuperrelations(Resource subject,
3032 Procedure<Set<Resource>> procedure) {
3033 forSuperrelations(subject, new NoneToAsyncProcedure<Set<Resource>>(
3038 final public void forEachObject(final Resource subject, final Resource relation, final AsyncMultiProcedure<Resource> procedure) {
3039 processor.forEachObject(this, subject, relation, procedure);
3043 public void forEachObject(Resource subject, Resource relation,
3044 SyncMultiProcedure<Resource> procedure) {
3045 forEachObject(subject, relation,
3046 new SyncToAsyncMultiProcedure<Resource>(procedure));
3050 public void forEachObject(Resource subject, Resource relation,
3051 MultiProcedure<Resource> procedure) {
3053 processor.forEachObject(this, subject, relation, procedure);
3058 final public void forEachDirectPredicate(final Resource subject, final AsyncProcedure<Set<Resource>> procedure) {
3059 processor.forEachDirectPredicate(this, subject, procedure);
3063 final public void forEachDirectPredicate(final Resource subject, final SyncProcedure<Set<Resource>> procedure) {
3064 forEachDirectPredicate(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
3068 public void forEachDirectPredicate(Resource subject, Procedure<Set<Resource>> procedure) {
3069 forEachDirectPredicate(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
3073 final public void forObjectSet(final Resource subject,
3074 final Resource relation, final AsyncSetListener<Resource> procedure) {
3076 assert (subject != null);
3077 assert (relation != null);
3078 assert (procedure != null);
3080 processor.forObjectSet(this, subject, relation, procedure);
3085 final public void forObjectSet(final Resource subject,
3086 final Resource relation, final SyncSetListener<Resource> procedure) {
3088 assert (subject != null);
3089 assert (relation != null);
3090 assert (procedure != null);
3092 forObjectSet(subject, relation, new SyncToAsyncSetProcedure<Resource>(
3098 final public void forObjectSet(final Resource subject,
3099 final Resource relation, final SetListener<Resource> procedure) {
3101 assert (subject != null);
3102 assert (relation != null);
3103 assert (procedure != null);
3105 forObjectSet(subject, relation, new NoneToAsyncSetProcedure<Resource>(
3111 final public void forEachAssertedObject(final Resource subject,
3112 final Resource relation,
3113 final AsyncMultiProcedure<Resource> procedure) {
3115 assert (subject != null);
3116 assert (relation != null);
3117 assert (procedure != null);
3119 processor.forEachAssertedObject(this, subject, relation, procedure);
3124 public void forEachAssertedObject(Resource subject, Resource relation,
3125 SyncMultiProcedure<Resource> procedure) {
3127 assert (subject != null);
3128 assert (relation != null);
3129 assert (procedure != null);
3131 forEachAssertedObject(subject, relation,
3132 new SyncToAsyncMultiProcedure<Resource>(procedure));
3137 public void forEachAssertedObject(Resource subject, Resource relation,
3138 MultiProcedure<Resource> procedure) {
3140 assert (subject != null);
3141 assert (relation != null);
3142 assert (procedure != null);
3144 forEachAssertedObject(subject, relation,
3145 new NoneToAsyncMultiProcedure<Resource>(procedure));
3150 public void forAssertedObjectSet(Resource subject, Resource relation,
3151 AsyncSetListener<Resource> procedure) {
3153 assert (subject != null);
3154 assert (relation != null);
3155 assert (procedure != null);
3157 processor.forAssertedObjectSet(this, subject, relation, procedure);
3162 public void forAssertedObjectSet(Resource subject, Resource relation,
3163 SyncSetListener<Resource> procedure) {
3165 assert (subject != null);
3166 assert (relation != null);
3167 assert (procedure != null);
3169 forAssertedObjectSet(subject, relation,
3170 new SyncToAsyncSetProcedure<Resource>(procedure));
3175 public void forAssertedObjectSet(Resource subject, Resource relation,
3176 SetListener<Resource> procedure) {
3178 assert (subject != null);
3179 assert (relation != null);
3180 assert (procedure != null);
3182 forAssertedObjectSet(subject, relation,
3183 new NoneToAsyncSetProcedure<Resource>(procedure));
3188 public void forInverse(Resource relation, AsyncListener<Resource> listener) {
3189 asyncRequest(new Inverse(relation), listener);
3193 public void forInverse(Resource relation, SyncListener<Resource> listener) {
3194 asyncRequest(new Inverse(relation), listener);
3198 public void forInverse(Resource relation, Listener<Resource> listener) {
3199 asyncRequest(new Inverse(relation), listener);
3203 final public void forInverse(final Resource relation,
3204 final AsyncProcedure<Resource> procedure) {
3206 assert (relation != null);
3207 assert (procedure != null);
3209 processor.forInverse(this, relation, new AsyncProcedure<Resource>() {
3212 public void execute(AsyncReadGraph graph, Resource result) {
3214 procedure.execute(graph, result);
3216 procedure.exception(graph, new NoInverseException(relation
3222 public void exception(AsyncReadGraph graph, Throwable throwable) {
3223 procedure.exception(graph, throwable);
3227 public String toString() {
3228 return "forInverse -> " + procedure;
3236 public void forInverse(Resource relation, SyncProcedure<Resource> procedure) {
3237 forInverse(relation, new SyncToAsyncProcedure<Resource>(procedure));
3241 public void forInverse(Resource relation, Procedure<Resource> procedure) {
3242 forInverse(relation, new NoneToAsyncProcedure<Resource>(procedure));
3246 public void forSingleObject(Resource subject, Resource relation,
3247 AsyncListener<Resource> listener) {
3248 asyncRequest(new SingleObject(subject, relation), listener);
3252 public void forSingleObject(Resource subject, Resource relation,
3253 SyncListener<Resource> listener) {
3254 asyncRequest(new SingleObject(subject, relation), listener);
3258 public void forSingleObject(Resource subject, Resource relation,
3259 Listener<Resource> listener) {
3260 asyncRequest(new SingleObject(subject, relation), listener);
3264 final public void forSingleObject(final Resource subject,
3265 final Resource relation, final AsyncProcedure<Resource> procedure) {
3267 assert (subject != null);
3268 assert (relation != null);
3269 assert (procedure != null);
3271 processor.forEachObject(this, subject, relation,
3272 new SingleOrErrorProcedure<Resource>(procedure));
3277 public void forSingleObject(Resource subject, Resource relation,
3278 SyncProcedure<Resource> procedure) {
3279 forSingleObject(subject, relation, new SyncToAsyncProcedure<Resource>(
3284 public void forSingleObject(Resource subject, Resource relation,
3285 Procedure<Resource> procedure) {
3286 forSingleObject(subject, relation, new NoneToAsyncProcedure<Resource>(
3291 public void forSingleStatement(Resource subject, Resource relation,
3292 AsyncListener<Statement> listener) {
3293 asyncRequest(new SingleStatement(subject, relation), listener);
3297 public void forSingleStatement(Resource subject, Resource relation,
3298 SyncListener<Statement> listener) {
3299 asyncRequest(new SingleStatement(subject, relation), listener);
3303 public void forSingleStatement(Resource subject, Resource relation,
3304 Listener<Statement> listener) {
3305 asyncRequest(new SingleStatement(subject, relation), listener);
3309 final public void forSingleStatement(final Resource subject,
3310 final Resource relation, final AsyncProcedure<Statement> procedure) {
3312 assert (subject != null);
3313 assert (relation != null);
3314 assert (procedure != null);
3316 processor.forEachStatement(this, subject, relation,
3317 new SingleOrErrorProcedure<Statement>(procedure));
3322 public void forSingleStatement(Resource subject, Resource relation,
3323 SyncProcedure<Statement> procedure) {
3324 forSingleStatement(subject, relation,
3325 new SyncToAsyncProcedure<Statement>(procedure));
3329 public void forSingleStatement(Resource subject, Resource relation,
3330 Procedure<Statement> procedure) {
3331 forSingleStatement(subject, relation,
3332 new NoneToAsyncProcedure<Statement>(procedure));
3336 public void forSingleType(Resource subject,
3337 AsyncListener<Resource> listener) {
3338 asyncRequest(new SingleTypeAny(subject), listener);
3342 public void forSingleType(Resource subject,
3343 SyncListener<Resource> listener) {
3344 asyncRequest(new SingleTypeAny(subject), listener);
3348 public void forSingleType(Resource subject,
3349 Listener<Resource> listener) {
3350 asyncRequest(new SingleTypeAny(subject), listener);
3354 final public void forSingleType(final Resource subject, final AsyncProcedure<Resource> procedure) {
3356 assert (subject != null);
3357 assert (procedure != null);
3359 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3361 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
3364 public void execute(AsyncReadGraph graph, final Resource principalType) {
3365 checkedProcedure.offer(graph, principalType);
3369 public void finished(AsyncReadGraph graph) {
3370 checkedProcedure.dec(graph);
3374 public void exception(AsyncReadGraph graph, Throwable t) {
3375 checkedProcedure.exception(graph, t);
3379 public String toString() {
3380 return "forSingleType -> " + procedure;
3388 public void forSingleType(Resource subject, SyncProcedure<Resource> procedure) {
3389 forSingleType(subject, new SyncToAsyncProcedure<Resource>(
3394 public void forSingleType(Resource subject, Procedure<Resource> procedure) {
3395 forSingleType(subject, new NoneToAsyncProcedure<Resource>(
3400 public void forSingleType(Resource subject, Resource relation,
3401 AsyncListener<Resource> listener) {
3402 asyncRequest(new SingleType(subject, relation), listener);
3406 public void forSingleType(Resource subject, Resource relation,
3407 SyncListener<Resource> listener) {
3408 asyncRequest(new SingleType(subject, relation), listener);
3412 public void forSingleType(Resource subject, Resource relation,
3413 Listener<Resource> listener) {
3414 asyncRequest(new SingleType(subject, relation), listener);
3418 final public void forSingleType(final Resource subject,
3419 final Resource baseType, final AsyncProcedure<Resource> procedure) {
3421 assert (subject != null);
3422 assert (procedure != null);
3424 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3426 processor.forEachPrincipalType(this, subject,
3427 new AsyncMultiProcedureAdapter<Resource>() {
3430 public void execute(AsyncReadGraph graph,
3431 final Resource principalType) {
3433 checkedProcedure.inc();
3435 if(baseType == null) {
3437 checkedProcedure.offer(graph, principalType);
3438 checkedProcedure.dec(graph);
3440 } else if(principalType.equals(baseType)) {
3442 checkedProcedure.offer(graph, principalType);
3443 checkedProcedure.dec(graph);
3447 processor.forSupertypes((ReadGraphImpl)graph, principalType,
3448 new AsyncProcedure<Set<Resource>>() {
3451 public void execute(
3452 AsyncReadGraph graph,
3453 Set<Resource> result) {
3455 if (result.contains(baseType))
3456 checkedProcedure.offer(graph,
3458 checkedProcedure.dec(graph);
3463 public void exception(
3464 AsyncReadGraph graph,
3467 .exception(graph, t);
3477 public void finished(AsyncReadGraph graph) {
3478 checkedProcedure.dec(graph);
3482 public void exception(AsyncReadGraph graph, Throwable t) {
3483 checkedProcedure.exception(graph, t);
3487 public String toString() {
3488 return "forSingleType -> " + procedure;
3496 public void forSingleType(Resource subject, Resource relation,
3497 SyncProcedure<Resource> procedure) {
3498 forSingleType(subject, relation, new SyncToAsyncProcedure<Resource>(
3503 public void forSingleType(Resource subject, Resource relation,
3504 Procedure<Resource> procedure) {
3505 forSingleType(subject, relation, new NoneToAsyncProcedure<Resource>(
3510 public <T> void forValue(Resource subject, Binding binding,
3511 AsyncListener<T> listener) {
3512 asyncRequest(new Value<T>(subject, binding), listener);
3516 public <T> void forValue(Resource subject, Binding binding,
3517 SyncListener<T> listener) {
3518 asyncRequest(new Value<T>(subject, binding), listener);
3522 public <T> void forValue(Resource subject, Binding binding,
3523 Listener<T> listener) {
3524 asyncRequest(new Value<T>(subject, binding), listener);
3528 public <T> void forValue(final Resource resource, final Binding binding,
3529 final AsyncProcedure<T> procedure) {
3531 assert (resource != null);
3532 assert (binding != null);
3533 assert (procedure != null);
3535 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
3538 public void execute(AsyncReadGraph graph, byte[] result) {
3542 if (result == null) {
3543 procedure.exception(graph,
3544 new DoesNotContainValueException(
3545 "No value for resource " + resource));
3549 Serializer serializer = binding.serializer();
3550 // Serializer serializer = Bindings.getSerializer( binding );
3551 Object obj = serializer.deserialize(result);
3552 // if (!binding.isInstance(obj))
3553 // procedure.exception(graph, new ClassCastException(
3554 // "Cannot get value " + obj + " with binding "
3557 procedure.execute(graph, (T) obj);
3559 } catch (Throwable t) {
3560 procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
3565 public void exception(AsyncReadGraph graph, Throwable t) {
3567 procedure.exception(graph, t);
3568 } catch (Throwable t2) {
3569 Logger.defaultLogError(t2);
3574 public String toString() {
3575 return "forValue -> " + procedure;
3582 private static String safeArrayToString(byte[] a) {
3585 int iMax = a.length - 1;
3589 StringBuilder b = new StringBuilder();
3591 for (int i = 0; i < 100; i++) { // limit to first 100 items
3594 return b.append(']').toString();
3597 return b.append(", ... (" + a.length + ")]").toString();
3601 public <T> void forValue(Resource subject, Binding binding,
3602 SyncProcedure<T> procedure) {
3603 forValue(subject, binding, new SyncToAsyncProcedure<T>(procedure));
3607 public <T> void forValue(Resource subject, Binding binding,
3608 Procedure<T> procedure) {
3609 forValue(subject, binding, new NoneToAsyncProcedure<T>(procedure));
3613 public <T> void forValue(Resource subject, AsyncListener<T> listener) {
3614 asyncRequest(new ValueImplied<T>(subject), listener);
3618 public <T> void forValue(Resource subject, SyncListener<T> listener) {
3619 asyncRequest(new ValueImplied<T>(subject), listener);
3623 public <T> void forValue(Resource subject, Listener<T> listener) {
3624 asyncRequest(new ValueImplied<T>(subject), listener);
3628 final public <T> void forValue(final Resource subject, final AsyncProcedure<T> procedure) {
3630 assert (subject != null);
3631 assert (procedure != null);
3633 forRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
3636 public void execute(AsyncReadGraph graph, Datatype type) {
3637 // TODO: consider trying Bindings.getBeanBinding(type);
3638 Binding binding = Bindings.getBinding(type);
3639 graph.forValue(subject, binding, procedure);
3643 public void exception(AsyncReadGraph graph, Throwable throwable) {
3644 procedure.exception(graph, new DoesNotContainValueException("Invalid data type", throwable));
3652 public <T> void forValue(Resource subject, SyncProcedure<T> procedure) {
3653 forValue(subject, new SyncToAsyncProcedure<T>(procedure));
3657 public <T> void forValue(Resource subject, Procedure<T> procedure) {
3658 forValue(subject, new NoneToAsyncProcedure<T>(procedure));
3662 public <T> void forRelatedValue(Resource subject, Resource relation,
3663 AsyncListener<T> listener) {
3664 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3668 public <T> void forRelatedValue(Resource subject, Resource relation,
3669 SyncListener<T> listener) {
3670 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3674 public <T> void forRelatedValue(Resource subject, Resource relation,
3675 Listener<T> listener) {
3676 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3680 final public <T> void forRelatedValue(final Resource subject,
3681 final Resource relation, final AsyncProcedure<T> procedure) {
3683 assert (subject != null);
3684 assert (relation != null);
3685 assert (procedure != null);
3687 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3689 processor.forEachObject(this, subject, relation,
3690 new AsyncMultiProcedureAdapter<Resource>() {
3693 public void execute(AsyncReadGraph graph,
3694 final Resource object) {
3696 checkedProcedure.inc();
3698 graph.forValue(object, new AsyncProcedure<Object>() {
3701 public void execute(AsyncReadGraph graph,
3703 checkedProcedure.offer(graph, (T) result);
3704 checkedProcedure.dec(graph);
3708 public void exception(AsyncReadGraph graph,
3710 checkedProcedure.exception(graph, t);
3714 public String toString() {
3715 return "forRelatedValue -> " + procedure;
3723 public void finished(AsyncReadGraph graph) {
3724 checkedProcedure.dec(graph);
3728 public void exception(AsyncReadGraph graph, Throwable t) {
3729 checkedProcedure.exception(graph, t);
3737 public <T> void forRelatedValue(Resource subject, Resource relation,
3738 SyncProcedure<T> procedure) {
3739 forRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
3744 public <T> void forRelatedValue(Resource subject, Resource relation,
3745 Procedure<T> procedure) {
3746 forRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
3751 public <T> void forRelatedValue(Resource subject, Resource relation,
3752 Binding binding, AsyncListener<T> listener) {
3753 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3757 public <T> void forRelatedValue(Resource subject, Resource relation,
3758 Binding binding, SyncListener<T> listener) {
3759 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3763 public <T> void forRelatedValue(Resource subject, Resource relation,
3764 Binding binding, Listener<T> listener) {
3765 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3769 final public <T> void forRelatedValue(final Resource subject,
3770 final Resource relation, final Binding binding,
3771 final AsyncProcedure<T> procedure) {
3773 assert (subject != null);
3774 assert (relation != null);
3775 assert (binding != null);
3776 assert (procedure != null);
3778 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3780 processor.forEachObject(this, subject, relation,
3781 new AsyncMultiProcedureAdapter<Resource>() {
3784 public void execute(AsyncReadGraph graph,
3785 final Resource object) {
3787 checkedProcedure.inc();
3789 graph.forValue(object, binding, new AsyncProcedure<Object>() {
3792 public void execute(AsyncReadGraph graph,
3795 checkedProcedure.offer(graph,
3797 checkedProcedure.dec(graph);
3801 public void exception(AsyncReadGraph graph,
3803 checkedProcedure.exception(graph, t);
3807 public String toString() {
3808 return "forRelatedValue -> "
3817 public void finished(AsyncReadGraph graph) {
3818 checkedProcedure.dec(graph);
3822 public void exception(AsyncReadGraph graph, Throwable t) {
3823 checkedProcedure.exception(graph, t);
3831 public <T> void forRelatedValue(Resource subject, Resource relation,
3832 Binding binding, SyncProcedure<T> procedure) {
3833 forRelatedValue(subject, relation, binding,
3834 new SyncToAsyncProcedure<T>(procedure));
3838 public <T> void forRelatedValue(Resource subject, Resource relation,
3839 Binding binding, Procedure<T> procedure) {
3840 forRelatedValue(subject, relation, binding,
3841 new NoneToAsyncProcedure<T>(procedure));
3845 public <T> void forAdapted(Resource resource, Class<T> clazz,
3846 AsyncListener<T> listener) {
3847 asyncRequest(new Adapter<T>(resource, clazz), listener);
3851 public <T> void forAdapted(Resource resource, Class<T> clazz,
3852 SyncListener<T> listener) {
3853 asyncRequest(new Adapter<T>(resource, clazz), listener);
3857 public <T> void forAdapted(Resource resource, Class<T> clazz,
3858 Listener<T> listener) {
3859 asyncRequest(new Adapter<T>(resource, clazz), listener);
3863 final public <T> void forAdapted(final Resource resource,
3864 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3866 assert (resource != null);
3867 assert (clazz != null);
3868 assert (procedure != null);
3870 final AdaptionService service = getSession().peekService(AdaptionService.class);
3871 if (service == null)
3872 procedure.exception(this, new ServiceException("No AdaptionService available"));
3874 service.adapt(this, resource, resource, Resource.class, clazz, false, procedure);
3879 public <T> void forAdapted(Resource resource, Class<T> clazz,
3880 SyncProcedure<T> procedure) {
3881 forAdapted(resource, clazz, new SyncToAsyncProcedure<T>(procedure));
3885 public <T> void forAdapted(Resource resource, Class<T> clazz,
3886 Procedure<T> procedure) {
3887 forAdapted(resource, clazz, new NoneToAsyncProcedure<T>(procedure));
3891 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3892 AsyncListener<T> listener) {
3893 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3897 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3898 SyncListener<T> listener) {
3899 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3903 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3904 Listener<T> listener) {
3905 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3909 final public <T> void forUniqueAdapted(final Resource resource,
3910 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3912 assert (resource != null);
3913 assert (clazz != null);
3914 assert (procedure != null);
3916 final AdaptionService service = getSession().peekService(AdaptionService.class);
3917 if (service == null)
3918 procedure.exception(this, new ServiceException("No AdaptionService available"));
3920 service.adaptNew(this, resource, clazz, false, procedure);
3925 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3926 SyncProcedure<T> procedure) {
3927 forUniqueAdapted(resource, clazz,
3928 new SyncToAsyncProcedure<T>(procedure));
3932 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3933 Procedure<T> procedure) {
3934 forUniqueAdapted(resource, clazz,
3935 new NoneToAsyncProcedure<T>(procedure));
3939 public void forPossibleInverse(Resource subject,
3940 AsyncListener<Resource> listener) {
3941 asyncRequest(new PossibleInverse(subject), listener);
3945 public void forPossibleInverse(Resource subject,
3946 SyncListener<Resource> listener) {
3947 asyncRequest(new PossibleInverse(subject), listener);
3951 public void forPossibleInverse(Resource subject, Listener<Resource> listener) {
3952 asyncRequest(new PossibleInverse(subject), listener);
3956 final public void forPossibleInverse(final Resource relation,
3957 final AsyncProcedure<Resource> procedure) {
3959 assert (relation != null);
3960 assert (procedure != null);
3962 processor.forInverse(this, relation, new ExceptionToNullProcedure<Resource>(procedure));
3967 public void forPossibleInverse(Resource subject,
3968 SyncProcedure<Resource> procedure) {
3969 forPossibleInverse(subject, new SyncToAsyncProcedure<Resource>(
3974 public void forPossibleInverse(Resource subject,
3975 Procedure<Resource> procedure) {
3976 forPossibleInverse(subject, new NoneToAsyncProcedure<Resource>(
3981 public void forPossibleObject(Resource subject, Resource relation,
3982 AsyncListener<Resource> listener) {
3983 asyncRequest(new PossibleObject(subject, relation), listener);
3987 public void forPossibleObject(Resource subject, Resource relation,
3988 SyncListener<Resource> listener) {
3989 asyncRequest(new PossibleObject(subject, relation), listener);
3993 public void forPossibleObject(Resource subject, Resource relation,
3994 Listener<Resource> listener) {
3995 asyncRequest(new PossibleObject(subject, relation), listener);
3999 final public void forPossibleObject(final Resource subject,
4000 final Resource relation, final AsyncProcedure<Resource> procedure) {
4002 assert (subject != null);
4003 assert (relation != null);
4004 assert (procedure != null);
4006 processor.forEachObject(this, subject, relation,
4007 new SingleOrNullProcedure<Resource>(procedure));
4012 public void forPossibleObject(Resource subject, Resource relation,
4013 SyncProcedure<Resource> procedure) {
4014 forPossibleObject(subject, relation,
4015 new SyncToAsyncProcedure<Resource>(procedure));
4019 public void forPossibleObject(Resource subject, Resource relation,
4020 Procedure<Resource> procedure) {
4021 forPossibleObject(subject, relation,
4022 new NoneToAsyncProcedure<Resource>(procedure));
4026 public void forPossibleStatement(Resource subject, Resource relation,
4027 AsyncListener<Statement> listener) {
4028 asyncRequest(new PossibleStatement(subject, relation), listener);
4032 public void forPossibleStatement(Resource subject, Resource relation,
4033 SyncListener<Statement> listener) {
4034 asyncRequest(new PossibleStatement(subject, relation), listener);
4038 public void forPossibleStatement(Resource subject, Resource relation,
4039 Listener<Statement> listener) {
4040 asyncRequest(new PossibleStatement(subject, relation), listener);
4044 final public void forPossibleStatement(final Resource subject,
4045 final Resource relation, final AsyncProcedure<Statement> procedure) {
4047 assert (subject != null);
4048 assert (relation != null);
4049 assert (procedure != null);
4051 processor.forEachStatement(this, subject, relation,
4052 new SingleFunctionalOrNullProcedure<Statement>(
4053 "forPossibleStatement", procedure));
4058 public void forPossibleStatement(Resource subject, Resource relation,
4059 SyncProcedure<Statement> procedure) {
4060 forPossibleStatement(subject, relation,
4061 new SyncToAsyncProcedure<Statement>(procedure));
4065 public void forPossibleStatement(Resource subject, Resource relation,
4066 Procedure<Statement> procedure) {
4067 forPossibleStatement(subject, relation,
4068 new NoneToAsyncProcedure<Statement>(procedure));
4072 public void forPossibleType(Resource subject, Resource relation,
4073 AsyncListener<Resource> listener) {
4074 asyncRequest(new PossibleType(subject, relation), listener);
4078 public void forPossibleType(Resource subject, Resource relation,
4079 SyncListener<Resource> listener) {
4080 asyncRequest(new PossibleType(subject, relation), listener);
4084 public void forPossibleType(Resource subject, Resource relation,
4085 Listener<Resource> listener) {
4086 asyncRequest(new PossibleType(subject, relation), listener);
4090 final public void forPossibleType(final Resource subject,
4091 final Resource baseType, final AsyncProcedure<Resource> procedure) {
4093 assert (subject != null);
4094 assert (procedure != null);
4096 final NullSingleOrNullProcedure<Resource> checkedProcedure = new NullSingleOrNullProcedure<Resource>(procedure);
4098 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
4101 public void execute(AsyncReadGraph graph,
4102 final Resource principalType) {
4104 if (baseType == null) {
4106 checkedProcedure.offer(graph, principalType);
4108 } else if (principalType.equals(baseType)) {
4110 checkedProcedure.offer(graph, principalType);
4114 checkedProcedure.inc();
4116 processor.forSupertypes((ReadGraphImpl)graph, principalType,
4117 new AsyncProcedure<Set<Resource>>() {
4120 public void execute(
4121 AsyncReadGraph graph,
4122 Set<Resource> result) {
4124 if (result.contains(baseType)) {
4125 checkedProcedure.offer(graph,
4129 checkedProcedure.dec(graph);
4134 public void exception(
4135 AsyncReadGraph graph,
4137 checkedProcedure.exception(graph, t);
4138 checkedProcedure.dec(graph);
4142 public String toString() {
4143 return "forPossibleType -> "
4154 public void finished(AsyncReadGraph graph) {
4155 checkedProcedure.dec(graph);
4159 public void exception(AsyncReadGraph graph, Throwable t) {
4160 checkedProcedure.exception(graph, t);
4161 checkedProcedure.dec(graph);
4169 public void forPossibleType(Resource subject, Resource relation,
4170 SyncProcedure<Resource> procedure) {
4171 forPossibleType(subject, relation, new SyncToAsyncProcedure<Resource>(
4176 public void forPossibleType(Resource subject, Resource relation,
4177 Procedure<Resource> procedure) {
4178 forPossibleType(subject, relation, new NoneToAsyncProcedure<Resource>(
4183 public <T> void forPossibleValue(Resource subject, AsyncListener<T> listener) {
4184 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4188 public <T> void forPossibleValue(Resource subject, SyncListener<T> listener) {
4189 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4193 public <T> void forPossibleValue(Resource subject, Listener<T> listener) {
4194 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4198 final public <T> void forPossibleValue(final Resource subject,
4199 final AsyncProcedure<T> procedure) {
4201 assert (subject != null);
4202 assert (procedure != null);
4204 forPossibleRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
4207 public void execute(AsyncReadGraph graph, final Datatype type) {
4209 procedure.execute(graph, null);
4212 // TODO: consider trying Bindings.getBeanBinding(type);
4213 Binding binding = Bindings.getBinding(type);
4214 graph.forPossibleValue(subject, binding, procedure);
4215 } catch (RuntimeBindingConstructionException e) {
4216 procedure.exception(graph, e);
4222 public void exception(AsyncReadGraph graph, Throwable t) {
4223 procedure.exception(graph, t);
4227 public String toString() {
4228 return "forPossibleValue -> " + procedure;
4236 public <T> void forPossibleValue(Resource subject,
4237 SyncProcedure<T> procedure) {
4238 forPossibleValue(subject, new SyncToAsyncProcedure<T>(procedure));
4242 public <T> void forPossibleValue(Resource subject, Procedure<T> procedure) {
4243 forPossibleValue(subject, new NoneToAsyncProcedure<T>(procedure));
4247 public <T> void forPossibleValue(Resource subject, Binding binding,
4248 AsyncListener<T> listener) {
4249 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4253 public <T> void forPossibleValue(Resource subject, Binding binding,
4254 SyncListener<T> listener) {
4255 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4259 public <T> void forPossibleValue(Resource subject, Binding binding,
4260 Listener<T> listener) {
4261 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4265 final public <T> void forPossibleValue(final Resource resource,
4266 final Binding binding, final AsyncProcedure<T> procedure) {
4268 assert (resource != null);
4269 assert (binding != null);
4270 assert (procedure != null);
4272 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
4275 public void execute(AsyncReadGraph graph, byte[] result) {
4279 if (result == null) {
4280 procedure.execute(graph, null);
4284 Serializer serializer = Bindings.getSerializer( binding );
4285 Object obj = serializer.deserialize(result);
4286 if (!binding.isInstance(obj))
4287 procedure.exception(graph, new ClassCastException(
4288 "Cannot get value " + obj + " with binding "
4291 procedure.execute(graph, (T) obj);
4293 } catch (Throwable t) {
4294 procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
4299 public void exception(AsyncReadGraph graph, Throwable t) {
4301 procedure.exception(graph, t);
4302 } catch (Throwable t2) {
4303 Logger.defaultLogError(t2);
4308 public String toString() {
4309 return "forPossibleValue -> " + procedure;
4317 public <T> void forPossibleValue(Resource subject, Binding binding,
4318 SyncProcedure<T> procedure) {
4319 forPossibleValue(subject, binding, new SyncToAsyncProcedure<T>(
4324 public <T> void forPossibleValue(Resource subject, Binding binding,
4325 Procedure<T> procedure) {
4326 forPossibleValue(subject, binding, new NoneToAsyncProcedure<T>(
4331 public <T> void forPossibleRelatedValue(Resource subject,
4332 Resource relation, AsyncListener<T> listener) {
4333 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4338 public <T> void forPossibleRelatedValue(Resource subject,
4339 Resource relation, SyncListener<T> listener) {
4340 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4345 public <T> void forPossibleRelatedValue(Resource subject,
4346 Resource relation, Listener<T> listener) {
4347 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4352 final public <T> void forPossibleRelatedValue(final Resource subject,
4353 final Resource relation, final AsyncProcedure<T> procedure) {
4355 assert (subject != null);
4356 assert (relation != null);
4357 assert (procedure != null);
4359 final DeepSingleOrNullProcedure<T> checkedProcedure = new DeepSingleOrNullProcedure<T>(procedure);
4361 processor.forEachObject(this, subject, relation,
4362 new AsyncMultiProcedureAdapter<Resource>() {
4365 public void execute(AsyncReadGraph graph,
4366 final Resource object) {
4368 checkedProcedure.inc();
4370 graph.forValue(object, new AsyncProcedure<Object>() {
4373 public void execute(AsyncReadGraph graph,
4375 checkedProcedure.offer(graph, (T) result);
4376 checkedProcedure.dec(graph);
4380 public void exception(AsyncReadGraph graph,
4382 checkedProcedure.exception(graph, t);
4383 checkedProcedure.dec(graph);
4391 public void finished(AsyncReadGraph graph) {
4393 checkedProcedure.dec(graph);
4397 public void exception(AsyncReadGraph graph, Throwable t) {
4398 checkedProcedure.exception(graph, t);
4399 checkedProcedure.dec(graph);
4403 public String toString() {
4404 return "forPossibleRelatedValue -> " + procedure;
4411 public <T> void forPossibleRelatedValue(Resource subject,
4412 Resource relation, SyncProcedure<T> procedure) {
4413 forPossibleRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
4418 public <T> void forPossibleRelatedValue(Resource subject,
4419 Resource relation, Procedure<T> procedure) {
4420 forPossibleRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
4425 public <T> void forPossibleRelatedValue(Resource subject,
4426 Resource relation, Binding binding, AsyncListener<T> listener) {
4427 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4432 public <T> void forPossibleRelatedValue(Resource subject,
4433 Resource relation, Binding binding, SyncListener<T> listener) {
4434 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4439 public <T> void forPossibleRelatedValue(Resource subject,
4440 Resource relation, Binding binding, Listener<T> listener) {
4441 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4445 final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4446 final AsyncProcedure<T> procedure) {
4448 assert (subject != null);
4449 assert (relation != null);
4450 assert (procedure != null);
4452 processor.forPossibleObject(this, subject, relation, new AsyncProcedure<Resource>() {
4455 public void execute(AsyncReadGraph graph, Resource object) {
4457 if(object == null) {
4458 procedure.execute(graph, null);
4462 processor.forPossibleValue((ReadGraphImpl)graph, object, new AsyncProcedure<byte[]>() {
4465 public void execute(AsyncReadGraph graph, byte[] bytes) {
4471 Serializer serializer = binding.serializer();
4472 Object obj = serializer.deserialize(bytes);
4473 if (!binding.isInstance(obj)) {
4474 procedure.exception(graph, new ClassCastException("Cannot get value " + obj + " with binding " + binding));
4476 procedure.execute(graph, (T) obj);
4479 } catch (Throwable t) {
4481 procedure.exception(graph, t);
4487 procedure.execute(graph, null);
4494 public void exception(AsyncReadGraph graph, Throwable t) {
4495 procedure.exception(graph, t);
4503 public void exception(AsyncReadGraph graph, Throwable throwable) {
4504 throwable.printStackTrace();
4505 procedure.exception(graph, throwable);
4513 public <T> void forPossibleRelatedValue(Resource subject,
4514 Resource relation, Binding binding, SyncProcedure<T> procedure) {
4515 forPossibleRelatedValue(subject, relation, binding,
4516 new SyncToAsyncProcedure<T>(procedure));
4520 public <T> void forPossibleRelatedValue(Resource subject,
4521 Resource relation, Binding binding, Procedure<T> procedure) {
4522 forPossibleRelatedValue(subject, relation, binding,
4523 new NoneToAsyncProcedure<T>(procedure));
4527 public void forIsInstanceOf(Resource subject, Resource relation,
4528 AsyncListener<Boolean> listener) {
4529 asyncRequest(new IsInstanceOf(subject, relation), listener);
4533 public void forIsInstanceOf(Resource subject, Resource relation,
4534 SyncListener<Boolean> listener) {
4535 asyncRequest(new IsInstanceOf(subject, relation), listener);
4539 public void forIsInstanceOf(Resource subject, Resource relation,
4540 Listener<Boolean> listener) {
4541 asyncRequest(new IsInstanceOf(subject, relation), listener);
4545 final public void forIsInstanceOf(final Resource resource,
4546 final Resource type, final AsyncProcedure<Boolean> procedure) {
4548 assert (resource != null);
4549 assert (type != null);
4550 assert (procedure != null);
4552 forTypes(resource, new AsyncProcedure<Set<Resource>>() {
4555 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4558 if (result.contains(type))
4559 procedure.execute(graph, true);
4561 procedure.execute(graph, false);
4562 } catch (Throwable t) {
4563 Logger.defaultLogError(t);
4568 public void exception(AsyncReadGraph graph, Throwable t) {
4570 procedure.exception(graph, t);
4571 } catch (Throwable t2) {
4572 Logger.defaultLogError(t2);
4577 public String toString() {
4578 return "forIsInstanceOf -> " + procedure;
4586 public void forIsInstanceOf(Resource subject, Resource relation,
4587 SyncProcedure<Boolean> procedure) {
4588 forIsInstanceOf(subject, relation, new SyncToAsyncProcedure<Boolean>(
4593 public void forIsInstanceOf(Resource subject, Resource relation,
4594 Procedure<Boolean> procedure) {
4595 forIsInstanceOf(subject, relation, new NoneToAsyncProcedure<Boolean>(
4600 public void forIsInheritedFrom(Resource subject, Resource relation,
4601 AsyncListener<Boolean> listener) {
4602 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4606 public void forIsInheritedFrom(Resource subject, Resource relation,
4607 SyncListener<Boolean> listener) {
4608 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4612 public void forIsInheritedFrom(Resource subject, Resource relation,
4613 Listener<Boolean> listener) {
4614 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4618 final public void forIsInheritedFrom(final Resource resource,
4619 final Resource type, final AsyncProcedure<Boolean> procedure) {
4621 assert (resource != null);
4622 assert (type != null);
4623 assert (procedure != null);
4625 if (resource.equals(type)) {
4627 procedure.execute(this, true);
4628 } catch (Throwable t) {
4629 Logger.defaultLogError(t);
4634 forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
4637 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4639 if (result.contains(type))
4640 procedure.execute(graph, true);
4642 procedure.execute(graph, false);
4643 } catch (Throwable t) {
4644 Logger.defaultLogError(t);
4649 public void exception(AsyncReadGraph graph, Throwable t) {
4651 procedure.exception(graph, t);
4652 } catch (Throwable t2) {
4653 Logger.defaultLogError(t2);
4658 public String toString() {
4659 return "forIsInheritedFrom -> " + procedure;
4667 public void forIsInheritedFrom(Resource subject, Resource relation,
4668 SyncProcedure<Boolean> procedure) {
4669 forIsInheritedFrom(subject, relation,
4670 new SyncToAsyncProcedure<Boolean>(procedure));
4674 public void forIsInheritedFrom(Resource subject, Resource relation,
4675 Procedure<Boolean> procedure) {
4676 forIsInheritedFrom(subject, relation,
4677 new NoneToAsyncProcedure<Boolean>(procedure));
4681 public void forIsSubrelationOf(Resource subject, Resource relation,
4682 AsyncListener<Boolean> listener) {
4683 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4687 public void forIsSubrelationOf(Resource subject, Resource relation,
4688 SyncListener<Boolean> listener) {
4689 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4693 public void forIsSubrelationOf(Resource subject, Resource relation,
4694 Listener<Boolean> listener) {
4695 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4699 final public void forIsSubrelationOf(final Resource resource,
4700 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4702 assert (resource != null);
4703 assert (relation != null);
4704 assert (procedure != null);
4706 if (resource.equals(relation)) {
4707 procedure.execute(this, true);
4711 forSuperrelations(resource, new AsyncProcedure<Set<Resource>>() {
4714 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4716 if (result.contains(relation))
4717 procedure.execute(graph, true);
4719 procedure.execute(graph, false);
4720 } catch (Throwable t) {
4721 Logger.defaultLogError(t);
4726 public void exception(AsyncReadGraph graph, Throwable t) {
4728 procedure.exception(graph, t);
4729 } catch (Throwable t2) {
4730 Logger.defaultLogError(t2);
4735 public String toString() {
4736 return "forIsSubrelationOf -> " + procedure;
4744 public void forIsSubrelationOf(Resource subject, Resource relation,
4745 SyncProcedure<Boolean> procedure) {
4746 forIsSubrelationOf(subject, relation,
4747 new SyncToAsyncProcedure<Boolean>(procedure));
4751 public void forIsSubrelationOf(Resource subject, Resource relation,
4752 Procedure<Boolean> procedure) {
4753 forIsSubrelationOf(subject, relation,
4754 new NoneToAsyncProcedure<Boolean>(procedure));
4758 public void forHasStatement(Resource subject,
4759 AsyncListener<Boolean> listener) {
4760 asyncRequest(new HasStatementSubject(subject), listener);
4764 public void forHasStatement(Resource subject, SyncListener<Boolean> listener) {
4765 asyncRequest(new HasStatementSubject(subject), listener);
4769 public void forHasStatement(Resource subject, Listener<Boolean> listener) {
4770 asyncRequest(new HasStatementSubject(subject), listener);
4774 final public void forHasStatement(final Resource subject,
4775 final AsyncProcedure<Boolean> procedure) {
4777 assert (subject != null);
4778 assert (procedure != null);
4780 processor.forHasStatement(this, subject, procedure);
4785 public void forHasStatement(Resource subject,
4786 SyncProcedure<Boolean> procedure) {
4787 forHasStatement(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4791 public void forHasStatement(Resource subject, Procedure<Boolean> procedure) {
4792 forHasStatement(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4796 public void forHasStatement(Resource subject, Resource relation,
4797 AsyncListener<Boolean> listener) {
4798 asyncRequest(new HasStatement(subject, relation), listener);
4802 public void forHasStatement(Resource subject, Resource relation,
4803 SyncListener<Boolean> listener) {
4804 asyncRequest(new HasStatement(subject, relation), listener);
4808 public void forHasStatement(Resource subject, Resource relation,
4809 Listener<Boolean> listener) {
4810 asyncRequest(new HasStatement(subject, relation), listener);
4814 final public void forHasStatement(final Resource subject,
4815 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4817 assert (subject != null);
4818 assert (relation != null);
4819 assert (procedure != null);
4821 processor.forHasStatement(this, subject, relation, procedure);
4826 public void forHasStatement(Resource subject, Resource relation,
4827 SyncProcedure<Boolean> procedure) {
4828 forHasStatement(subject, relation, new SyncToAsyncProcedure<Boolean>(
4833 public void forHasStatement(Resource subject, Resource relation,
4834 Procedure<Boolean> procedure) {
4835 forHasStatement(subject, relation, new NoneToAsyncProcedure<Boolean>(
4840 public void forHasStatement(Resource subject, Resource relation,
4841 Resource object, AsyncListener<Boolean> listener) {
4842 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4847 public void forHasStatement(Resource subject, Resource relation,
4848 Resource object, SyncListener<Boolean> listener) {
4849 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4854 public void forHasStatement(Resource subject, Resource relation,
4855 Resource object, Listener<Boolean> listener) {
4856 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4861 final public void forHasStatement(final Resource subject,
4862 final Resource relation, final Resource object,
4863 final AsyncProcedure<Boolean> procedure) {
4865 assert (subject != null);
4866 assert (relation != null);
4867 assert (object != null);
4868 assert (procedure != null);
4870 processor.forHasStatement(this, subject, relation, object, procedure);
4875 public void forHasStatement(Resource subject, Resource relation,
4876 Resource object, SyncProcedure<Boolean> procedure) {
4877 forHasStatement(subject, relation, object,
4878 new SyncToAsyncProcedure<Boolean>(procedure));
4882 public void forHasStatement(Resource subject, Resource relation,
4883 Resource object, Procedure<Boolean> procedure) {
4884 forHasStatement(subject, relation, object,
4885 new NoneToAsyncProcedure<Boolean>(procedure));
4889 public void forHasValue(Resource subject, AsyncListener<Boolean> listener) {
4890 asyncRequest(new HasValue(subject), listener);
4894 public void forHasValue(Resource subject, SyncListener<Boolean> listener) {
4895 asyncRequest(new HasValue(subject), listener);
4899 public void forHasValue(Resource subject, Listener<Boolean> listener) {
4900 asyncRequest(new HasValue(subject), listener);
4904 final public void forHasValue(final Resource subject,
4905 final AsyncProcedure<Boolean> procedure) {
4907 assert (subject != null);
4908 assert (procedure != null);
4910 processor.forValue(this, subject, new AsyncProcedure<byte[]>() {
4913 public void execute(AsyncReadGraph graph, byte[] result) {
4916 procedure.execute(graph, false);
4918 procedure.execute(graph, true);
4919 } catch (Throwable t) {
4920 Logger.defaultLogError(t);
4925 public void exception(AsyncReadGraph graph, Throwable t) {
4927 procedure.exception(graph, t);
4928 } catch (Throwable t2) {
4929 Logger.defaultLogError(t2);
4934 public String toString() {
4935 return "forHasValue -> " + procedure;
4943 public void forHasValue(Resource subject, SyncProcedure<Boolean> procedure) {
4944 forHasValue(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4948 public void forHasValue(Resource subject, Procedure<Boolean> procedure) {
4949 forHasValue(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4953 public void forOrderedSet(Resource subject,
4954 AsyncMultiListener<Resource> listener) {
4955 asyncRequest(new OrderedSet(subject), listener);
4959 public void forOrderedSet(Resource subject,
4960 SyncMultiListener<Resource> listener) {
4961 asyncRequest(new OrderedSet(subject), listener);
4965 public void forOrderedSet(Resource subject, MultiListener<Resource> listener) {
4966 asyncRequest(new OrderedSet(subject), listener);
4970 final public void forOrderedSet(final Resource subject,
4971 final AsyncMultiProcedure<Resource> procedure) {
4973 assert (subject != null);
4974 assert (procedure != null);
4976 processor.forOrderedSet(this, subject,
4977 new AsyncMultiProcedure<Resource>() {
4980 public void finished(AsyncReadGraph graph) {
4982 procedure.finished(graph);
4983 } catch (Throwable t) {
4984 Logger.defaultLogError(t);
4989 public void execute(AsyncReadGraph graph, Resource result) {
4991 procedure.execute(graph, result);
4992 } catch (Throwable t) {
4993 Logger.defaultLogError(t);
4998 public void exception(AsyncReadGraph graph, Throwable t) {
5000 procedure.exception(graph, t);
5001 } catch (Throwable t2) {
5002 Logger.defaultLogError(t2);
5007 public String toString() {
5008 return "forOrderedSet -> " + procedure;
5016 public void forOrderedSet(Resource subject,
5017 SyncMultiProcedure<Resource> procedure) {
5018 forOrderedSet(subject, new SyncToAsyncMultiProcedure<Resource>(
5023 public void forOrderedSet(Resource subject,
5024 MultiProcedure<Resource> procedure) {
5025 forOrderedSet(subject, new NoneToAsyncMultiProcedure<Resource>(
5030 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5031 AsyncListener<T> listener) {
5032 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5036 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5037 SyncListener<T> listener) {
5038 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5042 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5043 Listener<T> listener) {
5044 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5048 final public <T> void forPossibleAdapted(final Resource resource,
5049 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5051 assert (resource != null);
5052 assert (clazz != null);
5053 assert (procedure != null);
5055 final AdaptionService service = getSession().peekService(AdaptionService.class);
5056 if (service == null)
5057 procedure.exception(this, new ServiceException("No AdaptionService available"));
5059 service.adapt(this, resource, resource, Resource.class, clazz, true, procedure);
5063 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5064 SyncProcedure<T> procedure) {
5065 forPossibleAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5070 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5071 Procedure<T> procedure) {
5072 forPossibleAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5077 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5078 AsyncListener<T> listener) {
5079 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5083 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5084 SyncListener<T> listener) {
5085 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5089 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5090 Listener<T> listener) {
5091 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5095 final public <T> void forPossibleUniqueAdapted(final Resource resource,
5096 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5098 assert (resource != null);
5099 assert (clazz != null);
5100 assert (procedure != null);
5102 final AdaptionService service = getSession().peekService(AdaptionService.class);
5103 if (service == null)
5104 procedure.exception(this, new ServiceException("No AdaptionService available"));
5106 service.adaptNew(this, resource, clazz, true, procedure);
5111 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5112 SyncProcedure<T> procedure) {
5113 forPossibleUniqueAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5118 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5119 Procedure<T> procedure) {
5120 forPossibleUniqueAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5125 * Implementation of the interface AsyncRequestProcessor
5129 final public Session getSession() {
5130 return processor.getSession();
5134 public <T> void asyncRequest(final Read<T> request) {
5136 asyncRequest(request, new AsyncProcedure<T>() {
5139 public void execute(AsyncReadGraph graph, T result) {
5143 public void exception(AsyncReadGraph graph, Throwable t) {
5144 Logger.defaultLogError(t);
5148 public String toString() {
5149 return "asyncRequest(Read) -> " + request;
5157 public <T> void asyncRequest(Read<T> request, AsyncListener<T> procedure) {
5158 asyncRequest(request, (AsyncProcedure<T>) procedure);
5162 public <T> void asyncRequest(Read<T> request,
5163 final SyncListener<T> procedure) {
5164 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5168 public <T> void asyncRequest(Read<T> request, final Listener<T> procedure) {
5169 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5173 public <T> void asyncRequest(final Read<T> request, final AsyncProcedure<T> procedure) {
5175 assert (request != null);
5176 assert (procedure != null);
5178 AsyncBarrierImpl barrier = asyncBarrier;
5182 processor.scheduleNow(new SessionTask(this) {
5185 public void run0(int thread) {
5187 final ListenerBase listener = getListenerBase(procedure);
5188 QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5189 } catch (DatabaseException e) {
5190 Logger.defaultLogError(e);
5201 public static ReadGraphImpl createAsync(QueryProcessor support) {
5202 return new ReadGraphImpl(null, null, support);
5206 public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
5207 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5211 public <T> void asyncRequest(Read<T> request, final Procedure<T> procedure) {
5212 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5216 final public <T> void asyncRequest(final AsyncRead<T> request) {
5218 assert (request != null);
5220 asyncRequest(request, new AsyncProcedure<T>() {
5223 public void execute(AsyncReadGraph graph, T result) {
5227 public void exception(AsyncReadGraph graph, Throwable t) {
5228 Logger.defaultLogError(t);
5232 public String toString() {
5233 return "asyncRequest(AsyncRead) -> " + request;
5241 public <T> void asyncRequest(AsyncRead<T> request,
5242 AsyncListener<T> procedure) {
5243 asyncRequest(request, (AsyncProcedure<T>) procedure);
5247 final public <T> void asyncRequest(AsyncRead<T> request,
5248 final SyncListener<T> procedure) {
5249 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5253 final public <T> void asyncRequest(AsyncRead<T> request,
5254 final Listener<T> procedure) {
5255 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5259 final public <T> void asyncRequest(final AsyncRead<T> request,
5260 final AsyncProcedure<T> procedure) {
5262 assert (request != null);
5263 assert (procedure != null);
5265 AsyncBarrierImpl barrier = asyncBarrier;
5269 processor.scheduleNow(new SessionTask(this) {
5272 public void run0(int thread) {
5278 final ListenerBase listener = getListenerBase(procedure);
5279 QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, new AsyncProcedure<T>() {
5282 public void execute(AsyncReadGraph graph, T result) {
5283 procedure.execute(graph, result);
5289 public void exception(AsyncReadGraph graph, Throwable throwable) {
5290 procedure.exception(graph, throwable);
5296 } catch (DatabaseException e) {
5297 LOGGER.error("Error while executing async request", e);
5309 public <T> void asyncRequest(AsyncRead<T> request,
5310 SyncProcedure<T> procedure) {
5311 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5315 final public <T> void asyncRequest(final AsyncRead<T> request,
5316 final Procedure<T> procedure) {
5317 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5321 public <T> void asyncRequest(final MultiRead<T> request) {
5323 assert (request != null);
5325 asyncRequest(request, new SyncMultiProcedureAdapter<T>() {
5327 public void exception(ReadGraph graph, Throwable t) {
5328 Logger.defaultLogError(t);
5332 public String toString() {
5333 return "asyncRequest(MultiRead) -> " + request;
5340 public <T> void asyncRequest(MultiRead<T> request,
5341 SyncMultiListener<T> procedure) {
5342 asyncRequest(request, (SyncMultiProcedure<T>)procedure);
5346 public <T> void asyncRequest(MultiRead<T> request,
5347 MultiListener<T> procedure) {
5348 asyncRequest(request, new NoneToSyncMultiListener<T>(procedure));
5353 public <T> void asyncRequest(final MultiRead<T> request,
5354 final SyncMultiProcedure<T> procedure) {
5356 assert (request != null);
5357 assert (procedure != null);
5359 final ListenerBase listener = getListenerBase(procedure);
5361 if (parent != null || listener != null) {
5363 // final ReadGraphImpl newGraph = newSync();
5364 processor.query(this, request, parent, procedure,listener);
5368 // final ReadGraphImpl newGraph = newSync();
5372 request.perform(this, procedure);
5374 } catch (Throwable t) {
5377 procedure.exception(this, t);
5378 } catch (DatabaseException e) {
5379 LOGGER.error("Unexpected exception while handling exception", e);
5389 public <T> void asyncRequest(MultiRead<T> request,
5390 MultiProcedure<T> procedure) {
5391 asyncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
5395 final public <T> void asyncRequest(final AsyncMultiRead<T> request) {
5397 assert (request != null);
5399 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5401 public void exception(AsyncReadGraph graph, Throwable t) {
5402 Logger.defaultLogError(t);
5406 public String toString() {
5407 return "asyncRequest(AsyncMultiRead) -> " + request;
5414 public <T> void asyncRequest(AsyncMultiRead<T> request,
5415 AsyncMultiListener<T> procedure) {
5416 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5420 public <T> void asyncRequest(AsyncMultiRead<T> request,
5421 SyncMultiListener<T> procedure) {
5422 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5426 public <T> void asyncRequest(AsyncMultiRead<T> request,
5427 MultiListener<T> procedure) {
5428 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5432 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5433 final AsyncMultiProcedure<T> procedure) {
5435 assert (request != null);
5436 assert (procedure != null);
5438 ListenerBase listener = getListenerBase(procedure);
5440 if (parent != null || listener != null) {
5442 processor.query(this, request, parent, procedure, listener);
5448 request.perform(this, new AsyncMultiProcedure<T>() {
5451 public void execute(AsyncReadGraph graph, T result) {
5452 procedure.execute(graph, result);
5456 public void finished(AsyncReadGraph graph) {
5457 procedure.finished(graph);
5461 public void exception(AsyncReadGraph graph, Throwable t) {
5462 procedure.exception(graph, t);
5466 public String toString() {
5467 return "asyncRequest(AsyncMultiRead) -> " + procedure;
5472 } catch (Throwable t) {
5474 procedure.exception(this, new DatabaseException(t));
5482 public <T> void asyncRequest(AsyncMultiRead<T> request,
5483 SyncMultiProcedure<T> procedure) {
5484 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5488 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5489 final MultiProcedure<T> procedure) {
5490 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5494 final public <T> void asyncRequest(final ExternalRead<T> request) {
5496 assert (request != null);
5498 asyncRequest(request, new Procedure<T>() {
5501 public void execute(T result) {
5505 public void exception(Throwable t) {
5506 Logger.defaultLogError(t);
5510 public String toString() {
5511 return "asyncRequest(PrimitiveRead) -> " + request;
5519 public <T> void asyncRequest(ExternalRead<T> request,
5520 final Listener<T> procedure) {
5521 asyncRequest(request, (Procedure<T>) procedure);
5525 final public <T> void asyncRequest(final ExternalRead<T> request,
5526 final Procedure<T> procedure) {
5528 assert (request != null);
5529 assert (procedure != null);
5531 final ListenerBase listener = getListenerBase(procedure);
5533 if (parent != null || listener != null) {
5536 QueryCacheBase.resultExternalReadEntry(this, request, parent, listener, procedure);
5537 } catch (DatabaseException e) {
5538 Logger.defaultLogError(e);
5539 // This throwable has already been transferred to procedure at this point - do nothing about it
5544 request.register(this, new Listener<T>() {
5546 public void execute(T result) {
5547 procedure.execute(result);
5551 public void exception(Throwable t) {
5552 procedure.exception(t);
5556 public String toString() {
5557 return "asyncRequest(PrimitiveRead) -> " + request;
5561 public boolean isDisposed() {
5572 public void asyncRequest(final Write request) {
5574 assert (request != null);
5576 getSession().asyncRequest(request);
5578 // processor.asyncWrite(request);
5583 public <T> void asyncRequest(final WriteResult<T> request, Procedure<T> procedure) {
5584 throw new Error("Not implemented.");
5588 public void asyncRequest(Write request, Consumer<DatabaseException> callback) {
5590 assert (request != null);
5592 getSession().asyncRequest(request, callback);
5597 public void asyncRequest(final DelayedWrite request) {
5599 assert (request != null);
5601 getSession().asyncRequest(request);
5606 public <T> void asyncRequest(final DelayedWriteResult<T> request, Procedure<T> procedure) {
5607 throw new Error("Not implemented.");
5611 public void asyncRequest(DelayedWrite r,
5612 Consumer<DatabaseException> callback) {
5613 throw new Error("Not implemented.");
5617 public void asyncRequest(final WriteOnly request) {
5619 assert (request != null);
5621 getSession().asyncRequest(request);
5626 public <T> void asyncRequest(final WriteOnlyResult<T> request, Procedure<T> procedure) {
5627 throw new Error("Not implemented.");
5631 public void asyncRequest(WriteOnly r, Consumer<DatabaseException> callback) {
5632 throw new Error("Not implemented.");
5636 * Implementation of the interface ServiceLocator
5640 public <T> T getService(Class<T> api) {
5641 if(WriteSupport.class == api) {
5642 if(this instanceof WriteGraphImpl) {
5643 WriteGraphImpl impl = (WriteGraphImpl)this;
5644 return (T)impl.writeSupport;
5647 return getSession().getService(api);
5651 public <T> T peekService(Class<T> api) {
5652 return getSession().peekService(api);
5656 public boolean hasService(Class<?> api) {
5657 return getSession().hasService(api);
5661 public <T> void registerService(Class<T> api, T service) {
5662 getSession().registerService(api, service);
5666 public boolean isImmutable(Resource resource) throws DatabaseException {
5667 ResourceImpl impl = (ResourceImpl)resource;
5668 return processor.isImmutable(impl.id);
5675 protected static String INTERNAL_ERROR_STRING = "Transaction aborted due to internal client error.";
5678 * callerThread is the currently running thread state.syncThread is blocking for
5679 * this execution state.syncParent is the blocking request
5682 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support) {
5683 this.parentGraph = parentGraph;
5684 this.parent = parent;
5685 this.processor = support;
5686 this.asyncBarrier = prepareBarrier(parentGraph, parent, null, false);
5689 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support, AsyncBarrierImpl asyncBarrier) {
5690 this.parentGraph = parentGraph;
5691 this.parent = parent;
5692 this.processor = support;
5693 this.asyncBarrier = asyncBarrier;
5696 ReadGraphImpl(ReadGraphImpl graph, CacheEntry parent) {
5697 this(graph, parent, graph.processor);
5700 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, Runnable callback, boolean needsToBlock) {
5701 this(parentGraph, parent, parentGraph.processor, prepareBarrier(parentGraph, parent, callback, needsToBlock));
5704 static AsyncBarrierImpl prepareBarrier(ReadGraphImpl parentGraph, CacheEntry parent, Runnable callback, boolean needsToBlock) {
5705 return new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent, callback, needsToBlock);
5708 ReadGraphImpl(ReadGraphImpl graph) {
5709 this(graph, graph.parent);
5712 public ReadGraphImpl withParent(CacheEntry parent, Runnable callback, boolean needsToBlock) {
5713 return new ReadGraphImpl(this, parent, callback, needsToBlock);
5716 public ReadGraphImpl forRecompute(CacheEntry parent) {
5717 return new ReadGraphImpl(null, parent, processor);
5720 public static ReadGraphImpl create(QueryProcessor support) {
5721 ReadGraphImpl result = new ReadGraphImpl(null, null, support);
5725 public ReadGraphImpl newRestart(ReadGraphImpl impl) {
5727 WriteGraphImpl write = processor.getSession().getService(
5728 WriteGraphImpl.class);
5734 final private ListenerBase getListenerBase(final Object procedure) {
5735 if (procedure instanceof ListenerBase)
5736 return (ListenerBase) procedure;
5741 public <T> void waitAsyncProcedure(AsyncMultiReadProcedure<T> procedure) {
5743 assert(procedure.done());
5747 public <T> void waitAsyncProcedure(AsyncReadProcedure<T> procedure) {
5749 assert(procedure.done());
5753 public boolean resumeTasks() {
5754 return processor.resumeTasks(this);
5757 Class<?> singleClass(Set<Resource> types) {
5758 Class<?> result = null;
5759 for (Resource type : types) {
5760 Class<?> clazz = processor.getBuiltinValue(type);
5761 if (clazz != null) {
5771 private String debugString(Resource r) {
5774 name = getPossibleRelatedValue(r, processor.getL0(this).HasName);
5775 } catch (ManyObjectsForFunctionalRelationException e) {
5776 Logger.defaultLogError(e);
5777 } catch (ServiceException e) {
5778 Logger.defaultLogError(e);
5780 return "[" + name + " - " + r + "]";
5784 public String toString() {
5785 return "ReadGraphImpl[thread=" + Thread.currentThread() + "]";
5789 final public int thread() {
5793 static class MultiTripleIntProcedure implements TripleIntProcedure {
5795 final private AsyncMultiProcedure<Statement> procedure;
5796 final private ReadGraphImpl impl;
5797 final private QuerySupport support;
5799 public MultiTripleIntProcedure(AsyncMultiProcedure<Statement> procedure, ReadGraphImpl impl, QuerySupport support) {
5800 this.procedure = procedure;
5802 this.support = support;
5806 public void execute(ReadGraphImpl graph, int s, int p, int o) {
5808 procedure.execute(graph, support.getStatement(s, p, o));
5809 } catch (Throwable t2) {
5810 Logger.defaultLogError(t2);
5815 public void finished(ReadGraphImpl graph) {
5817 procedure.finished(graph);
5818 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5819 } catch (Throwable t2) {
5820 Logger.defaultLogError(t2);
5825 public void exception(ReadGraphImpl graph, Throwable t) {
5827 procedure.exception(graph, t);
5828 } catch (Throwable t2) {
5829 Logger.defaultLogError(t2);
5831 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5835 public String toString() {
5836 return "forEachObject with " + procedure;
5841 // private AsyncMultiProcedure<Resource> cacheKey = null;
5842 // private MultiIntProcedure cacheResult = null;
5844 // final IntProcedure forMultiProcedure(final AsyncMultiProcedure<Resource> procedure) {
5846 // if(procedure == cacheKey) return cacheResult;
5848 // cacheResult = new MultiIntProcedure(procedure, this, processor.support);
5849 // cacheKey = procedure;
5851 // return cacheResult;
5855 // private AsyncMultiProcedure<Statement> cacheKey2 = null;
5856 // private MultiTripleIntProcedure cacheResult2 = null;
5858 // final synchronized TripleIntProcedure forMultiProcedure(final AsyncMultiProcedure<Statement> procedure) {
5860 // if(procedure == cacheKey2) return cacheResult2;
5862 // cacheResult2 = new MultiTripleIntProcedure(procedure, this, processor.support);
5863 // cacheKey2 = procedure;
5865 // return cacheResult2;
5870 public Datatype getDataType(Resource subject) throws DatabaseException {
5871 for(Resource dataTypeResource : getObjects(subject, processor.getL0(this).HasDataType))
5872 return getValue(dataTypeResource, Bindings.getBindingUnchecked(Datatype.class));
5873 throw new DoesNotContainValueException("The literal has no data type.");
5876 protected <T extends Accessor> T getAccessor4File(Resource subject)
5877 throws DatabaseException {
5880 byte[] bytes = processor.support.getValue(g, subject);
5884 BinaryVariant va = (BinaryVariant) Accessors.getAccessor(bytes, Datatypes.VARIANT);
5885 Accessor ca = va.getContentAccessor();
5887 } catch (AccessorConstructionException e) {
5888 throw new DatabaseException(e);
5894 Binding datatype_binding = Bindings.getBindingUnchecked(DataType.class);
5895 Serializer datatype_serializer = datatype_binding.serializer();
5898 BinaryMemory in = new BinaryMemory(ByteBuffer.wrap(bytes));
5899 datatype = (DataType)datatype_serializer.deserialize(in);
5900 Binding data_binding = Bindings.getBinding(datatype);
5901 Serializer data_serializer = data_binding.serializer(BinarySerializationFormat.INSTANCE);
5902 Object o = data_serializer.deserialize(in);
5904 return (T)Accessors.getAccessor(data_binding, o);
5905 } catch(AccessorConstructionException e) {
5908 } catch (Exception e) {
5909 throw new DatabaseException(e);
5912 @SuppressWarnings("unchecked")
5914 public <T extends Accessor> T getAccessor(Resource subject) throws DatabaseException {
5915 RandomAccessBinary rab = getRandomAccessBinary(subject);
5917 return (T)Accessors.getAccessor(rab, getDataType(subject));
5918 } catch(AccessorConstructionException e) {
5919 throw new DatabaseException(e);
5922 @SuppressWarnings("unchecked")
5923 protected <T extends Accessor> T createAccessor(Resource resource, Datatype datatype, Object intialValue)
5924 throws DatabaseException {
5925 RandomAccessBinary rab = createRandomAccessBinary(resource, datatype, intialValue);
5927 return (T)Accessors.getAccessor(rab, datatype);
5928 } catch(AccessorConstructionException e) {
5929 throw new DatabaseException(e);
5933 public RandomAccessBinary getRandomAccessBinary(Resource subject) throws DatabaseException {
5934 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5935 ResourceData rd = ravs.get(subject);
5939 ExternalValueSupport evs = getService(ExternalValueSupport.class);
5940 long size = evs.getValueSize(this, subject); // Throws DatabaseException if no old external value.
5942 File platform = Platform.getLocation().toFile();
5943 File tempFiles = new File(platform, "tempFiles");
5944 File dbDir = new File(tempFiles, "db");
5946 File file = new File(dbDir, "ResourceFile" + subject.getResourceId());
5947 rd = new ResourceData(new BinaryFile(file), true); // Old external value.
5948 final int N = 1<<20;
5952 int length = N < left ? N : (int)left;
5953 byte[] bytes = evs.readValue(this, subject, offset, length);
5954 offset += bytes.length;
5955 left -= bytes.length;
5956 rd.binaryFile.write(bytes);
5958 ravs.put(subject, rd);
5960 } catch (Exception e) {
5961 throw new DatabaseException("Resource " + subject + " have value but there was problem with accessing value data.", e);
5963 } catch (Exception e) {
5964 if(Development.DEVELOPMENT) {
5965 if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
5966 e.printStackTrace();
5970 Datatype datatype = getDataType(subject);
5971 Object value = getPossibleValue(subject, Bindings.getBinding(datatype));
5972 return createRandomAccessBinary(subject, datatype, value);
5974 public RandomAccessBinary createRandomAccessBinary(Resource resource, Datatype datatype, Object initialValue)
5975 throws DatabaseException {
5976 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5978 File platform = Platform.getLocation().toFile();
5979 File tempFiles = new File(platform, "tempFiles");
5980 File dbDir = new File(tempFiles, "db");
5982 File file = new File(dbDir, "ResourceFile" + resource.getResourceId());
5983 ResourceData rd = new ResourceData(new BinaryFile(file), false);
5984 Binding binding = Bindings.getBinding(datatype);
5985 if (null == initialValue) {
5986 initialValue = binding.createDefault();
5988 Serializer serializer = binding.serializer();
5989 byte[] bytes = serializer.serialize(initialValue);
5990 // In case the file has been previously accessed and was larger we set the correct size now
5991 rd.binaryFile.setLength(bytes.length);
5992 rd.binaryFile.write(bytes);
5993 ravs.put(resource, rd);
5995 } catch (Exception e) {
5996 if (e instanceof DatabaseException)
5997 throw (DatabaseException)e;
5999 throw new DatabaseException(e);
6003 // static class ExternalValueRequest<T> extends ResourceRead<T> {
6005 // public ExternalValueRequest(Resource resource) {
6009 // @SuppressWarnings("unchecked")
6011 // public T perform(ReadGraph graph) throws DatabaseException {
6014 // String uri = graph.getURI(resource);
6015 // if(Layer0.URIs.Functions_functionApplication.equals(uri)) return (T)functionApplication;
6017 // return (T)ReflectionUtils.getValue(uri).getValue();
6019 // } catch(ValueNotFoundException e) {
6020 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
6021 // } catch(ClassCastException e) {
6022 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
6028 @SuppressWarnings("unchecked")
6030 public <T> T getValue2(Resource r, Object context) throws DatabaseException {
6031 Layer0 L0 = processor.getL0(this);
6032 Set<Resource> types = getTypes(r);
6034 if(types.contains(L0.Literal)) {
6035 if(isImmutable(r)) {
6036 return syncRequest(new ValueImplied<T>(r));
6041 else if(types.contains(L0.ExternalValue)) {
6042 return (T)syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6046 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6047 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6049 return function.apply(this, r, context);
6050 } catch(RuntimeException e) {
6051 DatabaseException dte = findPossibleRootException(e);
6052 if(dte != null) throw dte;
6053 else throw new DatabaseException(e);
6060 public Variant getVariantValue2(Resource r, Object context) throws DatabaseException {
6061 Layer0 L0 = processor.getL0(this);
6062 Set<Resource> types = getTypes(r);
6064 if(types.contains(L0.Literal)) {
6065 if(isImmutable(r)) {
6066 return syncRequest(new VariantValueImplied(r));
6068 return getVariantValue(r);
6071 else if(types.contains(L0.ExternalValue)) {
6072 Object value = syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6074 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6075 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6076 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6081 Function3<ReadGraph,Resource,Object,Object> function = requestValueFunction(r);
6082 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6084 Object value = function.apply(this, r, context);
6086 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6087 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6088 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6090 } catch(RuntimeException e) {
6091 DatabaseException dte = findPossibleRootException(e);
6092 if(dte != null) throw dte;
6093 else throw new DatabaseException(e);
6099 public <T> T getPossibleValue2(Resource subject, Object context) throws DatabaseException {
6101 return getValue2(subject, context);
6102 } catch (DatabaseException e) {
6107 static class PossibleConverterFunction<T> extends ResourceRead<Function3<ReadGraph,Resource,Object,T>> {
6109 public PossibleConverterFunction(Resource resource) {
6114 public Function3<ReadGraph,Resource,Object,T> perform(ReadGraph graph) throws DatabaseException {
6115 return compute(graph, resource);
6118 @SuppressWarnings("unchecked")
6119 public static <T> Function3<ReadGraph,Resource,Object,T> compute(ReadGraph graph, Resource resource) throws DatabaseException {
6120 Layer0 L0 = Layer0.getInstance(graph);
6121 for(Resource converter : graph.getObjects(resource, L0.ConvertsToValueWith)) {
6123 if(L0.Functions_functionApplication.equals(converter)) {
6124 return (Function3<ReadGraph,Resource,Object,T>)graph.syncRequest(new AdaptValue(resource));
6126 return graph.getValue2(converter, resource);
6128 } catch(RuntimeException e) {
6129 DatabaseException dte = findPossibleRootException(e);
6130 if(dte != null) throw dte;
6131 else throw new DatabaseException(e);
6139 <T> Function3<ReadGraph,Resource,Object,T> requestValueFunction(Resource r) throws DatabaseException {
6141 return syncRequest(new PossibleConverterFunction<T>(r), TransientCacheAsyncListener.<Function3<ReadGraph,Resource,Object,T>>instance());
6143 return syncRequest(new PossibleConverterFunction<T>(r));
6147 * Get a value associated with a graph {@link Resource}, using a possible context object and
6148 * a desired value binding. The following methods are tried in order to retreive the value:
6150 * <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>
6151 * <li>If the resource is a {@code L0.ExternalValue}, the value is acquired using
6152 * {@link ReflectionUtils#getValue(String)}.</li>
6153 * <li>If the resource is associated with a suitable value converter with relation {@code L0.ConvertsToValueWith}
6154 * (see {@link #requestValueFunction(Resource)}), the value function is called with the graph, the resource
6155 * and the context object.</li>
6158 * @param r A graph resource with which the value is associated
6159 * @param context A context object that is used for acquiring the value (only applied in case 3)
6160 * @param binding A binding for the value type (only applied in case 1)
6161 * @return The value of the graph node.
6162 * @throws DoesNotContainValueException No value is associated with the graph node.
6163 * @throws DatabaseException Other errors, such as an error in casting the value to the return type or
6164 * a runtime error in the value function.
6166 @SuppressWarnings("unchecked")
6168 public <T> T getValue2(Resource r, Object context, Binding binding) throws DatabaseException {
6169 if (binding instanceof ObjectVariantBinding)
6170 return getValue2(r, context);
6172 Layer0 L0 = processor.getL0(this);
6173 Set<Resource> types = getTypes(r);
6174 if(types.contains(L0.Literal)) {
6175 if(isImmutable(r)) {
6176 return syncRequest(new Value<T>(r, binding));
6178 return getValue(r, binding);
6180 } else if(types.contains(L0.ExternalValue)) {
6181 ComputationalValue cv = syncRequest(new PossibleAdapter<ComputationalValue>(r, ComputationalValue.class), TransientCacheAsyncListener.instance());
6183 return cv.getValue(this, r);
6185 // This should not even be possible since L0 defines an adapter for all values
6187 return (T)ReflectionUtils.getValue(getURI(r)).getValue();
6188 } catch(ValueNotFoundException e) {
6189 throw new DatabaseException(e);
6190 } catch(ClassCastException e) {
6191 throw new DatabaseException(e);
6196 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6197 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function.");
6199 Object value = function.apply(this, r, context);
6200 if(binding.isInstance(value)) return (T)value;
6201 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
6202 return (T)Bindings.adapt(value, srcBinding, binding);
6203 } catch(RuntimeException e) {
6204 DatabaseException dte = findPossibleRootException(e);
6205 if(dte != null) throw dte;
6206 else throw new DatabaseException(e);
6207 } catch (AdaptException e) {
6208 throw new DatabaseException(e);
6209 } catch (org.simantics.databoard.binding.error.BindingException e) {
6210 throw new DatabaseException(e);
6216 public <T> T getPossibleValue2(Resource subject, Object context, Binding binding) throws DatabaseException {
6218 return getValue2(subject, context, binding);
6219 } catch (DatabaseException e) {
6224 private static DatabaseException findPossibleRootException(Throwable t) {
6225 if(t == null) return null;
6226 if(t instanceof DatabaseException) return (DatabaseException)t;
6227 if(t instanceof RuntimeException || t instanceof InvocationTargetException) {
6228 return findPossibleRootException(t.getCause());
6234 public <T> T getRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6235 return getRelatedValue2(subject, relation, subject);
6239 public Variant getRelatedVariantValue2(Resource subject, Resource relation) throws DatabaseException {
6240 return getRelatedVariantValue2(subject, relation, subject);
6244 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6246 Resource object = getPossibleObject(subject, relation);
6247 if(object == null) return null;
6248 else return getValue2(object, subject);
6249 } catch (DatabaseException e) {
6255 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6256 if(Development.DEVELOPMENT) {
6257 if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
6258 String error = L0Validations.checkValueType(this, subject, relation);
6260 Logger.defaultLogError(new ValidationException(error));
6261 throw new ValidationException(error);
6265 return getValue2(getSingleObject(subject, relation), context);
6269 public Variant getRelatedVariantValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6270 if(Development.DEVELOPMENT) {
6271 if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
6272 String error = L0Validations.checkValueType(this, subject, relation);
6274 Logger.defaultLogError(new ValidationException(error));
6275 throw new ValidationException(error);
6279 return getVariantValue2(getSingleObject(subject, relation), context);
6283 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6285 Resource object = getPossibleObject(subject, relation);
6286 if(object == null) return null;
6287 else return getValue2(object, context);
6288 } catch (DatabaseException e) {
6294 public <T> T getRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6295 return getRelatedValue2(subject, relation, subject, binding);
6299 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6301 Resource object = getPossibleObject(subject, relation);
6302 if(object == null) return null;
6303 return getValue2(object, subject, binding);
6304 } catch (DatabaseException e) {
6310 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6311 return getValue2(getSingleObject(subject, relation), context, binding);
6315 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6317 Resource object = getPossibleObject(subject, relation);
6318 if(object == null) return null;
6319 else return getValue2(object, context, binding);
6320 } catch (DatabaseException e) {
6326 public Type getRelatedValueType(Resource subject, Resource relation) throws DatabaseException {
6327 Layer0 L0 = processor.getL0(this);
6328 Resource property = getSingleObject(subject, relation);
6329 String typeText = (String)getRelatedValue(property, L0.HasValueType, Bindings.STRING);
6331 return org.simantics.scl.compiler.types.Types.parseType(typeText);
6332 } catch (SCLTypeParseException e) {
6333 throw new DatabaseException(e);
6337 private static ThreadLocal<Boolean> syncGraph = new ThreadLocal<Boolean>() {
6338 protected Boolean initialValue() {
6344 public boolean setSynchronous(boolean value) {
6345 boolean old = getSynchronous();
6346 syncGraph.set(value);
6351 public boolean getSynchronous() {
6352 return syncGraph.get();
6355 public void ensureLoaded(int resource) {
6356 processor.querySupport.ensureLoaded(this, resource);
6359 public void ensureLoaded(int resource, int predicate) {
6360 processor.querySupport.ensureLoaded(this, resource, predicate);
6363 public byte[] getValue(int resource) {
6364 return processor.querySupport.getValue(this, resource);
6367 public int thread(int resource) {
6368 return (resource >>> 16) & processor.THREAD_MASK;
6371 public int thread(Resource resource) {
6372 return (((ResourceImpl)resource).id >>> 16) & processor.THREAD_MASK;
6375 public ResourceSupport getResourceSupport() {
6376 return processor.getResourceSupport();
6380 public Object getModificationCounter() {
6381 return processor.getSession().getModificationCounter();
6385 public boolean performPending() {
6386 return processor.performPending(this);
6389 public Set<ReadGraphImpl> ancestorSet() {
6390 HashSet<ReadGraphImpl> result = new HashSet<>();
6391 ReadGraphImpl g = this;
6399 public int getLevel() {
6400 return getLevelStatic(this);
6403 private static int getLevelStatic(ReadGraphImpl impl) {
6404 if(impl == null) return 0;
6405 else return 1 + getLevelStatic(impl.parentGraph);
6408 public boolean isParent(ReadGraphImpl impl) {
6409 if(impl == null) return false;
6410 if(this == impl) return true;
6411 return isParent(impl.parentGraph);
6414 public ReadGraphImpl getTopLevelGraph() {
6415 return getTopLevelGraphStatic(this);
6418 private static ReadGraphImpl getTopLevelGraphStatic(ReadGraphImpl impl) {
6419 if(impl.parentGraph == null) return impl;
6420 else return getTopLevelGraphStatic(impl.parentGraph);
6423 @SuppressWarnings("unchecked")
6426 return (T) processor.getL0();