1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl.graph;
14 import java.io.BufferedOutputStream;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.io.PrintStream;
19 import java.lang.reflect.Array;
20 import java.lang.reflect.InvocationTargetException;
21 import java.nio.BufferUnderflowException;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.IdentityHashMap;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.ListIterator;
32 import java.util.function.Consumer;
34 import org.eclipse.core.runtime.Platform;
35 import org.simantics.databoard.Accessors;
36 import org.simantics.databoard.Bindings;
37 import org.simantics.databoard.accessor.Accessor;
38 import org.simantics.databoard.accessor.error.AccessorConstructionException;
39 import org.simantics.databoard.adapter.AdaptException;
40 import org.simantics.databoard.binding.Binding;
41 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;
42 import org.simantics.databoard.binding.impl.ObjectVariantBinding;
43 import org.simantics.databoard.binding.mutable.Variant;
44 import org.simantics.databoard.serialization.Serializer;
45 import org.simantics.databoard.type.Datatype;
46 import org.simantics.databoard.util.binary.BinaryFile;
47 import org.simantics.databoard.util.binary.RandomAccessBinary;
48 import org.simantics.db.AsyncReadGraph;
49 import org.simantics.db.DevelopmentKeys;
50 import org.simantics.db.ExternalValueSupport;
51 import org.simantics.db.ReadGraph;
52 import org.simantics.db.RelationContext;
53 import org.simantics.db.Resource;
54 import org.simantics.db.Session;
55 import org.simantics.db.Statement;
56 import org.simantics.db.adaption.AdaptionService;
57 import org.simantics.db.common.primitiverequest.Adapter;
58 import org.simantics.db.common.primitiverequest.Builtin;
59 import org.simantics.db.common.primitiverequest.DatatypeBinding;
60 import org.simantics.db.common.primitiverequest.ForEachAssertedObject;
61 import org.simantics.db.common.primitiverequest.ForEachAssertedStatement;
62 import org.simantics.db.common.primitiverequest.HasStatement;
63 import org.simantics.db.common.primitiverequest.HasStatementSubject;
64 import org.simantics.db.common.primitiverequest.HasStatementSubjectObject;
65 import org.simantics.db.common.primitiverequest.HasValue;
66 import org.simantics.db.common.primitiverequest.Inverse;
67 import org.simantics.db.common.primitiverequest.IsInheritedFrom;
68 import org.simantics.db.common.primitiverequest.IsInstanceOf;
69 import org.simantics.db.common.primitiverequest.IsSubrelationOf;
70 import org.simantics.db.common.primitiverequest.OrderedSet;
71 import org.simantics.db.common.primitiverequest.PossibleAdapter;
72 import org.simantics.db.common.primitiverequest.PossibleInverse;
73 import org.simantics.db.common.primitiverequest.PossibleObject;
74 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;
75 import org.simantics.db.common.primitiverequest.PossibleRelatedValueImplied;
76 import org.simantics.db.common.primitiverequest.PossibleStatement;
77 import org.simantics.db.common.primitiverequest.PossibleType;
78 import org.simantics.db.common.primitiverequest.PossibleUniqueAdapter;
79 import org.simantics.db.common.primitiverequest.PossibleValue;
80 import org.simantics.db.common.primitiverequest.PossibleValueImplied;
81 import org.simantics.db.common.primitiverequest.RelatedValue;
82 import org.simantics.db.common.primitiverequest.RelatedValueImplied;
83 import org.simantics.db.common.primitiverequest.SingleObject;
84 import org.simantics.db.common.primitiverequest.SingleStatement;
85 import org.simantics.db.common.primitiverequest.SingleType;
86 import org.simantics.db.common.primitiverequest.SingleTypeAny;
87 import org.simantics.db.common.primitiverequest.Types;
88 import org.simantics.db.common.primitiverequest.UniqueAdapter;
89 import org.simantics.db.common.primitiverequest.Value;
90 import org.simantics.db.common.primitiverequest.ValueImplied;
91 import org.simantics.db.common.primitiverequest.VariantValueImplied;
92 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
93 import org.simantics.db.common.procedure.adapter.AsyncProcedureAdapter;
94 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
95 import org.simantics.db.common.procedure.adapter.SyncMultiProcedureAdapter;
96 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
97 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
98 import org.simantics.db.common.procedure.single.SyncReadProcedure;
99 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrErrorProcedure;
100 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrNullProcedure;
101 import org.simantics.db.common.procedure.single.wrapper.ExceptionToNullProcedure;
102 import org.simantics.db.common.procedure.single.wrapper.NullSingleOrNullProcedure;
103 import org.simantics.db.common.procedure.single.wrapper.SingleFunctionalOrNullProcedure;
104 import org.simantics.db.common.procedure.single.wrapper.SingleOrErrorProcedure;
105 import org.simantics.db.common.procedure.single.wrapper.SingleOrNullProcedure;
106 import org.simantics.db.common.procedure.wrapper.NoneToAsyncListener;
107 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiListener;
108 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiProcedure;
109 import org.simantics.db.common.procedure.wrapper.NoneToAsyncProcedure;
110 import org.simantics.db.common.procedure.wrapper.NoneToAsyncSetProcedure;
111 import org.simantics.db.common.procedure.wrapper.NoneToSyncMultiListener;
112 import org.simantics.db.common.procedure.wrapper.NoneToSyncMultiProcedure;
113 import org.simantics.db.common.procedure.wrapper.SyncToAsyncListener;
114 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiListener;
115 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiProcedure;
116 import org.simantics.db.common.procedure.wrapper.SyncToAsyncProcedure;
117 import org.simantics.db.common.procedure.wrapper.SyncToAsyncSetProcedure;
118 import org.simantics.db.common.request.AdaptValue;
119 import org.simantics.db.common.request.ResourceRead;
120 import org.simantics.db.common.utils.Logger;
121 import org.simantics.db.common.utils.NameUtils;
122 import org.simantics.db.common.validation.L0Validations;
123 import org.simantics.db.exception.AdaptionException;
124 import org.simantics.db.exception.ArgumentException;
125 import org.simantics.db.exception.AssumptionException;
126 import org.simantics.db.exception.BindingException;
127 import org.simantics.db.exception.DatabaseException;
128 import org.simantics.db.exception.DoesNotContainValueException;
129 import org.simantics.db.exception.EmptyResourceException;
130 import org.simantics.db.exception.InternalException;
131 import org.simantics.db.exception.InvalidLiteralException;
132 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
133 import org.simantics.db.exception.NoInverseException;
134 import org.simantics.db.exception.NoSingleResultException;
135 import org.simantics.db.exception.ResourceNotFoundException;
136 import org.simantics.db.exception.ServiceException;
137 import org.simantics.db.exception.ValidationException;
138 import org.simantics.db.impl.BlockingAsyncProcedure;
139 import org.simantics.db.impl.RelationContextImpl;
140 import org.simantics.db.impl.ResourceImpl;
141 import org.simantics.db.impl.internal.RandomAccessValueSupport;
142 import org.simantics.db.impl.internal.ResourceData;
143 import org.simantics.db.impl.procedure.ResultCallWrappedSyncQueryProcedure;
144 import org.simantics.db.impl.query.CacheEntry;
145 import org.simantics.db.impl.query.QueryCache;
146 import org.simantics.db.impl.query.QueryCacheBase;
147 import org.simantics.db.impl.query.QueryProcessor;
148 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
149 import org.simantics.db.impl.query.QuerySupport;
150 import org.simantics.db.impl.query.TripleIntProcedure;
151 import org.simantics.db.impl.support.ResourceSupport;
152 import org.simantics.db.procedure.AsyncListener;
153 import org.simantics.db.procedure.AsyncMultiListener;
154 import org.simantics.db.procedure.AsyncMultiProcedure;
155 import org.simantics.db.procedure.AsyncProcedure;
156 import org.simantics.db.procedure.AsyncSetListener;
157 import org.simantics.db.procedure.Listener;
158 import org.simantics.db.procedure.ListenerBase;
159 import org.simantics.db.procedure.MultiListener;
160 import org.simantics.db.procedure.MultiProcedure;
161 import org.simantics.db.procedure.Procedure;
162 import org.simantics.db.procedure.SetListener;
163 import org.simantics.db.procedure.StatementProcedure;
164 import org.simantics.db.procedure.SyncListener;
165 import org.simantics.db.procedure.SyncMultiListener;
166 import org.simantics.db.procedure.SyncMultiProcedure;
167 import org.simantics.db.procedure.SyncProcedure;
168 import org.simantics.db.procedure.SyncSetListener;
169 import org.simantics.db.request.AsyncMultiRead;
170 import org.simantics.db.request.AsyncRead;
171 import org.simantics.db.request.DelayedWrite;
172 import org.simantics.db.request.DelayedWriteResult;
173 import org.simantics.db.request.ExternalRead;
174 import org.simantics.db.request.MultiRead;
175 import org.simantics.db.request.Read;
176 import org.simantics.db.request.ReadInterface;
177 import org.simantics.db.request.Write;
178 import org.simantics.db.request.WriteInterface;
179 import org.simantics.db.request.WriteOnly;
180 import org.simantics.db.request.WriteOnlyResult;
181 import org.simantics.db.request.WriteResult;
182 import org.simantics.layer0.Layer0;
183 import org.simantics.scl.compiler.types.Type;
184 import org.simantics.scl.compiler.types.exceptions.SCLTypeParseException;
185 import org.simantics.scl.reflection.ReflectionUtils;
186 import org.simantics.scl.reflection.ValueNotFoundException;
187 import org.simantics.scl.runtime.function.Function3;
188 import org.simantics.utils.DataContainer;
189 import org.simantics.utils.Development;
190 import org.simantics.utils.datastructures.Pair;
191 import org.simantics.utils.datastructures.collections.CollectionUtils;
192 import org.slf4j.LoggerFactory;
194 import gnu.trove.map.hash.TObjectIntHashMap;
196 public class ReadGraphImpl implements AsyncReadGraph {
198 private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ReadGraphImpl.class);
200 final static boolean EMPTY_RESOURCE_CHECK = false;
202 final public CacheEntry parent;
203 public final ReadGraphImpl parentGraph;
204 final public QueryProcessor processor;
206 public final AsyncBarrierImpl asyncBarrier;
208 final static Binding DATA_TYPE_BINDING_INTERNAL = Bindings.getBindingUnchecked(Datatype.class);
209 final static Serializer DATA_TYPE_SERIALIZER = Bindings.getSerializerUnchecked(DATA_TYPE_BINDING_INTERNAL);
211 final public static TObjectIntHashMap<String> counters = new TObjectIntHashMap<String>();
213 public static void resetCounters() {
217 public static String listCounters(File file) throws IOException {
219 PrintStream b = new PrintStream(new BufferedOutputStream(new FileOutputStream(file)));
221 for(Pair<String,Integer> p : CollectionUtils.valueSortedEntries(counters)) {
222 b.print(-p.second + " " + p.first + "\n");
227 return "Dumped " + counters.size() + " queries.";
232 * Implementation of the interface ReadGraph
234 final public String getURI(final Resource resource) throws AssumptionException, ValidationException, ServiceException {
236 assert (resource != null);
240 return syncRequest(new org.simantics.db.common.uri.ResourceToURI(resource));
242 } catch (AssumptionException e) {
244 throw new AssumptionException(e);
246 } catch (ValidationException e) {
248 throw new ValidationException(e);
250 } catch (ServiceException e) {
252 throw new ServiceException(e);
254 } catch (DatabaseException e) {
256 throw new ServiceException(INTERNAL_ERROR_STRING, e);
262 final public String getPossibleURI(final Resource resource) throws ValidationException, ServiceException {
264 assert (resource != null);
268 return syncRequest(new org.simantics.db.common.uri.ResourceToPossibleURI(resource));
270 } catch (ValidationException e) {
272 throw new ValidationException(e);
274 } catch (ServiceException e) {
276 throw new ServiceException(e);
278 } catch (DatabaseException e) {
280 throw new ServiceException(INTERNAL_ERROR_STRING, e);
286 final public Resource getResource(final String id)
287 throws ResourceNotFoundException, ValidationException,
294 Integer rid = QueryCache.resultURIToResource(this, id, parent, null);
295 // FIXME: stupid to throw this here and catch and wrap it right away
296 if(rid == 0) throw new ResourceNotFoundException(id);
297 return processor.querySupport.getResource(rid);
299 } catch (ResourceNotFoundException e) {
301 throw new ResourceNotFoundException(id, e);
303 } catch (ValidationException e) {
305 throw new ValidationException(e);
307 } catch (ServiceException e) {
309 throw new ServiceException(e);
311 } catch (DatabaseException e) {
313 throw new ServiceException(INTERNAL_ERROR_STRING, e);
319 final public Resource getPossibleResource(final String id)
320 throws ResourceNotFoundException, ValidationException,
327 return getResource(id);
329 } catch (ResourceNotFoundException e) {
333 } catch (ValidationException e) {
335 throw new ValidationException(e);
337 } catch (ServiceException e) {
339 throw new ServiceException(e);
341 } catch (DatabaseException e) {
343 throw new ServiceException(INTERNAL_ERROR_STRING, e);
350 public Map<String, Resource> getChildren(Resource resource) throws ValidationException, ServiceException {
352 assert (resource != null);
356 int rId = processor.querySupport.getId(resource);
357 return QueryCache.resultChildMap(this, rId, parent, null);
359 } catch (ValidationException e) {
361 throw new ValidationException(e);
363 } catch (ServiceException e) {
365 throw new ServiceException(e);
367 } catch (DatabaseException e) {
369 throw new ServiceException(INTERNAL_ERROR_STRING, e);
375 final public Resource getRootLibrary() {
376 return processor.getRootLibraryResource();
379 final public Resource getBuiltin(final String id)
380 throws ResourceNotFoundException, ServiceException {
386 return syncRequest(new Builtin(id));
388 } catch (ResourceNotFoundException e) {
390 throw new ResourceNotFoundException(id, e);
392 } catch (ServiceException e) {
394 throw new ServiceException(e);
396 } catch (DatabaseException e) {
398 throw new ServiceException(INTERNAL_ERROR_STRING, e);
404 static class StatementReadProcedure extends TIntArrayListInternal implements StatementProcedure {
406 private static Throwable DONE = new Throwable();
408 Throwable exception = null;
410 final ResourceSupport support;
412 public StatementReadProcedure(ResourceSupport support) {
413 this.support = support;
417 public synchronized void execute(AsyncReadGraph graph, int s, int p, int o) {
424 public void finished(AsyncReadGraph graph) {
429 public void exception(AsyncReadGraph graph, Throwable t) {
433 public void checkAndThrow() throws DatabaseException {
434 if(exception != DONE) {
435 if (exception instanceof DatabaseException)
436 throw (DatabaseException) exception;
438 throw new DatabaseException(
439 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
444 public boolean done() {
445 return exception != null;
449 public boolean contains(Object obj) {
450 if(!(obj instanceof InternalStatement))
452 InternalStatement statement = (InternalStatement)obj;
456 for(int i=0;i<sizeInternal();i+=3)
457 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
462 @SuppressWarnings("unchecked")
464 public <T> T[] toArray(T[] a) {
465 int length = sizeInternal() / 3;
466 if(length > a.length) {
467 Class<?> arrayType = a.getClass();
468 a = (arrayType == Object[].class)
469 ? (T[]) new Object[length]
470 : (T[]) Array.newInstance(arrayType.getComponentType(), length);
473 for(int i=length;i<a.length;++i)
476 for(int i=0,j=0;i<sizeInternal();i+=3,++j)
477 a[j] = (T)new InternalStatement(support, getQuick(i), getQuick(i+1), getQuick(i+2));
482 public boolean add(Statement e) {
483 throw new UnsupportedOperationException();
487 public boolean remove(Object o) {
488 throw new UnsupportedOperationException();
492 public boolean addAll(Collection<? extends Statement> c) {
493 throw new UnsupportedOperationException();
496 class IteratorImpl implements ListIterator<Statement> {
500 public IteratorImpl(int index) {
505 public boolean hasNext() {
506 return index < sizeInternal();
510 public Statement next() {
511 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
517 public void remove() {
518 throw new Error("Not supported");
522 public boolean hasPrevious() {
527 public Statement previous() {
529 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
534 public int nextIndex() {
539 public int previousIndex() {
544 public void set(Statement e) {
545 throw new UnsupportedOperationException();
549 public void add(Statement e) {
550 throw new UnsupportedOperationException();
556 public Iterator<Statement> iterator() {
557 return new IteratorImpl(0);
562 return sizeInternal() / 3;
566 public Object[] toArray() {
567 Object[] result = new Object[sizeInternal() / 3];
568 for(int i=0,j=0;j<sizeInternal();i++,j+=3)
569 result[i] = new InternalStatement(support, getQuick(j), getQuick(j+1), getQuick(j+2));
574 public boolean addAll(int index, Collection<? extends Statement> c) {
575 throw new UnsupportedOperationException();
579 public Statement get(int index) {
581 if(index < 0 || index >= sizeInternal())
582 throw new IndexOutOfBoundsException();
583 return new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
587 public Statement set(int index, Statement element) {
588 throw new UnsupportedOperationException();
592 public void add(int index, Statement element) {
593 throw new UnsupportedOperationException();
597 public Statement remove(int index) {
598 throw new UnsupportedOperationException();
602 public int indexOf(Object obj) {
603 if(!(obj instanceof InternalStatement))
605 InternalStatement statement = (InternalStatement)obj;
609 for(int i=0;i<sizeInternal();i+=3)
610 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
616 public int lastIndexOf(Object obj) {
617 if(!(obj instanceof InternalStatement))
619 InternalStatement statement = (InternalStatement)obj;
623 for(int i=sizeInternal()-3;i>=0;i-=3)
624 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
630 public ListIterator<Statement> listIterator() {
631 return new IteratorImpl(0);
635 public ListIterator<Statement> listIterator(int index) {
636 return new IteratorImpl(index*3);
640 public List<Statement> subList(int fromIndex, int toIndex) {
641 if(fromIndex < 0 || toIndex*3 >= sizeInternal() || fromIndex > toIndex)
642 throw new IndexOutOfBoundsException();
643 return new RandomAccessSubList<Statement>(this, fromIndex, toIndex-fromIndex);
648 final public Collection<Statement> getStatements(final Resource subject,
649 final Resource relation)
650 throws ManyObjectsForFunctionalRelationException, ServiceException {
652 assert (subject != null);
653 assert (relation != null);
657 StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
658 processor.forEachStatement(this, subject, relation, procedure);
659 procedure.checkAndThrow();
662 } catch (DatabaseException e) {
664 System.err.println(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation);
666 StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
667 processor.forEachStatement(this, subject, relation, procedure);
669 return Collections.emptyList();
671 // throw new ServiceException(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation, e);
678 final public Collection<Statement> getAssertedStatements(final Resource subject, final Resource relation)
679 throws ManyObjectsForFunctionalRelationException, ServiceException {
681 assert (subject != null);
682 assert (relation != null);
686 return syncRequest(new ForEachAssertedStatement(subject, relation));
688 } catch (ManyObjectsForFunctionalRelationException e) {
690 throw new ManyObjectsForFunctionalRelationException(e);
692 } catch (ServiceException e) {
694 throw new ServiceException(e);
696 } catch (DatabaseException e) {
698 throw new ServiceException(INTERNAL_ERROR_STRING, e);
705 final public Collection<Resource> getPredicates(final Resource subject) throws ServiceException {
707 assert (subject != null);
711 return processor.getPredicates(this, subject);
713 // AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
714 // processor.forEachPredicate(this, subject, procedure);
715 // procedure.checkAndThrow();
718 } catch (ServiceException e) {
720 throw new ServiceException(e);
722 } catch (DatabaseException e) {
724 throw new ServiceException(INTERNAL_ERROR_STRING, e);
726 } catch (Throwable e) {
728 throw new ServiceException(e);
735 final public Collection<Resource> getPrincipalTypes(final Resource subject)
736 throws ServiceException {
738 assert (subject != null);
742 AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
743 processor.forEachPrincipalType(this, subject, procedure);
744 procedure.checkAndThrow();
747 } catch (ServiceException e) {
749 throw new ServiceException(e);
751 } catch (DatabaseException e) {
753 throw new ServiceException(INTERNAL_ERROR_STRING, e);
760 final public Set<Resource> getTypes(final Resource subject) throws ServiceException {
762 assert (subject != null);
766 return processor.getTypes(this, subject);
768 } catch (ServiceException e) {
770 throw new ServiceException(e);
772 } catch (DatabaseException e) {
774 throw new ServiceException(INTERNAL_ERROR_STRING, e);
776 } catch (Throwable e) {
778 throw new ServiceException(e);
785 final public Set<Resource> getSupertypes(final Resource subject)
786 throws ServiceException {
788 assert (subject != null);
792 SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
793 processor.forSupertypes(this, subject, procedure);
794 procedure.checkAndThrow();
795 return procedure.result;
797 } catch (ServiceException e) {
799 throw new ServiceException(e);
801 } catch (DatabaseException e) {
803 throw new ServiceException(INTERNAL_ERROR_STRING, e);
810 final public Set<Resource> getSuperrelations(final Resource subject)
811 throws ServiceException {
813 assert (subject != null);
817 SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
818 processor.forSuperrelations(this, subject, procedure);
819 procedure.checkAndThrow();
820 return procedure.result;
822 } catch (ServiceException e) {
824 throw new ServiceException(e);
826 } catch (DatabaseException e) {
828 throw new ServiceException(INTERNAL_ERROR_STRING, e);
835 public Resource getPossibleSuperrelation(Resource subject) throws ServiceException {
839 SyncReadProcedure<Resource> procedure = new SyncReadProcedure<Resource>();
840 processor.forPossibleSuperrelation(this, subject, procedure);
841 procedure.checkAndThrow();
842 return procedure.result;
844 } catch (ServiceException e) {
846 throw new ServiceException(e);
848 } catch (DatabaseException e) {
850 throw new ServiceException(INTERNAL_ERROR_STRING, e);
857 final public Collection<Resource> getObjects(final Resource subject, final Resource relation)
858 throws ServiceException {
860 assert (subject != null);
861 assert (relation != null);
863 if(Development.DEVELOPMENT) {
864 if(Development.isTrue(DevelopmentKeys.READGRAPH_COUNT)) {
865 counters.adjustOrPutValue("objects $" + subject.getResourceId() + " $" + relation.getResourceId(), 1, 1);
867 //if(subject.getResourceId()==xx && relation.getResourceId()==xx) new Exception().printStackTrace();
872 AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
873 processor.forEachObject(this, subject, relation, procedure);
874 procedure.checkAndThrow();
877 } catch (DatabaseException e) {
879 throw new ServiceException(INTERNAL_ERROR_STRING, e);
886 final public Collection<Resource> getAssertedObjects(
887 final Resource subject, final Resource relation)
888 throws ManyObjectsForFunctionalRelationException, ServiceException {
891 throw new ArgumentException("Subject must not be null.");
892 if (relation == null)
893 throw new ArgumentException("Relation must not be null. Subject=" + subject);
897 return syncRequest(new ForEachAssertedObject(subject, relation));
899 } catch (ManyObjectsForFunctionalRelationException e) {
901 throw new ManyObjectsForFunctionalRelationException(e);
903 } catch (ServiceException e) {
905 throw new ServiceException(e);
907 } catch (DatabaseException e) {
909 throw new ServiceException(INTERNAL_ERROR_STRING, e);
916 final public Resource getInverse(final Resource relation) throws NoInverseException, ServiceException {
918 assert (relation != null);
922 return getSingleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
924 } catch (NoSingleResultException e) {
926 throw new NoInverseException(e);
928 } catch (ServiceException e) {
930 throw new ServiceException(e);
937 final public Resource getSingleObject(final Resource subject, final Resource relation) throws NoSingleResultException, ServiceException {
939 if( subject == null) throw new IllegalArgumentException("subject can not be null");
940 if( relation == null) throw new IllegalArgumentException("relation can not be null");
943 int single = processor.getSingleObject(this, subject, relation);
945 if (EMPTY_RESOURCE_CHECK) {
946 if (!hasStatement(subject)) {
947 throw new EmptyResourceException("Resource " + debugString(subject));
950 throw new NoSingleResultException("No single object for subject " + debugString(subject)
951 + " and relation " + debugString(relation), single);
953 return processor.querySupport.getResource(single);
954 } catch (NoSingleResultException e) {
956 } catch (DatabaseException e) {
957 throw new ServiceException(e);
962 final public Statement getSingleStatement(final Resource subject, final Resource relation) throws NoSingleResultException, ManyObjectsForFunctionalRelationException, ServiceException {
963 assert (subject != null);
964 assert (relation != null);
966 Collection<Statement> statements = getStatements(subject, relation);
967 if (statements.size() == 1) {
968 return statements.iterator().next();
970 if (EMPTY_RESOURCE_CHECK)
971 if (!hasStatement(subject))
972 throw new EmptyResourceException("Resource " + debugString(subject));
973 throw new NoSingleResultException("No single statement for subject " + debugString(subject)
974 + " and relation " + debugString(relation), statements.size());
976 } catch (ServiceException e) {
977 throw new ServiceException(e);
982 final public Resource getSingleType(final Resource subject) throws NoSingleResultException, ServiceException {
983 assert (subject != null);
985 ArrayList<Resource> principalTypes = (ArrayList<Resource>)getPrincipalTypes(subject);
986 if (principalTypes.size() == 1) {
987 return principalTypes.get(0);
989 throw new NoSingleResultException("No single type for subject " + debugString(subject), principalTypes.size());
991 } catch (ServiceException e) {
992 throw new ServiceException(e);
997 final public Resource getSingleType(final Resource subject,
998 final Resource baseType) throws NoSingleResultException,
1001 assert (subject != null);
1002 assert (baseType != null);
1005 return syncRequest(new SingleType(subject, baseType));
1006 } catch (DatabaseException e) {
1007 throw new NoSingleResultException("subject=" + subject + ", baseType=" + baseType, 0, e);
1012 final public <T> T getValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
1014 assert (subject != null);
1018 Layer0 L0 = processor.getL0(this);
1019 int object = processor.getSingleObject(this, subject, L0.HasDataType);
1020 if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
1022 if(processor.isImmutable(object)) {
1023 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1024 return getValue(subject, binding);
1026 byte[] dt = processor.getValue(this, object);
1027 if(dt == null) throw new ServiceException("No data type for " + subject);
1028 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1029 Binding binding = Bindings.getBinding(datatype);
1030 return getValue(subject, binding);
1033 } catch (IOException e) {
1035 throw new ServiceException(e);
1037 } catch (DoesNotContainValueException e) {
1039 throw new DoesNotContainValueException(e, subject);
1041 } catch (ServiceException e) {
1043 throw new ServiceException(e);
1045 } catch (DatabaseException e) {
1047 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1054 final public Variant getVariantValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
1056 assert (subject != null);
1060 Layer0 L0 = processor.getL0(this);
1061 int object = processor.getSingleObject(this, subject, L0.HasDataType);
1062 if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
1064 if(processor.isImmutable(object)) {
1065 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1066 return new Variant(binding, getValue(subject, binding));
1068 byte[] dt = processor.getValue(this, object);
1069 if(dt == null) throw new ServiceException("No data type for " + subject);
1070 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1071 Binding binding = Bindings.getBinding(datatype);
1072 return new Variant(binding, getValue(subject, binding));
1075 } catch (IOException e) {
1077 throw new ServiceException(e);
1079 } catch (DoesNotContainValueException e) {
1081 throw new DoesNotContainValueException(e, subject);
1083 } catch (ServiceException e) {
1085 throw new ServiceException(e);
1087 } catch (DatabaseException e) {
1089 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1094 static final IdentityHashMap<Binding,Serializer> serializers = new IdentityHashMap<Binding,Serializer>();
1097 serializers.put(Bindings.STRING, Bindings.STRING.serializer());
1100 final protected Serializer getSerializer(Binding binding) {
1101 return binding.serializer();
1105 final public <T> T getValue(final Resource subject, final Binding binding) throws DoesNotContainValueException, BindingException,
1108 assert (subject != null);
1112 byte[] bytes = processor.getValue(this, subject);
1113 if (bytes == null) throw new DoesNotContainValueException("No value for resource " + subject);
1115 Serializer serializer = getSerializer(binding);
1116 return (T)serializer.deserialize(bytes);
1118 } catch (DoesNotContainValueException e) {
1120 throw new DoesNotContainValueException(e);
1122 } catch (IOException e) {
1124 throw new ServiceException(e);
1126 } catch (DatabaseException e) {
1128 throw new ServiceException(e);
1130 } catch (BufferUnderflowException e) {
1131 // This is sometimes thrown when deserialize fails because wrong format.
1132 // For callers of this method this is just an service exception.
1133 throw new ServiceException(e);
1139 final public <T> T getRelatedValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1140 DoesNotContainValueException, ServiceException {
1142 assert (subject != null);
1143 assert (relation != null);
1146 Resource object = getSingleObject(subject, relation);
1147 return getValue(object);
1148 } catch (NoSingleResultException e) {
1149 throw new NoSingleResultException("No single value found for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1150 } catch (DoesNotContainValueException e) {
1152 Layer0 L0 = processor.getL0(this);
1153 Resource object = getPossibleObject(subject, relation);
1154 if(isInstanceOf(object, L0.Value)) {
1155 if(isInstanceOf(object, L0.Literal)) {
1156 throw new DoesNotContainValueException(e);
1158 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1161 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1163 } catch (DoesNotContainValueException e2) {
1165 } catch (DatabaseException e2) {
1166 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1168 } catch (ServiceException e) {
1169 throw new ServiceException(e);
1174 final public Variant getRelatedVariantValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1175 DoesNotContainValueException, ServiceException {
1177 assert (subject != null);
1178 assert (relation != null);
1181 Resource object = getSingleObject(subject, relation);
1182 return getVariantValue(object);
1183 } catch (NoSingleResultException e) {
1184 throw new NoSingleResultException("No single object for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1185 } catch (DoesNotContainValueException e) {
1187 Layer0 L0 = processor.getL0(this);
1188 Resource object = getPossibleObject(subject, relation);
1189 if(isInstanceOf(object, L0.Value)) {
1190 if(isInstanceOf(object, L0.Literal)) {
1191 throw new DoesNotContainValueException(e);
1193 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1196 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1198 } catch (DoesNotContainValueException e2) {
1200 } catch (DatabaseException e2) {
1201 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1203 } catch (ServiceException e) {
1204 throw new ServiceException(e);
1209 final public <T> T getRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1210 throws NoSingleResultException, DoesNotContainValueException, BindingException, ServiceException {
1212 assert (subject != null);
1213 assert (relation != null);
1216 Resource object = getSingleObject(subject, relation);
1217 return getValue(object, binding);
1218 } catch (NoSingleResultException e) {
1219 String message = "";
1221 String subjectName = NameUtils.getSafeName(this, subject, true);
1222 String relationName = NameUtils.getSafeName(this, relation, true);
1223 message = "Subject: " + subjectName + ", Relation: " + relationName;
1224 } catch (DatabaseException e2) {
1227 throw new NoSingleResultException(message, e.getResultCount(), e);
1228 } catch (DoesNotContainValueException e) {
1229 throw new DoesNotContainValueException(e);
1230 } catch (ServiceException e) {
1231 throw new ServiceException(e);
1236 final public <T> T adapt(final Resource resource, final Class<T> clazz)
1237 throws AdaptionException, ValidationException, ServiceException {
1239 assert (resource != null);
1240 assert (clazz != null);
1244 return syncRequest(new Adapter<T>(resource, clazz));
1246 } catch (AdaptionException e) {
1248 throw new AdaptionException(e);
1250 } catch (ValidationException e) {
1252 throw new ValidationException(e);
1254 } catch (ServiceException e) {
1256 throw new ServiceException(e);
1258 } catch (DatabaseException e) {
1260 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1267 final public <T,C> T adaptContextual(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1268 throws AdaptionException, ValidationException, ServiceException {
1270 assert (resource != null);
1271 assert (context != null);
1273 class ContextualAdapter implements AsyncRead<T> {
1275 final private Resource resource;
1276 final private C context;
1277 final private Class<T> clazz;
1280 public int hashCode() {
1281 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1285 final public int threadHash() {
1286 return resource.getThreadHash();
1290 public boolean equals(Object object) {
1293 else if (object == null)
1295 else if (getClass() != object.getClass())
1297 ContextualAdapter r = (ContextualAdapter)object;
1298 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1302 public int getFlags() {
1306 public ContextualAdapter(Resource resource, C context, Class<T> clazz) {
1307 this.resource = resource;
1308 this.context = context;
1313 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1315 final AdaptionService service = getSession().peekService(AdaptionService.class);
1316 if (service == null)
1317 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1319 service.adapt(graph, resource, context, contextClass, clazz, false, procedure);
1324 public String toString() {
1325 return "Adapter for (" + resource + "," + context + ") as " + clazz.getName();
1332 return syncRequest(new ContextualAdapter(resource, context, clazz));
1334 } catch (AdaptionException e) {
1336 throw new AdaptionException(e);
1338 } catch (ValidationException e) {
1340 throw new ValidationException(e);
1342 } catch (ServiceException e) {
1344 throw new ServiceException(e);
1346 } catch (DatabaseException e) {
1348 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1355 final public <T> T adaptRelated(final Resource resource, final Resource relation, final Class<T> clazz)
1356 throws AdaptionException, NoSingleResultException, ValidationException, ServiceException {
1358 assert (resource != null);
1359 assert (clazz != null);
1361 Statement stm = getSingleStatement(resource, relation);
1363 return adaptContextual(stm.getObject(), new RelationContextImpl(resource, stm), RelationContext.class, clazz);
1368 final public <T> T getPossibleRelatedAdapter(final Resource resource, final Resource relation, final Class<T> clazz)
1369 throws ValidationException, ServiceException {
1372 return adaptRelated(resource, relation, clazz);
1373 } catch (DatabaseException e) {
1380 final public <T,C> T getPossibleContextualAdapter(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1381 throws ValidationException, ServiceException {
1383 assert (resource != null);
1384 assert (context != null);
1386 class PossibleContextualAdapter implements AsyncRead<T> {
1388 final private Resource resource;
1389 final private C context;
1390 final private Class<T> clazz;
1393 public int hashCode() {
1394 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1398 final public int threadHash() {
1399 return resource.getThreadHash();
1403 public boolean equals(Object object) {
1406 else if (object == null)
1408 else if (getClass() != object.getClass())
1410 PossibleContextualAdapter r = (PossibleContextualAdapter)object;
1411 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1415 public int getFlags() {
1419 public PossibleContextualAdapter(Resource resource, C context, Class<T> clazz) {
1420 this.resource = resource;
1421 this.context = context;
1426 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1428 final AdaptionService service = getSession().peekService(AdaptionService.class);
1429 if (service == null)
1430 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1432 service.adapt(graph, resource, context, contextClass, clazz, true, procedure);
1437 public String toString() {
1438 return "Possible adapter for (" + resource + "," + context + ") as " + clazz.getName();
1445 return syncRequest(new PossibleContextualAdapter(resource, context, clazz));
1447 } catch (ValidationException e) {
1449 throw new ValidationException(e);
1451 } catch (ServiceException e) {
1453 throw new ServiceException(e);
1455 } catch (DatabaseException e) {
1457 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1464 final public <T> T adaptUnique(final Resource resource, final Class<T> clazz)
1465 throws AdaptionException, ValidationException, ServiceException {
1467 assert (resource != null);
1468 assert (clazz != null);
1472 return syncRequest(new UniqueAdapter<T>(resource, clazz));
1474 } catch (AdaptionException e) {
1476 throw new AdaptionException(e);
1478 } catch (ValidationException e) {
1480 throw new ValidationException(e);
1482 } catch (ServiceException e) {
1484 throw new ServiceException(e);
1486 } catch (DatabaseException e) {
1488 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1495 final public Resource getPossibleInverse(final Resource relation)
1496 throws ServiceException {
1498 assert (relation != null);
1502 return getPossibleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
1504 } catch (ServiceException e) {
1506 throw new ServiceException(e);
1508 } catch (DatabaseException e) {
1510 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1517 public Resource getPossibleObject(final Resource subject, final Resource relation)
1518 throws ManyObjectsForFunctionalRelationException, ServiceException {
1520 assert (subject != null);
1521 assert (relation != null);
1525 int result = processor.getSingleObject(this, subject, relation);
1526 if(result == 0) return null;
1528 return processor.querySupport.getResource(result);
1530 } catch (ManyObjectsForFunctionalRelationException e) {
1532 throw new ManyObjectsForFunctionalRelationException("subject=" + subject + ", relation=" + relation, e);
1534 } catch (DatabaseException e) {
1536 throw new ServiceException(e);
1543 final public Statement getPossibleStatement(final Resource subject, final Resource relation)
1544 throws ManyObjectsForFunctionalRelationException, ServiceException {
1546 assert (subject != null);
1547 assert (relation != null);
1551 Collection<Statement> statements = getStatements(subject, relation);
1552 if(statements.size() == 1) return statements.iterator().next();
1555 } catch (ManyObjectsForFunctionalRelationException e) {
1557 throw new ManyObjectsForFunctionalRelationException("Many objects in " + subject + " for functional relation " + relation);
1559 } catch (ServiceException e) {
1561 throw new ServiceException(e);
1568 final public Resource getPossibleType(final Resource subject, final Resource baseType) throws ServiceException {
1570 assert (subject != null);
1571 assert (baseType != null);
1575 AsyncReadProcedure<Resource> procedure = new AsyncReadProcedure<Resource>();
1576 forPossibleType(subject, baseType, procedure);
1577 procedure.checkAndThrow();
1578 return procedure.result;
1580 } catch (ServiceException e) {
1582 throw new ServiceException(e);
1584 } catch (DatabaseException e) {
1586 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1593 final public <T> T getPossibleValue(final Resource subject) throws ServiceException {
1595 assert (subject != null);
1599 int object = processor.getSingleObject(this, subject, processor.getL0(this).HasDataType);
1600 if(object == 0) return null;
1602 if(processor.isImmutable(object)) {
1603 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1604 return getPossibleValue(subject, binding);
1606 byte[] dt = processor.getValue(this, object);
1607 if(dt == null) return null;
1608 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1609 Binding binding = Bindings.getBinding(datatype);
1610 return getPossibleValue(subject, binding);
1613 } catch (IOException e) {
1615 throw new ServiceException(e);
1617 } catch (ServiceException e) {
1619 throw new ServiceException(e);
1621 } catch (DatabaseException e) {
1623 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1630 final public <T> T getPossibleValue(final Resource subject, final Binding binding) throws BindingException, ServiceException {
1632 assert (subject != null);
1633 assert (binding != null);
1637 byte[] dt = processor.getValue(this, subject);
1638 if(dt == null) return null;
1639 Serializer serializer = getSerializer(binding);
1640 return (T)serializer.deserialize(dt);
1642 } catch (IOException e) {
1644 throw new ServiceException(e);
1646 } catch (BindingException e) {
1648 throw new BindingException(e);
1650 } catch (ServiceException e) {
1652 throw new ServiceException(e);
1654 } catch (DatabaseException e) {
1655 e.printStackTrace();
1656 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1662 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation)
1663 throws ManyObjectsForFunctionalRelationException, ServiceException {
1665 assert (subject != null);
1666 assert (relation != null);
1670 Resource object = getPossibleObject(subject, relation);
1671 if(object == null) return null;
1672 else return getPossibleValue(object);
1674 } catch (ManyObjectsForFunctionalRelationException e) {
1676 throw new ManyObjectsForFunctionalRelationException(e);
1678 } catch (ServiceException e) {
1680 throw new ServiceException(e);
1687 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1688 throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
1690 assert (subject != null);
1691 assert (relation != null);
1692 assert (binding != null);
1696 Resource object = getPossibleObject(subject, relation);
1697 if(object == null) return null;
1698 else return getPossibleValue(object, binding);
1700 } catch (ManyObjectsForFunctionalRelationException e) {
1702 throw new ManyObjectsForFunctionalRelationException(e);
1704 } catch (BindingException e) {
1706 throw new BindingException(e);
1708 } catch (ServiceException e) {
1710 throw new ServiceException(e);
1717 public <T> T getPossibleAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1719 assert (resource != null);
1720 assert (clazz != null);
1724 return syncRequest(new PossibleAdapter<T>(resource, clazz));
1726 } catch (ValidationException e) {
1728 throw new ValidationException(e);
1730 } catch (AdaptionException e) {
1734 } catch (DatabaseException e) {
1736 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1742 public <T> T getPossibleUniqueAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1744 assert (resource != null);
1745 assert (clazz != null);
1749 return syncRequest(new PossibleUniqueAdapter<T>(resource, clazz));
1751 } catch (AdaptionException e) {
1755 } catch (ValidationException e) {
1757 throw new ValidationException(e);
1759 } catch (DatabaseException e) {
1761 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1768 final public boolean isInstanceOf(final Resource resource, final Resource type) throws ServiceException {
1770 assert (resource != null);
1771 assert (type != null);
1773 Set<Resource> resources = getTypes(resource);
1774 // This check was necessary because some of the callers of this method got stuck when the NPE was thrown from here.
1775 if (null == resources)
1778 if(EMPTY_RESOURCE_CHECK) {
1779 if (resources.isEmpty()) {
1780 if(!hasStatement(resource)) throw new EmptyResourceException("Resource " + debugString(resource));
1784 return resources.contains(type);
1789 final public boolean isInheritedFrom(final Resource resource, final Resource type) throws ServiceException {
1791 assert (resource != null);
1792 assert (type != null);
1796 if(resource.equals(type)) return true;
1798 return getSupertypes(resource).contains(type);
1800 } catch (ServiceException e) {
1802 throw new ServiceException(e);
1809 final public boolean isSubrelationOf(final Resource resource, final Resource type) throws ServiceException {
1811 assert (resource != null);
1812 assert (type != null);
1816 if(resource.equals(type)) return true;
1818 return getSuperrelations(resource).contains(type);
1820 } catch (ServiceException e) {
1822 throw new ServiceException(e);
1829 final public boolean hasStatement(final Resource subject) throws ServiceException {
1831 assert (subject != null);
1835 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1836 processor.forHasStatement(this, subject, procedure);
1837 procedure.checkAndThrow();
1838 return procedure.result;
1840 } catch (ServiceException e) {
1842 throw new ServiceException(e);
1844 } catch (DatabaseException e) {
1846 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1853 final public boolean hasStatement(final Resource subject, final Resource relation) throws ServiceException {
1855 assert (subject != null);
1856 assert (relation != null);
1860 Collection<Resource> objects = getObjects(subject, relation);
1861 return !objects.isEmpty();
1863 } catch (ServiceException e) {
1865 throw new ServiceException(e);
1872 final public boolean hasStatement(final Resource subject, final Resource relation, final Resource object) throws ServiceException {
1874 assert (subject != null);
1875 assert (relation != null);
1876 assert (object != null);
1880 for(Resource o : getObjects(subject, relation)) {
1881 if(object.equals(o)) return true;
1886 } catch (ServiceException e) {
1888 throw new ServiceException(e);
1895 final public boolean hasValue(final Resource subject) throws ServiceException {
1897 assert (subject != null);
1901 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1902 processor.forHasValue(this, subject, procedure);
1903 procedure.checkAndThrow();
1904 return procedure.result;
1906 } catch (ServiceException e) {
1908 throw new ServiceException(e);
1910 } catch (DatabaseException e) {
1912 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1918 final AsyncProcedure<?> NONE = new AsyncProcedure<Object>() {
1921 public void execute(AsyncReadGraph graph, Object result) {
1925 public void exception(AsyncReadGraph graph, Throwable throwable) {
1931 * Implementation of the interface RequestProcessor
1935 public <T> T syncRequest(final Read<T> request) throws DatabaseException {
1936 assert (request != null);
1937 return (T)QueryCache.runnerReadEntry(this, request, parent, null, null, true);
1941 public <T> T syncRequest(Read<T> request, SyncListener<T> procedure)
1942 throws DatabaseException {
1943 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
1947 public <T> T syncRequest(Read<T> request, final Listener<T> procedure)
1948 throws DatabaseException {
1949 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
1953 public <T> T syncRequest(final Read<T> request, final AsyncProcedure<T> procedure) throws DatabaseException {
1955 assert (request != null);
1957 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
1959 return QueryCache.resultReadEntry(this, request, parent, listener, procedure);
1964 public <T> T syncRequest(final Read<T> request,
1965 final SyncProcedure<T> procedure) throws DatabaseException {
1966 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
1970 public <T> T syncRequest(Read<T> request, Procedure<T> procedure)
1971 throws DatabaseException {
1972 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
1975 static class AsyncReadProcedure<T> implements AsyncProcedure<T> {
1977 private static Throwable DONE = new Throwable();
1980 Throwable exception = null;
1983 public void execute(AsyncReadGraph graph, T t) {
1989 public void exception(AsyncReadGraph graph, Throwable t) {
1993 public void checkAndThrow() throws DatabaseException {
1994 if(exception != DONE) {
1995 if (exception instanceof DatabaseException)
1996 throw (DatabaseException) exception;
1998 throw new DatabaseException(
1999 "Unexpected exception in ReadGraph.syncRequest(AsyncRead)",
2004 public boolean done() {
2005 return exception != null;
2011 public <T> T syncRequest(final AsyncRead<T> request)
2012 throws DatabaseException {
2014 assert (request != null);
2015 return syncRequest(request, new AsyncProcedureAdapter<>() );
2020 public <T> T syncRequest(AsyncRead<T> request, AsyncListener<T> procedure)
2021 throws DatabaseException {
2022 return syncRequest(request, (AsyncProcedure<T>) procedure);
2026 public <T> T syncRequest(AsyncRead<T> request, SyncListener<T> procedure)
2027 throws DatabaseException {
2028 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
2032 public <T> T syncRequest(AsyncRead<T> request, Listener<T> procedure)
2033 throws DatabaseException {
2034 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
2038 final public <T> T syncRequest(final AsyncRead<T> request,
2039 final AsyncProcedure<T> procedure) throws DatabaseException {
2041 assert (request != null);
2043 ListenerBase listener = getListenerBase(procedure);
2045 // BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, procedure, request);
2046 return (T)QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure, true);
2052 public <T> T syncRequest(AsyncRead<T> request,
2053 final SyncProcedure<T> procedure) throws DatabaseException {
2054 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2058 final public <T> T syncRequest(final AsyncRead<T> request,
2059 final Procedure<T> procedure) throws DatabaseException {
2060 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2064 public <T> Collection<T> syncRequest(final MultiRead<T> request)
2065 throws DatabaseException {
2067 assert (request != null);
2069 final ArrayList<T> result = new ArrayList<T>();
2070 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2072 syncRequest(request, new SyncMultiProcedure<T>() {
2075 public void execute(ReadGraph graph, T t) {
2076 synchronized (result) {
2082 public void finished(ReadGraph graph) {
2086 public void exception(ReadGraph graph, Throwable t) {
2091 public String toString() {
2092 return "syncRequest(MultiRead) -> " + request;
2097 Throwable t = exception.get();
2099 if (t instanceof DatabaseException)
2100 throw (DatabaseException) t;
2102 throw new DatabaseException(
2103 "Unexpected exception in ReadGraph.syncRequest(Read)",
2112 public <T> Collection<T> syncRequest(MultiRead<T> request,
2113 SyncMultiListener<T> procedure) {
2114 return syncRequest(request, (SyncMultiProcedure<T>)procedure);
2118 public <T> Collection<T> syncRequest(MultiRead<T> request,
2119 MultiListener<T> procedure) {
2120 return syncRequest(request, new NoneToSyncMultiListener<T>(procedure));
2124 public <T> Collection<T> syncRequest(MultiRead<T> request,
2125 SyncMultiProcedure<T> procedure) {
2127 assert (request != null);
2129 ListenerBase listener = getListenerBase(procedure);
2131 final ResultCallWrappedSyncQueryProcedure<T> wrapper = new ResultCallWrappedSyncQueryProcedure<T>(procedure);
2133 if (parent != null || listener != null) {
2135 // Object syncParent = request;
2137 // final ReadGraphImpl newGraph = newSync();
2139 processor.query(this, request, parent, wrapper, listener);
2141 // newGraph.waitAsync(syncParent);
2145 // Object syncParent = request;
2147 // final ReadGraphImpl newGraph = newSync();
2150 request.perform(this, wrapper);
2151 } catch (Throwable t) {
2152 wrapper.exception(this, t);
2157 return wrapper.get();
2162 public <T> Collection<T> syncRequest(MultiRead<T> request,
2163 MultiProcedure<T> procedure) {
2164 return syncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
2167 static class AsyncMultiReadProcedure<T> extends ArrayList<T> implements AsyncMultiProcedure<T> {
2169 private static Throwable DONE = new Throwable();
2171 private static final long serialVersionUID = -6494230465108115812L;
2173 Throwable exception = null;
2176 public synchronized void execute(AsyncReadGraph graph, T t) {
2181 public void finished(AsyncReadGraph graph) {
2186 public void exception(AsyncReadGraph graph, Throwable t) {
2190 public void checkAndThrow() throws DatabaseException {
2191 if(exception != DONE) {
2192 if (exception instanceof DatabaseException)
2193 throw (DatabaseException) exception;
2195 throw new DatabaseException(
2196 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
2201 public boolean done() {
2202 return exception != null;
2208 final public <T> Collection<T> syncRequest(AsyncMultiRead<T> request)
2209 throws DatabaseException {
2211 assert (request != null);
2213 final AsyncMultiReadProcedure<T> procedure = new AsyncMultiReadProcedure<T>();
2215 syncRequest(request, procedure);
2217 procedure.checkAndThrow();
2223 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2224 AsyncMultiListener<T> procedure) {
2225 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2229 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2230 SyncMultiListener<T> procedure) {
2231 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2235 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2236 MultiListener<T> procedure) {
2237 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2240 final private <T> void syncRequest(final AsyncMultiRead<T> request,
2241 final AsyncMultiReadProcedure<T> procedure) {
2243 assert (request != null);
2244 assert (procedure != null);
2246 ListenerBase listener = getListenerBase(procedure);
2248 if (parent != null || listener != null) {
2250 // Object syncParent = request;
2252 // final ReadGraphImpl newGraph = newSync();
2254 processor.query(this, request, parent, procedure, listener);
2256 // newGraph.waitAsync(syncParent);
2257 waitAsyncProcedure(procedure);
2261 // Object syncParent = callerThread == Integer.MIN_VALUE ? null
2264 // final ReadGraphImpl newGraph = newSyncAsync(syncParent);
2269 // ReadGraphImpl sync = newSync();
2270 request.perform(this, procedure);
2271 // sync.waitAsync(null);
2272 waitAsyncProcedure(procedure);
2275 } catch (Throwable t) {
2277 waitAsyncProcedure(procedure);
2288 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2289 final AsyncMultiProcedure<T> procedure) {
2291 assert (request != null);
2292 assert (procedure != null);
2294 ListenerBase listener = getListenerBase(procedure);
2296 if (parent != null || listener != null) {
2298 // Object syncParent = request;
2300 // final ReadGraphImpl newGraph = newSync();
2302 processor.query(this, request, parent, procedure, listener);
2304 // newGraph.waitAsync(syncParent);
2308 // Object syncParent = request;
2310 // final ReadGraphImpl newGraph = newSync();
2314 request.perform(this, new AsyncMultiProcedure<T>() {
2317 public void execute(AsyncReadGraph graph, T result) {
2318 procedure.execute(graph, result);
2322 public void finished(AsyncReadGraph graph) {
2323 procedure.finished(graph);
2327 public void exception(AsyncReadGraph graph, Throwable t) {
2328 procedure.exception(graph, t);
2332 public String toString() {
2333 return "syncRequest(AsyncMultiRead) -> " + procedure;
2338 } catch (Throwable t) {
2350 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2351 final SyncMultiProcedure<T> procedure) {
2352 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2356 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2357 final MultiProcedure<T> procedure) {
2358 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2362 public <T> T syncRequest(final ExternalRead<T> request)
2363 throws DatabaseException {
2365 assert (request != null);
2367 return syncRequest(request, new Procedure<T>() {
2370 public void execute(T t) {
2374 public void exception(Throwable t) {
2378 public String toString() {
2379 return "syncRequest(AsyncRead) -> " + request;
2387 public <T> T syncRequest(ExternalRead<T> request, Listener<T> procedure) throws DatabaseException {
2388 return syncRequest(request, (Procedure<T>) procedure);
2392 final public <T> T syncRequest(final ExternalRead<T> request,
2393 final Procedure<T> procedure) throws DatabaseException {
2395 assert (request != null);
2397 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
2398 return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
2403 public void syncRequest(final Write request) throws DatabaseException {
2405 assert (request != null);
2407 throw new DatabaseException(
2408 "Write operations are not supported during read transactions!");
2413 public <T> T syncRequest(final WriteResult<T> request) throws DatabaseException {
2415 assert (request != null);
2417 throw new DatabaseException(
2418 "Write operations are not supported during read transactions!");
2423 public void syncRequest(final DelayedWrite request)
2424 throws DatabaseException {
2426 assert (request != null);
2428 throw new DatabaseException(
2429 "Write operations are not supported during read transactions!");
2434 public <T> T syncRequest(final DelayedWriteResult<T> request) throws DatabaseException {
2436 assert (request != null);
2438 throw new DatabaseException(
2439 "Write operations are not supported during read transactions!");
2444 public void syncRequest(final WriteOnly request) throws DatabaseException {
2446 assert (request != null);
2448 throw new DatabaseException(
2449 "Write operations are not supported during read transactions!");
2454 public <T> T syncRequest(final WriteOnlyResult<T> request) throws DatabaseException {
2456 assert (request != null);
2458 throw new DatabaseException(
2459 "Write operations are not supported during read transactions!");
2464 public <T> void async(ReadInterface<T> r, AsyncProcedure<T> procedure) {
2465 r.request(this, procedure);
2469 public <T> void async(ReadInterface<T> r, Procedure<T> procedure) {
2470 r.request(this, procedure);
2474 public <T> void async(ReadInterface<T> r, SyncProcedure<T> procedure) {
2475 r.request(this, procedure);
2479 public <T> void async(ReadInterface<T> r, AsyncListener<T> procedure) {
2480 r.request(this, procedure);
2484 public <T> void async(ReadInterface<T> r, Listener<T> procedure) {
2485 r.request(this, procedure);
2489 public <T> void async(ReadInterface<T> r, SyncListener<T> procedure) {
2490 r.request(this, procedure);
2494 public <T> T sync(ReadInterface<T> r) throws DatabaseException {
2495 return r.request(this);
2499 public <T> T sync(WriteInterface<T> r) throws DatabaseException {
2500 return r.request(this);
2504 public <T> void async(WriteInterface<T> r, Procedure<T> procedure) {
2505 r.request(this, procedure);
2509 public <T> void async(WriteInterface<T> r) {
2510 r.request(this, new ProcedureAdapter<T>());
2514 * Implementation of the interface AsyncReadGraph
2518 public void forURI(Resource resource, AsyncListener<String> listener) {
2519 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2524 public void forURI(Resource resource, SyncListener<String> listener) {
2525 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2530 public void forURI(Resource resource, Listener<String> listener) {
2531 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2536 final public void forURI(final Resource resource,
2537 final AsyncProcedure<String> procedure) {
2539 assert (resource != null);
2540 assert (procedure != null);
2542 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2548 public void forURI(Resource resource, SyncProcedure<String> procedure) {
2549 forURI(resource, new SyncToAsyncProcedure<String>(procedure));
2553 public void forURI(Resource resource, Procedure<String> procedure) {
2554 forURI(resource, new NoneToAsyncProcedure<String>(procedure));
2558 public void forResource(String id, AsyncListener<Resource> listener) {
2559 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2564 public void forResource(String id, SyncListener<Resource> listener) {
2565 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2570 public void forResource(String id, Listener<Resource> listener) {
2571 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2576 final public void forResource(final String id,
2577 final AsyncProcedure<Resource> procedure) {
2579 assert (id != null);
2580 assert (procedure != null);
2582 processor.forResource(this, id, procedure);
2587 public void forResource(String id, SyncProcedure<Resource> procedure) {
2588 forResource(id, new SyncToAsyncProcedure<Resource>(procedure));
2592 public void forResource(String id, Procedure<Resource> procedure) {
2593 forResource(id, new NoneToAsyncProcedure<Resource>(procedure));
2597 public void forBuiltin(String id, AsyncListener<Resource> listener) {
2598 asyncRequest(new Builtin(id), listener);
2602 public void forBuiltin(String id, SyncListener<Resource> listener) {
2603 asyncRequest(new Builtin(id), listener);
2607 public void forBuiltin(String id, Listener<Resource> listener) {
2608 asyncRequest(new Builtin(id), listener);
2612 final public void forBuiltin(final String id,
2613 final AsyncProcedure<Resource> procedure) {
2615 assert (id != null);
2616 assert (procedure != null);
2618 processor.forBuiltin(this, id, procedure);
2623 public void forBuiltin(String id, SyncProcedure<Resource> procedure) {
2624 forBuiltin(id, new SyncToAsyncProcedure<Resource>(procedure));
2628 public void forBuiltin(String id, Procedure<Resource> procedure) {
2629 forBuiltin(id, new NoneToAsyncProcedure<Resource>(procedure));
2633 final public void forEachStatement(Resource subject, Resource relation,
2634 AsyncMultiProcedure<Statement> procedure) {
2636 assert (subject != null);
2637 assert (relation != null);
2638 assert (procedure != null);
2640 processor.forEachStatement(this, subject, relation, procedure);
2645 public void forEachStatement(Resource subject, Resource relation,
2646 SyncMultiProcedure<Statement> procedure) {
2647 forEachStatement(subject, relation,
2648 new SyncToAsyncMultiProcedure<Statement>(procedure));
2652 final public void forEachStatement(Resource subject, Resource relation,
2653 MultiProcedure<Statement> procedure) {
2655 assert (subject != null);
2656 assert (relation != null);
2657 assert (procedure != null);
2659 processor.forEachStatement(this, subject, relation, procedure);
2664 final public void forStatementSet(Resource subject, Resource relation,
2665 AsyncSetListener<Statement> procedure) {
2667 assert (subject != null);
2668 assert (relation != null);
2669 assert (procedure != null);
2671 processor.forStatementSet(this, subject, relation, procedure);
2676 final public void forStatementSet(Resource subject, Resource relation,
2677 SyncSetListener<Statement> procedure) {
2678 forStatementSet(subject, relation,
2679 new SyncToAsyncSetProcedure<Statement>(procedure));
2683 public void forStatementSet(Resource subject, Resource relation,
2684 SetListener<Statement> listener) {
2685 forStatementSet(subject, relation,
2686 new NoneToAsyncSetProcedure<Statement>(listener));
2690 final public void forEachAssertedStatement(final Resource subject,
2691 final Resource relation,
2692 final AsyncMultiProcedure<Statement> procedure) {
2694 assert (subject != null);
2695 assert (relation != null);
2696 assert (procedure != null);
2698 processor.forEachAssertedStatement(this, subject, relation, procedure);
2703 public void forEachAssertedStatement(Resource subject, Resource relation,
2704 SyncMultiProcedure<Statement> procedure) {
2705 forEachAssertedStatement(subject, relation,
2706 new SyncToAsyncMultiProcedure<Statement>(procedure));
2710 public void forEachAssertedStatement(Resource subject, Resource relation,
2711 MultiProcedure<Statement> procedure) {
2712 forEachAssertedStatement(subject, relation,
2713 new NoneToAsyncMultiProcedure<Statement>(procedure));
2717 public void forAssertedStatementSet(Resource subject, Resource relation,
2718 AsyncSetListener<Statement> procedure) {
2720 assert (subject != null);
2721 assert (relation != null);
2722 assert (procedure != null);
2724 processor.forAssertedStatementSet(this, subject, relation, procedure);
2729 public void forAssertedStatementSet(Resource subject, Resource relation,
2730 SyncSetListener<Statement> procedure) {
2732 assert (subject != null);
2733 assert (relation != null);
2734 assert (procedure != null);
2736 forAssertedStatementSet(subject, relation,
2737 new SyncToAsyncSetProcedure<Statement>(procedure));
2742 public void forAssertedStatementSet(Resource subject, Resource relation,
2743 SetListener<Statement> procedure) {
2745 assert (subject != null);
2746 assert (relation != null);
2747 assert (procedure != null);
2749 forAssertedStatementSet(subject, relation,
2750 new NoneToAsyncSetProcedure<Statement>(procedure));
2755 final public void forEachPredicate(final Resource subject,
2756 final AsyncMultiProcedure<Resource> procedure) {
2758 assert (subject != null);
2759 assert (procedure != null);
2761 processor.forEachPredicate(this, subject, procedure);
2766 public void forEachPredicate(Resource subject,
2767 SyncMultiProcedure<Resource> procedure) {
2768 forEachPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(
2773 final public void forEachPredicate(final Resource subject,
2774 final MultiProcedure<Resource> procedure) {
2776 assert (subject != null);
2777 assert (procedure != null);
2779 processor.forEachPredicate(this, subject, procedure);
2784 final public void forPredicateSet(final Resource subject,
2785 final AsyncSetListener<Resource> procedure) {
2787 assert (subject != null);
2788 assert (procedure != null);
2790 processor.forPredicateSet(this, subject, procedure);
2795 final public void forPredicateSet(final Resource subject,
2796 final SyncSetListener<Resource> procedure) {
2798 assert (subject != null);
2799 assert (procedure != null);
2801 forPredicateSet(subject, new SyncToAsyncSetProcedure<Resource>(
2807 final public void forPredicateSet(final Resource subject,
2808 final SetListener<Resource> procedure) {
2810 assert (subject != null);
2811 assert (procedure != null);
2813 forPredicateSet(subject, new NoneToAsyncSetProcedure<Resource>(
2819 final public void forEachPrincipalType(final Resource subject,
2820 final AsyncMultiProcedure<Resource> procedure) {
2822 assert (subject != null);
2823 assert (procedure != null);
2825 processor.forEachPrincipalType(this, subject, procedure);
2830 public void forEachPrincipalType(Resource subject,
2831 SyncMultiProcedure<Resource> procedure) {
2832 forEachPrincipalType(subject, new SyncToAsyncMultiProcedure<Resource>(
2837 final public void forEachPrincipalType(final Resource subject,
2838 final MultiProcedure<Resource> procedure) {
2840 assert (subject != null);
2841 assert (procedure != null);
2843 processor.forEachPrincipalType(this, subject, procedure);
2848 final public void forPrincipalTypeSet(final Resource subject,
2849 final AsyncSetListener<Resource> procedure) {
2851 assert (subject != null);
2852 assert (procedure != null);
2854 processor.forPrincipalTypeSet(this, subject, procedure);
2859 final public void forPrincipalTypeSet(final Resource subject,
2860 final SyncSetListener<Resource> procedure) {
2862 assert (subject != null);
2863 assert (procedure != null);
2865 forPrincipalTypeSet(subject, new SyncToAsyncSetProcedure<Resource>(
2871 final public void forPrincipalTypeSet(final Resource subject,
2872 final SetListener<Resource> procedure) {
2874 assert (subject != null);
2875 assert (procedure != null);
2877 forPrincipalTypeSet(subject, new NoneToAsyncSetProcedure<Resource>(
2883 public void forTypes(Resource subject, AsyncListener<Set<Resource>> listener) {
2884 asyncRequest(new Types(subject), listener);
2888 public void forTypes(Resource subject, SyncListener<Set<Resource>> listener) {
2889 asyncRequest(new Types(subject), listener);
2893 public void forTypes(Resource subject, Listener<Set<Resource>> listener) {
2894 asyncRequest(new Types(subject), listener);
2898 final public void forTypes(final Resource subject,
2899 final AsyncProcedure<Set<Resource>> procedure) {
2901 assert (subject != null);
2902 assert (procedure != null);
2904 processor.forTypes(this, subject, procedure);
2909 public void forTypes(Resource subject,
2910 SyncProcedure<Set<Resource>> procedure) {
2911 forTypes(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
2915 public void forTypes(Resource subject, Procedure<Set<Resource>> procedure) {
2916 forTypes(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
2920 public void forSupertypes(Resource subject,
2921 AsyncListener<Set<Resource>> listener) {
2922 asyncRequest(new Types(subject), listener);
2926 public void forSupertypes(Resource subject,
2927 SyncListener<Set<Resource>> listener) {
2928 asyncRequest(new Types(subject), listener);
2932 public void forSupertypes(Resource subject, Listener<Set<Resource>> listener) {
2933 asyncRequest(new Types(subject), listener);
2937 final public void forSupertypes(final Resource subject,
2938 final AsyncProcedure<Set<Resource>> procedure) {
2940 assert (subject != null);
2941 assert (procedure != null);
2943 processor.forSupertypes(this, subject, procedure);
2948 public void forSupertypes(Resource subject,
2949 SyncProcedure<Set<Resource>> procedure) {
2950 forSupertypes(subject, new SyncToAsyncProcedure<Set<Resource>>(
2955 public void forSupertypes(Resource subject,
2956 Procedure<Set<Resource>> procedure) {
2957 forSupertypes(subject, new NoneToAsyncProcedure<Set<Resource>>(
2962 public void forDirectSuperrelations(Resource subject,
2963 AsyncMultiProcedure<Resource> procedure) {
2965 assert (subject != null);
2966 assert (procedure != null);
2968 processor.forDirectSuperrelations(this, subject, procedure);
2973 public void forPossibleSuperrelation(Resource subject, AsyncProcedure<Resource> procedure) {
2975 assert (subject != null);
2976 assert (procedure != null);
2978 processor.forPossibleSuperrelation(this, subject, procedure);
2983 public void forSuperrelations(Resource subject,
2984 AsyncListener<Set<Resource>> listener) {
2985 asyncRequest(new Types(subject), listener);
2989 public void forSuperrelations(Resource subject,
2990 SyncListener<Set<Resource>> listener) {
2991 asyncRequest(new Types(subject), listener);
2995 public void forSuperrelations(Resource subject,
2996 Listener<Set<Resource>> listener) {
2997 asyncRequest(new Types(subject), listener);
3001 final public void forSuperrelations(final Resource subject,
3002 final AsyncProcedure<Set<Resource>> procedure) {
3004 assert (subject != null);
3005 assert (procedure != null);
3007 processor.forSuperrelations(this, subject, procedure);
3012 public void forSuperrelations(Resource subject,
3013 SyncProcedure<Set<Resource>> procedure) {
3014 forSuperrelations(subject, new SyncToAsyncProcedure<Set<Resource>>(
3019 public void forSuperrelations(Resource subject,
3020 Procedure<Set<Resource>> procedure) {
3021 forSuperrelations(subject, new NoneToAsyncProcedure<Set<Resource>>(
3026 final public void forEachObject(final Resource subject, final Resource relation, final AsyncMultiProcedure<Resource> procedure) {
3027 processor.forEachObject(this, subject, relation, procedure);
3031 public void forEachObject(Resource subject, Resource relation,
3032 SyncMultiProcedure<Resource> procedure) {
3033 forEachObject(subject, relation,
3034 new SyncToAsyncMultiProcedure<Resource>(procedure));
3038 public void forEachObject(Resource subject, Resource relation,
3039 MultiProcedure<Resource> procedure) {
3041 processor.forEachObject(this, subject, relation, procedure);
3046 final public void forEachDirectPredicate(final Resource subject, final AsyncProcedure<Set<Resource>> procedure) {
3047 processor.forEachDirectPredicate(this, subject, procedure);
3051 final public void forEachDirectPredicate(final Resource subject, final SyncProcedure<Set<Resource>> procedure) {
3052 forEachDirectPredicate(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
3056 public void forEachDirectPredicate(Resource subject, Procedure<Set<Resource>> procedure) {
3057 forEachDirectPredicate(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
3061 final public void forObjectSet(final Resource subject,
3062 final Resource relation, final AsyncSetListener<Resource> procedure) {
3064 assert (subject != null);
3065 assert (relation != null);
3066 assert (procedure != null);
3068 processor.forObjectSet(this, subject, relation, procedure);
3073 final public void forObjectSet(final Resource subject,
3074 final Resource relation, final SyncSetListener<Resource> procedure) {
3076 assert (subject != null);
3077 assert (relation != null);
3078 assert (procedure != null);
3080 forObjectSet(subject, relation, new SyncToAsyncSetProcedure<Resource>(
3086 final public void forObjectSet(final Resource subject,
3087 final Resource relation, final SetListener<Resource> procedure) {
3089 assert (subject != null);
3090 assert (relation != null);
3091 assert (procedure != null);
3093 forObjectSet(subject, relation, new NoneToAsyncSetProcedure<Resource>(
3099 final public void forEachAssertedObject(final Resource subject,
3100 final Resource relation,
3101 final AsyncMultiProcedure<Resource> procedure) {
3103 assert (subject != null);
3104 assert (relation != null);
3105 assert (procedure != null);
3107 processor.forEachAssertedObject(this, subject, relation, procedure);
3112 public void forEachAssertedObject(Resource subject, Resource relation,
3113 SyncMultiProcedure<Resource> procedure) {
3115 assert (subject != null);
3116 assert (relation != null);
3117 assert (procedure != null);
3119 forEachAssertedObject(subject, relation,
3120 new SyncToAsyncMultiProcedure<Resource>(procedure));
3125 public void forEachAssertedObject(Resource subject, Resource relation,
3126 MultiProcedure<Resource> procedure) {
3128 assert (subject != null);
3129 assert (relation != null);
3130 assert (procedure != null);
3132 forEachAssertedObject(subject, relation,
3133 new NoneToAsyncMultiProcedure<Resource>(procedure));
3138 public void forAssertedObjectSet(Resource subject, Resource relation,
3139 AsyncSetListener<Resource> procedure) {
3141 assert (subject != null);
3142 assert (relation != null);
3143 assert (procedure != null);
3145 processor.forAssertedObjectSet(this, subject, relation, procedure);
3150 public void forAssertedObjectSet(Resource subject, Resource relation,
3151 SyncSetListener<Resource> procedure) {
3153 assert (subject != null);
3154 assert (relation != null);
3155 assert (procedure != null);
3157 forAssertedObjectSet(subject, relation,
3158 new SyncToAsyncSetProcedure<Resource>(procedure));
3163 public void forAssertedObjectSet(Resource subject, Resource relation,
3164 SetListener<Resource> procedure) {
3166 assert (subject != null);
3167 assert (relation != null);
3168 assert (procedure != null);
3170 forAssertedObjectSet(subject, relation,
3171 new NoneToAsyncSetProcedure<Resource>(procedure));
3176 public void forInverse(Resource relation, AsyncListener<Resource> listener) {
3177 asyncRequest(new Inverse(relation), listener);
3181 public void forInverse(Resource relation, SyncListener<Resource> listener) {
3182 asyncRequest(new Inverse(relation), listener);
3186 public void forInverse(Resource relation, Listener<Resource> listener) {
3187 asyncRequest(new Inverse(relation), listener);
3191 final public void forInverse(final Resource relation,
3192 final AsyncProcedure<Resource> procedure) {
3194 assert (relation != null);
3195 assert (procedure != null);
3197 processor.forInverse(this, relation, new AsyncProcedure<Resource>() {
3200 public void execute(AsyncReadGraph graph, Resource result) {
3202 procedure.execute(graph, result);
3204 procedure.exception(graph, new NoInverseException(relation
3210 public void exception(AsyncReadGraph graph, Throwable throwable) {
3211 procedure.exception(graph, throwable);
3215 public String toString() {
3216 return "forInverse -> " + procedure;
3224 public void forInverse(Resource relation, SyncProcedure<Resource> procedure) {
3225 forInverse(relation, new SyncToAsyncProcedure<Resource>(procedure));
3229 public void forInverse(Resource relation, Procedure<Resource> procedure) {
3230 forInverse(relation, new NoneToAsyncProcedure<Resource>(procedure));
3234 public void forSingleObject(Resource subject, Resource relation,
3235 AsyncListener<Resource> listener) {
3236 asyncRequest(new SingleObject(subject, relation), listener);
3240 public void forSingleObject(Resource subject, Resource relation,
3241 SyncListener<Resource> listener) {
3242 asyncRequest(new SingleObject(subject, relation), listener);
3246 public void forSingleObject(Resource subject, Resource relation,
3247 Listener<Resource> listener) {
3248 asyncRequest(new SingleObject(subject, relation), listener);
3252 final public void forSingleObject(final Resource subject,
3253 final Resource relation, final AsyncProcedure<Resource> procedure) {
3255 assert (subject != null);
3256 assert (relation != null);
3257 assert (procedure != null);
3259 processor.forEachObject(this, subject, relation,
3260 new SingleOrErrorProcedure<Resource>(procedure));
3265 public void forSingleObject(Resource subject, Resource relation,
3266 SyncProcedure<Resource> procedure) {
3267 forSingleObject(subject, relation, new SyncToAsyncProcedure<Resource>(
3272 public void forSingleObject(Resource subject, Resource relation,
3273 Procedure<Resource> procedure) {
3274 forSingleObject(subject, relation, new NoneToAsyncProcedure<Resource>(
3279 public void forSingleStatement(Resource subject, Resource relation,
3280 AsyncListener<Statement> listener) {
3281 asyncRequest(new SingleStatement(subject, relation), listener);
3285 public void forSingleStatement(Resource subject, Resource relation,
3286 SyncListener<Statement> listener) {
3287 asyncRequest(new SingleStatement(subject, relation), listener);
3291 public void forSingleStatement(Resource subject, Resource relation,
3292 Listener<Statement> listener) {
3293 asyncRequest(new SingleStatement(subject, relation), listener);
3297 final public void forSingleStatement(final Resource subject,
3298 final Resource relation, final AsyncProcedure<Statement> procedure) {
3300 assert (subject != null);
3301 assert (relation != null);
3302 assert (procedure != null);
3304 processor.forEachStatement(this, subject, relation,
3305 new SingleOrErrorProcedure<Statement>(procedure));
3310 public void forSingleStatement(Resource subject, Resource relation,
3311 SyncProcedure<Statement> procedure) {
3312 forSingleStatement(subject, relation,
3313 new SyncToAsyncProcedure<Statement>(procedure));
3317 public void forSingleStatement(Resource subject, Resource relation,
3318 Procedure<Statement> procedure) {
3319 forSingleStatement(subject, relation,
3320 new NoneToAsyncProcedure<Statement>(procedure));
3324 public void forSingleType(Resource subject,
3325 AsyncListener<Resource> listener) {
3326 asyncRequest(new SingleTypeAny(subject), listener);
3330 public void forSingleType(Resource subject,
3331 SyncListener<Resource> listener) {
3332 asyncRequest(new SingleTypeAny(subject), listener);
3336 public void forSingleType(Resource subject,
3337 Listener<Resource> listener) {
3338 asyncRequest(new SingleTypeAny(subject), listener);
3342 final public void forSingleType(final Resource subject, final AsyncProcedure<Resource> procedure) {
3344 assert (subject != null);
3345 assert (procedure != null);
3347 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3349 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
3352 public void execute(AsyncReadGraph graph, final Resource principalType) {
3353 checkedProcedure.offer(graph, principalType);
3357 public void finished(AsyncReadGraph graph) {
3358 checkedProcedure.dec(graph);
3362 public void exception(AsyncReadGraph graph, Throwable t) {
3363 checkedProcedure.exception(graph, t);
3367 public String toString() {
3368 return "forSingleType -> " + procedure;
3376 public void forSingleType(Resource subject, SyncProcedure<Resource> procedure) {
3377 forSingleType(subject, new SyncToAsyncProcedure<Resource>(
3382 public void forSingleType(Resource subject, Procedure<Resource> procedure) {
3383 forSingleType(subject, new NoneToAsyncProcedure<Resource>(
3388 public void forSingleType(Resource subject, Resource relation,
3389 AsyncListener<Resource> listener) {
3390 asyncRequest(new SingleType(subject, relation), listener);
3394 public void forSingleType(Resource subject, Resource relation,
3395 SyncListener<Resource> listener) {
3396 asyncRequest(new SingleType(subject, relation), listener);
3400 public void forSingleType(Resource subject, Resource relation,
3401 Listener<Resource> listener) {
3402 asyncRequest(new SingleType(subject, relation), listener);
3406 final public void forSingleType(final Resource subject,
3407 final Resource baseType, final AsyncProcedure<Resource> procedure) {
3409 assert (subject != null);
3410 assert (procedure != null);
3412 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3414 processor.forEachPrincipalType(this, subject,
3415 new AsyncMultiProcedureAdapter<Resource>() {
3418 public void execute(AsyncReadGraph graph,
3419 final Resource principalType) {
3421 checkedProcedure.inc();
3423 if(baseType == null) {
3425 checkedProcedure.offer(graph, principalType);
3426 checkedProcedure.dec(graph);
3428 } else if(principalType.equals(baseType)) {
3430 checkedProcedure.offer(graph, principalType);
3431 checkedProcedure.dec(graph);
3435 processor.forSupertypes((ReadGraphImpl)graph, principalType,
3436 new AsyncProcedure<Set<Resource>>() {
3439 public void execute(
3440 AsyncReadGraph graph,
3441 Set<Resource> result) {
3443 if (result.contains(baseType))
3444 checkedProcedure.offer(graph,
3446 checkedProcedure.dec(graph);
3451 public void exception(
3452 AsyncReadGraph graph,
3455 .exception(graph, t);
3465 public void finished(AsyncReadGraph graph) {
3466 checkedProcedure.dec(graph);
3470 public void exception(AsyncReadGraph graph, Throwable t) {
3471 checkedProcedure.exception(graph, t);
3475 public String toString() {
3476 return "forSingleType -> " + procedure;
3484 public void forSingleType(Resource subject, Resource relation,
3485 SyncProcedure<Resource> procedure) {
3486 forSingleType(subject, relation, new SyncToAsyncProcedure<Resource>(
3491 public void forSingleType(Resource subject, Resource relation,
3492 Procedure<Resource> procedure) {
3493 forSingleType(subject, relation, new NoneToAsyncProcedure<Resource>(
3498 public <T> void forValue(Resource subject, Binding binding,
3499 AsyncListener<T> listener) {
3500 asyncRequest(new Value<T>(subject, binding), listener);
3504 public <T> void forValue(Resource subject, Binding binding,
3505 SyncListener<T> listener) {
3506 asyncRequest(new Value<T>(subject, binding), listener);
3510 public <T> void forValue(Resource subject, Binding binding,
3511 Listener<T> listener) {
3512 asyncRequest(new Value<T>(subject, binding), listener);
3516 public <T> void forValue(final Resource resource, final Binding binding,
3517 final AsyncProcedure<T> procedure) {
3519 assert (resource != null);
3520 assert (binding != null);
3521 assert (procedure != null);
3523 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
3526 public void execute(AsyncReadGraph graph, byte[] result) {
3530 if (result == null) {
3531 procedure.exception(graph,
3532 new DoesNotContainValueException(
3533 "No value for resource " + resource));
3537 Serializer serializer = binding.serializer();
3538 // Serializer serializer = Bindings.getSerializer( binding );
3539 Object obj = serializer.deserialize(result);
3540 // if (!binding.isInstance(obj))
3541 // procedure.exception(graph, new ClassCastException(
3542 // "Cannot get value " + obj + " with binding "
3545 procedure.execute(graph, (T) obj);
3547 } catch (IOException e) {
3548 procedure.exception(graph, e);
3549 } catch (BufferUnderflowException e) {
3550 procedure.exception(graph, e);
3551 } catch (Throwable t) {
3552 procedure.exception(graph, t);
3558 public void exception(AsyncReadGraph graph, Throwable t) {
3560 procedure.exception(graph, t);
3561 } catch (Throwable t2) {
3562 Logger.defaultLogError(t2);
3567 public String toString() {
3568 return "forValue -> " + procedure;
3576 public <T> void forValue(Resource subject, Binding binding,
3577 SyncProcedure<T> procedure) {
3578 forValue(subject, binding, new SyncToAsyncProcedure<T>(procedure));
3582 public <T> void forValue(Resource subject, Binding binding,
3583 Procedure<T> procedure) {
3584 forValue(subject, binding, new NoneToAsyncProcedure<T>(procedure));
3588 public <T> void forValue(Resource subject, AsyncListener<T> listener) {
3589 asyncRequest(new ValueImplied<T>(subject), listener);
3593 public <T> void forValue(Resource subject, SyncListener<T> listener) {
3594 asyncRequest(new ValueImplied<T>(subject), listener);
3598 public <T> void forValue(Resource subject, Listener<T> listener) {
3599 asyncRequest(new ValueImplied<T>(subject), listener);
3603 final public <T> void forValue(final Resource subject, final AsyncProcedure<T> procedure) {
3605 assert (subject != null);
3606 assert (procedure != null);
3608 forRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
3611 public void execute(AsyncReadGraph graph, Datatype type) {
3612 // TODO: consider trying Bindings.getBeanBinding(type);
3613 Binding binding = Bindings.getBinding(type);
3614 graph.forValue(subject, binding, procedure);
3618 public void exception(AsyncReadGraph graph, Throwable throwable) {
3619 procedure.exception(graph, new DoesNotContainValueException("Invalid data type", throwable));
3627 public <T> void forValue(Resource subject, SyncProcedure<T> procedure) {
3628 forValue(subject, new SyncToAsyncProcedure<T>(procedure));
3632 public <T> void forValue(Resource subject, Procedure<T> procedure) {
3633 forValue(subject, new NoneToAsyncProcedure<T>(procedure));
3637 public <T> void forRelatedValue(Resource subject, Resource relation,
3638 AsyncListener<T> listener) {
3639 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3643 public <T> void forRelatedValue(Resource subject, Resource relation,
3644 SyncListener<T> listener) {
3645 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3649 public <T> void forRelatedValue(Resource subject, Resource relation,
3650 Listener<T> listener) {
3651 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3655 final public <T> void forRelatedValue(final Resource subject,
3656 final Resource relation, final AsyncProcedure<T> procedure) {
3658 assert (subject != null);
3659 assert (relation != null);
3660 assert (procedure != null);
3662 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3664 processor.forEachObject(this, subject, relation,
3665 new AsyncMultiProcedureAdapter<Resource>() {
3668 public void execute(AsyncReadGraph graph,
3669 final Resource object) {
3671 checkedProcedure.inc();
3673 graph.forValue(object, new AsyncProcedure<Object>() {
3676 public void execute(AsyncReadGraph graph,
3678 checkedProcedure.offer(graph, (T) result);
3679 checkedProcedure.dec(graph);
3683 public void exception(AsyncReadGraph graph,
3685 checkedProcedure.exception(graph, t);
3689 public String toString() {
3690 return "forRelatedValue -> " + procedure;
3698 public void finished(AsyncReadGraph graph) {
3699 checkedProcedure.dec(graph);
3703 public void exception(AsyncReadGraph graph, Throwable t) {
3704 checkedProcedure.exception(graph, t);
3712 public <T> void forRelatedValue(Resource subject, Resource relation,
3713 SyncProcedure<T> procedure) {
3714 forRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
3719 public <T> void forRelatedValue(Resource subject, Resource relation,
3720 Procedure<T> procedure) {
3721 forRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
3726 public <T> void forRelatedValue(Resource subject, Resource relation,
3727 Binding binding, AsyncListener<T> listener) {
3728 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3732 public <T> void forRelatedValue(Resource subject, Resource relation,
3733 Binding binding, SyncListener<T> listener) {
3734 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3738 public <T> void forRelatedValue(Resource subject, Resource relation,
3739 Binding binding, Listener<T> listener) {
3740 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3744 final public <T> void forRelatedValue(final Resource subject,
3745 final Resource relation, final Binding binding,
3746 final AsyncProcedure<T> procedure) {
3748 assert (subject != null);
3749 assert (relation != null);
3750 assert (binding != null);
3751 assert (procedure != null);
3753 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3755 processor.forEachObject(this, subject, relation,
3756 new AsyncMultiProcedureAdapter<Resource>() {
3759 public void execute(AsyncReadGraph graph,
3760 final Resource object) {
3762 checkedProcedure.inc();
3764 graph.forValue(object, binding, new AsyncProcedure<Object>() {
3767 public void execute(AsyncReadGraph graph,
3770 checkedProcedure.offer(graph,
3772 checkedProcedure.dec(graph);
3776 public void exception(AsyncReadGraph graph,
3778 checkedProcedure.exception(graph, t);
3782 public String toString() {
3783 return "forRelatedValue -> "
3792 public void finished(AsyncReadGraph graph) {
3793 checkedProcedure.dec(graph);
3797 public void exception(AsyncReadGraph graph, Throwable t) {
3798 checkedProcedure.exception(graph, t);
3806 public <T> void forRelatedValue(Resource subject, Resource relation,
3807 Binding binding, SyncProcedure<T> procedure) {
3808 forRelatedValue(subject, relation, binding,
3809 new SyncToAsyncProcedure<T>(procedure));
3813 public <T> void forRelatedValue(Resource subject, Resource relation,
3814 Binding binding, Procedure<T> procedure) {
3815 forRelatedValue(subject, relation, binding,
3816 new NoneToAsyncProcedure<T>(procedure));
3820 public <T> void forAdapted(Resource resource, Class<T> clazz,
3821 AsyncListener<T> listener) {
3822 asyncRequest(new Adapter<T>(resource, clazz), listener);
3826 public <T> void forAdapted(Resource resource, Class<T> clazz,
3827 SyncListener<T> listener) {
3828 asyncRequest(new Adapter<T>(resource, clazz), listener);
3832 public <T> void forAdapted(Resource resource, Class<T> clazz,
3833 Listener<T> listener) {
3834 asyncRequest(new Adapter<T>(resource, clazz), listener);
3838 final public <T> void forAdapted(final Resource resource,
3839 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3841 assert (resource != null);
3842 assert (clazz != null);
3843 assert (procedure != null);
3845 final AdaptionService service = getSession().peekService(AdaptionService.class);
3846 if (service == null)
3847 procedure.exception(this, new ServiceException("No AdaptionService available"));
3849 service.adapt(this, resource, resource, Resource.class, clazz, false, procedure);
3854 public <T> void forAdapted(Resource resource, Class<T> clazz,
3855 SyncProcedure<T> procedure) {
3856 forAdapted(resource, clazz, new SyncToAsyncProcedure<T>(procedure));
3860 public <T> void forAdapted(Resource resource, Class<T> clazz,
3861 Procedure<T> procedure) {
3862 forAdapted(resource, clazz, new NoneToAsyncProcedure<T>(procedure));
3866 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3867 AsyncListener<T> listener) {
3868 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3872 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3873 SyncListener<T> listener) {
3874 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3878 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3879 Listener<T> listener) {
3880 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3884 final public <T> void forUniqueAdapted(final Resource resource,
3885 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3887 assert (resource != null);
3888 assert (clazz != null);
3889 assert (procedure != null);
3891 final AdaptionService service = getSession().peekService(AdaptionService.class);
3892 if (service == null)
3893 procedure.exception(this, new ServiceException("No AdaptionService available"));
3895 service.adaptNew(this, resource, clazz, false, procedure);
3900 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3901 SyncProcedure<T> procedure) {
3902 forUniqueAdapted(resource, clazz,
3903 new SyncToAsyncProcedure<T>(procedure));
3907 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3908 Procedure<T> procedure) {
3909 forUniqueAdapted(resource, clazz,
3910 new NoneToAsyncProcedure<T>(procedure));
3914 public void forPossibleInverse(Resource subject,
3915 AsyncListener<Resource> listener) {
3916 asyncRequest(new PossibleInverse(subject), listener);
3920 public void forPossibleInverse(Resource subject,
3921 SyncListener<Resource> listener) {
3922 asyncRequest(new PossibleInverse(subject), listener);
3926 public void forPossibleInverse(Resource subject, Listener<Resource> listener) {
3927 asyncRequest(new PossibleInverse(subject), listener);
3931 final public void forPossibleInverse(final Resource relation,
3932 final AsyncProcedure<Resource> procedure) {
3934 assert (relation != null);
3935 assert (procedure != null);
3937 processor.forInverse(this, relation, new ExceptionToNullProcedure<Resource>(procedure));
3942 public void forPossibleInverse(Resource subject,
3943 SyncProcedure<Resource> procedure) {
3944 forPossibleInverse(subject, new SyncToAsyncProcedure<Resource>(
3949 public void forPossibleInverse(Resource subject,
3950 Procedure<Resource> procedure) {
3951 forPossibleInverse(subject, new NoneToAsyncProcedure<Resource>(
3956 public void forPossibleObject(Resource subject, Resource relation,
3957 AsyncListener<Resource> listener) {
3958 asyncRequest(new PossibleObject(subject, relation), listener);
3962 public void forPossibleObject(Resource subject, Resource relation,
3963 SyncListener<Resource> listener) {
3964 asyncRequest(new PossibleObject(subject, relation), listener);
3968 public void forPossibleObject(Resource subject, Resource relation,
3969 Listener<Resource> listener) {
3970 asyncRequest(new PossibleObject(subject, relation), listener);
3974 final public void forPossibleObject(final Resource subject,
3975 final Resource relation, final AsyncProcedure<Resource> procedure) {
3977 assert (subject != null);
3978 assert (relation != null);
3979 assert (procedure != null);
3981 processor.forEachObject(this, subject, relation,
3982 new SingleOrNullProcedure<Resource>(procedure));
3987 public void forPossibleObject(Resource subject, Resource relation,
3988 SyncProcedure<Resource> procedure) {
3989 forPossibleObject(subject, relation,
3990 new SyncToAsyncProcedure<Resource>(procedure));
3994 public void forPossibleObject(Resource subject, Resource relation,
3995 Procedure<Resource> procedure) {
3996 forPossibleObject(subject, relation,
3997 new NoneToAsyncProcedure<Resource>(procedure));
4001 public void forPossibleStatement(Resource subject, Resource relation,
4002 AsyncListener<Statement> listener) {
4003 asyncRequest(new PossibleStatement(subject, relation), listener);
4007 public void forPossibleStatement(Resource subject, Resource relation,
4008 SyncListener<Statement> listener) {
4009 asyncRequest(new PossibleStatement(subject, relation), listener);
4013 public void forPossibleStatement(Resource subject, Resource relation,
4014 Listener<Statement> listener) {
4015 asyncRequest(new PossibleStatement(subject, relation), listener);
4019 final public void forPossibleStatement(final Resource subject,
4020 final Resource relation, final AsyncProcedure<Statement> procedure) {
4022 assert (subject != null);
4023 assert (relation != null);
4024 assert (procedure != null);
4026 processor.forEachStatement(this, subject, relation,
4027 new SingleFunctionalOrNullProcedure<Statement>(
4028 "forPossibleStatement", procedure));
4033 public void forPossibleStatement(Resource subject, Resource relation,
4034 SyncProcedure<Statement> procedure) {
4035 forPossibleStatement(subject, relation,
4036 new SyncToAsyncProcedure<Statement>(procedure));
4040 public void forPossibleStatement(Resource subject, Resource relation,
4041 Procedure<Statement> procedure) {
4042 forPossibleStatement(subject, relation,
4043 new NoneToAsyncProcedure<Statement>(procedure));
4047 public void forPossibleType(Resource subject, Resource relation,
4048 AsyncListener<Resource> listener) {
4049 asyncRequest(new PossibleType(subject, relation), listener);
4053 public void forPossibleType(Resource subject, Resource relation,
4054 SyncListener<Resource> listener) {
4055 asyncRequest(new PossibleType(subject, relation), listener);
4059 public void forPossibleType(Resource subject, Resource relation,
4060 Listener<Resource> listener) {
4061 asyncRequest(new PossibleType(subject, relation), listener);
4065 final public void forPossibleType(final Resource subject,
4066 final Resource baseType, final AsyncProcedure<Resource> procedure) {
4068 assert (subject != null);
4069 assert (procedure != null);
4071 final NullSingleOrNullProcedure<Resource> checkedProcedure = new NullSingleOrNullProcedure<Resource>(procedure);
4073 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
4076 public void execute(AsyncReadGraph graph,
4077 final Resource principalType) {
4079 if (baseType == null) {
4081 checkedProcedure.offer(graph, principalType);
4083 } else if (principalType.equals(baseType)) {
4085 checkedProcedure.offer(graph, principalType);
4089 checkedProcedure.inc();
4091 processor.forSupertypes((ReadGraphImpl)graph, principalType,
4092 new AsyncProcedure<Set<Resource>>() {
4095 public void execute(
4096 AsyncReadGraph graph,
4097 Set<Resource> result) {
4099 if (result.contains(baseType)) {
4100 checkedProcedure.offer(graph,
4104 checkedProcedure.dec(graph);
4109 public void exception(
4110 AsyncReadGraph graph,
4112 checkedProcedure.exception(graph, t);
4113 checkedProcedure.dec(graph);
4117 public String toString() {
4118 return "forPossibleType -> "
4129 public void finished(AsyncReadGraph graph) {
4130 checkedProcedure.dec(graph);
4134 public void exception(AsyncReadGraph graph, Throwable t) {
4135 checkedProcedure.exception(graph, t);
4136 checkedProcedure.dec(graph);
4144 public void forPossibleType(Resource subject, Resource relation,
4145 SyncProcedure<Resource> procedure) {
4146 forPossibleType(subject, relation, new SyncToAsyncProcedure<Resource>(
4151 public void forPossibleType(Resource subject, Resource relation,
4152 Procedure<Resource> procedure) {
4153 forPossibleType(subject, relation, new NoneToAsyncProcedure<Resource>(
4158 public <T> void forPossibleValue(Resource subject, AsyncListener<T> listener) {
4159 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4163 public <T> void forPossibleValue(Resource subject, SyncListener<T> listener) {
4164 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4168 public <T> void forPossibleValue(Resource subject, Listener<T> listener) {
4169 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4173 final public <T> void forPossibleValue(final Resource subject,
4174 final AsyncProcedure<T> procedure) {
4176 assert (subject != null);
4177 assert (procedure != null);
4179 forPossibleRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
4182 public void execute(AsyncReadGraph graph, final Datatype type) {
4184 procedure.execute(graph, null);
4187 // TODO: consider trying Bindings.getBeanBinding(type);
4188 Binding binding = Bindings.getBinding(type);
4189 graph.forPossibleValue(subject, binding, procedure);
4190 } catch (RuntimeBindingConstructionException e) {
4191 procedure.exception(graph, e);
4197 public void exception(AsyncReadGraph graph, Throwable t) {
4198 procedure.exception(graph, t);
4202 public String toString() {
4203 return "forPossibleValue -> " + procedure;
4211 public <T> void forPossibleValue(Resource subject,
4212 SyncProcedure<T> procedure) {
4213 forPossibleValue(subject, new SyncToAsyncProcedure<T>(procedure));
4217 public <T> void forPossibleValue(Resource subject, Procedure<T> procedure) {
4218 forPossibleValue(subject, new NoneToAsyncProcedure<T>(procedure));
4222 public <T> void forPossibleValue(Resource subject, Binding binding,
4223 AsyncListener<T> listener) {
4224 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4228 public <T> void forPossibleValue(Resource subject, Binding binding,
4229 SyncListener<T> listener) {
4230 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4234 public <T> void forPossibleValue(Resource subject, Binding binding,
4235 Listener<T> listener) {
4236 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4240 final public <T> void forPossibleValue(final Resource resource,
4241 final Binding binding, final AsyncProcedure<T> procedure) {
4243 assert (resource != null);
4244 assert (binding != null);
4245 assert (procedure != null);
4247 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
4250 public void execute(AsyncReadGraph graph, byte[] result) {
4254 if (result == null) {
4255 procedure.execute(graph, null);
4259 Serializer serializer = Bindings.getSerializer( binding );
4260 Object obj = serializer.deserialize(result);
4261 if (!binding.isInstance(obj))
4262 procedure.exception(graph, new ClassCastException(
4263 "Cannot get value " + obj + " with binding "
4266 procedure.execute(graph, (T) obj);
4268 } catch (IOException e) {
4269 procedure.exception(graph, e);
4270 } catch (BufferUnderflowException e) {
4271 procedure.exception(graph, e);
4272 } catch (Throwable t) {
4273 procedure.exception(graph, t);
4279 public void exception(AsyncReadGraph graph, Throwable t) {
4281 procedure.exception(graph, t);
4282 } catch (Throwable t2) {
4283 Logger.defaultLogError(t2);
4288 public String toString() {
4289 return "forPossibleValue -> " + procedure;
4297 public <T> void forPossibleValue(Resource subject, Binding binding,
4298 SyncProcedure<T> procedure) {
4299 forPossibleValue(subject, binding, new SyncToAsyncProcedure<T>(
4304 public <T> void forPossibleValue(Resource subject, Binding binding,
4305 Procedure<T> procedure) {
4306 forPossibleValue(subject, binding, new NoneToAsyncProcedure<T>(
4311 public <T> void forPossibleRelatedValue(Resource subject,
4312 Resource relation, AsyncListener<T> listener) {
4313 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4318 public <T> void forPossibleRelatedValue(Resource subject,
4319 Resource relation, SyncListener<T> listener) {
4320 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4325 public <T> void forPossibleRelatedValue(Resource subject,
4326 Resource relation, Listener<T> listener) {
4327 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4332 final public <T> void forPossibleRelatedValue(final Resource subject,
4333 final Resource relation, final AsyncProcedure<T> procedure) {
4335 assert (subject != null);
4336 assert (relation != null);
4337 assert (procedure != null);
4339 final DeepSingleOrNullProcedure<T> checkedProcedure = new DeepSingleOrNullProcedure<T>(procedure);
4341 processor.forEachObject(this, subject, relation,
4342 new AsyncMultiProcedureAdapter<Resource>() {
4345 public void execute(AsyncReadGraph graph,
4346 final Resource object) {
4348 checkedProcedure.inc();
4350 graph.forValue(object, new AsyncProcedure<Object>() {
4353 public void execute(AsyncReadGraph graph,
4355 checkedProcedure.offer(graph, (T) result);
4356 checkedProcedure.dec(graph);
4360 public void exception(AsyncReadGraph graph,
4362 checkedProcedure.exception(graph, t);
4363 checkedProcedure.dec(graph);
4371 public void finished(AsyncReadGraph graph) {
4373 checkedProcedure.dec(graph);
4377 public void exception(AsyncReadGraph graph, Throwable t) {
4378 checkedProcedure.exception(graph, t);
4379 checkedProcedure.dec(graph);
4383 public String toString() {
4384 return "forPossibleRelatedValue -> " + procedure;
4391 public <T> void forPossibleRelatedValue(Resource subject,
4392 Resource relation, SyncProcedure<T> procedure) {
4393 forPossibleRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
4398 public <T> void forPossibleRelatedValue(Resource subject,
4399 Resource relation, Procedure<T> procedure) {
4400 forPossibleRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
4405 public <T> void forPossibleRelatedValue(Resource subject,
4406 Resource relation, Binding binding, AsyncListener<T> listener) {
4407 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4412 public <T> void forPossibleRelatedValue(Resource subject,
4413 Resource relation, Binding binding, SyncListener<T> listener) {
4414 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4419 public <T> void forPossibleRelatedValue(Resource subject,
4420 Resource relation, Binding binding, Listener<T> listener) {
4421 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4425 final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4426 final AsyncProcedure<T> procedure) {
4428 assert (subject != null);
4429 assert (relation != null);
4430 assert (procedure != null);
4432 processor.forPossibleObject(this, subject, relation, new AsyncProcedure<Resource>() {
4435 public void execute(AsyncReadGraph graph, Resource object) {
4437 if(object == null) {
4438 procedure.execute(graph, null);
4442 processor.forPossibleValue((ReadGraphImpl)graph, object, new AsyncProcedure<byte[]>() {
4445 public void execute(AsyncReadGraph graph, byte[] bytes) {
4451 Serializer serializer = binding.serializer();
4452 Object obj = serializer.deserialize(bytes);
4453 if (!binding.isInstance(obj)) {
4454 procedure.exception(graph, new ClassCastException("Cannot get value " + obj + " with binding " + binding));
4456 procedure.execute(graph, (T) obj);
4459 } catch (Throwable t) {
4461 procedure.exception(graph, t);
4467 procedure.execute(graph, null);
4474 public void exception(AsyncReadGraph graph, Throwable t) {
4475 procedure.exception(graph, t);
4483 public void exception(AsyncReadGraph graph, Throwable throwable) {
4484 throwable.printStackTrace();
4485 procedure.exception(graph, throwable);
4493 public <T> void forPossibleRelatedValue(Resource subject,
4494 Resource relation, Binding binding, SyncProcedure<T> procedure) {
4495 forPossibleRelatedValue(subject, relation, binding,
4496 new SyncToAsyncProcedure<T>(procedure));
4500 public <T> void forPossibleRelatedValue(Resource subject,
4501 Resource relation, Binding binding, Procedure<T> procedure) {
4502 forPossibleRelatedValue(subject, relation, binding,
4503 new NoneToAsyncProcedure<T>(procedure));
4507 public void forIsInstanceOf(Resource subject, Resource relation,
4508 AsyncListener<Boolean> listener) {
4509 asyncRequest(new IsInstanceOf(subject, relation), listener);
4513 public void forIsInstanceOf(Resource subject, Resource relation,
4514 SyncListener<Boolean> listener) {
4515 asyncRequest(new IsInstanceOf(subject, relation), listener);
4519 public void forIsInstanceOf(Resource subject, Resource relation,
4520 Listener<Boolean> listener) {
4521 asyncRequest(new IsInstanceOf(subject, relation), listener);
4525 final public void forIsInstanceOf(final Resource resource,
4526 final Resource type, final AsyncProcedure<Boolean> procedure) {
4528 assert (resource != null);
4529 assert (type != null);
4530 assert (procedure != null);
4532 forTypes(resource, new AsyncProcedure<Set<Resource>>() {
4535 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4538 if (result.contains(type))
4539 procedure.execute(graph, true);
4541 procedure.execute(graph, false);
4542 } catch (Throwable t) {
4543 Logger.defaultLogError(t);
4548 public void exception(AsyncReadGraph graph, Throwable t) {
4550 procedure.exception(graph, t);
4551 } catch (Throwable t2) {
4552 Logger.defaultLogError(t2);
4557 public String toString() {
4558 return "forIsInstanceOf -> " + procedure;
4566 public void forIsInstanceOf(Resource subject, Resource relation,
4567 SyncProcedure<Boolean> procedure) {
4568 forIsInstanceOf(subject, relation, new SyncToAsyncProcedure<Boolean>(
4573 public void forIsInstanceOf(Resource subject, Resource relation,
4574 Procedure<Boolean> procedure) {
4575 forIsInstanceOf(subject, relation, new NoneToAsyncProcedure<Boolean>(
4580 public void forIsInheritedFrom(Resource subject, Resource relation,
4581 AsyncListener<Boolean> listener) {
4582 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4586 public void forIsInheritedFrom(Resource subject, Resource relation,
4587 SyncListener<Boolean> listener) {
4588 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4592 public void forIsInheritedFrom(Resource subject, Resource relation,
4593 Listener<Boolean> listener) {
4594 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4598 final public void forIsInheritedFrom(final Resource resource,
4599 final Resource type, final AsyncProcedure<Boolean> procedure) {
4601 assert (resource != null);
4602 assert (type != null);
4603 assert (procedure != null);
4605 if (resource.equals(type)) {
4607 procedure.execute(this, true);
4608 } catch (Throwable t) {
4609 Logger.defaultLogError(t);
4614 forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
4617 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4619 if (result.contains(type))
4620 procedure.execute(graph, true);
4622 procedure.execute(graph, false);
4623 } catch (Throwable t) {
4624 Logger.defaultLogError(t);
4629 public void exception(AsyncReadGraph graph, Throwable t) {
4631 procedure.exception(graph, t);
4632 } catch (Throwable t2) {
4633 Logger.defaultLogError(t2);
4638 public String toString() {
4639 return "forIsInheritedFrom -> " + procedure;
4647 public void forIsInheritedFrom(Resource subject, Resource relation,
4648 SyncProcedure<Boolean> procedure) {
4649 forIsInheritedFrom(subject, relation,
4650 new SyncToAsyncProcedure<Boolean>(procedure));
4654 public void forIsInheritedFrom(Resource subject, Resource relation,
4655 Procedure<Boolean> procedure) {
4656 forIsInheritedFrom(subject, relation,
4657 new NoneToAsyncProcedure<Boolean>(procedure));
4661 public void forIsSubrelationOf(Resource subject, Resource relation,
4662 AsyncListener<Boolean> listener) {
4663 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4667 public void forIsSubrelationOf(Resource subject, Resource relation,
4668 SyncListener<Boolean> listener) {
4669 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4673 public void forIsSubrelationOf(Resource subject, Resource relation,
4674 Listener<Boolean> listener) {
4675 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4679 final public void forIsSubrelationOf(final Resource resource,
4680 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4682 assert (resource != null);
4683 assert (relation != null);
4684 assert (procedure != null);
4686 if (resource.equals(relation)) {
4687 procedure.execute(this, true);
4691 forSuperrelations(resource, new AsyncProcedure<Set<Resource>>() {
4694 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4696 if (result.contains(relation))
4697 procedure.execute(graph, true);
4699 procedure.execute(graph, false);
4700 } catch (Throwable t) {
4701 Logger.defaultLogError(t);
4706 public void exception(AsyncReadGraph graph, Throwable t) {
4708 procedure.exception(graph, t);
4709 } catch (Throwable t2) {
4710 Logger.defaultLogError(t2);
4715 public String toString() {
4716 return "forIsSubrelationOf -> " + procedure;
4724 public void forIsSubrelationOf(Resource subject, Resource relation,
4725 SyncProcedure<Boolean> procedure) {
4726 forIsSubrelationOf(subject, relation,
4727 new SyncToAsyncProcedure<Boolean>(procedure));
4731 public void forIsSubrelationOf(Resource subject, Resource relation,
4732 Procedure<Boolean> procedure) {
4733 forIsSubrelationOf(subject, relation,
4734 new NoneToAsyncProcedure<Boolean>(procedure));
4738 public void forHasStatement(Resource subject,
4739 AsyncListener<Boolean> listener) {
4740 asyncRequest(new HasStatementSubject(subject), listener);
4744 public void forHasStatement(Resource subject, SyncListener<Boolean> listener) {
4745 asyncRequest(new HasStatementSubject(subject), listener);
4749 public void forHasStatement(Resource subject, Listener<Boolean> listener) {
4750 asyncRequest(new HasStatementSubject(subject), listener);
4754 final public void forHasStatement(final Resource subject,
4755 final AsyncProcedure<Boolean> procedure) {
4757 assert (subject != null);
4758 assert (procedure != null);
4760 processor.forHasStatement(this, subject, procedure);
4765 public void forHasStatement(Resource subject,
4766 SyncProcedure<Boolean> procedure) {
4767 forHasStatement(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4771 public void forHasStatement(Resource subject, Procedure<Boolean> procedure) {
4772 forHasStatement(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4776 public void forHasStatement(Resource subject, Resource relation,
4777 AsyncListener<Boolean> listener) {
4778 asyncRequest(new HasStatement(subject, relation), listener);
4782 public void forHasStatement(Resource subject, Resource relation,
4783 SyncListener<Boolean> listener) {
4784 asyncRequest(new HasStatement(subject, relation), listener);
4788 public void forHasStatement(Resource subject, Resource relation,
4789 Listener<Boolean> listener) {
4790 asyncRequest(new HasStatement(subject, relation), listener);
4794 final public void forHasStatement(final Resource subject,
4795 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4797 assert (subject != null);
4798 assert (relation != null);
4799 assert (procedure != null);
4801 processor.forHasStatement(this, subject, relation, procedure);
4806 public void forHasStatement(Resource subject, Resource relation,
4807 SyncProcedure<Boolean> procedure) {
4808 forHasStatement(subject, relation, new SyncToAsyncProcedure<Boolean>(
4813 public void forHasStatement(Resource subject, Resource relation,
4814 Procedure<Boolean> procedure) {
4815 forHasStatement(subject, relation, new NoneToAsyncProcedure<Boolean>(
4820 public void forHasStatement(Resource subject, Resource relation,
4821 Resource object, AsyncListener<Boolean> listener) {
4822 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4827 public void forHasStatement(Resource subject, Resource relation,
4828 Resource object, SyncListener<Boolean> listener) {
4829 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4834 public void forHasStatement(Resource subject, Resource relation,
4835 Resource object, Listener<Boolean> listener) {
4836 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4841 final public void forHasStatement(final Resource subject,
4842 final Resource relation, final Resource object,
4843 final AsyncProcedure<Boolean> procedure) {
4845 assert (subject != null);
4846 assert (relation != null);
4847 assert (object != null);
4848 assert (procedure != null);
4850 processor.forHasStatement(this, subject, relation, object, procedure);
4855 public void forHasStatement(Resource subject, Resource relation,
4856 Resource object, SyncProcedure<Boolean> procedure) {
4857 forHasStatement(subject, relation, object,
4858 new SyncToAsyncProcedure<Boolean>(procedure));
4862 public void forHasStatement(Resource subject, Resource relation,
4863 Resource object, Procedure<Boolean> procedure) {
4864 forHasStatement(subject, relation, object,
4865 new NoneToAsyncProcedure<Boolean>(procedure));
4869 public void forHasValue(Resource subject, AsyncListener<Boolean> listener) {
4870 asyncRequest(new HasValue(subject), listener);
4874 public void forHasValue(Resource subject, SyncListener<Boolean> listener) {
4875 asyncRequest(new HasValue(subject), listener);
4879 public void forHasValue(Resource subject, Listener<Boolean> listener) {
4880 asyncRequest(new HasValue(subject), listener);
4884 final public void forHasValue(final Resource subject,
4885 final AsyncProcedure<Boolean> procedure) {
4887 assert (subject != null);
4888 assert (procedure != null);
4890 processor.forValue(this, subject, new AsyncProcedure<byte[]>() {
4893 public void execute(AsyncReadGraph graph, byte[] result) {
4896 procedure.execute(graph, false);
4898 procedure.execute(graph, true);
4899 } catch (Throwable t) {
4900 Logger.defaultLogError(t);
4905 public void exception(AsyncReadGraph graph, Throwable t) {
4907 procedure.exception(graph, t);
4908 } catch (Throwable t2) {
4909 Logger.defaultLogError(t2);
4914 public String toString() {
4915 return "forHasValue -> " + procedure;
4923 public void forHasValue(Resource subject, SyncProcedure<Boolean> procedure) {
4924 forHasValue(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4928 public void forHasValue(Resource subject, Procedure<Boolean> procedure) {
4929 forHasValue(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4933 public void forOrderedSet(Resource subject,
4934 AsyncMultiListener<Resource> listener) {
4935 asyncRequest(new OrderedSet(subject), listener);
4939 public void forOrderedSet(Resource subject,
4940 SyncMultiListener<Resource> listener) {
4941 asyncRequest(new OrderedSet(subject), listener);
4945 public void forOrderedSet(Resource subject, MultiListener<Resource> listener) {
4946 asyncRequest(new OrderedSet(subject), listener);
4950 final public void forOrderedSet(final Resource subject,
4951 final AsyncMultiProcedure<Resource> procedure) {
4953 assert (subject != null);
4954 assert (procedure != null);
4956 processor.forOrderedSet(this, subject,
4957 new AsyncMultiProcedure<Resource>() {
4960 public void finished(AsyncReadGraph graph) {
4962 procedure.finished(graph);
4963 } catch (Throwable t) {
4964 Logger.defaultLogError(t);
4969 public void execute(AsyncReadGraph graph, Resource result) {
4971 procedure.execute(graph, result);
4972 } catch (Throwable t) {
4973 Logger.defaultLogError(t);
4978 public void exception(AsyncReadGraph graph, Throwable t) {
4980 procedure.exception(graph, t);
4981 } catch (Throwable t2) {
4982 Logger.defaultLogError(t2);
4987 public String toString() {
4988 return "forOrderedSet -> " + procedure;
4996 public void forOrderedSet(Resource subject,
4997 SyncMultiProcedure<Resource> procedure) {
4998 forOrderedSet(subject, new SyncToAsyncMultiProcedure<Resource>(
5003 public void forOrderedSet(Resource subject,
5004 MultiProcedure<Resource> procedure) {
5005 forOrderedSet(subject, new NoneToAsyncMultiProcedure<Resource>(
5010 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5011 AsyncListener<T> listener) {
5012 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5016 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5017 SyncListener<T> listener) {
5018 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5022 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5023 Listener<T> listener) {
5024 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5028 final public <T> void forPossibleAdapted(final Resource resource,
5029 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5031 assert (resource != null);
5032 assert (clazz != null);
5033 assert (procedure != null);
5035 final AdaptionService service = getSession().peekService(AdaptionService.class);
5036 if (service == null)
5037 procedure.exception(this, new ServiceException("No AdaptionService available"));
5039 service.adapt(this, resource, resource, Resource.class, clazz, true, procedure);
5043 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5044 SyncProcedure<T> procedure) {
5045 forPossibleAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5050 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5051 Procedure<T> procedure) {
5052 forPossibleAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5057 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5058 AsyncListener<T> listener) {
5059 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5063 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5064 SyncListener<T> listener) {
5065 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5069 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5070 Listener<T> listener) {
5071 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5075 final public <T> void forPossibleUniqueAdapted(final Resource resource,
5076 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5078 assert (resource != null);
5079 assert (clazz != null);
5080 assert (procedure != null);
5082 final AdaptionService service = getSession().peekService(AdaptionService.class);
5083 if (service == null)
5084 procedure.exception(this, new ServiceException("No AdaptionService available"));
5086 service.adaptNew(this, resource, clazz, true, procedure);
5091 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5092 SyncProcedure<T> procedure) {
5093 forPossibleUniqueAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5098 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5099 Procedure<T> procedure) {
5100 forPossibleUniqueAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5105 * Implementation of the interface AsyncRequestProcessor
5109 final public Session getSession() {
5110 return processor.getSession();
5114 public <T> void asyncRequest(final Read<T> request) {
5116 asyncRequest(request, new AsyncProcedure<T>() {
5119 public void execute(AsyncReadGraph graph, T result) {
5123 public void exception(AsyncReadGraph graph, Throwable t) {
5124 Logger.defaultLogError(t);
5128 public String toString() {
5129 return "asyncRequest(Read) -> " + request;
5137 public <T> void asyncRequest(Read<T> request, AsyncListener<T> procedure) {
5138 asyncRequest(request, (AsyncProcedure<T>) procedure);
5142 public <T> void asyncRequest(Read<T> request,
5143 final SyncListener<T> procedure) {
5144 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5148 public <T> void asyncRequest(Read<T> request, final Listener<T> procedure) {
5149 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5153 public <T> void asyncRequest(final Read<T> request, final AsyncProcedure<T> procedure) {
5155 assert (request != null);
5156 assert (procedure != null);
5158 processor.schedule(new SessionTask(this) {
5161 public void run(int thread) {
5163 final ListenerBase listener = getListenerBase(procedure);
5164 QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5165 } catch (DatabaseException e) {
5166 Logger.defaultLogError(e);
5174 public static ReadGraphImpl createAsync(QueryProcessor support) {
5175 return new ReadGraphImpl(null, null, support);
5179 public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
5180 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5184 public <T> void asyncRequest(Read<T> request, final Procedure<T> procedure) {
5185 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5189 final public <T> void asyncRequest(final AsyncRead<T> request) {
5191 assert (request != null);
5193 asyncRequest(request, new AsyncProcedure<T>() {
5196 public void execute(AsyncReadGraph graph, T result) {
5200 public void exception(AsyncReadGraph graph, Throwable t) {
5201 Logger.defaultLogError(t);
5205 public String toString() {
5206 return "asyncRequest(AsyncRead) -> " + request;
5214 public <T> void asyncRequest(AsyncRead<T> request,
5215 AsyncListener<T> procedure) {
5216 asyncRequest(request, (AsyncProcedure<T>) procedure);
5220 final public <T> void asyncRequest(AsyncRead<T> request,
5221 final SyncListener<T> procedure) {
5222 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5226 final public <T> void asyncRequest(AsyncRead<T> request,
5227 final Listener<T> procedure) {
5228 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5232 final public <T> void asyncRequest(final AsyncRead<T> request,
5233 final AsyncProcedure<T> procedure) {
5235 assert (request != null);
5236 assert (procedure != null);
5238 processor.schedule(new SessionTask(this) {
5241 public void run(int thread) {
5243 final ListenerBase listener = getListenerBase(procedure);
5244 QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5245 } catch (DatabaseException e) {
5246 Logger.defaultLogError(e);
5255 public <T> void asyncRequest(AsyncRead<T> request,
5256 SyncProcedure<T> procedure) {
5257 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5261 final public <T> void asyncRequest(final AsyncRead<T> request,
5262 final Procedure<T> procedure) {
5263 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5267 public <T> void asyncRequest(final MultiRead<T> request) {
5269 assert (request != null);
5271 asyncRequest(request, new SyncMultiProcedureAdapter<T>() {
5273 public void exception(ReadGraph graph, Throwable t) {
5274 Logger.defaultLogError(t);
5278 public String toString() {
5279 return "asyncRequest(MultiRead) -> " + request;
5286 public <T> void asyncRequest(MultiRead<T> request,
5287 SyncMultiListener<T> procedure) {
5288 asyncRequest(request, (SyncMultiProcedure<T>)procedure);
5292 public <T> void asyncRequest(MultiRead<T> request,
5293 MultiListener<T> procedure) {
5294 asyncRequest(request, new NoneToSyncMultiListener<T>(procedure));
5299 public <T> void asyncRequest(final MultiRead<T> request,
5300 final SyncMultiProcedure<T> procedure) {
5302 assert (request != null);
5303 assert (procedure != null);
5305 final ListenerBase listener = getListenerBase(procedure);
5307 if (parent != null || listener != null) {
5309 // final ReadGraphImpl newGraph = newSync();
5310 processor.query(this, request, parent, procedure,listener);
5314 // final ReadGraphImpl newGraph = newSync();
5318 request.perform(this, procedure);
5320 } catch (Throwable t) {
5323 procedure.exception(this, t);
5324 } catch (DatabaseException e) {
5325 LOGGER.error("Unexpected exception while handling exception", e);
5335 public <T> void asyncRequest(MultiRead<T> request,
5336 MultiProcedure<T> procedure) {
5337 asyncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
5341 final public <T> void asyncRequest(final AsyncMultiRead<T> request) {
5343 assert (request != null);
5345 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5347 public void exception(AsyncReadGraph graph, Throwable t) {
5348 Logger.defaultLogError(t);
5352 public String toString() {
5353 return "asyncRequest(AsyncMultiRead) -> " + request;
5360 public <T> void asyncRequest(AsyncMultiRead<T> request,
5361 AsyncMultiListener<T> procedure) {
5362 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5366 public <T> void asyncRequest(AsyncMultiRead<T> request,
5367 SyncMultiListener<T> procedure) {
5368 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5372 public <T> void asyncRequest(AsyncMultiRead<T> request,
5373 MultiListener<T> procedure) {
5374 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5378 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5379 final AsyncMultiProcedure<T> procedure) {
5381 assert (request != null);
5382 assert (procedure != null);
5384 ListenerBase listener = getListenerBase(procedure);
5386 if (parent != null || listener != null) {
5388 processor.query(this, request, parent, procedure, listener);
5394 request.perform(this, new AsyncMultiProcedure<T>() {
5397 public void execute(AsyncReadGraph graph, T result) {
5398 procedure.execute(graph, result);
5402 public void finished(AsyncReadGraph graph) {
5403 procedure.finished(graph);
5407 public void exception(AsyncReadGraph graph, Throwable t) {
5408 procedure.exception(graph, t);
5412 public String toString() {
5413 return "asyncRequest(AsyncMultiRead) -> " + procedure;
5418 } catch (Throwable t) {
5420 procedure.exception(this, new DatabaseException(t));
5428 public <T> void asyncRequest(AsyncMultiRead<T> request,
5429 SyncMultiProcedure<T> procedure) {
5430 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5434 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5435 final MultiProcedure<T> procedure) {
5436 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5440 final public <T> void asyncRequest(final ExternalRead<T> request) {
5442 assert (request != null);
5444 asyncRequest(request, new Procedure<T>() {
5447 public void execute(T result) {
5451 public void exception(Throwable t) {
5452 Logger.defaultLogError(t);
5456 public String toString() {
5457 return "asyncRequest(PrimitiveRead) -> " + request;
5465 public <T> void asyncRequest(ExternalRead<T> request,
5466 final Listener<T> procedure) {
5467 asyncRequest(request, (Procedure<T>) procedure);
5471 final public <T> void asyncRequest(final ExternalRead<T> request,
5472 final Procedure<T> procedure) {
5474 assert (request != null);
5475 assert (procedure != null);
5477 final ListenerBase listener = getListenerBase(procedure);
5479 if (parent != null || listener != null) {
5482 QueryCacheBase.resultExternalReadEntry(this, request, parent, listener, procedure);
5483 } catch (DatabaseException e) {
5484 Logger.defaultLogError(e);
5485 // This throwable has already been transferred to procedure at this point - do nothing about it
5490 request.register(this, new Listener<T>() {
5492 public void execute(T result) {
5493 procedure.execute(result);
5497 public void exception(Throwable t) {
5498 procedure.exception(t);
5502 public String toString() {
5503 return "asyncRequest(PrimitiveRead) -> " + request;
5507 public boolean isDisposed() {
5518 public void asyncRequest(final Write request) {
5520 assert (request != null);
5522 getSession().asyncRequest(request);
5524 // processor.asyncWrite(request);
5529 public <T> void asyncRequest(final WriteResult<T> request, Procedure<T> procedure) {
5530 throw new Error("Not implemented.");
5534 public void asyncRequest(Write request, Consumer<DatabaseException> callback) {
5536 assert (request != null);
5538 getSession().asyncRequest(request, callback);
5543 public void asyncRequest(final DelayedWrite request) {
5545 assert (request != null);
5547 getSession().asyncRequest(request);
5552 public <T> void asyncRequest(final DelayedWriteResult<T> request, Procedure<T> procedure) {
5553 throw new Error("Not implemented.");
5557 public void asyncRequest(DelayedWrite r,
5558 Consumer<DatabaseException> callback) {
5559 throw new Error("Not implemented.");
5563 public void asyncRequest(final WriteOnly request) {
5565 assert (request != null);
5567 getSession().asyncRequest(request);
5572 public <T> void asyncRequest(final WriteOnlyResult<T> request, Procedure<T> procedure) {
5573 throw new Error("Not implemented.");
5577 public void asyncRequest(WriteOnly r, Consumer<DatabaseException> callback) {
5578 throw new Error("Not implemented.");
5582 * Implementation of the interface ServiceLocator
5586 public <T> T getService(Class<T> api) {
5587 if(WriteSupport.class == api) {
5588 if(this instanceof WriteGraphImpl) {
5589 WriteGraphImpl impl = (WriteGraphImpl)this;
5590 return (T)impl.writeSupport;
5593 return getSession().getService(api);
5597 public <T> T peekService(Class<T> api) {
5598 return getSession().peekService(api);
5602 public boolean hasService(Class<?> api) {
5603 return getSession().hasService(api);
5607 public <T> void registerService(Class<T> api, T service) {
5608 getSession().registerService(api, service);
5612 public boolean isImmutable(Resource resource) throws DatabaseException {
5613 ResourceImpl impl = (ResourceImpl)resource;
5614 return processor.isImmutable(impl.id);
5621 protected static String INTERNAL_ERROR_STRING = "Transaction aborted due to internal client error.";
5624 * callerThread is the currently running thread state.syncThread is blocking for
5625 * this execution state.syncParent is the blocking request
5628 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support) {
5629 this.parentGraph = parentGraph;
5630 this.parent = parent;
5631 this.processor = support;
5632 this.asyncBarrier = new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent);
5635 ReadGraphImpl(ReadGraphImpl graph, CacheEntry parent) {
5636 this(graph, parent, graph.processor);
5639 ReadGraphImpl(ReadGraphImpl graph) {
5640 this(graph, graph.parent);
5643 public ReadGraphImpl withParent(CacheEntry parent) {
5644 return new ReadGraphImpl(this, parent);
5647 public ReadGraphImpl forRecompute(CacheEntry parent) {
5648 return new ReadGraphImpl(null, parent, processor);
5651 public static ReadGraphImpl create(QueryProcessor support) {
5652 return new ReadGraphImpl(null, null, support);
5655 public ReadGraphImpl newRestart(ReadGraphImpl impl) {
5657 WriteGraphImpl write = processor.getSession().getService(
5658 WriteGraphImpl.class);
5664 final private ListenerBase getListenerBase(final Object procedure) {
5665 if (procedure instanceof ListenerBase)
5666 return (ListenerBase) procedure;
5671 public <T> void waitAsyncProcedure(AsyncMultiReadProcedure<T> procedure) {
5673 assert(procedure.done());
5677 public <T> void waitAsyncProcedure(AsyncReadProcedure<T> procedure) {
5679 assert(procedure.done());
5683 public boolean resumeTasks() {
5684 return processor.resumeTasks(this);
5687 Class<?> singleClass(Set<Resource> types) {
5688 Class<?> result = null;
5689 for (Resource type : types) {
5690 Class<?> clazz = processor.getBuiltinValue(type);
5691 if (clazz != null) {
5701 private String debugString(Resource r) {
5704 name = getPossibleRelatedValue(r, processor.getL0(this).HasName);
5705 } catch (ManyObjectsForFunctionalRelationException e) {
5706 Logger.defaultLogError(e);
5707 } catch (ServiceException e) {
5708 Logger.defaultLogError(e);
5710 return "[" + name + " - " + r + "]";
5714 public String toString() {
5715 return "ReadGraphImpl[thread=" + Thread.currentThread() + "]";
5719 final public int thread() {
5723 static class MultiTripleIntProcedure implements TripleIntProcedure {
5725 final private AsyncMultiProcedure<Statement> procedure;
5726 final private ReadGraphImpl impl;
5727 final private QuerySupport support;
5729 public MultiTripleIntProcedure(AsyncMultiProcedure<Statement> procedure, ReadGraphImpl impl, QuerySupport support) {
5730 this.procedure = procedure;
5732 this.support = support;
5736 public void execute(ReadGraphImpl graph, int s, int p, int o) {
5738 procedure.execute(graph, support.getStatement(s, p, o));
5739 } catch (Throwable t2) {
5740 Logger.defaultLogError(t2);
5745 public void finished(ReadGraphImpl graph) {
5747 procedure.finished(graph);
5748 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5749 } catch (Throwable t2) {
5750 Logger.defaultLogError(t2);
5755 public void exception(ReadGraphImpl graph, Throwable t) {
5757 procedure.exception(graph, t);
5758 } catch (Throwable t2) {
5759 Logger.defaultLogError(t2);
5761 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5765 public String toString() {
5766 return "forEachObject with " + procedure;
5771 // private AsyncMultiProcedure<Resource> cacheKey = null;
5772 // private MultiIntProcedure cacheResult = null;
5774 // final IntProcedure forMultiProcedure(final AsyncMultiProcedure<Resource> procedure) {
5776 // if(procedure == cacheKey) return cacheResult;
5778 // cacheResult = new MultiIntProcedure(procedure, this, processor.support);
5779 // cacheKey = procedure;
5781 // return cacheResult;
5785 // private AsyncMultiProcedure<Statement> cacheKey2 = null;
5786 // private MultiTripleIntProcedure cacheResult2 = null;
5788 // final synchronized TripleIntProcedure forMultiProcedure(final AsyncMultiProcedure<Statement> procedure) {
5790 // if(procedure == cacheKey2) return cacheResult2;
5792 // cacheResult2 = new MultiTripleIntProcedure(procedure, this, processor.support);
5793 // cacheKey2 = procedure;
5795 // return cacheResult2;
5800 public Datatype getDataType(Resource subject) throws DatabaseException {
5801 for(Resource dataTypeResource : getObjects(subject, processor.getL0(this).HasDataType))
5802 return getValue(dataTypeResource, Bindings.getBindingUnchecked(Datatype.class));
5803 throw new DoesNotContainValueException("The literal has no data type.");
5806 protected <T extends Accessor> T getAccessor4File(Resource subject)
5807 throws DatabaseException {
5810 byte[] bytes = processor.support.getValue(g, subject);
5814 BinaryVariant va = (BinaryVariant) Accessors.getAccessor(bytes, Datatypes.VARIANT);
5815 Accessor ca = va.getContentAccessor();
5817 } catch (AccessorConstructionException e) {
5818 throw new DatabaseException(e);
5824 Binding datatype_binding = Bindings.getBindingUnchecked(DataType.class);
5825 Serializer datatype_serializer = datatype_binding.serializer();
5828 BinaryMemory in = new BinaryMemory(ByteBuffer.wrap(bytes));
5829 datatype = (DataType)datatype_serializer.deserialize(in);
5830 Binding data_binding = Bindings.getBinding(datatype);
5831 Serializer data_serializer = data_binding.serializer(BinarySerializationFormat.INSTANCE);
5832 Object o = data_serializer.deserialize(in);
5834 return (T)Accessors.getAccessor(data_binding, o);
5835 } catch(AccessorConstructionException e) {
5838 } catch (Exception e) {
5839 throw new DatabaseException(e);
5842 @SuppressWarnings("unchecked")
5844 public <T extends Accessor> T getAccessor(Resource subject) throws DatabaseException {
5845 RandomAccessBinary rab = getRandomAccessBinary(subject);
5847 return (T)Accessors.getAccessor(rab, getDataType(subject));
5848 } catch(AccessorConstructionException e) {
5849 throw new DatabaseException(e);
5852 @SuppressWarnings("unchecked")
5853 protected <T extends Accessor> T createAccessor(Resource resource, Datatype datatype, Object intialValue)
5854 throws DatabaseException {
5855 RandomAccessBinary rab = createRandomAccessBinary(resource, datatype, intialValue);
5857 return (T)Accessors.getAccessor(rab, datatype);
5858 } catch(AccessorConstructionException e) {
5859 throw new DatabaseException(e);
5863 public RandomAccessBinary getRandomAccessBinary(Resource subject) throws DatabaseException {
5864 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5865 ResourceData rd = ravs.get(subject);
5869 ExternalValueSupport evs = getService(ExternalValueSupport.class);
5870 long size = evs.getValueSize(this, subject); // Throws DatabaseException if no old external value.
5872 File platform = Platform.getLocation().toFile();
5873 File tempFiles = new File(platform, "tempFiles");
5874 File dbDir = new File(tempFiles, "db");
5876 File file = new File(dbDir, "ResourceFile" + subject.getResourceId());
5877 rd = new ResourceData(new BinaryFile(file), true); // Old external value.
5878 final int N = 1<<20;
5882 int length = N < left ? N : (int)left;
5883 byte[] bytes = evs.readValue(this, subject, offset, length);
5884 offset += bytes.length;
5885 left -= bytes.length;
5886 rd.binaryFile.write(bytes);
5888 ravs.put(subject, rd);
5890 } catch (Exception e) {
5891 throw new DatabaseException("Resource " + subject + " have value but there was problem with accessing value data.", e);
5893 } catch (Exception e) {
5894 if(Development.DEVELOPMENT) {
5895 if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
5896 e.printStackTrace();
5900 Datatype datatype = getDataType(subject);
5901 Object value = getPossibleValue(subject, Bindings.getBinding(datatype));
5902 return createRandomAccessBinary(subject, datatype, value);
5904 public RandomAccessBinary createRandomAccessBinary(Resource resource, Datatype datatype, Object initialValue)
5905 throws DatabaseException {
5906 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5908 File platform = Platform.getLocation().toFile();
5909 File tempFiles = new File(platform, "tempFiles");
5910 File dbDir = new File(tempFiles, "db");
5912 File file = new File(dbDir, "ResourceFile" + resource.getResourceId());
5913 ResourceData rd = new ResourceData(new BinaryFile(file), false);
5914 Binding binding = Bindings.getBinding(datatype);
5915 if (null == initialValue) {
5916 initialValue = binding.createDefault();
5918 Serializer serializer = binding.serializer();
5919 byte[] bytes = serializer.serialize(initialValue);
5920 rd.binaryFile.write(bytes);
5921 ravs.put(resource, rd);
5923 } catch (Exception e) {
5924 if (e instanceof DatabaseException)
5925 throw (DatabaseException)e;
5927 throw new DatabaseException(e);
5931 // static class ExternalValueRequest<T> extends ResourceRead<T> {
5933 // public ExternalValueRequest(Resource resource) {
5937 // @SuppressWarnings("unchecked")
5939 // public T perform(ReadGraph graph) throws DatabaseException {
5942 // String uri = graph.getURI(resource);
5943 // if(Layer0.URIs.Functions_functionApplication.equals(uri)) return (T)functionApplication;
5945 // return (T)ReflectionUtils.getValue(uri).getValue();
5947 // } catch(ValueNotFoundException e) {
5948 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
5949 // } catch(ClassCastException e) {
5950 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
5956 @SuppressWarnings("unchecked")
5958 public <T> T getValue2(Resource r, Object context) throws DatabaseException {
5959 Layer0 L0 = processor.getL0(this);
5960 Set<Resource> types = getTypes(r);
5962 if(types.contains(L0.Literal)) {
5963 if(isImmutable(r)) {
5964 return syncRequest(new ValueImplied<T>(r));
5969 else if(types.contains(L0.ExternalValue)) {
5970 return (T)syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
5974 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
5975 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
5977 return function.apply(this, r, context);
5978 } catch(RuntimeException e) {
5979 DatabaseException dte = findPossibleRootException(e);
5980 if(dte != null) throw dte;
5981 else throw new DatabaseException(e);
5988 public Variant getVariantValue2(Resource r, Object context) throws DatabaseException {
5989 Layer0 L0 = processor.getL0(this);
5990 Set<Resource> types = getTypes(r);
5992 if(types.contains(L0.Literal)) {
5993 if(isImmutable(r)) {
5994 return syncRequest(new VariantValueImplied(r));
5996 return getVariantValue(r);
5999 else if(types.contains(L0.ExternalValue)) {
6000 Object value = syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6002 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6003 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6004 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6009 Function3<ReadGraph,Resource,Object,Object> function = requestValueFunction(r);
6010 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6012 Object value = function.apply(this, r, context);
6014 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6015 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6016 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6018 } catch(RuntimeException e) {
6019 DatabaseException dte = findPossibleRootException(e);
6020 if(dte != null) throw dte;
6021 else throw new DatabaseException(e);
6027 public <T> T getPossibleValue2(Resource subject, Object context) throws DatabaseException {
6029 return getValue2(subject, context);
6030 } catch (DatabaseException e) {
6035 static class PossibleConverterFunction<T> extends ResourceRead<Function3<ReadGraph,Resource,Object,T>> {
6037 public PossibleConverterFunction(Resource resource) {
6042 public Function3<ReadGraph,Resource,Object,T> perform(ReadGraph graph) throws DatabaseException {
6043 return compute(graph, resource);
6046 @SuppressWarnings("unchecked")
6047 public static <T> Function3<ReadGraph,Resource,Object,T> compute(ReadGraph graph, Resource resource) throws DatabaseException {
6048 Layer0 L0 = Layer0.getInstance(graph);
6049 for(Resource converter : graph.getObjects(resource, L0.ConvertsToValueWith)) {
6051 if(L0.Functions_functionApplication.equals(converter)) {
6052 return (Function3<ReadGraph,Resource,Object,T>)graph.syncRequest(new AdaptValue(resource));
6054 return graph.getValue2(converter, resource);
6056 } catch(RuntimeException e) {
6057 DatabaseException dte = findPossibleRootException(e);
6058 if(dte != null) throw dte;
6059 else throw new DatabaseException(e);
6067 <T> Function3<ReadGraph,Resource,Object,T> requestValueFunction(Resource r) throws DatabaseException {
6069 return syncRequest(new PossibleConverterFunction<T>(r), TransientCacheAsyncListener.<Function3<ReadGraph,Resource,Object,T>>instance());
6071 return syncRequest(new PossibleConverterFunction<T>(r));
6075 * Get a value associated with a graph {@link Resource}, using a possible context object and
6076 * a desired value binding. The following methods are tried in order to retreive the value:
6078 * <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>
6079 * <li>If the resource is a {@code L0.ExternalValue}, the value is acquired using
6080 * {@link ReflectionUtils#getValue(String)}.</li>
6081 * <li>If the resource is associated with a suitable value converter with relation {@code L0.ConvertsToValueWith}
6082 * (see {@link #requestValueFunction(Resource)}), the value function is called with the graph, the resource
6083 * and the context object.</li>
6086 * @param r A graph resource with which the value is associated
6087 * @param context A context object that is used for acquiring the value (only applied in case 3)
6088 * @param binding A binding for the value type (only applied in case 1)
6089 * @return The value of the graph node.
6090 * @throws DoesNotContainValueException No value is associated with the graph node.
6091 * @throws DatabaseException Other errors, such as an error in casting the value to the return type or
6092 * a runtime error in the value function.
6094 @SuppressWarnings("unchecked")
6096 public <T> T getValue2(Resource r, Object context, Binding binding) throws DatabaseException {
6097 if (binding instanceof ObjectVariantBinding)
6098 return getValue2(r, context);
6100 Layer0 L0 = processor.getL0(this);
6101 Set<Resource> types = getTypes(r);
6102 if(types.contains(L0.Literal)) {
6103 if(isImmutable(r)) {
6104 return syncRequest(new Value<T>(r, binding));
6106 return getValue(r, binding);
6108 } else if(types.contains(L0.ExternalValue)) {
6110 return (T)ReflectionUtils.getValue(getURI(r)).getValue();
6111 } catch(ValueNotFoundException e) {
6112 throw new DatabaseException(e);
6113 } catch(ClassCastException e) {
6114 throw new DatabaseException(e);
6118 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6119 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function.");
6121 Object value = function.apply(this, r, context);
6122 if(binding.isInstance(value)) return (T)value;
6123 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
6124 return (T)Bindings.adapt(value, srcBinding, binding);
6125 } catch(RuntimeException e) {
6126 DatabaseException dte = findPossibleRootException(e);
6127 if(dte != null) throw dte;
6128 else throw new DatabaseException(e);
6129 } catch (AdaptException e) {
6130 throw new DatabaseException(e);
6131 } catch (org.simantics.databoard.binding.error.BindingException e) {
6132 throw new DatabaseException(e);
6138 public <T> T getPossibleValue2(Resource subject, Object context, Binding binding) throws DatabaseException {
6140 return getValue2(subject, context, binding);
6141 } catch (DatabaseException e) {
6146 private static DatabaseException findPossibleRootException(Throwable t) {
6147 if(t == null) return null;
6148 if(t instanceof DatabaseException) return (DatabaseException)t;
6149 if(t instanceof RuntimeException || t instanceof InvocationTargetException) {
6150 return findPossibleRootException(t.getCause());
6156 public <T> T getRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6157 return getRelatedValue2(subject, relation, subject);
6161 public Variant getRelatedVariantValue2(Resource subject, Resource relation) throws DatabaseException {
6162 return getRelatedVariantValue2(subject, relation, subject);
6166 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6168 Resource object = getPossibleObject(subject, relation);
6169 if(object == null) return null;
6170 else return getValue2(object, subject);
6171 } catch (DatabaseException e) {
6177 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6178 if(Development.DEVELOPMENT) {
6179 String error = L0Validations.checkValueType(this, subject, relation);
6181 Logger.defaultLogError(new ValidationException(error));
6182 //throw new ValidationException(error);
6183 new ValidationException(error).printStackTrace();
6186 return getValue2(getSingleObject(subject, relation), context);
6190 public Variant getRelatedVariantValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6191 if(Development.DEVELOPMENT) {
6192 String error = L0Validations.checkValueType(this, subject, relation);
6194 Logger.defaultLogError(new ValidationException(error));
6195 //throw new ValidationException(error);
6196 new ValidationException(error).printStackTrace();
6199 return getVariantValue2(getSingleObject(subject, relation), context);
6203 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6205 Resource object = getPossibleObject(subject, relation);
6206 if(object == null) return null;
6207 else return getValue2(object, context);
6208 } catch (DatabaseException e) {
6214 public <T> T getRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6215 return getRelatedValue2(subject, relation, subject, binding);
6219 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6221 Resource object = getPossibleObject(subject, relation);
6222 if(object == null) return null;
6223 return getValue2(object, subject, binding);
6224 } catch (DatabaseException e) {
6230 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6231 return getValue2(getSingleObject(subject, relation), context, binding);
6235 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6237 Resource object = getPossibleObject(subject, relation);
6238 if(object == null) return null;
6239 else return getValue2(object, context, binding);
6240 } catch (DatabaseException e) {
6246 public Type getRelatedValueType(Resource subject, Resource relation) throws DatabaseException {
6247 Layer0 L0 = processor.getL0(this);
6248 Resource property = getSingleObject(subject, relation);
6249 String typeText = (String)getRelatedValue(property, L0.HasValueType, Bindings.STRING);
6251 return org.simantics.scl.compiler.types.Types.parseType(typeText);
6252 } catch (SCLTypeParseException e) {
6253 throw new DatabaseException(e);
6257 private static ThreadLocal<Boolean> syncGraph = new ThreadLocal<Boolean>() {
6258 protected Boolean initialValue() {
6264 public boolean setSynchronous(boolean value) {
6265 boolean old = getSynchronous();
6266 syncGraph.set(value);
6271 public boolean getSynchronous() {
6272 return syncGraph.get();
6275 public void ensureLoaded(int resource) {
6276 processor.querySupport.ensureLoaded(this, resource);
6279 public void ensureLoaded(int resource, int predicate) {
6280 processor.querySupport.ensureLoaded(this, resource, predicate);
6283 public byte[] getValue(int resource) {
6284 return processor.querySupport.getValue(this, resource);
6287 public int thread(int resource) {
6288 return (resource >>> 16) & processor.THREAD_MASK;
6291 public int thread(Resource resource) {
6292 return (((ResourceImpl)resource).id >>> 16) & processor.THREAD_MASK;
6295 public ResourceSupport getResourceSupport() {
6296 return processor.getResourceSupport();
6300 public Object getModificationCounter() {
6301 return processor.getSession().getModificationCounter();
6305 public boolean performPending() {
6306 return processor.performPending(this);
6309 public Set<ReadGraphImpl> ancestorSet() {
6310 HashSet<ReadGraphImpl> result = new HashSet<>();
6311 ReadGraphImpl g = this;