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

import tools.refinery.store.model.Interpretation;
import tools.refinery.store.model.Model;
import tools.refinery.store.reasoning.refinement.StorageRefiner;
import tools.refinery.store.representation.Symbol;
import tools.refinery.logic.term.cardinalityinterval.CardinalityInterval;
import tools.refinery.logic.term.cardinalityinterval.CardinalityIntervals;
import tools.refinery.store.tuple.Tuple;

class MultiObjectStorageRefiner implements StorageRefiner {
	private final Interpretation<CardinalityInterval> countInterpretation;

	public MultiObjectStorageRefiner(Symbol<CardinalityInterval> countSymbol, Model model) {
		countInterpretation = model.getInterpretation(countSymbol);
	}

	@Override
	public boolean split(int parentNode, int childNode) {
		var parentKey = Tuple.of(parentNode);
		var parentCount = countInterpretation.get(parentKey);
		if (parentCount == null) {
			return false;
		}
		var newParentCount = parentCount.take(1);
		if (newParentCount.isError()) {
			return false;
		}
		var childKey = Tuple.of(childNode);
		countInterpretation.put(parentKey, newParentCount);
		countInterpretation.put(childKey, CardinalityIntervals.ONE);
		return true;
	}

	@Override
	public boolean cleanup(int nodeToDelete) {
		var previousCount = countInterpretation.put(Tuple.of(nodeToDelete), null);
		return previousCount.lowerBound() == 0;
	}
}