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