-
Notifications
You must be signed in to change notification settings - Fork 1
/
TypeIndexer.java
238 lines (196 loc) · 7.73 KB
/
TypeIndexer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package org.emoflon.gips.core;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.stream.Collectors;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EContentAdapter;
import org.emoflon.gips.intermediate.GipsIntermediate.GipsIntermediateModel;
import org.emoflon.gips.intermediate.GipsIntermediate.Type;
import org.emoflon.ibex.gt.api.GraphTransformationAPI;
public class TypeIndexer {
final protected GraphTransformationAPI eMoflonAPI;
final protected GipsIntermediateModel gipsModel;
protected TypeListener listener;
final protected Map<EClass, Set<EClass>> class2subclass = Collections.synchronizedMap(new LinkedHashMap<>());
final protected Map<EClass, Set<EClass>> class2superclass = Collections.synchronizedMap(new LinkedHashMap<>());
final protected Map<EClass, Set<EObject>> index = Collections.synchronizedMap(new LinkedHashMap<>());
final protected Map<String, EClass> typeByName = Collections.synchronizedMap(new LinkedHashMap<>());
protected boolean cascadingNotifications = false;
public TypeIndexer(final GraphTransformationAPI eMoflonAPI, final GipsIntermediateModel gipsModel) {
this.eMoflonAPI = eMoflonAPI;
this.gipsModel = gipsModel;
initIndex();
injectListener();
}
public void terminate() {
listener.removeListener();
}
public Set<EObject> getObjectsOfType(final EClass type) {
Set<EObject> query = Collections.synchronizedSet(new LinkedHashSet<>());
query.addAll(index.get(type));
if (!class2subclass.containsKey(type))
return query;
for (EClass cls : class2subclass.get(type)) {
query.addAll(index.get(cls));
}
return query;
}
public Set<EObject> getObjectsOfType(final String type) {
return getObjectsOfType(typeByName.get(type));
}
protected EObject putObject(final EClass type, final EObject object) {
Set<EObject> elts = index.get(type);
if (elts.add(object))
return object;
return null;
}
protected EObject removeObject(final EClass type, final EObject object) {
Set<EObject> elts = index.get(type);
if (elts.remove(object))
return object;
return null;
}
protected void resolveAdd(Notification notification) {
EObject node = (EObject) notification.getNewValue();
if (node == null) {
return;
}
if (notification.getFeature() instanceof EReference eref && (eref.isContainer() || eref.isContainment())
&& index.keySet().contains(eref.getEType())) {
node = putObject(node.eClass(), node);
}
if (node != null && !cascadingNotifications)
explore(node);
}
protected void resolveAddResource(Notification notification) {
Resource r = (Resource) notification.getNewValue();
r.getContents().forEach(node -> {
if (index.keySet().contains(node.eClass()))
node = putObject(node.eClass(), node);
if (!cascadingNotifications)
explore(node);
});
}
protected void resolveAddMany(Notification notification) {
@SuppressWarnings("unchecked")
List<EObject> addedNodes = (List<EObject>) notification.getNewValue();
addedNodes.parallelStream().forEach(node -> {
if (index.keySet().contains(node.eClass()))
node = putObject(node.eClass(), node);
if (!cascadingNotifications)
explore(node);
});
}
protected void resolveSet(Notification notification) {
if (notification instanceof Resource)
return;
if (notification.getFeature() != null && notification.getFeature() instanceof EReference eRef
&& (eRef.isContainer() || eRef.isContainment())) {
if (notification.getNewValue() != null) {
EObject node = (EObject) notification.getNewValue();
if (index.keySet().contains(node.eClass()))
node = putObject(node.eClass(), node);
if (node != null && !cascadingNotifications)
explore(node);
}
}
}
protected void resolveRemoveAdapter(Notification notification) {
EContentAdapter adapter = (EContentAdapter) notification.getOldValue();
if (adapter == null)
return;
EObject node = (EObject) notification.getNotifier();
if (index.keySet().contains(node.eClass())) {
removeObject(node.eClass(), node);
}
}
private void initIndex() {
gipsModel.getVariables().stream().filter(var -> var instanceof Type).map(var -> (Type) var)
.filter(type -> !index.containsKey(type.getType())).forEach(type -> {
index.put(type.getType(), Collections.synchronizedSet(new LinkedHashSet<>()));
typeByName.put(type.getName(), type.getType());
// Add sub-classes
Set<EClass> subclasses = type.getType().getEPackage().getEClassifiers().parallelStream()
.filter(cls -> (cls instanceof EClass)).map(cls -> (EClass) cls)
.filter(cls -> !cls.equals(type.getType()))
.filter(cls -> cls.getEAllSuperTypes().contains(type.getType()))
.collect(Collectors.toSet());
class2subclass.putIfAbsent(type.getType(), subclasses);
subclasses.stream().filter(cls -> !index.containsKey(cls)).forEach(cls -> {
index.put(cls, Collections.synchronizedSet(new LinkedHashSet<>()));
typeByName.put(cls.getName(), cls);
});
// Add super-classes
Set<EClass> superclasses = Collections.synchronizedSet(new LinkedHashSet<>());
superclasses.addAll(type.getType().getEAllSuperTypes());
class2superclass.putIfAbsent(type.getType(), subclasses);
superclasses.stream().filter(cls -> !index.containsKey(cls)).forEach(cls -> {
index.put(cls, Collections.synchronizedSet(new LinkedHashSet<>()));
typeByName.put(cls.getName(), cls);
});
// Add all sub-classes of super-classes
superclasses.stream().filter(cls -> !class2subclass.containsKey(cls)).forEach(cls -> {
Set<EClass> supersubclasses = cls.getEPackage().getEClassifiers().parallelStream()
.filter(cls2 -> (cls2 instanceof EClass)).map(cls2 -> (EClass) cls2)
.filter(cls2 -> !cls2.equals(cls))
.filter(cls2 -> cls2.getEAllSuperTypes().contains(cls)).collect(Collectors.toSet());
class2subclass.putIfAbsent(cls, supersubclasses);
supersubclasses.stream().filter(cls2 -> !index.containsKey(cls2)).forEach(cls2 -> {
index.put(cls2, Collections.synchronizedSet(new LinkedHashSet<>()));
typeByName.put(cls2.getName(), cls2);
});
});
});
eMoflonAPI.getModel().getResources().parallelStream().filter(r -> !r.getURI().toString().contains("trash.xmi"))
.forEach(r -> {
r.getContents().forEach(node -> {
if (index.keySet().contains(node.eClass()))
node = putObject(node.eClass(), node);
explore(node);
});
});
}
private void injectListener() {
listener = new TypeListener(this, eMoflonAPI.getModel());
listener.injectListener();
}
private void explore(EObject rootObj) {
if (rootObj == null)
return;
Queue<EObject> frontier = getFrontier(rootObj);
while (!frontier.isEmpty()) {
frontier = frontier.parallelStream().flatMap(child -> {
if (index.keySet().contains(child.eClass())) {
putObject(child.eClass(), child);
}
return getFrontier(child).parallelStream();
}).collect(Collectors.toCollection(LinkedList::new));
}
}
@SuppressWarnings("unchecked")
private Queue<EObject> getFrontier(final EObject current) {
Queue<EObject> frontier = new LinkedList<>();
current.eClass().getEAllContainments().forEach(ref -> {
if (ref.getUpperBound() == 1) {
// Check if EObject to add is null
if (current.eGet(ref) != null) {
frontier.add((EObject) current.eGet(ref));
}
} else {
frontier.addAll((Collection<? extends EObject>) current.eGet(ref));
}
});
return frontier;
}
}