diff options
Diffstat (limited to 'subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/ExtendToEStructuralFeatureSource.java')
-rw-r--r-- | subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/ExtendToEStructuralFeatureSource.java | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/ExtendToEStructuralFeatureSource.java b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/ExtendToEStructuralFeatureSource.java new file mode 100644 index 00000000..04f0a8de --- /dev/null +++ b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/ExtendToEStructuralFeatureSource.java | |||
@@ -0,0 +1,112 @@ | |||
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 | import java.util.stream.StreamSupport; | ||
16 | |||
17 | import org.eclipse.emf.ecore.EObject; | ||
18 | import org.eclipse.emf.ecore.EStructuralFeature; | ||
19 | import tools.refinery.viatra.runtime.emf.types.EStructuralFeatureInstancesKey; | ||
20 | import tools.refinery.viatra.runtime.localsearch.MatchingFrame; | ||
21 | import tools.refinery.viatra.runtime.localsearch.matcher.ISearchContext; | ||
22 | import tools.refinery.viatra.runtime.localsearch.operations.IIteratingSearchOperation; | ||
23 | import tools.refinery.viatra.runtime.localsearch.operations.ISearchOperation; | ||
24 | import tools.refinery.viatra.runtime.matchers.context.IInputKey; | ||
25 | import tools.refinery.viatra.runtime.matchers.context.IQueryRuntimeContext; | ||
26 | import tools.refinery.viatra.runtime.matchers.tuple.TupleMask; | ||
27 | import tools.refinery.viatra.runtime.matchers.tuple.VolatileMaskedTuple; | ||
28 | |||
29 | /** | ||
30 | * Iterates over all sources of {@link EStructuralFeature} using an {@link IQueryRuntimeContext VIATRA Base indexer}. | ||
31 | * It is assumed that the indexer is initialized for the selected {@link EStructuralFeature}. | ||
32 | * | ||
33 | */ | ||
34 | public class ExtendToEStructuralFeatureSource implements IIteratingSearchOperation { | ||
35 | |||
36 | private class Executor extends SingleValueExtendOperationExecutor<EObject> { | ||
37 | |||
38 | private VolatileMaskedTuple maskedTuple; | ||
39 | |||
40 | public Executor(int position) { | ||
41 | super(position); | ||
42 | this.maskedTuple = new VolatileMaskedTuple(mask); | ||
43 | } | ||
44 | |||
45 | |||
46 | @Override | ||
47 | public Iterator<EObject> getIterator(MatchingFrame frame, ISearchContext context) { | ||
48 | maskedTuple.updateTuple(frame); | ||
49 | Iterable<? extends Object> values = context.getRuntimeContext().enumerateValues(type, indexerMask, maskedTuple); | ||
50 | return StreamSupport.stream(values.spliterator(), false) | ||
51 | .filter(EObject.class::isInstance) | ||
52 | .map(EObject.class::cast) | ||
53 | .iterator(); | ||
54 | } | ||
55 | |||
56 | @Override | ||
57 | public ISearchOperation getOperation() { | ||
58 | return ExtendToEStructuralFeatureSource.this; | ||
59 | } | ||
60 | } | ||
61 | |||
62 | private final int sourcePosition; | ||
63 | private final int targetPosition; | ||
64 | private final EStructuralFeature feature; | ||
65 | private final IInputKey type; | ||
66 | private static final TupleMask indexerMask = TupleMask.fromSelectedIndices(2, new int[] {1}); | ||
67 | private final TupleMask mask; | ||
68 | |||
69 | /** | ||
70 | * @since 1.7 | ||
71 | */ | ||
72 | public ExtendToEStructuralFeatureSource(int sourcePosition, int targetPosition, EStructuralFeature feature, TupleMask mask) { | ||
73 | this.sourcePosition = sourcePosition; | ||
74 | this.targetPosition = targetPosition; | ||
75 | this.feature = feature; | ||
76 | this.mask = mask; | ||
77 | this.type = new EStructuralFeatureInstancesKey(feature); | ||
78 | } | ||
79 | |||
80 | public EStructuralFeature getFeature() { | ||
81 | return feature; | ||
82 | } | ||
83 | |||
84 | @Override | ||
85 | public ISearchOperationExecutor createExecutor() { | ||
86 | return new Executor(sourcePosition); | ||
87 | } | ||
88 | |||
89 | @Override | ||
90 | public String toString() { | ||
91 | return toString(Object::toString); | ||
92 | } | ||
93 | |||
94 | @Override | ||
95 | public String toString(Function<Integer, String> variableMapping) { | ||
96 | return "extend "+feature.getContainerClass().getSimpleName()+"."+feature.getName()+"(-"+variableMapping.apply(sourcePosition)+", +"+variableMapping.apply(targetPosition)+") indexed"; | ||
97 | } | ||
98 | |||
99 | @Override | ||
100 | public List<Integer> getVariablePositions() { | ||
101 | return Arrays.asList(sourcePosition, targetPosition); | ||
102 | } | ||
103 | |||
104 | /** | ||
105 | * @since 1.4 | ||
106 | */ | ||
107 | @Override | ||
108 | public IInputKey getIteratedInputKey() { | ||
109 | return type; | ||
110 | } | ||
111 | |||
112 | } | ||