blob: adc4df9e9bfab473985ac881b263e2edbede049e (
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
119
120
121
122
123
124
125
126
127
128
129
|
/*
* SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
*
* SPDX-License-Identifier: EPL-2.0
*/
package tools.refinery.store.dse.internal.action;
import tools.refinery.store.model.Model;
import tools.refinery.store.tuple.Tuple;
import tools.refinery.store.tuple.Tuple2;
import java.util.*;
public class TransformationAction {
private final List<ActionVariable> actionVariables = new ArrayList<>();
private final List<InsertAction<?>> insertActions = new ArrayList<>();
private final List<DeleteAction> deleteActions = new ArrayList<>();
private boolean configured = false;
private final Map<Integer, List<Tuple2>> actionVariableUsageMap = new LinkedHashMap<>();
public TransformationAction add(ActionVariable action) {
checkConfigured();
actionVariables.add(action);
return this;
}
public TransformationAction add(InsertAction<?> action) {
checkConfigured();
insertActions.add(action);
return this;
}
public TransformationAction add(DeleteAction action) {
checkConfigured();
deleteActions.add(action);
return this;
}
private void checkConfigured() {
if (configured) {
throw new IllegalStateException("Action already configured.");
}
}
public TransformationAction prepare(Model model) {
for (ActionVariable action : actionVariables) {
action.prepare(model);
}
for (InsertAction<?> action : insertActions) {
action.prepare(model);
}
for (DeleteAction action : deleteActions) {
action.prepare(model);
}
for (var insertAction : insertActions) {
var actionIndex = insertActions.indexOf(insertAction);
var variables = insertAction.getVariables();
for (var i = 0; i < variables.length; i++) {
var variablelGlobalIndex = actionVariables.indexOf(variables[i]);
actionVariableUsageMap.computeIfAbsent(variablelGlobalIndex, k -> new ArrayList<>());
actionVariableUsageMap.get(variablelGlobalIndex).add(Tuple.of(actionIndex, i));
}
}
configured = true;
return this;
}
public boolean fire(Tuple activation) {
for (ActionVariable action : actionVariables) {
action.fire(activation);
}
for (InsertAction<?> action : insertActions) {
action.fire(activation);
}
for (DeleteAction action : deleteActions) {
action.fire(activation);
}
return true;
}
// Returns true if ActionVariables and InsertActions are inserted in same order, ActionVariables are equal (they
// have the same index for getting the value from the activation Tuple) and InsertActions are equal (they have
// the same arity and value to be set).
public boolean equalsWithSubstitution(TransformationAction other) {
if (other == this) {
return true;
}
if (actionVariables.size() != other.actionVariables.size()) {
return false;
}
if (insertActions.size() != other.insertActions.size()) {
return false;
}
if (deleteActions.size() != other.deleteActions.size()) {
return false;
}
for (var i = 0; i < actionVariables.size(); i++) {
var variable = actionVariables.get(i);
var otherVariable = other.actionVariables.get(i);
if (!variable.equalsWithSubstitution(otherVariable)) {
return false;
}
}
for (var i = 0; i < insertActions.size(); i++) {
var insertAction = insertActions.get(i);
var otherInsertAction = other.insertActions.get(i);
if (!insertAction.equalsWithSubstitution(otherInsertAction)) {
return false;
}
}
for (var i = 0; i < deleteActions.size(); i++) {
var deleteAction = deleteActions.get(i);
var otherDeleteAction = other.deleteActions.get(i);
if (!deleteAction.equalsWithSubstitution(otherDeleteAction)) {
return false;
}
}
return this.actionVariableUsageMap.equals(other.actionVariableUsageMap);
}
}
|