1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl.graph;
14 import java.io.BufferedOutputStream;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.io.PrintStream;
19 import java.lang.reflect.Array;
20 import java.lang.reflect.InvocationTargetException;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.HashSet;
25 import java.util.IdentityHashMap;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.ListIterator;
31 import java.util.function.Consumer;
33 import org.eclipse.core.runtime.Platform;
34 import org.simantics.databoard.Accessors;
35 import org.simantics.databoard.Bindings;
36 import org.simantics.databoard.accessor.Accessor;
37 import org.simantics.databoard.accessor.error.AccessorConstructionException;
38 import org.simantics.databoard.adapter.AdaptException;
39 import org.simantics.databoard.binding.Binding;
40 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;
41 import org.simantics.databoard.binding.impl.ObjectVariantBinding;
42 import org.simantics.databoard.binding.mutable.Variant;
43 import org.simantics.databoard.serialization.Serializer;
44 import org.simantics.databoard.type.Datatype;
45 import org.simantics.databoard.util.binary.BinaryFile;
46 import org.simantics.databoard.util.binary.RandomAccessBinary;
47 import org.simantics.db.AsyncReadGraph;
48 import org.simantics.db.ComputationalValue;
49 import org.simantics.db.DevelopmentKeys;
50 import org.simantics.db.ExternalValueSupport;
51 import org.simantics.db.ReadGraph;
52 import org.simantics.db.RelationContext;
53 import org.simantics.db.Resource;
54 import org.simantics.db.Session;
55 import org.simantics.db.Statement;
56 import org.simantics.db.adaption.AdaptionService;
57 import org.simantics.db.common.primitiverequest.Adapter;
58 import org.simantics.db.common.primitiverequest.Builtin;
59 import org.simantics.db.common.primitiverequest.DatatypeBinding;
60 import org.simantics.db.common.primitiverequest.ForEachAssertedObject;
61 import org.simantics.db.common.primitiverequest.ForEachAssertedStatement;
62 import org.simantics.db.common.primitiverequest.HasStatement;
63 import org.simantics.db.common.primitiverequest.HasStatementSubject;
64 import org.simantics.db.common.primitiverequest.HasStatementSubjectObject;
65 import org.simantics.db.common.primitiverequest.HasValue;
66 import org.simantics.db.common.primitiverequest.Inverse;
67 import org.simantics.db.common.primitiverequest.IsInheritedFrom;
68 import org.simantics.db.common.primitiverequest.IsInstanceOf;
69 import org.simantics.db.common.primitiverequest.IsSubrelationOf;
70 import org.simantics.db.common.primitiverequest.OrderedSet;
71 import org.simantics.db.common.primitiverequest.PossibleAdapter;
72 import org.simantics.db.common.primitiverequest.PossibleInverse;
73 import org.simantics.db.common.primitiverequest.PossibleObject;
74 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;
75 import org.simantics.db.common.primitiverequest.PossibleRelatedValueImplied;
76 import org.simantics.db.common.primitiverequest.PossibleStatement;
77 import org.simantics.db.common.primitiverequest.PossibleType;
78 import org.simantics.db.common.primitiverequest.PossibleUniqueAdapter;
79 import org.simantics.db.common.primitiverequest.PossibleValue;
80 import org.simantics.db.common.primitiverequest.PossibleValueImplied;
81 import org.simantics.db.common.primitiverequest.RelatedValue;
82 import org.simantics.db.common.primitiverequest.RelatedValueImplied;
83 import org.simantics.db.common.primitiverequest.SingleObject;
84 import org.simantics.db.common.primitiverequest.SingleStatement;
85 import org.simantics.db.common.primitiverequest.SingleType;
86 import org.simantics.db.common.primitiverequest.SingleTypeAny;
87 import org.simantics.db.common.primitiverequest.Types;
88 import org.simantics.db.common.primitiverequest.UniqueAdapter;
89 import org.simantics.db.common.primitiverequest.Value;
90 import org.simantics.db.common.primitiverequest.ValueImplied;
91 import org.simantics.db.common.primitiverequest.VariantValueImplied;
92 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
93 import org.simantics.db.common.procedure.adapter.AsyncProcedureAdapter;
94 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
95 import org.simantics.db.common.procedure.adapter.SyncMultiProcedureAdapter;
96 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
97 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
98 import org.simantics.db.common.procedure.single.SyncReadProcedure;
99 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrErrorProcedure;
100 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrNullProcedure;
101 import org.simantics.db.common.procedure.single.wrapper.ExceptionToNullProcedure;
102 import org.simantics.db.common.procedure.single.wrapper.NullSingleOrNullProcedure;
103 import org.simantics.db.common.procedure.single.wrapper.SingleFunctionalOrNullProcedure;
104 import org.simantics.db.common.procedure.single.wrapper.SingleOrErrorProcedure;
105 import org.simantics.db.common.procedure.single.wrapper.SingleOrNullProcedure;
106 import org.simantics.db.common.procedure.wrapper.NoneToAsyncListener;
107 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiListener;
108 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiProcedure;
109 import org.simantics.db.common.procedure.wrapper.NoneToAsyncProcedure;
110 import org.simantics.db.common.procedure.wrapper.NoneToAsyncSetProcedure;
111 import org.simantics.db.common.procedure.wrapper.NoneToSyncMultiListener;
112 import org.simantics.db.common.procedure.wrapper.NoneToSyncMultiProcedure;
113 import org.simantics.db.common.procedure.wrapper.SyncToAsyncListener;
114 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiListener;
115 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiProcedure;
116 import org.simantics.db.common.procedure.wrapper.SyncToAsyncProcedure;
117 import org.simantics.db.common.procedure.wrapper.SyncToAsyncSetProcedure;
118 import org.simantics.db.common.request.AdaptValue;
119 import org.simantics.db.common.request.ResourceRead;
120 import org.simantics.db.common.utils.Logger;
121 import org.simantics.db.common.utils.NameUtils;
122 import org.simantics.db.common.validation.L0Validations;
123 import org.simantics.db.exception.AdaptionException;
124 import org.simantics.db.exception.ArgumentException;
125 import org.simantics.db.exception.AssumptionException;
126 import org.simantics.db.exception.BindingException;
127 import org.simantics.db.exception.DatabaseException;
128 import org.simantics.db.exception.DoesNotContainValueException;
129 import org.simantics.db.exception.EmptyResourceException;
130 import org.simantics.db.exception.InternalException;
131 import org.simantics.db.exception.InvalidLiteralException;
132 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
133 import org.simantics.db.exception.NoInverseException;
134 import org.simantics.db.exception.NoSingleResultException;
135 import org.simantics.db.exception.ResourceNotFoundException;
136 import org.simantics.db.exception.ServiceException;
137 import org.simantics.db.exception.ValidationException;
138 import org.simantics.db.impl.RelationContextImpl;
139 import org.simantics.db.impl.ResourceImpl;
140 import org.simantics.db.impl.internal.RandomAccessValueSupport;
141 import org.simantics.db.impl.internal.ResourceData;
142 import org.simantics.db.impl.procedure.ResultCallWrappedSyncQueryProcedure;
143 import org.simantics.db.impl.query.CacheEntry;
144 import org.simantics.db.impl.query.QueryCache;
145 import org.simantics.db.impl.query.QueryCacheBase;
146 import org.simantics.db.impl.query.QueryProcessor;
147 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
148 import org.simantics.db.impl.query.QuerySupport;
149 import org.simantics.db.impl.query.TripleIntProcedure;
150 import org.simantics.db.impl.support.ResourceSupport;
151 import org.simantics.db.procedure.AsyncListener;
152 import org.simantics.db.procedure.AsyncMultiListener;
153 import org.simantics.db.procedure.AsyncMultiProcedure;
154 import org.simantics.db.procedure.AsyncProcedure;
155 import org.simantics.db.procedure.AsyncSetListener;
156 import org.simantics.db.procedure.Listener;
157 import org.simantics.db.procedure.ListenerBase;
158 import org.simantics.db.procedure.MultiListener;
159 import org.simantics.db.procedure.MultiProcedure;
160 import org.simantics.db.procedure.Procedure;
161 import org.simantics.db.procedure.SetListener;
162 import org.simantics.db.procedure.StatementProcedure;
163 import org.simantics.db.procedure.SyncListener;
164 import org.simantics.db.procedure.SyncMultiListener;
165 import org.simantics.db.procedure.SyncMultiProcedure;
166 import org.simantics.db.procedure.SyncProcedure;
167 import org.simantics.db.procedure.SyncSetListener;
168 import org.simantics.db.request.AsyncMultiRead;
169 import org.simantics.db.request.AsyncRead;
170 import org.simantics.db.request.DelayedWrite;
171 import org.simantics.db.request.DelayedWriteResult;
172 import org.simantics.db.request.ExternalRead;
173 import org.simantics.db.request.MultiRead;
174 import org.simantics.db.request.Read;
175 import org.simantics.db.request.ReadInterface;
176 import org.simantics.db.request.Write;
177 import org.simantics.db.request.WriteInterface;
178 import org.simantics.db.request.WriteOnly;
179 import org.simantics.db.request.WriteOnlyResult;
180 import org.simantics.db.request.WriteResult;
181 import org.simantics.layer0.Layer0;
182 import org.simantics.scl.compiler.types.Type;
183 import org.simantics.scl.compiler.types.exceptions.SCLTypeParseException;
184 import org.simantics.scl.reflection.ReflectionUtils;
185 import org.simantics.scl.reflection.ValueNotFoundException;
186 import org.simantics.scl.runtime.function.Function3;
187 import org.simantics.utils.DataContainer;
188 import org.simantics.utils.Development;
189 import org.simantics.utils.datastructures.Pair;
190 import org.simantics.utils.datastructures.collections.CollectionUtils;
191 import org.simantics.utils.threads.logger.ITask;
192 import org.simantics.utils.threads.logger.ThreadLogger;
193 import org.slf4j.LoggerFactory;
195 import gnu.trove.map.hash.TObjectIntHashMap;
197 public class ReadGraphImpl implements AsyncReadGraph {
199 private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ReadGraphImpl.class);
201 final static boolean EMPTY_RESOURCE_CHECK = false;
203 final public CacheEntry parent;
204 public final ReadGraphImpl parentGraph;
205 final public QueryProcessor processor;
207 public final AsyncBarrierImpl asyncBarrier;
209 final static Binding DATA_TYPE_BINDING_INTERNAL = Bindings.getBindingUnchecked(Datatype.class);
210 final static Serializer DATA_TYPE_SERIALIZER = Bindings.getSerializerUnchecked(DATA_TYPE_BINDING_INTERNAL);
212 final public static TObjectIntHashMap<String> counters = new TObjectIntHashMap<String>();
214 public static void resetCounters() {
218 public static String listCounters(File file) throws IOException {
220 PrintStream b = new PrintStream(new BufferedOutputStream(new FileOutputStream(file)));
222 for(Pair<String,Integer> p : CollectionUtils.valueSortedEntries(counters)) {
223 b.print(-p.second + " " + p.first + "\n");
228 return "Dumped " + counters.size() + " queries.";
233 * Implementation of the interface ReadGraph
235 final public String getURI(final Resource resource) throws AssumptionException, ValidationException, ServiceException {
237 assert (resource != null);
241 return syncRequest(new org.simantics.db.common.uri.ResourceToURI(resource));
243 } catch (AssumptionException e) {
245 throw new AssumptionException(e);
247 } catch (ValidationException e) {
249 throw new ValidationException(e);
251 } catch (ServiceException e) {
253 throw new ServiceException(e);
255 } catch (DatabaseException e) {
257 throw new ServiceException(INTERNAL_ERROR_STRING, e);
263 final public String getPossibleURI(final Resource resource) throws ValidationException, ServiceException {
265 assert (resource != null);
269 return syncRequest(new org.simantics.db.common.uri.ResourceToPossibleURI(resource));
271 } catch (ValidationException e) {
273 throw new ValidationException(e);
275 } catch (ServiceException e) {
277 throw new ServiceException(e);
279 } catch (DatabaseException e) {
281 throw new ServiceException(INTERNAL_ERROR_STRING, e);
287 final public Resource getResource(final String id)
288 throws ResourceNotFoundException, ValidationException,
295 Integer rid = QueryCache.resultURIToResource(this, id, parent, null);
296 // FIXME: stupid to throw this here and catch and wrap it right away
297 if(rid == 0) throw new ResourceNotFoundException(id);
298 return processor.querySupport.getResource(rid);
300 } catch (ResourceNotFoundException e) {
302 throw new ResourceNotFoundException(id, e);
304 } catch (ValidationException e) {
306 throw new ValidationException(e);
308 } catch (ServiceException e) {
310 throw new ServiceException(e);
312 } catch (DatabaseException e) {
314 throw new ServiceException(INTERNAL_ERROR_STRING, e);
320 final public Resource getPossibleResource(final String id)
321 throws ResourceNotFoundException, ValidationException,
328 return getResource(id);
330 } catch (ResourceNotFoundException e) {
334 } catch (ValidationException e) {
336 throw new ValidationException(e);
338 } catch (ServiceException e) {
340 throw new ServiceException(e);
342 } catch (DatabaseException e) {
344 throw new ServiceException(INTERNAL_ERROR_STRING, e);
351 public Map<String, Resource> getChildren(Resource resource) throws ValidationException, ServiceException {
353 assert (resource != null);
357 int rId = processor.querySupport.getId(resource);
358 return QueryCache.resultChildMap(this, rId, parent, null);
360 } catch (ValidationException e) {
362 throw new ValidationException(e);
364 } catch (ServiceException e) {
366 throw new ServiceException(e);
368 } catch (DatabaseException e) {
370 throw new ServiceException(INTERNAL_ERROR_STRING, e);
376 final public Resource getRootLibrary() {
377 return processor.getRootLibraryResource();
380 final public Resource getBuiltin(final String id)
381 throws ResourceNotFoundException, ServiceException {
387 return syncRequest(new Builtin(id));
389 } catch (ResourceNotFoundException e) {
391 throw new ResourceNotFoundException(id, e);
393 } catch (ServiceException e) {
395 throw new ServiceException(e);
397 } catch (DatabaseException e) {
399 throw new ServiceException(INTERNAL_ERROR_STRING, e);
405 static class StatementReadProcedure extends TIntArrayListInternal implements StatementProcedure {
407 private static Throwable DONE = new Throwable();
409 Throwable exception = null;
411 final ResourceSupport support;
413 public StatementReadProcedure(ResourceSupport support) {
414 this.support = support;
418 public synchronized void execute(AsyncReadGraph graph, int s, int p, int o) {
425 public void finished(AsyncReadGraph graph) {
430 public void exception(AsyncReadGraph graph, Throwable t) {
434 public void checkAndThrow() throws DatabaseException {
435 if(exception != DONE) {
436 if (exception instanceof DatabaseException)
437 throw (DatabaseException) exception;
439 throw new DatabaseException(
440 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
445 public boolean done() {
446 return exception != null;
450 public boolean contains(Object obj) {
451 if(!(obj instanceof InternalStatement))
453 InternalStatement statement = (InternalStatement)obj;
457 for(int i=0;i<sizeInternal();i+=3)
458 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
463 @SuppressWarnings("unchecked")
465 public <T> T[] toArray(T[] a) {
466 int length = sizeInternal() / 3;
467 if(length > a.length) {
468 Class<?> arrayType = a.getClass();
469 a = (arrayType == Object[].class)
470 ? (T[]) new Object[length]
471 : (T[]) Array.newInstance(arrayType.getComponentType(), length);
474 for(int i=length;i<a.length;++i)
477 for(int i=0,j=0;i<sizeInternal();i+=3,++j)
478 a[j] = (T)new InternalStatement(support, getQuick(i), getQuick(i+1), getQuick(i+2));
483 public boolean add(Statement e) {
484 throw new UnsupportedOperationException();
488 public boolean remove(Object o) {
489 throw new UnsupportedOperationException();
493 public boolean addAll(Collection<? extends Statement> c) {
494 throw new UnsupportedOperationException();
497 class IteratorImpl implements ListIterator<Statement> {
501 public IteratorImpl(int index) {
506 public boolean hasNext() {
507 return index < sizeInternal();
511 public Statement next() {
512 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
518 public void remove() {
519 throw new Error("Not supported");
523 public boolean hasPrevious() {
528 public Statement previous() {
530 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
535 public int nextIndex() {
540 public int previousIndex() {
545 public void set(Statement e) {
546 throw new UnsupportedOperationException();
550 public void add(Statement e) {
551 throw new UnsupportedOperationException();
557 public Iterator<Statement> iterator() {
558 return new IteratorImpl(0);
563 return sizeInternal() / 3;
567 public Object[] toArray() {
568 Object[] result = new Object[sizeInternal() / 3];
569 for(int i=0,j=0;j<sizeInternal();i++,j+=3)
570 result[i] = new InternalStatement(support, getQuick(j), getQuick(j+1), getQuick(j+2));
575 public boolean addAll(int index, Collection<? extends Statement> c) {
576 throw new UnsupportedOperationException();
580 public Statement get(int index) {
582 if(index < 0 || index >= sizeInternal())
583 throw new IndexOutOfBoundsException();
584 return new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
588 public Statement set(int index, Statement element) {
589 throw new UnsupportedOperationException();
593 public void add(int index, Statement element) {
594 throw new UnsupportedOperationException();
598 public Statement remove(int index) {
599 throw new UnsupportedOperationException();
603 public int indexOf(Object obj) {
604 if(!(obj instanceof InternalStatement))
606 InternalStatement statement = (InternalStatement)obj;
610 for(int i=0;i<sizeInternal();i+=3)
611 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
617 public int lastIndexOf(Object obj) {
618 if(!(obj instanceof InternalStatement))
620 InternalStatement statement = (InternalStatement)obj;
624 for(int i=sizeInternal()-3;i>=0;i-=3)
625 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
631 public ListIterator<Statement> listIterator() {
632 return new IteratorImpl(0);
636 public ListIterator<Statement> listIterator(int index) {
637 return new IteratorImpl(index*3);
641 public List<Statement> subList(int fromIndex, int toIndex) {
642 if(fromIndex < 0 || toIndex*3 >= sizeInternal() || fromIndex > toIndex)
643 throw new IndexOutOfBoundsException();
644 return new RandomAccessSubList<Statement>(this, fromIndex, toIndex-fromIndex);
649 final public Collection<Statement> getStatements(final Resource subject,
650 final Resource relation)
651 throws ManyObjectsForFunctionalRelationException, ServiceException {
653 assert (subject != null);
654 assert (relation != null);
658 StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
659 processor.forEachStatement(this, subject, relation, procedure);
660 procedure.checkAndThrow();
663 } catch (DatabaseException e) {
665 System.err.println(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation);
667 StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
668 processor.forEachStatement(this, subject, relation, procedure);
670 return Collections.emptyList();
672 // throw new ServiceException(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation, e);
679 final public Collection<Statement> getAssertedStatements(final Resource subject, final Resource relation)
680 throws ManyObjectsForFunctionalRelationException, ServiceException {
682 assert (subject != null);
683 assert (relation != null);
687 return syncRequest(new ForEachAssertedStatement(subject, relation));
689 } catch (ManyObjectsForFunctionalRelationException e) {
691 throw new ManyObjectsForFunctionalRelationException(e);
693 } catch (ServiceException e) {
695 throw new ServiceException(e);
697 } catch (DatabaseException e) {
699 throw new ServiceException(INTERNAL_ERROR_STRING, e);
706 final public Collection<Resource> getPredicates(final Resource subject) throws ServiceException {
708 assert (subject != null);
712 return processor.getPredicates(this, subject);
714 // AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
715 // processor.forEachPredicate(this, subject, procedure);
716 // procedure.checkAndThrow();
719 } catch (ServiceException e) {
721 throw new ServiceException(e);
723 } catch (DatabaseException e) {
725 throw new ServiceException(INTERNAL_ERROR_STRING, e);
727 } catch (Throwable e) {
729 throw new ServiceException(e);
736 final public Collection<Resource> getPrincipalTypes(final Resource subject)
737 throws ServiceException {
739 assert (subject != null);
743 AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
744 processor.forEachPrincipalType(this, subject, procedure);
745 procedure.checkAndThrow();
748 } catch (ServiceException e) {
750 throw new ServiceException(e);
752 } catch (DatabaseException e) {
754 throw new ServiceException(INTERNAL_ERROR_STRING, e);
761 final public Set<Resource> getTypes(final Resource subject) throws ServiceException {
763 assert (subject != null);
767 return processor.getTypes(this, subject);
769 } catch (ServiceException e) {
771 throw new ServiceException(e);
773 } catch (DatabaseException e) {
775 throw new ServiceException(INTERNAL_ERROR_STRING, e);
777 } catch (Throwable e) {
779 throw new ServiceException(e);
786 final public Set<Resource> getSupertypes(final Resource subject)
787 throws ServiceException {
789 assert (subject != null);
793 SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
794 processor.forSupertypes(this, subject, procedure);
795 procedure.checkAndThrow();
796 return procedure.result;
798 } catch (ServiceException e) {
800 throw new ServiceException(e);
802 } catch (DatabaseException e) {
804 throw new ServiceException(INTERNAL_ERROR_STRING, e);
811 final public Set<Resource> getSuperrelations(final Resource subject)
812 throws ServiceException {
814 assert (subject != null);
818 SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
819 processor.forSuperrelations(this, subject, procedure);
820 procedure.checkAndThrow();
821 return procedure.result;
823 } catch (ServiceException e) {
825 throw new ServiceException(e);
827 } catch (DatabaseException e) {
829 throw new ServiceException(INTERNAL_ERROR_STRING, e);
836 public Resource getPossibleSuperrelation(Resource subject) throws ServiceException {
840 SyncReadProcedure<Resource> procedure = new SyncReadProcedure<Resource>();
841 processor.forPossibleSuperrelation(this, subject, procedure);
842 procedure.checkAndThrow();
843 return procedure.result;
845 } catch (ServiceException e) {
847 throw new ServiceException(e);
849 } catch (DatabaseException e) {
851 throw new ServiceException(INTERNAL_ERROR_STRING, e);
858 final public Collection<Resource> getObjects(final Resource subject, final Resource relation)
859 throws ServiceException {
861 assert (subject != null);
862 assert (relation != null);
864 if(Development.DEVELOPMENT) {
865 if(Development.isTrue(DevelopmentKeys.READGRAPH_COUNT)) {
866 counters.adjustOrPutValue("objects $" + subject.getResourceId() + " $" + relation.getResourceId(), 1, 1);
868 //if(subject.getResourceId()==xx && relation.getResourceId()==xx) new Exception().printStackTrace();
873 AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
874 processor.forEachObject(this, subject, relation, procedure);
875 procedure.checkAndThrow();
878 } catch (DatabaseException e) {
880 throw new ServiceException(INTERNAL_ERROR_STRING, e);
887 final public Collection<Resource> getAssertedObjects(
888 final Resource subject, final Resource relation)
889 throws ManyObjectsForFunctionalRelationException, ServiceException {
892 throw new ArgumentException("Subject must not be null.");
893 if (relation == null)
894 throw new ArgumentException("Relation must not be null. Subject=" + subject);
898 return syncRequest(new ForEachAssertedObject(subject, relation));
900 } catch (ManyObjectsForFunctionalRelationException e) {
902 throw new ManyObjectsForFunctionalRelationException(e);
904 } catch (ServiceException e) {
906 throw new ServiceException(e);
908 } catch (DatabaseException e) {
910 throw new ServiceException(INTERNAL_ERROR_STRING, e);
917 final public Resource getInverse(final Resource relation) throws NoInverseException, ServiceException {
919 assert (relation != null);
923 return getSingleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
925 } catch (NoSingleResultException e) {
927 throw new NoInverseException(e);
929 } catch (ServiceException e) {
931 throw new ServiceException(e);
938 final public Resource getSingleObject(final Resource subject, final Resource relation) throws NoSingleResultException, ServiceException {
940 if( subject == null) throw new IllegalArgumentException("subject can not be null");
941 if( relation == null) throw new IllegalArgumentException("relation can not be null");
944 int single = processor.getSingleObject(this, subject, relation);
946 if (EMPTY_RESOURCE_CHECK) {
947 if (!hasStatement(subject)) {
948 throw new EmptyResourceException("Resource " + debugString(subject));
951 throw new NoSingleResultException("No single object for subject " + debugString(subject)
952 + " and relation " + debugString(relation), single);
954 return processor.querySupport.getResource(single);
955 } catch (NoSingleResultException e) {
957 } catch (DatabaseException e) {
958 throw new ServiceException(e);
963 final public Statement getSingleStatement(final Resource subject, final Resource relation) throws NoSingleResultException, ManyObjectsForFunctionalRelationException, ServiceException {
964 assert (subject != null);
965 assert (relation != null);
967 Collection<Statement> statements = getStatements(subject, relation);
968 if (statements.size() == 1) {
969 return statements.iterator().next();
971 if (EMPTY_RESOURCE_CHECK)
972 if (!hasStatement(subject))
973 throw new EmptyResourceException("Resource " + debugString(subject));
974 throw new NoSingleResultException("No single statement for subject " + debugString(subject)
975 + " and relation " + debugString(relation), statements.size());
977 } catch (ServiceException e) {
978 throw new ServiceException(e);
983 final public Resource getSingleType(final Resource subject) throws NoSingleResultException, ServiceException {
984 assert (subject != null);
986 ArrayList<Resource> principalTypes = (ArrayList<Resource>)getPrincipalTypes(subject);
987 if (principalTypes.size() == 1) {
988 return principalTypes.get(0);
990 throw new NoSingleResultException("No single type for subject " + debugString(subject), principalTypes.size());
992 } catch (ServiceException e) {
993 throw new ServiceException(e);
998 final public Resource getSingleType(final Resource subject,
999 final Resource baseType) throws NoSingleResultException,
1002 assert (subject != null);
1003 assert (baseType != null);
1006 return syncRequest(new SingleType(subject, baseType));
1007 } catch (DatabaseException e) {
1008 throw new NoSingleResultException("subject=" + subject + ", baseType=" + baseType, 0, e);
1013 final public <T> T getValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
1015 assert (subject != null);
1019 Layer0 L0 = processor.getL0(this);
1020 int object = processor.getSingleObject(this, subject, L0.HasDataType);
1021 if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
1023 if(processor.isImmutable(object)) {
1024 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1025 return getValue(subject, binding);
1027 byte[] dt = processor.getValue(this, object);
1028 if(dt == null) throw new ServiceException("No data type for " + subject);
1029 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1030 Binding binding = Bindings.getBinding(datatype);
1031 return getValue(subject, binding);
1034 } catch (IOException e) {
1036 throw new ServiceException(e);
1038 } catch (DoesNotContainValueException e) {
1040 throw new DoesNotContainValueException(e, subject);
1042 } catch (ServiceException e) {
1044 throw new ServiceException(e);
1046 } catch (DatabaseException e) {
1048 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1055 final public Variant getVariantValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
1057 assert (subject != null);
1061 Layer0 L0 = processor.getL0(this);
1062 int object = processor.getSingleObject(this, subject, L0.HasDataType);
1063 if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
1065 if(processor.isImmutable(object)) {
1066 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1067 return new Variant(binding, getValue(subject, binding));
1069 byte[] dt = processor.getValue(this, object);
1070 if(dt == null) throw new ServiceException("No data type for " + subject);
1071 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1072 Binding binding = Bindings.getBinding(datatype);
1073 return new Variant(binding, getValue(subject, binding));
1076 } catch (IOException e) {
1078 throw new ServiceException(e);
1080 } catch (DoesNotContainValueException e) {
1082 throw new DoesNotContainValueException(e, subject);
1084 } catch (ServiceException e) {
1086 throw new ServiceException(e);
1088 } catch (DatabaseException e) {
1090 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1095 static final IdentityHashMap<Binding,Serializer> serializers = new IdentityHashMap<Binding,Serializer>();
1098 serializers.put(Bindings.STRING, Bindings.STRING.serializer());
1101 final protected Serializer getSerializer(Binding binding) {
1102 return binding.serializer();
1106 final public <T> T getValue(final Resource subject, final Binding binding) throws DoesNotContainValueException, BindingException,
1109 assert (subject != null);
1111 byte[] bytes = null;
1114 bytes = processor.getValue(this, subject);
1115 if (bytes == null) throw new DoesNotContainValueException("No value for resource " + subject);
1117 Serializer serializer = getSerializer(binding);
1118 return (T)serializer.deserialize(bytes);
1120 } catch (DoesNotContainValueException e) {
1122 throw new DoesNotContainValueException(e);
1124 } catch (Throwable t) {
1125 throw new ServiceException("Could not getValue for subject " + debugString(subject) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(bytes), t);
1130 final public <T> T getRelatedValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1131 DoesNotContainValueException, ServiceException {
1133 assert (subject != null);
1134 assert (relation != null);
1137 Resource object = getSingleObject(subject, relation);
1138 return getValue(object);
1139 } catch (NoSingleResultException e) {
1140 throw new NoSingleResultException("No single value found for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1141 } catch (DoesNotContainValueException e) {
1143 Layer0 L0 = processor.getL0(this);
1144 Resource object = getPossibleObject(subject, relation);
1145 if(isInstanceOf(object, L0.Value)) {
1146 if(isInstanceOf(object, L0.Literal)) {
1147 throw new DoesNotContainValueException(e);
1149 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1152 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1154 } catch (DoesNotContainValueException e2) {
1156 } catch (DatabaseException e2) {
1157 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1159 } catch (ServiceException e) {
1160 throw new ServiceException(e);
1165 final public Variant getRelatedVariantValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1166 DoesNotContainValueException, ServiceException {
1168 assert (subject != null);
1169 assert (relation != null);
1172 Resource object = getSingleObject(subject, relation);
1173 return getVariantValue(object);
1174 } catch (NoSingleResultException e) {
1175 throw new NoSingleResultException("No single object for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1176 } catch (DoesNotContainValueException e) {
1178 Layer0 L0 = processor.getL0(this);
1179 Resource object = getPossibleObject(subject, relation);
1180 if(isInstanceOf(object, L0.Value)) {
1181 if(isInstanceOf(object, L0.Literal)) {
1182 throw new DoesNotContainValueException(e);
1184 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1187 throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1189 } catch (DoesNotContainValueException e2) {
1191 } catch (DatabaseException e2) {
1192 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1194 } catch (ServiceException e) {
1195 throw new ServiceException(e);
1200 final public <T> T getRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1201 throws NoSingleResultException, DoesNotContainValueException, BindingException, ServiceException {
1203 assert (subject != null);
1204 assert (relation != null);
1207 Resource object = getSingleObject(subject, relation);
1208 return getValue(object, binding);
1209 } catch (NoSingleResultException e) {
1210 String message = "";
1212 String subjectName = NameUtils.getSafeName(this, subject, true);
1213 String relationName = NameUtils.getSafeName(this, relation, true);
1214 message = "Subject: " + subjectName + ", Relation: " + relationName;
1215 } catch (DatabaseException e2) {
1218 throw new NoSingleResultException(message, e.getResultCount(), e);
1219 } catch (DoesNotContainValueException e) {
1220 throw new DoesNotContainValueException(e);
1221 } catch (ServiceException e) {
1222 throw new ServiceException(e);
1227 final public <T> T adapt(final Resource resource, final Class<T> clazz)
1228 throws AdaptionException, ValidationException, ServiceException {
1230 assert (resource != null);
1231 assert (clazz != null);
1235 return syncRequest(new Adapter<T>(resource, clazz));
1237 } catch (AdaptionException e) {
1239 throw new AdaptionException(e);
1241 } catch (ValidationException e) {
1243 throw new ValidationException(e);
1245 } catch (ServiceException e) {
1247 throw new ServiceException(e);
1249 } catch (DatabaseException e) {
1251 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1258 final public <T,C> T adaptContextual(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1259 throws AdaptionException, ValidationException, ServiceException {
1261 assert (resource != null);
1262 assert (context != null);
1264 class ContextualAdapter implements AsyncRead<T> {
1266 final private Resource resource;
1267 final private C context;
1268 final private Class<T> clazz;
1271 public int hashCode() {
1272 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1276 final public int threadHash() {
1277 return resource.getThreadHash();
1281 public boolean equals(Object object) {
1284 else if (object == null)
1286 else if (getClass() != object.getClass())
1288 ContextualAdapter r = (ContextualAdapter)object;
1289 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1293 public int getFlags() {
1297 public ContextualAdapter(Resource resource, C context, Class<T> clazz) {
1298 this.resource = resource;
1299 this.context = context;
1304 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1306 final AdaptionService service = getSession().peekService(AdaptionService.class);
1307 if (service == null)
1308 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1310 service.adapt(graph, resource, context, contextClass, clazz, false, procedure);
1315 public String toString() {
1316 return "Adapter for (" + resource + "," + context + ") as " + clazz.getName();
1323 return syncRequest(new ContextualAdapter(resource, context, clazz));
1325 } catch (AdaptionException e) {
1327 throw new AdaptionException(e);
1329 } catch (ValidationException e) {
1331 throw new ValidationException(e);
1333 } catch (ServiceException e) {
1335 throw new ServiceException(e);
1337 } catch (DatabaseException e) {
1339 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1346 final public <T> T adaptRelated(final Resource resource, final Resource relation, final Class<T> clazz)
1347 throws AdaptionException, NoSingleResultException, ValidationException, ServiceException {
1349 assert (resource != null);
1350 assert (clazz != null);
1352 Statement stm = getSingleStatement(resource, relation);
1354 return adaptContextual(stm.getObject(), new RelationContextImpl(resource, stm), RelationContext.class, clazz);
1359 final public <T> T getPossibleRelatedAdapter(final Resource resource, final Resource relation, final Class<T> clazz)
1360 throws ValidationException, ServiceException {
1363 return adaptRelated(resource, relation, clazz);
1364 } catch (DatabaseException e) {
1371 final public <T,C> T getPossibleContextualAdapter(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1372 throws ValidationException, ServiceException {
1374 assert (resource != null);
1375 assert (context != null);
1377 class PossibleContextualAdapter implements AsyncRead<T> {
1379 final private Resource resource;
1380 final private C context;
1381 final private Class<T> clazz;
1384 public int hashCode() {
1385 return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1389 final public int threadHash() {
1390 return resource.getThreadHash();
1394 public boolean equals(Object object) {
1397 else if (object == null)
1399 else if (getClass() != object.getClass())
1401 PossibleContextualAdapter r = (PossibleContextualAdapter)object;
1402 return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1406 public int getFlags() {
1410 public PossibleContextualAdapter(Resource resource, C context, Class<T> clazz) {
1411 this.resource = resource;
1412 this.context = context;
1417 public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1419 final AdaptionService service = getSession().peekService(AdaptionService.class);
1420 if (service == null)
1421 procedure.exception(graph, new ServiceException("No AdaptionService available"));
1423 service.adapt(graph, resource, context, contextClass, clazz, true, procedure);
1428 public String toString() {
1429 return "Possible adapter for (" + resource + "," + context + ") as " + clazz.getName();
1436 return syncRequest(new PossibleContextualAdapter(resource, context, clazz));
1438 } catch (ValidationException e) {
1440 throw new ValidationException(e);
1442 } catch (ServiceException e) {
1444 throw new ServiceException(e);
1446 } catch (DatabaseException e) {
1448 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1455 final public <T> T adaptUnique(final Resource resource, final Class<T> clazz)
1456 throws AdaptionException, ValidationException, ServiceException {
1458 assert (resource != null);
1459 assert (clazz != null);
1463 return syncRequest(new UniqueAdapter<T>(resource, clazz));
1465 } catch (AdaptionException e) {
1467 throw new AdaptionException(e);
1469 } catch (ValidationException e) {
1471 throw new ValidationException(e);
1473 } catch (ServiceException e) {
1475 throw new ServiceException(e);
1477 } catch (DatabaseException e) {
1479 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1486 final public Resource getPossibleInverse(final Resource relation)
1487 throws ServiceException {
1489 assert (relation != null);
1493 return getPossibleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
1495 } catch (ServiceException e) {
1497 throw new ServiceException(e);
1499 } catch (DatabaseException e) {
1501 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1508 public Resource getPossibleObject(final Resource subject, final Resource relation)
1509 throws ManyObjectsForFunctionalRelationException, ServiceException {
1511 assert (subject != null);
1512 assert (relation != null);
1516 int result = processor.getSingleObject(this, subject, relation);
1517 if(result == 0) return null;
1519 return processor.querySupport.getResource(result);
1521 } catch (ManyObjectsForFunctionalRelationException e) {
1523 throw new ManyObjectsForFunctionalRelationException("subject=" + subject + ", relation=" + relation, e);
1525 } catch (DatabaseException e) {
1527 throw new ServiceException(e);
1534 final public Statement getPossibleStatement(final Resource subject, final Resource relation)
1535 throws ManyObjectsForFunctionalRelationException, ServiceException {
1537 assert (subject != null);
1538 assert (relation != null);
1542 Collection<Statement> statements = getStatements(subject, relation);
1543 if(statements.size() == 1) return statements.iterator().next();
1546 } catch (ManyObjectsForFunctionalRelationException e) {
1548 throw new ManyObjectsForFunctionalRelationException("Many objects in " + subject + " for functional relation " + relation);
1550 } catch (ServiceException e) {
1552 throw new ServiceException(e);
1559 final public Resource getPossibleType(final Resource subject, final Resource baseType) throws ServiceException {
1561 assert (subject != null);
1562 assert (baseType != null);
1566 AsyncReadProcedure<Resource> procedure = new AsyncReadProcedure<Resource>();
1567 forPossibleType(subject, baseType, procedure);
1568 procedure.checkAndThrow();
1569 return procedure.result;
1571 } catch (ServiceException e) {
1573 throw new ServiceException(e);
1575 } catch (DatabaseException e) {
1577 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1584 final public <T> T getPossibleValue(final Resource subject) throws ServiceException {
1586 assert (subject != null);
1590 int object = processor.getSingleObject(this, subject, processor.getL0(this).HasDataType);
1591 if(object == 0) return null;
1593 if(processor.isImmutable(object)) {
1594 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance());
1595 return getPossibleValue(subject, binding);
1597 byte[] dt = processor.getValue(this, object);
1598 if(dt == null) return null;
1599 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1600 Binding binding = Bindings.getBinding(datatype);
1601 return getPossibleValue(subject, binding);
1604 } catch (IOException e) {
1606 throw new ServiceException(e);
1608 } catch (ServiceException e) {
1610 throw new ServiceException(e);
1612 } catch (DatabaseException e) {
1614 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1621 final public <T> T getPossibleValue(final Resource subject, final Binding binding) throws BindingException, ServiceException {
1623 assert (subject != null);
1624 assert (binding != null);
1628 byte[] dt = processor.getValue(this, subject);
1629 if(dt == null) return null;
1630 Serializer serializer = getSerializer(binding);
1631 return (T)serializer.deserialize(dt);
1633 } catch (IOException e) {
1635 throw new ServiceException(e);
1637 } catch (BindingException e) {
1639 throw new BindingException(e);
1641 } catch (ServiceException e) {
1643 throw new ServiceException(e);
1645 } catch (DatabaseException e) {
1646 e.printStackTrace();
1647 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1653 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation)
1654 throws ManyObjectsForFunctionalRelationException, ServiceException {
1656 assert (subject != null);
1657 assert (relation != null);
1661 Resource object = getPossibleObject(subject, relation);
1662 if(object == null) return null;
1663 else return getPossibleValue(object);
1665 } catch (ManyObjectsForFunctionalRelationException e) {
1667 throw new ManyObjectsForFunctionalRelationException(e);
1669 } catch (ServiceException e) {
1671 throw new ServiceException(e);
1678 public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1679 throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
1681 assert (subject != null);
1682 assert (relation != null);
1683 assert (binding != null);
1687 Resource object = getPossibleObject(subject, relation);
1688 if(object == null) return null;
1689 else return getPossibleValue(object, binding);
1691 } catch (ManyObjectsForFunctionalRelationException e) {
1693 throw new ManyObjectsForFunctionalRelationException(e);
1695 } catch (BindingException e) {
1697 throw new BindingException(e);
1699 } catch (ServiceException e) {
1701 throw new ServiceException(e);
1708 public <T> T getPossibleAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1710 assert (resource != null);
1711 assert (clazz != null);
1715 return syncRequest(new PossibleAdapter<T>(resource, clazz));
1717 } catch (ValidationException e) {
1719 throw new ValidationException(e);
1721 } catch (AdaptionException e) {
1725 } catch (DatabaseException e) {
1727 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1733 public <T> T getPossibleUniqueAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1735 assert (resource != null);
1736 assert (clazz != null);
1740 return syncRequest(new PossibleUniqueAdapter<T>(resource, clazz));
1742 } catch (AdaptionException e) {
1746 } catch (ValidationException e) {
1748 throw new ValidationException(e);
1750 } catch (DatabaseException e) {
1752 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1759 final public boolean isInstanceOf(final Resource resource, final Resource type) throws ServiceException {
1761 assert (resource != null);
1762 assert (type != null);
1764 Set<Resource> resources = getTypes(resource);
1765 // This check was necessary because some of the callers of this method got stuck when the NPE was thrown from here.
1766 if (null == resources)
1769 if(EMPTY_RESOURCE_CHECK) {
1770 if (resources.isEmpty()) {
1771 if(!hasStatement(resource)) throw new EmptyResourceException("Resource " + debugString(resource));
1775 return resources.contains(type);
1780 final public boolean isInheritedFrom(final Resource resource, final Resource type) throws ServiceException {
1782 assert (resource != null);
1783 assert (type != null);
1787 if(resource.equals(type)) return true;
1789 return getSupertypes(resource).contains(type);
1791 } catch (ServiceException e) {
1793 throw new ServiceException(e);
1800 final public boolean isSubrelationOf(final Resource resource, final Resource type) throws ServiceException {
1802 assert (resource != null);
1803 assert (type != null);
1807 if(resource.equals(type)) return true;
1809 return getSuperrelations(resource).contains(type);
1811 } catch (ServiceException e) {
1813 throw new ServiceException(e);
1820 final public boolean hasStatement(final Resource subject) throws ServiceException {
1822 assert (subject != null);
1826 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1827 processor.forHasStatement(this, subject, procedure);
1828 procedure.checkAndThrow();
1829 return procedure.result;
1831 } catch (ServiceException e) {
1833 throw new ServiceException(e);
1835 } catch (DatabaseException e) {
1837 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1844 final public boolean hasStatement(final Resource subject, final Resource relation) throws ServiceException {
1846 assert (subject != null);
1847 assert (relation != null);
1851 Collection<Resource> objects = getObjects(subject, relation);
1852 return !objects.isEmpty();
1854 } catch (ServiceException e) {
1856 throw new ServiceException(e);
1863 final public boolean hasStatement(final Resource subject, final Resource relation, final Resource object) throws ServiceException {
1865 assert (subject != null);
1866 assert (relation != null);
1867 assert (object != null);
1871 for(Resource o : getObjects(subject, relation)) {
1872 if(object.equals(o)) return true;
1877 } catch (ServiceException e) {
1879 throw new ServiceException(e);
1886 final public boolean hasValue(final Resource subject) throws ServiceException {
1888 assert (subject != null);
1892 SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1893 processor.forHasValue(this, subject, procedure);
1894 procedure.checkAndThrow();
1895 return procedure.result;
1897 } catch (ServiceException e) {
1899 throw new ServiceException(e);
1901 } catch (DatabaseException e) {
1903 throw new ServiceException(INTERNAL_ERROR_STRING, e);
1909 final AsyncProcedure<?> NONE = new AsyncProcedure<Object>() {
1912 public void execute(AsyncReadGraph graph, Object result) {
1916 public void exception(AsyncReadGraph graph, Throwable throwable) {
1922 * Implementation of the interface RequestProcessor
1926 public <T> T syncRequest(final Read<T> request) throws DatabaseException {
1927 assert (request != null);
1929 T result = (T)QueryCache.runnerReadEntry(this, request, parent, null, null, true);
1935 public <T> T syncRequest(Read<T> request, SyncListener<T> procedure)
1936 throws DatabaseException {
1937 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
1941 public <T> T syncRequest(Read<T> request, final Listener<T> procedure)
1942 throws DatabaseException {
1943 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
1947 public <T> T syncRequest(final Read<T> request, final AsyncProcedure<T> procedure) throws DatabaseException {
1949 assert (request != null);
1951 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
1952 T result = (T)QueryCache.runnerReadEntry(this, request, parent, listener, procedure, true);
1958 public <T> T syncRequest(final Read<T> request,
1959 final SyncProcedure<T> procedure) throws DatabaseException {
1960 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
1964 public <T> T syncRequest(Read<T> request, Procedure<T> procedure)
1965 throws DatabaseException {
1966 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
1969 static class AsyncReadProcedure<T> implements AsyncProcedure<T> {
1971 private static Throwable DONE = new Throwable();
1974 Throwable exception = null;
1977 public void execute(AsyncReadGraph graph, T t) {
1983 public void exception(AsyncReadGraph graph, Throwable t) {
1987 public void checkAndThrow() throws DatabaseException {
1988 if(exception != DONE) {
1989 if (exception instanceof DatabaseException)
1990 throw (DatabaseException) exception;
1992 throw new DatabaseException(
1993 "Unexpected exception in ReadGraph.syncRequest(AsyncRead)",
1998 public boolean done() {
1999 return exception != null;
2005 public <T> T syncRequest(final AsyncRead<T> request)
2006 throws DatabaseException {
2008 assert (request != null);
2009 return syncRequest(request, new AsyncProcedureAdapter<>() );
2014 public <T> T syncRequest(AsyncRead<T> request, AsyncListener<T> procedure)
2015 throws DatabaseException {
2016 return syncRequest(request, (AsyncProcedure<T>) procedure);
2020 public <T> T syncRequest(AsyncRead<T> request, SyncListener<T> procedure)
2021 throws DatabaseException {
2022 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
2026 public <T> T syncRequest(AsyncRead<T> request, Listener<T> procedure)
2027 throws DatabaseException {
2028 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
2032 final public <T> T syncRequest(final AsyncRead<T> request,
2033 final AsyncProcedure<T> procedure) throws DatabaseException {
2035 assert (request != null);
2037 ListenerBase listener = getListenerBase(procedure);
2038 T result = (T)QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure, true);
2044 public <T> T syncRequest(AsyncRead<T> request,
2045 final SyncProcedure<T> procedure) throws DatabaseException {
2046 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2050 final public <T> T syncRequest(final AsyncRead<T> request,
2051 final Procedure<T> procedure) throws DatabaseException {
2052 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2056 public <T> Collection<T> syncRequest(final MultiRead<T> request)
2057 throws DatabaseException {
2059 assert (request != null);
2061 final ArrayList<T> result = new ArrayList<T>();
2062 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2064 syncRequest(request, new SyncMultiProcedure<T>() {
2067 public void execute(ReadGraph graph, T t) {
2068 synchronized (result) {
2074 public void finished(ReadGraph graph) {
2078 public void exception(ReadGraph graph, Throwable t) {
2083 public String toString() {
2084 return "syncRequest(MultiRead) -> " + request;
2089 Throwable t = exception.get();
2091 if (t instanceof DatabaseException)
2092 throw (DatabaseException) t;
2094 throw new DatabaseException(
2095 "Unexpected exception in ReadGraph.syncRequest(Read)",
2104 public <T> Collection<T> syncRequest(MultiRead<T> request,
2105 SyncMultiListener<T> procedure) {
2106 return syncRequest(request, (SyncMultiProcedure<T>)procedure);
2110 public <T> Collection<T> syncRequest(MultiRead<T> request,
2111 MultiListener<T> procedure) {
2112 return syncRequest(request, new NoneToSyncMultiListener<T>(procedure));
2116 public <T> Collection<T> syncRequest(MultiRead<T> request,
2117 SyncMultiProcedure<T> procedure) {
2119 assert (request != null);
2121 ListenerBase listener = getListenerBase(procedure);
2123 final ResultCallWrappedSyncQueryProcedure<T> wrapper = new ResultCallWrappedSyncQueryProcedure<T>(procedure);
2125 if (parent != null || listener != null) {
2127 // Object syncParent = request;
2129 // final ReadGraphImpl newGraph = newSync();
2131 processor.query(this, request, parent, wrapper, listener);
2133 // newGraph.waitAsync(syncParent);
2137 // Object syncParent = request;
2139 // final ReadGraphImpl newGraph = newSync();
2142 request.perform(this, wrapper);
2143 } catch (Throwable t) {
2144 wrapper.exception(this, t);
2149 return wrapper.get();
2154 public <T> Collection<T> syncRequest(MultiRead<T> request,
2155 MultiProcedure<T> procedure) {
2156 return syncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
2159 static class AsyncMultiReadProcedure<T> extends ArrayList<T> implements AsyncMultiProcedure<T> {
2161 private static Throwable DONE = new Throwable();
2163 private static final long serialVersionUID = -6494230465108115812L;
2165 Throwable exception = null;
2168 public synchronized void execute(AsyncReadGraph graph, T t) {
2173 public void finished(AsyncReadGraph graph) {
2178 public void exception(AsyncReadGraph graph, Throwable t) {
2182 public void checkAndThrow() throws DatabaseException {
2183 if(exception != DONE) {
2184 if (exception instanceof DatabaseException)
2185 throw (DatabaseException) exception;
2187 throw new DatabaseException(
2188 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
2193 public boolean done() {
2194 return exception != null;
2200 final public <T> Collection<T> syncRequest(AsyncMultiRead<T> request)
2201 throws DatabaseException {
2203 assert (request != null);
2205 final AsyncMultiReadProcedure<T> procedure = new AsyncMultiReadProcedure<T>();
2207 syncRequest(request, procedure);
2209 procedure.checkAndThrow();
2215 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2216 AsyncMultiListener<T> procedure) {
2217 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2221 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2222 SyncMultiListener<T> procedure) {
2223 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2227 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2228 MultiListener<T> procedure) {
2229 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2232 final private <T> void syncRequest(final AsyncMultiRead<T> request,
2233 final AsyncMultiReadProcedure<T> procedure) {
2235 assert (request != null);
2236 assert (procedure != null);
2238 ListenerBase listener = getListenerBase(procedure);
2240 if (parent != null || listener != null) {
2242 // Object syncParent = request;
2244 // final ReadGraphImpl newGraph = newSync();
2246 processor.query(this, request, parent, procedure, listener);
2248 // newGraph.waitAsync(syncParent);
2249 waitAsyncProcedure(procedure);
2253 // Object syncParent = callerThread == Integer.MIN_VALUE ? null
2256 // final ReadGraphImpl newGraph = newSyncAsync(syncParent);
2261 // ReadGraphImpl sync = newSync();
2262 request.perform(this, procedure);
2263 // sync.waitAsync(null);
2264 waitAsyncProcedure(procedure);
2267 } catch (Throwable t) {
2269 waitAsyncProcedure(procedure);
2280 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2281 final AsyncMultiProcedure<T> procedure) {
2283 assert (request != null);
2284 assert (procedure != null);
2286 ListenerBase listener = getListenerBase(procedure);
2288 if (parent != null || listener != null) {
2290 // Object syncParent = request;
2292 // final ReadGraphImpl newGraph = newSync();
2294 processor.query(this, request, parent, procedure, listener);
2296 // newGraph.waitAsync(syncParent);
2300 // Object syncParent = request;
2302 // final ReadGraphImpl newGraph = newSync();
2306 request.perform(this, new AsyncMultiProcedure<T>() {
2309 public void execute(AsyncReadGraph graph, T result) {
2310 procedure.execute(graph, result);
2314 public void finished(AsyncReadGraph graph) {
2315 procedure.finished(graph);
2319 public void exception(AsyncReadGraph graph, Throwable t) {
2320 procedure.exception(graph, t);
2324 public String toString() {
2325 return "syncRequest(AsyncMultiRead) -> " + procedure;
2330 } catch (Throwable t) {
2342 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2343 final SyncMultiProcedure<T> procedure) {
2344 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2348 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2349 final MultiProcedure<T> procedure) {
2350 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2354 public <T> T syncRequest(final ExternalRead<T> request)
2355 throws DatabaseException {
2357 assert (request != null);
2359 return syncRequest(request, new Procedure<T>() {
2362 public void execute(T t) {
2366 public void exception(Throwable t) {
2370 public String toString() {
2371 return "syncRequest(AsyncRead) -> " + request;
2379 public <T> T syncRequest(ExternalRead<T> request, Listener<T> procedure) throws DatabaseException {
2380 return syncRequest(request, (Procedure<T>) procedure);
2384 final public <T> T syncRequest(final ExternalRead<T> request,
2385 final Procedure<T> procedure) throws DatabaseException {
2387 assert (request != null);
2389 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
2390 return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
2395 public void syncRequest(final Write request) throws DatabaseException {
2397 assert (request != null);
2399 throw new DatabaseException(
2400 "Write operations are not supported during read transactions!");
2405 public <T> T syncRequest(final WriteResult<T> request) throws DatabaseException {
2407 assert (request != null);
2409 throw new DatabaseException(
2410 "Write operations are not supported during read transactions!");
2415 public void syncRequest(final DelayedWrite request)
2416 throws DatabaseException {
2418 assert (request != null);
2420 throw new DatabaseException(
2421 "Write operations are not supported during read transactions!");
2426 public <T> T syncRequest(final DelayedWriteResult<T> request) throws DatabaseException {
2428 assert (request != null);
2430 throw new DatabaseException(
2431 "Write operations are not supported during read transactions!");
2436 public void syncRequest(final WriteOnly request) throws DatabaseException {
2438 assert (request != null);
2440 throw new DatabaseException(
2441 "Write operations are not supported during read transactions!");
2446 public <T> T syncRequest(final WriteOnlyResult<T> request) throws DatabaseException {
2448 assert (request != null);
2450 throw new DatabaseException(
2451 "Write operations are not supported during read transactions!");
2456 public <T> void async(ReadInterface<T> r, AsyncProcedure<T> procedure) {
2457 r.request(this, procedure);
2461 public <T> void async(ReadInterface<T> r, Procedure<T> procedure) {
2462 r.request(this, procedure);
2466 public <T> void async(ReadInterface<T> r, SyncProcedure<T> procedure) {
2467 r.request(this, procedure);
2471 public <T> void async(ReadInterface<T> r, AsyncListener<T> procedure) {
2472 r.request(this, procedure);
2476 public <T> void async(ReadInterface<T> r, Listener<T> procedure) {
2477 r.request(this, procedure);
2481 public <T> void async(ReadInterface<T> r, SyncListener<T> procedure) {
2482 r.request(this, procedure);
2486 public <T> T sync(ReadInterface<T> r) throws DatabaseException {
2487 return r.request(this);
2491 public <T> T sync(WriteInterface<T> r) throws DatabaseException {
2492 return r.request(this);
2496 public <T> void async(WriteInterface<T> r, Procedure<T> procedure) {
2497 r.request(this, procedure);
2501 public <T> void async(WriteInterface<T> r) {
2502 r.request(this, new ProcedureAdapter<T>());
2506 * Implementation of the interface AsyncReadGraph
2510 public void forURI(Resource resource, AsyncListener<String> listener) {
2511 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2516 public void forURI(Resource resource, SyncListener<String> listener) {
2517 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2522 public void forURI(Resource resource, Listener<String> listener) {
2523 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2528 final public void forURI(final Resource resource,
2529 final AsyncProcedure<String> procedure) {
2531 assert (resource != null);
2532 assert (procedure != null);
2534 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2540 public void forURI(Resource resource, SyncProcedure<String> procedure) {
2541 forURI(resource, new SyncToAsyncProcedure<String>(procedure));
2545 public void forURI(Resource resource, Procedure<String> procedure) {
2546 forURI(resource, new NoneToAsyncProcedure<String>(procedure));
2550 public void forResource(String id, AsyncListener<Resource> listener) {
2551 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2556 public void forResource(String id, SyncListener<Resource> listener) {
2557 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2562 public void forResource(String id, Listener<Resource> listener) {
2563 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2568 final public void forResource(final String id,
2569 final AsyncProcedure<Resource> procedure) {
2571 assert (id != null);
2572 assert (procedure != null);
2574 processor.forResource(this, id, procedure);
2579 public void forResource(String id, SyncProcedure<Resource> procedure) {
2580 forResource(id, new SyncToAsyncProcedure<Resource>(procedure));
2584 public void forResource(String id, Procedure<Resource> procedure) {
2585 forResource(id, new NoneToAsyncProcedure<Resource>(procedure));
2589 public void forBuiltin(String id, AsyncListener<Resource> listener) {
2590 asyncRequest(new Builtin(id), listener);
2594 public void forBuiltin(String id, SyncListener<Resource> listener) {
2595 asyncRequest(new Builtin(id), listener);
2599 public void forBuiltin(String id, Listener<Resource> listener) {
2600 asyncRequest(new Builtin(id), listener);
2604 final public void forBuiltin(final String id,
2605 final AsyncProcedure<Resource> procedure) {
2607 assert (id != null);
2608 assert (procedure != null);
2610 processor.forBuiltin(this, id, procedure);
2615 public void forBuiltin(String id, SyncProcedure<Resource> procedure) {
2616 forBuiltin(id, new SyncToAsyncProcedure<Resource>(procedure));
2620 public void forBuiltin(String id, Procedure<Resource> procedure) {
2621 forBuiltin(id, new NoneToAsyncProcedure<Resource>(procedure));
2625 final public void forEachStatement(Resource subject, Resource relation,
2626 AsyncMultiProcedure<Statement> procedure) {
2628 assert (subject != null);
2629 assert (relation != null);
2630 assert (procedure != null);
2632 processor.forEachStatement(this, subject, relation, procedure);
2637 public void forEachStatement(Resource subject, Resource relation,
2638 SyncMultiProcedure<Statement> procedure) {
2639 forEachStatement(subject, relation,
2640 new SyncToAsyncMultiProcedure<Statement>(procedure));
2644 final public void forEachStatement(Resource subject, Resource relation,
2645 MultiProcedure<Statement> procedure) {
2647 assert (subject != null);
2648 assert (relation != null);
2649 assert (procedure != null);
2651 processor.forEachStatement(this, subject, relation, procedure);
2656 final public void forStatementSet(Resource subject, Resource relation,
2657 AsyncSetListener<Statement> procedure) {
2659 assert (subject != null);
2660 assert (relation != null);
2661 assert (procedure != null);
2663 processor.forStatementSet(this, subject, relation, procedure);
2668 final public void forStatementSet(Resource subject, Resource relation,
2669 SyncSetListener<Statement> procedure) {
2670 forStatementSet(subject, relation,
2671 new SyncToAsyncSetProcedure<Statement>(procedure));
2675 public void forStatementSet(Resource subject, Resource relation,
2676 SetListener<Statement> listener) {
2677 forStatementSet(subject, relation,
2678 new NoneToAsyncSetProcedure<Statement>(listener));
2682 final public void forEachAssertedStatement(final Resource subject,
2683 final Resource relation,
2684 final AsyncMultiProcedure<Statement> procedure) {
2686 assert (subject != null);
2687 assert (relation != null);
2688 assert (procedure != null);
2690 processor.forEachAssertedStatement(this, subject, relation, procedure);
2695 public void forEachAssertedStatement(Resource subject, Resource relation,
2696 SyncMultiProcedure<Statement> procedure) {
2697 forEachAssertedStatement(subject, relation,
2698 new SyncToAsyncMultiProcedure<Statement>(procedure));
2702 public void forEachAssertedStatement(Resource subject, Resource relation,
2703 MultiProcedure<Statement> procedure) {
2704 forEachAssertedStatement(subject, relation,
2705 new NoneToAsyncMultiProcedure<Statement>(procedure));
2709 public void forAssertedStatementSet(Resource subject, Resource relation,
2710 AsyncSetListener<Statement> procedure) {
2712 assert (subject != null);
2713 assert (relation != null);
2714 assert (procedure != null);
2716 processor.forAssertedStatementSet(this, subject, relation, procedure);
2721 public void forAssertedStatementSet(Resource subject, Resource relation,
2722 SyncSetListener<Statement> procedure) {
2724 assert (subject != null);
2725 assert (relation != null);
2726 assert (procedure != null);
2728 forAssertedStatementSet(subject, relation,
2729 new SyncToAsyncSetProcedure<Statement>(procedure));
2734 public void forAssertedStatementSet(Resource subject, Resource relation,
2735 SetListener<Statement> procedure) {
2737 assert (subject != null);
2738 assert (relation != null);
2739 assert (procedure != null);
2741 forAssertedStatementSet(subject, relation,
2742 new NoneToAsyncSetProcedure<Statement>(procedure));
2747 final public void forEachPredicate(final Resource subject,
2748 final AsyncMultiProcedure<Resource> procedure) {
2750 assert (subject != null);
2751 assert (procedure != null);
2753 processor.forEachPredicate(this, subject, procedure);
2758 public void forEachPredicate(Resource subject,
2759 SyncMultiProcedure<Resource> procedure) {
2760 forEachPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(
2765 final public void forEachPredicate(final Resource subject,
2766 final MultiProcedure<Resource> procedure) {
2768 assert (subject != null);
2769 assert (procedure != null);
2771 processor.forEachPredicate(this, subject, procedure);
2776 final public void forPredicateSet(final Resource subject,
2777 final AsyncSetListener<Resource> procedure) {
2779 assert (subject != null);
2780 assert (procedure != null);
2782 processor.forPredicateSet(this, subject, procedure);
2787 final public void forPredicateSet(final Resource subject,
2788 final SyncSetListener<Resource> procedure) {
2790 assert (subject != null);
2791 assert (procedure != null);
2793 forPredicateSet(subject, new SyncToAsyncSetProcedure<Resource>(
2799 final public void forPredicateSet(final Resource subject,
2800 final SetListener<Resource> procedure) {
2802 assert (subject != null);
2803 assert (procedure != null);
2805 forPredicateSet(subject, new NoneToAsyncSetProcedure<Resource>(
2811 final public void forEachPrincipalType(final Resource subject,
2812 final AsyncMultiProcedure<Resource> procedure) {
2814 assert (subject != null);
2815 assert (procedure != null);
2817 processor.forEachPrincipalType(this, subject, procedure);
2822 public void forEachPrincipalType(Resource subject,
2823 SyncMultiProcedure<Resource> procedure) {
2824 forEachPrincipalType(subject, new SyncToAsyncMultiProcedure<Resource>(
2829 final public void forEachPrincipalType(final Resource subject,
2830 final MultiProcedure<Resource> procedure) {
2832 assert (subject != null);
2833 assert (procedure != null);
2835 processor.forEachPrincipalType(this, subject, procedure);
2840 final public void forPrincipalTypeSet(final Resource subject,
2841 final AsyncSetListener<Resource> procedure) {
2843 assert (subject != null);
2844 assert (procedure != null);
2846 processor.forPrincipalTypeSet(this, subject, procedure);
2851 final public void forPrincipalTypeSet(final Resource subject,
2852 final SyncSetListener<Resource> procedure) {
2854 assert (subject != null);
2855 assert (procedure != null);
2857 forPrincipalTypeSet(subject, new SyncToAsyncSetProcedure<Resource>(
2863 final public void forPrincipalTypeSet(final Resource subject,
2864 final SetListener<Resource> procedure) {
2866 assert (subject != null);
2867 assert (procedure != null);
2869 forPrincipalTypeSet(subject, new NoneToAsyncSetProcedure<Resource>(
2875 public void forTypes(Resource subject, AsyncListener<Set<Resource>> listener) {
2876 asyncRequest(new Types(subject), listener);
2880 public void forTypes(Resource subject, SyncListener<Set<Resource>> listener) {
2881 asyncRequest(new Types(subject), listener);
2885 public void forTypes(Resource subject, Listener<Set<Resource>> listener) {
2886 asyncRequest(new Types(subject), listener);
2890 final public void forTypes(final Resource subject,
2891 final AsyncProcedure<Set<Resource>> procedure) {
2893 assert (subject != null);
2894 assert (procedure != null);
2896 processor.forTypes(this, subject, procedure);
2901 public void forTypes(Resource subject,
2902 SyncProcedure<Set<Resource>> procedure) {
2903 forTypes(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
2907 public void forTypes(Resource subject, Procedure<Set<Resource>> procedure) {
2908 forTypes(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
2912 public void forSupertypes(Resource subject,
2913 AsyncListener<Set<Resource>> listener) {
2914 asyncRequest(new Types(subject), listener);
2918 public void forSupertypes(Resource subject,
2919 SyncListener<Set<Resource>> listener) {
2920 asyncRequest(new Types(subject), listener);
2924 public void forSupertypes(Resource subject, Listener<Set<Resource>> listener) {
2925 asyncRequest(new Types(subject), listener);
2929 final public void forSupertypes(final Resource subject,
2930 final AsyncProcedure<Set<Resource>> procedure) {
2932 assert (subject != null);
2933 assert (procedure != null);
2935 processor.forSupertypes(this, subject, procedure);
2940 public void forSupertypes(Resource subject,
2941 SyncProcedure<Set<Resource>> procedure) {
2942 forSupertypes(subject, new SyncToAsyncProcedure<Set<Resource>>(
2947 public void forSupertypes(Resource subject,
2948 Procedure<Set<Resource>> procedure) {
2949 forSupertypes(subject, new NoneToAsyncProcedure<Set<Resource>>(
2954 public void forDirectSuperrelations(Resource subject,
2955 AsyncMultiProcedure<Resource> procedure) {
2957 assert (subject != null);
2958 assert (procedure != null);
2960 processor.forDirectSuperrelations(this, subject, procedure);
2965 public void forPossibleSuperrelation(Resource subject, AsyncProcedure<Resource> procedure) {
2967 assert (subject != null);
2968 assert (procedure != null);
2970 processor.forPossibleSuperrelation(this, subject, procedure);
2975 public void forSuperrelations(Resource subject,
2976 AsyncListener<Set<Resource>> listener) {
2977 asyncRequest(new Types(subject), listener);
2981 public void forSuperrelations(Resource subject,
2982 SyncListener<Set<Resource>> listener) {
2983 asyncRequest(new Types(subject), listener);
2987 public void forSuperrelations(Resource subject,
2988 Listener<Set<Resource>> listener) {
2989 asyncRequest(new Types(subject), listener);
2993 final public void forSuperrelations(final Resource subject,
2994 final AsyncProcedure<Set<Resource>> procedure) {
2996 assert (subject != null);
2997 assert (procedure != null);
2999 processor.forSuperrelations(this, subject, procedure);
3004 public void forSuperrelations(Resource subject,
3005 SyncProcedure<Set<Resource>> procedure) {
3006 forSuperrelations(subject, new SyncToAsyncProcedure<Set<Resource>>(
3011 public void forSuperrelations(Resource subject,
3012 Procedure<Set<Resource>> procedure) {
3013 forSuperrelations(subject, new NoneToAsyncProcedure<Set<Resource>>(
3018 final public void forEachObject(final Resource subject, final Resource relation, final AsyncMultiProcedure<Resource> procedure) {
3019 processor.forEachObject(this, subject, relation, procedure);
3023 public void forEachObject(Resource subject, Resource relation,
3024 SyncMultiProcedure<Resource> procedure) {
3025 forEachObject(subject, relation,
3026 new SyncToAsyncMultiProcedure<Resource>(procedure));
3030 public void forEachObject(Resource subject, Resource relation,
3031 MultiProcedure<Resource> procedure) {
3033 processor.forEachObject(this, subject, relation, procedure);
3038 final public void forEachDirectPredicate(final Resource subject, final AsyncProcedure<Set<Resource>> procedure) {
3039 processor.forEachDirectPredicate(this, subject, procedure);
3043 final public void forEachDirectPredicate(final Resource subject, final SyncProcedure<Set<Resource>> procedure) {
3044 forEachDirectPredicate(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
3048 public void forEachDirectPredicate(Resource subject, Procedure<Set<Resource>> procedure) {
3049 forEachDirectPredicate(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
3053 final public void forObjectSet(final Resource subject,
3054 final Resource relation, final AsyncSetListener<Resource> procedure) {
3056 assert (subject != null);
3057 assert (relation != null);
3058 assert (procedure != null);
3060 processor.forObjectSet(this, subject, relation, procedure);
3065 final public void forObjectSet(final Resource subject,
3066 final Resource relation, final SyncSetListener<Resource> procedure) {
3068 assert (subject != null);
3069 assert (relation != null);
3070 assert (procedure != null);
3072 forObjectSet(subject, relation, new SyncToAsyncSetProcedure<Resource>(
3078 final public void forObjectSet(final Resource subject,
3079 final Resource relation, final SetListener<Resource> procedure) {
3081 assert (subject != null);
3082 assert (relation != null);
3083 assert (procedure != null);
3085 forObjectSet(subject, relation, new NoneToAsyncSetProcedure<Resource>(
3091 final public void forEachAssertedObject(final Resource subject,
3092 final Resource relation,
3093 final AsyncMultiProcedure<Resource> procedure) {
3095 assert (subject != null);
3096 assert (relation != null);
3097 assert (procedure != null);
3099 processor.forEachAssertedObject(this, subject, relation, procedure);
3104 public void forEachAssertedObject(Resource subject, Resource relation,
3105 SyncMultiProcedure<Resource> procedure) {
3107 assert (subject != null);
3108 assert (relation != null);
3109 assert (procedure != null);
3111 forEachAssertedObject(subject, relation,
3112 new SyncToAsyncMultiProcedure<Resource>(procedure));
3117 public void forEachAssertedObject(Resource subject, Resource relation,
3118 MultiProcedure<Resource> procedure) {
3120 assert (subject != null);
3121 assert (relation != null);
3122 assert (procedure != null);
3124 forEachAssertedObject(subject, relation,
3125 new NoneToAsyncMultiProcedure<Resource>(procedure));
3130 public void forAssertedObjectSet(Resource subject, Resource relation,
3131 AsyncSetListener<Resource> procedure) {
3133 assert (subject != null);
3134 assert (relation != null);
3135 assert (procedure != null);
3137 processor.forAssertedObjectSet(this, subject, relation, procedure);
3142 public void forAssertedObjectSet(Resource subject, Resource relation,
3143 SyncSetListener<Resource> procedure) {
3145 assert (subject != null);
3146 assert (relation != null);
3147 assert (procedure != null);
3149 forAssertedObjectSet(subject, relation,
3150 new SyncToAsyncSetProcedure<Resource>(procedure));
3155 public void forAssertedObjectSet(Resource subject, Resource relation,
3156 SetListener<Resource> procedure) {
3158 assert (subject != null);
3159 assert (relation != null);
3160 assert (procedure != null);
3162 forAssertedObjectSet(subject, relation,
3163 new NoneToAsyncSetProcedure<Resource>(procedure));
3168 public void forInverse(Resource relation, AsyncListener<Resource> listener) {
3169 asyncRequest(new Inverse(relation), listener);
3173 public void forInverse(Resource relation, SyncListener<Resource> listener) {
3174 asyncRequest(new Inverse(relation), listener);
3178 public void forInverse(Resource relation, Listener<Resource> listener) {
3179 asyncRequest(new Inverse(relation), listener);
3183 final public void forInverse(final Resource relation,
3184 final AsyncProcedure<Resource> procedure) {
3186 assert (relation != null);
3187 assert (procedure != null);
3189 processor.forInverse(this, relation, new AsyncProcedure<Resource>() {
3192 public void execute(AsyncReadGraph graph, Resource result) {
3194 procedure.execute(graph, result);
3196 procedure.exception(graph, new NoInverseException(relation
3202 public void exception(AsyncReadGraph graph, Throwable throwable) {
3203 procedure.exception(graph, throwable);
3207 public String toString() {
3208 return "forInverse -> " + procedure;
3216 public void forInverse(Resource relation, SyncProcedure<Resource> procedure) {
3217 forInverse(relation, new SyncToAsyncProcedure<Resource>(procedure));
3221 public void forInverse(Resource relation, Procedure<Resource> procedure) {
3222 forInverse(relation, new NoneToAsyncProcedure<Resource>(procedure));
3226 public void forSingleObject(Resource subject, Resource relation,
3227 AsyncListener<Resource> listener) {
3228 asyncRequest(new SingleObject(subject, relation), listener);
3232 public void forSingleObject(Resource subject, Resource relation,
3233 SyncListener<Resource> listener) {
3234 asyncRequest(new SingleObject(subject, relation), listener);
3238 public void forSingleObject(Resource subject, Resource relation,
3239 Listener<Resource> listener) {
3240 asyncRequest(new SingleObject(subject, relation), listener);
3244 final public void forSingleObject(final Resource subject,
3245 final Resource relation, final AsyncProcedure<Resource> procedure) {
3247 assert (subject != null);
3248 assert (relation != null);
3249 assert (procedure != null);
3251 processor.forEachObject(this, subject, relation,
3252 new SingleOrErrorProcedure<Resource>(procedure));
3257 public void forSingleObject(Resource subject, Resource relation,
3258 SyncProcedure<Resource> procedure) {
3259 forSingleObject(subject, relation, new SyncToAsyncProcedure<Resource>(
3264 public void forSingleObject(Resource subject, Resource relation,
3265 Procedure<Resource> procedure) {
3266 forSingleObject(subject, relation, new NoneToAsyncProcedure<Resource>(
3271 public void forSingleStatement(Resource subject, Resource relation,
3272 AsyncListener<Statement> listener) {
3273 asyncRequest(new SingleStatement(subject, relation), listener);
3277 public void forSingleStatement(Resource subject, Resource relation,
3278 SyncListener<Statement> listener) {
3279 asyncRequest(new SingleStatement(subject, relation), listener);
3283 public void forSingleStatement(Resource subject, Resource relation,
3284 Listener<Statement> listener) {
3285 asyncRequest(new SingleStatement(subject, relation), listener);
3289 final public void forSingleStatement(final Resource subject,
3290 final Resource relation, final AsyncProcedure<Statement> procedure) {
3292 assert (subject != null);
3293 assert (relation != null);
3294 assert (procedure != null);
3296 processor.forEachStatement(this, subject, relation,
3297 new SingleOrErrorProcedure<Statement>(procedure));
3302 public void forSingleStatement(Resource subject, Resource relation,
3303 SyncProcedure<Statement> procedure) {
3304 forSingleStatement(subject, relation,
3305 new SyncToAsyncProcedure<Statement>(procedure));
3309 public void forSingleStatement(Resource subject, Resource relation,
3310 Procedure<Statement> procedure) {
3311 forSingleStatement(subject, relation,
3312 new NoneToAsyncProcedure<Statement>(procedure));
3316 public void forSingleType(Resource subject,
3317 AsyncListener<Resource> listener) {
3318 asyncRequest(new SingleTypeAny(subject), listener);
3322 public void forSingleType(Resource subject,
3323 SyncListener<Resource> listener) {
3324 asyncRequest(new SingleTypeAny(subject), listener);
3328 public void forSingleType(Resource subject,
3329 Listener<Resource> listener) {
3330 asyncRequest(new SingleTypeAny(subject), listener);
3334 final public void forSingleType(final Resource subject, final AsyncProcedure<Resource> procedure) {
3336 assert (subject != null);
3337 assert (procedure != null);
3339 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3341 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
3344 public void execute(AsyncReadGraph graph, final Resource principalType) {
3345 checkedProcedure.offer(graph, principalType);
3349 public void finished(AsyncReadGraph graph) {
3350 checkedProcedure.dec(graph);
3354 public void exception(AsyncReadGraph graph, Throwable t) {
3355 checkedProcedure.exception(graph, t);
3359 public String toString() {
3360 return "forSingleType -> " + procedure;
3368 public void forSingleType(Resource subject, SyncProcedure<Resource> procedure) {
3369 forSingleType(subject, new SyncToAsyncProcedure<Resource>(
3374 public void forSingleType(Resource subject, Procedure<Resource> procedure) {
3375 forSingleType(subject, new NoneToAsyncProcedure<Resource>(
3380 public void forSingleType(Resource subject, Resource relation,
3381 AsyncListener<Resource> listener) {
3382 asyncRequest(new SingleType(subject, relation), listener);
3386 public void forSingleType(Resource subject, Resource relation,
3387 SyncListener<Resource> listener) {
3388 asyncRequest(new SingleType(subject, relation), listener);
3392 public void forSingleType(Resource subject, Resource relation,
3393 Listener<Resource> listener) {
3394 asyncRequest(new SingleType(subject, relation), listener);
3398 final public void forSingleType(final Resource subject,
3399 final Resource baseType, final AsyncProcedure<Resource> procedure) {
3401 assert (subject != null);
3402 assert (procedure != null);
3404 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3406 processor.forEachPrincipalType(this, subject,
3407 new AsyncMultiProcedureAdapter<Resource>() {
3410 public void execute(AsyncReadGraph graph,
3411 final Resource principalType) {
3413 checkedProcedure.inc();
3415 if(baseType == null) {
3417 checkedProcedure.offer(graph, principalType);
3418 checkedProcedure.dec(graph);
3420 } else if(principalType.equals(baseType)) {
3422 checkedProcedure.offer(graph, principalType);
3423 checkedProcedure.dec(graph);
3427 processor.forSupertypes((ReadGraphImpl)graph, principalType,
3428 new AsyncProcedure<Set<Resource>>() {
3431 public void execute(
3432 AsyncReadGraph graph,
3433 Set<Resource> result) {
3435 if (result.contains(baseType))
3436 checkedProcedure.offer(graph,
3438 checkedProcedure.dec(graph);
3443 public void exception(
3444 AsyncReadGraph graph,
3447 .exception(graph, t);
3457 public void finished(AsyncReadGraph graph) {
3458 checkedProcedure.dec(graph);
3462 public void exception(AsyncReadGraph graph, Throwable t) {
3463 checkedProcedure.exception(graph, t);
3467 public String toString() {
3468 return "forSingleType -> " + procedure;
3476 public void forSingleType(Resource subject, Resource relation,
3477 SyncProcedure<Resource> procedure) {
3478 forSingleType(subject, relation, new SyncToAsyncProcedure<Resource>(
3483 public void forSingleType(Resource subject, Resource relation,
3484 Procedure<Resource> procedure) {
3485 forSingleType(subject, relation, new NoneToAsyncProcedure<Resource>(
3490 public <T> void forValue(Resource subject, Binding binding,
3491 AsyncListener<T> listener) {
3492 asyncRequest(new Value<T>(subject, binding), listener);
3496 public <T> void forValue(Resource subject, Binding binding,
3497 SyncListener<T> listener) {
3498 asyncRequest(new Value<T>(subject, binding), listener);
3502 public <T> void forValue(Resource subject, Binding binding,
3503 Listener<T> listener) {
3504 asyncRequest(new Value<T>(subject, binding), listener);
3508 public <T> void forValue(final Resource resource, final Binding binding,
3509 final AsyncProcedure<T> procedure) {
3511 assert (resource != null);
3512 assert (binding != null);
3513 assert (procedure != null);
3515 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
3518 public void execute(AsyncReadGraph graph, byte[] result) {
3522 if (result == null) {
3523 procedure.exception(graph,
3524 new DoesNotContainValueException(
3525 "No value for resource " + resource));
3529 Serializer serializer = binding.serializer();
3530 // Serializer serializer = Bindings.getSerializer( binding );
3531 Object obj = serializer.deserialize(result);
3532 // if (!binding.isInstance(obj))
3533 // procedure.exception(graph, new ClassCastException(
3534 // "Cannot get value " + obj + " with binding "
3537 procedure.execute(graph, (T) obj);
3539 } catch (Throwable t) {
3540 procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
3545 public void exception(AsyncReadGraph graph, Throwable t) {
3547 procedure.exception(graph, t);
3548 } catch (Throwable t2) {
3549 Logger.defaultLogError(t2);
3554 public String toString() {
3555 return "forValue -> " + procedure;
3562 private static String safeArrayToString(byte[] a) {
3565 int iMax = a.length - 1;
3569 StringBuilder b = new StringBuilder();
3571 for (int i = 0; i < 100; i++) { // limit to first 100 items
3574 return b.append(']').toString();
3577 return b.append(", ... (" + a.length + ")]").toString();
3581 public <T> void forValue(Resource subject, Binding binding,
3582 SyncProcedure<T> procedure) {
3583 forValue(subject, binding, new SyncToAsyncProcedure<T>(procedure));
3587 public <T> void forValue(Resource subject, Binding binding,
3588 Procedure<T> procedure) {
3589 forValue(subject, binding, new NoneToAsyncProcedure<T>(procedure));
3593 public <T> void forValue(Resource subject, AsyncListener<T> listener) {
3594 asyncRequest(new ValueImplied<T>(subject), listener);
3598 public <T> void forValue(Resource subject, SyncListener<T> listener) {
3599 asyncRequest(new ValueImplied<T>(subject), listener);
3603 public <T> void forValue(Resource subject, Listener<T> listener) {
3604 asyncRequest(new ValueImplied<T>(subject), listener);
3608 final public <T> void forValue(final Resource subject, final AsyncProcedure<T> procedure) {
3610 assert (subject != null);
3611 assert (procedure != null);
3613 forRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
3616 public void execute(AsyncReadGraph graph, Datatype type) {
3617 // TODO: consider trying Bindings.getBeanBinding(type);
3618 Binding binding = Bindings.getBinding(type);
3619 graph.forValue(subject, binding, procedure);
3623 public void exception(AsyncReadGraph graph, Throwable throwable) {
3624 procedure.exception(graph, new DoesNotContainValueException("Invalid data type", throwable));
3632 public <T> void forValue(Resource subject, SyncProcedure<T> procedure) {
3633 forValue(subject, new SyncToAsyncProcedure<T>(procedure));
3637 public <T> void forValue(Resource subject, Procedure<T> procedure) {
3638 forValue(subject, new NoneToAsyncProcedure<T>(procedure));
3642 public <T> void forRelatedValue(Resource subject, Resource relation,
3643 AsyncListener<T> listener) {
3644 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3648 public <T> void forRelatedValue(Resource subject, Resource relation,
3649 SyncListener<T> listener) {
3650 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3654 public <T> void forRelatedValue(Resource subject, Resource relation,
3655 Listener<T> listener) {
3656 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3660 final public <T> void forRelatedValue(final Resource subject,
3661 final Resource relation, final AsyncProcedure<T> procedure) {
3663 assert (subject != null);
3664 assert (relation != null);
3665 assert (procedure != null);
3667 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3669 processor.forEachObject(this, subject, relation,
3670 new AsyncMultiProcedureAdapter<Resource>() {
3673 public void execute(AsyncReadGraph graph,
3674 final Resource object) {
3676 checkedProcedure.inc();
3678 graph.forValue(object, new AsyncProcedure<Object>() {
3681 public void execute(AsyncReadGraph graph,
3683 checkedProcedure.offer(graph, (T) result);
3684 checkedProcedure.dec(graph);
3688 public void exception(AsyncReadGraph graph,
3690 checkedProcedure.exception(graph, t);
3694 public String toString() {
3695 return "forRelatedValue -> " + procedure;
3703 public void finished(AsyncReadGraph graph) {
3704 checkedProcedure.dec(graph);
3708 public void exception(AsyncReadGraph graph, Throwable t) {
3709 checkedProcedure.exception(graph, t);
3717 public <T> void forRelatedValue(Resource subject, Resource relation,
3718 SyncProcedure<T> procedure) {
3719 forRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
3724 public <T> void forRelatedValue(Resource subject, Resource relation,
3725 Procedure<T> procedure) {
3726 forRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
3731 public <T> void forRelatedValue(Resource subject, Resource relation,
3732 Binding binding, AsyncListener<T> listener) {
3733 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3737 public <T> void forRelatedValue(Resource subject, Resource relation,
3738 Binding binding, SyncListener<T> listener) {
3739 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3743 public <T> void forRelatedValue(Resource subject, Resource relation,
3744 Binding binding, Listener<T> listener) {
3745 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3749 final public <T> void forRelatedValue(final Resource subject,
3750 final Resource relation, final Binding binding,
3751 final AsyncProcedure<T> procedure) {
3753 assert (subject != null);
3754 assert (relation != null);
3755 assert (binding != null);
3756 assert (procedure != null);
3758 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3760 processor.forEachObject(this, subject, relation,
3761 new AsyncMultiProcedureAdapter<Resource>() {
3764 public void execute(AsyncReadGraph graph,
3765 final Resource object) {
3767 checkedProcedure.inc();
3769 graph.forValue(object, binding, new AsyncProcedure<Object>() {
3772 public void execute(AsyncReadGraph graph,
3775 checkedProcedure.offer(graph,
3777 checkedProcedure.dec(graph);
3781 public void exception(AsyncReadGraph graph,
3783 checkedProcedure.exception(graph, t);
3787 public String toString() {
3788 return "forRelatedValue -> "
3797 public void finished(AsyncReadGraph graph) {
3798 checkedProcedure.dec(graph);
3802 public void exception(AsyncReadGraph graph, Throwable t) {
3803 checkedProcedure.exception(graph, t);
3811 public <T> void forRelatedValue(Resource subject, Resource relation,
3812 Binding binding, SyncProcedure<T> procedure) {
3813 forRelatedValue(subject, relation, binding,
3814 new SyncToAsyncProcedure<T>(procedure));
3818 public <T> void forRelatedValue(Resource subject, Resource relation,
3819 Binding binding, Procedure<T> procedure) {
3820 forRelatedValue(subject, relation, binding,
3821 new NoneToAsyncProcedure<T>(procedure));
3825 public <T> void forAdapted(Resource resource, Class<T> clazz,
3826 AsyncListener<T> listener) {
3827 asyncRequest(new Adapter<T>(resource, clazz), listener);
3831 public <T> void forAdapted(Resource resource, Class<T> clazz,
3832 SyncListener<T> listener) {
3833 asyncRequest(new Adapter<T>(resource, clazz), listener);
3837 public <T> void forAdapted(Resource resource, Class<T> clazz,
3838 Listener<T> listener) {
3839 asyncRequest(new Adapter<T>(resource, clazz), listener);
3843 final public <T> void forAdapted(final Resource resource,
3844 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3846 assert (resource != null);
3847 assert (clazz != null);
3848 assert (procedure != null);
3850 final AdaptionService service = getSession().peekService(AdaptionService.class);
3851 if (service == null)
3852 procedure.exception(this, new ServiceException("No AdaptionService available"));
3854 service.adapt(this, resource, resource, Resource.class, clazz, false, procedure);
3859 public <T> void forAdapted(Resource resource, Class<T> clazz,
3860 SyncProcedure<T> procedure) {
3861 forAdapted(resource, clazz, new SyncToAsyncProcedure<T>(procedure));
3865 public <T> void forAdapted(Resource resource, Class<T> clazz,
3866 Procedure<T> procedure) {
3867 forAdapted(resource, clazz, new NoneToAsyncProcedure<T>(procedure));
3871 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3872 AsyncListener<T> listener) {
3873 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3877 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3878 SyncListener<T> listener) {
3879 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3883 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3884 Listener<T> listener) {
3885 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3889 final public <T> void forUniqueAdapted(final Resource resource,
3890 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3892 assert (resource != null);
3893 assert (clazz != null);
3894 assert (procedure != null);
3896 final AdaptionService service = getSession().peekService(AdaptionService.class);
3897 if (service == null)
3898 procedure.exception(this, new ServiceException("No AdaptionService available"));
3900 service.adaptNew(this, resource, clazz, false, procedure);
3905 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3906 SyncProcedure<T> procedure) {
3907 forUniqueAdapted(resource, clazz,
3908 new SyncToAsyncProcedure<T>(procedure));
3912 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3913 Procedure<T> procedure) {
3914 forUniqueAdapted(resource, clazz,
3915 new NoneToAsyncProcedure<T>(procedure));
3919 public void forPossibleInverse(Resource subject,
3920 AsyncListener<Resource> listener) {
3921 asyncRequest(new PossibleInverse(subject), listener);
3925 public void forPossibleInverse(Resource subject,
3926 SyncListener<Resource> listener) {
3927 asyncRequest(new PossibleInverse(subject), listener);
3931 public void forPossibleInverse(Resource subject, Listener<Resource> listener) {
3932 asyncRequest(new PossibleInverse(subject), listener);
3936 final public void forPossibleInverse(final Resource relation,
3937 final AsyncProcedure<Resource> procedure) {
3939 assert (relation != null);
3940 assert (procedure != null);
3942 processor.forInverse(this, relation, new ExceptionToNullProcedure<Resource>(procedure));
3947 public void forPossibleInverse(Resource subject,
3948 SyncProcedure<Resource> procedure) {
3949 forPossibleInverse(subject, new SyncToAsyncProcedure<Resource>(
3954 public void forPossibleInverse(Resource subject,
3955 Procedure<Resource> procedure) {
3956 forPossibleInverse(subject, new NoneToAsyncProcedure<Resource>(
3961 public void forPossibleObject(Resource subject, Resource relation,
3962 AsyncListener<Resource> listener) {
3963 asyncRequest(new PossibleObject(subject, relation), listener);
3967 public void forPossibleObject(Resource subject, Resource relation,
3968 SyncListener<Resource> listener) {
3969 asyncRequest(new PossibleObject(subject, relation), listener);
3973 public void forPossibleObject(Resource subject, Resource relation,
3974 Listener<Resource> listener) {
3975 asyncRequest(new PossibleObject(subject, relation), listener);
3979 final public void forPossibleObject(final Resource subject,
3980 final Resource relation, final AsyncProcedure<Resource> procedure) {
3982 assert (subject != null);
3983 assert (relation != null);
3984 assert (procedure != null);
3986 processor.forEachObject(this, subject, relation,
3987 new SingleOrNullProcedure<Resource>(procedure));
3992 public void forPossibleObject(Resource subject, Resource relation,
3993 SyncProcedure<Resource> procedure) {
3994 forPossibleObject(subject, relation,
3995 new SyncToAsyncProcedure<Resource>(procedure));
3999 public void forPossibleObject(Resource subject, Resource relation,
4000 Procedure<Resource> procedure) {
4001 forPossibleObject(subject, relation,
4002 new NoneToAsyncProcedure<Resource>(procedure));
4006 public void forPossibleStatement(Resource subject, Resource relation,
4007 AsyncListener<Statement> listener) {
4008 asyncRequest(new PossibleStatement(subject, relation), listener);
4012 public void forPossibleStatement(Resource subject, Resource relation,
4013 SyncListener<Statement> listener) {
4014 asyncRequest(new PossibleStatement(subject, relation), listener);
4018 public void forPossibleStatement(Resource subject, Resource relation,
4019 Listener<Statement> listener) {
4020 asyncRequest(new PossibleStatement(subject, relation), listener);
4024 final public void forPossibleStatement(final Resource subject,
4025 final Resource relation, final AsyncProcedure<Statement> procedure) {
4027 assert (subject != null);
4028 assert (relation != null);
4029 assert (procedure != null);
4031 processor.forEachStatement(this, subject, relation,
4032 new SingleFunctionalOrNullProcedure<Statement>(
4033 "forPossibleStatement", procedure));
4038 public void forPossibleStatement(Resource subject, Resource relation,
4039 SyncProcedure<Statement> procedure) {
4040 forPossibleStatement(subject, relation,
4041 new SyncToAsyncProcedure<Statement>(procedure));
4045 public void forPossibleStatement(Resource subject, Resource relation,
4046 Procedure<Statement> procedure) {
4047 forPossibleStatement(subject, relation,
4048 new NoneToAsyncProcedure<Statement>(procedure));
4052 public void forPossibleType(Resource subject, Resource relation,
4053 AsyncListener<Resource> listener) {
4054 asyncRequest(new PossibleType(subject, relation), listener);
4058 public void forPossibleType(Resource subject, Resource relation,
4059 SyncListener<Resource> listener) {
4060 asyncRequest(new PossibleType(subject, relation), listener);
4064 public void forPossibleType(Resource subject, Resource relation,
4065 Listener<Resource> listener) {
4066 asyncRequest(new PossibleType(subject, relation), listener);
4070 final public void forPossibleType(final Resource subject,
4071 final Resource baseType, final AsyncProcedure<Resource> procedure) {
4073 assert (subject != null);
4074 assert (procedure != null);
4076 final NullSingleOrNullProcedure<Resource> checkedProcedure = new NullSingleOrNullProcedure<Resource>(procedure);
4078 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
4081 public void execute(AsyncReadGraph graph,
4082 final Resource principalType) {
4084 if (baseType == null) {
4086 checkedProcedure.offer(graph, principalType);
4088 } else if (principalType.equals(baseType)) {
4090 checkedProcedure.offer(graph, principalType);
4094 checkedProcedure.inc();
4096 processor.forSupertypes((ReadGraphImpl)graph, principalType,
4097 new AsyncProcedure<Set<Resource>>() {
4100 public void execute(
4101 AsyncReadGraph graph,
4102 Set<Resource> result) {
4104 if (result.contains(baseType)) {
4105 checkedProcedure.offer(graph,
4109 checkedProcedure.dec(graph);
4114 public void exception(
4115 AsyncReadGraph graph,
4117 checkedProcedure.exception(graph, t);
4118 checkedProcedure.dec(graph);
4122 public String toString() {
4123 return "forPossibleType -> "
4134 public void finished(AsyncReadGraph graph) {
4135 checkedProcedure.dec(graph);
4139 public void exception(AsyncReadGraph graph, Throwable t) {
4140 checkedProcedure.exception(graph, t);
4141 checkedProcedure.dec(graph);
4149 public void forPossibleType(Resource subject, Resource relation,
4150 SyncProcedure<Resource> procedure) {
4151 forPossibleType(subject, relation, new SyncToAsyncProcedure<Resource>(
4156 public void forPossibleType(Resource subject, Resource relation,
4157 Procedure<Resource> procedure) {
4158 forPossibleType(subject, relation, new NoneToAsyncProcedure<Resource>(
4163 public <T> void forPossibleValue(Resource subject, AsyncListener<T> listener) {
4164 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4168 public <T> void forPossibleValue(Resource subject, SyncListener<T> listener) {
4169 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4173 public <T> void forPossibleValue(Resource subject, Listener<T> listener) {
4174 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4178 final public <T> void forPossibleValue(final Resource subject,
4179 final AsyncProcedure<T> procedure) {
4181 assert (subject != null);
4182 assert (procedure != null);
4184 forPossibleRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
4187 public void execute(AsyncReadGraph graph, final Datatype type) {
4189 procedure.execute(graph, null);
4192 // TODO: consider trying Bindings.getBeanBinding(type);
4193 Binding binding = Bindings.getBinding(type);
4194 graph.forPossibleValue(subject, binding, procedure);
4195 } catch (RuntimeBindingConstructionException e) {
4196 procedure.exception(graph, e);
4202 public void exception(AsyncReadGraph graph, Throwable t) {
4203 procedure.exception(graph, t);
4207 public String toString() {
4208 return "forPossibleValue -> " + procedure;
4216 public <T> void forPossibleValue(Resource subject,
4217 SyncProcedure<T> procedure) {
4218 forPossibleValue(subject, new SyncToAsyncProcedure<T>(procedure));
4222 public <T> void forPossibleValue(Resource subject, Procedure<T> procedure) {
4223 forPossibleValue(subject, new NoneToAsyncProcedure<T>(procedure));
4227 public <T> void forPossibleValue(Resource subject, Binding binding,
4228 AsyncListener<T> listener) {
4229 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4233 public <T> void forPossibleValue(Resource subject, Binding binding,
4234 SyncListener<T> listener) {
4235 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4239 public <T> void forPossibleValue(Resource subject, Binding binding,
4240 Listener<T> listener) {
4241 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4245 final public <T> void forPossibleValue(final Resource resource,
4246 final Binding binding, final AsyncProcedure<T> procedure) {
4248 assert (resource != null);
4249 assert (binding != null);
4250 assert (procedure != null);
4252 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
4255 public void execute(AsyncReadGraph graph, byte[] result) {
4259 if (result == null) {
4260 procedure.execute(graph, null);
4264 Serializer serializer = Bindings.getSerializer( binding );
4265 Object obj = serializer.deserialize(result);
4266 if (!binding.isInstance(obj))
4267 procedure.exception(graph, new ClassCastException(
4268 "Cannot get value " + obj + " with binding "
4271 procedure.execute(graph, (T) obj);
4273 } catch (Throwable t) {
4274 procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
4279 public void exception(AsyncReadGraph graph, Throwable t) {
4281 procedure.exception(graph, t);
4282 } catch (Throwable t2) {
4283 Logger.defaultLogError(t2);
4288 public String toString() {
4289 return "forPossibleValue -> " + procedure;
4297 public <T> void forPossibleValue(Resource subject, Binding binding,
4298 SyncProcedure<T> procedure) {
4299 forPossibleValue(subject, binding, new SyncToAsyncProcedure<T>(
4304 public <T> void forPossibleValue(Resource subject, Binding binding,
4305 Procedure<T> procedure) {
4306 forPossibleValue(subject, binding, new NoneToAsyncProcedure<T>(
4311 public <T> void forPossibleRelatedValue(Resource subject,
4312 Resource relation, AsyncListener<T> listener) {
4313 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4318 public <T> void forPossibleRelatedValue(Resource subject,
4319 Resource relation, SyncListener<T> listener) {
4320 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4325 public <T> void forPossibleRelatedValue(Resource subject,
4326 Resource relation, Listener<T> listener) {
4327 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4332 final public <T> void forPossibleRelatedValue(final Resource subject,
4333 final Resource relation, final AsyncProcedure<T> procedure) {
4335 assert (subject != null);
4336 assert (relation != null);
4337 assert (procedure != null);
4339 final DeepSingleOrNullProcedure<T> checkedProcedure = new DeepSingleOrNullProcedure<T>(procedure);
4341 processor.forEachObject(this, subject, relation,
4342 new AsyncMultiProcedureAdapter<Resource>() {
4345 public void execute(AsyncReadGraph graph,
4346 final Resource object) {
4348 checkedProcedure.inc();
4350 graph.forValue(object, new AsyncProcedure<Object>() {
4353 public void execute(AsyncReadGraph graph,
4355 checkedProcedure.offer(graph, (T) result);
4356 checkedProcedure.dec(graph);
4360 public void exception(AsyncReadGraph graph,
4362 checkedProcedure.exception(graph, t);
4363 checkedProcedure.dec(graph);
4371 public void finished(AsyncReadGraph graph) {
4373 checkedProcedure.dec(graph);
4377 public void exception(AsyncReadGraph graph, Throwable t) {
4378 checkedProcedure.exception(graph, t);
4379 checkedProcedure.dec(graph);
4383 public String toString() {
4384 return "forPossibleRelatedValue -> " + procedure;
4391 public <T> void forPossibleRelatedValue(Resource subject,
4392 Resource relation, SyncProcedure<T> procedure) {
4393 forPossibleRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
4398 public <T> void forPossibleRelatedValue(Resource subject,
4399 Resource relation, Procedure<T> procedure) {
4400 forPossibleRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
4405 public <T> void forPossibleRelatedValue(Resource subject,
4406 Resource relation, Binding binding, AsyncListener<T> listener) {
4407 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4412 public <T> void forPossibleRelatedValue(Resource subject,
4413 Resource relation, Binding binding, SyncListener<T> listener) {
4414 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4419 public <T> void forPossibleRelatedValue(Resource subject,
4420 Resource relation, Binding binding, Listener<T> listener) {
4421 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4425 final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4426 final AsyncProcedure<T> procedure) {
4428 assert (subject != null);
4429 assert (relation != null);
4430 assert (procedure != null);
4432 processor.forPossibleObject(this, subject, relation, new AsyncProcedure<Resource>() {
4435 public void execute(AsyncReadGraph graph, Resource object) {
4437 if(object == null) {
4438 procedure.execute(graph, null);
4442 processor.forPossibleValue((ReadGraphImpl)graph, object, new AsyncProcedure<byte[]>() {
4445 public void execute(AsyncReadGraph graph, byte[] bytes) {
4451 Serializer serializer = binding.serializer();
4452 Object obj = serializer.deserialize(bytes);
4453 if (!binding.isInstance(obj)) {
4454 procedure.exception(graph, new ClassCastException("Cannot get value " + obj + " with binding " + binding));
4456 procedure.execute(graph, (T) obj);
4459 } catch (Throwable t) {
4461 procedure.exception(graph, t);
4467 procedure.execute(graph, null);
4474 public void exception(AsyncReadGraph graph, Throwable t) {
4475 procedure.exception(graph, t);
4483 public void exception(AsyncReadGraph graph, Throwable throwable) {
4484 throwable.printStackTrace();
4485 procedure.exception(graph, throwable);
4493 public <T> void forPossibleRelatedValue(Resource subject,
4494 Resource relation, Binding binding, SyncProcedure<T> procedure) {
4495 forPossibleRelatedValue(subject, relation, binding,
4496 new SyncToAsyncProcedure<T>(procedure));
4500 public <T> void forPossibleRelatedValue(Resource subject,
4501 Resource relation, Binding binding, Procedure<T> procedure) {
4502 forPossibleRelatedValue(subject, relation, binding,
4503 new NoneToAsyncProcedure<T>(procedure));
4507 public void forIsInstanceOf(Resource subject, Resource relation,
4508 AsyncListener<Boolean> listener) {
4509 asyncRequest(new IsInstanceOf(subject, relation), listener);
4513 public void forIsInstanceOf(Resource subject, Resource relation,
4514 SyncListener<Boolean> listener) {
4515 asyncRequest(new IsInstanceOf(subject, relation), listener);
4519 public void forIsInstanceOf(Resource subject, Resource relation,
4520 Listener<Boolean> listener) {
4521 asyncRequest(new IsInstanceOf(subject, relation), listener);
4525 final public void forIsInstanceOf(final Resource resource,
4526 final Resource type, final AsyncProcedure<Boolean> procedure) {
4528 assert (resource != null);
4529 assert (type != null);
4530 assert (procedure != null);
4532 forTypes(resource, new AsyncProcedure<Set<Resource>>() {
4535 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4538 if (result.contains(type))
4539 procedure.execute(graph, true);
4541 procedure.execute(graph, false);
4542 } catch (Throwable t) {
4543 Logger.defaultLogError(t);
4548 public void exception(AsyncReadGraph graph, Throwable t) {
4550 procedure.exception(graph, t);
4551 } catch (Throwable t2) {
4552 Logger.defaultLogError(t2);
4557 public String toString() {
4558 return "forIsInstanceOf -> " + procedure;
4566 public void forIsInstanceOf(Resource subject, Resource relation,
4567 SyncProcedure<Boolean> procedure) {
4568 forIsInstanceOf(subject, relation, new SyncToAsyncProcedure<Boolean>(
4573 public void forIsInstanceOf(Resource subject, Resource relation,
4574 Procedure<Boolean> procedure) {
4575 forIsInstanceOf(subject, relation, new NoneToAsyncProcedure<Boolean>(
4580 public void forIsInheritedFrom(Resource subject, Resource relation,
4581 AsyncListener<Boolean> listener) {
4582 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4586 public void forIsInheritedFrom(Resource subject, Resource relation,
4587 SyncListener<Boolean> listener) {
4588 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4592 public void forIsInheritedFrom(Resource subject, Resource relation,
4593 Listener<Boolean> listener) {
4594 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4598 final public void forIsInheritedFrom(final Resource resource,
4599 final Resource type, final AsyncProcedure<Boolean> procedure) {
4601 assert (resource != null);
4602 assert (type != null);
4603 assert (procedure != null);
4605 if (resource.equals(type)) {
4607 procedure.execute(this, true);
4608 } catch (Throwable t) {
4609 Logger.defaultLogError(t);
4614 forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
4617 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4619 if (result.contains(type))
4620 procedure.execute(graph, true);
4622 procedure.execute(graph, false);
4623 } catch (Throwable t) {
4624 Logger.defaultLogError(t);
4629 public void exception(AsyncReadGraph graph, Throwable t) {
4631 procedure.exception(graph, t);
4632 } catch (Throwable t2) {
4633 Logger.defaultLogError(t2);
4638 public String toString() {
4639 return "forIsInheritedFrom -> " + procedure;
4647 public void forIsInheritedFrom(Resource subject, Resource relation,
4648 SyncProcedure<Boolean> procedure) {
4649 forIsInheritedFrom(subject, relation,
4650 new SyncToAsyncProcedure<Boolean>(procedure));
4654 public void forIsInheritedFrom(Resource subject, Resource relation,
4655 Procedure<Boolean> procedure) {
4656 forIsInheritedFrom(subject, relation,
4657 new NoneToAsyncProcedure<Boolean>(procedure));
4661 public void forIsSubrelationOf(Resource subject, Resource relation,
4662 AsyncListener<Boolean> listener) {
4663 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4667 public void forIsSubrelationOf(Resource subject, Resource relation,
4668 SyncListener<Boolean> listener) {
4669 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4673 public void forIsSubrelationOf(Resource subject, Resource relation,
4674 Listener<Boolean> listener) {
4675 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4679 final public void forIsSubrelationOf(final Resource resource,
4680 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4682 assert (resource != null);
4683 assert (relation != null);
4684 assert (procedure != null);
4686 if (resource.equals(relation)) {
4687 procedure.execute(this, true);
4691 forSuperrelations(resource, new AsyncProcedure<Set<Resource>>() {
4694 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4696 if (result.contains(relation))
4697 procedure.execute(graph, true);
4699 procedure.execute(graph, false);
4700 } catch (Throwable t) {
4701 Logger.defaultLogError(t);
4706 public void exception(AsyncReadGraph graph, Throwable t) {
4708 procedure.exception(graph, t);
4709 } catch (Throwable t2) {
4710 Logger.defaultLogError(t2);
4715 public String toString() {
4716 return "forIsSubrelationOf -> " + procedure;
4724 public void forIsSubrelationOf(Resource subject, Resource relation,
4725 SyncProcedure<Boolean> procedure) {
4726 forIsSubrelationOf(subject, relation,
4727 new SyncToAsyncProcedure<Boolean>(procedure));
4731 public void forIsSubrelationOf(Resource subject, Resource relation,
4732 Procedure<Boolean> procedure) {
4733 forIsSubrelationOf(subject, relation,
4734 new NoneToAsyncProcedure<Boolean>(procedure));
4738 public void forHasStatement(Resource subject,
4739 AsyncListener<Boolean> listener) {
4740 asyncRequest(new HasStatementSubject(subject), listener);
4744 public void forHasStatement(Resource subject, SyncListener<Boolean> listener) {
4745 asyncRequest(new HasStatementSubject(subject), listener);
4749 public void forHasStatement(Resource subject, Listener<Boolean> listener) {
4750 asyncRequest(new HasStatementSubject(subject), listener);
4754 final public void forHasStatement(final Resource subject,
4755 final AsyncProcedure<Boolean> procedure) {
4757 assert (subject != null);
4758 assert (procedure != null);
4760 processor.forHasStatement(this, subject, procedure);
4765 public void forHasStatement(Resource subject,
4766 SyncProcedure<Boolean> procedure) {
4767 forHasStatement(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4771 public void forHasStatement(Resource subject, Procedure<Boolean> procedure) {
4772 forHasStatement(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4776 public void forHasStatement(Resource subject, Resource relation,
4777 AsyncListener<Boolean> listener) {
4778 asyncRequest(new HasStatement(subject, relation), listener);
4782 public void forHasStatement(Resource subject, Resource relation,
4783 SyncListener<Boolean> listener) {
4784 asyncRequest(new HasStatement(subject, relation), listener);
4788 public void forHasStatement(Resource subject, Resource relation,
4789 Listener<Boolean> listener) {
4790 asyncRequest(new HasStatement(subject, relation), listener);
4794 final public void forHasStatement(final Resource subject,
4795 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4797 assert (subject != null);
4798 assert (relation != null);
4799 assert (procedure != null);
4801 processor.forHasStatement(this, subject, relation, procedure);
4806 public void forHasStatement(Resource subject, Resource relation,
4807 SyncProcedure<Boolean> procedure) {
4808 forHasStatement(subject, relation, new SyncToAsyncProcedure<Boolean>(
4813 public void forHasStatement(Resource subject, Resource relation,
4814 Procedure<Boolean> procedure) {
4815 forHasStatement(subject, relation, new NoneToAsyncProcedure<Boolean>(
4820 public void forHasStatement(Resource subject, Resource relation,
4821 Resource object, AsyncListener<Boolean> listener) {
4822 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4827 public void forHasStatement(Resource subject, Resource relation,
4828 Resource object, SyncListener<Boolean> listener) {
4829 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4834 public void forHasStatement(Resource subject, Resource relation,
4835 Resource object, Listener<Boolean> listener) {
4836 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4841 final public void forHasStatement(final Resource subject,
4842 final Resource relation, final Resource object,
4843 final AsyncProcedure<Boolean> procedure) {
4845 assert (subject != null);
4846 assert (relation != null);
4847 assert (object != null);
4848 assert (procedure != null);
4850 processor.forHasStatement(this, subject, relation, object, procedure);
4855 public void forHasStatement(Resource subject, Resource relation,
4856 Resource object, SyncProcedure<Boolean> procedure) {
4857 forHasStatement(subject, relation, object,
4858 new SyncToAsyncProcedure<Boolean>(procedure));
4862 public void forHasStatement(Resource subject, Resource relation,
4863 Resource object, Procedure<Boolean> procedure) {
4864 forHasStatement(subject, relation, object,
4865 new NoneToAsyncProcedure<Boolean>(procedure));
4869 public void forHasValue(Resource subject, AsyncListener<Boolean> listener) {
4870 asyncRequest(new HasValue(subject), listener);
4874 public void forHasValue(Resource subject, SyncListener<Boolean> listener) {
4875 asyncRequest(new HasValue(subject), listener);
4879 public void forHasValue(Resource subject, Listener<Boolean> listener) {
4880 asyncRequest(new HasValue(subject), listener);
4884 final public void forHasValue(final Resource subject,
4885 final AsyncProcedure<Boolean> procedure) {
4887 assert (subject != null);
4888 assert (procedure != null);
4890 processor.forValue(this, subject, new AsyncProcedure<byte[]>() {
4893 public void execute(AsyncReadGraph graph, byte[] result) {
4896 procedure.execute(graph, false);
4898 procedure.execute(graph, true);
4899 } catch (Throwable t) {
4900 Logger.defaultLogError(t);
4905 public void exception(AsyncReadGraph graph, Throwable t) {
4907 procedure.exception(graph, t);
4908 } catch (Throwable t2) {
4909 Logger.defaultLogError(t2);
4914 public String toString() {
4915 return "forHasValue -> " + procedure;
4923 public void forHasValue(Resource subject, SyncProcedure<Boolean> procedure) {
4924 forHasValue(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4928 public void forHasValue(Resource subject, Procedure<Boolean> procedure) {
4929 forHasValue(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4933 public void forOrderedSet(Resource subject,
4934 AsyncMultiListener<Resource> listener) {
4935 asyncRequest(new OrderedSet(subject), listener);
4939 public void forOrderedSet(Resource subject,
4940 SyncMultiListener<Resource> listener) {
4941 asyncRequest(new OrderedSet(subject), listener);
4945 public void forOrderedSet(Resource subject, MultiListener<Resource> listener) {
4946 asyncRequest(new OrderedSet(subject), listener);
4950 final public void forOrderedSet(final Resource subject,
4951 final AsyncMultiProcedure<Resource> procedure) {
4953 assert (subject != null);
4954 assert (procedure != null);
4956 processor.forOrderedSet(this, subject,
4957 new AsyncMultiProcedure<Resource>() {
4960 public void finished(AsyncReadGraph graph) {
4962 procedure.finished(graph);
4963 } catch (Throwable t) {
4964 Logger.defaultLogError(t);
4969 public void execute(AsyncReadGraph graph, Resource result) {
4971 procedure.execute(graph, result);
4972 } catch (Throwable t) {
4973 Logger.defaultLogError(t);
4978 public void exception(AsyncReadGraph graph, Throwable t) {
4980 procedure.exception(graph, t);
4981 } catch (Throwable t2) {
4982 Logger.defaultLogError(t2);
4987 public String toString() {
4988 return "forOrderedSet -> " + procedure;
4996 public void forOrderedSet(Resource subject,
4997 SyncMultiProcedure<Resource> procedure) {
4998 forOrderedSet(subject, new SyncToAsyncMultiProcedure<Resource>(
5003 public void forOrderedSet(Resource subject,
5004 MultiProcedure<Resource> procedure) {
5005 forOrderedSet(subject, new NoneToAsyncMultiProcedure<Resource>(
5010 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5011 AsyncListener<T> listener) {
5012 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5016 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5017 SyncListener<T> listener) {
5018 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5022 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5023 Listener<T> listener) {
5024 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5028 final public <T> void forPossibleAdapted(final Resource resource,
5029 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5031 assert (resource != null);
5032 assert (clazz != null);
5033 assert (procedure != null);
5035 final AdaptionService service = getSession().peekService(AdaptionService.class);
5036 if (service == null)
5037 procedure.exception(this, new ServiceException("No AdaptionService available"));
5039 service.adapt(this, resource, resource, Resource.class, clazz, true, procedure);
5043 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5044 SyncProcedure<T> procedure) {
5045 forPossibleAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5050 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5051 Procedure<T> procedure) {
5052 forPossibleAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5057 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5058 AsyncListener<T> listener) {
5059 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5063 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5064 SyncListener<T> listener) {
5065 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5069 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5070 Listener<T> listener) {
5071 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5075 final public <T> void forPossibleUniqueAdapted(final Resource resource,
5076 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5078 assert (resource != null);
5079 assert (clazz != null);
5080 assert (procedure != null);
5082 final AdaptionService service = getSession().peekService(AdaptionService.class);
5083 if (service == null)
5084 procedure.exception(this, new ServiceException("No AdaptionService available"));
5086 service.adaptNew(this, resource, clazz, true, procedure);
5091 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5092 SyncProcedure<T> procedure) {
5093 forPossibleUniqueAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5098 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5099 Procedure<T> procedure) {
5100 forPossibleUniqueAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5105 * Implementation of the interface AsyncRequestProcessor
5109 final public Session getSession() {
5110 return processor.getSession();
5114 public <T> void asyncRequest(final Read<T> request) {
5116 asyncRequest(request, new AsyncProcedure<T>() {
5119 public void execute(AsyncReadGraph graph, T result) {
5123 public void exception(AsyncReadGraph graph, Throwable t) {
5124 Logger.defaultLogError(t);
5128 public String toString() {
5129 return "asyncRequest(Read) -> " + request;
5137 public <T> void asyncRequest(Read<T> request, AsyncListener<T> procedure) {
5138 asyncRequest(request, (AsyncProcedure<T>) procedure);
5142 public <T> void asyncRequest(Read<T> request,
5143 final SyncListener<T> procedure) {
5144 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5148 public <T> void asyncRequest(Read<T> request, final Listener<T> procedure) {
5149 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5153 public <T> void asyncRequest(final Read<T> request, final AsyncProcedure<T> procedure) {
5155 assert (request != null);
5156 assert (procedure != null);
5158 AsyncBarrierImpl barrier = asyncBarrier;
5162 processor.scheduleNow(new SessionTask(this) {
5165 public void run0(int thread) {
5167 final ListenerBase listener = getListenerBase(procedure);
5168 QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5169 } catch (DatabaseException e) {
5170 Logger.defaultLogError(e);
5181 public static ReadGraphImpl createAsync(QueryProcessor support) {
5182 return new ReadGraphImpl(null, null, support);
5186 public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
5187 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5191 public <T> void asyncRequest(Read<T> request, final Procedure<T> procedure) {
5192 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5196 final public <T> void asyncRequest(final AsyncRead<T> request) {
5198 assert (request != null);
5200 asyncRequest(request, new AsyncProcedure<T>() {
5203 public void execute(AsyncReadGraph graph, T result) {
5207 public void exception(AsyncReadGraph graph, Throwable t) {
5208 Logger.defaultLogError(t);
5212 public String toString() {
5213 return "asyncRequest(AsyncRead) -> " + request;
5221 public <T> void asyncRequest(AsyncRead<T> request,
5222 AsyncListener<T> procedure) {
5223 asyncRequest(request, (AsyncProcedure<T>) procedure);
5227 final public <T> void asyncRequest(AsyncRead<T> request,
5228 final SyncListener<T> procedure) {
5229 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5233 final public <T> void asyncRequest(AsyncRead<T> request,
5234 final Listener<T> procedure) {
5235 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5239 final public <T> void asyncRequest(final AsyncRead<T> request,
5240 final AsyncProcedure<T> procedure) {
5242 assert (request != null);
5243 assert (procedure != null);
5245 AsyncBarrierImpl barrier = asyncBarrier;
5249 processor.scheduleNow(new SessionTask(this) {
5252 public void run0(int thread) {
5258 final ListenerBase listener = getListenerBase(procedure);
5259 QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, new AsyncProcedure<T>() {
5262 public void execute(AsyncReadGraph graph, T result) {
5263 procedure.execute(graph, result);
5269 public void exception(AsyncReadGraph graph, Throwable throwable) {
5270 procedure.exception(graph, throwable);
5278 } catch (DatabaseException e) {
5279 Logger.defaultLogError(e);
5288 public <T> void asyncRequest(AsyncRead<T> request,
5289 SyncProcedure<T> procedure) {
5290 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5294 final public <T> void asyncRequest(final AsyncRead<T> request,
5295 final Procedure<T> procedure) {
5296 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5300 public <T> void asyncRequest(final MultiRead<T> request) {
5302 assert (request != null);
5304 asyncRequest(request, new SyncMultiProcedureAdapter<T>() {
5306 public void exception(ReadGraph graph, Throwable t) {
5307 Logger.defaultLogError(t);
5311 public String toString() {
5312 return "asyncRequest(MultiRead) -> " + request;
5319 public <T> void asyncRequest(MultiRead<T> request,
5320 SyncMultiListener<T> procedure) {
5321 asyncRequest(request, (SyncMultiProcedure<T>)procedure);
5325 public <T> void asyncRequest(MultiRead<T> request,
5326 MultiListener<T> procedure) {
5327 asyncRequest(request, new NoneToSyncMultiListener<T>(procedure));
5332 public <T> void asyncRequest(final MultiRead<T> request,
5333 final SyncMultiProcedure<T> procedure) {
5335 assert (request != null);
5336 assert (procedure != null);
5338 final ListenerBase listener = getListenerBase(procedure);
5340 if (parent != null || listener != null) {
5342 // final ReadGraphImpl newGraph = newSync();
5343 processor.query(this, request, parent, procedure,listener);
5347 // final ReadGraphImpl newGraph = newSync();
5351 request.perform(this, procedure);
5353 } catch (Throwable t) {
5356 procedure.exception(this, t);
5357 } catch (DatabaseException e) {
5358 LOGGER.error("Unexpected exception while handling exception", e);
5368 public <T> void asyncRequest(MultiRead<T> request,
5369 MultiProcedure<T> procedure) {
5370 asyncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
5374 final public <T> void asyncRequest(final AsyncMultiRead<T> request) {
5376 assert (request != null);
5378 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5380 public void exception(AsyncReadGraph graph, Throwable t) {
5381 Logger.defaultLogError(t);
5385 public String toString() {
5386 return "asyncRequest(AsyncMultiRead) -> " + request;
5393 public <T> void asyncRequest(AsyncMultiRead<T> request,
5394 AsyncMultiListener<T> procedure) {
5395 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5399 public <T> void asyncRequest(AsyncMultiRead<T> request,
5400 SyncMultiListener<T> procedure) {
5401 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5405 public <T> void asyncRequest(AsyncMultiRead<T> request,
5406 MultiListener<T> procedure) {
5407 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5411 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5412 final AsyncMultiProcedure<T> procedure) {
5414 assert (request != null);
5415 assert (procedure != null);
5417 ListenerBase listener = getListenerBase(procedure);
5419 if (parent != null || listener != null) {
5421 processor.query(this, request, parent, procedure, listener);
5427 request.perform(this, new AsyncMultiProcedure<T>() {
5430 public void execute(AsyncReadGraph graph, T result) {
5431 procedure.execute(graph, result);
5435 public void finished(AsyncReadGraph graph) {
5436 procedure.finished(graph);
5440 public void exception(AsyncReadGraph graph, Throwable t) {
5441 procedure.exception(graph, t);
5445 public String toString() {
5446 return "asyncRequest(AsyncMultiRead) -> " + procedure;
5451 } catch (Throwable t) {
5453 procedure.exception(this, new DatabaseException(t));
5461 public <T> void asyncRequest(AsyncMultiRead<T> request,
5462 SyncMultiProcedure<T> procedure) {
5463 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5467 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5468 final MultiProcedure<T> procedure) {
5469 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5473 final public <T> void asyncRequest(final ExternalRead<T> request) {
5475 assert (request != null);
5477 asyncRequest(request, new Procedure<T>() {
5480 public void execute(T result) {
5484 public void exception(Throwable t) {
5485 Logger.defaultLogError(t);
5489 public String toString() {
5490 return "asyncRequest(PrimitiveRead) -> " + request;
5498 public <T> void asyncRequest(ExternalRead<T> request,
5499 final Listener<T> procedure) {
5500 asyncRequest(request, (Procedure<T>) procedure);
5504 final public <T> void asyncRequest(final ExternalRead<T> request,
5505 final Procedure<T> procedure) {
5507 assert (request != null);
5508 assert (procedure != null);
5510 final ListenerBase listener = getListenerBase(procedure);
5512 if (parent != null || listener != null) {
5515 QueryCacheBase.resultExternalReadEntry(this, request, parent, listener, procedure);
5516 } catch (DatabaseException e) {
5517 Logger.defaultLogError(e);
5518 // This throwable has already been transferred to procedure at this point - do nothing about it
5523 request.register(this, new Listener<T>() {
5525 public void execute(T result) {
5526 procedure.execute(result);
5530 public void exception(Throwable t) {
5531 procedure.exception(t);
5535 public String toString() {
5536 return "asyncRequest(PrimitiveRead) -> " + request;
5540 public boolean isDisposed() {
5551 public void asyncRequest(final Write request) {
5553 assert (request != null);
5555 getSession().asyncRequest(request);
5557 // processor.asyncWrite(request);
5562 public <T> void asyncRequest(final WriteResult<T> request, Procedure<T> procedure) {
5563 throw new Error("Not implemented.");
5567 public void asyncRequest(Write request, Consumer<DatabaseException> callback) {
5569 assert (request != null);
5571 getSession().asyncRequest(request, callback);
5576 public void asyncRequest(final DelayedWrite request) {
5578 assert (request != null);
5580 getSession().asyncRequest(request);
5585 public <T> void asyncRequest(final DelayedWriteResult<T> request, Procedure<T> procedure) {
5586 throw new Error("Not implemented.");
5590 public void asyncRequest(DelayedWrite r,
5591 Consumer<DatabaseException> callback) {
5592 throw new Error("Not implemented.");
5596 public void asyncRequest(final WriteOnly request) {
5598 assert (request != null);
5600 getSession().asyncRequest(request);
5605 public <T> void asyncRequest(final WriteOnlyResult<T> request, Procedure<T> procedure) {
5606 throw new Error("Not implemented.");
5610 public void asyncRequest(WriteOnly r, Consumer<DatabaseException> callback) {
5611 throw new Error("Not implemented.");
5615 * Implementation of the interface ServiceLocator
5619 public <T> T getService(Class<T> api) {
5620 if(WriteSupport.class == api) {
5621 if(this instanceof WriteGraphImpl) {
5622 WriteGraphImpl impl = (WriteGraphImpl)this;
5623 return (T)impl.writeSupport;
5626 return getSession().getService(api);
5630 public <T> T peekService(Class<T> api) {
5631 return getSession().peekService(api);
5635 public boolean hasService(Class<?> api) {
5636 return getSession().hasService(api);
5640 public <T> void registerService(Class<T> api, T service) {
5641 getSession().registerService(api, service);
5645 public boolean isImmutable(Resource resource) throws DatabaseException {
5646 ResourceImpl impl = (ResourceImpl)resource;
5647 return processor.isImmutable(impl.id);
5654 protected static String INTERNAL_ERROR_STRING = "Transaction aborted due to internal client error.";
5657 * callerThread is the currently running thread state.syncThread is blocking for
5658 * this execution state.syncParent is the blocking request
5661 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support) {
5662 this.parentGraph = parentGraph;
5663 this.parent = parent;
5664 this.processor = support;
5665 this.asyncBarrier = prepareBarrier(parentGraph, parent, null, false);
5668 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support, AsyncBarrierImpl asyncBarrier) {
5669 this.parentGraph = parentGraph;
5670 this.parent = parent;
5671 this.processor = support;
5672 this.asyncBarrier = asyncBarrier;
5675 ReadGraphImpl(ReadGraphImpl graph, CacheEntry parent) {
5676 this(graph, parent, graph.processor);
5679 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, Runnable callback, boolean needsToBlock) {
5680 this(parentGraph, parent, parentGraph.processor, prepareBarrier(parentGraph, parent, callback, needsToBlock));
5683 static AsyncBarrierImpl prepareBarrier(ReadGraphImpl parentGraph, CacheEntry parent, Runnable callback, boolean needsToBlock) {
5684 return new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent, callback, needsToBlock);
5687 ReadGraphImpl(ReadGraphImpl graph) {
5688 this(graph, graph.parent);
5691 public ReadGraphImpl withParent(CacheEntry parent, Runnable callback, boolean needsToBlock) {
5692 return new ReadGraphImpl(this, parent, callback, needsToBlock);
5695 // public ReadGraphImpl withParent(CacheEntry parent) {
5696 // return withParent(parent, null);
5699 public ReadGraphImpl syncWithParent(CacheEntry parent) {
5700 return new ReadGraphImpl(this, parent, processor, null);
5703 public ReadGraphImpl forRecompute(CacheEntry parent) {
5704 return new ReadGraphImpl(null, parent, processor);
5707 public static ReadGraphImpl create(QueryProcessor support) {
5708 ReadGraphImpl result = new ReadGraphImpl(null, null, support);
5712 public ReadGraphImpl newRestart(ReadGraphImpl impl) {
5714 WriteGraphImpl write = processor.getSession().getService(
5715 WriteGraphImpl.class);
5721 final private ListenerBase getListenerBase(final Object procedure) {
5722 if (procedure instanceof ListenerBase)
5723 return (ListenerBase) procedure;
5728 public <T> void waitAsyncProcedure(AsyncMultiReadProcedure<T> procedure) {
5730 assert(procedure.done());
5734 public <T> void waitAsyncProcedure(AsyncReadProcedure<T> procedure) {
5736 assert(procedure.done());
5740 public boolean resumeTasks() {
5741 return processor.resumeTasks(this);
5744 Class<?> singleClass(Set<Resource> types) {
5745 Class<?> result = null;
5746 for (Resource type : types) {
5747 Class<?> clazz = processor.getBuiltinValue(type);
5748 if (clazz != null) {
5758 private String debugString(Resource r) {
5761 name = getPossibleRelatedValue(r, processor.getL0(this).HasName);
5762 } catch (ManyObjectsForFunctionalRelationException e) {
5763 Logger.defaultLogError(e);
5764 } catch (ServiceException e) {
5765 Logger.defaultLogError(e);
5767 return "[" + name + " - " + r + "]";
5771 public String toString() {
5772 return "ReadGraphImpl[thread=" + Thread.currentThread() + "]";
5776 final public int thread() {
5780 static class MultiTripleIntProcedure implements TripleIntProcedure {
5782 final private AsyncMultiProcedure<Statement> procedure;
5783 final private ReadGraphImpl impl;
5784 final private QuerySupport support;
5786 public MultiTripleIntProcedure(AsyncMultiProcedure<Statement> procedure, ReadGraphImpl impl, QuerySupport support) {
5787 this.procedure = procedure;
5789 this.support = support;
5793 public void execute(ReadGraphImpl graph, int s, int p, int o) {
5795 procedure.execute(graph, support.getStatement(s, p, o));
5796 } catch (Throwable t2) {
5797 Logger.defaultLogError(t2);
5802 public void finished(ReadGraphImpl graph) {
5804 procedure.finished(graph);
5805 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5806 } catch (Throwable t2) {
5807 Logger.defaultLogError(t2);
5812 public void exception(ReadGraphImpl graph, Throwable t) {
5814 procedure.exception(graph, t);
5815 } catch (Throwable t2) {
5816 Logger.defaultLogError(t2);
5818 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5822 public String toString() {
5823 return "forEachObject with " + procedure;
5828 // private AsyncMultiProcedure<Resource> cacheKey = null;
5829 // private MultiIntProcedure cacheResult = null;
5831 // final IntProcedure forMultiProcedure(final AsyncMultiProcedure<Resource> procedure) {
5833 // if(procedure == cacheKey) return cacheResult;
5835 // cacheResult = new MultiIntProcedure(procedure, this, processor.support);
5836 // cacheKey = procedure;
5838 // return cacheResult;
5842 // private AsyncMultiProcedure<Statement> cacheKey2 = null;
5843 // private MultiTripleIntProcedure cacheResult2 = null;
5845 // final synchronized TripleIntProcedure forMultiProcedure(final AsyncMultiProcedure<Statement> procedure) {
5847 // if(procedure == cacheKey2) return cacheResult2;
5849 // cacheResult2 = new MultiTripleIntProcedure(procedure, this, processor.support);
5850 // cacheKey2 = procedure;
5852 // return cacheResult2;
5857 public Datatype getDataType(Resource subject) throws DatabaseException {
5858 for(Resource dataTypeResource : getObjects(subject, processor.getL0(this).HasDataType))
5859 return getValue(dataTypeResource, Bindings.getBindingUnchecked(Datatype.class));
5860 throw new DoesNotContainValueException("The literal has no data type.");
5863 protected <T extends Accessor> T getAccessor4File(Resource subject)
5864 throws DatabaseException {
5867 byte[] bytes = processor.support.getValue(g, subject);
5871 BinaryVariant va = (BinaryVariant) Accessors.getAccessor(bytes, Datatypes.VARIANT);
5872 Accessor ca = va.getContentAccessor();
5874 } catch (AccessorConstructionException e) {
5875 throw new DatabaseException(e);
5881 Binding datatype_binding = Bindings.getBindingUnchecked(DataType.class);
5882 Serializer datatype_serializer = datatype_binding.serializer();
5885 BinaryMemory in = new BinaryMemory(ByteBuffer.wrap(bytes));
5886 datatype = (DataType)datatype_serializer.deserialize(in);
5887 Binding data_binding = Bindings.getBinding(datatype);
5888 Serializer data_serializer = data_binding.serializer(BinarySerializationFormat.INSTANCE);
5889 Object o = data_serializer.deserialize(in);
5891 return (T)Accessors.getAccessor(data_binding, o);
5892 } catch(AccessorConstructionException e) {
5895 } catch (Exception e) {
5896 throw new DatabaseException(e);
5899 @SuppressWarnings("unchecked")
5901 public <T extends Accessor> T getAccessor(Resource subject) throws DatabaseException {
5902 RandomAccessBinary rab = getRandomAccessBinary(subject);
5904 return (T)Accessors.getAccessor(rab, getDataType(subject));
5905 } catch(AccessorConstructionException e) {
5906 throw new DatabaseException(e);
5909 @SuppressWarnings("unchecked")
5910 protected <T extends Accessor> T createAccessor(Resource resource, Datatype datatype, Object intialValue)
5911 throws DatabaseException {
5912 RandomAccessBinary rab = createRandomAccessBinary(resource, datatype, intialValue);
5914 return (T)Accessors.getAccessor(rab, datatype);
5915 } catch(AccessorConstructionException e) {
5916 throw new DatabaseException(e);
5920 public RandomAccessBinary getRandomAccessBinary(Resource subject) throws DatabaseException {
5921 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5922 ResourceData rd = ravs.get(subject);
5926 ExternalValueSupport evs = getService(ExternalValueSupport.class);
5927 long size = evs.getValueSize(this, subject); // Throws DatabaseException if no old external value.
5929 File platform = Platform.getLocation().toFile();
5930 File tempFiles = new File(platform, "tempFiles");
5931 File dbDir = new File(tempFiles, "db");
5933 File file = new File(dbDir, "ResourceFile" + subject.getResourceId());
5934 rd = new ResourceData(new BinaryFile(file), true); // Old external value.
5935 final int N = 1<<20;
5939 int length = N < left ? N : (int)left;
5940 byte[] bytes = evs.readValue(this, subject, offset, length);
5941 offset += bytes.length;
5942 left -= bytes.length;
5943 rd.binaryFile.write(bytes);
5945 ravs.put(subject, rd);
5947 } catch (Exception e) {
5948 throw new DatabaseException("Resource " + subject + " have value but there was problem with accessing value data.", e);
5950 } catch (Exception e) {
5951 if(Development.DEVELOPMENT) {
5952 if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
5953 e.printStackTrace();
5957 Datatype datatype = getDataType(subject);
5958 Object value = getPossibleValue(subject, Bindings.getBinding(datatype));
5959 return createRandomAccessBinary(subject, datatype, value);
5961 public RandomAccessBinary createRandomAccessBinary(Resource resource, Datatype datatype, Object initialValue)
5962 throws DatabaseException {
5963 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5965 File platform = Platform.getLocation().toFile();
5966 File tempFiles = new File(platform, "tempFiles");
5967 File dbDir = new File(tempFiles, "db");
5969 File file = new File(dbDir, "ResourceFile" + resource.getResourceId());
5970 ResourceData rd = new ResourceData(new BinaryFile(file), false);
5971 Binding binding = Bindings.getBinding(datatype);
5972 if (null == initialValue) {
5973 initialValue = binding.createDefault();
5975 Serializer serializer = binding.serializer();
5976 byte[] bytes = serializer.serialize(initialValue);
5977 // In case the file has been previously accessed and was larger we set the correct size now
5978 rd.binaryFile.setLength(bytes.length);
5979 rd.binaryFile.write(bytes);
5980 ravs.put(resource, rd);
5982 } catch (Exception e) {
5983 if (e instanceof DatabaseException)
5984 throw (DatabaseException)e;
5986 throw new DatabaseException(e);
5990 // static class ExternalValueRequest<T> extends ResourceRead<T> {
5992 // public ExternalValueRequest(Resource resource) {
5996 // @SuppressWarnings("unchecked")
5998 // public T perform(ReadGraph graph) throws DatabaseException {
6001 // String uri = graph.getURI(resource);
6002 // if(Layer0.URIs.Functions_functionApplication.equals(uri)) return (T)functionApplication;
6004 // return (T)ReflectionUtils.getValue(uri).getValue();
6006 // } catch(ValueNotFoundException e) {
6007 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
6008 // } catch(ClassCastException e) {
6009 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
6015 @SuppressWarnings("unchecked")
6017 public <T> T getValue2(Resource r, Object context) throws DatabaseException {
6018 Layer0 L0 = processor.getL0(this);
6019 Set<Resource> types = getTypes(r);
6021 if(types.contains(L0.Literal)) {
6022 if(isImmutable(r)) {
6023 return syncRequest(new ValueImplied<T>(r));
6028 else if(types.contains(L0.ExternalValue)) {
6029 return (T)syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6033 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6034 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6036 return function.apply(this, r, context);
6037 } catch(RuntimeException e) {
6038 DatabaseException dte = findPossibleRootException(e);
6039 if(dte != null) throw dte;
6040 else throw new DatabaseException(e);
6047 public Variant getVariantValue2(Resource r, Object context) throws DatabaseException {
6048 Layer0 L0 = processor.getL0(this);
6049 Set<Resource> types = getTypes(r);
6051 if(types.contains(L0.Literal)) {
6052 if(isImmutable(r)) {
6053 return syncRequest(new VariantValueImplied(r));
6055 return getVariantValue(r);
6058 else if(types.contains(L0.ExternalValue)) {
6059 Object value = syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6061 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6062 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6063 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6068 Function3<ReadGraph,Resource,Object,Object> function = requestValueFunction(r);
6069 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6071 Object value = function.apply(this, r, context);
6073 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6074 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6075 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6077 } catch(RuntimeException e) {
6078 DatabaseException dte = findPossibleRootException(e);
6079 if(dte != null) throw dte;
6080 else throw new DatabaseException(e);
6086 public <T> T getPossibleValue2(Resource subject, Object context) throws DatabaseException {
6088 return getValue2(subject, context);
6089 } catch (DatabaseException e) {
6094 static class PossibleConverterFunction<T> extends ResourceRead<Function3<ReadGraph,Resource,Object,T>> {
6096 public PossibleConverterFunction(Resource resource) {
6101 public Function3<ReadGraph,Resource,Object,T> perform(ReadGraph graph) throws DatabaseException {
6102 return compute(graph, resource);
6105 @SuppressWarnings("unchecked")
6106 public static <T> Function3<ReadGraph,Resource,Object,T> compute(ReadGraph graph, Resource resource) throws DatabaseException {
6107 Layer0 L0 = Layer0.getInstance(graph);
6108 for(Resource converter : graph.getObjects(resource, L0.ConvertsToValueWith)) {
6110 if(L0.Functions_functionApplication.equals(converter)) {
6111 return (Function3<ReadGraph,Resource,Object,T>)graph.syncRequest(new AdaptValue(resource));
6113 return graph.getValue2(converter, resource);
6115 } catch(RuntimeException e) {
6116 DatabaseException dte = findPossibleRootException(e);
6117 if(dte != null) throw dte;
6118 else throw new DatabaseException(e);
6126 <T> Function3<ReadGraph,Resource,Object,T> requestValueFunction(Resource r) throws DatabaseException {
6128 return syncRequest(new PossibleConverterFunction<T>(r), TransientCacheAsyncListener.<Function3<ReadGraph,Resource,Object,T>>instance());
6130 return syncRequest(new PossibleConverterFunction<T>(r));
6134 * Get a value associated with a graph {@link Resource}, using a possible context object and
6135 * a desired value binding. The following methods are tried in order to retreive the value:
6137 * <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>
6138 * <li>If the resource is a {@code L0.ExternalValue}, the value is acquired using
6139 * {@link ReflectionUtils#getValue(String)}.</li>
6140 * <li>If the resource is associated with a suitable value converter with relation {@code L0.ConvertsToValueWith}
6141 * (see {@link #requestValueFunction(Resource)}), the value function is called with the graph, the resource
6142 * and the context object.</li>
6145 * @param r A graph resource with which the value is associated
6146 * @param context A context object that is used for acquiring the value (only applied in case 3)
6147 * @param binding A binding for the value type (only applied in case 1)
6148 * @return The value of the graph node.
6149 * @throws DoesNotContainValueException No value is associated with the graph node.
6150 * @throws DatabaseException Other errors, such as an error in casting the value to the return type or
6151 * a runtime error in the value function.
6153 @SuppressWarnings("unchecked")
6155 public <T> T getValue2(Resource r, Object context, Binding binding) throws DatabaseException {
6156 if (binding instanceof ObjectVariantBinding)
6157 return getValue2(r, context);
6159 Layer0 L0 = processor.getL0(this);
6160 Set<Resource> types = getTypes(r);
6161 if(types.contains(L0.Literal)) {
6162 if(isImmutable(r)) {
6163 return syncRequest(new Value<T>(r, binding));
6165 return getValue(r, binding);
6167 } else if(types.contains(L0.ExternalValue)) {
6168 ComputationalValue cv = syncRequest(new PossibleAdapter<ComputationalValue>(r, ComputationalValue.class), TransientCacheAsyncListener.instance());
6170 return cv.getValue(this, r);
6172 // This should not even be possible since L0 defines an adapter for all values
6174 return (T)ReflectionUtils.getValue(getURI(r)).getValue();
6175 } catch(ValueNotFoundException e) {
6176 throw new DatabaseException(e);
6177 } catch(ClassCastException e) {
6178 throw new DatabaseException(e);
6183 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6184 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function.");
6186 Object value = function.apply(this, r, context);
6187 if(binding.isInstance(value)) return (T)value;
6188 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
6189 return (T)Bindings.adapt(value, srcBinding, binding);
6190 } catch(RuntimeException e) {
6191 DatabaseException dte = findPossibleRootException(e);
6192 if(dte != null) throw dte;
6193 else throw new DatabaseException(e);
6194 } catch (AdaptException e) {
6195 throw new DatabaseException(e);
6196 } catch (org.simantics.databoard.binding.error.BindingException e) {
6197 throw new DatabaseException(e);
6203 public <T> T getPossibleValue2(Resource subject, Object context, Binding binding) throws DatabaseException {
6205 return getValue2(subject, context, binding);
6206 } catch (DatabaseException e) {
6211 private static DatabaseException findPossibleRootException(Throwable t) {
6212 if(t == null) return null;
6213 if(t instanceof DatabaseException) return (DatabaseException)t;
6214 if(t instanceof RuntimeException || t instanceof InvocationTargetException) {
6215 return findPossibleRootException(t.getCause());
6221 public <T> T getRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6222 return getRelatedValue2(subject, relation, subject);
6226 public Variant getRelatedVariantValue2(Resource subject, Resource relation) throws DatabaseException {
6227 return getRelatedVariantValue2(subject, relation, subject);
6231 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6233 Resource object = getPossibleObject(subject, relation);
6234 if(object == null) return null;
6235 else return getValue2(object, subject);
6236 } catch (DatabaseException e) {
6242 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6243 if(Development.DEVELOPMENT) {
6244 if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
6245 String error = L0Validations.checkValueType(this, subject, relation);
6247 Logger.defaultLogError(new ValidationException(error));
6248 throw new ValidationException(error);
6252 return getValue2(getSingleObject(subject, relation), context);
6256 public Variant getRelatedVariantValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6257 if(Development.DEVELOPMENT) {
6258 if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
6259 String error = L0Validations.checkValueType(this, subject, relation);
6261 Logger.defaultLogError(new ValidationException(error));
6262 throw new ValidationException(error);
6266 return getVariantValue2(getSingleObject(subject, relation), context);
6270 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6272 Resource object = getPossibleObject(subject, relation);
6273 if(object == null) return null;
6274 else return getValue2(object, context);
6275 } catch (DatabaseException e) {
6281 public <T> T getRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6282 return getRelatedValue2(subject, relation, subject, binding);
6286 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6288 Resource object = getPossibleObject(subject, relation);
6289 if(object == null) return null;
6290 return getValue2(object, subject, binding);
6291 } catch (DatabaseException e) {
6297 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6298 return getValue2(getSingleObject(subject, relation), context, binding);
6302 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6304 Resource object = getPossibleObject(subject, relation);
6305 if(object == null) return null;
6306 else return getValue2(object, context, binding);
6307 } catch (DatabaseException e) {
6313 public Type getRelatedValueType(Resource subject, Resource relation) throws DatabaseException {
6314 Layer0 L0 = processor.getL0(this);
6315 Resource property = getSingleObject(subject, relation);
6316 String typeText = (String)getRelatedValue(property, L0.HasValueType, Bindings.STRING);
6318 return org.simantics.scl.compiler.types.Types.parseType(typeText);
6319 } catch (SCLTypeParseException e) {
6320 throw new DatabaseException(e);
6324 private static ThreadLocal<Boolean> syncGraph = new ThreadLocal<Boolean>() {
6325 protected Boolean initialValue() {
6331 public boolean setSynchronous(boolean value) {
6332 boolean old = getSynchronous();
6333 syncGraph.set(value);
6338 public boolean getSynchronous() {
6339 return syncGraph.get();
6342 public void ensureLoaded(int resource) {
6343 processor.querySupport.ensureLoaded(this, resource);
6346 public void ensureLoaded(int resource, int predicate) {
6347 processor.querySupport.ensureLoaded(this, resource, predicate);
6350 public byte[] getValue(int resource) {
6351 return processor.querySupport.getValue(this, resource);
6354 public int thread(int resource) {
6355 return (resource >>> 16) & processor.THREAD_MASK;
6358 public int thread(Resource resource) {
6359 return (((ResourceImpl)resource).id >>> 16) & processor.THREAD_MASK;
6362 public ResourceSupport getResourceSupport() {
6363 return processor.getResourceSupport();
6367 public Object getModificationCounter() {
6368 return processor.getSession().getModificationCounter();
6372 public boolean performPending() {
6373 return processor.performPending(this);
6376 public Set<ReadGraphImpl> ancestorSet() {
6377 HashSet<ReadGraphImpl> result = new HashSet<>();
6378 ReadGraphImpl g = this;
6386 public int getLevel() {
6387 return getLevelStatic(this);
6390 private static int getLevelStatic(ReadGraphImpl impl) {
6391 if(impl == null) return 0;
6392 else return 1 + getLevelStatic(impl.parentGraph);
6395 public boolean isParent(ReadGraphImpl impl) {
6396 if(impl == null) return false;
6397 if(this == impl) return true;
6398 return isParent(impl.parentGraph);
6401 public ReadGraphImpl getTopLevelGraph() {
6402 return getTopLevelGraphStatic(this);
6405 private static ReadGraphImpl getTopLevelGraphStatic(ReadGraphImpl impl) {
6406 if(impl.parentGraph == null) return impl;
6407 else return getTopLevelGraphStatic(impl.parentGraph);