aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/TransformationRule.java
blob: 37117164f8cf0383c1351a78068f2fb5c89159f2 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.dse.internal;

import org.eclipse.collections.api.block.procedure.Procedure;
import tools.refinery.store.model.Model;
import tools.refinery.store.query.ModelQueryAdapter;
import tools.refinery.store.query.dnf.RelationalQuery;
import tools.refinery.store.dse.ActionFactory;
import tools.refinery.store.query.resultset.OrderedResultSet;
import tools.refinery.store.query.resultset.ResultSet;
import tools.refinery.store.tuple.Tuple;

import java.util.*;

public class TransformationRule {

	private final String name;
	private final RelationalQuery precondition;
	private final ActionFactory actionFactory;
	private Procedure<Tuple> action;
	private OrderedResultSet<Boolean> activations;
	private Random random;
	private ModelQueryAdapter queryEngine;

	@SuppressWarnings("squid:S2245")
	public TransformationRule(String name, RelationalQuery precondition, ActionFactory actionFactory) {
		this(name, precondition, actionFactory, new Random());
	}

	@SuppressWarnings("squid:S2245")
	public TransformationRule(String name, RelationalQuery precondition, ActionFactory actionFactory, long seed) {
		this(name, precondition, actionFactory, new Random(seed));
	}

	public TransformationRule(String name, RelationalQuery precondition, ActionFactory actionFactory, Random random) {
		this.name = name;
		this.precondition = precondition;
		this.actionFactory = actionFactory;
		this.random = random;
	}
	public boolean prepare(Model model, ModelQueryAdapter queryEngine) {
		action = actionFactory.prepare(model);
		this.queryEngine = queryEngine;
		activations = new OrderedResultSet<>(queryEngine.getResultSet(precondition));
		return true;
	}

	public boolean fireActivation(Tuple activation) {
		action.accept(activation);
		queryEngine.flushChanges();
		return true;
	}

	public boolean fireRandomActivation() {
		return getRandomActivation().fire();
	}

	public String getName() {
		return name;
	}

	public RelationalQuery getPrecondition() {
		return precondition;
	}

	public ResultSet<Boolean> getAllActivationsAsResultSet() {
		return activations;
	}

	public Set<Activation> getAllActivations() {
		var result = new LinkedHashSet<Activation>();
		var cursor = activations.getAll();
		while (cursor.move()) {
			result.add(new Activation(this, cursor.getKey()));
		}
		return result;
	}

	public List<Activation> getAllActivationsAsList() {
		var result = new ArrayList<Activation>();
		var cursor = activations.getAll();
		while (cursor.move()) {
			result.add(new Activation(this, cursor.getKey()));
		}
		return result;
	}

	public Activation getRandomActivation() {
		return new Activation(this, activations.getKey(random.nextInt(activations.size())));
	}

	public Activation getActivation(int index) {
		return new Activation(this, activations.getKey(index));
	}
}