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
|
/*******************************************************************************
* Copyright (c) 2010-2017, Zoltan Ujhelyi, IncQuery Labs Ltd.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-v20.html.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package tools.refinery.viatra.runtime.localsearch.operations.generic;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import tools.refinery.viatra.runtime.localsearch.MatchingFrame;
import tools.refinery.viatra.runtime.localsearch.matcher.ISearchContext;
import tools.refinery.viatra.runtime.localsearch.operations.IIteratingSearchOperation;
import tools.refinery.viatra.runtime.localsearch.operations.ISearchOperation;
import tools.refinery.viatra.runtime.localsearch.operations.extend.SingleValueExtendOperationExecutor;
import tools.refinery.viatra.runtime.matchers.context.IInputKey;
import tools.refinery.viatra.runtime.matchers.tuple.TupleMask;
import tools.refinery.viatra.runtime.matchers.tuple.VolatileMaskedTuple;
import tools.refinery.viatra.runtime.matchers.util.Preconditions;
/**
* @author Zoltan Ujhelyi
* @since 1.7
* @noextend This class is not intended to be subclassed by clients.
*/
public class GenericTypeExtendSingleValue implements IIteratingSearchOperation {
private class Executor extends SingleValueExtendOperationExecutor<Object> {
private final VolatileMaskedTuple maskedTuple;
public Executor(int position) {
super(position);
this.maskedTuple = new VolatileMaskedTuple(callMask);
}
@Override
protected Iterator<? extends Object> getIterator(MatchingFrame frame, ISearchContext context) {
maskedTuple.updateTuple(frame);
return context.getRuntimeContext().enumerateValues(type, indexerMask, maskedTuple).iterator();
}
@Override
public ISearchOperation getOperation() {
return GenericTypeExtendSingleValue.this;
}
}
private final IInputKey type;
private final List<Integer> positionList;
private final TupleMask indexerMask;
private final TupleMask callMask;
private final int unboundVariableIndex;
/**
*
* @param type
* the type to execute the extend operation on
* @param positions
* the parameter positions that represent the variables of the input key
*/
public GenericTypeExtendSingleValue(IInputKey type, int[] positions, TupleMask callMask, TupleMask indexerMask, int unboundVariableIndex) {
Preconditions.checkArgument(positions.length == type.getArity(),
"The type %s requires %d parameters, but %d positions are provided", type.getPrettyPrintableName(),
type.getArity(), positions.length);
List<Integer> modifiablePositionList = new ArrayList<>();
for (int position : positions) {
modifiablePositionList.add(position);
}
this.unboundVariableIndex = unboundVariableIndex;
this.positionList = Collections.unmodifiableList(modifiablePositionList);
this.type = type;
this.callMask = callMask;
this.indexerMask = indexerMask;
}
@Override
public IInputKey getIteratedInputKey() {
return type;
}
@Override
public ISearchOperationExecutor createExecutor() {
return new Executor(unboundVariableIndex);
}
@Override
public List<Integer> getVariablePositions() {
return positionList;
}
@Override
public String toString() {
return toString(Object::toString);
}
@Override
public String toString(Function<Integer, String> variableMapping) {
return "extend " + type.getPrettyPrintableName() + "("
+ positionList.stream().map(
input -> String.format("%s%s", Objects.equals(input, unboundVariableIndex) ? "-" : "+", variableMapping.apply(input)))
.collect(Collectors.joining(", "))
+ ")";
}
}
|