]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java
Merge "Trying to wait for procedures"
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / graph / ReadGraphImpl.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2018 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.impl.graph;
13
14 import java.io.BufferedOutputStream;
15 import java.io.File;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.io.PrintStream;
19 import java.lang.reflect.Array;
20 import java.lang.reflect.InvocationTargetException;
21 import java.nio.BufferUnderflowException;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.IdentityHashMap;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.ListIterator;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.function.Consumer;
33
34 import org.eclipse.core.runtime.Platform;
35 import org.simantics.databoard.Accessors;
36 import org.simantics.databoard.Bindings;
37 import org.simantics.databoard.accessor.Accessor;
38 import org.simantics.databoard.accessor.error.AccessorConstructionException;
39 import org.simantics.databoard.adapter.AdaptException;
40 import org.simantics.databoard.binding.Binding;
41 import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;
42 import org.simantics.databoard.binding.impl.ObjectVariantBinding;
43 import org.simantics.databoard.binding.mutable.Variant;
44 import org.simantics.databoard.serialization.Serializer;
45 import org.simantics.databoard.type.Datatype;
46 import org.simantics.databoard.util.binary.BinaryFile;
47 import org.simantics.databoard.util.binary.RandomAccessBinary;
48 import org.simantics.db.AsyncReadGraph;
49 import org.simantics.db.DevelopmentKeys;
50 import org.simantics.db.ExternalValueSupport;
51 import org.simantics.db.ReadGraph;
52 import org.simantics.db.RelationContext;
53 import org.simantics.db.Resource;
54 import org.simantics.db.Session;
55 import org.simantics.db.Statement;
56 import org.simantics.db.adaption.AdaptionService;
57 import org.simantics.db.common.primitiverequest.Adapter;
58 import org.simantics.db.common.primitiverequest.Builtin;
59 import org.simantics.db.common.primitiverequest.DatatypeBinding;
60 import org.simantics.db.common.primitiverequest.ForEachAssertedObject;
61 import org.simantics.db.common.primitiverequest.ForEachAssertedStatement;
62 import org.simantics.db.common.primitiverequest.HasStatement;
63 import org.simantics.db.common.primitiverequest.HasStatementSubject;
64 import org.simantics.db.common.primitiverequest.HasStatementSubjectObject;
65 import org.simantics.db.common.primitiverequest.HasValue;
66 import org.simantics.db.common.primitiverequest.Inverse;
67 import org.simantics.db.common.primitiverequest.IsInheritedFrom;
68 import org.simantics.db.common.primitiverequest.IsInstanceOf;
69 import org.simantics.db.common.primitiverequest.IsSubrelationOf;
70 import org.simantics.db.common.primitiverequest.OrderedSet;
71 import org.simantics.db.common.primitiverequest.PossibleAdapter;
72 import org.simantics.db.common.primitiverequest.PossibleInverse;
73 import org.simantics.db.common.primitiverequest.PossibleObject;
74 import org.simantics.db.common.primitiverequest.PossibleRelatedValue;
75 import org.simantics.db.common.primitiverequest.PossibleRelatedValueImplied;
76 import org.simantics.db.common.primitiverequest.PossibleStatement;
77 import org.simantics.db.common.primitiverequest.PossibleType;
78 import org.simantics.db.common.primitiverequest.PossibleUniqueAdapter;
79 import org.simantics.db.common.primitiverequest.PossibleValue;
80 import org.simantics.db.common.primitiverequest.PossibleValueImplied;
81 import org.simantics.db.common.primitiverequest.RelatedValue;
82 import org.simantics.db.common.primitiverequest.RelatedValueImplied;
83 import org.simantics.db.common.primitiverequest.SingleObject;
84 import org.simantics.db.common.primitiverequest.SingleStatement;
85 import org.simantics.db.common.primitiverequest.SingleType;
86 import org.simantics.db.common.primitiverequest.SingleTypeAny;
87 import org.simantics.db.common.primitiverequest.Types;
88 import org.simantics.db.common.primitiverequest.UniqueAdapter;
89 import org.simantics.db.common.primitiverequest.Value;
90 import org.simantics.db.common.primitiverequest.ValueImplied;
91 import org.simantics.db.common.primitiverequest.VariantValueImplied;
92 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
93 import org.simantics.db.common.procedure.adapter.AsyncProcedureAdapter;
94 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
95 import org.simantics.db.common.procedure.adapter.SyncMultiProcedureAdapter;
96 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
97 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
98 import org.simantics.db.common.procedure.single.SyncReadProcedure;
99 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrErrorProcedure;
100 import org.simantics.db.common.procedure.single.wrapper.DeepSingleOrNullProcedure;
101 import org.simantics.db.common.procedure.single.wrapper.ExceptionToNullProcedure;
102 import org.simantics.db.common.procedure.single.wrapper.NullSingleOrNullProcedure;
103 import org.simantics.db.common.procedure.single.wrapper.SingleFunctionalOrNullProcedure;
104 import org.simantics.db.common.procedure.single.wrapper.SingleOrErrorProcedure;
105 import org.simantics.db.common.procedure.single.wrapper.SingleOrNullProcedure;
106 import org.simantics.db.common.procedure.wrapper.NoneToAsyncListener;
107 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiListener;
108 import org.simantics.db.common.procedure.wrapper.NoneToAsyncMultiProcedure;
109 import org.simantics.db.common.procedure.wrapper.NoneToAsyncProcedure;
110 import org.simantics.db.common.procedure.wrapper.NoneToAsyncSetProcedure;
111 import org.simantics.db.common.procedure.wrapper.NoneToSyncMultiListener;
112 import org.simantics.db.common.procedure.wrapper.NoneToSyncMultiProcedure;
113 import org.simantics.db.common.procedure.wrapper.SyncToAsyncListener;
114 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiListener;
115 import org.simantics.db.common.procedure.wrapper.SyncToAsyncMultiProcedure;
116 import org.simantics.db.common.procedure.wrapper.SyncToAsyncProcedure;
117 import org.simantics.db.common.procedure.wrapper.SyncToAsyncSetProcedure;
118 import org.simantics.db.common.request.AdaptValue;
119 import org.simantics.db.common.request.ResourceRead;
120 import org.simantics.db.common.utils.Logger;
121 import org.simantics.db.common.utils.NameUtils;
122 import org.simantics.db.common.validation.L0Validations;
123 import org.simantics.db.exception.AdaptionException;
124 import org.simantics.db.exception.ArgumentException;
125 import org.simantics.db.exception.AssumptionException;
126 import org.simantics.db.exception.BindingException;
127 import org.simantics.db.exception.DatabaseException;
128 import org.simantics.db.exception.DoesNotContainValueException;
129 import org.simantics.db.exception.EmptyResourceException;
130 import org.simantics.db.exception.InternalException;
131 import org.simantics.db.exception.InvalidLiteralException;
132 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
133 import org.simantics.db.exception.NoInverseException;
134 import org.simantics.db.exception.NoSingleResultException;
135 import org.simantics.db.exception.ResourceNotFoundException;
136 import org.simantics.db.exception.ServiceException;
137 import org.simantics.db.exception.ValidationException;
138 import org.simantics.db.impl.BlockingAsyncProcedure;
139 import org.simantics.db.impl.RelationContextImpl;
140 import org.simantics.db.impl.ResourceImpl;
141 import org.simantics.db.impl.internal.RandomAccessValueSupport;
142 import org.simantics.db.impl.internal.ResourceData;
143 import org.simantics.db.impl.procedure.ResultCallWrappedSyncQueryProcedure;
144 import org.simantics.db.impl.query.CacheEntry;
145 import org.simantics.db.impl.query.QueryCache;
146 import org.simantics.db.impl.query.QueryCacheBase;
147 import org.simantics.db.impl.query.QueryProcessor;
148 import org.simantics.db.impl.query.QueryProcessor.SessionTask;
149 import org.simantics.db.impl.query.QuerySupport;
150 import org.simantics.db.impl.query.TripleIntProcedure;
151 import org.simantics.db.impl.support.ResourceSupport;
152 import org.simantics.db.procedure.AsyncListener;
153 import org.simantics.db.procedure.AsyncMultiListener;
154 import org.simantics.db.procedure.AsyncMultiProcedure;
155 import org.simantics.db.procedure.AsyncProcedure;
156 import org.simantics.db.procedure.AsyncSetListener;
157 import org.simantics.db.procedure.Listener;
158 import org.simantics.db.procedure.ListenerBase;
159 import org.simantics.db.procedure.MultiListener;
160 import org.simantics.db.procedure.MultiProcedure;
161 import org.simantics.db.procedure.Procedure;
162 import org.simantics.db.procedure.SetListener;
163 import org.simantics.db.procedure.StatementProcedure;
164 import org.simantics.db.procedure.SyncListener;
165 import org.simantics.db.procedure.SyncMultiListener;
166 import org.simantics.db.procedure.SyncMultiProcedure;
167 import org.simantics.db.procedure.SyncProcedure;
168 import org.simantics.db.procedure.SyncSetListener;
169 import org.simantics.db.request.AsyncMultiRead;
170 import org.simantics.db.request.AsyncRead;
171 import org.simantics.db.request.DelayedWrite;
172 import org.simantics.db.request.DelayedWriteResult;
173 import org.simantics.db.request.ExternalRead;
174 import org.simantics.db.request.MultiRead;
175 import org.simantics.db.request.Read;
176 import org.simantics.db.request.ReadInterface;
177 import org.simantics.db.request.Write;
178 import org.simantics.db.request.WriteInterface;
179 import org.simantics.db.request.WriteOnly;
180 import org.simantics.db.request.WriteOnlyResult;
181 import org.simantics.db.request.WriteResult;
182 import org.simantics.layer0.Layer0;
183 import org.simantics.scl.compiler.types.Type;
184 import org.simantics.scl.compiler.types.exceptions.SCLTypeParseException;
185 import org.simantics.scl.reflection.ReflectionUtils;
186 import org.simantics.scl.reflection.ValueNotFoundException;
187 import org.simantics.scl.runtime.function.Function3;
188 import org.simantics.utils.DataContainer;
189 import org.simantics.utils.Development;
190 import org.simantics.utils.datastructures.Pair;
191 import org.simantics.utils.datastructures.collections.CollectionUtils;
192 import org.slf4j.LoggerFactory;
193
194 import gnu.trove.map.hash.TObjectIntHashMap;
195
196 public class ReadGraphImpl implements AsyncReadGraph {
197
198     private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ReadGraphImpl.class);
199
200     final static boolean EMPTY_RESOURCE_CHECK = false;
201     
202         final public CacheEntry parent;
203         public final ReadGraphImpl parentGraph;
204         final public QueryProcessor processor;
205         
206         public final AsyncBarrierImpl asyncBarrier;
207         
208         final static Binding DATA_TYPE_BINDING_INTERNAL = Bindings.getBindingUnchecked(Datatype.class);
209         final static Serializer DATA_TYPE_SERIALIZER = Bindings.getSerializerUnchecked(DATA_TYPE_BINDING_INTERNAL);
210
211         final public static TObjectIntHashMap<String> counters = new TObjectIntHashMap<String>(); 
212         
213         public static void resetCounters() {
214                 counters.clear();
215         }
216         
217         public static String listCounters(File file) throws IOException {
218                 
219                 PrintStream b = new PrintStream(new BufferedOutputStream(new FileOutputStream(file)));
220
221                 for(Pair<String,Integer> p : CollectionUtils.valueSortedEntries(counters)) {
222                         b.print(-p.second + " " + p.first + "\n");
223                 }
224
225                 b.close();
226
227                 return "Dumped " + counters.size() + " queries.";
228                 
229         }
230         
231         /*
232          * Implementation of the interface ReadGraph
233          */
234         final public String getURI(final Resource resource)     throws AssumptionException, ValidationException, ServiceException {
235                 
236                 assert (resource != null);
237
238                 try {
239
240                         return syncRequest(new org.simantics.db.common.uri.ResourceToURI(resource));
241
242                 } catch (AssumptionException e) {
243
244                         throw new AssumptionException(e);
245
246                 } catch (ValidationException e) {
247
248                         throw new ValidationException(e);
249
250                 } catch (ServiceException e) {
251
252                         throw new ServiceException(e);
253
254                 } catch (DatabaseException e) {
255
256                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
257
258                 }
259
260         }
261         
262         final public String getPossibleURI(final Resource resource)     throws ValidationException,     ServiceException {
263
264                 assert (resource != null);
265
266                 try {
267
268                         return syncRequest(new org.simantics.db.common.uri.ResourceToPossibleURI(resource));
269
270                 } catch (ValidationException e) {
271
272                         throw new ValidationException(e);
273
274                 } catch (ServiceException e) {
275
276                         throw new ServiceException(e);
277
278                 } catch (DatabaseException e) {
279
280                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
281
282                 }
283
284         }
285
286         final public Resource getResource(final String id)
287                         throws ResourceNotFoundException, ValidationException,
288                         ServiceException {
289
290                 assert (id != null);
291
292                 try {
293
294                         Integer rid = QueryCache.resultURIToResource(this, id, parent, null);
295                         // FIXME: stupid to throw this here and catch and wrap it right away
296                         if(rid == 0) throw new ResourceNotFoundException(id);
297                         return processor.querySupport.getResource(rid);
298
299                 } catch (ResourceNotFoundException e) {
300
301                         throw new ResourceNotFoundException(id, e);
302
303                 } catch (ValidationException e) {
304
305                         throw new ValidationException(e);
306
307                 } catch (ServiceException e) {
308
309                         throw new ServiceException(e);
310
311                 } catch (DatabaseException e) {
312
313                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
314
315                 }
316
317         }
318
319         final public Resource getPossibleResource(final String id)
320         throws ResourceNotFoundException, ValidationException,
321         ServiceException {
322
323                 assert (id != null);
324
325                 try {
326
327                         return getResource(id);
328
329                 } catch (ResourceNotFoundException e) {
330                         
331                         return null;
332
333                 } catch (ValidationException e) {
334
335                         throw new ValidationException(e);
336
337                 } catch (ServiceException e) {
338
339                         throw new ServiceException(e);
340
341                 } catch (DatabaseException e) {
342
343                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
344
345                 }
346
347         }
348         
349         @Override
350         public Map<String, Resource> getChildren(Resource resource) throws ValidationException, ServiceException {
351                 
352                 assert (resource != null);
353
354                 try {
355
356                         int rId = processor.querySupport.getId(resource);
357                         return QueryCache.resultChildMap(this, rId, parent, null);
358
359                 } catch (ValidationException e) {
360
361                         throw new ValidationException(e);
362
363                 } catch (ServiceException e) {
364
365                         throw new ServiceException(e);
366
367                 } catch (DatabaseException e) {
368
369                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
370
371                 }
372                 
373         }
374
375         final public Resource getRootLibrary() {
376                 return processor.getRootLibraryResource();
377         }
378         
379         final public Resource getBuiltin(final String id)
380                         throws ResourceNotFoundException, ServiceException {
381
382                 assert (id != null);
383
384                 try {
385
386                         return syncRequest(new Builtin(id));
387
388                 } catch (ResourceNotFoundException e) {
389
390                         throw new ResourceNotFoundException(id, e);
391
392                 } catch (ServiceException e) {
393
394                         throw new ServiceException(e);
395
396                 } catch (DatabaseException e) {
397
398                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
399
400                 }
401
402         }
403
404         static class StatementReadProcedure extends TIntArrayListInternal implements StatementProcedure {
405
406                 private static Throwable DONE = new Throwable();
407                 
408                 Throwable exception = null;
409                 
410                 final ResourceSupport support;
411                 
412                 public StatementReadProcedure(ResourceSupport support) {
413                         this.support = support;
414                 }
415                 
416                 @Override
417                 public synchronized void execute(AsyncReadGraph graph, int s, int p, int o) {
418                         add(s);
419                         add(p);
420                         add(o);
421                 }
422                 
423                 @Override
424                 public void finished(AsyncReadGraph graph) {
425                         exception = DONE;
426                 }
427
428                 @Override
429                 public void exception(AsyncReadGraph graph, Throwable t) {
430                         exception = t;
431                 }
432                 
433                 public void checkAndThrow() throws DatabaseException {
434                         if(exception != DONE) {
435                                 if (exception instanceof DatabaseException)
436                                         throw (DatabaseException) exception;
437                                 else
438                                         throw new DatabaseException(
439                                                         "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
440                                                         exception);
441                         }
442                 }
443                 
444                 public boolean done() {
445                         return exception != null;
446                 }
447
448                 @Override
449                 public boolean contains(Object obj) {
450                     if(!(obj instanceof InternalStatement))
451                         return false;
452                     InternalStatement statement = (InternalStatement)obj;
453                     int s = statement.s;
454                     int p = statement.p;
455                     int o = statement.o;
456                     for(int i=0;i<sizeInternal();i+=3)
457                 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
458                     return true;
459                     return false;
460                 }
461
462                 @SuppressWarnings("unchecked")
463         @Override
464                 public <T> T[] toArray(T[] a) {
465                     int length = sizeInternal() / 3;
466                     if(length > a.length) {
467                         Class<?> arrayType = a.getClass();
468                         a = (arrayType == Object[].class) 
469                                 ? (T[]) new Object[length]
470                                 : (T[]) Array.newInstance(arrayType.getComponentType(), length);
471                     }
472                     else {
473                         for(int i=length;i<a.length;++i)
474                             a[i] = null;
475                     }
476             for(int i=0,j=0;i<sizeInternal();i+=3,++j)
477                 a[j] = (T)new InternalStatement(support, getQuick(i), getQuick(i+1), getQuick(i+2));
478             return a;
479                 }
480
481                 @Override
482                 public boolean add(Statement e) {
483                         throw new UnsupportedOperationException();
484                 }
485
486                 @Override
487                 public boolean remove(Object o) {
488                         throw new UnsupportedOperationException();
489                 }
490
491                 @Override
492                 public boolean addAll(Collection<? extends Statement> c) {
493                         throw new UnsupportedOperationException();
494                 }
495
496                 class IteratorImpl implements ListIterator<Statement> {
497             
498             int index;
499             
500             public IteratorImpl(int index) {
501                 this.index = index;
502             }
503
504             @Override
505             public boolean hasNext() {
506                 return index < sizeInternal(); 
507             }
508
509             @Override
510             public Statement next() {
511                 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2)); 
512                 index += 3;
513                 return result;
514             }
515
516             @Override
517             public void remove() {
518                 throw new Error("Not supported");
519             }
520
521             @Override
522             public boolean hasPrevious() {
523                 return index > 0;
524             }
525
526             @Override
527             public Statement previous() {
528                 index -= 3;
529                 Statement result = new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2)); 
530                 return result;
531             }
532
533             @Override
534             public int nextIndex() {
535                 return index/3;
536             }
537
538             @Override
539             public int previousIndex() {
540                 return index/3-1;
541             }
542
543             @Override
544             public void set(Statement e) {
545                 throw new UnsupportedOperationException();
546             }
547
548             @Override
549             public void add(Statement e) {
550                 throw new UnsupportedOperationException();
551             }
552             
553         };
554         
555                 @Override
556                 public Iterator<Statement> iterator() {
557                         return new IteratorImpl(0);
558                 }
559                 
560                 @Override
561                 public int size() {
562                         return sizeInternal() / 3;
563                 }
564
565                 @Override
566                 public Object[] toArray() {
567                     Object[] result = new Object[sizeInternal() / 3];
568                     for(int i=0,j=0;j<sizeInternal();i++,j+=3)
569                         result[i] = new InternalStatement(support, getQuick(j), getQuick(j+1), getQuick(j+2));
570                         return result;
571                 }
572
573         @Override
574         public boolean addAll(int index, Collection<? extends Statement> c) {
575             throw new UnsupportedOperationException();
576         }
577
578         @Override
579         public Statement get(int index) {
580             index += 3;
581             if(index < 0 || index >= sizeInternal())
582                 throw new IndexOutOfBoundsException();
583             return new InternalStatement(support, getQuick(index), getQuick(index+1), getQuick(index+2));
584         }
585
586         @Override
587         public Statement set(int index, Statement element) {
588             throw new UnsupportedOperationException();
589         }
590
591         @Override
592         public void add(int index, Statement element) {
593             throw new UnsupportedOperationException();
594         }
595
596         @Override
597         public Statement remove(int index) {
598             throw new UnsupportedOperationException();
599         }
600
601         @Override
602         public int indexOf(Object obj) {
603             if(!(obj instanceof InternalStatement))
604                 return -1;
605             InternalStatement statement = (InternalStatement)obj;
606             int s = statement.s;
607             int p = statement.p;
608             int o = statement.o;
609             for(int i=0;i<sizeInternal();i+=3)
610                 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
611                     return i/3;
612             return -1;
613         }
614
615         @Override
616         public int lastIndexOf(Object obj) {
617             if(!(obj instanceof InternalStatement))
618                 return -1;
619             InternalStatement statement = (InternalStatement)obj;
620             int s = statement.s;
621             int p = statement.p;
622             int o = statement.o;
623             for(int i=sizeInternal()-3;i>=0;i-=3)
624                 if(s==getQuick(i) && p==getQuick(i+1) && o==getQuick(i+2))
625                     return i/3;
626             return -1;
627         }
628
629         @Override
630         public ListIterator<Statement> listIterator() {
631             return new IteratorImpl(0);
632         }
633
634         @Override
635         public ListIterator<Statement> listIterator(int index) {
636             return new IteratorImpl(index*3);
637         }
638
639         @Override
640         public List<Statement> subList(int fromIndex, int toIndex) {
641             if(fromIndex < 0 || toIndex*3 >= sizeInternal() || fromIndex > toIndex)
642                 throw new IndexOutOfBoundsException();
643             return new RandomAccessSubList<Statement>(this, fromIndex, toIndex-fromIndex);
644         }
645         }
646         
647         @Override
648         final public Collection<Statement> getStatements(final Resource subject,
649                         final Resource relation)
650                         throws ManyObjectsForFunctionalRelationException, ServiceException {
651
652                 assert (subject != null);
653                 assert (relation != null);
654
655                 try {
656
657                         StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
658                         processor.forEachStatement(this, subject, relation, procedure);
659                         procedure.checkAndThrow();
660                         return procedure;
661                         
662                 } catch (DatabaseException e) {
663
664                         System.err.println(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation);
665
666                         StatementReadProcedure procedure = new StatementReadProcedure(getResourceSupport());
667                         processor.forEachStatement(this, subject, relation, procedure);
668                         
669                         return Collections.emptyList();
670                 
671 //                      throw new ServiceException(INTERNAL_ERROR_STRING + " getStatements " + subject + " " + relation, e);
672
673                 }
674
675         }
676
677         @Override
678         final public Collection<Statement> getAssertedStatements(final Resource subject, final Resource relation)
679                         throws ManyObjectsForFunctionalRelationException, ServiceException {
680
681                 assert (subject != null);
682                 assert (relation != null);
683
684                 try {
685
686                         return syncRequest(new ForEachAssertedStatement(subject, relation));
687
688                 } catch (ManyObjectsForFunctionalRelationException e) {
689
690                         throw new ManyObjectsForFunctionalRelationException(e);
691
692                 } catch (ServiceException e) {
693
694                         throw new ServiceException(e);
695
696                 } catch (DatabaseException e) {
697
698                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
699
700                 }
701
702         }
703
704         @Override
705         final public Collection<Resource> getPredicates(final Resource subject) throws ServiceException {
706
707                 assert (subject != null);
708
709                 try {
710
711                         return processor.getPredicates(this, subject);
712
713 //                      AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
714 //                      processor.forEachPredicate(this, subject, procedure);
715 //                      procedure.checkAndThrow();
716 //                      return procedure;
717
718                 } catch (ServiceException e) {
719
720                         throw new ServiceException(e);
721
722                 } catch (DatabaseException e) {
723
724                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
725
726                 }  catch (Throwable e) {
727
728             throw new ServiceException(e);
729
730                 }
731
732         }
733
734         @Override
735         final public Collection<Resource> getPrincipalTypes(final Resource subject)
736                         throws ServiceException {
737
738                 assert (subject != null);
739
740                 try {
741
742                         AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
743                         processor.forEachPrincipalType(this, subject, procedure);
744                         procedure.checkAndThrow();
745                         return procedure;
746                         
747                 } catch (ServiceException e) {
748
749                         throw new ServiceException(e);
750
751                 } catch (DatabaseException e) {
752
753                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
754
755                 }
756
757         }
758
759         @Override
760         final public Set<Resource> getTypes(final Resource subject) throws ServiceException {
761
762                 assert (subject != null);
763
764                 try {
765                         
766                         return processor.getTypes(this, subject);
767                         
768                 } catch (ServiceException e) {
769
770                         throw new ServiceException(e);
771                         
772                 } catch (DatabaseException e) {
773
774                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
775
776         } catch (Throwable e) {
777
778             throw new ServiceException(e);
779
780                 }
781
782         }
783
784         @Override
785         final public Set<Resource> getSupertypes(final Resource subject)
786                         throws ServiceException {
787
788                 assert (subject != null);
789
790                 try {
791
792                         SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
793                         processor.forSupertypes(this, subject, procedure);
794                         procedure.checkAndThrow();
795                         return procedure.result;
796
797                 } catch (ServiceException e) {
798
799                         throw new ServiceException(e);
800
801                 } catch (DatabaseException e) {
802
803                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
804
805                 }
806
807         }
808
809         @Override
810         final public Set<Resource> getSuperrelations(final Resource subject)
811                         throws ServiceException {
812
813                 assert (subject != null);
814
815                 try {
816
817                         SyncReadProcedure<Set<Resource>> procedure = new SyncReadProcedure<Set<Resource>>();
818                         processor.forSuperrelations(this, subject, procedure);
819                         procedure.checkAndThrow();
820                         return procedure.result;
821
822                 } catch (ServiceException e) {
823
824                         throw new ServiceException(e);
825
826                 } catch (DatabaseException e) {
827
828                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
829
830                 }
831
832         }
833         
834         @Override
835         public Resource getPossibleSuperrelation(Resource subject) throws ServiceException {
836                 
837                 try {
838
839                         SyncReadProcedure<Resource> procedure = new SyncReadProcedure<Resource>();
840                         processor.forPossibleSuperrelation(this, subject, procedure);
841                         procedure.checkAndThrow();
842                         return procedure.result;
843
844                 } catch (ServiceException e) {
845
846                         throw new ServiceException(e);
847
848                 } catch (DatabaseException e) {
849
850                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
851
852                 }
853
854         }
855
856         @Override
857         final public Collection<Resource> getObjects(final Resource subject, final Resource relation)
858                         throws ServiceException {
859
860                 assert (subject != null);
861                 assert (relation != null);
862
863                 if(Development.DEVELOPMENT) {
864             if(Development.isTrue(DevelopmentKeys.READGRAPH_COUNT)) {
865                 counters.adjustOrPutValue("objects $" + subject.getResourceId() + " $" + relation.getResourceId(), 1, 1);
866             }
867             //if(subject.getResourceId()==xx && relation.getResourceId()==xx) new Exception().printStackTrace();
868                 }
869                 
870                 try {
871
872                         AsyncMultiReadProcedure<Resource> procedure = new AsyncMultiReadProcedure<Resource>();
873                         processor.forEachObject(this, subject, relation, procedure);
874                         procedure.checkAndThrow();
875                         return procedure;
876                         
877                 } catch (DatabaseException e) {
878
879                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
880
881                 }
882
883         }
884
885         @Override
886         final public Collection<Resource> getAssertedObjects(
887                         final Resource subject, final Resource relation)
888                         throws ManyObjectsForFunctionalRelationException, ServiceException {
889
890         if (subject == null)
891             throw new ArgumentException("Subject must not be null.");
892         if (relation == null)
893             throw new ArgumentException("Relation must not be null. Subject=" + subject);
894
895                 try {
896
897                         return syncRequest(new ForEachAssertedObject(subject, relation));
898
899                 } catch (ManyObjectsForFunctionalRelationException e) {
900
901                         throw new ManyObjectsForFunctionalRelationException(e);
902
903                 } catch (ServiceException e) {
904
905                         throw new ServiceException(e);
906
907                 } catch (DatabaseException e) {
908
909                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
910
911                 }
912
913         }
914
915         @Override
916         final public Resource getInverse(final Resource relation) throws NoInverseException, ServiceException {
917
918                 assert (relation != null);
919
920                 try {
921
922                         return getSingleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
923
924                 } catch (NoSingleResultException e) {
925
926                         throw new NoInverseException(e);
927
928                 } catch (ServiceException e) {
929
930                         throw new ServiceException(e);
931
932                 }
933
934         }
935
936         @Override
937         final public Resource getSingleObject(final Resource subject, final Resource relation) throws NoSingleResultException, ServiceException {
938
939                 if( subject == null) throw new IllegalArgumentException("subject can not be null");
940                 if( relation == null) throw new IllegalArgumentException("relation can not be null");
941
942                 try {
943                         int single = processor.getSingleObject(this, subject, relation);
944                         if (single == 0) {
945                                 if (EMPTY_RESOURCE_CHECK) {
946                                         if (!hasStatement(subject)) {
947                                                 throw new EmptyResourceException("Resource " + debugString(subject));
948                                         }
949                                 }
950                                 throw new NoSingleResultException("No single object for subject " + debugString(subject)
951                                                 + " and relation " + debugString(relation), single);
952                         }
953                         return processor.querySupport.getResource(single);
954                 } catch (NoSingleResultException e) {
955                         throw e;
956                 } catch (DatabaseException e) {
957                         throw new ServiceException(e);
958                 } 
959         }
960
961         @Override
962         final public Statement getSingleStatement(final Resource subject, final Resource relation) throws NoSingleResultException, ManyObjectsForFunctionalRelationException, ServiceException {
963                 assert (subject != null);
964                 assert (relation != null);
965                 try {
966                         Collection<Statement> statements = getStatements(subject, relation);
967                         if (statements.size() == 1) {
968                                 return statements.iterator().next();
969                         } else {
970                                 if (EMPTY_RESOURCE_CHECK)
971                                         if (!hasStatement(subject))
972                                                 throw new EmptyResourceException("Resource " + debugString(subject));
973                                 throw new NoSingleResultException("No single statement for subject " + debugString(subject)
974                                                 + " and relation " + debugString(relation), statements.size());
975                         }
976                 } catch (ServiceException e) {
977                         throw new ServiceException(e);
978                 } 
979         }
980
981         @Override
982         final public Resource getSingleType(final Resource subject) throws NoSingleResultException, ServiceException {
983                 assert (subject != null);
984                 try {
985                         ArrayList<Resource> principalTypes = (ArrayList<Resource>)getPrincipalTypes(subject);
986                         if (principalTypes.size() == 1) {
987                             return principalTypes.get(0);
988                         } else {
989                             throw new NoSingleResultException("No single type for subject " + debugString(subject), principalTypes.size());
990                         }
991                 } catch (ServiceException e) {
992                         throw new ServiceException(e);
993                 } 
994         }
995
996         @Override
997         final public Resource getSingleType(final Resource subject,
998                         final Resource baseType) throws NoSingleResultException,
999                         ServiceException {
1000
1001                 assert (subject != null);
1002                 assert (baseType != null);
1003
1004                 try {
1005                         return syncRequest(new SingleType(subject, baseType));
1006                 } catch (DatabaseException e) {
1007                     throw new NoSingleResultException("subject=" + subject + ", baseType=" + baseType, 0, e);
1008                 }
1009         }
1010
1011         @Override
1012         final public <T> T getValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
1013
1014                 assert (subject != null);
1015
1016                 try {
1017
1018                         Layer0 L0 = processor.getL0(this);
1019             int object = processor.getSingleObject(this, subject, L0.HasDataType);
1020             if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
1021             
1022             if(processor.isImmutable(object)) {
1023                 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance()); 
1024                         return getValue(subject, binding);
1025             } else {
1026                     byte[] dt = processor.getValue(this, object);
1027                     if(dt == null) throw new ServiceException("No data type for " + subject);
1028                     Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1029                     Binding binding = Bindings.getBinding(datatype);
1030                     return getValue(subject, binding);
1031             }
1032                         
1033         } catch (IOException e) {
1034
1035             throw new ServiceException(e);
1036
1037                 } catch (DoesNotContainValueException e) {
1038
1039                         throw new DoesNotContainValueException(e, subject);
1040
1041                 } catch (ServiceException e) {
1042
1043                         throw new ServiceException(e);
1044
1045                 } catch (DatabaseException e) {
1046
1047                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
1048
1049         }
1050
1051         }
1052
1053     @Override
1054     final public Variant getVariantValue(final Resource subject) throws DoesNotContainValueException, ServiceException {
1055
1056         assert (subject != null);
1057
1058         try {
1059
1060             Layer0 L0 = processor.getL0(this);
1061             int object = processor.getSingleObject(this, subject, L0.HasDataType);
1062             if(object == 0) throw new DoesNotContainValueException("No data type for " + subject);
1063             
1064             if(processor.isImmutable(object)) {
1065                 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance()); 
1066                 return new Variant(binding, getValue(subject, binding));
1067             } else {
1068                 byte[] dt = processor.getValue(this, object);
1069                 if(dt == null) throw new ServiceException("No data type for " + subject);
1070                 Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1071                 Binding binding = Bindings.getBinding(datatype);
1072                 return new Variant(binding, getValue(subject, binding));
1073             }
1074             
1075         } catch (IOException e) {
1076
1077             throw new ServiceException(e);
1078
1079         } catch (DoesNotContainValueException e) {
1080
1081             throw new DoesNotContainValueException(e, subject);
1082
1083         } catch (ServiceException e) {
1084
1085             throw new ServiceException(e);
1086
1087         } catch (DatabaseException e) {
1088
1089             throw new ServiceException(INTERNAL_ERROR_STRING, e);
1090
1091         }               
1092         }
1093
1094         static final IdentityHashMap<Binding,Serializer> serializers = new IdentityHashMap<Binding,Serializer>();
1095         
1096         static {
1097                 serializers.put(Bindings.STRING, Bindings.STRING.serializer());
1098         }
1099         
1100         final protected Serializer getSerializer(Binding binding) {
1101             return binding.serializer();
1102         }
1103         
1104         @Override
1105         final public <T> T getValue(final Resource subject, final Binding binding) throws DoesNotContainValueException, BindingException,
1106                         ServiceException {
1107
1108                 assert (subject != null);
1109
1110                 try {
1111                         
1112                         byte[] bytes = processor.getValue(this, subject);
1113                         if (bytes == null) throw new DoesNotContainValueException("No value for resource " + subject);
1114
1115                         Serializer serializer = getSerializer(binding);
1116                         return (T)serializer.deserialize(bytes);
1117
1118                 } catch (DoesNotContainValueException e) {
1119
1120                         throw new DoesNotContainValueException(e);
1121
1122                 } catch (IOException e) {
1123
1124                         throw new ServiceException(e);
1125
1126                 } catch (DatabaseException e) {
1127                     
1128             throw new ServiceException(e);
1129             
1130         } catch (BufferUnderflowException e) {
1131             // This is sometimes thrown when deserialize fails because wrong format.
1132             // For callers of this method this is just an service exception.
1133             throw new ServiceException(e);
1134         }
1135
1136         }
1137
1138         @Override
1139         final public <T> T getRelatedValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1140                         DoesNotContainValueException, ServiceException {
1141
1142                 assert (subject != null);
1143                 assert (relation != null);
1144
1145                 try {
1146                         Resource object = getSingleObject(subject, relation);
1147                         return getValue(object);
1148                 } catch (NoSingleResultException e) {
1149                         throw new NoSingleResultException("No single value found for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1150                 } catch (DoesNotContainValueException e) {
1151                         try {
1152                                 Layer0 L0 = processor.getL0(this);
1153                                 Resource object = getPossibleObject(subject, relation);
1154                                 if(isInstanceOf(object, L0.Value)) {
1155                                         if(isInstanceOf(object, L0.Literal)) {
1156                                                 throw new DoesNotContainValueException(e);
1157                                         } else {
1158                                                 throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1159                                         }
1160                                 } else {
1161                                         throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1162                                 }
1163                         } catch (DoesNotContainValueException e2) {
1164                                 throw e2;
1165                         } catch (DatabaseException e2) {
1166                                 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1167                         }
1168                 } catch (ServiceException e) {
1169                         throw new ServiceException(e);
1170                 }
1171         }
1172
1173     @Override
1174     final public Variant getRelatedVariantValue(final Resource subject, final Resource relation) throws NoSingleResultException,
1175             DoesNotContainValueException, ServiceException {
1176
1177         assert (subject != null);
1178         assert (relation != null);
1179
1180         try {
1181             Resource object = getSingleObject(subject, relation);
1182             return getVariantValue(object);
1183         } catch (NoSingleResultException e) {
1184             throw new NoSingleResultException("No single object for subject " + debugString(subject) + " and relation " + debugString(relation), e.getResultCount(), e);
1185         } catch (DoesNotContainValueException e) {
1186             try {
1187                 Layer0 L0 = processor.getL0(this);
1188                 Resource object = getPossibleObject(subject, relation);
1189                 if(isInstanceOf(object, L0.Value)) {
1190                     if(isInstanceOf(object, L0.Literal)) {
1191                         throw new DoesNotContainValueException(e);
1192                     } else {
1193                         throw new InvalidLiteralException("The object " + object + " is not an instance of L0.Literal (use getRelatedValue2 instead)", e);
1194                     }
1195                 } else {
1196                     throw new DoesNotContainValueException("The object " + object + " is not an instance of L0.Value", e);
1197                 }
1198             } catch (DoesNotContainValueException e2) {
1199                 throw e2;
1200             } catch (DatabaseException e2) {
1201                 throw new InternalException("The client failed to analyse the cause of the following exception", e);
1202             }
1203         } catch (ServiceException e) {
1204             throw new ServiceException(e);
1205         } 
1206     }
1207     
1208         @Override
1209         final public <T> T getRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1210                         throws NoSingleResultException, DoesNotContainValueException, BindingException, ServiceException {
1211
1212                 assert (subject != null);
1213                 assert (relation != null);
1214
1215                 try {
1216                         Resource object = getSingleObject(subject, relation);
1217                         return getValue(object, binding);
1218                 } catch (NoSingleResultException e) {
1219                     String message = "";
1220                     try {
1221                     String subjectName = NameUtils.getSafeName(this, subject, true);
1222                     String relationName = NameUtils.getSafeName(this, relation, true);
1223                     message = "Subject: " + subjectName + ", Relation: " + relationName;
1224                     } catch (DatabaseException e2) {
1225                         
1226                     }
1227             throw new NoSingleResultException(message, e.getResultCount(), e);
1228                 } catch (DoesNotContainValueException e) {
1229                         throw new DoesNotContainValueException(e);
1230                 } catch (ServiceException e) {
1231                         throw new ServiceException(e);
1232                 }
1233         }
1234
1235         @Override
1236         final public <T> T adapt(final Resource resource, final Class<T> clazz)
1237                         throws AdaptionException, ValidationException, ServiceException {
1238
1239                 assert (resource != null);
1240                 assert (clazz != null);
1241
1242                 try {
1243
1244                         return syncRequest(new Adapter<T>(resource, clazz));
1245
1246                 } catch (AdaptionException e) {
1247
1248                         throw new AdaptionException(e);
1249
1250                 } catch (ValidationException e) {
1251
1252                         throw new ValidationException(e);
1253
1254                 } catch (ServiceException e) {
1255
1256                         throw new ServiceException(e);
1257
1258                 } catch (DatabaseException e) {
1259
1260                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
1261
1262                 }
1263
1264         }
1265
1266         @Override
1267         final public <T,C> T adaptContextual(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1268                         throws AdaptionException, ValidationException, ServiceException {
1269
1270                 assert (resource != null);
1271                 assert (context != null);
1272
1273                 class ContextualAdapter implements AsyncRead<T> {
1274
1275                         final private Resource resource;
1276                         final private C context;
1277                     final private Class<T> clazz;
1278                     
1279                     @Override
1280                     public int hashCode() {
1281                         return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1282                     }
1283                     
1284                     @Override
1285                     final public int threadHash() {
1286                         return resource.getThreadHash();
1287                     }
1288                     
1289                     @Override
1290                     public boolean equals(Object object) {
1291                         if (this == object)
1292                             return true;
1293                         else if (object == null)
1294                             return false;
1295                         else if (getClass() != object.getClass())
1296                             return false;
1297                         ContextualAdapter r = (ContextualAdapter)object;
1298                         return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1299                     }
1300
1301                     @Override
1302                     public int getFlags() {
1303                         return 0;
1304                     }
1305                     
1306                     public ContextualAdapter(Resource resource, C context, Class<T> clazz) {
1307                         this.resource = resource;
1308                         this.context = context;
1309                         this.clazz = clazz;
1310                     }
1311
1312                     @Override
1313                     public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1314                         
1315                                 final AdaptionService service = getSession().peekService(AdaptionService.class);
1316                                 if (service == null)
1317                                         procedure.exception(graph, new ServiceException("No AdaptionService available")); 
1318                                 else
1319                                         service.adapt(graph, resource, context, contextClass, clazz, false, procedure); 
1320                         
1321                     }
1322
1323                     @Override
1324                     public String toString() {
1325                         return "Adapter for (" + resource + "," + context + ") as " + clazz.getName();
1326                     }
1327                     
1328                 }
1329                 
1330                 try {
1331
1332                         return syncRequest(new ContextualAdapter(resource, context, clazz));
1333
1334                 } catch (AdaptionException e) {
1335
1336                         throw new AdaptionException(e);
1337
1338                 } catch (ValidationException e) {
1339
1340                         throw new ValidationException(e);
1341
1342                 } catch (ServiceException e) {
1343
1344                         throw new ServiceException(e);
1345
1346                 } catch (DatabaseException e) {
1347
1348                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
1349
1350                 }
1351
1352         }
1353         
1354         @Override
1355         final public <T> T adaptRelated(final Resource resource, final Resource relation, final Class<T> clazz)
1356                         throws AdaptionException, NoSingleResultException, ValidationException, ServiceException {
1357
1358                 assert (resource != null);
1359                 assert (clazz != null);
1360
1361                 Statement stm = getSingleStatement(resource, relation);
1362                 
1363                 return adaptContextual(stm.getObject(), new RelationContextImpl(resource, stm), RelationContext.class, clazz);
1364                 
1365         }
1366
1367         @Override
1368         final public <T> T getPossibleRelatedAdapter(final Resource resource, final Resource relation, final Class<T> clazz)
1369                         throws ValidationException, ServiceException {
1370
1371                 try {
1372                         return adaptRelated(resource, relation, clazz);
1373                 } catch (DatabaseException e) {
1374                         return null;
1375                 }
1376                 
1377         }
1378
1379         @Override
1380         final public <T,C> T getPossibleContextualAdapter(final Resource resource, final C context, final Class<C> contextClass, final Class<T> clazz)
1381                         throws ValidationException, ServiceException {
1382
1383                 assert (resource != null);
1384                 assert (context != null);
1385
1386                 class PossibleContextualAdapter implements AsyncRead<T> {
1387
1388                         final private Resource resource;
1389                         final private C context;
1390                     final private Class<T> clazz;
1391                     
1392                     @Override
1393                     public int hashCode() {
1394                         return resource.hashCode() + 31 * (clazz.hashCode() + 41 * context.hashCode());
1395                     }
1396                     
1397                     @Override
1398                     final public int threadHash() {
1399                         return resource.getThreadHash();
1400                     }
1401                     
1402                     @Override
1403                     public boolean equals(Object object) {
1404                         if (this == object)
1405                             return true;
1406                         else if (object == null)
1407                             return false;
1408                         else if (getClass() != object.getClass())
1409                             return false;
1410                         PossibleContextualAdapter r = (PossibleContextualAdapter)object;
1411                         return resource.equals(r.resource) && context.equals(r.context) && clazz.equals(r.clazz);
1412                     }
1413
1414                     @Override
1415                     public int getFlags() {
1416                         return 0;
1417                     }
1418                     
1419                     public PossibleContextualAdapter(Resource resource, C context, Class<T> clazz) {
1420                         this.resource = resource;
1421                         this.context = context;
1422                         this.clazz = clazz;
1423                     }
1424
1425                     @Override
1426                     public void perform(AsyncReadGraph graph, AsyncProcedure<T> procedure) {
1427                         
1428                                 final AdaptionService service = getSession().peekService(AdaptionService.class);
1429                                 if (service == null)
1430                                         procedure.exception(graph, new ServiceException("No AdaptionService available")); 
1431                                 else
1432                                         service.adapt(graph, resource, context, contextClass, clazz, true, procedure); 
1433                         
1434                     }
1435
1436                     @Override
1437                     public String toString() {
1438                         return "Possible adapter for (" + resource + "," + context + ") as " + clazz.getName();
1439                     }
1440                     
1441                 }
1442                 
1443                 try {
1444
1445                         return syncRequest(new PossibleContextualAdapter(resource, context, clazz));
1446
1447                 } catch (ValidationException e) {
1448
1449                         throw new ValidationException(e);
1450
1451                 } catch (ServiceException e) {
1452
1453                         throw new ServiceException(e);
1454
1455                 } catch (DatabaseException e) {
1456
1457                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
1458
1459                 }
1460                 
1461         }
1462
1463         @Override
1464         final public <T> T adaptUnique(final Resource resource, final Class<T> clazz)
1465                         throws AdaptionException, ValidationException, ServiceException {
1466
1467                 assert (resource != null);
1468                 assert (clazz != null);
1469
1470                 try {
1471
1472                         return syncRequest(new UniqueAdapter<T>(resource, clazz));
1473
1474                 } catch (AdaptionException e) {
1475
1476                         throw new AdaptionException(e);
1477
1478                 } catch (ValidationException e) {
1479
1480                         throw new ValidationException(e);
1481
1482                 } catch (ServiceException e) {
1483
1484                         throw new ServiceException(e);
1485
1486                 } catch (DatabaseException e) {
1487
1488                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
1489
1490                 }
1491
1492         }
1493
1494         @Override
1495         final public Resource getPossibleInverse(final Resource relation)
1496                         throws ServiceException {
1497
1498                 assert (relation != null);
1499
1500                 try {
1501
1502                         return getPossibleObject(relation, processor.querySupport.getResource(processor.getInverseOf()));
1503
1504                 } catch (ServiceException e) {
1505
1506                         throw new ServiceException(e);
1507
1508                 } catch (DatabaseException e) {
1509
1510                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
1511
1512                 }
1513
1514         }
1515
1516         @Override
1517         public Resource getPossibleObject(final Resource subject, final Resource relation)
1518                         throws ManyObjectsForFunctionalRelationException, ServiceException {
1519
1520                 assert (subject != null);
1521                 assert (relation != null);
1522
1523                 try {
1524
1525                     int result = processor.getSingleObject(this, subject, relation);
1526                     if(result == 0) return null;
1527                     
1528                     return processor.querySupport.getResource(result);
1529
1530              } catch (ManyObjectsForFunctionalRelationException e) {
1531
1532                  throw new ManyObjectsForFunctionalRelationException("subject=" + subject + ", relation=" + relation, e);
1533                  
1534                 } catch (DatabaseException e) {
1535
1536                         throw new ServiceException(e);
1537
1538                 }
1539                 
1540         }
1541
1542         @Override
1543         final public Statement getPossibleStatement(final Resource subject, final Resource relation)
1544                         throws ManyObjectsForFunctionalRelationException, ServiceException {
1545
1546                 assert (subject != null);
1547                 assert (relation != null);
1548
1549                 try {
1550
1551                         Collection<Statement> statements = getStatements(subject, relation);
1552                         if(statements.size() == 1) return statements.iterator().next();
1553                         else return null;
1554
1555                 } catch (ManyObjectsForFunctionalRelationException e) {
1556
1557                         throw new ManyObjectsForFunctionalRelationException("Many objects in " + subject + " for functional relation " + relation);
1558
1559                 } catch (ServiceException e) {
1560
1561                         throw new ServiceException(e);
1562
1563                 } 
1564
1565         }
1566
1567         @Override
1568         final public Resource getPossibleType(final Resource subject, final Resource baseType) throws ServiceException {
1569
1570                 assert (subject != null);
1571                 assert (baseType != null);
1572
1573                 try {
1574
1575                         AsyncReadProcedure<Resource> procedure = new AsyncReadProcedure<Resource>();
1576                         forPossibleType(subject, baseType, procedure);
1577                         procedure.checkAndThrow();
1578                         return procedure.result;                        
1579
1580                 } catch (ServiceException e) {
1581
1582                         throw new ServiceException(e);
1583
1584                 } catch (DatabaseException e) {
1585
1586                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
1587
1588                 }
1589
1590         }
1591
1592         @Override
1593         final public <T> T getPossibleValue(final Resource subject) throws ServiceException {
1594
1595                 assert (subject != null);
1596
1597                 try {
1598                     
1599                     int object = processor.getSingleObject(this, subject, processor.getL0(this).HasDataType);
1600                     if(object == 0) return null;
1601                     
1602             if(processor.isImmutable(object)) {
1603                 Binding binding = syncRequest(new DatatypeBinding(processor.querySupport.getResource(object)), TransientCacheListener.<Binding>instance()); 
1604                         return getPossibleValue(subject, binding);
1605             } else {
1606                     byte[] dt = processor.getValue(this, object);
1607                     if(dt == null) return null;
1608                     Datatype datatype = (Datatype)DATA_TYPE_SERIALIZER.deserialize(dt);
1609                     Binding binding = Bindings.getBinding(datatype);
1610                     return getPossibleValue(subject, binding);
1611             }
1612                     
1613         } catch (IOException e) {
1614             
1615             throw new ServiceException(e);
1616             
1617                 } catch (ServiceException e) {
1618
1619                         throw new ServiceException(e);
1620
1621                 } catch (DatabaseException e) {
1622
1623                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
1624
1625         }
1626
1627         }
1628
1629         @Override
1630         final public <T> T getPossibleValue(final Resource subject, final Binding binding) throws BindingException, ServiceException {
1631
1632                 assert (subject != null);
1633                 assert (binding != null);
1634
1635                 try {
1636
1637             byte[] dt = processor.getValue(this, subject);
1638             if(dt == null) return null;
1639                         Serializer serializer = getSerializer(binding);
1640             return (T)serializer.deserialize(dt);
1641
1642         } catch (IOException e) {
1643
1644             throw new ServiceException(e);
1645             
1646                 } catch (BindingException e) {
1647
1648                         throw new BindingException(e);
1649
1650                 } catch (ServiceException e) {
1651
1652                         throw new ServiceException(e);
1653
1654                 } catch (DatabaseException e) {
1655                         e.printStackTrace();
1656                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
1657         }
1658
1659         }
1660
1661         @Override
1662         public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation)
1663                         throws ManyObjectsForFunctionalRelationException, ServiceException {
1664
1665                 assert (subject != null);
1666                 assert (relation != null);
1667
1668                 try {
1669
1670                         Resource object = getPossibleObject(subject, relation);
1671                         if(object == null) return null;
1672                         else return getPossibleValue(object);
1673
1674                 } catch (ManyObjectsForFunctionalRelationException e) {
1675
1676                         throw new ManyObjectsForFunctionalRelationException(e);
1677
1678                 } catch (ServiceException e) {
1679
1680                         throw new ServiceException(e);
1681
1682                 } 
1683
1684         }
1685
1686         @Override
1687         public <T> T getPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding)
1688                         throws ManyObjectsForFunctionalRelationException, BindingException, ServiceException {
1689
1690                 assert (subject != null);
1691                 assert (relation != null);
1692                 assert (binding != null);
1693
1694                 try {
1695
1696                         Resource object = getPossibleObject(subject, relation);
1697                         if(object == null) return null;
1698                         else return getPossibleValue(object, binding);
1699
1700                 } catch (ManyObjectsForFunctionalRelationException e) {
1701
1702                         throw new ManyObjectsForFunctionalRelationException(e);
1703
1704                 } catch (BindingException e) {
1705
1706                         throw new BindingException(e);
1707
1708                 } catch (ServiceException e) {
1709
1710                         throw new ServiceException(e);
1711
1712                 }
1713
1714         }
1715
1716         @Override
1717         public <T> T getPossibleAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1718
1719                 assert (resource != null);
1720                 assert (clazz != null);
1721
1722                 try {
1723
1724                         return syncRequest(new PossibleAdapter<T>(resource, clazz));
1725
1726                 } catch (ValidationException e) {
1727
1728                         throw new ValidationException(e);
1729
1730                 } catch (AdaptionException e) {
1731
1732                         return null;
1733
1734                 } catch (DatabaseException e) {
1735
1736                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
1737
1738                 }
1739         }
1740
1741         @Override
1742         public <T> T getPossibleUniqueAdapter(Resource resource, Class<T> clazz) throws ValidationException, ServiceException {
1743
1744                 assert (resource != null);
1745                 assert (clazz != null);
1746
1747                 try {
1748
1749                         return syncRequest(new PossibleUniqueAdapter<T>(resource, clazz));
1750
1751                 } catch (AdaptionException e) {
1752
1753                         return null;
1754
1755                 } catch (ValidationException e) {
1756
1757                         throw new ValidationException(e);
1758
1759                 } catch (DatabaseException e) {
1760
1761                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
1762
1763                 }
1764
1765         }
1766
1767     @Override
1768     final public boolean isInstanceOf(final Resource resource, final Resource type) throws ServiceException {
1769
1770         assert (resource != null);
1771         assert (type != null);
1772
1773         Set<Resource> resources = getTypes(resource);
1774         // This check was necessary because some of the callers of this method got stuck when the NPE was thrown from here.
1775         if (null == resources)
1776             return false;
1777         
1778         if(EMPTY_RESOURCE_CHECK) {
1779             if (resources.isEmpty()) {
1780                 if(!hasStatement(resource)) throw new EmptyResourceException("Resource " + debugString(resource));
1781             }
1782         }
1783         
1784         return resources.contains(type);
1785
1786     }
1787
1788         @Override
1789         final public boolean isInheritedFrom(final Resource resource, final Resource type) throws ServiceException {
1790
1791                 assert (resource != null);
1792                 assert (type != null);
1793
1794                 try {
1795
1796                         if(resource.equals(type)) return true;
1797                         
1798                         return getSupertypes(resource).contains(type);
1799
1800                 } catch (ServiceException e) {
1801
1802                         throw new ServiceException(e);
1803
1804                 } 
1805                 
1806         }
1807
1808         @Override
1809         final public boolean isSubrelationOf(final Resource resource, final Resource type) throws ServiceException {
1810
1811                 assert (resource != null);
1812                 assert (type != null);
1813
1814                 try {
1815
1816                         if(resource.equals(type)) return true;
1817                         
1818                         return getSuperrelations(resource).contains(type);
1819
1820                 } catch (ServiceException e) {
1821
1822                         throw new ServiceException(e);
1823
1824                 } 
1825
1826         }
1827
1828         @Override
1829         final public boolean hasStatement(final Resource subject) throws ServiceException {
1830
1831                 assert (subject != null);
1832
1833                 try {
1834
1835                         SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1836                         processor.forHasStatement(this, subject, procedure);
1837                         procedure.checkAndThrow();
1838                         return procedure.result;
1839
1840                 } catch (ServiceException e) {
1841
1842                         throw new ServiceException(e);
1843
1844                 } catch (DatabaseException e) {
1845
1846                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
1847
1848                 }
1849
1850         }
1851
1852         @Override
1853         final public boolean hasStatement(final Resource subject, final Resource relation) throws ServiceException {
1854
1855                 assert (subject != null);
1856                 assert (relation != null);
1857
1858                 try {
1859
1860                         Collection<Resource> objects = getObjects(subject, relation);
1861                         return !objects.isEmpty();
1862
1863                 } catch (ServiceException e) {
1864
1865                         throw new ServiceException(e);
1866
1867                 } 
1868                 
1869         }
1870
1871         @Override
1872         final public boolean hasStatement(final Resource subject, final Resource relation, final Resource object) throws ServiceException {
1873
1874                 assert (subject != null);
1875                 assert (relation != null);
1876                 assert (object != null);
1877
1878                 try {
1879
1880                         for(Resource o : getObjects(subject, relation)) {
1881                                 if(object.equals(o)) return true;
1882                         }
1883                         
1884                         return false;
1885
1886                 } catch (ServiceException e) {
1887
1888                         throw new ServiceException(e);
1889
1890                 }
1891
1892         }
1893
1894         @Override
1895         final public boolean hasValue(final Resource subject) throws ServiceException {
1896
1897                 assert (subject != null);
1898
1899                 try {
1900
1901                         SyncReadProcedure<Boolean> procedure = new SyncReadProcedure<Boolean>();
1902                         processor.forHasValue(this, subject, procedure);
1903                         procedure.checkAndThrow();
1904                         return procedure.result;
1905
1906                 } catch (ServiceException e) {
1907
1908                         throw new ServiceException(e);
1909
1910                 } catch (DatabaseException e) {
1911
1912                         throw new ServiceException(INTERNAL_ERROR_STRING, e);
1913
1914                 }
1915
1916         }
1917
1918         final AsyncProcedure<?> NONE = new AsyncProcedure<Object>() {
1919
1920                 @Override
1921                 public void execute(AsyncReadGraph graph, Object result) {
1922                 }
1923
1924                 @Override
1925                 public void exception(AsyncReadGraph graph, Throwable throwable) {
1926                 }
1927                 
1928         };
1929         
1930         /*
1931          * Implementation of the interface RequestProcessor
1932          */
1933
1934         @Override
1935         public <T> T syncRequest(final Read<T> request) throws DatabaseException {
1936                 assert (request != null);
1937                 return (T)QueryCache.runnerReadEntry(this, request, parent, null, null, true);
1938         }
1939
1940         @Override
1941         public <T> T syncRequest(Read<T> request, SyncListener<T> procedure)
1942                         throws DatabaseException {
1943                 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
1944         }
1945
1946         @Override
1947         public <T> T syncRequest(Read<T> request, final Listener<T> procedure)
1948                         throws DatabaseException {
1949                 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
1950         }
1951
1952         @Override
1953         public <T> T syncRequest(final Read<T> request, final AsyncProcedure<T> procedure) throws DatabaseException {
1954
1955                 assert (request != null);
1956
1957                 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
1958
1959                 return QueryCache.resultReadEntry(this, request, parent, listener, procedure);
1960
1961         }
1962
1963         @Override
1964         public <T> T syncRequest(final Read<T> request,
1965                         final SyncProcedure<T> procedure) throws DatabaseException {
1966                 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
1967         }
1968
1969         @Override
1970         public <T> T syncRequest(Read<T> request, Procedure<T> procedure)
1971                         throws DatabaseException {
1972                 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
1973         }
1974
1975         static class AsyncReadProcedure<T> implements AsyncProcedure<T> {
1976
1977                 private static Throwable DONE = new Throwable();
1978                 
1979                 T result = null;
1980                 Throwable exception = null;
1981                 
1982                 @Override
1983                 public void execute(AsyncReadGraph graph, T t) {
1984                         result = t;
1985                         exception = DONE;
1986                 }
1987
1988                 @Override
1989                 public void exception(AsyncReadGraph graph, Throwable t) {
1990                         exception = t;
1991                 }
1992                 
1993                 public void checkAndThrow() throws DatabaseException {
1994                         if(exception != DONE) {
1995                                 if (exception instanceof DatabaseException)
1996                                         throw (DatabaseException) exception;
1997                                 else
1998                                         throw new DatabaseException(
1999                                                         "Unexpected exception in ReadGraph.syncRequest(AsyncRead)",
2000                                                         exception);
2001                         }
2002                 }
2003                 
2004                 public boolean done() {
2005                         return exception != null;
2006                 }
2007                 
2008         }
2009         
2010         @Override
2011         public <T> T syncRequest(final AsyncRead<T> request)
2012                         throws DatabaseException {
2013
2014                 assert (request != null);
2015                 return syncRequest(request, new AsyncProcedureAdapter<>() );
2016
2017         }
2018
2019         @Override
2020         public <T> T syncRequest(AsyncRead<T> request, AsyncListener<T> procedure)
2021                         throws DatabaseException {
2022                 return syncRequest(request, (AsyncProcedure<T>) procedure);
2023         }
2024
2025         @Override
2026         public <T> T syncRequest(AsyncRead<T> request, SyncListener<T> procedure)
2027                         throws DatabaseException {
2028                 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
2029         }
2030
2031         @Override
2032         public <T> T syncRequest(AsyncRead<T> request, Listener<T> procedure)
2033                         throws DatabaseException {
2034                 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
2035         }
2036
2037         @Override
2038         final public <T> T syncRequest(final AsyncRead<T> request,
2039                         final AsyncProcedure<T> procedure) throws DatabaseException {
2040
2041                 assert (request != null);
2042
2043                 ListenerBase listener = getListenerBase(procedure);
2044
2045 //              BlockingAsyncProcedure<T> ap = new BlockingAsyncProcedure<>(this, procedure, request);
2046                 return (T)QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure, true);
2047 //              return ap.get();
2048
2049         }
2050
2051         @Override
2052         public <T> T syncRequest(AsyncRead<T> request,
2053                         final SyncProcedure<T> procedure) throws DatabaseException {
2054                 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2055         }
2056
2057         @Override
2058         final public <T> T syncRequest(final AsyncRead<T> request,
2059                         final Procedure<T> procedure) throws DatabaseException {
2060                 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2061         }
2062
2063         @Override
2064         public <T> Collection<T> syncRequest(final MultiRead<T> request)
2065                         throws DatabaseException {
2066
2067                 assert (request != null);
2068
2069                 final ArrayList<T> result = new ArrayList<T>();
2070                 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2071
2072                 syncRequest(request, new SyncMultiProcedure<T>() {
2073
2074                         @Override
2075                         public void execute(ReadGraph graph, T t) {
2076                                 synchronized (result) {
2077                                         result.add(t);
2078                                 }
2079                         }
2080
2081                         @Override
2082                         public void finished(ReadGraph graph) {
2083                         }
2084
2085                         @Override
2086                         public void exception(ReadGraph graph, Throwable t) {
2087                                 exception.set(t);
2088                         }
2089
2090                         @Override
2091                         public String toString() {
2092                                 return "syncRequest(MultiRead) -> " + request;
2093                         }
2094
2095                 });
2096
2097                 Throwable t = exception.get();
2098                 if (t != null) {
2099                         if (t instanceof DatabaseException)
2100                                 throw (DatabaseException) t;
2101                         else
2102                                 throw new DatabaseException(
2103                                                 "Unexpected exception in ReadGraph.syncRequest(Read)",
2104                                                 t);
2105                 }
2106
2107                 return result;
2108
2109         }
2110
2111         @Override
2112         public <T> Collection<T> syncRequest(MultiRead<T> request,
2113                         SyncMultiListener<T> procedure) {
2114                 return syncRequest(request, (SyncMultiProcedure<T>)procedure);
2115         }
2116
2117         @Override
2118         public <T> Collection<T> syncRequest(MultiRead<T> request,
2119                         MultiListener<T> procedure) {
2120                 return syncRequest(request, new NoneToSyncMultiListener<T>(procedure));
2121         }
2122
2123         @Override
2124         public <T> Collection<T> syncRequest(MultiRead<T> request,
2125                         SyncMultiProcedure<T> procedure) {
2126
2127                 assert (request != null);
2128
2129                 ListenerBase listener = getListenerBase(procedure);
2130
2131                 final ResultCallWrappedSyncQueryProcedure<T> wrapper = new ResultCallWrappedSyncQueryProcedure<T>(procedure);
2132
2133                 if (parent != null || listener != null) {
2134
2135 //                      Object syncParent = request;
2136
2137 //                      final ReadGraphImpl newGraph = newSync();
2138
2139                         processor.query(this, request, parent, wrapper, listener);
2140
2141 //                      newGraph.waitAsync(syncParent);
2142
2143                 } else {
2144
2145 //                      Object syncParent = request;
2146
2147 //                      final ReadGraphImpl newGraph = newSync();
2148
2149                         try {
2150                                 request.perform(this, wrapper);
2151                         } catch (Throwable t) {
2152                                 wrapper.exception(this, t);
2153                         }
2154
2155                 }
2156
2157                 return wrapper.get();
2158
2159         }
2160
2161         @Override
2162         public <T> Collection<T> syncRequest(MultiRead<T> request,
2163                         MultiProcedure<T> procedure) {
2164                 return syncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
2165         }
2166
2167         static class AsyncMultiReadProcedure<T> extends ArrayList<T> implements AsyncMultiProcedure<T> {
2168
2169                 private static Throwable DONE = new Throwable();
2170                 
2171                 private static final long serialVersionUID = -6494230465108115812L;
2172                 
2173                 Throwable exception = null;
2174                 
2175                 @Override
2176                 public synchronized void execute(AsyncReadGraph graph, T t) {
2177                         add(t);
2178                 }
2179
2180                 @Override
2181                 public void finished(AsyncReadGraph graph) {
2182                         exception = DONE;
2183                 }
2184
2185                 @Override
2186                 public void exception(AsyncReadGraph graph, Throwable t) {
2187                         exception = t;
2188                 }
2189                 
2190                 public void checkAndThrow() throws DatabaseException {
2191                         if(exception != DONE) {
2192                                 if (exception instanceof DatabaseException)
2193                                         throw (DatabaseException) exception;
2194                                 else
2195                                         throw new DatabaseException(
2196                                                         "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
2197                                                         exception);
2198                         }
2199                 }
2200                 
2201                 public boolean done() {
2202                         return exception != null;
2203                 }
2204                 
2205         }
2206
2207         @Override
2208         final public <T> Collection<T> syncRequest(AsyncMultiRead<T> request)
2209                         throws DatabaseException {
2210
2211                 assert (request != null);
2212
2213                 final AsyncMultiReadProcedure<T> procedure = new AsyncMultiReadProcedure<T>();
2214                 
2215                 syncRequest(request, procedure);
2216                 
2217                 procedure.checkAndThrow();
2218                 return procedure;
2219
2220         }
2221
2222         @Override
2223         public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2224                         AsyncMultiListener<T> procedure) {
2225                 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2226         }
2227
2228         @Override
2229         public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2230                         SyncMultiListener<T> procedure) {
2231                 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2232         }
2233
2234         @Override
2235         public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2236                         MultiListener<T> procedure) {
2237                 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2238         }
2239
2240         final private <T> void syncRequest(final AsyncMultiRead<T> request,
2241                         final AsyncMultiReadProcedure<T> procedure) {
2242
2243                 assert (request != null);
2244                 assert (procedure != null);
2245
2246                 ListenerBase listener = getListenerBase(procedure);
2247
2248                 if (parent != null || listener != null) {
2249
2250 //                      Object syncParent = request;
2251
2252 //                      final ReadGraphImpl newGraph = newSync();
2253
2254                         processor.query(this, request, parent, procedure, listener);
2255
2256 //                      newGraph.waitAsync(syncParent);
2257                         waitAsyncProcedure(procedure);
2258
2259                 } else {
2260
2261 //                      Object syncParent = callerThread == Integer.MIN_VALUE ? null
2262 //                                      : request;
2263 //
2264 //                      final ReadGraphImpl newGraph = newSyncAsync(syncParent);
2265
2266                         try {
2267
2268 //                              inc();
2269 //                              ReadGraphImpl sync = newSync();
2270                                 request.perform(this, procedure);
2271 //                              sync.waitAsync(null);
2272                                 waitAsyncProcedure(procedure);
2273 //                              dec();
2274
2275                         } catch (Throwable t) {
2276
2277                                 waitAsyncProcedure(procedure);
2278 //                              dec();
2279
2280                         }
2281
2282                 }
2283
2284         }
2285         
2286         
2287         @Override
2288         final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2289                         final AsyncMultiProcedure<T> procedure) {
2290
2291                 assert (request != null);
2292                 assert (procedure != null);
2293
2294                 ListenerBase listener = getListenerBase(procedure);
2295
2296                 if (parent != null || listener != null) {
2297
2298 //                      Object syncParent = request;
2299
2300 //                      final ReadGraphImpl newGraph = newSync();
2301
2302                         processor.query(this, request, parent, procedure, listener);
2303
2304 //                      newGraph.waitAsync(syncParent);
2305
2306                 } else {
2307
2308 //                      Object syncParent = request;
2309
2310 //                      final ReadGraphImpl newGraph = newSync();
2311
2312                         try {
2313
2314                                 request.perform(this, new AsyncMultiProcedure<T>() {
2315
2316                                         @Override
2317                                         public void execute(AsyncReadGraph graph, T result) {
2318                                                 procedure.execute(graph, result);
2319                                         }
2320
2321                                         @Override
2322                                         public void finished(AsyncReadGraph graph) {
2323                                                 procedure.finished(graph);
2324                                         }
2325
2326                                         @Override
2327                                         public void exception(AsyncReadGraph graph, Throwable t) {
2328                                                 procedure.exception(graph, t);
2329                                         }
2330
2331                                         @Override
2332                                         public String toString() {
2333                                                 return "syncRequest(AsyncMultiRead) -> " + procedure;
2334                                         }
2335
2336                                 });
2337
2338                         } catch (Throwable t) {
2339
2340                         }
2341
2342                 }
2343
2344                 // TODO!!
2345                 return null;
2346
2347         }
2348
2349         @Override
2350         public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2351                         final SyncMultiProcedure<T> procedure) {
2352                 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2353         }
2354
2355         @Override
2356         final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2357                         final MultiProcedure<T> procedure) {
2358                 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2359         }
2360
2361         @Override
2362         public <T> T syncRequest(final ExternalRead<T> request)
2363                         throws DatabaseException {
2364
2365                 assert (request != null);
2366
2367                 return syncRequest(request, new Procedure<T>() {
2368
2369                         @Override
2370                         public void execute(T t) {
2371                         }
2372
2373                         @Override
2374                         public void exception(Throwable t) {
2375                         }
2376
2377                         @Override
2378                         public String toString() {
2379                                 return "syncRequest(AsyncRead) -> " + request;
2380                         }
2381
2382                 });
2383
2384         }
2385
2386         @Override
2387         public <T> T syncRequest(ExternalRead<T> request, Listener<T> procedure) throws DatabaseException {
2388                 return syncRequest(request, (Procedure<T>) procedure);
2389         }
2390
2391         @Override
2392         final public <T> T syncRequest(final ExternalRead<T> request,
2393                         final Procedure<T> procedure) throws DatabaseException {
2394
2395         assert (request != null);
2396
2397         ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
2398         return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
2399
2400         }
2401
2402         @Override
2403         public void syncRequest(final Write request) throws DatabaseException {
2404
2405                 assert (request != null);
2406
2407                 throw new DatabaseException(
2408                                 "Write operations are not supported during read transactions!");
2409
2410         }
2411
2412         @Override
2413         public <T> T syncRequest(final WriteResult<T> request) throws DatabaseException {
2414
2415                 assert (request != null);
2416
2417                 throw new DatabaseException(
2418                                 "Write operations are not supported during read transactions!");
2419
2420         }
2421
2422         @Override
2423         public void syncRequest(final DelayedWrite request)
2424                         throws DatabaseException {
2425
2426                 assert (request != null);
2427
2428                 throw new DatabaseException(
2429                                 "Write operations are not supported during read transactions!");
2430
2431         }
2432
2433         @Override
2434         public <T> T syncRequest(final DelayedWriteResult<T> request) throws DatabaseException {
2435
2436                 assert (request != null);
2437
2438                 throw new DatabaseException(
2439                                 "Write operations are not supported during read transactions!");
2440
2441         }
2442         
2443         @Override
2444         public void syncRequest(final WriteOnly request) throws DatabaseException {
2445
2446                 assert (request != null);
2447
2448                 throw new DatabaseException(
2449                                 "Write operations are not supported during read transactions!");
2450
2451         }
2452
2453         @Override
2454         public <T> T syncRequest(final WriteOnlyResult<T> request) throws DatabaseException {
2455
2456                 assert (request != null);
2457
2458                 throw new DatabaseException(
2459                                 "Write operations are not supported during read transactions!");
2460
2461         }
2462         
2463         @Override
2464         public <T> void async(ReadInterface<T> r, AsyncProcedure<T> procedure) {
2465                 r.request(this, procedure);
2466         }
2467         
2468         @Override
2469         public <T> void async(ReadInterface<T> r, Procedure<T> procedure) {
2470                 r.request(this, procedure);
2471         }
2472         
2473         @Override
2474         public <T> void async(ReadInterface<T> r, SyncProcedure<T> procedure) {
2475                 r.request(this, procedure);
2476         }
2477         
2478         @Override
2479         public <T> void async(ReadInterface<T> r, AsyncListener<T> procedure) {
2480                 r.request(this, procedure);
2481         }
2482         
2483         @Override
2484         public <T> void async(ReadInterface<T> r, Listener<T> procedure) {
2485                 r.request(this, procedure);
2486         }
2487         
2488         @Override
2489         public <T> void async(ReadInterface<T> r, SyncListener<T> procedure) {
2490                 r.request(this, procedure);
2491         }
2492
2493         @Override
2494         public <T> T sync(ReadInterface<T> r) throws DatabaseException {
2495                 return r.request(this);
2496         }
2497         
2498         @Override
2499         public <T> T sync(WriteInterface<T> r) throws DatabaseException {
2500                 return r.request(this);
2501         }
2502         
2503         @Override
2504         public <T> void async(WriteInterface<T> r, Procedure<T> procedure) {
2505                 r.request(this, procedure);
2506         }
2507
2508         @Override
2509         public <T> void async(WriteInterface<T> r) {
2510                 r.request(this, new ProcedureAdapter<T>());
2511         }
2512
2513         /*
2514          * Implementation of the interface AsyncReadGraph
2515          */
2516
2517         @Override
2518         public void forURI(Resource resource, AsyncListener<String> listener) {
2519                 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2520                                 listener);
2521         }
2522
2523         @Override
2524         public void forURI(Resource resource, SyncListener<String> listener) {
2525                 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2526                                 listener);
2527         }
2528
2529         @Override
2530         public void forURI(Resource resource, Listener<String> listener) {
2531                 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2532                                 listener);
2533         }
2534
2535         @Override
2536         final public void forURI(final Resource resource,
2537                         final AsyncProcedure<String> procedure) {
2538
2539                 assert (resource != null);
2540                 assert (procedure != null);
2541
2542                 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2543                                 procedure);
2544
2545         }
2546
2547         @Override
2548         public void forURI(Resource resource, SyncProcedure<String> procedure) {
2549                 forURI(resource, new SyncToAsyncProcedure<String>(procedure));
2550         }
2551
2552         @Override
2553         public void forURI(Resource resource, Procedure<String> procedure) {
2554                 forURI(resource, new NoneToAsyncProcedure<String>(procedure));
2555         }
2556         
2557         @Override
2558         public void forResource(String id, AsyncListener<Resource> listener) {
2559                 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2560                                 listener);
2561         }
2562
2563         @Override
2564         public void forResource(String id, SyncListener<Resource> listener) {
2565                 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2566                                 listener);
2567         }
2568
2569         @Override
2570         public void forResource(String id, Listener<Resource> listener) {
2571                 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2572                                 listener);
2573         }
2574
2575         @Override
2576         final public void forResource(final String id,
2577                         final AsyncProcedure<Resource> procedure) {
2578
2579                 assert (id != null);
2580                 assert (procedure != null);
2581
2582                 processor.forResource(this, id, procedure);
2583
2584         }
2585
2586         @Override
2587         public void forResource(String id, SyncProcedure<Resource> procedure) {
2588                 forResource(id, new SyncToAsyncProcedure<Resource>(procedure));
2589         }
2590
2591         @Override
2592         public void forResource(String id, Procedure<Resource> procedure) {
2593                 forResource(id, new NoneToAsyncProcedure<Resource>(procedure));
2594         }
2595
2596         @Override
2597         public void forBuiltin(String id, AsyncListener<Resource> listener) {
2598                 asyncRequest(new Builtin(id), listener);
2599         }
2600
2601         @Override
2602         public void forBuiltin(String id, SyncListener<Resource> listener) {
2603                 asyncRequest(new Builtin(id), listener);
2604         }
2605
2606         @Override
2607         public void forBuiltin(String id, Listener<Resource> listener) {
2608                 asyncRequest(new Builtin(id), listener);
2609         }
2610
2611         @Override
2612         final public void forBuiltin(final String id,
2613                         final AsyncProcedure<Resource> procedure) {
2614
2615                 assert (id != null);
2616                 assert (procedure != null);
2617
2618                 processor.forBuiltin(this, id, procedure);
2619
2620         }
2621
2622         @Override
2623         public void forBuiltin(String id, SyncProcedure<Resource> procedure) {
2624                 forBuiltin(id, new SyncToAsyncProcedure<Resource>(procedure));
2625         }
2626
2627         @Override
2628         public void forBuiltin(String id, Procedure<Resource> procedure) {
2629                 forBuiltin(id, new NoneToAsyncProcedure<Resource>(procedure));
2630         }
2631
2632         @Override
2633         final public void forEachStatement(Resource subject, Resource relation,
2634                         AsyncMultiProcedure<Statement> procedure) {
2635
2636                 assert (subject != null);
2637                 assert (relation != null);
2638                 assert (procedure != null);
2639
2640                 processor.forEachStatement(this, subject, relation, procedure);
2641
2642         }
2643
2644         @Override
2645         public void forEachStatement(Resource subject, Resource relation,
2646                         SyncMultiProcedure<Statement> procedure) {
2647                 forEachStatement(subject, relation,
2648                                 new SyncToAsyncMultiProcedure<Statement>(procedure));
2649         }
2650
2651         @Override
2652         final public void forEachStatement(Resource subject, Resource relation,
2653                         MultiProcedure<Statement> procedure) {
2654
2655                 assert (subject != null);
2656                 assert (relation != null);
2657                 assert (procedure != null);
2658
2659                 processor.forEachStatement(this, subject, relation, procedure);
2660
2661         }
2662
2663         @Override
2664         final public void forStatementSet(Resource subject, Resource relation,
2665                         AsyncSetListener<Statement> procedure) {
2666
2667                 assert (subject != null);
2668                 assert (relation != null);
2669                 assert (procedure != null);
2670
2671                 processor.forStatementSet(this, subject, relation, procedure);
2672
2673         }
2674
2675         @Override
2676         final public void forStatementSet(Resource subject, Resource relation,
2677                         SyncSetListener<Statement> procedure) {
2678                 forStatementSet(subject, relation,
2679                                 new SyncToAsyncSetProcedure<Statement>(procedure));
2680         }
2681
2682         @Override
2683         public void forStatementSet(Resource subject, Resource relation,
2684                         SetListener<Statement> listener) {
2685                 forStatementSet(subject, relation,
2686                                 new NoneToAsyncSetProcedure<Statement>(listener));
2687         }
2688
2689         @Override
2690         final public void forEachAssertedStatement(final Resource subject,
2691                         final Resource relation,
2692                         final AsyncMultiProcedure<Statement> procedure) {
2693
2694                 assert (subject != null);
2695                 assert (relation != null);
2696                 assert (procedure != null);
2697
2698                 processor.forEachAssertedStatement(this, subject, relation, procedure);
2699
2700         }
2701
2702         @Override
2703         public void forEachAssertedStatement(Resource subject, Resource relation,
2704                         SyncMultiProcedure<Statement> procedure) {
2705                 forEachAssertedStatement(subject, relation,
2706                                 new SyncToAsyncMultiProcedure<Statement>(procedure));
2707         }
2708
2709         @Override
2710         public void forEachAssertedStatement(Resource subject, Resource relation,
2711                         MultiProcedure<Statement> procedure) {
2712                 forEachAssertedStatement(subject, relation,
2713                                 new NoneToAsyncMultiProcedure<Statement>(procedure));
2714         }
2715
2716         @Override
2717         public void forAssertedStatementSet(Resource subject, Resource relation,
2718                         AsyncSetListener<Statement> procedure) {
2719
2720                 assert (subject != null);
2721                 assert (relation != null);
2722                 assert (procedure != null);
2723
2724                 processor.forAssertedStatementSet(this, subject, relation, procedure);
2725
2726         }
2727
2728         @Override
2729         public void forAssertedStatementSet(Resource subject, Resource relation,
2730                         SyncSetListener<Statement> procedure) {
2731
2732                 assert (subject != null);
2733                 assert (relation != null);
2734                 assert (procedure != null);
2735
2736                 forAssertedStatementSet(subject, relation,
2737                                 new SyncToAsyncSetProcedure<Statement>(procedure));
2738
2739         }
2740
2741         @Override
2742         public void forAssertedStatementSet(Resource subject, Resource relation,
2743                         SetListener<Statement> procedure) {
2744
2745                 assert (subject != null);
2746                 assert (relation != null);
2747                 assert (procedure != null);
2748
2749                 forAssertedStatementSet(subject, relation,
2750                                 new NoneToAsyncSetProcedure<Statement>(procedure));
2751
2752         }
2753
2754         @Override
2755         final public void forEachPredicate(final Resource subject,
2756                         final AsyncMultiProcedure<Resource> procedure) {
2757
2758                 assert (subject != null);
2759                 assert (procedure != null);
2760
2761                 processor.forEachPredicate(this, subject, procedure);
2762
2763         }
2764
2765         @Override
2766         public void forEachPredicate(Resource subject,
2767                         SyncMultiProcedure<Resource> procedure) {
2768                 forEachPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(
2769                                 procedure));
2770         }
2771
2772         @Override
2773         final public void forEachPredicate(final Resource subject,
2774                         final MultiProcedure<Resource> procedure) {
2775
2776                 assert (subject != null);
2777                 assert (procedure != null);
2778
2779                 processor.forEachPredicate(this, subject, procedure);
2780
2781         }
2782
2783         @Override
2784         final public void forPredicateSet(final Resource subject,
2785                         final AsyncSetListener<Resource> procedure) {
2786
2787                 assert (subject != null);
2788                 assert (procedure != null);
2789
2790                 processor.forPredicateSet(this, subject, procedure);
2791
2792         }
2793
2794         @Override
2795         final public void forPredicateSet(final Resource subject,
2796                         final SyncSetListener<Resource> procedure) {
2797
2798                 assert (subject != null);
2799                 assert (procedure != null);
2800
2801                 forPredicateSet(subject, new SyncToAsyncSetProcedure<Resource>(
2802                                 procedure));
2803
2804         }
2805
2806         @Override
2807         final public void forPredicateSet(final Resource subject,
2808                         final SetListener<Resource> procedure) {
2809
2810                 assert (subject != null);
2811                 assert (procedure != null);
2812
2813                 forPredicateSet(subject, new NoneToAsyncSetProcedure<Resource>(
2814                                 procedure));
2815
2816         }
2817
2818         @Override
2819         final public void forEachPrincipalType(final Resource subject,
2820                         final AsyncMultiProcedure<Resource> procedure) {
2821
2822                 assert (subject != null);
2823                 assert (procedure != null);
2824
2825                 processor.forEachPrincipalType(this, subject, procedure);
2826
2827         }
2828
2829         @Override
2830         public void forEachPrincipalType(Resource subject,
2831                         SyncMultiProcedure<Resource> procedure) {
2832                 forEachPrincipalType(subject, new SyncToAsyncMultiProcedure<Resource>(
2833                                 procedure));
2834         }
2835
2836         @Override
2837         final public void forEachPrincipalType(final Resource subject,
2838                         final MultiProcedure<Resource> procedure) {
2839
2840                 assert (subject != null);
2841                 assert (procedure != null);
2842
2843                 processor.forEachPrincipalType(this, subject, procedure);
2844
2845         }
2846
2847         @Override
2848         final public void forPrincipalTypeSet(final Resource subject,
2849                         final AsyncSetListener<Resource> procedure) {
2850
2851                 assert (subject != null);
2852                 assert (procedure != null);
2853
2854                 processor.forPrincipalTypeSet(this, subject, procedure);
2855
2856         }
2857
2858         @Override
2859         final public void forPrincipalTypeSet(final Resource subject,
2860                         final SyncSetListener<Resource> procedure) {
2861
2862                 assert (subject != null);
2863                 assert (procedure != null);
2864
2865                 forPrincipalTypeSet(subject, new SyncToAsyncSetProcedure<Resource>(
2866                                 procedure));
2867
2868         }
2869
2870         @Override
2871         final public void forPrincipalTypeSet(final Resource subject,
2872                         final SetListener<Resource> procedure) {
2873
2874                 assert (subject != null);
2875                 assert (procedure != null);
2876
2877                 forPrincipalTypeSet(subject, new NoneToAsyncSetProcedure<Resource>(
2878                                 procedure));
2879
2880         }
2881
2882         @Override
2883         public void forTypes(Resource subject, AsyncListener<Set<Resource>> listener) {
2884                 asyncRequest(new Types(subject), listener);
2885         }
2886
2887         @Override
2888         public void forTypes(Resource subject, SyncListener<Set<Resource>> listener) {
2889                 asyncRequest(new Types(subject), listener);
2890         }
2891
2892         @Override
2893         public void forTypes(Resource subject, Listener<Set<Resource>> listener) {
2894                 asyncRequest(new Types(subject), listener);
2895         }
2896
2897         @Override
2898         final public void forTypes(final Resource subject,
2899                         final AsyncProcedure<Set<Resource>> procedure) {
2900
2901                 assert (subject != null);
2902                 assert (procedure != null);
2903
2904                 processor.forTypes(this, subject, procedure);
2905
2906         }
2907
2908         @Override
2909         public void forTypes(Resource subject,
2910                         SyncProcedure<Set<Resource>> procedure) {
2911                 forTypes(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
2912         }
2913
2914         @Override
2915         public void forTypes(Resource subject, Procedure<Set<Resource>> procedure) {
2916                 forTypes(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
2917         }
2918
2919         @Override
2920         public void forSupertypes(Resource subject,
2921                         AsyncListener<Set<Resource>> listener) {
2922                 asyncRequest(new Types(subject), listener);
2923         }
2924
2925         @Override
2926         public void forSupertypes(Resource subject,
2927                         SyncListener<Set<Resource>> listener) {
2928                 asyncRequest(new Types(subject), listener);
2929         }
2930
2931         @Override
2932         public void forSupertypes(Resource subject, Listener<Set<Resource>> listener) {
2933                 asyncRequest(new Types(subject), listener);
2934         }
2935
2936         @Override
2937         final public void forSupertypes(final Resource subject,
2938                         final AsyncProcedure<Set<Resource>> procedure) {
2939
2940                 assert (subject != null);
2941                 assert (procedure != null);
2942
2943                 processor.forSupertypes(this, subject, procedure);
2944
2945         }
2946
2947         @Override
2948         public void forSupertypes(Resource subject,
2949                         SyncProcedure<Set<Resource>> procedure) {
2950                 forSupertypes(subject, new SyncToAsyncProcedure<Set<Resource>>(
2951                                 procedure));
2952         }
2953
2954         @Override
2955         public void forSupertypes(Resource subject,
2956                         Procedure<Set<Resource>> procedure) {
2957                 forSupertypes(subject, new NoneToAsyncProcedure<Set<Resource>>(
2958                                 procedure));
2959         }
2960
2961         @Override
2962         public void forDirectSuperrelations(Resource subject,
2963                         AsyncMultiProcedure<Resource> procedure) {
2964                 
2965                 assert (subject != null);
2966                 assert (procedure != null);
2967
2968                 processor.forDirectSuperrelations(this, subject, procedure);
2969                 
2970         }
2971
2972         @Override
2973         public void forPossibleSuperrelation(Resource subject, AsyncProcedure<Resource> procedure) {
2974                 
2975                 assert (subject != null);
2976                 assert (procedure != null);
2977
2978                 processor.forPossibleSuperrelation(this, subject, procedure);
2979                 
2980         }
2981
2982         @Override
2983         public void forSuperrelations(Resource subject,
2984                         AsyncListener<Set<Resource>> listener) {
2985                 asyncRequest(new Types(subject), listener);
2986         }
2987
2988         @Override
2989         public void forSuperrelations(Resource subject,
2990                         SyncListener<Set<Resource>> listener) {
2991                 asyncRequest(new Types(subject), listener);
2992         }
2993
2994         @Override
2995         public void forSuperrelations(Resource subject,
2996                         Listener<Set<Resource>> listener) {
2997                 asyncRequest(new Types(subject), listener);
2998         }
2999
3000         @Override
3001         final public void forSuperrelations(final Resource subject,
3002                         final AsyncProcedure<Set<Resource>> procedure) {
3003
3004                 assert (subject != null);
3005                 assert (procedure != null);
3006
3007                 processor.forSuperrelations(this, subject, procedure);
3008
3009         }
3010
3011         @Override
3012         public void forSuperrelations(Resource subject,
3013                         SyncProcedure<Set<Resource>> procedure) {
3014                 forSuperrelations(subject, new SyncToAsyncProcedure<Set<Resource>>(
3015                                 procedure));
3016         }
3017
3018         @Override
3019         public void forSuperrelations(Resource subject,
3020                         Procedure<Set<Resource>> procedure) {
3021                 forSuperrelations(subject, new NoneToAsyncProcedure<Set<Resource>>(
3022                                 procedure));
3023         }
3024
3025         @Override
3026         final public void forEachObject(final Resource subject, final Resource relation, final AsyncMultiProcedure<Resource> procedure) {
3027                 processor.forEachObject(this, subject, relation, procedure);
3028         }
3029
3030         @Override
3031         public void forEachObject(Resource subject, Resource relation,
3032                         SyncMultiProcedure<Resource> procedure) {
3033                 forEachObject(subject, relation,
3034                                 new SyncToAsyncMultiProcedure<Resource>(procedure));
3035         }
3036
3037         @Override
3038         public void forEachObject(Resource subject, Resource relation,
3039                         MultiProcedure<Resource> procedure) {
3040
3041                 processor.forEachObject(this, subject, relation, procedure);
3042
3043         }
3044
3045         @Override
3046         final public void forEachDirectPredicate(final Resource subject, final AsyncProcedure<Set<Resource>> procedure) {
3047                 processor.forEachDirectPredicate(this, subject, procedure);
3048         }
3049
3050         @Override
3051         final public void forEachDirectPredicate(final Resource subject, final SyncProcedure<Set<Resource>> procedure) {
3052                 forEachDirectPredicate(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
3053         }
3054
3055         @Override
3056         public void forEachDirectPredicate(Resource subject, Procedure<Set<Resource>> procedure) {
3057                 forEachDirectPredicate(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
3058         }
3059
3060         @Override
3061         final public void forObjectSet(final Resource subject,
3062                         final Resource relation, final AsyncSetListener<Resource> procedure) {
3063
3064                 assert (subject != null);
3065                 assert (relation != null);
3066                 assert (procedure != null);
3067
3068                 processor.forObjectSet(this, subject, relation, procedure);
3069
3070         }
3071
3072         @Override
3073         final public void forObjectSet(final Resource subject,
3074                         final Resource relation, final SyncSetListener<Resource> procedure) {
3075
3076                 assert (subject != null);
3077                 assert (relation != null);
3078                 assert (procedure != null);
3079
3080                 forObjectSet(subject, relation, new SyncToAsyncSetProcedure<Resource>(
3081                                 procedure));
3082
3083         }
3084
3085         @Override
3086         final public void forObjectSet(final Resource subject,
3087                         final Resource relation, final SetListener<Resource> procedure) {
3088
3089                 assert (subject != null);
3090                 assert (relation != null);
3091                 assert (procedure != null);
3092
3093                 forObjectSet(subject, relation, new NoneToAsyncSetProcedure<Resource>(
3094                                 procedure));
3095
3096         }
3097
3098         @Override
3099         final public void forEachAssertedObject(final Resource subject,
3100                         final Resource relation,
3101                         final AsyncMultiProcedure<Resource> procedure) {
3102
3103                 assert (subject != null);
3104                 assert (relation != null);
3105                 assert (procedure != null);
3106
3107                 processor.forEachAssertedObject(this, subject, relation, procedure);
3108
3109         }
3110
3111         @Override
3112         public void forEachAssertedObject(Resource subject, Resource relation,
3113                         SyncMultiProcedure<Resource> procedure) {
3114
3115                 assert (subject != null);
3116                 assert (relation != null);
3117                 assert (procedure != null);
3118
3119                 forEachAssertedObject(subject, relation,
3120                                 new SyncToAsyncMultiProcedure<Resource>(procedure));
3121
3122         }
3123
3124         @Override
3125         public void forEachAssertedObject(Resource subject, Resource relation,
3126                         MultiProcedure<Resource> procedure) {
3127
3128                 assert (subject != null);
3129                 assert (relation != null);
3130                 assert (procedure != null);
3131
3132                 forEachAssertedObject(subject, relation,
3133                                 new NoneToAsyncMultiProcedure<Resource>(procedure));
3134
3135         }
3136
3137         @Override
3138         public void forAssertedObjectSet(Resource subject, Resource relation,
3139                         AsyncSetListener<Resource> procedure) {
3140
3141                 assert (subject != null);
3142                 assert (relation != null);
3143                 assert (procedure != null);
3144
3145                 processor.forAssertedObjectSet(this, subject, relation, procedure);
3146
3147         }
3148
3149         @Override
3150         public void forAssertedObjectSet(Resource subject, Resource relation,
3151                         SyncSetListener<Resource> procedure) {
3152
3153                 assert (subject != null);
3154                 assert (relation != null);
3155                 assert (procedure != null);
3156
3157                 forAssertedObjectSet(subject, relation,
3158                                 new SyncToAsyncSetProcedure<Resource>(procedure));
3159
3160         }
3161
3162         @Override
3163         public void forAssertedObjectSet(Resource subject, Resource relation,
3164                         SetListener<Resource> procedure) {
3165
3166                 assert (subject != null);
3167                 assert (relation != null);
3168                 assert (procedure != null);
3169
3170                 forAssertedObjectSet(subject, relation,
3171                                 new NoneToAsyncSetProcedure<Resource>(procedure));
3172
3173         }
3174
3175         @Override
3176         public void forInverse(Resource relation, AsyncListener<Resource> listener) {
3177                 asyncRequest(new Inverse(relation), listener);
3178         }
3179
3180         @Override
3181         public void forInverse(Resource relation, SyncListener<Resource> listener) {
3182                 asyncRequest(new Inverse(relation), listener);
3183         }
3184
3185         @Override
3186         public void forInverse(Resource relation, Listener<Resource> listener) {
3187                 asyncRequest(new Inverse(relation), listener);
3188         }
3189
3190         @Override
3191         final public void forInverse(final Resource relation,
3192                         final AsyncProcedure<Resource> procedure) {
3193
3194                 assert (relation != null);
3195                 assert (procedure != null);
3196
3197                 processor.forInverse(this, relation, new AsyncProcedure<Resource>() {
3198
3199                         @Override
3200                         public void execute(AsyncReadGraph graph, Resource result) {
3201                                 if (result != null)
3202                                         procedure.execute(graph, result);
3203                                 else {
3204                                         procedure.exception(graph, new NoInverseException(relation
3205                                                         .toString()));
3206                                 }
3207                         }
3208
3209                         @Override
3210                         public void exception(AsyncReadGraph graph, Throwable throwable) {
3211                                 procedure.exception(graph, throwable);
3212                         }
3213
3214                         @Override
3215                         public String toString() {
3216                                 return "forInverse -> " + procedure;
3217                         }
3218
3219                 });
3220
3221         }
3222
3223         @Override
3224         public void forInverse(Resource relation, SyncProcedure<Resource> procedure) {
3225                 forInverse(relation, new SyncToAsyncProcedure<Resource>(procedure));
3226         }
3227
3228         @Override
3229         public void forInverse(Resource relation, Procedure<Resource> procedure) {
3230                 forInverse(relation, new NoneToAsyncProcedure<Resource>(procedure));
3231         }
3232
3233         @Override
3234         public void forSingleObject(Resource subject, Resource relation,
3235                         AsyncListener<Resource> listener) {
3236                 asyncRequest(new SingleObject(subject, relation), listener);
3237         }
3238
3239         @Override
3240         public void forSingleObject(Resource subject, Resource relation,
3241                         SyncListener<Resource> listener) {
3242                 asyncRequest(new SingleObject(subject, relation), listener);
3243         }
3244
3245         @Override
3246         public void forSingleObject(Resource subject, Resource relation,
3247                         Listener<Resource> listener) {
3248                 asyncRequest(new SingleObject(subject, relation), listener);
3249         }
3250
3251         @Override
3252         final public void forSingleObject(final Resource subject,
3253                         final Resource relation, final AsyncProcedure<Resource> procedure) {
3254
3255                 assert (subject != null);
3256                 assert (relation != null);
3257                 assert (procedure != null);
3258
3259                 processor.forEachObject(this, subject, relation,
3260                                 new SingleOrErrorProcedure<Resource>(procedure));
3261
3262         }
3263
3264         @Override
3265         public void forSingleObject(Resource subject, Resource relation,
3266                         SyncProcedure<Resource> procedure) {
3267                 forSingleObject(subject, relation, new SyncToAsyncProcedure<Resource>(
3268                                 procedure));
3269         }
3270
3271         @Override
3272         public void forSingleObject(Resource subject, Resource relation,
3273                         Procedure<Resource> procedure) {
3274                 forSingleObject(subject, relation, new NoneToAsyncProcedure<Resource>(
3275                                 procedure));
3276         }
3277
3278         @Override
3279         public void forSingleStatement(Resource subject, Resource relation,
3280                         AsyncListener<Statement> listener) {
3281                 asyncRequest(new SingleStatement(subject, relation), listener);
3282         }
3283
3284         @Override
3285         public void forSingleStatement(Resource subject, Resource relation,
3286                         SyncListener<Statement> listener) {
3287                 asyncRequest(new SingleStatement(subject, relation), listener);
3288         }
3289
3290         @Override
3291         public void forSingleStatement(Resource subject, Resource relation,
3292                         Listener<Statement> listener) {
3293                 asyncRequest(new SingleStatement(subject, relation), listener);
3294         }
3295
3296         @Override
3297         final public void forSingleStatement(final Resource subject,
3298                         final Resource relation, final AsyncProcedure<Statement> procedure) {
3299
3300                 assert (subject != null);
3301                 assert (relation != null);
3302                 assert (procedure != null);
3303
3304                 processor.forEachStatement(this, subject, relation,
3305                                 new SingleOrErrorProcedure<Statement>(procedure));
3306
3307         }
3308
3309         @Override
3310         public void forSingleStatement(Resource subject, Resource relation,
3311                         SyncProcedure<Statement> procedure) {
3312                 forSingleStatement(subject, relation,
3313                                 new SyncToAsyncProcedure<Statement>(procedure));
3314         }
3315
3316         @Override
3317         public void forSingleStatement(Resource subject, Resource relation,
3318                         Procedure<Statement> procedure) {
3319                 forSingleStatement(subject, relation,
3320                                 new NoneToAsyncProcedure<Statement>(procedure));
3321         }
3322
3323         @Override
3324         public void forSingleType(Resource subject,
3325                         AsyncListener<Resource> listener) {
3326                 asyncRequest(new SingleTypeAny(subject), listener);
3327         }
3328
3329         @Override
3330         public void forSingleType(Resource subject,
3331                         SyncListener<Resource> listener) {
3332                 asyncRequest(new SingleTypeAny(subject), listener);
3333         }
3334
3335         @Override
3336         public void forSingleType(Resource subject,
3337                         Listener<Resource> listener) {
3338                 asyncRequest(new SingleTypeAny(subject), listener);
3339         }
3340
3341         @Override
3342         final public void forSingleType(final Resource subject, final AsyncProcedure<Resource> procedure) {
3343
3344                 assert (subject != null);
3345                 assert (procedure != null);
3346
3347                 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3348
3349                 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
3350
3351                         @Override
3352                         public void execute(AsyncReadGraph graph, final Resource principalType) {
3353                                 checkedProcedure.offer(graph, principalType);
3354                         }
3355
3356                         @Override
3357                         public void finished(AsyncReadGraph graph) {
3358                                 checkedProcedure.dec(graph);
3359                         }
3360
3361                         @Override
3362                         public void exception(AsyncReadGraph graph, Throwable t) {
3363                                 checkedProcedure.exception(graph, t);
3364                         }
3365
3366                         @Override
3367                         public String toString() {
3368                                 return "forSingleType -> " + procedure;
3369                         }
3370
3371                 });
3372
3373         }
3374
3375         @Override
3376         public void forSingleType(Resource subject, SyncProcedure<Resource> procedure) {
3377                 forSingleType(subject, new SyncToAsyncProcedure<Resource>(
3378                                 procedure));
3379         }
3380
3381         @Override
3382         public void forSingleType(Resource subject, Procedure<Resource> procedure) {
3383                 forSingleType(subject, new NoneToAsyncProcedure<Resource>(
3384                                 procedure));
3385         }
3386
3387         @Override
3388         public void forSingleType(Resource subject, Resource relation,
3389                         AsyncListener<Resource> listener) {
3390                 asyncRequest(new SingleType(subject, relation), listener);
3391         }
3392
3393         @Override
3394         public void forSingleType(Resource subject, Resource relation,
3395                         SyncListener<Resource> listener) {
3396                 asyncRequest(new SingleType(subject, relation), listener);
3397         }
3398
3399         @Override
3400         public void forSingleType(Resource subject, Resource relation,
3401                         Listener<Resource> listener) {
3402                 asyncRequest(new SingleType(subject, relation), listener);
3403         }
3404
3405         @Override
3406         final public void forSingleType(final Resource subject,
3407                         final Resource baseType, final AsyncProcedure<Resource> procedure) {
3408
3409                 assert (subject != null);
3410                 assert (procedure != null);
3411
3412                 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3413
3414                 processor.forEachPrincipalType(this, subject,
3415                                 new AsyncMultiProcedureAdapter<Resource>() {
3416
3417                                         @Override
3418                                         public void execute(AsyncReadGraph graph,
3419                                                         final Resource principalType) {
3420
3421                                                 checkedProcedure.inc();
3422
3423                                                 if(baseType == null) {
3424
3425                                                         checkedProcedure.offer(graph, principalType);
3426                                                         checkedProcedure.dec(graph);
3427
3428                                                 } else if(principalType.equals(baseType)) {
3429
3430                                                         checkedProcedure.offer(graph, principalType);
3431                                                         checkedProcedure.dec(graph);
3432
3433                                                 } else {
3434
3435                                                         processor.forSupertypes((ReadGraphImpl)graph, principalType,
3436                                                                         new AsyncProcedure<Set<Resource>>() {
3437
3438                                                                                 @Override
3439                                                                                 public void execute(
3440                                                                                                 AsyncReadGraph graph,
3441                                                                                                 Set<Resource> result) {
3442
3443                                                                                         if (result.contains(baseType))
3444                                                                                                 checkedProcedure.offer(graph,
3445                                                                                                                 principalType);
3446                                                                                         checkedProcedure.dec(graph);
3447
3448                                                                                 }
3449
3450                                                                                 @Override
3451                                                                                 public void exception(
3452                                                                                                 AsyncReadGraph graph,
3453                                                                                                 Throwable t) {
3454                                                                                         checkedProcedure
3455                                                                                                         .exception(graph, t);
3456                                                                                 }
3457
3458                                                                         });
3459
3460                                                 }
3461
3462                                         }
3463
3464                                         @Override
3465                                         public void finished(AsyncReadGraph graph) {
3466                                                 checkedProcedure.dec(graph);
3467                                         }
3468
3469                                         @Override
3470                                         public void exception(AsyncReadGraph graph, Throwable t) {
3471                                                 checkedProcedure.exception(graph, t);
3472                                         }
3473
3474                                         @Override
3475                                         public String toString() {
3476                                                 return "forSingleType -> " + procedure;
3477                                         }
3478
3479                                 });
3480
3481         }
3482
3483         @Override
3484         public void forSingleType(Resource subject, Resource relation,
3485                         SyncProcedure<Resource> procedure) {
3486                 forSingleType(subject, relation, new SyncToAsyncProcedure<Resource>(
3487                                 procedure));
3488         }
3489
3490         @Override
3491         public void forSingleType(Resource subject, Resource relation,
3492                         Procedure<Resource> procedure) {
3493                 forSingleType(subject, relation, new NoneToAsyncProcedure<Resource>(
3494                                 procedure));
3495         }
3496
3497         @Override
3498         public <T> void forValue(Resource subject, Binding binding,
3499                         AsyncListener<T> listener) {
3500                 asyncRequest(new Value<T>(subject, binding), listener);
3501         }
3502
3503         @Override
3504         public <T> void forValue(Resource subject, Binding binding,
3505                         SyncListener<T> listener) {
3506                 asyncRequest(new Value<T>(subject, binding), listener);
3507         }
3508
3509         @Override
3510         public <T> void forValue(Resource subject, Binding binding,
3511                         Listener<T> listener) {
3512                 asyncRequest(new Value<T>(subject, binding), listener);
3513         }
3514
3515         @Override
3516         public <T> void forValue(final Resource resource, final Binding binding,
3517                         final AsyncProcedure<T> procedure) {
3518
3519                 assert (resource != null);
3520                 assert (binding != null);
3521                 assert (procedure != null);
3522                 
3523                 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
3524
3525                         @Override
3526                         public void execute(AsyncReadGraph graph, byte[] result) {
3527                                 
3528                                 try {
3529
3530                                         if (result == null) {
3531                                                 procedure.exception(graph,
3532                                                                 new DoesNotContainValueException(
3533                                                                                 "No value for resource " + resource));
3534                                                 return;
3535                                         }
3536
3537                                         Serializer serializer = binding.serializer();
3538 //                                      Serializer serializer = Bindings.getSerializer( binding );
3539                                         Object obj = serializer.deserialize(result);
3540 //                                      if (!binding.isInstance(obj))
3541 //                                              procedure.exception(graph, new ClassCastException(
3542 //                                                              "Cannot get value " + obj + " with binding "
3543 //                                                                              + binding));
3544 //                                      else
3545                                                 procedure.execute(graph, (T) obj);
3546
3547                                 } catch (IOException e) {
3548                                         procedure.exception(graph, e);
3549                                 } catch (BufferUnderflowException e) {
3550                                         procedure.exception(graph, e);
3551                                 } catch (Throwable t) {
3552                                         procedure.exception(graph, t);
3553                                 }
3554
3555                         }
3556
3557                         @Override
3558                         public void exception(AsyncReadGraph graph, Throwable t) {
3559                                 try {
3560                                         procedure.exception(graph, t);
3561                                 } catch (Throwable t2) {
3562                                 Logger.defaultLogError(t2);
3563                                 }
3564                         }
3565
3566                         @Override
3567                         public String toString() {
3568                                 return "forValue -> " + procedure;
3569                         }
3570
3571                 });
3572
3573         }
3574
3575         @Override
3576         public <T> void forValue(Resource subject, Binding binding,
3577                         SyncProcedure<T> procedure) {
3578                 forValue(subject, binding, new SyncToAsyncProcedure<T>(procedure));
3579         }
3580
3581         @Override
3582         public <T> void forValue(Resource subject, Binding binding,
3583                         Procedure<T> procedure) {
3584                 forValue(subject, binding, new NoneToAsyncProcedure<T>(procedure));
3585         }
3586
3587         @Override
3588         public <T> void forValue(Resource subject, AsyncListener<T> listener) {
3589                 asyncRequest(new ValueImplied<T>(subject), listener);
3590         }
3591
3592         @Override
3593         public <T> void forValue(Resource subject, SyncListener<T> listener) {
3594                 asyncRequest(new ValueImplied<T>(subject), listener);
3595         }
3596
3597         @Override
3598         public <T> void forValue(Resource subject, Listener<T> listener) {
3599                 asyncRequest(new ValueImplied<T>(subject), listener);
3600         }
3601
3602         @Override
3603         final public <T> void forValue(final Resource subject, final AsyncProcedure<T> procedure) {
3604
3605                 assert (subject != null);
3606                 assert (procedure != null);
3607                 
3608                 forRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
3609
3610                         @Override
3611                         public void execute(AsyncReadGraph graph, Datatype type) {
3612                                 // TODO: consider trying Bindings.getBeanBinding(type);
3613                                 Binding binding = Bindings.getBinding(type);
3614                                 graph.forValue(subject, binding, procedure);
3615                         }
3616
3617                         @Override
3618                         public void exception(AsyncReadGraph graph, Throwable throwable) {
3619                                 procedure.exception(graph, new DoesNotContainValueException("Invalid data type", throwable));
3620                         }
3621                         
3622                 });
3623
3624         }
3625
3626         @Override
3627         public <T> void forValue(Resource subject, SyncProcedure<T> procedure) {
3628                 forValue(subject, new SyncToAsyncProcedure<T>(procedure));
3629         }
3630
3631         @Override
3632         public <T> void forValue(Resource subject, Procedure<T> procedure) {
3633                 forValue(subject, new NoneToAsyncProcedure<T>(procedure));
3634         }
3635
3636         @Override
3637         public <T> void forRelatedValue(Resource subject, Resource relation,
3638                         AsyncListener<T> listener) {
3639                 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3640         }
3641
3642         @Override
3643         public <T> void forRelatedValue(Resource subject, Resource relation,
3644                         SyncListener<T> listener) {
3645                 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3646         }
3647
3648         @Override
3649         public <T> void forRelatedValue(Resource subject, Resource relation,
3650                         Listener<T> listener) {
3651                 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3652         }
3653
3654         @Override
3655         final public <T> void forRelatedValue(final Resource subject,
3656                         final Resource relation, final AsyncProcedure<T> procedure) {
3657
3658                 assert (subject != null);
3659                 assert (relation != null);
3660                 assert (procedure != null);
3661
3662                 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3663
3664                 processor.forEachObject(this, subject, relation,
3665                                 new AsyncMultiProcedureAdapter<Resource>() {
3666
3667                                         @Override
3668                                         public void execute(AsyncReadGraph graph,
3669                                                         final Resource object) {
3670
3671                                                 checkedProcedure.inc();
3672
3673                                                 graph.forValue(object, new AsyncProcedure<Object>() {
3674
3675                                                         @Override
3676                                                         public void execute(AsyncReadGraph graph,
3677                                                                         Object result) {
3678                                                                 checkedProcedure.offer(graph, (T) result);
3679                                                                 checkedProcedure.dec(graph);
3680                                                         }
3681
3682                                                         @Override
3683                                                         public void exception(AsyncReadGraph graph,
3684                                                                         Throwable t) {
3685                                                                 checkedProcedure.exception(graph, t);
3686                                                         }
3687
3688                                                         @Override
3689                                                         public String toString() {
3690                                                                 return "forRelatedValue -> " + procedure;
3691                                                         }
3692
3693                                                 });
3694
3695                                         }
3696
3697                                         @Override
3698                                         public void finished(AsyncReadGraph graph) {
3699                                                 checkedProcedure.dec(graph);
3700                                         }
3701
3702                                         @Override
3703                                         public void exception(AsyncReadGraph graph, Throwable t) {
3704                                                 checkedProcedure.exception(graph, t);
3705                                         }
3706
3707                                 });
3708
3709         }
3710
3711         @Override
3712         public <T> void forRelatedValue(Resource subject, Resource relation,
3713                         SyncProcedure<T> procedure) {
3714                 forRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
3715                                 procedure));
3716         }
3717
3718         @Override
3719         public <T> void forRelatedValue(Resource subject, Resource relation,
3720                         Procedure<T> procedure) {
3721                 forRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
3722                                 procedure));
3723         }
3724
3725         @Override
3726         public <T> void forRelatedValue(Resource subject, Resource relation,
3727                         Binding binding, AsyncListener<T> listener) {
3728                 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3729         }
3730
3731         @Override
3732         public <T> void forRelatedValue(Resource subject, Resource relation,
3733                         Binding binding, SyncListener<T> listener) {
3734                 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3735         }
3736
3737         @Override
3738         public <T> void forRelatedValue(Resource subject, Resource relation,
3739                         Binding binding, Listener<T> listener) {
3740                 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3741         }
3742
3743         @Override
3744         final public <T> void forRelatedValue(final Resource subject,
3745                         final Resource relation, final Binding binding,
3746                         final AsyncProcedure<T> procedure) {
3747
3748                 assert (subject != null);
3749                 assert (relation != null);
3750                 assert (binding != null);
3751                 assert (procedure != null);
3752
3753                 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3754                 
3755                 processor.forEachObject(this, subject, relation,
3756                                 new AsyncMultiProcedureAdapter<Resource>() {
3757
3758                                         @Override
3759                                         public void execute(AsyncReadGraph graph,
3760                                                         final Resource object) {
3761
3762                                                 checkedProcedure.inc();
3763
3764                                                 graph.forValue(object, binding, new AsyncProcedure<Object>() {
3765
3766                                                                         @Override
3767                                                                         public void execute(AsyncReadGraph graph,
3768                                                                                         Object result) {
3769                                                                                 
3770                                                                                 checkedProcedure.offer(graph,
3771                                                                                                 (T) result);
3772                                                                                 checkedProcedure.dec(graph);
3773                                                                         }
3774
3775                                                                         @Override
3776                                                                         public void exception(AsyncReadGraph graph,
3777                                                                                         Throwable t) {
3778                                                                                 checkedProcedure.exception(graph, t);
3779                                                                         }
3780
3781                                                                         @Override
3782                                                                         public String toString() {
3783                                                                                 return "forRelatedValue -> "
3784                                                                                                 + procedure;
3785                                                                         }
3786
3787                                                                 });
3788
3789                                         }
3790
3791                                         @Override
3792                                         public void finished(AsyncReadGraph graph) {
3793                                                 checkedProcedure.dec(graph);
3794                                         }
3795
3796                                         @Override
3797                                         public void exception(AsyncReadGraph graph, Throwable t) {
3798                                                 checkedProcedure.exception(graph, t);
3799                                         }
3800
3801                                 });
3802
3803         }
3804
3805         @Override
3806         public <T> void forRelatedValue(Resource subject, Resource relation,
3807                         Binding binding, SyncProcedure<T> procedure) {
3808                 forRelatedValue(subject, relation, binding,
3809                                 new SyncToAsyncProcedure<T>(procedure));
3810         }
3811
3812         @Override
3813         public <T> void forRelatedValue(Resource subject, Resource relation,
3814                         Binding binding, Procedure<T> procedure) {
3815                 forRelatedValue(subject, relation, binding,
3816                                 new NoneToAsyncProcedure<T>(procedure));
3817         }
3818
3819         @Override
3820         public <T> void forAdapted(Resource resource, Class<T> clazz,
3821                         AsyncListener<T> listener) {
3822                 asyncRequest(new Adapter<T>(resource, clazz), listener);
3823         }
3824
3825         @Override
3826         public <T> void forAdapted(Resource resource, Class<T> clazz,
3827                         SyncListener<T> listener) {
3828                 asyncRequest(new Adapter<T>(resource, clazz), listener);
3829         }
3830
3831         @Override
3832         public <T> void forAdapted(Resource resource, Class<T> clazz,
3833                         Listener<T> listener) {
3834                 asyncRequest(new Adapter<T>(resource, clazz), listener);
3835         }
3836
3837         @Override
3838         final public <T> void forAdapted(final Resource resource,
3839                         final Class<T> clazz, final AsyncProcedure<T> procedure) {
3840
3841                 assert (resource != null);
3842                 assert (clazz != null);
3843                 assert (procedure != null);
3844
3845                 final AdaptionService service = getSession().peekService(AdaptionService.class);
3846                 if (service == null)
3847                         procedure.exception(this, new ServiceException("No AdaptionService available")); 
3848                 else
3849                         service.adapt(this, resource, resource, Resource.class, clazz, false, procedure); 
3850
3851         }
3852
3853         @Override
3854         public <T> void forAdapted(Resource resource, Class<T> clazz,
3855                         SyncProcedure<T> procedure) {
3856                 forAdapted(resource, clazz, new SyncToAsyncProcedure<T>(procedure));
3857         }
3858
3859         @Override
3860         public <T> void forAdapted(Resource resource, Class<T> clazz,
3861                         Procedure<T> procedure) {
3862                 forAdapted(resource, clazz, new NoneToAsyncProcedure<T>(procedure));
3863         }
3864
3865         @Override
3866         public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3867                         AsyncListener<T> listener) {
3868                 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3869         }
3870
3871         @Override
3872         public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3873                         SyncListener<T> listener) {
3874                 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3875         }
3876
3877         @Override
3878         public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3879                         Listener<T> listener) {
3880                 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3881         }
3882
3883         @Override
3884         final public <T> void forUniqueAdapted(final Resource resource,
3885                         final Class<T> clazz, final AsyncProcedure<T> procedure) {
3886
3887                 assert (resource != null);
3888                 assert (clazz != null);
3889                 assert (procedure != null);
3890
3891                 final AdaptionService service = getSession().peekService(AdaptionService.class);
3892                 if (service == null)
3893                         procedure.exception(this, new ServiceException("No AdaptionService available")); 
3894                 else
3895                         service.adaptNew(this, resource, clazz, false, procedure);
3896
3897         }
3898
3899         @Override
3900         public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3901                         SyncProcedure<T> procedure) {
3902                 forUniqueAdapted(resource, clazz,
3903                                 new SyncToAsyncProcedure<T>(procedure));
3904         }
3905
3906         @Override
3907         public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3908                         Procedure<T> procedure) {
3909                 forUniqueAdapted(resource, clazz,
3910                                 new NoneToAsyncProcedure<T>(procedure));
3911         }
3912
3913         @Override
3914         public void forPossibleInverse(Resource subject,
3915                         AsyncListener<Resource> listener) {
3916                 asyncRequest(new PossibleInverse(subject), listener);
3917         }
3918
3919         @Override
3920         public void forPossibleInverse(Resource subject,
3921                         SyncListener<Resource> listener) {
3922                 asyncRequest(new PossibleInverse(subject), listener);
3923         }
3924
3925         @Override
3926         public void forPossibleInverse(Resource subject, Listener<Resource> listener) {
3927                 asyncRequest(new PossibleInverse(subject), listener);
3928         }
3929
3930         @Override
3931         final public void forPossibleInverse(final Resource relation,
3932                         final AsyncProcedure<Resource> procedure) {
3933
3934                 assert (relation != null);
3935                 assert (procedure != null);
3936
3937                 processor.forInverse(this, relation, new ExceptionToNullProcedure<Resource>(procedure));
3938
3939         }
3940
3941         @Override
3942         public void forPossibleInverse(Resource subject,
3943                         SyncProcedure<Resource> procedure) {
3944                 forPossibleInverse(subject, new SyncToAsyncProcedure<Resource>(
3945                                 procedure));
3946         }
3947
3948         @Override
3949         public void forPossibleInverse(Resource subject,
3950                         Procedure<Resource> procedure) {
3951                 forPossibleInverse(subject, new NoneToAsyncProcedure<Resource>(
3952                                 procedure));
3953         }
3954
3955         @Override
3956         public void forPossibleObject(Resource subject, Resource relation,
3957                         AsyncListener<Resource> listener) {
3958                 asyncRequest(new PossibleObject(subject, relation), listener);
3959         }
3960
3961         @Override
3962         public void forPossibleObject(Resource subject, Resource relation,
3963                         SyncListener<Resource> listener) {
3964                 asyncRequest(new PossibleObject(subject, relation), listener);
3965         }
3966
3967         @Override
3968         public void forPossibleObject(Resource subject, Resource relation,
3969                         Listener<Resource> listener) {
3970                 asyncRequest(new PossibleObject(subject, relation), listener);
3971         }
3972
3973         @Override
3974         final public void forPossibleObject(final Resource subject,
3975                         final Resource relation, final AsyncProcedure<Resource> procedure) {
3976
3977                 assert (subject != null);
3978                 assert (relation != null);
3979                 assert (procedure != null);
3980
3981                 processor.forEachObject(this, subject, relation,
3982                                 new SingleOrNullProcedure<Resource>(procedure));
3983
3984         }
3985
3986         @Override
3987         public void forPossibleObject(Resource subject, Resource relation,
3988                         SyncProcedure<Resource> procedure) {
3989                 forPossibleObject(subject, relation,
3990                                 new SyncToAsyncProcedure<Resource>(procedure));
3991         }
3992
3993         @Override
3994         public void forPossibleObject(Resource subject, Resource relation,
3995                         Procedure<Resource> procedure) {
3996                 forPossibleObject(subject, relation,
3997                                 new NoneToAsyncProcedure<Resource>(procedure));
3998         }
3999
4000         @Override
4001         public void forPossibleStatement(Resource subject, Resource relation,
4002                         AsyncListener<Statement> listener) {
4003                 asyncRequest(new PossibleStatement(subject, relation), listener);
4004         }
4005
4006         @Override
4007         public void forPossibleStatement(Resource subject, Resource relation,
4008                         SyncListener<Statement> listener) {
4009                 asyncRequest(new PossibleStatement(subject, relation), listener);
4010         }
4011
4012         @Override
4013         public void forPossibleStatement(Resource subject, Resource relation,
4014                         Listener<Statement> listener) {
4015                 asyncRequest(new PossibleStatement(subject, relation), listener);
4016         }
4017
4018         @Override
4019         final public void forPossibleStatement(final Resource subject,
4020                         final Resource relation, final AsyncProcedure<Statement> procedure) {
4021
4022                 assert (subject != null);
4023                 assert (relation != null);
4024                 assert (procedure != null);
4025
4026                 processor.forEachStatement(this, subject, relation,
4027                                 new SingleFunctionalOrNullProcedure<Statement>(
4028                                                 "forPossibleStatement", procedure));
4029
4030         }
4031
4032         @Override
4033         public void forPossibleStatement(Resource subject, Resource relation,
4034                         SyncProcedure<Statement> procedure) {
4035                 forPossibleStatement(subject, relation,
4036                                 new SyncToAsyncProcedure<Statement>(procedure));
4037         }
4038
4039         @Override
4040         public void forPossibleStatement(Resource subject, Resource relation,
4041                         Procedure<Statement> procedure) {
4042                 forPossibleStatement(subject, relation,
4043                                 new NoneToAsyncProcedure<Statement>(procedure));
4044         }
4045
4046         @Override
4047         public void forPossibleType(Resource subject, Resource relation,
4048                         AsyncListener<Resource> listener) {
4049                 asyncRequest(new PossibleType(subject, relation), listener);
4050         }
4051
4052         @Override
4053         public void forPossibleType(Resource subject, Resource relation,
4054                         SyncListener<Resource> listener) {
4055                 asyncRequest(new PossibleType(subject, relation), listener);
4056         }
4057
4058         @Override
4059         public void forPossibleType(Resource subject, Resource relation,
4060                         Listener<Resource> listener) {
4061                 asyncRequest(new PossibleType(subject, relation), listener);
4062         }
4063
4064         @Override
4065         final public void forPossibleType(final Resource subject,
4066                         final Resource baseType, final AsyncProcedure<Resource> procedure) {
4067
4068                 assert (subject != null);
4069                 assert (procedure != null);
4070
4071                 final NullSingleOrNullProcedure<Resource> checkedProcedure = new NullSingleOrNullProcedure<Resource>(procedure);
4072
4073                 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
4074
4075                                         @Override
4076                                         public void execute(AsyncReadGraph graph,
4077                                                         final Resource principalType) {
4078
4079                                                 if (baseType == null) {
4080
4081                                                         checkedProcedure.offer(graph, principalType);
4082
4083                                                 } else if (principalType.equals(baseType)) {
4084
4085                                                         checkedProcedure.offer(graph, principalType);
4086
4087                                                 } else {
4088
4089                                                         checkedProcedure.inc();
4090
4091                                                         processor.forSupertypes((ReadGraphImpl)graph, principalType,
4092                                                                         new AsyncProcedure<Set<Resource>>() {
4093
4094                                                                                 @Override
4095                                                                                 public void execute(
4096                                                                                                 AsyncReadGraph graph,
4097                                                                                                 Set<Resource> result) {
4098
4099                                                                                         if (result.contains(baseType)) {
4100                                                                                                 checkedProcedure.offer(graph,
4101                                                                                                                 principalType);
4102                                                                                         }
4103
4104                                                                                         checkedProcedure.dec(graph);
4105
4106                                                                                 }
4107
4108                                                                                 @Override
4109                                                                                 public void exception(
4110                                                                                                 AsyncReadGraph graph,
4111                                                                                                 Throwable t) {
4112                                                                                         checkedProcedure.exception(graph, t);
4113                                                                                         checkedProcedure.dec(graph);
4114                                                                                 }
4115
4116                                                                                 @Override
4117                                                                                 public String toString() {
4118                                                                                         return "forPossibleType -> "
4119                                                                                                         + procedure;
4120                                                                                 }
4121
4122                                                                         });
4123
4124                                                 }
4125
4126                                         }
4127
4128                                         @Override
4129                                         public void finished(AsyncReadGraph graph) {
4130                                                 checkedProcedure.dec(graph);
4131                                         }
4132
4133                                         @Override
4134                                         public void exception(AsyncReadGraph graph, Throwable t) {
4135                                                 checkedProcedure.exception(graph, t);
4136                                                 checkedProcedure.dec(graph);
4137                                         }
4138
4139                                 });
4140
4141         }
4142
4143         @Override
4144         public void forPossibleType(Resource subject, Resource relation,
4145                         SyncProcedure<Resource> procedure) {
4146                 forPossibleType(subject, relation, new SyncToAsyncProcedure<Resource>(
4147                                 procedure));
4148         }
4149
4150         @Override
4151         public void forPossibleType(Resource subject, Resource relation,
4152                         Procedure<Resource> procedure) {
4153                 forPossibleType(subject, relation, new NoneToAsyncProcedure<Resource>(
4154                                 procedure));
4155         }
4156
4157         @Override
4158         public <T> void forPossibleValue(Resource subject, AsyncListener<T> listener) {
4159                 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4160         }
4161
4162         @Override
4163         public <T> void forPossibleValue(Resource subject, SyncListener<T> listener) {
4164                 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4165         }
4166
4167         @Override
4168         public <T> void forPossibleValue(Resource subject, Listener<T> listener) {
4169                 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4170         }
4171
4172         @Override
4173         final public <T> void forPossibleValue(final Resource subject,
4174                         final AsyncProcedure<T> procedure) {
4175
4176                 assert (subject != null);
4177                 assert (procedure != null);
4178                 
4179                 forPossibleRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
4180
4181                         @Override
4182                         public void execute(AsyncReadGraph graph, final Datatype type) {
4183                                 if (type == null) {
4184                                         procedure.execute(graph, null);
4185                                 } else {
4186                                         try {
4187                                                 // TODO: consider trying Bindings.getBeanBinding(type);
4188                                                 Binding binding = Bindings.getBinding(type);
4189                                                 graph.forPossibleValue(subject, binding, procedure);
4190                                         } catch (RuntimeBindingConstructionException e) {
4191                                                 procedure.exception(graph, e);
4192                                         }
4193                                 }
4194                         }
4195
4196                         @Override
4197                         public void exception(AsyncReadGraph graph, Throwable t) {
4198                                 procedure.exception(graph, t);
4199                         }
4200
4201                         @Override
4202                         public String toString() {
4203                                 return "forPossibleValue -> " + procedure;
4204                         }
4205
4206                 });
4207
4208         }
4209
4210         @Override
4211         public <T> void forPossibleValue(Resource subject,
4212                         SyncProcedure<T> procedure) {
4213                 forPossibleValue(subject, new SyncToAsyncProcedure<T>(procedure));
4214         }
4215
4216         @Override
4217         public <T> void forPossibleValue(Resource subject, Procedure<T> procedure) {
4218                 forPossibleValue(subject, new NoneToAsyncProcedure<T>(procedure));
4219         }
4220
4221         @Override
4222         public <T> void forPossibleValue(Resource subject, Binding binding,
4223                         AsyncListener<T> listener) {
4224                 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4225         }
4226
4227         @Override
4228         public <T> void forPossibleValue(Resource subject, Binding binding,
4229                         SyncListener<T> listener) {
4230                 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4231         }
4232
4233         @Override
4234         public <T> void forPossibleValue(Resource subject, Binding binding,
4235                         Listener<T> listener) {
4236                 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4237         }
4238
4239         @Override
4240         final public <T> void forPossibleValue(final Resource resource,
4241                         final Binding binding, final AsyncProcedure<T> procedure) {
4242
4243                 assert (resource != null);
4244                 assert (binding != null);
4245                 assert (procedure != null);
4246
4247                 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
4248
4249                         @Override
4250                         public void execute(AsyncReadGraph graph, byte[] result) {
4251
4252                                 try {
4253
4254                                         if (result == null) {
4255                                                 procedure.execute(graph, null);
4256                                                 return;
4257                                         }
4258
4259                                         Serializer serializer = Bindings.getSerializer( binding );
4260                                         Object obj = serializer.deserialize(result);
4261                                         if (!binding.isInstance(obj))
4262                                                 procedure.exception(graph, new ClassCastException(
4263                                                                 "Cannot get value " + obj + " with binding "
4264                                                                                 + binding));
4265                                         else
4266                                                 procedure.execute(graph, (T) obj);
4267
4268                                 } catch (IOException e) {
4269                                         procedure.exception(graph, e);
4270                                 } catch (BufferUnderflowException e) {
4271                                         procedure.exception(graph, e);
4272                                 } catch (Throwable t) {
4273                                         procedure.exception(graph, t);
4274                                 }
4275
4276                         }
4277
4278                         @Override
4279                         public void exception(AsyncReadGraph graph, Throwable t) {
4280                                 try {
4281                                         procedure.exception(graph, t);
4282                                 } catch (Throwable t2) {
4283                                 Logger.defaultLogError(t2);
4284                                 }
4285                         }
4286
4287                         @Override
4288                         public String toString() {
4289                                 return "forPossibleValue -> " + procedure;
4290                         }
4291
4292                 });
4293
4294         }
4295
4296         @Override
4297         public <T> void forPossibleValue(Resource subject, Binding binding,
4298                         SyncProcedure<T> procedure) {
4299                 forPossibleValue(subject, binding, new SyncToAsyncProcedure<T>(
4300                                 procedure));
4301         }
4302
4303         @Override
4304         public <T> void forPossibleValue(Resource subject, Binding binding,
4305                         Procedure<T> procedure) {
4306                 forPossibleValue(subject, binding, new NoneToAsyncProcedure<T>(
4307                                 procedure));
4308         }
4309
4310         @Override
4311         public <T> void forPossibleRelatedValue(Resource subject,
4312                         Resource relation, AsyncListener<T> listener) {
4313                 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4314                                 listener);
4315         }
4316
4317         @Override
4318         public <T> void forPossibleRelatedValue(Resource subject,
4319                         Resource relation, SyncListener<T> listener) {
4320                 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4321                                 listener);
4322         }
4323
4324         @Override
4325         public <T> void forPossibleRelatedValue(Resource subject,
4326                         Resource relation, Listener<T> listener) {
4327                 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4328                                 listener);
4329         }
4330
4331         @Override
4332         final public <T> void forPossibleRelatedValue(final Resource subject,
4333                         final Resource relation, final AsyncProcedure<T> procedure) {
4334
4335                 assert (subject != null);
4336                 assert (relation != null);
4337                 assert (procedure != null);
4338
4339                 final DeepSingleOrNullProcedure<T> checkedProcedure = new DeepSingleOrNullProcedure<T>(procedure);
4340                 
4341                 processor.forEachObject(this, subject, relation,
4342                                 new AsyncMultiProcedureAdapter<Resource>() {
4343
4344                                         @Override
4345                                         public void execute(AsyncReadGraph graph,
4346                                                         final Resource object) {
4347
4348                                                 checkedProcedure.inc();
4349
4350                                                 graph.forValue(object, new AsyncProcedure<Object>() {
4351
4352                                                         @Override
4353                                                         public void execute(AsyncReadGraph graph,
4354                                                                         Object result) {
4355                                                                 checkedProcedure.offer(graph, (T) result);
4356                                                                 checkedProcedure.dec(graph);
4357                                                         }
4358
4359                                                         @Override
4360                                                         public void exception(AsyncReadGraph graph,
4361                                                                         Throwable t) {
4362                                                                 checkedProcedure.exception(graph, t);
4363                                                                 checkedProcedure.dec(graph);
4364                                                         }
4365
4366                                                 });
4367
4368                                         }
4369
4370                                         @Override
4371                                         public void finished(AsyncReadGraph graph) {
4372
4373                                                 checkedProcedure.dec(graph);
4374                                         }
4375
4376                                         @Override
4377                                         public void exception(AsyncReadGraph graph, Throwable t) {
4378                                                 checkedProcedure.exception(graph, t);
4379                                                 checkedProcedure.dec(graph);
4380                                         }
4381
4382                                         @Override
4383                                         public String toString() {
4384                                                 return "forPossibleRelatedValue -> " + procedure;
4385                                         }
4386                                 });
4387
4388         }
4389
4390         @Override
4391         public <T> void forPossibleRelatedValue(Resource subject,
4392                         Resource relation, SyncProcedure<T> procedure) {
4393                 forPossibleRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
4394                                 procedure));
4395         }
4396
4397         @Override
4398         public <T> void forPossibleRelatedValue(Resource subject,
4399                         Resource relation, Procedure<T> procedure) {
4400                 forPossibleRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
4401                                 procedure));
4402         }
4403
4404         @Override
4405         public <T> void forPossibleRelatedValue(Resource subject,
4406                         Resource relation, Binding binding, AsyncListener<T> listener) {
4407                 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4408                                 listener);
4409         }
4410
4411         @Override
4412         public <T> void forPossibleRelatedValue(Resource subject,
4413                         Resource relation, Binding binding, SyncListener<T> listener) {
4414                 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4415                                 listener);
4416         }
4417
4418         @Override
4419         public <T> void forPossibleRelatedValue(Resource subject,
4420                         Resource relation, Binding binding, Listener<T> listener) {
4421                 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4422                                 listener);
4423         }
4424
4425         final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4426                         final AsyncProcedure<T> procedure) {
4427
4428                 assert (subject != null);
4429                 assert (relation != null);
4430                 assert (procedure != null);
4431
4432                 processor.forPossibleObject(this, subject, relation, new AsyncProcedure<Resource>() {
4433                         
4434                         @Override
4435                         public void execute(AsyncReadGraph graph, Resource object) {
4436                                 
4437                                 if(object == null) {
4438                                         procedure.execute(graph, null);
4439                                         return;
4440                                 }
4441
4442                                 processor.forPossibleValue((ReadGraphImpl)graph, object, new AsyncProcedure<byte[]>() {
4443
4444                                         @Override
4445                                         public void execute(AsyncReadGraph graph, byte[] bytes) {
4446
4447                                                 if(bytes != null) {
4448
4449                                                         try {
4450                                                         
4451                                                                 Serializer serializer = binding.serializer();
4452                                                                 Object obj = serializer.deserialize(bytes);
4453                                                                 if (!binding.isInstance(obj)) {
4454                                                                         procedure.exception(graph, new ClassCastException("Cannot get value " + obj + " with binding " + binding));
4455                                                                 } else {
4456                                                                         procedure.execute(graph, (T) obj);
4457                                                                 }
4458
4459                                                         } catch (Throwable t) {
4460                                                                 
4461                                                                 procedure.exception(graph, t);
4462                                                                 
4463                                                         }
4464                                                         
4465                                                 } else {
4466                                                         
4467                                                         procedure.execute(graph, null);
4468                                                         
4469                                                 }
4470                                                 
4471                                         }
4472
4473                                         @Override
4474                                         public void exception(AsyncReadGraph graph, Throwable t) {
4475                                                 procedure.exception(graph, t);
4476                                         }
4477
4478                                 });
4479                                 
4480                         }
4481                         
4482                         @Override
4483                         public void exception(AsyncReadGraph graph, Throwable throwable) {
4484                                 throwable.printStackTrace();
4485                                 procedure.exception(graph, throwable);
4486                         }
4487                         
4488                 });
4489
4490         }
4491
4492         @Override
4493         public <T> void forPossibleRelatedValue(Resource subject,
4494                         Resource relation, Binding binding, SyncProcedure<T> procedure) {
4495                 forPossibleRelatedValue(subject, relation, binding,
4496                                 new SyncToAsyncProcedure<T>(procedure));
4497         }
4498
4499         @Override
4500         public <T> void forPossibleRelatedValue(Resource subject,
4501                         Resource relation, Binding binding, Procedure<T> procedure) {
4502                 forPossibleRelatedValue(subject, relation, binding,
4503                                 new NoneToAsyncProcedure<T>(procedure));
4504         }
4505
4506         @Override
4507         public void forIsInstanceOf(Resource subject, Resource relation,
4508                         AsyncListener<Boolean> listener) {
4509                 asyncRequest(new IsInstanceOf(subject, relation), listener);
4510         }
4511
4512         @Override
4513         public void forIsInstanceOf(Resource subject, Resource relation,
4514                         SyncListener<Boolean> listener) {
4515                 asyncRequest(new IsInstanceOf(subject, relation), listener);
4516         }
4517
4518         @Override
4519         public void forIsInstanceOf(Resource subject, Resource relation,
4520                         Listener<Boolean> listener) {
4521                 asyncRequest(new IsInstanceOf(subject, relation), listener);
4522         }
4523
4524         @Override
4525         final public void forIsInstanceOf(final Resource resource,
4526                         final Resource type, final AsyncProcedure<Boolean> procedure) {
4527
4528                 assert (resource != null);
4529                 assert (type != null);
4530                 assert (procedure != null);
4531
4532                 forTypes(resource, new AsyncProcedure<Set<Resource>>() {
4533
4534                         @Override
4535                         public void execute(AsyncReadGraph graph, Set<Resource> result) {
4536                                 
4537                                 try {
4538                                         if (result.contains(type))
4539                                                 procedure.execute(graph, true);
4540                                         else
4541                                                 procedure.execute(graph, false);
4542                                 } catch (Throwable t) {
4543                                 Logger.defaultLogError(t);
4544                                 }
4545                         }
4546
4547                         @Override
4548                         public void exception(AsyncReadGraph graph, Throwable t) {
4549                                 try {
4550                                         procedure.exception(graph, t);
4551                                 } catch (Throwable t2) {
4552                                 Logger.defaultLogError(t2);
4553                                 }
4554                         }
4555
4556                         @Override
4557                         public String toString() {
4558                                 return "forIsInstanceOf -> " + procedure;
4559                         }
4560
4561                 });
4562
4563         }
4564
4565         @Override
4566         public void forIsInstanceOf(Resource subject, Resource relation,
4567                         SyncProcedure<Boolean> procedure) {
4568                 forIsInstanceOf(subject, relation, new SyncToAsyncProcedure<Boolean>(
4569                                 procedure));
4570         }
4571
4572         @Override
4573         public void forIsInstanceOf(Resource subject, Resource relation,
4574                         Procedure<Boolean> procedure) {
4575                 forIsInstanceOf(subject, relation, new NoneToAsyncProcedure<Boolean>(
4576                                 procedure));
4577         }
4578
4579         @Override
4580         public void forIsInheritedFrom(Resource subject, Resource relation,
4581                         AsyncListener<Boolean> listener) {
4582                 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4583         }
4584
4585         @Override
4586         public void forIsInheritedFrom(Resource subject, Resource relation,
4587                         SyncListener<Boolean> listener) {
4588                 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4589         }
4590
4591         @Override
4592         public void forIsInheritedFrom(Resource subject, Resource relation,
4593                         Listener<Boolean> listener) {
4594                 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4595         }
4596
4597         @Override
4598         final public void forIsInheritedFrom(final Resource resource,
4599                         final Resource type, final AsyncProcedure<Boolean> procedure) {
4600
4601                 assert (resource != null);
4602                 assert (type != null);
4603                 assert (procedure != null);
4604
4605                 if (resource.equals(type)) {
4606                         try {
4607                                 procedure.execute(this, true);
4608                         } catch (Throwable t) {
4609                         Logger.defaultLogError(t);
4610                         }
4611                         return;
4612                 }
4613
4614                 forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
4615
4616                         @Override
4617                         public void execute(AsyncReadGraph graph, Set<Resource> result) {
4618                                 try {
4619                                         if (result.contains(type))
4620                                                 procedure.execute(graph, true);
4621                                         else
4622                                                 procedure.execute(graph, false);
4623                                 } catch (Throwable t) {
4624                                 Logger.defaultLogError(t);
4625                                 }
4626                         }
4627
4628                         @Override
4629                         public void exception(AsyncReadGraph graph, Throwable t) {
4630                                 try {
4631                                         procedure.exception(graph, t);
4632                                 } catch (Throwable t2) {
4633                                 Logger.defaultLogError(t2);
4634                                 }
4635                         }
4636
4637                         @Override
4638                         public String toString() {
4639                                 return "forIsInheritedFrom -> " + procedure;
4640                         }
4641
4642                 });
4643
4644         }
4645
4646         @Override
4647         public void forIsInheritedFrom(Resource subject, Resource relation,
4648                         SyncProcedure<Boolean> procedure) {
4649                 forIsInheritedFrom(subject, relation,
4650                                 new SyncToAsyncProcedure<Boolean>(procedure));
4651         }
4652
4653         @Override
4654         public void forIsInheritedFrom(Resource subject, Resource relation,
4655                         Procedure<Boolean> procedure) {
4656                 forIsInheritedFrom(subject, relation,
4657                                 new NoneToAsyncProcedure<Boolean>(procedure));
4658         }
4659
4660         @Override
4661         public void forIsSubrelationOf(Resource subject, Resource relation,
4662                         AsyncListener<Boolean> listener) {
4663                 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4664         }
4665
4666         @Override
4667         public void forIsSubrelationOf(Resource subject, Resource relation,
4668                         SyncListener<Boolean> listener) {
4669                 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4670         }
4671
4672         @Override
4673         public void forIsSubrelationOf(Resource subject, Resource relation,
4674                         Listener<Boolean> listener) {
4675                 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4676         }
4677
4678         @Override
4679         final public void forIsSubrelationOf(final Resource resource,
4680                         final Resource relation, final AsyncProcedure<Boolean> procedure) {
4681
4682                 assert (resource != null);
4683                 assert (relation != null);
4684                 assert (procedure != null);
4685
4686                 if (resource.equals(relation)) {
4687                         procedure.execute(this, true);
4688                         return;
4689                 }
4690
4691                 forSuperrelations(resource, new AsyncProcedure<Set<Resource>>() {
4692
4693                         @Override
4694                         public void execute(AsyncReadGraph graph, Set<Resource> result) {
4695                                 try {
4696                                         if (result.contains(relation))
4697                                                 procedure.execute(graph, true);
4698                                         else
4699                                                 procedure.execute(graph, false);
4700                                 } catch (Throwable t) {
4701                                 Logger.defaultLogError(t);
4702                                 }
4703                         }
4704
4705                         @Override
4706                         public void exception(AsyncReadGraph graph, Throwable t) {
4707                                 try {
4708                                         procedure.exception(graph, t);
4709                                 } catch (Throwable t2) {
4710                                 Logger.defaultLogError(t2);
4711                                 }
4712                         }
4713
4714                         @Override
4715                         public String toString() {
4716                                 return "forIsSubrelationOf -> " + procedure;
4717                         }
4718
4719                 });
4720
4721         }
4722
4723         @Override
4724         public void forIsSubrelationOf(Resource subject, Resource relation,
4725                         SyncProcedure<Boolean> procedure) {
4726                 forIsSubrelationOf(subject, relation,
4727                                 new SyncToAsyncProcedure<Boolean>(procedure));
4728         }
4729
4730         @Override
4731         public void forIsSubrelationOf(Resource subject, Resource relation,
4732                         Procedure<Boolean> procedure) {
4733                 forIsSubrelationOf(subject, relation,
4734                                 new NoneToAsyncProcedure<Boolean>(procedure));
4735         }
4736
4737         @Override
4738         public void forHasStatement(Resource subject,
4739                         AsyncListener<Boolean> listener) {
4740                 asyncRequest(new HasStatementSubject(subject), listener);
4741         }
4742
4743         @Override
4744         public void forHasStatement(Resource subject, SyncListener<Boolean> listener) {
4745                 asyncRequest(new HasStatementSubject(subject), listener);
4746         }
4747
4748         @Override
4749         public void forHasStatement(Resource subject, Listener<Boolean> listener) {
4750                 asyncRequest(new HasStatementSubject(subject), listener);
4751         }
4752
4753         @Override
4754         final public void forHasStatement(final Resource subject,
4755                         final AsyncProcedure<Boolean> procedure) {
4756
4757                 assert (subject != null);
4758                 assert (procedure != null);
4759
4760                 processor.forHasStatement(this, subject, procedure);
4761
4762         }
4763
4764         @Override
4765         public void forHasStatement(Resource subject,
4766                         SyncProcedure<Boolean> procedure) {
4767                 forHasStatement(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4768         }
4769
4770         @Override
4771         public void forHasStatement(Resource subject, Procedure<Boolean> procedure) {
4772                 forHasStatement(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4773         }
4774
4775         @Override
4776         public void forHasStatement(Resource subject, Resource relation,
4777                         AsyncListener<Boolean> listener) {
4778                 asyncRequest(new HasStatement(subject, relation), listener);
4779         }
4780
4781         @Override
4782         public void forHasStatement(Resource subject, Resource relation,
4783                         SyncListener<Boolean> listener) {
4784                 asyncRequest(new HasStatement(subject, relation), listener);
4785         }
4786
4787         @Override
4788         public void forHasStatement(Resource subject, Resource relation,
4789                         Listener<Boolean> listener) {
4790                 asyncRequest(new HasStatement(subject, relation), listener);
4791         }
4792
4793         @Override
4794         final public void forHasStatement(final Resource subject,
4795                         final Resource relation, final AsyncProcedure<Boolean> procedure) {
4796
4797                 assert (subject != null);
4798                 assert (relation != null);
4799                 assert (procedure != null);
4800
4801                 processor.forHasStatement(this, subject, relation, procedure);
4802
4803         }
4804
4805         @Override
4806         public void forHasStatement(Resource subject, Resource relation,
4807                         SyncProcedure<Boolean> procedure) {
4808                 forHasStatement(subject, relation, new SyncToAsyncProcedure<Boolean>(
4809                                 procedure));
4810         }
4811
4812         @Override
4813         public void forHasStatement(Resource subject, Resource relation,
4814                         Procedure<Boolean> procedure) {
4815                 forHasStatement(subject, relation, new NoneToAsyncProcedure<Boolean>(
4816                                 procedure));
4817         }
4818
4819         @Override
4820         public void forHasStatement(Resource subject, Resource relation,
4821                         Resource object, AsyncListener<Boolean> listener) {
4822                 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4823                                 listener);
4824         }
4825
4826         @Override
4827         public void forHasStatement(Resource subject, Resource relation,
4828                         Resource object, SyncListener<Boolean> listener) {
4829                 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4830                                 listener);
4831         }
4832
4833         @Override
4834         public void forHasStatement(Resource subject, Resource relation,
4835                         Resource object, Listener<Boolean> listener) {
4836                 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4837                                 listener);
4838         }
4839
4840         @Override
4841         final public void forHasStatement(final Resource subject,
4842                         final Resource relation, final Resource object,
4843                         final AsyncProcedure<Boolean> procedure) {
4844
4845                 assert (subject != null);
4846                 assert (relation != null);
4847                 assert (object != null);
4848                 assert (procedure != null);
4849
4850                 processor.forHasStatement(this, subject, relation, object, procedure);
4851
4852         }
4853
4854         @Override
4855         public void forHasStatement(Resource subject, Resource relation,
4856                         Resource object, SyncProcedure<Boolean> procedure) {
4857                 forHasStatement(subject, relation, object,
4858                                 new SyncToAsyncProcedure<Boolean>(procedure));
4859         }
4860
4861         @Override
4862         public void forHasStatement(Resource subject, Resource relation,
4863                         Resource object, Procedure<Boolean> procedure) {
4864                 forHasStatement(subject, relation, object,
4865                                 new NoneToAsyncProcedure<Boolean>(procedure));
4866         }
4867
4868         @Override
4869         public void forHasValue(Resource subject, AsyncListener<Boolean> listener) {
4870                 asyncRequest(new HasValue(subject), listener);
4871         }
4872
4873         @Override
4874         public void forHasValue(Resource subject, SyncListener<Boolean> listener) {
4875                 asyncRequest(new HasValue(subject), listener);
4876         }
4877
4878         @Override
4879         public void forHasValue(Resource subject, Listener<Boolean> listener) {
4880                 asyncRequest(new HasValue(subject), listener);
4881         }
4882
4883         @Override
4884         final public void forHasValue(final Resource subject,
4885                         final AsyncProcedure<Boolean> procedure) {
4886
4887                 assert (subject != null);
4888                 assert (procedure != null);
4889
4890                 processor.forValue(this, subject, new AsyncProcedure<byte[]>() {
4891
4892                         @Override
4893                         public void execute(AsyncReadGraph graph, byte[] result) {
4894                                 try {
4895                                         if (result == null)
4896                                                 procedure.execute(graph, false);
4897                                         else
4898                                                 procedure.execute(graph, true);
4899                                 } catch (Throwable t) {
4900                                 Logger.defaultLogError(t);
4901                                 }
4902                         }
4903
4904                         @Override
4905                         public void exception(AsyncReadGraph graph, Throwable t) {
4906                                 try {
4907                                         procedure.exception(graph, t);
4908                                 } catch (Throwable t2) {
4909                                 Logger.defaultLogError(t2);
4910                                 }
4911                         }
4912
4913                         @Override
4914                         public String toString() {
4915                                 return "forHasValue -> " + procedure;
4916                         }
4917
4918                 });
4919
4920         }
4921
4922         @Override
4923         public void forHasValue(Resource subject, SyncProcedure<Boolean> procedure) {
4924                 forHasValue(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4925         }
4926
4927         @Override
4928         public void forHasValue(Resource subject, Procedure<Boolean> procedure) {
4929                 forHasValue(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4930         }
4931
4932         @Override
4933         public void forOrderedSet(Resource subject,
4934                         AsyncMultiListener<Resource> listener) {
4935                 asyncRequest(new OrderedSet(subject), listener);
4936         }
4937
4938         @Override
4939         public void forOrderedSet(Resource subject,
4940                         SyncMultiListener<Resource> listener) {
4941                 asyncRequest(new OrderedSet(subject), listener);
4942         }
4943
4944         @Override
4945         public void forOrderedSet(Resource subject, MultiListener<Resource> listener) {
4946                 asyncRequest(new OrderedSet(subject), listener);
4947         }
4948
4949         @Override
4950         final public void forOrderedSet(final Resource subject,
4951                         final AsyncMultiProcedure<Resource> procedure) {
4952
4953                 assert (subject != null);
4954                 assert (procedure != null);
4955
4956                 processor.forOrderedSet(this, subject,
4957                                 new AsyncMultiProcedure<Resource>() {
4958
4959                                         @Override
4960                                         public void finished(AsyncReadGraph graph) {
4961                                                 try {
4962                                                         procedure.finished(graph);
4963                                                 } catch (Throwable t) {
4964                                                 Logger.defaultLogError(t);
4965                                                 }
4966                                         }
4967
4968                                         @Override
4969                                         public void execute(AsyncReadGraph graph, Resource result) {
4970                                                 try {
4971                                                         procedure.execute(graph, result);
4972                                                 } catch (Throwable t) {
4973                                                 Logger.defaultLogError(t);
4974                                                 }
4975                                         }
4976
4977                                         @Override
4978                                         public void exception(AsyncReadGraph graph, Throwable t) {
4979                                                 try {
4980                                                         procedure.exception(graph, t);
4981                                                 } catch (Throwable t2) {
4982                                                 Logger.defaultLogError(t2);
4983                                                 }
4984                                         }
4985
4986                                         @Override
4987                                         public String toString() {
4988                                                 return "forOrderedSet -> " + procedure;
4989                                         }
4990
4991                                 });
4992
4993         }
4994
4995         @Override
4996         public void forOrderedSet(Resource subject,
4997                         SyncMultiProcedure<Resource> procedure) {
4998                 forOrderedSet(subject, new SyncToAsyncMultiProcedure<Resource>(
4999                                 procedure));
5000         }
5001
5002         @Override
5003         public void forOrderedSet(Resource subject,
5004                         MultiProcedure<Resource> procedure) {
5005                 forOrderedSet(subject, new NoneToAsyncMultiProcedure<Resource>(
5006                                 procedure));
5007         }
5008
5009         @Override
5010         public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5011                         AsyncListener<T> listener) {
5012                 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5013         }
5014
5015         @Override
5016         public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5017                         SyncListener<T> listener) {
5018                 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5019         }
5020
5021         @Override
5022         public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5023                         Listener<T> listener) {
5024                 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5025         }
5026
5027         @Override
5028         final public <T> void forPossibleAdapted(final Resource resource,
5029                         final Class<T> clazz, final AsyncProcedure<T> procedure) {
5030
5031                 assert (resource != null);
5032                 assert (clazz != null);
5033                 assert (procedure != null);
5034
5035                 final AdaptionService service = getSession().peekService(AdaptionService.class);
5036                 if (service == null)
5037                         procedure.exception(this, new ServiceException("No AdaptionService available")); 
5038                 else
5039                         service.adapt(this, resource, resource, Resource.class, clazz, true, procedure);
5040         }
5041
5042         @Override
5043         public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5044                         SyncProcedure<T> procedure) {
5045                 forPossibleAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5046                                 procedure));
5047         }
5048
5049         @Override
5050         public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5051                         Procedure<T> procedure) {
5052                 forPossibleAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5053                                 procedure));
5054         }
5055
5056         @Override
5057         public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5058                         AsyncListener<T> listener) {
5059                 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5060         }
5061
5062         @Override
5063         public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5064                         SyncListener<T> listener) {
5065                 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5066         }
5067
5068         @Override
5069         public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5070                         Listener<T> listener) {
5071                 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5072         }
5073
5074         @Override
5075         final public <T> void forPossibleUniqueAdapted(final Resource resource,
5076                         final Class<T> clazz, final AsyncProcedure<T> procedure) {
5077
5078                 assert (resource != null);
5079                 assert (clazz != null);
5080                 assert (procedure != null);
5081
5082                 final AdaptionService service = getSession().peekService(AdaptionService.class);
5083                 if (service == null)
5084                         procedure.exception(this, new ServiceException("No AdaptionService available")); 
5085                 else
5086                         service.adaptNew(this, resource, clazz, true, procedure);
5087
5088         }
5089
5090         @Override
5091         public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5092                         SyncProcedure<T> procedure) {
5093                 forPossibleUniqueAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5094                                 procedure));
5095         }
5096
5097         @Override
5098         public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5099                         Procedure<T> procedure) {
5100                 forPossibleUniqueAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5101                                 procedure));
5102         }
5103
5104         /*
5105          * Implementation of the interface AsyncRequestProcessor
5106          */
5107
5108         @Override
5109         final public Session getSession() {
5110                 return processor.getSession();
5111         }
5112
5113         @Override
5114         public <T> void asyncRequest(final Read<T> request) {
5115
5116                 asyncRequest(request, new AsyncProcedure<T>() {
5117
5118                         @Override
5119                         public void execute(AsyncReadGraph graph, T result) {
5120                         }
5121
5122                         @Override
5123                         public void exception(AsyncReadGraph graph, Throwable t) {
5124                                 Logger.defaultLogError(t);
5125                         }
5126
5127                         @Override
5128                         public String toString() {
5129                                 return "asyncRequest(Read) -> " + request;
5130                         }
5131
5132                 });
5133
5134         }
5135
5136         @Override
5137         public <T> void asyncRequest(Read<T> request, AsyncListener<T> procedure) {
5138                 asyncRequest(request, (AsyncProcedure<T>) procedure);
5139         }
5140
5141         @Override
5142         public <T> void asyncRequest(Read<T> request,
5143                         final SyncListener<T> procedure) {
5144                 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5145         }
5146
5147         @Override
5148         public <T> void asyncRequest(Read<T> request, final Listener<T> procedure) {
5149                 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5150         }
5151
5152         @Override
5153         public <T> void asyncRequest(final Read<T> request, final AsyncProcedure<T> procedure) {
5154
5155                 assert (request != null);
5156                 assert (procedure != null);
5157         
5158                 processor.schedule(new SessionTask(this) {
5159
5160                         @Override
5161                         public void run0(int thread) {
5162                                 try {
5163                                         final ListenerBase listener = getListenerBase(procedure);
5164                                         QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5165                                 } catch (DatabaseException e) {
5166                                         Logger.defaultLogError(e);
5167                                 }
5168                         }
5169                         
5170                 });
5171
5172         }
5173
5174     public static ReadGraphImpl createAsync(QueryProcessor support) {
5175         return new ReadGraphImpl(null, null, support);
5176     }
5177
5178         @Override
5179         public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
5180                 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5181         }
5182
5183         @Override
5184         public <T> void asyncRequest(Read<T> request, final Procedure<T> procedure) {
5185                 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5186         }
5187
5188         @Override
5189         final public <T> void asyncRequest(final AsyncRead<T> request) {
5190
5191                 assert (request != null);
5192
5193                 asyncRequest(request, new AsyncProcedure<T>() {
5194
5195                         @Override
5196                         public void execute(AsyncReadGraph graph, T result) {
5197                         }
5198
5199                         @Override
5200                         public void exception(AsyncReadGraph graph, Throwable t) {
5201                         Logger.defaultLogError(t);
5202                         }
5203
5204                         @Override
5205                         public String toString() {
5206                                 return "asyncRequest(AsyncRead) -> " + request;
5207                         }
5208
5209                 });
5210
5211         }
5212
5213         @Override
5214         public <T> void asyncRequest(AsyncRead<T> request,
5215                         AsyncListener<T> procedure) {
5216                 asyncRequest(request, (AsyncProcedure<T>) procedure);
5217         }
5218
5219         @Override
5220         final public <T> void asyncRequest(AsyncRead<T> request,
5221                         final SyncListener<T> procedure) {
5222                 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5223         }
5224
5225         @Override
5226         final public <T> void asyncRequest(AsyncRead<T> request,
5227                         final Listener<T> procedure) {
5228                 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5229         }
5230
5231         @Override
5232         final public <T> void asyncRequest(final AsyncRead<T> request,
5233                         final AsyncProcedure<T> procedure) {
5234
5235                 assert (request != null);
5236                 assert (procedure != null);
5237
5238                 processor.schedule(new SessionTask(this) {
5239
5240                         @Override
5241                         public void run0(int thread) {
5242                                 try {
5243                                         final ListenerBase listener = getListenerBase(procedure);
5244                                         QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5245                                 } catch (DatabaseException e) {
5246                                         Logger.defaultLogError(e);
5247                                 }
5248                         }
5249                         
5250                 });
5251
5252         }
5253
5254         @Override
5255         public <T> void asyncRequest(AsyncRead<T> request,
5256                         SyncProcedure<T> procedure) {
5257                 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5258         }
5259
5260         @Override
5261         final public <T> void asyncRequest(final AsyncRead<T> request,
5262                         final Procedure<T> procedure) {
5263                 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5264         }
5265
5266         @Override
5267         public <T> void asyncRequest(final MultiRead<T> request) {
5268
5269                 assert (request != null);
5270
5271                 asyncRequest(request, new SyncMultiProcedureAdapter<T>() {
5272                         @Override
5273                         public void exception(ReadGraph graph, Throwable t) {
5274                         Logger.defaultLogError(t);
5275                         }
5276
5277                         @Override
5278                         public String toString() {
5279                                 return "asyncRequest(MultiRead) -> " + request;
5280                         }
5281                 });
5282
5283         }
5284
5285         @Override
5286         public <T> void asyncRequest(MultiRead<T> request,
5287                         SyncMultiListener<T> procedure) {
5288                 asyncRequest(request, (SyncMultiProcedure<T>)procedure);
5289         }
5290
5291         @Override
5292         public <T> void asyncRequest(MultiRead<T> request,
5293                         MultiListener<T> procedure) {
5294                 asyncRequest(request, new NoneToSyncMultiListener<T>(procedure));
5295         }
5296
5297         
5298         @Override
5299         public <T> void asyncRequest(final MultiRead<T> request,
5300                         final SyncMultiProcedure<T> procedure) {
5301
5302                 assert (request != null);
5303                 assert (procedure != null);
5304
5305                 final ListenerBase listener = getListenerBase(procedure);
5306
5307                 if (parent != null || listener != null) {
5308
5309 //                  final ReadGraphImpl newGraph = newSync();
5310                     processor.query(this, request, parent, procedure,listener);
5311
5312                 } else {
5313
5314 //                  final ReadGraphImpl newGraph = newSync();
5315
5316                     try {
5317
5318                         request.perform(this, procedure);
5319
5320                     } catch (Throwable t) {
5321
5322                         try {
5323                                         procedure.exception(this, t);
5324                                 } catch (DatabaseException e) {
5325                                         LOGGER.error("Unexpected exception while handling exception", e);
5326                                 }
5327
5328                     }                           
5329
5330                 }
5331
5332         }
5333
5334         @Override
5335         public <T> void asyncRequest(MultiRead<T> request,
5336                         MultiProcedure<T> procedure) {
5337                 asyncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
5338         }
5339
5340         @Override
5341         final public <T> void asyncRequest(final AsyncMultiRead<T> request) {
5342
5343                 assert (request != null);
5344
5345                 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5346                         @Override
5347                         public void exception(AsyncReadGraph graph, Throwable t) {
5348                         Logger.defaultLogError(t);
5349                         }
5350
5351                         @Override
5352                         public String toString() {
5353                                 return "asyncRequest(AsyncMultiRead) -> " + request;
5354                         }
5355                 });
5356
5357         }
5358
5359         @Override
5360         public <T> void asyncRequest(AsyncMultiRead<T> request,
5361                         AsyncMultiListener<T> procedure) {
5362                 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5363         }
5364
5365         @Override
5366         public <T> void asyncRequest(AsyncMultiRead<T> request,
5367                         SyncMultiListener<T> procedure) {
5368                 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5369         }
5370
5371         @Override
5372         public <T> void asyncRequest(AsyncMultiRead<T> request,
5373                         MultiListener<T> procedure) {
5374                 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5375         }
5376
5377         @Override
5378         final public <T> void asyncRequest(AsyncMultiRead<T> request,
5379                         final AsyncMultiProcedure<T> procedure) {
5380
5381                 assert (request != null);
5382                 assert (procedure != null);
5383
5384                 ListenerBase listener = getListenerBase(procedure);
5385
5386                 if (parent != null || listener != null) {
5387
5388                         processor.query(this, request, parent, procedure, listener);
5389
5390                 } else {
5391
5392                         try {
5393
5394                                 request.perform(this, new AsyncMultiProcedure<T>() {
5395
5396                                         @Override
5397                                         public void execute(AsyncReadGraph graph, T result) {
5398                                                 procedure.execute(graph, result);
5399                                         }
5400
5401                                         @Override
5402                                         public void finished(AsyncReadGraph graph) {
5403                                                 procedure.finished(graph);
5404                                         }
5405
5406                                         @Override
5407                                         public void exception(AsyncReadGraph graph, Throwable t) {
5408                                                 procedure.exception(graph, t);
5409                                         }
5410
5411                                         @Override
5412                                         public String toString() {
5413                                                 return "asyncRequest(AsyncMultiRead) -> " + procedure;
5414                                         }
5415
5416                                 });
5417
5418                         } catch (Throwable t) {
5419
5420                                 procedure.exception(this, new DatabaseException(t));
5421
5422                         }
5423                 }
5424
5425         }
5426
5427         @Override
5428         public <T> void asyncRequest(AsyncMultiRead<T> request,
5429                         SyncMultiProcedure<T> procedure) {
5430                 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5431         }
5432
5433         @Override
5434         final public <T> void asyncRequest(AsyncMultiRead<T> request,
5435                         final MultiProcedure<T> procedure) {
5436                 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5437         }
5438
5439         @Override
5440         final public <T> void asyncRequest(final ExternalRead<T> request) {
5441
5442                 assert (request != null);
5443
5444                 asyncRequest(request, new Procedure<T>() {
5445
5446                         @Override
5447                         public void execute(T result) {
5448                         }
5449
5450                         @Override
5451                         public void exception(Throwable t) {
5452                         Logger.defaultLogError(t);
5453                         }
5454
5455                         @Override
5456                         public String toString() {
5457                                 return "asyncRequest(PrimitiveRead) -> " + request;
5458                         }
5459
5460                 });
5461
5462         }
5463
5464         @Override
5465         public <T> void asyncRequest(ExternalRead<T> request,
5466                         final Listener<T> procedure) {
5467                 asyncRequest(request, (Procedure<T>) procedure);
5468         }
5469
5470         @Override
5471         final public <T> void asyncRequest(final ExternalRead<T> request,
5472                         final Procedure<T> procedure) {
5473
5474                 assert (request != null);
5475                 assert (procedure != null);
5476
5477                 final ListenerBase listener = getListenerBase(procedure);
5478
5479                 if (parent != null || listener != null) {
5480
5481                         try {
5482                                 QueryCacheBase.resultExternalReadEntry(this, request, parent, listener, procedure);
5483                         } catch (DatabaseException e) {
5484                                 Logger.defaultLogError(e);
5485                                 // This throwable has already been transferred to procedure at this point - do nothing about it
5486                         }
5487
5488                 } else {
5489
5490                         request.register(this, new Listener<T>() {
5491                                 @Override
5492                                 public void execute(T result) {
5493                                         procedure.execute(result);
5494                                 }
5495
5496                                 @Override
5497                                 public void exception(Throwable t) {
5498                                         procedure.exception(t);
5499                                 }
5500
5501                                 @Override
5502                                 public String toString() {
5503                                         return "asyncRequest(PrimitiveRead) -> " + request;
5504                                 }
5505
5506                                 @Override
5507                                 public boolean isDisposed() {
5508                                         return true;
5509                                 }
5510
5511                         });
5512
5513                 }
5514
5515         }
5516
5517         @Override
5518         public void asyncRequest(final Write request) {
5519
5520                 assert (request != null);
5521                 
5522                 getSession().asyncRequest(request);
5523
5524 //              processor.asyncWrite(request);
5525
5526         }
5527
5528         @Override
5529         public <T> void asyncRequest(final WriteResult<T> request, Procedure<T> procedure) {
5530                 throw new Error("Not implemented.");
5531         }
5532         
5533         @Override
5534         public void asyncRequest(Write request, Consumer<DatabaseException> callback) {
5535                 
5536                 assert (request != null);
5537                 
5538                 getSession().asyncRequest(request, callback);
5539                 
5540         }
5541
5542         @Override
5543         public void asyncRequest(final DelayedWrite request) {
5544
5545                 assert (request != null);
5546
5547                 getSession().asyncRequest(request);
5548
5549         }
5550
5551         @Override
5552         public <T> void asyncRequest(final DelayedWriteResult<T> request, Procedure<T> procedure) {
5553                 throw new Error("Not implemented.");
5554         }
5555         
5556         @Override
5557         public void asyncRequest(DelayedWrite r,
5558                         Consumer<DatabaseException> callback) {
5559                 throw new Error("Not implemented.");
5560         }
5561
5562         @Override
5563         public void asyncRequest(final WriteOnly request) {
5564
5565                 assert (request != null);
5566
5567                 getSession().asyncRequest(request);
5568
5569         }
5570
5571         @Override
5572         public <T> void asyncRequest(final WriteOnlyResult<T> request, Procedure<T> procedure) {
5573                 throw new Error("Not implemented.");
5574         }
5575         
5576         @Override
5577         public void asyncRequest(WriteOnly r, Consumer<DatabaseException> callback) {
5578                 throw new Error("Not implemented.");
5579         }
5580
5581         /*
5582          * Implementation of the interface ServiceLocator
5583          */
5584
5585         @Override
5586         public <T> T getService(Class<T> api) {
5587             if(WriteSupport.class == api) {
5588                 if(this instanceof WriteGraphImpl) {
5589                     WriteGraphImpl impl = (WriteGraphImpl)this;
5590                     return (T)impl.writeSupport;
5591                 }
5592             }
5593                 return getSession().getService(api);
5594         }
5595
5596         @Override
5597         public <T> T peekService(Class<T> api) {
5598                 return getSession().peekService(api);
5599         }
5600
5601         @Override
5602         public boolean hasService(Class<?> api) {
5603                 return getSession().hasService(api);
5604         }
5605
5606         @Override
5607         public <T> void registerService(Class<T> api, T service) {
5608                 getSession().registerService(api, service);
5609         }
5610         
5611         @Override
5612         public boolean isImmutable(Resource resource) throws DatabaseException {
5613                 ResourceImpl impl = (ResourceImpl)resource;
5614                 return processor.isImmutable(impl.id);
5615         }
5616
5617         /*
5618          * Internal routines
5619          */
5620
5621         protected static String INTERNAL_ERROR_STRING = "Transaction aborted due to internal client error.";
5622
5623         /*
5624          * callerThread is the currently running thread state.syncThread is blocking for
5625          * this execution state.syncParent is the blocking request
5626          */
5627
5628         ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support) {
5629                 this.parentGraph = parentGraph;
5630                 this.parent = parent;
5631                 this.processor = support;
5632                 this.asyncBarrier = new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent);
5633         }
5634
5635         ReadGraphImpl(ReadGraphImpl graph, CacheEntry parent) {
5636                 this(graph, parent, graph.processor);
5637         }
5638
5639         ReadGraphImpl(ReadGraphImpl graph) {
5640                 this(graph, graph.parent);
5641         }
5642
5643         public ReadGraphImpl withParent(CacheEntry parent) {
5644                 return new ReadGraphImpl(this, parent);
5645         }
5646
5647         public ReadGraphImpl forRecompute(CacheEntry parent) {
5648                 return new ReadGraphImpl(null, parent, processor);
5649         }
5650
5651         public static ReadGraphImpl create(QueryProcessor support) {
5652                 return new ReadGraphImpl(null, null, support);
5653         }
5654
5655         public ReadGraphImpl newRestart(ReadGraphImpl impl) {
5656
5657                 WriteGraphImpl write = processor.getSession().getService(
5658                                 WriteGraphImpl.class);
5659
5660                 return write;
5661
5662         }
5663
5664         final private ListenerBase getListenerBase(final Object procedure) {
5665                 if (procedure instanceof ListenerBase)
5666                         return (ListenerBase) procedure;
5667                 else
5668                         return null;
5669         }
5670
5671         public <T> void waitAsyncProcedure(AsyncMultiReadProcedure<T> procedure) {
5672                 
5673                 assert(procedure.done());
5674                 
5675         }
5676
5677         public <T> void waitAsyncProcedure(AsyncReadProcedure<T> procedure) {
5678                 
5679                 assert(procedure.done());
5680                 
5681         }
5682
5683         public boolean resumeTasks() {
5684                 return processor.resumeTasks(this);
5685         }
5686
5687         Class<?> singleClass(Set<Resource> types) {
5688                 Class<?> result = null;
5689                 for (Resource type : types) {
5690                         Class<?> clazz = processor.getBuiltinValue(type);
5691                         if (clazz != null) {
5692                                 if (result != null)
5693                                         return null;
5694                                 else
5695                                         result = clazz;
5696                         }
5697                 }
5698                 return result;
5699         }
5700
5701         private String debugString(Resource r) {
5702                 String name = null;
5703                 try {
5704                         name = getPossibleRelatedValue(r, processor.getL0(this).HasName);
5705                 } catch (ManyObjectsForFunctionalRelationException e) {
5706                 Logger.defaultLogError(e);
5707                 } catch (ServiceException e) {
5708                 Logger.defaultLogError(e);
5709                 }
5710                 return "[" + name + " - " + r + "]";
5711         }
5712
5713         @Override
5714         public String toString() {
5715                 return "ReadGraphImpl[thread=" + Thread.currentThread() + "]";
5716         }
5717
5718         @Override
5719         final public int thread() {
5720                 return 0;
5721         }
5722         
5723     static class MultiTripleIntProcedure implements TripleIntProcedure {
5724
5725         final private AsyncMultiProcedure<Statement> procedure;
5726         final private ReadGraphImpl impl;
5727         final private QuerySupport support;
5728         
5729         public MultiTripleIntProcedure(AsyncMultiProcedure<Statement> procedure, ReadGraphImpl impl, QuerySupport support) {
5730                 this.procedure = procedure;
5731                 this.impl = impl;
5732                 this.support = support;
5733         }
5734         
5735         @Override
5736         public void execute(ReadGraphImpl graph, int s, int p, int o) {
5737                 try {
5738                         procedure.execute(graph, support.getStatement(s, p, o));
5739                 } catch (Throwable t2) {
5740                         Logger.defaultLogError(t2);
5741                 }
5742         }
5743
5744         @Override
5745         public void finished(ReadGraphImpl graph) {
5746                 try {
5747                         procedure.finished(graph);
5748 //                      impl.state.barrier.dec("ReadGraphSupportImpl.516");
5749                 } catch (Throwable t2) {
5750                         Logger.defaultLogError(t2);
5751                 }
5752         }
5753
5754         @Override
5755         public void exception(ReadGraphImpl graph, Throwable t) {
5756                 try {
5757                         procedure.exception(graph, t);
5758                 } catch (Throwable t2) {
5759                         Logger.defaultLogError(t2);
5760                 }
5761 //              impl.state.barrier.dec("ReadGraphSupportImpl.516");
5762         }
5763
5764         @Override
5765         public String toString() {
5766                 return "forEachObject with " + procedure;
5767         }
5768         
5769     }
5770
5771 //    private AsyncMultiProcedure<Resource> cacheKey = null;
5772 //    private MultiIntProcedure cacheResult = null;
5773 //    
5774 //    final IntProcedure forMultiProcedure(final AsyncMultiProcedure<Resource> procedure) {
5775 //      
5776 //      if(procedure == cacheKey) return cacheResult; 
5777 //      
5778 //      cacheResult = new MultiIntProcedure(procedure, this, processor.support);
5779 //      cacheKey = procedure;
5780 //      
5781 //      return cacheResult;
5782 //      
5783 //    }
5784 //
5785 //    private AsyncMultiProcedure<Statement> cacheKey2 = null;
5786 //    private MultiTripleIntProcedure cacheResult2 = null;
5787 //
5788 //    final synchronized TripleIntProcedure forMultiProcedure(final AsyncMultiProcedure<Statement> procedure) {
5789 //      
5790 //      if(procedure == cacheKey2) return cacheResult2; 
5791 //      
5792 //      cacheResult2 = new MultiTripleIntProcedure(procedure, this, processor.support);
5793 //      cacheKey2 = procedure;
5794 //      
5795 //      return cacheResult2;
5796 //      
5797 //    }
5798     
5799     @Override
5800     public Datatype getDataType(Resource subject) throws DatabaseException {
5801         for(Resource dataTypeResource : getObjects(subject, processor.getL0(this).HasDataType))
5802                 return getValue(dataTypeResource, Bindings.getBindingUnchecked(Datatype.class));
5803         throw new DoesNotContainValueException("The literal has no data type.");
5804     }
5805     
5806     protected <T extends Accessor> T getAccessor4File(Resource subject)
5807     throws DatabaseException {
5808         return null;
5809         /*        
5810         byte[]  bytes = processor.support.getValue(g, subject);
5811         if (null == bytes)
5812             return null;
5813         try {
5814             BinaryVariant va = (BinaryVariant) Accessors.getAccessor(bytes, Datatypes.VARIANT);
5815             Accessor ca = va.getContentAccessor();
5816             return (T)ca;
5817         } catch (AccessorConstructionException e) {
5818             throw new DatabaseException(e);
5819         }
5820         */
5821         /*
5822         if (null == bytes)
5823             return null;
5824         Binding datatype_binding = Bindings.getBindingUnchecked(DataType.class);
5825         Serializer datatype_serializer = datatype_binding.serializer();
5826         DataType datatype;
5827         try {
5828             BinaryMemory in = new BinaryMemory(ByteBuffer.wrap(bytes));
5829             datatype = (DataType)datatype_serializer.deserialize(in);
5830             Binding data_binding = Bindings.getBinding(datatype);
5831             Serializer data_serializer = data_binding.serializer(BinarySerializationFormat.INSTANCE);
5832             Object o = data_serializer.deserialize(in);
5833             try {
5834                 return (T)Accessors.getAccessor(data_binding, o);
5835             } catch(AccessorConstructionException e) {
5836                 return null;
5837             }
5838         } catch (Exception e) {
5839             throw new DatabaseException(e);
5840         }*/
5841     }
5842     @SuppressWarnings("unchecked")
5843     @Override
5844     public <T extends Accessor> T getAccessor(Resource subject) throws DatabaseException {
5845         RandomAccessBinary rab = getRandomAccessBinary(subject);
5846         try {
5847             return (T)Accessors.getAccessor(rab, getDataType(subject));
5848         } catch(AccessorConstructionException e) {
5849             throw new DatabaseException(e);
5850         }
5851     }
5852     @SuppressWarnings("unchecked")
5853     protected <T extends Accessor> T createAccessor(Resource resource, Datatype datatype, Object intialValue)
5854     throws DatabaseException {
5855         RandomAccessBinary rab = createRandomAccessBinary(resource, datatype, intialValue);
5856         try {
5857             return (T)Accessors.getAccessor(rab, datatype);
5858         } catch(AccessorConstructionException e) {
5859             throw new DatabaseException(e);
5860         }
5861     }
5862     @Override
5863     public RandomAccessBinary getRandomAccessBinary(Resource subject) throws DatabaseException {
5864         RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5865         ResourceData rd = ravs.get(subject);
5866         if (null != rd)
5867             return rd;
5868         try {
5869             ExternalValueSupport evs = getService(ExternalValueSupport.class);
5870             long size = evs.getValueSize(this, subject); // Throws DatabaseException if no old external value.
5871             try {
5872                 File platform = Platform.getLocation().toFile();
5873                 File tempFiles = new File(platform, "tempFiles");
5874                 File dbDir = new File(tempFiles, "db");
5875                 dbDir.mkdirs();
5876                 File file = new File(dbDir, "ResourceFile" + subject.getResourceId());
5877                 rd = new ResourceData(new BinaryFile(file), true); // Old external value.
5878                 final int N = 1<<20;
5879                 long left = size;
5880                 long offset = 0;
5881                 while (left > 0) {
5882                     int length = N < left ? N : (int)left;
5883                     byte[] bytes = evs.readValue(this, subject, offset, length);
5884                     offset += bytes.length;
5885                     left -= bytes.length;
5886                     rd.binaryFile.write(bytes);
5887                 }
5888                 ravs.put(subject, rd);
5889                 return rd;
5890             } catch (Exception e) {
5891                 throw new DatabaseException("Resource " + subject + " have value but there was problem with accessing value data.", e);
5892             }
5893         } catch (Exception e) {
5894             if(Development.DEVELOPMENT) {
5895                 if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
5896                     e.printStackTrace();
5897                 }
5898             }
5899         }
5900         Datatype datatype = getDataType(subject);
5901         Object value = getPossibleValue(subject, Bindings.getBinding(datatype));
5902         return createRandomAccessBinary(subject, datatype, value);
5903     }
5904     public RandomAccessBinary createRandomAccessBinary(Resource resource, Datatype datatype, Object initialValue)
5905     throws DatabaseException {
5906         RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5907         try {
5908             File platform = Platform.getLocation().toFile();
5909             File tempFiles = new File(platform, "tempFiles");
5910             File dbDir = new File(tempFiles, "db");
5911             dbDir.mkdirs();
5912             File file = new File(dbDir, "ResourceFile" + resource.getResourceId());
5913             ResourceData rd = new ResourceData(new BinaryFile(file), false);
5914             Binding binding = Bindings.getBinding(datatype);
5915             if (null == initialValue) {
5916                 initialValue = binding.createDefault();
5917             }
5918             Serializer serializer = binding.serializer();
5919             byte[] bytes = serializer.serialize(initialValue);
5920             rd.binaryFile.write(bytes);
5921             ravs.put(resource, rd);
5922             return rd;
5923         } catch (Exception e) {
5924             if (e instanceof DatabaseException)
5925                 throw (DatabaseException)e;
5926             else
5927                 throw new DatabaseException(e);
5928         }
5929     }
5930
5931 //    static class ExternalValueRequest<T> extends ResourceRead<T> {
5932 //
5933 //              public ExternalValueRequest(Resource resource) {
5934 //                      super(resource);
5935 //              }
5936 //
5937 //              @SuppressWarnings("unchecked")
5938 //              @Override
5939 //              public T perform(ReadGraph graph) throws DatabaseException {
5940 //              try {
5941 //                      
5942 //                      String uri = graph.getURI(resource);
5943 //                      if(Layer0.URIs.Functions_functionApplication.equals(uri)) return (T)functionApplication;
5944 //                      
5945 //                      return (T)ReflectionUtils.getValue(uri).getValue();
5946 //                      
5947 //              } catch(ValueNotFoundException e) {
5948 //                      throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
5949 //              } catch(ClassCastException e) {
5950 //                      throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
5951 //              }
5952 //              }
5953 //      
5954 //    }
5955     
5956     @SuppressWarnings("unchecked")
5957     @Override
5958     public <T> T getValue2(Resource r, Object context) throws DatabaseException {
5959         Layer0 L0 = processor.getL0(this);
5960         Set<Resource> types = getTypes(r);
5961         
5962         if(types.contains(L0.Literal)) {
5963                 if(isImmutable(r)) {
5964                         return syncRequest(new ValueImplied<T>(r));
5965                 } else {
5966                         return getValue(r);
5967                 }
5968         }
5969         else if(types.contains(L0.ExternalValue)) {
5970                 return (T)syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
5971         }
5972         else {
5973
5974             Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
5975             if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
5976             try {
5977                 return function.apply(this, r, context);
5978             } catch(RuntimeException e) {
5979                 DatabaseException dte = findPossibleRootException(e);
5980                 if(dte != null) throw dte;
5981                 else throw new DatabaseException(e);
5982             }
5983                 
5984         }
5985     }
5986
5987     @Override
5988     public Variant getVariantValue2(Resource r, Object context) throws DatabaseException {
5989         Layer0 L0 = processor.getL0(this);
5990         Set<Resource> types = getTypes(r);
5991         
5992         if(types.contains(L0.Literal)) {
5993             if(isImmutable(r)) {
5994                 return syncRequest(new VariantValueImplied(r));
5995             } else {
5996                 return getVariantValue(r);
5997             }
5998         }
5999         else if(types.contains(L0.ExternalValue)) {
6000             Object value = syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6001             try {
6002                 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6003             } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6004                 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6005             }
6006         }
6007         else {
6008
6009             Function3<ReadGraph,Resource,Object,Object> function = requestValueFunction(r);
6010             if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6011             try {
6012                 Object value = function.apply(this, r, context);
6013                 try {
6014                     return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6015                 } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6016                     throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6017                 }
6018             } catch(RuntimeException e) {
6019                 DatabaseException dte = findPossibleRootException(e);
6020                 if(dte != null) throw dte;
6021                 else throw new DatabaseException(e);
6022             }
6023         }
6024     }
6025     
6026     @Override
6027     public <T> T getPossibleValue2(Resource subject, Object context) throws DatabaseException {
6028         try {
6029                 return getValue2(subject, context);
6030         } catch (DatabaseException e) {
6031                 return null;
6032         }
6033     }
6034
6035     static class PossibleConverterFunction<T> extends ResourceRead<Function3<ReadGraph,Resource,Object,T>> {
6036         
6037         public PossibleConverterFunction(Resource resource) {
6038                         super(resource);
6039                 }
6040
6041         @Override
6042         public Function3<ReadGraph,Resource,Object,T> perform(ReadGraph graph) throws DatabaseException {
6043                 return compute(graph, resource);
6044         }
6045         
6046         @SuppressWarnings("unchecked")
6047                 public static <T> Function3<ReadGraph,Resource,Object,T> compute(ReadGraph graph, Resource resource) throws DatabaseException {
6048             Layer0 L0 = Layer0.getInstance(graph);
6049             for(Resource converter : graph.getObjects(resource, L0.ConvertsToValueWith)) {
6050                 try {
6051                         if(L0.Functions_functionApplication.equals(converter)) {
6052                                 return (Function3<ReadGraph,Resource,Object,T>)graph.syncRequest(new AdaptValue(resource));
6053                         } else {
6054                                 return graph.getValue2(converter, resource);
6055                         }
6056                 } catch(RuntimeException e) {
6057                     DatabaseException dte = findPossibleRootException(e);
6058                     if(dte != null) throw dte;
6059                     else throw new DatabaseException(e);
6060                 }
6061             }
6062             return null;
6063         }
6064         
6065     }
6066     
6067     <T> Function3<ReadGraph,Resource,Object,T> requestValueFunction(Resource r) throws DatabaseException {
6068         if(isImmutable(r))
6069                 return syncRequest(new PossibleConverterFunction<T>(r), TransientCacheAsyncListener.<Function3<ReadGraph,Resource,Object,T>>instance());
6070         else
6071                 return syncRequest(new PossibleConverterFunction<T>(r));
6072     }
6073     
6074     /**
6075      * Get a value associated with a graph {@link Resource}, using a possible context object and
6076      * a desired value binding. The following methods are tried in order to retreive the value:
6077      * <ol>
6078      *   <li>If the given resource is a {@code L0.Literal}, the value of the literal is returned, using the binding specified by {@code binding}.</li>
6079      *   <li>If the resource is a {@code L0.ExternalValue}, the value is acquired using
6080      *       {@link ReflectionUtils#getValue(String)}.</li>
6081      *   <li>If the resource is associated with a suitable value converter with relation {@code L0.ConvertsToValueWith}
6082      *       (see {@link #requestValueFunction(Resource)}), the value function is called with the graph, the resource
6083      *       and the context object.</li>
6084      * </ul>
6085      * 
6086      * @param r  A graph resource with which the value is associated
6087      * @param context  A context object that is used for acquiring the value (only applied in case 3)
6088      * @param binding  A binding for the value type (only applied in case 1)
6089      * @return  The value of the graph node.
6090      * @throws DoesNotContainValueException  No value is associated with the graph node.
6091      * @throws DatabaseException  Other errors, such as an error in casting the value to the return type or
6092      *         a runtime error in the value function.
6093      */
6094     @SuppressWarnings("unchecked")
6095         @Override
6096     public <T> T getValue2(Resource r, Object context, Binding binding) throws DatabaseException {
6097         if (binding instanceof ObjectVariantBinding)
6098                 return getValue2(r, context);
6099         
6100         Layer0 L0 = processor.getL0(this);
6101         Set<Resource> types = getTypes(r);
6102         if(types.contains(L0.Literal)) {
6103                 if(isImmutable(r)) {
6104                         return syncRequest(new Value<T>(r, binding));
6105                 } else {
6106                         return getValue(r, binding);
6107                 }
6108         } else if(types.contains(L0.ExternalValue)) {
6109                 try {
6110                         return (T)ReflectionUtils.getValue(getURI(r)).getValue();
6111                 } catch(ValueNotFoundException e) {
6112                         throw new DatabaseException(e);
6113                 } catch(ClassCastException e) {
6114                         throw new DatabaseException(e);
6115                 }
6116         }
6117         else {
6118             Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6119             if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function.");
6120             try {
6121                 Object value = function.apply(this, r, context);
6122                 if(binding.isInstance(value)) return (T)value;
6123                 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
6124                 return (T)Bindings.adapt(value, srcBinding, binding);
6125             } catch(RuntimeException e) {
6126                 DatabaseException dte = findPossibleRootException(e);
6127                 if(dte != null) throw dte;
6128                 else throw new DatabaseException(e);
6129             } catch (AdaptException e) {
6130                 throw new DatabaseException(e);
6131                         } catch (org.simantics.databoard.binding.error.BindingException e) {
6132                 throw new DatabaseException(e);
6133                         }
6134         }
6135     }
6136     
6137     @Override
6138     public <T> T getPossibleValue2(Resource subject, Object context, Binding binding) throws DatabaseException {
6139         try {
6140                 return getValue2(subject, context, binding);
6141         } catch (DatabaseException e) {
6142                 return null;
6143         }
6144     }
6145     
6146         private static DatabaseException findPossibleRootException(Throwable t) {
6147                 if(t == null) return null;
6148                 if(t instanceof DatabaseException) return (DatabaseException)t;
6149                 if(t instanceof RuntimeException || t instanceof InvocationTargetException) {
6150                         return findPossibleRootException(t.getCause());
6151                 }
6152                 return null;
6153         }
6154     
6155     @Override
6156     public <T> T getRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6157         return getRelatedValue2(subject, relation, subject);
6158     }
6159     
6160     @Override
6161     public Variant getRelatedVariantValue2(Resource subject, Resource relation) throws DatabaseException {
6162         return getRelatedVariantValue2(subject, relation, subject);
6163     }
6164     
6165     @Override
6166     public <T> T getPossibleRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6167         try {
6168                 Resource object = getPossibleObject(subject, relation);
6169                 if(object == null) return null;
6170                 else return getValue2(object, subject);
6171         } catch (DatabaseException e) {
6172                 return null;
6173         }
6174     }
6175
6176     @Override
6177     public <T> T getRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6178                 if(Development.DEVELOPMENT) {
6179                         String error = L0Validations.checkValueType(this, subject, relation);
6180                         if(error != null) {
6181                                 Logger.defaultLogError(new ValidationException(error));
6182                                 //throw new ValidationException(error);
6183                                 new ValidationException(error).printStackTrace();
6184                         }
6185                 }
6186         return getValue2(getSingleObject(subject, relation), context);
6187     }
6188     
6189     @Override
6190     public Variant getRelatedVariantValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6191         if(Development.DEVELOPMENT) {
6192             String error = L0Validations.checkValueType(this, subject, relation);
6193             if(error != null) {
6194                 Logger.defaultLogError(new ValidationException(error));
6195                 //throw new ValidationException(error);
6196                 new ValidationException(error).printStackTrace();
6197             }
6198         }
6199         return getVariantValue2(getSingleObject(subject, relation), context);
6200     }
6201     
6202     @Override
6203     public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6204         try {
6205                 Resource object = getPossibleObject(subject, relation);
6206                 if(object == null) return null;
6207                 else return getValue2(object, context);
6208         } catch (DatabaseException e) {
6209                 return null;
6210         }
6211     }
6212
6213     @Override
6214     public <T> T getRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6215         return getRelatedValue2(subject, relation, subject, binding);
6216     }
6217     
6218     @Override
6219     public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6220         try {
6221                 Resource object = getPossibleObject(subject, relation);
6222                 if(object == null) return null;
6223                 return getValue2(object, subject, binding);
6224         } catch (DatabaseException e) {
6225                 return null;
6226         }
6227     }
6228
6229     @Override
6230     public <T> T getRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6231         return getValue2(getSingleObject(subject, relation), context, binding);
6232     }
6233     
6234     @Override
6235     public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6236         try {
6237                 Resource object = getPossibleObject(subject, relation);
6238                 if(object == null) return null;
6239                 else return getValue2(object, context, binding);
6240         } catch (DatabaseException e) {
6241                 return null;
6242         }
6243     }
6244     
6245     @Override
6246     public Type getRelatedValueType(Resource subject, Resource relation) throws DatabaseException {
6247         Layer0 L0 = processor.getL0(this);
6248         Resource property = getSingleObject(subject, relation);
6249         String typeText = (String)getRelatedValue(property, L0.HasValueType, Bindings.STRING);
6250         try {
6251             return org.simantics.scl.compiler.types.Types.parseType(typeText);
6252         } catch (SCLTypeParseException e) {
6253             throw new DatabaseException(e);
6254         }
6255     }
6256
6257     private static ThreadLocal<Boolean> syncGraph = new ThreadLocal<Boolean>() {
6258         protected Boolean initialValue() {
6259             return true;
6260         }
6261     };
6262
6263     @Override
6264     public boolean setSynchronous(boolean value) {
6265         boolean old = getSynchronous();
6266         syncGraph.set(value);
6267         return old;
6268     }
6269
6270     @Override
6271     public boolean getSynchronous() {
6272         return syncGraph.get();
6273     }
6274     
6275     public void ensureLoaded(int resource) {
6276         processor.querySupport.ensureLoaded(this, resource);
6277     }
6278     
6279     public void ensureLoaded(int resource, int predicate) {
6280         processor.querySupport.ensureLoaded(this, resource, predicate);
6281     }
6282
6283     public byte[] getValue(int resource) {
6284         return processor.querySupport.getValue(this, resource);
6285     }
6286     
6287     public int thread(int resource) {
6288         return (resource >>> 16) & processor.THREAD_MASK;
6289     }
6290
6291     public int thread(Resource resource) {
6292         return (((ResourceImpl)resource).id >>> 16) & processor.THREAD_MASK;
6293     }
6294     
6295     public ResourceSupport getResourceSupport() {
6296         return processor.getResourceSupport();
6297     }
6298     
6299     @Override
6300     public Object getModificationCounter() {
6301         return processor.getSession().getModificationCounter();
6302     }
6303
6304     @Override
6305     public boolean performPending() {
6306         return processor.performPending(this);
6307     }
6308
6309     public Set<ReadGraphImpl> ancestorSet() {
6310         HashSet<ReadGraphImpl> result = new HashSet<>();
6311         ReadGraphImpl g = this;
6312         while(g != null) {
6313             result.add(g);
6314             g = g.parentGraph;
6315         }
6316         return result;
6317     }
6318
6319 }