diff options
Diffstat (limited to 'subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/nobase/ExtendToEStructuralFeatureSource.java')
-rw-r--r-- | subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/nobase/ExtendToEStructuralFeatureSource.java | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/nobase/ExtendToEStructuralFeatureSource.java b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/nobase/ExtendToEStructuralFeatureSource.java new file mode 100644 index 00000000..fc79640b --- /dev/null +++ b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/nobase/ExtendToEStructuralFeatureSource.java | |||
@@ -0,0 +1,118 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2015, Marton Bur, 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.nobase; | ||
10 | |||
11 | import java.util.Arrays; | ||
12 | import java.util.Collection; | ||
13 | import java.util.Collections; | ||
14 | import java.util.Iterator; | ||
15 | import java.util.List; | ||
16 | import java.util.function.Function; | ||
17 | |||
18 | import org.eclipse.emf.ecore.EObject; | ||
19 | import org.eclipse.emf.ecore.EReference; | ||
20 | import org.eclipse.emf.ecore.EStructuralFeature; | ||
21 | import tools.refinery.viatra.runtime.base.api.NavigationHelper; | ||
22 | import tools.refinery.viatra.runtime.localsearch.MatchingFrame; | ||
23 | import tools.refinery.viatra.runtime.localsearch.exceptions.LocalSearchException; | ||
24 | import tools.refinery.viatra.runtime.localsearch.matcher.ISearchContext; | ||
25 | import tools.refinery.viatra.runtime.localsearch.operations.ISearchOperation; | ||
26 | import tools.refinery.viatra.runtime.localsearch.operations.extend.SingleValueExtendOperationExecutor; | ||
27 | |||
28 | /** | ||
29 | * Iterates over all sources of {@link EStructuralFeature} using an {@link NavigationHelper VIATRA Base indexer}. | ||
30 | * It is assumed that the indexer is initialized for the selected {@link EStructuralFeature}. | ||
31 | * | ||
32 | */ | ||
33 | public class ExtendToEStructuralFeatureSource implements ISearchOperation { | ||
34 | |||
35 | private class Executor extends SingleValueExtendOperationExecutor<Object> { | ||
36 | |||
37 | private Executor() { | ||
38 | super(sourcePosition); | ||
39 | } | ||
40 | |||
41 | @SuppressWarnings("unchecked") | ||
42 | @Override | ||
43 | public Iterator<?> getIterator(MatchingFrame frame, ISearchContext context) { | ||
44 | if(!(feature instanceof EReference)){ | ||
45 | throw new LocalSearchException("Without base index, inverse navigation only possible along " | ||
46 | + "EReferences with defined EOpposite."); | ||
47 | } | ||
48 | EReference oppositeFeature = ((EReference)feature).getEOpposite(); | ||
49 | if(oppositeFeature == null){ | ||
50 | throw new LocalSearchException("Feature has no EOpposite, so cannot do inverse navigation " + feature.toString()); | ||
51 | } | ||
52 | try { | ||
53 | final EObject value = (EObject) frame.getValue(targetPosition); | ||
54 | if(! oppositeFeature.getEContainingClass().isSuperTypeOf(value.eClass()) ){ | ||
55 | // TODO planner should ensure the proper supertype relation | ||
56 | return Collections.emptyIterator(); | ||
57 | } | ||
58 | final Object featureValue = value.eGet(oppositeFeature); | ||
59 | if (oppositeFeature.isMany()) { | ||
60 | if (featureValue != null) { | ||
61 | final Collection<Object> objectCollection = (Collection<Object>) featureValue; | ||
62 | return objectCollection.iterator(); | ||
63 | } else { | ||
64 | return Collections.emptyIterator(); | ||
65 | } | ||
66 | } else { | ||
67 | if (featureValue != null) { | ||
68 | return Collections.singleton(featureValue).iterator(); | ||
69 | } else { | ||
70 | return Collections.emptyIterator(); | ||
71 | } | ||
72 | } | ||
73 | } catch (ClassCastException e) { | ||
74 | throw new LocalSearchException("Invalid feature target in parameter" + Integer.toString(targetPosition), e); | ||
75 | } | ||
76 | } | ||
77 | |||
78 | @Override | ||
79 | public ISearchOperation getOperation() { | ||
80 | return ExtendToEStructuralFeatureSource.this; | ||
81 | } | ||
82 | } | ||
83 | |||
84 | private int targetPosition; | ||
85 | private EStructuralFeature feature; | ||
86 | private int sourcePosition; | ||
87 | |||
88 | public ExtendToEStructuralFeatureSource(int sourcePosition, int targetPosition, EStructuralFeature feature) { | ||
89 | this.sourcePosition = sourcePosition; | ||
90 | this.targetPosition = targetPosition; | ||
91 | this.feature = feature; | ||
92 | } | ||
93 | |||
94 | public EStructuralFeature getFeature() { | ||
95 | return feature; | ||
96 | } | ||
97 | |||
98 | @Override | ||
99 | public ISearchOperationExecutor createExecutor() { | ||
100 | return new Executor(); | ||
101 | } | ||
102 | |||
103 | @Override | ||
104 | public String toString() { | ||
105 | return toString(Object::toString); | ||
106 | } | ||
107 | |||
108 | @Override | ||
109 | public String toString(Function<Integer, String> variableMapping) { | ||
110 | return "extend "+feature.getContainerClass().getSimpleName()+"."+feature.getName()+"(-"+variableMapping.apply(sourcePosition)+", +"+variableMapping.apply(targetPosition)+") iterating"; | ||
111 | } | ||
112 | |||
113 | @Override | ||
114 | public List<Integer> getVariablePositions() { | ||
115 | return Arrays.asList(sourcePosition, targetPosition); | ||
116 | } | ||
117 | |||
118 | } | ||