aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-dse/src/main/java/tools/refinery/store
diff options
context:
space:
mode:
authorLibravatar Attila Ficsor <33845035+nagilooh@users.noreply.github.com>2023-08-25 17:59:07 +0200
committerLibravatar GitHub <noreply@github.com>2023-08-25 17:59:07 +0200
commiteeeb6fd53776e5c25745d02fd7166a3ad8fa9979 (patch)
tree904c7a674e4f0c9945ae68871ddad101f161efa1 /subprojects/store-dse/src/main/java/tools/refinery/store
parentMerge pull request #37 from nagilooh/design-space-exploration (diff)
parentSuppressWarnings for using pseudorandom number generator in non-security sens... (diff)
downloadrefinery-eeeb6fd53776e5c25745d02fd7166a3ad8fa9979.tar.gz
refinery-eeeb6fd53776e5c25745d02fd7166a3ad8fa9979.tar.zst
refinery-eeeb6fd53776e5c25745d02fd7166a3ad8fa9979.zip
Merge pull request #38 from nagilooh/design-space-exploration
Comparable transformation rule action
Diffstat (limited to 'subprojects/store-dse/src/main/java/tools/refinery/store')
-rw-r--r--subprojects/store-dse/src/main/java/tools/refinery/store/dse/DesignSpaceExplorationAdapter.java3
-rw-r--r--subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/DesignSpaceExplorationAdapterImpl.java9
-rw-r--r--subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/TransformationRule.java2
-rw-r--r--subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/ActionVariable.java12
-rw-r--r--subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/ActivationVariable.java54
-rw-r--r--subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/AtomicAction.java18
-rw-r--r--subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/DeleteAction.java40
-rw-r--r--subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/InsertAction.java94
-rw-r--r--subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/NewItemVariable.java45
-rw-r--r--subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/TransformationAction.java129
-rw-r--r--subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/AlwaysSatisfiedRandomHardObjective.java1
11 files changed, 407 insertions, 0 deletions
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/DesignSpaceExplorationAdapter.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/DesignSpaceExplorationAdapter.java
index 5aed5298..ab87ddd5 100644
--- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/DesignSpaceExplorationAdapter.java
+++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/DesignSpaceExplorationAdapter.java
@@ -6,6 +6,7 @@
6package tools.refinery.store.dse; 6package tools.refinery.store.dse;
7 7
8import tools.refinery.store.adapter.ModelAdapter; 8import tools.refinery.store.adapter.ModelAdapter;
9import tools.refinery.store.dse.internal.TransformationRule;
9import tools.refinery.store.map.Version; 10import tools.refinery.store.map.Version;
10import tools.refinery.store.dse.internal.Activation; 11import tools.refinery.store.dse.internal.Activation;
11import tools.refinery.store.dse.internal.DesignSpaceExplorationBuilderImpl; 12import tools.refinery.store.dse.internal.DesignSpaceExplorationBuilderImpl;
@@ -65,4 +66,6 @@ public interface DesignSpaceExplorationAdapter extends ModelAdapter {
65 public void setRandom(long seed); 66 public void setRandom(long seed);
66 67
67 public List<Version> getSolutions(); 68 public List<Version> getSolutions();
69
70 void addTransformationRule(TransformationRule transformationRule);
68} 71}
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/DesignSpaceExplorationAdapterImpl.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/DesignSpaceExplorationAdapterImpl.java
index 220f0b2d..1ae09916 100644
--- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/DesignSpaceExplorationAdapterImpl.java
+++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/DesignSpaceExplorationAdapterImpl.java
@@ -13,6 +13,7 @@ import tools.refinery.store.map.Version;
13import tools.refinery.store.model.Interpretation; 13import tools.refinery.store.model.Interpretation;
14import tools.refinery.store.model.Model; 14import tools.refinery.store.model.Model;
15import tools.refinery.store.query.ModelQueryAdapter; 15import tools.refinery.store.query.ModelQueryAdapter;
16import tools.refinery.store.query.dnf.Query;
16import tools.refinery.store.query.dnf.RelationalQuery; 17import tools.refinery.store.query.dnf.RelationalQuery;
17import tools.refinery.store.dse.DesignSpaceExplorationAdapter; 18import tools.refinery.store.dse.DesignSpaceExplorationAdapter;
18import tools.refinery.store.dse.DesignSpaceExplorationStoreAdapter; 19import tools.refinery.store.dse.DesignSpaceExplorationStoreAdapter;
@@ -45,6 +46,7 @@ public class DesignSpaceExplorationAdapterImpl implements DesignSpaceExploration
45 private Map<Version, Version> parents = new HashMap<>(); 46 private Map<Version, Version> parents = new HashMap<>();
46 private final List<Version> solutions = new ArrayList<>(); 47 private final List<Version> solutions = new ArrayList<>();
47 private Map<Version, List<Activation>> statesAndTraversedActivations; 48 private Map<Version, List<Activation>> statesAndTraversedActivations;
49 @SuppressWarnings("squid:S2245")
48 private Random random = new Random(); 50 private Random random = new Random();
49 private boolean isNewState = false; 51 private boolean isNewState = false;
50 private final boolean isVisualizationEnabled; 52 private final boolean isVisualizationEnabled;
@@ -77,6 +79,12 @@ public class DesignSpaceExplorationAdapterImpl implements DesignSpaceExploration
77 79
78 } 80 }
79 81
82 @Override
83 public void addTransformationRule(TransformationRule rule) {
84 transformationRules.add(rule);
85 rule.prepare(model, queryEngine);
86 }
87
80 public List<Version> getTrajectory() { 88 public List<Version> getTrajectory() {
81 return new ArrayList<>(trajectory); 89 return new ArrayList<>(trajectory);
82 } 90 }
@@ -175,6 +183,7 @@ public class DesignSpaceExplorationAdapterImpl implements DesignSpaceExploration
175 } 183 }
176 184
177 @Override 185 @Override
186 @SuppressWarnings("squid:S2245")
178 public void setRandom(long seed) { 187 public void setRandom(long seed) {
179 this.random = new Random(seed); 188 this.random = new Random(seed);
180 } 189 }
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/TransformationRule.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/TransformationRule.java
index 8123c0d6..37117164 100644
--- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/TransformationRule.java
+++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/TransformationRule.java
@@ -26,10 +26,12 @@ public class TransformationRule {
26 private Random random; 26 private Random random;
27 private ModelQueryAdapter queryEngine; 27 private ModelQueryAdapter queryEngine;
28 28
29 @SuppressWarnings("squid:S2245")
29 public TransformationRule(String name, RelationalQuery precondition, ActionFactory actionFactory) { 30 public TransformationRule(String name, RelationalQuery precondition, ActionFactory actionFactory) {
30 this(name, precondition, actionFactory, new Random()); 31 this(name, precondition, actionFactory, new Random());
31 } 32 }
32 33
34 @SuppressWarnings("squid:S2245")
33 public TransformationRule(String name, RelationalQuery precondition, ActionFactory actionFactory, long seed) { 35 public TransformationRule(String name, RelationalQuery precondition, ActionFactory actionFactory, long seed) {
34 this(name, precondition, actionFactory, new Random(seed)); 36 this(name, precondition, actionFactory, new Random(seed));
35 } 37 }
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/ActionVariable.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/ActionVariable.java
new file mode 100644
index 00000000..92de565d
--- /dev/null
+++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/ActionVariable.java
@@ -0,0 +1,12 @@
1/*
2 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
3 *
4 * SPDX-License-Identifier: EPL-2.0
5 */
6package tools.refinery.store.dse.internal.action;
7
8import tools.refinery.store.tuple.Tuple;
9
10public interface ActionVariable extends AtomicAction {
11 Tuple getValue();
12}
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/ActivationVariable.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/ActivationVariable.java
new file mode 100644
index 00000000..6b4c6340
--- /dev/null
+++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/ActivationVariable.java
@@ -0,0 +1,54 @@
1/*
2 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
3 *
4 * SPDX-License-Identifier: EPL-2.0
5 */
6package tools.refinery.store.dse.internal.action;
7
8import tools.refinery.store.model.Model;
9import tools.refinery.store.tuple.Tuple;
10
11public class ActivationVariable implements ActionVariable {
12
13 private final int index;
14 private Tuple value;
15
16 public ActivationVariable() {
17 this(0);
18 }
19
20 public ActivationVariable(int index) {
21 this.index = index;
22 }
23
24 @Override
25 public void fire(Tuple activation) {
26 value = Tuple.of(activation.get(index));
27 }
28
29 @Override
30 public ActivationVariable prepare(Model model) {
31 return this;
32 }
33
34 @Override
35 public Tuple getValue() {
36 return value;
37 }
38
39 @Override
40 public boolean equalsWithSubstitution(AtomicAction other) {
41 if (other == null || getClass() != other.getClass()) {
42 return false;
43 }
44 var otherAction = (ActivationVariable) other;
45
46 if (index != otherAction.index) {
47 return false;
48 }
49 if (value == null) {
50 return otherAction.value == null;
51 }
52 return value.equals(otherAction.value);
53 }
54}
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/AtomicAction.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/AtomicAction.java
new file mode 100644
index 00000000..a8f10bca
--- /dev/null
+++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/AtomicAction.java
@@ -0,0 +1,18 @@
1/*
2 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
3 *
4 * SPDX-License-Identifier: EPL-2.0
5 */
6package tools.refinery.store.dse.internal.action;
7
8import tools.refinery.store.model.Model;
9import tools.refinery.store.tuple.Tuple;
10
11public interface AtomicAction {
12
13 void fire(Tuple activation);
14
15 AtomicAction prepare(Model model);
16
17 boolean equalsWithSubstitution(AtomicAction other);
18}
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
new file mode 100644
index 00000000..9900390f
--- /dev/null
+++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/DeleteAction.java
@@ -0,0 +1,40 @@
1/*
2 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
3 *
4 * SPDX-License-Identifier: EPL-2.0
5 */
6package tools.refinery.store.dse.internal.action;
7
8import tools.refinery.store.dse.DesignSpaceExplorationAdapter;
9import tools.refinery.store.model.Model;
10import tools.refinery.store.tuple.Tuple;
11
12public class DeleteAction implements AtomicAction {
13
14 private final ActionVariable variable;
15 private DesignSpaceExplorationAdapter dseAdapter;
16
17 public DeleteAction(ActionVariable variable) {
18 this.variable = variable;
19 }
20
21 @Override
22 public void fire(Tuple activation) {
23 dseAdapter.deleteObject(variable.getValue());
24 }
25
26 @Override
27 public DeleteAction prepare(Model model) {
28 dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class);
29 return this;
30 }
31
32 @Override
33 public boolean equalsWithSubstitution(AtomicAction other) {
34 if (other == null || getClass() != other.getClass()) {
35 return false;
36 }
37 var otherAction = (DeleteAction) other;
38 return this.variable.getClass() == otherAction.variable.getClass();
39 }
40}
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
new file mode 100644
index 00000000..90fcc5ac
--- /dev/null
+++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/InsertAction.java
@@ -0,0 +1,94 @@
1/*
2 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
3 *
4 * SPDX-License-Identifier: EPL-2.0
5 */
6package tools.refinery.store.dse.internal.action;
7
8import tools.refinery.store.model.Interpretation;
9import tools.refinery.store.model.Model;
10import tools.refinery.store.tuple.Tuple;
11import tools.refinery.store.tuple.Tuple0;
12
13import java.util.Arrays;
14
15public class InsertAction<T> implements AtomicAction {
16
17 private final Interpretation<T> interpretation;
18 private final T value;
19 private final int arity;
20 private final ActionVariable[] variables;
21
22 public InsertAction(Interpretation<T> interpretation, T value, ActionVariable... variables) {
23 this.interpretation = interpretation;
24 this.value = value;
25 this.variables = variables;
26 this.arity = interpretation.getSymbol().arity();
27 if (variables.length != arity) {
28 throw new IllegalArgumentException("Expected " + arity + " variables, but got " + variables.length);
29 }
30 }
31
32 @Override
33 public void fire(Tuple activation) {
34 Tuple tuple;
35 if (arity == 0) {
36 tuple = Tuple0.INSTANCE;
37 }
38 else if (arity == 1) {
39 tuple = variables[0].getValue();
40 }
41 else if (arity == 2) {
42 tuple = Tuple.of(variables[0].getValue().get(0), variables[1].getValue().get(0));
43 }
44 else if (arity == 3) {
45 tuple = Tuple.of(variables[0].getValue().get(0), variables[1].getValue().get(0), variables[2].getValue().get(0));
46 }
47 else {
48 tuple = Tuple.of(Arrays.stream(variables).map(variable -> variable.getValue().get(0))
49 .mapToInt(Integer::intValue).toArray());
50 }
51 interpretation.put(tuple, value);
52 }
53
54 public void put(Tuple tuple) {
55 interpretation.put(tuple, value);
56 }
57
58 @Override
59 public InsertAction<T> prepare(Model model) {
60 return this;
61 }
62
63 public ActionVariable[] getVariables() {
64 return variables;
65 }
66
67 @Override
68 public boolean equalsWithSubstitution(AtomicAction other) {
69 if (other == null || getClass() != other.getClass()) {
70 return false;
71 }
72 var otherAction = (InsertAction<?>) other;
73 if (variables.length != otherAction.variables.length) {
74 return false;
75 }
76 if (!interpretation.equals(otherAction.interpretation)) {
77 return false;
78 }
79 if (value == null) {
80 if (otherAction.value != null) {
81 return false;
82 }
83 }
84 else if (!value.equals(otherAction.value)) {
85 return false;
86 }
87 for (var i = 0; i < variables.length; i++) {
88 if (!variables[i].equalsWithSubstitution(otherAction.variables[i])) {
89 return false;
90 }
91 }
92 return true;
93 }
94}
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/NewItemVariable.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/NewItemVariable.java
new file mode 100644
index 00000000..cbb9697e
--- /dev/null
+++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/NewItemVariable.java
@@ -0,0 +1,45 @@
1/*
2 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
3 *
4 * SPDX-License-Identifier: EPL-2.0
5 */
6package tools.refinery.store.dse.internal.action;
7
8import tools.refinery.store.dse.DesignSpaceExplorationAdapter;
9import tools.refinery.store.model.Model;
10import tools.refinery.store.tuple.Tuple;
11import tools.refinery.store.tuple.Tuple1;
12
13public class NewItemVariable implements ActionVariable {
14 private DesignSpaceExplorationAdapter dseAdapter;
15 private Tuple1 value;
16
17 @Override
18 public void fire(Tuple activation) {
19 value = dseAdapter.createObject();
20 }
21
22 @Override
23 public NewItemVariable prepare(Model model) {
24 dseAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class);
25 return this;
26 }
27
28 @Override
29 public Tuple1 getValue() {
30 return value;
31 }
32
33 @Override
34 public boolean equalsWithSubstitution(AtomicAction other) {
35 if (other == null || getClass() != other.getClass()) {
36 return false;
37 }
38 var otherAction = (NewItemVariable) other;
39 if (value == null) {
40 return otherAction.value == null;
41 }
42 return value.equals(otherAction.value);
43
44 }
45}
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
new file mode 100644
index 00000000..adc4df9e
--- /dev/null
+++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/action/TransformationAction.java
@@ -0,0 +1,129 @@
1/*
2 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
3 *
4 * SPDX-License-Identifier: EPL-2.0
5 */
6package tools.refinery.store.dse.internal.action;
7
8import tools.refinery.store.model.Model;
9import tools.refinery.store.tuple.Tuple;
10import tools.refinery.store.tuple.Tuple2;
11
12import java.util.*;
13
14public class TransformationAction {
15 private final List<ActionVariable> actionVariables = new ArrayList<>();
16 private final List<InsertAction<?>> insertActions = new ArrayList<>();
17 private final List<DeleteAction> deleteActions = new ArrayList<>();
18 private boolean configured = false;
19 private final Map<Integer, List<Tuple2>> actionVariableUsageMap = new LinkedHashMap<>();
20
21 public TransformationAction add(ActionVariable action) {
22 checkConfigured();
23 actionVariables.add(action);
24 return this;
25 }
26
27 public TransformationAction add(InsertAction<?> action) {
28 checkConfigured();
29 insertActions.add(action);
30 return this;
31 }
32
33 public TransformationAction add(DeleteAction action) {
34 checkConfigured();
35 deleteActions.add(action);
36 return this;
37 }
38
39 private void checkConfigured() {
40 if (configured) {
41 throw new IllegalStateException("Action already configured.");
42 }
43 }
44
45 public TransformationAction prepare(Model model) {
46 for (ActionVariable action : actionVariables) {
47 action.prepare(model);
48 }
49 for (InsertAction<?> action : insertActions) {
50 action.prepare(model);
51 }
52 for (DeleteAction action : deleteActions) {
53 action.prepare(model);
54 }
55
56 for (var insertAction : insertActions) {
57 var actionIndex = insertActions.indexOf(insertAction);
58 var variables = insertAction.getVariables();
59 for (var i = 0; i < variables.length; i++) {
60 var variablelGlobalIndex = actionVariables.indexOf(variables[i]);
61 actionVariableUsageMap.computeIfAbsent(variablelGlobalIndex, k -> new ArrayList<>());
62 actionVariableUsageMap.get(variablelGlobalIndex).add(Tuple.of(actionIndex, i));
63 }
64 }
65
66 configured = true;
67 return this;
68 }
69
70 public boolean fire(Tuple activation) {
71 for (ActionVariable action : actionVariables) {
72 action.fire(activation);
73 }
74 for (InsertAction<?> action : insertActions) {
75 action.fire(activation);
76 }
77 for (DeleteAction action : deleteActions) {
78 action.fire(activation);
79 }
80 return true;
81 }
82
83 // Returns true if ActionVariables and InsertActions are inserted in same order, ActionVariables are equal (they
84 // have the same index for getting the value from the activation Tuple) and InsertActions are equal (they have
85 // the same arity and value to be set).
86 public boolean equalsWithSubstitution(TransformationAction other) {
87 if (other == this) {
88 return true;
89 }
90
91 if (actionVariables.size() != other.actionVariables.size()) {
92 return false;
93 }
94
95 if (insertActions.size() != other.insertActions.size()) {
96 return false;
97 }
98
99 if (deleteActions.size() != other.deleteActions.size()) {
100 return false;
101 }
102
103 for (var i = 0; i < actionVariables.size(); i++) {
104 var variable = actionVariables.get(i);
105 var otherVariable = other.actionVariables.get(i);
106 if (!variable.equalsWithSubstitution(otherVariable)) {
107 return false;
108 }
109 }
110
111 for (var i = 0; i < insertActions.size(); i++) {
112 var insertAction = insertActions.get(i);
113 var otherInsertAction = other.insertActions.get(i);
114 if (!insertAction.equalsWithSubstitution(otherInsertAction)) {
115 return false;
116 }
117 }
118
119 for (var i = 0; i < deleteActions.size(); i++) {
120 var deleteAction = deleteActions.get(i);
121 var otherDeleteAction = other.deleteActions.get(i);
122 if (!deleteAction.equalsWithSubstitution(otherDeleteAction)) {
123 return false;
124 }
125 }
126 return this.actionVariableUsageMap.equals(other.actionVariableUsageMap);
127
128 }
129}
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/AlwaysSatisfiedRandomHardObjective.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/AlwaysSatisfiedRandomHardObjective.java
index 327d5e2f..cdd1754f 100644
--- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/AlwaysSatisfiedRandomHardObjective.java
+++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/objectives/AlwaysSatisfiedRandomHardObjective.java
@@ -22,6 +22,7 @@ import java.util.Random;
22public class AlwaysSatisfiedRandomHardObjective extends BaseObjective { 22public class AlwaysSatisfiedRandomHardObjective extends BaseObjective {
23 23
24 private static final String DEFAULT_NAME = "AlwaysSatisfiedDummyHardObjective"; 24 private static final String DEFAULT_NAME = "AlwaysSatisfiedDummyHardObjective";
25 @SuppressWarnings("squid:S2245")
25 private static final Random random = new Random(0); 26 private static final Random random = new Random(0);
26 27
27 public AlwaysSatisfiedRandomHardObjective() { 28 public AlwaysSatisfiedRandomHardObjective() {