diff options
author | Kristóf Marussy <kristof@marussy.com> | 2024-06-28 19:43:52 +0200 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2024-06-28 21:44:58 +0200 |
commit | 6095f6eca9ae96f94d1afdcba9971ac2ec4d8c5b (patch) | |
tree | 1ed444c895dd6b383299397b5a97acc11aea5a33 /subprojects/store-dse | |
parent | refactor: show semantics even if propagation fails (diff) | |
download | refinery-6095f6eca9ae96f94d1afdcba9971ac2ec4d8c5b.tar.gz refinery-6095f6eca9ae96f94d1afdcba9971ac2ec4d8c5b.tar.zst refinery-6095f6eca9ae96f94d1afdcba9971ac2ec4d8c5b.zip |
refactor: improve propagation traceability
Diffstat (limited to 'subprojects/store-dse')
4 files changed, 76 insertions, 18 deletions
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/propagation/PropagationRejectedResult.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/propagation/PropagationRejectedResult.java new file mode 100644 index 00000000..6d696de7 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/propagation/PropagationRejectedResult.java | |||
@@ -0,0 +1,32 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2024 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.propagation; | ||
7 | |||
8 | public record PropagationRejectedResult(Object reason, String message) implements PropagationResult { | ||
9 | @Override | ||
10 | public PropagationResult andThen(PropagationResult next) { | ||
11 | return this; | ||
12 | } | ||
13 | |||
14 | @Override | ||
15 | public boolean isRejected() { | ||
16 | return true; | ||
17 | } | ||
18 | |||
19 | @Override | ||
20 | public void throwIfRejected() { | ||
21 | throw new IllegalArgumentException(formatMessage()); | ||
22 | } | ||
23 | |||
24 | public String formatMessage() { | ||
25 | return "Propagation failed: %s".formatted(message); | ||
26 | } | ||
27 | |||
28 | @Override | ||
29 | public boolean isChanged() { | ||
30 | return false; | ||
31 | } | ||
32 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/propagation/PropagationResult.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/propagation/PropagationResult.java index ea56653a..e07e443b 100644 --- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/propagation/PropagationResult.java +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/propagation/PropagationResult.java | |||
@@ -5,24 +5,15 @@ | |||
5 | */ | 5 | */ |
6 | package tools.refinery.store.dse.propagation; | 6 | package tools.refinery.store.dse.propagation; |
7 | 7 | ||
8 | public enum PropagationResult { | 8 | public sealed interface PropagationResult permits PropagationSuccessResult, PropagationRejectedResult { |
9 | UNCHANGED, | 9 | PropagationResult UNCHANGED = PropagationSuccessResult.UNCHANGED; |
10 | PROPAGATED, | 10 | PropagationResult PROPAGATED = PropagationSuccessResult.PROPAGATED; |
11 | REJECTED; | ||
12 | 11 | ||
13 | public PropagationResult andThen(PropagationResult next) { | 12 | PropagationResult andThen(PropagationResult next); |
14 | return switch (this) { | ||
15 | case UNCHANGED -> next; | ||
16 | case PROPAGATED -> next == REJECTED ? REJECTED : PROPAGATED; | ||
17 | case REJECTED -> REJECTED; | ||
18 | }; | ||
19 | } | ||
20 | 13 | ||
21 | public boolean isRejected() { | 14 | boolean isRejected(); |
22 | return this == REJECTED; | ||
23 | } | ||
24 | 15 | ||
25 | public boolean isChanged() { | 16 | void throwIfRejected(); |
26 | return this == PROPAGATED; | 17 | |
27 | } | 18 | boolean isChanged(); |
28 | } | 19 | } |
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/propagation/PropagationSuccessResult.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/propagation/PropagationSuccessResult.java new file mode 100644 index 00000000..22117772 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/propagation/PropagationSuccessResult.java | |||
@@ -0,0 +1,34 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2024 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.propagation; | ||
7 | |||
8 | public enum PropagationSuccessResult implements PropagationResult { | ||
9 | UNCHANGED, | ||
10 | PROPAGATED; | ||
11 | |||
12 | @Override | ||
13 | public PropagationResult andThen(PropagationResult next) { | ||
14 | return switch (this) { | ||
15 | case UNCHANGED -> next; | ||
16 | case PROPAGATED -> next instanceof PropagationRejectedResult ? next : PROPAGATED; | ||
17 | }; | ||
18 | } | ||
19 | |||
20 | @Override | ||
21 | public boolean isRejected() { | ||
22 | return false; | ||
23 | } | ||
24 | |||
25 | @Override | ||
26 | public void throwIfRejected() { | ||
27 | // Nothing to throw. | ||
28 | } | ||
29 | |||
30 | @Override | ||
31 | public boolean isChanged() { | ||
32 | return this == PROPAGATED; | ||
33 | } | ||
34 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/propagation/impl/rule/BoundPropagationRule.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/propagation/impl/rule/BoundPropagationRule.java index 5f69b7a2..f91882be 100644 --- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/propagation/impl/rule/BoundPropagationRule.java +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/propagation/impl/rule/BoundPropagationRule.java | |||
@@ -5,6 +5,7 @@ | |||
5 | */ | 5 | */ |
6 | package tools.refinery.store.dse.propagation.impl.rule; | 6 | package tools.refinery.store.dse.propagation.impl.rule; |
7 | 7 | ||
8 | import tools.refinery.store.dse.propagation.PropagationRejectedResult; | ||
8 | import tools.refinery.store.dse.propagation.PropagationResult; | 9 | import tools.refinery.store.dse.propagation.PropagationResult; |
9 | import tools.refinery.store.dse.transition.Rule; | 10 | import tools.refinery.store.dse.transition.Rule; |
10 | import tools.refinery.store.dse.transition.actions.BoundAction; | 11 | import tools.refinery.store.dse.transition.actions.BoundAction; |
@@ -54,7 +55,7 @@ class BoundPropagationRule { | |||
54 | var tuple = cursor.getKey(); | 55 | var tuple = cursor.getKey(); |
55 | var result = action.fire(tuple); | 56 | var result = action.fire(tuple); |
56 | if (!result) { | 57 | if (!result) { |
57 | return PropagationResult.REJECTED; | 58 | return new PropagationRejectedResult(rule, "Propagation rule '%s' failed.".formatted(rule.getName())); |
58 | } | 59 | } |
59 | firedActivations.add(tuple); | 60 | firedActivations.add(tuple); |
60 | } | 61 | } |