--- /dev/null
+/*******************************************************************************\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.graph.store;\r
+\r
+import gnu.trove.list.array.TIntArrayList;\r
+import gnu.trove.map.hash.THashMap;\r
+import gnu.trove.map.hash.TIntIntHashMap;\r
+import gnu.trove.procedure.TIntProcedure;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.concurrent.Callable;\r
+\r
+import org.simantics.graph.query.Path;\r
+import org.simantics.graph.query.Res;\r
+import org.simantics.graph.utils.GraphExecutor;\r
+\r
+public class GraphStore { \r
+ public StatementStore statements;\r
+ public IdentityStore identities;\r
+ public ValueStore values;\r
+ protected THashMap<Class<?>, IStore> stores;\r
+ \r
+ public GraphStore(StatementStore statements, IdentityStore identities, ValueStore values, THashMap<Class<?>, IStore> stores) {\r
+ this.statements = statements;\r
+ this.identities = identities;\r
+ this.values = values;\r
+ this.stores = stores;\r
+ }\r
+ \r
+ public GraphStore() {\r
+ this(new StatementStore(), new IdentityStore(), new ValueStore(), new THashMap<Class<?>, IStore>()); \r
+ }\r
+ \r
+ public GraphStore(GraphStore store) {\r
+ this(store.statements, store.identities, store.values, store.stores);\r
+ }\r
+\r
+ public void map(final TIntIntHashMap map) {\r
+ ArrayList<Callable<Object>> tasks = new ArrayList<Callable<Object>>();\r
+ tasks.add(new IStore.MapTask(statements, map));\r
+ tasks.add(new IStore.MapTask(values, map));\r
+ for(IStore store : stores.values())\r
+ tasks.add(new IStore.MapTask(store, map));\r
+ tasks.add(new IStore.MapTask(identities, map));\r
+ \r
+ try {\r
+ GraphExecutor.EXECUTOR.invokeAll(tasks);\r
+ } catch (InterruptedException e) {\r
+ throw new RuntimeException(e);\r
+ }\r
+ }\r
+ \r
+ public <T extends IStore> void addStore(Class<T> clazz, T store) {\r
+ stores.put(clazz, store);\r
+ }\r
+ \r
+ @SuppressWarnings("unchecked")\r
+ public <T extends IStore> T getStore(Class<T> clazz) {\r
+ return (T)stores.get(clazz);\r
+ }\r
+ \r
+ public int resToId(Res res) {\r
+ if(res instanceof Path)\r
+ return identities.pathToId((Path)res);\r
+ else {\r
+ IdRes idRes = (IdRes)res;\r
+ if(idRes.fragment != this)\r
+ return -1;\r
+ else\r
+ return idRes.id;\r
+ }\r
+ }\r
+ \r
+ public int createResToId(Res res) {\r
+ if(res instanceof Path)\r
+ return identities.createPathToId((Path)res);\r
+ else {\r
+ IdRes idRes = (IdRes)res;\r
+ if(idRes.fragment != this)\r
+ throw new RuntimeException("Cannot crate reference to an internal resource of other graph");\r
+ else\r
+ return idRes.id;\r
+ }\r
+ }\r
+ \r
+ public Res idToRes(int id) {\r
+ Path path = identities.idToPath(id);\r
+ if(path == null)\r
+ return new IdRes(this, id);\r
+ else\r
+ return path;\r
+ }\r
+ \r
+ public void addIdsToResult(TIntArrayList ids, final Collection<Res> result) {\r
+ ids.forEach(new TIntProcedure() {\r
+ \r
+ @Override\r
+ public boolean execute(int value) {\r
+ result.add(idToRes(value));\r
+ return true;\r
+ }\r
+ });\r
+ }\r
+ \r
+ public void collectReferences(boolean[] set) {\r
+ statements.collectReferences(set);\r
+ identities.collectReferences(set);\r
+ values.collectReferences(set); \r
+ }\r
+ \r
+ @Override\r
+ public String toString() {\r
+ final StringBuilder b = new StringBuilder();\r
+ statements.forStatements(new IStatementProcedure() {\r
+ \r
+ public String name(int id) {\r
+ Path path = identities.idToPath(id);\r
+ if(path == null)\r
+ return "(" + id + ")";\r
+ else\r
+ return path + "(" + id + ")";\r
+ }\r
+ \r
+ @Override\r
+ public void execute(int s, int p, int o) {\r
+ b.append(name(s));\r
+ b.append(" ");\r
+ b.append(name(p));\r
+ b.append(" ");\r
+ b.append(name(o));\r
+ b.append("\n");\r
+ }\r
+ });\r
+ return b.toString();\r
+ } \r
+}\r