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