aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/nobase/IterateOverEDatatypeInstances.java
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/nobase/IterateOverEDatatypeInstances.java')
-rw-r--r--subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/nobase/IterateOverEDatatypeInstances.java123
1 files changed, 123 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/nobase/IterateOverEDatatypeInstances.java b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/nobase/IterateOverEDatatypeInstances.java
new file mode 100644
index 00000000..999e0a48
--- /dev/null
+++ b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/nobase/IterateOverEDatatypeInstances.java
@@ -0,0 +1,123 @@
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 *******************************************************************************/
9package tools.refinery.viatra.runtime.localsearch.operations.extend.nobase;
10
11import java.util.Collections;
12import java.util.Iterator;
13import java.util.List;
14import java.util.Map;
15import java.util.Objects;
16import java.util.Set;
17import java.util.function.Function;
18import java.util.stream.Collectors;
19import java.util.stream.Stream;
20
21import org.eclipse.emf.ecore.EAttribute;
22import org.eclipse.emf.ecore.EClass;
23import org.eclipse.emf.ecore.EDataType;
24import org.eclipse.emf.ecore.EObject;
25import tools.refinery.viatra.runtime.base.api.NavigationHelper;
26import tools.refinery.viatra.runtime.emf.EMFScope;
27import tools.refinery.viatra.runtime.emf.types.EDataTypeInSlotsKey;
28import tools.refinery.viatra.runtime.localsearch.MatchingFrame;
29import tools.refinery.viatra.runtime.localsearch.matcher.ISearchContext;
30import tools.refinery.viatra.runtime.localsearch.operations.IIteratingSearchOperation;
31import tools.refinery.viatra.runtime.localsearch.operations.ISearchOperation;
32import tools.refinery.viatra.runtime.matchers.context.IInputKey;
33import tools.refinery.viatra.runtime.matchers.tuple.Tuple;
34import tools.refinery.viatra.runtime.matchers.tuple.Tuples;
35import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory;
36
37/**
38 * Iterates over all {@link EDataType} instances without using an {@link NavigationHelper VIATRA Base indexer}.
39 *
40 */
41public class IterateOverEDatatypeInstances implements IIteratingSearchOperation {
42
43 private class Executor extends AbstractIteratingExtendOperationExecutor<Object> {
44
45 public Executor(int position, EMFScope scope) {
46 super(position, scope);
47 }
48
49 @Override
50 public Iterator<? extends Object> getIterator(MatchingFrame frame, final ISearchContext context) {
51 return getModelContents().filter(EObject.class::isInstance).map(EObject.class::cast)
52 .map(input -> doGetEAttributes(input.eClass(), context)
53 .map(attribute -> {
54 if (attribute.isMany()) {
55 return ((List<?>) input.eGet(attribute)).stream();
56 } else {
57 Object o = input.eGet(attribute);
58 return o == null ? Stream.empty() : Stream.of(o);
59 }
60 }))
61 .flatMap(i -> i)
62 .<Object>flatMap(i -> i)
63 .iterator();
64 }
65
66 @Override
67 public ISearchOperation getOperation() {
68 return IterateOverEDatatypeInstances.this;
69 }
70 }
71
72 private final EDataType dataType;
73 private final int position;
74 private final EMFScope scope;
75
76 public IterateOverEDatatypeInstances(int position, EDataType dataType, EMFScope scope) {
77 this.position = position;
78 this.dataType = dataType;
79 this.scope = scope;
80 }
81
82 protected Stream<EAttribute> doGetEAttributes(EClass eclass, ISearchContext context){
83 @SuppressWarnings({ "unchecked"})
84 Map<Tuple, Set<EAttribute>> cache = context.accessBackendLevelCache(getClass(), Map.class, CollectionsFactory::createMap);
85 Tuple compositeKey = Tuples.staticArityFlatTupleOf(dataType, eclass);
86 return cache.computeIfAbsent(compositeKey, k ->
87 eclass.getEAllAttributes().stream().filter(input -> Objects.equals(input.getEType(), dataType)).collect(Collectors.toSet())
88 ).stream();
89 }
90
91 public EDataType getDataType() {
92 return dataType;
93 }
94
95 @Override
96 public ISearchOperationExecutor createExecutor() {
97 return new Executor(position, scope);
98 }
99
100 @Override
101 public String toString() {
102 return toString(Object::toString);
103 }
104
105 @Override
106 public String toString(Function<Integer, String> variableMapping) {
107 return "extend "+dataType.getName()+"(-"+variableMapping.apply(position)+") iterating";
108 }
109
110 @Override
111 public List<Integer> getVariablePositions() {
112 return Collections.singletonList(position);
113 }
114
115 /**
116 * @since 1.4
117 */
118 @Override
119 public IInputKey getIteratedInputKey() {
120 return new EDataTypeInSlotsKey(dataType);
121 }
122
123}