1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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.IdentityHashMap;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.ListIterator;
30 import java.util.function.Consumer;
32 import org.eclipse.core.runtime.Platform;
33 import org.simantics.databoard.Accessors;
34 import org.simantics.databoard.Bindings;
35 import org.simantics.databoard.accessor.Accessor;
36 import org.simantics.databoard.accessor.error.AccessorConstructionException;
37 import org.simantics.databoard.adapter.AdaptException;
38 import org.simantics.databoard.binding.Binding;
39 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;
40 import org.simantics.databoard.binding.impl.ObjectVariantBinding;
41 import org.simantics.databoard.binding.mutable.Variant;
42 import org.simantics.databoard.serialization.Serializer;
43 import org.simantics.databoard.type.Datatype;
44 import org.simantics.databoard.util.binary.BinaryFile;
45 import org.simantics.databoard.util.binary.RandomAccessBinary;
46 import org.simantics.db.AsyncReadGraph;
47 import org.simantics.db.DevelopmentKeys;
48 import org.simantics.db.ExternalValueSupport;
49 import org.simantics.db.ReadGraph;
50 import org.simantics.db.RelationContext;
51 import org.simantics.db.RelationInfo;
52 import org.simantics.db.Resource;
53 import org.simantics.db.Session;
54 import org.simantics.db.Statement;
55 import org.simantics.db.adaption.AdaptionService;
56 import org.simantics.db.common.primitiverequest.Adapter;
57 import org.simantics.db.common.primitiverequest.Builtin;
58 import org.simantics.db.common.primitiverequest.DatatypeBinding;
59 import org.simantics.db.common.primitiverequest.ForEachAssertedObject;
60 import org.simantics.db.common.primitiverequest.ForEachAssertedStatement;
61 import org.simantics.db.common.primitiverequest.HasStatement;
62 import org.simantics.db.common.primitiverequest.HasStatementSubject;
63 import org.simantics.db.common.primitiverequest.HasStatementSubjectObject;
64 import org.simantics.db.common.primitiverequest.HasValue;
65 import org.simantics.db.common.primitiverequest.Inverse;
66 import org.simantics.db.common.primitiverequest.IsInheritedFrom;
67 import org.simantics.db.common.primitiverequest.IsInstanceOf;
68 import org.simantics.db.common.primitiverequest.IsSubrelationOf;
69 import org.simantics.db.common.primitiverequest.OrderedSet;
70 import org.simantics.db.common.primitiverequest.PossibleAdapter;
71 import org.simantics.db.common.primitiverequest.PossibleInverse;
72 import org.simantics.db.common.primitiverequest.PossibleObject;
73 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;
74 import org.simantics.db.common.primitiverequest.PossibleRelatedValueImplied;
75 import org.simantics.db.common.primitiverequest.PossibleStatement;
76 import org.simantics.db.common.primitiverequest.PossibleType;
77 import org.simantics.db.common.primitiverequest.PossibleUniqueAdapter;
78 import org.simantics.db.common.primitiverequest.PossibleValue;
79 import org.simantics.db.common.primitiverequest.PossibleValueImplied;
80 import org.simantics.db.common.primitiverequest.RelatedValue;
81 import org.simantics.db.common.primitiverequest.RelatedValueImplied;
82 import org.simantics.db.common.primitiverequest.SingleObject;
83 import org.simantics.db.common.primitiverequest.SingleStatement;
84 import org.simantics.db.common.primitiverequest.SingleType;
85 import org.simantics.db.common.primitiverequest.SingleTypeAny;
86 import org.simantics.db.common.primitiverequest.Types;
87 import org.simantics.db.common.primitiverequest.UniqueAdapter;
88 import org.simantics.db.common.primitiverequest.Value;
89 import org.simantics.db.common.primitiverequest.ValueImplied;
90 import org.simantics.db.common.primitiverequest.VariantValueImplied;
91 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
92 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
93 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
94 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
95 import org.simantics.db.common.procedure.single.SyncReadProcedure;
96 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrErrorProcedure;
97 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrNullProcedure;
98 import org.simantics.db.common.procedure.single.wrapper.ExceptionToNullProcedure;
99 import org.simantics.db.common.procedure.single.wrapper.NullSingleOrNullProcedure;
100 import org.simantics.db.common.procedure.single.wrapper.SingleFunctionalOrNullProcedure;
101 import org.simantics.db.common.procedure.single.wrapper.SingleOrErrorProcedure;
102 import org.simantics.db.common.procedure.single.wrapper.SingleOrNullProcedure;
103 import org.simantics.db.common.procedure.wrapper.NoneToAsyncListener;
104 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiListener;
105 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiProcedure;
106 import org.simantics.db.common.procedure.wrapper.NoneToAsyncProcedure;
107 import org.simantics.db.common.procedure.wrapper.NoneToAsyncSetProcedure;
108 import org.simantics.db.common.procedure.wrapper.SyncToAsyncListener;
109 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiListener;
110 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiProcedure;
111 import org.simantics.db.common.procedure.wrapper.SyncToAsyncProcedure;
112 import org.simantics.db.common.procedure.wrapper.SyncToAsyncSetProcedure;
113 import org.simantics.db.common.request.AdaptValue;
114 import org.simantics.db.common.request.ResourceRead;
115 import org.simantics.db.common.utils.Logger;
116 import org.simantics.db.common.utils.NameUtils;
117 import org.simantics.db.common.validation.L0Validations;
118 import org.simantics.db.exception.AdaptionException;
119 import org.simantics.db.exception.ArgumentException;
120 import org.simantics.db.exception.AssumptionException;
121 import org.simantics.db.exception.BindingException;
122 import org.simantics.db.exception.DatabaseException;
123 import org.simantics.db.exception.DoesNotContainValueException;
124 import org.simantics.db.exception.EmptyResourceException;
125 import org.simantics.db.exception.InternalException;
126 import org.simantics.db.exception.InvalidLiteralException;
127 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
128 import org.simantics.db.exception.NoInverseException;
129 import org.simantics.db.exception.NoSingleResultException;
130 import org.simantics.db.exception.ResourceNotFoundException;
131 import org.simantics.db.exception.ServiceException;
132 import org.simantics.db.exception.ValidationException;
133 import org.simantics.db.impl.RelationContextImpl;
134 import org.simantics.db.impl.ResourceImpl;
135 import org.simantics.db.impl.internal.RandomAccessValueSupport;
136 import org.simantics.db.impl.internal.ResourceData;
137 import org.simantics.db.impl.procedure.CallWrappedSingleQueryProcedure4;
138 import org.simantics.db.impl.procedure.ResultCallWrappedQueryProcedure4;
139 import org.simantics.db.impl.procedure.ResultCallWrappedSingleQueryProcedure4;
140 import org.simantics.db.impl.query.CacheEntry;
141 import org.simantics.db.impl.query.QueryProcessor;
142 import org.simantics.db.impl.query.QuerySupport;
143 import org.simantics.db.impl.query.TripleIntProcedure;
144 import org.simantics.db.impl.support.ResourceSupport;
145 import org.simantics.db.procedure.AsyncListener;
146 import org.simantics.db.procedure.AsyncMultiListener;
147 import org.simantics.db.procedure.AsyncMultiProcedure;
148 import org.simantics.db.procedure.AsyncProcedure;
149 import org.simantics.db.procedure.AsyncSetListener;
150 import org.simantics.db.procedure.Listener;
151 import org.simantics.db.procedure.ListenerBase;
152 import org.simantics.db.procedure.MultiListener;
153 import org.simantics.db.procedure.MultiProcedure;
154 import org.simantics.db.procedure.Procedure;
155 import org.simantics.db.procedure.SetListener;
156 import org.simantics.db.procedure.StatementProcedure;
157 import org.simantics.db.procedure.SyncListener;
158 import org.simantics.db.procedure.SyncMultiListener;
159 import org.simantics.db.procedure.SyncMultiProcedure;
160 import org.simantics.db.procedure.SyncProcedure;
161 import org.simantics.db.procedure.SyncSetListener;
162 import org.simantics.db.request.AsyncMultiRead;
163 import org.simantics.db.request.AsyncRead;
164 import org.simantics.db.request.DelayedWrite;
165 import org.simantics.db.request.DelayedWriteResult;
166 import org.simantics.db.request.ExternalRead;
167 import org.simantics.db.request.MultiRead;
168 import org.simantics.db.request.Read;
169 import org.simantics.db.request.ReadInterface;
170 import org.simantics.db.request.RequestFlags;
171 import org.simantics.db.request.Write;
172 import org.simantics.db.request.WriteInterface;
173 import org.simantics.db.request.WriteOnly;
174 import org.simantics.db.request.WriteOnlyResult;
175 import org.simantics.db.request.WriteResult;
176 import org.simantics.layer0.Layer0;
177 import org.simantics.scl.compiler.types.Type;
178 import org.simantics.scl.compiler.types.exceptions.SCLTypeParseException;
179 import org.simantics.scl.reflection.ReflectionUtils;
180 import org.simantics.scl.reflection.ValueNotFoundException;
181 import org.simantics.scl.runtime.function.Function3;
182 import org.simantics.utils.DataContainer;
183 import org.simantics.utils.Development;
184 import org.simantics.utils.datastructures.Pair;
185 import org.simantics.utils.datastructures.collections.CollectionUtils;
187 import gnu.trove.map.hash.TObjectIntHashMap;
189 public class ReadGraphImpl implements ReadGraph {
191 final static boolean EMPTY_RESOURCE_CHECK = false;
193 final public CacheEntry parent;
194 final public QueryProcessor processor;
196 final static Binding DATA_TYPE_BINDING_INTERNAL = Bindings.getBindingUnchecked(Datatype.class);
197 final static Serializer DATA_TYPE_SERIALIZER = Bindings.getSerializerUnchecked(DATA_TYPE_BINDING_INTERNAL);
199 final public static TObjectIntHashMap<String> counters = new TObjectIntHashMap<String>();
201 public static void resetCounters() {
205 public static String listCounters(File file) throws IOException {
207 PrintStream b = new PrintStream(new BufferedOutputStream(new FileOutputStream(file)));
209 for(Pair<String,Integer> p : CollectionUtils.valueSortedEntries(counters)) {
210 b.print(-p.second + " " + p.first + "\n");
215 return "Dumped " + counters.size() + " queries.";
220 * Implementation of the interface ReadGraph
222 final public String getURI(final Resource resource) throws AssumptionException, ValidationException, ServiceException {
224 assert (resource != null);
228 return syncRequest(new org.simantics.db.common.uri.ResourceToURI(resource));
230 } catch (AssumptionException e) {
232 throw new AssumptionException(e);
234 } catch (ValidationException e) {
236 throw new ValidationException(e);
238 } catch (ServiceException e) {
240 throw new ServiceException(e);
242 } catch (DatabaseException e) {
244 throw new ServiceException(INTERNAL_ERROR_STRING, e);
250 final public String getPossibleURI(final Resource resource) throws ValidationException, ServiceException {
252 assert (resource != null);
256 return syncRequest(new org.simantics.db.common.uri.ResourceToPossibleURI(resource));
258 } catch (ValidationException e) {
260 throw new ValidationException(e);
262 } catch (ServiceException e) {
264 throw new ServiceException(e);
266 } catch (DatabaseException e) {
268 throw new ServiceException(INTERNAL_ERROR_STRING, e);
274 final public Resource getResource(final String id)
275 throws ResourceNotFoundException, ValidationException,
282 return syncRequest(new org.simantics.db.common.primitiverequest.Resource(
285 } catch (ResourceNotFoundException e) {
287 throw new ResourceNotFoundException(id, e);
289 } catch (ValidationException e) {
291 throw new ValidationException(e);
293 } catch (ServiceException e) {
295 throw new ServiceException(e);
297 } catch (DatabaseException e) {
299 throw new ServiceException(INTERNAL_ERROR_STRING, e);
305 final public Resource getPossibleResource(final String id)
306 throws ResourceNotFoundException, ValidationException,
313 return syncRequest(new org.simantics.db.common.primitiverequest.Resource(
316 } catch (ResourceNotFoundException e) {
320 } catch (ValidationException e) {
322 throw new ValidationException(e);
324 } catch (ServiceException e) {
326 throw new ServiceException(e);
328 } catch (DatabaseException e) {
330 throw new ServiceException(INTERNAL_ERROR_STRING, e);
336 final public Resource getRootLibrary() {
337 return processor.getRootLibraryResource();
340 final public Resource getBuiltin(final String id)
341 throws ResourceNotFoundException, ServiceException {
347 return syncRequest(new Builtin(id));
349 } catch (ResourceNotFoundException e) {
351 throw new ResourceNotFoundException(id, e);
353 } catch (ServiceException e) {
355 throw new ServiceException(e);
357 } catch (DatabaseException e) {
359 throw new ServiceException(INTERNAL_ERROR_STRING, e);
365 static class StatementReadProcedure extends TIntArrayListInternal implements StatementProcedure {
367 private static Throwable DONE = new Throwable();
369 Throwable exception = null;
371 final ResourceSupport support;
373 public StatementReadProcedure(ResourceSupport support) {
374 this.support = support;
378 public synchronized void execute(AsyncReadGraph graph, int s, int p, int o) {
385 public void finished(AsyncReadGraph graph) {
390 public void exception(AsyncReadGraph graph, Throwable t) {
394 public void checkAndThrow() throws DatabaseException {
395 if(exception != DONE) {
396 if (exception instanceof DatabaseException)
397 throw (DatabaseException) exception;
399 throw new DatabaseException(
400 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
405 public boolean done() {
406 return exception != null;
410 public boolean contains(Object obj) {
411 if(!(obj instanceof InternalStatement))
413 InternalStatement statement = (InternalStatement)obj;
417 for(int i=0;i<sizeInternal();i+=3)
418 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
423 @SuppressWarnings("unchecked")
425 public <T> T[] toArray(T[] a) {
426 int length = sizeInternal() / 3;
427 if(length > a.length) {
428 Class<?> arrayType = a.getClass();
429 a = (arrayType == Object[].class)
430 ? (T[]) new Object[length]
431 : (T[]) Array.newInstance(arrayType.getComponentType(), length);
434 for(int i=length;i<a.length;++i)
437 for(int i=0,j=0;i<sizeInternal();i+=3,++j)
438 a[j] = (T)new InternalStatement(support, getQuick(i), getQuick(i+1), getQuick(i+2));
443 public boolean add(Statement e) {
444 throw new UnsupportedOperationException();
448 public boolean remove(Object o) {
449 throw new UnsupportedOperationException();
453 public boolean addAll(Collection<? extends Statement> c) {
454 throw new UnsupportedOperationException();
457 class IteratorImpl implements ListIterator<Statement> {
461 public IteratorImpl(int index) {
466 public boolean hasNext() {
467 return index < sizeInternal();
471 public Statement next() {
472 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
478 public void remove() {
479 throw new Error("Not supported");
483 public boolean hasPrevious() {
488 public Statement previous() {
490 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
495 public int nextIndex() {
500 public int previousIndex() {
505 public void set(Statement e) {
506 throw new UnsupportedOperationException();
510 public void add(Statement e) {
511 throw new UnsupportedOperationException();
517 public Iterator<Statement> iterator() {
518 return new IteratorImpl(0);
523 return sizeInternal() / 3;
527 public Object[] toArray() {
528 Object[] result = new Object[sizeInternal() / 3];
529 for(int i=0,j=0;j<sizeInternal();i++,j+=3)
530 result[i] = new InternalStatement(support, getQuick(j), getQuick(j+1), getQuick(j+2));
535 public boolean addAll(int index, Collection<? extends Statement> c) {
536 throw new UnsupportedOperationException();
540 public Statement get(int index) {
542 if(index < 0 || index >= sizeInternal())
543 throw new IndexOutOfBoundsException();
544 return new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
548 public Statement set(int index, Statement element) {
549 throw new UnsupportedOperationException();
553 public void add(int index, Statement element) {
554 throw new UnsupportedOperationException();
558 public Statement remove(int index) {
559 throw new UnsupportedOperationException();
563 public int indexOf(Object obj) {
564 if(!(obj instanceof InternalStatement))
566 InternalStatement statement = (InternalStatement)obj;
570 for(int i=0;i<sizeInternal();i+=3)
571 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
577 public int lastIndexOf(Object obj) {
578 if(!(obj instanceof InternalStatement))
580 InternalStatement statement = (InternalStatement)obj;
584 for(int i=sizeInternal()-3;i>=0;i-=3)
585 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
591 public ListIterator<Statement> listIterator() {
592 return new IteratorImpl(0);
596 public ListIterator<Statement> listIterator(int index) {
597 return new IteratorImpl(index*3);
601 public List<Statement> subList(int fromIndex, int toIndex) {
602 if(fromIndex < 0 || toIndex*3 >= sizeInternal() || fromIndex > toIndex)
603 throw new IndexOutOfBoundsException();
604 return new RandomAccessSubList<Statement>(this, fromIndex, toIndex-fromIndex);
609 final public Collection<Statement> getStatements(final Resource subject,
610 final Resource relation)
611 throws ManyObjectsForFunctionalRelationException, ServiceException {
613 assert (subject != null);
614 assert (relation != null);
618 StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
619 processor.forEachStatement(this, subject, relation, procedure);
620 procedure.checkAndThrow();
623 } catch (DatabaseException e) {
625 System.err.println(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation);
627 StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
628 processor.forEachStatement(this, subject, relation, procedure);
630 return Collections.emptyList();
632 // throw new ServiceException(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation, e);
639 final public Collection<Statement> getAssertedStatements(final Resource subject, final Resource relation)
640 throws ManyObjectsForFunctionalRelationException, ServiceException {
642 assert (subject != null);
643 assert (relation != null);
647 return syncRequest(new ForEachAssertedStatement(subject, relation));
649 } catch (ManyObjectsForFunctionalRelationException e) {
651 throw new ManyObjectsForFunctionalRelationException(e);
653 } catch (ServiceException e) {
655 throw new ServiceException(e);
657 } catch (DatabaseException e) {
659 throw new ServiceException(INTERNAL_ERROR_STRING, e);
666 final public Collection<Resource> getPredicates(final Resource subject) throws ServiceException {
668 assert (subject != null);
672 return processor.getPredicates(this, subject);
674 // AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
675 // processor.forEachPredicate(this, subject, procedure);
676 // procedure.checkAndThrow();
679 } catch (ServiceException e) {
681 throw new ServiceException(e);
683 } catch (DatabaseException e) {
685 throw new ServiceException(INTERNAL_ERROR_STRING, e);
687 } catch (Throwable e) {
689 throw new ServiceException(e);
696 final public Collection<Resource> getPrincipalTypes(final Resource subject)
697 throws ServiceException {
699 assert (subject != null);
703 AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
704 processor.forEachPrincipalType(this, subject, procedure);
705 procedure.checkAndThrow();
708 } catch (ServiceException e) {
710 throw new ServiceException(e);
712 } catch (DatabaseException e) {
714 throw new ServiceException(INTERNAL_ERROR_STRING, e);
721 final public Set<Resource> getTypes(final Resource subject) throws ServiceException {
723 assert (subject != null);
727 return processor.getTypes(this, subject);
729 } catch (ServiceException e) {
731 throw new ServiceException(e);
733 } catch (DatabaseException e) {
735 throw new ServiceException(INTERNAL_ERROR_STRING, e);
737 } catch (Throwable e) {
739 throw new ServiceException(e);
746 final public Set<Resource> getSupertypes(final Resource subject)
747 throws ServiceException {
749 assert (subject != null);
753 SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
754 processor.forSupertypes(this, subject, procedure);
755 procedure.checkAndThrow();
756 return procedure.result;
758 } catch (ServiceException e) {
760 throw new ServiceException(e);
762 } catch (DatabaseException e) {
764 throw new ServiceException(INTERNAL_ERROR_STRING, e);
771 final public Set<Resource> getSuperrelations(final Resource subject)
772 throws ServiceException {
774 assert (subject != null);
778 SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
779 processor.forSuperrelations(this, subject, procedure);
780 procedure.checkAndThrow();
781 return procedure.result;
783 } catch (ServiceException e) {
785 throw new ServiceException(e);
787 } catch (DatabaseException e) {
789 throw new ServiceException(INTERNAL_ERROR_STRING, e);
796 public Resource getPossibleSuperrelation(Resource subject) throws ServiceException {
800 SyncReadProcedure<Resource> procedure = new SyncReadProcedure<Resource>();
801 processor.forPossibleSuperrelation(this, subject, procedure);
802 procedure.checkAndThrow();
803 return procedure.result;
805 } catch (ServiceException e) {
807 throw new ServiceException(e);
809 } catch (DatabaseException e) {
811 throw new ServiceException(INTERNAL_ERROR_STRING, e);
818 final public Collection<Resource> getObjects(final Resource subject, final Resource relation)
819 throws ServiceException {
821 assert (subject != null);
822 assert (relation != null);
824 if(Development.DEVELOPMENT) {
825 if(Development.isTrue(DevelopmentKeys.READGRAPH_COUNT)) {
826 counters.adjustOrPutValue("objects $" + subject.getResourceId() + " $" + relation.getResourceId(), 1, 1);
828 //if(subject.getResourceId()==xx && relation.getResourceId()==xx) new Exception().printStackTrace();
833 AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
834 processor.forEachObject(this, subject, relation, procedure);
835 procedure.checkAndThrow();
838 } catch (DatabaseException e) {
840 throw new ServiceException(INTERNAL_ERROR_STRING, e);
847 final public Collection<Resource> getAssertedObjects(
848 final Resource subject, final Resource relation)
849 throws ManyObjectsForFunctionalRelationException, ServiceException {
852 throw new ArgumentException("Subject must not be null.");
853 if (relation == null)
854 throw new ArgumentException("Relation must not be null. Subject=" + subject);
858 return syncRequest(new ForEachAssertedObject(subject, relation));
860 } catch (ManyObjectsForFunctionalRelationException e) {
862 throw new ManyObjectsForFunctionalRelationException(e);
864 } catch (ServiceException e) {
866 throw new ServiceException(e);
868 } catch (DatabaseException e) {
870 throw new ServiceException(INTERNAL_ERROR_STRING, e);
877 final public Resource getInverse(final Resource relation) throws NoInverseException, ServiceException {
879 assert (relation != null);
883 return getSingleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
885 } catch (NoSingleResultException e) {
887 throw new NoInverseException(e);
889 } catch (ServiceException e) {
891 throw new ServiceException(e);
898 final public Resource getSingleObject(final Resource subject, final Resource relation) throws NoSingleResultException, ServiceException {
900 if( subject == null) throw new IllegalArgumentException("subject can not be null");
901 if( relation == null) throw new IllegalArgumentException("relation can not be null");
904 int single = processor.getSingleObject(this, subject, relation);
906 if (EMPTY_RESOURCE_CHECK) {
907 if (!hasStatement(subject)) {
908 throw new EmptyResourceException("Resource " + debugString(subject));
911 throw new NoSingleResultException("No single object for subject " + debugString(subject)
912 + " and relation " + debugString(relation), single);
914 return processor.querySupport.getResource(single);
915 } catch (NoSingleResultException e) {
917 } catch (DatabaseException e) {
918 throw new ServiceException(e);
923 final public Statement getSingleStatement(final Resource subject, final Resource relation) throws NoSingleResultException, ManyObjectsForFunctionalRelationException, ServiceException {
924 assert (subject != null);
925 assert (relation != null);
927 Collection<Statement> statements = getStatements(subject, relation);
928 if (statements.size() == 1) {
929 return statements.iterator().next();
931 if (EMPTY_RESOURCE_CHECK)
932 if (!hasStatement(subject))
933 throw new EmptyResourceException("Resource " + debugString(subject));
934 throw new NoSingleResultException("No single statement for subject " + debugString(subject)
935 + " and relation " + debugString(relation), statements.size());
937 } catch (ServiceException e) {
938 throw new ServiceException(e);
943 final public Resource getSingleType(final Resource subject) throws NoSingleResultException, ServiceException {
944 assert (subject != null);
946 ArrayList<Resource> principalTypes = (ArrayList<Resource>)getPrincipalTypes(subject);
947 if (principalTypes.size() == 1) {
948 return principalTypes.get(0);
950 throw new NoSingleResultException("No single type for subject " + debugString(subject), principalTypes.size());
952 } catch (ServiceException e) {
953 throw new ServiceException(e);
958 final public Resource getSingleType(final Resource subject,
959 final Resource baseType) throws NoSingleResultException,
962 assert (subject != null);
963 assert (baseType != null);
966 return syncRequest(new SingleType(subject, baseType));
967 } catch (DatabaseException e) {
968 throw new NoSingleResultException("subject=" + subject + ", baseType=" + baseType, 0, e);
973 final public <T> T getValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
975 assert (subject != null);
979 Layer0 L0 = processor.getL0(this);
980 int object = processor.getSingleObject(this, subject, L0.HasDataType);
981 if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
983 if(processor.isImmutable(object)) {
984 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
985 return getValue(subject, binding);
987 byte[] dt = processor.getValue(this, object);
988 if(dt == null) throw new ServiceException("No data type for " + subject);
989 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
990 Binding binding = Bindings.getBinding(datatype);
991 return getValue(subject, binding);
994 } catch (IOException e) {
996 throw new ServiceException(e);
998 } catch (DoesNotContainValueException e) {
1000 throw new DoesNotContainValueException(e, subject);
1002 } catch (ServiceException e) {
1004 throw new ServiceException(e);
1006 } catch (DatabaseException e) {
1008 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1015 final public Variant getVariantValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
1017 assert (subject != null);
1021 Layer0 L0 = processor.getL0(this);
1022 int object = processor.getSingleObject(this, subject, L0.HasDataType);
1023 if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
1025 if(processor.isImmutable(object)) {
1026 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1027 return new Variant(binding, getValue(subject, binding));
1029 byte[] dt = processor.getValue(this, object);
1030 if(dt == null) throw new ServiceException("No data type for " + subject);
1031 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1032 Binding binding = Bindings.getBinding(datatype);
1033 return new Variant(binding, getValue(subject, binding));
1036 } catch (IOException e) {
1038 throw new ServiceException(e);
1040 } catch (DoesNotContainValueException e) {
1042 throw new DoesNotContainValueException(e, subject);
1044 } catch (ServiceException e) {
1046 throw new ServiceException(e);
1048 } catch (DatabaseException e) {
1050 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1055 static final IdentityHashMap<Binding,Serializer> serializers = new IdentityHashMap<Binding,Serializer>();
1058 serializers.put(Bindings.STRING, Bindings.STRING.serializer());
1061 final protected Serializer getSerializer(Binding binding) {
1062 return binding.serializer();
1066 final public <T> T getValue(final Resource subject, final Binding binding) throws DoesNotContainValueException, BindingException,
1069 assert (subject != null);
1073 byte[] bytes = processor.getValue(this, subject);
1074 if (bytes == null) throw new DoesNotContainValueException("No value for resource " + subject);
1076 Serializer serializer = getSerializer(binding);
1077 return (T)serializer.deserialize(bytes);
1079 } catch (DoesNotContainValueException e) {
1081 throw new DoesNotContainValueException(e);
1083 } catch (IOException e) {
1085 throw new ServiceException(e);
1087 } catch (DatabaseException e) {
1089 throw new ServiceException(e);
1091 } catch (BufferUnderflowException e) {
1092 // This is sometimes thrown when deserialize fails because wrong format.
1093 // For callers of this method this is just an service exception.
1094 throw new ServiceException(e);
1100 final public <T> T getRelatedValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1101 DoesNotContainValueException, ServiceException {
1103 assert (subject != null);
1104 assert (relation != null);
1107 Resource object = getSingleObject(subject, relation);
1108 return getValue(object);
1109 } catch (NoSingleResultException e) {
1110 throw new NoSingleResultException("No single value found for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1111 } catch (DoesNotContainValueException e) {
1113 Layer0 L0 = processor.getL0(this);
1114 Resource object = getPossibleObject(subject, relation);
1115 if(isInstanceOf(object, L0.Value)) {
1116 if(isInstanceOf(object, L0.Literal)) {
1117 throw new DoesNotContainValueException(e);
1119 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1122 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1124 } catch (DoesNotContainValueException e2) {
1126 } catch (DatabaseException e2) {
1127 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1129 } catch (ServiceException e) {
1130 throw new ServiceException(e);
1135 final public Variant getRelatedVariantValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1136 DoesNotContainValueException, ServiceException {
1138 assert (subject != null);
1139 assert (relation != null);
1142 Resource object = getSingleObject(subject, relation);
1143 return getVariantValue(object);
1144 } catch (NoSingleResultException e) {
1145 throw new NoSingleResultException("No single object for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1146 } catch (DoesNotContainValueException e) {
1148 Layer0 L0 = processor.getL0(this);
1149 Resource object = getPossibleObject(subject, relation);
1150 if(isInstanceOf(object, L0.Value)) {
1151 if(isInstanceOf(object, L0.Literal)) {
1152 throw new DoesNotContainValueException(e);
1154 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1157 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1159 } catch (DoesNotContainValueException e2) {
1161 } catch (DatabaseException e2) {
1162 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1164 } catch (ServiceException e) {
1165 throw new ServiceException(e);
1170 final public <T> T getRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1171 throws NoSingleResultException, DoesNotContainValueException, BindingException, ServiceException {
1173 assert (subject != null);
1174 assert (relation != null);
1177 Resource object = getSingleObject(subject, relation);
1178 return getValue(object, binding);
1179 } catch (NoSingleResultException e) {
1180 String message = "";
1182 String subjectName = NameUtils.getSafeName(this, subject, true);
1183 String relationName = NameUtils.getSafeName(this, relation, true);
1184 message = "Subject: " + subjectName + ", Relation: " + relationName;
1185 } catch (DatabaseException e2) {
1188 throw new NoSingleResultException(message, e.getResultCount(), e);
1189 } catch (DoesNotContainValueException e) {
1190 throw new DoesNotContainValueException(e);
1191 } catch (ServiceException e) {
1192 throw new ServiceException(e);
1197 final public <T> T adapt(final Resource resource, final Class<T> clazz)
1198 throws AdaptionException, ValidationException, ServiceException {
1200 assert (resource != null);
1201 assert (clazz != null);
1205 return syncRequest(new Adapter<T>(resource, clazz));
1207 } catch (AdaptionException e) {
1209 throw new AdaptionException(e);
1211 } catch (ValidationException e) {
1213 throw new ValidationException(e);
1215 } catch (ServiceException e) {
1217 throw new ServiceException(e);
1219 } catch (DatabaseException e) {
1221 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1228 final public <T,C> T adaptContextual(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1229 throws AdaptionException, ValidationException, ServiceException {
1231 assert (resource != null);
1232 assert (context != null);
1234 class ContextualAdapter implements AsyncRead<T> {
1236 final private Resource resource;
1237 final private C context;
1238 final private Class<T> clazz;
1241 public int hashCode() {
1242 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1246 final public int threadHash() {
1247 return resource.getThreadHash();
1251 public boolean equals(Object object) {
1254 else if (object == null)
1256 else if (getClass() != object.getClass())
1258 ContextualAdapter r = (ContextualAdapter)object;
1259 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1263 public int getFlags() {
1267 public ContextualAdapter(Resource resource, C context, Class<T> clazz) {
1268 this.resource = resource;
1269 this.context = context;
1274 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1276 final AdaptionService service = getSession().peekService(AdaptionService.class);
1277 if (service == null)
1278 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1280 service.adapt(graph, resource, context, contextClass, clazz, false, procedure);
1285 public String toString() {
1286 return "Adapter for (" + resource + "," + context + ") as " + clazz.getName();
1293 return syncRequest(new ContextualAdapter(resource, context, clazz));
1295 } catch (AdaptionException e) {
1297 throw new AdaptionException(e);
1299 } catch (ValidationException e) {
1301 throw new ValidationException(e);
1303 } catch (ServiceException e) {
1305 throw new ServiceException(e);
1307 } catch (DatabaseException e) {
1309 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1316 final public <T> T adaptRelated(final Resource resource, final Resource relation, final Class<T> clazz)
1317 throws AdaptionException, NoSingleResultException, ValidationException, ServiceException {
1319 assert (resource != null);
1320 assert (clazz != null);
1322 Statement stm = getSingleStatement(resource, relation);
1324 return adaptContextual(stm.getObject(), new RelationContextImpl(resource, stm), RelationContext.class, clazz);
1329 final public <T> T getPossibleRelatedAdapter(final Resource resource, final Resource relation, final Class<T> clazz)
1330 throws ValidationException, ServiceException {
1333 return adaptRelated(resource, relation, clazz);
1334 } catch (DatabaseException e) {
1341 final public <T,C> T getPossibleContextualAdapter(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1342 throws ValidationException, ServiceException {
1344 assert (resource != null);
1345 assert (context != null);
1347 class PossibleContextualAdapter implements AsyncRead<T> {
1349 final private Resource resource;
1350 final private C context;
1351 final private Class<T> clazz;
1354 public int hashCode() {
1355 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1359 final public int threadHash() {
1360 return resource.getThreadHash();
1364 public boolean equals(Object object) {
1367 else if (object == null)
1369 else if (getClass() != object.getClass())
1371 PossibleContextualAdapter r = (PossibleContextualAdapter)object;
1372 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1376 public int getFlags() {
1380 public PossibleContextualAdapter(Resource resource, C context, Class<T> clazz) {
1381 this.resource = resource;
1382 this.context = context;
1387 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1389 final AdaptionService service = getSession().peekService(AdaptionService.class);
1390 if (service == null)
1391 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1393 service.adapt(graph, resource, context, contextClass, clazz, true, procedure);
1398 public String toString() {
1399 return "Possible adapter for (" + resource + "," + context + ") as " + clazz.getName();
1406 return syncRequest(new PossibleContextualAdapter(resource, context, clazz));
1408 } catch (ValidationException e) {
1410 throw new ValidationException(e);
1412 } catch (ServiceException e) {
1414 throw new ServiceException(e);
1416 } catch (DatabaseException e) {
1418 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1425 final public <T> T adaptUnique(final Resource resource, final Class<T> clazz)
1426 throws AdaptionException, ValidationException, ServiceException {
1428 assert (resource != null);
1429 assert (clazz != null);
1433 return syncRequest(new UniqueAdapter<T>(resource, clazz));
1435 } catch (AdaptionException e) {
1437 throw new AdaptionException(e);
1439 } catch (ValidationException e) {
1441 throw new ValidationException(e);
1443 } catch (ServiceException e) {
1445 throw new ServiceException(e);
1447 } catch (DatabaseException e) {
1449 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1456 final public Resource getPossibleInverse(final Resource relation)
1457 throws ServiceException {
1459 assert (relation != null);
1463 return getPossibleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
1465 } catch (ServiceException e) {
1467 throw new ServiceException(e);
1469 } catch (DatabaseException e) {
1471 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1478 public Resource getPossibleObject(final Resource subject, final Resource relation)
1479 throws ManyObjectsForFunctionalRelationException, ServiceException {
1481 assert (subject != null);
1482 assert (relation != null);
1486 int result = processor.getSingleObject(this, subject, relation);
1487 if(result == 0) return null;
1489 return processor.querySupport.getResource(result);
1491 } catch (ManyObjectsForFunctionalRelationException e) {
1493 throw new ManyObjectsForFunctionalRelationException("subject=" + subject + ", relation=" + relation, e);
1495 } catch (DatabaseException e) {
1497 throw new ServiceException(e);
1504 final public Statement getPossibleStatement(final Resource subject, final Resource relation)
1505 throws ManyObjectsForFunctionalRelationException, ServiceException {
1507 assert (subject != null);
1508 assert (relation != null);
1512 Collection<Statement> statements = getStatements(subject, relation);
1513 if(statements.size() == 1) return statements.iterator().next();
1516 } catch (ManyObjectsForFunctionalRelationException e) {
1518 throw new ManyObjectsForFunctionalRelationException("Many objects in " + subject + " for functional relation " + relation);
1520 } catch (ServiceException e) {
1522 throw new ServiceException(e);
1529 final public Resource getPossibleType(final Resource subject, final Resource baseType) throws ServiceException {
1531 assert (subject != null);
1532 assert (baseType != null);
1536 AsyncReadProcedure<Resource> procedure = new AsyncReadProcedure<Resource>();
1537 forPossibleType(subject, baseType, procedure);
1538 procedure.checkAndThrow();
1539 return procedure.result;
1541 } catch (ServiceException e) {
1543 throw new ServiceException(e);
1545 } catch (DatabaseException e) {
1547 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1554 final public <T> T getPossibleValue(final Resource subject) throws ServiceException {
1556 assert (subject != null);
1560 int object = processor.getSingleObject(this, subject, processor.getL0(this).HasDataType);
1561 if(object == 0) return null;
1563 if(processor.isImmutable(object)) {
1564 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1565 return getPossibleValue(subject, binding);
1567 byte[] dt = processor.getValue(this, object);
1568 if(dt == null) return null;
1569 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1570 Binding binding = Bindings.getBinding(datatype);
1571 return getPossibleValue(subject, binding);
1574 } catch (IOException e) {
1576 throw new ServiceException(e);
1578 } catch (ServiceException e) {
1580 throw new ServiceException(e);
1582 } catch (DatabaseException e) {
1584 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1591 final public <T> T getPossibleValue(final Resource subject, final Binding binding) throws BindingException, ServiceException {
1593 assert (subject != null);
1594 assert (binding != null);
1598 byte[] dt = processor.getValue(this, subject);
1599 if(dt == null) return null;
1600 Serializer serializer = getSerializer(binding);
1601 return (T)serializer.deserialize(dt);
1603 } catch (IOException e) {
1605 throw new ServiceException(e);
1607 } catch (BindingException e) {
1609 throw new BindingException(e);
1611 } catch (ServiceException e) {
1613 throw new ServiceException(e);
1615 } catch (DatabaseException e) {
1616 e.printStackTrace();
1617 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1623 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation)
1624 throws ManyObjectsForFunctionalRelationException, ServiceException {
1626 assert (subject != null);
1627 assert (relation != null);
1631 Resource object = getPossibleObject(subject, relation);
1632 if(object == null) return null;
1633 else return getPossibleValue(object);
1635 } catch (ManyObjectsForFunctionalRelationException e) {
1637 throw new ManyObjectsForFunctionalRelationException(e);
1639 } catch (ServiceException e) {
1641 throw new ServiceException(e);
1648 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1649 throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
1651 assert (subject != null);
1652 assert (relation != null);
1653 assert (binding != null);
1657 Resource object = getPossibleObject(subject, relation);
1658 if(object == null) return null;
1659 else return getPossibleValue(object, binding);
1661 } catch (ManyObjectsForFunctionalRelationException e) {
1663 throw new ManyObjectsForFunctionalRelationException(e);
1665 } catch (BindingException e) {
1667 throw new BindingException(e);
1669 } catch (ServiceException e) {
1671 throw new ServiceException(e);
1678 public <T> T getPossibleAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1680 assert (resource != null);
1681 assert (clazz != null);
1685 return syncRequest(new PossibleAdapter<T>(resource, clazz));
1687 } catch (ValidationException e) {
1689 throw new ValidationException(e);
1691 } catch (AdaptionException e) {
1695 } catch (DatabaseException e) {
1697 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1703 public <T> T getPossibleUniqueAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1705 assert (resource != null);
1706 assert (clazz != null);
1710 return syncRequest(new PossibleUniqueAdapter<T>(resource, clazz));
1712 } catch (AdaptionException e) {
1716 } catch (ValidationException e) {
1718 throw new ValidationException(e);
1720 } catch (DatabaseException e) {
1722 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1729 final public boolean isInstanceOf(final Resource resource, final Resource type) throws ServiceException {
1731 assert (resource != null);
1732 assert (type != null);
1734 Set<Resource> resources = getTypes(resource);
1735 // This check was necessary because some of the callers of this method got stuck when the NPE was thrown from here.
1736 if (null == resources)
1739 if(EMPTY_RESOURCE_CHECK) {
1740 if (resources.isEmpty()) {
1741 if(!hasStatement(resource)) throw new EmptyResourceException("Resource " + debugString(resource));
1745 return resources.contains(type);
1750 final public boolean isInheritedFrom(final Resource resource, final Resource type) throws ServiceException {
1752 assert (resource != null);
1753 assert (type != null);
1757 if(resource.equals(type)) return true;
1759 return getSupertypes(resource).contains(type);
1761 } catch (ServiceException e) {
1763 throw new ServiceException(e);
1770 final public boolean isSubrelationOf(final Resource resource, final Resource type) throws ServiceException {
1772 assert (resource != null);
1773 assert (type != null);
1777 if(resource.equals(type)) return true;
1779 return getSuperrelations(resource).contains(type);
1781 } catch (ServiceException e) {
1783 throw new ServiceException(e);
1790 final public boolean hasStatement(final Resource subject) throws ServiceException {
1792 assert (subject != null);
1796 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1797 processor.forHasStatement(this, subject, procedure);
1798 procedure.checkAndThrow();
1799 return procedure.result;
1801 } catch (ServiceException e) {
1803 throw new ServiceException(e);
1805 } catch (DatabaseException e) {
1807 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1814 final public boolean hasStatement(final Resource subject, final Resource relation) throws ServiceException {
1816 assert (subject != null);
1817 assert (relation != null);
1821 SyncReadProcedure<RelationInfo> procedure = new SyncReadProcedure<RelationInfo>();
1822 processor.forRelationInfo(this, relation, procedure);
1823 Collection<Resource> predicates = getPredicates(subject);
1825 if(procedure.result.isFinal) {
1827 return predicates.contains(relation);
1829 } else if (procedure.result.isFunctional) {
1832 int result = processor.getSingleObject(this, subject, relation);
1834 } catch (ManyObjectsForFunctionalRelationException e) {
1836 } catch (DatabaseException e) {
1837 throw new ServiceException(e);
1842 for(Resource predicate : getPredicates(subject)) {
1843 if(isSubrelationOf(predicate, relation))
1851 } catch (ServiceException e) {
1853 throw new ServiceException(e);
1860 final public boolean hasStatement(final Resource subject, final Resource relation, final Resource object) throws ServiceException {
1862 assert (subject != null);
1863 assert (relation != null);
1864 assert (object != null);
1868 for(Resource o : getObjects(subject, relation)) {
1869 if(object.equals(o)) return true;
1874 } catch (ServiceException e) {
1876 throw new ServiceException(e);
1883 final public boolean hasValue(final Resource subject) throws ServiceException {
1885 assert (subject != null);
1889 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1890 processor.forHasValue(this, subject, procedure);
1891 procedure.checkAndThrow();
1892 return procedure.result;
1894 } catch (ServiceException e) {
1896 throw new ServiceException(e);
1898 } catch (DatabaseException e) {
1900 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1906 final AsyncProcedure<?> NONE = new AsyncProcedure<Object>() {
1909 public void execute(AsyncReadGraph graph, Object result) {
1913 public void exception(AsyncReadGraph graph, Throwable throwable) {
1919 * Implementation of the interface RequestProcessor
1923 public <T> T syncRequest(final Read<T> request) throws DatabaseException {
1925 assert (request != null);
1927 if (parent != null) {
1930 return processor.queryRead(this, request, parent, null, null);
1931 } catch (Throwable e) {
1932 if(e instanceof DatabaseException) throw (DatabaseException)e;
1933 else throw new DatabaseException(e);
1940 return processor.tryQuery(this, request);
1942 } catch (Throwable throwable) {
1944 //Logger.defaultLogError("Internal read request failure", throwable);
1946 if (throwable instanceof DatabaseException)
1947 throw (DatabaseException) throwable;
1949 throw new DatabaseException(
1950 "Unexpected exception in ReadGraph.syncRequest(Read,Procedure)",
1960 public <T> T syncRequest(Read<T> request, SyncListener<T> procedure)
1961 throws DatabaseException {
1962 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
1966 public <T> T syncRequest(Read<T> request, final Listener<T> procedure)
1967 throws DatabaseException {
1968 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
1972 public <T> T syncRequest(final Read<T> request, final AsyncProcedure<T> procedure) throws DatabaseException {
1974 assert (request != null);
1976 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
1978 if (parent != null || listener != null) {
1981 return processor.queryRead(this, request, parent, procedure, listener);
1982 } catch (Throwable e) {
1983 if(e instanceof DatabaseException) throw (DatabaseException)e;
1984 else throw new DatabaseException(e);
1991 T t = processor.tryQuery(this, request);
1992 if(procedure != null)
1993 procedure.execute(this, t);
1997 } catch (Throwable throwable) {
1999 Logger.defaultLogError("Internal read request failure", throwable);
2001 if(procedure != null)
2002 procedure.exception(this, throwable);
2004 if (throwable instanceof DatabaseException)
2005 throw (DatabaseException) throwable;
2007 throw new DatabaseException(
2008 "Unexpected exception in ReadGraph.syncRequest(Read,Procedure)",
2018 public <T> T syncRequest(final Read<T> request,
2019 final SyncProcedure<T> procedure) throws DatabaseException {
2020 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2024 public <T> T syncRequest(Read<T> request, Procedure<T> procedure)
2025 throws DatabaseException {
2026 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2029 static class AsyncReadProcedure<T> implements AsyncProcedure<T> {
2031 private static Throwable DONE = new Throwable();
2034 Throwable exception = null;
2037 public void execute(AsyncReadGraph graph, T t) {
2043 public void exception(AsyncReadGraph graph, Throwable t) {
2047 public void checkAndThrow() throws DatabaseException {
2048 if(exception != DONE) {
2049 if (exception instanceof DatabaseException)
2050 throw (DatabaseException) exception;
2052 throw new DatabaseException(
2053 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
2058 public boolean done() {
2059 return exception != null;
2065 public <T> T syncRequest(final AsyncRead<T> request)
2066 throws DatabaseException {
2068 assert (request != null);
2069 AsyncReadProcedure<T> procedure = new AsyncReadProcedure<T>();
2070 syncRequest(request, procedure);
2071 procedure.checkAndThrow();
2072 return procedure.result;
2074 // return syncRequest(request, new AsyncProcedureAdapter<T>());
2079 public <T> T syncRequest(AsyncRead<T> request, AsyncListener<T> procedure)
2080 throws DatabaseException {
2081 return syncRequest(request, (AsyncProcedure<T>) procedure);
2085 public <T> T syncRequest(AsyncRead<T> request, SyncListener<T> procedure)
2086 throws DatabaseException {
2087 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
2091 public <T> T syncRequest(AsyncRead<T> request, Listener<T> procedure)
2092 throws DatabaseException {
2093 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
2097 final public <T> T syncRequest(final AsyncRead<T> request,
2098 final AsyncProcedure<T> procedure) throws DatabaseException {
2100 assert (request != null);
2102 // System.out.println("syncRequest " + request + " syncParent=" +
2105 ListenerBase listener = getListenerBase(procedure);
2107 if (parent != null || listener != null || ((request.getFlags() & RequestFlags.SCHEDULE) > 0)) {
2109 // Object syncParent = request;
2111 // final ReadGraphImpl newGraph = newSync();
2113 final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
2114 procedure, request);
2116 processor.query(this, request, parent, wrapper, listener);
2118 // newGraph.waitAsync(syncParent);
2120 Throwable e = wrapper.getException();
2122 // The request was async - produce meaningful stack trace by
2124 if (e instanceof DatabaseException)
2125 throw (DatabaseException) e;
2127 throw new DatabaseException(e);
2130 return wrapper.getResult();
2134 // System.out.println("direct call " + request );
2136 // Do not set the sync state.parent for external threads
2137 // Object syncParent = request;
2139 // final ReadGraphImpl newGraph = newSync();
2141 final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
2142 procedure, request);
2146 processor.tryQuery(this, request, wrapper);
2148 } catch (Throwable t) {
2150 wrapper.exception(this, t);
2154 Throwable e = wrapper.getException();
2156 // The request was async - produce meaningful stack trace by
2158 if (e instanceof DatabaseException)
2159 throw (DatabaseException) e;
2161 throw new DatabaseException(e);
2164 return wrapper.getResult();
2170 final private <T> void syncRequest(final AsyncRead<T> request, final AsyncReadProcedure<T> procedure) throws DatabaseException {
2172 assert (request != null);
2174 // System.out.println("syncRequest " + request + " syncParent=" +
2177 ListenerBase listener = getListenerBase(procedure);
2179 if (parent != null || listener != null || ((request.getFlags() & RequestFlags.SCHEDULE) > 0)) {
2181 // final ReadGraphImpl newGraph = newSync();
2183 final ResultCallWrappedSingleQueryProcedure4<T> wrapper = new ResultCallWrappedSingleQueryProcedure4<T>(
2184 procedure, request);
2186 processor.query(this, request, parent, wrapper, listener);
2192 // final ReadGraphImpl newGraph = newSync();
2193 processor.tryQuery(this, request, procedure);
2194 // newGraph.waitAsync(null);
2195 waitAsyncProcedure(procedure);
2197 } catch (Throwable t) {
2198 if(Development.DEVELOPMENT) {
2199 if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
2200 t.printStackTrace();
2203 procedure.exception(this, t);
2204 waitAsyncProcedure(procedure);
2212 public <T> T syncRequest(AsyncRead<T> request,
2213 final SyncProcedure<T> procedure) throws DatabaseException {
2214 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2218 final public <T> T syncRequest(final AsyncRead<T> request,
2219 final Procedure<T> procedure) throws DatabaseException {
2220 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2224 public <T> Collection<T> syncRequest(final MultiRead<T> request)
2225 throws DatabaseException {
2227 assert (request != null);
2229 final ArrayList<T> result = new ArrayList<T>();
2230 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2232 syncRequest(request, new AsyncMultiProcedure<T>() {
2235 public void execute(AsyncReadGraph graph, T t) {
2236 synchronized (result) {
2242 public void finished(AsyncReadGraph graph) {
2246 public void exception(AsyncReadGraph graph, Throwable t) {
2251 public String toString() {
2252 return "syncRequest(MultiRead) -> " + request;
2257 Throwable t = exception.get();
2259 if (t instanceof DatabaseException)
2260 throw (DatabaseException) t;
2262 throw new DatabaseException(
2263 "Unexpected exception in ReadGraph.syncRequest(Read)",
2272 public <T> Collection<T> syncRequest(MultiRead<T> request,
2273 AsyncMultiListener<T> procedure) {
2274 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2278 public <T> Collection<T> syncRequest(MultiRead<T> request,
2279 SyncMultiListener<T> procedure) {
2280 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2284 public <T> Collection<T> syncRequest(MultiRead<T> request,
2285 MultiListener<T> procedure) {
2286 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2290 public <T> Collection<T> syncRequest(MultiRead<T> request,
2291 AsyncMultiProcedure<T> procedure) {
2293 assert (request != null);
2295 ListenerBase listener = getListenerBase(procedure);
2297 if (parent != null || listener != null) {
2299 // Object syncParent = request;
2301 // final ReadGraphImpl newGraph = newSync();
2303 processor.query(this, request, parent, procedure, listener);
2305 // newGraph.waitAsync(syncParent);
2309 // Object syncParent = request;
2311 // final ReadGraphImpl newGraph = newSync();
2313 final ResultCallWrappedQueryProcedure4<T> wrapper = new ResultCallWrappedQueryProcedure4<T>(procedure);
2317 request.perform(this, wrapper);
2319 } catch (Throwable t) {
2321 wrapper.exception(this, t);
2322 // newGraph.waitAsync(syncParent);
2334 public <T> Collection<T> syncRequest(MultiRead<T> request,
2335 SyncMultiProcedure<T> procedure) {
2336 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2340 public <T> Collection<T> syncRequest(MultiRead<T> request,
2341 MultiProcedure<T> procedure) {
2342 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2345 static class AsyncMultiReadProcedure<T> extends ArrayList<T> implements AsyncMultiProcedure<T> {
2347 private static Throwable DONE = new Throwable();
2349 private static final long serialVersionUID = -6494230465108115812L;
2351 Throwable exception = null;
2354 public synchronized void execute(AsyncReadGraph graph, T t) {
2359 public void finished(AsyncReadGraph graph) {
2364 public void exception(AsyncReadGraph graph, Throwable t) {
2368 public void checkAndThrow() throws DatabaseException {
2369 if(exception != DONE) {
2370 if (exception instanceof DatabaseException)
2371 throw (DatabaseException) exception;
2373 throw new DatabaseException(
2374 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
2379 public boolean done() {
2380 return exception != null;
2386 final public <T> Collection<T> syncRequest(AsyncMultiRead<T> request)
2387 throws DatabaseException {
2389 assert (request != null);
2391 final AsyncMultiReadProcedure<T> procedure = new AsyncMultiReadProcedure<T>();
2393 syncRequest(request, procedure);
2395 procedure.checkAndThrow();
2401 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2402 AsyncMultiListener<T> procedure) {
2403 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2407 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2408 SyncMultiListener<T> procedure) {
2409 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2413 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2414 MultiListener<T> procedure) {
2415 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2418 final private <T> void syncRequest(final AsyncMultiRead<T> request,
2419 final AsyncMultiReadProcedure<T> procedure) {
2421 assert (request != null);
2422 assert (procedure != null);
2424 ListenerBase listener = getListenerBase(procedure);
2426 if (parent != null || listener != null) {
2428 // Object syncParent = request;
2430 // final ReadGraphImpl newGraph = newSync();
2432 processor.query(this, request, parent, procedure, listener);
2434 // newGraph.waitAsync(syncParent);
2435 waitAsyncProcedure(procedure);
2439 // Object syncParent = callerThread == Integer.MIN_VALUE ? null
2442 // final ReadGraphImpl newGraph = newSyncAsync(syncParent);
2447 // ReadGraphImpl sync = newSync();
2448 request.perform(this, procedure);
2449 // sync.waitAsync(null);
2450 waitAsyncProcedure(procedure);
2453 } catch (Throwable t) {
2455 waitAsyncProcedure(procedure);
2466 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2467 final AsyncMultiProcedure<T> procedure) {
2469 assert (request != null);
2470 assert (procedure != null);
2472 ListenerBase listener = getListenerBase(procedure);
2474 if (parent != null || listener != null) {
2476 // Object syncParent = request;
2478 // final ReadGraphImpl newGraph = newSync();
2480 processor.query(this, request, parent, procedure, listener);
2482 // newGraph.waitAsync(syncParent);
2486 // Object syncParent = request;
2488 // final ReadGraphImpl newGraph = newSync();
2492 request.perform(this, new AsyncMultiProcedure<T>() {
2495 public void execute(AsyncReadGraph graph, T result) {
2496 procedure.execute(graph, result);
2500 public void finished(AsyncReadGraph graph) {
2501 procedure.finished(graph);
2505 public void exception(AsyncReadGraph graph, Throwable t) {
2506 procedure.exception(graph, t);
2510 public String toString() {
2511 return "syncRequest(AsyncMultiRead) -> " + procedure;
2516 } catch (Throwable t) {
2528 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2529 final SyncMultiProcedure<T> procedure) {
2530 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2534 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2535 final MultiProcedure<T> procedure) {
2536 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2540 public <T> T syncRequest(final ExternalRead<T> request)
2541 throws DatabaseException {
2543 assert (request != null);
2545 return syncRequest(request, new Procedure<T>() {
2548 public void execute(T t) {
2552 public void exception(Throwable t) {
2556 public String toString() {
2557 return "syncRequest(AsyncRead) -> " + request;
2565 public <T> T syncRequest(ExternalRead<T> request, Listener<T> procedure) throws DatabaseException {
2566 return syncRequest(request, (Procedure<T>) procedure);
2570 final public <T> T syncRequest(final ExternalRead<T> request,
2571 final Procedure<T> procedure) throws DatabaseException {
2573 assert (request != null);
2575 ListenerBase listener = getListenerBase(procedure);
2577 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2578 final DataContainer<T> result = new DataContainer<T>();
2580 if (parent != null || listener != null) {
2582 // final ReadGraphImpl newGraph = newSync();
2584 processor.query(this, request, parent, new Procedure<T>() {
2587 public void exception(Throwable throwable) {
2588 exception.set(throwable);
2589 procedure.exception(throwable);
2593 public void execute(T t) {
2595 procedure.execute(t);
2600 // newGraph.waitAsync(request);
2606 T t = processor.tryQuery(this, request);
2608 procedure.execute(t);
2610 } catch (Throwable t) {
2612 if (t instanceof DatabaseException) {
2613 exception.set((DatabaseException)t);
2614 procedure.exception(exception.get());
2616 exception.set(new DatabaseException(
2617 "Unexpected exception in ReadGraph.syncRequest(Read)",
2619 procedure.exception(exception.get());
2626 Throwable t = exception.get();
2628 if (t instanceof DatabaseException)
2629 throw (DatabaseException) t;
2631 throw new DatabaseException(
2632 "Unexpected exception in ReadGraph.syncRequest(Read)",
2636 return result.get();
2641 public void syncRequest(final Write request) throws DatabaseException {
2643 assert (request != null);
2645 throw new DatabaseException(
2646 "Write operations are not supported during read transactions!");
2651 public <T> T syncRequest(final WriteResult<T> request) throws DatabaseException {
2653 assert (request != null);
2655 throw new DatabaseException(
2656 "Write operations are not supported during read transactions!");
2661 public void syncRequest(final DelayedWrite request)
2662 throws DatabaseException {
2664 assert (request != null);
2666 throw new DatabaseException(
2667 "Write operations are not supported during read transactions!");
2672 public <T> T syncRequest(final DelayedWriteResult<T> request) throws DatabaseException {
2674 assert (request != null);
2676 throw new DatabaseException(
2677 "Write operations are not supported during read transactions!");
2682 public void syncRequest(final WriteOnly request) throws DatabaseException {
2684 assert (request != null);
2686 throw new DatabaseException(
2687 "Write operations are not supported during read transactions!");
2692 public <T> T syncRequest(final WriteOnlyResult<T> request) throws DatabaseException {
2694 assert (request != null);
2696 throw new DatabaseException(
2697 "Write operations are not supported during read transactions!");
2702 public <T> void async(ReadInterface<T> r, AsyncProcedure<T> procedure) {
2703 r.request(this, procedure);
2707 public <T> void async(ReadInterface<T> r, Procedure<T> procedure) {
2708 r.request(this, procedure);
2712 public <T> void async(ReadInterface<T> r, SyncProcedure<T> procedure) {
2713 r.request(this, procedure);
2717 public <T> void async(ReadInterface<T> r, AsyncListener<T> procedure) {
2718 r.request(this, procedure);
2722 public <T> void async(ReadInterface<T> r, Listener<T> procedure) {
2723 r.request(this, procedure);
2727 public <T> void async(ReadInterface<T> r, SyncListener<T> procedure) {
2728 r.request(this, procedure);
2732 public <T> T sync(ReadInterface<T> r) throws DatabaseException {
2733 return r.request(this);
2737 public <T> T sync(WriteInterface<T> r) throws DatabaseException {
2738 return r.request(this);
2742 public <T> void async(WriteInterface<T> r, Procedure<T> procedure) {
2743 r.request(this, procedure);
2747 public <T> void async(WriteInterface<T> r) {
2748 r.request(this, new ProcedureAdapter<T>());
2752 * Implementation of the interface AsyncReadGraph
2756 public void forURI(Resource resource, AsyncListener<String> listener) {
2757 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2762 public void forURI(Resource resource, SyncListener<String> listener) {
2763 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2768 public void forURI(Resource resource, Listener<String> listener) {
2769 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2774 final public void forURI(final Resource resource,
2775 final AsyncProcedure<String> procedure) {
2777 assert (resource != null);
2778 assert (procedure != null);
2780 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2786 public void forURI(Resource resource, SyncProcedure<String> procedure) {
2787 forURI(resource, new SyncToAsyncProcedure<String>(procedure));
2791 public void forURI(Resource resource, Procedure<String> procedure) {
2792 forURI(resource, new NoneToAsyncProcedure<String>(procedure));
2796 public void forResource(String id, AsyncListener<Resource> listener) {
2797 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2802 public void forResource(String id, SyncListener<Resource> listener) {
2803 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2808 public void forResource(String id, Listener<Resource> listener) {
2809 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2814 final public void forResource(final String id,
2815 final AsyncProcedure<Resource> procedure) {
2817 assert (id != null);
2818 assert (procedure != null);
2820 processor.forResource(this, id, procedure);
2825 public void forResource(String id, SyncProcedure<Resource> procedure) {
2826 forResource(id, new SyncToAsyncProcedure<Resource>(procedure));
2830 public void forResource(String id, Procedure<Resource> procedure) {
2831 forResource(id, new NoneToAsyncProcedure<Resource>(procedure));
2835 public void forBuiltin(String id, AsyncListener<Resource> listener) {
2836 asyncRequest(new Builtin(id), listener);
2840 public void forBuiltin(String id, SyncListener<Resource> listener) {
2841 asyncRequest(new Builtin(id), listener);
2845 public void forBuiltin(String id, Listener<Resource> listener) {
2846 asyncRequest(new Builtin(id), listener);
2850 final public void forBuiltin(final String id,
2851 final AsyncProcedure<Resource> procedure) {
2853 assert (id != null);
2854 assert (procedure != null);
2856 processor.forBuiltin(this, id, procedure);
2861 public void forBuiltin(String id, SyncProcedure<Resource> procedure) {
2862 forBuiltin(id, new SyncToAsyncProcedure<Resource>(procedure));
2866 public void forBuiltin(String id, Procedure<Resource> procedure) {
2867 forBuiltin(id, new NoneToAsyncProcedure<Resource>(procedure));
2871 final public void forEachStatement(Resource subject, Resource relation,
2872 AsyncMultiProcedure<Statement> procedure) {
2874 assert (subject != null);
2875 assert (relation != null);
2876 assert (procedure != null);
2878 processor.forEachStatement(this, subject, relation, procedure);
2883 public void forEachStatement(Resource subject, Resource relation,
2884 SyncMultiProcedure<Statement> procedure) {
2885 forEachStatement(subject, relation,
2886 new SyncToAsyncMultiProcedure<Statement>(procedure));
2890 final public void forEachStatement(Resource subject, Resource relation,
2891 MultiProcedure<Statement> procedure) {
2893 assert (subject != null);
2894 assert (relation != null);
2895 assert (procedure != null);
2897 processor.forEachStatement(this, subject, relation, procedure);
2902 final public void forStatementSet(Resource subject, Resource relation,
2903 AsyncSetListener<Statement> procedure) {
2905 assert (subject != null);
2906 assert (relation != null);
2907 assert (procedure != null);
2909 processor.forStatementSet(this, subject, relation, procedure);
2914 final public void forStatementSet(Resource subject, Resource relation,
2915 SyncSetListener<Statement> procedure) {
2916 forStatementSet(subject, relation,
2917 new SyncToAsyncSetProcedure<Statement>(procedure));
2921 public void forStatementSet(Resource subject, Resource relation,
2922 SetListener<Statement> listener) {
2923 forStatementSet(subject, relation,
2924 new NoneToAsyncSetProcedure<Statement>(listener));
2928 final public void forEachAssertedStatement(final Resource subject,
2929 final Resource relation,
2930 final AsyncMultiProcedure<Statement> procedure) {
2932 assert (subject != null);
2933 assert (relation != null);
2934 assert (procedure != null);
2936 processor.forEachAssertedStatement(this, subject, relation, procedure);
2941 public void forEachAssertedStatement(Resource subject, Resource relation,
2942 SyncMultiProcedure<Statement> procedure) {
2943 forEachAssertedStatement(subject, relation,
2944 new SyncToAsyncMultiProcedure<Statement>(procedure));
2948 public void forEachAssertedStatement(Resource subject, Resource relation,
2949 MultiProcedure<Statement> procedure) {
2950 forEachAssertedStatement(subject, relation,
2951 new NoneToAsyncMultiProcedure<Statement>(procedure));
2955 public void forAssertedStatementSet(Resource subject, Resource relation,
2956 AsyncSetListener<Statement> procedure) {
2958 assert (subject != null);
2959 assert (relation != null);
2960 assert (procedure != null);
2962 processor.forAssertedStatementSet(this, subject, relation, procedure);
2967 public void forAssertedStatementSet(Resource subject, Resource relation,
2968 SyncSetListener<Statement> procedure) {
2970 assert (subject != null);
2971 assert (relation != null);
2972 assert (procedure != null);
2974 forAssertedStatementSet(subject, relation,
2975 new SyncToAsyncSetProcedure<Statement>(procedure));
2980 public void forAssertedStatementSet(Resource subject, Resource relation,
2981 SetListener<Statement> procedure) {
2983 assert (subject != null);
2984 assert (relation != null);
2985 assert (procedure != null);
2987 forAssertedStatementSet(subject, relation,
2988 new NoneToAsyncSetProcedure<Statement>(procedure));
2993 final public void forEachPredicate(final Resource subject,
2994 final AsyncMultiProcedure<Resource> procedure) {
2996 assert (subject != null);
2997 assert (procedure != null);
2999 processor.forEachPredicate(this, subject, procedure);
3004 public void forEachPredicate(Resource subject,
3005 SyncMultiProcedure<Resource> procedure) {
3006 forEachPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(
3011 final public void forEachPredicate(final Resource subject,
3012 final MultiProcedure<Resource> procedure) {
3014 assert (subject != null);
3015 assert (procedure != null);
3017 processor.forEachPredicate(this, subject, procedure);
3022 final public void forPredicateSet(final Resource subject,
3023 final AsyncSetListener<Resource> procedure) {
3025 assert (subject != null);
3026 assert (procedure != null);
3028 processor.forPredicateSet(this, subject, procedure);
3033 final public void forPredicateSet(final Resource subject,
3034 final SyncSetListener<Resource> procedure) {
3036 assert (subject != null);
3037 assert (procedure != null);
3039 forPredicateSet(subject, new SyncToAsyncSetProcedure<Resource>(
3045 final public void forPredicateSet(final Resource subject,
3046 final SetListener<Resource> procedure) {
3048 assert (subject != null);
3049 assert (procedure != null);
3051 forPredicateSet(subject, new NoneToAsyncSetProcedure<Resource>(
3057 final public void forEachPrincipalType(final Resource subject,
3058 final AsyncMultiProcedure<Resource> procedure) {
3060 assert (subject != null);
3061 assert (procedure != null);
3063 processor.forEachPrincipalType(this, subject, procedure);
3068 public void forEachPrincipalType(Resource subject,
3069 SyncMultiProcedure<Resource> procedure) {
3070 forEachPrincipalType(subject, new SyncToAsyncMultiProcedure<Resource>(
3075 final public void forEachPrincipalType(final Resource subject,
3076 final MultiProcedure<Resource> procedure) {
3078 assert (subject != null);
3079 assert (procedure != null);
3081 processor.forEachPrincipalType(this, subject, procedure);
3086 final public void forPrincipalTypeSet(final Resource subject,
3087 final AsyncSetListener<Resource> procedure) {
3089 assert (subject != null);
3090 assert (procedure != null);
3092 processor.forPrincipalTypeSet(this, subject, procedure);
3097 final public void forPrincipalTypeSet(final Resource subject,
3098 final SyncSetListener<Resource> procedure) {
3100 assert (subject != null);
3101 assert (procedure != null);
3103 forPrincipalTypeSet(subject, new SyncToAsyncSetProcedure<Resource>(
3109 final public void forPrincipalTypeSet(final Resource subject,
3110 final SetListener<Resource> procedure) {
3112 assert (subject != null);
3113 assert (procedure != null);
3115 forPrincipalTypeSet(subject, new NoneToAsyncSetProcedure<Resource>(
3121 public void forTypes(Resource subject, AsyncListener<Set<Resource>> listener) {
3122 asyncRequest(new Types(subject), listener);
3126 public void forTypes(Resource subject, SyncListener<Set<Resource>> listener) {
3127 asyncRequest(new Types(subject), listener);
3131 public void forTypes(Resource subject, Listener<Set<Resource>> listener) {
3132 asyncRequest(new Types(subject), listener);
3136 final public void forTypes(final Resource subject,
3137 final AsyncProcedure<Set<Resource>> procedure) {
3139 assert (subject != null);
3140 assert (procedure != null);
3142 processor.forTypes(this, subject, procedure);
3147 public void forTypes(Resource subject,
3148 SyncProcedure<Set<Resource>> procedure) {
3149 forTypes(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
3153 public void forTypes(Resource subject, Procedure<Set<Resource>> procedure) {
3154 forTypes(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
3158 public void forSupertypes(Resource subject,
3159 AsyncListener<Set<Resource>> listener) {
3160 asyncRequest(new Types(subject), listener);
3164 public void forSupertypes(Resource subject,
3165 SyncListener<Set<Resource>> listener) {
3166 asyncRequest(new Types(subject), listener);
3170 public void forSupertypes(Resource subject, Listener<Set<Resource>> listener) {
3171 asyncRequest(new Types(subject), listener);
3175 final public void forSupertypes(final Resource subject,
3176 final AsyncProcedure<Set<Resource>> procedure) {
3178 assert (subject != null);
3179 assert (procedure != null);
3181 processor.forSupertypes(this, subject, procedure);
3186 public void forSupertypes(Resource subject,
3187 SyncProcedure<Set<Resource>> procedure) {
3188 forSupertypes(subject, new SyncToAsyncProcedure<Set<Resource>>(
3193 public void forSupertypes(Resource subject,
3194 Procedure<Set<Resource>> procedure) {
3195 forSupertypes(subject, new NoneToAsyncProcedure<Set<Resource>>(
3200 public void forDirectSuperrelations(Resource subject,
3201 AsyncMultiProcedure<Resource> procedure) {
3203 assert (subject != null);
3204 assert (procedure != null);
3206 processor.forDirectSuperrelations(this, subject, procedure);
3211 public void forPossibleSuperrelation(Resource subject, AsyncProcedure<Resource> procedure) {
3213 assert (subject != null);
3214 assert (procedure != null);
3216 processor.forPossibleSuperrelation(this, subject, procedure);
3221 public void forSuperrelations(Resource subject,
3222 AsyncListener<Set<Resource>> listener) {
3223 asyncRequest(new Types(subject), listener);
3227 public void forSuperrelations(Resource subject,
3228 SyncListener<Set<Resource>> listener) {
3229 asyncRequest(new Types(subject), listener);
3233 public void forSuperrelations(Resource subject,
3234 Listener<Set<Resource>> listener) {
3235 asyncRequest(new Types(subject), listener);
3239 final public void forSuperrelations(final Resource subject,
3240 final AsyncProcedure<Set<Resource>> procedure) {
3242 assert (subject != null);
3243 assert (procedure != null);
3245 processor.forSuperrelations(this, subject, procedure);
3250 public void forSuperrelations(Resource subject,
3251 SyncProcedure<Set<Resource>> procedure) {
3252 forSuperrelations(subject, new SyncToAsyncProcedure<Set<Resource>>(
3257 public void forSuperrelations(Resource subject,
3258 Procedure<Set<Resource>> procedure) {
3259 forSuperrelations(subject, new NoneToAsyncProcedure<Set<Resource>>(
3264 final public void forEachObject(final Resource subject, final Resource relation, final AsyncMultiProcedure<Resource> procedure) {
3265 processor.forEachObject(this, subject, relation, procedure);
3269 public void forEachObject(Resource subject, Resource relation,
3270 SyncMultiProcedure<Resource> procedure) {
3271 forEachObject(subject, relation,
3272 new SyncToAsyncMultiProcedure<Resource>(procedure));
3276 public void forEachObject(Resource subject, Resource relation,
3277 MultiProcedure<Resource> procedure) {
3279 processor.forEachObject(this, subject, relation, procedure);
3284 // final public void forEachDirectObject(final Resource subject,
3285 // final Resource relation,
3286 // final AsyncMultiProcedure<Resource> procedure) {
3288 // processor.forEachDirectObject(this, subject, relation, procedure);
3293 // public void forEachDirectObject(Resource subject, Resource relation,
3294 // SyncMultiProcedure<Resource> procedure) {
3295 // forEachDirectObject(subject, relation,
3296 // new SyncToAsyncMultiProcedure<Resource>(procedure));
3300 // public void forEachDirectObject(Resource subject, Resource relation,
3301 // MultiProcedure<Resource> procedure) {
3302 // forEachDirectObject(subject, relation,
3303 // new NoneToAsyncMultiProcedure<Resource>(procedure));
3307 final public void forEachDirectPredicate(final Resource subject, final AsyncMultiProcedure<Resource> procedure) {
3308 processor.forEachDirectPredicate(this, subject, procedure);
3312 public void forEachDirectPredicate(Resource subject, SyncMultiProcedure<Resource> procedure) {
3313 forEachDirectPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(procedure));
3317 public void forEachDirectPredicate(Resource subject, MultiProcedure<Resource> procedure) {
3318 forEachDirectPredicate(subject, new NoneToAsyncMultiProcedure<Resource>(procedure));
3322 final public void forObjectSet(final Resource subject,
3323 final Resource relation, final AsyncSetListener<Resource> procedure) {
3325 assert (subject != null);
3326 assert (relation != null);
3327 assert (procedure != null);
3329 processor.forObjectSet(this, subject, relation, procedure);
3334 final public void forObjectSet(final Resource subject,
3335 final Resource relation, final SyncSetListener<Resource> procedure) {
3337 assert (subject != null);
3338 assert (relation != null);
3339 assert (procedure != null);
3341 forObjectSet(subject, relation, new SyncToAsyncSetProcedure<Resource>(
3347 final public void forObjectSet(final Resource subject,
3348 final Resource relation, final SetListener<Resource> procedure) {
3350 assert (subject != null);
3351 assert (relation != null);
3352 assert (procedure != null);
3354 forObjectSet(subject, relation, new NoneToAsyncSetProcedure<Resource>(
3360 final public void forEachAssertedObject(final Resource subject,
3361 final Resource relation,
3362 final AsyncMultiProcedure<Resource> procedure) {
3364 assert (subject != null);
3365 assert (relation != null);
3366 assert (procedure != null);
3368 processor.forEachAssertedObject(this, subject, relation, procedure);
3373 public void forEachAssertedObject(Resource subject, Resource relation,
3374 SyncMultiProcedure<Resource> procedure) {
3376 assert (subject != null);
3377 assert (relation != null);
3378 assert (procedure != null);
3380 forEachAssertedObject(subject, relation,
3381 new SyncToAsyncMultiProcedure<Resource>(procedure));
3386 public void forEachAssertedObject(Resource subject, Resource relation,
3387 MultiProcedure<Resource> procedure) {
3389 assert (subject != null);
3390 assert (relation != null);
3391 assert (procedure != null);
3393 forEachAssertedObject(subject, relation,
3394 new NoneToAsyncMultiProcedure<Resource>(procedure));
3399 public void forAssertedObjectSet(Resource subject, Resource relation,
3400 AsyncSetListener<Resource> procedure) {
3402 assert (subject != null);
3403 assert (relation != null);
3404 assert (procedure != null);
3406 processor.forAssertedObjectSet(this, subject, relation, procedure);
3411 public void forAssertedObjectSet(Resource subject, Resource relation,
3412 SyncSetListener<Resource> procedure) {
3414 assert (subject != null);
3415 assert (relation != null);
3416 assert (procedure != null);
3418 forAssertedObjectSet(subject, relation,
3419 new SyncToAsyncSetProcedure<Resource>(procedure));
3424 public void forAssertedObjectSet(Resource subject, Resource relation,
3425 SetListener<Resource> procedure) {
3427 assert (subject != null);
3428 assert (relation != null);
3429 assert (procedure != null);
3431 forAssertedObjectSet(subject, relation,
3432 new NoneToAsyncSetProcedure<Resource>(procedure));
3437 public void forInverse(Resource relation, AsyncListener<Resource> listener) {
3438 asyncRequest(new Inverse(relation), listener);
3442 public void forInverse(Resource relation, SyncListener<Resource> listener) {
3443 asyncRequest(new Inverse(relation), listener);
3447 public void forInverse(Resource relation, Listener<Resource> listener) {
3448 asyncRequest(new Inverse(relation), listener);
3452 final public void forInverse(final Resource relation,
3453 final AsyncProcedure<Resource> procedure) {
3455 assert (relation != null);
3456 assert (procedure != null);
3458 processor.forInverse(this, relation, new AsyncProcedure<Resource>() {
3461 public void execute(AsyncReadGraph graph, Resource result) {
3463 procedure.execute(graph, result);
3465 procedure.exception(graph, new NoInverseException(relation
3471 public void exception(AsyncReadGraph graph, Throwable throwable) {
3472 procedure.exception(graph, throwable);
3476 public String toString() {
3477 return "forInverse -> " + procedure;
3485 public void forInverse(Resource relation, SyncProcedure<Resource> procedure) {
3486 forInverse(relation, new SyncToAsyncProcedure<Resource>(procedure));
3490 public void forInverse(Resource relation, Procedure<Resource> procedure) {
3491 forInverse(relation, new NoneToAsyncProcedure<Resource>(procedure));
3495 public void forSingleObject(Resource subject, Resource relation,
3496 AsyncListener<Resource> listener) {
3497 asyncRequest(new SingleObject(subject, relation), listener);
3501 public void forSingleObject(Resource subject, Resource relation,
3502 SyncListener<Resource> listener) {
3503 asyncRequest(new SingleObject(subject, relation), listener);
3507 public void forSingleObject(Resource subject, Resource relation,
3508 Listener<Resource> listener) {
3509 asyncRequest(new SingleObject(subject, relation), listener);
3513 final public void forSingleObject(final Resource subject,
3514 final Resource relation, final AsyncProcedure<Resource> procedure) {
3516 assert (subject != null);
3517 assert (relation != null);
3518 assert (procedure != null);
3520 processor.forEachObject(this, subject, relation,
3521 new SingleOrErrorProcedure<Resource>(procedure));
3526 public void forSingleObject(Resource subject, Resource relation,
3527 SyncProcedure<Resource> procedure) {
3528 forSingleObject(subject, relation, new SyncToAsyncProcedure<Resource>(
3533 public void forSingleObject(Resource subject, Resource relation,
3534 Procedure<Resource> procedure) {
3535 forSingleObject(subject, relation, new NoneToAsyncProcedure<Resource>(
3540 public void forSingleStatement(Resource subject, Resource relation,
3541 AsyncListener<Statement> listener) {
3542 asyncRequest(new SingleStatement(subject, relation), listener);
3546 public void forSingleStatement(Resource subject, Resource relation,
3547 SyncListener<Statement> listener) {
3548 asyncRequest(new SingleStatement(subject, relation), listener);
3552 public void forSingleStatement(Resource subject, Resource relation,
3553 Listener<Statement> listener) {
3554 asyncRequest(new SingleStatement(subject, relation), listener);
3558 final public void forSingleStatement(final Resource subject,
3559 final Resource relation, final AsyncProcedure<Statement> procedure) {
3561 assert (subject != null);
3562 assert (relation != null);
3563 assert (procedure != null);
3565 processor.forEachStatement(this, subject, relation,
3566 new SingleOrErrorProcedure<Statement>(procedure));
3571 public void forSingleStatement(Resource subject, Resource relation,
3572 SyncProcedure<Statement> procedure) {
3573 forSingleStatement(subject, relation,
3574 new SyncToAsyncProcedure<Statement>(procedure));
3578 public void forSingleStatement(Resource subject, Resource relation,
3579 Procedure<Statement> procedure) {
3580 forSingleStatement(subject, relation,
3581 new NoneToAsyncProcedure<Statement>(procedure));
3585 public void forSingleType(Resource subject,
3586 AsyncListener<Resource> listener) {
3587 asyncRequest(new SingleTypeAny(subject), listener);
3591 public void forSingleType(Resource subject,
3592 SyncListener<Resource> listener) {
3593 asyncRequest(new SingleTypeAny(subject), listener);
3597 public void forSingleType(Resource subject,
3598 Listener<Resource> listener) {
3599 asyncRequest(new SingleTypeAny(subject), listener);
3603 final public void forSingleType(final Resource subject, final AsyncProcedure<Resource> procedure) {
3605 assert (subject != null);
3606 assert (procedure != null);
3608 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3610 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
3613 public void execute(AsyncReadGraph graph, final Resource principalType) {
3614 checkedProcedure.offer(graph, principalType);
3618 public void finished(AsyncReadGraph graph) {
3619 checkedProcedure.dec(graph);
3623 public void exception(AsyncReadGraph graph, Throwable t) {
3624 checkedProcedure.exception(graph, t);
3628 public String toString() {
3629 return "forSingleType -> " + procedure;
3637 public void forSingleType(Resource subject, SyncProcedure<Resource> procedure) {
3638 forSingleType(subject, new SyncToAsyncProcedure<Resource>(
3643 public void forSingleType(Resource subject, Procedure<Resource> procedure) {
3644 forSingleType(subject, new NoneToAsyncProcedure<Resource>(
3649 public void forSingleType(Resource subject, Resource relation,
3650 AsyncListener<Resource> listener) {
3651 asyncRequest(new SingleType(subject, relation), listener);
3655 public void forSingleType(Resource subject, Resource relation,
3656 SyncListener<Resource> listener) {
3657 asyncRequest(new SingleType(subject, relation), listener);
3661 public void forSingleType(Resource subject, Resource relation,
3662 Listener<Resource> listener) {
3663 asyncRequest(new SingleType(subject, relation), listener);
3667 final public void forSingleType(final Resource subject,
3668 final Resource baseType, final AsyncProcedure<Resource> procedure) {
3670 assert (subject != null);
3671 assert (procedure != null);
3673 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3675 processor.forEachPrincipalType(this, subject,
3676 new AsyncMultiProcedureAdapter<Resource>() {
3679 public void execute(AsyncReadGraph graph,
3680 final Resource principalType) {
3682 checkedProcedure.inc();
3684 if(baseType == null) {
3686 checkedProcedure.offer(graph, principalType);
3687 checkedProcedure.dec(graph);
3689 } else if(principalType.equals(baseType)) {
3691 checkedProcedure.offer(graph, principalType);
3692 checkedProcedure.dec(graph);
3696 processor.forSupertypes((ReadGraphImpl)graph, principalType,
3697 new AsyncProcedure<Set<Resource>>() {
3700 public void execute(
3701 AsyncReadGraph graph,
3702 Set<Resource> result) {
3704 if (result.contains(baseType))
3705 checkedProcedure.offer(graph,
3707 checkedProcedure.dec(graph);
3712 public void exception(
3713 AsyncReadGraph graph,
3716 .exception(graph, t);
3726 public void finished(AsyncReadGraph graph) {
3727 checkedProcedure.dec(graph);
3731 public void exception(AsyncReadGraph graph, Throwable t) {
3732 checkedProcedure.exception(graph, t);
3736 public String toString() {
3737 return "forSingleType -> " + procedure;
3745 public void forSingleType(Resource subject, Resource relation,
3746 SyncProcedure<Resource> procedure) {
3747 forSingleType(subject, relation, new SyncToAsyncProcedure<Resource>(
3752 public void forSingleType(Resource subject, Resource relation,
3753 Procedure<Resource> procedure) {
3754 forSingleType(subject, relation, new NoneToAsyncProcedure<Resource>(
3759 public <T> void forValue(Resource subject, Binding binding,
3760 AsyncListener<T> listener) {
3761 asyncRequest(new Value<T>(subject, binding), listener);
3765 public <T> void forValue(Resource subject, Binding binding,
3766 SyncListener<T> listener) {
3767 asyncRequest(new Value<T>(subject, binding), listener);
3771 public <T> void forValue(Resource subject, Binding binding,
3772 Listener<T> listener) {
3773 asyncRequest(new Value<T>(subject, binding), listener);
3777 public <T> void forValue(final Resource resource, final Binding binding,
3778 final AsyncProcedure<T> procedure) {
3780 assert (resource != null);
3781 assert (binding != null);
3782 assert (procedure != null);
3784 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
3787 public void execute(AsyncReadGraph graph, byte[] result) {
3791 if (result == null) {
3792 procedure.exception(graph,
3793 new DoesNotContainValueException(
3794 "No value for resource " + resource));
3798 Serializer serializer = binding.serializer();
3799 // Serializer serializer = Bindings.getSerializer( binding );
3800 Object obj = serializer.deserialize(result);
3801 // if (!binding.isInstance(obj))
3802 // procedure.exception(graph, new ClassCastException(
3803 // "Cannot get value " + obj + " with binding "
3806 procedure.execute(graph, (T) obj);
3808 } catch (IOException e) {
3809 procedure.exception(graph, e);
3810 } catch (BufferUnderflowException e) {
3811 procedure.exception(graph, e);
3812 } catch (Throwable t) {
3813 procedure.exception(graph, t);
3819 public void exception(AsyncReadGraph graph, Throwable t) {
3821 procedure.exception(graph, t);
3822 } catch (Throwable t2) {
3823 Logger.defaultLogError(t2);
3828 public String toString() {
3829 return "forValue -> " + procedure;
3837 public <T> void forValue(Resource subject, Binding binding,
3838 SyncProcedure<T> procedure) {
3839 forValue(subject, binding, new SyncToAsyncProcedure<T>(procedure));
3843 public <T> void forValue(Resource subject, Binding binding,
3844 Procedure<T> procedure) {
3845 forValue(subject, binding, new NoneToAsyncProcedure<T>(procedure));
3849 public <T> void forValue(Resource subject, AsyncListener<T> listener) {
3850 asyncRequest(new ValueImplied<T>(subject), listener);
3854 public <T> void forValue(Resource subject, SyncListener<T> listener) {
3855 asyncRequest(new ValueImplied<T>(subject), listener);
3859 public <T> void forValue(Resource subject, Listener<T> listener) {
3860 asyncRequest(new ValueImplied<T>(subject), listener);
3864 final public <T> void forValue(final Resource subject, final AsyncProcedure<T> procedure) {
3866 assert (subject != null);
3867 assert (procedure != null);
3869 forRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
3872 public void execute(AsyncReadGraph graph, Datatype type) {
3873 // TODO: consider trying Bindings.getBeanBinding(type);
3874 Binding binding = Bindings.getBinding(type);
3875 graph.forValue(subject, binding, procedure);
3879 public void exception(AsyncReadGraph graph, Throwable throwable) {
3880 procedure.exception(graph, new DoesNotContainValueException("Invalid data type", throwable));
3888 public <T> void forValue(Resource subject, SyncProcedure<T> procedure) {
3889 forValue(subject, new SyncToAsyncProcedure<T>(procedure));
3893 public <T> void forValue(Resource subject, Procedure<T> procedure) {
3894 forValue(subject, new NoneToAsyncProcedure<T>(procedure));
3898 public <T> void forRelatedValue(Resource subject, Resource relation,
3899 AsyncListener<T> listener) {
3900 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3904 public <T> void forRelatedValue(Resource subject, Resource relation,
3905 SyncListener<T> listener) {
3906 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3910 public <T> void forRelatedValue(Resource subject, Resource relation,
3911 Listener<T> listener) {
3912 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3916 final public <T> void forRelatedValue(final Resource subject,
3917 final Resource relation, final AsyncProcedure<T> procedure) {
3919 assert (subject != null);
3920 assert (relation != null);
3921 assert (procedure != null);
3923 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3925 processor.forEachObject(this, subject, relation,
3926 new AsyncMultiProcedureAdapter<Resource>() {
3929 public void execute(AsyncReadGraph graph,
3930 final Resource object) {
3932 checkedProcedure.inc();
3934 graph.forValue(object, new AsyncProcedure<Object>() {
3937 public void execute(AsyncReadGraph graph,
3939 checkedProcedure.offer(graph, (T) result);
3940 checkedProcedure.dec(graph);
3944 public void exception(AsyncReadGraph graph,
3946 checkedProcedure.exception(graph, t);
3950 public String toString() {
3951 return "forRelatedValue -> " + procedure;
3959 public void finished(AsyncReadGraph graph) {
3960 checkedProcedure.dec(graph);
3964 public void exception(AsyncReadGraph graph, Throwable t) {
3965 checkedProcedure.exception(graph, t);
3973 public <T> void forRelatedValue(Resource subject, Resource relation,
3974 SyncProcedure<T> procedure) {
3975 forRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
3980 public <T> void forRelatedValue(Resource subject, Resource relation,
3981 Procedure<T> procedure) {
3982 forRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
3987 public <T> void forRelatedValue(Resource subject, Resource relation,
3988 Binding binding, AsyncListener<T> listener) {
3989 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3993 public <T> void forRelatedValue(Resource subject, Resource relation,
3994 Binding binding, SyncListener<T> listener) {
3995 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3999 public <T> void forRelatedValue(Resource subject, Resource relation,
4000 Binding binding, Listener<T> listener) {
4001 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
4005 final public <T> void forRelatedValue(final Resource subject,
4006 final Resource relation, final Binding binding,
4007 final AsyncProcedure<T> procedure) {
4009 assert (subject != null);
4010 assert (relation != null);
4011 assert (binding != null);
4012 assert (procedure != null);
4014 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
4016 processor.forEachObject(this, subject, relation,
4017 new AsyncMultiProcedureAdapter<Resource>() {
4020 public void execute(AsyncReadGraph graph,
4021 final Resource object) {
4023 checkedProcedure.inc();
4025 graph.forValue(object, binding, new AsyncProcedure<Object>() {
4028 public void execute(AsyncReadGraph graph,
4031 checkedProcedure.offer(graph,
4033 checkedProcedure.dec(graph);
4037 public void exception(AsyncReadGraph graph,
4039 checkedProcedure.exception(graph, t);
4043 public String toString() {
4044 return "forRelatedValue -> "
4053 public void finished(AsyncReadGraph graph) {
4054 checkedProcedure.dec(graph);
4058 public void exception(AsyncReadGraph graph, Throwable t) {
4059 checkedProcedure.exception(graph, t);
4067 public <T> void forRelatedValue(Resource subject, Resource relation,
4068 Binding binding, SyncProcedure<T> procedure) {
4069 forRelatedValue(subject, relation, binding,
4070 new SyncToAsyncProcedure<T>(procedure));
4074 public <T> void forRelatedValue(Resource subject, Resource relation,
4075 Binding binding, Procedure<T> procedure) {
4076 forRelatedValue(subject, relation, binding,
4077 new NoneToAsyncProcedure<T>(procedure));
4081 public <T> void forAdapted(Resource resource, Class<T> clazz,
4082 AsyncListener<T> listener) {
4083 asyncRequest(new Adapter<T>(resource, clazz), listener);
4087 public <T> void forAdapted(Resource resource, Class<T> clazz,
4088 SyncListener<T> listener) {
4089 asyncRequest(new Adapter<T>(resource, clazz), listener);
4093 public <T> void forAdapted(Resource resource, Class<T> clazz,
4094 Listener<T> listener) {
4095 asyncRequest(new Adapter<T>(resource, clazz), listener);
4099 final public <T> void forAdapted(final Resource resource,
4100 final Class<T> clazz, final AsyncProcedure<T> procedure) {
4102 assert (resource != null);
4103 assert (clazz != null);
4104 assert (procedure != null);
4106 final AdaptionService service = getSession().peekService(AdaptionService.class);
4107 if (service == null)
4108 procedure.exception(this, new ServiceException("No AdaptionService available"));
4110 service.adapt(this, resource, resource, Resource.class, clazz, false, procedure);
4115 public <T> void forAdapted(Resource resource, Class<T> clazz,
4116 SyncProcedure<T> procedure) {
4117 forAdapted(resource, clazz, new SyncToAsyncProcedure<T>(procedure));
4121 public <T> void forAdapted(Resource resource, Class<T> clazz,
4122 Procedure<T> procedure) {
4123 forAdapted(resource, clazz, new NoneToAsyncProcedure<T>(procedure));
4127 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
4128 AsyncListener<T> listener) {
4129 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
4133 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
4134 SyncListener<T> listener) {
4135 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
4139 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
4140 Listener<T> listener) {
4141 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
4145 final public <T> void forUniqueAdapted(final Resource resource,
4146 final Class<T> clazz, final AsyncProcedure<T> procedure) {
4148 assert (resource != null);
4149 assert (clazz != null);
4150 assert (procedure != null);
4152 final AdaptionService service = getSession().peekService(AdaptionService.class);
4153 if (service == null)
4154 procedure.exception(this, new ServiceException("No AdaptionService available"));
4156 service.adaptNew(this, resource, clazz, false, procedure);
4161 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
4162 SyncProcedure<T> procedure) {
4163 forUniqueAdapted(resource, clazz,
4164 new SyncToAsyncProcedure<T>(procedure));
4168 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
4169 Procedure<T> procedure) {
4170 forUniqueAdapted(resource, clazz,
4171 new NoneToAsyncProcedure<T>(procedure));
4175 public void forPossibleInverse(Resource subject,
4176 AsyncListener<Resource> listener) {
4177 asyncRequest(new PossibleInverse(subject), listener);
4181 public void forPossibleInverse(Resource subject,
4182 SyncListener<Resource> listener) {
4183 asyncRequest(new PossibleInverse(subject), listener);
4187 public void forPossibleInverse(Resource subject, Listener<Resource> listener) {
4188 asyncRequest(new PossibleInverse(subject), listener);
4192 final public void forPossibleInverse(final Resource relation,
4193 final AsyncProcedure<Resource> procedure) {
4195 assert (relation != null);
4196 assert (procedure != null);
4198 processor.forInverse(this, relation, new ExceptionToNullProcedure<Resource>(procedure));
4203 public void forPossibleInverse(Resource subject,
4204 SyncProcedure<Resource> procedure) {
4205 forPossibleInverse(subject, new SyncToAsyncProcedure<Resource>(
4210 public void forPossibleInverse(Resource subject,
4211 Procedure<Resource> procedure) {
4212 forPossibleInverse(subject, new NoneToAsyncProcedure<Resource>(
4217 public void forPossibleObject(Resource subject, Resource relation,
4218 AsyncListener<Resource> listener) {
4219 asyncRequest(new PossibleObject(subject, relation), listener);
4223 public void forPossibleObject(Resource subject, Resource relation,
4224 SyncListener<Resource> listener) {
4225 asyncRequest(new PossibleObject(subject, relation), listener);
4229 public void forPossibleObject(Resource subject, Resource relation,
4230 Listener<Resource> listener) {
4231 asyncRequest(new PossibleObject(subject, relation), listener);
4235 final public void forPossibleObject(final Resource subject,
4236 final Resource relation, final AsyncProcedure<Resource> procedure) {
4238 assert (subject != null);
4239 assert (relation != null);
4240 assert (procedure != null);
4242 processor.forEachObject(this, subject, relation,
4243 new SingleOrNullProcedure<Resource>(procedure));
4248 public void forPossibleObject(Resource subject, Resource relation,
4249 SyncProcedure<Resource> procedure) {
4250 forPossibleObject(subject, relation,
4251 new SyncToAsyncProcedure<Resource>(procedure));
4255 public void forPossibleObject(Resource subject, Resource relation,
4256 Procedure<Resource> procedure) {
4257 forPossibleObject(subject, relation,
4258 new NoneToAsyncProcedure<Resource>(procedure));
4262 public void forPossibleStatement(Resource subject, Resource relation,
4263 AsyncListener<Statement> listener) {
4264 asyncRequest(new PossibleStatement(subject, relation), listener);
4268 public void forPossibleStatement(Resource subject, Resource relation,
4269 SyncListener<Statement> listener) {
4270 asyncRequest(new PossibleStatement(subject, relation), listener);
4274 public void forPossibleStatement(Resource subject, Resource relation,
4275 Listener<Statement> listener) {
4276 asyncRequest(new PossibleStatement(subject, relation), listener);
4280 final public void forPossibleStatement(final Resource subject,
4281 final Resource relation, final AsyncProcedure<Statement> procedure) {
4283 assert (subject != null);
4284 assert (relation != null);
4285 assert (procedure != null);
4287 processor.forEachStatement(this, subject, relation,
4288 new SingleFunctionalOrNullProcedure<Statement>(
4289 "forPossibleStatement", procedure));
4294 public void forPossibleStatement(Resource subject, Resource relation,
4295 SyncProcedure<Statement> procedure) {
4296 forPossibleStatement(subject, relation,
4297 new SyncToAsyncProcedure<Statement>(procedure));
4301 public void forPossibleStatement(Resource subject, Resource relation,
4302 Procedure<Statement> procedure) {
4303 forPossibleStatement(subject, relation,
4304 new NoneToAsyncProcedure<Statement>(procedure));
4308 public void forPossibleType(Resource subject, Resource relation,
4309 AsyncListener<Resource> listener) {
4310 asyncRequest(new PossibleType(subject, relation), listener);
4314 public void forPossibleType(Resource subject, Resource relation,
4315 SyncListener<Resource> listener) {
4316 asyncRequest(new PossibleType(subject, relation), listener);
4320 public void forPossibleType(Resource subject, Resource relation,
4321 Listener<Resource> listener) {
4322 asyncRequest(new PossibleType(subject, relation), listener);
4326 final public void forPossibleType(final Resource subject,
4327 final Resource baseType, final AsyncProcedure<Resource> procedure) {
4329 assert (subject != null);
4330 assert (procedure != null);
4332 final NullSingleOrNullProcedure<Resource> checkedProcedure = new NullSingleOrNullProcedure<Resource>(procedure);
4334 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
4337 public void execute(AsyncReadGraph graph,
4338 final Resource principalType) {
4340 if (baseType == null) {
4342 checkedProcedure.offer(graph, principalType);
4344 } else if (principalType.equals(baseType)) {
4346 checkedProcedure.offer(graph, principalType);
4350 checkedProcedure.inc();
4352 processor.forSupertypes((ReadGraphImpl)graph, principalType,
4353 new AsyncProcedure<Set<Resource>>() {
4356 public void execute(
4357 AsyncReadGraph graph,
4358 Set<Resource> result) {
4360 if (result.contains(baseType)) {
4361 checkedProcedure.offer(graph,
4365 checkedProcedure.dec(graph);
4370 public void exception(
4371 AsyncReadGraph graph,
4373 checkedProcedure.exception(graph, t);
4374 checkedProcedure.dec(graph);
4378 public String toString() {
4379 return "forPossibleType -> "
4390 public void finished(AsyncReadGraph graph) {
4391 checkedProcedure.dec(graph);
4395 public void exception(AsyncReadGraph graph, Throwable t) {
4396 checkedProcedure.exception(graph, t);
4397 checkedProcedure.dec(graph);
4405 public void forPossibleType(Resource subject, Resource relation,
4406 SyncProcedure<Resource> procedure) {
4407 forPossibleType(subject, relation, new SyncToAsyncProcedure<Resource>(
4412 public void forPossibleType(Resource subject, Resource relation,
4413 Procedure<Resource> procedure) {
4414 forPossibleType(subject, relation, new NoneToAsyncProcedure<Resource>(
4419 public <T> void forPossibleValue(Resource subject, AsyncListener<T> listener) {
4420 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4424 public <T> void forPossibleValue(Resource subject, SyncListener<T> listener) {
4425 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4429 public <T> void forPossibleValue(Resource subject, Listener<T> listener) {
4430 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4434 final public <T> void forPossibleValue(final Resource subject,
4435 final AsyncProcedure<T> procedure) {
4437 assert (subject != null);
4438 assert (procedure != null);
4440 forPossibleRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
4443 public void execute(AsyncReadGraph graph, final Datatype type) {
4445 procedure.execute(graph, null);
4448 // TODO: consider trying Bindings.getBeanBinding(type);
4449 Binding binding = Bindings.getBinding(type);
4450 graph.forPossibleValue(subject, binding, procedure);
4451 } catch (RuntimeBindingConstructionException e) {
4452 procedure.exception(graph, e);
4458 public void exception(AsyncReadGraph graph, Throwable t) {
4459 procedure.exception(graph, t);
4463 public String toString() {
4464 return "forPossibleValue -> " + procedure;
4472 public <T> void forPossibleValue(Resource subject,
4473 SyncProcedure<T> procedure) {
4474 forPossibleValue(subject, new SyncToAsyncProcedure<T>(procedure));
4478 public <T> void forPossibleValue(Resource subject, Procedure<T> procedure) {
4479 forPossibleValue(subject, new NoneToAsyncProcedure<T>(procedure));
4483 public <T> void forPossibleValue(Resource subject, Binding binding,
4484 AsyncListener<T> listener) {
4485 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4489 public <T> void forPossibleValue(Resource subject, Binding binding,
4490 SyncListener<T> listener) {
4491 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4495 public <T> void forPossibleValue(Resource subject, Binding binding,
4496 Listener<T> listener) {
4497 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4501 final public <T> void forPossibleValue(final Resource resource,
4502 final Binding binding, final AsyncProcedure<T> procedure) {
4504 assert (resource != null);
4505 assert (binding != null);
4506 assert (procedure != null);
4508 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
4511 public void execute(AsyncReadGraph graph, byte[] result) {
4515 if (result == null) {
4516 procedure.execute(graph, null);
4520 Serializer serializer = Bindings.getSerializer( binding );
4521 Object obj = serializer.deserialize(result);
4522 if (!binding.isInstance(obj))
4523 procedure.exception(graph, new ClassCastException(
4524 "Cannot get value " + obj + " with binding "
4527 procedure.execute(graph, (T) obj);
4529 } catch (IOException e) {
4530 procedure.exception(graph, e);
4531 } catch (BufferUnderflowException e) {
4532 procedure.exception(graph, e);
4533 } catch (Throwable t) {
4534 procedure.exception(graph, t);
4540 public void exception(AsyncReadGraph graph, Throwable t) {
4542 procedure.exception(graph, t);
4543 } catch (Throwable t2) {
4544 Logger.defaultLogError(t2);
4549 public String toString() {
4550 return "forPossibleValue -> " + procedure;
4558 public <T> void forPossibleValue(Resource subject, Binding binding,
4559 SyncProcedure<T> procedure) {
4560 forPossibleValue(subject, binding, new SyncToAsyncProcedure<T>(
4565 public <T> void forPossibleValue(Resource subject, Binding binding,
4566 Procedure<T> procedure) {
4567 forPossibleValue(subject, binding, new NoneToAsyncProcedure<T>(
4572 public <T> void forPossibleRelatedValue(Resource subject,
4573 Resource relation, AsyncListener<T> listener) {
4574 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4579 public <T> void forPossibleRelatedValue(Resource subject,
4580 Resource relation, SyncListener<T> listener) {
4581 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4586 public <T> void forPossibleRelatedValue(Resource subject,
4587 Resource relation, Listener<T> listener) {
4588 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4593 final public <T> void forPossibleRelatedValue(final Resource subject,
4594 final Resource relation, final AsyncProcedure<T> procedure) {
4596 assert (subject != null);
4597 assert (relation != null);
4598 assert (procedure != null);
4600 final DeepSingleOrNullProcedure<T> checkedProcedure = new DeepSingleOrNullProcedure<T>(procedure);
4602 processor.forEachObject(this, subject, relation,
4603 new AsyncMultiProcedureAdapter<Resource>() {
4606 public void execute(AsyncReadGraph graph,
4607 final Resource object) {
4609 checkedProcedure.inc();
4611 graph.forValue(object, new AsyncProcedure<Object>() {
4614 public void execute(AsyncReadGraph graph,
4616 checkedProcedure.offer(graph, (T) result);
4617 checkedProcedure.dec(graph);
4621 public void exception(AsyncReadGraph graph,
4623 checkedProcedure.exception(graph, t);
4624 checkedProcedure.dec(graph);
4632 public void finished(AsyncReadGraph graph) {
4634 checkedProcedure.dec(graph);
4638 public void exception(AsyncReadGraph graph, Throwable t) {
4639 checkedProcedure.exception(graph, t);
4640 checkedProcedure.dec(graph);
4644 public String toString() {
4645 return "forPossibleRelatedValue -> " + procedure;
4652 public <T> void forPossibleRelatedValue(Resource subject,
4653 Resource relation, SyncProcedure<T> procedure) {
4654 forPossibleRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
4659 public <T> void forPossibleRelatedValue(Resource subject,
4660 Resource relation, Procedure<T> procedure) {
4661 forPossibleRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
4666 public <T> void forPossibleRelatedValue(Resource subject,
4667 Resource relation, Binding binding, AsyncListener<T> listener) {
4668 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4673 public <T> void forPossibleRelatedValue(Resource subject,
4674 Resource relation, Binding binding, SyncListener<T> listener) {
4675 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4680 public <T> void forPossibleRelatedValue(Resource subject,
4681 Resource relation, Binding binding, Listener<T> listener) {
4682 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4687 // final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4688 // final AsyncProcedure<T> procedure) {
4690 // forPossibleRelatedValue(subject, relation, binding, procedure, false);
4694 final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4695 final AsyncProcedure<T> procedure) {
4697 assert (subject != null);
4698 assert (relation != null);
4699 assert (procedure != null);
4701 processor.forPossibleObject(this, subject, relation, new AsyncProcedure<Resource>() {
4704 public void execute(AsyncReadGraph graph, Resource object) {
4706 if(object == null) {
4707 procedure.execute(graph, null);
4711 processor.forPossibleValue((ReadGraphImpl)graph, object, new AsyncProcedure<byte[]>() {
4714 public void execute(AsyncReadGraph graph, byte[] bytes) {
4720 Serializer serializer = binding.serializer();
4721 Object obj = serializer.deserialize(bytes);
4722 if (!binding.isInstance(obj)) {
4723 procedure.exception(graph, new ClassCastException("Cannot get value " + obj + " with binding " + binding));
4725 procedure.execute(graph, (T) obj);
4728 } catch (Throwable t) {
4730 procedure.exception(graph, t);
4736 procedure.execute(graph, null);
4743 public void exception(AsyncReadGraph graph, Throwable t) {
4744 procedure.exception(graph, t);
4752 public void exception(AsyncReadGraph graph, Throwable throwable) {
4753 throwable.printStackTrace();
4754 procedure.exception(graph, throwable);
4762 public <T> void forPossibleRelatedValue(Resource subject,
4763 Resource relation, Binding binding, SyncProcedure<T> procedure) {
4764 forPossibleRelatedValue(subject, relation, binding,
4765 new SyncToAsyncProcedure<T>(procedure));
4769 public <T> void forPossibleRelatedValue(Resource subject,
4770 Resource relation, Binding binding, Procedure<T> procedure) {
4771 forPossibleRelatedValue(subject, relation, binding,
4772 new NoneToAsyncProcedure<T>(procedure));
4776 public void forIsInstanceOf(Resource subject, Resource relation,
4777 AsyncListener<Boolean> listener) {
4778 asyncRequest(new IsInstanceOf(subject, relation), listener);
4782 public void forIsInstanceOf(Resource subject, Resource relation,
4783 SyncListener<Boolean> listener) {
4784 asyncRequest(new IsInstanceOf(subject, relation), listener);
4788 public void forIsInstanceOf(Resource subject, Resource relation,
4789 Listener<Boolean> listener) {
4790 asyncRequest(new IsInstanceOf(subject, relation), listener);
4794 final public void forIsInstanceOf(final Resource resource,
4795 final Resource type, final AsyncProcedure<Boolean> procedure) {
4797 assert (resource != null);
4798 assert (type != null);
4799 assert (procedure != null);
4801 forTypes(resource, new AsyncProcedure<Set<Resource>>() {
4804 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4807 if (result.contains(type))
4808 procedure.execute(graph, true);
4810 procedure.execute(graph, false);
4811 } catch (Throwable t) {
4812 Logger.defaultLogError(t);
4817 public void exception(AsyncReadGraph graph, Throwable t) {
4819 procedure.exception(graph, t);
4820 } catch (Throwable t2) {
4821 Logger.defaultLogError(t2);
4826 public String toString() {
4827 return "forIsInstanceOf -> " + procedure;
4835 public void forIsInstanceOf(Resource subject, Resource relation,
4836 SyncProcedure<Boolean> procedure) {
4837 forIsInstanceOf(subject, relation, new SyncToAsyncProcedure<Boolean>(
4842 public void forIsInstanceOf(Resource subject, Resource relation,
4843 Procedure<Boolean> procedure) {
4844 forIsInstanceOf(subject, relation, new NoneToAsyncProcedure<Boolean>(
4849 public void forIsInheritedFrom(Resource subject, Resource relation,
4850 AsyncListener<Boolean> listener) {
4851 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4855 public void forIsInheritedFrom(Resource subject, Resource relation,
4856 SyncListener<Boolean> listener) {
4857 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4861 public void forIsInheritedFrom(Resource subject, Resource relation,
4862 Listener<Boolean> listener) {
4863 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4867 final public void forIsInheritedFrom(final Resource resource,
4868 final Resource type, final AsyncProcedure<Boolean> procedure) {
4870 assert (resource != null);
4871 assert (type != null);
4872 assert (procedure != null);
4874 if (resource.equals(type)) {
4876 procedure.execute(this, true);
4877 } catch (Throwable t) {
4878 Logger.defaultLogError(t);
4883 forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
4886 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4888 if (result.contains(type))
4889 procedure.execute(graph, true);
4891 procedure.execute(graph, false);
4892 } catch (Throwable t) {
4893 Logger.defaultLogError(t);
4898 public void exception(AsyncReadGraph graph, Throwable t) {
4900 procedure.exception(graph, t);
4901 } catch (Throwable t2) {
4902 Logger.defaultLogError(t2);
4907 public String toString() {
4908 return "forIsInheritedFrom -> " + procedure;
4916 public void forIsInheritedFrom(Resource subject, Resource relation,
4917 SyncProcedure<Boolean> procedure) {
4918 forIsInheritedFrom(subject, relation,
4919 new SyncToAsyncProcedure<Boolean>(procedure));
4923 public void forIsInheritedFrom(Resource subject, Resource relation,
4924 Procedure<Boolean> procedure) {
4925 forIsInheritedFrom(subject, relation,
4926 new NoneToAsyncProcedure<Boolean>(procedure));
4930 public void forIsSubrelationOf(Resource subject, Resource relation,
4931 AsyncListener<Boolean> listener) {
4932 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4936 public void forIsSubrelationOf(Resource subject, Resource relation,
4937 SyncListener<Boolean> listener) {
4938 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4942 public void forIsSubrelationOf(Resource subject, Resource relation,
4943 Listener<Boolean> listener) {
4944 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4948 final public void forIsSubrelationOf(final Resource resource,
4949 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4951 assert (resource != null);
4952 assert (relation != null);
4953 assert (procedure != null);
4955 if (resource.equals(relation)) {
4956 procedure.execute(this, true);
4960 forSuperrelations(resource, new AsyncProcedure<Set<Resource>>() {
4963 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4965 if (result.contains(relation))
4966 procedure.execute(graph, true);
4968 procedure.execute(graph, false);
4969 } catch (Throwable t) {
4970 Logger.defaultLogError(t);
4975 public void exception(AsyncReadGraph graph, Throwable t) {
4977 procedure.exception(graph, t);
4978 } catch (Throwable t2) {
4979 Logger.defaultLogError(t2);
4984 public String toString() {
4985 return "forIsSubrelationOf -> " + procedure;
4993 public void forIsSubrelationOf(Resource subject, Resource relation,
4994 SyncProcedure<Boolean> procedure) {
4995 forIsSubrelationOf(subject, relation,
4996 new SyncToAsyncProcedure<Boolean>(procedure));
5000 public void forIsSubrelationOf(Resource subject, Resource relation,
5001 Procedure<Boolean> procedure) {
5002 forIsSubrelationOf(subject, relation,
5003 new NoneToAsyncProcedure<Boolean>(procedure));
5007 public void forHasStatement(Resource subject,
5008 AsyncListener<Boolean> listener) {
5009 asyncRequest(new HasStatementSubject(subject), listener);
5013 public void forHasStatement(Resource subject, SyncListener<Boolean> listener) {
5014 asyncRequest(new HasStatementSubject(subject), listener);
5018 public void forHasStatement(Resource subject, Listener<Boolean> listener) {
5019 asyncRequest(new HasStatementSubject(subject), listener);
5023 final public void forHasStatement(final Resource subject,
5024 final AsyncProcedure<Boolean> procedure) {
5026 assert (subject != null);
5027 assert (procedure != null);
5029 processor.forHasStatement(this, subject, procedure);
5034 public void forHasStatement(Resource subject,
5035 SyncProcedure<Boolean> procedure) {
5036 forHasStatement(subject, new SyncToAsyncProcedure<Boolean>(procedure));
5040 public void forHasStatement(Resource subject, Procedure<Boolean> procedure) {
5041 forHasStatement(subject, new NoneToAsyncProcedure<Boolean>(procedure));
5045 public void forHasStatement(Resource subject, Resource relation,
5046 AsyncListener<Boolean> listener) {
5047 asyncRequest(new HasStatement(subject, relation), listener);
5051 public void forHasStatement(Resource subject, Resource relation,
5052 SyncListener<Boolean> listener) {
5053 asyncRequest(new HasStatement(subject, relation), listener);
5057 public void forHasStatement(Resource subject, Resource relation,
5058 Listener<Boolean> listener) {
5059 asyncRequest(new HasStatement(subject, relation), listener);
5063 final public void forHasStatement(final Resource subject,
5064 final Resource relation, final AsyncProcedure<Boolean> procedure) {
5066 assert (subject != null);
5067 assert (relation != null);
5068 assert (procedure != null);
5070 processor.forHasStatement(this, subject, relation, procedure);
5075 public void forHasStatement(Resource subject, Resource relation,
5076 SyncProcedure<Boolean> procedure) {
5077 forHasStatement(subject, relation, new SyncToAsyncProcedure<Boolean>(
5082 public void forHasStatement(Resource subject, Resource relation,
5083 Procedure<Boolean> procedure) {
5084 forHasStatement(subject, relation, new NoneToAsyncProcedure<Boolean>(
5089 public void forHasStatement(Resource subject, Resource relation,
5090 Resource object, AsyncListener<Boolean> listener) {
5091 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
5096 public void forHasStatement(Resource subject, Resource relation,
5097 Resource object, SyncListener<Boolean> listener) {
5098 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
5103 public void forHasStatement(Resource subject, Resource relation,
5104 Resource object, Listener<Boolean> listener) {
5105 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
5110 final public void forHasStatement(final Resource subject,
5111 final Resource relation, final Resource object,
5112 final AsyncProcedure<Boolean> procedure) {
5114 assert (subject != null);
5115 assert (relation != null);
5116 assert (object != null);
5117 assert (procedure != null);
5119 processor.forHasStatement(this, subject, relation, object, procedure);
5124 public void forHasStatement(Resource subject, Resource relation,
5125 Resource object, SyncProcedure<Boolean> procedure) {
5126 forHasStatement(subject, relation, object,
5127 new SyncToAsyncProcedure<Boolean>(procedure));
5131 public void forHasStatement(Resource subject, Resource relation,
5132 Resource object, Procedure<Boolean> procedure) {
5133 forHasStatement(subject, relation, object,
5134 new NoneToAsyncProcedure<Boolean>(procedure));
5138 public void forHasValue(Resource subject, AsyncListener<Boolean> listener) {
5139 asyncRequest(new HasValue(subject), listener);
5143 public void forHasValue(Resource subject, SyncListener<Boolean> listener) {
5144 asyncRequest(new HasValue(subject), listener);
5148 public void forHasValue(Resource subject, Listener<Boolean> listener) {
5149 asyncRequest(new HasValue(subject), listener);
5153 final public void forHasValue(final Resource subject,
5154 final AsyncProcedure<Boolean> procedure) {
5156 assert (subject != null);
5157 assert (procedure != null);
5159 processor.forValue(this, subject, new AsyncProcedure<byte[]>() {
5162 public void execute(AsyncReadGraph graph, byte[] result) {
5165 procedure.execute(graph, false);
5167 procedure.execute(graph, true);
5168 } catch (Throwable t) {
5169 Logger.defaultLogError(t);
5174 public void exception(AsyncReadGraph graph, Throwable t) {
5176 procedure.exception(graph, t);
5177 } catch (Throwable t2) {
5178 Logger.defaultLogError(t2);
5183 public String toString() {
5184 return "forHasValue -> " + procedure;
5192 public void forHasValue(Resource subject, SyncProcedure<Boolean> procedure) {
5193 forHasValue(subject, new SyncToAsyncProcedure<Boolean>(procedure));
5197 public void forHasValue(Resource subject, Procedure<Boolean> procedure) {
5198 forHasValue(subject, new NoneToAsyncProcedure<Boolean>(procedure));
5202 public void forOrderedSet(Resource subject,
5203 AsyncMultiListener<Resource> listener) {
5204 asyncRequest(new OrderedSet(subject), listener);
5208 public void forOrderedSet(Resource subject,
5209 SyncMultiListener<Resource> listener) {
5210 asyncRequest(new OrderedSet(subject), listener);
5214 public void forOrderedSet(Resource subject, MultiListener<Resource> listener) {
5215 asyncRequest(new OrderedSet(subject), listener);
5219 final public void forOrderedSet(final Resource subject,
5220 final AsyncMultiProcedure<Resource> procedure) {
5222 assert (subject != null);
5223 assert (procedure != null);
5225 processor.forOrderedSet(this, subject,
5226 new AsyncMultiProcedure<Resource>() {
5229 public void finished(AsyncReadGraph graph) {
5231 procedure.finished(graph);
5232 } catch (Throwable t) {
5233 Logger.defaultLogError(t);
5238 public void execute(AsyncReadGraph graph, Resource result) {
5240 procedure.execute(graph, result);
5241 } catch (Throwable t) {
5242 Logger.defaultLogError(t);
5247 public void exception(AsyncReadGraph graph, Throwable t) {
5249 procedure.exception(graph, t);
5250 } catch (Throwable t2) {
5251 Logger.defaultLogError(t2);
5256 public String toString() {
5257 return "forOrderedSet -> " + procedure;
5265 public void forOrderedSet(Resource subject,
5266 SyncMultiProcedure<Resource> procedure) {
5267 forOrderedSet(subject, new SyncToAsyncMultiProcedure<Resource>(
5272 public void forOrderedSet(Resource subject,
5273 MultiProcedure<Resource> procedure) {
5274 forOrderedSet(subject, new NoneToAsyncMultiProcedure<Resource>(
5279 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5280 AsyncListener<T> listener) {
5281 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5285 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5286 SyncListener<T> listener) {
5287 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5291 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5292 Listener<T> listener) {
5293 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5297 final public <T> void forPossibleAdapted(final Resource resource,
5298 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5300 assert (resource != null);
5301 assert (clazz != null);
5302 assert (procedure != null);
5304 final AdaptionService service = getSession().peekService(AdaptionService.class);
5305 if (service == null)
5306 procedure.exception(this, new ServiceException("No AdaptionService available"));
5308 service.adapt(this, resource, resource, Resource.class, clazz, true, procedure);
5312 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5313 SyncProcedure<T> procedure) {
5314 forPossibleAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5319 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5320 Procedure<T> procedure) {
5321 forPossibleAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5326 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5327 AsyncListener<T> listener) {
5328 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5332 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5333 SyncListener<T> listener) {
5334 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5338 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5339 Listener<T> listener) {
5340 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5344 final public <T> void forPossibleUniqueAdapted(final Resource resource,
5345 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5347 assert (resource != null);
5348 assert (clazz != null);
5349 assert (procedure != null);
5351 final AdaptionService service = getSession().peekService(AdaptionService.class);
5352 if (service == null)
5353 procedure.exception(this, new ServiceException("No AdaptionService available"));
5355 service.adaptNew(this, resource, clazz, true, procedure);
5360 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5361 SyncProcedure<T> procedure) {
5362 forPossibleUniqueAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5367 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5368 Procedure<T> procedure) {
5369 forPossibleUniqueAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5374 * Implementation of the interface AsyncRequestProcessor
5378 final public Session getSession() {
5379 return processor.getSession();
5383 // final public Builtins getBuiltins() {
5384 // return processor.getSession().getBuiltins();
5388 public <T> void asyncRequest(final Read<T> request) {
5390 asyncRequest(request, new AsyncProcedure<T>() {
5393 public void execute(AsyncReadGraph graph, T result) {
5397 public void exception(AsyncReadGraph graph, Throwable t) {
5398 Logger.defaultLogError(t);
5402 public String toString() {
5403 return "asyncRequest(Read) -> " + request;
5411 public <T> void asyncRequest(Read<T> request, AsyncListener<T> procedure) {
5412 asyncRequest(request, (AsyncProcedure<T>) procedure);
5416 public <T> void asyncRequest(Read<T> request,
5417 final SyncListener<T> procedure) {
5418 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5422 public <T> void asyncRequest(Read<T> request, final Listener<T> procedure) {
5423 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5427 public <T> void asyncRequest(final Read<T> request, final AsyncProcedure<T> procedure) {
5429 assert (request != null);
5430 assert (procedure != null);
5432 final ListenerBase listener = getListenerBase(procedure);
5434 if (parent != null || listener != null) {
5437 processor.queryRead(this, request, parent, procedure,
5439 } catch (Throwable e) {
5440 // This throwable has already been transferred to procedure at this point - do nothing about it
5441 //Logger.defaultLogError("Internal error ", e);
5446 // final ReadGraphImpl newGraph = newSync();
5450 T result = request.perform(this);
5453 procedure.execute(this, result);
5454 } catch (Throwable t) {
5455 Logger.defaultLogError(t);
5458 } catch (Throwable t) {
5461 procedure.exception(this, t);
5462 } catch (Throwable t2) {
5463 Logger.defaultLogError(t2);
5474 public static ReadGraphImpl createAsync(QueryProcessor support) {
5475 return new ReadGraphImpl(null, support);
5478 public static ReadGraphImpl forRecompute(CacheEntry entry, QueryProcessor support) {
5479 return new ReadGraphImpl(entry, support);
5483 public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
5484 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5488 public <T> void asyncRequest(Read<T> request, final Procedure<T> procedure) {
5489 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5493 final public <T> void asyncRequest(final AsyncRead<T> request) {
5495 assert (request != null);
5497 asyncRequest(request, new AsyncProcedure<T>() {
5500 public void execute(AsyncReadGraph graph, T result) {
5504 public void exception(AsyncReadGraph graph, Throwable t) {
5505 Logger.defaultLogError(t);
5509 public String toString() {
5510 return "asyncRequest(AsyncRead) -> " + request;
5518 public <T> void asyncRequest(AsyncRead<T> request,
5519 AsyncListener<T> procedure) {
5520 asyncRequest(request, (AsyncProcedure<T>) procedure);
5524 final public <T> void asyncRequest(AsyncRead<T> request,
5525 final SyncListener<T> procedure) {
5526 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5530 final public <T> void asyncRequest(AsyncRead<T> request,
5531 final Listener<T> procedure) {
5532 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5536 final public <T> void asyncRequest(final AsyncRead<T> request,
5537 final AsyncProcedure<T> procedure) {
5539 assert (request != null);
5540 assert (procedure != null);
5542 final ListenerBase listener = getListenerBase(procedure);
5544 if (parent != null || listener != null) {
5546 processor.query(this, request, parent, procedure, listener);
5552 request.perform(this, new CallWrappedSingleQueryProcedure4<T>(procedure, request));
5554 } catch (Throwable t) {
5556 if (t instanceof DatabaseException)
5557 procedure.exception(this, t);
5562 new DatabaseException(
5563 "Unexpected exception in ReadGraph.asyncRequest(SingleAsyncRead, SingleProcedure)",
5573 public <T> void asyncRequest(AsyncRead<T> request,
5574 SyncProcedure<T> procedure) {
5575 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5579 final public <T> void asyncRequest(final AsyncRead<T> request,
5580 final Procedure<T> procedure) {
5581 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5585 public <T> void asyncRequest(final MultiRead<T> request) {
5587 assert (request != null);
5589 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5591 public void exception(AsyncReadGraph graph, Throwable t) {
5592 Logger.defaultLogError(t);
5596 public String toString() {
5597 return "asyncRequest(MultiRead) -> " + request;
5604 public <T> void asyncRequest(MultiRead<T> request,
5605 AsyncMultiListener<T> procedure) {
5606 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5610 public <T> void asyncRequest(MultiRead<T> request,
5611 SyncMultiListener<T> procedure) {
5612 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5616 public <T> void asyncRequest(MultiRead<T> request,
5617 MultiListener<T> procedure) {
5618 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5622 public <T> void asyncRequest(final MultiRead<T> request,
5623 final AsyncMultiProcedure<T> procedure) {
5625 assert (request != null);
5626 assert (procedure != null);
5628 final ListenerBase listener = getListenerBase(procedure);
5630 if (parent != null || listener != null) {
5632 // final ReadGraphImpl newGraph = newSync();
5633 processor.query(this, request, parent, procedure,listener);
5637 // final ReadGraphImpl newGraph = newSync();
5641 request.perform(this, procedure);
5643 } catch (Throwable t) {
5645 procedure.exception(this, t);
5654 public <T> void asyncRequest(MultiRead<T> request,
5655 SyncMultiProcedure<T> procedure) {
5656 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5660 public <T> void asyncRequest(MultiRead<T> request,
5661 MultiProcedure<T> procedure) {
5662 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5666 final public <T> void asyncRequest(final AsyncMultiRead<T> request) {
5668 assert (request != null);
5670 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5672 public void exception(AsyncReadGraph graph, Throwable t) {
5673 Logger.defaultLogError(t);
5677 public String toString() {
5678 return "asyncRequest(AsyncMultiRead) -> " + request;
5685 public <T> void asyncRequest(AsyncMultiRead<T> request,
5686 AsyncMultiListener<T> procedure) {
5687 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5691 public <T> void asyncRequest(AsyncMultiRead<T> request,
5692 SyncMultiListener<T> procedure) {
5693 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5697 public <T> void asyncRequest(AsyncMultiRead<T> request,
5698 MultiListener<T> procedure) {
5699 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5703 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5704 final AsyncMultiProcedure<T> procedure) {
5706 assert (request != null);
5707 assert (procedure != null);
5709 ListenerBase listener = getListenerBase(procedure);
5711 if (parent != null || listener != null) {
5713 processor.query(this, request, parent, procedure, listener);
5719 request.perform(this, new AsyncMultiProcedure<T>() {
5722 public void execute(AsyncReadGraph graph, T result) {
5723 procedure.execute(graph, result);
5727 public void finished(AsyncReadGraph graph) {
5728 procedure.finished(graph);
5732 public void exception(AsyncReadGraph graph, Throwable t) {
5733 procedure.exception(graph, t);
5737 public String toString() {
5738 return "asyncRequest(AsyncMultiRead) -> " + procedure;
5743 } catch (Throwable t) {
5745 procedure.exception(this, new DatabaseException(t));
5753 public <T> void asyncRequest(AsyncMultiRead<T> request,
5754 SyncMultiProcedure<T> procedure) {
5755 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5759 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5760 final MultiProcedure<T> procedure) {
5761 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5765 final public <T> void asyncRequest(final ExternalRead<T> request) {
5767 assert (request != null);
5769 asyncRequest(request, new Procedure<T>() {
5772 public void execute(T result) {
5776 public void exception(Throwable t) {
5777 Logger.defaultLogError(t);
5781 public String toString() {
5782 return "asyncRequest(PrimitiveRead) -> " + request;
5790 public <T> void asyncRequest(ExternalRead<T> request,
5791 final Listener<T> procedure) {
5792 asyncRequest(request, (Procedure<T>) procedure);
5796 final public <T> void asyncRequest(final ExternalRead<T> request,
5797 final Procedure<T> procedure) {
5799 assert (request != null);
5800 assert (procedure != null);
5802 ListenerBase listener = getListenerBase(procedure);
5804 if (parent != null || listener != null) {
5806 processor.query(this, request, parent, procedure, listener);
5812 request.register(this, new Listener<T>() {
5815 public void execute(T result) {
5816 procedure.execute(result);
5820 public void exception(Throwable t) {
5821 procedure.exception(t);
5825 public String toString() {
5826 return "asyncRequest(PrimitiveRead) -> " + request;
5830 public boolean isDisposed() {
5836 } catch (Throwable t) {
5838 if (t instanceof DatabaseException)
5839 procedure.exception(t);
5842 .exception(new DatabaseException(
5843 "Unexpected exception in ReadGraph.asyncRequest(SingleAsyncRead, SingleProcedure)",
5853 public void asyncRequest(final Write request) {
5855 assert (request != null);
5857 getSession().asyncRequest(request);
5859 // processor.asyncWrite(request);
5864 public <T> void asyncRequest(final WriteResult<T> request, Procedure<T> procedure) {
5865 throw new Error("Not implemented.");
5869 public void asyncRequest(Write request, Consumer<DatabaseException> callback) {
5871 assert (request != null);
5873 getSession().asyncRequest(request, callback);
5878 public void asyncRequest(final DelayedWrite request) {
5880 assert (request != null);
5882 getSession().asyncRequest(request);
5887 public <T> void asyncRequest(final DelayedWriteResult<T> request, Procedure<T> procedure) {
5888 throw new Error("Not implemented.");
5892 public void asyncRequest(DelayedWrite r,
5893 Consumer<DatabaseException> callback) {
5894 throw new Error("Not implemented.");
5898 public void asyncRequest(final WriteOnly request) {
5900 assert (request != null);
5902 getSession().asyncRequest(request);
5907 public <T> void asyncRequest(final WriteOnlyResult<T> request, Procedure<T> procedure) {
5908 throw new Error("Not implemented.");
5912 public void asyncRequest(WriteOnly r, Consumer<DatabaseException> callback) {
5913 throw new Error("Not implemented.");
5917 * Implementation of the interface ServiceLocator
5921 public <T> T getService(Class<T> api) {
5922 if(WriteSupport.class == api) {
5923 if(this instanceof WriteGraphImpl) {
5924 WriteGraphImpl impl = (WriteGraphImpl)this;
5925 return (T)impl.writeSupport;
5928 return getSession().getService(api);
5932 public <T> T peekService(Class<T> api) {
5933 return getSession().peekService(api);
5937 public boolean hasService(Class<?> api) {
5938 return getSession().hasService(api);
5942 public <T> void registerService(Class<T> api, T service) {
5943 getSession().registerService(api, service);
5947 public boolean isImmutable(Resource resource) throws DatabaseException {
5948 ResourceImpl impl = (ResourceImpl)resource;
5949 return processor.isImmutable(impl.id);
5956 protected static String INTERNAL_ERROR_STRING = "Transaction aborted due to internal client error. Contact application support.";
5959 * callerThread is the currently running thread state.syncThread is blocking for
5960 * this execution state.syncParent is the blocking request
5963 final private boolean isExternal(int thread) {
5964 return thread == Integer.MIN_VALUE;
5967 // final private boolean isSync(int thread) {
5968 // return thread < -1 && thread > Integer.MIN_VALUE;
5971 ReadGraphImpl(ReadGraphImpl graph) {
5972 this(graph.parent, graph.processor);
5975 ReadGraphImpl(CacheEntry parent, QueryProcessor support) {
5976 // this.state = new ReadGraphState(barrier, support);
5977 this.parent = parent;
5978 this.processor = support;
5981 ReadGraphImpl(final QueryProcessor support) {
5983 // this.state = state;
5984 this.processor = support;
5989 // public static ReadGraphImpl createSync(int syncThread, Object syncParent,
5990 // ReadGraphSupportImpl support) {
5991 // return new ReadGraphImpl(syncThread, syncThread, syncParent, null,
5992 // support, new AsyncBarrierImpl(null));
5995 public static ReadGraphImpl create(QueryProcessor support) {
5996 return new ReadGraphImpl(support);
5999 // public ReadGraphImpl newAsync() {
6001 //// if(!state.synchronizedExecution) {
6004 //// return new ReadGraphImpl(false, parent, state.support, state.barrier);
6008 // public ReadGraphImpl newSync() {
6009 // return new ReadGraphImpl(parent, processor);
6012 public ReadGraphImpl newSync(CacheEntry parentEntry) {
6013 return new ReadGraphImpl(parentEntry, processor);
6016 public ReadGraphImpl newRestart(ReadGraphImpl impl) {
6018 WriteGraphImpl write = processor.getSession().getService(
6019 WriteGraphImpl.class);
6021 // if (write.callerThread != impl.callerThread)
6022 // return new WriteGraphImpl(impl.callerThread, parent, state.support, write.writeSupport, write.provider, write.state.barrier);
6027 // public ReadGraphImpl newSync(Object parentRequest) {
6028 // return new ReadGraphImpl(callerThread, state.parent, state.support, new AsyncBarrierImpl(state.barrier));
6031 // public ReadGraphImpl newSync(final int callerThread, Object parentRequest) {
6032 // assert (state.syncThread == callerThread || (state.syncThread == Integer.MIN_VALUE && callerThread != Integer.MIN_VALUE));
6033 // return new ReadGraphImpl(callerThread, callerThread, parentRequest,
6034 // state.parent, state.support, new AsyncBarrierImpl(state.barrier));
6037 // public ReadGraphImpl newSyncAsync(Object parentRequest) {
6038 //// assert (callerThread < 0);
6039 // return new ReadGraphImpl(callerThread, state.syncThread, parentRequest,
6040 // state.parent, state.support, new AsyncBarrierImpl(state.barrier));
6043 // public ReadGraphImpl newSyncAsync(final int callerThread,
6044 // Object parentRequest) {
6045 //// assert (callerThread < 0);
6046 // // assert(state.syncThread == callerThread || (state.syncThread == Integer.MIN_VALUE
6047 // // && callerThread != Integer.MIN_VALUE) );
6048 // return new ReadGraphImpl(callerThread, callerThread, parentRequest,
6049 // state.parent, state.support, new AsyncBarrierImpl(state.barrier));
6052 public ReadGraphImpl withAsyncParent(CacheEntry parent) {
6053 return new ReadGraphImpl(parent, processor);
6056 public ReadGraphImpl withParent(CacheEntry parent) {
6057 if(parent == this.parent) return this;
6058 else return new ReadGraphImpl(parent, processor);
6061 final private ListenerBase getListenerBase(final Object procedure) {
6062 if (procedure instanceof ListenerBase)
6063 return (ListenerBase) procedure;
6068 public <T> void waitAsyncProcedure(AsyncMultiReadProcedure<T> procedure) {
6070 assert(procedure.done());
6072 // while (!procedure.done()) {
6074 // boolean executed = processor.resumeTasks(callerThread, null, null);
6078 // // sema.tryAcquire(1, TimeUnit.MILLISECONDS);
6079 // } catch (InterruptedException e) {
6080 // e.printStackTrace();
6088 public <T> void waitAsyncProcedure(AsyncReadProcedure<T> procedure) {
6090 assert(procedure.done());
6092 // while (!procedure.done()) {
6094 // boolean executed = processor.processor.resume(this);
6098 // // sema.tryAcquire(1, TimeUnit.MILLISECONDS);
6099 // } catch (InterruptedException e) {
6100 // e.printStackTrace();
6108 // public void waitAsync(Object request) {
6110 // state.barrier.waitBarrier(request, this);
6111 // } catch (Throwable t) {
6112 // t.printStackTrace();
6113 // processor.scanPending();
6114 // processor.querySupport.checkTasks();
6115 // throw new RuntimeDatabaseException(t);
6119 // public void restart() {
6120 // state.barrier.restart();
6123 public boolean resumeTasks() {
6124 return processor.resumeTasks(this);
6127 Class<?> singleClass(Set<Resource> types) {
6128 Class<?> result = null;
6129 for (Resource type : types) {
6130 Class<?> clazz = processor.getBuiltinValue(type);
6131 if (clazz != null) {
6141 private String debugString(Resource r) {
6144 name = getPossibleRelatedValue(r, processor.getL0(this).HasName);
6145 } catch (ManyObjectsForFunctionalRelationException e) {
6146 Logger.defaultLogError(e);
6147 } catch (ServiceException e) {
6148 Logger.defaultLogError(e);
6150 return "[" + name + " - " + r + "]";
6154 public String toString() {
6155 return "ReadGraphImpl[thread=" + Thread.currentThread() + "]";
6159 final public int thread() {
6163 static class MultiTripleIntProcedure implements TripleIntProcedure {
6165 final private AsyncMultiProcedure<Statement> procedure;
6166 final private ReadGraphImpl impl;
6167 final private QuerySupport support;
6169 public MultiTripleIntProcedure(AsyncMultiProcedure<Statement> procedure, ReadGraphImpl impl, QuerySupport support) {
6170 this.procedure = procedure;
6172 this.support = support;
6176 public void execute(ReadGraphImpl graph, int s, int p, int o) {
6178 procedure.execute(graph, support.getStatement(s, p, o));
6179 } catch (Throwable t2) {
6180 Logger.defaultLogError(t2);
6185 public void finished(ReadGraphImpl graph) {
6187 procedure.finished(graph);
6188 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
6189 } catch (Throwable t2) {
6190 Logger.defaultLogError(t2);
6195 public void exception(ReadGraphImpl graph, Throwable t) {
6197 procedure.exception(graph, t);
6198 } catch (Throwable t2) {
6199 Logger.defaultLogError(t2);
6201 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
6205 public String toString() {
6206 return "forEachObject with " + procedure;
6211 // private AsyncMultiProcedure<Resource> cacheKey = null;
6212 // private MultiIntProcedure cacheResult = null;
6214 // final IntProcedure forMultiProcedure(final AsyncMultiProcedure<Resource> procedure) {
6216 // if(procedure == cacheKey) return cacheResult;
6218 // cacheResult = new MultiIntProcedure(procedure, this, processor.support);
6219 // cacheKey = procedure;
6221 // return cacheResult;
6225 // private AsyncMultiProcedure<Statement> cacheKey2 = null;
6226 // private MultiTripleIntProcedure cacheResult2 = null;
6228 // final synchronized TripleIntProcedure forMultiProcedure(final AsyncMultiProcedure<Statement> procedure) {
6230 // if(procedure == cacheKey2) return cacheResult2;
6232 // cacheResult2 = new MultiTripleIntProcedure(procedure, this, processor.support);
6233 // cacheKey2 = procedure;
6235 // return cacheResult2;
6240 public Datatype getDataType(Resource subject) throws DatabaseException {
6241 for(Resource dataTypeResource : getObjects(subject, processor.getL0(this).HasDataType))
6242 return getValue(dataTypeResource, Bindings.getBindingUnchecked(Datatype.class));
6243 throw new DoesNotContainValueException("The literal has no data type.");
6246 protected <T extends Accessor> T getAccessor4File(Resource subject)
6247 throws DatabaseException {
6250 byte[] bytes = processor.support.getValue(g, subject);
6254 BinaryVariant va = (BinaryVariant) Accessors.getAccessor(bytes, Datatypes.VARIANT);
6255 Accessor ca = va.getContentAccessor();
6257 } catch (AccessorConstructionException e) {
6258 throw new DatabaseException(e);
6264 Binding datatype_binding = Bindings.getBindingUnchecked(DataType.class);
6265 Serializer datatype_serializer = datatype_binding.serializer();
6268 BinaryMemory in = new BinaryMemory(ByteBuffer.wrap(bytes));
6269 datatype = (DataType)datatype_serializer.deserialize(in);
6270 Binding data_binding = Bindings.getBinding(datatype);
6271 Serializer data_serializer = data_binding.serializer(BinarySerializationFormat.INSTANCE);
6272 Object o = data_serializer.deserialize(in);
6274 return (T)Accessors.getAccessor(data_binding, o);
6275 } catch(AccessorConstructionException e) {
6278 } catch (Exception e) {
6279 throw new DatabaseException(e);
6282 @SuppressWarnings("unchecked")
6284 public <T extends Accessor> T getAccessor(Resource subject) throws DatabaseException {
6285 RandomAccessBinary rab = getRandomAccessBinary(subject);
6287 return (T)Accessors.getAccessor(rab, getDataType(subject));
6288 } catch(AccessorConstructionException e) {
6289 throw new DatabaseException(e);
6292 @SuppressWarnings("unchecked")
6293 protected <T extends Accessor> T createAccessor(Resource resource, Datatype datatype, Object intialValue)
6294 throws DatabaseException {
6295 RandomAccessBinary rab = createRandomAccessBinary(resource, datatype, intialValue);
6297 return (T)Accessors.getAccessor(rab, datatype);
6298 } catch(AccessorConstructionException e) {
6299 throw new DatabaseException(e);
6303 public RandomAccessBinary getRandomAccessBinary(Resource subject) throws DatabaseException {
6304 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
6305 ResourceData rd = ravs.get(subject);
6309 ExternalValueSupport evs = getService(ExternalValueSupport.class);
6310 long size = evs.getValueSize(this, subject); // Throws DatabaseException if no old external value.
6312 File platform = Platform.getLocation().toFile();
6313 File tempFiles = new File(platform, "tempFiles");
6314 File dbDir = new File(tempFiles, "db");
6316 File file = new File(dbDir, "ResourceFile" + subject.getResourceId());
6317 rd = new ResourceData(new BinaryFile(file), true); // Old external value.
6318 final int N = 1<<20;
6322 int length = N < left ? N : (int)left;
6323 byte[] bytes = evs.readValue(this, subject, offset, length);
6324 offset += bytes.length;
6325 left -= bytes.length;
6326 rd.binaryFile.write(bytes);
6328 ravs.put(subject, rd);
6330 } catch (Exception e) {
6331 throw new DatabaseException("Resource " + subject + " have value but there was problem with accessing value data.", e);
6333 } catch (Exception e) {
6334 if(Development.DEVELOPMENT) {
6335 if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
6336 e.printStackTrace();
6340 Datatype datatype = getDataType(subject);
6341 Object value = getPossibleValue(subject, Bindings.getBinding(datatype));
6342 return createRandomAccessBinary(subject, datatype, value);
6344 public RandomAccessBinary createRandomAccessBinary(Resource resource, Datatype datatype, Object initialValue)
6345 throws DatabaseException {
6346 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
6348 File platform = Platform.getLocation().toFile();
6349 File tempFiles = new File(platform, "tempFiles");
6350 File dbDir = new File(tempFiles, "db");
6352 File file = new File(dbDir, "ResourceFile" + resource.getResourceId());
6353 ResourceData rd = new ResourceData(new BinaryFile(file), false);
6354 Binding binding = Bindings.getBinding(datatype);
6355 if (null == initialValue) {
6356 initialValue = binding.createDefault();
6358 Serializer serializer = binding.serializer();
6359 byte[] bytes = serializer.serialize(initialValue);
6360 rd.binaryFile.write(bytes);
6361 ravs.put(resource, rd);
6363 } catch (Exception e) {
6364 if (e instanceof DatabaseException)
6365 throw (DatabaseException)e;
6367 throw new DatabaseException(e);
6371 // static class ExternalValueRequest<T> extends ResourceRead<T> {
6373 // public ExternalValueRequest(Resource resource) {
6377 // @SuppressWarnings("unchecked")
6379 // public T perform(ReadGraph graph) throws DatabaseException {
6382 // String uri = graph.getURI(resource);
6383 // if(Layer0.URIs.Functions_functionApplication.equals(uri)) return (T)functionApplication;
6385 // return (T)ReflectionUtils.getValue(uri).getValue();
6387 // } catch(ValueNotFoundException e) {
6388 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
6389 // } catch(ClassCastException e) {
6390 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
6396 @SuppressWarnings("unchecked")
6398 public <T> T getValue2(Resource r, Object context) throws DatabaseException {
6399 Layer0 L0 = processor.getL0(this);
6400 Set<Resource> types = getTypes(r);
6402 if(types.contains(L0.Literal)) {
6403 if(isImmutable(r)) {
6404 return syncRequest(new ValueImplied<T>(r));
6409 else if(types.contains(L0.ExternalValue)) {
6410 return (T)syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6414 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6415 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6417 return function.apply(this, r, context);
6418 } catch(RuntimeException e) {
6419 DatabaseException dte = findPossibleRootException(e);
6420 if(dte != null) throw dte;
6421 else throw new DatabaseException(e);
6428 public Variant getVariantValue2(Resource r, Object context) throws DatabaseException {
6429 Layer0 L0 = processor.getL0(this);
6430 Set<Resource> types = getTypes(r);
6432 if(types.contains(L0.Literal)) {
6433 if(isImmutable(r)) {
6434 return syncRequest(new VariantValueImplied(r));
6436 return getVariantValue(r);
6439 else if(types.contains(L0.ExternalValue)) {
6440 Object value = syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6442 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6443 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6444 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6449 Function3<ReadGraph,Resource,Object,Object> function = requestValueFunction(r);
6450 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6452 Object value = function.apply(this, r, context);
6454 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6455 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6456 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6458 } catch(RuntimeException e) {
6459 DatabaseException dte = findPossibleRootException(e);
6460 if(dte != null) throw dte;
6461 else throw new DatabaseException(e);
6467 public <T> T getPossibleValue2(Resource subject, Object context) throws DatabaseException {
6469 return getValue2(subject, context);
6470 } catch (DatabaseException e) {
6475 static class PossibleConverterFunction<T> extends ResourceRead<Function3<ReadGraph,Resource,Object,T>> {
6477 public PossibleConverterFunction(Resource resource) {
6482 public Function3<ReadGraph,Resource,Object,T> perform(ReadGraph graph) throws DatabaseException {
6483 return compute(graph, resource);
6486 @SuppressWarnings("unchecked")
6487 public static <T> Function3<ReadGraph,Resource,Object,T> compute(ReadGraph graph, Resource resource) throws DatabaseException {
6488 Layer0 L0 = Layer0.getInstance(graph);
6489 for(Resource converter : graph.getObjects(resource, L0.ConvertsToValueWith)) {
6491 if(L0.Functions_functionApplication.equals(converter)) {
6492 return (Function3<ReadGraph,Resource,Object,T>)graph.syncRequest(new AdaptValue(resource));
6494 return graph.getValue2(converter, resource);
6496 } catch(RuntimeException e) {
6497 DatabaseException dte = findPossibleRootException(e);
6498 if(dte != null) throw dte;
6499 else throw new DatabaseException(e);
6507 <T> Function3<ReadGraph,Resource,Object,T> requestValueFunction(Resource r) throws DatabaseException {
6509 return syncRequest(new PossibleConverterFunction<T>(r), TransientCacheAsyncListener.<Function3<ReadGraph,Resource,Object,T>>instance());
6511 return syncRequest(new PossibleConverterFunction<T>(r));
6515 * Get a value associated with a graph {@link Resource}, using a possible context object and
6516 * a desired value binding. The following methods are tried in order to retreive the value:
6518 * <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>
6519 * <li>If the resource is a {@code L0.ExternalValue}, the value is acquired using
6520 * {@link ReflectionUtils#getValue(String)}.</li>
6521 * <li>If the resource is associated with a suitable value converter with relation {@code L0.ConvertsToValueWith}
6522 * (see {@link #requestValueFunction(Resource)}), the value function is called with the graph, the resource
6523 * and the context object.</li>
6526 * @param r A graph resource with which the value is associated
6527 * @param context A context object that is used for acquiring the value (only applied in case 3)
6528 * @param binding A binding for the value type (only applied in case 1)
6529 * @return The value of the graph node.
6530 * @throws DoesNotContainValueException No value is associated with the graph node.
6531 * @throws DatabaseException Other errors, such as an error in casting the value to the return type or
6532 * a runtime error in the value function.
6534 @SuppressWarnings("unchecked")
6536 public <T> T getValue2(Resource r, Object context, Binding binding) throws DatabaseException {
6537 if (binding instanceof ObjectVariantBinding)
6538 return getValue2(r, context);
6540 Layer0 L0 = processor.getL0(this);
6541 Set<Resource> types = getTypes(r);
6542 if(types.contains(L0.Literal)) {
6543 if(isImmutable(r)) {
6544 return syncRequest(new Value<T>(r, binding));
6546 return getValue(r, binding);
6548 } else if(types.contains(L0.ExternalValue)) {
6550 return (T)ReflectionUtils.getValue(getURI(r)).getValue();
6551 } catch(ValueNotFoundException e) {
6552 throw new DatabaseException(e);
6553 } catch(ClassCastException e) {
6554 throw new DatabaseException(e);
6558 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6559 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function.");
6561 Object value = function.apply(this, r, context);
6562 if(binding.isInstance(value)) return (T)value;
6563 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
6564 return (T)Bindings.adapt(value, srcBinding, binding);
6565 } catch(RuntimeException e) {
6566 DatabaseException dte = findPossibleRootException(e);
6567 if(dte != null) throw dte;
6568 else throw new DatabaseException(e);
6569 } catch (AdaptException e) {
6570 throw new DatabaseException(e);
6571 } catch (org.simantics.databoard.binding.error.BindingException e) {
6572 throw new DatabaseException(e);
6578 public <T> T getPossibleValue2(Resource subject, Object context, Binding binding) throws DatabaseException {
6580 return getValue2(subject, context, binding);
6581 } catch (DatabaseException e) {
6586 private static DatabaseException findPossibleRootException(Throwable t) {
6587 if(t == null) return null;
6588 if(t instanceof DatabaseException) return (DatabaseException)t;
6589 if(t instanceof RuntimeException || t instanceof InvocationTargetException) {
6590 return findPossibleRootException(t.getCause());
6596 public <T> T getRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6597 return getRelatedValue2(subject, relation, subject);
6601 public Variant getRelatedVariantValue2(Resource subject, Resource relation) throws DatabaseException {
6602 return getRelatedVariantValue2(subject, relation, subject);
6606 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6608 Resource object = getPossibleObject(subject, relation);
6609 if(object == null) return null;
6610 else return getValue2(object, subject);
6611 } catch (DatabaseException e) {
6617 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6618 if(Development.DEVELOPMENT) {
6619 String error = L0Validations.checkValueType(this, subject, relation);
6621 Logger.defaultLogError(new ValidationException(error));
6622 //throw new ValidationException(error);
6623 new ValidationException(error).printStackTrace();
6626 return getValue2(getSingleObject(subject, relation), context);
6630 public Variant getRelatedVariantValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6631 if(Development.DEVELOPMENT) {
6632 String error = L0Validations.checkValueType(this, subject, relation);
6634 Logger.defaultLogError(new ValidationException(error));
6635 //throw new ValidationException(error);
6636 new ValidationException(error).printStackTrace();
6639 return getVariantValue2(getSingleObject(subject, relation), context);
6643 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6645 Resource object = getPossibleObject(subject, relation);
6646 if(object == null) return null;
6647 else return getValue2(object, context);
6648 } catch (DatabaseException e) {
6654 public <T> T getRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6655 return getRelatedValue2(subject, relation, subject, binding);
6659 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6661 Resource object = getPossibleObject(subject, relation);
6662 if(object == null) return null;
6663 return getValue2(object, subject, binding);
6664 } catch (DatabaseException e) {
6670 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6671 return getValue2(getSingleObject(subject, relation), context, binding);
6675 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6677 Resource object = getPossibleObject(subject, relation);
6678 if(object == null) return null;
6679 else return getValue2(object, context, binding);
6680 } catch (DatabaseException e) {
6686 public Type getRelatedValueType(Resource subject, Resource relation) throws DatabaseException {
6687 Layer0 L0 = processor.getL0(this);
6688 Resource property = getSingleObject(subject, relation);
6689 String typeText = (String)getRelatedValue(property, L0.HasValueType, Bindings.STRING);
6691 return org.simantics.scl.compiler.types.Types.parseType(typeText);
6692 } catch (SCLTypeParseException e) {
6693 throw new DatabaseException(e);
6698 public boolean setSynchronous(boolean value) {
6699 boolean old = processor.synch;
6700 processor.synch = value;
6705 public boolean getSynchronous() {
6706 return processor.synch;
6709 public void ensureLoaded(int resource) {
6710 processor.querySupport.ensureLoaded(this, resource);
6713 public void ensureLoaded(int resource, int predicate) {
6714 processor.querySupport.ensureLoaded(this, resource, predicate);
6717 public byte[] getValue(int resource) {
6718 return processor.querySupport.getValue(this, resource);
6721 public int thread(int resource) {
6722 return (resource >>> 16) & processor.THREAD_MASK;
6725 public int thread(Resource resource) {
6726 return (((ResourceImpl)resource).id >>> 16) & processor.THREAD_MASK;
6729 public ResourceSupport getResourceSupport() {
6730 return processor.getResourceSupport();
6734 public Object getModificationCounter() {
6735 return processor.getSession().getModificationCounter();