1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.graph.store;
\r
14 import gnu.trove.list.array.TIntArrayList;
\r
15 import gnu.trove.map.hash.THashMap;
\r
16 import gnu.trove.map.hash.TIntIntHashMap;
\r
17 import gnu.trove.procedure.TIntProcedure;
\r
19 import java.util.ArrayList;
\r
20 import java.util.Collection;
\r
21 import java.util.concurrent.Callable;
\r
23 import org.simantics.graph.query.Path;
\r
24 import org.simantics.graph.query.Res;
\r
25 import org.simantics.graph.utils.GraphExecutor;
\r
27 public class GraphStore {
\r
28 public StatementStore statements;
\r
29 public IdentityStore identities;
\r
30 public ValueStore values;
\r
31 protected THashMap<Class<?>, IStore> stores;
\r
33 public GraphStore(StatementStore statements, IdentityStore identities, ValueStore values, THashMap<Class<?>, IStore> stores) {
\r
34 this.statements = statements;
\r
35 this.identities = identities;
\r
36 this.values = values;
\r
37 this.stores = stores;
\r
40 public GraphStore() {
\r
41 this(new StatementStore(), new IdentityStore(), new ValueStore(), new THashMap<Class<?>, IStore>());
\r
44 public GraphStore(GraphStore store) {
\r
45 this(store.statements, store.identities, store.values, store.stores);
\r
48 public void map(final TIntIntHashMap map) {
\r
49 ArrayList<Callable<Object>> tasks = new ArrayList<Callable<Object>>();
\r
50 tasks.add(new IStore.MapTask(statements, map));
\r
51 tasks.add(new IStore.MapTask(values, map));
\r
52 for(IStore store : stores.values())
\r
53 tasks.add(new IStore.MapTask(store, map));
\r
54 tasks.add(new IStore.MapTask(identities, map));
\r
57 GraphExecutor.EXECUTOR.invokeAll(tasks);
\r
58 } catch (InterruptedException e) {
\r
59 throw new RuntimeException(e);
\r
63 public <T extends IStore> void addStore(Class<T> clazz, T store) {
\r
64 stores.put(clazz, store);
\r
67 @SuppressWarnings("unchecked")
\r
68 public <T extends IStore> T getStore(Class<T> clazz) {
\r
69 return (T)stores.get(clazz);
\r
72 public int resToId(Res res) {
\r
73 if(res instanceof Path)
\r
74 return identities.pathToId((Path)res);
\r
76 IdRes idRes = (IdRes)res;
\r
77 if(idRes.fragment != this)
\r
84 public int createResToId(Res res) {
\r
85 if(res instanceof Path)
\r
86 return identities.createPathToId((Path)res);
\r
88 IdRes idRes = (IdRes)res;
\r
89 if(idRes.fragment != this)
\r
90 throw new RuntimeException("Cannot crate reference to an internal resource of other graph");
\r
96 public Res idToRes(int id) {
\r
97 Path path = identities.idToPath(id);
\r
99 return new IdRes(this, id);
\r
104 public void addIdsToResult(TIntArrayList ids, final Collection<Res> result) {
\r
105 ids.forEach(new TIntProcedure() {
\r
108 public boolean execute(int value) {
\r
109 result.add(idToRes(value));
\r
115 public void collectReferences(boolean[] set) {
\r
116 statements.collectReferences(set);
\r
117 identities.collectReferences(set);
\r
118 values.collectReferences(set);
\r
122 public String toString() {
\r
123 final StringBuilder b = new StringBuilder();
\r
124 statements.forStatements(new IStatementProcedure() {
\r
126 public String name(int id) {
\r
127 Path path = identities.idToPath(id);
\r
129 return "(" + id + ")";
\r
131 return path + "(" + id + ")";
\r
135 public void execute(int s, int p, int o) {
\r
144 return b.toString();
\r