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