From 96a6ea2aabe8f04915daf2a680683352148c2fa3 Mon Sep 17 00:00:00 2001 From: nagilooh Date: Fri, 25 Aug 2023 13:09:30 +0200 Subject: Add delete action and additional tests for equals --- .../dse/internal/action/ActivationSymbol.java | 14 +- .../store/dse/internal/action/DeleteAction.java | 19 +- .../store/dse/internal/action/InsertAction.java | 17 +- .../store/dse/internal/action/NewItemSymbol.java | 12 +- .../dse/internal/action/TransformationAction.java | 17 ++ .../tools/refinery/store/dse/ActionEqualsTest.java | 246 ++++++++++++++++++++- 6 files changed, 316 insertions(+), 9 deletions(-) (limited to 'subprojects') diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/ActivationSymbol.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/ActivationSymbol.java index 181a5020..7598de0a 100644 --- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/ActivationSymbol.java +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/ActivationSymbol.java @@ -6,7 +6,7 @@ import tools.refinery.store.tuple.Tuple; public class ActivationSymbol extends ActionSymbol { private final int index; - private Tuple value = null; + private Tuple value; public ActivationSymbol() { this(0); @@ -36,16 +36,24 @@ public class ActivationSymbol extends ActionSymbol { if (obj == this) { return true; } - if (!(obj instanceof ActivationSymbol)) { + if (!(obj instanceof ActivationSymbol other)) { return false; } - return index == ((ActivationSymbol) obj).index; + + if (index != other.index) { + return false; + } + if (value == null) { + return other.value == null; + } + return value.equals(other.value); } @Override public int hashCode() { int result = 17; result = 31 * result + Integer.hashCode(index); + result = 31 * result + value.hashCode(); return result; } } diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/DeleteAction.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/DeleteAction.java index 589a33c4..c7cc4600 100644 --- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/DeleteAction.java +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/DeleteAction.java @@ -18,10 +18,27 @@ public class DeleteAction implements AtomicAction { dseAdapter.deleteObject(symbol.getValue(activation)); } - @Override public DeleteAction prepare(Model model) { dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); return this; } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof DeleteAction other)) { + return false; + } + return this.symbol.getClass() == other.symbol.getClass(); + } + + @Override + public int hashCode() { + int result = 17; + result = 31 * result + symbol.getClass().hashCode(); + return result; + } } 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 5c02bd45..7adab93b 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 @@ -51,13 +51,26 @@ public class InsertAction implements AtomicAction { if (!interpretation.equals(other.interpretation)) { return false; } - return value.equals(other.value); + if (value == null) { + if (other.value != null) { + return false; + } + } + else if (!value.equals(other.value)) { + return false; + } + for (var i = 0; i < symbols.length; i++) { + if (!symbols[i].equals(other.symbols[i])) { + return false; + } + } + return true; } @Override public int hashCode() { int result = 17; - result = 31 * result + Integer.hashCode(symbols.length); + result = 31 * result + Arrays.hashCode(symbols); result = 31 * result + interpretation.hashCode(); result = 31 * result + value.hashCode(); return result; diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/NewItemSymbol.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/NewItemSymbol.java index 3c69b9a0..acdc3cae 100644 --- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/NewItemSymbol.java +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/NewItemSymbol.java @@ -30,11 +30,19 @@ public class NewItemSymbol extends ActionSymbol { if (obj == this) { return true; } - return obj instanceof NewItemSymbol; + if (!(obj instanceof NewItemSymbol other)) { + return false; + } + if (value == null) { + return other.value == null; + } + return value.equals(other.value); } @Override public int hashCode() { - return 42; + int result = 17; + result = 31 * result + value.hashCode(); + return result; } } diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/TransformationAction.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/TransformationAction.java index 2e58a8c4..19ab4575 100644 --- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/TransformationAction.java +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/TransformationAction.java @@ -9,6 +9,7 @@ import java.util.*; public class TransformationAction { private final List actionSymbols = new ArrayList<>(); private final List> insertActions = new ArrayList<>(); + private final List deleteActions = new ArrayList<>(); private boolean configured = false; private final Map> activationSymbolUsageMap = new LinkedHashMap<>(); @@ -24,6 +25,12 @@ public class TransformationAction { return this; } + public TransformationAction add(DeleteAction action) { + checkConfigured(); + deleteActions.add(action); + return this; + } + private void checkConfigured() { if (configured) { throw new IllegalStateException("Action already configured."); @@ -37,6 +44,9 @@ public class TransformationAction { for (InsertAction action : insertActions) { action.prepare(model); } + for (DeleteAction action : deleteActions) { + action.prepare(model); + } for (var insertAction : insertActions) { var actionIndex = insertActions.indexOf(insertAction); @@ -59,6 +69,9 @@ public class TransformationAction { for (InsertAction action : insertActions) { action.fire(activation); } + for (DeleteAction action : deleteActions) { + action.fire(activation); + } return true; } @@ -79,6 +92,9 @@ public class TransformationAction { if (!insertActions.equals(other.insertActions)) { return false; } + if (!deleteActions.equals(other.deleteActions)) { + return false; + } return this.activationSymbolUsageMap.equals(other.activationSymbolUsageMap); } @@ -88,6 +104,7 @@ public class TransformationAction { var result = 17; result = 31 * result + actionSymbols.hashCode(); result = 31 * result + insertActions.hashCode(); + result = 31 * result + deleteActions.hashCode(); return result; } } diff --git a/subprojects/store-dse/src/test/java/tools/refinery/store/dse/ActionEqualsTest.java b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/ActionEqualsTest.java index d54bc7d3..e29260e9 100644 --- a/subprojects/store-dse/src/test/java/tools/refinery/store/dse/ActionEqualsTest.java +++ b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/ActionEqualsTest.java @@ -124,7 +124,8 @@ public class ActionEqualsTest { void actionSymbolGlobalOrderTest() { var newItemSymbol1 = new NewItemSymbol(); var activationSymbol1 = new ActivationSymbol(); - var insertAction1 = new InsertAction<>(model.getInterpretation(type1), true, newItemSymbol1, activationSymbol1); + var insertAction1 = new InsertAction<>(model.getInterpretation(type1), true, newItemSymbol1, + activationSymbol1); var action1 = new TransformationAction(); action1.add(newItemSymbol1); @@ -388,4 +389,247 @@ public class ActionEqualsTest { assertNotEquals(action1, action2); } + + @Test + void deleteActionTest() { + var newItemSymbol = new NewItemSymbol(); + var activationSymbol = new ActivationSymbol(0); + var insertAction = new InsertAction<>(model.getInterpretation(type1), false, newItemSymbol, + activationSymbol); + var deleteAction = new DeleteAction(activationSymbol); + + var action1 = new TransformationAction(); + action1.add(newItemSymbol); + action1.add(activationSymbol); + action1.add(insertAction); + action1.add(deleteAction); + action1.prepare(model); + + var action2 = new TransformationAction(); + action2.add(newItemSymbol); + action2.add(activationSymbol); + action2.add(insertAction); + action2.add(deleteAction); + action2.prepare(model); + + assertEquals(action1, action2); + } + + @Test + void deleteActionMissingTest() { + var newItemSymbol = new NewItemSymbol(); + var activationSymbol = new ActivationSymbol(0); + var insertAction = new InsertAction<>(model.getInterpretation(type1), false, newItemSymbol, + activationSymbol); + var deleteAction = new DeleteAction(activationSymbol); + + var action1 = new TransformationAction(); + action1.add(newItemSymbol); + action1.add(activationSymbol); + action1.add(insertAction); + action1.add(deleteAction); + action1.prepare(model); + + var action2 = new TransformationAction(); + action2.add(newItemSymbol); + action2.add(activationSymbol); + action2.add(insertAction); + action2.prepare(model); + + assertNotEquals(action1, action2); + } + + @Test + void deleteActionIdenticalTest() { + var newItemSymbol = new NewItemSymbol(); + var activationSymbol = new ActivationSymbol(0); + var insertAction = new InsertAction<>(model.getInterpretation(type1), false, newItemSymbol, + activationSymbol); + var deleteAction1 = new DeleteAction(activationSymbol); + var deleteAction2 = new DeleteAction(activationSymbol); + + var action1 = new TransformationAction(); + action1.add(newItemSymbol); + action1.add(activationSymbol); + action1.add(insertAction); + action1.add(deleteAction1); + action1.prepare(model); + + var action2 = new TransformationAction(); + action2.add(newItemSymbol); + action2.add(activationSymbol); + action2.add(insertAction); + action2.add(deleteAction2); + action2.prepare(model); + + assertEquals(action1, action2); + } + + @Test + void deleteActionSymbolTypeTest() { + var newItemSymbol = new NewItemSymbol(); + var activationSymbol = new ActivationSymbol(0); + var insertAction = new InsertAction<>(model.getInterpretation(type1), false, newItemSymbol, + activationSymbol); + var deleteAction1 = new DeleteAction(activationSymbol); + var deleteAction2 = new DeleteAction(newItemSymbol); + + var action1 = new TransformationAction(); + action1.add(newItemSymbol); + action1.add(activationSymbol); + action1.add(insertAction); + action1.add(deleteAction1); + action1.prepare(model); + + var action2 = new TransformationAction(); + action2.add(newItemSymbol); + action2.add(activationSymbol); + action2.add(insertAction); + action2.add(deleteAction2); + action2.prepare(model); + + assertNotEquals(action1, action2); + } + + @Test + void deleteActionOrderTest() { + var newItemSymbol = new NewItemSymbol(); + var activationSymbol = new ActivationSymbol(0); + var insertAction = new InsertAction<>(model.getInterpretation(type1), false, newItemSymbol, + activationSymbol); + var deleteAction1 = new DeleteAction(activationSymbol); + var deleteAction2 = new DeleteAction(newItemSymbol); + + var action1 = new TransformationAction(); + action1.add(newItemSymbol); + action1.add(activationSymbol); + action1.add(insertAction); + action1.add(deleteAction1); + action1.add(deleteAction2); + action1.prepare(model); + + var action2 = new TransformationAction(); + action2.add(newItemSymbol); + action2.add(activationSymbol); + action2.add(insertAction); + action2.add(deleteAction2); + action2.add(deleteAction1); + action2.prepare(model); + + assertNotEquals(action1, action2); + } + + @Test + void actionsMixedOrderTest() { + var newItemSymbol1 = new NewItemSymbol(); + var activationSymbol1 = new ActivationSymbol(); + var insertAction1 = new InsertAction<>(model.getInterpretation(type1), true, newItemSymbol1, + activationSymbol1); + var deleteAction1 = new DeleteAction(newItemSymbol1); + + var newItemSymbol2 = new NewItemSymbol(); + var activationSymbol2 = new ActivationSymbol(); + var insertAction2 = new InsertAction<>(model.getInterpretation(type1), true, newItemSymbol2, + activationSymbol2); + var deleteAction2 = new DeleteAction(activationSymbol2); + + var action1 = new TransformationAction(); + action1.add(newItemSymbol1); + action1.add(newItemSymbol2); + action1.add(activationSymbol1); + action1.add(activationSymbol2); + action1.add(insertAction1); + action1.add(insertAction2); + action1.add(deleteAction1); + action1.add(deleteAction2); + action1.prepare(model); + + var action2 = new TransformationAction(); + action2.add(deleteAction1); + action2.add(newItemSymbol1); + action2.add(insertAction1); + action2.add(newItemSymbol2); + action2.add(deleteAction2); + action2.add(activationSymbol1); + action2.add(insertAction2); + action2.add(activationSymbol2); + action2.prepare(model); + + assertEquals(action1, action2); + } + + @Test + void twoUnpreparedActionsTest() { + var newItemSymbol1 = new NewItemSymbol(); + var activationSymbol1 = new ActivationSymbol(); + var insertAction1 = new InsertAction<>(model.getInterpretation(type1), true, newItemSymbol1, + activationSymbol1); + var deleteAction1 = new DeleteAction(newItemSymbol1); + + var newItemSymbol2 = new NewItemSymbol(); + var activationSymbol2 = new ActivationSymbol(); + var insertAction2 = new InsertAction<>(model.getInterpretation(type1), true, newItemSymbol2, + activationSymbol2); + var deleteAction2 = new DeleteAction(activationSymbol2); + + var action1 = new TransformationAction(); + action1.add(newItemSymbol1); + action1.add(newItemSymbol2); + action1.add(activationSymbol1); + action1.add(activationSymbol2); + action1.add(insertAction1); + action1.add(insertAction2); + action1.add(deleteAction1); + action1.add(deleteAction2); + + var action2 = new TransformationAction(); + action2.add(deleteAction1); + action2.add(newItemSymbol1); + action2.add(insertAction1); + action2.add(newItemSymbol2); + action2.add(deleteAction2); + action2.add(activationSymbol1); + action2.add(insertAction2); + action2.add(activationSymbol2); + + assertEquals(action1, action2); + } + + @Test + void oneUnpreparedActionTest() { + var newItemSymbol1 = new NewItemSymbol(); + var activationSymbol1 = new ActivationSymbol(); + var insertAction1 = new InsertAction<>(model.getInterpretation(type1), true, newItemSymbol1, + activationSymbol1); + var deleteAction1 = new DeleteAction(newItemSymbol1); + + var newItemSymbol2 = new NewItemSymbol(); + var activationSymbol2 = new ActivationSymbol(); + var insertAction2 = new InsertAction<>(model.getInterpretation(type1), true, newItemSymbol2, + activationSymbol2); + var deleteAction2 = new DeleteAction(activationSymbol2); + + var action1 = new TransformationAction(); + action1.add(newItemSymbol1); + action1.add(newItemSymbol2); + action1.add(activationSymbol1); + action1.add(activationSymbol2); + action1.add(insertAction1); + action1.add(insertAction2); + action1.add(deleteAction1); + action1.add(deleteAction2); + action1.prepare(model); + + var action2 = new TransformationAction(); + action2.add(deleteAction1); + action2.add(newItemSymbol1); + action2.add(insertAction1); + action2.add(newItemSymbol2); + action2.add(deleteAction2); + action2.add(activationSymbol1); + action2.add(insertAction2); + action2.add(activationSymbol2); + + assertNotEquals(action1, action2); + } } -- cgit v1.2.3-70-g09d2