diff options
Diffstat (limited to 'subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/IterateOverChildren.java')
-rw-r--r-- | subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/IterateOverChildren.java | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/IterateOverChildren.java b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/IterateOverChildren.java new file mode 100644 index 00000000..10764aea --- /dev/null +++ b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/IterateOverChildren.java | |||
@@ -0,0 +1,90 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2013, Zoltan Ujhelyi, Akos Horvath, Istvan Rath and Daniel Varro | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.localsearch.operations.extend; | ||
10 | |||
11 | import java.util.Arrays; | ||
12 | import java.util.Iterator; | ||
13 | import java.util.List; | ||
14 | import java.util.function.Function; | ||
15 | |||
16 | import org.eclipse.emf.ecore.EObject; | ||
17 | import tools.refinery.viatra.runtime.localsearch.MatchingFrame; | ||
18 | import tools.refinery.viatra.runtime.localsearch.matcher.ISearchContext; | ||
19 | import tools.refinery.viatra.runtime.localsearch.operations.ISearchOperation; | ||
20 | import tools.refinery.viatra.runtime.matchers.util.Preconditions; | ||
21 | |||
22 | /** | ||
23 | * Iterates all child elements of a selected EObjects. | ||
24 | * | ||
25 | * @author Zoltan Ujhelyi | ||
26 | * | ||
27 | */ | ||
28 | public class IterateOverChildren implements ISearchOperation { | ||
29 | |||
30 | private class Executor extends SingleValueExtendOperationExecutor<EObject> { | ||
31 | |||
32 | public Executor(int position) { | ||
33 | super(position); | ||
34 | } | ||
35 | |||
36 | @Override | ||
37 | public Iterator<EObject> getIterator(MatchingFrame frame, ISearchContext context) { | ||
38 | Preconditions.checkState(frame.get(sourcePosition) instanceof EObject, "Only children of EObject elements are supported."); | ||
39 | EObject source = (EObject) frame.get(sourcePosition); | ||
40 | if(transitive) { | ||
41 | return source.eAllContents(); | ||
42 | } else { | ||
43 | return source.eContents().iterator(); | ||
44 | } | ||
45 | } | ||
46 | |||
47 | @Override | ||
48 | public ISearchOperation getOperation() { | ||
49 | return IterateOverChildren.this; | ||
50 | } | ||
51 | } | ||
52 | |||
53 | private final int position; | ||
54 | private int sourcePosition; | ||
55 | private final boolean transitive; | ||
56 | |||
57 | /** | ||
58 | * | ||
59 | * @param position the position of the variable storing the child elements | ||
60 | * @param sourcePosition the position of the variable storing the parent root; must be bound | ||
61 | * @param transitive if true, child elements are iterated over transitively | ||
62 | */ | ||
63 | public IterateOverChildren(int position, int sourcePosition, boolean transitive) { | ||
64 | this.position = position; | ||
65 | this.sourcePosition = sourcePosition; | ||
66 | this.transitive = transitive; | ||
67 | } | ||
68 | |||
69 | @Override | ||
70 | public String toString() { | ||
71 | return toString(Object::toString); | ||
72 | } | ||
73 | |||
74 | @Override | ||
75 | public String toString(Function<Integer, String> variableMapping) { | ||
76 | return "extend containment +"+variableMapping.apply(sourcePosition)+" <>--> -"+variableMapping.apply(position)+(transitive ? " transitively" : " directly"); | ||
77 | } | ||
78 | |||
79 | @Override | ||
80 | public ISearchOperationExecutor createExecutor() { | ||
81 | return new Executor(position); | ||
82 | } | ||
83 | |||
84 | |||
85 | @Override | ||
86 | public List<Integer> getVariablePositions() { | ||
87 | return Arrays.asList(position, sourcePosition); | ||
88 | } | ||
89 | |||
90 | } \ No newline at end of file | ||