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 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
1954 T result = (T)QueryCache.runnerReadEntry(this, request, parent, listener, procedure, true);
1960 public <T> T syncRequest(final Read<T> request,
1961 final SyncProcedure<T> procedure) throws DatabaseException {
1962 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
1966 public <T> T syncRequest(Read<T> request, Procedure<T> procedure)
1967 throws DatabaseException {
1968 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
1971 static class AsyncReadProcedure<T> implements AsyncProcedure<T> {
1973 private static Throwable DONE = new Throwable();
1976 Throwable exception = null;
1979 public void execute(AsyncReadGraph graph, T t) {
1985 public void exception(AsyncReadGraph graph, Throwable t) {
1989 public void checkAndThrow() throws DatabaseException {
1990 if(exception != DONE) {
1991 if (exception instanceof DatabaseException)
1992 throw (DatabaseException) exception;
1994 throw new DatabaseException(
1995 "Unexpected exception in ReadGraph.syncRequest(AsyncRead)",
2000 public boolean done() {
2001 return exception != null;
2007 public <T> T syncRequest(final AsyncRead<T> request)
2008 throws DatabaseException {
2010 assert (request != null);
2011 return syncRequest(request, new AsyncProcedureAdapter<>() );
2016 public <T> T syncRequest(AsyncRead<T> request, AsyncListener<T> procedure)
2017 throws DatabaseException {
2018 return syncRequest(request, (AsyncProcedure<T>) procedure);
2022 public <T> T syncRequest(AsyncRead<T> request, SyncListener<T> procedure)
2023 throws DatabaseException {
2024 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
2028 public <T> T syncRequest(AsyncRead<T> request, Listener<T> procedure)
2029 throws DatabaseException {
2030 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
2034 final public <T> T syncRequest(final AsyncRead<T> request,
2035 final AsyncProcedure<T> procedure) throws DatabaseException {
2037 assert (request != null);
2039 //ITask task = ThreadLogger.task(request);
2040 ListenerBase listener = getListenerBase(procedure);
2041 T result = (T)QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure, true);
2048 public <T> T syncRequest(AsyncRead<T> request,
2049 final SyncProcedure<T> procedure) throws DatabaseException {
2050 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2054 final public <T> T syncRequest(final AsyncRead<T> request,
2055 final Procedure<T> procedure) throws DatabaseException {
2056 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2060 public <T> Collection<T> syncRequest(final MultiRead<T> request)
2061 throws DatabaseException {
2063 assert (request != null);
2065 final ArrayList<T> result = new ArrayList<T>();
2066 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2068 syncRequest(request, new SyncMultiProcedure<T>() {
2071 public void execute(ReadGraph graph, T t) {
2072 synchronized (result) {
2078 public void finished(ReadGraph graph) {
2082 public void exception(ReadGraph graph, Throwable t) {
2087 public String toString() {
2088 return "syncRequest(MultiRead) -> " + request;
2093 Throwable t = exception.get();
2095 if (t instanceof DatabaseException)
2096 throw (DatabaseException) t;
2098 throw new DatabaseException(
2099 "Unexpected exception in ReadGraph.syncRequest(Read)",
2108 public <T> Collection<T> syncRequest(MultiRead<T> request,
2109 SyncMultiListener<T> procedure) {
2110 return syncRequest(request, (SyncMultiProcedure<T>)procedure);
2114 public <T> Collection<T> syncRequest(MultiRead<T> request,
2115 MultiListener<T> procedure) {
2116 return syncRequest(request, new NoneToSyncMultiListener<T>(procedure));
2120 public <T> Collection<T> syncRequest(MultiRead<T> request,
2121 SyncMultiProcedure<T> procedure) {
2123 assert (request != null);
2125 ListenerBase listener = getListenerBase(procedure);
2127 final ResultCallWrappedSyncQueryProcedure<T> wrapper = new ResultCallWrappedSyncQueryProcedure<T>(procedure);
2129 if (parent != null || listener != null) {
2131 // Object syncParent = request;
2133 // final ReadGraphImpl newGraph = newSync();
2135 processor.query(this, request, parent, wrapper, listener);
2137 // newGraph.waitAsync(syncParent);
2141 // Object syncParent = request;
2143 // final ReadGraphImpl newGraph = newSync();
2146 request.perform(this, wrapper);
2147 } catch (Throwable t) {
2148 wrapper.exception(this, t);
2153 return wrapper.get();
2158 public <T> Collection<T> syncRequest(MultiRead<T> request,
2159 MultiProcedure<T> procedure) {
2160 return syncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
2163 static class AsyncMultiReadProcedure<T> extends ArrayList<T> implements AsyncMultiProcedure<T> {
2165 private static Throwable DONE = new Throwable();
2167 private static final long serialVersionUID = -6494230465108115812L;
2169 Throwable exception = null;
2172 public synchronized void execute(AsyncReadGraph graph, T t) {
2177 public void finished(AsyncReadGraph graph) {
2182 public void exception(AsyncReadGraph graph, Throwable t) {
2186 public void checkAndThrow() throws DatabaseException {
2187 if(exception != DONE) {
2188 if (exception instanceof DatabaseException)
2189 throw (DatabaseException) exception;
2191 throw new DatabaseException(
2192 "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
2197 public boolean done() {
2198 return exception != null;
2204 final public <T> Collection<T> syncRequest(AsyncMultiRead<T> request)
2205 throws DatabaseException {
2207 assert (request != null);
2209 final AsyncMultiReadProcedure<T> procedure = new AsyncMultiReadProcedure<T>();
2211 syncRequest(request, procedure);
2213 procedure.checkAndThrow();
2219 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2220 AsyncMultiListener<T> procedure) {
2221 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2225 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2226 SyncMultiListener<T> procedure) {
2227 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2231 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2232 MultiListener<T> procedure) {
2233 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2236 final private <T> void syncRequest(final AsyncMultiRead<T> request,
2237 final AsyncMultiReadProcedure<T> procedure) {
2239 assert (request != null);
2240 assert (procedure != null);
2242 ListenerBase listener = getListenerBase(procedure);
2244 if (parent != null || listener != null) {
2246 // Object syncParent = request;
2248 // final ReadGraphImpl newGraph = newSync();
2250 processor.query(this, request, parent, procedure, listener);
2252 // newGraph.waitAsync(syncParent);
2253 waitAsyncProcedure(procedure);
2257 // Object syncParent = callerThread == Integer.MIN_VALUE ? null
2260 // final ReadGraphImpl newGraph = newSyncAsync(syncParent);
2265 // ReadGraphImpl sync = newSync();
2266 request.perform(this, procedure);
2267 // sync.waitAsync(null);
2268 waitAsyncProcedure(procedure);
2271 } catch (Throwable t) {
2273 waitAsyncProcedure(procedure);
2284 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2285 final AsyncMultiProcedure<T> procedure) {
2287 assert (request != null);
2288 assert (procedure != null);
2290 ListenerBase listener = getListenerBase(procedure);
2292 if (parent != null || listener != null) {
2294 // Object syncParent = request;
2296 // final ReadGraphImpl newGraph = newSync();
2298 processor.query(this, request, parent, procedure, listener);
2300 // newGraph.waitAsync(syncParent);
2304 // Object syncParent = request;
2306 // final ReadGraphImpl newGraph = newSync();
2310 request.perform(this, new AsyncMultiProcedure<T>() {
2313 public void execute(AsyncReadGraph graph, T result) {
2314 procedure.execute(graph, result);
2318 public void finished(AsyncReadGraph graph) {
2319 procedure.finished(graph);
2323 public void exception(AsyncReadGraph graph, Throwable t) {
2324 procedure.exception(graph, t);
2328 public String toString() {
2329 return "syncRequest(AsyncMultiRead) -> " + procedure;
2334 } catch (Throwable t) {
2346 public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2347 final SyncMultiProcedure<T> procedure) {
2348 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2352 final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2353 final MultiProcedure<T> procedure) {
2354 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2358 public <T> T syncRequest(final ExternalRead<T> request)
2359 throws DatabaseException {
2361 assert (request != null);
2363 return syncRequest(request, new Procedure<T>() {
2366 public void execute(T t) {
2370 public void exception(Throwable t) {
2374 public String toString() {
2375 return "syncRequest(AsyncRead) -> " + request;
2383 public <T> T syncRequest(ExternalRead<T> request, Listener<T> procedure) throws DatabaseException {
2384 return syncRequest(request, (Procedure<T>) procedure);
2388 final public <T> T syncRequest(final ExternalRead<T> request,
2389 final Procedure<T> procedure) throws DatabaseException {
2391 assert (request != null);
2393 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
2394 return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
2399 public void syncRequest(final Write request) throws DatabaseException {
2401 assert (request != null);
2403 throw new DatabaseException(
2404 "Write operations are not supported during read transactions!");
2409 public <T> T syncRequest(final WriteResult<T> request) throws DatabaseException {
2411 assert (request != null);
2413 throw new DatabaseException(
2414 "Write operations are not supported during read transactions!");
2419 public void syncRequest(final DelayedWrite request)
2420 throws DatabaseException {
2422 assert (request != null);
2424 throw new DatabaseException(
2425 "Write operations are not supported during read transactions!");
2430 public <T> T syncRequest(final DelayedWriteResult<T> request) throws DatabaseException {
2432 assert (request != null);
2434 throw new DatabaseException(
2435 "Write operations are not supported during read transactions!");
2440 public void syncRequest(final WriteOnly request) throws DatabaseException {
2442 assert (request != null);
2444 throw new DatabaseException(
2445 "Write operations are not supported during read transactions!");
2450 public <T> T syncRequest(final WriteOnlyResult<T> request) throws DatabaseException {
2452 assert (request != null);
2454 throw new DatabaseException(
2455 "Write operations are not supported during read transactions!");
2460 public <T> void async(ReadInterface<T> r, AsyncProcedure<T> procedure) {
2461 r.request(this, procedure);
2465 public <T> void async(ReadInterface<T> r, Procedure<T> procedure) {
2466 r.request(this, procedure);
2470 public <T> void async(ReadInterface<T> r, SyncProcedure<T> procedure) {
2471 r.request(this, procedure);
2475 public <T> void async(ReadInterface<T> r, AsyncListener<T> procedure) {
2476 r.request(this, procedure);
2480 public <T> void async(ReadInterface<T> r, Listener<T> procedure) {
2481 r.request(this, procedure);
2485 public <T> void async(ReadInterface<T> r, SyncListener<T> procedure) {
2486 r.request(this, procedure);
2490 public <T> T sync(ReadInterface<T> r) throws DatabaseException {
2491 return r.request(this);
2495 public <T> T sync(WriteInterface<T> r) throws DatabaseException {
2496 return r.request(this);
2500 public <T> void async(WriteInterface<T> r, Procedure<T> procedure) {
2501 r.request(this, procedure);
2505 public <T> void async(WriteInterface<T> r) {
2506 r.request(this, new ProcedureAdapter<T>());
2510 * Implementation of the interface AsyncReadGraph
2514 public void forURI(Resource resource, AsyncListener<String> listener) {
2515 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2520 public void forURI(Resource resource, SyncListener<String> listener) {
2521 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2526 public void forURI(Resource resource, Listener<String> listener) {
2527 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2532 final public void forURI(final Resource resource,
2533 final AsyncProcedure<String> procedure) {
2535 assert (resource != null);
2536 assert (procedure != null);
2538 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2544 public void forURI(Resource resource, SyncProcedure<String> procedure) {
2545 forURI(resource, new SyncToAsyncProcedure<String>(procedure));
2549 public void forURI(Resource resource, Procedure<String> procedure) {
2550 forURI(resource, new NoneToAsyncProcedure<String>(procedure));
2554 public void forResource(String id, AsyncListener<Resource> listener) {
2555 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2560 public void forResource(String id, SyncListener<Resource> listener) {
2561 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2566 public void forResource(String id, Listener<Resource> listener) {
2567 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2572 final public void forResource(final String id,
2573 final AsyncProcedure<Resource> procedure) {
2575 assert (id != null);
2576 assert (procedure != null);
2578 processor.forResource(this, id, procedure);
2583 public void forResource(String id, SyncProcedure<Resource> procedure) {
2584 forResource(id, new SyncToAsyncProcedure<Resource>(procedure));
2588 public void forResource(String id, Procedure<Resource> procedure) {
2589 forResource(id, new NoneToAsyncProcedure<Resource>(procedure));
2593 public void forBuiltin(String id, AsyncListener<Resource> listener) {
2594 asyncRequest(new Builtin(id), listener);
2598 public void forBuiltin(String id, SyncListener<Resource> listener) {
2599 asyncRequest(new Builtin(id), listener);
2603 public void forBuiltin(String id, Listener<Resource> listener) {
2604 asyncRequest(new Builtin(id), listener);
2608 final public void forBuiltin(final String id,
2609 final AsyncProcedure<Resource> procedure) {
2611 assert (id != null);
2612 assert (procedure != null);
2614 processor.forBuiltin(this, id, procedure);
2619 public void forBuiltin(String id, SyncProcedure<Resource> procedure) {
2620 forBuiltin(id, new SyncToAsyncProcedure<Resource>(procedure));
2624 public void forBuiltin(String id, Procedure<Resource> procedure) {
2625 forBuiltin(id, new NoneToAsyncProcedure<Resource>(procedure));
2629 final public void forEachStatement(Resource subject, Resource relation,
2630 AsyncMultiProcedure<Statement> procedure) {
2632 assert (subject != null);
2633 assert (relation != null);
2634 assert (procedure != null);
2636 processor.forEachStatement(this, subject, relation, procedure);
2641 public void forEachStatement(Resource subject, Resource relation,
2642 SyncMultiProcedure<Statement> procedure) {
2643 forEachStatement(subject, relation,
2644 new SyncToAsyncMultiProcedure<Statement>(procedure));
2648 final public void forEachStatement(Resource subject, Resource relation,
2649 MultiProcedure<Statement> procedure) {
2651 assert (subject != null);
2652 assert (relation != null);
2653 assert (procedure != null);
2655 processor.forEachStatement(this, subject, relation, procedure);
2660 final public void forStatementSet(Resource subject, Resource relation,
2661 AsyncSetListener<Statement> procedure) {
2663 assert (subject != null);
2664 assert (relation != null);
2665 assert (procedure != null);
2667 processor.forStatementSet(this, subject, relation, procedure);
2672 final public void forStatementSet(Resource subject, Resource relation,
2673 SyncSetListener<Statement> procedure) {
2674 forStatementSet(subject, relation,
2675 new SyncToAsyncSetProcedure<Statement>(procedure));
2679 public void forStatementSet(Resource subject, Resource relation,
2680 SetListener<Statement> listener) {
2681 forStatementSet(subject, relation,
2682 new NoneToAsyncSetProcedure<Statement>(listener));
2686 final public void forEachAssertedStatement(final Resource subject,
2687 final Resource relation,
2688 final AsyncMultiProcedure<Statement> procedure) {
2690 assert (subject != null);
2691 assert (relation != null);
2692 assert (procedure != null);
2694 processor.forEachAssertedStatement(this, subject, relation, procedure);
2699 public void forEachAssertedStatement(Resource subject, Resource relation,
2700 SyncMultiProcedure<Statement> procedure) {
2701 forEachAssertedStatement(subject, relation,
2702 new SyncToAsyncMultiProcedure<Statement>(procedure));
2706 public void forEachAssertedStatement(Resource subject, Resource relation,
2707 MultiProcedure<Statement> procedure) {
2708 forEachAssertedStatement(subject, relation,
2709 new NoneToAsyncMultiProcedure<Statement>(procedure));
2713 public void forAssertedStatementSet(Resource subject, Resource relation,
2714 AsyncSetListener<Statement> procedure) {
2716 assert (subject != null);
2717 assert (relation != null);
2718 assert (procedure != null);
2720 processor.forAssertedStatementSet(this, subject, relation, procedure);
2725 public void forAssertedStatementSet(Resource subject, Resource relation,
2726 SyncSetListener<Statement> procedure) {
2728 assert (subject != null);
2729 assert (relation != null);
2730 assert (procedure != null);
2732 forAssertedStatementSet(subject, relation,
2733 new SyncToAsyncSetProcedure<Statement>(procedure));
2738 public void forAssertedStatementSet(Resource subject, Resource relation,
2739 SetListener<Statement> procedure) {
2741 assert (subject != null);
2742 assert (relation != null);
2743 assert (procedure != null);
2745 forAssertedStatementSet(subject, relation,
2746 new NoneToAsyncSetProcedure<Statement>(procedure));
2751 final public void forEachPredicate(final Resource subject,
2752 final AsyncMultiProcedure<Resource> procedure) {
2754 assert (subject != null);
2755 assert (procedure != null);
2757 processor.forEachPredicate(this, subject, procedure);
2762 public void forEachPredicate(Resource subject,
2763 SyncMultiProcedure<Resource> procedure) {
2764 forEachPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(
2769 final public void forEachPredicate(final Resource subject,
2770 final MultiProcedure<Resource> procedure) {
2772 assert (subject != null);
2773 assert (procedure != null);
2775 processor.forEachPredicate(this, subject, procedure);
2780 final public void forPredicateSet(final Resource subject,
2781 final AsyncSetListener<Resource> procedure) {
2783 assert (subject != null);
2784 assert (procedure != null);
2786 processor.forPredicateSet(this, subject, procedure);
2791 final public void forPredicateSet(final Resource subject,
2792 final SyncSetListener<Resource> procedure) {
2794 assert (subject != null);
2795 assert (procedure != null);
2797 forPredicateSet(subject, new SyncToAsyncSetProcedure<Resource>(
2803 final public void forPredicateSet(final Resource subject,
2804 final SetListener<Resource> procedure) {
2806 assert (subject != null);
2807 assert (procedure != null);
2809 forPredicateSet(subject, new NoneToAsyncSetProcedure<Resource>(
2815 final public void forEachPrincipalType(final Resource subject,
2816 final AsyncMultiProcedure<Resource> procedure) {
2818 assert (subject != null);
2819 assert (procedure != null);
2821 processor.forEachPrincipalType(this, subject, procedure);
2826 public void forEachPrincipalType(Resource subject,
2827 SyncMultiProcedure<Resource> procedure) {
2828 forEachPrincipalType(subject, new SyncToAsyncMultiProcedure<Resource>(
2833 final public void forEachPrincipalType(final Resource subject,
2834 final MultiProcedure<Resource> procedure) {
2836 assert (subject != null);
2837 assert (procedure != null);
2839 processor.forEachPrincipalType(this, subject, procedure);
2844 final public void forPrincipalTypeSet(final Resource subject,
2845 final AsyncSetListener<Resource> procedure) {
2847 assert (subject != null);
2848 assert (procedure != null);
2850 processor.forPrincipalTypeSet(this, subject, procedure);
2855 final public void forPrincipalTypeSet(final Resource subject,
2856 final SyncSetListener<Resource> procedure) {
2858 assert (subject != null);
2859 assert (procedure != null);
2861 forPrincipalTypeSet(subject, new SyncToAsyncSetProcedure<Resource>(
2867 final public void forPrincipalTypeSet(final Resource subject,
2868 final SetListener<Resource> procedure) {
2870 assert (subject != null);
2871 assert (procedure != null);
2873 forPrincipalTypeSet(subject, new NoneToAsyncSetProcedure<Resource>(
2879 public void forTypes(Resource subject, AsyncListener<Set<Resource>> listener) {
2880 asyncRequest(new Types(subject), listener);
2884 public void forTypes(Resource subject, SyncListener<Set<Resource>> listener) {
2885 asyncRequest(new Types(subject), listener);
2889 public void forTypes(Resource subject, Listener<Set<Resource>> listener) {
2890 asyncRequest(new Types(subject), listener);
2894 final public void forTypes(final Resource subject,
2895 final AsyncProcedure<Set<Resource>> procedure) {
2897 assert (subject != null);
2898 assert (procedure != null);
2900 processor.forTypes(this, subject, procedure);
2905 public void forTypes(Resource subject,
2906 SyncProcedure<Set<Resource>> procedure) {
2907 forTypes(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
2911 public void forTypes(Resource subject, Procedure<Set<Resource>> procedure) {
2912 forTypes(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
2916 public void forSupertypes(Resource subject,
2917 AsyncListener<Set<Resource>> listener) {
2918 asyncRequest(new Types(subject), listener);
2922 public void forSupertypes(Resource subject,
2923 SyncListener<Set<Resource>> listener) {
2924 asyncRequest(new Types(subject), listener);
2928 public void forSupertypes(Resource subject, Listener<Set<Resource>> listener) {
2929 asyncRequest(new Types(subject), listener);
2933 final public void forSupertypes(final Resource subject,
2934 final AsyncProcedure<Set<Resource>> procedure) {
2936 assert (subject != null);
2937 assert (procedure != null);
2939 processor.forSupertypes(this, subject, procedure);
2944 public void forSupertypes(Resource subject,
2945 SyncProcedure<Set<Resource>> procedure) {
2946 forSupertypes(subject, new SyncToAsyncProcedure<Set<Resource>>(
2951 public void forSupertypes(Resource subject,
2952 Procedure<Set<Resource>> procedure) {
2953 forSupertypes(subject, new NoneToAsyncProcedure<Set<Resource>>(
2958 public void forDirectSuperrelations(Resource subject,
2959 AsyncMultiProcedure<Resource> procedure) {
2961 assert (subject != null);
2962 assert (procedure != null);
2964 processor.forDirectSuperrelations(this, subject, procedure);
2969 public void forPossibleSuperrelation(Resource subject, AsyncProcedure<Resource> procedure) {
2971 assert (subject != null);
2972 assert (procedure != null);
2974 processor.forPossibleSuperrelation(this, subject, procedure);
2979 public void forSuperrelations(Resource subject,
2980 AsyncListener<Set<Resource>> listener) {
2981 asyncRequest(new Types(subject), listener);
2985 public void forSuperrelations(Resource subject,
2986 SyncListener<Set<Resource>> listener) {
2987 asyncRequest(new Types(subject), listener);
2991 public void forSuperrelations(Resource subject,
2992 Listener<Set<Resource>> listener) {
2993 asyncRequest(new Types(subject), listener);
2997 final public void forSuperrelations(final Resource subject,
2998 final AsyncProcedure<Set<Resource>> procedure) {
3000 assert (subject != null);
3001 assert (procedure != null);
3003 processor.forSuperrelations(this, subject, procedure);
3008 public void forSuperrelations(Resource subject,
3009 SyncProcedure<Set<Resource>> procedure) {
3010 forSuperrelations(subject, new SyncToAsyncProcedure<Set<Resource>>(
3015 public void forSuperrelations(Resource subject,
3016 Procedure<Set<Resource>> procedure) {
3017 forSuperrelations(subject, new NoneToAsyncProcedure<Set<Resource>>(
3022 final public void forEachObject(final Resource subject, final Resource relation, final AsyncMultiProcedure<Resource> procedure) {
3023 processor.forEachObject(this, subject, relation, procedure);
3027 public void forEachObject(Resource subject, Resource relation,
3028 SyncMultiProcedure<Resource> procedure) {
3029 forEachObject(subject, relation,
3030 new SyncToAsyncMultiProcedure<Resource>(procedure));
3034 public void forEachObject(Resource subject, Resource relation,
3035 MultiProcedure<Resource> procedure) {
3037 processor.forEachObject(this, subject, relation, procedure);
3042 final public void forEachDirectPredicate(final Resource subject, final AsyncProcedure<Set<Resource>> procedure) {
3043 processor.forEachDirectPredicate(this, subject, procedure);
3047 final public void forEachDirectPredicate(final Resource subject, final SyncProcedure<Set<Resource>> procedure) {
3048 forEachDirectPredicate(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
3052 public void forEachDirectPredicate(Resource subject, Procedure<Set<Resource>> procedure) {
3053 forEachDirectPredicate(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
3057 final public void forObjectSet(final Resource subject,
3058 final Resource relation, final AsyncSetListener<Resource> procedure) {
3060 assert (subject != null);
3061 assert (relation != null);
3062 assert (procedure != null);
3064 processor.forObjectSet(this, subject, relation, procedure);
3069 final public void forObjectSet(final Resource subject,
3070 final Resource relation, final SyncSetListener<Resource> procedure) {
3072 assert (subject != null);
3073 assert (relation != null);
3074 assert (procedure != null);
3076 forObjectSet(subject, relation, new SyncToAsyncSetProcedure<Resource>(
3082 final public void forObjectSet(final Resource subject,
3083 final Resource relation, final SetListener<Resource> procedure) {
3085 assert (subject != null);
3086 assert (relation != null);
3087 assert (procedure != null);
3089 forObjectSet(subject, relation, new NoneToAsyncSetProcedure<Resource>(
3095 final public void forEachAssertedObject(final Resource subject,
3096 final Resource relation,
3097 final AsyncMultiProcedure<Resource> procedure) {
3099 assert (subject != null);
3100 assert (relation != null);
3101 assert (procedure != null);
3103 processor.forEachAssertedObject(this, subject, relation, procedure);
3108 public void forEachAssertedObject(Resource subject, Resource relation,
3109 SyncMultiProcedure<Resource> procedure) {
3111 assert (subject != null);
3112 assert (relation != null);
3113 assert (procedure != null);
3115 forEachAssertedObject(subject, relation,
3116 new SyncToAsyncMultiProcedure<Resource>(procedure));
3121 public void forEachAssertedObject(Resource subject, Resource relation,
3122 MultiProcedure<Resource> procedure) {
3124 assert (subject != null);
3125 assert (relation != null);
3126 assert (procedure != null);
3128 forEachAssertedObject(subject, relation,
3129 new NoneToAsyncMultiProcedure<Resource>(procedure));
3134 public void forAssertedObjectSet(Resource subject, Resource relation,
3135 AsyncSetListener<Resource> procedure) {
3137 assert (subject != null);
3138 assert (relation != null);
3139 assert (procedure != null);
3141 processor.forAssertedObjectSet(this, subject, relation, procedure);
3146 public void forAssertedObjectSet(Resource subject, Resource relation,
3147 SyncSetListener<Resource> procedure) {
3149 assert (subject != null);
3150 assert (relation != null);
3151 assert (procedure != null);
3153 forAssertedObjectSet(subject, relation,
3154 new SyncToAsyncSetProcedure<Resource>(procedure));
3159 public void forAssertedObjectSet(Resource subject, Resource relation,
3160 SetListener<Resource> procedure) {
3162 assert (subject != null);
3163 assert (relation != null);
3164 assert (procedure != null);
3166 forAssertedObjectSet(subject, relation,
3167 new NoneToAsyncSetProcedure<Resource>(procedure));
3172 public void forInverse(Resource relation, AsyncListener<Resource> listener) {
3173 asyncRequest(new Inverse(relation), listener);
3177 public void forInverse(Resource relation, SyncListener<Resource> listener) {
3178 asyncRequest(new Inverse(relation), listener);
3182 public void forInverse(Resource relation, Listener<Resource> listener) {
3183 asyncRequest(new Inverse(relation), listener);
3187 final public void forInverse(final Resource relation,
3188 final AsyncProcedure<Resource> procedure) {
3190 assert (relation != null);
3191 assert (procedure != null);
3193 processor.forInverse(this, relation, new AsyncProcedure<Resource>() {
3196 public void execute(AsyncReadGraph graph, Resource result) {
3198 procedure.execute(graph, result);
3200 procedure.exception(graph, new NoInverseException(relation
3206 public void exception(AsyncReadGraph graph, Throwable throwable) {
3207 procedure.exception(graph, throwable);
3211 public String toString() {
3212 return "forInverse -> " + procedure;
3220 public void forInverse(Resource relation, SyncProcedure<Resource> procedure) {
3221 forInverse(relation, new SyncToAsyncProcedure<Resource>(procedure));
3225 public void forInverse(Resource relation, Procedure<Resource> procedure) {
3226 forInverse(relation, new NoneToAsyncProcedure<Resource>(procedure));
3230 public void forSingleObject(Resource subject, Resource relation,
3231 AsyncListener<Resource> listener) {
3232 asyncRequest(new SingleObject(subject, relation), listener);
3236 public void forSingleObject(Resource subject, Resource relation,
3237 SyncListener<Resource> listener) {
3238 asyncRequest(new SingleObject(subject, relation), listener);
3242 public void forSingleObject(Resource subject, Resource relation,
3243 Listener<Resource> listener) {
3244 asyncRequest(new SingleObject(subject, relation), listener);
3248 final public void forSingleObject(final Resource subject,
3249 final Resource relation, final AsyncProcedure<Resource> procedure) {
3251 assert (subject != null);
3252 assert (relation != null);
3253 assert (procedure != null);
3255 processor.forEachObject(this, subject, relation,
3256 new SingleOrErrorProcedure<Resource>(procedure));
3261 public void forSingleObject(Resource subject, Resource relation,
3262 SyncProcedure<Resource> procedure) {
3263 forSingleObject(subject, relation, new SyncToAsyncProcedure<Resource>(
3268 public void forSingleObject(Resource subject, Resource relation,
3269 Procedure<Resource> procedure) {
3270 forSingleObject(subject, relation, new NoneToAsyncProcedure<Resource>(
3275 public void forSingleStatement(Resource subject, Resource relation,
3276 AsyncListener<Statement> listener) {
3277 asyncRequest(new SingleStatement(subject, relation), listener);
3281 public void forSingleStatement(Resource subject, Resource relation,
3282 SyncListener<Statement> listener) {
3283 asyncRequest(new SingleStatement(subject, relation), listener);
3287 public void forSingleStatement(Resource subject, Resource relation,
3288 Listener<Statement> listener) {
3289 asyncRequest(new SingleStatement(subject, relation), listener);
3293 final public void forSingleStatement(final Resource subject,
3294 final Resource relation, final AsyncProcedure<Statement> procedure) {
3296 assert (subject != null);
3297 assert (relation != null);
3298 assert (procedure != null);
3300 processor.forEachStatement(this, subject, relation,
3301 new SingleOrErrorProcedure<Statement>(procedure));
3306 public void forSingleStatement(Resource subject, Resource relation,
3307 SyncProcedure<Statement> procedure) {
3308 forSingleStatement(subject, relation,
3309 new SyncToAsyncProcedure<Statement>(procedure));
3313 public void forSingleStatement(Resource subject, Resource relation,
3314 Procedure<Statement> procedure) {
3315 forSingleStatement(subject, relation,
3316 new NoneToAsyncProcedure<Statement>(procedure));
3320 public void forSingleType(Resource subject,
3321 AsyncListener<Resource> listener) {
3322 asyncRequest(new SingleTypeAny(subject), listener);
3326 public void forSingleType(Resource subject,
3327 SyncListener<Resource> listener) {
3328 asyncRequest(new SingleTypeAny(subject), listener);
3332 public void forSingleType(Resource subject,
3333 Listener<Resource> listener) {
3334 asyncRequest(new SingleTypeAny(subject), listener);
3338 final public void forSingleType(final Resource subject, final AsyncProcedure<Resource> procedure) {
3340 assert (subject != null);
3341 assert (procedure != null);
3343 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3345 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
3348 public void execute(AsyncReadGraph graph, final Resource principalType) {
3349 checkedProcedure.offer(graph, principalType);
3353 public void finished(AsyncReadGraph graph) {
3354 checkedProcedure.dec(graph);
3358 public void exception(AsyncReadGraph graph, Throwable t) {
3359 checkedProcedure.exception(graph, t);
3363 public String toString() {
3364 return "forSingleType -> " + procedure;
3372 public void forSingleType(Resource subject, SyncProcedure<Resource> procedure) {
3373 forSingleType(subject, new SyncToAsyncProcedure<Resource>(
3378 public void forSingleType(Resource subject, Procedure<Resource> procedure) {
3379 forSingleType(subject, new NoneToAsyncProcedure<Resource>(
3384 public void forSingleType(Resource subject, Resource relation,
3385 AsyncListener<Resource> listener) {
3386 asyncRequest(new SingleType(subject, relation), listener);
3390 public void forSingleType(Resource subject, Resource relation,
3391 SyncListener<Resource> listener) {
3392 asyncRequest(new SingleType(subject, relation), listener);
3396 public void forSingleType(Resource subject, Resource relation,
3397 Listener<Resource> listener) {
3398 asyncRequest(new SingleType(subject, relation), listener);
3402 final public void forSingleType(final Resource subject,
3403 final Resource baseType, final AsyncProcedure<Resource> procedure) {
3405 assert (subject != null);
3406 assert (procedure != null);
3408 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3410 processor.forEachPrincipalType(this, subject,
3411 new AsyncMultiProcedureAdapter<Resource>() {
3414 public void execute(AsyncReadGraph graph,
3415 final Resource principalType) {
3417 checkedProcedure.inc();
3419 if(baseType == null) {
3421 checkedProcedure.offer(graph, principalType);
3422 checkedProcedure.dec(graph);
3424 } else if(principalType.equals(baseType)) {
3426 checkedProcedure.offer(graph, principalType);
3427 checkedProcedure.dec(graph);
3431 processor.forSupertypes((ReadGraphImpl)graph, principalType,
3432 new AsyncProcedure<Set<Resource>>() {
3435 public void execute(
3436 AsyncReadGraph graph,
3437 Set<Resource> result) {
3439 if (result.contains(baseType))
3440 checkedProcedure.offer(graph,
3442 checkedProcedure.dec(graph);
3447 public void exception(
3448 AsyncReadGraph graph,
3451 .exception(graph, t);
3461 public void finished(AsyncReadGraph graph) {
3462 checkedProcedure.dec(graph);
3466 public void exception(AsyncReadGraph graph, Throwable t) {
3467 checkedProcedure.exception(graph, t);
3471 public String toString() {
3472 return "forSingleType -> " + procedure;
3480 public void forSingleType(Resource subject, Resource relation,
3481 SyncProcedure<Resource> procedure) {
3482 forSingleType(subject, relation, new SyncToAsyncProcedure<Resource>(
3487 public void forSingleType(Resource subject, Resource relation,
3488 Procedure<Resource> procedure) {
3489 forSingleType(subject, relation, new NoneToAsyncProcedure<Resource>(
3494 public <T> void forValue(Resource subject, Binding binding,
3495 AsyncListener<T> listener) {
3496 asyncRequest(new Value<T>(subject, binding), listener);
3500 public <T> void forValue(Resource subject, Binding binding,
3501 SyncListener<T> listener) {
3502 asyncRequest(new Value<T>(subject, binding), listener);
3506 public <T> void forValue(Resource subject, Binding binding,
3507 Listener<T> listener) {
3508 asyncRequest(new Value<T>(subject, binding), listener);
3512 public <T> void forValue(final Resource resource, final Binding binding,
3513 final AsyncProcedure<T> procedure) {
3515 assert (resource != null);
3516 assert (binding != null);
3517 assert (procedure != null);
3519 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
3522 public void execute(AsyncReadGraph graph, byte[] result) {
3526 if (result == null) {
3527 procedure.exception(graph,
3528 new DoesNotContainValueException(
3529 "No value for resource " + resource));
3533 Serializer serializer = binding.serializer();
3534 // Serializer serializer = Bindings.getSerializer( binding );
3535 Object obj = serializer.deserialize(result);
3536 // if (!binding.isInstance(obj))
3537 // procedure.exception(graph, new ClassCastException(
3538 // "Cannot get value " + obj + " with binding "
3541 procedure.execute(graph, (T) obj);
3543 } catch (Throwable t) {
3544 procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
3549 public void exception(AsyncReadGraph graph, Throwable t) {
3551 procedure.exception(graph, t);
3552 } catch (Throwable t2) {
3553 Logger.defaultLogError(t2);
3558 public String toString() {
3559 return "forValue -> " + procedure;
3566 private static String safeArrayToString(byte[] a) {
3569 int iMax = a.length - 1;
3573 StringBuilder b = new StringBuilder();
3575 for (int i = 0; i < 100; i++) { // limit to first 100 items
3578 return b.append(']').toString();
3581 return b.append(", ... (" + a.length + ")]").toString();
3585 public <T> void forValue(Resource subject, Binding binding,
3586 SyncProcedure<T> procedure) {
3587 forValue(subject, binding, new SyncToAsyncProcedure<T>(procedure));
3591 public <T> void forValue(Resource subject, Binding binding,
3592 Procedure<T> procedure) {
3593 forValue(subject, binding, new NoneToAsyncProcedure<T>(procedure));
3597 public <T> void forValue(Resource subject, AsyncListener<T> listener) {
3598 asyncRequest(new ValueImplied<T>(subject), listener);
3602 public <T> void forValue(Resource subject, SyncListener<T> listener) {
3603 asyncRequest(new ValueImplied<T>(subject), listener);
3607 public <T> void forValue(Resource subject, Listener<T> listener) {
3608 asyncRequest(new ValueImplied<T>(subject), listener);
3612 final public <T> void forValue(final Resource subject, final AsyncProcedure<T> procedure) {
3614 assert (subject != null);
3615 assert (procedure != null);
3617 forRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
3620 public void execute(AsyncReadGraph graph, Datatype type) {
3621 // TODO: consider trying Bindings.getBeanBinding(type);
3622 Binding binding = Bindings.getBinding(type);
3623 graph.forValue(subject, binding, procedure);
3627 public void exception(AsyncReadGraph graph, Throwable throwable) {
3628 procedure.exception(graph, new DoesNotContainValueException("Invalid data type", throwable));
3636 public <T> void forValue(Resource subject, SyncProcedure<T> procedure) {
3637 forValue(subject, new SyncToAsyncProcedure<T>(procedure));
3641 public <T> void forValue(Resource subject, Procedure<T> procedure) {
3642 forValue(subject, new NoneToAsyncProcedure<T>(procedure));
3646 public <T> void forRelatedValue(Resource subject, Resource relation,
3647 AsyncListener<T> listener) {
3648 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3652 public <T> void forRelatedValue(Resource subject, Resource relation,
3653 SyncListener<T> listener) {
3654 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3658 public <T> void forRelatedValue(Resource subject, Resource relation,
3659 Listener<T> listener) {
3660 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3664 final public <T> void forRelatedValue(final Resource subject,
3665 final Resource relation, final AsyncProcedure<T> procedure) {
3667 assert (subject != null);
3668 assert (relation != null);
3669 assert (procedure != null);
3671 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3673 processor.forEachObject(this, subject, relation,
3674 new AsyncMultiProcedureAdapter<Resource>() {
3677 public void execute(AsyncReadGraph graph,
3678 final Resource object) {
3680 checkedProcedure.inc();
3682 graph.forValue(object, new AsyncProcedure<Object>() {
3685 public void execute(AsyncReadGraph graph,
3687 checkedProcedure.offer(graph, (T) result);
3688 checkedProcedure.dec(graph);
3692 public void exception(AsyncReadGraph graph,
3694 checkedProcedure.exception(graph, t);
3698 public String toString() {
3699 return "forRelatedValue -> " + procedure;
3707 public void finished(AsyncReadGraph graph) {
3708 checkedProcedure.dec(graph);
3712 public void exception(AsyncReadGraph graph, Throwable t) {
3713 checkedProcedure.exception(graph, t);
3721 public <T> void forRelatedValue(Resource subject, Resource relation,
3722 SyncProcedure<T> procedure) {
3723 forRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
3728 public <T> void forRelatedValue(Resource subject, Resource relation,
3729 Procedure<T> procedure) {
3730 forRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
3735 public <T> void forRelatedValue(Resource subject, Resource relation,
3736 Binding binding, AsyncListener<T> listener) {
3737 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3741 public <T> void forRelatedValue(Resource subject, Resource relation,
3742 Binding binding, SyncListener<T> listener) {
3743 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3747 public <T> void forRelatedValue(Resource subject, Resource relation,
3748 Binding binding, Listener<T> listener) {
3749 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3753 final public <T> void forRelatedValue(final Resource subject,
3754 final Resource relation, final Binding binding,
3755 final AsyncProcedure<T> procedure) {
3757 assert (subject != null);
3758 assert (relation != null);
3759 assert (binding != null);
3760 assert (procedure != null);
3762 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3764 processor.forEachObject(this, subject, relation,
3765 new AsyncMultiProcedureAdapter<Resource>() {
3768 public void execute(AsyncReadGraph graph,
3769 final Resource object) {
3771 checkedProcedure.inc();
3773 graph.forValue(object, binding, new AsyncProcedure<Object>() {
3776 public void execute(AsyncReadGraph graph,
3779 checkedProcedure.offer(graph,
3781 checkedProcedure.dec(graph);
3785 public void exception(AsyncReadGraph graph,
3787 checkedProcedure.exception(graph, t);
3791 public String toString() {
3792 return "forRelatedValue -> "
3801 public void finished(AsyncReadGraph graph) {
3802 checkedProcedure.dec(graph);
3806 public void exception(AsyncReadGraph graph, Throwable t) {
3807 checkedProcedure.exception(graph, t);
3815 public <T> void forRelatedValue(Resource subject, Resource relation,
3816 Binding binding, SyncProcedure<T> procedure) {
3817 forRelatedValue(subject, relation, binding,
3818 new SyncToAsyncProcedure<T>(procedure));
3822 public <T> void forRelatedValue(Resource subject, Resource relation,
3823 Binding binding, Procedure<T> procedure) {
3824 forRelatedValue(subject, relation, binding,
3825 new NoneToAsyncProcedure<T>(procedure));
3829 public <T> void forAdapted(Resource resource, Class<T> clazz,
3830 AsyncListener<T> listener) {
3831 asyncRequest(new Adapter<T>(resource, clazz), listener);
3835 public <T> void forAdapted(Resource resource, Class<T> clazz,
3836 SyncListener<T> listener) {
3837 asyncRequest(new Adapter<T>(resource, clazz), listener);
3841 public <T> void forAdapted(Resource resource, Class<T> clazz,
3842 Listener<T> listener) {
3843 asyncRequest(new Adapter<T>(resource, clazz), listener);
3847 final public <T> void forAdapted(final Resource resource,
3848 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3850 assert (resource != null);
3851 assert (clazz != null);
3852 assert (procedure != null);
3854 final AdaptionService service = getSession().peekService(AdaptionService.class);
3855 if (service == null)
3856 procedure.exception(this, new ServiceException("No AdaptionService available"));
3858 service.adapt(this, resource, resource, Resource.class, clazz, false, procedure);
3863 public <T> void forAdapted(Resource resource, Class<T> clazz,
3864 SyncProcedure<T> procedure) {
3865 forAdapted(resource, clazz, new SyncToAsyncProcedure<T>(procedure));
3869 public <T> void forAdapted(Resource resource, Class<T> clazz,
3870 Procedure<T> procedure) {
3871 forAdapted(resource, clazz, new NoneToAsyncProcedure<T>(procedure));
3875 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3876 AsyncListener<T> listener) {
3877 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3881 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3882 SyncListener<T> listener) {
3883 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3887 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3888 Listener<T> listener) {
3889 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3893 final public <T> void forUniqueAdapted(final Resource resource,
3894 final Class<T> clazz, final AsyncProcedure<T> procedure) {
3896 assert (resource != null);
3897 assert (clazz != null);
3898 assert (procedure != null);
3900 final AdaptionService service = getSession().peekService(AdaptionService.class);
3901 if (service == null)
3902 procedure.exception(this, new ServiceException("No AdaptionService available"));
3904 service.adaptNew(this, resource, clazz, false, procedure);
3909 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3910 SyncProcedure<T> procedure) {
3911 forUniqueAdapted(resource, clazz,
3912 new SyncToAsyncProcedure<T>(procedure));
3916 public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3917 Procedure<T> procedure) {
3918 forUniqueAdapted(resource, clazz,
3919 new NoneToAsyncProcedure<T>(procedure));
3923 public void forPossibleInverse(Resource subject,
3924 AsyncListener<Resource> listener) {
3925 asyncRequest(new PossibleInverse(subject), listener);
3929 public void forPossibleInverse(Resource subject,
3930 SyncListener<Resource> listener) {
3931 asyncRequest(new PossibleInverse(subject), listener);
3935 public void forPossibleInverse(Resource subject, Listener<Resource> listener) {
3936 asyncRequest(new PossibleInverse(subject), listener);
3940 final public void forPossibleInverse(final Resource relation,
3941 final AsyncProcedure<Resource> procedure) {
3943 assert (relation != null);
3944 assert (procedure != null);
3946 processor.forInverse(this, relation, new ExceptionToNullProcedure<Resource>(procedure));
3951 public void forPossibleInverse(Resource subject,
3952 SyncProcedure<Resource> procedure) {
3953 forPossibleInverse(subject, new SyncToAsyncProcedure<Resource>(
3958 public void forPossibleInverse(Resource subject,
3959 Procedure<Resource> procedure) {
3960 forPossibleInverse(subject, new NoneToAsyncProcedure<Resource>(
3965 public void forPossibleObject(Resource subject, Resource relation,
3966 AsyncListener<Resource> listener) {
3967 asyncRequest(new PossibleObject(subject, relation), listener);
3971 public void forPossibleObject(Resource subject, Resource relation,
3972 SyncListener<Resource> listener) {
3973 asyncRequest(new PossibleObject(subject, relation), listener);
3977 public void forPossibleObject(Resource subject, Resource relation,
3978 Listener<Resource> listener) {
3979 asyncRequest(new PossibleObject(subject, relation), listener);
3983 final public void forPossibleObject(final Resource subject,
3984 final Resource relation, final AsyncProcedure<Resource> procedure) {
3986 assert (subject != null);
3987 assert (relation != null);
3988 assert (procedure != null);
3990 processor.forEachObject(this, subject, relation,
3991 new SingleOrNullProcedure<Resource>(procedure));
3996 public void forPossibleObject(Resource subject, Resource relation,
3997 SyncProcedure<Resource> procedure) {
3998 forPossibleObject(subject, relation,
3999 new SyncToAsyncProcedure<Resource>(procedure));
4003 public void forPossibleObject(Resource subject, Resource relation,
4004 Procedure<Resource> procedure) {
4005 forPossibleObject(subject, relation,
4006 new NoneToAsyncProcedure<Resource>(procedure));
4010 public void forPossibleStatement(Resource subject, Resource relation,
4011 AsyncListener<Statement> listener) {
4012 asyncRequest(new PossibleStatement(subject, relation), listener);
4016 public void forPossibleStatement(Resource subject, Resource relation,
4017 SyncListener<Statement> listener) {
4018 asyncRequest(new PossibleStatement(subject, relation), listener);
4022 public void forPossibleStatement(Resource subject, Resource relation,
4023 Listener<Statement> listener) {
4024 asyncRequest(new PossibleStatement(subject, relation), listener);
4028 final public void forPossibleStatement(final Resource subject,
4029 final Resource relation, final AsyncProcedure<Statement> procedure) {
4031 assert (subject != null);
4032 assert (relation != null);
4033 assert (procedure != null);
4035 processor.forEachStatement(this, subject, relation,
4036 new SingleFunctionalOrNullProcedure<Statement>(
4037 "forPossibleStatement", procedure));
4042 public void forPossibleStatement(Resource subject, Resource relation,
4043 SyncProcedure<Statement> procedure) {
4044 forPossibleStatement(subject, relation,
4045 new SyncToAsyncProcedure<Statement>(procedure));
4049 public void forPossibleStatement(Resource subject, Resource relation,
4050 Procedure<Statement> procedure) {
4051 forPossibleStatement(subject, relation,
4052 new NoneToAsyncProcedure<Statement>(procedure));
4056 public void forPossibleType(Resource subject, Resource relation,
4057 AsyncListener<Resource> listener) {
4058 asyncRequest(new PossibleType(subject, relation), listener);
4062 public void forPossibleType(Resource subject, Resource relation,
4063 SyncListener<Resource> listener) {
4064 asyncRequest(new PossibleType(subject, relation), listener);
4068 public void forPossibleType(Resource subject, Resource relation,
4069 Listener<Resource> listener) {
4070 asyncRequest(new PossibleType(subject, relation), listener);
4074 final public void forPossibleType(final Resource subject,
4075 final Resource baseType, final AsyncProcedure<Resource> procedure) {
4077 assert (subject != null);
4078 assert (procedure != null);
4080 final NullSingleOrNullProcedure<Resource> checkedProcedure = new NullSingleOrNullProcedure<Resource>(procedure);
4082 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
4085 public void execute(AsyncReadGraph graph,
4086 final Resource principalType) {
4088 if (baseType == null) {
4090 checkedProcedure.offer(graph, principalType);
4092 } else if (principalType.equals(baseType)) {
4094 checkedProcedure.offer(graph, principalType);
4098 checkedProcedure.inc();
4100 processor.forSupertypes((ReadGraphImpl)graph, principalType,
4101 new AsyncProcedure<Set<Resource>>() {
4104 public void execute(
4105 AsyncReadGraph graph,
4106 Set<Resource> result) {
4108 if (result.contains(baseType)) {
4109 checkedProcedure.offer(graph,
4113 checkedProcedure.dec(graph);
4118 public void exception(
4119 AsyncReadGraph graph,
4121 checkedProcedure.exception(graph, t);
4122 checkedProcedure.dec(graph);
4126 public String toString() {
4127 return "forPossibleType -> "
4138 public void finished(AsyncReadGraph graph) {
4139 checkedProcedure.dec(graph);
4143 public void exception(AsyncReadGraph graph, Throwable t) {
4144 checkedProcedure.exception(graph, t);
4145 checkedProcedure.dec(graph);
4153 public void forPossibleType(Resource subject, Resource relation,
4154 SyncProcedure<Resource> procedure) {
4155 forPossibleType(subject, relation, new SyncToAsyncProcedure<Resource>(
4160 public void forPossibleType(Resource subject, Resource relation,
4161 Procedure<Resource> procedure) {
4162 forPossibleType(subject, relation, new NoneToAsyncProcedure<Resource>(
4167 public <T> void forPossibleValue(Resource subject, AsyncListener<T> listener) {
4168 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4172 public <T> void forPossibleValue(Resource subject, SyncListener<T> listener) {
4173 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4177 public <T> void forPossibleValue(Resource subject, Listener<T> listener) {
4178 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4182 final public <T> void forPossibleValue(final Resource subject,
4183 final AsyncProcedure<T> procedure) {
4185 assert (subject != null);
4186 assert (procedure != null);
4188 forPossibleRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
4191 public void execute(AsyncReadGraph graph, final Datatype type) {
4193 procedure.execute(graph, null);
4196 // TODO: consider trying Bindings.getBeanBinding(type);
4197 Binding binding = Bindings.getBinding(type);
4198 graph.forPossibleValue(subject, binding, procedure);
4199 } catch (RuntimeBindingConstructionException e) {
4200 procedure.exception(graph, e);
4206 public void exception(AsyncReadGraph graph, Throwable t) {
4207 procedure.exception(graph, t);
4211 public String toString() {
4212 return "forPossibleValue -> " + procedure;
4220 public <T> void forPossibleValue(Resource subject,
4221 SyncProcedure<T> procedure) {
4222 forPossibleValue(subject, new SyncToAsyncProcedure<T>(procedure));
4226 public <T> void forPossibleValue(Resource subject, Procedure<T> procedure) {
4227 forPossibleValue(subject, new NoneToAsyncProcedure<T>(procedure));
4231 public <T> void forPossibleValue(Resource subject, Binding binding,
4232 AsyncListener<T> listener) {
4233 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4237 public <T> void forPossibleValue(Resource subject, Binding binding,
4238 SyncListener<T> listener) {
4239 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4243 public <T> void forPossibleValue(Resource subject, Binding binding,
4244 Listener<T> listener) {
4245 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4249 final public <T> void forPossibleValue(final Resource resource,
4250 final Binding binding, final AsyncProcedure<T> procedure) {
4252 assert (resource != null);
4253 assert (binding != null);
4254 assert (procedure != null);
4256 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
4259 public void execute(AsyncReadGraph graph, byte[] result) {
4263 if (result == null) {
4264 procedure.execute(graph, null);
4268 Serializer serializer = Bindings.getSerializer( binding );
4269 Object obj = serializer.deserialize(result);
4270 if (!binding.isInstance(obj))
4271 procedure.exception(graph, new ClassCastException(
4272 "Cannot get value " + obj + " with binding "
4275 procedure.execute(graph, (T) obj);
4277 } catch (Throwable t) {
4278 procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
4283 public void exception(AsyncReadGraph graph, Throwable t) {
4285 procedure.exception(graph, t);
4286 } catch (Throwable t2) {
4287 Logger.defaultLogError(t2);
4292 public String toString() {
4293 return "forPossibleValue -> " + procedure;
4301 public <T> void forPossibleValue(Resource subject, Binding binding,
4302 SyncProcedure<T> procedure) {
4303 forPossibleValue(subject, binding, new SyncToAsyncProcedure<T>(
4308 public <T> void forPossibleValue(Resource subject, Binding binding,
4309 Procedure<T> procedure) {
4310 forPossibleValue(subject, binding, new NoneToAsyncProcedure<T>(
4315 public <T> void forPossibleRelatedValue(Resource subject,
4316 Resource relation, AsyncListener<T> listener) {
4317 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4322 public <T> void forPossibleRelatedValue(Resource subject,
4323 Resource relation, SyncListener<T> listener) {
4324 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4329 public <T> void forPossibleRelatedValue(Resource subject,
4330 Resource relation, Listener<T> listener) {
4331 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4336 final public <T> void forPossibleRelatedValue(final Resource subject,
4337 final Resource relation, final AsyncProcedure<T> procedure) {
4339 assert (subject != null);
4340 assert (relation != null);
4341 assert (procedure != null);
4343 final DeepSingleOrNullProcedure<T> checkedProcedure = new DeepSingleOrNullProcedure<T>(procedure);
4345 processor.forEachObject(this, subject, relation,
4346 new AsyncMultiProcedureAdapter<Resource>() {
4349 public void execute(AsyncReadGraph graph,
4350 final Resource object) {
4352 checkedProcedure.inc();
4354 graph.forValue(object, new AsyncProcedure<Object>() {
4357 public void execute(AsyncReadGraph graph,
4359 checkedProcedure.offer(graph, (T) result);
4360 checkedProcedure.dec(graph);
4364 public void exception(AsyncReadGraph graph,
4366 checkedProcedure.exception(graph, t);
4367 checkedProcedure.dec(graph);
4375 public void finished(AsyncReadGraph graph) {
4377 checkedProcedure.dec(graph);
4381 public void exception(AsyncReadGraph graph, Throwable t) {
4382 checkedProcedure.exception(graph, t);
4383 checkedProcedure.dec(graph);
4387 public String toString() {
4388 return "forPossibleRelatedValue -> " + procedure;
4395 public <T> void forPossibleRelatedValue(Resource subject,
4396 Resource relation, SyncProcedure<T> procedure) {
4397 forPossibleRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
4402 public <T> void forPossibleRelatedValue(Resource subject,
4403 Resource relation, Procedure<T> procedure) {
4404 forPossibleRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
4409 public <T> void forPossibleRelatedValue(Resource subject,
4410 Resource relation, Binding binding, AsyncListener<T> listener) {
4411 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4416 public <T> void forPossibleRelatedValue(Resource subject,
4417 Resource relation, Binding binding, SyncListener<T> listener) {
4418 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4423 public <T> void forPossibleRelatedValue(Resource subject,
4424 Resource relation, Binding binding, Listener<T> listener) {
4425 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4429 final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4430 final AsyncProcedure<T> procedure) {
4432 assert (subject != null);
4433 assert (relation != null);
4434 assert (procedure != null);
4436 processor.forPossibleObject(this, subject, relation, new AsyncProcedure<Resource>() {
4439 public void execute(AsyncReadGraph graph, Resource object) {
4441 if(object == null) {
4442 procedure.execute(graph, null);
4446 processor.forPossibleValue((ReadGraphImpl)graph, object, new AsyncProcedure<byte[]>() {
4449 public void execute(AsyncReadGraph graph, byte[] bytes) {
4455 Serializer serializer = binding.serializer();
4456 Object obj = serializer.deserialize(bytes);
4457 if (!binding.isInstance(obj)) {
4458 procedure.exception(graph, new ClassCastException("Cannot get value " + obj + " with binding " + binding));
4460 procedure.execute(graph, (T) obj);
4463 } catch (Throwable t) {
4465 procedure.exception(graph, t);
4471 procedure.execute(graph, null);
4478 public void exception(AsyncReadGraph graph, Throwable t) {
4479 procedure.exception(graph, t);
4487 public void exception(AsyncReadGraph graph, Throwable throwable) {
4488 throwable.printStackTrace();
4489 procedure.exception(graph, throwable);
4497 public <T> void forPossibleRelatedValue(Resource subject,
4498 Resource relation, Binding binding, SyncProcedure<T> procedure) {
4499 forPossibleRelatedValue(subject, relation, binding,
4500 new SyncToAsyncProcedure<T>(procedure));
4504 public <T> void forPossibleRelatedValue(Resource subject,
4505 Resource relation, Binding binding, Procedure<T> procedure) {
4506 forPossibleRelatedValue(subject, relation, binding,
4507 new NoneToAsyncProcedure<T>(procedure));
4511 public void forIsInstanceOf(Resource subject, Resource relation,
4512 AsyncListener<Boolean> listener) {
4513 asyncRequest(new IsInstanceOf(subject, relation), listener);
4517 public void forIsInstanceOf(Resource subject, Resource relation,
4518 SyncListener<Boolean> listener) {
4519 asyncRequest(new IsInstanceOf(subject, relation), listener);
4523 public void forIsInstanceOf(Resource subject, Resource relation,
4524 Listener<Boolean> listener) {
4525 asyncRequest(new IsInstanceOf(subject, relation), listener);
4529 final public void forIsInstanceOf(final Resource resource,
4530 final Resource type, final AsyncProcedure<Boolean> procedure) {
4532 assert (resource != null);
4533 assert (type != null);
4534 assert (procedure != null);
4536 forTypes(resource, new AsyncProcedure<Set<Resource>>() {
4539 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4542 if (result.contains(type))
4543 procedure.execute(graph, true);
4545 procedure.execute(graph, false);
4546 } catch (Throwable t) {
4547 Logger.defaultLogError(t);
4552 public void exception(AsyncReadGraph graph, Throwable t) {
4554 procedure.exception(graph, t);
4555 } catch (Throwable t2) {
4556 Logger.defaultLogError(t2);
4561 public String toString() {
4562 return "forIsInstanceOf -> " + procedure;
4570 public void forIsInstanceOf(Resource subject, Resource relation,
4571 SyncProcedure<Boolean> procedure) {
4572 forIsInstanceOf(subject, relation, new SyncToAsyncProcedure<Boolean>(
4577 public void forIsInstanceOf(Resource subject, Resource relation,
4578 Procedure<Boolean> procedure) {
4579 forIsInstanceOf(subject, relation, new NoneToAsyncProcedure<Boolean>(
4584 public void forIsInheritedFrom(Resource subject, Resource relation,
4585 AsyncListener<Boolean> listener) {
4586 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4590 public void forIsInheritedFrom(Resource subject, Resource relation,
4591 SyncListener<Boolean> listener) {
4592 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4596 public void forIsInheritedFrom(Resource subject, Resource relation,
4597 Listener<Boolean> listener) {
4598 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4602 final public void forIsInheritedFrom(final Resource resource,
4603 final Resource type, final AsyncProcedure<Boolean> procedure) {
4605 assert (resource != null);
4606 assert (type != null);
4607 assert (procedure != null);
4609 if (resource.equals(type)) {
4611 procedure.execute(this, true);
4612 } catch (Throwable t) {
4613 Logger.defaultLogError(t);
4618 forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
4621 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4623 if (result.contains(type))
4624 procedure.execute(graph, true);
4626 procedure.execute(graph, false);
4627 } catch (Throwable t) {
4628 Logger.defaultLogError(t);
4633 public void exception(AsyncReadGraph graph, Throwable t) {
4635 procedure.exception(graph, t);
4636 } catch (Throwable t2) {
4637 Logger.defaultLogError(t2);
4642 public String toString() {
4643 return "forIsInheritedFrom -> " + procedure;
4651 public void forIsInheritedFrom(Resource subject, Resource relation,
4652 SyncProcedure<Boolean> procedure) {
4653 forIsInheritedFrom(subject, relation,
4654 new SyncToAsyncProcedure<Boolean>(procedure));
4658 public void forIsInheritedFrom(Resource subject, Resource relation,
4659 Procedure<Boolean> procedure) {
4660 forIsInheritedFrom(subject, relation,
4661 new NoneToAsyncProcedure<Boolean>(procedure));
4665 public void forIsSubrelationOf(Resource subject, Resource relation,
4666 AsyncListener<Boolean> listener) {
4667 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4671 public void forIsSubrelationOf(Resource subject, Resource relation,
4672 SyncListener<Boolean> listener) {
4673 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4677 public void forIsSubrelationOf(Resource subject, Resource relation,
4678 Listener<Boolean> listener) {
4679 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4683 final public void forIsSubrelationOf(final Resource resource,
4684 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4686 assert (resource != null);
4687 assert (relation != null);
4688 assert (procedure != null);
4690 if (resource.equals(relation)) {
4691 procedure.execute(this, true);
4695 forSuperrelations(resource, new AsyncProcedure<Set<Resource>>() {
4698 public void execute(AsyncReadGraph graph, Set<Resource> result) {
4700 if (result.contains(relation))
4701 procedure.execute(graph, true);
4703 procedure.execute(graph, false);
4704 } catch (Throwable t) {
4705 Logger.defaultLogError(t);
4710 public void exception(AsyncReadGraph graph, Throwable t) {
4712 procedure.exception(graph, t);
4713 } catch (Throwable t2) {
4714 Logger.defaultLogError(t2);
4719 public String toString() {
4720 return "forIsSubrelationOf -> " + procedure;
4728 public void forIsSubrelationOf(Resource subject, Resource relation,
4729 SyncProcedure<Boolean> procedure) {
4730 forIsSubrelationOf(subject, relation,
4731 new SyncToAsyncProcedure<Boolean>(procedure));
4735 public void forIsSubrelationOf(Resource subject, Resource relation,
4736 Procedure<Boolean> procedure) {
4737 forIsSubrelationOf(subject, relation,
4738 new NoneToAsyncProcedure<Boolean>(procedure));
4742 public void forHasStatement(Resource subject,
4743 AsyncListener<Boolean> listener) {
4744 asyncRequest(new HasStatementSubject(subject), listener);
4748 public void forHasStatement(Resource subject, SyncListener<Boolean> listener) {
4749 asyncRequest(new HasStatementSubject(subject), listener);
4753 public void forHasStatement(Resource subject, Listener<Boolean> listener) {
4754 asyncRequest(new HasStatementSubject(subject), listener);
4758 final public void forHasStatement(final Resource subject,
4759 final AsyncProcedure<Boolean> procedure) {
4761 assert (subject != null);
4762 assert (procedure != null);
4764 processor.forHasStatement(this, subject, procedure);
4769 public void forHasStatement(Resource subject,
4770 SyncProcedure<Boolean> procedure) {
4771 forHasStatement(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4775 public void forHasStatement(Resource subject, Procedure<Boolean> procedure) {
4776 forHasStatement(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4780 public void forHasStatement(Resource subject, Resource relation,
4781 AsyncListener<Boolean> listener) {
4782 asyncRequest(new HasStatement(subject, relation), listener);
4786 public void forHasStatement(Resource subject, Resource relation,
4787 SyncListener<Boolean> listener) {
4788 asyncRequest(new HasStatement(subject, relation), listener);
4792 public void forHasStatement(Resource subject, Resource relation,
4793 Listener<Boolean> listener) {
4794 asyncRequest(new HasStatement(subject, relation), listener);
4798 final public void forHasStatement(final Resource subject,
4799 final Resource relation, final AsyncProcedure<Boolean> procedure) {
4801 assert (subject != null);
4802 assert (relation != null);
4803 assert (procedure != null);
4805 processor.forHasStatement(this, subject, relation, procedure);
4810 public void forHasStatement(Resource subject, Resource relation,
4811 SyncProcedure<Boolean> procedure) {
4812 forHasStatement(subject, relation, new SyncToAsyncProcedure<Boolean>(
4817 public void forHasStatement(Resource subject, Resource relation,
4818 Procedure<Boolean> procedure) {
4819 forHasStatement(subject, relation, new NoneToAsyncProcedure<Boolean>(
4824 public void forHasStatement(Resource subject, Resource relation,
4825 Resource object, AsyncListener<Boolean> listener) {
4826 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4831 public void forHasStatement(Resource subject, Resource relation,
4832 Resource object, SyncListener<Boolean> listener) {
4833 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4838 public void forHasStatement(Resource subject, Resource relation,
4839 Resource object, Listener<Boolean> listener) {
4840 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4845 final public void forHasStatement(final Resource subject,
4846 final Resource relation, final Resource object,
4847 final AsyncProcedure<Boolean> procedure) {
4849 assert (subject != null);
4850 assert (relation != null);
4851 assert (object != null);
4852 assert (procedure != null);
4854 processor.forHasStatement(this, subject, relation, object, procedure);
4859 public void forHasStatement(Resource subject, Resource relation,
4860 Resource object, SyncProcedure<Boolean> procedure) {
4861 forHasStatement(subject, relation, object,
4862 new SyncToAsyncProcedure<Boolean>(procedure));
4866 public void forHasStatement(Resource subject, Resource relation,
4867 Resource object, Procedure<Boolean> procedure) {
4868 forHasStatement(subject, relation, object,
4869 new NoneToAsyncProcedure<Boolean>(procedure));
4873 public void forHasValue(Resource subject, AsyncListener<Boolean> listener) {
4874 asyncRequest(new HasValue(subject), listener);
4878 public void forHasValue(Resource subject, SyncListener<Boolean> listener) {
4879 asyncRequest(new HasValue(subject), listener);
4883 public void forHasValue(Resource subject, Listener<Boolean> listener) {
4884 asyncRequest(new HasValue(subject), listener);
4888 final public void forHasValue(final Resource subject,
4889 final AsyncProcedure<Boolean> procedure) {
4891 assert (subject != null);
4892 assert (procedure != null);
4894 processor.forValue(this, subject, new AsyncProcedure<byte[]>() {
4897 public void execute(AsyncReadGraph graph, byte[] result) {
4900 procedure.execute(graph, false);
4902 procedure.execute(graph, true);
4903 } catch (Throwable t) {
4904 Logger.defaultLogError(t);
4909 public void exception(AsyncReadGraph graph, Throwable t) {
4911 procedure.exception(graph, t);
4912 } catch (Throwable t2) {
4913 Logger.defaultLogError(t2);
4918 public String toString() {
4919 return "forHasValue -> " + procedure;
4927 public void forHasValue(Resource subject, SyncProcedure<Boolean> procedure) {
4928 forHasValue(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4932 public void forHasValue(Resource subject, Procedure<Boolean> procedure) {
4933 forHasValue(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4937 public void forOrderedSet(Resource subject,
4938 AsyncMultiListener<Resource> listener) {
4939 asyncRequest(new OrderedSet(subject), listener);
4943 public void forOrderedSet(Resource subject,
4944 SyncMultiListener<Resource> listener) {
4945 asyncRequest(new OrderedSet(subject), listener);
4949 public void forOrderedSet(Resource subject, MultiListener<Resource> listener) {
4950 asyncRequest(new OrderedSet(subject), listener);
4954 final public void forOrderedSet(final Resource subject,
4955 final AsyncMultiProcedure<Resource> procedure) {
4957 assert (subject != null);
4958 assert (procedure != null);
4960 processor.forOrderedSet(this, subject,
4961 new AsyncMultiProcedure<Resource>() {
4964 public void finished(AsyncReadGraph graph) {
4966 procedure.finished(graph);
4967 } catch (Throwable t) {
4968 Logger.defaultLogError(t);
4973 public void execute(AsyncReadGraph graph, Resource result) {
4975 procedure.execute(graph, result);
4976 } catch (Throwable t) {
4977 Logger.defaultLogError(t);
4982 public void exception(AsyncReadGraph graph, Throwable t) {
4984 procedure.exception(graph, t);
4985 } catch (Throwable t2) {
4986 Logger.defaultLogError(t2);
4991 public String toString() {
4992 return "forOrderedSet -> " + procedure;
5000 public void forOrderedSet(Resource subject,
5001 SyncMultiProcedure<Resource> procedure) {
5002 forOrderedSet(subject, new SyncToAsyncMultiProcedure<Resource>(
5007 public void forOrderedSet(Resource subject,
5008 MultiProcedure<Resource> procedure) {
5009 forOrderedSet(subject, new NoneToAsyncMultiProcedure<Resource>(
5014 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5015 AsyncListener<T> listener) {
5016 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5020 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5021 SyncListener<T> listener) {
5022 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5026 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5027 Listener<T> listener) {
5028 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5032 final public <T> void forPossibleAdapted(final Resource resource,
5033 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5035 assert (resource != null);
5036 assert (clazz != null);
5037 assert (procedure != null);
5039 final AdaptionService service = getSession().peekService(AdaptionService.class);
5040 if (service == null)
5041 procedure.exception(this, new ServiceException("No AdaptionService available"));
5043 service.adapt(this, resource, resource, Resource.class, clazz, true, procedure);
5047 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5048 SyncProcedure<T> procedure) {
5049 forPossibleAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5054 public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5055 Procedure<T> procedure) {
5056 forPossibleAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5061 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5062 AsyncListener<T> listener) {
5063 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5067 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5068 SyncListener<T> listener) {
5069 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5073 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5074 Listener<T> listener) {
5075 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5079 final public <T> void forPossibleUniqueAdapted(final Resource resource,
5080 final Class<T> clazz, final AsyncProcedure<T> procedure) {
5082 assert (resource != null);
5083 assert (clazz != null);
5084 assert (procedure != null);
5086 final AdaptionService service = getSession().peekService(AdaptionService.class);
5087 if (service == null)
5088 procedure.exception(this, new ServiceException("No AdaptionService available"));
5090 service.adaptNew(this, resource, clazz, true, procedure);
5095 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5096 SyncProcedure<T> procedure) {
5097 forPossibleUniqueAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5102 public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5103 Procedure<T> procedure) {
5104 forPossibleUniqueAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5109 * Implementation of the interface AsyncRequestProcessor
5113 final public Session getSession() {
5114 return processor.getSession();
5118 public <T> void asyncRequest(final Read<T> request) {
5120 asyncRequest(request, new AsyncProcedure<T>() {
5123 public void execute(AsyncReadGraph graph, T result) {
5127 public void exception(AsyncReadGraph graph, Throwable t) {
5128 Logger.defaultLogError(t);
5132 public String toString() {
5133 return "asyncRequest(Read) -> " + request;
5141 public <T> void asyncRequest(Read<T> request, AsyncListener<T> procedure) {
5142 asyncRequest(request, (AsyncProcedure<T>) procedure);
5146 public <T> void asyncRequest(Read<T> request,
5147 final SyncListener<T> procedure) {
5148 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5152 public <T> void asyncRequest(Read<T> request, final Listener<T> procedure) {
5153 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5157 public <T> void asyncRequest(final Read<T> request, final AsyncProcedure<T> procedure) {
5159 assert (request != null);
5160 assert (procedure != null);
5162 AsyncBarrierImpl barrier = asyncBarrier;
5166 processor.scheduleNow(new SessionTask(this) {
5169 public void run0(int thread) {
5171 final ListenerBase listener = getListenerBase(procedure);
5172 QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5173 } catch (DatabaseException e) {
5174 Logger.defaultLogError(e);
5185 public static ReadGraphImpl createAsync(QueryProcessor support) {
5186 return new ReadGraphImpl(null, null, support);
5190 public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
5191 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5195 public <T> void asyncRequest(Read<T> request, final Procedure<T> procedure) {
5196 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5200 final public <T> void asyncRequest(final AsyncRead<T> request) {
5202 assert (request != null);
5204 asyncRequest(request, new AsyncProcedure<T>() {
5207 public void execute(AsyncReadGraph graph, T result) {
5211 public void exception(AsyncReadGraph graph, Throwable t) {
5212 Logger.defaultLogError(t);
5216 public String toString() {
5217 return "asyncRequest(AsyncRead) -> " + request;
5225 public <T> void asyncRequest(AsyncRead<T> request,
5226 AsyncListener<T> procedure) {
5227 asyncRequest(request, (AsyncProcedure<T>) procedure);
5231 final public <T> void asyncRequest(AsyncRead<T> request,
5232 final SyncListener<T> procedure) {
5233 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5237 final public <T> void asyncRequest(AsyncRead<T> request,
5238 final Listener<T> procedure) {
5239 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5243 final public <T> void asyncRequest(final AsyncRead<T> request,
5244 final AsyncProcedure<T> procedure) {
5246 assert (request != null);
5247 assert (procedure != null);
5249 //ITask task = ThreadLogger.task(request);
5251 AsyncBarrierImpl barrier = asyncBarrier;
5255 processor.scheduleNow(new SessionTask(this) {
5258 public void run0(int thread) {
5264 final ListenerBase listener = getListenerBase(procedure);
5265 QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, new AsyncProcedure<T>() {
5268 public void execute(AsyncReadGraph graph, T result) {
5270 procedure.execute(graph, result);
5276 public void exception(AsyncReadGraph graph, Throwable throwable) {
5278 procedure.exception(graph, throwable);
5286 } catch (DatabaseException e) {
5287 Logger.defaultLogError(e);
5296 public <T> void asyncRequest(AsyncRead<T> request,
5297 SyncProcedure<T> procedure) {
5298 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5302 final public <T> void asyncRequest(final AsyncRead<T> request,
5303 final Procedure<T> procedure) {
5304 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5308 public <T> void asyncRequest(final MultiRead<T> request) {
5310 assert (request != null);
5312 asyncRequest(request, new SyncMultiProcedureAdapter<T>() {
5314 public void exception(ReadGraph graph, Throwable t) {
5315 Logger.defaultLogError(t);
5319 public String toString() {
5320 return "asyncRequest(MultiRead) -> " + request;
5327 public <T> void asyncRequest(MultiRead<T> request,
5328 SyncMultiListener<T> procedure) {
5329 asyncRequest(request, (SyncMultiProcedure<T>)procedure);
5333 public <T> void asyncRequest(MultiRead<T> request,
5334 MultiListener<T> procedure) {
5335 asyncRequest(request, new NoneToSyncMultiListener<T>(procedure));
5340 public <T> void asyncRequest(final MultiRead<T> request,
5341 final SyncMultiProcedure<T> procedure) {
5343 assert (request != null);
5344 assert (procedure != null);
5346 final ListenerBase listener = getListenerBase(procedure);
5348 if (parent != null || listener != null) {
5350 // final ReadGraphImpl newGraph = newSync();
5351 processor.query(this, request, parent, procedure,listener);
5355 // final ReadGraphImpl newGraph = newSync();
5359 request.perform(this, procedure);
5361 } catch (Throwable t) {
5364 procedure.exception(this, t);
5365 } catch (DatabaseException e) {
5366 LOGGER.error("Unexpected exception while handling exception", e);
5376 public <T> void asyncRequest(MultiRead<T> request,
5377 MultiProcedure<T> procedure) {
5378 asyncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
5382 final public <T> void asyncRequest(final AsyncMultiRead<T> request) {
5384 assert (request != null);
5386 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5388 public void exception(AsyncReadGraph graph, Throwable t) {
5389 Logger.defaultLogError(t);
5393 public String toString() {
5394 return "asyncRequest(AsyncMultiRead) -> " + request;
5401 public <T> void asyncRequest(AsyncMultiRead<T> request,
5402 AsyncMultiListener<T> procedure) {
5403 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5407 public <T> void asyncRequest(AsyncMultiRead<T> request,
5408 SyncMultiListener<T> procedure) {
5409 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5413 public <T> void asyncRequest(AsyncMultiRead<T> request,
5414 MultiListener<T> procedure) {
5415 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5419 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5420 final AsyncMultiProcedure<T> procedure) {
5422 assert (request != null);
5423 assert (procedure != null);
5425 ListenerBase listener = getListenerBase(procedure);
5427 if (parent != null || listener != null) {
5429 processor.query(this, request, parent, procedure, listener);
5435 request.perform(this, new AsyncMultiProcedure<T>() {
5438 public void execute(AsyncReadGraph graph, T result) {
5439 procedure.execute(graph, result);
5443 public void finished(AsyncReadGraph graph) {
5444 procedure.finished(graph);
5448 public void exception(AsyncReadGraph graph, Throwable t) {
5449 procedure.exception(graph, t);
5453 public String toString() {
5454 return "asyncRequest(AsyncMultiRead) -> " + procedure;
5459 } catch (Throwable t) {
5461 procedure.exception(this, new DatabaseException(t));
5469 public <T> void asyncRequest(AsyncMultiRead<T> request,
5470 SyncMultiProcedure<T> procedure) {
5471 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5475 final public <T> void asyncRequest(AsyncMultiRead<T> request,
5476 final MultiProcedure<T> procedure) {
5477 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5481 final public <T> void asyncRequest(final ExternalRead<T> request) {
5483 assert (request != null);
5485 asyncRequest(request, new Procedure<T>() {
5488 public void execute(T result) {
5492 public void exception(Throwable t) {
5493 Logger.defaultLogError(t);
5497 public String toString() {
5498 return "asyncRequest(PrimitiveRead) -> " + request;
5506 public <T> void asyncRequest(ExternalRead<T> request,
5507 final Listener<T> procedure) {
5508 asyncRequest(request, (Procedure<T>) procedure);
5512 final public <T> void asyncRequest(final ExternalRead<T> request,
5513 final Procedure<T> procedure) {
5515 assert (request != null);
5516 assert (procedure != null);
5518 final ListenerBase listener = getListenerBase(procedure);
5520 if (parent != null || listener != null) {
5523 QueryCacheBase.resultExternalReadEntry(this, request, parent, listener, procedure);
5524 } catch (DatabaseException e) {
5525 Logger.defaultLogError(e);
5526 // This throwable has already been transferred to procedure at this point - do nothing about it
5531 request.register(this, new Listener<T>() {
5533 public void execute(T result) {
5534 procedure.execute(result);
5538 public void exception(Throwable t) {
5539 procedure.exception(t);
5543 public String toString() {
5544 return "asyncRequest(PrimitiveRead) -> " + request;
5548 public boolean isDisposed() {
5559 public void asyncRequest(final Write request) {
5561 assert (request != null);
5563 getSession().asyncRequest(request);
5565 // processor.asyncWrite(request);
5570 public <T> void asyncRequest(final WriteResult<T> request, Procedure<T> procedure) {
5571 throw new Error("Not implemented.");
5575 public void asyncRequest(Write request, Consumer<DatabaseException> callback) {
5577 assert (request != null);
5579 getSession().asyncRequest(request, callback);
5584 public void asyncRequest(final DelayedWrite request) {
5586 assert (request != null);
5588 getSession().asyncRequest(request);
5593 public <T> void asyncRequest(final DelayedWriteResult<T> request, Procedure<T> procedure) {
5594 throw new Error("Not implemented.");
5598 public void asyncRequest(DelayedWrite r,
5599 Consumer<DatabaseException> callback) {
5600 throw new Error("Not implemented.");
5604 public void asyncRequest(final WriteOnly request) {
5606 assert (request != null);
5608 getSession().asyncRequest(request);
5613 public <T> void asyncRequest(final WriteOnlyResult<T> request, Procedure<T> procedure) {
5614 throw new Error("Not implemented.");
5618 public void asyncRequest(WriteOnly r, Consumer<DatabaseException> callback) {
5619 throw new Error("Not implemented.");
5623 * Implementation of the interface ServiceLocator
5627 public <T> T getService(Class<T> api) {
5628 if(WriteSupport.class == api) {
5629 if(this instanceof WriteGraphImpl) {
5630 WriteGraphImpl impl = (WriteGraphImpl)this;
5631 return (T)impl.writeSupport;
5634 return getSession().getService(api);
5638 public <T> T peekService(Class<T> api) {
5639 return getSession().peekService(api);
5643 public boolean hasService(Class<?> api) {
5644 return getSession().hasService(api);
5648 public <T> void registerService(Class<T> api, T service) {
5649 getSession().registerService(api, service);
5653 public boolean isImmutable(Resource resource) throws DatabaseException {
5654 ResourceImpl impl = (ResourceImpl)resource;
5655 return processor.isImmutable(impl.id);
5662 protected static String INTERNAL_ERROR_STRING = "Transaction aborted due to internal client error.";
5665 * callerThread is the currently running thread state.syncThread is blocking for
5666 * this execution state.syncParent is the blocking request
5669 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support) {
5670 this.parentGraph = parentGraph;
5671 this.parent = parent;
5672 this.processor = support;
5673 this.asyncBarrier = new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent, null);
5676 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support, AsyncBarrierImpl asyncBarrier) {
5677 this.parentGraph = parentGraph;
5678 this.parent = parent;
5679 this.processor = support;
5680 this.asyncBarrier = asyncBarrier;
5683 ReadGraphImpl(ReadGraphImpl graph, CacheEntry parent) {
5684 this(graph, parent, graph.processor);
5687 ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, Runnable callback) {
5688 this(parentGraph, parent, parentGraph.processor, new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent, callback));
5691 ReadGraphImpl(ReadGraphImpl graph) {
5692 this(graph, graph.parent);
5695 public ReadGraphImpl withParent(CacheEntry parent, Runnable callback) {
5696 return new ReadGraphImpl(this, parent, callback);
5699 public ReadGraphImpl withParent(CacheEntry parent) {
5700 return withParent(parent, null);
5703 public ReadGraphImpl syncWithParent(CacheEntry parent) {
5704 return new ReadGraphImpl(this, parent, processor, null);
5707 public ReadGraphImpl forRecompute(CacheEntry parent) {
5708 return new ReadGraphImpl(null, parent, processor);
5711 public static ReadGraphImpl create(QueryProcessor support) {
5712 ReadGraphImpl result = new ReadGraphImpl(null, null, support);
5716 public ReadGraphImpl newRestart(ReadGraphImpl impl) {
5718 WriteGraphImpl write = processor.getSession().getService(
5719 WriteGraphImpl.class);
5725 final private ListenerBase getListenerBase(final Object procedure) {
5726 if (procedure instanceof ListenerBase)
5727 return (ListenerBase) procedure;
5732 public <T> void waitAsyncProcedure(AsyncMultiReadProcedure<T> procedure) {
5734 assert(procedure.done());
5738 public <T> void waitAsyncProcedure(AsyncReadProcedure<T> procedure) {
5740 assert(procedure.done());
5744 public boolean resumeTasks() {
5745 return processor.resumeTasks(this);
5748 Class<?> singleClass(Set<Resource> types) {
5749 Class<?> result = null;
5750 for (Resource type : types) {
5751 Class<?> clazz = processor.getBuiltinValue(type);
5752 if (clazz != null) {
5762 private String debugString(Resource r) {
5765 name = getPossibleRelatedValue(r, processor.getL0(this).HasName);
5766 } catch (ManyObjectsForFunctionalRelationException e) {
5767 Logger.defaultLogError(e);
5768 } catch (ServiceException e) {
5769 Logger.defaultLogError(e);
5771 return "[" + name + " - " + r + "]";
5775 public String toString() {
5776 return "ReadGraphImpl[thread=" + Thread.currentThread() + "]";
5780 final public int thread() {
5784 static class MultiTripleIntProcedure implements TripleIntProcedure {
5786 final private AsyncMultiProcedure<Statement> procedure;
5787 final private ReadGraphImpl impl;
5788 final private QuerySupport support;
5790 public MultiTripleIntProcedure(AsyncMultiProcedure<Statement> procedure, ReadGraphImpl impl, QuerySupport support) {
5791 this.procedure = procedure;
5793 this.support = support;
5797 public void execute(ReadGraphImpl graph, int s, int p, int o) {
5799 procedure.execute(graph, support.getStatement(s, p, o));
5800 } catch (Throwable t2) {
5801 Logger.defaultLogError(t2);
5806 public void finished(ReadGraphImpl graph) {
5808 procedure.finished(graph);
5809 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5810 } catch (Throwable t2) {
5811 Logger.defaultLogError(t2);
5816 public void exception(ReadGraphImpl graph, Throwable t) {
5818 procedure.exception(graph, t);
5819 } catch (Throwable t2) {
5820 Logger.defaultLogError(t2);
5822 // impl.state.barrier.dec("ReadGraphSupportImpl.516");
5826 public String toString() {
5827 return "forEachObject with " + procedure;
5832 // private AsyncMultiProcedure<Resource> cacheKey = null;
5833 // private MultiIntProcedure cacheResult = null;
5835 // final IntProcedure forMultiProcedure(final AsyncMultiProcedure<Resource> procedure) {
5837 // if(procedure == cacheKey) return cacheResult;
5839 // cacheResult = new MultiIntProcedure(procedure, this, processor.support);
5840 // cacheKey = procedure;
5842 // return cacheResult;
5846 // private AsyncMultiProcedure<Statement> cacheKey2 = null;
5847 // private MultiTripleIntProcedure cacheResult2 = null;
5849 // final synchronized TripleIntProcedure forMultiProcedure(final AsyncMultiProcedure<Statement> procedure) {
5851 // if(procedure == cacheKey2) return cacheResult2;
5853 // cacheResult2 = new MultiTripleIntProcedure(procedure, this, processor.support);
5854 // cacheKey2 = procedure;
5856 // return cacheResult2;
5861 public Datatype getDataType(Resource subject) throws DatabaseException {
5862 for(Resource dataTypeResource : getObjects(subject, processor.getL0(this).HasDataType))
5863 return getValue(dataTypeResource, Bindings.getBindingUnchecked(Datatype.class));
5864 throw new DoesNotContainValueException("The literal has no data type.");
5867 protected <T extends Accessor> T getAccessor4File(Resource subject)
5868 throws DatabaseException {
5871 byte[] bytes = processor.support.getValue(g, subject);
5875 BinaryVariant va = (BinaryVariant) Accessors.getAccessor(bytes, Datatypes.VARIANT);
5876 Accessor ca = va.getContentAccessor();
5878 } catch (AccessorConstructionException e) {
5879 throw new DatabaseException(e);
5885 Binding datatype_binding = Bindings.getBindingUnchecked(DataType.class);
5886 Serializer datatype_serializer = datatype_binding.serializer();
5889 BinaryMemory in = new BinaryMemory(ByteBuffer.wrap(bytes));
5890 datatype = (DataType)datatype_serializer.deserialize(in);
5891 Binding data_binding = Bindings.getBinding(datatype);
5892 Serializer data_serializer = data_binding.serializer(BinarySerializationFormat.INSTANCE);
5893 Object o = data_serializer.deserialize(in);
5895 return (T)Accessors.getAccessor(data_binding, o);
5896 } catch(AccessorConstructionException e) {
5899 } catch (Exception e) {
5900 throw new DatabaseException(e);
5903 @SuppressWarnings("unchecked")
5905 public <T extends Accessor> T getAccessor(Resource subject) throws DatabaseException {
5906 RandomAccessBinary rab = getRandomAccessBinary(subject);
5908 return (T)Accessors.getAccessor(rab, getDataType(subject));
5909 } catch(AccessorConstructionException e) {
5910 throw new DatabaseException(e);
5913 @SuppressWarnings("unchecked")
5914 protected <T extends Accessor> T createAccessor(Resource resource, Datatype datatype, Object intialValue)
5915 throws DatabaseException {
5916 RandomAccessBinary rab = createRandomAccessBinary(resource, datatype, intialValue);
5918 return (T)Accessors.getAccessor(rab, datatype);
5919 } catch(AccessorConstructionException e) {
5920 throw new DatabaseException(e);
5924 public RandomAccessBinary getRandomAccessBinary(Resource subject) throws DatabaseException {
5925 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5926 ResourceData rd = ravs.get(subject);
5930 ExternalValueSupport evs = getService(ExternalValueSupport.class);
5931 long size = evs.getValueSize(this, subject); // Throws DatabaseException if no old external value.
5933 File platform = Platform.getLocation().toFile();
5934 File tempFiles = new File(platform, "tempFiles");
5935 File dbDir = new File(tempFiles, "db");
5937 File file = new File(dbDir, "ResourceFile" + subject.getResourceId());
5938 rd = new ResourceData(new BinaryFile(file), true); // Old external value.
5939 final int N = 1<<20;
5943 int length = N < left ? N : (int)left;
5944 byte[] bytes = evs.readValue(this, subject, offset, length);
5945 offset += bytes.length;
5946 left -= bytes.length;
5947 rd.binaryFile.write(bytes);
5949 ravs.put(subject, rd);
5951 } catch (Exception e) {
5952 throw new DatabaseException("Resource " + subject + " have value but there was problem with accessing value data.", e);
5954 } catch (Exception e) {
5955 if(Development.DEVELOPMENT) {
5956 if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
5957 e.printStackTrace();
5961 Datatype datatype = getDataType(subject);
5962 Object value = getPossibleValue(subject, Bindings.getBinding(datatype));
5963 return createRandomAccessBinary(subject, datatype, value);
5965 public RandomAccessBinary createRandomAccessBinary(Resource resource, Datatype datatype, Object initialValue)
5966 throws DatabaseException {
5967 RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5969 File platform = Platform.getLocation().toFile();
5970 File tempFiles = new File(platform, "tempFiles");
5971 File dbDir = new File(tempFiles, "db");
5973 File file = new File(dbDir, "ResourceFile" + resource.getResourceId());
5974 ResourceData rd = new ResourceData(new BinaryFile(file), false);
5975 Binding binding = Bindings.getBinding(datatype);
5976 if (null == initialValue) {
5977 initialValue = binding.createDefault();
5979 Serializer serializer = binding.serializer();
5980 byte[] bytes = serializer.serialize(initialValue);
5981 // In case the file has been previously accessed and was larger we set the correct size now
5982 rd.binaryFile.setLength(bytes.length);
5983 rd.binaryFile.write(bytes);
5984 ravs.put(resource, rd);
5986 } catch (Exception e) {
5987 if (e instanceof DatabaseException)
5988 throw (DatabaseException)e;
5990 throw new DatabaseException(e);
5994 // static class ExternalValueRequest<T> extends ResourceRead<T> {
5996 // public ExternalValueRequest(Resource resource) {
6000 // @SuppressWarnings("unchecked")
6002 // public T perform(ReadGraph graph) throws DatabaseException {
6005 // String uri = graph.getURI(resource);
6006 // if(Layer0.URIs.Functions_functionApplication.equals(uri)) return (T)functionApplication;
6008 // return (T)ReflectionUtils.getValue(uri).getValue();
6010 // } catch(ValueNotFoundException e) {
6011 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
6012 // } catch(ClassCastException e) {
6013 // throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
6019 @SuppressWarnings("unchecked")
6021 public <T> T getValue2(Resource r, Object context) throws DatabaseException {
6022 Layer0 L0 = processor.getL0(this);
6023 Set<Resource> types = getTypes(r);
6025 if(types.contains(L0.Literal)) {
6026 if(isImmutable(r)) {
6027 return syncRequest(new ValueImplied<T>(r));
6032 else if(types.contains(L0.ExternalValue)) {
6033 return (T)syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6037 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6038 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6040 return function.apply(this, r, context);
6041 } catch(RuntimeException e) {
6042 DatabaseException dte = findPossibleRootException(e);
6043 if(dte != null) throw dte;
6044 else throw new DatabaseException(e);
6051 public Variant getVariantValue2(Resource r, Object context) throws DatabaseException {
6052 Layer0 L0 = processor.getL0(this);
6053 Set<Resource> types = getTypes(r);
6055 if(types.contains(L0.Literal)) {
6056 if(isImmutable(r)) {
6057 return syncRequest(new VariantValueImplied(r));
6059 return getVariantValue(r);
6062 else if(types.contains(L0.ExternalValue)) {
6063 Object value = syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6065 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6066 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6067 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6072 Function3<ReadGraph,Resource,Object,Object> function = requestValueFunction(r);
6073 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6075 Object value = function.apply(this, r, context);
6077 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6078 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6079 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6081 } catch(RuntimeException e) {
6082 DatabaseException dte = findPossibleRootException(e);
6083 if(dte != null) throw dte;
6084 else throw new DatabaseException(e);
6090 public <T> T getPossibleValue2(Resource subject, Object context) throws DatabaseException {
6092 return getValue2(subject, context);
6093 } catch (DatabaseException e) {
6098 static class PossibleConverterFunction<T> extends ResourceRead<Function3<ReadGraph,Resource,Object,T>> {
6100 public PossibleConverterFunction(Resource resource) {
6105 public Function3<ReadGraph,Resource,Object,T> perform(ReadGraph graph) throws DatabaseException {
6106 return compute(graph, resource);
6109 @SuppressWarnings("unchecked")
6110 public static <T> Function3<ReadGraph,Resource,Object,T> compute(ReadGraph graph, Resource resource) throws DatabaseException {
6111 Layer0 L0 = Layer0.getInstance(graph);
6112 for(Resource converter : graph.getObjects(resource, L0.ConvertsToValueWith)) {
6114 if(L0.Functions_functionApplication.equals(converter)) {
6115 return (Function3<ReadGraph,Resource,Object,T>)graph.syncRequest(new AdaptValue(resource));
6117 return graph.getValue2(converter, resource);
6119 } catch(RuntimeException e) {
6120 DatabaseException dte = findPossibleRootException(e);
6121 if(dte != null) throw dte;
6122 else throw new DatabaseException(e);
6130 <T> Function3<ReadGraph,Resource,Object,T> requestValueFunction(Resource r) throws DatabaseException {
6132 return syncRequest(new PossibleConverterFunction<T>(r), TransientCacheAsyncListener.<Function3<ReadGraph,Resource,Object,T>>instance());
6134 return syncRequest(new PossibleConverterFunction<T>(r));
6138 * Get a value associated with a graph {@link Resource}, using a possible context object and
6139 * a desired value binding. The following methods are tried in order to retreive the value:
6141 * <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>
6142 * <li>If the resource is a {@code L0.ExternalValue}, the value is acquired using
6143 * {@link ReflectionUtils#getValue(String)}.</li>
6144 * <li>If the resource is associated with a suitable value converter with relation {@code L0.ConvertsToValueWith}
6145 * (see {@link #requestValueFunction(Resource)}), the value function is called with the graph, the resource
6146 * and the context object.</li>
6149 * @param r A graph resource with which the value is associated
6150 * @param context A context object that is used for acquiring the value (only applied in case 3)
6151 * @param binding A binding for the value type (only applied in case 1)
6152 * @return The value of the graph node.
6153 * @throws DoesNotContainValueException No value is associated with the graph node.
6154 * @throws DatabaseException Other errors, such as an error in casting the value to the return type or
6155 * a runtime error in the value function.
6157 @SuppressWarnings("unchecked")
6159 public <T> T getValue2(Resource r, Object context, Binding binding) throws DatabaseException {
6160 if (binding instanceof ObjectVariantBinding)
6161 return getValue2(r, context);
6163 Layer0 L0 = processor.getL0(this);
6164 Set<Resource> types = getTypes(r);
6165 if(types.contains(L0.Literal)) {
6166 if(isImmutable(r)) {
6167 return syncRequest(new Value<T>(r, binding));
6169 return getValue(r, binding);
6171 } else if(types.contains(L0.ExternalValue)) {
6172 ComputationalValue cv = syncRequest(new PossibleAdapter<ComputationalValue>(r, ComputationalValue.class), TransientCacheAsyncListener.instance());
6174 return cv.getValue(this, r);
6176 // This should not even be possible since L0 defines an adapter for all values
6178 return (T)ReflectionUtils.getValue(getURI(r)).getValue();
6179 } catch(ValueNotFoundException e) {
6180 throw new DatabaseException(e);
6181 } catch(ClassCastException e) {
6182 throw new DatabaseException(e);
6187 Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6188 if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function.");
6190 Object value = function.apply(this, r, context);
6191 if(binding.isInstance(value)) return (T)value;
6192 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
6193 return (T)Bindings.adapt(value, srcBinding, binding);
6194 } catch(RuntimeException e) {
6195 DatabaseException dte = findPossibleRootException(e);
6196 if(dte != null) throw dte;
6197 else throw new DatabaseException(e);
6198 } catch (AdaptException e) {
6199 throw new DatabaseException(e);
6200 } catch (org.simantics.databoard.binding.error.BindingException e) {
6201 throw new DatabaseException(e);
6207 public <T> T getPossibleValue2(Resource subject, Object context, Binding binding) throws DatabaseException {
6209 return getValue2(subject, context, binding);
6210 } catch (DatabaseException e) {
6215 private static DatabaseException findPossibleRootException(Throwable t) {
6216 if(t == null) return null;
6217 if(t instanceof DatabaseException) return (DatabaseException)t;
6218 if(t instanceof RuntimeException || t instanceof InvocationTargetException) {
6219 return findPossibleRootException(t.getCause());
6225 public <T> T getRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6226 return getRelatedValue2(subject, relation, subject);
6230 public Variant getRelatedVariantValue2(Resource subject, Resource relation) throws DatabaseException {
6231 return getRelatedVariantValue2(subject, relation, subject);
6235 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6237 Resource object = getPossibleObject(subject, relation);
6238 if(object == null) return null;
6239 else return getValue2(object, subject);
6240 } catch (DatabaseException e) {
6246 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6247 if(Development.DEVELOPMENT) {
6248 if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
6249 String error = L0Validations.checkValueType(this, subject, relation);
6251 Logger.defaultLogError(new ValidationException(error));
6252 throw new ValidationException(error);
6256 return getValue2(getSingleObject(subject, relation), context);
6260 public Variant getRelatedVariantValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6261 if(Development.DEVELOPMENT) {
6262 if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
6263 String error = L0Validations.checkValueType(this, subject, relation);
6265 Logger.defaultLogError(new ValidationException(error));
6266 throw new ValidationException(error);
6270 return getVariantValue2(getSingleObject(subject, relation), context);
6274 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6276 Resource object = getPossibleObject(subject, relation);
6277 if(object == null) return null;
6278 else return getValue2(object, context);
6279 } catch (DatabaseException e) {
6285 public <T> T getRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6286 return getRelatedValue2(subject, relation, subject, binding);
6290 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6292 Resource object = getPossibleObject(subject, relation);
6293 if(object == null) return null;
6294 return getValue2(object, subject, binding);
6295 } catch (DatabaseException e) {
6301 public <T> T getRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6302 return getValue2(getSingleObject(subject, relation), context, binding);
6306 public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6308 Resource object = getPossibleObject(subject, relation);
6309 if(object == null) return null;
6310 else return getValue2(object, context, binding);
6311 } catch (DatabaseException e) {
6317 public Type getRelatedValueType(Resource subject, Resource relation) throws DatabaseException {
6318 Layer0 L0 = processor.getL0(this);
6319 Resource property = getSingleObject(subject, relation);
6320 String typeText = (String)getRelatedValue(property, L0.HasValueType, Bindings.STRING);
6322 return org.simantics.scl.compiler.types.Types.parseType(typeText);
6323 } catch (SCLTypeParseException e) {
6324 throw new DatabaseException(e);
6328 private static ThreadLocal<Boolean> syncGraph = new ThreadLocal<Boolean>() {
6329 protected Boolean initialValue() {
6335 public boolean setSynchronous(boolean value) {
6336 boolean old = getSynchronous();
6337 syncGraph.set(value);
6342 public boolean getSynchronous() {
6343 return syncGraph.get();
6346 public void ensureLoaded(int resource) {
6347 processor.querySupport.ensureLoaded(this, resource);
6350 public void ensureLoaded(int resource, int predicate) {
6351 processor.querySupport.ensureLoaded(this, resource, predicate);
6354 public byte[] getValue(int resource) {
6355 return processor.querySupport.getValue(this, resource);
6358 public int thread(int resource) {
6359 return (resource >>> 16) & processor.THREAD_MASK;
6362 public int thread(Resource resource) {
6363 return (((ResourceImpl)resource).id >>> 16) & processor.THREAD_MASK;
6366 public ResourceSupport getResourceSupport() {
6367 return processor.getResourceSupport();
6371 public Object getModificationCounter() {
6372 return processor.getSession().getModificationCounter();
6376 public boolean performPending() {
6377 return processor.performPending(this);
6380 public Set<ReadGraphImpl> ancestorSet() {
6381 HashSet<ReadGraphImpl> result = new HashSet<>();
6382 ReadGraphImpl g = this;
6390 public int getLevel() {
6391 return getLevelStatic(this);
6394 private static int getLevelStatic(ReadGraphImpl impl) {
6395 if(impl == null) return 0;
6396 else return 1 + getLevelStatic(impl.parentGraph);
6399 public boolean isParent(ReadGraphImpl impl) {
6400 if(impl == null) return false;
6401 if(this == impl) return true;
6402 return isParent(impl.parentGraph);
6405 public ReadGraphImpl getTopLevelGraph() {
6406 return getTopLevelGraphStatic(this);
6409 private static ReadGraphImpl getTopLevelGraphStatic(ReadGraphImpl impl) {
6410 if(impl.parentGraph == null) return impl;
6411 else return getTopLevelGraphStatic(impl.parentGraph);