]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph/src/org/simantics/graph/store/GraphStore.java
Check statement collisions
[simantics/platform.git] / bundles / org.simantics.graph / src / org / simantics / graph / store / GraphStore.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.graph.store;\r
13 \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
18 \r
19 import java.util.ArrayList;\r
20 import java.util.Collection;\r
21 import java.util.concurrent.Callable;\r
22 \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
26 \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
32         \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
38         }\r
39         \r
40         public GraphStore() {\r
41                 this(new StatementStore(), new IdentityStore(), new ValueStore(), new THashMap<Class<?>, IStore>());    \r
42         }\r
43         \r
44         public GraphStore(GraphStore store) {\r
45                 this(store.statements, store.identities, store.values, store.stores);\r
46         }\r
47 \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
55                 \r
56                 try {\r
57                         GraphExecutor.EXECUTOR.invokeAll(tasks);\r
58                 } catch (InterruptedException e) {\r
59                         throw new RuntimeException(e);\r
60                 }\r
61         }\r
62         \r
63         public <T extends IStore> void addStore(Class<T> clazz, T store) {\r
64                 stores.put(clazz, store);\r
65         }\r
66         \r
67         @SuppressWarnings("unchecked")\r
68         public <T extends IStore> T getStore(Class<T> clazz) {\r
69                 return (T)stores.get(clazz);\r
70         }\r
71         \r
72         public int resToId(Res res) {\r
73                 if(res instanceof Path)\r
74                         return identities.pathToId((Path)res);\r
75                 else {\r
76                         IdRes idRes = (IdRes)res;\r
77                         if(idRes.fragment != this)\r
78                                 return -1;\r
79                         else\r
80                                 return idRes.id;\r
81                 }\r
82         }\r
83         \r
84         public int createResToId(Res res) {\r
85                 if(res instanceof Path)\r
86                         return identities.createPathToId((Path)res);\r
87                 else {\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
91                         else\r
92                                 return idRes.id;\r
93                 }\r
94         }\r
95         \r
96         public Res idToRes(int id) {\r
97                 Path path = identities.idToPath(id);\r
98                 if(path == null)\r
99                         return new IdRes(this, id);\r
100                 else\r
101                         return path;\r
102         }\r
103         \r
104         public void addIdsToResult(TIntArrayList ids, final Collection<Res> result) {\r
105                 ids.forEach(new TIntProcedure() {\r
106                         \r
107                         @Override\r
108                         public boolean execute(int value) {\r
109                                 result.add(idToRes(value));\r
110                                 return true;\r
111                         }\r
112                 });\r
113         }\r
114         \r
115         public void collectReferences(boolean[] set) {\r
116                 statements.collectReferences(set);\r
117                 identities.collectReferences(set);\r
118                 values.collectReferences(set);          \r
119         }\r
120         \r
121         @Override\r
122         public String toString() {\r
123                 final StringBuilder b = new StringBuilder();\r
124                 statements.forStatements(new IStatementProcedure() {\r
125                         \r
126                         public String name(int id) {\r
127                                 Path path = identities.idToPath(id);\r
128                                 if(path == null)\r
129                                         return "(" + id + ")";\r
130                                 else\r
131                                         return path + "(" + id + ")";\r
132                         }\r
133                         \r
134                         @Override\r
135                         public void execute(int s, int p, int o) {\r
136                                 b.append(name(s));\r
137                                 b.append(" ");\r
138                                 b.append(name(p));\r
139                                 b.append(" ");\r
140                                 b.append(name(o));\r
141                                 b.append("\n");\r
142                         }\r
143                 });\r
144                 return b.toString();\r
145         }       \r
146 }\r