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 ITask task = ThreadLogger.task(request);
1930 T result = (T)QueryCache.runnerReadEntry(this, request, parent, null, null, true);
1937 public <T> T syncRequest(Read<T> request, SyncListener<T> procedure)
1938 throws DatabaseException {
1939 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
1943 public <T> T syncRequest(Read<T> request, final Listener<T> procedure)
1944 throws DatabaseException {
1945 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
1949 public <T> T syncRequest(final Read<T> request, final AsyncProcedure<T> procedure) throws DatabaseException {
1951 assert (request != null);
1953 ITask task = ThreadLogger.task(request);
1954 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
1955 T result = QueryCache.resultReadEntry(this, request, parent, listener, procedure);
1962 public <T> T syncRequest(final Read<T> request,
1963 final SyncProcedure<T> procedure) throws DatabaseException {
1964 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
1968 public <T> T syncRequest(Read<T> request, Procedure<T> procedure)
1969 throws DatabaseException {
1970 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
1973 static class AsyncReadProcedure<T> implements AsyncProcedure<T> {
1975 private static Throwable DONE = new Throwable();
1978 Throwable exception = null;
1981 public void execute(AsyncReadGraph graph, T t) {
1987 public void exception(AsyncReadGraph graph, Throwable t) {
1991 public void checkAndThrow() throws DatabaseException {
1992 if(exception != DONE) {
1993 if (exception instanceof DatabaseException)
1994 throw (DatabaseException) exception;
1996 throw new DatabaseException(
1997 "Unexpected exception in ReadGraph.syncRequest(AsyncRead)",
2002 public boolean done() {
2003 return exception != null;
2009 public <T> T syncRequest(final AsyncRead<T> request)
2010 throws DatabaseException {
2012 assert (request != null);
2013 return syncRequest(request, new AsyncProcedureAdapter<>() );
2018 public <T> T syncRequest(AsyncRead<T> request, AsyncListener<T> procedure)
2019 throws DatabaseException {
2020 return syncRequest(request, (AsyncProcedure<T>) procedure);
2024 public <T> T syncRequest(AsyncRead<T> request, SyncListener<T> procedure)
2025 throws DatabaseException {
2026 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
2030 public <T> T syncRequest(AsyncRead<T> request, Listener<T> procedure)
2031 throws DatabaseException {
2032 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
2036 final public <T> T syncRequest(final AsyncRead<T> request,
2037 final AsyncProcedure<T> procedure) throws DatabaseException {
2039 assert (request != null);
2041 ITask task = ThreadLogger.task(request);
2042 ListenerBase listener = getListenerBase(procedure);
2043 T result = (T)QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure, true);
2050 public <T> T syncRequest(AsyncRead<T> request,
2051 final SyncProcedure<T> procedure) throws DatabaseException {
2052 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2056 final public <T> T syncRequest(final AsyncRead<T> request,
2057 final Procedure<T> procedure) throws DatabaseException {
2058 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2062 public <T> Collection<T> syncRequest(final MultiRead<T> request)
2063 throws DatabaseException {
2065 assert (request != null);
2067 final ArrayList<T> result = new ArrayList<T>();
2068 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2070 syncRequest(request, new SyncMultiProcedure<T>() {
2073 public void execute(ReadGraph graph, T t) {
2074 synchronized (result) {
2080 public void finished(ReadGraph graph) {
2084 public void exception(ReadGraph graph, Throwable t) {
2089 public String toString() {
2090 return "syncRequest(MultiRead) -> " + request;
2095 Throwable t = exception.get();
2097 if (t instanceof DatabaseException)
2098 throw (DatabaseException) t;
2100 throw new DatabaseException(
2101 "Unexpected exception in ReadGraph.syncRequest(Read)",
2110 public <T> Collection<T> syncRequest(MultiRead<T> request,
2111 SyncMultiListener<T> procedure) {
2112 return syncRequest(request, (SyncMultiProcedure<T>)procedure);
2116 public <T> Collection<T> syncRequest(MultiRead<T> request,
2117 MultiListener<T> procedure) {
2118 return syncRequest(request, new NoneToSyncMultiListener<T>(procedure));
2122 public <T> Collection<T> syncRequest(MultiRead<T> request,
2123 SyncMultiProcedure<T> procedure) {
2125 assert (request != null);
2127 ListenerBase listener = getListenerBase(procedure);
2129 final ResultCallWrappedSyncQueryProcedure<T> wrapper = new ResultCallWrappedSyncQueryProcedure<T>(procedure);
2131 if (parent != null || listener != null) {
2133 // Object syncParent = request;
2135 // final ReadGraphImpl newGraph = newSync();
2137 processor.query(this, request, parent, wrapper, listener);
2139 // newGraph.waitAsync(syncParent);
2143 // Object syncParent = request;
2145 // final ReadGraphImpl newGraph = newSync();
2148 request.perform(this, wrapper);
2149 } catch (Throwable t) {
2150 wrapper.exception(this, t);
2155 return wrapper.get();
2160 public <T> Collection<T> syncRequest(MultiRead<T> request,
2161 MultiProcedure<T> procedure) {
2162 return syncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
2165 static class AsyncMultiReadProcedure<T> extends ArrayList<T> implements AsyncMultiProcedure<T> {
2167 private static Throwable DONE = new Throwable();
2169 private static final long serialVersionUID = -6494230465108115812L;
2171 Throwable exception = null;
2174 public synchronized void execute(AsyncReadGraph graph, T t) {
2179 public void finished(AsyncReadGraph graph) {
2184 public void exception(AsyncReadGraph graph, Throwable t) {
2188 public void checkAndThrow() throws DatabaseException {
2189 if(exception != DONE) {
2190 if (exception instanceof DatabaseException)
2191 throw (DatabaseException) exception;
2193 throw new DatabaseException(
2194 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
2199 public boolean done() {
2200 return exception != null;
2206 final public <T> Collection<T> syncRequest(AsyncMultiRead<T> request)
2207 throws DatabaseException {
2209 assert (request != null);
2211 final AsyncMultiReadProcedure<T> procedure = new AsyncMultiReadProcedure<T>();
2213 syncRequest(request, procedure);
2215 procedure.checkAndThrow();
2221 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2222 AsyncMultiListener<T> procedure) {
2223 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2227 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2228 SyncMultiListener<T> procedure) {
2229 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2233 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2234 MultiListener<T> procedure) {
2235 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2238 final private <T> void syncRequest(final AsyncMultiRead<T> request,
2239 final AsyncMultiReadProcedure<T> procedure) {
2241 assert (request != null);
2242 assert (procedure != null);
2244 ListenerBase listener = getListenerBase(procedure);
2246 if (parent != null || listener != null) {
2248 // Object syncParent = request;
2250 // final ReadGraphImpl newGraph = newSync();
2252 processor.query(this, request, parent, procedure, listener);
2254 // newGraph.waitAsync(syncParent);
2255 waitAsyncProcedure(procedure);
2259 // Object syncParent = callerThread == Integer.MIN_VALUE ? null
2262 // final ReadGraphImpl newGraph = newSyncAsync(syncParent);
2267 // ReadGraphImpl sync = newSync();
2268 request.perform(this, procedure);
2269 // sync.waitAsync(null);
2270 waitAsyncProcedure(procedure);
2273 } catch (Throwable t) {
2275 waitAsyncProcedure(procedure);
2286 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2287 final AsyncMultiProcedure<T> procedure) {
2289 assert (request != null);
2290 assert (procedure != null);
2292 ListenerBase listener = getListenerBase(procedure);
2294 if (parent != null || listener != null) {
2296 // Object syncParent = request;
2298 // final ReadGraphImpl newGraph = newSync();
2300 processor.query(this, request, parent, procedure, listener);
2302 // newGraph.waitAsync(syncParent);
2306 // Object syncParent = request;
2308 // final ReadGraphImpl newGraph = newSync();
2312 request.perform(this, new AsyncMultiProcedure<T>() {
2315 public void execute(AsyncReadGraph graph, T result) {
2316 procedure.execute(graph, result);
2320 public void finished(AsyncReadGraph graph) {
2321 procedure.finished(graph);
2325 public void exception(AsyncReadGraph graph, Throwable t) {
2326 procedure.exception(graph, t);
2330 public String toString() {
2331 return "syncRequest(AsyncMultiRead) -> " + procedure;
2336 } catch (Throwable t) {
2348 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2349 final SyncMultiProcedure<T> procedure) {
2350 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2354 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2355 final MultiProcedure<T> procedure) {
2356 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2360 public <T> T syncRequest(final ExternalRead<T> request)
2361 throws DatabaseException {
2363 assert (request != null);
2365 return syncRequest(request, new Procedure<T>() {
2368 public void execute(T t) {
2372 public void exception(Throwable t) {
2376 public String toString() {
2377 return "syncRequest(AsyncRead) -> " + request;
2385 public <T> T syncRequest(ExternalRead<T> request, Listener<T> procedure) throws DatabaseException {
2386 return syncRequest(request, (Procedure<T>) procedure);
2390 final public <T> T syncRequest(final ExternalRead<T> request,
2391 final Procedure<T> procedure) throws DatabaseException {
2393 assert (request != null);
2395 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
2396 return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
2401 public void syncRequest(final Write request) throws DatabaseException {
2403 assert (request != null);
2405 throw new DatabaseException(
2406 "Write operations are not supported during read transactions!");
2411 public <T> T syncRequest(final WriteResult<T> request) throws DatabaseException {
2413 assert (request != null);
2415 throw new DatabaseException(
2416 "Write operations are not supported during read transactions!");
2421 public void syncRequest(final DelayedWrite request)
2422 throws DatabaseException {
2424 assert (request != null);
2426 throw new DatabaseException(
2427 "Write operations are not supported during read transactions!");
2432 public <T> T syncRequest(final DelayedWriteResult<T> request) throws DatabaseException {
2434 assert (request != null);
2436 throw new DatabaseException(
2437 "Write operations are not supported during read transactions!");
2442 public void syncRequest(final WriteOnly request) throws DatabaseException {
2444 assert (request != null);
2446 throw new DatabaseException(
2447 "Write operations are not supported during read transactions!");
2452 public <T> T syncRequest(final WriteOnlyResult<T> request) throws DatabaseException {
2454 assert (request != null);
2456 throw new DatabaseException(
2457 "Write operations are not supported during read transactions!");
2462 public <T> void async(ReadInterface<T> r, AsyncProcedure<T> procedure) {
2463 r.request(this, procedure);
2467 public <T> void async(ReadInterface<T> r, Procedure<T> procedure) {
2468 r.request(this, procedure);
2472 public <T> void async(ReadInterface<T> r, SyncProcedure<T> procedure) {
2473 r.request(this, procedure);
2477 public <T> void async(ReadInterface<T> r, AsyncListener<T> procedure) {
2478 r.request(this, procedure);
2482 public <T> void async(ReadInterface<T> r, Listener<T> procedure) {
2483 r.request(this, procedure);
2487 public <T> void async(ReadInterface<T> r, SyncListener<T> procedure) {
2488 r.request(this, procedure);
2492 public <T> T sync(ReadInterface<T> r) throws DatabaseException {
2493 return r.request(this);
2497 public <T> T sync(WriteInterface<T> r) throws DatabaseException {
2498 return r.request(this);
2502 public <T> void async(WriteInterface<T> r, Procedure<T> procedure) {
2503 r.request(this, procedure);
2507 public <T> void async(WriteInterface<T> r) {
2508 r.request(this, new ProcedureAdapter<T>());
2512 * Implementation of the interface AsyncReadGraph
2516 public void forURI(Resource resource, AsyncListener<String> listener) {
2517 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2522 public void forURI(Resource resource, SyncListener<String> listener) {
2523 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2528 public void forURI(Resource resource, Listener<String> listener) {
2529 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2534 final public void forURI(final Resource resource,
2535 final AsyncProcedure<String> procedure) {
2537 assert (resource != null);
2538 assert (procedure != null);
2540 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2546 public void forURI(Resource resource, SyncProcedure<String> procedure) {
2547 forURI(resource, new SyncToAsyncProcedure<String>(procedure));
2551 public void forURI(Resource resource, Procedure<String> procedure) {
2552 forURI(resource, new NoneToAsyncProcedure<String>(procedure));
2556 public void forResource(String id, AsyncListener<Resource> listener) {
2557 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2562 public void forResource(String id, SyncListener<Resource> listener) {
2563 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2568 public void forResource(String id, Listener<Resource> listener) {
2569 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2574 final public void forResource(final String id,
2575 final AsyncProcedure<Resource> procedure) {
2577 assert (id != null);
2578 assert (procedure != null);
2580 processor.forResource(this, id, procedure);
2585 public void forResource(String id, SyncProcedure<Resource> procedure) {
2586 forResource(id, new SyncToAsyncProcedure<Resource>(procedure));
2590 public void forResource(String id, Procedure<Resource> procedure) {
2591 forResource(id, new NoneToAsyncProcedure<Resource>(procedure));
2595 public void forBuiltin(String id, AsyncListener<Resource> listener) {
2596 asyncRequest(new Builtin(id), listener);
2600 public void forBuiltin(String id, SyncListener<Resource> listener) {
2601 asyncRequest(new Builtin(id), listener);
2605 public void forBuiltin(String id, Listener<Resource> listener) {
2606 asyncRequest(new Builtin(id), listener);
2610 final public void forBuiltin(final String id,
2611 final AsyncProcedure<Resource> procedure) {
2613 assert (id != null);
2614 assert (procedure != null);
2616 processor.forBuiltin(this, id, procedure);
2621 public void forBuiltin(String id, SyncProcedure<Resource> procedure) {
2622 forBuiltin(id, new SyncToAsyncProcedure<Resource>(procedure));
2626 public void forBuiltin(String id, Procedure<Resource> procedure) {
2627 forBuiltin(id, new NoneToAsyncProcedure<Resource>(procedure));
2631 final public void forEachStatement(Resource subject, Resource relation,
2632 AsyncMultiProcedure<Statement> procedure) {
2634 assert (subject != null);
2635 assert (relation != null);
2636 assert (procedure != null);
2638 processor.forEachStatement(this, subject, relation, procedure);
2643 public void forEachStatement(Resource subject, Resource relation,
2644 SyncMultiProcedure<Statement> procedure) {
2645 forEachStatement(subject, relation,
2646 new SyncToAsyncMultiProcedure<Statement>(procedure));
2650 final public void forEachStatement(Resource subject, Resource relation,
2651 MultiProcedure<Statement> procedure) {
2653 assert (subject != null);
2654 assert (relation != null);
2655 assert (procedure != null);
2657 processor.forEachStatement(this, subject, relation, procedure);
2662 final public void forStatementSet(Resource subject, Resource relation,
2663 AsyncSetListener<Statement> procedure) {
2665 assert (subject != null);
2666 assert (relation != null);
2667 assert (procedure != null);
2669 processor.forStatementSet(this, subject, relation, procedure);
2674 final public void forStatementSet(Resource subject, Resource relation,
2675 SyncSetListener<Statement> procedure) {
2676 forStatementSet(subject, relation,
2677 new SyncToAsyncSetProcedure<Statement>(procedure));
2681 public void forStatementSet(Resource subject, Resource relation,
2682 SetListener<Statement> listener) {
2683 forStatementSet(subject, relation,
2684 new NoneToAsyncSetProcedure<Statement>(listener));
2688 final public void forEachAssertedStatement(final Resource subject,
2689 final Resource relation,
2690 final AsyncMultiProcedure<Statement> procedure) {
2692 assert (subject != null);
2693 assert (relation != null);
2694 assert (procedure != null);
2696 processor.forEachAssertedStatement(this, subject, relation, procedure);
2701 public void forEachAssertedStatement(Resource subject, Resource relation,
2702 SyncMultiProcedure<Statement> procedure) {
2703 forEachAssertedStatement(subject, relation,
2704 new SyncToAsyncMultiProcedure<Statement>(procedure));
2708 public void forEachAssertedStatement(Resource subject, Resource relation,
2709 MultiProcedure<Statement> procedure) {
2710 forEachAssertedStatement(subject, relation,
2711 new NoneToAsyncMultiProcedure<Statement>(procedure));
2715 public void forAssertedStatementSet(Resource subject, Resource relation,
2716 AsyncSetListener<Statement> procedure) {
2718 assert (subject != null);
2719 assert (relation != null);
2720 assert (procedure != null);
2722 processor.forAssertedStatementSet(this, subject, relation, procedure);
2727 public void forAssertedStatementSet(Resource subject, Resource relation,
2728 SyncSetListener<Statement> procedure) {
2730 assert (subject != null);
2731 assert (relation != null);
2732 assert (procedure != null);
2734 forAssertedStatementSet(subject, relation,
2735 new SyncToAsyncSetProcedure<Statement>(procedure));
2740 public void forAssertedStatementSet(Resource subject, Resource relation,
2741 SetListener<Statement> procedure) {
2743 assert (subject != null);
2744 assert (relation != null);
2745 assert (procedure != null);
2747 forAssertedStatementSet(subject, relation,
2748 new NoneToAsyncSetProcedure<Statement>(procedure));
2753 final public void forEachPredicate(final Resource subject,
2754 final AsyncMultiProcedure<Resource> procedure) {
2756 assert (subject != null);
2757 assert (procedure != null);
2759 processor.forEachPredicate(this, subject, procedure);
2764 public void forEachPredicate(Resource subject,
2765 SyncMultiProcedure<Resource> procedure) {
2766 forEachPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(
2771 final public void forEachPredicate(final Resource subject,
2772 final MultiProcedure<Resource> procedure) {
2774 assert (subject != null);
2775 assert (procedure != null);
2777 processor.forEachPredicate(this, subject, procedure);
2782 final public void forPredicateSet(final Resource subject,
2783 final AsyncSetListener<Resource> procedure) {
2785 assert (subject != null);
2786 assert (procedure != null);
2788 processor.forPredicateSet(this, subject, procedure);
2793 final public void forPredicateSet(final Resource subject,
2794 final SyncSetListener<Resource> procedure) {
2796 assert (subject != null);
2797 assert (procedure != null);
2799 forPredicateSet(subject, new SyncToAsyncSetProcedure<Resource>(
2805 final public void forPredicateSet(final Resource subject,
2806 final SetListener<Resource> procedure) {
2808 assert (subject != null);
2809 assert (procedure != null);
2811 forPredicateSet(subject, new NoneToAsyncSetProcedure<Resource>(
2817 final public void forEachPrincipalType(final Resource subject,
2818 final AsyncMultiProcedure<Resource> procedure) {
2820 assert (subject != null);
2821 assert (procedure != null);
2823 processor.forEachPrincipalType(this, subject, procedure);
2828 public void forEachPrincipalType(Resource subject,
2829 SyncMultiProcedure<Resource> procedure) {
2830 forEachPrincipalType(subject, new SyncToAsyncMultiProcedure<Resource>(
2835 final public void forEachPrincipalType(final Resource subject,
2836 final MultiProcedure<Resource> procedure) {
2838 assert (subject != null);
2839 assert (procedure != null);
2841 processor.forEachPrincipalType(this, subject, procedure);
2846 final public void forPrincipalTypeSet(final Resource subject,
2847 final AsyncSetListener<Resource> procedure) {
2849 assert (subject != null);
2850 assert (procedure != null);
2852 processor.forPrincipalTypeSet(this, subject, procedure);
2857 final public void forPrincipalTypeSet(final Resource subject,
2858 final SyncSetListener<Resource> procedure) {
2860 assert (subject != null);
2861 assert (procedure != null);
2863 forPrincipalTypeSet(subject, new SyncToAsyncSetProcedure<Resource>(
2869 final public void forPrincipalTypeSet(final Resource subject,
2870 final SetListener<Resource> procedure) {
2872 assert (subject != null);
2873 assert (procedure != null);
2875 forPrincipalTypeSet(subject, new NoneToAsyncSetProcedure<Resource>(
2881 public void forTypes(Resource subject, AsyncListener<Set<Resource>> listener) {
2882 asyncRequest(new Types(subject), listener);
2886 public void forTypes(Resource subject, SyncListener<Set<Resource>> listener) {
2887 asyncRequest(new Types(subject), listener);
2891 public void forTypes(Resource subject, Listener<Set<Resource>> listener) {
2892 asyncRequest(new Types(subject), listener);
2896 final public void forTypes(final Resource subject,
2897 final AsyncProcedure<Set<Resource>> procedure) {
2899 assert (subject != null);
2900 assert (procedure != null);
2902 processor.forTypes(this, subject, procedure);
2907 public void forTypes(Resource subject,
2908 SyncProcedure<Set<Resource>> procedure) {
2909 forTypes(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
2913 public void forTypes(Resource subject, Procedure<Set<Resource>> procedure) {
2914 forTypes(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
2918 public void forSupertypes(Resource subject,
2919 AsyncListener<Set<Resource>> listener) {
2920 asyncRequest(new Types(subject), listener);
2924 public void forSupertypes(Resource subject,
2925 SyncListener<Set<Resource>> listener) {
2926 asyncRequest(new Types(subject), listener);
2930 public void forSupertypes(Resource subject, Listener<Set<Resource>> listener) {
2931 asyncRequest(new Types(subject), listener);
2935 final public void forSupertypes(final Resource subject,
2936 final AsyncProcedure<Set<Resource>> procedure) {
2938 assert (subject != null);
2939 assert (procedure != null);
2941 processor.forSupertypes(this, subject, procedure);
2946 public void forSupertypes(Resource subject,
2947 SyncProcedure<Set<Resource>> procedure) {
2948 forSupertypes(subject, new SyncToAsyncProcedure<Set<Resource>>(
2953 public void forSupertypes(Resource subject,
2954 Procedure<Set<Resource>> procedure) {
2955 forSupertypes(subject, new NoneToAsyncProcedure<Set<Resource>>(
2960 public void forDirectSuperrelations(Resource subject,
2961 AsyncMultiProcedure<Resource> procedure) {
2963 assert (subject != null);
2964 assert (procedure != null);
2966 processor.forDirectSuperrelations(this, subject, procedure);
2971 public void forPossibleSuperrelation(Resource subject, AsyncProcedure<Resource> procedure) {
2973 assert (subject != null);
2974 assert (procedure != null);
2976 processor.forPossibleSuperrelation(this, subject, procedure);
2981 public void forSuperrelations(Resource subject,
2982 AsyncListener<Set<Resource>> listener) {
2983 asyncRequest(new Types(subject), listener);
2987 public void forSuperrelations(Resource subject,
2988 SyncListener<Set<Resource>> listener) {
2989 asyncRequest(new Types(subject), listener);
2993 public void forSuperrelations(Resource subject,
2994 Listener<Set<Resource>> listener) {
2995 asyncRequest(new Types(subject), listener);
2999 final public void forSuperrelations(final Resource subject,
3000 final AsyncProcedure<Set<Resource>> procedure) {
3002 assert (subject != null);
3003 assert (procedure != null);
3005 processor.forSuperrelations(this, subject, procedure);
3010 public void forSuperrelations(Resource subject,
3011 SyncProcedure<Set<Resource>> procedure) {
3012 forSuperrelations(subject, new SyncToAsyncProcedure<Set<Resource>>(
3017 public void forSuperrelations(Resource subject,
3018 Procedure<Set<Resource>> procedure) {
3019 forSuperrelations(subject, new NoneToAsyncProcedure<Set<Resource>>(
3024 final public void forEachObject(final Resource subject, final Resource relation, final AsyncMultiProcedure<Resource> procedure) {
3025 processor.forEachObject(this, subject, relation, procedure);
3029 public void forEachObject(Resource subject, Resource relation,
3030 SyncMultiProcedure<Resource> procedure) {
3031 forEachObject(subject, relation,
3032 new SyncToAsyncMultiProcedure<Resource>(procedure));
3036 public void forEachObject(Resource subject, Resource relation,
3037 MultiProcedure<Resource> procedure) {
3039 processor.forEachObject(this, subject, relation, procedure);
3044 final public void forEachDirectPredicate(final Resource subject, final AsyncProcedure<Set<Resource>> procedure) {
3045 processor.forEachDirectPredicate(this, subject, procedure);
3049 final public void forEachDirectPredicate(final Resource subject, final SyncProcedure<Set<Resource>> procedure) {
3050 forEachDirectPredicate(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
3054 public void forEachDirectPredicate(Resource subject, Procedure<Set<Resource>> procedure) {
3055 forEachDirectPredicate(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
3059 final public void forObjectSet(final Resource subject,
3060 final Resource relation, final AsyncSetListener<Resource> procedure) {
3062 assert (subject != null);
3063 assert (relation != null);
3064 assert (procedure != null);
3066 processor.forObjectSet(this, subject, relation, procedure);
3071 final public void forObjectSet(final Resource subject,
3072 final Resource relation, final SyncSetListener<Resource> procedure) {
3074 assert (subject != null);
3075 assert (relation != null);
3076 assert (procedure != null);
3078 forObjectSet(subject, relation, new SyncToAsyncSetProcedure<Resource>(
3084 final public void forObjectSet(final Resource subject,
3085 final Resource relation, final SetListener<Resource> procedure) {
3087 assert (subject != null);
3088 assert (relation != null);
3089 assert (procedure != null);
3091 forObjectSet(subject, relation, new NoneToAsyncSetProcedure<Resource>(
3097 final public void forEachAssertedObject(final Resource subject,
3098 final Resource relation,
3099 final AsyncMultiProcedure<Resource> procedure) {
3101 assert (subject != null);
3102 assert (relation != null);
3103 assert (procedure != null);
3105 processor.forEachAssertedObject(this, subject, relation, procedure);
3110 public void forEachAssertedObject(Resource subject, Resource relation,
3111 SyncMultiProcedure<Resource> procedure) {
3113 assert (subject != null);
3114 assert (relation != null);
3115 assert (procedure != null);
3117 forEachAssertedObject(subject, relation,
3118 new SyncToAsyncMultiProcedure<Resource>(procedure));
3123 public void forEachAssertedObject(Resource subject, Resource relation,
3124 MultiProcedure<Resource> procedure) {
3126 assert (subject != null);
3127 assert (relation != null);
3128 assert (procedure != null);
3130 forEachAssertedObject(subject, relation,
3131 new NoneToAsyncMultiProcedure<Resource>(procedure));
3136 public void forAssertedObjectSet(Resource subject, Resource relation,
3137 AsyncSetListener<Resource> procedure) {
3139 assert (subject != null);
3140 assert (relation != null);
3141 assert (procedure != null);
3143 processor.forAssertedObjectSet(this, subject, relation, procedure);
3148 public void forAssertedObjectSet(Resource subject, Resource relation,
3149 SyncSetListener<Resource> procedure) {
3151 assert (subject != null);
3152 assert (relation != null);
3153 assert (procedure != null);
3155 forAssertedObjectSet(subject, relation,
3156 new SyncToAsyncSetProcedure<Resource>(procedure));
3161 public void forAssertedObjectSet(Resource subject, Resource relation,
3162 SetListener<Resource> procedure) {
3164 assert (subject != null);
3165 assert (relation != null);
3166 assert (procedure != null);
3168 forAssertedObjectSet(subject, relation,
3169 new NoneToAsyncSetProcedure<Resource>(procedure));
3174 public void forInverse(Resource relation, AsyncListener<Resource> listener) {
3175 asyncRequest(new Inverse(relation), listener);
3179 public void forInverse(Resource relation, SyncListener<Resource> listener) {
3180 asyncRequest(new Inverse(relation), listener);
3184 public void forInverse(Resource relation, Listener<Resource> listener) {
3185 asyncRequest(new Inverse(relation), listener);
3189 final public void forInverse(final Resource relation,
3190 final AsyncProcedure<Resource> procedure) {
3192 assert (relation != null);
3193 assert (procedure != null);
3195 processor.forInverse(this, relation, new AsyncProcedure<Resource>() {
3198 public void execute(AsyncReadGraph graph, Resource result) {
3200 procedure.execute(graph, result);
3202 procedure.exception(graph, new NoInverseException(relation
3208 public void exception(AsyncReadGraph graph, Throwable throwable) {
3209 procedure.exception(graph, throwable);
3213 public String toString() {
3214 return "forInverse -> " + procedure;
3222 public void forInverse(Resource relation, SyncProcedure<Resource> procedure) {
3223 forInverse(relation, new SyncToAsyncProcedure<Resource>(procedure));
3227 public void forInverse(Resource relation, Procedure<Resource> procedure) {
3228 forInverse(relation, new NoneToAsyncProcedure<Resource>(procedure));
3232 public void forSingleObject(Resource subject, Resource relation,
3233 AsyncListener<Resource> listener) {
3234 asyncRequest(new SingleObject(subject, relation), listener);
3238 public void forSingleObject(Resource subject, Resource relation,
3239 SyncListener<Resource> listener) {
3240 asyncRequest(new SingleObject(subject, relation), listener);
3244 public void forSingleObject(Resource subject, Resource relation,
3245 Listener<Resource> listener) {
3246 asyncRequest(new SingleObject(subject, relation), listener);
3250 final public void forSingleObject(final Resource subject,
3251 final Resource relation, final AsyncProcedure<Resource> procedure) {
3253 assert (subject != null);
3254 assert (relation != null);
3255 assert (procedure != null);
3257 processor.forEachObject(this, subject, relation,
3258 new SingleOrErrorProcedure<Resource>(procedure));
3263 public void forSingleObject(Resource subject, Resource relation,
3264 SyncProcedure<Resource> procedure) {
3265 forSingleObject(subject, relation, new SyncToAsyncProcedure<Resource>(
3270 public void forSingleObject(Resource subject, Resource relation,
3271 Procedure<Resource> procedure) {
3272 forSingleObject(subject, relation, new NoneToAsyncProcedure<Resource>(
3277 public void forSingleStatement(Resource subject, Resource relation,
3278 AsyncListener<Statement> listener) {
3279 asyncRequest(new SingleStatement(subject, relation), listener);
3283 public void forSingleStatement(Resource subject, Resource relation,
3284 SyncListener<Statement> listener) {
3285 asyncRequest(new SingleStatement(subject, relation), listener);
3289 public void forSingleStatement(Resource subject, Resource relation,
3290 Listener<Statement> listener) {
3291 asyncRequest(new SingleStatement(subject, relation), listener);
3295 final public void forSingleStatement(final Resource subject,
3296 final Resource relation, final AsyncProcedure<Statement> procedure) {
3298 assert (subject != null);
3299 assert (relation != null);
3300 assert (procedure != null);
3302 processor.forEachStatement(this, subject, relation,
3303 new SingleOrErrorProcedure<Statement>(procedure));
3308 public void forSingleStatement(Resource subject, Resource relation,
3309 SyncProcedure<Statement> procedure) {
3310 forSingleStatement(subject, relation,
3311 new SyncToAsyncProcedure<Statement>(procedure));
3315 public void forSingleStatement(Resource subject, Resource relation,
3316 Procedure<Statement> procedure) {
3317 forSingleStatement(subject, relation,
3318 new NoneToAsyncProcedure<Statement>(procedure));
3322 public void forSingleType(Resource subject,
3323 AsyncListener<Resource> listener) {
3324 asyncRequest(new SingleTypeAny(subject), listener);
3328 public void forSingleType(Resource subject,
3329 SyncListener<Resource> listener) {
3330 asyncRequest(new SingleTypeAny(subject), listener);
3334 public void forSingleType(Resource subject,
3335 Listener<Resource> listener) {
3336 asyncRequest(new SingleTypeAny(subject), listener);
3340 final public void forSingleType(final Resource subject, final AsyncProcedure<Resource> procedure) {
3342 assert (subject != null);
3343 assert (procedure != null);
3345 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3347 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
3350 public void execute(AsyncReadGraph graph, final Resource principalType) {
3351 checkedProcedure.offer(graph, principalType);
3355 public void finished(AsyncReadGraph graph) {
3356 checkedProcedure.dec(graph);
3360 public void exception(AsyncReadGraph graph, Throwable t) {
3361 checkedProcedure.exception(graph, t);
3365 public String toString() {
3366 return "forSingleType -> " + procedure;
3374 public void forSingleType(Resource subject, SyncProcedure<Resource> procedure) {
3375 forSingleType(subject, new SyncToAsyncProcedure<Resource>(
3380 public void forSingleType(Resource subject, Procedure<Resource> procedure) {
3381 forSingleType(subject, new NoneToAsyncProcedure<Resource>(
3386 public void forSingleType(Resource subject, Resource relation,
3387 AsyncListener<Resource> listener) {
3388 asyncRequest(new SingleType(subject, relation), listener);
3392 public void forSingleType(Resource subject, Resource relation,
3393 SyncListener<Resource> listener) {
3394 asyncRequest(new SingleType(subject, relation), listener);
3398 public void forSingleType(Resource subject, Resource relation,
3399 Listener<Resource> listener) {
3400 asyncRequest(new SingleType(subject, relation), listener);
3404 final public void forSingleType(final Resource subject,
3405 final Resource baseType, final AsyncProcedure<Resource> procedure) {
3407 assert (subject != null);
3408 assert (procedure != null);
3410 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3412 processor.forEachPrincipalType(this, subject,
3413 new AsyncMultiProcedureAdapter<Resource>() {
3416 public void execute(AsyncReadGraph graph,
3417 final Resource principalType) {
3419 checkedProcedure.inc();
3421 if(baseType == null) {
3423 checkedProcedure.offer(graph, principalType);
3424 checkedProcedure.dec(graph);
3426 } else if(principalType.equals(baseType)) {
3428 checkedProcedure.offer(graph, principalType);
3429 checkedProcedure.dec(graph);
3433 processor.forSupertypes((ReadGraphImpl)graph, principalType,
3434 new AsyncProcedure<Set<Resource>>() {
3437 public void execute(
3438 AsyncReadGraph graph,
3439 Set<Resource> result) {
3441 if (result.contains(baseType))
3442 checkedProcedure.offer(graph,
3444 checkedProcedure.dec(graph);
3449 public void exception(
3450 AsyncReadGraph graph,
3453 .exception(graph, t);
3463 public void finished(AsyncReadGraph graph) {
3464 checkedProcedure.dec(graph);
3468 public void exception(AsyncReadGraph graph, Throwable t) {
3469 checkedProcedure.exception(graph, t);
3473 public String toString() {
3474 return "forSingleType -> " + procedure;
3482 public void forSingleType(Resource subject, Resource relation,
3483 SyncProcedure<Resource> procedure) {
3484 forSingleType(subject, relation, new SyncToAsyncProcedure<Resource>(
3489 public void forSingleType(Resource subject, Resource relation,
3490 Procedure<Resource> procedure) {
3491 forSingleType(subject, relation, new NoneToAsyncProcedure<Resource>(
3496 public <T> void forValue(Resource subject, Binding binding,
3497 AsyncListener<T> listener) {
3498 asyncRequest(new Value<T>(subject, binding), listener);
3502 public <T> void forValue(Resource subject, Binding binding,
3503 SyncListener<T> listener) {
3504 asyncRequest(new Value<T>(subject, binding), listener);
3508 public <T> void forValue(Resource subject, Binding binding,
3509 Listener<T> listener) {
3510 asyncRequest(new Value<T>(subject, binding), listener);
3514 public <T> void forValue(final Resource resource, final Binding binding,
3515 final AsyncProcedure<T> procedure) {
3517 assert (resource != null);
3518 assert (binding != null);
3519 assert (procedure != null);
3521 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
3524 public void execute(AsyncReadGraph graph, byte[] result) {
3528 if (result == null) {
3529 procedure.exception(graph,
3530 new DoesNotContainValueException(
3531 "No value for resource " + resource));
3535 Serializer serializer = binding.serializer();
3536 // Serializer serializer = Bindings.getSerializer( binding );
3537 Object obj = serializer.deserialize(result);
3538 // if (!binding.isInstance(obj))
3539 // procedure.exception(graph, new ClassCastException(
3540 // "Cannot get value " + obj + " with binding "
3543 procedure.execute(graph, (T) obj);
3545 } catch (Throwable t) {
3546 procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
3551 public void exception(AsyncReadGraph graph, Throwable t) {
3553 procedure.exception(graph, t);
3554 } catch (Throwable t2) {
3555 Logger.defaultLogError(t2);
3560 public String toString() {
3561 return "forValue -> " + procedure;
3568 private static String safeArrayToString(byte[] a) {
3571 int iMax = a.length - 1;
3575 StringBuilder b = new StringBuilder();
3577 for (int i = 0; i < 100; i++) { // limit to first 100 items
3580 return b.append(']').toString();
3583 return b.append(", ... (" + a.length + ")]").toString();
3587 public <T> void forValue(Resource subject, Binding binding,
3588 SyncProcedure<T> procedure) {
3589 forValue(subject, binding, new SyncToAsyncProcedure<T>(procedure));
3593 public <T> void forValue(Resource subject, Binding binding,
3594 Procedure<T> procedure) {
3595 forValue(subject, binding, new NoneToAsyncProcedure<T>(procedure));
3599 public <T> void forValue(Resource subject, AsyncListener<T> listener) {
3600 asyncRequest(new ValueImplied<T>(subject), listener);
3604 public <T> void forValue(Resource subject, SyncListener<T> listener) {
3605 asyncRequest(new ValueImplied<T>(subject), listener);
3609 public <T> void forValue(Resource subject, Listener<T> listener) {
3610 asyncRequest(new ValueImplied<T>(subject), listener);
3614 final public <T> void forValue(final Resource subject, final AsyncProcedure<T> procedure) {
3616 assert (subject != null);
3617 assert (procedure != null);
3619 forRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
3622 public void execute(AsyncReadGraph graph, Datatype type) {
3623 // TODO: consider trying Bindings.getBeanBinding(type);
3624 Binding binding = Bindings.getBinding(type);
3625 graph.forValue(subject, binding, procedure);
3629 public void exception(AsyncReadGraph graph, Throwable throwable) {
3630 procedure.exception(graph, new DoesNotContainValueException("Invalid data type", throwable));
3638 public <T> void forValue(Resource subject, SyncProcedure<T> procedure) {
3639 forValue(subject, new SyncToAsyncProcedure<T>(procedure));
3643 public <T> void forValue(Resource subject, Procedure<T> procedure) {
3644 forValue(subject, new NoneToAsyncProcedure<T>(procedure));
3648 public <T> void forRelatedValue(Resource subject, Resource relation,
3649 AsyncListener<T> listener) {
3650 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3654 public <T> void forRelatedValue(Resource subject, Resource relation,
3655 SyncListener<T> listener) {
3656 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3660 public <T> void forRelatedValue(Resource subject, Resource relation,
3661 Listener<T> listener) {
3662 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3666 final public <T> void forRelatedValue(final Resource subject,
3667 final Resource relation, final AsyncProcedure<T> procedure) {
3669 assert (subject != null);
3670 assert (relation != null);
3671 assert (procedure != null);
3673 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3675 processor.forEachObject(this, subject, relation,
3676 new AsyncMultiProcedureAdapter<Resource>() {
3679 public void execute(AsyncReadGraph graph,
3680 final Resource object) {
3682 checkedProcedure.inc();
3684 graph.forValue(object, new AsyncProcedure<Object>() {
3687 public void execute(AsyncReadGraph graph,
3689 checkedProcedure.offer(graph, (T) result);
3690 checkedProcedure.dec(graph);
3694 public void exception(AsyncReadGraph graph,
3696 checkedProcedure.exception(graph, t);
3700 public String toString() {
3701 return "forRelatedValue -> " + procedure;
3709 public void finished(AsyncReadGraph graph) {
3710 checkedProcedure.dec(graph);
3714 public void exception(AsyncReadGraph graph, Throwable t) {
3715 checkedProcedure.exception(graph, t);
3723 public <T> void forRelatedValue(Resource subject, Resource relation,
3724 SyncProcedure<T> procedure) {
3725 forRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
3730 public <T> void forRelatedValue(Resource subject, Resource relation,
3731 Procedure<T> procedure) {
3732 forRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
3737 public <T> void forRelatedValue(Resource subject, Resource relation,
3738 Binding binding, AsyncListener<T> listener) {
3739 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3743 public <T> void forRelatedValue(Resource subject, Resource relation,
3744 Binding binding, SyncListener<T> listener) {
3745 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3749 public <T> void forRelatedValue(Resource subject, Resource relation,
3750 Binding binding, Listener<T> listener) {
3751 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3755 final public <T> void forRelatedValue(final Resource subject,
3756 final Resource relation, final Binding binding,
3757 final AsyncProcedure<T> procedure) {
3759 assert (subject != null);
3760 assert (relation != null);
3761 assert (binding != null);
3762 assert (procedure != null);
3764 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3766 processor.forEachObject(this, subject, relation,
3767 new AsyncMultiProcedureAdapter<Resource>() {
3770 public void execute(AsyncReadGraph graph,
3771 final Resource object) {
3773 checkedProcedure.inc();
3775 graph.forValue(object, binding, new AsyncProcedure<Object>() {
3778 public void execute(AsyncReadGraph graph,
3781 checkedProcedure.offer(graph,
3783 checkedProcedure.dec(graph);
3787 public void exception(AsyncReadGraph graph,
3789 checkedProcedure.exception(graph, t);
3793 public String toString() {
3794 return "forRelatedValue -> "
3803 public void finished(AsyncReadGraph graph) {
3804 checkedProcedure.dec(graph);
3808 public void exception(AsyncReadGraph graph, Throwable t) {
3809 checkedProcedure.exception(graph, t);
3817 public <T> void forRelatedValue(Resource subject, Resource relation,
3818 Binding binding, SyncProcedure<T> procedure) {
3819 forRelatedValue(subject, relation, binding,
3820 new SyncToAsyncProcedure<T>(procedure));
3824 public <T> void forRelatedValue(Resource subject, Resource relation,
3825 Binding binding, Procedure<T> procedure) {
3826 forRelatedValue(subject, relation, binding,
3827 new NoneToAsyncProcedure<T>(procedure));
3831 public <T> void forAdapted(Resource resource, Class<T> clazz,
3832 AsyncListener<T> listener) {
3833 asyncRequest(new Adapter<T>(resource, clazz), listener);
3837 public <T> void forAdapted(Resource resource, Class<T> clazz,
3838 SyncListener<T> listener) {
3839 asyncRequest(new Adapter<T>(resource, clazz), listener);
3843 public <T> void forAdapted(Resource resource, Class<T> clazz,
3844 Listener<T> listener) {
3845 asyncRequest(new Adapter<T>(resource, clazz), listener);
3849 final public <T> void forAdapted(final Resource resource,
3850 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3852 assert (resource != null);
3853 assert (clazz != null);
3854 assert (procedure != null);
3856 final AdaptionService service = getSession().peekService(AdaptionService.class);
3857 if (service == null)
3858 procedure.exception(this, new ServiceException("No AdaptionService available"));
3860 service.adapt(this, resource, resource, Resource.class, clazz, false, procedure);
3865 public <T> void forAdapted(Resource resource, Class<T> clazz,
3866 SyncProcedure<T> procedure) {
3867 forAdapted(resource, clazz, new SyncToAsyncProcedure<T>(procedure));
3871 public <T> void forAdapted(Resource resource, Class<T> clazz,
3872 Procedure<T> procedure) {
3873 forAdapted(resource, clazz, new NoneToAsyncProcedure<T>(procedure));
3877 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3878 AsyncListener<T> listener) {
3879 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3883 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3884 SyncListener<T> listener) {
3885 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3889 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3890 Listener<T> listener) {
3891 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3895 final public <T> void forUniqueAdapted(final Resource resource,
3896 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3898 assert (resource != null);
3899 assert (clazz != null);
3900 assert (procedure != null);
3902 final AdaptionService service = getSession().peekService(AdaptionService.class);
3903 if (service == null)
3904 procedure.exception(this, new ServiceException("No AdaptionService available"));
3906 service.adaptNew(this, resource, clazz, false, procedure);
3911 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3912 SyncProcedure<T> procedure) {
3913 forUniqueAdapted(resource, clazz,
3914 new SyncToAsyncProcedure<T>(procedure));
3918 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3919 Procedure<T> procedure) {
3920 forUniqueAdapted(resource, clazz,
3921 new NoneToAsyncProcedure<T>(procedure));
3925 public void forPossibleInverse(Resource subject,
3926 AsyncListener<Resource> listener) {
3927 asyncRequest(new PossibleInverse(subject), listener);
3931 public void forPossibleInverse(Resource subject,
3932 SyncListener<Resource> listener) {
3933 asyncRequest(new PossibleInverse(subject), listener);
3937 public void forPossibleInverse(Resource subject, Listener<Resource> listener) {
3938 asyncRequest(new PossibleInverse(subject), listener);
3942 final public void forPossibleInverse(final Resource relation,
3943 final AsyncProcedure<Resource> procedure) {
3945 assert (relation != null);
3946 assert (procedure != null);
3948 processor.forInverse(this, relation, new ExceptionToNullProcedure<Resource>(procedure));
3953 public void forPossibleInverse(Resource subject,
3954 SyncProcedure<Resource> procedure) {
3955 forPossibleInverse(subject, new SyncToAsyncProcedure<Resource>(
3960 public void forPossibleInverse(Resource subject,
3961 Procedure<Resource> procedure) {
3962 forPossibleInverse(subject, new NoneToAsyncProcedure<Resource>(
3967 public void forPossibleObject(Resource subject, Resource relation,
3968 AsyncListener<Resource> listener) {
3969 asyncRequest(new PossibleObject(subject, relation), listener);
3973 public void forPossibleObject(Resource subject, Resource relation,
3974 SyncListener<Resource> listener) {
3975 asyncRequest(new PossibleObject(subject, relation), listener);
3979 public void forPossibleObject(Resource subject, Resource relation,
3980 Listener<Resource> listener) {
3981 asyncRequest(new PossibleObject(subject, relation), listener);
3985 final public void forPossibleObject(final Resource subject,
3986 final Resource relation, final AsyncProcedure<Resource> procedure) {
3988 assert (subject != null);
3989 assert (relation != null);
3990 assert (procedure != null);
3992 processor.forEachObject(this, subject, relation,
3993 new SingleOrNullProcedure<Resource>(procedure));
3998 public void forPossibleObject(Resource subject, Resource relation,
3999 SyncProcedure<Resource> procedure) {
4000 forPossibleObject(subject, relation,
4001 new SyncToAsyncProcedure<Resource>(procedure));
4005 public void forPossibleObject(Resource subject, Resource relation,
4006 Procedure<Resource> procedure) {
4007 forPossibleObject(subject, relation,
4008 new NoneToAsyncProcedure<Resource>(procedure));
4012 public void forPossibleStatement(Resource subject, Resource relation,
4013 AsyncListener<Statement> listener) {
4014 asyncRequest(new PossibleStatement(subject, relation), listener);
4018 public void forPossibleStatement(Resource subject, Resource relation,
4019 SyncListener<Statement> listener) {
4020 asyncRequest(new PossibleStatement(subject, relation), listener);
4024 public void forPossibleStatement(Resource subject, Resource relation,
4025 Listener<Statement> listener) {
4026 asyncRequest(new PossibleStatement(subject, relation), listener);
4030 final public void forPossibleStatement(final Resource subject,
4031 final Resource relation, final AsyncProcedure<Statement> procedure) {
4033 assert (subject != null);
4034 assert (relation != null);
4035 assert (procedure != null);
4037 processor.forEachStatement(this, subject, relation,
4038 new SingleFunctionalOrNullProcedure<Statement>(
4039 "forPossibleStatement", procedure));
4044 public void forPossibleStatement(Resource subject, Resource relation,
4045 SyncProcedure<Statement> procedure) {
4046 forPossibleStatement(subject, relation,
4047 new SyncToAsyncProcedure<Statement>(procedure));
4051 public void forPossibleStatement(Resource subject, Resource relation,
4052 Procedure<Statement> procedure) {
4053 forPossibleStatement(subject, relation,
4054 new NoneToAsyncProcedure<Statement>(procedure));
4058 public void forPossibleType(Resource subject, Resource relation,
4059 AsyncListener<Resource> listener) {
4060 asyncRequest(new PossibleType(subject, relation), listener);
4064 public void forPossibleType(Resource subject, Resource relation,
4065 SyncListener<Resource> listener) {
4066 asyncRequest(new PossibleType(subject, relation), listener);
4070 public void forPossibleType(Resource subject, Resource relation,
4071 Listener<Resource> listener) {
4072 asyncRequest(new PossibleType(subject, relation), listener);
4076 final public void forPossibleType(final Resource subject,
4077 final Resource baseType, final AsyncProcedure<Resource> procedure) {
4079 assert (subject != null);
4080 assert (procedure != null);
4082 final NullSingleOrNullProcedure<Resource> checkedProcedure = new NullSingleOrNullProcedure<Resource>(procedure);
4084 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
4087 public void execute(AsyncReadGraph graph,
4088 final Resource principalType) {
4090 if (baseType == null) {
4092 checkedProcedure.offer(graph, principalType);
4094 } else if (principalType.equals(baseType)) {
4096 checkedProcedure.offer(graph, principalType);
4100 checkedProcedure.inc();
4102 processor.forSupertypes((ReadGraphImpl)graph, principalType,
4103 new AsyncProcedure<Set<Resource>>() {
4106 public void execute(
4107 AsyncReadGraph graph,
4108 Set<Resource> result) {
4110 if (result.contains(baseType)) {
4111 checkedProcedure.offer(graph,
4115 checkedProcedure.dec(graph);
4120 public void exception(
4121 AsyncReadGraph graph,
4123 checkedProcedure.exception(graph, t);
4124 checkedProcedure.dec(graph);
4128 public String toString() {
4129 return "forPossibleType -> "
4140 public void finished(AsyncReadGraph graph) {
4141 checkedProcedure.dec(graph);
4145 public void exception(AsyncReadGraph graph, Throwable t) {
4146 checkedProcedure.exception(graph, t);
4147 checkedProcedure.dec(graph);
4155 public void forPossibleType(Resource subject, Resource relation,
4156 SyncProcedure<Resource> procedure) {
4157 forPossibleType(subject, relation, new SyncToAsyncProcedure<Resource>(
4162 public void forPossibleType(Resource subject, Resource relation,
4163 Procedure<Resource> procedure) {
4164 forPossibleType(subject, relation, new NoneToAsyncProcedure<Resource>(
4169 public <T> void forPossibleValue(Resource subject, AsyncListener<T> listener) {
4170 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4174 public <T> void forPossibleValue(Resource subject, SyncListener<T> listener) {
4175 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4179 public <T> void forPossibleValue(Resource subject, Listener<T> listener) {
4180 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4184 final public <T> void forPossibleValue(final Resource subject,
4185 final AsyncProcedure<T> procedure) {
4187 assert (subject != null);
4188 assert (procedure != null);
4190 forPossibleRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
4193 public void execute(AsyncReadGraph graph, final Datatype type) {
4195 procedure.execute(graph, null);
4198 // TODO: consider trying Bindings.getBeanBinding(type);
4199 Binding binding = Bindings.getBinding(type);
4200 graph.forPossibleValue(subject, binding, procedure);
4201 } catch (RuntimeBindingConstructionException e) {
4202 procedure.exception(graph, e);
4208 public void exception(AsyncReadGraph graph, Throwable t) {
4209 procedure.exception(graph, t);
4213 public String toString() {
4214 return "forPossibleValue -> " + procedure;
4222 public <T> void forPossibleValue(Resource subject,
4223 SyncProcedure<T> procedure) {
4224 forPossibleValue(subject, new SyncToAsyncProcedure<T>(procedure));
4228 public <T> void forPossibleValue(Resource subject, Procedure<T> procedure) {
4229 forPossibleValue(subject, new NoneToAsyncProcedure<T>(procedure));
4233 public <T> void forPossibleValue(Resource subject, Binding binding,
4234 AsyncListener<T> listener) {
4235 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4239 public <T> void forPossibleValue(Resource subject, Binding binding,
4240 SyncListener<T> listener) {
4241 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4245 public <T> void forPossibleValue(Resource subject, Binding binding,
4246 Listener<T> listener) {
4247 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4251 final public <T> void forPossibleValue(final Resource resource,
4252 final Binding binding, final AsyncProcedure<T> procedure) {
4254 assert (resource != null);
4255 assert (binding != null);
4256 assert (procedure != null);
4258 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
4261 public void execute(AsyncReadGraph graph, byte[] result) {
4265 if (result == null) {
4266 procedure.execute(graph, null);
4270 Serializer serializer = Bindings.getSerializer( binding );
4271 Object obj = serializer.deserialize(result);
4272 if (!binding.isInstance(obj))
4273 procedure.exception(graph, new ClassCastException(
4274 "Cannot get value " + obj + " with binding "
4277 procedure.execute(graph, (T) obj);
4279 } catch (Throwable t) {
4280 procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
4285 public void exception(AsyncReadGraph graph, Throwable t) {
4287 procedure.exception(graph, t);
4288 } catch (Throwable t2) {
4289 Logger.defaultLogError(t2);
4294 public String toString() {
4295 return "forPossibleValue -> " + procedure;
4303 public <T> void forPossibleValue(Resource subject, Binding binding,
4304 SyncProcedure<T> procedure) {
4305 forPossibleValue(subject, binding, new SyncToAsyncProcedure<T>(
4310 public <T> void forPossibleValue(Resource subject, Binding binding,
4311 Procedure<T> procedure) {
4312 forPossibleValue(subject, binding, new NoneToAsyncProcedure<T>(
4317 public <T> void forPossibleRelatedValue(Resource subject,
4318 Resource relation, AsyncListener<T> listener) {
4319 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4324 public <T> void forPossibleRelatedValue(Resource subject,
4325 Resource relation, SyncListener<T> listener) {
4326 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4331 public <T> void forPossibleRelatedValue(Resource subject,
4332 Resource relation, Listener<T> listener) {
4333 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4338 final public <T> void forPossibleRelatedValue(final Resource subject,
4339 final Resource relation, final AsyncProcedure<T> procedure) {
4341 assert (subject != null);
4342 assert (relation != null);
4343 assert (procedure != null);
4345 final DeepSingleOrNullProcedure<T> checkedProcedure = new DeepSingleOrNullProcedure<T>(procedure);
4347 processor.forEachObject(this, subject, relation,
4348 new AsyncMultiProcedureAdapter<Resource>() {
4351 public void execute(AsyncReadGraph graph,
4352 final Resource object) {
4354 checkedProcedure.inc();
4356 graph.forValue(object, new AsyncProcedure<Object>() {
4359 public void execute(AsyncReadGraph graph,
4361 checkedProcedure.offer(graph, (T) result);
4362 checkedProcedure.dec(graph);
4366 public void exception(AsyncReadGraph graph,
4368 checkedProcedure.exception(graph, t);
4369 checkedProcedure.dec(graph);
4377 public void finished(AsyncReadGraph graph) {
4379 checkedProcedure.dec(graph);
4383 public void exception(AsyncReadGraph graph, Throwable t) {
4384 checkedProcedure.exception(graph, t);
4385 checkedProcedure.dec(graph);
4389 public String toString() {
4390 return "forPossibleRelatedValue -> " + procedure;
4397 public <T> void forPossibleRelatedValue(Resource subject,
4398 Resource relation, SyncProcedure<T> procedure) {
4399 forPossibleRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
4404 public <T> void forPossibleRelatedValue(Resource subject,
4405 Resource relation, Procedure<T> procedure) {
4406 forPossibleRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
4411 public <T> void forPossibleRelatedValue(Resource subject,
4412 Resource relation, Binding binding, AsyncListener<T> listener) {
4413 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4418 public <T> void forPossibleRelatedValue(Resource subject,
4419 Resource relation, Binding binding, SyncListener<T> listener) {
4420 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4425 public <T> void forPossibleRelatedValue(Resource subject,
4426 Resource relation, Binding binding, Listener<T> listener) {
4427 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4431 final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4432 final AsyncProcedure<T> procedure) {
4434 assert (subject != null);
4435 assert (relation != null);
4436 assert (procedure != null);
4438 processor.forPossibleObject(this, subject, relation, new AsyncProcedure<Resource>() {
4441 public void execute(AsyncReadGraph graph, Resource object) {
4443 if(object == null) {
4444 procedure.execute(graph, null);
4448 processor.forPossibleValue((ReadGraphImpl)graph, object, new AsyncProcedure<byte[]>() {
4451 public void execute(AsyncReadGraph graph, byte[] bytes) {
4457 Serializer serializer = binding.serializer();
4458 Object obj = serializer.deserialize(bytes);
4459 if (!binding.isInstance(obj)) {
4460 procedure.exception(graph, new ClassCastException("Cannot get value " + obj + " with binding " + binding));
4462 procedure.execute(graph, (T) obj);
4465 } catch (Throwable t) {
4467 procedure.exception(graph, t);
4473 procedure.execute(graph, null);
4480 public void exception(AsyncReadGraph graph, Throwable t) {
4481 procedure.exception(graph, t);
4489 public void exception(AsyncReadGraph graph, Throwable throwable) {
4490 throwable.printStackTrace();
4491 procedure.exception(graph, throwable);
4499 public <T> void forPossibleRelatedValue(Resource subject,
4500 Resource relation, Binding binding, SyncProcedure<T> procedure) {
4501 forPossibleRelatedValue(subject, relation, binding,
4502 new SyncToAsyncProcedure<T>(procedure));
4506 public <T> void forPossibleRelatedValue(Resource subject,
4507 Resource relation, Binding binding, Procedure<T> procedure) {
4508 forPossibleRelatedValue(subject, relation, binding,
4509 new NoneToAsyncProcedure<T>(procedure));
4513 public void forIsInstanceOf(Resource subject, Resource relation,
4514 AsyncListener<Boolean> listener) {
4515 asyncRequest(new IsInstanceOf(subject, relation), listener);
4519 public void forIsInstanceOf(Resource subject, Resource relation,
4520 SyncListener<Boolean> listener) {
4521 asyncRequest(new IsInstanceOf(subject, relation), listener);
4525 public void forIsInstanceOf(Resource subject, Resource relation,
4526 Listener<Boolean> listener) {
4527 asyncRequest(new IsInstanceOf(subject, relation), listener);
4531 final public void forIsInstanceOf(final Resource resource,
4532 final Resource type, final AsyncProcedure<Boolean> procedure) {
4534 assert (resource != null);
4535 assert (type != null);
4536 assert (procedure != null);
4538 forTypes(resource, new AsyncProcedure<Set<Resource>>() {
4541 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4544 if (result.contains(type))
4545 procedure.execute(graph, true);
4547 procedure.execute(graph, false);
4548 } catch (Throwable t) {
4549 Logger.defaultLogError(t);
4554 public void exception(AsyncReadGraph graph, Throwable t) {
4556 procedure.exception(graph, t);
4557 } catch (Throwable t2) {
4558 Logger.defaultLogError(t2);
4563 public String toString() {
4564 return "forIsInstanceOf -> " + procedure;
4572 public void forIsInstanceOf(Resource subject, Resource relation,
4573 SyncProcedure<Boolean> procedure) {
4574 forIsInstanceOf(subject, relation, new SyncToAsyncProcedure<Boolean>(
4579 public void forIsInstanceOf(Resource subject, Resource relation,
4580 Procedure<Boolean> procedure) {
4581 forIsInstanceOf(subject, relation, new NoneToAsyncProcedure<Boolean>(
4586 public void forIsInheritedFrom(Resource subject, Resource relation,
4587 AsyncListener<Boolean> listener) {
4588 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4592 public void forIsInheritedFrom(Resource subject, Resource relation,
4593 SyncListener<Boolean> listener) {
4594 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4598 public void forIsInheritedFrom(Resource subject, Resource relation,
4599 Listener<Boolean> listener) {
4600 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4604 final public void forIsInheritedFrom(final Resource resource,
4605 final Resource type, final AsyncProcedure<Boolean> procedure) {
4607 assert (resource != null);
4608 assert (type != null);
4609 assert (procedure != null);
4611 if (resource.equals(type)) {
4613 procedure.execute(this, true);
4614 } catch (Throwable t) {
4615 Logger.defaultLogError(t);
4620 forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
4623 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4625 if (result.contains(type))
4626 procedure.execute(graph, true);
4628 procedure.execute(graph, false);
4629 } catch (Throwable t) {
4630 Logger.defaultLogError(t);
4635 public void exception(AsyncReadGraph graph, Throwable t) {
4637 procedure.exception(graph, t);
4638 } catch (Throwable t2) {
4639 Logger.defaultLogError(t2);
4644 public String toString() {
4645 return "forIsInheritedFrom -> " + procedure;
4653 public void forIsInheritedFrom(Resource subject, Resource relation,
4654 SyncProcedure<Boolean> procedure) {
4655 forIsInheritedFrom(subject, relation,
4656 new SyncToAsyncProcedure<Boolean>(procedure));
4660 public void forIsInheritedFrom(Resource subject, Resource relation,
4661 Procedure<Boolean> procedure) {
4662 forIsInheritedFrom(subject, relation,
4663 new NoneToAsyncProcedure<Boolean>(procedure));
4667 public void forIsSubrelationOf(Resource subject, Resource relation,
4668 AsyncListener<Boolean> listener) {
4669 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4673 public void forIsSubrelationOf(Resource subject, Resource relation,
4674 SyncListener<Boolean> listener) {
4675 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4679 public void forIsSubrelationOf(Resource subject, Resource relation,
4680 Listener<Boolean> listener) {
4681 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4685 final public void forIsSubrelationOf(final Resource resource,
4686 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4688 assert (resource != null);
4689 assert (relation != null);
4690 assert (procedure != null);
4692 if (resource.equals(relation)) {
4693 procedure.execute(this, true);
4697 forSuperrelations(resource, new AsyncProcedure<Set<Resource>>() {
4700 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4702 if (result.contains(relation))
4703 procedure.execute(graph, true);
4705 procedure.execute(graph, false);
4706 } catch (Throwable t) {
4707 Logger.defaultLogError(t);
4712 public void exception(AsyncReadGraph graph, Throwable t) {
4714 procedure.exception(graph, t);
4715 } catch (Throwable t2) {
4716 Logger.defaultLogError(t2);
4721 public String toString() {
4722 return "forIsSubrelationOf -> " + procedure;
4730 public void forIsSubrelationOf(Resource subject, Resource relation,
4731 SyncProcedure<Boolean> procedure) {
4732 forIsSubrelationOf(subject, relation,
4733 new SyncToAsyncProcedure<Boolean>(procedure));
4737 public void forIsSubrelationOf(Resource subject, Resource relation,
4738 Procedure<Boolean> procedure) {
4739 forIsSubrelationOf(subject, relation,
4740 new NoneToAsyncProcedure<Boolean>(procedure));
4744 public void forHasStatement(Resource subject,
4745 AsyncListener<Boolean> listener) {
4746 asyncRequest(new HasStatementSubject(subject), listener);
4750 public void forHasStatement(Resource subject, SyncListener<Boolean> listener) {
4751 asyncRequest(new HasStatementSubject(subject), listener);
4755 public void forHasStatement(Resource subject, Listener<Boolean> listener) {
4756 asyncRequest(new HasStatementSubject(subject), listener);
4760 final public void forHasStatement(final Resource subject,
4761 final AsyncProcedure<Boolean> procedure) {
4763 assert (subject != null);
4764 assert (procedure != null);
4766 processor.forHasStatement(this, subject, procedure);
4771 public void forHasStatement(Resource subject,
4772 SyncProcedure<Boolean> procedure) {
4773 forHasStatement(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4777 public void forHasStatement(Resource subject, Procedure<Boolean> procedure) {
4778 forHasStatement(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4782 public void forHasStatement(Resource subject, Resource relation,
4783 AsyncListener<Boolean> listener) {
4784 asyncRequest(new HasStatement(subject, relation), listener);
4788 public void forHasStatement(Resource subject, Resource relation,
4789 SyncListener<Boolean> listener) {
4790 asyncRequest(new HasStatement(subject, relation), listener);
4794 public void forHasStatement(Resource subject, Resource relation,
4795 Listener<Boolean> listener) {
4796 asyncRequest(new HasStatement(subject, relation), listener);
4800 final public void forHasStatement(final Resource subject,
4801 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4803 assert (subject != null);
4804 assert (relation != null);
4805 assert (procedure != null);
4807 processor.forHasStatement(this, subject, relation, procedure);
4812 public void forHasStatement(Resource subject, Resource relation,
4813 SyncProcedure<Boolean> procedure) {
4814 forHasStatement(subject, relation, new SyncToAsyncProcedure<Boolean>(
4819 public void forHasStatement(Resource subject, Resource relation,
4820 Procedure<Boolean> procedure) {
4821 forHasStatement(subject, relation, new NoneToAsyncProcedure<Boolean>(
4826 public void forHasStatement(Resource subject, Resource relation,
4827 Resource object, AsyncListener<Boolean> listener) {
4828 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4833 public void forHasStatement(Resource subject, Resource relation,
4834 Resource object, SyncListener<Boolean> listener) {
4835 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4840 public void forHasStatement(Resource subject, Resource relation,
4841 Resource object, Listener<Boolean> listener) {
4842 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4847 final public void forHasStatement(final Resource subject,
4848 final Resource relation, final Resource object,
4849 final AsyncProcedure<Boolean> procedure) {
4851 assert (subject != null);
4852 assert (relation != null);
4853 assert (object != null);
4854 assert (procedure != null);
4856 processor.forHasStatement(this, subject, relation, object, procedure);
4861 public void forHasStatement(Resource subject, Resource relation,
4862 Resource object, SyncProcedure<Boolean> procedure) {
4863 forHasStatement(subject, relation, object,
4864 new SyncToAsyncProcedure<Boolean>(procedure));
4868 public void forHasStatement(Resource subject, Resource relation,
4869 Resource object, Procedure<Boolean> procedure) {
4870 forHasStatement(subject, relation, object,
4871 new NoneToAsyncProcedure<Boolean>(procedure));
4875 public void forHasValue(Resource subject, AsyncListener<Boolean> listener) {
4876 asyncRequest(new HasValue(subject), listener);
4880 public void forHasValue(Resource subject, SyncListener<Boolean> listener) {
4881 asyncRequest(new HasValue(subject), listener);
4885 public void forHasValue(Resource subject, Listener<Boolean> listener) {
4886 asyncRequest(new HasValue(subject), listener);
4890 final public void forHasValue(final Resource subject,
4891 final AsyncProcedure<Boolean> procedure) {
4893 assert (subject != null);
4894 assert (procedure != null);
4896 processor.forValue(this, subject, new AsyncProcedure<byte[]>() {
4899 public void execute(AsyncReadGraph graph, byte[] result) {
4902 procedure.execute(graph, false);
4904 procedure.execute(graph, true);
4905 } catch (Throwable t) {
4906 Logger.defaultLogError(t);
4911 public void exception(AsyncReadGraph graph, Throwable t) {
4913 procedure.exception(graph, t);
4914 } catch (Throwable t2) {
4915 Logger.defaultLogError(t2);
4920 public String toString() {
4921 return "forHasValue -> " + procedure;
4929 public void forHasValue(Resource subject, SyncProcedure<Boolean> procedure) {
4930 forHasValue(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4934 public void forHasValue(Resource subject, Procedure<Boolean> procedure) {
4935 forHasValue(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4939 public void forOrderedSet(Resource subject,
4940 AsyncMultiListener<Resource> listener) {
4941 asyncRequest(new OrderedSet(subject), listener);
4945 public void forOrderedSet(Resource subject,
4946 SyncMultiListener<Resource> listener) {
4947 asyncRequest(new OrderedSet(subject), listener);
4951 public void forOrderedSet(Resource subject, MultiListener<Resource> listener) {
4952 asyncRequest(new OrderedSet(subject), listener);
4956 final public void forOrderedSet(final Resource subject,
4957 final AsyncMultiProcedure<Resource> procedure) {
4959 assert (subject != null);
4960 assert (procedure != null);
4962 processor.forOrderedSet(this, subject,
4963 new AsyncMultiProcedure<Resource>() {
4966 public void finished(AsyncReadGraph graph) {
4968 procedure.finished(graph);
4969 } catch (Throwable t) {
4970 Logger.defaultLogError(t);
4975 public void execute(AsyncReadGraph graph, Resource result) {
4977 procedure.execute(graph, result);
4978 } catch (Throwable t) {
4979 Logger.defaultLogError(t);
4984 public void exception(AsyncReadGraph graph, Throwable t) {
4986 procedure.exception(graph, t);
4987 } catch (Throwable t2) {
4988 Logger.defaultLogError(t2);
4993 public String toString() {
4994 return "forOrderedSet -> " + procedure;
5002 public void forOrderedSet(Resource subject,
5003 SyncMultiProcedure<Resource> procedure) {
5004 forOrderedSet(subject, new SyncToAsyncMultiProcedure<Resource>(
5009 public void forOrderedSet(Resource subject,
5010 MultiProcedure<Resource> procedure) {
5011 forOrderedSet(subject, new NoneToAsyncMultiProcedure<Resource>(
5016 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5017 AsyncListener<T> listener) {
5018 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5022 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5023 SyncListener<T> listener) {
5024 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5028 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5029 Listener<T> listener) {
5030 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5034 final public <T> void forPossibleAdapted(final Resource resource,
5035 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5037 assert (resource != null);
5038 assert (clazz != null);
5039 assert (procedure != null);
5041 final AdaptionService service = getSession().peekService(AdaptionService.class);
5042 if (service == null)
5043 procedure.exception(this, new ServiceException("No AdaptionService available"));
5045 service.adapt(this, resource, resource, Resource.class, clazz, true, procedure);
5049 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5050 SyncProcedure<T> procedure) {
5051 forPossibleAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5056 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5057 Procedure<T> procedure) {
5058 forPossibleAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5063 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5064 AsyncListener<T> listener) {
5065 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5069 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5070 SyncListener<T> listener) {
5071 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5075 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5076 Listener<T> listener) {
5077 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5081 final public <T> void forPossibleUniqueAdapted(final Resource resource,
5082 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5084 assert (resource != null);
5085 assert (clazz != null);
5086 assert (procedure != null);
5088 final AdaptionService service = getSession().peekService(AdaptionService.class);
5089 if (service == null)
5090 procedure.exception(this, new ServiceException("No AdaptionService available"));
5092 service.adaptNew(this, resource, clazz, true, procedure);
5097 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5098 SyncProcedure<T> procedure) {
5099 forPossibleUniqueAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5104 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5105 Procedure<T> procedure) {
5106 forPossibleUniqueAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5111 * Implementation of the interface AsyncRequestProcessor
5115 final public Session getSession() {
5116 return processor.getSession();
5120 public <T> void asyncRequest(final Read<T> request) {
5122 asyncRequest(request, new AsyncProcedure<T>() {
5125 public void execute(AsyncReadGraph graph, T result) {
5129 public void exception(AsyncReadGraph graph, Throwable t) {
5130 Logger.defaultLogError(t);
5134 public String toString() {
5135 return "asyncRequest(Read) -> " + request;
5143 public <T> void asyncRequest(Read<T> request, AsyncListener<T> procedure) {
5144 asyncRequest(request, (AsyncProcedure<T>) procedure);
5148 public <T> void asyncRequest(Read<T> request,
5149 final SyncListener<T> procedure) {
5150 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5154 public <T> void asyncRequest(Read<T> request, final Listener<T> procedure) {
5155 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5159 public <T> void asyncRequest(final Read<T> request, final AsyncProcedure<T> procedure) {
5161 assert (request != null);
5162 assert (procedure != null);
5164 processor.scheduleNow(new SessionTask(this) {
5167 public void run0(int thread) {
5169 final ListenerBase listener = getListenerBase(procedure);
5170 QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5171 } catch (DatabaseException e) {
5172 Logger.defaultLogError(e);
5180 public static ReadGraphImpl createAsync(QueryProcessor support) {
5181 return new ReadGraphImpl(null, null, support);
5185 public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
5186 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5190 public <T> void asyncRequest(Read<T> request, final Procedure<T> procedure) {
5191 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5195 final public <T> void asyncRequest(final AsyncRead<T> request) {
5197 assert (request != null);
5199 asyncRequest(request, new AsyncProcedure<T>() {
5202 public void execute(AsyncReadGraph graph, T result) {
5206 public void exception(AsyncReadGraph graph, Throwable t) {
5207 Logger.defaultLogError(t);
5211 public String toString() {
5212 return "asyncRequest(AsyncRead) -> " + request;
5220 public <T> void asyncRequest(AsyncRead<T> request,
5221 AsyncListener<T> procedure) {
5222 asyncRequest(request, (AsyncProcedure<T>) procedure);
5226 final public <T> void asyncRequest(AsyncRead<T> request,
5227 final SyncListener<T> procedure) {
5228 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5232 final public <T> void asyncRequest(AsyncRead<T> request,
5233 final Listener<T> procedure) {
5234 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5238 final public <T> void asyncRequest(final AsyncRead<T> request,
5239 final AsyncProcedure<T> procedure) {
5241 assert (request != null);
5242 assert (procedure != null);
5244 ITask task = ThreadLogger.task(request);
5246 processor.scheduleNow(new SessionTask(this) {
5249 public void run0(int thread) {
5251 final ListenerBase listener = getListenerBase(procedure);
5252 QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, new AsyncProcedure<T>() {
5255 public void execute(AsyncReadGraph graph, T result) {
5257 procedure.execute(graph, result);
5261 public void exception(AsyncReadGraph graph, Throwable throwable) {
5263 procedure.exception(graph, throwable);
5267 } catch (DatabaseException e) {
5268 Logger.defaultLogError(e);
5277 public <T> void asyncRequest(AsyncRead<T> request,
5278 SyncProcedure<T> procedure) {
5279 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5283 final public <T> void asyncRequest(final AsyncRead<T> request,
5284 final Procedure<T> procedure) {
5285 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5289 public <T> void asyncRequest(final MultiRead<T> request) {
5291 assert (request != null);
5293 asyncRequest(request, new SyncMultiProcedureAdapter<T>() {
5295 public void exception(ReadGraph graph, Throwable t) {
5296 Logger.defaultLogError(t);
5300 public String toString() {
5301 return "asyncRequest(MultiRead) -> " + request;
5308 public <T> void asyncRequest(MultiRead<T> request,
5309 SyncMultiListener<T> procedure) {
5310 asyncRequest(request, (SyncMultiProcedure<T>)procedure);
5314 public <T> void asyncRequest(MultiRead<T> request,
5315 MultiListener<T> procedure) {
5316 asyncRequest(request, new NoneToSyncMultiListener<T>(procedure));
5321 public <T> void asyncRequest(final MultiRead<T> request,
5322 final SyncMultiProcedure<T> procedure) {
5324 assert (request != null);
5325 assert (procedure != null);
5327 final ListenerBase listener = getListenerBase(procedure);
5329 if (parent != null || listener != null) {
5331 // final ReadGraphImpl newGraph = newSync();
5332 processor.query(this, request, parent, procedure,listener);
5336 // final ReadGraphImpl newGraph = newSync();
5340 request.perform(this, procedure);
5342 } catch (Throwable t) {
5345 procedure.exception(this, t);
5346 } catch (DatabaseException e) {
5347 LOGGER.error("Unexpected exception while handling exception", e);
5357 public <T> void asyncRequest(MultiRead<T> request,
5358 MultiProcedure<T> procedure) {
5359 asyncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
5363 final public <T> void asyncRequest(final AsyncMultiRead<T> request) {
5365 assert (request != null);
5367 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5369 public void exception(AsyncReadGraph graph, Throwable t) {
5370 Logger.defaultLogError(t);
5374 public String toString() {
5375 return "asyncRequest(AsyncMultiRead) -> " + request;
5382 public <T> void asyncRequest(AsyncMultiRead<T> request,
5383 AsyncMultiListener<T> procedure) {
5384 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5388 public <T> void asyncRequest(AsyncMultiRead<T> request,
5389 SyncMultiListener<T> procedure) {
5390 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5394 public <T> void asyncRequest(AsyncMultiRead<T> request,
5395 MultiListener<T> procedure) {
5396 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5400 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5401 final AsyncMultiProcedure<T> procedure) {
5403 assert (request != null);
5404 assert (procedure != null);
5406 ListenerBase listener = getListenerBase(procedure);
5408 if (parent != null || listener != null) {
5410 processor.query(this, request, parent, procedure, listener);
5416 request.perform(this, new AsyncMultiProcedure<T>() {
5419 public void execute(AsyncReadGraph graph, T result) {
5420 procedure.execute(graph, result);
5424 public void finished(AsyncReadGraph graph) {
5425 procedure.finished(graph);
5429 public void exception(AsyncReadGraph graph, Throwable t) {
5430 procedure.exception(graph, t);
5434 public String toString() {
5435 return "asyncRequest(AsyncMultiRead) -> " + procedure;
5440 } catch (Throwable t) {
5442 procedure.exception(this, new DatabaseException(t));
5450 public <T> void asyncRequest(AsyncMultiRead<T> request,
5451 SyncMultiProcedure<T> procedure) {
5452 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5456 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5457 final MultiProcedure<T> procedure) {
5458 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5462 final public <T> void asyncRequest(final ExternalRead<T> request) {
5464 assert (request != null);
5466 asyncRequest(request, new Procedure<T>() {
5469 public void execute(T result) {
5473 public void exception(Throwable t) {
5474 Logger.defaultLogError(t);
5478 public String toString() {
5479 return "asyncRequest(PrimitiveRead) -> " + request;
5487 public <T> void asyncRequest(ExternalRead<T> request,
5488 final Listener<T> procedure) {
5489 asyncRequest(request, (Procedure<T>) procedure);
5493 final public <T> void asyncRequest(final ExternalRead<T> request,
5494 final Procedure<T> procedure) {
5496 assert (request != null);
5497 assert (procedure != null);
5499 final ListenerBase listener = getListenerBase(procedure);
5501 if (parent != null || listener != null) {
5504 QueryCacheBase.resultExternalReadEntry(this, request, parent, listener, procedure);
5505 } catch (DatabaseException e) {
5506 Logger.defaultLogError(e);
5507 // This throwable has already been transferred to procedure at this point - do nothing about it
5512 request.register(this, new Listener<T>() {
5514 public void execute(T result) {
5515 procedure.execute(result);
5519 public void exception(Throwable t) {
5520 procedure.exception(t);
5524 public String toString() {
5525 return "asyncRequest(PrimitiveRead) -> " + request;
5529 public boolean isDisposed() {
5540 public void asyncRequest(final Write request) {
5542 assert (request != null);
5544 getSession().asyncRequest(request);
5546 // processor.asyncWrite(request);
5551 public <T> void asyncRequest(final WriteResult<T> request, Procedure<T> procedure) {
5552 throw new Error("Not implemented.");
5556 public void asyncRequest(Write request, Consumer<DatabaseException> callback) {
5558 assert (request != null);
5560 getSession().asyncRequest(request, callback);
5565 public void asyncRequest(final DelayedWrite request) {
5567 assert (request != null);
5569 getSession().asyncRequest(request);
5574 public <T> void asyncRequest(final DelayedWriteResult<T> request, Procedure<T> procedure) {
5575 throw new Error("Not implemented.");
5579 public void asyncRequest(DelayedWrite r,
5580 Consumer<DatabaseException> callback) {
5581 throw new Error("Not implemented.");
5585 public void asyncRequest(final WriteOnly request) {
5587 assert (request != null);
5589 getSession().asyncRequest(request);
5594 public <T> void asyncRequest(final WriteOnlyResult<T> request, Procedure<T> procedure) {
5595 throw new Error("Not implemented.");
5599 public void asyncRequest(WriteOnly r, Consumer<DatabaseException> callback) {
5600 throw new Error("Not implemented.");
5604 * Implementation of the interface ServiceLocator
5608 public <T> T getService(Class<T> api) {
5609 if(WriteSupport.class == api) {
5610 if(this instanceof WriteGraphImpl) {
5611 WriteGraphImpl impl = (WriteGraphImpl)this;
5612 return (T)impl.writeSupport;
5615 return getSession().getService(api);
5619 public <T> T peekService(Class<T> api) {
5620 return getSession().peekService(api);
5624 public boolean hasService(Class<?> api) {
5625 return getSession().hasService(api);
5629 public <T> void registerService(Class<T> api, T service) {
5630 getSession().registerService(api, service);
5634 public boolean isImmutable(Resource resource) throws DatabaseException {
5635 ResourceImpl impl = (ResourceImpl)resource;
5636 return processor.isImmutable(impl.id);
5643 protected static String INTERNAL_ERROR_STRING = "Transaction aborted due to internal client error.";
5646 * callerThread is the currently running thread state.syncThread is blocking for
5647 * this execution state.syncParent is the blocking request
5650 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support) {
5651 this.parentGraph = parentGraph;
5652 this.parent = parent;
5653 this.processor = support;
5654 this.asyncBarrier = new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent);
5657 ReadGraphImpl(ReadGraphImpl graph, CacheEntry parent) {
5658 this(graph, parent, graph.processor);
5661 ReadGraphImpl(ReadGraphImpl graph) {
5662 this(graph, graph.parent);
5665 public ReadGraphImpl withParent(CacheEntry parent) {
5666 return new ReadGraphImpl(this, parent);
5669 public ReadGraphImpl forRecompute(CacheEntry parent) {
5670 return new ReadGraphImpl(null, parent, processor);
5673 public static ReadGraphImpl create(QueryProcessor support) {
5674 return new ReadGraphImpl(null, null, support);
5677 public ReadGraphImpl newRestart(ReadGraphImpl impl) {
5679 WriteGraphImpl write = processor.getSession().getService(
5680 WriteGraphImpl.class);
5686 final private ListenerBase getListenerBase(final Object procedure) {
5687 if (procedure instanceof ListenerBase)
5688 return (ListenerBase) procedure;
5693 public <T> void waitAsyncProcedure(AsyncMultiReadProcedure<T> procedure) {
5695 assert(procedure.done());
5699 public <T> void waitAsyncProcedure(AsyncReadProcedure<T> procedure) {
5701 assert(procedure.done());
5705 public boolean resumeTasks() {
5706 return processor.resumeTasks(this);
5709 Class<?> singleClass(Set<Resource> types) {
5710 Class<?> result = null;
5711 for (Resource type : types) {
5712 Class<?> clazz = processor.getBuiltinValue(type);
5713 if (clazz != null) {
5723 private String debugString(Resource r) {
5726 name = getPossibleRelatedValue(r, processor.getL0(this).HasName);
5727 } catch (ManyObjectsForFunctionalRelationException e) {
5728 Logger.defaultLogError(e);
5729 } catch (ServiceException e) {
5730 Logger.defaultLogError(e);
5732 return "[" + name + " - " + r + "]";
5736 public String toString() {
5737 return "ReadGraphImpl[thread=" + Thread.currentThread() + "]";
5741 final public int thread() {
5745 static class MultiTripleIntProcedure implements TripleIntProcedure {
5747 final private AsyncMultiProcedure<Statement> procedure;
5748 final private ReadGraphImpl impl;
5749 final private QuerySupport support;
5751 public MultiTripleIntProcedure(AsyncMultiProcedure<Statement> procedure, ReadGraphImpl impl, QuerySupport support) {
5752 this.procedure = procedure;
5754 this.support = support;
5758 public void execute(ReadGraphImpl graph, int s, int p, int o) {
5760 procedure.execute(graph, support.getStatement(s, p, o));
5761 } catch (Throwable t2) {
5762 Logger.defaultLogError(t2);
5767 public void finished(ReadGraphImpl graph) {
5769 procedure.finished(graph);
5770 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5771 } catch (Throwable t2) {
5772 Logger.defaultLogError(t2);
5777 public void exception(ReadGraphImpl graph, Throwable t) {
5779 procedure.exception(graph, t);
5780 } catch (Throwable t2) {
5781 Logger.defaultLogError(t2);
5783 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5787 public String toString() {
5788 return "forEachObject with " + procedure;
5793 // private AsyncMultiProcedure<Resource> cacheKey = null;
5794 // private MultiIntProcedure cacheResult = null;
5796 // final IntProcedure forMultiProcedure(final AsyncMultiProcedure<Resource> procedure) {
5798 // if(procedure == cacheKey) return cacheResult;
5800 // cacheResult = new MultiIntProcedure(procedure, this, processor.support);
5801 // cacheKey = procedure;
5803 // return cacheResult;
5807 // private AsyncMultiProcedure<Statement> cacheKey2 = null;
5808 // private MultiTripleIntProcedure cacheResult2 = null;
5810 // final synchronized TripleIntProcedure forMultiProcedure(final AsyncMultiProcedure<Statement> procedure) {
5812 // if(procedure == cacheKey2) return cacheResult2;
5814 // cacheResult2 = new MultiTripleIntProcedure(procedure, this, processor.support);
5815 // cacheKey2 = procedure;
5817 // return cacheResult2;
5822 public Datatype getDataType(Resource subject) throws DatabaseException {
5823 for(Resource dataTypeResource : getObjects(subject, processor.getL0(this).HasDataType))
5824 return getValue(dataTypeResource, Bindings.getBindingUnchecked(Datatype.class));
5825 throw new DoesNotContainValueException("The literal has no data type.");
5828 protected <T extends Accessor> T getAccessor4File(Resource subject)
5829 throws DatabaseException {
5832 byte[] bytes = processor.support.getValue(g, subject);
5836 BinaryVariant va = (BinaryVariant) Accessors.getAccessor(bytes, Datatypes.VARIANT);
5837 Accessor ca = va.getContentAccessor();
5839 } catch (AccessorConstructionException e) {
5840 throw new DatabaseException(e);
5846 Binding datatype_binding = Bindings.getBindingUnchecked(DataType.class);
5847 Serializer datatype_serializer = datatype_binding.serializer();
5850 BinaryMemory in = new BinaryMemory(ByteBuffer.wrap(bytes));
5851 datatype = (DataType)datatype_serializer.deserialize(in);
5852 Binding data_binding = Bindings.getBinding(datatype);
5853 Serializer data_serializer = data_binding.serializer(BinarySerializationFormat.INSTANCE);
5854 Object o = data_serializer.deserialize(in);
5856 return (T)Accessors.getAccessor(data_binding, o);
5857 } catch(AccessorConstructionException e) {
5860 } catch (Exception e) {
5861 throw new DatabaseException(e);
5864 @SuppressWarnings("unchecked")
5866 public <T extends Accessor> T getAccessor(Resource subject) throws DatabaseException {
5867 RandomAccessBinary rab = getRandomAccessBinary(subject);
5869 return (T)Accessors.getAccessor(rab, getDataType(subject));
5870 } catch(AccessorConstructionException e) {
5871 throw new DatabaseException(e);
5874 @SuppressWarnings("unchecked")
5875 protected <T extends Accessor> T createAccessor(Resource resource, Datatype datatype, Object intialValue)
5876 throws DatabaseException {
5877 RandomAccessBinary rab = createRandomAccessBinary(resource, datatype, intialValue);
5879 return (T)Accessors.getAccessor(rab, datatype);
5880 } catch(AccessorConstructionException e) {
5881 throw new DatabaseException(e);
5885 public RandomAccessBinary getRandomAccessBinary(Resource subject) throws DatabaseException {
5886 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5887 ResourceData rd = ravs.get(subject);
5891 ExternalValueSupport evs = getService(ExternalValueSupport.class);
5892 long size = evs.getValueSize(this, subject); // Throws DatabaseException if no old external value.
5894 File platform = Platform.getLocation().toFile();
5895 File tempFiles = new File(platform, "tempFiles");
5896 File dbDir = new File(tempFiles, "db");
5898 File file = new File(dbDir, "ResourceFile" + subject.getResourceId());
5899 rd = new ResourceData(new BinaryFile(file), true); // Old external value.
5900 final int N = 1<<20;
5904 int length = N < left ? N : (int)left;
5905 byte[] bytes = evs.readValue(this, subject, offset, length);
5906 offset += bytes.length;
5907 left -= bytes.length;
5908 rd.binaryFile.write(bytes);
5910 ravs.put(subject, rd);
5912 } catch (Exception e) {
5913 throw new DatabaseException("Resource " + subject + " have value but there was problem with accessing value data.", e);
5915 } catch (Exception e) {
5916 if(Development.DEVELOPMENT) {
5917 if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
5918 e.printStackTrace();
5922 Datatype datatype = getDataType(subject);
5923 Object value = getPossibleValue(subject, Bindings.getBinding(datatype));
5924 return createRandomAccessBinary(subject, datatype, value);
5926 public RandomAccessBinary createRandomAccessBinary(Resource resource, Datatype datatype, Object initialValue)
5927 throws DatabaseException {
5928 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5930 File platform = Platform.getLocation().toFile();
5931 File tempFiles = new File(platform, "tempFiles");
5932 File dbDir = new File(tempFiles, "db");
5934 File file = new File(dbDir, "ResourceFile" + resource.getResourceId());
5935 ResourceData rd = new ResourceData(new BinaryFile(file), false);
5936 Binding binding = Bindings.getBinding(datatype);
5937 if (null == initialValue) {
5938 initialValue = binding.createDefault();
5940 Serializer serializer = binding.serializer();
5941 byte[] bytes = serializer.serialize(initialValue);
5942 // In case the file has been previously accessed and was larger we set the correct size now
5943 rd.binaryFile.setLength(bytes.length);
5944 rd.binaryFile.write(bytes);
5945 ravs.put(resource, rd);
5947 } catch (Exception e) {
5948 if (e instanceof DatabaseException)
5949 throw (DatabaseException)e;
5951 throw new DatabaseException(e);
5955 // static class ExternalValueRequest<T> extends ResourceRead<T> {
5957 // public ExternalValueRequest(Resource resource) {
5961 // @SuppressWarnings("unchecked")
5963 // public T perform(ReadGraph graph) throws DatabaseException {
5966 // String uri = graph.getURI(resource);
5967 // if(Layer0.URIs.Functions_functionApplication.equals(uri)) return (T)functionApplication;
5969 // return (T)ReflectionUtils.getValue(uri).getValue();
5971 // } catch(ValueNotFoundException e) {
5972 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
5973 // } catch(ClassCastException e) {
5974 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
5980 @SuppressWarnings("unchecked")
5982 public <T> T getValue2(Resource r, Object context) throws DatabaseException {
5983 Layer0 L0 = processor.getL0(this);
5984 Set<Resource> types = getTypes(r);
5986 if(types.contains(L0.Literal)) {
5987 if(isImmutable(r)) {
5988 return syncRequest(new ValueImplied<T>(r));
5993 else if(types.contains(L0.ExternalValue)) {
5994 return (T)syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
5998 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
5999 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6001 return function.apply(this, r, context);
6002 } catch(RuntimeException e) {
6003 DatabaseException dte = findPossibleRootException(e);
6004 if(dte != null) throw dte;
6005 else throw new DatabaseException(e);
6012 public Variant getVariantValue2(Resource r, Object context) throws DatabaseException {
6013 Layer0 L0 = processor.getL0(this);
6014 Set<Resource> types = getTypes(r);
6016 if(types.contains(L0.Literal)) {
6017 if(isImmutable(r)) {
6018 return syncRequest(new VariantValueImplied(r));
6020 return getVariantValue(r);
6023 else if(types.contains(L0.ExternalValue)) {
6024 Object value = syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6026 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6027 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6028 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6033 Function3<ReadGraph,Resource,Object,Object> function = requestValueFunction(r);
6034 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6036 Object value = function.apply(this, r, context);
6038 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6039 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6040 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6042 } catch(RuntimeException e) {
6043 DatabaseException dte = findPossibleRootException(e);
6044 if(dte != null) throw dte;
6045 else throw new DatabaseException(e);
6051 public <T> T getPossibleValue2(Resource subject, Object context) throws DatabaseException {
6053 return getValue2(subject, context);
6054 } catch (DatabaseException e) {
6059 static class PossibleConverterFunction<T> extends ResourceRead<Function3<ReadGraph,Resource,Object,T>> {
6061 public PossibleConverterFunction(Resource resource) {
6066 public Function3<ReadGraph,Resource,Object,T> perform(ReadGraph graph) throws DatabaseException {
6067 return compute(graph, resource);
6070 @SuppressWarnings("unchecked")
6071 public static <T> Function3<ReadGraph,Resource,Object,T> compute(ReadGraph graph, Resource resource) throws DatabaseException {
6072 Layer0 L0 = Layer0.getInstance(graph);
6073 for(Resource converter : graph.getObjects(resource, L0.ConvertsToValueWith)) {
6075 if(L0.Functions_functionApplication.equals(converter)) {
6076 return (Function3<ReadGraph,Resource,Object,T>)graph.syncRequest(new AdaptValue(resource));
6078 return graph.getValue2(converter, resource);
6080 } catch(RuntimeException e) {
6081 DatabaseException dte = findPossibleRootException(e);
6082 if(dte != null) throw dte;
6083 else throw new DatabaseException(e);
6091 <T> Function3<ReadGraph,Resource,Object,T> requestValueFunction(Resource r) throws DatabaseException {
6093 return syncRequest(new PossibleConverterFunction<T>(r), TransientCacheAsyncListener.<Function3<ReadGraph,Resource,Object,T>>instance());
6095 return syncRequest(new PossibleConverterFunction<T>(r));
6099 * Get a value associated with a graph {@link Resource}, using a possible context object and
6100 * a desired value binding. The following methods are tried in order to retreive the value:
6102 * <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>
6103 * <li>If the resource is a {@code L0.ExternalValue}, the value is acquired using
6104 * {@link ReflectionUtils#getValue(String)}.</li>
6105 * <li>If the resource is associated with a suitable value converter with relation {@code L0.ConvertsToValueWith}
6106 * (see {@link #requestValueFunction(Resource)}), the value function is called with the graph, the resource
6107 * and the context object.</li>
6110 * @param r A graph resource with which the value is associated
6111 * @param context A context object that is used for acquiring the value (only applied in case 3)
6112 * @param binding A binding for the value type (only applied in case 1)
6113 * @return The value of the graph node.
6114 * @throws DoesNotContainValueException No value is associated with the graph node.
6115 * @throws DatabaseException Other errors, such as an error in casting the value to the return type or
6116 * a runtime error in the value function.
6118 @SuppressWarnings("unchecked")
6120 public <T> T getValue2(Resource r, Object context, Binding binding) throws DatabaseException {
6121 if (binding instanceof ObjectVariantBinding)
6122 return getValue2(r, context);
6124 Layer0 L0 = processor.getL0(this);
6125 Set<Resource> types = getTypes(r);
6126 if(types.contains(L0.Literal)) {
6127 if(isImmutable(r)) {
6128 return syncRequest(new Value<T>(r, binding));
6130 return getValue(r, binding);
6132 } else if(types.contains(L0.ExternalValue)) {
6133 ComputationalValue cv = syncRequest(new PossibleAdapter<ComputationalValue>(r, ComputationalValue.class), TransientCacheAsyncListener.instance());
6135 return cv.getValue(this, r);
6137 // This should not even be possible since L0 defines an adapter for all values
6139 return (T)ReflectionUtils.getValue(getURI(r)).getValue();
6140 } catch(ValueNotFoundException e) {
6141 throw new DatabaseException(e);
6142 } catch(ClassCastException e) {
6143 throw new DatabaseException(e);
6148 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6149 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function.");
6151 Object value = function.apply(this, r, context);
6152 if(binding.isInstance(value)) return (T)value;
6153 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
6154 return (T)Bindings.adapt(value, srcBinding, binding);
6155 } catch(RuntimeException e) {
6156 DatabaseException dte = findPossibleRootException(e);
6157 if(dte != null) throw dte;
6158 else throw new DatabaseException(e);
6159 } catch (AdaptException e) {
6160 throw new DatabaseException(e);
6161 } catch (org.simantics.databoard.binding.error.BindingException e) {
6162 throw new DatabaseException(e);
6168 public <T> T getPossibleValue2(Resource subject, Object context, Binding binding) throws DatabaseException {
6170 return getValue2(subject, context, binding);
6171 } catch (DatabaseException e) {
6176 private static DatabaseException findPossibleRootException(Throwable t) {
6177 if(t == null) return null;
6178 if(t instanceof DatabaseException) return (DatabaseException)t;
6179 if(t instanceof RuntimeException || t instanceof InvocationTargetException) {
6180 return findPossibleRootException(t.getCause());
6186 public <T> T getRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6187 return getRelatedValue2(subject, relation, subject);
6191 public Variant getRelatedVariantValue2(Resource subject, Resource relation) throws DatabaseException {
6192 return getRelatedVariantValue2(subject, relation, subject);
6196 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6198 Resource object = getPossibleObject(subject, relation);
6199 if(object == null) return null;
6200 else return getValue2(object, subject);
6201 } catch (DatabaseException e) {
6207 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6208 if(Development.DEVELOPMENT) {
6209 if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
6210 String error = L0Validations.checkValueType(this, subject, relation);
6212 Logger.defaultLogError(new ValidationException(error));
6213 throw new ValidationException(error);
6217 return getValue2(getSingleObject(subject, relation), context);
6221 public Variant getRelatedVariantValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6222 if(Development.DEVELOPMENT) {
6223 if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
6224 String error = L0Validations.checkValueType(this, subject, relation);
6226 Logger.defaultLogError(new ValidationException(error));
6227 throw new ValidationException(error);
6231 return getVariantValue2(getSingleObject(subject, relation), context);
6235 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6237 Resource object = getPossibleObject(subject, relation);
6238 if(object == null) return null;
6239 else return getValue2(object, context);
6240 } catch (DatabaseException e) {
6246 public <T> T getRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6247 return getRelatedValue2(subject, relation, subject, binding);
6251 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6253 Resource object = getPossibleObject(subject, relation);
6254 if(object == null) return null;
6255 return getValue2(object, subject, binding);
6256 } catch (DatabaseException e) {
6262 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6263 return getValue2(getSingleObject(subject, relation), context, binding);
6267 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6269 Resource object = getPossibleObject(subject, relation);
6270 if(object == null) return null;
6271 else return getValue2(object, context, binding);
6272 } catch (DatabaseException e) {
6278 public Type getRelatedValueType(Resource subject, Resource relation) throws DatabaseException {
6279 Layer0 L0 = processor.getL0(this);
6280 Resource property = getSingleObject(subject, relation);
6281 String typeText = (String)getRelatedValue(property, L0.HasValueType, Bindings.STRING);
6283 return org.simantics.scl.compiler.types.Types.parseType(typeText);
6284 } catch (SCLTypeParseException e) {
6285 throw new DatabaseException(e);
6289 private static ThreadLocal<Boolean> syncGraph = new ThreadLocal<Boolean>() {
6290 protected Boolean initialValue() {
6296 public boolean setSynchronous(boolean value) {
6297 boolean old = getSynchronous();
6298 syncGraph.set(value);
6303 public boolean getSynchronous() {
6304 return syncGraph.get();
6307 public void ensureLoaded(int resource) {
6308 processor.querySupport.ensureLoaded(this, resource);
6311 public void ensureLoaded(int resource, int predicate) {
6312 processor.querySupport.ensureLoaded(this, resource, predicate);
6315 public byte[] getValue(int resource) {
6316 return processor.querySupport.getValue(this, resource);
6319 public int thread(int resource) {
6320 return (resource >>> 16) & processor.THREAD_MASK;
6323 public int thread(Resource resource) {
6324 return (((ResourceImpl)resource).id >>> 16) & processor.THREAD_MASK;
6327 public ResourceSupport getResourceSupport() {
6328 return processor.getResourceSupport();
6332 public Object getModificationCounter() {
6333 return processor.getSession().getModificationCounter();
6337 public boolean performPending() {
6338 return processor.performPending(this);
6341 public Set<ReadGraphImpl> ancestorSet() {
6342 HashSet<ReadGraphImpl> result = new HashSet<>();
6343 ReadGraphImpl g = this;
6351 public int getLevel() {
6352 return getLevelStatic(this);
6355 private static int getLevelStatic(ReadGraphImpl impl) {
6356 if(impl == null) return 0;
6357 else return 1 + getLevelStatic(impl.parentGraph);
6360 public ReadGraphImpl getTopLevelGraph() {
6361 return getTopLevelGraphStatic(this);
6364 private static ReadGraphImpl getTopLevelGraphStatic(ReadGraphImpl impl) {
6365 if(impl.parentGraph == null) return impl;
6366 else return getTopLevelGraphStatic(impl.parentGraph);