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
|
package tools.refinery.store.query.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.query.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.LinkedHashSet;
import java.util.Random;
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;
public TransformationRule(String name, RelationalQuery precondition, ActionFactory actionFactory) {
this(name, precondition, actionFactory, new Random());
}
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> getAllActivationsAsSets() {
return activations;
}
public LinkedHashSet<Activation> getAllActivations() {
var result = new LinkedHashSet<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));
}
}
|