diff options
author | Kristóf Marussy <kristof@marussy.com> | 2023-09-06 19:05:10 +0200 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2023-09-06 19:05:10 +0200 |
commit | 895b26df7a806a2136c2f7a46d56b542326e561f (patch) | |
tree | 52915b0bdb44a3b5d4c150050e89d987383e17e0 | |
parent | refactor: rename store-dse-visualization (diff) | |
download | refinery-895b26df7a806a2136c2f7a46d56b542326e561f.tar.gz refinery-895b26df7a806a2136c2f7a46d56b542326e561f.tar.zst refinery-895b26df7a806a2136c2f7a46d56b542326e561f.zip |
feat(dse): transformation rule builder
46 files changed, 964 insertions, 165 deletions
diff --git a/subprojects/visualization/build.gradle.kts b/subprojects/store-dse-visualization/build.gradle.kts index abad0491..abad0491 100644 --- a/subprojects/visualization/build.gradle.kts +++ b/subprojects/store-dse-visualization/build.gradle.kts | |||
diff --git a/subprojects/visualization/src/main/java/tools/refinery/visualization/ModelVisualizerAdapter.java b/subprojects/store-dse-visualization/src/main/java/tools/refinery/visualization/ModelVisualizerAdapter.java index ae87d8ac..ae87d8ac 100644 --- a/subprojects/visualization/src/main/java/tools/refinery/visualization/ModelVisualizerAdapter.java +++ b/subprojects/store-dse-visualization/src/main/java/tools/refinery/visualization/ModelVisualizerAdapter.java | |||
diff --git a/subprojects/visualization/src/main/java/tools/refinery/visualization/ModelVisualizerBuilder.java b/subprojects/store-dse-visualization/src/main/java/tools/refinery/visualization/ModelVisualizerBuilder.java index 592f5fcf..592f5fcf 100644 --- a/subprojects/visualization/src/main/java/tools/refinery/visualization/ModelVisualizerBuilder.java +++ b/subprojects/store-dse-visualization/src/main/java/tools/refinery/visualization/ModelVisualizerBuilder.java | |||
diff --git a/subprojects/visualization/src/main/java/tools/refinery/visualization/ModelVisualizerStoreAdapter.java b/subprojects/store-dse-visualization/src/main/java/tools/refinery/visualization/ModelVisualizerStoreAdapter.java index 46663b2a..46663b2a 100644 --- a/subprojects/visualization/src/main/java/tools/refinery/visualization/ModelVisualizerStoreAdapter.java +++ b/subprojects/store-dse-visualization/src/main/java/tools/refinery/visualization/ModelVisualizerStoreAdapter.java | |||
diff --git a/subprojects/visualization/src/main/java/tools/refinery/visualization/internal/FileFormat.java b/subprojects/store-dse-visualization/src/main/java/tools/refinery/visualization/internal/FileFormat.java index c5dffeb2..c5dffeb2 100644 --- a/subprojects/visualization/src/main/java/tools/refinery/visualization/internal/FileFormat.java +++ b/subprojects/store-dse-visualization/src/main/java/tools/refinery/visualization/internal/FileFormat.java | |||
diff --git a/subprojects/visualization/src/main/java/tools/refinery/visualization/internal/ModelVisualizeStoreAdapterImpl.java b/subprojects/store-dse-visualization/src/main/java/tools/refinery/visualization/internal/ModelVisualizeStoreAdapterImpl.java index 04be22d6..04be22d6 100644 --- a/subprojects/visualization/src/main/java/tools/refinery/visualization/internal/ModelVisualizeStoreAdapterImpl.java +++ b/subprojects/store-dse-visualization/src/main/java/tools/refinery/visualization/internal/ModelVisualizeStoreAdapterImpl.java | |||
diff --git a/subprojects/visualization/src/main/java/tools/refinery/visualization/internal/ModelVisualizerAdapterImpl.java b/subprojects/store-dse-visualization/src/main/java/tools/refinery/visualization/internal/ModelVisualizerAdapterImpl.java index 531969b4..531969b4 100644 --- a/subprojects/visualization/src/main/java/tools/refinery/visualization/internal/ModelVisualizerAdapterImpl.java +++ b/subprojects/store-dse-visualization/src/main/java/tools/refinery/visualization/internal/ModelVisualizerAdapterImpl.java | |||
diff --git a/subprojects/visualization/src/main/java/tools/refinery/visualization/internal/ModelVisualizerBuilderImpl.java b/subprojects/store-dse-visualization/src/main/java/tools/refinery/visualization/internal/ModelVisualizerBuilderImpl.java index e4d801d8..e4d801d8 100644 --- a/subprojects/visualization/src/main/java/tools/refinery/visualization/internal/ModelVisualizerBuilderImpl.java +++ b/subprojects/store-dse-visualization/src/main/java/tools/refinery/visualization/internal/ModelVisualizerBuilderImpl.java | |||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/ActionFactory.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/ActionFactory.java deleted file mode 100644 index 48a508b4..00000000 --- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/ActionFactory.java +++ /dev/null | |||
@@ -1,15 +0,0 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse; | ||
7 | |||
8 | import tools.refinery.store.model.Model; | ||
9 | import tools.refinery.store.tuple.Tuple; | ||
10 | |||
11 | import java.util.function.Consumer; | ||
12 | |||
13 | public interface ActionFactory { | ||
14 | Consumer<Tuple> prepare(Model model); | ||
15 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/DanglingEdges.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/DanglingEdges.java new file mode 100644 index 00000000..ac9d125b --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/DanglingEdges.java | |||
@@ -0,0 +1,12 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.modification; | ||
7 | |||
8 | public enum DanglingEdges { | ||
9 | IGNORE, | ||
10 | DELETE, | ||
11 | FAIL | ||
12 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/ModificationAdapter.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/ModificationAdapter.java index f15c16e0..58b60499 100644 --- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/ModificationAdapter.java +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/ModificationAdapter.java | |||
@@ -16,7 +16,7 @@ public interface ModificationAdapter extends ModelAdapter { | |||
16 | 16 | ||
17 | Tuple1 createObject(); | 17 | Tuple1 createObject(); |
18 | 18 | ||
19 | Tuple deleteObject(Tuple tuple); | 19 | boolean deleteObject(Tuple tuple, DanglingEdges danglingEdges); |
20 | 20 | ||
21 | static ModificationBuilder builder() { | 21 | static ModificationBuilder builder() { |
22 | return new ModificationBuilderImpl(); | 22 | return new ModificationBuilderImpl(); |
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/actions/CreateActionLiteral.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/actions/CreateActionLiteral.java new file mode 100644 index 00000000..5b86a5e1 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/actions/CreateActionLiteral.java | |||
@@ -0,0 +1,43 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.modification.actions; | ||
7 | |||
8 | import tools.refinery.store.dse.modification.ModificationAdapter; | ||
9 | import tools.refinery.store.dse.transition.actions.AbstractActionLiteral; | ||
10 | import tools.refinery.store.dse.transition.actions.BoundActionLiteral; | ||
11 | import tools.refinery.store.model.Model; | ||
12 | import tools.refinery.store.query.term.NodeVariable; | ||
13 | |||
14 | import java.util.List; | ||
15 | |||
16 | public class CreateActionLiteral extends AbstractActionLiteral { | ||
17 | private final NodeVariable variable; | ||
18 | |||
19 | public CreateActionLiteral(NodeVariable variable) { | ||
20 | |||
21 | this.variable = variable; | ||
22 | } | ||
23 | |||
24 | public NodeVariable getVariable() { | ||
25 | return variable; | ||
26 | } | ||
27 | |||
28 | @Override | ||
29 | public List<NodeVariable> getInputVariables() { | ||
30 | return List.of(); | ||
31 | } | ||
32 | |||
33 | @Override | ||
34 | public List<NodeVariable> getOutputVariables() { | ||
35 | return List.of(variable); | ||
36 | } | ||
37 | |||
38 | @Override | ||
39 | public BoundActionLiteral bindToModel(Model model) { | ||
40 | var adapter = model.getAdapter(ModificationAdapter.class); | ||
41 | return ignoredTuple -> adapter.createObject(); | ||
42 | } | ||
43 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/actions/DeleteActionLiteral.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/actions/DeleteActionLiteral.java new file mode 100644 index 00000000..18ad2b9d --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/actions/DeleteActionLiteral.java | |||
@@ -0,0 +1,51 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.modification.actions; | ||
7 | |||
8 | import tools.refinery.store.dse.modification.DanglingEdges; | ||
9 | import tools.refinery.store.dse.modification.ModificationAdapter; | ||
10 | import tools.refinery.store.dse.transition.actions.AbstractActionLiteral; | ||
11 | import tools.refinery.store.dse.transition.actions.BoundActionLiteral; | ||
12 | import tools.refinery.store.model.Model; | ||
13 | import tools.refinery.store.query.term.NodeVariable; | ||
14 | import tools.refinery.store.tuple.Tuple; | ||
15 | |||
16 | import java.util.List; | ||
17 | |||
18 | public class DeleteActionLiteral extends AbstractActionLiteral { | ||
19 | private final NodeVariable variable; | ||
20 | private final DanglingEdges danglingEdges; | ||
21 | |||
22 | public DeleteActionLiteral(NodeVariable variable, DanglingEdges danglingEdges) { | ||
23 | |||
24 | this.variable = variable; | ||
25 | this.danglingEdges = danglingEdges; | ||
26 | } | ||
27 | |||
28 | public NodeVariable getVariable() { | ||
29 | return variable; | ||
30 | } | ||
31 | |||
32 | public DanglingEdges getDanglingEdges() { | ||
33 | return danglingEdges; | ||
34 | } | ||
35 | |||
36 | @Override | ||
37 | public List<NodeVariable> getInputVariables() { | ||
38 | return List.of(variable); | ||
39 | } | ||
40 | |||
41 | @Override | ||
42 | public List<NodeVariable> getOutputVariables() { | ||
43 | return List.of(); | ||
44 | } | ||
45 | |||
46 | @Override | ||
47 | public BoundActionLiteral bindToModel(Model model) { | ||
48 | var adapter = model.getAdapter(ModificationAdapter.class); | ||
49 | return tuple -> adapter.deleteObject(tuple, danglingEdges) ? Tuple.of() : null; | ||
50 | } | ||
51 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/actions/ModificationActionLiterals.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/actions/ModificationActionLiterals.java new file mode 100644 index 00000000..31f50ac7 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/actions/ModificationActionLiterals.java | |||
@@ -0,0 +1,23 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.modification.actions; | ||
7 | |||
8 | import tools.refinery.store.dse.modification.DanglingEdges; | ||
9 | import tools.refinery.store.query.term.NodeVariable; | ||
10 | |||
11 | public class ModificationActionLiterals { | ||
12 | private ModificationActionLiterals() { | ||
13 | throw new IllegalArgumentException("This is a static utility class and should not be instantiated directly"); | ||
14 | } | ||
15 | |||
16 | public static CreateActionLiteral create(NodeVariable variable) { | ||
17 | return new CreateActionLiteral(variable); | ||
18 | } | ||
19 | |||
20 | public static DeleteActionLiteral delete(NodeVariable variable, DanglingEdges danglingEdges) { | ||
21 | return new DeleteActionLiteral(variable, danglingEdges); | ||
22 | } | ||
23 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/internal/ModificationAdapterImpl.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/internal/ModificationAdapterImpl.java index b2a80d71..4e77c462 100644 --- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/internal/ModificationAdapterImpl.java +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/internal/ModificationAdapterImpl.java | |||
@@ -6,6 +6,7 @@ | |||
6 | package tools.refinery.store.dse.modification.internal; | 6 | package tools.refinery.store.dse.modification.internal; |
7 | 7 | ||
8 | import tools.refinery.store.adapter.ModelStoreAdapter; | 8 | import tools.refinery.store.adapter.ModelStoreAdapter; |
9 | import tools.refinery.store.dse.modification.DanglingEdges; | ||
9 | import tools.refinery.store.dse.modification.ModificationAdapter; | 10 | import tools.refinery.store.dse.modification.ModificationAdapter; |
10 | import tools.refinery.store.model.Interpretation; | 11 | import tools.refinery.store.model.Interpretation; |
11 | import tools.refinery.store.model.Model; | 12 | import tools.refinery.store.model.Model; |
@@ -13,6 +14,8 @@ import tools.refinery.store.representation.Symbol; | |||
13 | import tools.refinery.store.tuple.Tuple; | 14 | import tools.refinery.store.tuple.Tuple; |
14 | import tools.refinery.store.tuple.Tuple1; | 15 | import tools.refinery.store.tuple.Tuple1; |
15 | 16 | ||
17 | import java.util.HashSet; | ||
18 | |||
16 | public class ModificationAdapterImpl implements ModificationAdapter { | 19 | public class ModificationAdapterImpl implements ModificationAdapter { |
17 | static final Symbol<Integer> NEXT_ID = Symbol.of("NEXT_ID", 0, Integer.class, 0); | 20 | static final Symbol<Integer> NEXT_ID = Symbol.of("NEXT_ID", 0, Integer.class, 0); |
18 | 21 | ||
@@ -49,14 +52,56 @@ public class ModificationAdapterImpl implements ModificationAdapter { | |||
49 | } | 52 | } |
50 | 53 | ||
51 | @Override | 54 | @Override |
52 | public Tuple deleteObject(Tuple tuple) { | 55 | public boolean deleteObject(Tuple tuple, DanglingEdges danglingEdges) { |
53 | if (tuple.getSize() != 1) { | 56 | if (tuple.getSize() != 1) { |
54 | throw new IllegalArgumentException("Tuple size must be 1"); | 57 | throw new IllegalArgumentException("Tuple size must be 1"); |
55 | } | 58 | } |
56 | // TODO: implement more efficient deletion | 59 | int objectId = tuple.get(0); |
57 | if (tuple.get(0) == getModelSize() - 1) { | 60 | if (danglingEdges == DanglingEdges.DELETE) { |
58 | nodeCountInterpretation.put(Tuple.of(), getModelSize() - 1); | 61 | deleteDanglingEdges(objectId); |
62 | } else if (danglingEdges == DanglingEdges.FAIL && hasDanglingEdges(objectId)) { | ||
63 | return false; | ||
64 | |||
65 | } | ||
66 | int modelSize = getModelSize(); | ||
67 | if (objectId == modelSize - 1) { | ||
68 | nodeCountInterpretation.put(Tuple.of(), modelSize - 1); | ||
69 | } | ||
70 | return true; | ||
71 | } | ||
72 | |||
73 | private void deleteDanglingEdges(int objectId) { | ||
74 | for (var symbol : model.getStore().getSymbols()) { | ||
75 | deleteDanglingEdges(objectId, (Symbol<?>) symbol); | ||
76 | } | ||
77 | } | ||
78 | |||
79 | private <T> void deleteDanglingEdges(int objectId, Symbol<T> symbol) { | ||
80 | var interpretation = model.getInterpretation(symbol); | ||
81 | var toDelete = new HashSet<Tuple>(); | ||
82 | int arity = symbol.arity(); | ||
83 | for (int i = 0; i < arity; i++) { | ||
84 | var cursor = interpretation.getAdjacent(i, objectId); | ||
85 | while (cursor.move()) { | ||
86 | toDelete.add(cursor.getKey()); | ||
87 | } | ||
88 | } | ||
89 | var defaultValue = symbol.defaultValue(); | ||
90 | for (var tuple : toDelete) { | ||
91 | interpretation.put(tuple, defaultValue); | ||
92 | } | ||
93 | } | ||
94 | |||
95 | private boolean hasDanglingEdges(int objectId) { | ||
96 | for (var symbol : model.getStore().getSymbols()) { | ||
97 | var interpretation = model.getInterpretation(symbol); | ||
98 | int arity = symbol.arity(); | ||
99 | for (int i = 0; i < arity; i++) { | ||
100 | if (interpretation.getAdjacentSize(i, objectId) > 0) { | ||
101 | return true; | ||
102 | } | ||
103 | } | ||
59 | } | 104 | } |
60 | return tuple; | 105 | return false; |
61 | } | 106 | } |
62 | } | 107 | } |
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/DesignSpaceExplorationBuilder.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/DesignSpaceExplorationBuilder.java index 3855a20a..800cf8f7 100644 --- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/DesignSpaceExplorationBuilder.java +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/DesignSpaceExplorationBuilder.java | |||
@@ -12,14 +12,16 @@ import tools.refinery.store.dse.transition.objectives.Objective; | |||
12 | import java.util.Collection; | 12 | import java.util.Collection; |
13 | import java.util.List; | 13 | import java.util.List; |
14 | 14 | ||
15 | // Builder pattern with methods returning {@code this} for convenience. | ||
16 | @SuppressWarnings("UnusedReturnValue") | ||
15 | public interface DesignSpaceExplorationBuilder extends ModelAdapterBuilder { | 17 | public interface DesignSpaceExplorationBuilder extends ModelAdapterBuilder { |
18 | DesignSpaceExplorationBuilder transformation(Rule transformationRuleDefinition); | ||
16 | 19 | ||
17 | DesignSpaceExplorationBuilder transformation(TransformationRule transformationRuleDefinition); | 20 | default DesignSpaceExplorationBuilder transformations(Rule... transformationRuleDefinitions) { |
18 | default DesignSpaceExplorationBuilder transformations(TransformationRule... transformationRuleDefinitions) { | ||
19 | return transformations(List.of(transformationRuleDefinitions)); | 21 | return transformations(List.of(transformationRuleDefinitions)); |
20 | } | 22 | } |
21 | 23 | ||
22 | default DesignSpaceExplorationBuilder transformations(Collection<? extends TransformationRule> transformationRules) { | 24 | default DesignSpaceExplorationBuilder transformations(Collection<? extends Rule> transformationRules) { |
23 | transformationRules.forEach(this::transformation); | 25 | transformationRules.forEach(this::transformation); |
24 | return this; | 26 | return this; |
25 | } | 27 | } |
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/DesignSpaceExplorationStoreAdapter.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/DesignSpaceExplorationStoreAdapter.java index 5c8c7a4d..fb082fae 100644 --- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/DesignSpaceExplorationStoreAdapter.java +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/DesignSpaceExplorationStoreAdapter.java | |||
@@ -17,7 +17,7 @@ public interface DesignSpaceExplorationStoreAdapter extends ModelStoreAdapter | |||
17 | @Override | 17 | @Override |
18 | DesignSpaceExplorationAdapter createModelAdapter(Model model); | 18 | DesignSpaceExplorationAdapter createModelAdapter(Model model); |
19 | 19 | ||
20 | List<TransformationRule> getTransformations(); | 20 | List<Rule> getTransformations(); |
21 | 21 | ||
22 | List<Criterion> getAccepts(); | 22 | List<Criterion> getAccepts(); |
23 | 23 | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/Rule.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/Rule.java new file mode 100644 index 00000000..ff45ed3e --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/Rule.java | |||
@@ -0,0 +1,99 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.actions.Action; | ||
9 | import tools.refinery.store.dse.transition.actions.BoundAction; | ||
10 | import tools.refinery.store.dse.transition.callback.*; | ||
11 | import tools.refinery.store.model.Model; | ||
12 | import tools.refinery.store.query.dnf.RelationalQuery; | ||
13 | |||
14 | public class Rule { | ||
15 | private final String name; | ||
16 | private final RelationalQuery precondition; | ||
17 | private final Action action; | ||
18 | |||
19 | public Rule(String name, RelationalQuery precondition, Action action) { | ||
20 | if (precondition.arity() != action.getArity()) { | ||
21 | throw new IllegalArgumentException("Expected an action clause with %d parameters, got %d instead" | ||
22 | .formatted(precondition.arity(), action.getArity())); | ||
23 | } | ||
24 | this.name = name; | ||
25 | this.precondition = precondition; | ||
26 | this.action = action; | ||
27 | } | ||
28 | |||
29 | public String getName() { | ||
30 | return name; | ||
31 | } | ||
32 | |||
33 | public RelationalQuery getPrecondition() { | ||
34 | return precondition; | ||
35 | } | ||
36 | |||
37 | public BoundAction createAction(Model model) { | ||
38 | return action.bindToModel(model); | ||
39 | } | ||
40 | |||
41 | public static RuleBuilder builder(String name) { | ||
42 | return new RuleBuilder(name); | ||
43 | } | ||
44 | |||
45 | public static RuleBuilder builder() { | ||
46 | return builder(null); | ||
47 | } | ||
48 | |||
49 | public static Rule of(String name, RuleCallback0 callback) { | ||
50 | var builder = builder(name); | ||
51 | callback.accept(builder); | ||
52 | return builder.build(); | ||
53 | } | ||
54 | |||
55 | public static Rule of(RuleCallback0 callback) { | ||
56 | return of(null, callback); | ||
57 | } | ||
58 | |||
59 | public static Rule of(String name, RuleCallback1 callback) { | ||
60 | var builder = builder(name); | ||
61 | callback.accept(builder, builder.parameter("p1")); | ||
62 | return builder.build(); | ||
63 | } | ||
64 | |||
65 | public static Rule of(RuleCallback1 callback) { | ||
66 | return of(null, callback); | ||
67 | } | ||
68 | |||
69 | public static Rule of(String name, RuleCallback2 callback) { | ||
70 | var builder = builder(name); | ||
71 | callback.accept(builder, builder.parameter("p1"), builder.parameter("p2")); | ||
72 | return builder.build(); | ||
73 | } | ||
74 | |||
75 | public static Rule of(RuleCallback2 callback) { | ||
76 | return of(null, callback); | ||
77 | } | ||
78 | |||
79 | public static Rule of(String name, RuleCallback3 callback) { | ||
80 | var builder = builder(name); | ||
81 | callback.accept(builder, builder.parameter("p1"), builder.parameter("p2"), builder.parameter("p3")); | ||
82 | return builder.build(); | ||
83 | } | ||
84 | |||
85 | public static Rule of(RuleCallback3 callback) { | ||
86 | return of(null, callback); | ||
87 | } | ||
88 | |||
89 | public static Rule of(String name, RuleCallback4 callback) { | ||
90 | var builder = builder(name); | ||
91 | callback.accept(builder, builder.parameter("p1"), builder.parameter("p2"), builder.parameter("p3"), | ||
92 | builder.parameter("p4")); | ||
93 | return builder.build(); | ||
94 | } | ||
95 | |||
96 | public static Rule of(RuleCallback4 callback) { | ||
97 | return of(null, callback); | ||
98 | } | ||
99 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/RuleBuilder.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/RuleBuilder.java new file mode 100644 index 00000000..865ac369 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/RuleBuilder.java | |||
@@ -0,0 +1,71 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.actions.Action; | ||
9 | import tools.refinery.store.dse.transition.actions.ActionLiteral; | ||
10 | import tools.refinery.store.dse.transition.callback.*; | ||
11 | import tools.refinery.store.query.dnf.AbstractQueryBuilder; | ||
12 | import tools.refinery.store.query.dnf.Dnf; | ||
13 | import tools.refinery.store.query.term.Variable; | ||
14 | |||
15 | import java.util.List; | ||
16 | |||
17 | public class RuleBuilder extends AbstractQueryBuilder<RuleBuilder> { | ||
18 | private final String name; | ||
19 | private List<ActionLiteral> action; | ||
20 | |||
21 | RuleBuilder(String name) { | ||
22 | super(Dnf.builder(name == null ? null : name + "#precondition")); | ||
23 | this.name = name; | ||
24 | } | ||
25 | |||
26 | @Override | ||
27 | protected RuleBuilder self() { | ||
28 | return this; | ||
29 | } | ||
30 | |||
31 | public RuleBuilder action(ActionLiteral... literals) { | ||
32 | return action(List.of(literals)); | ||
33 | } | ||
34 | |||
35 | public RuleBuilder action(List<? extends ActionLiteral> literals) { | ||
36 | if (this.action != null) { | ||
37 | throw new IllegalStateException("Actions have already been set"); | ||
38 | } | ||
39 | this.action = List.copyOf(literals); | ||
40 | return this; | ||
41 | } | ||
42 | |||
43 | public RuleBuilder action(Action action) { | ||
44 | return action(action.getActionLiterals()); | ||
45 | } | ||
46 | |||
47 | public RuleBuilder action(ActionCallback0 callback) { | ||
48 | return action(callback.toLiterals()); | ||
49 | } | ||
50 | |||
51 | public RuleBuilder action(ActionCallback1 callback) { | ||
52 | return action(callback.toLiterals(Variable.of("v1"))); | ||
53 | } | ||
54 | |||
55 | public RuleBuilder action(ActionCallback2 callback) { | ||
56 | return action(callback.toLiterals(Variable.of("v1"), Variable.of("v2"))); | ||
57 | } | ||
58 | |||
59 | public RuleBuilder action(ActionCallback3 callback) { | ||
60 | return action(callback.toLiterals(Variable.of("v1"), Variable.of("v2"), Variable.of("v3"))); | ||
61 | } | ||
62 | |||
63 | public RuleBuilder action(ActionCallback4 callback) { | ||
64 | return action(callback.toLiterals(Variable.of("v1"), Variable.of("v2"), Variable.of("v3"), Variable.of("v4"))); | ||
65 | } | ||
66 | |||
67 | public Rule build() { | ||
68 | var precondition = dnfBuilder.build().asRelation(); | ||
69 | return new Rule(name, precondition, Action.ofPrecondition(precondition, action)); | ||
70 | } | ||
71 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/Transformation.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/Transformation.java index ab9fda3e..0eeccbdf 100644 --- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/Transformation.java +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/Transformation.java | |||
@@ -5,26 +5,27 @@ | |||
5 | */ | 5 | */ |
6 | package tools.refinery.store.dse.transition; | 6 | package tools.refinery.store.dse.transition; |
7 | 7 | ||
8 | import tools.refinery.store.dse.transition.actions.BoundAction; | ||
9 | import tools.refinery.store.model.Model; | ||
10 | import tools.refinery.store.query.ModelQueryAdapter; | ||
8 | import tools.refinery.store.query.resultset.OrderedResultSet; | 11 | import tools.refinery.store.query.resultset.OrderedResultSet; |
9 | import tools.refinery.store.query.resultset.ResultSet; | 12 | import tools.refinery.store.query.resultset.ResultSet; |
10 | import tools.refinery.store.tuple.Tuple; | 13 | import tools.refinery.store.tuple.Tuple; |
11 | 14 | ||
12 | import java.util.function.Consumer; | ||
13 | |||
14 | public class Transformation { | 15 | public class Transformation { |
15 | private final TransformationRule definition; | 16 | private final Rule definition; |
16 | |||
17 | private final OrderedResultSet<Boolean> activations; | 17 | private final OrderedResultSet<Boolean> activations; |
18 | private final BoundAction action; | ||
18 | 19 | ||
19 | private final Consumer<Tuple> action; | 20 | public Transformation(Model model, Rule definition) { |
20 | |||
21 | public Transformation(TransformationRule definition, OrderedResultSet<Boolean> activations, Consumer<Tuple> action) { | ||
22 | this.definition = definition; | 21 | this.definition = definition; |
23 | this.activations = activations; | 22 | var precondition = definition.getPrecondition(); |
24 | this.action = action; | 23 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); |
24 | activations = new OrderedResultSet<>(queryEngine.getResultSet(precondition)); | ||
25 | action = definition.createAction(model); | ||
25 | } | 26 | } |
26 | 27 | ||
27 | public TransformationRule getDefinition() { | 28 | public Rule getDefinition() { |
28 | return definition; | 29 | return definition; |
29 | } | 30 | } |
30 | 31 | ||
@@ -37,8 +38,6 @@ public class Transformation { | |||
37 | } | 38 | } |
38 | 39 | ||
39 | public boolean fireActivation(Tuple activation) { | 40 | public boolean fireActivation(Tuple activation) { |
40 | action.accept(activation); | 41 | return action.fire(activation); |
41 | //queryEngine.flushChanges(); | ||
42 | return true; | ||
43 | } | 42 | } |
44 | } | 43 | } |
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/TransformationRule.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/TransformationRule.java deleted file mode 100644 index d64a3db1..00000000 --- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/TransformationRule.java +++ /dev/null | |||
@@ -1,63 +0,0 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition; | ||
7 | |||
8 | import tools.refinery.store.model.Model; | ||
9 | import tools.refinery.store.model.ModelStoreBuilder; | ||
10 | import tools.refinery.store.query.ModelQueryAdapter; | ||
11 | import tools.refinery.store.query.ModelQueryBuilder; | ||
12 | import tools.refinery.store.query.dnf.RelationalQuery; | ||
13 | import tools.refinery.store.dse.ActionFactory; | ||
14 | import tools.refinery.store.query.resultset.OrderedResultSet; | ||
15 | import tools.refinery.store.query.resultset.ResultSet; | ||
16 | import tools.refinery.store.tuple.Tuple; | ||
17 | |||
18 | import java.util.*; | ||
19 | |||
20 | public class TransformationRule { | ||
21 | |||
22 | private final String name; | ||
23 | private final RelationalQuery precondition; | ||
24 | private final ActionFactory actionFactory; | ||
25 | |||
26 | private Random random; | ||
27 | private ModelQueryAdapter queryEngine; | ||
28 | |||
29 | public TransformationRule(String name, RelationalQuery precondition, ActionFactory actionFactory) { | ||
30 | this(name, precondition, actionFactory, new Random()); | ||
31 | } | ||
32 | |||
33 | public TransformationRule(String name, RelationalQuery precondition, ActionFactory actionFactory, long seed) { | ||
34 | this(name, precondition, actionFactory, new Random(seed)); | ||
35 | } | ||
36 | |||
37 | public TransformationRule(String name, RelationalQuery precondition, ActionFactory actionFactory, Random random) { | ||
38 | this.name = name; | ||
39 | this.precondition = precondition; | ||
40 | this.actionFactory = actionFactory; | ||
41 | this.random = random; | ||
42 | } | ||
43 | public void doConfigure(ModelStoreBuilder storeBuilder) { | ||
44 | var queryBuilder = storeBuilder.getAdapter(ModelQueryBuilder.class); | ||
45 | queryBuilder.query(this.precondition); | ||
46 | } | ||
47 | |||
48 | public Transformation prepare(Model model) { | ||
49 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
50 | var activations = new OrderedResultSet<>(queryEngine.getResultSet(precondition)); | ||
51 | var action = actionFactory.prepare(model); | ||
52 | return new Transformation(this,activations,action); | ||
53 | } | ||
54 | |||
55 | public String getName() { | ||
56 | return name; | ||
57 | } | ||
58 | |||
59 | public RelationalQuery getPrecondition() { | ||
60 | return precondition; | ||
61 | } | ||
62 | |||
63 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/AbstractActionLiteral.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/AbstractActionLiteral.java new file mode 100644 index 00000000..e30f06bb --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/AbstractActionLiteral.java | |||
@@ -0,0 +1,9 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.actions; | ||
7 | |||
8 | public abstract class AbstractActionLiteral implements ActionLiteral { | ||
9 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/Action.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/Action.java new file mode 100644 index 00000000..d63ddfdd --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/Action.java | |||
@@ -0,0 +1,132 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.actions; | ||
7 | |||
8 | import org.eclipse.collections.api.factory.primitive.ObjectIntMaps; | ||
9 | import org.eclipse.collections.api.map.primitive.MutableObjectIntMap; | ||
10 | import org.jetbrains.annotations.Nullable; | ||
11 | import tools.refinery.store.model.Model; | ||
12 | import tools.refinery.store.query.dnf.RelationalQuery; | ||
13 | import tools.refinery.store.query.dnf.SymbolicParameter; | ||
14 | import tools.refinery.store.query.term.NodeVariable; | ||
15 | |||
16 | import java.util.*; | ||
17 | |||
18 | public class Action { | ||
19 | private final List<NodeVariable> parameters; | ||
20 | private final Set<NodeVariable> localVariables; | ||
21 | private final List<ActionLiteral> actionLiterals; | ||
22 | private final int[] @Nullable [] inputAllocations; | ||
23 | private final int[] @Nullable [] outputAllocations; | ||
24 | |||
25 | public Action(List<NodeVariable> parameters, List<? extends ActionLiteral> actionLiterals) { | ||
26 | this.parameters = List.copyOf(parameters); | ||
27 | this.actionLiterals = List.copyOf(actionLiterals); | ||
28 | var allocation = ObjectIntMaps.mutable.<NodeVariable>empty(); | ||
29 | int arity = parameters.size(); | ||
30 | for (int i = 0; i < arity; i++) { | ||
31 | allocation.put(parameters.get(i), i); | ||
32 | } | ||
33 | var mutableLocalVariables = new LinkedHashSet<NodeVariable>(); | ||
34 | int size = actionLiterals.size(); | ||
35 | inputAllocations = new int[size][]; | ||
36 | outputAllocations = new int[size][]; | ||
37 | for (int i = 0; i < size; i++) { | ||
38 | computeInputAllocation(i, parameters, allocation); | ||
39 | computeOutputAllocation(i, mutableLocalVariables, allocation); | ||
40 | } | ||
41 | this.localVariables = Collections.unmodifiableSet(mutableLocalVariables); | ||
42 | } | ||
43 | |||
44 | private void computeInputAllocation(int actionIndex, List<NodeVariable> parameters, | ||
45 | MutableObjectIntMap<NodeVariable> allocation) { | ||
46 | var actionLiteral = actionLiterals.get(actionIndex); | ||
47 | var inputVariables = actionLiteral.getInputVariables(); | ||
48 | if (inputVariables.equals(parameters)) { | ||
49 | // Identity mappings use a {@code null} allocation to pass the activation tuple unchanged. | ||
50 | return; | ||
51 | } | ||
52 | var inputs = new int[inputVariables.size()]; | ||
53 | for (int i = 0; i < inputs.length; i++) { | ||
54 | var variable = inputVariables.get(i); | ||
55 | if (!allocation.containsKey(variable)) { | ||
56 | throw new IllegalArgumentException("Unbound input variable %s of action literal %s" | ||
57 | .formatted(variable, actionLiteral)); | ||
58 | } | ||
59 | inputs[i] = allocation.get(variable); | ||
60 | } | ||
61 | inputAllocations[actionIndex] = inputs; | ||
62 | } | ||
63 | |||
64 | private void computeOutputAllocation(int actionIndex, Set<NodeVariable> mutableLocalVariable, | ||
65 | MutableObjectIntMap<NodeVariable> allocation) { | ||
66 | var actionLiteral = actionLiterals.get(actionIndex); | ||
67 | var outputVariables = actionLiteral.getOutputVariables(); | ||
68 | int size = outputVariables.size(); | ||
69 | if (size == 0) { | ||
70 | // Identity mappings use a {@code null} allocation to avoid iterating over the output tuple. | ||
71 | return; | ||
72 | } | ||
73 | if (size >= 2 && new HashSet<>(outputVariables).size() != size) { | ||
74 | throw new IllegalArgumentException("Action literal %s has duplicate output variables %s" | ||
75 | .formatted(actionLiteral, outputVariables)); | ||
76 | } | ||
77 | int arity = parameters.size(); | ||
78 | var outputs = new int[size]; | ||
79 | for (int i = 0; i < size; i++) { | ||
80 | var variable = outputVariables.get(i); | ||
81 | if (allocation.containsKey(variable)) { | ||
82 | throw new IllegalArgumentException("Output variable %s of action literal %s was already assigned" | ||
83 | .formatted(variable, actionLiteral)); | ||
84 | } | ||
85 | int variableId = mutableLocalVariable.size(); | ||
86 | allocation.put(variable, arity + variableId); | ||
87 | outputs[i] = variableId; | ||
88 | mutableLocalVariable.add(variable); | ||
89 | } | ||
90 | outputAllocations[actionIndex] = outputs; | ||
91 | } | ||
92 | |||
93 | public List<NodeVariable> getParameters() { | ||
94 | return parameters; | ||
95 | } | ||
96 | |||
97 | public int getArity() { | ||
98 | return parameters.size(); | ||
99 | } | ||
100 | |||
101 | public Set<NodeVariable> getLocalVariables() { | ||
102 | return localVariables; | ||
103 | } | ||
104 | |||
105 | public List<ActionLiteral> getActionLiterals() { | ||
106 | return actionLiterals; | ||
107 | } | ||
108 | |||
109 | int @Nullable [] getInputAllocation(int actionIndex) { | ||
110 | return inputAllocations[actionIndex]; | ||
111 | } | ||
112 | |||
113 | int @Nullable [] getOutputAllocation(int actionIndex) { | ||
114 | return outputAllocations[actionIndex]; | ||
115 | } | ||
116 | |||
117 | public BoundAction bindToModel(Model model) { | ||
118 | return new BoundAction(this, model); | ||
119 | } | ||
120 | |||
121 | public static Action ofSymbolicParameters(List<SymbolicParameter> symbolicParameters, | ||
122 | List<? extends ActionLiteral> actionLiterals) { | ||
123 | var nodeVariables = symbolicParameters.stream() | ||
124 | .map(symbolicParameter -> symbolicParameter.getVariable().asNodeVariable()) | ||
125 | .toList(); | ||
126 | return new Action(nodeVariables, actionLiterals); | ||
127 | } | ||
128 | |||
129 | public static Action ofPrecondition(RelationalQuery precondition, List<? extends ActionLiteral> actionLiterals) { | ||
130 | return ofSymbolicParameters(precondition.getDnf().getSymbolicParameters(), actionLiterals); | ||
131 | } | ||
132 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/ActionLiteral.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/ActionLiteral.java new file mode 100644 index 00000000..a721ef73 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/ActionLiteral.java | |||
@@ -0,0 +1,19 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.actions; | ||
7 | |||
8 | import tools.refinery.store.model.Model; | ||
9 | import tools.refinery.store.query.term.NodeVariable; | ||
10 | |||
11 | import java.util.List; | ||
12 | |||
13 | public interface ActionLiteral { | ||
14 | List<NodeVariable> getInputVariables(); | ||
15 | |||
16 | List<NodeVariable> getOutputVariables(); | ||
17 | |||
18 | BoundActionLiteral bindToModel(Model model); | ||
19 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/ActionLiterals.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/ActionLiterals.java new file mode 100644 index 00000000..275e1e25 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/ActionLiterals.java | |||
@@ -0,0 +1,33 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.actions; | ||
7 | |||
8 | import tools.refinery.store.query.term.NodeVariable; | ||
9 | import tools.refinery.store.representation.Symbol; | ||
10 | |||
11 | import java.util.List; | ||
12 | import java.util.Objects; | ||
13 | |||
14 | public final class ActionLiterals { | ||
15 | private ActionLiterals() { | ||
16 | throw new IllegalArgumentException("This is a static utility class and should not be instantiated directly"); | ||
17 | } | ||
18 | |||
19 | public static <T> PutActionLiteral<T> put(Symbol<T> symbol, T value, NodeVariable... parameters) { | ||
20 | return new PutActionLiteral<>(symbol, value, List.of(parameters)); | ||
21 | } | ||
22 | |||
23 | public static PutActionLiteral<Boolean> add(Symbol<Boolean> symbol, NodeVariable... parameters) { | ||
24 | if (!Objects.equals(symbol.defaultValue(), false)) { | ||
25 | throw new IllegalArgumentException("Use put to add a value to symbols other than two-valued logic"); | ||
26 | } | ||
27 | return put(symbol, true, parameters); | ||
28 | } | ||
29 | |||
30 | public static <T> PutActionLiteral<T> remove(Symbol<T> symbol, NodeVariable... parameters) { | ||
31 | return put(symbol, symbol.defaultValue(), parameters); | ||
32 | } | ||
33 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/BoundAction.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/BoundAction.java new file mode 100644 index 00000000..55f43735 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/BoundAction.java | |||
@@ -0,0 +1,109 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.actions; | ||
7 | |||
8 | import org.jetbrains.annotations.Nullable; | ||
9 | import tools.refinery.store.model.Model; | ||
10 | import tools.refinery.store.tuple.Tuple; | ||
11 | |||
12 | public class BoundAction { | ||
13 | private final Action action; | ||
14 | private final BoundActionLiteral[] boundLiterals; | ||
15 | private Tuple activation; | ||
16 | private final int[] localVariables; | ||
17 | |||
18 | BoundAction(Action action, Model model) { | ||
19 | this.action = action; | ||
20 | var actionLiterals = action.getActionLiterals(); | ||
21 | int size = actionLiterals.size(); | ||
22 | boundLiterals = new BoundActionLiteral[size]; | ||
23 | for (int i = 0; i < size; i++) { | ||
24 | boundLiterals[i] = actionLiterals.get(i).bindToModel(model); | ||
25 | } | ||
26 | localVariables = new int[action.getLocalVariables().size()]; | ||
27 | } | ||
28 | |||
29 | public boolean fire(Tuple activation) { | ||
30 | if (this.activation != null) { | ||
31 | throw new IllegalStateException("Reentrant firing is not allowed"); | ||
32 | } | ||
33 | this.activation = activation; | ||
34 | try { | ||
35 | int size = boundLiterals.length; | ||
36 | for (int i = 0; i < size; i++) { | ||
37 | var inputAllocation = action.getInputAllocation(i); | ||
38 | var boundLiteral = boundLiterals[i]; | ||
39 | var input = getInputTuple(inputAllocation); | ||
40 | var output = boundLiteral.fire(input); | ||
41 | if (output == null) { | ||
42 | return false; | ||
43 | } | ||
44 | var outputAllocation = this.action.getOutputAllocation(i); | ||
45 | setOutputTuple(outputAllocation, output); | ||
46 | } | ||
47 | } finally { | ||
48 | this.activation = null; | ||
49 | } | ||
50 | return true; | ||
51 | } | ||
52 | |||
53 | private Tuple getInputTuple(int @Nullable [] inputAllocation) { | ||
54 | if (inputAllocation == null) { | ||
55 | // Identity allocation. | ||
56 | return activation; | ||
57 | } | ||
58 | return switch (inputAllocation.length) { | ||
59 | case 0 -> Tuple.of(); | ||
60 | case 1 -> Tuple.of(getInput(inputAllocation[0])); | ||
61 | case 2 -> Tuple.of(getInput(inputAllocation[0]), getInput(inputAllocation[1])); | ||
62 | case 3 -> Tuple.of(getInput(inputAllocation[0]), getInput(inputAllocation[1]), | ||
63 | getInput(inputAllocation[2])); | ||
64 | case 4 -> Tuple.of(getInput(inputAllocation[0]), getInput(inputAllocation[1]), | ||
65 | getInput(inputAllocation[2]), getInput(inputAllocation[3])); | ||
66 | default -> { | ||
67 | var elements = new int[inputAllocation.length]; | ||
68 | for (var i = 0; i < inputAllocation.length; i++) { | ||
69 | elements[i] = getInput(inputAllocation[i]); | ||
70 | } | ||
71 | yield Tuple.of(elements); | ||
72 | } | ||
73 | }; | ||
74 | } | ||
75 | |||
76 | private int getInput(int index) { | ||
77 | int arity = action.getArity(); | ||
78 | return index < arity ? activation.get(index) : localVariables[index - arity]; | ||
79 | } | ||
80 | |||
81 | private void setOutputTuple(int @Nullable [] outputAllocation, Tuple output) { | ||
82 | if (outputAllocation == null || outputAllocation.length == 0) { | ||
83 | return; | ||
84 | } | ||
85 | switch (outputAllocation.length) { | ||
86 | case 1 -> localVariables[outputAllocation[0]] = output.get(0); | ||
87 | case 2 -> { | ||
88 | localVariables[outputAllocation[0]] = output.get(0); | ||
89 | localVariables[outputAllocation[1]] = output.get(1); | ||
90 | } | ||
91 | case 3 -> { | ||
92 | localVariables[outputAllocation[0]] = output.get(0); | ||
93 | localVariables[outputAllocation[1]] = output.get(1); | ||
94 | localVariables[outputAllocation[2]] = output.get(2); | ||
95 | } | ||
96 | case 4 -> { | ||
97 | localVariables[outputAllocation[0]] = output.get(0); | ||
98 | localVariables[outputAllocation[1]] = output.get(1); | ||
99 | localVariables[outputAllocation[2]] = output.get(2); | ||
100 | localVariables[outputAllocation[3]] = output.get(3); | ||
101 | } | ||
102 | default -> { | ||
103 | for (int i = 0; i < outputAllocation.length; i++) { | ||
104 | localVariables[outputAllocation[i]] = output.get(i); | ||
105 | } | ||
106 | } | ||
107 | } | ||
108 | } | ||
109 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/BoundActionLiteral.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/BoundActionLiteral.java new file mode 100644 index 00000000..09c3c58c --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/BoundActionLiteral.java | |||
@@ -0,0 +1,16 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.actions; | ||
7 | |||
8 | import org.jetbrains.annotations.NotNull; | ||
9 | import org.jetbrains.annotations.Nullable; | ||
10 | import tools.refinery.store.tuple.Tuple; | ||
11 | |||
12 | @FunctionalInterface | ||
13 | public interface BoundActionLiteral { | ||
14 | @Nullable | ||
15 | Tuple fire(@NotNull Tuple tuple); | ||
16 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/PutActionLiteral.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/PutActionLiteral.java new file mode 100644 index 00000000..86288921 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/actions/PutActionLiteral.java | |||
@@ -0,0 +1,64 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.actions; | ||
7 | |||
8 | import tools.refinery.store.model.Model; | ||
9 | import tools.refinery.store.query.term.NodeVariable; | ||
10 | import tools.refinery.store.representation.Symbol; | ||
11 | import tools.refinery.store.tuple.Tuple; | ||
12 | |||
13 | import java.util.List; | ||
14 | |||
15 | public class PutActionLiteral<T> extends AbstractActionLiteral { | ||
16 | private final Symbol<T> symbol; | ||
17 | private final List<NodeVariable> parameters; | ||
18 | private final T value; | ||
19 | |||
20 | public PutActionLiteral(Symbol<T> symbol, T value, List<NodeVariable> parameters) { | ||
21 | if (symbol.arity() != parameters.size()) { | ||
22 | throw new IllegalArgumentException("Expected %d parameters for symbol %s, got %d instead" | ||
23 | .formatted(symbol.arity(), symbol, parameters.size())); | ||
24 | } | ||
25 | if (value != null && !symbol.valueType().isInstance(value)) { | ||
26 | throw new IllegalArgumentException("Expected value of type %s for symbol %s, got %s of type %s instead" | ||
27 | .formatted(symbol.valueType().getName(), symbol, value, value.getClass().getName())); | ||
28 | } | ||
29 | this.symbol = symbol; | ||
30 | this.parameters = List.copyOf(parameters); | ||
31 | this.value = value; | ||
32 | } | ||
33 | |||
34 | public Symbol<T> getSymbol() { | ||
35 | return symbol; | ||
36 | } | ||
37 | |||
38 | public List<NodeVariable> getParameters() { | ||
39 | return parameters; | ||
40 | } | ||
41 | |||
42 | public T getValue() { | ||
43 | return value; | ||
44 | } | ||
45 | |||
46 | @Override | ||
47 | public List<NodeVariable> getInputVariables() { | ||
48 | return getParameters(); | ||
49 | } | ||
50 | |||
51 | @Override | ||
52 | public List<NodeVariable> getOutputVariables() { | ||
53 | return List.of(); | ||
54 | } | ||
55 | |||
56 | @Override | ||
57 | public BoundActionLiteral bindToModel(Model model) { | ||
58 | var interpretation = model.getInterpretation(symbol); | ||
59 | return tuple -> { | ||
60 | interpretation.put(tuple, value); | ||
61 | return Tuple.of(); | ||
62 | }; | ||
63 | } | ||
64 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/ActionCallback0.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/ActionCallback0.java new file mode 100644 index 00000000..1190fdeb --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/ActionCallback0.java | |||
@@ -0,0 +1,15 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.callback; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.actions.ActionLiteral; | ||
9 | |||
10 | import java.util.List; | ||
11 | |||
12 | @FunctionalInterface | ||
13 | public interface ActionCallback0 { | ||
14 | List<ActionLiteral> toLiterals(); | ||
15 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/ActionCallback1.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/ActionCallback1.java new file mode 100644 index 00000000..869f1a96 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/ActionCallback1.java | |||
@@ -0,0 +1,16 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.callback; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.actions.ActionLiteral; | ||
9 | import tools.refinery.store.query.term.NodeVariable; | ||
10 | |||
11 | import java.util.List; | ||
12 | |||
13 | @FunctionalInterface | ||
14 | public interface ActionCallback1 { | ||
15 | List<ActionLiteral> toLiterals(NodeVariable v1); | ||
16 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/ActionCallback2.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/ActionCallback2.java new file mode 100644 index 00000000..a648fc93 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/ActionCallback2.java | |||
@@ -0,0 +1,16 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.callback; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.actions.ActionLiteral; | ||
9 | import tools.refinery.store.query.term.NodeVariable; | ||
10 | |||
11 | import java.util.List; | ||
12 | |||
13 | @FunctionalInterface | ||
14 | public interface ActionCallback2 { | ||
15 | List<ActionLiteral> toLiterals(NodeVariable v1, NodeVariable v2); | ||
16 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/ActionCallback3.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/ActionCallback3.java new file mode 100644 index 00000000..a9b1d334 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/ActionCallback3.java | |||
@@ -0,0 +1,16 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.callback; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.actions.ActionLiteral; | ||
9 | import tools.refinery.store.query.term.NodeVariable; | ||
10 | |||
11 | import java.util.List; | ||
12 | |||
13 | @FunctionalInterface | ||
14 | public interface ActionCallback3 { | ||
15 | List<ActionLiteral> toLiterals(NodeVariable v1, NodeVariable v2, NodeVariable v3); | ||
16 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/ActionCallback4.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/ActionCallback4.java new file mode 100644 index 00000000..aef1351c --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/ActionCallback4.java | |||
@@ -0,0 +1,16 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.callback; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.actions.ActionLiteral; | ||
9 | import tools.refinery.store.query.term.NodeVariable; | ||
10 | |||
11 | import java.util.List; | ||
12 | |||
13 | @FunctionalInterface | ||
14 | public interface ActionCallback4 { | ||
15 | List<ActionLiteral> toLiterals(NodeVariable v1, NodeVariable v2, NodeVariable v3, NodeVariable v4); | ||
16 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/RuleCallback0.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/RuleCallback0.java new file mode 100644 index 00000000..538c23ba --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/RuleCallback0.java | |||
@@ -0,0 +1,13 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.callback; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.RuleBuilder; | ||
9 | |||
10 | @FunctionalInterface | ||
11 | public interface RuleCallback0 { | ||
12 | void accept(RuleBuilder builder); | ||
13 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/RuleCallback1.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/RuleCallback1.java new file mode 100644 index 00000000..bd7bf4f5 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/RuleCallback1.java | |||
@@ -0,0 +1,14 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.callback; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.RuleBuilder; | ||
9 | import tools.refinery.store.query.term.NodeVariable; | ||
10 | |||
11 | @FunctionalInterface | ||
12 | public interface RuleCallback1 { | ||
13 | void accept(RuleBuilder builder, NodeVariable p1); | ||
14 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/RuleCallback2.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/RuleCallback2.java new file mode 100644 index 00000000..7b02b68a --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/RuleCallback2.java | |||
@@ -0,0 +1,14 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.callback; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.RuleBuilder; | ||
9 | import tools.refinery.store.query.term.NodeVariable; | ||
10 | |||
11 | @FunctionalInterface | ||
12 | public interface RuleCallback2 { | ||
13 | void accept(RuleBuilder builder, NodeVariable p1, NodeVariable p2); | ||
14 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/RuleCallback3.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/RuleCallback3.java new file mode 100644 index 00000000..6f112d48 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/RuleCallback3.java | |||
@@ -0,0 +1,14 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.callback; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.RuleBuilder; | ||
9 | import tools.refinery.store.query.term.NodeVariable; | ||
10 | |||
11 | @FunctionalInterface | ||
12 | public interface RuleCallback3 { | ||
13 | void accept(RuleBuilder builder, NodeVariable p1, NodeVariable p2, NodeVariable p3); | ||
14 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/RuleCallback4.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/RuleCallback4.java new file mode 100644 index 00000000..dbcf8567 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/callback/RuleCallback4.java | |||
@@ -0,0 +1,14 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.callback; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.RuleBuilder; | ||
9 | import tools.refinery.store.query.term.NodeVariable; | ||
10 | |||
11 | @FunctionalInterface | ||
12 | public interface RuleCallback4 { | ||
13 | void accept(RuleBuilder builder, NodeVariable p1, NodeVariable p2, NodeVariable p3, NodeVariable p4); | ||
14 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/internal/DesignSpaceExplorationBuilderImpl.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/internal/DesignSpaceExplorationBuilderImpl.java index 4371cc03..a91f6870 100644 --- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/internal/DesignSpaceExplorationBuilderImpl.java +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/internal/DesignSpaceExplorationBuilderImpl.java | |||
@@ -7,13 +7,13 @@ package tools.refinery.store.dse.transition.internal; | |||
7 | 7 | ||
8 | import tools.refinery.store.adapter.AbstractModelAdapterBuilder; | 8 | import tools.refinery.store.adapter.AbstractModelAdapterBuilder; |
9 | import tools.refinery.store.dse.transition.DesignSpaceExplorationBuilder; | 9 | import tools.refinery.store.dse.transition.DesignSpaceExplorationBuilder; |
10 | import tools.refinery.store.dse.transition.TransformationRule; | 10 | import tools.refinery.store.dse.transition.Rule; |
11 | import tools.refinery.store.dse.transition.objectives.Criterion; | 11 | import tools.refinery.store.dse.transition.objectives.Criterion; |
12 | import tools.refinery.store.dse.transition.objectives.Objective; | 12 | import tools.refinery.store.dse.transition.objectives.Objective; |
13 | import tools.refinery.store.model.ModelStore; | 13 | import tools.refinery.store.model.ModelStore; |
14 | import tools.refinery.store.model.ModelStoreBuilder; | 14 | import tools.refinery.store.model.ModelStoreBuilder; |
15 | import tools.refinery.store.query.ModelQueryBuilder; | ||
15 | 16 | ||
16 | import java.util.ArrayList; | ||
17 | import java.util.LinkedHashSet; | 17 | import java.util.LinkedHashSet; |
18 | import java.util.List; | 18 | import java.util.List; |
19 | 19 | ||
@@ -21,13 +21,13 @@ public class DesignSpaceExplorationBuilderImpl | |||
21 | extends AbstractModelAdapterBuilder<DesignSpaceExplorationStoreAdapterImpl> | 21 | extends AbstractModelAdapterBuilder<DesignSpaceExplorationStoreAdapterImpl> |
22 | implements DesignSpaceExplorationBuilder { | 22 | implements DesignSpaceExplorationBuilder { |
23 | 23 | ||
24 | LinkedHashSet<TransformationRule> transformationRuleDefinitions = new LinkedHashSet<>(); | 24 | LinkedHashSet<Rule> transformationRuleDefinitions = new LinkedHashSet<>(); |
25 | LinkedHashSet<Criterion> accepts = new LinkedHashSet<>(); | 25 | LinkedHashSet<Criterion> accepts = new LinkedHashSet<>(); |
26 | LinkedHashSet<Criterion> excludes = new LinkedHashSet<>(); | 26 | LinkedHashSet<Criterion> excludes = new LinkedHashSet<>(); |
27 | LinkedHashSet<Objective> objectives = new LinkedHashSet<>(); | 27 | LinkedHashSet<Objective> objectives = new LinkedHashSet<>(); |
28 | 28 | ||
29 | @Override | 29 | @Override |
30 | public DesignSpaceExplorationBuilder transformation(TransformationRule transformationRuleDefinition) { | 30 | public DesignSpaceExplorationBuilder transformation(Rule transformationRuleDefinition) { |
31 | transformationRuleDefinitions.add(transformationRuleDefinition); | 31 | transformationRuleDefinitions.add(transformationRuleDefinition); |
32 | return this; | 32 | return this; |
33 | } | 33 | } |
@@ -53,23 +53,23 @@ public class DesignSpaceExplorationBuilderImpl | |||
53 | 53 | ||
54 | @Override | 54 | @Override |
55 | protected void doConfigure(ModelStoreBuilder storeBuilder) { | 55 | protected void doConfigure(ModelStoreBuilder storeBuilder) { |
56 | transformationRuleDefinitions.forEach(x -> x.doConfigure(storeBuilder)); | 56 | var queryEngine = storeBuilder.getAdapter(ModelQueryBuilder.class); |
57 | accepts.forEach(x -> x.doConfigure(storeBuilder)); | 57 | transformationRuleDefinitions.forEach(x -> queryEngine.query(x.getPrecondition())); |
58 | excludes.forEach(x -> x.doConfigure(storeBuilder)); | 58 | accepts.forEach(x -> x.configure(storeBuilder)); |
59 | objectives.forEach(x -> x.doConfigure(storeBuilder)); | 59 | excludes.forEach(x -> x.configure(storeBuilder)); |
60 | objectives.forEach(x -> x.configure(storeBuilder)); | ||
60 | 61 | ||
61 | super.doConfigure(storeBuilder); | 62 | super.doConfigure(storeBuilder); |
62 | } | 63 | } |
63 | 64 | ||
64 | @Override | 65 | @Override |
65 | protected DesignSpaceExplorationStoreAdapterImpl doBuild(ModelStore store) { | 66 | protected DesignSpaceExplorationStoreAdapterImpl doBuild(ModelStore store) { |
66 | List<TransformationRule> transformationRuleDefinitiions1 = new ArrayList<>(transformationRuleDefinitions); | 67 | List<Rule> transformationRuleDefinitionsList = List.copyOf(transformationRuleDefinitions); |
67 | List<Criterion> accepts1 = new ArrayList<>(accepts); | 68 | List<Criterion> acceptsList = List.copyOf(accepts); |
68 | List<Criterion> excludes1 = new ArrayList<>(excludes); | 69 | List<Criterion> excludesList = List.copyOf(excludes); |
69 | List<Objective> objectives1 = new ArrayList<>(objectives); | 70 | List<Objective> objectivesList = List.copyOf(objectives); |
70 | 71 | ||
71 | return new DesignSpaceExplorationStoreAdapterImpl(store, | 72 | return new DesignSpaceExplorationStoreAdapterImpl(store, transformationRuleDefinitionsList, acceptsList, |
72 | transformationRuleDefinitiions1, accepts1, | 73 | excludesList, objectivesList); |
73 | excludes1, objectives1); | ||
74 | } | 74 | } |
75 | } | 75 | } |
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/internal/DesignSpaceExplorationStoreAdapterImpl.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/internal/DesignSpaceExplorationStoreAdapterImpl.java index 3319e148..bd85e7a6 100644 --- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/internal/DesignSpaceExplorationStoreAdapterImpl.java +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/internal/DesignSpaceExplorationStoreAdapterImpl.java | |||
@@ -7,7 +7,7 @@ package tools.refinery.store.dse.transition.internal; | |||
7 | 7 | ||
8 | import tools.refinery.store.dse.transition.DesignSpaceExplorationStoreAdapter; | 8 | import tools.refinery.store.dse.transition.DesignSpaceExplorationStoreAdapter; |
9 | import tools.refinery.store.dse.transition.Transformation; | 9 | import tools.refinery.store.dse.transition.Transformation; |
10 | import tools.refinery.store.dse.transition.TransformationRule; | 10 | import tools.refinery.store.dse.transition.Rule; |
11 | import tools.refinery.store.dse.transition.objectives.Criterion; | 11 | import tools.refinery.store.dse.transition.objectives.Criterion; |
12 | import tools.refinery.store.dse.transition.objectives.CriterionCalculator; | 12 | import tools.refinery.store.dse.transition.objectives.CriterionCalculator; |
13 | import tools.refinery.store.dse.transition.objectives.Objective; | 13 | import tools.refinery.store.dse.transition.objectives.Objective; |
@@ -20,18 +20,16 @@ import java.util.List; | |||
20 | public class DesignSpaceExplorationStoreAdapterImpl implements DesignSpaceExplorationStoreAdapter { | 20 | public class DesignSpaceExplorationStoreAdapterImpl implements DesignSpaceExplorationStoreAdapter { |
21 | protected final ModelStore store; | 21 | protected final ModelStore store; |
22 | 22 | ||
23 | protected final List<TransformationRule> transformationRuleDefinitions; | 23 | protected final List<Rule> ruleDefinitions; |
24 | protected final List<Criterion> accepts; | 24 | protected final List<Criterion> accepts; |
25 | protected final List<Criterion> excludes; | 25 | protected final List<Criterion> excludes; |
26 | protected final List<Objective> objectives; | 26 | protected final List<Objective> objectives; |
27 | 27 | ||
28 | public DesignSpaceExplorationStoreAdapterImpl(ModelStore store, | 28 | public DesignSpaceExplorationStoreAdapterImpl( |
29 | List<TransformationRule> transformationRuleDefinitions, | 29 | ModelStore store, List<Rule> ruleDefinitions, List<Criterion> accepts, List<Criterion> excludes, |
30 | List<Criterion> accepts, List<Criterion> excludes, | 30 | List<Objective> objectives) { |
31 | List<Objective> objectives) { | ||
32 | this.store = store; | 31 | this.store = store; |
33 | 32 | this.ruleDefinitions = ruleDefinitions; | |
34 | this.transformationRuleDefinitions = transformationRuleDefinitions; | ||
35 | this.accepts = accepts; | 33 | this.accepts = accepts; |
36 | this.excludes = excludes; | 34 | this.excludes = excludes; |
37 | this.objectives = objectives; | 35 | this.objectives = objectives; |
@@ -44,25 +42,31 @@ public class DesignSpaceExplorationStoreAdapterImpl implements DesignSpaceExplor | |||
44 | 42 | ||
45 | @Override | 43 | @Override |
46 | public DesignSpaceExplorationAdapterImpl createModelAdapter(Model model) { | 44 | public DesignSpaceExplorationAdapterImpl createModelAdapter(Model model) { |
47 | final List<Transformation> t = this.transformationRuleDefinitions.stream().map(x->x.prepare(model)).toList(); | 45 | final List<Transformation> t = this.ruleDefinitions.stream() |
48 | final List<CriterionCalculator> a = this.accepts.stream().map(x->x.createCalculator(model)).toList(); | 46 | .map(x -> new Transformation(model, x)) |
49 | final List<CriterionCalculator> e = this.excludes.stream().map(x->x.createCalculator(model)).toList(); | 47 | .toList(); |
50 | final List<ObjectiveCalculator> o = this.objectives.stream().map(x->x.createCalculator(model)).toList(); | 48 | final List<CriterionCalculator> a = this.accepts.stream().map(x -> x.createCalculator(model)).toList(); |
49 | final List<CriterionCalculator> e = this.excludes.stream().map(x -> x.createCalculator(model)).toList(); | ||
50 | final List<ObjectiveCalculator> o = this.objectives.stream().map(x -> x.createCalculator(model)).toList(); | ||
51 | 51 | ||
52 | return new DesignSpaceExplorationAdapterImpl(model, this, t, a, e, o); | 52 | return new DesignSpaceExplorationAdapterImpl(model, this, t, a, e, o); |
53 | } | 53 | } |
54 | |||
54 | @Override | 55 | @Override |
55 | public List<TransformationRule> getTransformations() { | 56 | public List<Rule> getTransformations() { |
56 | return transformationRuleDefinitions; | 57 | return ruleDefinitions; |
57 | } | 58 | } |
59 | |||
58 | @Override | 60 | @Override |
59 | public List<Criterion> getAccepts() { | 61 | public List<Criterion> getAccepts() { |
60 | return accepts; | 62 | return accepts; |
61 | } | 63 | } |
64 | |||
62 | @Override | 65 | @Override |
63 | public List<Criterion> getExcludes() { | 66 | public List<Criterion> getExcludes() { |
64 | return excludes; | 67 | return excludes; |
65 | } | 68 | } |
69 | |||
66 | @Override | 70 | @Override |
67 | public List<Objective> getObjectives() { | 71 | public List<Objective> getObjectives() { |
68 | return objectives; | 72 | return objectives; |
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/Criterion.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/Criterion.java index 66ca6f5e..4365cf9c 100644 --- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/Criterion.java +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/Criterion.java | |||
@@ -9,7 +9,7 @@ import tools.refinery.store.model.Model; | |||
9 | import tools.refinery.store.model.ModelStoreBuilder; | 9 | import tools.refinery.store.model.ModelStoreBuilder; |
10 | 10 | ||
11 | public interface Criterion { | 11 | public interface Criterion { |
12 | default void doConfigure(ModelStoreBuilder storeBuilder) { | 12 | default void configure(ModelStoreBuilder storeBuilder) { |
13 | } | 13 | } |
14 | CriterionCalculator createCalculator(Model model); | 14 | CriterionCalculator createCalculator(Model model); |
15 | } | 15 | } |
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/Objective.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/Objective.java index b5924455..d2476a2e 100644 --- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/Objective.java +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/Objective.java | |||
@@ -9,7 +9,7 @@ import tools.refinery.store.model.Model; | |||
9 | import tools.refinery.store.model.ModelStoreBuilder; | 9 | import tools.refinery.store.model.ModelStoreBuilder; |
10 | 10 | ||
11 | public interface Objective { | 11 | public interface Objective { |
12 | default void doConfigure(ModelStoreBuilder storeBuilder) { | 12 | default void configure(ModelStoreBuilder storeBuilder) { |
13 | } | 13 | } |
14 | ObjectiveCalculator createCalculator(Model model); | 14 | ObjectiveCalculator createCalculator(Model model); |
15 | } | 15 | } |
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/QueryCriteria.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/QueryCriteria.java index e2260cca..8d0a56cf 100644 --- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/QueryCriteria.java +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/QueryCriteria.java | |||
@@ -37,8 +37,8 @@ public class QueryCriteria implements Criterion { | |||
37 | } | 37 | } |
38 | 38 | ||
39 | @Override | 39 | @Override |
40 | public void doConfigure(ModelStoreBuilder storeBuilder) { | 40 | public void configure(ModelStoreBuilder storeBuilder) { |
41 | Criterion.super.doConfigure(storeBuilder); | 41 | Criterion.super.configure(storeBuilder); |
42 | storeBuilder.getAdapter(ModelQueryBuilder.class).query(query); | 42 | storeBuilder.getAdapter(ModelQueryBuilder.class).query(query); |
43 | } | 43 | } |
44 | } | 44 | } |
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/QueryObjective.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/QueryObjective.java index dfddccfc..9553e0e0 100644 --- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/QueryObjective.java +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/QueryObjective.java | |||
@@ -37,8 +37,8 @@ public class QueryObjective implements Objective { | |||
37 | } | 37 | } |
38 | 38 | ||
39 | @Override | 39 | @Override |
40 | public void doConfigure(ModelStoreBuilder storeBuilder) { | 40 | public void configure(ModelStoreBuilder storeBuilder) { |
41 | Objective.super.doConfigure(storeBuilder); | 41 | Objective.super.configure(storeBuilder); |
42 | storeBuilder.getAdapter(ModelQueryBuilder.class).query(objectiveFunction); | 42 | storeBuilder.getAdapter(ModelQueryBuilder.class).query(objectiveFunction); |
43 | } | 43 | } |
44 | } | 44 | } |
diff --git a/subprojects/store-dse/src/test/java/tools/refinery/store/dse/CRAExamplesTest.java b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/CRAExamplesTest.java index 36517709..0c50718e 100644 --- a/subprojects/store-dse/src/test/java/tools/refinery/store/dse/CRAExamplesTest.java +++ b/subprojects/store-dse/src/test/java/tools/refinery/store/dse/CRAExamplesTest.java | |||
@@ -5,13 +5,11 @@ | |||
5 | */ | 5 | */ |
6 | package tools.refinery.store.dse; | 6 | package tools.refinery.store.dse; |
7 | 7 | ||
8 | import tools.refinery.store.dse.transition.TransformationRule; | ||
9 | import tools.refinery.store.query.dnf.Query; | 8 | import tools.refinery.store.query.dnf.Query; |
10 | import tools.refinery.store.query.dnf.RelationalQuery; | 9 | import tools.refinery.store.query.dnf.RelationalQuery; |
11 | import tools.refinery.store.query.view.AnySymbolView; | 10 | import tools.refinery.store.query.view.AnySymbolView; |
12 | import tools.refinery.store.query.view.KeyOnlyView; | 11 | import tools.refinery.store.query.view.KeyOnlyView; |
13 | import tools.refinery.store.representation.Symbol; | 12 | import tools.refinery.store.representation.Symbol; |
14 | import tools.refinery.store.tuple.Tuple; | ||
15 | 13 | ||
16 | import java.util.List; | 14 | import java.util.List; |
17 | 15 | ||
@@ -107,19 +105,19 @@ class CRAExamplesTest { | |||
107 | encapsulatesView.call(c1, f) | 105 | encapsulatesView.call(c1, f) |
108 | )); | 106 | )); |
109 | 107 | ||
110 | private static final TransformationRule assignFeatureRule = new TransformationRule("AssignFeature", | 108 | // private static final Rule assignFeatureRule = new Rule("AssignFeature", |
111 | assignFeaturePrecondition, | 109 | // assignFeaturePrecondition, |
112 | (model) -> { | 110 | // (model) -> { |
113 | // var isEncapsulatedByInterpretation = model.getInterpretation(isEncapsulatedBy); | 111 | //// var isEncapsulatedByInterpretation = model.getInterpretation(isEncapsulatedBy); |
114 | var encapsulatesInterpretation = model.getInterpretation(encapsulates); | 112 | // var encapsulatesInterpretation = model.getInterpretation(encapsulates); |
115 | return ((Tuple activation) -> { | 113 | // return ((Tuple activation) -> { |
116 | var feature = activation.get(0); | 114 | // var feature = activation.get(0); |
117 | var classElement = activation.get(1); | 115 | // var classElement = activation.get(1); |
118 | 116 | // | |
119 | // isEncapsulatedByInterpretation.put(Tuple.of(feature, classElement), true); | 117 | //// isEncapsulatedByInterpretation.put(Tuple.of(feature, classElement), true); |
120 | encapsulatesInterpretation.put(Tuple.of(classElement, feature), true); | 118 | // encapsulatesInterpretation.put(Tuple.of(classElement, feature), true); |
121 | }); | 119 | // }); |
122 | }); | 120 | // }); |
123 | 121 | ||
124 | // private static final TransformationRule deleteEmptyClassRule = new TransformationRule("DeleteEmptyClass", | 122 | // private static final TransformationRule deleteEmptyClassRule = new TransformationRule("DeleteEmptyClass", |
125 | // deleteEmptyClassPrecondition, | 123 | // deleteEmptyClassPrecondition, |
@@ -158,19 +156,19 @@ class CRAExamplesTest { | |||
158 | // }); | 156 | // }); |
159 | // }); | 157 | // }); |
160 | 158 | ||
161 | private static final TransformationRule moveFeatureRule = new TransformationRule("MoveFeature", | 159 | // private static final Rule moveFeatureRule = new Rule("MoveFeature", |
162 | moveFeaturePrecondition, | 160 | // moveFeaturePrecondition, |
163 | (model) -> { | 161 | // (model) -> { |
164 | var encapsulatesInterpretation = model.getInterpretation(encapsulates); | 162 | // var encapsulatesInterpretation = model.getInterpretation(encapsulates); |
165 | return ((Tuple activation) -> { | 163 | // return ((Tuple activation) -> { |
166 | var classElement1 = activation.get(0); | 164 | // var classElement1 = activation.get(0); |
167 | var classElement2 = activation.get(1); | 165 | // var classElement2 = activation.get(1); |
168 | var feature = activation.get(2); | 166 | // var feature = activation.get(2); |
169 | 167 | // | |
170 | encapsulatesInterpretation.put(Tuple.of(classElement1, feature), false); | 168 | // encapsulatesInterpretation.put(Tuple.of(classElement1, feature), false); |
171 | encapsulatesInterpretation.put(Tuple.of(classElement2, feature), true); | 169 | // encapsulatesInterpretation.put(Tuple.of(classElement2, feature), true); |
172 | }); | 170 | // }); |
173 | }); | 171 | // }); |
174 | 172 | ||
175 | // @Test | 173 | // @Test |
176 | // @Disabled("This test is only for debugging purposes") | 174 | // @Disabled("This test is only for debugging purposes") |
diff --git a/subprojects/store-reasoning/build.gradle.kts b/subprojects/store-reasoning/build.gradle.kts index 5885da83..ed8355f3 100644 --- a/subprojects/store-reasoning/build.gradle.kts +++ b/subprojects/store-reasoning/build.gradle.kts | |||
@@ -9,7 +9,7 @@ plugins { | |||
9 | } | 9 | } |
10 | 10 | ||
11 | dependencies { | 11 | dependencies { |
12 | api(project(":refinery-store-query")) | 12 | api(project(":refinery-store-dse")) |
13 | testImplementation(testFixtures(project(":refinery-store-query"))) | 13 | testImplementation(testFixtures(project(":refinery-store-query"))) |
14 | testImplementation(project(":refinery-store-query-viatra")) | 14 | testImplementation(project(":refinery-store-query-viatra")) |
15 | } | 15 | } |