aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/TransformationRule.java
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/TransformationRule.java')
-rw-r--r--subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/TransformationRule.java84
1 files changed, 84 insertions, 0 deletions
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/TransformationRule.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/TransformationRule.java
new file mode 100644
index 00000000..ed2e77f1
--- /dev/null
+++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/TransformationRule.java
@@ -0,0 +1,84 @@
1package tools.refinery.store.dse.internal;
2
3import org.eclipse.collections.api.block.procedure.Procedure;
4import tools.refinery.store.model.Model;
5import tools.refinery.store.query.ModelQueryAdapter;
6import tools.refinery.store.query.dnf.RelationalQuery;
7import tools.refinery.store.dse.ActionFactory;
8import tools.refinery.store.query.resultset.OrderedResultSet;
9import tools.refinery.store.query.resultset.ResultSet;
10import tools.refinery.store.tuple.Tuple;
11
12import java.util.LinkedHashSet;
13import java.util.Random;
14
15public class TransformationRule {
16
17 private final String name;
18 private final RelationalQuery precondition;
19 private final ActionFactory actionFactory;
20 private Procedure<Tuple> action;
21 private OrderedResultSet<Boolean> activations;
22 private Random random;
23 private ModelQueryAdapter queryEngine;
24
25 public TransformationRule(String name, RelationalQuery precondition, ActionFactory actionFactory) {
26 this(name, precondition, actionFactory, new Random());
27 }
28
29 public TransformationRule(String name, RelationalQuery precondition, ActionFactory actionFactory, long seed) {
30 this(name, precondition, actionFactory, new Random(seed));
31 }
32
33 public TransformationRule(String name, RelationalQuery precondition, ActionFactory actionFactory, Random random) {
34 this.name = name;
35 this.precondition = precondition;
36 this.actionFactory = actionFactory;
37 this.random = random;
38 }
39 public boolean prepare(Model model, ModelQueryAdapter queryEngine) {
40 action = actionFactory.prepare(model);
41 this.queryEngine = queryEngine;
42 activations = new OrderedResultSet<>(queryEngine.getResultSet(precondition));
43 return true;
44 }
45
46 public boolean fireActivation(Tuple activation) {
47 action.accept(activation);
48 queryEngine.flushChanges();
49 return true;
50 }
51
52 public boolean fireRandomActivation() {
53 return getRandomActivation().fire();
54 }
55
56 public String getName() {
57 return name;
58 }
59
60 public RelationalQuery getPrecondition() {
61 return precondition;
62 }
63
64 public ResultSet<Boolean> getAllActivationsAsSets() {
65 return activations;
66 }
67
68 public LinkedHashSet<Activation> getAllActivations() {
69 var result = new LinkedHashSet<Activation>();
70 var cursor = activations.getAll();
71 while (cursor.move()) {
72 result.add(new Activation(this, cursor.getKey()));
73 }
74 return result;
75 }
76
77 public Activation getRandomActivation() {
78 return new Activation(this, activations.getKey(random.nextInt(activations.size())));
79 }
80
81 public Activation getActivation(int index) {
82 return new Activation(this, activations.getKey(index));
83 }
84}