]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/TransferableGraphRequest.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / TransferableGraphRequest.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/TransferableGraphRequest.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/TransferableGraphRequest.java
new file mode 100644 (file)
index 0000000..2953337
--- /dev/null
@@ -0,0 +1,748 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.layer0.util;\r
+\r
+import gnu.trove.list.array.TIntArrayList;\r
+\r
+import java.io.ByteArrayInputStream;\r
+import java.io.DataOutput;\r
+import java.io.DataOutputStream;\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileNotFoundException;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.ObjectInputStream;\r
+import java.io.ObjectOutputStream;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.LinkedList;\r
+import java.util.Map;\r
+import java.util.Set;\r
+import java.util.UUID;\r
+import java.util.concurrent.ConcurrentSkipListSet;\r
+import java.util.concurrent.atomic.AtomicInteger;\r
+\r
+import org.apache.commons.io.output.DeferredFileOutputStream;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Databoard;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.databoard.serialization.Serializer;\r
+import org.simantics.db.AsyncReadGraph;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Statement;\r
+import org.simantics.db.common.StandardStatement;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ValidationException;\r
+import org.simantics.db.layer0.adapter.Instances;\r
+import org.simantics.db.layer0.adapter.SubgraphAdvisor;\r
+import org.simantics.db.layer0.adapter.SubgraphExtent;\r
+import org.simantics.db.layer0.adapter.SubgraphExtent.Classifier;\r
+import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus;\r
+import org.simantics.db.procedure.AsyncProcedure;\r
+import org.simantics.db.request.AsyncRead;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.db.service.CollectionSupport;\r
+import org.simantics.db.service.QueryControl;\r
+import org.simantics.db.service.QueryControl.ControlProcedure;\r
+import org.simantics.db.service.SerialisationSupport;\r
+import org.simantics.graph.representation.External;\r
+import org.simantics.graph.representation.Identity;\r
+import org.simantics.graph.representation.Root;\r
+import org.simantics.graph.representation.TransferableGraph1;\r
+import org.simantics.graph.representation.Value;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.operation.Layer0X;\r
+import org.simantics.utils.datastructures.Pair;\r
+import org.simantics.utils.datastructures.Triple;\r
+\r
+/**\r
+ * IsComposedOf objects are always in domain\r
+ * Ordered set elements are always in domain\r
+ * r is in domain if for all statements (s, IsRelatedTo, r) s is in domain\r
+ *\r
+ * IsWeaklyRelatedTo statements where subject is in domain are only accepted if object is in domain or has URI\r
+ * \r
+ * @deprecated in favor of {@link TransferableGraphRequest2}\r
+ */\r
+public class TransferableGraphRequest implements Read<TransferableGraph1> {\r
+\r
+       public static String LOG_FILE = "transferableGraph.log";\r
+       final static private boolean LOG = false;\r
+       final static private boolean DEBUG = false;\r
+       final static private boolean PROFILE = false;\r
+       \r
+//     final private Collection<Pair<Resource, String>> roots;\r
+//     private Collection<Resource> preExternals = Collections.emptyList();\r
+//     private Resource model;\r
+       \r
+       private TransferableGraphConfiguration configuration;\r
+       \r
+       static DataOutput log;\r
+\r
+       static {\r
+\r
+               if (LOG) {\r
+                       try {\r
+                               FileOutputStream stream = new FileOutputStream(LOG_FILE);\r
+                               log = new DataOutputStream(stream);\r
+                       } catch (FileNotFoundException e) {\r
+                               e.printStackTrace();\r
+                       }\r
+               }\r
+\r
+       }\r
+       \r
+       private static void log(String line) {\r
+               if (LOG) {\r
+                       try {\r
+                               log.writeUTF(line + "\n");\r
+                       } catch (IOException e) {\r
+                               e.printStackTrace();\r
+                       }\r
+               }\r
+       }\r
+       \r
+       \r
+       \r
+       public TransferableGraphRequest(Collection<Pair<Resource, String>> roots, Resource model) {\r
+               \r
+               configuration = new TransferableGraphConfiguration();\r
+               configuration.roots = roots;\r
+               configuration.model = model;\r
+               \r
+//             this.roots = roots;\r
+//             this.model = model;\r
+               \r
+       }\r
+\r
+       \r
+       public TransferableGraphRequest(Collection<Pair<Resource, String>> roots) {\r
+               this(roots, null);\r
+       }\r
+\r
+       public TransferableGraphRequest(TransferableGraphConfiguration conf) {\r
+               this.configuration = conf;\r
+       }\r
+\r
+       Layer0 l0;\r
+       \r
+       TIntArrayList inverses = new TIntArrayList();\r
+       int statements[];\r
+       int statementIndex = 0;\r
+       Map<Resource, Integer> ids;\r
+       Map<Resource, Variant> values;\r
+       TIntArrayList externalParents = new TIntArrayList();\r
+       ArrayList<String> externalNames = new ArrayList<String>(); \r
+//     ConcurrentLinkedQueue<Pair<Resource, byte[]>> values = new ConcurrentLinkedQueue<Pair<Resource, byte[]>>();\r
+       int id = 0;\r
+       int internalCount;\r
+       \r
+       int indent = 0;\r
+\r
+       private Serializer variantSerializer;\r
+\r
+       private boolean validateExternal(Resource r) {\r
+               if(configuration.disallowedExternals != null) {\r
+                       System.err.println("validateExternal agains " + configuration.disallowedExternals);\r
+                       return !configuration.disallowedExternals.contains(r);\r
+               }\r
+               return true;\r
+       }\r
+       \r
+       public int getId(ReadGraph graph, Resource r, Resource predicate) throws DatabaseException {\r
+//         for(int i=0;i<indent;++i)\r
+//             System.out.print("  ");\r
+//         System.out.println(NameUtils.getSafeName(graph, r));\r
+               if(ids.containsKey(r)) {\r
+                   int ret = ids.get(r);\r
+                   if(ret == -1) {\r
+                       for(int i=0;i<=indent;++i)\r
+                           System.out.print("  ");\r
+                       System.out.println("Cycle!!!"); // with " + GraphUtils.getReadableName(g, r));\r
+                   }\r
+                       return ret;\r
+               }\r
+               else {\r
+                   //ids.put(r, -1);\r
+                       Collection<Resource> parents = graph.getObjects(r, l0.PartOf);                  \r
+                       if(parents.size() != 1) {\r
+                           if(parents.size() == 0) {\r
+                               Resource inv = graph.getPossibleObject(r, l0.InverseOf);\r
+                               if(inv != null) {                       \r
+                                   ++indent;\r
+                                   if(DEBUG) System.out.println("inverse " + NameUtils.getSafeName(graph, inv));\r
+                                   int invId = getId(graph, inv, null);\r
+                                   externalParents.add(invId);\r
+                                   --indent;\r
+                                   externalNames.add("@inverse");\r
+                                   ids.put(r, id);\r
+                                   return id++;\r
+                               }\r
+                           }\r
+//                         for(Statement stat : graph.getStatements(r, b.IsWeaklyRelatedTo))\r
+//                             System.out.println(NameUtils.getSafeName(graph, stat.getPredicate()) \r
+//                                 + " -> " + NameUtils.getSafeName(graph, stat.getObject()));\r
+//                         if(predicate != null) {\r
+//                             if(!graph.isSubrelationOf(predicate, b.IsRelatedTo)) {\r
+//                                     return -2;\r
+//                             }\r
+//                         }\r
+                               throw new ValidationException("Reference to external resource " \r
+                                               + NameUtils.getSafeName(graph, r, true) + " without unique uri (" + parents.size() + " parents).");\r
+                       }\r
+                       for(Resource p : parents) {\r
+//                 System.out.println("Parent " + NameUtils.getSafeName(graph, p)); \r
+                           ++indent;\r
+                   if(!validateExternal(p)) throw new ValidationException("References to '" + graph.getURI(p) + "' are not allowed.");\r
+                               externalParents.add(getId(graph, p, null));\r
+                               --indent;\r
+                       }\r
+//          System.out.println("Request named for " + GraphUtils.getReadableName(g, r)); \r
+//                     String name = graph.getPossibleRelatedValue(r, b.HasName);\r
+            externalNames.add((String)graph.getRelatedValue(r, l0.HasName));\r
+//                     if(name != null) externalNames.add(name);\r
+//                     else externalNames.add("@resource");\r
+                       ids.put(r, id);\r
+                       return id++;\r
+               }\r
+       }\r
+       \r
+       public void addId(ReadGraph graph, Resource r, Resource predicate) throws DatabaseException {\r
+               statements[statementIndex++] = getId(graph, r, predicate); \r
+       }\r
+       \r
+       private void searchStatementPart(ReadGraph graph, final ConcurrentSkipListSet<Statement> statementSet, final ArrayList<Pair<Resource, Triple[]>> part, final Set<SubgraphExtent> extents) throws DatabaseException {\r
+\r
+               final SubgraphExtent.Callback callback = new SubgraphExtent.Callback() {\r
+\r
+                       @Override\r
+                       public void statement(Statement statement, boolean accept) {\r
+                               if(accept) {\r
+                                       statementSet.add(statement);\r
+                               }\r
+                       }\r
+                       \r
+               };\r
+\r
+               graph.syncRequest(new AsyncRead<Boolean>() {\r
+\r
+                   @Override\r
+                   public int threadHash() {\r
+                       return hashCode();\r
+                   }\r
+                       \r
+                       @Override\r
+                       public void perform(AsyncReadGraph graph, AsyncProcedure<Boolean> procedure) {\r
+\r
+                               QueryControl control = graph.getService(QueryControl.class);\r
+\r
+                               int slice = (int)(part.size() / control.getAmountOfQueryThreads()) + 1;\r
+\r
+                               final Pair[] rootArray = (Pair[])part.toArray(new Pair[part.size()]);\r
+                               for(int i=0;i<control.getAmountOfQueryThreads();i++) {\r
+\r
+                                       final int start = i * slice;\r
+                                       final int end = Math.min(start + slice, rootArray.length);\r
+\r
+                                       control.schedule(graph, i, new ControlProcedure() {\r
+\r
+                                               @Override\r
+                                               public void execute(AsyncReadGraph graph) {\r
+\r
+                                                       for(int index = start;index < end;index++) {\r
+\r
+                                                               final Pair<Resource, Triple[]> r = (Pair<Resource, Triple[]>)rootArray[index];\r
+\r
+                                                               final AtomicInteger position = new AtomicInteger(0);\r
+                                                               final SubgraphExtent.Classifier[] classifiers = new SubgraphExtent.Classifier[extents.size()];\r
+\r
+                                                               for(SubgraphExtent extent : extents) {\r
+\r
+                                                                       extent.accept(graph, r.first, new AsyncProcedure<Classifier>() {\r
+\r
+                                                                               public void execute(AsyncReadGraph graph, Classifier c) {\r
+\r
+                                                                                       int current = position.incrementAndGet();\r
+                                                                                       classifiers[current-1] = c;\r
+\r
+                                                                                       if(current == extents.size()) {\r
+\r
+                                                                                               for(Triple statement : r.second) {\r
+                                                                                                       if(!r.first.isPersistent()) continue;\r
+                                                                                                       for(Classifier classifier : classifiers) {\r
+                                                                                                               classifier.classify(graph, new StandardStatement(r.first, (Resource)statement.first, (Resource)statement.second), (ExtentStatus)statement.third, callback);\r
+                                                                                                       }\r
+                                                                                               }\r
+                                                                                               \r
+//                                                                                             graph.forEachDirectStatement(r, new AsyncMultiProcedure<Statement>() {\r
+//\r
+//                                                                                                     @Override\r
+//                                                                                                     public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+//                                                                                                             throwable.printStackTrace();\r
+//                                                                                                     }\r
+//\r
+//                                                                                                     @Override\r
+//                                                                                                     public void execute(AsyncReadGraph graph, Statement statement) {\r
+//                                                                                                     }\r
+//\r
+//                                                                                                     @Override\r
+//                                                                                                     public void finished(AsyncReadGraph graph) {\r
+//                                                                                                     }\r
+//\r
+//                                                                                             });\r
+\r
+                                                                                       }\r
+\r
+                                                                               }\r
+\r
+                                                                               public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+                                                                                       throwable.printStackTrace();\r
+                                                                               }\r
+\r
+                                                                       }, callback);\r
+\r
+                                                               }\r
+\r
+                                                       }\r
+\r
+                                               }\r
+\r
+                                       });\r
+\r
+                               }\r
+\r
+                               procedure.execute(graph, false);\r
+\r
+                       }\r
+\r
+                       @Override\r
+                       public int getFlags() {\r
+                               return 0;\r
+                       }\r
+\r
+               });\r
+\r
+       }\r
+\r
+       class StatementSetBuilder {\r
+\r
+               private int SLICE = 100000;\r
+               \r
+               final private CollectionSupport cs;\r
+               final private LinkedList<Collection<Statement>> sets = new LinkedList<Collection<Statement>>();\r
+\r
+               private Collection<Statement> current;\r
+               \r
+               StatementSetBuilder(CollectionSupport cs) {\r
+                       this.cs = cs;\r
+                       current = cs.createStatementList();\r
+               }\r
+               \r
+               LinkedList<Collection<Statement>> get() {\r
+                       sets.add(current);\r
+                       return sets;\r
+               }\r
+               \r
+               void add(Statement stm) {\r
+                       current.add(stm);\r
+                       if(current.size() == SLICE) {\r
+                               sets.add(current);\r
+                               current = cs.createStatementList();\r
+                       }\r
+               }\r
+\r
+               int size() {\r
+                       int result = 0;\r
+                       for(Collection<Statement> c : sets) result += c.size();\r
+                       return result;\r
+               }\r
+               \r
+               \r
+               void addAll(Collection<Statement> ss) {\r
+                       for(Statement s : ss) add(s);\r
+               }\r
+               \r
+       }\r
+       \r
+       private void searchStatements(ReadGraph graph, ObjectInputStream composedInput, ObjectInputStream statementInput, ObjectInputStream valueInput, final Set<SubgraphExtent> extents) throws DatabaseException, IOException {\r
+\r
+               CollectionSupport cs = graph.getService(CollectionSupport.class);\r
+               \r
+               ConcurrentSkipListSet<Statement> statementSet = new ConcurrentSkipListSet<Statement>();\r
+\r
+               StatementSetBuilder builder = new StatementSetBuilder(cs);\r
+\r
+               SerialisationSupport support = graph.getService(SerialisationSupport.class);\r
+\r
+               while(composedInput.available() > 0) {\r
+\r
+                       int s = composedInput.readInt();\r
+                       Resource subject = support.getResource(s);\r
+\r
+                       int size = composedInput.readInt();\r
+                       for(int i=0;i<size;i++) {\r
+                               int p = composedInput.readInt();\r
+                               int o = composedInput.readInt();\r
+                               StandardStatement stm = new StandardStatement(subject, support.getResource(p), support.getResource(o));\r
+//                             if(DEBUG) System.out.println("composed " + NameUtils.toString(graph, stm));\r
+                               builder.add(stm);\r
+                       }\r
+                       \r
+               }\r
+\r
+               while(statementInput.available() > 0) {\r
+\r
+                       ArrayList<Pair<Resource, Triple[]>> stms = new ArrayList<Pair<Resource, Triple[]>>(); \r
+\r
+                       int counter = 0;\r
+                       \r
+                       while(statementInput.available() > 0 && counter++ < 1000) {\r
+                       \r
+                               int s = statementInput.readInt();\r
+                               Resource subject = support.getResource(s);\r
+                               int size = statementInput.readInt();\r
+                               Triple[] list = new Triple[size];\r
+                               stms.add(Pair.make(subject, list));\r
+                               for(int i=0;i<size;i++) {\r
+                                       int p = statementInput.readInt();\r
+                                       int o = statementInput.readInt();\r
+                                       if(DEBUG) {\r
+                                               System.out.println("otherInput " + NameUtils.toString(graph, new StandardStatement(support.getResource(s), support.getResource(p), support.getResource(o))));\r
+                                       }\r
+                                       list[i] = new Triple<Resource, Resource, ExtentStatus>(support.getResource(p), support.getResource(o), ExtentStatus.EXTERNAL);\r
+                               }\r
+                       }\r
+                       \r
+                       searchStatementPart(graph, statementSet, stms, extents);\r
+                       \r
+               }\r
+\r
+               if(DEBUG) {\r
+                       for(Statement stm : statementSet)\r
+                               System.out.println("other " + NameUtils.toString(graph, stm));\r
+               }\r
+               \r
+               builder.addAll(statementSet);\r
+               \r
+               if(DEBUG) System.out.println("total " + statementSet.size() + " statements found.");\r
+\r
+               statementSet = null;\r
+               \r
+               LinkedList<Collection<Statement>> statementSets = builder.get();\r
+               \r
+               Map<Resource, Resource> inverses = cs.createMap(Resource.class);\r
+               Set<Resource> predicateSet = cs.createSet();\r
+               for(Collection<Statement> set : statementSets)\r
+                       for(Statement s : set) {\r
+                               Resource predicate = s.getPredicate();\r
+                               if(predicateSet.add(predicate)) {\r
+                                       Resource inverse = graph.getPossibleInverse(predicate);\r
+                                       inverses.put(predicate, inverse);\r
+                               }\r
+                       }\r
+\r
+               predicateSet = null;\r
+               \r
+               statements = new int[4 * builder.size()];\r
+               \r
+        while(!statementSets.isEmpty()) {\r
+               Collection<Statement> set = statementSets.pop();\r
+                       for(Statement s : set) {\r
+                               Resource subject = s.getSubject();\r
+                               Resource predicate = s.getPredicate();\r
+                               Resource object = s.getObject();\r
+                               int subjectId = ids.get(subject);\r
+                               int objectId = getId(graph, object, predicate);\r
+                               // The statement can be denied still\r
+                               if(objectId != -2) {\r
+                                       statements[statementIndex++] = subjectId;\r
+                                       addId(graph, predicate, null);\r
+                                       Resource inverse = inverses.get(predicate);\r
+                                       if(inverse != null) {\r
+                                               if(LOG) log("[STM] " + subject.getResourceId() + ", " + predicate.getResourceId() + ", " + inverse.getResourceId() + ", " + object.getResourceId());\r
+                                               addId(graph, inverse, null);\r
+                                       } else {\r
+                                               if(LOG) log("[STM] " + subject.getResourceId() + ", " + predicate.getResourceId() + ", -1, " + object.getResourceId());\r
+                                               statements[statementIndex++] = -1;\r
+                                       }\r
+                                       statements[statementIndex++] = objectId;\r
+                                       \r
+                                       \r
+                                       \r
+                               } else {\r
+                                       System.out.println("denied");\r
+                               }\r
+                       }\r
+               }\r
+               \r
+               inverses = null;\r
+               \r
+               while(valueInput.available() > 0) {\r
+\r
+                       int s = valueInput.readInt();\r
+                       Resource subject = support.getResource(s);\r
+                       int valueSize = valueInput.readInt();\r
+                       byte[] value = new byte[valueSize];\r
+                       valueInput.readFully(value);\r
+                       Variant variant = (Variant) variantSerializer.deserialize(value);\r
+                       values.put(subject, variant);\r
+                       \r
+               }\r
+               \r
+       }\r
+       \r
+       public void setExternals(Collection<Resource> rs) {\r
+               configuration.externals = rs;\r
+       }\r
+       \r
+       @Override\r
+       public TransferableGraph1 perform(ReadGraph graph) throws DatabaseException {\r
+\r
+               this.variantSerializer = graph.getService(Databoard.class).getSerializerUnchecked(Bindings.VARIANT);\r
+\r
+               this.l0 = Layer0.getInstance(graph);\r
+               Layer0X L0X = Layer0X.getInstance(graph);\r
+               \r
+               long total = System.nanoTime();\r
+\r
+       long startupTime = System.nanoTime();\r
+\r
+       CollectionSupport cs = graph.getService(CollectionSupport.class);\r
+       \r
+       ids = cs.createMap(Integer.class);\r
+       values = cs.createMap(byte[].class);\r
+       \r
+               ArrayList<Resource> rootResources = new ArrayList<Resource>();\r
+               for(Pair<Resource, String> p : configuration.roots) rootResources.add(p.first);\r
+\r
+//             if(model == null) {\r
+//                     SimulationResource SIMU = SimulationResource.getInstance(graph);\r
+//                     for(Resource root : rootResources) {\r
+//                             if(graph.isInstanceOf(root, SIMU.Model)) {\r
+//                                     model = root;\r
+//                                     break;\r
+//                             }\r
+//                             model = graph.syncRequest(new TypedParent(root, SIMU.Model));\r
+//                             if(model != null) break;\r
+//                     }\r
+//             }\r
+\r
+               Map<Resource, ExtentStatus> preStatus = new HashMap<Resource, ExtentStatus>();\r
+               \r
+               for(Resource root : rootResources) {\r
+                       Resource name = graph.getPossibleObject(root, l0.HasName);\r
+                       if(name != null) {\r
+                               preStatus.put(name, ExtentStatus.EXCLUDED);\r
+                       }\r
+               }\r
+               \r
+               for(Resource r : configuration.externals) preStatus.put(r, ExtentStatus.EXTERNAL); \r
+\r
+               Set<SubgraphExtent> extents = configuration.extents;\r
+               if(extents == null) {\r
+                       \r
+                       Instances extentInstances = graph.adapt(L0X.SubgraphExtent, Instances.class);\r
+                       Collection<Resource> extentResources = extentInstances.find(graph, configuration.model);\r
+                       extents = new HashSet<SubgraphExtent>();\r
+                       for(Resource r : extentResources) {\r
+                               extents.add(graph.getPossibleAdapter(r, SubgraphExtent.class));\r
+                       }\r
+\r
+                       if(DEBUG) {\r
+                               System.out.println("Extents " + extents.size());\r
+                               for(Resource extent : extentResources) System.out.println("-" + NameUtils.getSafeName(graph, extent));\r
+                       }\r
+                       \r
+               }\r
+               \r
+               Set<SubgraphAdvisor> advisors = configuration.advisors;\r
+               if(advisors == null) {\r
+\r
+                       Instances advisorInstances = graph.adapt(L0X.SubgraphAdvisor, Instances.class);\r
+                       Collection<Resource> advisorResources = advisorInstances.find(graph, configuration.model);\r
+                       advisors = new HashSet<SubgraphAdvisor>();\r
+                       for(Resource r : advisorResources) {\r
+                               advisors.add(graph.getPossibleAdapter(r, SubgraphAdvisor.class));\r
+                       }\r
+\r
+                       if(DEBUG) {\r
+                               System.out.println("Advisors " + advisors.size());\r
+                               for(Resource advisor : advisorResources) System.out.println("-" + NameUtils.getSafeName(graph, advisor));\r
+                       }\r
+                       \r
+               }\r
+\r
+       long startupTimeEnd = System.nanoTime();\r
+\r
+               long domainTime = System.nanoTime();\r
+               \r
+               String composedStatements = "composed" + UUID.randomUUID().toString();\r
+               String otherStatements = "other" + UUID.randomUUID().toString();\r
+               String valueFileName = "value" + UUID.randomUUID().toString();\r
+               \r
+        File composedStatementsFile = new File(composedStatements);\r
+        File otherStatementsFile = new File(otherStatements);\r
+        File valueFile = new File(valueFileName);\r
+        \r
+        if(DEBUG)\r
+               System.out.println("getDomain writes " + composedStatementsFile.getAbsolutePath());\r
+\r
+        try {\r
+        \r
+               DeferredFileOutputStream composedStatementsStream = new DeferredFileOutputStream(5 * 1024*1024, composedStatementsFile);\r
+               DeferredFileOutputStream otherStatementsStream = new DeferredFileOutputStream(1024*1024, otherStatementsFile);\r
+               DeferredFileOutputStream valueStream = new DeferredFileOutputStream(1024*1024, valueFile);\r
+               \r
+               ObjectOutputStream composedStatementsOutput = new ObjectOutputStream(composedStatementsStream);\r
+               ObjectOutputStream otherStatementsOutput = new ObjectOutputStream(otherStatementsStream);\r
+               ObjectOutputStream valueOutput = new ObjectOutputStream(valueStream);\r
+\r
+               Subgraphs.getDomain(graph, ids, rootResources, preStatus, advisors, composedStatementsOutput, otherStatementsOutput, valueOutput);\r
+               id = ids.size();\r
+\r
+               composedStatementsOutput.flush();\r
+               otherStatementsOutput.flush();\r
+               valueOutput.flush();\r
+               composedStatementsStream.close();\r
+               otherStatementsStream.close();\r
+               valueStream.close();\r
+               \r
+               long domainTimeEnd = System.nanoTime();\r
+\r
+               long fileTime = System.nanoTime();\r
+\r
+               internalCount = id;\r
+\r
+               ids.put(graph.getRootLibrary(), id++);\r
+               externalNames.add("http:/");\r
+               externalParents.add(-1);\r
+\r
+//             if(model == null) return null;\r
+\r
+               InputStream composedStatementsInputStream = null;\r
+               InputStream otherStatementsInputStream = null;\r
+               InputStream valueInputStream = null;\r
+               \r
+               if(composedStatementsStream.isInMemory()) {\r
+                       composedStatementsInputStream = new ByteArrayInputStream(composedStatementsStream.getData());\r
+               } else {\r
+                       composedStatementsInputStream = new FileInputStream(composedStatementsFile);\r
+               }\r
+               \r
+               if(otherStatementsStream.isInMemory()) {\r
+                       otherStatementsInputStream = new ByteArrayInputStream(otherStatementsStream.getData());\r
+               } else {\r
+                       otherStatementsInputStream = new FileInputStream(otherStatementsFile);\r
+               }\r
+\r
+               if(valueStream.isInMemory()) {\r
+                       valueInputStream = new ByteArrayInputStream(valueStream.getData());\r
+               } else {\r
+                       valueInputStream = new FileInputStream(valueFile);\r
+               }\r
+\r
+               composedStatementsStream = null;\r
+               otherStatementsStream = null;\r
+               valueStream = null;\r
+               \r
+               ObjectInputStream composedStatementsInput = new ObjectInputStream(composedStatementsInputStream);\r
+               ObjectInputStream otherStatementsInput = new ObjectInputStream(otherStatementsInputStream);\r
+               ObjectInputStream valueInput = new ObjectInputStream(valueInputStream);\r
+\r
+               long fileTimeEnd = System.nanoTime();\r
+\r
+               long statementTime = System.nanoTime();\r
+               searchStatements(graph, composedStatementsInput, otherStatementsInput, valueInput, extents);\r
+               long statementTimeEnd = System.nanoTime();\r
+\r
+               //              domainParts = null;\r
+\r
+               long buildTime = System.nanoTime();\r
+\r
+               int resourceCount = ids.size();\r
+\r
+               Identity[] identityArray;\r
+               { // Identities\r
+                       ArrayList<Identity> identities = new ArrayList<Identity>();\r
+\r
+                       for(Pair<Resource, String> r : configuration.roots) {\r
+                               Resource type = graph.getPossibleType(r.first, l0.Entity);\r
+                               if(type == null) type = l0.Entity;\r
+                               identities.add(new Identity(\r
+                                               ids.get(r.first),\r
+                                               new Root(r.second, graph.getURI(type))\r
+                               ));\r
+                       }\r
+\r
+                       int internalsPlusExternals = ids.size();                \r
+                       for(int i = internalCount; i < internalsPlusExternals ; i++) {\r
+                               int parent = externalParents.get(i - internalCount);\r
+                               String name = externalNames.get(i - internalCount);\r
+                               identities.add(new Identity(\r
+                                               i, \r
+                                               new External(parent, name)\r
+                               ));\r
+                       }\r
+                       identityArray = identities.toArray(new Identity[identities.size()]);\r
+               }            \r
+\r
+               Value[] valueArray = new Value[values.size()];\r
+               { // Values\r
+                       int index = 0;\r
+                       for(Map.Entry<Resource, Variant> entry : values.entrySet()) {\r
+                               if(LOG) log("[VALUE] " + entry.getKey().getResourceId());\r
+                               int r = getId(graph, entry.getKey(), null);\r
+                               valueArray[index++] = new Value(r, entry.getValue());\r
+                       }\r
+               }\r
+               ids = null;\r
+               values = null;\r
+\r
+               long buildTimeEnd = System.nanoTime();\r
+\r
+               TransferableGraph1 result = \r
+                   new TransferableGraph1(resourceCount, \r
+                           identityArray, \r
+                           statements, \r
+                           valueArray);\r
+\r
+               if(DEBUG) {\r
+                       System.out.println("transferable graph content: " + result);\r
+               }\r
+\r
+               long totalEnd = System.nanoTime();\r
+\r
+               if(PROFILE) {   \r
+                       System.out.println("startup in " + 1e-9*(startupTimeEnd - startupTime) + "s.");\r
+                       System.out.println("domain was found in " + 1e-9*(domainTimeEnd - domainTime) + "s.");\r
+                       System.out.println("files were written in " + 1e-9*(fileTimeEnd - fileTime) + "s.");\r
+                       System.out.println("statements were found in " + 1e-9*(statementTimeEnd - statementTime) + "s.");\r
+                       System.out.println("tg was built in " + 1e-9*(buildTimeEnd - buildTime) + "s.");\r
+                       System.out.println("total time for building subgraph was " + 1e-9*(totalEnd-total) + "s.");\r
+               }               \r
+\r
+               return result;\r
+        } catch (IOException e) {\r
+               e.printStackTrace();\r
+        }\r
+               \r
+        return null;\r
+               \r
+       }\r
+       \r
+}\r