diff options
Diffstat (limited to 'subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/RuleBuilder.java')
-rw-r--r-- | subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/RuleBuilder.java | 71 |
1 files changed, 71 insertions, 0 deletions
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 | } | ||