aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/InsertAction.java
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/InsertAction.java')
-rw-r--r--subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/InsertAction.java69
1 files changed, 40 insertions, 29 deletions
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/InsertAction.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/InsertAction.java
index 7adab93b..2335abad 100644
--- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/InsertAction.java
+++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/InsertAction.java
@@ -3,6 +3,7 @@ package tools.refinery.store.dse.internal.action;
3import tools.refinery.store.model.Interpretation; 3import tools.refinery.store.model.Interpretation;
4import tools.refinery.store.model.Model; 4import tools.refinery.store.model.Model;
5import tools.refinery.store.tuple.Tuple; 5import tools.refinery.store.tuple.Tuple;
6import tools.refinery.store.tuple.Tuple0;
6 7
7import java.util.Arrays; 8import java.util.Arrays;
8 9
@@ -10,21 +11,42 @@ public class InsertAction<T> implements AtomicAction {
10 11
11 private final Interpretation<T> interpretation; 12 private final Interpretation<T> interpretation;
12 private final T value; 13 private final T value;
14 private final int arity;
15 private final ActionVariable[] variables;
13 16
14 private final ActionSymbol[] symbols; 17 public InsertAction(Interpretation<T> interpretation, T value, ActionVariable... variables) {
15
16 public InsertAction(Interpretation<T> interpretation, T value, ActionSymbol... symbols) {
17 this.interpretation = interpretation; 18 this.interpretation = interpretation;
18 this.value = value; 19 this.value = value;
19 this.symbols = symbols; 20 this.variables = variables;
20 21 this.arity = interpretation.getSymbol().arity();
22 if (variables.length != arity) {
23 throw new IllegalArgumentException("Expected " + arity + " variables, but got " + variables.length);
24 }
21 } 25 }
22 26
23 @Override 27 @Override
24 public void fire(Tuple activation) { 28 public void fire(Tuple activation) {
25 var tuple = Tuple.of(Arrays.stream(symbols).map(symbol -> symbol.getValue(activation).get(0)) 29 Tuple tuple;
26 .mapToInt(Integer::intValue).toArray()); 30 if (arity == 0) {
31 tuple = Tuple0.INSTANCE;
32 }
33 else if (arity == 1) {
34 tuple = variables[0].getValue();
35 }
36 else if (arity == 2) {
37 tuple = Tuple.of(variables[0].getValue().get(0), variables[1].getValue().get(0));
38 }
39 else if (arity == 3) {
40 tuple = Tuple.of(variables[0].getValue().get(0), variables[1].getValue().get(0), variables[2].getValue().get(0));
41 }
42 else {
43 tuple = Tuple.of(Arrays.stream(variables).map(variable -> variable.getValue().get(0))
44 .mapToInt(Integer::intValue).toArray());
45 }
46 interpretation.put(tuple, value);
47 }
27 48
49 public void put(Tuple tuple) {
28 interpretation.put(tuple, value); 50 interpretation.put(tuple, value);
29 } 51 }
30 52
@@ -33,46 +55,35 @@ public class InsertAction<T> implements AtomicAction {
33 return this; 55 return this;
34 } 56 }
35 57
36 public ActionSymbol[] getSymbols() { 58 public ActionVariable[] getVariables() {
37 return symbols; 59 return variables;
38 } 60 }
39 61
40 @Override 62 @Override
41 public boolean equals(Object obj) { 63 public boolean equalsWithSubstitution(AtomicAction other) {
42 if (obj == this) { 64 if (other == null || getClass() != other.getClass()) {
43 return true;
44 }
45 if (!(obj instanceof InsertAction<?> other)) {
46 return false; 65 return false;
47 } 66 }
48 if (symbols.length != other.symbols.length) { 67 var otherAction = (InsertAction<?>) other;
68 if (variables.length != otherAction.variables.length) {
49 return false; 69 return false;
50 } 70 }
51 if (!interpretation.equals(other.interpretation)) { 71 if (!interpretation.equals(otherAction.interpretation)) {
52 return false; 72 return false;
53 } 73 }
54 if (value == null) { 74 if (value == null) {
55 if (other.value != null) { 75 if (otherAction.value != null) {
56 return false; 76 return false;
57 } 77 }
58 } 78 }
59 else if (!value.equals(other.value)) { 79 else if (!value.equals(otherAction.value)) {
60 return false; 80 return false;
61 } 81 }
62 for (var i = 0; i < symbols.length; i++) { 82 for (var i = 0; i < variables.length; i++) {
63 if (!symbols[i].equals(other.symbols[i])) { 83 if (!variables[i].equalsWithSubstitution(otherAction.variables[i])) {
64 return false; 84 return false;
65 } 85 }
66 } 86 }
67 return true; 87 return true;
68 } 88 }
69
70 @Override
71 public int hashCode() {
72 int result = 17;
73 result = 31 * result + Arrays.hashCode(symbols);
74 result = 31 * result + interpretation.hashCode();
75 result = 31 * result + value.hashCode();
76 return result;
77 }
78} 89}