blob: ed2ff33d0abf66ae2e7230ec98a60d6345bfb892 (
plain) (
blame)
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
115
116
117
118
|
/*
* SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
*
* SPDX-License-Identifier: EPL-2.0
*/
package tools.refinery.store.dse.transition.actions;
import org.jetbrains.annotations.Nullable;
import tools.refinery.store.model.Model;
import tools.refinery.store.tuple.Tuple;
public class BoundAction {
private final Action action;
private final Model model;
private BoundActionLiteral @Nullable [] boundLiterals;
private Tuple activation;
private final int[] localVariables;
BoundAction(Action action, Model model) {
this.action = action;
this.model = model;
localVariables = new int[action.getLocalVariables().size()];
}
public boolean fire(Tuple activation) {
if (this.activation != null) {
throw new IllegalStateException("Reentrant firing is not allowed");
}
this.activation = activation;
if (boundLiterals == null) {
boundLiterals = bindLiterals();
}
try {
int size = boundLiterals.length;
for (int i = 0; i < size; i++) {
var inputAllocation = action.getInputAllocation(i);
var boundLiteral = boundLiterals[i];
var input = getInputTuple(inputAllocation);
var output = boundLiteral.fire(input);
if (output == null) {
return false;
}
var outputAllocation = this.action.getOutputAllocation(i);
setOutputTuple(outputAllocation, output);
}
} finally {
this.activation = null;
}
return true;
}
private BoundActionLiteral[] bindLiterals() {
var actionLiterals = action.getActionLiterals();
int size = actionLiterals.size();
var boundLiteralsArray = new BoundActionLiteral[size];
for (int i = 0; i < size; i++) {
boundLiteralsArray[i] = actionLiterals.get(i).bindToModel(model);
}
return boundLiteralsArray;
}
private Tuple getInputTuple(int @Nullable [] inputAllocation) {
if (inputAllocation == null) {
// Identity allocation.
return activation;
}
return switch (inputAllocation.length) {
case 0 -> Tuple.of();
case 1 -> Tuple.of(getInput(inputAllocation[0]));
case 2 -> Tuple.of(getInput(inputAllocation[0]), getInput(inputAllocation[1]));
case 3 -> Tuple.of(getInput(inputAllocation[0]), getInput(inputAllocation[1]),
getInput(inputAllocation[2]));
case 4 -> Tuple.of(getInput(inputAllocation[0]), getInput(inputAllocation[1]),
getInput(inputAllocation[2]), getInput(inputAllocation[3]));
default -> {
var elements = new int[inputAllocation.length];
for (var i = 0; i < inputAllocation.length; i++) {
elements[i] = getInput(inputAllocation[i]);
}
yield Tuple.of(elements);
}
};
}
private int getInput(int index) {
int arity = action.getArity();
return index < arity ? activation.get(index) : localVariables[index - arity];
}
private void setOutputTuple(int @Nullable [] outputAllocation, Tuple output) {
if (outputAllocation == null || outputAllocation.length == 0) {
return;
}
switch (outputAllocation.length) {
case 1 -> localVariables[outputAllocation[0]] = output.get(0);
case 2 -> {
localVariables[outputAllocation[0]] = output.get(0);
localVariables[outputAllocation[1]] = output.get(1);
}
case 3 -> {
localVariables[outputAllocation[0]] = output.get(0);
localVariables[outputAllocation[1]] = output.get(1);
localVariables[outputAllocation[2]] = output.get(2);
}
case 4 -> {
localVariables[outputAllocation[0]] = output.get(0);
localVariables[outputAllocation[1]] = output.get(1);
localVariables[outputAllocation[2]] = output.get(2);
localVariables[outputAllocation[3]] = output.get(3);
}
default -> {
for (int i = 0; i < outputAllocation.length; i++) {
localVariables[outputAllocation[i]] = output.get(i);
}
}
}
}
}
|