]> gerrit.simantics Code Review - simantics/platform.git/blob
21ccc1464de7ee82a154536c58b219d24af302ad
[simantics/platform.git] /
1 package org.simantics.graph.compiler.internal.procedures;
2
3 import gnu.trove.list.array.TIntArrayList;
4 import gnu.trove.map.hash.TIntIntHashMap;
5
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;
11
12 public class CreateInverseRelations implements Runnable {
13         IGraph graph;
14         GraphStore store;
15         
16         public CreateInverseRelations(IGraph graph, GraphStore store) {
17                 this.graph = graph;
18                 this.store = store;
19         }
20         
21         @Override
22         public void run() {
23             Paths paths = graph.getPaths();
24                 int subrelationOf = store.identities.pathToId(paths.SubrelationOf);
25                 if(subrelationOf < 0)
26                         return;
27                 
28                 int inverseOf = store.identities.createPathToId(paths.InverseOf);
29                 
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);
39                         }
40                 }
41                 boolean mod = true;
42                 while(mod) {
43                         mod = false;
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), 
51                                                         paths.InverseOf))                                               
52                                                         invSuperrelations.add(store.createResToId(invSuperrelation));
53                                         
54                                         // Create inverse
55                                         if(!invSuperrelations.isEmpty()) {
56                                                 //TIntArrayList inverses = store.statements.getObjects(id, inverseOf);
57                                                 int inverse;
58                                                 if(!inverseMap.containsKey(id)) {                                               
59                                                         if(store.identities.hasIdentity(id)) {
60                                                                 inverse = store.identities.getChild(id, "Inverse");
61                                                                 store.identities.markNew(inverse);
62                                                         }
63                                                         else
64                                                                 inverse = store.identities.newResource();
65                                                         inverseMap.put(id, inverse);
66                                                         inverseMap.put(inverse, id);
67                                                         store.statements.add(id, inverseOf, inverse);
68                                                         mod = true;
69                                                 }
70                                                 else {
71                                                         inverse = inverseMap.get(id);
72                                                         for(int curSuperrelation : store.statements.getObjects(inverse, subrelationOf).toArray()) {
73                                                                 int i = invSuperrelations.indexOf(curSuperrelation);
74                                                                 if(i >= 0) {
75                                                                         invSuperrelations.set(i, 
76                                                                                         invSuperrelations.get(invSuperrelations.size()-1));
77                                                                         invSuperrelations.removeAt(invSuperrelations.size()-1);
78                                                                 }
79                                                         }
80                                                 }                                        
81                                                 
82                                                 for(int invSuperrelation : invSuperrelations.toArray())
83                                                         store.statements.add(inverse, subrelationOf, invSuperrelation);
84                                         }
85                                 }
86                 }
87         }
88 }