aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/RuleBuilder.java
blob: c2e43e0d418591ee93a443e993635d14fcebdb4b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.dse.transition;

import tools.refinery.store.dse.transition.actions.Action;
import tools.refinery.store.dse.transition.actions.ActionLiteral;
import tools.refinery.store.dse.transition.callback.*;
import tools.refinery.logic.dnf.AbstractQueryBuilder;
import tools.refinery.logic.dnf.Dnf;
import tools.refinery.logic.term.Variable;

import java.util.List;

public class RuleBuilder extends AbstractQueryBuilder<RuleBuilder> {
	private final String name;
	private List<ActionLiteral> action;

	RuleBuilder(String name) {
		super(Dnf.builder(name == null ? null : name + "#precondition"));
		this.name = name;
	}

	@Override
	protected RuleBuilder self() {
		return this;
	}

	public RuleBuilder action(ActionLiteral... literals) {
		return action(List.of(literals));
	}

	public RuleBuilder action(List<? extends ActionLiteral> literals) {
		if (this.action != null) {
			throw new IllegalStateException("Actions have already been set");
		}
		this.action = List.copyOf(literals);
		return this;
	}

	public RuleBuilder action(Action action) {
		return action(action.getActionLiterals());
	}

	public RuleBuilder action(ActionCallback0 callback) {
		return action(callback.toLiterals());
	}

	public RuleBuilder action(ActionCallback1 callback) {
		return action(callback.toLiterals(Variable.of("v1")));
	}

	public RuleBuilder action(ActionCallback2 callback) {
		return action(callback.toLiterals(Variable.of("v1"), Variable.of("v2")));
	}

	public RuleBuilder action(ActionCallback3 callback) {
		return action(callback.toLiterals(Variable.of("v1"), Variable.of("v2"), Variable.of("v3")));
	}

	public RuleBuilder action(ActionCallback4 callback) {
		return action(callback.toLiterals(Variable.of("v1"), Variable.of("v2"), Variable.of("v3"), Variable.of("v4")));
	}

	public Rule build() {
		var precondition = dnfBuilder.build().asRelation();
		return new Rule(name, precondition, Action.ofPrecondition(precondition, action));
	}
}