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