Parent: [9d46ed] (diff)

Download this file

ArchiUtils.java    336 lines (269 with data), 12.9 kB

  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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package net.timbusproject.extractors.utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.taverna.t2.activities.stringconstant.StringConstantActivity;
import net.sf.taverna.t2.workflowmodel.Processor;
import net.sf.taverna.t2.workflowmodel.processor.activity.Activity;
import uk.ac.bolton.archimate.model.FolderType;
import uk.ac.bolton.archimate.model.IAccessRelationship;
import uk.ac.bolton.archimate.model.IArchimateElement;
import uk.ac.bolton.archimate.model.IArchimateFactory;
import uk.ac.bolton.archimate.model.IArchimateModel;
import uk.ac.bolton.archimate.model.IArtifact;
import uk.ac.bolton.archimate.model.IBusinessProcess;
import uk.ac.bolton.archimate.model.IFolder;
import uk.ac.bolton.archimate.model.IRelationship;
import com.google.common.collect.Iterators;
import com.google.common.collect.UnmodifiableIterator;
public class ArchiUtils {
/**
* Group {@link IArchimateElement}s which have a relation of type {@link IAccessRelationship} between a source and a
* target element
*
* @param processors
* @param relationsList
* @return map containing source and target element (as keys) and a list of {@link IArchimateElement} between those
* two elements.
*/
public static Map<List<IArchimateElement>, List<IArchimateElement>> groupElements(
List<IArchimateElement> processors, List<IRelationship> relationsList) {
Map<List<IArchimateElement>, List<IArchimateElement>> map = new HashMap<List<IArchimateElement>, List<IArchimateElement>>();
for (IArchimateElement source : processors) {
for (IArchimateElement target : processors) {
if (!source.equals(target)) {
// get all access relations between a source and a target element
List<IArchimateElement> elementsInBetween = ArchiUtils.getElementsInBetween(source, target,
relationsList);
if (elementsInBetween.size() > 0) {
ArrayList<IArchimateElement> key = new ArrayList<IArchimateElement>();
key.add(source);
key.add(target);
map.put(key, elementsInBetween);
}
}
}
}
return map;
}
public static boolean isListComplete(List<IArchimateElement> testList, List<IArchimateElement> completeList) {
return testList.containsAll(completeList);
}
/**
* Return all {@link IArchimateElement}s which have a relation of type {@link IAccessRelationship} between a source
* and a target element
*
* @param source
* @param target
* @param relationsList
* All relations
* @return elements inbetween
*/
public static List<IArchimateElement> getElementsInBetween(IArchimateElement source, IArchimateElement target,
List<IRelationship> relationsList) {
List<IArchimateElement> elements = new ArrayList<IArchimateElement>();
List<IRelationship> sourceRelations = ofClass(relationsList, IAccessRelationship.class, source, true);
List<IRelationship> targetRelations = ofClass(relationsList, IAccessRelationship.class, target, false);
for (IRelationship sourceRelation : sourceRelations) {
for (IRelationship targetRelation : targetRelations) {
if (!sourceRelation.getClass().equals(targetRelation.getClass())) {
break;
}
if (sourceRelation.getTarget().equals(targetRelation.getTarget())) {
elements.add(sourceRelation.getTarget());
}
}
}
return elements;
}
public static <E extends IRelationship> List<E> ofClass(List<IRelationship> relationsList, Class<?> relation) {
return ArchiUtils.ofClass(relationsList, relation, null);
}
@SuppressWarnings("unchecked")
public static <E extends IArchimateElement> List<E> elementsOfClass(List<IArchimateElement> elements, Class<?> clazz) {
List<E> sublist = new ArrayList<E>();
UnmodifiableIterator<?> iter = Iterators.filter(elements.iterator(), clazz);
while (iter.hasNext()) {
sublist.add((E) iter.next());
}
return sublist;
}
@SuppressWarnings("unchecked")
public static <E extends IRelationship> List<E> ofClass(List<IRelationship> relationsList, Class<?> relation,
IArchimateElement element) {
List<E> sublist = new ArrayList<E>();
UnmodifiableIterator<?> iter = Iterators.filter(relationsList.iterator(), relation);
while (iter.hasNext()) {
E _relation = (E) iter.next();
// not considering a specific element
if (element == null) {
sublist.add(_relation);
}
// considering specific element
if (_relation.getSource().equals(element) || _relation.getTarget().equals(element)) {
sublist.add(_relation);
}
}
return sublist;
}
@SuppressWarnings("unchecked")
public static <E extends IRelationship> List<E> ofClass(List<IRelationship> relationsList, Class<?> relation,
IArchimateElement element, boolean isOutgoing) {
List<E> sublist = new ArrayList<E>();
UnmodifiableIterator<?> iter = Iterators.filter(relationsList.iterator(), relation);
while (iter.hasNext()) {
E _relation = (E) iter.next();
// special treatment
if (relation == IAccessRelationship.class) {
IAccessRelationship _accessRelation = (IAccessRelationship) _relation;
// if (_accessRelation.getSource() != null && _accessRelation.getTarget() != null) {
// outgoing access
if (isOutgoing && _accessRelation.getSource().equals(element)
&& _accessRelation.getAccessType() == IAccessRelationship.WRITE_ACCESS) {
sublist.add((E) _accessRelation);
}
// incoming access
if (!isOutgoing && _accessRelation.getSource().equals(element)
&& _accessRelation.getAccessType() == IAccessRelationship.READ_ACCESS) {
sublist.add((E) _accessRelation);
}
// }
}
// all other relations
else {
// if (_relation.getSource() != null && _relation.getTarget() != null) {
if (_relation.getSource().equals(element) || _relation.getTarget().equals(element)) {
// outgoing
if (isOutgoing && _relation.getSource().equals(element)) {
sublist.add(_relation);
}
// incoming
if (!isOutgoing && _relation.getTarget().equals(element)) {
sublist.add(_relation);
}
}
// }
}
}
return sublist;
}
public static List<IArchimateElement> getInputElementsOfProcess(IBusinessProcess process,
List<IRelationship> relationsList) {
List<IArchimateElement> elements = new ArrayList<IArchimateElement>();
for (IRelationship _relation : ArchiUtils.ofClass(relationsList, IAccessRelationship.class, process, false)) {
elements.add(_relation.getTarget());
}
return elements;
}
public static List<IArchimateElement> getOutputElementsOfProcess(IBusinessProcess process,
List<IRelationship> relationsList) {
List<IArchimateElement> elements = new ArrayList<IArchimateElement>();
for (IRelationship _relation : ArchiUtils.ofClass(relationsList, IAccessRelationship.class, process, true)) {
elements.add(_relation.getTarget());
}
return elements;
}
public static IRelationship initRelation(IRelationship relation, IArchimateElement source,
IArchimateElement target, IFolder folder) {
relation.setSource(source);
relation.setTarget(target);
// relation.setName(source.getName() + "-->" + target.getName());
folder.getElements().add(relation);
return relation;
}
public static void removeRelation(Collection<IRelationship> relationsToRemove, Collection<IRelationship> relations,
IFolder folder) {
relations.removeAll(relationsToRemove);
folder.getElements().removeAll(relationsToRemove);
}
public static <E extends IRelationship> E initRelation(E relation, IArchimateElement source,
IArchimateElement target, IFolder folder, List<IRelationship> relationsList) {
initRelation(relation, source, target, folder);
relationsList.add(relation);
return relation;
}
public static void redirectRelations(IArchimateElement parent, IArchimateElement source, IArchimateElement target,
List<IArchimateElement> childs, List<IRelationship> relations) {
List<IRelationship> markedToRemove = new ArrayList<IRelationship>();
for (IRelationship relation : relations) {
for (IArchimateElement _target : childs) {
if (relation.getSource().equals(source) && relation.getTarget().equals(_target)
|| relation.getSource().equals(target) && relation.getTarget().equals(_target)) {
markedToRemove.add(relation);
}
}
}
relations.removeAll(markedToRemove);
}
/**
* @param accessType
* one of {@link IAccessRelationship#READ_ACCESS}, {@link IAccessRelationship#WRITE_ACCESS},
* {@link IAccessRelationship#READ_WRITE_ACCESS}, {@link IAccessRelationship#UNSPECIFIED_ACCESS}
*/
public static <E extends IAccessRelationship> E initAccessRelation(E relation, IArchimateElement source,
IArchimateElement target, IFolder folder, List<IRelationship> relationsList, int accessType) {
IAccessRelationship accessRelation = initRelation(relation, source, target, folder, relationsList);
accessRelation.setAccessType(accessType);
return relation;
}
public static IRelationship initRelationNodeElement(IRelationship relation, IArchimateElement source,
IArchimateElement target, IFolder folder, List<IArchimateElement> elementList) {
initRelation(relation, source, target, folder);
elementList.add(relation);
return relation;
}
public static boolean containsRelation(IArchimateElement targetElement, IArchimateElement sourceElement,
Collection<IRelationship> relations) {
for (IRelationship r : relations) {
if (r.getSource() == sourceElement && r.getTarget() == targetElement) {
return true;
}
}
return false;
}
public static ArrayList<IArchimateElement> getElementsByname(String name, Collection<IArchimateElement> elements) {
ArrayList<IArchimateElement> matchingElements = new ArrayList<IArchimateElement>();
for (IArchimateElement item : elements) {
if (item.getName().equals(name)) {
matchingElements.add(item);
}
}
return matchingElements;
}
public static boolean isValueProcessor(Processor processor) {
if (processor.getActivityList().size() > 0) {
for (Activity<?> activity : processor.getActivityList()) {
if (!(activity instanceof StringConstantActivity)) {
return false;
}
}
return true;
} else {
return false;
}
}
public static <E extends IArchimateElement> E initElement(E element, String name, IFolder folder,
Container container) {
// create the archimate element
element.setName(name);
folder.getElements().add(element);
container.add(element);
return element;
}
public static <E extends IArchimateElement> E initElement(E element, String name, IFolder folder,
List<E> nodeElements) {
element.setName(name);
folder.getElements().add(element);
nodeElements.add(element);
return element;
}
public static List<IArchimateElement> getSourceElements(List<IRelationship> relations) {
List<IArchimateElement> elements = new ArrayList<IArchimateElement>();
for (IRelationship relation : relations) {
elements.add(relation.getSource());
}
return elements;
}
}