aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/rule/Rule.java
blob: c7b16d47233acb225e9cf8ac3dc1a821e9d0f9d6 (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
package tools.refinery.store.reasoning.rule;

import tools.refinery.store.model.Model;
import tools.refinery.store.query.dnf.Dnf;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public record Rule(Dnf precondition, List<RuleAction> actions) {
	public Rule {
		var parameterSet = new HashSet<>(precondition.getParameters());
		for (var action : actions) {
			for (var argument : action.arguments()) {
				if (!parameterSet.contains(argument)) {
					throw new IllegalArgumentException(
							"Argument %s of action %s does not appear in the parameter list %s of %s"
									.formatted(argument, action, precondition.getParameters(), precondition.name()));
				}
			}
		}
	}

	public RuleExecutor createExecutor(Model model) {
		var parameters = precondition.getParameters();
		var actionExecutors = new ArrayList<RuleActionExecutor>(actions.size());
		for (var action : actions) {
			var arguments = action.arguments();
			int arity = arguments.size();
			var argumentIndices = new int[arity];
			for (int i = 0; i < arity; i++) {
				argumentIndices[i] = parameters.indexOf(arguments.get(i));
			}
			actionExecutors.add(action.createExecutor(argumentIndices, model));
		}
		return new RuleExecutor(this, model, actionExecutors);
	}
}