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