1 package org.simantics.graph.query;
3 import gnu.trove.list.array.TIntArrayList;
4 import gnu.trove.map.hash.THashMap;
5 import gnu.trove.set.hash.THashSet;
6 import gnu.trove.set.hash.TIntHashSet;
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import java.util.Collections;
12 import org.simantics.databoard.adapter.AdaptException;
13 import org.simantics.databoard.adapter.RuntimeAdaptException;
14 import org.simantics.databoard.binding.Binding;
15 import org.simantics.databoard.binding.mutable.Variant;
16 import org.simantics.databoard.type.Datatype;
17 import org.simantics.graph.store.GraphStore;
18 import org.simantics.graph.store.IdRes;
19 import org.simantics.graph.store.IdentityStore;
20 import org.simantics.graph.store.PathPattern;
21 import org.simantics.graph.store.StatementStore;
23 public class CompositeGraph implements IGraph {
25 ArrayList<GraphStore> fragments = new ArrayList<GraphStore>();
28 public CompositeGraph(Paths paths) {
32 public void addFragment(GraphStore fragment) {
33 fragments.add(fragment);
36 public void undoAddFragment() {
37 fragments.remove(fragments.size()-1);
40 public void addFragments(Collection<GraphStore> fragments) {
41 fragments.addAll(fragments);
44 protected void rawGetObjects(GraphStore fragment, int subject, Res predicate, Collection<Res> result) {
46 if(predicate instanceof Path) {
47 predicateId = fragment.identities.pathToId((Path)predicate);
52 IdRes idPredicate = (IdRes)predicate;
53 if(idPredicate.fragment != fragment)
55 predicateId = idPredicate.id;
58 TIntArrayList objects = fragment.statements.getObjects(subject, predicateId);
59 fragment.addIdsToResult(objects, result);
62 private void rawGetObjects(Res subject, Res predicate, Collection<Res> result) {
63 if(subject instanceof Path) {
64 Path path = (Path)subject;
65 for(GraphStore fragment : fragments) {
66 int id = fragment.identities.pathToId(path);
68 rawGetObjects(fragment, id, predicate, result);
72 IdRes idRes = (IdRes)subject;
73 rawGetObjects(idRes.fragment, idRes.id, predicate, result);
77 public boolean hasRawObjects(Res subject, Path predicate) {
78 ArrayList<Res> result = new ArrayList<Res>();
79 rawGetObjects(subject, predicate, result);
80 return !result.isEmpty();
84 public Collection<Res> rawGetObjects(Res subject, Res predicate) {
85 ArrayList<Res> result = new ArrayList<Res>();
86 rawGetObjects(subject, predicate, result);
91 public Res singleRawObject(Res subject, Res predicate) throws NoUniqueObjectException {
92 ArrayList<Res> result = new ArrayList<Res>(1);
93 rawGetObjects(subject, predicate, result);
94 if(result.size() != 1)
95 throw new NoUniqueObjectException("No unique objects (" + result.size()+ ") for " +
96 subject + " -> " + predicate);
101 public Collection<Res> getTypes(Res subject) {
102 THashSet<Res> result = new THashSet<Res>();
103 rawGetObjects(subject, paths.InstanceOf, result);
104 for(Res type : result.toArray(new Res[result.size()]))
105 collectSupertypes(type, result);
109 public Collection<Res> getSupertypes(Res subject) {
110 THashSet<Res> result = new THashSet<Res>();
112 collectSupertypes(subject, result);
116 private void collectSupertypes(Res type, THashSet<Res> result) {
117 for(Res supertype : rawGetObjects(type, paths.Inherits))
118 if(result.add(supertype))
119 collectSupertypes(supertype, result);
123 public Collection<Res> getObjects(Res subject, Res predicate) {
124 ArrayList<Res> result = new ArrayList<Res>();
125 rawGetObjects(subject, predicate, result);
126 for(Res type : getTypes(subject))
127 for(Res assertion : rawGetObjects(type, paths.Asserts)) {
128 Res pred = singleRawObject(assertion, paths.HasPredicate);
129 if(equals(pred, predicate))
130 result.add(singleRawObject(assertion, paths.HasObject));
135 public Collection<Res> getAssertedObjects(Res subject, Path predicate) {
136 ArrayList<Res> result = new ArrayList<Res>();
137 for(Res type : getSupertypes(subject))
138 for(Res assertion : rawGetObjects(type, paths.Asserts)) {
139 Res pred = singleRawObject(assertion, paths.HasPredicate);
140 if(equals(pred, predicate))
141 result.add(singleRawObject(assertion, paths.HasObject));
146 private static boolean equals(Res r1, Res r2) {
147 return r1.equals(r2);
150 interface ResourceProcedure {
151 public void execute(GraphStore fragment, int id);
154 interface ResourceFunction<T> {
155 public T execute(GraphStore fragment, int id);
158 public void forEachFragmentContaining(Res resource, ResourceProcedure proc) {
159 if(resource instanceof Path) {
160 Path path = (Path)resource;
161 for(GraphStore fragment : fragments) {
162 int id = fragment.identities.pathToId(path);
164 proc.execute(fragment, id);
168 IdRes res = (IdRes)resource;
169 proc.execute(res.fragment, res.id);
173 public <T> T apply(Res resource, ResourceFunction <T> func) {
174 if(resource instanceof Path) {
175 Path path = (Path)resource;
176 for(GraphStore fragment : fragments) {
177 int id = fragment.identities.pathToId(path);
179 T value = func.execute(fragment, id);
187 IdRes res = (IdRes)resource;
188 return func.execute(res.fragment, res.id);
192 private static ResourceFunction<Datatype> getDatatype =
193 new ResourceFunction<Datatype>() {
195 public Datatype execute(GraphStore fragment, int id) {
196 return fragment.values.getDatatypeValue(id);
200 private static ResourceFunction<Variant> getValue =
201 new ResourceFunction<Variant>() {
203 public Variant execute(GraphStore fragment, int id) {
204 return fragment.values.getByteValue(id);
208 THashMap<Res, Datatype> datatypeCache = new THashMap<Res, Datatype>();
211 public Datatype getDatatype(Res resource) {
212 for(Res dt : getObjects(resource, paths.HasDatatype)) {
213 Datatype type = datatypeCache.get(dt);
215 type = apply(dt, getDatatype);
216 datatypeCache.put(dt, type);
224 public Datatype getAssertedDatatype(Res resource) {
225 for(Res dt : getAssertedObjects(resource, paths.HasDatatype)) {
226 Datatype type = datatypeCache.get(dt);
228 type = apply(dt, getDatatype);
229 datatypeCache.put(dt, type);
237 public Variant getValue(Res resource) {
238 return apply(resource, getValue);
242 public Object getValue(Res resource, Binding binding) throws NoValueException {
243 Variant value = getValue(resource);
245 throw new NoValueException();
247 return value.getValue(binding);
248 } catch (AdaptException e) {
249 throw new RuntimeAdaptException(e);
254 public void setValue(Res resource, Object value, Binding binding) {
255 final Variant variant = new Variant(binding, value);
256 apply(resource, new ResourceFunction<Object>() {
258 public Object execute(GraphStore fragment, int id) {
259 fragment.values.setValue(id, variant);
266 * Tells in how many fragments the resource occurs.
268 public int countOccurences(Res resource) {
269 if(resource instanceof IdRes)
271 else if(resource instanceof Path) {
272 Path path = (Path)resource;
274 for(GraphStore fragment : fragments)
275 if(fragment.identities.contains(path))
283 private void collectSubtypes(THashSet<Res> types, Res type) {
285 for(Res subtype : rawGetObjects(type, paths.SupertypeOf))
286 collectSubtypes(types, subtype);
290 public Collection<Res> getInstances(Res supertype) {
291 THashSet<Res> types = new THashSet<Res>();
292 collectSubtypes(types, supertype);
294 ArrayList<Res> result = new ArrayList<Res>();
296 for(GraphStore fragment : fragments) {
297 IdentityStore identities = fragment.identities;
298 StatementStore statements = fragment.statements;
300 TIntHashSet ids = new TIntHashSet(types.size());
301 for(Res type : types) {
302 if(type instanceof Path) {
303 int id = identities.pathToId((Path)type);
308 IdRes idRes = (IdRes)type;
309 if(idRes.fragment == fragment)
316 int instanceOfId = identities.pathToId(paths.InstanceOf);
320 int resourceCount = identities.getResourceCount();
322 for(int i=0;i<resourceCount;++i)
323 for(int typeId : statements.getObjects(i, instanceOfId).toArray())
324 if(ids.contains(typeId)) {
325 result.add(fragment.idToRes(i));
326 continue nextResource;
334 public Collection<Res> getChildren(Res res) {
335 if(res instanceof Path) {
336 THashSet<Res> result = new THashSet<Res>();
337 for(GraphStore store : fragments) {
338 IdentityStore ids = store.identities;
339 int id = ids.pathToId((Path)res);
341 for(int child : ids.getChildren(id))
342 result.add(store.idToRes(child));
348 return Collections.<Res>emptyList();
351 public Collection<Path> searchByPattern(String pattern) {
352 THashSet<Path> result = new THashSet<Path>();
353 PathPattern pathPattern = PathPattern.compile(pattern);
354 for(GraphStore store : fragments)
355 pathPattern.search(store.identities, result);
360 public Paths getPaths() {