]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/graph/ReadGraphImpl.java
Wrong graph was used when performing async query from session
[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                 T result = (T)QueryCache.runnerReadEntry(this, request, parent, null, null, true);
1930                 return result;
1931                 
1932         }
1933
1934         @Override
1935         public <T> T syncRequest(Read<T> request, SyncListener<T> procedure)
1936                         throws DatabaseException {
1937                 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
1938         }
1939
1940         @Override
1941         public <T> T syncRequest(Read<T> request, final Listener<T> procedure)
1942                         throws DatabaseException {
1943                 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
1944         }
1945
1946         @Override
1947         public <T> T syncRequest(final Read<T> request, final AsyncProcedure<T> procedure) throws DatabaseException {
1948
1949                 assert (request != null);
1950
1951                 ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
1952                 T result = (T)QueryCache.runnerReadEntry(this, request, parent, listener, procedure, true);
1953                 return result;
1954
1955         }
1956
1957         @Override
1958         public <T> T syncRequest(final Read<T> request,
1959                         final SyncProcedure<T> procedure) throws DatabaseException {
1960                 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
1961         }
1962
1963         @Override
1964         public <T> T syncRequest(Read<T> request, Procedure<T> procedure)
1965                         throws DatabaseException {
1966                 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
1967         }
1968
1969         static class AsyncReadProcedure<T> implements AsyncProcedure<T> {
1970
1971                 private static Throwable DONE = new Throwable();
1972                 
1973                 T result = null;
1974                 Throwable exception = null;
1975                 
1976                 @Override
1977                 public void execute(AsyncReadGraph graph, T t) {
1978                         result = t;
1979                         exception = DONE;
1980                 }
1981
1982                 @Override
1983                 public void exception(AsyncReadGraph graph, Throwable t) {
1984                         exception = t;
1985                 }
1986                 
1987                 public void checkAndThrow() throws DatabaseException {
1988                         if(exception != DONE) {
1989                                 if (exception instanceof DatabaseException)
1990                                         throw (DatabaseException) exception;
1991                                 else
1992                                         throw new DatabaseException(
1993                                                         "Unexpected exception in ReadGraph.syncRequest(AsyncRead)",
1994                                                         exception);
1995                         }
1996                 }
1997                 
1998                 public boolean done() {
1999                         return exception != null;
2000                 }
2001                 
2002         }
2003         
2004         @Override
2005         public <T> T syncRequest(final AsyncRead<T> request)
2006                         throws DatabaseException {
2007
2008                 assert (request != null);
2009                 return syncRequest(request, new AsyncProcedureAdapter<>() );
2010
2011         }
2012
2013         @Override
2014         public <T> T syncRequest(AsyncRead<T> request, AsyncListener<T> procedure)
2015                         throws DatabaseException {
2016                 return syncRequest(request, (AsyncProcedure<T>) procedure);
2017         }
2018
2019         @Override
2020         public <T> T syncRequest(AsyncRead<T> request, SyncListener<T> procedure)
2021                         throws DatabaseException {
2022                 return syncRequest(request, new SyncToAsyncListener<T>(procedure));
2023         }
2024
2025         @Override
2026         public <T> T syncRequest(AsyncRead<T> request, Listener<T> procedure)
2027                         throws DatabaseException {
2028                 return syncRequest(request, new NoneToAsyncListener<T>(procedure));
2029         }
2030
2031         @Override
2032         final public <T> T syncRequest(final AsyncRead<T> request,
2033                         final AsyncProcedure<T> procedure) throws DatabaseException {
2034
2035                 assert (request != null);
2036
2037                 ListenerBase listener = getListenerBase(procedure);
2038                 T result = (T)QueryCache.runnerAsyncReadEntry(this, request, parent, listener, procedure, true); 
2039                 return result;
2040
2041         }
2042
2043         @Override
2044         public <T> T syncRequest(AsyncRead<T> request,
2045                         final SyncProcedure<T> procedure) throws DatabaseException {
2046                 return syncRequest(request, new SyncToAsyncProcedure<T>(procedure));
2047         }
2048
2049         @Override
2050         final public <T> T syncRequest(final AsyncRead<T> request,
2051                         final Procedure<T> procedure) throws DatabaseException {
2052                 return syncRequest(request, new NoneToAsyncProcedure<T>(procedure));
2053         }
2054
2055         @Override
2056         public <T> Collection<T> syncRequest(final MultiRead<T> request)
2057                         throws DatabaseException {
2058
2059                 assert (request != null);
2060
2061                 final ArrayList<T> result = new ArrayList<T>();
2062                 final DataContainer<Throwable> exception = new DataContainer<Throwable>();
2063
2064                 syncRequest(request, new SyncMultiProcedure<T>() {
2065
2066                         @Override
2067                         public void execute(ReadGraph graph, T t) {
2068                                 synchronized (result) {
2069                                         result.add(t);
2070                                 }
2071                         }
2072
2073                         @Override
2074                         public void finished(ReadGraph graph) {
2075                         }
2076
2077                         @Override
2078                         public void exception(ReadGraph graph, Throwable t) {
2079                                 exception.set(t);
2080                         }
2081
2082                         @Override
2083                         public String toString() {
2084                                 return "syncRequest(MultiRead) -> " + request;
2085                         }
2086
2087                 });
2088
2089                 Throwable t = exception.get();
2090                 if (t != null) {
2091                         if (t instanceof DatabaseException)
2092                                 throw (DatabaseException) t;
2093                         else
2094                                 throw new DatabaseException(
2095                                                 "Unexpected exception in ReadGraph.syncRequest(Read)",
2096                                                 t);
2097                 }
2098
2099                 return result;
2100
2101         }
2102
2103         @Override
2104         public <T> Collection<T> syncRequest(MultiRead<T> request,
2105                         SyncMultiListener<T> procedure) {
2106                 return syncRequest(request, (SyncMultiProcedure<T>)procedure);
2107         }
2108
2109         @Override
2110         public <T> Collection<T> syncRequest(MultiRead<T> request,
2111                         MultiListener<T> procedure) {
2112                 return syncRequest(request, new NoneToSyncMultiListener<T>(procedure));
2113         }
2114
2115         @Override
2116         public <T> Collection<T> syncRequest(MultiRead<T> request,
2117                         SyncMultiProcedure<T> procedure) {
2118
2119                 assert (request != null);
2120
2121                 ListenerBase listener = getListenerBase(procedure);
2122
2123                 final ResultCallWrappedSyncQueryProcedure<T> wrapper = new ResultCallWrappedSyncQueryProcedure<T>(procedure);
2124
2125                 if (parent != null || listener != null) {
2126
2127 //                      Object syncParent = request;
2128
2129 //                      final ReadGraphImpl newGraph = newSync();
2130
2131                         processor.query(this, request, parent, wrapper, listener);
2132
2133 //                      newGraph.waitAsync(syncParent);
2134
2135                 } else {
2136
2137 //                      Object syncParent = request;
2138
2139 //                      final ReadGraphImpl newGraph = newSync();
2140
2141                         try {
2142                                 request.perform(this, wrapper);
2143                         } catch (Throwable t) {
2144                                 wrapper.exception(this, t);
2145                         }
2146
2147                 }
2148
2149                 return wrapper.get();
2150
2151         }
2152
2153         @Override
2154         public <T> Collection<T> syncRequest(MultiRead<T> request,
2155                         MultiProcedure<T> procedure) {
2156                 return syncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
2157         }
2158
2159         static class AsyncMultiReadProcedure<T> extends ArrayList<T> implements AsyncMultiProcedure<T> {
2160
2161                 private static Throwable DONE = new Throwable();
2162                 
2163                 private static final long serialVersionUID = -6494230465108115812L;
2164                 
2165                 Throwable exception = null;
2166                 
2167                 @Override
2168                 public synchronized void execute(AsyncReadGraph graph, T t) {
2169                         add(t);
2170                 }
2171
2172                 @Override
2173                 public void finished(AsyncReadGraph graph) {
2174                         exception = DONE;
2175                 }
2176
2177                 @Override
2178                 public void exception(AsyncReadGraph graph, Throwable t) {
2179                         exception = t;
2180                 }
2181                 
2182                 public void checkAndThrow() throws DatabaseException {
2183                         if(exception != DONE) {
2184                                 if (exception instanceof DatabaseException)
2185                                         throw (DatabaseException) exception;
2186                                 else
2187                                         throw new DatabaseException(
2188                                                         "Unexpected exception in ReadGraph.syncRequest(AsyncMultiRead)",
2189                                                         exception);
2190                         }
2191                 }
2192                 
2193                 public boolean done() {
2194                         return exception != null;
2195                 }
2196                 
2197         }
2198
2199         @Override
2200         final public <T> Collection<T> syncRequest(AsyncMultiRead<T> request)
2201                         throws DatabaseException {
2202
2203                 assert (request != null);
2204
2205                 final AsyncMultiReadProcedure<T> procedure = new AsyncMultiReadProcedure<T>();
2206                 
2207                 syncRequest(request, procedure);
2208                 
2209                 procedure.checkAndThrow();
2210                 return procedure;
2211
2212         }
2213
2214         @Override
2215         public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2216                         AsyncMultiListener<T> procedure) {
2217                 return syncRequest(request, (AsyncMultiProcedure<T>) procedure);
2218         }
2219
2220         @Override
2221         public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2222                         SyncMultiListener<T> procedure) {
2223                 return syncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
2224         }
2225
2226         @Override
2227         public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2228                         MultiListener<T> procedure) {
2229                 return syncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
2230         }
2231
2232         final private <T> void syncRequest(final AsyncMultiRead<T> request,
2233                         final AsyncMultiReadProcedure<T> procedure) {
2234
2235                 assert (request != null);
2236                 assert (procedure != null);
2237
2238                 ListenerBase listener = getListenerBase(procedure);
2239
2240                 if (parent != null || listener != null) {
2241
2242 //                      Object syncParent = request;
2243
2244 //                      final ReadGraphImpl newGraph = newSync();
2245
2246                         processor.query(this, request, parent, procedure, listener);
2247
2248 //                      newGraph.waitAsync(syncParent);
2249                         waitAsyncProcedure(procedure);
2250
2251                 } else {
2252
2253 //                      Object syncParent = callerThread == Integer.MIN_VALUE ? null
2254 //                                      : request;
2255 //
2256 //                      final ReadGraphImpl newGraph = newSyncAsync(syncParent);
2257
2258                         try {
2259
2260 //                              inc();
2261 //                              ReadGraphImpl sync = newSync();
2262                                 request.perform(this, procedure);
2263 //                              sync.waitAsync(null);
2264                                 waitAsyncProcedure(procedure);
2265 //                              dec();
2266
2267                         } catch (Throwable t) {
2268
2269                                 waitAsyncProcedure(procedure);
2270 //                              dec();
2271
2272                         }
2273
2274                 }
2275
2276         }
2277         
2278         
2279         @Override
2280         final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2281                         final AsyncMultiProcedure<T> procedure) {
2282
2283                 assert (request != null);
2284                 assert (procedure != null);
2285
2286                 ListenerBase listener = getListenerBase(procedure);
2287
2288                 if (parent != null || listener != null) {
2289
2290 //                      Object syncParent = request;
2291
2292 //                      final ReadGraphImpl newGraph = newSync();
2293
2294                         processor.query(this, request, parent, procedure, listener);
2295
2296 //                      newGraph.waitAsync(syncParent);
2297
2298                 } else {
2299
2300 //                      Object syncParent = request;
2301
2302 //                      final ReadGraphImpl newGraph = newSync();
2303
2304                         try {
2305
2306                                 request.perform(this, new AsyncMultiProcedure<T>() {
2307
2308                                         @Override
2309                                         public void execute(AsyncReadGraph graph, T result) {
2310                                                 procedure.execute(graph, result);
2311                                         }
2312
2313                                         @Override
2314                                         public void finished(AsyncReadGraph graph) {
2315                                                 procedure.finished(graph);
2316                                         }
2317
2318                                         @Override
2319                                         public void exception(AsyncReadGraph graph, Throwable t) {
2320                                                 procedure.exception(graph, t);
2321                                         }
2322
2323                                         @Override
2324                                         public String toString() {
2325                                                 return "syncRequest(AsyncMultiRead) -> " + procedure;
2326                                         }
2327
2328                                 });
2329
2330                         } catch (Throwable t) {
2331
2332                         }
2333
2334                 }
2335
2336                 // TODO!!
2337                 return null;
2338
2339         }
2340
2341         @Override
2342         public <T> Collection<T> syncRequest(AsyncMultiRead<T> request,
2343                         final SyncMultiProcedure<T> procedure) {
2344                 return syncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
2345         }
2346
2347         @Override
2348         final public <T> Collection<T> syncRequest(final AsyncMultiRead<T> request,
2349                         final MultiProcedure<T> procedure) {
2350                 return syncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
2351         }
2352
2353         @Override
2354         public <T> T syncRequest(final ExternalRead<T> request)
2355                         throws DatabaseException {
2356
2357                 assert (request != null);
2358
2359                 return syncRequest(request, new Procedure<T>() {
2360
2361                         @Override
2362                         public void execute(T t) {
2363                         }
2364
2365                         @Override
2366                         public void exception(Throwable t) {
2367                         }
2368
2369                         @Override
2370                         public String toString() {
2371                                 return "syncRequest(AsyncRead) -> " + request;
2372                         }
2373
2374                 });
2375
2376         }
2377
2378         @Override
2379         public <T> T syncRequest(ExternalRead<T> request, Listener<T> procedure) throws DatabaseException {
2380                 return syncRequest(request, (Procedure<T>) procedure);
2381         }
2382
2383         @Override
2384         final public <T> T syncRequest(final ExternalRead<T> request,
2385                         final Procedure<T> procedure) throws DatabaseException {
2386
2387         assert (request != null);
2388
2389         ListenerBase listener = procedure != null ? getListenerBase(procedure) : null;
2390         return QueryCache.resultExternalReadEntry(this, request, parent, listener, procedure);
2391
2392         }
2393
2394         @Override
2395         public void syncRequest(final Write request) throws DatabaseException {
2396
2397                 assert (request != null);
2398
2399                 throw new DatabaseException(
2400                                 "Write operations are not supported during read transactions!");
2401
2402         }
2403
2404         @Override
2405         public <T> T syncRequest(final WriteResult<T> request) throws DatabaseException {
2406
2407                 assert (request != null);
2408
2409                 throw new DatabaseException(
2410                                 "Write operations are not supported during read transactions!");
2411
2412         }
2413
2414         @Override
2415         public void syncRequest(final DelayedWrite request)
2416                         throws DatabaseException {
2417
2418                 assert (request != null);
2419
2420                 throw new DatabaseException(
2421                                 "Write operations are not supported during read transactions!");
2422
2423         }
2424
2425         @Override
2426         public <T> T syncRequest(final DelayedWriteResult<T> request) throws DatabaseException {
2427
2428                 assert (request != null);
2429
2430                 throw new DatabaseException(
2431                                 "Write operations are not supported during read transactions!");
2432
2433         }
2434         
2435         @Override
2436         public void syncRequest(final WriteOnly request) throws DatabaseException {
2437
2438                 assert (request != null);
2439
2440                 throw new DatabaseException(
2441                                 "Write operations are not supported during read transactions!");
2442
2443         }
2444
2445         @Override
2446         public <T> T syncRequest(final WriteOnlyResult<T> request) throws DatabaseException {
2447
2448                 assert (request != null);
2449
2450                 throw new DatabaseException(
2451                                 "Write operations are not supported during read transactions!");
2452
2453         }
2454         
2455         @Override
2456         public <T> void async(ReadInterface<T> r, AsyncProcedure<T> procedure) {
2457                 r.request(this, procedure);
2458         }
2459         
2460         @Override
2461         public <T> void async(ReadInterface<T> r, Procedure<T> procedure) {
2462                 r.request(this, procedure);
2463         }
2464         
2465         @Override
2466         public <T> void async(ReadInterface<T> r, SyncProcedure<T> procedure) {
2467                 r.request(this, procedure);
2468         }
2469         
2470         @Override
2471         public <T> void async(ReadInterface<T> r, AsyncListener<T> procedure) {
2472                 r.request(this, procedure);
2473         }
2474         
2475         @Override
2476         public <T> void async(ReadInterface<T> r, Listener<T> procedure) {
2477                 r.request(this, procedure);
2478         }
2479         
2480         @Override
2481         public <T> void async(ReadInterface<T> r, SyncListener<T> procedure) {
2482                 r.request(this, procedure);
2483         }
2484
2485         @Override
2486         public <T> T sync(ReadInterface<T> r) throws DatabaseException {
2487                 return r.request(this);
2488         }
2489         
2490         @Override
2491         public <T> T sync(WriteInterface<T> r) throws DatabaseException {
2492                 return r.request(this);
2493         }
2494         
2495         @Override
2496         public <T> void async(WriteInterface<T> r, Procedure<T> procedure) {
2497                 r.request(this, procedure);
2498         }
2499
2500         @Override
2501         public <T> void async(WriteInterface<T> r) {
2502                 r.request(this, new ProcedureAdapter<T>());
2503         }
2504
2505         /*
2506          * Implementation of the interface AsyncReadGraph
2507          */
2508
2509         @Override
2510         public void forURI(Resource resource, AsyncListener<String> listener) {
2511                 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2512                                 listener);
2513         }
2514
2515         @Override
2516         public void forURI(Resource resource, SyncListener<String> listener) {
2517                 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2518                                 listener);
2519         }
2520
2521         @Override
2522         public void forURI(Resource resource, Listener<String> listener) {
2523                 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2524                                 listener);
2525         }
2526
2527         @Override
2528         final public void forURI(final Resource resource,
2529                         final AsyncProcedure<String> procedure) {
2530
2531                 assert (resource != null);
2532                 assert (procedure != null);
2533
2534                 asyncRequest(new org.simantics.db.common.uri.ResourceToURI(resource),
2535                                 procedure);
2536
2537         }
2538
2539         @Override
2540         public void forURI(Resource resource, SyncProcedure<String> procedure) {
2541                 forURI(resource, new SyncToAsyncProcedure<String>(procedure));
2542         }
2543
2544         @Override
2545         public void forURI(Resource resource, Procedure<String> procedure) {
2546                 forURI(resource, new NoneToAsyncProcedure<String>(procedure));
2547         }
2548         
2549         @Override
2550         public void forResource(String id, AsyncListener<Resource> listener) {
2551                 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2552                                 listener);
2553         }
2554
2555         @Override
2556         public void forResource(String id, SyncListener<Resource> listener) {
2557                 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2558                                 listener);
2559         }
2560
2561         @Override
2562         public void forResource(String id, Listener<Resource> listener) {
2563                 asyncRequest(new org.simantics.db.common.primitiverequest.Resource(id),
2564                                 listener);
2565         }
2566
2567         @Override
2568         final public void forResource(final String id,
2569                         final AsyncProcedure<Resource> procedure) {
2570
2571                 assert (id != null);
2572                 assert (procedure != null);
2573
2574                 processor.forResource(this, id, procedure);
2575
2576         }
2577
2578         @Override
2579         public void forResource(String id, SyncProcedure<Resource> procedure) {
2580                 forResource(id, new SyncToAsyncProcedure<Resource>(procedure));
2581         }
2582
2583         @Override
2584         public void forResource(String id, Procedure<Resource> procedure) {
2585                 forResource(id, new NoneToAsyncProcedure<Resource>(procedure));
2586         }
2587
2588         @Override
2589         public void forBuiltin(String id, AsyncListener<Resource> listener) {
2590                 asyncRequest(new Builtin(id), listener);
2591         }
2592
2593         @Override
2594         public void forBuiltin(String id, SyncListener<Resource> listener) {
2595                 asyncRequest(new Builtin(id), listener);
2596         }
2597
2598         @Override
2599         public void forBuiltin(String id, Listener<Resource> listener) {
2600                 asyncRequest(new Builtin(id), listener);
2601         }
2602
2603         @Override
2604         final public void forBuiltin(final String id,
2605                         final AsyncProcedure<Resource> procedure) {
2606
2607                 assert (id != null);
2608                 assert (procedure != null);
2609
2610                 processor.forBuiltin(this, id, procedure);
2611
2612         }
2613
2614         @Override
2615         public void forBuiltin(String id, SyncProcedure<Resource> procedure) {
2616                 forBuiltin(id, new SyncToAsyncProcedure<Resource>(procedure));
2617         }
2618
2619         @Override
2620         public void forBuiltin(String id, Procedure<Resource> procedure) {
2621                 forBuiltin(id, new NoneToAsyncProcedure<Resource>(procedure));
2622         }
2623
2624         @Override
2625         final public void forEachStatement(Resource subject, Resource relation,
2626                         AsyncMultiProcedure<Statement> procedure) {
2627
2628                 assert (subject != null);
2629                 assert (relation != null);
2630                 assert (procedure != null);
2631
2632                 processor.forEachStatement(this, subject, relation, procedure);
2633
2634         }
2635
2636         @Override
2637         public void forEachStatement(Resource subject, Resource relation,
2638                         SyncMultiProcedure<Statement> procedure) {
2639                 forEachStatement(subject, relation,
2640                                 new SyncToAsyncMultiProcedure<Statement>(procedure));
2641         }
2642
2643         @Override
2644         final public void forEachStatement(Resource subject, Resource relation,
2645                         MultiProcedure<Statement> procedure) {
2646
2647                 assert (subject != null);
2648                 assert (relation != null);
2649                 assert (procedure != null);
2650
2651                 processor.forEachStatement(this, subject, relation, procedure);
2652
2653         }
2654
2655         @Override
2656         final public void forStatementSet(Resource subject, Resource relation,
2657                         AsyncSetListener<Statement> procedure) {
2658
2659                 assert (subject != null);
2660                 assert (relation != null);
2661                 assert (procedure != null);
2662
2663                 processor.forStatementSet(this, subject, relation, procedure);
2664
2665         }
2666
2667         @Override
2668         final public void forStatementSet(Resource subject, Resource relation,
2669                         SyncSetListener<Statement> procedure) {
2670                 forStatementSet(subject, relation,
2671                                 new SyncToAsyncSetProcedure<Statement>(procedure));
2672         }
2673
2674         @Override
2675         public void forStatementSet(Resource subject, Resource relation,
2676                         SetListener<Statement> listener) {
2677                 forStatementSet(subject, relation,
2678                                 new NoneToAsyncSetProcedure<Statement>(listener));
2679         }
2680
2681         @Override
2682         final public void forEachAssertedStatement(final Resource subject,
2683                         final Resource relation,
2684                         final AsyncMultiProcedure<Statement> procedure) {
2685
2686                 assert (subject != null);
2687                 assert (relation != null);
2688                 assert (procedure != null);
2689
2690                 processor.forEachAssertedStatement(this, subject, relation, procedure);
2691
2692         }
2693
2694         @Override
2695         public void forEachAssertedStatement(Resource subject, Resource relation,
2696                         SyncMultiProcedure<Statement> procedure) {
2697                 forEachAssertedStatement(subject, relation,
2698                                 new SyncToAsyncMultiProcedure<Statement>(procedure));
2699         }
2700
2701         @Override
2702         public void forEachAssertedStatement(Resource subject, Resource relation,
2703                         MultiProcedure<Statement> procedure) {
2704                 forEachAssertedStatement(subject, relation,
2705                                 new NoneToAsyncMultiProcedure<Statement>(procedure));
2706         }
2707
2708         @Override
2709         public void forAssertedStatementSet(Resource subject, Resource relation,
2710                         AsyncSetListener<Statement> procedure) {
2711
2712                 assert (subject != null);
2713                 assert (relation != null);
2714                 assert (procedure != null);
2715
2716                 processor.forAssertedStatementSet(this, subject, relation, procedure);
2717
2718         }
2719
2720         @Override
2721         public void forAssertedStatementSet(Resource subject, Resource relation,
2722                         SyncSetListener<Statement> procedure) {
2723
2724                 assert (subject != null);
2725                 assert (relation != null);
2726                 assert (procedure != null);
2727
2728                 forAssertedStatementSet(subject, relation,
2729                                 new SyncToAsyncSetProcedure<Statement>(procedure));
2730
2731         }
2732
2733         @Override
2734         public void forAssertedStatementSet(Resource subject, Resource relation,
2735                         SetListener<Statement> procedure) {
2736
2737                 assert (subject != null);
2738                 assert (relation != null);
2739                 assert (procedure != null);
2740
2741                 forAssertedStatementSet(subject, relation,
2742                                 new NoneToAsyncSetProcedure<Statement>(procedure));
2743
2744         }
2745
2746         @Override
2747         final public void forEachPredicate(final Resource subject,
2748                         final AsyncMultiProcedure<Resource> procedure) {
2749
2750                 assert (subject != null);
2751                 assert (procedure != null);
2752
2753                 processor.forEachPredicate(this, subject, procedure);
2754
2755         }
2756
2757         @Override
2758         public void forEachPredicate(Resource subject,
2759                         SyncMultiProcedure<Resource> procedure) {
2760                 forEachPredicate(subject, new SyncToAsyncMultiProcedure<Resource>(
2761                                 procedure));
2762         }
2763
2764         @Override
2765         final public void forEachPredicate(final Resource subject,
2766                         final MultiProcedure<Resource> procedure) {
2767
2768                 assert (subject != null);
2769                 assert (procedure != null);
2770
2771                 processor.forEachPredicate(this, subject, procedure);
2772
2773         }
2774
2775         @Override
2776         final public void forPredicateSet(final Resource subject,
2777                         final AsyncSetListener<Resource> procedure) {
2778
2779                 assert (subject != null);
2780                 assert (procedure != null);
2781
2782                 processor.forPredicateSet(this, subject, procedure);
2783
2784         }
2785
2786         @Override
2787         final public void forPredicateSet(final Resource subject,
2788                         final SyncSetListener<Resource> procedure) {
2789
2790                 assert (subject != null);
2791                 assert (procedure != null);
2792
2793                 forPredicateSet(subject, new SyncToAsyncSetProcedure<Resource>(
2794                                 procedure));
2795
2796         }
2797
2798         @Override
2799         final public void forPredicateSet(final Resource subject,
2800                         final SetListener<Resource> procedure) {
2801
2802                 assert (subject != null);
2803                 assert (procedure != null);
2804
2805                 forPredicateSet(subject, new NoneToAsyncSetProcedure<Resource>(
2806                                 procedure));
2807
2808         }
2809
2810         @Override
2811         final public void forEachPrincipalType(final Resource subject,
2812                         final AsyncMultiProcedure<Resource> procedure) {
2813
2814                 assert (subject != null);
2815                 assert (procedure != null);
2816
2817                 processor.forEachPrincipalType(this, subject, procedure);
2818
2819         }
2820
2821         @Override
2822         public void forEachPrincipalType(Resource subject,
2823                         SyncMultiProcedure<Resource> procedure) {
2824                 forEachPrincipalType(subject, new SyncToAsyncMultiProcedure<Resource>(
2825                                 procedure));
2826         }
2827
2828         @Override
2829         final public void forEachPrincipalType(final Resource subject,
2830                         final MultiProcedure<Resource> procedure) {
2831
2832                 assert (subject != null);
2833                 assert (procedure != null);
2834
2835                 processor.forEachPrincipalType(this, subject, procedure);
2836
2837         }
2838
2839         @Override
2840         final public void forPrincipalTypeSet(final Resource subject,
2841                         final AsyncSetListener<Resource> procedure) {
2842
2843                 assert (subject != null);
2844                 assert (procedure != null);
2845
2846                 processor.forPrincipalTypeSet(this, subject, procedure);
2847
2848         }
2849
2850         @Override
2851         final public void forPrincipalTypeSet(final Resource subject,
2852                         final SyncSetListener<Resource> procedure) {
2853
2854                 assert (subject != null);
2855                 assert (procedure != null);
2856
2857                 forPrincipalTypeSet(subject, new SyncToAsyncSetProcedure<Resource>(
2858                                 procedure));
2859
2860         }
2861
2862         @Override
2863         final public void forPrincipalTypeSet(final Resource subject,
2864                         final SetListener<Resource> procedure) {
2865
2866                 assert (subject != null);
2867                 assert (procedure != null);
2868
2869                 forPrincipalTypeSet(subject, new NoneToAsyncSetProcedure<Resource>(
2870                                 procedure));
2871
2872         }
2873
2874         @Override
2875         public void forTypes(Resource subject, AsyncListener<Set<Resource>> listener) {
2876                 asyncRequest(new Types(subject), listener);
2877         }
2878
2879         @Override
2880         public void forTypes(Resource subject, SyncListener<Set<Resource>> listener) {
2881                 asyncRequest(new Types(subject), listener);
2882         }
2883
2884         @Override
2885         public void forTypes(Resource subject, Listener<Set<Resource>> listener) {
2886                 asyncRequest(new Types(subject), listener);
2887         }
2888
2889         @Override
2890         final public void forTypes(final Resource subject,
2891                         final AsyncProcedure<Set<Resource>> procedure) {
2892
2893                 assert (subject != null);
2894                 assert (procedure != null);
2895
2896                 processor.forTypes(this, subject, procedure);
2897
2898         }
2899
2900         @Override
2901         public void forTypes(Resource subject,
2902                         SyncProcedure<Set<Resource>> procedure) {
2903                 forTypes(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
2904         }
2905
2906         @Override
2907         public void forTypes(Resource subject, Procedure<Set<Resource>> procedure) {
2908                 forTypes(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
2909         }
2910
2911         @Override
2912         public void forSupertypes(Resource subject,
2913                         AsyncListener<Set<Resource>> listener) {
2914                 asyncRequest(new Types(subject), listener);
2915         }
2916
2917         @Override
2918         public void forSupertypes(Resource subject,
2919                         SyncListener<Set<Resource>> listener) {
2920                 asyncRequest(new Types(subject), listener);
2921         }
2922
2923         @Override
2924         public void forSupertypes(Resource subject, Listener<Set<Resource>> listener) {
2925                 asyncRequest(new Types(subject), listener);
2926         }
2927
2928         @Override
2929         final public void forSupertypes(final Resource subject,
2930                         final AsyncProcedure<Set<Resource>> procedure) {
2931
2932                 assert (subject != null);
2933                 assert (procedure != null);
2934
2935                 processor.forSupertypes(this, subject, procedure);
2936
2937         }
2938
2939         @Override
2940         public void forSupertypes(Resource subject,
2941                         SyncProcedure<Set<Resource>> procedure) {
2942                 forSupertypes(subject, new SyncToAsyncProcedure<Set<Resource>>(
2943                                 procedure));
2944         }
2945
2946         @Override
2947         public void forSupertypes(Resource subject,
2948                         Procedure<Set<Resource>> procedure) {
2949                 forSupertypes(subject, new NoneToAsyncProcedure<Set<Resource>>(
2950                                 procedure));
2951         }
2952
2953         @Override
2954         public void forDirectSuperrelations(Resource subject,
2955                         AsyncMultiProcedure<Resource> procedure) {
2956                 
2957                 assert (subject != null);
2958                 assert (procedure != null);
2959
2960                 processor.forDirectSuperrelations(this, subject, procedure);
2961                 
2962         }
2963
2964         @Override
2965         public void forPossibleSuperrelation(Resource subject, AsyncProcedure<Resource> procedure) {
2966                 
2967                 assert (subject != null);
2968                 assert (procedure != null);
2969
2970                 processor.forPossibleSuperrelation(this, subject, procedure);
2971                 
2972         }
2973
2974         @Override
2975         public void forSuperrelations(Resource subject,
2976                         AsyncListener<Set<Resource>> listener) {
2977                 asyncRequest(new Types(subject), listener);
2978         }
2979
2980         @Override
2981         public void forSuperrelations(Resource subject,
2982                         SyncListener<Set<Resource>> listener) {
2983                 asyncRequest(new Types(subject), listener);
2984         }
2985
2986         @Override
2987         public void forSuperrelations(Resource subject,
2988                         Listener<Set<Resource>> listener) {
2989                 asyncRequest(new Types(subject), listener);
2990         }
2991
2992         @Override
2993         final public void forSuperrelations(final Resource subject,
2994                         final AsyncProcedure<Set<Resource>> procedure) {
2995
2996                 assert (subject != null);
2997                 assert (procedure != null);
2998
2999                 processor.forSuperrelations(this, subject, procedure);
3000
3001         }
3002
3003         @Override
3004         public void forSuperrelations(Resource subject,
3005                         SyncProcedure<Set<Resource>> procedure) {
3006                 forSuperrelations(subject, new SyncToAsyncProcedure<Set<Resource>>(
3007                                 procedure));
3008         }
3009
3010         @Override
3011         public void forSuperrelations(Resource subject,
3012                         Procedure<Set<Resource>> procedure) {
3013                 forSuperrelations(subject, new NoneToAsyncProcedure<Set<Resource>>(
3014                                 procedure));
3015         }
3016
3017         @Override
3018         final public void forEachObject(final Resource subject, final Resource relation, final AsyncMultiProcedure<Resource> procedure) {
3019                 processor.forEachObject(this, subject, relation, procedure);
3020         }
3021
3022         @Override
3023         public void forEachObject(Resource subject, Resource relation,
3024                         SyncMultiProcedure<Resource> procedure) {
3025                 forEachObject(subject, relation,
3026                                 new SyncToAsyncMultiProcedure<Resource>(procedure));
3027         }
3028
3029         @Override
3030         public void forEachObject(Resource subject, Resource relation,
3031                         MultiProcedure<Resource> procedure) {
3032
3033                 processor.forEachObject(this, subject, relation, procedure);
3034
3035         }
3036
3037         @Override
3038         final public void forEachDirectPredicate(final Resource subject, final AsyncProcedure<Set<Resource>> procedure) {
3039                 processor.forEachDirectPredicate(this, subject, procedure);
3040         }
3041
3042         @Override
3043         final public void forEachDirectPredicate(final Resource subject, final SyncProcedure<Set<Resource>> procedure) {
3044                 forEachDirectPredicate(subject, new SyncToAsyncProcedure<Set<Resource>>(procedure));
3045         }
3046
3047         @Override
3048         public void forEachDirectPredicate(Resource subject, Procedure<Set<Resource>> procedure) {
3049                 forEachDirectPredicate(subject, new NoneToAsyncProcedure<Set<Resource>>(procedure));
3050         }
3051
3052         @Override
3053         final public void forObjectSet(final Resource subject,
3054                         final Resource relation, final AsyncSetListener<Resource> procedure) {
3055
3056                 assert (subject != null);
3057                 assert (relation != null);
3058                 assert (procedure != null);
3059
3060                 processor.forObjectSet(this, subject, relation, procedure);
3061
3062         }
3063
3064         @Override
3065         final public void forObjectSet(final Resource subject,
3066                         final Resource relation, final SyncSetListener<Resource> procedure) {
3067
3068                 assert (subject != null);
3069                 assert (relation != null);
3070                 assert (procedure != null);
3071
3072                 forObjectSet(subject, relation, new SyncToAsyncSetProcedure<Resource>(
3073                                 procedure));
3074
3075         }
3076
3077         @Override
3078         final public void forObjectSet(final Resource subject,
3079                         final Resource relation, final SetListener<Resource> procedure) {
3080
3081                 assert (subject != null);
3082                 assert (relation != null);
3083                 assert (procedure != null);
3084
3085                 forObjectSet(subject, relation, new NoneToAsyncSetProcedure<Resource>(
3086                                 procedure));
3087
3088         }
3089
3090         @Override
3091         final public void forEachAssertedObject(final Resource subject,
3092                         final Resource relation,
3093                         final AsyncMultiProcedure<Resource> procedure) {
3094
3095                 assert (subject != null);
3096                 assert (relation != null);
3097                 assert (procedure != null);
3098
3099                 processor.forEachAssertedObject(this, subject, relation, procedure);
3100
3101         }
3102
3103         @Override
3104         public void forEachAssertedObject(Resource subject, Resource relation,
3105                         SyncMultiProcedure<Resource> procedure) {
3106
3107                 assert (subject != null);
3108                 assert (relation != null);
3109                 assert (procedure != null);
3110
3111                 forEachAssertedObject(subject, relation,
3112                                 new SyncToAsyncMultiProcedure<Resource>(procedure));
3113
3114         }
3115
3116         @Override
3117         public void forEachAssertedObject(Resource subject, Resource relation,
3118                         MultiProcedure<Resource> procedure) {
3119
3120                 assert (subject != null);
3121                 assert (relation != null);
3122                 assert (procedure != null);
3123
3124                 forEachAssertedObject(subject, relation,
3125                                 new NoneToAsyncMultiProcedure<Resource>(procedure));
3126
3127         }
3128
3129         @Override
3130         public void forAssertedObjectSet(Resource subject, Resource relation,
3131                         AsyncSetListener<Resource> procedure) {
3132
3133                 assert (subject != null);
3134                 assert (relation != null);
3135                 assert (procedure != null);
3136
3137                 processor.forAssertedObjectSet(this, subject, relation, procedure);
3138
3139         }
3140
3141         @Override
3142         public void forAssertedObjectSet(Resource subject, Resource relation,
3143                         SyncSetListener<Resource> procedure) {
3144
3145                 assert (subject != null);
3146                 assert (relation != null);
3147                 assert (procedure != null);
3148
3149                 forAssertedObjectSet(subject, relation,
3150                                 new SyncToAsyncSetProcedure<Resource>(procedure));
3151
3152         }
3153
3154         @Override
3155         public void forAssertedObjectSet(Resource subject, Resource relation,
3156                         SetListener<Resource> procedure) {
3157
3158                 assert (subject != null);
3159                 assert (relation != null);
3160                 assert (procedure != null);
3161
3162                 forAssertedObjectSet(subject, relation,
3163                                 new NoneToAsyncSetProcedure<Resource>(procedure));
3164
3165         }
3166
3167         @Override
3168         public void forInverse(Resource relation, AsyncListener<Resource> listener) {
3169                 asyncRequest(new Inverse(relation), listener);
3170         }
3171
3172         @Override
3173         public void forInverse(Resource relation, SyncListener<Resource> listener) {
3174                 asyncRequest(new Inverse(relation), listener);
3175         }
3176
3177         @Override
3178         public void forInverse(Resource relation, Listener<Resource> listener) {
3179                 asyncRequest(new Inverse(relation), listener);
3180         }
3181
3182         @Override
3183         final public void forInverse(final Resource relation,
3184                         final AsyncProcedure<Resource> procedure) {
3185
3186                 assert (relation != null);
3187                 assert (procedure != null);
3188
3189                 processor.forInverse(this, relation, new AsyncProcedure<Resource>() {
3190
3191                         @Override
3192                         public void execute(AsyncReadGraph graph, Resource result) {
3193                                 if (result != null)
3194                                         procedure.execute(graph, result);
3195                                 else {
3196                                         procedure.exception(graph, new NoInverseException(relation
3197                                                         .toString()));
3198                                 }
3199                         }
3200
3201                         @Override
3202                         public void exception(AsyncReadGraph graph, Throwable throwable) {
3203                                 procedure.exception(graph, throwable);
3204                         }
3205
3206                         @Override
3207                         public String toString() {
3208                                 return "forInverse -> " + procedure;
3209                         }
3210
3211                 });
3212
3213         }
3214
3215         @Override
3216         public void forInverse(Resource relation, SyncProcedure<Resource> procedure) {
3217                 forInverse(relation, new SyncToAsyncProcedure<Resource>(procedure));
3218         }
3219
3220         @Override
3221         public void forInverse(Resource relation, Procedure<Resource> procedure) {
3222                 forInverse(relation, new NoneToAsyncProcedure<Resource>(procedure));
3223         }
3224
3225         @Override
3226         public void forSingleObject(Resource subject, Resource relation,
3227                         AsyncListener<Resource> listener) {
3228                 asyncRequest(new SingleObject(subject, relation), listener);
3229         }
3230
3231         @Override
3232         public void forSingleObject(Resource subject, Resource relation,
3233                         SyncListener<Resource> listener) {
3234                 asyncRequest(new SingleObject(subject, relation), listener);
3235         }
3236
3237         @Override
3238         public void forSingleObject(Resource subject, Resource relation,
3239                         Listener<Resource> listener) {
3240                 asyncRequest(new SingleObject(subject, relation), listener);
3241         }
3242
3243         @Override
3244         final public void forSingleObject(final Resource subject,
3245                         final Resource relation, final AsyncProcedure<Resource> procedure) {
3246
3247                 assert (subject != null);
3248                 assert (relation != null);
3249                 assert (procedure != null);
3250
3251                 processor.forEachObject(this, subject, relation,
3252                                 new SingleOrErrorProcedure<Resource>(procedure));
3253
3254         }
3255
3256         @Override
3257         public void forSingleObject(Resource subject, Resource relation,
3258                         SyncProcedure<Resource> procedure) {
3259                 forSingleObject(subject, relation, new SyncToAsyncProcedure<Resource>(
3260                                 procedure));
3261         }
3262
3263         @Override
3264         public void forSingleObject(Resource subject, Resource relation,
3265                         Procedure<Resource> procedure) {
3266                 forSingleObject(subject, relation, new NoneToAsyncProcedure<Resource>(
3267                                 procedure));
3268         }
3269
3270         @Override
3271         public void forSingleStatement(Resource subject, Resource relation,
3272                         AsyncListener<Statement> listener) {
3273                 asyncRequest(new SingleStatement(subject, relation), listener);
3274         }
3275
3276         @Override
3277         public void forSingleStatement(Resource subject, Resource relation,
3278                         SyncListener<Statement> listener) {
3279                 asyncRequest(new SingleStatement(subject, relation), listener);
3280         }
3281
3282         @Override
3283         public void forSingleStatement(Resource subject, Resource relation,
3284                         Listener<Statement> listener) {
3285                 asyncRequest(new SingleStatement(subject, relation), listener);
3286         }
3287
3288         @Override
3289         final public void forSingleStatement(final Resource subject,
3290                         final Resource relation, final AsyncProcedure<Statement> procedure) {
3291
3292                 assert (subject != null);
3293                 assert (relation != null);
3294                 assert (procedure != null);
3295
3296                 processor.forEachStatement(this, subject, relation,
3297                                 new SingleOrErrorProcedure<Statement>(procedure));
3298
3299         }
3300
3301         @Override
3302         public void forSingleStatement(Resource subject, Resource relation,
3303                         SyncProcedure<Statement> procedure) {
3304                 forSingleStatement(subject, relation,
3305                                 new SyncToAsyncProcedure<Statement>(procedure));
3306         }
3307
3308         @Override
3309         public void forSingleStatement(Resource subject, Resource relation,
3310                         Procedure<Statement> procedure) {
3311                 forSingleStatement(subject, relation,
3312                                 new NoneToAsyncProcedure<Statement>(procedure));
3313         }
3314
3315         @Override
3316         public void forSingleType(Resource subject,
3317                         AsyncListener<Resource> listener) {
3318                 asyncRequest(new SingleTypeAny(subject), listener);
3319         }
3320
3321         @Override
3322         public void forSingleType(Resource subject,
3323                         SyncListener<Resource> listener) {
3324                 asyncRequest(new SingleTypeAny(subject), listener);
3325         }
3326
3327         @Override
3328         public void forSingleType(Resource subject,
3329                         Listener<Resource> listener) {
3330                 asyncRequest(new SingleTypeAny(subject), listener);
3331         }
3332
3333         @Override
3334         final public void forSingleType(final Resource subject, final AsyncProcedure<Resource> procedure) {
3335
3336                 assert (subject != null);
3337                 assert (procedure != null);
3338
3339                 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3340
3341                 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
3342
3343                         @Override
3344                         public void execute(AsyncReadGraph graph, final Resource principalType) {
3345                                 checkedProcedure.offer(graph, principalType);
3346                         }
3347
3348                         @Override
3349                         public void finished(AsyncReadGraph graph) {
3350                                 checkedProcedure.dec(graph);
3351                         }
3352
3353                         @Override
3354                         public void exception(AsyncReadGraph graph, Throwable t) {
3355                                 checkedProcedure.exception(graph, t);
3356                         }
3357
3358                         @Override
3359                         public String toString() {
3360                                 return "forSingleType -> " + procedure;
3361                         }
3362
3363                 });
3364
3365         }
3366
3367         @Override
3368         public void forSingleType(Resource subject, SyncProcedure<Resource> procedure) {
3369                 forSingleType(subject, new SyncToAsyncProcedure<Resource>(
3370                                 procedure));
3371         }
3372
3373         @Override
3374         public void forSingleType(Resource subject, Procedure<Resource> procedure) {
3375                 forSingleType(subject, new NoneToAsyncProcedure<Resource>(
3376                                 procedure));
3377         }
3378
3379         @Override
3380         public void forSingleType(Resource subject, Resource relation,
3381                         AsyncListener<Resource> listener) {
3382                 asyncRequest(new SingleType(subject, relation), listener);
3383         }
3384
3385         @Override
3386         public void forSingleType(Resource subject, Resource relation,
3387                         SyncListener<Resource> listener) {
3388                 asyncRequest(new SingleType(subject, relation), listener);
3389         }
3390
3391         @Override
3392         public void forSingleType(Resource subject, Resource relation,
3393                         Listener<Resource> listener) {
3394                 asyncRequest(new SingleType(subject, relation), listener);
3395         }
3396
3397         @Override
3398         final public void forSingleType(final Resource subject,
3399                         final Resource baseType, final AsyncProcedure<Resource> procedure) {
3400
3401                 assert (subject != null);
3402                 assert (procedure != null);
3403
3404                 final DeepSingleOrErrorProcedure<Resource> checkedProcedure = new DeepSingleOrErrorProcedure<Resource>(procedure);
3405
3406                 processor.forEachPrincipalType(this, subject,
3407                                 new AsyncMultiProcedureAdapter<Resource>() {
3408
3409                                         @Override
3410                                         public void execute(AsyncReadGraph graph,
3411                                                         final Resource principalType) {
3412
3413                                                 checkedProcedure.inc();
3414
3415                                                 if(baseType == null) {
3416
3417                                                         checkedProcedure.offer(graph, principalType);
3418                                                         checkedProcedure.dec(graph);
3419
3420                                                 } else if(principalType.equals(baseType)) {
3421
3422                                                         checkedProcedure.offer(graph, principalType);
3423                                                         checkedProcedure.dec(graph);
3424
3425                                                 } else {
3426
3427                                                         processor.forSupertypes((ReadGraphImpl)graph, principalType,
3428                                                                         new AsyncProcedure<Set<Resource>>() {
3429
3430                                                                                 @Override
3431                                                                                 public void execute(
3432                                                                                                 AsyncReadGraph graph,
3433                                                                                                 Set<Resource> result) {
3434
3435                                                                                         if (result.contains(baseType))
3436                                                                                                 checkedProcedure.offer(graph,
3437                                                                                                                 principalType);
3438                                                                                         checkedProcedure.dec(graph);
3439
3440                                                                                 }
3441
3442                                                                                 @Override
3443                                                                                 public void exception(
3444                                                                                                 AsyncReadGraph graph,
3445                                                                                                 Throwable t) {
3446                                                                                         checkedProcedure
3447                                                                                                         .exception(graph, t);
3448                                                                                 }
3449
3450                                                                         });
3451
3452                                                 }
3453
3454                                         }
3455
3456                                         @Override
3457                                         public void finished(AsyncReadGraph graph) {
3458                                                 checkedProcedure.dec(graph);
3459                                         }
3460
3461                                         @Override
3462                                         public void exception(AsyncReadGraph graph, Throwable t) {
3463                                                 checkedProcedure.exception(graph, t);
3464                                         }
3465
3466                                         @Override
3467                                         public String toString() {
3468                                                 return "forSingleType -> " + procedure;
3469                                         }
3470
3471                                 });
3472
3473         }
3474
3475         @Override
3476         public void forSingleType(Resource subject, Resource relation,
3477                         SyncProcedure<Resource> procedure) {
3478                 forSingleType(subject, relation, new SyncToAsyncProcedure<Resource>(
3479                                 procedure));
3480         }
3481
3482         @Override
3483         public void forSingleType(Resource subject, Resource relation,
3484                         Procedure<Resource> procedure) {
3485                 forSingleType(subject, relation, new NoneToAsyncProcedure<Resource>(
3486                                 procedure));
3487         }
3488
3489         @Override
3490         public <T> void forValue(Resource subject, Binding binding,
3491                         AsyncListener<T> listener) {
3492                 asyncRequest(new Value<T>(subject, binding), listener);
3493         }
3494
3495         @Override
3496         public <T> void forValue(Resource subject, Binding binding,
3497                         SyncListener<T> listener) {
3498                 asyncRequest(new Value<T>(subject, binding), listener);
3499         }
3500
3501         @Override
3502         public <T> void forValue(Resource subject, Binding binding,
3503                         Listener<T> listener) {
3504                 asyncRequest(new Value<T>(subject, binding), listener);
3505         }
3506
3507         @Override
3508         public <T> void forValue(final Resource resource, final Binding binding,
3509                         final AsyncProcedure<T> procedure) {
3510
3511                 assert (resource != null);
3512                 assert (binding != null);
3513                 assert (procedure != null);
3514                 
3515                 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
3516
3517                         @Override
3518                         public void execute(AsyncReadGraph graph, byte[] result) {
3519                                 
3520                                 try {
3521
3522                                         if (result == null) {
3523                                                 procedure.exception(graph,
3524                                                                 new DoesNotContainValueException(
3525                                                                                 "No value for resource " + resource));
3526                                                 return;
3527                                         }
3528
3529                                         Serializer serializer = binding.serializer();
3530 //                                      Serializer serializer = Bindings.getSerializer( binding );
3531                                         Object obj = serializer.deserialize(result);
3532 //                                      if (!binding.isInstance(obj))
3533 //                                              procedure.exception(graph, new ClassCastException(
3534 //                                                              "Cannot get value " + obj + " with binding "
3535 //                                                                              + binding));
3536 //                                      else
3537                                                 procedure.execute(graph, (T) obj);
3538
3539                                 } catch (Throwable t) {
3540                                     procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
3541                                 }
3542                         }
3543
3544                         @Override
3545                         public void exception(AsyncReadGraph graph, Throwable t) {
3546                                 try {
3547                                         procedure.exception(graph, t);
3548                                 } catch (Throwable t2) {
3549                                 Logger.defaultLogError(t2);
3550                                 }
3551                         }
3552
3553                         @Override
3554                         public String toString() {
3555                                 return "forValue -> " + procedure;
3556                         }
3557
3558                 });
3559
3560         }
3561         
3562     private static String safeArrayToString(byte[] a) {
3563         if (a == null)
3564             return "null";
3565         int iMax = a.length - 1;
3566         if (iMax == -1)
3567             return "[]";
3568
3569         StringBuilder b = new StringBuilder();
3570         b.append('[');
3571         for (int i = 0; i < 100; i++) { // limit to first 100 items 
3572             b.append(a[i]);
3573             if (i == iMax)
3574                 return b.append(']').toString();
3575             b.append(", ");
3576         }
3577         return b.append(", ... (" + a.length + ")]").toString();
3578     }
3579
3580         @Override
3581         public <T> void forValue(Resource subject, Binding binding,
3582                         SyncProcedure<T> procedure) {
3583                 forValue(subject, binding, new SyncToAsyncProcedure<T>(procedure));
3584         }
3585
3586         @Override
3587         public <T> void forValue(Resource subject, Binding binding,
3588                         Procedure<T> procedure) {
3589                 forValue(subject, binding, new NoneToAsyncProcedure<T>(procedure));
3590         }
3591
3592         @Override
3593         public <T> void forValue(Resource subject, AsyncListener<T> listener) {
3594                 asyncRequest(new ValueImplied<T>(subject), listener);
3595         }
3596
3597         @Override
3598         public <T> void forValue(Resource subject, SyncListener<T> listener) {
3599                 asyncRequest(new ValueImplied<T>(subject), listener);
3600         }
3601
3602         @Override
3603         public <T> void forValue(Resource subject, Listener<T> listener) {
3604                 asyncRequest(new ValueImplied<T>(subject), listener);
3605         }
3606
3607         @Override
3608         final public <T> void forValue(final Resource subject, final AsyncProcedure<T> procedure) {
3609
3610                 assert (subject != null);
3611                 assert (procedure != null);
3612                 
3613                 forRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
3614
3615                         @Override
3616                         public void execute(AsyncReadGraph graph, Datatype type) {
3617                                 // TODO: consider trying Bindings.getBeanBinding(type);
3618                                 Binding binding = Bindings.getBinding(type);
3619                                 graph.forValue(subject, binding, procedure);
3620                         }
3621
3622                         @Override
3623                         public void exception(AsyncReadGraph graph, Throwable throwable) {
3624                                 procedure.exception(graph, new DoesNotContainValueException("Invalid data type", throwable));
3625                         }
3626                         
3627                 });
3628
3629         }
3630
3631         @Override
3632         public <T> void forValue(Resource subject, SyncProcedure<T> procedure) {
3633                 forValue(subject, new SyncToAsyncProcedure<T>(procedure));
3634         }
3635
3636         @Override
3637         public <T> void forValue(Resource subject, Procedure<T> procedure) {
3638                 forValue(subject, new NoneToAsyncProcedure<T>(procedure));
3639         }
3640
3641         @Override
3642         public <T> void forRelatedValue(Resource subject, Resource relation,
3643                         AsyncListener<T> listener) {
3644                 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3645         }
3646
3647         @Override
3648         public <T> void forRelatedValue(Resource subject, Resource relation,
3649                         SyncListener<T> listener) {
3650                 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3651         }
3652
3653         @Override
3654         public <T> void forRelatedValue(Resource subject, Resource relation,
3655                         Listener<T> listener) {
3656                 asyncRequest(new RelatedValueImplied<T>(subject, relation), listener);
3657         }
3658
3659         @Override
3660         final public <T> void forRelatedValue(final Resource subject,
3661                         final Resource relation, final AsyncProcedure<T> procedure) {
3662
3663                 assert (subject != null);
3664                 assert (relation != null);
3665                 assert (procedure != null);
3666
3667                 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3668
3669                 processor.forEachObject(this, subject, relation,
3670                                 new AsyncMultiProcedureAdapter<Resource>() {
3671
3672                                         @Override
3673                                         public void execute(AsyncReadGraph graph,
3674                                                         final Resource object) {
3675
3676                                                 checkedProcedure.inc();
3677
3678                                                 graph.forValue(object, new AsyncProcedure<Object>() {
3679
3680                                                         @Override
3681                                                         public void execute(AsyncReadGraph graph,
3682                                                                         Object result) {
3683                                                                 checkedProcedure.offer(graph, (T) result);
3684                                                                 checkedProcedure.dec(graph);
3685                                                         }
3686
3687                                                         @Override
3688                                                         public void exception(AsyncReadGraph graph,
3689                                                                         Throwable t) {
3690                                                                 checkedProcedure.exception(graph, t);
3691                                                         }
3692
3693                                                         @Override
3694                                                         public String toString() {
3695                                                                 return "forRelatedValue -> " + procedure;
3696                                                         }
3697
3698                                                 });
3699
3700                                         }
3701
3702                                         @Override
3703                                         public void finished(AsyncReadGraph graph) {
3704                                                 checkedProcedure.dec(graph);
3705                                         }
3706
3707                                         @Override
3708                                         public void exception(AsyncReadGraph graph, Throwable t) {
3709                                                 checkedProcedure.exception(graph, t);
3710                                         }
3711
3712                                 });
3713
3714         }
3715
3716         @Override
3717         public <T> void forRelatedValue(Resource subject, Resource relation,
3718                         SyncProcedure<T> procedure) {
3719                 forRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
3720                                 procedure));
3721         }
3722
3723         @Override
3724         public <T> void forRelatedValue(Resource subject, Resource relation,
3725                         Procedure<T> procedure) {
3726                 forRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
3727                                 procedure));
3728         }
3729
3730         @Override
3731         public <T> void forRelatedValue(Resource subject, Resource relation,
3732                         Binding binding, AsyncListener<T> listener) {
3733                 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3734         }
3735
3736         @Override
3737         public <T> void forRelatedValue(Resource subject, Resource relation,
3738                         Binding binding, SyncListener<T> listener) {
3739                 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3740         }
3741
3742         @Override
3743         public <T> void forRelatedValue(Resource subject, Resource relation,
3744                         Binding binding, Listener<T> listener) {
3745                 asyncRequest(new RelatedValue<T>(subject, relation, binding), listener);
3746         }
3747
3748         @Override
3749         final public <T> void forRelatedValue(final Resource subject,
3750                         final Resource relation, final Binding binding,
3751                         final AsyncProcedure<T> procedure) {
3752
3753                 assert (subject != null);
3754                 assert (relation != null);
3755                 assert (binding != null);
3756                 assert (procedure != null);
3757
3758                 final DeepSingleOrErrorProcedure<T> checkedProcedure = new DeepSingleOrErrorProcedure<T>(procedure);
3759                 
3760                 processor.forEachObject(this, subject, relation,
3761                                 new AsyncMultiProcedureAdapter<Resource>() {
3762
3763                                         @Override
3764                                         public void execute(AsyncReadGraph graph,
3765                                                         final Resource object) {
3766
3767                                                 checkedProcedure.inc();
3768
3769                                                 graph.forValue(object, binding, new AsyncProcedure<Object>() {
3770
3771                                                                         @Override
3772                                                                         public void execute(AsyncReadGraph graph,
3773                                                                                         Object result) {
3774                                                                                 
3775                                                                                 checkedProcedure.offer(graph,
3776                                                                                                 (T) result);
3777                                                                                 checkedProcedure.dec(graph);
3778                                                                         }
3779
3780                                                                         @Override
3781                                                                         public void exception(AsyncReadGraph graph,
3782                                                                                         Throwable t) {
3783                                                                                 checkedProcedure.exception(graph, t);
3784                                                                         }
3785
3786                                                                         @Override
3787                                                                         public String toString() {
3788                                                                                 return "forRelatedValue -> "
3789                                                                                                 + procedure;
3790                                                                         }
3791
3792                                                                 });
3793
3794                                         }
3795
3796                                         @Override
3797                                         public void finished(AsyncReadGraph graph) {
3798                                                 checkedProcedure.dec(graph);
3799                                         }
3800
3801                                         @Override
3802                                         public void exception(AsyncReadGraph graph, Throwable t) {
3803                                                 checkedProcedure.exception(graph, t);
3804                                         }
3805
3806                                 });
3807
3808         }
3809
3810         @Override
3811         public <T> void forRelatedValue(Resource subject, Resource relation,
3812                         Binding binding, SyncProcedure<T> procedure) {
3813                 forRelatedValue(subject, relation, binding,
3814                                 new SyncToAsyncProcedure<T>(procedure));
3815         }
3816
3817         @Override
3818         public <T> void forRelatedValue(Resource subject, Resource relation,
3819                         Binding binding, Procedure<T> procedure) {
3820                 forRelatedValue(subject, relation, binding,
3821                                 new NoneToAsyncProcedure<T>(procedure));
3822         }
3823
3824         @Override
3825         public <T> void forAdapted(Resource resource, Class<T> clazz,
3826                         AsyncListener<T> listener) {
3827                 asyncRequest(new Adapter<T>(resource, clazz), listener);
3828         }
3829
3830         @Override
3831         public <T> void forAdapted(Resource resource, Class<T> clazz,
3832                         SyncListener<T> listener) {
3833                 asyncRequest(new Adapter<T>(resource, clazz), listener);
3834         }
3835
3836         @Override
3837         public <T> void forAdapted(Resource resource, Class<T> clazz,
3838                         Listener<T> listener) {
3839                 asyncRequest(new Adapter<T>(resource, clazz), listener);
3840         }
3841
3842         @Override
3843         final public <T> void forAdapted(final Resource resource,
3844                         final Class<T> clazz, final AsyncProcedure<T> procedure) {
3845
3846                 assert (resource != null);
3847                 assert (clazz != null);
3848                 assert (procedure != null);
3849
3850                 final AdaptionService service = getSession().peekService(AdaptionService.class);
3851                 if (service == null)
3852                         procedure.exception(this, new ServiceException("No AdaptionService available")); 
3853                 else
3854                         service.adapt(this, resource, resource, Resource.class, clazz, false, procedure); 
3855
3856         }
3857
3858         @Override
3859         public <T> void forAdapted(Resource resource, Class<T> clazz,
3860                         SyncProcedure<T> procedure) {
3861                 forAdapted(resource, clazz, new SyncToAsyncProcedure<T>(procedure));
3862         }
3863
3864         @Override
3865         public <T> void forAdapted(Resource resource, Class<T> clazz,
3866                         Procedure<T> procedure) {
3867                 forAdapted(resource, clazz, new NoneToAsyncProcedure<T>(procedure));
3868         }
3869
3870         @Override
3871         public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3872                         AsyncListener<T> listener) {
3873                 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3874         }
3875
3876         @Override
3877         public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3878                         SyncListener<T> listener) {
3879                 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3880         }
3881
3882         @Override
3883         public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3884                         Listener<T> listener) {
3885                 asyncRequest(new UniqueAdapter<T>(resource, clazz), listener);
3886         }
3887
3888         @Override
3889         final public <T> void forUniqueAdapted(final Resource resource,
3890                         final Class<T> clazz, final AsyncProcedure<T> procedure) {
3891
3892                 assert (resource != null);
3893                 assert (clazz != null);
3894                 assert (procedure != null);
3895
3896                 final AdaptionService service = getSession().peekService(AdaptionService.class);
3897                 if (service == null)
3898                         procedure.exception(this, new ServiceException("No AdaptionService available")); 
3899                 else
3900                         service.adaptNew(this, resource, clazz, false, procedure);
3901
3902         }
3903
3904         @Override
3905         public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3906                         SyncProcedure<T> procedure) {
3907                 forUniqueAdapted(resource, clazz,
3908                                 new SyncToAsyncProcedure<T>(procedure));
3909         }
3910
3911         @Override
3912         public <T> void forUniqueAdapted(Resource resource, Class<T> clazz,
3913                         Procedure<T> procedure) {
3914                 forUniqueAdapted(resource, clazz,
3915                                 new NoneToAsyncProcedure<T>(procedure));
3916         }
3917
3918         @Override
3919         public void forPossibleInverse(Resource subject,
3920                         AsyncListener<Resource> listener) {
3921                 asyncRequest(new PossibleInverse(subject), listener);
3922         }
3923
3924         @Override
3925         public void forPossibleInverse(Resource subject,
3926                         SyncListener<Resource> listener) {
3927                 asyncRequest(new PossibleInverse(subject), listener);
3928         }
3929
3930         @Override
3931         public void forPossibleInverse(Resource subject, Listener<Resource> listener) {
3932                 asyncRequest(new PossibleInverse(subject), listener);
3933         }
3934
3935         @Override
3936         final public void forPossibleInverse(final Resource relation,
3937                         final AsyncProcedure<Resource> procedure) {
3938
3939                 assert (relation != null);
3940                 assert (procedure != null);
3941
3942                 processor.forInverse(this, relation, new ExceptionToNullProcedure<Resource>(procedure));
3943
3944         }
3945
3946         @Override
3947         public void forPossibleInverse(Resource subject,
3948                         SyncProcedure<Resource> procedure) {
3949                 forPossibleInverse(subject, new SyncToAsyncProcedure<Resource>(
3950                                 procedure));
3951         }
3952
3953         @Override
3954         public void forPossibleInverse(Resource subject,
3955                         Procedure<Resource> procedure) {
3956                 forPossibleInverse(subject, new NoneToAsyncProcedure<Resource>(
3957                                 procedure));
3958         }
3959
3960         @Override
3961         public void forPossibleObject(Resource subject, Resource relation,
3962                         AsyncListener<Resource> listener) {
3963                 asyncRequest(new PossibleObject(subject, relation), listener);
3964         }
3965
3966         @Override
3967         public void forPossibleObject(Resource subject, Resource relation,
3968                         SyncListener<Resource> listener) {
3969                 asyncRequest(new PossibleObject(subject, relation), listener);
3970         }
3971
3972         @Override
3973         public void forPossibleObject(Resource subject, Resource relation,
3974                         Listener<Resource> listener) {
3975                 asyncRequest(new PossibleObject(subject, relation), listener);
3976         }
3977
3978         @Override
3979         final public void forPossibleObject(final Resource subject,
3980                         final Resource relation, final AsyncProcedure<Resource> procedure) {
3981
3982                 assert (subject != null);
3983                 assert (relation != null);
3984                 assert (procedure != null);
3985
3986                 processor.forEachObject(this, subject, relation,
3987                                 new SingleOrNullProcedure<Resource>(procedure));
3988
3989         }
3990
3991         @Override
3992         public void forPossibleObject(Resource subject, Resource relation,
3993                         SyncProcedure<Resource> procedure) {
3994                 forPossibleObject(subject, relation,
3995                                 new SyncToAsyncProcedure<Resource>(procedure));
3996         }
3997
3998         @Override
3999         public void forPossibleObject(Resource subject, Resource relation,
4000                         Procedure<Resource> procedure) {
4001                 forPossibleObject(subject, relation,
4002                                 new NoneToAsyncProcedure<Resource>(procedure));
4003         }
4004
4005         @Override
4006         public void forPossibleStatement(Resource subject, Resource relation,
4007                         AsyncListener<Statement> listener) {
4008                 asyncRequest(new PossibleStatement(subject, relation), listener);
4009         }
4010
4011         @Override
4012         public void forPossibleStatement(Resource subject, Resource relation,
4013                         SyncListener<Statement> listener) {
4014                 asyncRequest(new PossibleStatement(subject, relation), listener);
4015         }
4016
4017         @Override
4018         public void forPossibleStatement(Resource subject, Resource relation,
4019                         Listener<Statement> listener) {
4020                 asyncRequest(new PossibleStatement(subject, relation), listener);
4021         }
4022
4023         @Override
4024         final public void forPossibleStatement(final Resource subject,
4025                         final Resource relation, final AsyncProcedure<Statement> procedure) {
4026
4027                 assert (subject != null);
4028                 assert (relation != null);
4029                 assert (procedure != null);
4030
4031                 processor.forEachStatement(this, subject, relation,
4032                                 new SingleFunctionalOrNullProcedure<Statement>(
4033                                                 "forPossibleStatement", procedure));
4034
4035         }
4036
4037         @Override
4038         public void forPossibleStatement(Resource subject, Resource relation,
4039                         SyncProcedure<Statement> procedure) {
4040                 forPossibleStatement(subject, relation,
4041                                 new SyncToAsyncProcedure<Statement>(procedure));
4042         }
4043
4044         @Override
4045         public void forPossibleStatement(Resource subject, Resource relation,
4046                         Procedure<Statement> procedure) {
4047                 forPossibleStatement(subject, relation,
4048                                 new NoneToAsyncProcedure<Statement>(procedure));
4049         }
4050
4051         @Override
4052         public void forPossibleType(Resource subject, Resource relation,
4053                         AsyncListener<Resource> listener) {
4054                 asyncRequest(new PossibleType(subject, relation), listener);
4055         }
4056
4057         @Override
4058         public void forPossibleType(Resource subject, Resource relation,
4059                         SyncListener<Resource> listener) {
4060                 asyncRequest(new PossibleType(subject, relation), listener);
4061         }
4062
4063         @Override
4064         public void forPossibleType(Resource subject, Resource relation,
4065                         Listener<Resource> listener) {
4066                 asyncRequest(new PossibleType(subject, relation), listener);
4067         }
4068
4069         @Override
4070         final public void forPossibleType(final Resource subject,
4071                         final Resource baseType, final AsyncProcedure<Resource> procedure) {
4072
4073                 assert (subject != null);
4074                 assert (procedure != null);
4075
4076                 final NullSingleOrNullProcedure<Resource> checkedProcedure = new NullSingleOrNullProcedure<Resource>(procedure);
4077
4078                 processor.forEachPrincipalType(this, subject, new AsyncMultiProcedureAdapter<Resource>() {
4079
4080                                         @Override
4081                                         public void execute(AsyncReadGraph graph,
4082                                                         final Resource principalType) {
4083
4084                                                 if (baseType == null) {
4085
4086                                                         checkedProcedure.offer(graph, principalType);
4087
4088                                                 } else if (principalType.equals(baseType)) {
4089
4090                                                         checkedProcedure.offer(graph, principalType);
4091
4092                                                 } else {
4093
4094                                                         checkedProcedure.inc();
4095
4096                                                         processor.forSupertypes((ReadGraphImpl)graph, principalType,
4097                                                                         new AsyncProcedure<Set<Resource>>() {
4098
4099                                                                                 @Override
4100                                                                                 public void execute(
4101                                                                                                 AsyncReadGraph graph,
4102                                                                                                 Set<Resource> result) {
4103
4104                                                                                         if (result.contains(baseType)) {
4105                                                                                                 checkedProcedure.offer(graph,
4106                                                                                                                 principalType);
4107                                                                                         }
4108
4109                                                                                         checkedProcedure.dec(graph);
4110
4111                                                                                 }
4112
4113                                                                                 @Override
4114                                                                                 public void exception(
4115                                                                                                 AsyncReadGraph graph,
4116                                                                                                 Throwable t) {
4117                                                                                         checkedProcedure.exception(graph, t);
4118                                                                                         checkedProcedure.dec(graph);
4119                                                                                 }
4120
4121                                                                                 @Override
4122                                                                                 public String toString() {
4123                                                                                         return "forPossibleType -> "
4124                                                                                                         + procedure;
4125                                                                                 }
4126
4127                                                                         });
4128
4129                                                 }
4130
4131                                         }
4132
4133                                         @Override
4134                                         public void finished(AsyncReadGraph graph) {
4135                                                 checkedProcedure.dec(graph);
4136                                         }
4137
4138                                         @Override
4139                                         public void exception(AsyncReadGraph graph, Throwable t) {
4140                                                 checkedProcedure.exception(graph, t);
4141                                                 checkedProcedure.dec(graph);
4142                                         }
4143
4144                                 });
4145
4146         }
4147
4148         @Override
4149         public void forPossibleType(Resource subject, Resource relation,
4150                         SyncProcedure<Resource> procedure) {
4151                 forPossibleType(subject, relation, new SyncToAsyncProcedure<Resource>(
4152                                 procedure));
4153         }
4154
4155         @Override
4156         public void forPossibleType(Resource subject, Resource relation,
4157                         Procedure<Resource> procedure) {
4158                 forPossibleType(subject, relation, new NoneToAsyncProcedure<Resource>(
4159                                 procedure));
4160         }
4161
4162         @Override
4163         public <T> void forPossibleValue(Resource subject, AsyncListener<T> listener) {
4164                 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4165         }
4166
4167         @Override
4168         public <T> void forPossibleValue(Resource subject, SyncListener<T> listener) {
4169                 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4170         }
4171
4172         @Override
4173         public <T> void forPossibleValue(Resource subject, Listener<T> listener) {
4174                 asyncRequest(new PossibleValueImplied<T>(subject), listener);
4175         }
4176
4177         @Override
4178         final public <T> void forPossibleValue(final Resource subject,
4179                         final AsyncProcedure<T> procedure) {
4180
4181                 assert (subject != null);
4182                 assert (procedure != null);
4183                 
4184                 forPossibleRelatedValue(subject, processor.getL0(this).HasDataType, DATA_TYPE_BINDING_INTERNAL, new AsyncProcedure<Datatype>() {
4185
4186                         @Override
4187                         public void execute(AsyncReadGraph graph, final Datatype type) {
4188                                 if (type == null) {
4189                                         procedure.execute(graph, null);
4190                                 } else {
4191                                         try {
4192                                                 // TODO: consider trying Bindings.getBeanBinding(type);
4193                                                 Binding binding = Bindings.getBinding(type);
4194                                                 graph.forPossibleValue(subject, binding, procedure);
4195                                         } catch (RuntimeBindingConstructionException e) {
4196                                                 procedure.exception(graph, e);
4197                                         }
4198                                 }
4199                         }
4200
4201                         @Override
4202                         public void exception(AsyncReadGraph graph, Throwable t) {
4203                                 procedure.exception(graph, t);
4204                         }
4205
4206                         @Override
4207                         public String toString() {
4208                                 return "forPossibleValue -> " + procedure;
4209                         }
4210
4211                 });
4212
4213         }
4214
4215         @Override
4216         public <T> void forPossibleValue(Resource subject,
4217                         SyncProcedure<T> procedure) {
4218                 forPossibleValue(subject, new SyncToAsyncProcedure<T>(procedure));
4219         }
4220
4221         @Override
4222         public <T> void forPossibleValue(Resource subject, Procedure<T> procedure) {
4223                 forPossibleValue(subject, new NoneToAsyncProcedure<T>(procedure));
4224         }
4225
4226         @Override
4227         public <T> void forPossibleValue(Resource subject, Binding binding,
4228                         AsyncListener<T> listener) {
4229                 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4230         }
4231
4232         @Override
4233         public <T> void forPossibleValue(Resource subject, Binding binding,
4234                         SyncListener<T> listener) {
4235                 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4236         }
4237
4238         @Override
4239         public <T> void forPossibleValue(Resource subject, Binding binding,
4240                         Listener<T> listener) {
4241                 asyncRequest(new PossibleValue<T>(subject, binding), listener);
4242         }
4243
4244         @Override
4245         final public <T> void forPossibleValue(final Resource resource,
4246                         final Binding binding, final AsyncProcedure<T> procedure) {
4247
4248                 assert (resource != null);
4249                 assert (binding != null);
4250                 assert (procedure != null);
4251
4252                 processor.forValue(this, resource, new AsyncProcedure<byte[]>() {
4253
4254                         @Override
4255                         public void execute(AsyncReadGraph graph, byte[] result) {
4256
4257                                 try {
4258
4259                                         if (result == null) {
4260                                                 procedure.execute(graph, null);
4261                                                 return;
4262                                         }
4263
4264                                         Serializer serializer = Bindings.getSerializer( binding );
4265                                         Object obj = serializer.deserialize(result);
4266                                         if (!binding.isInstance(obj))
4267                                                 procedure.exception(graph, new ClassCastException(
4268                                                                 "Cannot get value " + obj + " with binding "
4269                                                                                 + binding));
4270                                         else
4271                                                 procedure.execute(graph, (T) obj);
4272
4273                                 } catch (Throwable t) {
4274                                         procedure.exception(graph, new ServiceException("Could not forValue for subject " + debugString(resource) + " and binding " + String.valueOf(binding) + " with bytes " + safeArrayToString(result), t));
4275                                 }
4276                         }
4277
4278                         @Override
4279                         public void exception(AsyncReadGraph graph, Throwable t) {
4280                                 try {
4281                                         procedure.exception(graph, t);
4282                                 } catch (Throwable t2) {
4283                                 Logger.defaultLogError(t2);
4284                                 }
4285                         }
4286
4287                         @Override
4288                         public String toString() {
4289                                 return "forPossibleValue -> " + procedure;
4290                         }
4291
4292                 });
4293
4294         }
4295
4296         @Override
4297         public <T> void forPossibleValue(Resource subject, Binding binding,
4298                         SyncProcedure<T> procedure) {
4299                 forPossibleValue(subject, binding, new SyncToAsyncProcedure<T>(
4300                                 procedure));
4301         }
4302
4303         @Override
4304         public <T> void forPossibleValue(Resource subject, Binding binding,
4305                         Procedure<T> procedure) {
4306                 forPossibleValue(subject, binding, new NoneToAsyncProcedure<T>(
4307                                 procedure));
4308         }
4309
4310         @Override
4311         public <T> void forPossibleRelatedValue(Resource subject,
4312                         Resource relation, AsyncListener<T> listener) {
4313                 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4314                                 listener);
4315         }
4316
4317         @Override
4318         public <T> void forPossibleRelatedValue(Resource subject,
4319                         Resource relation, SyncListener<T> listener) {
4320                 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4321                                 listener);
4322         }
4323
4324         @Override
4325         public <T> void forPossibleRelatedValue(Resource subject,
4326                         Resource relation, Listener<T> listener) {
4327                 asyncRequest(new PossibleRelatedValueImplied<T>(subject, relation),
4328                                 listener);
4329         }
4330
4331         @Override
4332         final public <T> void forPossibleRelatedValue(final Resource subject,
4333                         final Resource relation, final AsyncProcedure<T> procedure) {
4334
4335                 assert (subject != null);
4336                 assert (relation != null);
4337                 assert (procedure != null);
4338
4339                 final DeepSingleOrNullProcedure<T> checkedProcedure = new DeepSingleOrNullProcedure<T>(procedure);
4340                 
4341                 processor.forEachObject(this, subject, relation,
4342                                 new AsyncMultiProcedureAdapter<Resource>() {
4343
4344                                         @Override
4345                                         public void execute(AsyncReadGraph graph,
4346                                                         final Resource object) {
4347
4348                                                 checkedProcedure.inc();
4349
4350                                                 graph.forValue(object, new AsyncProcedure<Object>() {
4351
4352                                                         @Override
4353                                                         public void execute(AsyncReadGraph graph,
4354                                                                         Object result) {
4355                                                                 checkedProcedure.offer(graph, (T) result);
4356                                                                 checkedProcedure.dec(graph);
4357                                                         }
4358
4359                                                         @Override
4360                                                         public void exception(AsyncReadGraph graph,
4361                                                                         Throwable t) {
4362                                                                 checkedProcedure.exception(graph, t);
4363                                                                 checkedProcedure.dec(graph);
4364                                                         }
4365
4366                                                 });
4367
4368                                         }
4369
4370                                         @Override
4371                                         public void finished(AsyncReadGraph graph) {
4372
4373                                                 checkedProcedure.dec(graph);
4374                                         }
4375
4376                                         @Override
4377                                         public void exception(AsyncReadGraph graph, Throwable t) {
4378                                                 checkedProcedure.exception(graph, t);
4379                                                 checkedProcedure.dec(graph);
4380                                         }
4381
4382                                         @Override
4383                                         public String toString() {
4384                                                 return "forPossibleRelatedValue -> " + procedure;
4385                                         }
4386                                 });
4387
4388         }
4389
4390         @Override
4391         public <T> void forPossibleRelatedValue(Resource subject,
4392                         Resource relation, SyncProcedure<T> procedure) {
4393                 forPossibleRelatedValue(subject, relation, new SyncToAsyncProcedure<T>(
4394                                 procedure));
4395         }
4396
4397         @Override
4398         public <T> void forPossibleRelatedValue(Resource subject,
4399                         Resource relation, Procedure<T> procedure) {
4400                 forPossibleRelatedValue(subject, relation, new NoneToAsyncProcedure<T>(
4401                                 procedure));
4402         }
4403
4404         @Override
4405         public <T> void forPossibleRelatedValue(Resource subject,
4406                         Resource relation, Binding binding, AsyncListener<T> listener) {
4407                 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4408                                 listener);
4409         }
4410
4411         @Override
4412         public <T> void forPossibleRelatedValue(Resource subject,
4413                         Resource relation, Binding binding, SyncListener<T> listener) {
4414                 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4415                                 listener);
4416         }
4417
4418         @Override
4419         public <T> void forPossibleRelatedValue(Resource subject,
4420                         Resource relation, Binding binding, Listener<T> listener) {
4421                 asyncRequest(new PossibleRelatedValue<T>(subject, relation, binding),
4422                                 listener);
4423         }
4424
4425         final public <T> void forPossibleRelatedValue(final Resource subject, final Resource relation, final Binding binding,
4426                         final AsyncProcedure<T> procedure) {
4427
4428                 assert (subject != null);
4429                 assert (relation != null);
4430                 assert (procedure != null);
4431
4432                 processor.forPossibleObject(this, subject, relation, new AsyncProcedure<Resource>() {
4433                         
4434                         @Override
4435                         public void execute(AsyncReadGraph graph, Resource object) {
4436                                 
4437                                 if(object == null) {
4438                                         procedure.execute(graph, null);
4439                                         return;
4440                                 }
4441
4442                                 processor.forPossibleValue((ReadGraphImpl)graph, object, new AsyncProcedure<byte[]>() {
4443
4444                                         @Override
4445                                         public void execute(AsyncReadGraph graph, byte[] bytes) {
4446
4447                                                 if(bytes != null) {
4448
4449                                                         try {
4450                                                         
4451                                                                 Serializer serializer = binding.serializer();
4452                                                                 Object obj = serializer.deserialize(bytes);
4453                                                                 if (!binding.isInstance(obj)) {
4454                                                                         procedure.exception(graph, new ClassCastException("Cannot get value " + obj + " with binding " + binding));
4455                                                                 } else {
4456                                                                         procedure.execute(graph, (T) obj);
4457                                                                 }
4458
4459                                                         } catch (Throwable t) {
4460                                                                 
4461                                                                 procedure.exception(graph, t);
4462                                                                 
4463                                                         }
4464                                                         
4465                                                 } else {
4466                                                         
4467                                                         procedure.execute(graph, null);
4468                                                         
4469                                                 }
4470                                                 
4471                                         }
4472
4473                                         @Override
4474                                         public void exception(AsyncReadGraph graph, Throwable t) {
4475                                                 procedure.exception(graph, t);
4476                                         }
4477
4478                                 });
4479                                 
4480                         }
4481                         
4482                         @Override
4483                         public void exception(AsyncReadGraph graph, Throwable throwable) {
4484                                 throwable.printStackTrace();
4485                                 procedure.exception(graph, throwable);
4486                         }
4487                         
4488                 });
4489
4490         }
4491
4492         @Override
4493         public <T> void forPossibleRelatedValue(Resource subject,
4494                         Resource relation, Binding binding, SyncProcedure<T> procedure) {
4495                 forPossibleRelatedValue(subject, relation, binding,
4496                                 new SyncToAsyncProcedure<T>(procedure));
4497         }
4498
4499         @Override
4500         public <T> void forPossibleRelatedValue(Resource subject,
4501                         Resource relation, Binding binding, Procedure<T> procedure) {
4502                 forPossibleRelatedValue(subject, relation, binding,
4503                                 new NoneToAsyncProcedure<T>(procedure));
4504         }
4505
4506         @Override
4507         public void forIsInstanceOf(Resource subject, Resource relation,
4508                         AsyncListener<Boolean> listener) {
4509                 asyncRequest(new IsInstanceOf(subject, relation), listener);
4510         }
4511
4512         @Override
4513         public void forIsInstanceOf(Resource subject, Resource relation,
4514                         SyncListener<Boolean> listener) {
4515                 asyncRequest(new IsInstanceOf(subject, relation), listener);
4516         }
4517
4518         @Override
4519         public void forIsInstanceOf(Resource subject, Resource relation,
4520                         Listener<Boolean> listener) {
4521                 asyncRequest(new IsInstanceOf(subject, relation), listener);
4522         }
4523
4524         @Override
4525         final public void forIsInstanceOf(final Resource resource,
4526                         final Resource type, final AsyncProcedure<Boolean> procedure) {
4527
4528                 assert (resource != null);
4529                 assert (type != null);
4530                 assert (procedure != null);
4531
4532                 forTypes(resource, new AsyncProcedure<Set<Resource>>() {
4533
4534                         @Override
4535                         public void execute(AsyncReadGraph graph, Set<Resource> result) {
4536                                 
4537                                 try {
4538                                         if (result.contains(type))
4539                                                 procedure.execute(graph, true);
4540                                         else
4541                                                 procedure.execute(graph, false);
4542                                 } catch (Throwable t) {
4543                                 Logger.defaultLogError(t);
4544                                 }
4545                         }
4546
4547                         @Override
4548                         public void exception(AsyncReadGraph graph, Throwable t) {
4549                                 try {
4550                                         procedure.exception(graph, t);
4551                                 } catch (Throwable t2) {
4552                                 Logger.defaultLogError(t2);
4553                                 }
4554                         }
4555
4556                         @Override
4557                         public String toString() {
4558                                 return "forIsInstanceOf -> " + procedure;
4559                         }
4560
4561                 });
4562
4563         }
4564
4565         @Override
4566         public void forIsInstanceOf(Resource subject, Resource relation,
4567                         SyncProcedure<Boolean> procedure) {
4568                 forIsInstanceOf(subject, relation, new SyncToAsyncProcedure<Boolean>(
4569                                 procedure));
4570         }
4571
4572         @Override
4573         public void forIsInstanceOf(Resource subject, Resource relation,
4574                         Procedure<Boolean> procedure) {
4575                 forIsInstanceOf(subject, relation, new NoneToAsyncProcedure<Boolean>(
4576                                 procedure));
4577         }
4578
4579         @Override
4580         public void forIsInheritedFrom(Resource subject, Resource relation,
4581                         AsyncListener<Boolean> listener) {
4582                 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4583         }
4584
4585         @Override
4586         public void forIsInheritedFrom(Resource subject, Resource relation,
4587                         SyncListener<Boolean> listener) {
4588                 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4589         }
4590
4591         @Override
4592         public void forIsInheritedFrom(Resource subject, Resource relation,
4593                         Listener<Boolean> listener) {
4594                 asyncRequest(new IsInheritedFrom(subject, relation), listener);
4595         }
4596
4597         @Override
4598         final public void forIsInheritedFrom(final Resource resource,
4599                         final Resource type, final AsyncProcedure<Boolean> procedure) {
4600
4601                 assert (resource != null);
4602                 assert (type != null);
4603                 assert (procedure != null);
4604
4605                 if (resource.equals(type)) {
4606                         try {
4607                                 procedure.execute(this, true);
4608                         } catch (Throwable t) {
4609                         Logger.defaultLogError(t);
4610                         }
4611                         return;
4612                 }
4613
4614                 forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
4615
4616                         @Override
4617                         public void execute(AsyncReadGraph graph, Set<Resource> result) {
4618                                 try {
4619                                         if (result.contains(type))
4620                                                 procedure.execute(graph, true);
4621                                         else
4622                                                 procedure.execute(graph, false);
4623                                 } catch (Throwable t) {
4624                                 Logger.defaultLogError(t);
4625                                 }
4626                         }
4627
4628                         @Override
4629                         public void exception(AsyncReadGraph graph, Throwable t) {
4630                                 try {
4631                                         procedure.exception(graph, t);
4632                                 } catch (Throwable t2) {
4633                                 Logger.defaultLogError(t2);
4634                                 }
4635                         }
4636
4637                         @Override
4638                         public String toString() {
4639                                 return "forIsInheritedFrom -> " + procedure;
4640                         }
4641
4642                 });
4643
4644         }
4645
4646         @Override
4647         public void forIsInheritedFrom(Resource subject, Resource relation,
4648                         SyncProcedure<Boolean> procedure) {
4649                 forIsInheritedFrom(subject, relation,
4650                                 new SyncToAsyncProcedure<Boolean>(procedure));
4651         }
4652
4653         @Override
4654         public void forIsInheritedFrom(Resource subject, Resource relation,
4655                         Procedure<Boolean> procedure) {
4656                 forIsInheritedFrom(subject, relation,
4657                                 new NoneToAsyncProcedure<Boolean>(procedure));
4658         }
4659
4660         @Override
4661         public void forIsSubrelationOf(Resource subject, Resource relation,
4662                         AsyncListener<Boolean> listener) {
4663                 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4664         }
4665
4666         @Override
4667         public void forIsSubrelationOf(Resource subject, Resource relation,
4668                         SyncListener<Boolean> listener) {
4669                 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4670         }
4671
4672         @Override
4673         public void forIsSubrelationOf(Resource subject, Resource relation,
4674                         Listener<Boolean> listener) {
4675                 asyncRequest(new IsSubrelationOf(subject, relation), listener);
4676         }
4677
4678         @Override
4679         final public void forIsSubrelationOf(final Resource resource,
4680                         final Resource relation, final AsyncProcedure<Boolean> procedure) {
4681
4682                 assert (resource != null);
4683                 assert (relation != null);
4684                 assert (procedure != null);
4685
4686                 if (resource.equals(relation)) {
4687                         procedure.execute(this, true);
4688                         return;
4689                 }
4690
4691                 forSuperrelations(resource, new AsyncProcedure<Set<Resource>>() {
4692
4693                         @Override
4694                         public void execute(AsyncReadGraph graph, Set<Resource> result) {
4695                                 try {
4696                                         if (result.contains(relation))
4697                                                 procedure.execute(graph, true);
4698                                         else
4699                                                 procedure.execute(graph, false);
4700                                 } catch (Throwable t) {
4701                                 Logger.defaultLogError(t);
4702                                 }
4703                         }
4704
4705                         @Override
4706                         public void exception(AsyncReadGraph graph, Throwable t) {
4707                                 try {
4708                                         procedure.exception(graph, t);
4709                                 } catch (Throwable t2) {
4710                                 Logger.defaultLogError(t2);
4711                                 }
4712                         }
4713
4714                         @Override
4715                         public String toString() {
4716                                 return "forIsSubrelationOf -> " + procedure;
4717                         }
4718
4719                 });
4720
4721         }
4722
4723         @Override
4724         public void forIsSubrelationOf(Resource subject, Resource relation,
4725                         SyncProcedure<Boolean> procedure) {
4726                 forIsSubrelationOf(subject, relation,
4727                                 new SyncToAsyncProcedure<Boolean>(procedure));
4728         }
4729
4730         @Override
4731         public void forIsSubrelationOf(Resource subject, Resource relation,
4732                         Procedure<Boolean> procedure) {
4733                 forIsSubrelationOf(subject, relation,
4734                                 new NoneToAsyncProcedure<Boolean>(procedure));
4735         }
4736
4737         @Override
4738         public void forHasStatement(Resource subject,
4739                         AsyncListener<Boolean> listener) {
4740                 asyncRequest(new HasStatementSubject(subject), listener);
4741         }
4742
4743         @Override
4744         public void forHasStatement(Resource subject, SyncListener<Boolean> listener) {
4745                 asyncRequest(new HasStatementSubject(subject), listener);
4746         }
4747
4748         @Override
4749         public void forHasStatement(Resource subject, Listener<Boolean> listener) {
4750                 asyncRequest(new HasStatementSubject(subject), listener);
4751         }
4752
4753         @Override
4754         final public void forHasStatement(final Resource subject,
4755                         final AsyncProcedure<Boolean> procedure) {
4756
4757                 assert (subject != null);
4758                 assert (procedure != null);
4759
4760                 processor.forHasStatement(this, subject, procedure);
4761
4762         }
4763
4764         @Override
4765         public void forHasStatement(Resource subject,
4766                         SyncProcedure<Boolean> procedure) {
4767                 forHasStatement(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4768         }
4769
4770         @Override
4771         public void forHasStatement(Resource subject, Procedure<Boolean> procedure) {
4772                 forHasStatement(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4773         }
4774
4775         @Override
4776         public void forHasStatement(Resource subject, Resource relation,
4777                         AsyncListener<Boolean> listener) {
4778                 asyncRequest(new HasStatement(subject, relation), listener);
4779         }
4780
4781         @Override
4782         public void forHasStatement(Resource subject, Resource relation,
4783                         SyncListener<Boolean> listener) {
4784                 asyncRequest(new HasStatement(subject, relation), listener);
4785         }
4786
4787         @Override
4788         public void forHasStatement(Resource subject, Resource relation,
4789                         Listener<Boolean> listener) {
4790                 asyncRequest(new HasStatement(subject, relation), listener);
4791         }
4792
4793         @Override
4794         final public void forHasStatement(final Resource subject,
4795                         final Resource relation, final AsyncProcedure<Boolean> procedure) {
4796
4797                 assert (subject != null);
4798                 assert (relation != null);
4799                 assert (procedure != null);
4800
4801                 processor.forHasStatement(this, subject, relation, procedure);
4802
4803         }
4804
4805         @Override
4806         public void forHasStatement(Resource subject, Resource relation,
4807                         SyncProcedure<Boolean> procedure) {
4808                 forHasStatement(subject, relation, new SyncToAsyncProcedure<Boolean>(
4809                                 procedure));
4810         }
4811
4812         @Override
4813         public void forHasStatement(Resource subject, Resource relation,
4814                         Procedure<Boolean> procedure) {
4815                 forHasStatement(subject, relation, new NoneToAsyncProcedure<Boolean>(
4816                                 procedure));
4817         }
4818
4819         @Override
4820         public void forHasStatement(Resource subject, Resource relation,
4821                         Resource object, AsyncListener<Boolean> listener) {
4822                 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4823                                 listener);
4824         }
4825
4826         @Override
4827         public void forHasStatement(Resource subject, Resource relation,
4828                         Resource object, SyncListener<Boolean> listener) {
4829                 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4830                                 listener);
4831         }
4832
4833         @Override
4834         public void forHasStatement(Resource subject, Resource relation,
4835                         Resource object, Listener<Boolean> listener) {
4836                 asyncRequest(new HasStatementSubjectObject(subject, relation, object),
4837                                 listener);
4838         }
4839
4840         @Override
4841         final public void forHasStatement(final Resource subject,
4842                         final Resource relation, final Resource object,
4843                         final AsyncProcedure<Boolean> procedure) {
4844
4845                 assert (subject != null);
4846                 assert (relation != null);
4847                 assert (object != null);
4848                 assert (procedure != null);
4849
4850                 processor.forHasStatement(this, subject, relation, object, procedure);
4851
4852         }
4853
4854         @Override
4855         public void forHasStatement(Resource subject, Resource relation,
4856                         Resource object, SyncProcedure<Boolean> procedure) {
4857                 forHasStatement(subject, relation, object,
4858                                 new SyncToAsyncProcedure<Boolean>(procedure));
4859         }
4860
4861         @Override
4862         public void forHasStatement(Resource subject, Resource relation,
4863                         Resource object, Procedure<Boolean> procedure) {
4864                 forHasStatement(subject, relation, object,
4865                                 new NoneToAsyncProcedure<Boolean>(procedure));
4866         }
4867
4868         @Override
4869         public void forHasValue(Resource subject, AsyncListener<Boolean> listener) {
4870                 asyncRequest(new HasValue(subject), listener);
4871         }
4872
4873         @Override
4874         public void forHasValue(Resource subject, SyncListener<Boolean> listener) {
4875                 asyncRequest(new HasValue(subject), listener);
4876         }
4877
4878         @Override
4879         public void forHasValue(Resource subject, Listener<Boolean> listener) {
4880                 asyncRequest(new HasValue(subject), listener);
4881         }
4882
4883         @Override
4884         final public void forHasValue(final Resource subject,
4885                         final AsyncProcedure<Boolean> procedure) {
4886
4887                 assert (subject != null);
4888                 assert (procedure != null);
4889
4890                 processor.forValue(this, subject, new AsyncProcedure<byte[]>() {
4891
4892                         @Override
4893                         public void execute(AsyncReadGraph graph, byte[] result) {
4894                                 try {
4895                                         if (result == null)
4896                                                 procedure.execute(graph, false);
4897                                         else
4898                                                 procedure.execute(graph, true);
4899                                 } catch (Throwable t) {
4900                                 Logger.defaultLogError(t);
4901                                 }
4902                         }
4903
4904                         @Override
4905                         public void exception(AsyncReadGraph graph, Throwable t) {
4906                                 try {
4907                                         procedure.exception(graph, t);
4908                                 } catch (Throwable t2) {
4909                                 Logger.defaultLogError(t2);
4910                                 }
4911                         }
4912
4913                         @Override
4914                         public String toString() {
4915                                 return "forHasValue -> " + procedure;
4916                         }
4917
4918                 });
4919
4920         }
4921
4922         @Override
4923         public void forHasValue(Resource subject, SyncProcedure<Boolean> procedure) {
4924                 forHasValue(subject, new SyncToAsyncProcedure<Boolean>(procedure));
4925         }
4926
4927         @Override
4928         public void forHasValue(Resource subject, Procedure<Boolean> procedure) {
4929                 forHasValue(subject, new NoneToAsyncProcedure<Boolean>(procedure));
4930         }
4931
4932         @Override
4933         public void forOrderedSet(Resource subject,
4934                         AsyncMultiListener<Resource> listener) {
4935                 asyncRequest(new OrderedSet(subject), listener);
4936         }
4937
4938         @Override
4939         public void forOrderedSet(Resource subject,
4940                         SyncMultiListener<Resource> listener) {
4941                 asyncRequest(new OrderedSet(subject), listener);
4942         }
4943
4944         @Override
4945         public void forOrderedSet(Resource subject, MultiListener<Resource> listener) {
4946                 asyncRequest(new OrderedSet(subject), listener);
4947         }
4948
4949         @Override
4950         final public void forOrderedSet(final Resource subject,
4951                         final AsyncMultiProcedure<Resource> procedure) {
4952
4953                 assert (subject != null);
4954                 assert (procedure != null);
4955
4956                 processor.forOrderedSet(this, subject,
4957                                 new AsyncMultiProcedure<Resource>() {
4958
4959                                         @Override
4960                                         public void finished(AsyncReadGraph graph) {
4961                                                 try {
4962                                                         procedure.finished(graph);
4963                                                 } catch (Throwable t) {
4964                                                 Logger.defaultLogError(t);
4965                                                 }
4966                                         }
4967
4968                                         @Override
4969                                         public void execute(AsyncReadGraph graph, Resource result) {
4970                                                 try {
4971                                                         procedure.execute(graph, result);
4972                                                 } catch (Throwable t) {
4973                                                 Logger.defaultLogError(t);
4974                                                 }
4975                                         }
4976
4977                                         @Override
4978                                         public void exception(AsyncReadGraph graph, Throwable t) {
4979                                                 try {
4980                                                         procedure.exception(graph, t);
4981                                                 } catch (Throwable t2) {
4982                                                 Logger.defaultLogError(t2);
4983                                                 }
4984                                         }
4985
4986                                         @Override
4987                                         public String toString() {
4988                                                 return "forOrderedSet -> " + procedure;
4989                                         }
4990
4991                                 });
4992
4993         }
4994
4995         @Override
4996         public void forOrderedSet(Resource subject,
4997                         SyncMultiProcedure<Resource> procedure) {
4998                 forOrderedSet(subject, new SyncToAsyncMultiProcedure<Resource>(
4999                                 procedure));
5000         }
5001
5002         @Override
5003         public void forOrderedSet(Resource subject,
5004                         MultiProcedure<Resource> procedure) {
5005                 forOrderedSet(subject, new NoneToAsyncMultiProcedure<Resource>(
5006                                 procedure));
5007         }
5008
5009         @Override
5010         public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5011                         AsyncListener<T> listener) {
5012                 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5013         }
5014
5015         @Override
5016         public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5017                         SyncListener<T> listener) {
5018                 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5019         }
5020
5021         @Override
5022         public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5023                         Listener<T> listener) {
5024                 asyncRequest(new PossibleAdapter<T>(resource, clazz), listener);
5025         }
5026
5027         @Override
5028         final public <T> void forPossibleAdapted(final Resource resource,
5029                         final Class<T> clazz, final AsyncProcedure<T> procedure) {
5030
5031                 assert (resource != null);
5032                 assert (clazz != null);
5033                 assert (procedure != null);
5034
5035                 final AdaptionService service = getSession().peekService(AdaptionService.class);
5036                 if (service == null)
5037                         procedure.exception(this, new ServiceException("No AdaptionService available")); 
5038                 else
5039                         service.adapt(this, resource, resource, Resource.class, clazz, true, procedure);
5040         }
5041
5042         @Override
5043         public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5044                         SyncProcedure<T> procedure) {
5045                 forPossibleAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5046                                 procedure));
5047         }
5048
5049         @Override
5050         public <T> void forPossibleAdapted(Resource resource, Class<T> clazz,
5051                         Procedure<T> procedure) {
5052                 forPossibleAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5053                                 procedure));
5054         }
5055
5056         @Override
5057         public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5058                         AsyncListener<T> listener) {
5059                 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5060         }
5061
5062         @Override
5063         public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5064                         SyncListener<T> listener) {
5065                 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5066         }
5067
5068         @Override
5069         public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5070                         Listener<T> listener) {
5071                 asyncRequest(new PossibleUniqueAdapter<T>(resource, clazz), listener);
5072         }
5073
5074         @Override
5075         final public <T> void forPossibleUniqueAdapted(final Resource resource,
5076                         final Class<T> clazz, final AsyncProcedure<T> procedure) {
5077
5078                 assert (resource != null);
5079                 assert (clazz != null);
5080                 assert (procedure != null);
5081
5082                 final AdaptionService service = getSession().peekService(AdaptionService.class);
5083                 if (service == null)
5084                         procedure.exception(this, new ServiceException("No AdaptionService available")); 
5085                 else
5086                         service.adaptNew(this, resource, clazz, true, procedure);
5087
5088         }
5089
5090         @Override
5091         public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5092                         SyncProcedure<T> procedure) {
5093                 forPossibleUniqueAdapted(resource, clazz, new SyncToAsyncProcedure<T>(
5094                                 procedure));
5095         }
5096
5097         @Override
5098         public <T> void forPossibleUniqueAdapted(Resource resource, Class<T> clazz,
5099                         Procedure<T> procedure) {
5100                 forPossibleUniqueAdapted(resource, clazz, new NoneToAsyncProcedure<T>(
5101                                 procedure));
5102         }
5103
5104         /*
5105          * Implementation of the interface AsyncRequestProcessor
5106          */
5107
5108         @Override
5109         final public Session getSession() {
5110                 return processor.getSession();
5111         }
5112
5113         @Override
5114         public <T> void asyncRequest(final Read<T> request) {
5115
5116                 asyncRequest(request, new AsyncProcedure<T>() {
5117
5118                         @Override
5119                         public void execute(AsyncReadGraph graph, T result) {
5120                         }
5121
5122                         @Override
5123                         public void exception(AsyncReadGraph graph, Throwable t) {
5124                                 Logger.defaultLogError(t);
5125                         }
5126
5127                         @Override
5128                         public String toString() {
5129                                 return "asyncRequest(Read) -> " + request;
5130                         }
5131
5132                 });
5133
5134         }
5135
5136         @Override
5137         public <T> void asyncRequest(Read<T> request, AsyncListener<T> procedure) {
5138                 asyncRequest(request, (AsyncProcedure<T>) procedure);
5139         }
5140
5141         @Override
5142         public <T> void asyncRequest(Read<T> request,
5143                         final SyncListener<T> procedure) {
5144                 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5145         }
5146
5147         @Override
5148         public <T> void asyncRequest(Read<T> request, final Listener<T> procedure) {
5149                 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5150         }
5151
5152         @Override
5153         public <T> void asyncRequest(final Read<T> request, final AsyncProcedure<T> procedure) {
5154
5155                 assert (request != null);
5156                 assert (procedure != null);
5157                 
5158                 AsyncBarrierImpl barrier = asyncBarrier;
5159                 if(barrier != null)
5160                     barrier.inc();
5161                 
5162                 processor.scheduleNow(new SessionTask(this) {
5163
5164                         @Override
5165                         public void run0(int thread) {
5166                                 try {
5167                                         final ListenerBase listener = getListenerBase(procedure);
5168                                         QueryCache.runnerReadEntry(ReadGraphImpl.this, request, parent, listener, procedure, false);
5169                                 } catch (DatabaseException e) {
5170                                         Logger.defaultLogError(e);
5171                                 } finally {
5172                                 if(barrier != null)
5173                                     barrier.dec();
5174                                 }
5175                         }
5176                         
5177                 });
5178
5179         }
5180
5181     public static ReadGraphImpl createAsync(QueryProcessor support) {
5182         return new ReadGraphImpl(null, null, support);
5183     }
5184
5185         @Override
5186         public <T> void asyncRequest(Read<T> request, SyncProcedure<T> procedure) {
5187                 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5188         }
5189
5190         @Override
5191         public <T> void asyncRequest(Read<T> request, final Procedure<T> procedure) {
5192                 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5193         }
5194
5195         @Override
5196         final public <T> void asyncRequest(final AsyncRead<T> request) {
5197
5198                 assert (request != null);
5199
5200                 asyncRequest(request, new AsyncProcedure<T>() {
5201
5202                         @Override
5203                         public void execute(AsyncReadGraph graph, T result) {
5204                         }
5205
5206                         @Override
5207                         public void exception(AsyncReadGraph graph, Throwable t) {
5208                         Logger.defaultLogError(t);
5209                         }
5210
5211                         @Override
5212                         public String toString() {
5213                                 return "asyncRequest(AsyncRead) -> " + request;
5214                         }
5215
5216                 });
5217
5218         }
5219
5220         @Override
5221         public <T> void asyncRequest(AsyncRead<T> request,
5222                         AsyncListener<T> procedure) {
5223                 asyncRequest(request, (AsyncProcedure<T>) procedure);
5224         }
5225
5226         @Override
5227         final public <T> void asyncRequest(AsyncRead<T> request,
5228                         final SyncListener<T> procedure) {
5229                 asyncRequest(request, new SyncToAsyncListener<T>(procedure));
5230         }
5231
5232         @Override
5233         final public <T> void asyncRequest(AsyncRead<T> request,
5234                         final Listener<T> procedure) {
5235                 asyncRequest(request, new NoneToAsyncListener<T>(procedure));
5236         }
5237
5238         @Override
5239         final public <T> void asyncRequest(final AsyncRead<T> request,
5240                         final AsyncProcedure<T> procedure) {
5241
5242                 assert (request != null);
5243                 assert (procedure != null);
5244
5245         AsyncBarrierImpl barrier = asyncBarrier;
5246         if(barrier != null)
5247             barrier.inc();
5248
5249                 processor.scheduleNow(new SessionTask(this) {
5250
5251                         @Override
5252                         public void run0(int thread) {
5253
5254                             if(barrier != null)
5255                             barrier.inc();
5256
5257                             try {
5258                                         final ListenerBase listener = getListenerBase(procedure);
5259                                         QueryCache.runnerAsyncReadEntry(ReadGraphImpl.this, request, parent, listener, new AsyncProcedure<T>() {
5260
5261                                                 @Override
5262                                                 public void execute(AsyncReadGraph graph, T result) {
5263                                                         procedure.execute(graph, result);
5264                                                 if(barrier != null)
5265                                                     barrier.dec();
5266                                                 }
5267
5268                                                 @Override
5269                                                 public void exception(AsyncReadGraph graph, Throwable throwable) {
5270                                                         procedure.exception(graph, throwable);
5271                             if(barrier != null)
5272                                 barrier.dec();
5273                                                 }
5274                                                 
5275                                         }, false);
5276                                 if(barrier != null)
5277                                     barrier.dec();
5278                                 } catch (DatabaseException e) {
5279                                         Logger.defaultLogError(e);
5280                                 }
5281                         }
5282                         
5283                 });
5284
5285         }
5286
5287         @Override
5288         public <T> void asyncRequest(AsyncRead<T> request,
5289                         SyncProcedure<T> procedure) {
5290                 asyncRequest(request, new SyncToAsyncProcedure<T>(procedure));
5291         }
5292
5293         @Override
5294         final public <T> void asyncRequest(final AsyncRead<T> request,
5295                         final Procedure<T> procedure) {
5296                 asyncRequest(request, new NoneToAsyncProcedure<T>(procedure));
5297         }
5298
5299         @Override
5300         public <T> void asyncRequest(final MultiRead<T> request) {
5301
5302                 assert (request != null);
5303
5304                 asyncRequest(request, new SyncMultiProcedureAdapter<T>() {
5305                         @Override
5306                         public void exception(ReadGraph graph, Throwable t) {
5307                         Logger.defaultLogError(t);
5308                         }
5309
5310                         @Override
5311                         public String toString() {
5312                                 return "asyncRequest(MultiRead) -> " + request;
5313                         }
5314                 });
5315
5316         }
5317
5318         @Override
5319         public <T> void asyncRequest(MultiRead<T> request,
5320                         SyncMultiListener<T> procedure) {
5321                 asyncRequest(request, (SyncMultiProcedure<T>)procedure);
5322         }
5323
5324         @Override
5325         public <T> void asyncRequest(MultiRead<T> request,
5326                         MultiListener<T> procedure) {
5327                 asyncRequest(request, new NoneToSyncMultiListener<T>(procedure));
5328         }
5329
5330         
5331         @Override
5332         public <T> void asyncRequest(final MultiRead<T> request,
5333                         final SyncMultiProcedure<T> procedure) {
5334
5335                 assert (request != null);
5336                 assert (procedure != null);
5337
5338                 final ListenerBase listener = getListenerBase(procedure);
5339
5340                 if (parent != null || listener != null) {
5341
5342 //                  final ReadGraphImpl newGraph = newSync();
5343                     processor.query(this, request, parent, procedure,listener);
5344
5345                 } else {
5346
5347 //                  final ReadGraphImpl newGraph = newSync();
5348
5349                     try {
5350
5351                         request.perform(this, procedure);
5352
5353                     } catch (Throwable t) {
5354
5355                         try {
5356                                         procedure.exception(this, t);
5357                                 } catch (DatabaseException e) {
5358                                         LOGGER.error("Unexpected exception while handling exception", e);
5359                                 }
5360
5361                     }                           
5362
5363                 }
5364
5365         }
5366
5367         @Override
5368         public <T> void asyncRequest(MultiRead<T> request,
5369                         MultiProcedure<T> procedure) {
5370                 asyncRequest(request, new NoneToSyncMultiProcedure<T>(procedure));
5371         }
5372
5373         @Override
5374         final public <T> void asyncRequest(final AsyncMultiRead<T> request) {
5375
5376                 assert (request != null);
5377
5378                 asyncRequest(request, new AsyncMultiProcedureAdapter<T>() {
5379                         @Override
5380                         public void exception(AsyncReadGraph graph, Throwable t) {
5381                         Logger.defaultLogError(t);
5382                         }
5383
5384                         @Override
5385                         public String toString() {
5386                                 return "asyncRequest(AsyncMultiRead) -> " + request;
5387                         }
5388                 });
5389
5390         }
5391
5392         @Override
5393         public <T> void asyncRequest(AsyncMultiRead<T> request,
5394                         AsyncMultiListener<T> procedure) {
5395                 asyncRequest(request, (AsyncMultiProcedure<T>) procedure);
5396         }
5397
5398         @Override
5399         public <T> void asyncRequest(AsyncMultiRead<T> request,
5400                         SyncMultiListener<T> procedure) {
5401                 asyncRequest(request, new SyncToAsyncMultiListener<T>(procedure));
5402         }
5403
5404         @Override
5405         public <T> void asyncRequest(AsyncMultiRead<T> request,
5406                         MultiListener<T> procedure) {
5407                 asyncRequest(request, new NoneToAsyncMultiListener<T>(procedure));
5408         }
5409
5410         @Override
5411         final public <T> void asyncRequest(AsyncMultiRead<T> request,
5412                         final AsyncMultiProcedure<T> procedure) {
5413
5414                 assert (request != null);
5415                 assert (procedure != null);
5416
5417                 ListenerBase listener = getListenerBase(procedure);
5418
5419                 if (parent != null || listener != null) {
5420
5421                         processor.query(this, request, parent, procedure, listener);
5422
5423                 } else {
5424
5425                         try {
5426
5427                                 request.perform(this, new AsyncMultiProcedure<T>() {
5428
5429                                         @Override
5430                                         public void execute(AsyncReadGraph graph, T result) {
5431                                                 procedure.execute(graph, result);
5432                                         }
5433
5434                                         @Override
5435                                         public void finished(AsyncReadGraph graph) {
5436                                                 procedure.finished(graph);
5437                                         }
5438
5439                                         @Override
5440                                         public void exception(AsyncReadGraph graph, Throwable t) {
5441                                                 procedure.exception(graph, t);
5442                                         }
5443
5444                                         @Override
5445                                         public String toString() {
5446                                                 return "asyncRequest(AsyncMultiRead) -> " + procedure;
5447                                         }
5448
5449                                 });
5450
5451                         } catch (Throwable t) {
5452
5453                                 procedure.exception(this, new DatabaseException(t));
5454
5455                         }
5456                 }
5457
5458         }
5459
5460         @Override
5461         public <T> void asyncRequest(AsyncMultiRead<T> request,
5462                         SyncMultiProcedure<T> procedure) {
5463                 asyncRequest(request, new SyncToAsyncMultiProcedure<T>(procedure));
5464         }
5465
5466         @Override
5467         final public <T> void asyncRequest(AsyncMultiRead<T> request,
5468                         final MultiProcedure<T> procedure) {
5469                 asyncRequest(request, new NoneToAsyncMultiProcedure<T>(procedure));
5470         }
5471
5472         @Override
5473         final public <T> void asyncRequest(final ExternalRead<T> request) {
5474
5475                 assert (request != null);
5476
5477                 asyncRequest(request, new Procedure<T>() {
5478
5479                         @Override
5480                         public void execute(T result) {
5481                         }
5482
5483                         @Override
5484                         public void exception(Throwable t) {
5485                         Logger.defaultLogError(t);
5486                         }
5487
5488                         @Override
5489                         public String toString() {
5490                                 return "asyncRequest(PrimitiveRead) -> " + request;
5491                         }
5492
5493                 });
5494
5495         }
5496
5497         @Override
5498         public <T> void asyncRequest(ExternalRead<T> request,
5499                         final Listener<T> procedure) {
5500                 asyncRequest(request, (Procedure<T>) procedure);
5501         }
5502
5503         @Override
5504         final public <T> void asyncRequest(final ExternalRead<T> request,
5505                         final Procedure<T> procedure) {
5506
5507                 assert (request != null);
5508                 assert (procedure != null);
5509
5510                 final ListenerBase listener = getListenerBase(procedure);
5511
5512                 if (parent != null || listener != null) {
5513
5514                         try {
5515                                 QueryCacheBase.resultExternalReadEntry(this, request, parent, listener, procedure);
5516                         } catch (DatabaseException e) {
5517                                 Logger.defaultLogError(e);
5518                                 // This throwable has already been transferred to procedure at this point - do nothing about it
5519                         }
5520
5521                 } else {
5522
5523                         request.register(this, new Listener<T>() {
5524                                 @Override
5525                                 public void execute(T result) {
5526                                         procedure.execute(result);
5527                                 }
5528
5529                                 @Override
5530                                 public void exception(Throwable t) {
5531                                         procedure.exception(t);
5532                                 }
5533
5534                                 @Override
5535                                 public String toString() {
5536                                         return "asyncRequest(PrimitiveRead) -> " + request;
5537                                 }
5538
5539                                 @Override
5540                                 public boolean isDisposed() {
5541                                         return true;
5542                                 }
5543
5544                         });
5545
5546                 }
5547
5548         }
5549
5550         @Override
5551         public void asyncRequest(final Write request) {
5552
5553                 assert (request != null);
5554                 
5555                 getSession().asyncRequest(request);
5556
5557 //              processor.asyncWrite(request);
5558
5559         }
5560
5561         @Override
5562         public <T> void asyncRequest(final WriteResult<T> request, Procedure<T> procedure) {
5563                 throw new Error("Not implemented.");
5564         }
5565         
5566         @Override
5567         public void asyncRequest(Write request, Consumer<DatabaseException> callback) {
5568                 
5569                 assert (request != null);
5570                 
5571                 getSession().asyncRequest(request, callback);
5572                 
5573         }
5574
5575         @Override
5576         public void asyncRequest(final DelayedWrite request) {
5577
5578                 assert (request != null);
5579
5580                 getSession().asyncRequest(request);
5581
5582         }
5583
5584         @Override
5585         public <T> void asyncRequest(final DelayedWriteResult<T> request, Procedure<T> procedure) {
5586                 throw new Error("Not implemented.");
5587         }
5588         
5589         @Override
5590         public void asyncRequest(DelayedWrite r,
5591                         Consumer<DatabaseException> callback) {
5592                 throw new Error("Not implemented.");
5593         }
5594
5595         @Override
5596         public void asyncRequest(final WriteOnly request) {
5597
5598                 assert (request != null);
5599
5600                 getSession().asyncRequest(request);
5601
5602         }
5603
5604         @Override
5605         public <T> void asyncRequest(final WriteOnlyResult<T> request, Procedure<T> procedure) {
5606                 throw new Error("Not implemented.");
5607         }
5608         
5609         @Override
5610         public void asyncRequest(WriteOnly r, Consumer<DatabaseException> callback) {
5611                 throw new Error("Not implemented.");
5612         }
5613
5614         /*
5615          * Implementation of the interface ServiceLocator
5616          */
5617
5618         @Override
5619         public <T> T getService(Class<T> api) {
5620             if(WriteSupport.class == api) {
5621                 if(this instanceof WriteGraphImpl) {
5622                     WriteGraphImpl impl = (WriteGraphImpl)this;
5623                     return (T)impl.writeSupport;
5624                 }
5625             }
5626                 return getSession().getService(api);
5627         }
5628
5629         @Override
5630         public <T> T peekService(Class<T> api) {
5631                 return getSession().peekService(api);
5632         }
5633
5634         @Override
5635         public boolean hasService(Class<?> api) {
5636                 return getSession().hasService(api);
5637         }
5638
5639         @Override
5640         public <T> void registerService(Class<T> api, T service) {
5641                 getSession().registerService(api, service);
5642         }
5643         
5644         @Override
5645         public boolean isImmutable(Resource resource) throws DatabaseException {
5646                 ResourceImpl impl = (ResourceImpl)resource;
5647                 return processor.isImmutable(impl.id);
5648         }
5649
5650         /*
5651          * Internal routines
5652          */
5653
5654         protected static String INTERNAL_ERROR_STRING = "Transaction aborted due to internal client error.";
5655
5656         /*
5657          * callerThread is the currently running thread state.syncThread is blocking for
5658          * this execution state.syncParent is the blocking request
5659          */
5660
5661         ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support) {
5662                 this.parentGraph = parentGraph;
5663                 this.parent = parent;
5664                 this.processor = support;
5665                 this.asyncBarrier = prepareBarrier(parentGraph, parent, null, false);
5666         }
5667
5668     ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, QueryProcessor support, AsyncBarrierImpl asyncBarrier) {
5669         this.parentGraph = parentGraph;
5670         this.parent = parent;
5671         this.processor = support;
5672         this.asyncBarrier = asyncBarrier;
5673     }
5674
5675         ReadGraphImpl(ReadGraphImpl graph, CacheEntry parent) {
5676                 this(graph, parent, graph.processor);
5677         }
5678
5679     ReadGraphImpl(ReadGraphImpl parentGraph, CacheEntry parent, Runnable callback, boolean needsToBlock) {
5680         this(parentGraph, parent, parentGraph.processor, prepareBarrier(parentGraph, parent, callback, needsToBlock));
5681     }
5682
5683     static AsyncBarrierImpl prepareBarrier(ReadGraphImpl parentGraph, CacheEntry parent, Runnable callback, boolean needsToBlock) {
5684         return new AsyncBarrierImpl(parentGraph != null ? parentGraph.asyncBarrier : null, parent, callback, needsToBlock);
5685     }
5686     
5687         ReadGraphImpl(ReadGraphImpl graph) {
5688                 this(graph, graph.parent);
5689         }
5690
5691         public ReadGraphImpl withParent(CacheEntry parent, Runnable callback, boolean needsToBlock) {
5692                 return new ReadGraphImpl(this, parent, callback, needsToBlock);
5693         }
5694
5695         public ReadGraphImpl forRecompute(CacheEntry parent) {
5696                 return new ReadGraphImpl(null, parent, processor);
5697         }
5698
5699     public static ReadGraphImpl create(QueryProcessor support) {
5700         ReadGraphImpl result = new ReadGraphImpl(null, null, support);
5701         return result;
5702     }
5703
5704         public ReadGraphImpl newRestart(ReadGraphImpl impl) {
5705
5706                 WriteGraphImpl write = processor.getSession().getService(
5707                                 WriteGraphImpl.class);
5708
5709                 return write;
5710
5711         }
5712
5713         final private ListenerBase getListenerBase(final Object procedure) {
5714                 if (procedure instanceof ListenerBase)
5715                         return (ListenerBase) procedure;
5716                 else
5717                         return null;
5718         }
5719
5720         public <T> void waitAsyncProcedure(AsyncMultiReadProcedure<T> procedure) {
5721                 
5722                 assert(procedure.done());
5723                 
5724         }
5725
5726         public <T> void waitAsyncProcedure(AsyncReadProcedure<T> procedure) {
5727                 
5728                 assert(procedure.done());
5729                 
5730         }
5731
5732         public boolean resumeTasks() {
5733                 return processor.resumeTasks(this);
5734         }
5735
5736         Class<?> singleClass(Set<Resource> types) {
5737                 Class<?> result = null;
5738                 for (Resource type : types) {
5739                         Class<?> clazz = processor.getBuiltinValue(type);
5740                         if (clazz != null) {
5741                                 if (result != null)
5742                                         return null;
5743                                 else
5744                                         result = clazz;
5745                         }
5746                 }
5747                 return result;
5748         }
5749
5750         private String debugString(Resource r) {
5751                 String name = null;
5752                 try {
5753                         name = getPossibleRelatedValue(r, processor.getL0(this).HasName);
5754                 } catch (ManyObjectsForFunctionalRelationException e) {
5755                 Logger.defaultLogError(e);
5756                 } catch (ServiceException e) {
5757                 Logger.defaultLogError(e);
5758                 }
5759                 return "[" + name + " - " + r + "]";
5760         }
5761
5762         @Override
5763         public String toString() {
5764                 return "ReadGraphImpl[thread=" + Thread.currentThread() + "]";
5765         }
5766
5767         @Override
5768         final public int thread() {
5769                 return 0;
5770         }
5771         
5772     static class MultiTripleIntProcedure implements TripleIntProcedure {
5773
5774         final private AsyncMultiProcedure<Statement> procedure;
5775         final private ReadGraphImpl impl;
5776         final private QuerySupport support;
5777         
5778         public MultiTripleIntProcedure(AsyncMultiProcedure<Statement> procedure, ReadGraphImpl impl, QuerySupport support) {
5779                 this.procedure = procedure;
5780                 this.impl = impl;
5781                 this.support = support;
5782         }
5783         
5784         @Override
5785         public void execute(ReadGraphImpl graph, int s, int p, int o) {
5786                 try {
5787                         procedure.execute(graph, support.getStatement(s, p, o));
5788                 } catch (Throwable t2) {
5789                         Logger.defaultLogError(t2);
5790                 }
5791         }
5792
5793         @Override
5794         public void finished(ReadGraphImpl graph) {
5795                 try {
5796                         procedure.finished(graph);
5797 //                      impl.state.barrier.dec("ReadGraphSupportImpl.516");
5798                 } catch (Throwable t2) {
5799                         Logger.defaultLogError(t2);
5800                 }
5801         }
5802
5803         @Override
5804         public void exception(ReadGraphImpl graph, Throwable t) {
5805                 try {
5806                         procedure.exception(graph, t);
5807                 } catch (Throwable t2) {
5808                         Logger.defaultLogError(t2);
5809                 }
5810 //              impl.state.barrier.dec("ReadGraphSupportImpl.516");
5811         }
5812
5813         @Override
5814         public String toString() {
5815                 return "forEachObject with " + procedure;
5816         }
5817         
5818     }
5819
5820 //    private AsyncMultiProcedure<Resource> cacheKey = null;
5821 //    private MultiIntProcedure cacheResult = null;
5822 //    
5823 //    final IntProcedure forMultiProcedure(final AsyncMultiProcedure<Resource> procedure) {
5824 //      
5825 //      if(procedure == cacheKey) return cacheResult; 
5826 //      
5827 //      cacheResult = new MultiIntProcedure(procedure, this, processor.support);
5828 //      cacheKey = procedure;
5829 //      
5830 //      return cacheResult;
5831 //      
5832 //    }
5833 //
5834 //    private AsyncMultiProcedure<Statement> cacheKey2 = null;
5835 //    private MultiTripleIntProcedure cacheResult2 = null;
5836 //
5837 //    final synchronized TripleIntProcedure forMultiProcedure(final AsyncMultiProcedure<Statement> procedure) {
5838 //      
5839 //      if(procedure == cacheKey2) return cacheResult2; 
5840 //      
5841 //      cacheResult2 = new MultiTripleIntProcedure(procedure, this, processor.support);
5842 //      cacheKey2 = procedure;
5843 //      
5844 //      return cacheResult2;
5845 //      
5846 //    }
5847     
5848     @Override
5849     public Datatype getDataType(Resource subject) throws DatabaseException {
5850         for(Resource dataTypeResource : getObjects(subject, processor.getL0(this).HasDataType))
5851                 return getValue(dataTypeResource, Bindings.getBindingUnchecked(Datatype.class));
5852         throw new DoesNotContainValueException("The literal has no data type.");
5853     }
5854     
5855     protected <T extends Accessor> T getAccessor4File(Resource subject)
5856     throws DatabaseException {
5857         return null;
5858         /*        
5859         byte[]  bytes = processor.support.getValue(g, subject);
5860         if (null == bytes)
5861             return null;
5862         try {
5863             BinaryVariant va = (BinaryVariant) Accessors.getAccessor(bytes, Datatypes.VARIANT);
5864             Accessor ca = va.getContentAccessor();
5865             return (T)ca;
5866         } catch (AccessorConstructionException e) {
5867             throw new DatabaseException(e);
5868         }
5869         */
5870         /*
5871         if (null == bytes)
5872             return null;
5873         Binding datatype_binding = Bindings.getBindingUnchecked(DataType.class);
5874         Serializer datatype_serializer = datatype_binding.serializer();
5875         DataType datatype;
5876         try {
5877             BinaryMemory in = new BinaryMemory(ByteBuffer.wrap(bytes));
5878             datatype = (DataType)datatype_serializer.deserialize(in);
5879             Binding data_binding = Bindings.getBinding(datatype);
5880             Serializer data_serializer = data_binding.serializer(BinarySerializationFormat.INSTANCE);
5881             Object o = data_serializer.deserialize(in);
5882             try {
5883                 return (T)Accessors.getAccessor(data_binding, o);
5884             } catch(AccessorConstructionException e) {
5885                 return null;
5886             }
5887         } catch (Exception e) {
5888             throw new DatabaseException(e);
5889         }*/
5890     }
5891     @SuppressWarnings("unchecked")
5892     @Override
5893     public <T extends Accessor> T getAccessor(Resource subject) throws DatabaseException {
5894         RandomAccessBinary rab = getRandomAccessBinary(subject);
5895         try {
5896             return (T)Accessors.getAccessor(rab, getDataType(subject));
5897         } catch(AccessorConstructionException e) {
5898             throw new DatabaseException(e);
5899         }
5900     }
5901     @SuppressWarnings("unchecked")
5902     protected <T extends Accessor> T createAccessor(Resource resource, Datatype datatype, Object intialValue)
5903     throws DatabaseException {
5904         RandomAccessBinary rab = createRandomAccessBinary(resource, datatype, intialValue);
5905         try {
5906             return (T)Accessors.getAccessor(rab, datatype);
5907         } catch(AccessorConstructionException e) {
5908             throw new DatabaseException(e);
5909         }
5910     }
5911     @Override
5912     public RandomAccessBinary getRandomAccessBinary(Resource subject) throws DatabaseException {
5913         RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5914         ResourceData rd = ravs.get(subject);
5915         if (null != rd)
5916             return rd;
5917         try {
5918             ExternalValueSupport evs = getService(ExternalValueSupport.class);
5919             long size = evs.getValueSize(this, subject); // Throws DatabaseException if no old external value.
5920             try {
5921                 File platform = Platform.getLocation().toFile();
5922                 File tempFiles = new File(platform, "tempFiles");
5923                 File dbDir = new File(tempFiles, "db");
5924                 dbDir.mkdirs();
5925                 File file = new File(dbDir, "ResourceFile" + subject.getResourceId());
5926                 rd = new ResourceData(new BinaryFile(file), true); // Old external value.
5927                 final int N = 1<<20;
5928                 long left = size;
5929                 long offset = 0;
5930                 while (left > 0) {
5931                     int length = N < left ? N : (int)left;
5932                     byte[] bytes = evs.readValue(this, subject, offset, length);
5933                     offset += bytes.length;
5934                     left -= bytes.length;
5935                     rd.binaryFile.write(bytes);
5936                 }
5937                 ravs.put(subject, rd);
5938                 return rd;
5939             } catch (Exception e) {
5940                 throw new DatabaseException("Resource " + subject + " have value but there was problem with accessing value data.", e);
5941             }
5942         } catch (Exception e) {
5943             if(Development.DEVELOPMENT) {
5944                 if(Development.<Boolean>getProperty(DevelopmentKeys.WRITEGRAPH_EXCEPTION_STACKTRACES, Bindings.BOOLEAN)) {
5945                     e.printStackTrace();
5946                 }
5947             }
5948         }
5949         Datatype datatype = getDataType(subject);
5950         Object value = getPossibleValue(subject, Bindings.getBinding(datatype));
5951         return createRandomAccessBinary(subject, datatype, value);
5952     }
5953     public RandomAccessBinary createRandomAccessBinary(Resource resource, Datatype datatype, Object initialValue)
5954     throws DatabaseException {
5955         RandomAccessValueSupport ravs = getSession().getService(RandomAccessValueSupport.class);
5956         try {
5957             File platform = Platform.getLocation().toFile();
5958             File tempFiles = new File(platform, "tempFiles");
5959             File dbDir = new File(tempFiles, "db");
5960             dbDir.mkdirs();
5961             File file = new File(dbDir, "ResourceFile" + resource.getResourceId());
5962             ResourceData rd = new ResourceData(new BinaryFile(file), false);
5963             Binding binding = Bindings.getBinding(datatype);
5964             if (null == initialValue) {
5965                 initialValue = binding.createDefault();
5966             }
5967             Serializer serializer = binding.serializer();
5968             byte[] bytes = serializer.serialize(initialValue);
5969             // In case the file has been previously accessed and was larger we set the correct size now
5970             rd.binaryFile.setLength(bytes.length);
5971             rd.binaryFile.write(bytes);
5972             ravs.put(resource, rd);
5973             return rd;
5974         } catch (Exception e) {
5975             if (e instanceof DatabaseException)
5976                 throw (DatabaseException)e;
5977             else
5978                 throw new DatabaseException(e);
5979         }
5980     }
5981
5982 //    static class ExternalValueRequest<T> extends ResourceRead<T> {
5983 //
5984 //              public ExternalValueRequest(Resource resource) {
5985 //                      super(resource);
5986 //              }
5987 //
5988 //              @SuppressWarnings("unchecked")
5989 //              @Override
5990 //              public T perform(ReadGraph graph) throws DatabaseException {
5991 //              try {
5992 //                      
5993 //                      String uri = graph.getURI(resource);
5994 //                      if(Layer0.URIs.Functions_functionApplication.equals(uri)) return (T)functionApplication;
5995 //                      
5996 //                      return (T)ReflectionUtils.getValue(uri).getValue();
5997 //                      
5998 //              } catch(ValueNotFoundException e) {
5999 //                      throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
6000 //              } catch(ClassCastException e) {
6001 //                      throw new DatabaseException("Couldn't convert to external value (r=" + resource + ")", e);
6002 //              }
6003 //              }
6004 //      
6005 //    }
6006     
6007     @SuppressWarnings("unchecked")
6008     @Override
6009     public <T> T getValue2(Resource r, Object context) throws DatabaseException {
6010         Layer0 L0 = processor.getL0(this);
6011         Set<Resource> types = getTypes(r);
6012         
6013         if(types.contains(L0.Literal)) {
6014                 if(isImmutable(r)) {
6015                         return syncRequest(new ValueImplied<T>(r));
6016                 } else {
6017                         return getValue(r);
6018                 }
6019         }
6020         else if(types.contains(L0.ExternalValue)) {
6021                 return (T)syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6022         }
6023         else {
6024
6025             Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6026             if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6027             try {
6028                 return function.apply(this, r, context);
6029             } catch(RuntimeException e) {
6030                 DatabaseException dte = findPossibleRootException(e);
6031                 if(dte != null) throw dte;
6032                 else throw new DatabaseException(e);
6033             }
6034                 
6035         }
6036     }
6037
6038     @Override
6039     public Variant getVariantValue2(Resource r, Object context) throws DatabaseException {
6040         Layer0 L0 = processor.getL0(this);
6041         Set<Resource> types = getTypes(r);
6042         
6043         if(types.contains(L0.Literal)) {
6044             if(isImmutable(r)) {
6045                 return syncRequest(new VariantValueImplied(r));
6046             } else {
6047                 return getVariantValue(r);
6048             }
6049         }
6050         else if(types.contains(L0.ExternalValue)) {
6051             Object value = syncRequest(new AdaptValue(r), TransientCacheListener.<Object>instance());
6052             try {
6053                 return new Variant(Bindings.OBJECT.getContentBinding(value), value);
6054             } catch ( org.simantics.databoard.binding.error.BindingException e ) {
6055                 throw new BindingException( "No binding found for class " + value.getClass().getName(), e );
6056             }
6057         }
6058         else {
6059
6060             Function3<ReadGraph,Resource,Object,Object> function = requestValueFunction(r);
6061             if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function " + r);
6062             try {
6063                 Object value = function.apply(this, r, context);
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             } catch(RuntimeException e) {
6070                 DatabaseException dte = findPossibleRootException(e);
6071                 if(dte != null) throw dte;
6072                 else throw new DatabaseException(e);
6073             }
6074         }
6075     }
6076     
6077     @Override
6078     public <T> T getPossibleValue2(Resource subject, Object context) throws DatabaseException {
6079         try {
6080                 return getValue2(subject, context);
6081         } catch (DatabaseException e) {
6082                 return null;
6083         }
6084     }
6085
6086     static class PossibleConverterFunction<T> extends ResourceRead<Function3<ReadGraph,Resource,Object,T>> {
6087         
6088         public PossibleConverterFunction(Resource resource) {
6089                         super(resource);
6090                 }
6091
6092         @Override
6093         public Function3<ReadGraph,Resource,Object,T> perform(ReadGraph graph) throws DatabaseException {
6094                 return compute(graph, resource);
6095         }
6096         
6097         @SuppressWarnings("unchecked")
6098                 public static <T> Function3<ReadGraph,Resource,Object,T> compute(ReadGraph graph, Resource resource) throws DatabaseException {
6099             Layer0 L0 = Layer0.getInstance(graph);
6100             for(Resource converter : graph.getObjects(resource, L0.ConvertsToValueWith)) {
6101                 try {
6102                         if(L0.Functions_functionApplication.equals(converter)) {
6103                                 return (Function3<ReadGraph,Resource,Object,T>)graph.syncRequest(new AdaptValue(resource));
6104                         } else {
6105                                 return graph.getValue2(converter, resource);
6106                         }
6107                 } catch(RuntimeException e) {
6108                     DatabaseException dte = findPossibleRootException(e);
6109                     if(dte != null) throw dte;
6110                     else throw new DatabaseException(e);
6111                 }
6112             }
6113             return null;
6114         }
6115         
6116     }
6117     
6118     <T> Function3<ReadGraph,Resource,Object,T> requestValueFunction(Resource r) throws DatabaseException {
6119         if(isImmutable(r))
6120                 return syncRequest(new PossibleConverterFunction<T>(r), TransientCacheAsyncListener.<Function3<ReadGraph,Resource,Object,T>>instance());
6121         else
6122                 return syncRequest(new PossibleConverterFunction<T>(r));
6123     }
6124     
6125     /**
6126      * Get a value associated with a graph {@link Resource}, using a possible context object and
6127      * a desired value binding. The following methods are tried in order to retreive the value:
6128      * <ol>
6129      *   <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>
6130      *   <li>If the resource is a {@code L0.ExternalValue}, the value is acquired using
6131      *       {@link ReflectionUtils#getValue(String)}.</li>
6132      *   <li>If the resource is associated with a suitable value converter with relation {@code L0.ConvertsToValueWith}
6133      *       (see {@link #requestValueFunction(Resource)}), the value function is called with the graph, the resource
6134      *       and the context object.</li>
6135      * </ul>
6136      * 
6137      * @param r  A graph resource with which the value is associated
6138      * @param context  A context object that is used for acquiring the value (only applied in case 3)
6139      * @param binding  A binding for the value type (only applied in case 1)
6140      * @return  The value of the graph node.
6141      * @throws DoesNotContainValueException  No value is associated with the graph node.
6142      * @throws DatabaseException  Other errors, such as an error in casting the value to the return type or
6143      *         a runtime error in the value function.
6144      */
6145     @SuppressWarnings("unchecked")
6146         @Override
6147     public <T> T getValue2(Resource r, Object context, Binding binding) throws DatabaseException {
6148         if (binding instanceof ObjectVariantBinding)
6149                 return getValue2(r, context);
6150         
6151         Layer0 L0 = processor.getL0(this);
6152         Set<Resource> types = getTypes(r);
6153         if(types.contains(L0.Literal)) {
6154                 if(isImmutable(r)) {
6155                         return syncRequest(new Value<T>(r, binding));
6156                 } else {
6157                         return getValue(r, binding);
6158                 }
6159         } else if(types.contains(L0.ExternalValue)) {
6160                 ComputationalValue cv = syncRequest(new PossibleAdapter<ComputationalValue>(r, ComputationalValue.class), TransientCacheAsyncListener.instance());
6161                 if(cv != null) {
6162                         return cv.getValue(this, r);
6163                 } else {
6164                         // This should not even be possible since L0 defines an adapter for all values
6165                         try {
6166                                 return (T)ReflectionUtils.getValue(getURI(r)).getValue();
6167                         } catch(ValueNotFoundException e) {
6168                                 throw new DatabaseException(e);
6169                         } catch(ClassCastException e) {
6170                                 throw new DatabaseException(e);
6171                         }
6172                 }
6173         }
6174         else {
6175             Function3<ReadGraph,Resource,Object,T> function = requestValueFunction(r);
6176             if(function == null) throw new DoesNotContainValueException("Couldn't convert to a value function.");
6177             try {
6178                 Object value = function.apply(this, r, context);
6179                 if(binding.isInstance(value)) return (T)value;
6180                 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
6181                 return (T)Bindings.adapt(value, srcBinding, binding);
6182             } catch(RuntimeException e) {
6183                 DatabaseException dte = findPossibleRootException(e);
6184                 if(dte != null) throw dte;
6185                 else throw new DatabaseException(e);
6186             } catch (AdaptException e) {
6187                 throw new DatabaseException(e);
6188                         } catch (org.simantics.databoard.binding.error.BindingException e) {
6189                 throw new DatabaseException(e);
6190                         }
6191         }
6192     }
6193     
6194     @Override
6195     public <T> T getPossibleValue2(Resource subject, Object context, Binding binding) throws DatabaseException {
6196         try {
6197                 return getValue2(subject, context, binding);
6198         } catch (DatabaseException e) {
6199                 return null;
6200         }
6201     }
6202     
6203         private static DatabaseException findPossibleRootException(Throwable t) {
6204                 if(t == null) return null;
6205                 if(t instanceof DatabaseException) return (DatabaseException)t;
6206                 if(t instanceof RuntimeException || t instanceof InvocationTargetException) {
6207                         return findPossibleRootException(t.getCause());
6208                 }
6209                 return null;
6210         }
6211     
6212     @Override
6213     public <T> T getRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6214         return getRelatedValue2(subject, relation, subject);
6215     }
6216     
6217     @Override
6218     public Variant getRelatedVariantValue2(Resource subject, Resource relation) throws DatabaseException {
6219         return getRelatedVariantValue2(subject, relation, subject);
6220     }
6221     
6222     @Override
6223     public <T> T getPossibleRelatedValue2(Resource subject, Resource relation) throws DatabaseException {
6224         try {
6225                 Resource object = getPossibleObject(subject, relation);
6226                 if(object == null) return null;
6227                 else return getValue2(object, subject);
6228         } catch (DatabaseException e) {
6229                 return null;
6230         }
6231     }
6232
6233     @Override
6234     public <T> T getRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6235                 if(Development.DEVELOPMENT) {
6236                         if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
6237                                 String error = L0Validations.checkValueType(this, subject, relation);
6238                                 if(error != null) {
6239                                         Logger.defaultLogError(new ValidationException(error));
6240                                         throw new ValidationException(error);
6241                                 }
6242                         }
6243                 }
6244         return getValue2(getSingleObject(subject, relation), context);
6245     }
6246     
6247     @Override
6248     public Variant getRelatedVariantValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6249         if(Development.DEVELOPMENT) {
6250                         if(Development.<Boolean>getProperty(DevelopmentKeys.L0_VALIDATION, Bindings.BOOLEAN)) {
6251                                 String error = L0Validations.checkValueType(this, subject, relation);
6252                                 if(error != null) {
6253                                         Logger.defaultLogError(new ValidationException(error));
6254                                         throw new ValidationException(error);
6255                                 }
6256                         }
6257         }
6258         return getVariantValue2(getSingleObject(subject, relation), context);
6259     }
6260     
6261     @Override
6262     public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context) throws DatabaseException {
6263         try {
6264                 Resource object = getPossibleObject(subject, relation);
6265                 if(object == null) return null;
6266                 else return getValue2(object, context);
6267         } catch (DatabaseException e) {
6268                 return null;
6269         }
6270     }
6271
6272     @Override
6273     public <T> T getRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6274         return getRelatedValue2(subject, relation, subject, binding);
6275     }
6276     
6277     @Override
6278     public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Binding binding) throws DatabaseException {
6279         try {
6280                 Resource object = getPossibleObject(subject, relation);
6281                 if(object == null) return null;
6282                 return getValue2(object, subject, binding);
6283         } catch (DatabaseException e) {
6284                 return null;
6285         }
6286     }
6287
6288     @Override
6289     public <T> T getRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6290         return getValue2(getSingleObject(subject, relation), context, binding);
6291     }
6292     
6293     @Override
6294     public <T> T getPossibleRelatedValue2(Resource subject, Resource relation, Object context, Binding binding) throws DatabaseException {
6295         try {
6296                 Resource object = getPossibleObject(subject, relation);
6297                 if(object == null) return null;
6298                 else return getValue2(object, context, binding);
6299         } catch (DatabaseException e) {
6300                 return null;
6301         }
6302     }
6303     
6304     @Override
6305     public Type getRelatedValueType(Resource subject, Resource relation) throws DatabaseException {
6306         Layer0 L0 = processor.getL0(this);
6307         Resource property = getSingleObject(subject, relation);
6308         String typeText = (String)getRelatedValue(property, L0.HasValueType, Bindings.STRING);
6309         try {
6310             return org.simantics.scl.compiler.types.Types.parseType(typeText);
6311         } catch (SCLTypeParseException e) {
6312             throw new DatabaseException(e);
6313         }
6314     }
6315
6316     private static ThreadLocal<Boolean> syncGraph = new ThreadLocal<Boolean>() {
6317         protected Boolean initialValue() {
6318             return true;
6319         }
6320     };
6321
6322     @Override
6323     public boolean setSynchronous(boolean value) {
6324         boolean old = getSynchronous();
6325         syncGraph.set(value);
6326         return old;
6327     }
6328
6329     @Override
6330     public boolean getSynchronous() {
6331         return syncGraph.get();
6332     }
6333     
6334     public void ensureLoaded(int resource) {
6335         processor.querySupport.ensureLoaded(this, resource);
6336     }
6337     
6338     public void ensureLoaded(int resource, int predicate) {
6339         processor.querySupport.ensureLoaded(this, resource, predicate);
6340     }
6341
6342     public byte[] getValue(int resource) {
6343         return processor.querySupport.getValue(this, resource);
6344     }
6345     
6346     public int thread(int resource) {
6347         return (resource >>> 16) & processor.THREAD_MASK;
6348     }
6349
6350     public int thread(Resource resource) {
6351         return (((ResourceImpl)resource).id >>> 16) & processor.THREAD_MASK;
6352     }
6353     
6354     public ResourceSupport getResourceSupport() {
6355         return processor.getResourceSupport();
6356     }
6357     
6358     @Override
6359     public Object getModificationCounter() {
6360         return processor.getSession().getModificationCounter();
6361     }
6362
6363     @Override
6364     public boolean performPending() {
6365         return processor.performPending(this);
6366     }
6367     
6368     public Set<ReadGraphImpl> ancestorSet() {
6369         HashSet<ReadGraphImpl> result = new HashSet<>();
6370         ReadGraphImpl g = this;
6371         while(g != null) {
6372             result.add(g);
6373             g = g.parentGraph;
6374         }
6375         return result;
6376     }
6377     
6378     public int getLevel() {
6379         return getLevelStatic(this);
6380     }
6381     
6382     private static int getLevelStatic(ReadGraphImpl impl) {
6383         if(impl == null) return 0;
6384         else return 1 + getLevelStatic(impl.parentGraph);
6385     }
6386     
6387     public boolean isParent(ReadGraphImpl impl) {
6388         if(impl == null) return false;
6389         if(this == impl) return true;
6390         return isParent(impl.parentGraph);
6391     }
6392     
6393     public ReadGraphImpl getTopLevelGraph() {
6394         return getTopLevelGraphStatic(this);
6395     }
6396
6397     private static ReadGraphImpl getTopLevelGraphStatic(ReadGraphImpl impl) {
6398         if(impl.parentGraph == null) return impl;
6399         else return getTopLevelGraphStatic(impl.parentGraph);
6400     }
6401
6402 }