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 SyncReadProcedure<RelationInfo> procedure = new SyncReadProcedure<RelationInfo>();
1854 processor.forRelationInfo(this, relation, procedure);
1855 Collection<Resource> predicates = getPredicates(subject);
1857 if(procedure.result.isFinal) {
1859 return predicates.contains(relation);
1861 } else if (procedure.result.isFunctional) {
1864 int result = processor.getSingleObject(this, subject, relation);
1866 } catch (ManyObjectsForFunctionalRelationException e) {
1868 } catch (DatabaseException e) {
1869 throw new ServiceException(e);
1874 for(Resource predicate : getPredicates(subject)) {
1875 if(isSubrelationOf(predicate, relation))
1883 } catch (ServiceException e) {
1885 throw new ServiceException(e);
1892 final public boolean hasStatement(final Resource subject, final Resource relation, final Resource object) throws ServiceException {
1894 assert (subject != null);
1895 assert (relation != null);
1896 assert (object != null);
1900 for(Resource o : getObjects(subject, relation)) {
1901 if(object.equals(o)) return true;
1906 } catch (ServiceException e) {
1908 throw new ServiceException(e);
1915 final public boolean hasValue(final Resource subject) throws ServiceException {
1917 assert (subject != null);
1921 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1922 processor.forHasValue(this, subject, procedure);
1923 procedure.checkAndThrow();
1924 return procedure.result;
1926 } catch (ServiceException e) {
1928 throw new ServiceException(e);
1930 } catch (DatabaseException e) {
1932 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1938 final AsyncProcedure<?> NONE = new AsyncProcedure<Object>() {
1941 public void execute(AsyncReadGraph graph, Object result) {
1945 public void exception(AsyncReadGraph graph, Throwable throwable) {
1951 * Implementation of the interface RequestProcessor
1955 public <T> T syncRequest(final Read<T> request) throws DatabaseException {
1956 assert (request != null);
1957 return (T)QueryCache.runnerReadEntry(this, request, parent, null, null, true);
1961 public <T> T syncRequest(Read<T> request, SyncListener<T> procedure)
1962 throws DatabaseException {
1963 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
1967 public <T> T syncRequest(Read<T> request, final Listener<T> procedure)
1968 throws DatabaseException {
1969 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
1973 public <T> T syncRequest(final Read<T> request, final AsyncProcedure<T> procedure) throws DatabaseException {
1974 assert (request != null);
1975 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
1976 return (T)QueryCache.runnerReadEntry(this, request, parent, listener, procedure, true);
1980 public <T> T syncRequest(final Read<T> request,
1981 final SyncProcedure<T> procedure) throws DatabaseException {
1982 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
1986 public <T> T syncRequest(Read<T> request, Procedure<T> procedure)
1987 throws DatabaseException {
1988 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
1991 static class AsyncReadProcedure<T> implements AsyncProcedure<T> {
1993 private static Throwable DONE = new Throwable();
1996 Throwable exception = null;
1999 public void execute(AsyncReadGraph graph, T t) {
2005 public void exception(AsyncReadGraph graph, Throwable t) {
2009 public void checkAndThrow() throws DatabaseException {
2010 if(exception != DONE) {
2011 if (exception instanceof DatabaseException)
2012 throw (DatabaseException) exception;
2014 throw new DatabaseException(
2015 "Unexpected exception in ReadGraph.syncRequest(AsyncRead)",
2020 public boolean done() {
2021 return exception != null;
2027 public <T> T syncRequest(final AsyncRead<T> request)
2028 throws DatabaseException {
2030 assert (request != null);
2031 return syncRequest(request, new AsyncProcedureAdapter<>() );
2036 public <T> T syncRequest(AsyncRead<T> request, AsyncListener<T> procedure)
2037 throws DatabaseException {
2038 return syncRequest(request, (AsyncProcedure<T>) procedure);
2042 public <T> T syncRequest(AsyncRead<T> request, SyncListener<T> procedure)
2043 throws DatabaseException {
2044 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
2048 public <T> T syncRequest(AsyncRead<T> request, Listener<T> procedure)
2049 throws DatabaseException {
2050 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
2054 final public <T> T syncRequest(final AsyncRead<T> request,
2055 final AsyncProcedure<T> procedure) throws DatabaseException {
2056 assert (request != null);
2057 ListenerBase listener = getListenerBase(procedure);
2058 return (T)QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure, true);
2062 public <T> T syncRequest(AsyncRead<T> request,
2063 final SyncProcedure<T> procedure) throws DatabaseException {
2064 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2068 final public <T> T syncRequest(final AsyncRead<T> request,
2069 final Procedure<T> procedure) throws DatabaseException {
2070 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2074 public <T> Collection<T> syncRequest(final MultiRead<T> request)
2075 throws DatabaseException {
2077 assert (request != null);
2079 final ArrayList<T> result = new ArrayList<T>();
2080 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2082 syncRequest(request, new SyncMultiProcedure<T>() {
2085 public void execute(ReadGraph graph, T t) {
2086 synchronized (result) {
2092 public void finished(ReadGraph graph) {
2096 public void exception(ReadGraph graph, Throwable t) {
2101 public String toString() {
2102 return "syncRequest(MultiRead) -> " + request;
2107 Throwable t = exception.get();
2109 if (t instanceof DatabaseException)
2110 throw (DatabaseException) t;
2112 throw new DatabaseException(
2113 "Unexpected exception in ReadGraph.syncRequest(Read)",
2122 public <T> Collection<T> syncRequest(MultiRead<T> request,
2123 SyncMultiListener<T> procedure) {
2124 return syncRequest(request, (SyncMultiProcedure<T>)procedure);
2128 public <T> Collection<T> syncRequest(MultiRead<T> request,
2129 MultiListener<T> procedure) {
2130 return syncRequest(request, new NoneToSyncMultiListener<T>(procedure));
2134 public <T> Collection<T> syncRequest(MultiRead<T> request,
2135 SyncMultiProcedure<T> procedure) {
2137 assert (request != null);
2139 ListenerBase listener = getListenerBase(procedure);
2141 final ResultCallWrappedSyncQueryProcedure<T> wrapper = new ResultCallWrappedSyncQueryProcedure<T>(procedure);
2143 if (parent != null || listener != null) {
2145 // Object syncParent = request;
2147 // final ReadGraphImpl newGraph = newSync();
2149 processor.query(this, request, parent, wrapper, listener);
2151 // newGraph.waitAsync(syncParent);
2155 // Object syncParent = request;
2157 // final ReadGraphImpl newGraph = newSync();
2160 request.perform(this, wrapper);
2161 } catch (Throwable t) {
2162 wrapper.exception(this, t);
2167 return wrapper.get();
2172 public <T> Collection<T> syncRequest(MultiRead<T> request,
2173 MultiProcedure<T> procedure) {
2174 return syncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
2177 static class AsyncMultiReadProcedure<T> extends ArrayList<T> implements AsyncMultiProcedure<T> {
2179 private static Throwable DONE = new Throwable();
2181 private static final long serialVersionUID = -6494230465108115812L;
2183 Throwable exception = null;
2186 public synchronized void execute(AsyncReadGraph graph, T t) {
2191 public void finished(AsyncReadGraph graph) {
2196 public void exception(AsyncReadGraph graph, Throwable t) {
2200 public void checkAndThrow() throws DatabaseException {
2201 if(exception != DONE) {
2202 if (exception instanceof DatabaseException)
2203 throw (DatabaseException) exception;
2205 throw new DatabaseException(
2206 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
2211 public boolean done() {
2212 return exception != null;
2218 final public <T> Collection<T> syncRequest(AsyncMultiRead<T> request)
2219 throws DatabaseException {
2221 assert (request != null);
2223 final AsyncMultiReadProcedure<T> procedure = new AsyncMultiReadProcedure<T>();
2225 syncRequest(request, procedure);
2227 procedure.checkAndThrow();
2233 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2234 AsyncMultiListener<T> procedure) {
2235 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2239 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2240 SyncMultiListener<T> procedure) {
2241 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2245 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2246 MultiListener<T> procedure) {
2247 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2250 final private <T> void syncRequest(final AsyncMultiRead<T> request,
2251 final AsyncMultiReadProcedure<T> procedure) {
2253 assert (request != null);
2254 assert (procedure != null);
2256 ListenerBase listener = getListenerBase(procedure);
2258 if (parent != null || listener != null) {
2260 // Object syncParent = request;
2262 // final ReadGraphImpl newGraph = newSync();
2264 processor.query(this, request, parent, procedure, listener);
2266 // newGraph.waitAsync(syncParent);
2267 waitAsyncProcedure(procedure);
2271 // Object syncParent = callerThread == Integer.MIN_VALUE ? null
2274 // final ReadGraphImpl newGraph = newSyncAsync(syncParent);
2279 // ReadGraphImpl sync = newSync();
2280 request.perform(this, procedure);
2281 // sync.waitAsync(null);
2282 waitAsyncProcedure(procedure);
2285 } catch (Throwable t) {
2287 waitAsyncProcedure(procedure);
2298 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2299 final AsyncMultiProcedure<T> procedure) {
2301 assert (request != null);
2302 assert (procedure != null);
2304 ListenerBase listener = getListenerBase(procedure);
2306 if (parent != null || listener != null) {
2308 // Object syncParent = request;
2310 // final ReadGraphImpl newGraph = newSync();
2312 processor.query(this, request, parent, procedure, listener);
2314 // newGraph.waitAsync(syncParent);
2318 // Object syncParent = request;
2320 // final ReadGraphImpl newGraph = newSync();
2324 request.perform(this, new AsyncMultiProcedure<T>() {
2327 public void execute(AsyncReadGraph graph, T result) {
2328 procedure.execute(graph, result);
2332 public void finished(AsyncReadGraph graph) {
2333 procedure.finished(graph);
2337 public void exception(AsyncReadGraph graph, Throwable t) {
2338 procedure.exception(graph, t);
2342 public String toString() {
2343 return "syncRequest(AsyncMultiRead) -> " + procedure;
2348 } catch (Throwable t) {
2360 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2361 final SyncMultiProcedure<T> procedure) {
2362 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2366 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2367 final MultiProcedure<T> procedure) {
2368 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2372 public <T> T syncRequest(final ExternalRead<T> request)
2373 throws DatabaseException {
2375 assert (request != null);
2377 return syncRequest(request, new Procedure<T>() {
2380 public void execute(T t) {
2384 public void exception(Throwable t) {
2388 public String toString() {
2389 return "syncRequest(AsyncRead) -> " + request;
2397 public <T> T syncRequest(ExternalRead<T> request, Listener<T> procedure) throws DatabaseException {
2398 return syncRequest(request, (Procedure<T>) procedure);
2402 final public <T> T syncRequest(final ExternalRead<T> request,
2403 final Procedure<T> procedure) throws DatabaseException {
2405 assert (request != null);
2407 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
2408 return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
2413 public void syncRequest(final Write request) throws DatabaseException {
2415 assert (request != null);
2417 throw new DatabaseException(
2418 "Write operations are not supported during read transactions!");
2423 public <T> T syncRequest(final WriteResult<T> request) throws DatabaseException {
2425 assert (request != null);
2427 throw new DatabaseException(
2428 "Write operations are not supported during read transactions!");
2433 public void syncRequest(final DelayedWrite request)
2434 throws DatabaseException {
2436 assert (request != null);
2438 throw new DatabaseException(
2439 "Write operations are not supported during read transactions!");
2444 public <T> T syncRequest(final DelayedWriteResult<T> request) throws DatabaseException {
2446 assert (request != null);
2448 throw new DatabaseException(
2449 "Write operations are not supported during read transactions!");
2454 public void syncRequest(final WriteOnly request) throws DatabaseException {
2456 assert (request != null);
2458 throw new DatabaseException(
2459 "Write operations are not supported during read transactions!");
2464 public <T> T syncRequest(final WriteOnlyResult<T> request) throws DatabaseException {
2466 assert (request != null);
2468 throw new DatabaseException(
2469 "Write operations are not supported during read transactions!");
2474 public <T> void async(ReadInterface<T> r, AsyncProcedure<T> procedure) {
2475 r.request(this, procedure);
2479 public <T> void async(ReadInterface<T> r, Procedure<T> procedure) {
2480 r.request(this, procedure);
2484 public <T> void async(ReadInterface<T> r, SyncProcedure<T> procedure) {
2485 r.request(this, procedure);
2489 public <T> void async(ReadInterface<T> r, AsyncListener<T> procedure) {
2490 r.request(this, procedure);
2494 public <T> void async(ReadInterface<T> r, Listener<T> procedure) {
2495 r.request(this, procedure);
2499 public <T> void async(ReadInterface<T> r, SyncListener<T> procedure) {
2500 r.request(this, procedure);
2504 public <T> T sync(ReadInterface<T> r) throws DatabaseException {
2505 return r.request(this);
2509 public <T> T sync(WriteInterface<T> r) throws DatabaseException {
2510 return r.request(this);
2514 public <T> void async(WriteInterface<T> r, Procedure<T> procedure) {
2515 r.request(this, procedure);
2519 public <T> void async(WriteInterface<T> r) {
2520 r.request(this, new ProcedureAdapter<T>());
2524 * Implementation of the interface AsyncReadGraph
2528 public void forURI(Resource resource, AsyncListener<String> listener) {
2529 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2534 public void forURI(Resource resource, SyncListener<String> listener) {
2535 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2540 public void forURI(Resource resource, Listener<String> listener) {
2541 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2546 final public void forURI(final Resource resource,
2547 final AsyncProcedure<String> procedure) {
2549 assert (resource != null);
2550 assert (procedure != null);
2552 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2558 public void forURI(Resource resource, SyncProcedure<String> procedure) {
2559 forURI(resource, new SyncToAsyncProcedure<String>(procedure));
2563 public void forURI(Resource resource, Procedure<String> procedure) {
2564 forURI(resource, new NoneToAsyncProcedure<String>(procedure));
2568 public void forResource(String id, AsyncListener<Resource> listener) {
2569 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2574 public void forResource(String id, SyncListener<Resource> listener) {
2575 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2580 public void forResource(String id, Listener<Resource> listener) {
2581 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2586 final public void forResource(final String id,
2587 final AsyncProcedure<Resource> procedure) {
2589 assert (id != null);
2590 assert (procedure != null);
2592 processor.forResource(this, id, procedure);
2597 public void forResource(String id, SyncProcedure<Resource> procedure) {
2598 forResource(id, new SyncToAsyncProcedure<Resource>(procedure));
2602 public void forResource(String id, Procedure<Resource> procedure) {
2603 forResource(id, new NoneToAsyncProcedure<Resource>(procedure));
2607 public void forBuiltin(String id, AsyncListener<Resource> listener) {
2608 asyncRequest(new Builtin(id), listener);
2612 public void forBuiltin(String id, SyncListener<Resource> listener) {
2613 asyncRequest(new Builtin(id), listener);
2617 public void forBuiltin(String id, Listener<Resource> listener) {
2618 asyncRequest(new Builtin(id), listener);
2622 final public void forBuiltin(final String id,
2623 final AsyncProcedure<Resource> procedure) {
2625 assert (id != null);
2626 assert (procedure != null);
2628 processor.forBuiltin(this, id, procedure);
2633 public void forBuiltin(String id, SyncProcedure<Resource> procedure) {
2634 forBuiltin(id, new SyncToAsyncProcedure<Resource>(procedure));
2638 public void forBuiltin(String id, Procedure<Resource> procedure) {
2639 forBuiltin(id, new NoneToAsyncProcedure<Resource>(procedure));
2643 final public void forEachStatement(Resource subject, Resource relation,
2644 AsyncMultiProcedure<Statement> procedure) {
2646 assert (subject != null);
2647 assert (relation != null);
2648 assert (procedure != null);
2650 processor.forEachStatement(this, subject, relation, procedure);
2655 public void forEachStatement(Resource subject, Resource relation,
2656 SyncMultiProcedure<Statement> procedure) {
2657 forEachStatement(subject, relation,
2658 new SyncToAsyncMultiProcedure<Statement>(procedure));
2662 final public void forEachStatement(Resource subject, Resource relation,
2663 MultiProcedure<Statement> procedure) {
2665 assert (subject != null);
2666 assert (relation != null);
2667 assert (procedure != null);
2669 processor.forEachStatement(this, subject, relation, procedure);
2674 final public void forStatementSet(Resource subject, Resource relation,
2675 AsyncSetListener<Statement> procedure) {
2677 assert (subject != null);
2678 assert (relation != null);
2679 assert (procedure != null);
2681 processor.forStatementSet(this, subject, relation, procedure);
2686 final public void forStatementSet(Resource subject, Resource relation,
2687 SyncSetListener<Statement> procedure) {
2688 forStatementSet(subject, relation,
2689 new SyncToAsyncSetProcedure<Statement>(procedure));
2693 public void forStatementSet(Resource subject, Resource relation,
2694 SetListener<Statement> listener) {
2695 forStatementSet(subject, relation,
2696 new NoneToAsyncSetProcedure<Statement>(listener));
2700 final public void forEachAssertedStatement(final Resource subject,
2701 final Resource relation,
2702 final AsyncMultiProcedure<Statement> procedure) {
2704 assert (subject != null);
2705 assert (relation != null);
2706 assert (procedure != null);
2708 processor.forEachAssertedStatement(this, subject, relation, procedure);
2713 public void forEachAssertedStatement(Resource subject, Resource relation,
2714 SyncMultiProcedure<Statement> procedure) {
2715 forEachAssertedStatement(subject, relation,
2716 new SyncToAsyncMultiProcedure<Statement>(procedure));
2720 public void forEachAssertedStatement(Resource subject, Resource relation,
2721 MultiProcedure<Statement> procedure) {
2722 forEachAssertedStatement(subject, relation,
2723 new NoneToAsyncMultiProcedure<Statement>(procedure));
2727 public void forAssertedStatementSet(Resource subject, Resource relation,
2728 AsyncSetListener<Statement> procedure) {
2730 assert (subject != null);
2731 assert (relation != null);
2732 assert (procedure != null);
2734 processor.forAssertedStatementSet(this, subject, relation, procedure);
2739 public void forAssertedStatementSet(Resource subject, Resource relation,
2740 SyncSetListener<Statement> procedure) {
2742 assert (subject != null);
2743 assert (relation != null);
2744 assert (procedure != null);
2746 forAssertedStatementSet(subject, relation,
2747 new SyncToAsyncSetProcedure<Statement>(procedure));
2752 public void forAssertedStatementSet(Resource subject, Resource relation,
2753 SetListener<Statement> procedure) {
2755 assert (subject != null);
2756 assert (relation != null);
2757 assert (procedure != null);
2759 forAssertedStatementSet(subject, relation,
2760 new NoneToAsyncSetProcedure<Statement>(procedure));
2765 final public void forEachPredicate(final Resource subject,
2766 final AsyncMultiProcedure<Resource> procedure) {
2768 assert (subject != null);
2769 assert (procedure != null);
2771 processor.forEachPredicate(this, subject, procedure);
2776 public void forEachPredicate(Resource subject,
2777 SyncMultiProcedure<Resource> procedure) {
2778 forEachPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(
2783 final public void forEachPredicate(final Resource subject,
2784 final MultiProcedure<Resource> procedure) {
2786 assert (subject != null);
2787 assert (procedure != null);
2789 processor.forEachPredicate(this, subject, procedure);
2794 final public void forPredicateSet(final Resource subject,
2795 final AsyncSetListener<Resource> procedure) {
2797 assert (subject != null);
2798 assert (procedure != null);
2800 processor.forPredicateSet(this, subject, procedure);
2805 final public void forPredicateSet(final Resource subject,
2806 final SyncSetListener<Resource> procedure) {
2808 assert (subject != null);
2809 assert (procedure != null);
2811 forPredicateSet(subject, new SyncToAsyncSetProcedure<Resource>(
2817 final public void forPredicateSet(final Resource subject,
2818 final SetListener<Resource> procedure) {
2820 assert (subject != null);
2821 assert (procedure != null);
2823 forPredicateSet(subject, new NoneToAsyncSetProcedure<Resource>(
2829 final public void forEachPrincipalType(final Resource subject,
2830 final AsyncMultiProcedure<Resource> procedure) {
2832 assert (subject != null);
2833 assert (procedure != null);
2835 processor.forEachPrincipalType(this, subject, procedure);
2840 public void forEachPrincipalType(Resource subject,
2841 SyncMultiProcedure<Resource> procedure) {
2842 forEachPrincipalType(subject, new SyncToAsyncMultiProcedure<Resource>(
2847 final public void forEachPrincipalType(final Resource subject,
2848 final MultiProcedure<Resource> procedure) {
2850 assert (subject != null);
2851 assert (procedure != null);
2853 processor.forEachPrincipalType(this, subject, procedure);
2858 final public void forPrincipalTypeSet(final Resource subject,
2859 final AsyncSetListener<Resource> procedure) {
2861 assert (subject != null);
2862 assert (procedure != null);
2864 processor.forPrincipalTypeSet(this, subject, procedure);
2869 final public void forPrincipalTypeSet(final Resource subject,
2870 final SyncSetListener<Resource> procedure) {
2872 assert (subject != null);
2873 assert (procedure != null);
2875 forPrincipalTypeSet(subject, new SyncToAsyncSetProcedure<Resource>(
2881 final public void forPrincipalTypeSet(final Resource subject,
2882 final SetListener<Resource> procedure) {
2884 assert (subject != null);
2885 assert (procedure != null);
2887 forPrincipalTypeSet(subject, new NoneToAsyncSetProcedure<Resource>(
2893 public void forTypes(Resource subject, AsyncListener<Set<Resource>> listener) {
2894 asyncRequest(new Types(subject), listener);
2898 public void forTypes(Resource subject, SyncListener<Set<Resource>> listener) {
2899 asyncRequest(new Types(subject), listener);
2903 public void forTypes(Resource subject, Listener<Set<Resource>> listener) {
2904 asyncRequest(new Types(subject), listener);
2908 final public void forTypes(final Resource subject,
2909 final AsyncProcedure<Set<Resource>> procedure) {
2911 assert (subject != null);
2912 assert (procedure != null);
2914 processor.forTypes(this, subject, procedure);
2919 public void forTypes(Resource subject,
2920 SyncProcedure<Set<Resource>> procedure) {
2921 forTypes(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
2925 public void forTypes(Resource subject, Procedure<Set<Resource>> procedure) {
2926 forTypes(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
2930 public void forSupertypes(Resource subject,
2931 AsyncListener<Set<Resource>> listener) {
2932 asyncRequest(new Types(subject), listener);
2936 public void forSupertypes(Resource subject,
2937 SyncListener<Set<Resource>> listener) {
2938 asyncRequest(new Types(subject), listener);
2942 public void forSupertypes(Resource subject, Listener<Set<Resource>> listener) {
2943 asyncRequest(new Types(subject), listener);
2947 final public void forSupertypes(final Resource subject,
2948 final AsyncProcedure<Set<Resource>> procedure) {
2950 assert (subject != null);
2951 assert (procedure != null);
2953 processor.forSupertypes(this, subject, procedure);
2958 public void forSupertypes(Resource subject,
2959 SyncProcedure<Set<Resource>> procedure) {
2960 forSupertypes(subject, new SyncToAsyncProcedure<Set<Resource>>(
2965 public void forSupertypes(Resource subject,
2966 Procedure<Set<Resource>> procedure) {
2967 forSupertypes(subject, new NoneToAsyncProcedure<Set<Resource>>(
2972 public void forDirectSuperrelations(Resource subject,
2973 AsyncMultiProcedure<Resource> procedure) {
2975 assert (subject != null);
2976 assert (procedure != null);
2978 processor.forDirectSuperrelations(this, subject, procedure);
2983 public void forPossibleSuperrelation(Resource subject, AsyncProcedure<Resource> procedure) {
2985 assert (subject != null);
2986 assert (procedure != null);
2988 processor.forPossibleSuperrelation(this, subject, procedure);
2993 public void forSuperrelations(Resource subject,
2994 AsyncListener<Set<Resource>> listener) {
2995 asyncRequest(new Types(subject), listener);
2999 public void forSuperrelations(Resource subject,
3000 SyncListener<Set<Resource>> listener) {
3001 asyncRequest(new Types(subject), listener);
3005 public void forSuperrelations(Resource subject,
3006 Listener<Set<Resource>> listener) {
3007 asyncRequest(new Types(subject), listener);
3011 final public void forSuperrelations(final Resource subject,
3012 final AsyncProcedure<Set<Resource>> procedure) {
3014 assert (subject != null);
3015 assert (procedure != null);
3017 processor.forSuperrelations(this, subject, procedure);
3022 public void forSuperrelations(Resource subject,
3023 SyncProcedure<Set<Resource>> procedure) {
3024 forSuperrelations(subject, new SyncToAsyncProcedure<Set<Resource>>(
3029 public void forSuperrelations(Resource subject,
3030 Procedure<Set<Resource>> procedure) {
3031 forSuperrelations(subject, new NoneToAsyncProcedure<Set<Resource>>(
3036 final public void forEachObject(final Resource subject, final Resource relation, final AsyncMultiProcedure<Resource> procedure) {
3037 processor.forEachObject(this, subject, relation, procedure);
3041 public void forEachObject(Resource subject, Resource relation,
3042 SyncMultiProcedure<Resource> procedure) {
3043 forEachObject(subject, relation,
3044 new SyncToAsyncMultiProcedure<Resource>(procedure));
3048 public void forEachObject(Resource subject, Resource relation,
3049 MultiProcedure<Resource> procedure) {
3051 processor.forEachObject(this, subject, relation, procedure);
3056 final public void forEachDirectPredicate(final Resource subject, final AsyncProcedure<Set<Resource>> procedure) {
3057 processor.forEachDirectPredicate(this, subject, procedure);
3061 final public void forEachDirectPredicate(final Resource subject, final SyncProcedure<Set<Resource>> procedure) {
3062 forEachDirectPredicate(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
3066 public void forEachDirectPredicate(Resource subject, Procedure<Set<Resource>> procedure) {
3067 forEachDirectPredicate(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
3071 final public void forObjectSet(final Resource subject,
3072 final Resource relation, final AsyncSetListener<Resource> procedure) {
3074 assert (subject != null);
3075 assert (relation != null);
3076 assert (procedure != null);
3078 processor.forObjectSet(this, subject, relation, procedure);
3083 final public void forObjectSet(final Resource subject,
3084 final Resource relation, final SyncSetListener<Resource> procedure) {
3086 assert (subject != null);
3087 assert (relation != null);
3088 assert (procedure != null);
3090 forObjectSet(subject, relation, new SyncToAsyncSetProcedure<Resource>(
3096 final public void forObjectSet(final Resource subject,
3097 final Resource relation, final SetListener<Resource> procedure) {
3099 assert (subject != null);
3100 assert (relation != null);
3101 assert (procedure != null);
3103 forObjectSet(subject, relation, new NoneToAsyncSetProcedure<Resource>(
3109 final public void forEachAssertedObject(final Resource subject,
3110 final Resource relation,
3111 final AsyncMultiProcedure<Resource> procedure) {
3113 assert (subject != null);
3114 assert (relation != null);
3115 assert (procedure != null);
3117 processor.forEachAssertedObject(this, subject, relation, procedure);
3122 public void forEachAssertedObject(Resource subject, Resource relation,
3123 SyncMultiProcedure<Resource> procedure) {
3125 assert (subject != null);
3126 assert (relation != null);
3127 assert (procedure != null);
3129 forEachAssertedObject(subject, relation,
3130 new SyncToAsyncMultiProcedure<Resource>(procedure));
3135 public void forEachAssertedObject(Resource subject, Resource relation,
3136 MultiProcedure<Resource> procedure) {
3138 assert (subject != null);
3139 assert (relation != null);
3140 assert (procedure != null);
3142 forEachAssertedObject(subject, relation,
3143 new NoneToAsyncMultiProcedure<Resource>(procedure));
3148 public void forAssertedObjectSet(Resource subject, Resource relation,
3149 AsyncSetListener<Resource> procedure) {
3151 assert (subject != null);
3152 assert (relation != null);
3153 assert (procedure != null);
3155 processor.forAssertedObjectSet(this, subject, relation, procedure);
3160 public void forAssertedObjectSet(Resource subject, Resource relation,
3161 SyncSetListener<Resource> procedure) {
3163 assert (subject != null);
3164 assert (relation != null);
3165 assert (procedure != null);
3167 forAssertedObjectSet(subject, relation,
3168 new SyncToAsyncSetProcedure<Resource>(procedure));
3173 public void forAssertedObjectSet(Resource subject, Resource relation,
3174 SetListener<Resource> procedure) {
3176 assert (subject != null);
3177 assert (relation != null);
3178 assert (procedure != null);
3180 forAssertedObjectSet(subject, relation,
3181 new NoneToAsyncSetProcedure<Resource>(procedure));
3186 public void forInverse(Resource relation, AsyncListener<Resource> listener) {
3187 asyncRequest(new Inverse(relation), listener);
3191 public void forInverse(Resource relation, SyncListener<Resource> listener) {
3192 asyncRequest(new Inverse(relation), listener);
3196 public void forInverse(Resource relation, Listener<Resource> listener) {
3197 asyncRequest(new Inverse(relation), listener);
3201 final public void forInverse(final Resource relation,
3202 final AsyncProcedure<Resource> procedure) {
3204 assert (relation != null);
3205 assert (procedure != null);
3207 processor.forInverse(this, relation, new AsyncProcedure<Resource>() {
3210 public void execute(AsyncReadGraph graph, Resource result) {
3212 procedure.execute(graph, result);
3214 procedure.exception(graph, new NoInverseException(relation
3220 public void exception(AsyncReadGraph graph, Throwable throwable) {
3221 procedure.exception(graph, throwable);
3225 public String toString() {
3226 return "forInverse -> " + procedure;
3234 public void forInverse(Resource relation, SyncProcedure<Resource> procedure) {
3235 forInverse(relation, new SyncToAsyncProcedure<Resource>(procedure));
3239 public void forInverse(Resource relation, Procedure<Resource> procedure) {
3240 forInverse(relation, new NoneToAsyncProcedure<Resource>(procedure));
3244 public void forSingleObject(Resource subject, Resource relation,
3245 AsyncListener<Resource> listener) {
3246 asyncRequest(new SingleObject(subject, relation), listener);
3250 public void forSingleObject(Resource subject, Resource relation,
3251 SyncListener<Resource> listener) {
3252 asyncRequest(new SingleObject(subject, relation), listener);
3256 public void forSingleObject(Resource subject, Resource relation,
3257 Listener<Resource> listener) {
3258 asyncRequest(new SingleObject(subject, relation), listener);
3262 final public void forSingleObject(final Resource subject,
3263 final Resource relation, final AsyncProcedure<Resource> procedure) {
3265 assert (subject != null);
3266 assert (relation != null);
3267 assert (procedure != null);
3269 processor.forEachObject(this, subject, relation,
3270 new SingleOrErrorProcedure<Resource>(procedure));
3275 public void forSingleObject(Resource subject, Resource relation,
3276 SyncProcedure<Resource> procedure) {
3277 forSingleObject(subject, relation, new SyncToAsyncProcedure<Resource>(
3282 public void forSingleObject(Resource subject, Resource relation,
3283 Procedure<Resource> procedure) {
3284 forSingleObject(subject, relation, new NoneToAsyncProcedure<Resource>(
3289 public void forSingleStatement(Resource subject, Resource relation,
3290 AsyncListener<Statement> listener) {
3291 asyncRequest(new SingleStatement(subject, relation), listener);
3295 public void forSingleStatement(Resource subject, Resource relation,
3296 SyncListener<Statement> listener) {
3297 asyncRequest(new SingleStatement(subject, relation), listener);
3301 public void forSingleStatement(Resource subject, Resource relation,
3302 Listener<Statement> listener) {
3303 asyncRequest(new SingleStatement(subject, relation), listener);
3307 final public void forSingleStatement(final Resource subject,
3308 final Resource relation, final AsyncProcedure<Statement> procedure) {
3310 assert (subject != null);
3311 assert (relation != null);
3312 assert (procedure != null);
3314 processor.forEachStatement(this, subject, relation,
3315 new SingleOrErrorProcedure<Statement>(procedure));
3320 public void forSingleStatement(Resource subject, Resource relation,
3321 SyncProcedure<Statement> procedure) {
3322 forSingleStatement(subject, relation,
3323 new SyncToAsyncProcedure<Statement>(procedure));
3327 public void forSingleStatement(Resource subject, Resource relation,
3328 Procedure<Statement> procedure) {
3329 forSingleStatement(subject, relation,
3330 new NoneToAsyncProcedure<Statement>(procedure));
3334 public void forSingleType(Resource subject,
3335 AsyncListener<Resource> listener) {
3336 asyncRequest(new SingleTypeAny(subject), listener);
3340 public void forSingleType(Resource subject,
3341 SyncListener<Resource> listener) {
3342 asyncRequest(new SingleTypeAny(subject), listener);
3346 public void forSingleType(Resource subject,
3347 Listener<Resource> listener) {
3348 asyncRequest(new SingleTypeAny(subject), listener);
3352 final public void forSingleType(final Resource subject, final AsyncProcedure<Resource> procedure) {
3354 assert (subject != null);
3355 assert (procedure != null);
3357 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3359 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
3362 public void execute(AsyncReadGraph graph, final Resource principalType) {
3363 checkedProcedure.offer(graph, principalType);
3367 public void finished(AsyncReadGraph graph) {
3368 checkedProcedure.dec(graph);
3372 public void exception(AsyncReadGraph graph, Throwable t) {
3373 checkedProcedure.exception(graph, t);
3377 public String toString() {
3378 return "forSingleType -> " + procedure;
3386 public void forSingleType(Resource subject, SyncProcedure<Resource> procedure) {
3387 forSingleType(subject, new SyncToAsyncProcedure<Resource>(
3392 public void forSingleType(Resource subject, Procedure<Resource> procedure) {
3393 forSingleType(subject, new NoneToAsyncProcedure<Resource>(
3398 public void forSingleType(Resource subject, Resource relation,
3399 AsyncListener<Resource> listener) {
3400 asyncRequest(new SingleType(subject, relation), listener);
3404 public void forSingleType(Resource subject, Resource relation,
3405 SyncListener<Resource> listener) {
3406 asyncRequest(new SingleType(subject, relation), listener);
3410 public void forSingleType(Resource subject, Resource relation,
3411 Listener<Resource> listener) {
3412 asyncRequest(new SingleType(subject, relation), listener);
3416 final public void forSingleType(final Resource subject,
3417 final Resource baseType, final AsyncProcedure<Resource> procedure) {
3419 assert (subject != null);
3420 assert (procedure != null);
3422 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3424 processor.forEachPrincipalType(this, subject,
3425 new AsyncMultiProcedureAdapter<Resource>() {
3428 public void execute(AsyncReadGraph graph,
3429 final Resource principalType) {
3431 checkedProcedure.inc();
3433 if(baseType == null) {
3435 checkedProcedure.offer(graph, principalType);
3436 checkedProcedure.dec(graph);
3438 } else if(principalType.equals(baseType)) {
3440 checkedProcedure.offer(graph, principalType);
3441 checkedProcedure.dec(graph);
3445 processor.forSupertypes((ReadGraphImpl)graph, principalType,
3446 new AsyncProcedure<Set<Resource>>() {
3449 public void execute(
3450 AsyncReadGraph graph,
3451 Set<Resource> result) {
3453 if (result.contains(baseType))
3454 checkedProcedure.offer(graph,
3456 checkedProcedure.dec(graph);
3461 public void exception(
3462 AsyncReadGraph graph,
3465 .exception(graph, t);
3475 public void finished(AsyncReadGraph graph) {
3476 checkedProcedure.dec(graph);
3480 public void exception(AsyncReadGraph graph, Throwable t) {
3481 checkedProcedure.exception(graph, t);
3485 public String toString() {
3486 return "forSingleType -> " + procedure;
3494 public void forSingleType(Resource subject, Resource relation,
3495 SyncProcedure<Resource> procedure) {
3496 forSingleType(subject, relation, new SyncToAsyncProcedure<Resource>(
3501 public void forSingleType(Resource subject, Resource relation,
3502 Procedure<Resource> procedure) {
3503 forSingleType(subject, relation, new NoneToAsyncProcedure<Resource>(
3508 public <T> void forValue(Resource subject, Binding binding,
3509 AsyncListener<T> listener) {
3510 asyncRequest(new Value<T>(subject, binding), listener);
3514 public <T> void forValue(Resource subject, Binding binding,
3515 SyncListener<T> listener) {
3516 asyncRequest(new Value<T>(subject, binding), listener);
3520 public <T> void forValue(Resource subject, Binding binding,
3521 Listener<T> listener) {
3522 asyncRequest(new Value<T>(subject, binding), listener);
3526 public <T> void forValue(final Resource resource, final Binding binding,
3527 final AsyncProcedure<T> procedure) {
3529 assert (resource != null);
3530 assert (binding != null);
3531 assert (procedure != null);
3533 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
3536 public void execute(AsyncReadGraph graph, byte[] result) {
3540 if (result == null) {
3541 procedure.exception(graph,
3542 new DoesNotContainValueException(
3543 "No value for resource " + resource));
3547 Serializer serializer = binding.serializer();
3548 // Serializer serializer = Bindings.getSerializer( binding );
3549 Object obj = serializer.deserialize(result);
3550 // if (!binding.isInstance(obj))
3551 // procedure.exception(graph, new ClassCastException(
3552 // "Cannot get value " + obj + " with binding "
3555 procedure.execute(graph, (T) obj);
3557 } catch (Throwable t) {
3558 procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
3563 public void exception(AsyncReadGraph graph, Throwable t) {
3565 procedure.exception(graph, t);
3566 } catch (Throwable t2) {
3567 Logger.defaultLogError(t2);
3572 public String toString() {
3573 return "forValue -> " + procedure;
3580 private static String safeArrayToString(byte[] a) {
3583 int iMax = a.length - 1;
3587 StringBuilder b = new StringBuilder();
3589 for (int i = 0; i < 100; i++) { // limit to first 100 items
3592 return b.append(']').toString();
3595 return b.append(", ... (" + a.length + ")]").toString();
3599 public <T> void forValue(Resource subject, Binding binding,
3600 SyncProcedure<T> procedure) {
3601 forValue(subject, binding, new SyncToAsyncProcedure<T>(procedure));
3605 public <T> void forValue(Resource subject, Binding binding,
3606 Procedure<T> procedure) {
3607 forValue(subject, binding, new NoneToAsyncProcedure<T>(procedure));
3611 public <T> void forValue(Resource subject, AsyncListener<T> listener) {
3612 asyncRequest(new ValueImplied<T>(subject), listener);
3616 public <T> void forValue(Resource subject, SyncListener<T> listener) {
3617 asyncRequest(new ValueImplied<T>(subject), listener);
3621 public <T> void forValue(Resource subject, Listener<T> listener) {
3622 asyncRequest(new ValueImplied<T>(subject), listener);
3626 final public <T> void forValue(final Resource subject, final AsyncProcedure<T> procedure) {
3628 assert (subject != null);
3629 assert (procedure != null);
3631 forRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
3634 public void execute(AsyncReadGraph graph, Datatype type) {
3635 // TODO: consider trying Bindings.getBeanBinding(type);
3636 Binding binding = Bindings.getBinding(type);
3637 graph.forValue(subject, binding, procedure);
3641 public void exception(AsyncReadGraph graph, Throwable throwable) {
3642 procedure.exception(graph, new DoesNotContainValueException("Invalid data type", throwable));
3650 public <T> void forValue(Resource subject, SyncProcedure<T> procedure) {
3651 forValue(subject, new SyncToAsyncProcedure<T>(procedure));
3655 public <T> void forValue(Resource subject, Procedure<T> procedure) {
3656 forValue(subject, new NoneToAsyncProcedure<T>(procedure));
3660 public <T> void forRelatedValue(Resource subject, Resource relation,
3661 AsyncListener<T> listener) {
3662 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3666 public <T> void forRelatedValue(Resource subject, Resource relation,
3667 SyncListener<T> listener) {
3668 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3672 public <T> void forRelatedValue(Resource subject, Resource relation,
3673 Listener<T> listener) {
3674 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3678 final public <T> void forRelatedValue(final Resource subject,
3679 final Resource relation, final AsyncProcedure<T> procedure) {
3681 assert (subject != null);
3682 assert (relation != null);
3683 assert (procedure != null);
3685 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3687 processor.forEachObject(this, subject, relation,
3688 new AsyncMultiProcedureAdapter<Resource>() {
3691 public void execute(AsyncReadGraph graph,
3692 final Resource object) {
3694 checkedProcedure.inc();
3696 graph.forValue(object, new AsyncProcedure<Object>() {
3699 public void execute(AsyncReadGraph graph,
3701 checkedProcedure.offer(graph, (T) result);
3702 checkedProcedure.dec(graph);
3706 public void exception(AsyncReadGraph graph,
3708 checkedProcedure.exception(graph, t);
3712 public String toString() {
3713 return "forRelatedValue -> " + procedure;
3721 public void finished(AsyncReadGraph graph) {
3722 checkedProcedure.dec(graph);
3726 public void exception(AsyncReadGraph graph, Throwable t) {
3727 checkedProcedure.exception(graph, t);
3735 public <T> void forRelatedValue(Resource subject, Resource relation,
3736 SyncProcedure<T> procedure) {
3737 forRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
3742 public <T> void forRelatedValue(Resource subject, Resource relation,
3743 Procedure<T> procedure) {
3744 forRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
3749 public <T> void forRelatedValue(Resource subject, Resource relation,
3750 Binding binding, AsyncListener<T> listener) {
3751 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3755 public <T> void forRelatedValue(Resource subject, Resource relation,
3756 Binding binding, SyncListener<T> listener) {
3757 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3761 public <T> void forRelatedValue(Resource subject, Resource relation,
3762 Binding binding, Listener<T> listener) {
3763 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3767 final public <T> void forRelatedValue(final Resource subject,
3768 final Resource relation, final Binding binding,
3769 final AsyncProcedure<T> procedure) {
3771 assert (subject != null);
3772 assert (relation != null);
3773 assert (binding != null);
3774 assert (procedure != null);
3776 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3778 processor.forEachObject(this, subject, relation,
3779 new AsyncMultiProcedureAdapter<Resource>() {
3782 public void execute(AsyncReadGraph graph,
3783 final Resource object) {
3785 checkedProcedure.inc();
3787 graph.forValue(object, binding, new AsyncProcedure<Object>() {
3790 public void execute(AsyncReadGraph graph,
3793 checkedProcedure.offer(graph,
3795 checkedProcedure.dec(graph);
3799 public void exception(AsyncReadGraph graph,
3801 checkedProcedure.exception(graph, t);
3805 public String toString() {
3806 return "forRelatedValue -> "
3815 public void finished(AsyncReadGraph graph) {
3816 checkedProcedure.dec(graph);
3820 public void exception(AsyncReadGraph graph, Throwable t) {
3821 checkedProcedure.exception(graph, t);
3829 public <T> void forRelatedValue(Resource subject, Resource relation,
3830 Binding binding, SyncProcedure<T> procedure) {
3831 forRelatedValue(subject, relation, binding,
3832 new SyncToAsyncProcedure<T>(procedure));
3836 public <T> void forRelatedValue(Resource subject, Resource relation,
3837 Binding binding, Procedure<T> procedure) {
3838 forRelatedValue(subject, relation, binding,
3839 new NoneToAsyncProcedure<T>(procedure));
3843 public <T> void forAdapted(Resource resource, Class<T> clazz,
3844 AsyncListener<T> listener) {
3845 asyncRequest(new Adapter<T>(resource, clazz), listener);
3849 public <T> void forAdapted(Resource resource, Class<T> clazz,
3850 SyncListener<T> listener) {
3851 asyncRequest(new Adapter<T>(resource, clazz), listener);
3855 public <T> void forAdapted(Resource resource, Class<T> clazz,
3856 Listener<T> listener) {
3857 asyncRequest(new Adapter<T>(resource, clazz), listener);
3861 final public <T> void forAdapted(final Resource resource,
3862 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3864 assert (resource != null);
3865 assert (clazz != null);
3866 assert (procedure != null);
3868 final AdaptionService service = getSession().peekService(AdaptionService.class);
3869 if (service == null)
3870 procedure.exception(this, new ServiceException("No AdaptionService available"));
3872 service.adapt(this, resource, resource, Resource.class, clazz, false, procedure);
3877 public <T> void forAdapted(Resource resource, Class<T> clazz,
3878 SyncProcedure<T> procedure) {
3879 forAdapted(resource, clazz, new SyncToAsyncProcedure<T>(procedure));
3883 public <T> void forAdapted(Resource resource, Class<T> clazz,
3884 Procedure<T> procedure) {
3885 forAdapted(resource, clazz, new NoneToAsyncProcedure<T>(procedure));
3889 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3890 AsyncListener<T> listener) {
3891 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3895 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3896 SyncListener<T> listener) {
3897 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3901 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3902 Listener<T> listener) {
3903 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3907 final public <T> void forUniqueAdapted(final Resource resource,
3908 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3910 assert (resource != null);
3911 assert (clazz != null);
3912 assert (procedure != null);
3914 final AdaptionService service = getSession().peekService(AdaptionService.class);
3915 if (service == null)
3916 procedure.exception(this, new ServiceException("No AdaptionService available"));
3918 service.adaptNew(this, resource, clazz, false, procedure);
3923 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3924 SyncProcedure<T> procedure) {
3925 forUniqueAdapted(resource, clazz,
3926 new SyncToAsyncProcedure<T>(procedure));
3930 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3931 Procedure<T> procedure) {
3932 forUniqueAdapted(resource, clazz,
3933 new NoneToAsyncProcedure<T>(procedure));
3937 public void forPossibleInverse(Resource subject,
3938 AsyncListener<Resource> listener) {
3939 asyncRequest(new PossibleInverse(subject), listener);
3943 public void forPossibleInverse(Resource subject,
3944 SyncListener<Resource> listener) {
3945 asyncRequest(new PossibleInverse(subject), listener);
3949 public void forPossibleInverse(Resource subject, Listener<Resource> listener) {
3950 asyncRequest(new PossibleInverse(subject), listener);
3954 final public void forPossibleInverse(final Resource relation,
3955 final AsyncProcedure<Resource> procedure) {
3957 assert (relation != null);
3958 assert (procedure != null);
3960 processor.forInverse(this, relation, new ExceptionToNullProcedure<Resource>(procedure));
3965 public void forPossibleInverse(Resource subject,
3966 SyncProcedure<Resource> procedure) {
3967 forPossibleInverse(subject, new SyncToAsyncProcedure<Resource>(
3972 public void forPossibleInverse(Resource subject,
3973 Procedure<Resource> procedure) {
3974 forPossibleInverse(subject, new NoneToAsyncProcedure<Resource>(
3979 public void forPossibleObject(Resource subject, Resource relation,
3980 AsyncListener<Resource> listener) {
3981 asyncRequest(new PossibleObject(subject, relation), listener);
3985 public void forPossibleObject(Resource subject, Resource relation,
3986 SyncListener<Resource> listener) {
3987 asyncRequest(new PossibleObject(subject, relation), listener);
3991 public void forPossibleObject(Resource subject, Resource relation,
3992 Listener<Resource> listener) {
3993 asyncRequest(new PossibleObject(subject, relation), listener);
3997 final public void forPossibleObject(final Resource subject,
3998 final Resource relation, final AsyncProcedure<Resource> procedure) {
4000 assert (subject != null);
4001 assert (relation != null);
4002 assert (procedure != null);
4004 processor.forEachObject(this, subject, relation,
4005 new SingleOrNullProcedure<Resource>(procedure));
4010 public void forPossibleObject(Resource subject, Resource relation,
4011 SyncProcedure<Resource> procedure) {
4012 forPossibleObject(subject, relation,
4013 new SyncToAsyncProcedure<Resource>(procedure));
4017 public void forPossibleObject(Resource subject, Resource relation,
4018 Procedure<Resource> procedure) {
4019 forPossibleObject(subject, relation,
4020 new NoneToAsyncProcedure<Resource>(procedure));
4024 public void forPossibleStatement(Resource subject, Resource relation,
4025 AsyncListener<Statement> listener) {
4026 asyncRequest(new PossibleStatement(subject, relation), listener);
4030 public void forPossibleStatement(Resource subject, Resource relation,
4031 SyncListener<Statement> listener) {
4032 asyncRequest(new PossibleStatement(subject, relation), listener);
4036 public void forPossibleStatement(Resource subject, Resource relation,
4037 Listener<Statement> listener) {
4038 asyncRequest(new PossibleStatement(subject, relation), listener);
4042 final public void forPossibleStatement(final Resource subject,
4043 final Resource relation, final AsyncProcedure<Statement> procedure) {
4045 assert (subject != null);
4046 assert (relation != null);
4047 assert (procedure != null);
4049 processor.forEachStatement(this, subject, relation,
4050 new SingleFunctionalOrNullProcedure<Statement>(
4051 "forPossibleStatement", procedure));
4056 public void forPossibleStatement(Resource subject, Resource relation,
4057 SyncProcedure<Statement> procedure) {
4058 forPossibleStatement(subject, relation,
4059 new SyncToAsyncProcedure<Statement>(procedure));
4063 public void forPossibleStatement(Resource subject, Resource relation,
4064 Procedure<Statement> procedure) {
4065 forPossibleStatement(subject, relation,
4066 new NoneToAsyncProcedure<Statement>(procedure));
4070 public void forPossibleType(Resource subject, Resource relation,
4071 AsyncListener<Resource> listener) {
4072 asyncRequest(new PossibleType(subject, relation), listener);
4076 public void forPossibleType(Resource subject, Resource relation,
4077 SyncListener<Resource> listener) {
4078 asyncRequest(new PossibleType(subject, relation), listener);
4082 public void forPossibleType(Resource subject, Resource relation,
4083 Listener<Resource> listener) {
4084 asyncRequest(new PossibleType(subject, relation), listener);
4088 final public void forPossibleType(final Resource subject,
4089 final Resource baseType, final AsyncProcedure<Resource> procedure) {
4091 assert (subject != null);
4092 assert (procedure != null);
4094 final NullSingleOrNullProcedure<Resource> checkedProcedure = new NullSingleOrNullProcedure<Resource>(procedure);
4096 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
4099 public void execute(AsyncReadGraph graph,
4100 final Resource principalType) {
4102 if (baseType == null) {
4104 checkedProcedure.offer(graph, principalType);
4106 } else if (principalType.equals(baseType)) {
4108 checkedProcedure.offer(graph, principalType);
4112 checkedProcedure.inc();
4114 processor.forSupertypes((ReadGraphImpl)graph, principalType,
4115 new AsyncProcedure<Set<Resource>>() {
4118 public void execute(
4119 AsyncReadGraph graph,
4120 Set<Resource> result) {
4122 if (result.contains(baseType)) {
4123 checkedProcedure.offer(graph,
4127 checkedProcedure.dec(graph);
4132 public void exception(
4133 AsyncReadGraph graph,
4135 checkedProcedure.exception(graph, t);
4136 checkedProcedure.dec(graph);
4140 public String toString() {
4141 return "forPossibleType -> "
4152 public void finished(AsyncReadGraph graph) {
4153 checkedProcedure.dec(graph);
4157 public void exception(AsyncReadGraph graph, Throwable t) {
4158 checkedProcedure.exception(graph, t);
4159 checkedProcedure.dec(graph);
4167 public void forPossibleType(Resource subject, Resource relation,
4168 SyncProcedure<Resource> procedure) {
4169 forPossibleType(subject, relation, new SyncToAsyncProcedure<Resource>(
4174 public void forPossibleType(Resource subject, Resource relation,
4175 Procedure<Resource> procedure) {
4176 forPossibleType(subject, relation, new NoneToAsyncProcedure<Resource>(
4181 public <T> void forPossibleValue(Resource subject, AsyncListener<T> listener) {
4182 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4186 public <T> void forPossibleValue(Resource subject, SyncListener<T> listener) {
4187 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4191 public <T> void forPossibleValue(Resource subject, Listener<T> listener) {
4192 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4196 final public <T> void forPossibleValue(final Resource subject,
4197 final AsyncProcedure<T> procedure) {
4199 assert (subject != null);
4200 assert (procedure != null);
4202 forPossibleRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
4205 public void execute(AsyncReadGraph graph, final Datatype type) {
4207 procedure.execute(graph, null);
4210 // TODO: consider trying Bindings.getBeanBinding(type);
4211 Binding binding = Bindings.getBinding(type);
4212 graph.forPossibleValue(subject, binding, procedure);
4213 } catch (RuntimeBindingConstructionException e) {
4214 procedure.exception(graph, e);
4220 public void exception(AsyncReadGraph graph, Throwable t) {
4221 procedure.exception(graph, t);
4225 public String toString() {
4226 return "forPossibleValue -> " + procedure;
4234 public <T> void forPossibleValue(Resource subject,
4235 SyncProcedure<T> procedure) {
4236 forPossibleValue(subject, new SyncToAsyncProcedure<T>(procedure));
4240 public <T> void forPossibleValue(Resource subject, Procedure<T> procedure) {
4241 forPossibleValue(subject, new NoneToAsyncProcedure<T>(procedure));
4245 public <T> void forPossibleValue(Resource subject, Binding binding,
4246 AsyncListener<T> listener) {
4247 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4251 public <T> void forPossibleValue(Resource subject, Binding binding,
4252 SyncListener<T> listener) {
4253 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4257 public <T> void forPossibleValue(Resource subject, Binding binding,
4258 Listener<T> listener) {
4259 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4263 final public <T> void forPossibleValue(final Resource resource,
4264 final Binding binding, final AsyncProcedure<T> procedure) {
4266 assert (resource != null);
4267 assert (binding != null);
4268 assert (procedure != null);
4270 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
4273 public void execute(AsyncReadGraph graph, byte[] result) {
4277 if (result == null) {
4278 procedure.execute(graph, null);
4282 Serializer serializer = Bindings.getSerializer( binding );
4283 Object obj = serializer.deserialize(result);
4284 if (!binding.isInstance(obj))
4285 procedure.exception(graph, new ClassCastException(
4286 "Cannot get value " + obj + " with binding "
4289 procedure.execute(graph, (T) obj);
4291 } catch (Throwable t) {
4292 procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
4297 public void exception(AsyncReadGraph graph, Throwable t) {
4299 procedure.exception(graph, t);
4300 } catch (Throwable t2) {
4301 Logger.defaultLogError(t2);
4306 public String toString() {
4307 return "forPossibleValue -> " + procedure;
4315 public <T> void forPossibleValue(Resource subject, Binding binding,
4316 SyncProcedure<T> procedure) {
4317 forPossibleValue(subject, binding, new SyncToAsyncProcedure<T>(
4322 public <T> void forPossibleValue(Resource subject, Binding binding,
4323 Procedure<T> procedure) {
4324 forPossibleValue(subject, binding, new NoneToAsyncProcedure<T>(
4329 public <T> void forPossibleRelatedValue(Resource subject,
4330 Resource relation, AsyncListener<T> listener) {
4331 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4336 public <T> void forPossibleRelatedValue(Resource subject,
4337 Resource relation, SyncListener<T> listener) {
4338 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4343 public <T> void forPossibleRelatedValue(Resource subject,
4344 Resource relation, Listener<T> listener) {
4345 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4350 final public <T> void forPossibleRelatedValue(final Resource subject,
4351 final Resource relation, final AsyncProcedure<T> procedure) {
4353 assert (subject != null);
4354 assert (relation != null);
4355 assert (procedure != null);
4357 final DeepSingleOrNullProcedure<T> checkedProcedure = new DeepSingleOrNullProcedure<T>(procedure);
4359 processor.forEachObject(this, subject, relation,
4360 new AsyncMultiProcedureAdapter<Resource>() {
4363 public void execute(AsyncReadGraph graph,
4364 final Resource object) {
4366 checkedProcedure.inc();
4368 graph.forValue(object, new AsyncProcedure<Object>() {
4371 public void execute(AsyncReadGraph graph,
4373 checkedProcedure.offer(graph, (T) result);
4374 checkedProcedure.dec(graph);
4378 public void exception(AsyncReadGraph graph,
4380 checkedProcedure.exception(graph, t);
4381 checkedProcedure.dec(graph);
4389 public void finished(AsyncReadGraph graph) {
4391 checkedProcedure.dec(graph);
4395 public void exception(AsyncReadGraph graph, Throwable t) {
4396 checkedProcedure.exception(graph, t);
4397 checkedProcedure.dec(graph);
4401 public String toString() {
4402 return "forPossibleRelatedValue -> " + procedure;
4409 public <T> void forPossibleRelatedValue(Resource subject,
4410 Resource relation, SyncProcedure<T> procedure) {
4411 forPossibleRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
4416 public <T> void forPossibleRelatedValue(Resource subject,
4417 Resource relation, Procedure<T> procedure) {
4418 forPossibleRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
4423 public <T> void forPossibleRelatedValue(Resource subject,
4424 Resource relation, Binding binding, AsyncListener<T> listener) {
4425 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4430 public <T> void forPossibleRelatedValue(Resource subject,
4431 Resource relation, Binding binding, SyncListener<T> listener) {
4432 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4437 public <T> void forPossibleRelatedValue(Resource subject,
4438 Resource relation, Binding binding, Listener<T> listener) {
4439 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4443 final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4444 final AsyncProcedure<T> procedure) {
4446 assert (subject != null);
4447 assert (relation != null);
4448 assert (procedure != null);
4450 processor.forPossibleObject(this, subject, relation, new AsyncProcedure<Resource>() {
4453 public void execute(AsyncReadGraph graph, Resource object) {
4455 if(object == null) {
4456 procedure.execute(graph, null);
4460 processor.forPossibleValue((ReadGraphImpl)graph, object, new AsyncProcedure<byte[]>() {
4463 public void execute(AsyncReadGraph graph, byte[] bytes) {
4469 Serializer serializer = binding.serializer();
4470 Object obj = serializer.deserialize(bytes);
4471 if (!binding.isInstance(obj)) {
4472 procedure.exception(graph, new ClassCastException("Cannot get value " + obj + " with binding " + binding));
4474 procedure.execute(graph, (T) obj);
4477 } catch (Throwable t) {
4479 procedure.exception(graph, t);
4485 procedure.execute(graph, null);
4492 public void exception(AsyncReadGraph graph, Throwable t) {
4493 procedure.exception(graph, t);
4501 public void exception(AsyncReadGraph graph, Throwable throwable) {
4502 throwable.printStackTrace();
4503 procedure.exception(graph, throwable);
4511 public <T> void forPossibleRelatedValue(Resource subject,
4512 Resource relation, Binding binding, SyncProcedure<T> procedure) {
4513 forPossibleRelatedValue(subject, relation, binding,
4514 new SyncToAsyncProcedure<T>(procedure));
4518 public <T> void forPossibleRelatedValue(Resource subject,
4519 Resource relation, Binding binding, Procedure<T> procedure) {
4520 forPossibleRelatedValue(subject, relation, binding,
4521 new NoneToAsyncProcedure<T>(procedure));
4525 public void forIsInstanceOf(Resource subject, Resource relation,
4526 AsyncListener<Boolean> listener) {
4527 asyncRequest(new IsInstanceOf(subject, relation), listener);
4531 public void forIsInstanceOf(Resource subject, Resource relation,
4532 SyncListener<Boolean> listener) {
4533 asyncRequest(new IsInstanceOf(subject, relation), listener);
4537 public void forIsInstanceOf(Resource subject, Resource relation,
4538 Listener<Boolean> listener) {
4539 asyncRequest(new IsInstanceOf(subject, relation), listener);
4543 final public void forIsInstanceOf(final Resource resource,
4544 final Resource type, final AsyncProcedure<Boolean> procedure) {
4546 assert (resource != null);
4547 assert (type != null);
4548 assert (procedure != null);
4550 forTypes(resource, new AsyncProcedure<Set<Resource>>() {
4553 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4556 if (result.contains(type))
4557 procedure.execute(graph, true);
4559 procedure.execute(graph, false);
4560 } catch (Throwable t) {
4561 Logger.defaultLogError(t);
4566 public void exception(AsyncReadGraph graph, Throwable t) {
4568 procedure.exception(graph, t);
4569 } catch (Throwable t2) {
4570 Logger.defaultLogError(t2);
4575 public String toString() {
4576 return "forIsInstanceOf -> " + procedure;
4584 public void forIsInstanceOf(Resource subject, Resource relation,
4585 SyncProcedure<Boolean> procedure) {
4586 forIsInstanceOf(subject, relation, new SyncToAsyncProcedure<Boolean>(
4591 public void forIsInstanceOf(Resource subject, Resource relation,
4592 Procedure<Boolean> procedure) {
4593 forIsInstanceOf(subject, relation, new NoneToAsyncProcedure<Boolean>(
4598 public void forIsInheritedFrom(Resource subject, Resource relation,
4599 AsyncListener<Boolean> listener) {
4600 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4604 public void forIsInheritedFrom(Resource subject, Resource relation,
4605 SyncListener<Boolean> listener) {
4606 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4610 public void forIsInheritedFrom(Resource subject, Resource relation,
4611 Listener<Boolean> listener) {
4612 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4616 final public void forIsInheritedFrom(final Resource resource,
4617 final Resource type, final AsyncProcedure<Boolean> procedure) {
4619 assert (resource != null);
4620 assert (type != null);
4621 assert (procedure != null);
4623 if (resource.equals(type)) {
4625 procedure.execute(this, true);
4626 } catch (Throwable t) {
4627 Logger.defaultLogError(t);
4632 forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
4635 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4637 if (result.contains(type))
4638 procedure.execute(graph, true);
4640 procedure.execute(graph, false);
4641 } catch (Throwable t) {
4642 Logger.defaultLogError(t);
4647 public void exception(AsyncReadGraph graph, Throwable t) {
4649 procedure.exception(graph, t);
4650 } catch (Throwable t2) {
4651 Logger.defaultLogError(t2);
4656 public String toString() {
4657 return "forIsInheritedFrom -> " + procedure;
4665 public void forIsInheritedFrom(Resource subject, Resource relation,
4666 SyncProcedure<Boolean> procedure) {
4667 forIsInheritedFrom(subject, relation,
4668 new SyncToAsyncProcedure<Boolean>(procedure));
4672 public void forIsInheritedFrom(Resource subject, Resource relation,
4673 Procedure<Boolean> procedure) {
4674 forIsInheritedFrom(subject, relation,
4675 new NoneToAsyncProcedure<Boolean>(procedure));
4679 public void forIsSubrelationOf(Resource subject, Resource relation,
4680 AsyncListener<Boolean> listener) {
4681 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4685 public void forIsSubrelationOf(Resource subject, Resource relation,
4686 SyncListener<Boolean> listener) {
4687 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4691 public void forIsSubrelationOf(Resource subject, Resource relation,
4692 Listener<Boolean> listener) {
4693 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4697 final public void forIsSubrelationOf(final Resource resource,
4698 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4700 assert (resource != null);
4701 assert (relation != null);
4702 assert (procedure != null);
4704 if (resource.equals(relation)) {
4705 procedure.execute(this, true);
4709 forSuperrelations(resource, new AsyncProcedure<Set<Resource>>() {
4712 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4714 if (result.contains(relation))
4715 procedure.execute(graph, true);
4717 procedure.execute(graph, false);
4718 } catch (Throwable t) {
4719 Logger.defaultLogError(t);
4724 public void exception(AsyncReadGraph graph, Throwable t) {
4726 procedure.exception(graph, t);
4727 } catch (Throwable t2) {
4728 Logger.defaultLogError(t2);
4733 public String toString() {
4734 return "forIsSubrelationOf -> " + procedure;
4742 public void forIsSubrelationOf(Resource subject, Resource relation,
4743 SyncProcedure<Boolean> procedure) {
4744 forIsSubrelationOf(subject, relation,
4745 new SyncToAsyncProcedure<Boolean>(procedure));
4749 public void forIsSubrelationOf(Resource subject, Resource relation,
4750 Procedure<Boolean> procedure) {
4751 forIsSubrelationOf(subject, relation,
4752 new NoneToAsyncProcedure<Boolean>(procedure));
4756 public void forHasStatement(Resource subject,
4757 AsyncListener<Boolean> listener) {
4758 asyncRequest(new HasStatementSubject(subject), listener);
4762 public void forHasStatement(Resource subject, SyncListener<Boolean> listener) {
4763 asyncRequest(new HasStatementSubject(subject), listener);
4767 public void forHasStatement(Resource subject, Listener<Boolean> listener) {
4768 asyncRequest(new HasStatementSubject(subject), listener);
4772 final public void forHasStatement(final Resource subject,
4773 final AsyncProcedure<Boolean> procedure) {
4775 assert (subject != null);
4776 assert (procedure != null);
4778 processor.forHasStatement(this, subject, procedure);
4783 public void forHasStatement(Resource subject,
4784 SyncProcedure<Boolean> procedure) {
4785 forHasStatement(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4789 public void forHasStatement(Resource subject, Procedure<Boolean> procedure) {
4790 forHasStatement(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4794 public void forHasStatement(Resource subject, Resource relation,
4795 AsyncListener<Boolean> listener) {
4796 asyncRequest(new HasStatement(subject, relation), listener);
4800 public void forHasStatement(Resource subject, Resource relation,
4801 SyncListener<Boolean> listener) {
4802 asyncRequest(new HasStatement(subject, relation), listener);
4806 public void forHasStatement(Resource subject, Resource relation,
4807 Listener<Boolean> listener) {
4808 asyncRequest(new HasStatement(subject, relation), listener);
4812 final public void forHasStatement(final Resource subject,
4813 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4815 assert (subject != null);
4816 assert (relation != null);
4817 assert (procedure != null);
4819 processor.forHasStatement(this, subject, relation, procedure);
4824 public void forHasStatement(Resource subject, Resource relation,
4825 SyncProcedure<Boolean> procedure) {
4826 forHasStatement(subject, relation, new SyncToAsyncProcedure<Boolean>(
4831 public void forHasStatement(Resource subject, Resource relation,
4832 Procedure<Boolean> procedure) {
4833 forHasStatement(subject, relation, new NoneToAsyncProcedure<Boolean>(
4838 public void forHasStatement(Resource subject, Resource relation,
4839 Resource object, AsyncListener<Boolean> listener) {
4840 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4845 public void forHasStatement(Resource subject, Resource relation,
4846 Resource object, SyncListener<Boolean> listener) {
4847 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4852 public void forHasStatement(Resource subject, Resource relation,
4853 Resource object, Listener<Boolean> listener) {
4854 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4859 final public void forHasStatement(final Resource subject,
4860 final Resource relation, final Resource object,
4861 final AsyncProcedure<Boolean> procedure) {
4863 assert (subject != null);
4864 assert (relation != null);
4865 assert (object != null);
4866 assert (procedure != null);
4868 processor.forHasStatement(this, subject, relation, object, procedure);
4873 public void forHasStatement(Resource subject, Resource relation,
4874 Resource object, SyncProcedure<Boolean> procedure) {
4875 forHasStatement(subject, relation, object,
4876 new SyncToAsyncProcedure<Boolean>(procedure));
4880 public void forHasStatement(Resource subject, Resource relation,
4881 Resource object, Procedure<Boolean> procedure) {
4882 forHasStatement(subject, relation, object,
4883 new NoneToAsyncProcedure<Boolean>(procedure));
4887 public void forHasValue(Resource subject, AsyncListener<Boolean> listener) {
4888 asyncRequest(new HasValue(subject), listener);
4892 public void forHasValue(Resource subject, SyncListener<Boolean> listener) {
4893 asyncRequest(new HasValue(subject), listener);
4897 public void forHasValue(Resource subject, Listener<Boolean> listener) {
4898 asyncRequest(new HasValue(subject), listener);
4902 final public void forHasValue(final Resource subject,
4903 final AsyncProcedure<Boolean> procedure) {
4905 assert (subject != null);
4906 assert (procedure != null);
4908 processor.forValue(this, subject, new AsyncProcedure<byte[]>() {
4911 public void execute(AsyncReadGraph graph, byte[] result) {
4914 procedure.execute(graph, false);
4916 procedure.execute(graph, true);
4917 } catch (Throwable t) {
4918 Logger.defaultLogError(t);
4923 public void exception(AsyncReadGraph graph, Throwable t) {
4925 procedure.exception(graph, t);
4926 } catch (Throwable t2) {
4927 Logger.defaultLogError(t2);
4932 public String toString() {
4933 return "forHasValue -> " + procedure;
4941 public void forHasValue(Resource subject, SyncProcedure<Boolean> procedure) {
4942 forHasValue(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4946 public void forHasValue(Resource subject, Procedure<Boolean> procedure) {
4947 forHasValue(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4951 public void forOrderedSet(Resource subject,
4952 AsyncMultiListener<Resource> listener) {
4953 asyncRequest(new OrderedSet(subject), listener);
4957 public void forOrderedSet(Resource subject,
4958 SyncMultiListener<Resource> listener) {
4959 asyncRequest(new OrderedSet(subject), listener);
4963 public void forOrderedSet(Resource subject, MultiListener<Resource> listener) {
4964 asyncRequest(new OrderedSet(subject), listener);
4968 final public void forOrderedSet(final Resource subject,
4969 final AsyncMultiProcedure<Resource> procedure) {
4971 assert (subject != null);
4972 assert (procedure != null);
4974 processor.forOrderedSet(this, subject,
4975 new AsyncMultiProcedure<Resource>() {
4978 public void finished(AsyncReadGraph graph) {
4980 procedure.finished(graph);
4981 } catch (Throwable t) {
4982 Logger.defaultLogError(t);
4987 public void execute(AsyncReadGraph graph, Resource result) {
4989 procedure.execute(graph, result);
4990 } catch (Throwable t) {
4991 Logger.defaultLogError(t);
4996 public void exception(AsyncReadGraph graph, Throwable t) {
4998 procedure.exception(graph, t);
4999 } catch (Throwable t2) {
5000 Logger.defaultLogError(t2);
5005 public String toString() {
5006 return "forOrderedSet -> " + procedure;
5014 public void forOrderedSet(Resource subject,
5015 SyncMultiProcedure<Resource> procedure) {
5016 forOrderedSet(subject, new SyncToAsyncMultiProcedure<Resource>(
5021 public void forOrderedSet(Resource subject,
5022 MultiProcedure<Resource> procedure) {
5023 forOrderedSet(subject, new NoneToAsyncMultiProcedure<Resource>(
5028 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5029 AsyncListener<T> listener) {
5030 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5034 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5035 SyncListener<T> listener) {
5036 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5040 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5041 Listener<T> listener) {
5042 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5046 final public <T> void forPossibleAdapted(final Resource resource,
5047 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5049 assert (resource != null);
5050 assert (clazz != null);
5051 assert (procedure != null);
5053 final AdaptionService service = getSession().peekService(AdaptionService.class);
5054 if (service == null)
5055 procedure.exception(this, new ServiceException("No AdaptionService available"));
5057 service.adapt(this, resource, resource, Resource.class, clazz, true, procedure);
5061 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5062 SyncProcedure<T> procedure) {
5063 forPossibleAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5068 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5069 Procedure<T> procedure) {
5070 forPossibleAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5075 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5076 AsyncListener<T> listener) {
5077 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5081 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5082 SyncListener<T> listener) {
5083 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5087 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5088 Listener<T> listener) {
5089 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5093 final public <T> void forPossibleUniqueAdapted(final Resource resource,
5094 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5096 assert (resource != null);
5097 assert (clazz != null);
5098 assert (procedure != null);
5100 final AdaptionService service = getSession().peekService(AdaptionService.class);
5101 if (service == null)
5102 procedure.exception(this, new ServiceException("No AdaptionService available"));
5104 service.adaptNew(this, resource, clazz, true, procedure);
5109 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5110 SyncProcedure<T> procedure) {
5111 forPossibleUniqueAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5116 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5117 Procedure<T> procedure) {
5118 forPossibleUniqueAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5123 * Implementation of the interface AsyncRequestProcessor
5127 final public Session getSession() {
5128 return processor.getSession();
5132 public <T> void asyncRequest(final Read<T> request) {
5134 asyncRequest(request, new AsyncProcedure<T>() {
5137 public void execute(AsyncReadGraph graph, T result) {
5141 public void exception(AsyncReadGraph graph, Throwable t) {
5142 Logger.defaultLogError(t);
5146 public String toString() {
5147 return "asyncRequest(Read) -> " + request;
5155 public <T> void asyncRequest(Read<T> request, AsyncListener<T> procedure) {
5156 asyncRequest(request, (AsyncProcedure<T>) procedure);
5160 public <T> void asyncRequest(Read<T> request,
5161 final SyncListener<T> procedure) {
5162 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5166 public <T> void asyncRequest(Read<T> request, final Listener<T> procedure) {
5167 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5171 public <T> void asyncRequest(final Read<T> request, final AsyncProcedure<T> procedure) {
5173 assert (request != null);
5174 assert (procedure != null);
5176 AsyncBarrierImpl barrier = asyncBarrier;
5180 processor.scheduleNow(new SessionTask(this) {
5183 public void run0(int thread) {
5185 final ListenerBase listener = getListenerBase(procedure);
5186 QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5187 } catch (DatabaseException e) {
5188 Logger.defaultLogError(e);
5199 public static ReadGraphImpl createAsync(QueryProcessor support) {
5200 return new ReadGraphImpl(null, null, support);
5204 public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
5205 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5209 public <T> void asyncRequest(Read<T> request, final Procedure<T> procedure) {
5210 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5214 final public <T> void asyncRequest(final AsyncRead<T> request) {
5216 assert (request != null);
5218 asyncRequest(request, new AsyncProcedure<T>() {
5221 public void execute(AsyncReadGraph graph, T result) {
5225 public void exception(AsyncReadGraph graph, Throwable t) {
5226 Logger.defaultLogError(t);
5230 public String toString() {
5231 return "asyncRequest(AsyncRead) -> " + request;
5239 public <T> void asyncRequest(AsyncRead<T> request,
5240 AsyncListener<T> procedure) {
5241 asyncRequest(request, (AsyncProcedure<T>) procedure);
5245 final public <T> void asyncRequest(AsyncRead<T> request,
5246 final SyncListener<T> procedure) {
5247 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5251 final public <T> void asyncRequest(AsyncRead<T> request,
5252 final Listener<T> procedure) {
5253 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5257 final public <T> void asyncRequest(final AsyncRead<T> request,
5258 final AsyncProcedure<T> procedure) {
5260 assert (request != null);
5261 assert (procedure != null);
5263 AsyncBarrierImpl barrier = asyncBarrier;
5267 processor.scheduleNow(new SessionTask(this) {
5270 public void run0(int thread) {
5276 final ListenerBase listener = getListenerBase(procedure);
5277 QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, new AsyncProcedure<T>() {
5280 public void execute(AsyncReadGraph graph, T result) {
5281 procedure.execute(graph, result);
5287 public void exception(AsyncReadGraph graph, Throwable throwable) {
5288 procedure.exception(graph, throwable);
5294 } catch (DatabaseException e) {
5295 LOGGER.error("Error while executing async request", e);
5307 public <T> void asyncRequest(AsyncRead<T> request,
5308 SyncProcedure<T> procedure) {
5309 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5313 final public <T> void asyncRequest(final AsyncRead<T> request,
5314 final Procedure<T> procedure) {
5315 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5319 public <T> void asyncRequest(final MultiRead<T> request) {
5321 assert (request != null);
5323 asyncRequest(request, new SyncMultiProcedureAdapter<T>() {
5325 public void exception(ReadGraph graph, Throwable t) {
5326 Logger.defaultLogError(t);
5330 public String toString() {
5331 return "asyncRequest(MultiRead) -> " + request;
5338 public <T> void asyncRequest(MultiRead<T> request,
5339 SyncMultiListener<T> procedure) {
5340 asyncRequest(request, (SyncMultiProcedure<T>)procedure);
5344 public <T> void asyncRequest(MultiRead<T> request,
5345 MultiListener<T> procedure) {
5346 asyncRequest(request, new NoneToSyncMultiListener<T>(procedure));
5351 public <T> void asyncRequest(final MultiRead<T> request,
5352 final SyncMultiProcedure<T> procedure) {
5354 assert (request != null);
5355 assert (procedure != null);
5357 final ListenerBase listener = getListenerBase(procedure);
5359 if (parent != null || listener != null) {
5361 // final ReadGraphImpl newGraph = newSync();
5362 processor.query(this, request, parent, procedure,listener);
5366 // final ReadGraphImpl newGraph = newSync();
5370 request.perform(this, procedure);
5372 } catch (Throwable t) {
5375 procedure.exception(this, t);
5376 } catch (DatabaseException e) {
5377 LOGGER.error("Unexpected exception while handling exception", e);
5387 public <T> void asyncRequest(MultiRead<T> request,
5388 MultiProcedure<T> procedure) {
5389 asyncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
5393 final public <T> void asyncRequest(final AsyncMultiRead<T> request) {
5395 assert (request != null);
5397 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5399 public void exception(AsyncReadGraph graph, Throwable t) {
5400 Logger.defaultLogError(t);
5404 public String toString() {
5405 return "asyncRequest(AsyncMultiRead) -> " + request;
5412 public <T> void asyncRequest(AsyncMultiRead<T> request,
5413 AsyncMultiListener<T> procedure) {
5414 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5418 public <T> void asyncRequest(AsyncMultiRead<T> request,
5419 SyncMultiListener<T> procedure) {
5420 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5424 public <T> void asyncRequest(AsyncMultiRead<T> request,
5425 MultiListener<T> procedure) {
5426 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5430 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5431 final AsyncMultiProcedure<T> procedure) {
5433 assert (request != null);
5434 assert (procedure != null);
5436 ListenerBase listener = getListenerBase(procedure);
5438 if (parent != null || listener != null) {
5440 processor.query(this, request, parent, procedure, listener);
5446 request.perform(this, new AsyncMultiProcedure<T>() {
5449 public void execute(AsyncReadGraph graph, T result) {
5450 procedure.execute(graph, result);
5454 public void finished(AsyncReadGraph graph) {
5455 procedure.finished(graph);
5459 public void exception(AsyncReadGraph graph, Throwable t) {
5460 procedure.exception(graph, t);
5464 public String toString() {
5465 return "asyncRequest(AsyncMultiRead) -> " + procedure;
5470 } catch (Throwable t) {
5472 procedure.exception(this, new DatabaseException(t));
5480 public <T> void asyncRequest(AsyncMultiRead<T> request,
5481 SyncMultiProcedure<T> procedure) {
5482 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5486 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5487 final MultiProcedure<T> procedure) {
5488 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5492 final public <T> void asyncRequest(final ExternalRead<T> request) {
5494 assert (request != null);
5496 asyncRequest(request, new Procedure<T>() {
5499 public void execute(T result) {
5503 public void exception(Throwable t) {
5504 Logger.defaultLogError(t);
5508 public String toString() {
5509 return "asyncRequest(PrimitiveRead) -> " + request;
5517 public <T> void asyncRequest(ExternalRead<T> request,
5518 final Listener<T> procedure) {
5519 asyncRequest(request, (Procedure<T>) procedure);
5523 final public <T> void asyncRequest(final ExternalRead<T> request,
5524 final Procedure<T> procedure) {
5526 assert (request != null);
5527 assert (procedure != null);
5529 final ListenerBase listener = getListenerBase(procedure);
5531 if (parent != null || listener != null) {
5534 QueryCacheBase.resultExternalReadEntry(this, request, parent, listener, procedure);
5535 } catch (DatabaseException e) {
5536 Logger.defaultLogError(e);
5537 // This throwable has already been transferred to procedure at this point - do nothing about it
5542 request.register(this, new Listener<T>() {
5544 public void execute(T result) {
5545 procedure.execute(result);
5549 public void exception(Throwable t) {
5550 procedure.exception(t);
5554 public String toString() {
5555 return "asyncRequest(PrimitiveRead) -> " + request;
5559 public boolean isDisposed() {
5570 public void asyncRequest(final Write request) {
5572 assert (request != null);
5574 getSession().asyncRequest(request);
5576 // processor.asyncWrite(request);
5581 public <T> void asyncRequest(final WriteResult<T> request, Procedure<T> procedure) {
5582 throw new Error("Not implemented.");
5586 public void asyncRequest(Write request, Consumer<DatabaseException> callback) {
5588 assert (request != null);
5590 getSession().asyncRequest(request, callback);
5595 public void asyncRequest(final DelayedWrite request) {
5597 assert (request != null);
5599 getSession().asyncRequest(request);
5604 public <T> void asyncRequest(final DelayedWriteResult<T> request, Procedure<T> procedure) {
5605 throw new Error("Not implemented.");
5609 public void asyncRequest(DelayedWrite r,
5610 Consumer<DatabaseException> callback) {
5611 throw new Error("Not implemented.");
5615 public void asyncRequest(final WriteOnly request) {
5617 assert (request != null);
5619 getSession().asyncRequest(request);
5624 public <T> void asyncRequest(final WriteOnlyResult<T> request, Procedure<T> procedure) {
5625 throw new Error("Not implemented.");
5629 public void asyncRequest(WriteOnly r, Consumer<DatabaseException> callback) {
5630 throw new Error("Not implemented.");
5634 * Implementation of the interface ServiceLocator
5638 public <T> T getService(Class<T> api) {
5639 if(WriteSupport.class == api) {
5640 if(this instanceof WriteGraphImpl) {
5641 WriteGraphImpl impl = (WriteGraphImpl)this;
5642 return (T)impl.writeSupport;
5645 return getSession().getService(api);
5649 public <T> T peekService(Class<T> api) {
5650 return getSession().peekService(api);
5654 public boolean hasService(Class<?> api) {
5655 return getSession().hasService(api);
5659 public <T> void registerService(Class<T> api, T service) {
5660 getSession().registerService(api, service);
5664 public boolean isImmutable(Resource resource) throws DatabaseException {
5665 ResourceImpl impl = (ResourceImpl)resource;
5666 return processor.isImmutable(impl.id);
5673 protected static String INTERNAL_ERROR_STRING = "Transaction aborted due to internal client error.";
5676 * callerThread is the currently running thread state.syncThread is blocking for
5677 * this execution state.syncParent is the blocking request
5680 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support) {
5681 this.parentGraph = parentGraph;
5682 this.parent = parent;
5683 this.processor = support;
5684 this.asyncBarrier = prepareBarrier(parentGraph, parent, null, false);
5687 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support, AsyncBarrierImpl asyncBarrier) {
5688 this.parentGraph = parentGraph;
5689 this.parent = parent;
5690 this.processor = support;
5691 this.asyncBarrier = asyncBarrier;
5694 ReadGraphImpl(ReadGraphImpl graph, CacheEntry parent) {
5695 this(graph, parent, graph.processor);
5698 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, Runnable callback, boolean needsToBlock) {
5699 this(parentGraph, parent, parentGraph.processor, prepareBarrier(parentGraph, parent, callback, needsToBlock));
5702 static AsyncBarrierImpl prepareBarrier(ReadGraphImpl parentGraph, CacheEntry parent, Runnable callback, boolean needsToBlock) {
5703 return new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent, callback, needsToBlock);
5706 ReadGraphImpl(ReadGraphImpl graph) {
5707 this(graph, graph.parent);
5710 public ReadGraphImpl withParent(CacheEntry parent, Runnable callback, boolean needsToBlock) {
5711 return new ReadGraphImpl(this, parent, callback, needsToBlock);
5714 public ReadGraphImpl forRecompute(CacheEntry parent) {
5715 return new ReadGraphImpl(null, parent, processor);
5718 public static ReadGraphImpl create(QueryProcessor support) {
5719 ReadGraphImpl result = new ReadGraphImpl(null, null, support);
5723 public ReadGraphImpl newRestart(ReadGraphImpl impl) {
5725 WriteGraphImpl write = processor.getSession().getService(
5726 WriteGraphImpl.class);
5732 final private ListenerBase getListenerBase(final Object procedure) {
5733 if (procedure instanceof ListenerBase)
5734 return (ListenerBase) procedure;
5739 public <T> void waitAsyncProcedure(AsyncMultiReadProcedure<T> procedure) {
5741 assert(procedure.done());
5745 public <T> void waitAsyncProcedure(AsyncReadProcedure<T> procedure) {
5747 assert(procedure.done());
5751 public boolean resumeTasks() {
5752 return processor.resumeTasks(this);
5755 Class<?> singleClass(Set<Resource> types) {
5756 Class<?> result = null;
5757 for (Resource type : types) {
5758 Class<?> clazz = processor.getBuiltinValue(type);
5759 if (clazz != null) {
5769 private String debugString(Resource r) {
5772 name = getPossibleRelatedValue(r, processor.getL0(this).HasName);
5773 } catch (ManyObjectsForFunctionalRelationException e) {
5774 Logger.defaultLogError(e);
5775 } catch (ServiceException e) {
5776 Logger.defaultLogError(e);
5778 return "[" + name + " - " + r + "]";
5782 public String toString() {
5783 return "ReadGraphImpl[thread=" + Thread.currentThread() + "]";
5787 final public int thread() {
5791 static class MultiTripleIntProcedure implements TripleIntProcedure {
5793 final private AsyncMultiProcedure<Statement> procedure;
5794 final private ReadGraphImpl impl;
5795 final private QuerySupport support;
5797 public MultiTripleIntProcedure(AsyncMultiProcedure<Statement> procedure, ReadGraphImpl impl, QuerySupport support) {
5798 this.procedure = procedure;
5800 this.support = support;
5804 public void execute(ReadGraphImpl graph, int s, int p, int o) {
5806 procedure.execute(graph, support.getStatement(s, p, o));
5807 } catch (Throwable t2) {
5808 Logger.defaultLogError(t2);
5813 public void finished(ReadGraphImpl graph) {
5815 procedure.finished(graph);
5816 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5817 } catch (Throwable t2) {
5818 Logger.defaultLogError(t2);
5823 public void exception(ReadGraphImpl graph, Throwable t) {
5825 procedure.exception(graph, t);
5826 } catch (Throwable t2) {
5827 Logger.defaultLogError(t2);
5829 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5833 public String toString() {
5834 return "forEachObject with " + procedure;
5839 // private AsyncMultiProcedure<Resource> cacheKey = null;
5840 // private MultiIntProcedure cacheResult = null;
5842 // final IntProcedure forMultiProcedure(final AsyncMultiProcedure<Resource> procedure) {
5844 // if(procedure == cacheKey) return cacheResult;
5846 // cacheResult = new MultiIntProcedure(procedure, this, processor.support);
5847 // cacheKey = procedure;
5849 // return cacheResult;
5853 // private AsyncMultiProcedure<Statement> cacheKey2 = null;
5854 // private MultiTripleIntProcedure cacheResult2 = null;
5856 // final synchronized TripleIntProcedure forMultiProcedure(final AsyncMultiProcedure<Statement> procedure) {
5858 // if(procedure == cacheKey2) return cacheResult2;
5860 // cacheResult2 = new MultiTripleIntProcedure(procedure, this, processor.support);
5861 // cacheKey2 = procedure;
5863 // return cacheResult2;
5868 public Datatype getDataType(Resource subject) throws DatabaseException {
5869 for(Resource dataTypeResource : getObjects(subject, processor.getL0(this).HasDataType))
5870 return getValue(dataTypeResource, Bindings.getBindingUnchecked(Datatype.class));
5871 throw new DoesNotContainValueException("The literal has no data type.");
5874 protected <T extends Accessor> T getAccessor4File(Resource subject)
5875 throws DatabaseException {
5878 byte[] bytes = processor.support.getValue(g, subject);
5882 BinaryVariant va = (BinaryVariant) Accessors.getAccessor(bytes, Datatypes.VARIANT);
5883 Accessor ca = va.getContentAccessor();
5885 } catch (AccessorConstructionException e) {
5886 throw new DatabaseException(e);
5892 Binding datatype_binding = Bindings.getBindingUnchecked(DataType.class);
5893 Serializer datatype_serializer = datatype_binding.serializer();
5896 BinaryMemory in = new BinaryMemory(ByteBuffer.wrap(bytes));
5897 datatype = (DataType)datatype_serializer.deserialize(in);
5898 Binding data_binding = Bindings.getBinding(datatype);
5899 Serializer data_serializer = data_binding.serializer(BinarySerializationFormat.INSTANCE);
5900 Object o = data_serializer.deserialize(in);
5902 return (T)Accessors.getAccessor(data_binding, o);
5903 } catch(AccessorConstructionException e) {
5906 } catch (Exception e) {
5907 throw new DatabaseException(e);
5910 @SuppressWarnings("unchecked")
5912 public <T extends Accessor> T getAccessor(Resource subject) throws DatabaseException {
5913 RandomAccessBinary rab = getRandomAccessBinary(subject);
5915 return (T)Accessors.getAccessor(rab, getDataType(subject));
5916 } catch(AccessorConstructionException e) {
5917 throw new DatabaseException(e);
5920 @SuppressWarnings("unchecked")
5921 protected <T extends Accessor> T createAccessor(Resource resource, Datatype datatype, Object intialValue)
5922 throws DatabaseException {
5923 RandomAccessBinary rab = createRandomAccessBinary(resource, datatype, intialValue);
5925 return (T)Accessors.getAccessor(rab, datatype);
5926 } catch(AccessorConstructionException e) {
5927 throw new DatabaseException(e);
5931 public RandomAccessBinary getRandomAccessBinary(Resource subject) throws DatabaseException {
5932 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5933 ResourceData rd = ravs.get(subject);
5937 ExternalValueSupport evs = getService(ExternalValueSupport.class);
5938 long size = evs.getValueSize(this, subject); // Throws DatabaseException if no old external value.
5940 File platform = Platform.getLocation().toFile();
5941 File tempFiles = new File(platform, "tempFiles");
5942 File dbDir = new File(tempFiles, "db");
5944 File file = new File(dbDir, "ResourceFile" + subject.getResourceId());
5945 rd = new ResourceData(new BinaryFile(file), true); // Old external value.
5946 final int N = 1<<20;
5950 int length = N < left ? N : (int)left;
5951 byte[] bytes = evs.readValue(this, subject, offset, length);
5952 offset += bytes.length;
5953 left -= bytes.length;
5954 rd.binaryFile.write(bytes);
5956 ravs.put(subject, rd);
5958 } catch (Exception e) {
5959 throw new DatabaseException("Resource " + subject + " have value but there was problem with accessing value data.", e);
5961 } catch (Exception e) {
5962 if(Development.DEVELOPMENT) {
5963 if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
5964 e.printStackTrace();
5968 Datatype datatype = getDataType(subject);
5969 Object value = getPossibleValue(subject, Bindings.getBinding(datatype));
5970 return createRandomAccessBinary(subject, datatype, value);
5972 public RandomAccessBinary createRandomAccessBinary(Resource resource, Datatype datatype, Object initialValue)
5973 throws DatabaseException {
5974 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5976 File platform = Platform.getLocation().toFile();
5977 File tempFiles = new File(platform, "tempFiles");
5978 File dbDir = new File(tempFiles, "db");
5980 File file = new File(dbDir, "ResourceFile" + resource.getResourceId());
5981 ResourceData rd = new ResourceData(new BinaryFile(file), false);
5982 Binding binding = Bindings.getBinding(datatype);
5983 if (null == initialValue) {
5984 initialValue = binding.createDefault();
5986 Serializer serializer = binding.serializer();
5987 byte[] bytes = serializer.serialize(initialValue);
5988 // In case the file has been previously accessed and was larger we set the correct size now
5989 rd.binaryFile.setLength(bytes.length);
5990 rd.binaryFile.write(bytes);
5991 ravs.put(resource, rd);
5993 } catch (Exception e) {
5994 if (e instanceof DatabaseException)
5995 throw (DatabaseException)e;
5997 throw new DatabaseException(e);
6001 // static class ExternalValueRequest<T> extends ResourceRead<T> {
6003 // public ExternalValueRequest(Resource resource) {
6007 // @SuppressWarnings("unchecked")
6009 // public T perform(ReadGraph graph) throws DatabaseException {
6012 // String uri = graph.getURI(resource);
6013 // if(Layer0.URIs.Functions_functionApplication.equals(uri)) return (T)functionApplication;
6015 // return (T)ReflectionUtils.getValue(uri).getValue();
6017 // } catch(ValueNotFoundException e) {
6018 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
6019 // } catch(ClassCastException e) {
6020 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
6026 @SuppressWarnings("unchecked")
6028 public <T> T getValue2(Resource r, Object context) throws DatabaseException {
6029 Layer0 L0 = processor.getL0(this);
6030 Set<Resource> types = getTypes(r);
6032 if(types.contains(L0.Literal)) {
6033 if(isImmutable(r)) {
6034 return syncRequest(new ValueImplied<T>(r));
6039 else if(types.contains(L0.ExternalValue)) {
6040 return (T)syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6044 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6045 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6047 return function.apply(this, r, context);
6048 } catch(RuntimeException e) {
6049 DatabaseException dte = findPossibleRootException(e);
6050 if(dte != null) throw dte;
6051 else throw new DatabaseException(e);
6058 public Variant getVariantValue2(Resource r, Object context) throws DatabaseException {
6059 Layer0 L0 = processor.getL0(this);
6060 Set<Resource> types = getTypes(r);
6062 if(types.contains(L0.Literal)) {
6063 if(isImmutable(r)) {
6064 return syncRequest(new VariantValueImplied(r));
6066 return getVariantValue(r);
6069 else if(types.contains(L0.ExternalValue)) {
6070 Object value = syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6072 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6073 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6074 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6079 Function3<ReadGraph,Resource,Object,Object> function = requestValueFunction(r);
6080 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6082 Object value = function.apply(this, r, context);
6084 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6085 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6086 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6088 } catch(RuntimeException e) {
6089 DatabaseException dte = findPossibleRootException(e);
6090 if(dte != null) throw dte;
6091 else throw new DatabaseException(e);
6097 public <T> T getPossibleValue2(Resource subject, Object context) throws DatabaseException {
6099 return getValue2(subject, context);
6100 } catch (DatabaseException e) {
6105 static class PossibleConverterFunction<T> extends ResourceRead<Function3<ReadGraph,Resource,Object,T>> {
6107 public PossibleConverterFunction(Resource resource) {
6112 public Function3<ReadGraph,Resource,Object,T> perform(ReadGraph graph) throws DatabaseException {
6113 return compute(graph, resource);
6116 @SuppressWarnings("unchecked")
6117 public static <T> Function3<ReadGraph,Resource,Object,T> compute(ReadGraph graph, Resource resource) throws DatabaseException {
6118 Layer0 L0 = Layer0.getInstance(graph);
6119 for(Resource converter : graph.getObjects(resource, L0.ConvertsToValueWith)) {
6121 if(L0.Functions_functionApplication.equals(converter)) {
6122 return (Function3<ReadGraph,Resource,Object,T>)graph.syncRequest(new AdaptValue(resource));
6124 return graph.getValue2(converter, resource);
6126 } catch(RuntimeException e) {
6127 DatabaseException dte = findPossibleRootException(e);
6128 if(dte != null) throw dte;
6129 else throw new DatabaseException(e);
6137 <T> Function3<ReadGraph,Resource,Object,T> requestValueFunction(Resource r) throws DatabaseException {
6139 return syncRequest(new PossibleConverterFunction<T>(r), TransientCacheAsyncListener.<Function3<ReadGraph,Resource,Object,T>>instance());
6141 return syncRequest(new PossibleConverterFunction<T>(r));
6145 * Get a value associated with a graph {@link Resource}, using a possible context object and
6146 * a desired value binding. The following methods are tried in order to retreive the value:
6148 * <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>
6149 * <li>If the resource is a {@code L0.ExternalValue}, the value is acquired using
6150 * {@link ReflectionUtils#getValue(String)}.</li>
6151 * <li>If the resource is associated with a suitable value converter with relation {@code L0.ConvertsToValueWith}
6152 * (see {@link #requestValueFunction(Resource)}), the value function is called with the graph, the resource
6153 * and the context object.</li>
6156 * @param r A graph resource with which the value is associated
6157 * @param context A context object that is used for acquiring the value (only applied in case 3)
6158 * @param binding A binding for the value type (only applied in case 1)
6159 * @return The value of the graph node.
6160 * @throws DoesNotContainValueException No value is associated with the graph node.
6161 * @throws DatabaseException Other errors, such as an error in casting the value to the return type or
6162 * a runtime error in the value function.
6164 @SuppressWarnings("unchecked")
6166 public <T> T getValue2(Resource r, Object context, Binding binding) throws DatabaseException {
6167 if (binding instanceof ObjectVariantBinding)
6168 return getValue2(r, context);
6170 Layer0 L0 = processor.getL0(this);
6171 Set<Resource> types = getTypes(r);
6172 if(types.contains(L0.Literal)) {
6173 if(isImmutable(r)) {
6174 return syncRequest(new Value<T>(r, binding));
6176 return getValue(r, binding);
6178 } else if(types.contains(L0.ExternalValue)) {
6179 ComputationalValue cv = syncRequest(new PossibleAdapter<ComputationalValue>(r, ComputationalValue.class), TransientCacheAsyncListener.instance());
6181 return cv.getValue(this, r);
6183 // This should not even be possible since L0 defines an adapter for all values
6185 return (T)ReflectionUtils.getValue(getURI(r)).getValue();
6186 } catch(ValueNotFoundException e) {
6187 throw new DatabaseException(e);
6188 } catch(ClassCastException e) {
6189 throw new DatabaseException(e);
6194 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6195 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function.");
6197 Object value = function.apply(this, r, context);
6198 if(binding.isInstance(value)) return (T)value;
6199 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
6200 return (T)Bindings.adapt(value, srcBinding, binding);
6201 } catch(RuntimeException e) {
6202 DatabaseException dte = findPossibleRootException(e);
6203 if(dte != null) throw dte;
6204 else throw new DatabaseException(e);
6205 } catch (AdaptException e) {
6206 throw new DatabaseException(e);
6207 } catch (org.simantics.databoard.binding.error.BindingException e) {
6208 throw new DatabaseException(e);
6214 public <T> T getPossibleValue2(Resource subject, Object context, Binding binding) throws DatabaseException {
6216 return getValue2(subject, context, binding);
6217 } catch (DatabaseException e) {
6222 private static DatabaseException findPossibleRootException(Throwable t) {
6223 if(t == null) return null;
6224 if(t instanceof DatabaseException) return (DatabaseException)t;
6225 if(t instanceof RuntimeException || t instanceof InvocationTargetException) {
6226 return findPossibleRootException(t.getCause());
6232 public <T> T getRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6233 return getRelatedValue2(subject, relation, subject);
6237 public Variant getRelatedVariantValue2(Resource subject, Resource relation) throws DatabaseException {
6238 return getRelatedVariantValue2(subject, relation, subject);
6242 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6244 Resource object = getPossibleObject(subject, relation);
6245 if(object == null) return null;
6246 else return getValue2(object, subject);
6247 } catch (DatabaseException e) {
6253 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6254 if(Development.DEVELOPMENT) {
6255 if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
6256 String error = L0Validations.checkValueType(this, subject, relation);
6258 Logger.defaultLogError(new ValidationException(error));
6259 throw new ValidationException(error);
6263 return getValue2(getSingleObject(subject, relation), context);
6267 public Variant getRelatedVariantValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6268 if(Development.DEVELOPMENT) {
6269 if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
6270 String error = L0Validations.checkValueType(this, subject, relation);
6272 Logger.defaultLogError(new ValidationException(error));
6273 throw new ValidationException(error);
6277 return getVariantValue2(getSingleObject(subject, relation), context);
6281 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6283 Resource object = getPossibleObject(subject, relation);
6284 if(object == null) return null;
6285 else return getValue2(object, context);
6286 } catch (DatabaseException e) {
6292 public <T> T getRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6293 return getRelatedValue2(subject, relation, subject, binding);
6297 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6299 Resource object = getPossibleObject(subject, relation);
6300 if(object == null) return null;
6301 return getValue2(object, subject, binding);
6302 } catch (DatabaseException e) {
6308 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6309 return getValue2(getSingleObject(subject, relation), context, binding);
6313 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6315 Resource object = getPossibleObject(subject, relation);
6316 if(object == null) return null;
6317 else return getValue2(object, context, binding);
6318 } catch (DatabaseException e) {
6324 public Type getRelatedValueType(Resource subject, Resource relation) throws DatabaseException {
6325 Layer0 L0 = processor.getL0(this);
6326 Resource property = getSingleObject(subject, relation);
6327 String typeText = (String)getRelatedValue(property, L0.HasValueType, Bindings.STRING);
6329 return org.simantics.scl.compiler.types.Types.parseType(typeText);
6330 } catch (SCLTypeParseException e) {
6331 throw new DatabaseException(e);
6335 private static ThreadLocal<Boolean> syncGraph = new ThreadLocal<Boolean>() {
6336 protected Boolean initialValue() {
6342 public boolean setSynchronous(boolean value) {
6343 boolean old = getSynchronous();
6344 syncGraph.set(value);
6349 public boolean getSynchronous() {
6350 return syncGraph.get();
6353 public void ensureLoaded(int resource) {
6354 processor.querySupport.ensureLoaded(this, resource);
6357 public void ensureLoaded(int resource, int predicate) {
6358 processor.querySupport.ensureLoaded(this, resource, predicate);
6361 public byte[] getValue(int resource) {
6362 return processor.querySupport.getValue(this, resource);
6365 public int thread(int resource) {
6366 return (resource >>> 16) & processor.THREAD_MASK;
6369 public int thread(Resource resource) {
6370 return (((ResourceImpl)resource).id >>> 16) & processor.THREAD_MASK;
6373 public ResourceSupport getResourceSupport() {
6374 return processor.getResourceSupport();
6378 public Object getModificationCounter() {
6379 return processor.getSession().getModificationCounter();
6383 public boolean performPending() {
6384 return processor.performPending(this);
6387 public Set<ReadGraphImpl> ancestorSet() {
6388 HashSet<ReadGraphImpl> result = new HashSet<>();
6389 ReadGraphImpl g = this;
6397 public int getLevel() {
6398 return getLevelStatic(this);
6401 private static int getLevelStatic(ReadGraphImpl impl) {
6402 if(impl == null) return 0;
6403 else return 1 + getLevelStatic(impl.parentGraph);
6406 public boolean isParent(ReadGraphImpl impl) {
6407 if(impl == null) return false;
6408 if(this == impl) return true;
6409 return isParent(impl.parentGraph);
6412 public ReadGraphImpl getTopLevelGraph() {
6413 return getTopLevelGraphStatic(this);
6416 private static ReadGraphImpl getTopLevelGraphStatic(ReadGraphImpl impl) {
6417 if(impl.parentGraph == null) return impl;
6418 else return getTopLevelGraphStatic(impl.parentGraph);
6421 @SuppressWarnings("unchecked")
6424 return (T) processor.getL0();