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