1 package org.simantics.graph.compiler.internal.procedures;
3 import gnu.trove.list.array.TIntArrayList;
4 import gnu.trove.map.hash.TIntIntHashMap;
6 import org.simantics.graph.query.IGraph;
7 import org.simantics.graph.query.Paths;
8 import org.simantics.graph.query.Res;
9 import org.simantics.graph.store.GraphStore;
10 import org.simantics.graph.store.StatementStore;
12 public class CreateInverseRelations implements Runnable {
16 public CreateInverseRelations(IGraph graph, GraphStore store) {
23 Paths paths = graph.getPaths();
24 int subrelationOf = store.identities.pathToId(paths.SubrelationOf);
28 int inverseOf = store.identities.createPathToId(paths.InverseOf);
30 int resourceCount = store.identities.getResourceCount();
31 TIntIntHashMap inverseMap = new TIntIntHashMap();
32 StatementStore statements = store.statements;
33 for(int id = 0;id<resourceCount;++id) {
34 TIntArrayList objects = statements.getObjects(id, inverseOf);
35 if(!objects.isEmpty()) {
36 inverseMap.put(id, objects.get(0));
37 for(int i=0;i<objects.size();++i)
38 inverseMap.put(objects.get(i), id);
44 for(int id = 0;id<resourceCount;++id)
45 if(store.identities.isNewResource(id) || !store.identities.hasIdentity(id)) {
46 TIntArrayList superrelations =
47 store.statements.getObjects(id, subrelationOf);
48 TIntArrayList invSuperrelations = new TIntArrayList(superrelations.size());
49 for(int superrelation : superrelations.toArray())
50 for(Res invSuperrelation : graph.rawGetObjects(store.idToRes(superrelation),
52 invSuperrelations.add(store.createResToId(invSuperrelation));
55 if(!invSuperrelations.isEmpty()) {
56 //TIntArrayList inverses = store.statements.getObjects(id, inverseOf);
58 if(!inverseMap.containsKey(id)) {
59 if(store.identities.hasIdentity(id)) {
60 inverse = store.identities.getChild(id, "Inverse");
61 store.identities.markNew(inverse);
64 inverse = store.identities.newResource();
65 inverseMap.put(id, inverse);
66 inverseMap.put(inverse, id);
67 store.statements.add(id, inverseOf, inverse);
71 inverse = inverseMap.get(id);
72 for(int curSuperrelation : store.statements.getObjects(inverse, subrelationOf).toArray()) {
73 int i = invSuperrelations.indexOf(curSuperrelation);
75 invSuperrelations.set(i,
76 invSuperrelations.get(invSuperrelations.size()-1));
77 invSuperrelations.removeAt(invSuperrelations.size()-1);
82 for(int invSuperrelation : invSuperrelations.toArray())
83 store.statements.add(inverse, subrelationOf, invSuperrelation);