aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/crossreference/DirectedCrossReferenceInitializer.java
blob: 7cb16a282b2ea06ee87afc1716c58b9a44d49f77 (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
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.reasoning.translator.crossreference;

import tools.refinery.store.model.Model;
import tools.refinery.store.reasoning.ReasoningAdapter;
import tools.refinery.store.reasoning.refinement.PartialModelInitializer;
import tools.refinery.store.reasoning.representation.PartialRelation;
import tools.refinery.store.reasoning.seed.ModelSeed;
import tools.refinery.store.representation.Symbol;
import tools.refinery.logic.term.truthvalue.TruthValue;
import tools.refinery.store.tuple.Tuple;

class DirectedCrossReferenceInitializer implements PartialModelInitializer {
	private final PartialRelation linkType;
	private final PartialRelation sourceType;
	private final PartialRelation targetType;
	private final Symbol<TruthValue> symbol;

	public DirectedCrossReferenceInitializer(PartialRelation linkType, PartialRelation sourceType,
											 PartialRelation targetType, Symbol<TruthValue> symbol) {
		this.linkType = linkType;
		this.sourceType = sourceType;
		this.targetType = targetType;
		this.symbol = symbol;
	}

	@Override
	public void initialize(Model model, ModelSeed modelSeed) {
		var reasoningAdapter = model.getAdapter(ReasoningAdapter.class);
		var sourceRefiner = reasoningAdapter.getRefiner(sourceType);
		var targetRefiner = reasoningAdapter.getRefiner(targetType);
		var interpretation = model.getInterpretation(symbol);
		var cursor = modelSeed.getCursor(linkType, symbol.defaultValue());
		while (cursor.move()) {
			var key = cursor.getKey();
			var value = cursor.getValue();
			interpretation.put(key, value);
			if (value.must()) {
				boolean merged = sourceRefiner.merge(Tuple.of(key.get(0)), TruthValue.TRUE) &&
						targetRefiner.merge(Tuple.of(key.get(1)), TruthValue.TRUE);
				if (!merged) {
					throw new IllegalArgumentException("Failed to merge end types of reference %s for key %s"
							.formatted(linkType, key));
				}
			}
		}
	}
}