aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-dse/src/main/java/tools/refinery/store/dse/propagation/impl/rule/BoundPropagationRule.java
blob: 6e6a78d292c1b201e5e4a94f07fa668662e08945 (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
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.dse.propagation.impl.rule;

import tools.refinery.store.dse.propagation.PropagationResult;
import tools.refinery.store.dse.transition.Rule;
import tools.refinery.store.dse.transition.actions.BoundAction;
import tools.refinery.store.model.Model;
import tools.refinery.store.query.ModelQueryAdapter;
import tools.refinery.store.query.resultset.ResultSet;

class BoundPropagationRule {
	private final ResultSet<Boolean> resultSet;
	private final BoundAction action;

	public BoundPropagationRule(Model model, Rule rule) {
		resultSet = model.getAdapter(ModelQueryAdapter.class).getResultSet(rule.getPrecondition());
		action = rule.createAction(model);
	}

	public PropagationResult fireAll() {
		if (resultSet.size() == 0) {
			return PropagationResult.UNCHANGED;
		}
		var cursor = resultSet.getAll();
		while (cursor.move()) {
			var result = action.fire(cursor.getKey());
			if (!result) {
				return PropagationResult.REJECTED;
			}
		}
		return PropagationResult.PROPAGATED;
	}
}