aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/ScopePropagator.java
blob: 25b1966c5551608d8e5b48b034298a7ac81e9ce7 (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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.reasoning.scope;

import com.google.ortools.Loader;
import tools.refinery.store.dse.propagation.PropagationBuilder;
import tools.refinery.store.model.ModelStoreBuilder;
import tools.refinery.store.model.ModelStoreConfiguration;
import tools.refinery.store.reasoning.representation.PartialRelation;
import tools.refinery.store.reasoning.translator.TranslationException;
import tools.refinery.store.reasoning.translator.multiobject.MultiObjectTranslator;
import tools.refinery.store.representation.Symbol;
import tools.refinery.store.representation.cardinality.CardinalityInterval;
import tools.refinery.store.representation.cardinality.FiniteUpperCardinality;

import java.util.*;

public class ScopePropagator implements ModelStoreConfiguration {
	private final Symbol<CardinalityInterval> countSymbol;
	private final Map<PartialRelation, CardinalityInterval> scopes = new LinkedHashMap<>();
	private final List<TypeScopePropagator.Factory> typeScopePropagatorFactories = new ArrayList<>();

	public ScopePropagator() {
		this(MultiObjectTranslator.COUNT_STORAGE);
	}

	public ScopePropagator(Symbol<CardinalityInterval> countSymbol) {
		if (countSymbol.arity() != 1) {
			throw new IllegalArgumentException("Count symbol must have arty 1, got %s with arity %d instead"
					.formatted(countSymbol, countSymbol.arity()));
		}
		if (!countSymbol.valueType().equals(CardinalityInterval.class)) {
			throw new IllegalArgumentException("Count symbol must have CardinalityInterval values");
		}
		if (countSymbol.defaultValue() != null) {
			throw new IllegalArgumentException("Count symbol must default value null");
		}
		this.countSymbol = countSymbol;
	}

	public ScopePropagator scope(PartialRelation type, CardinalityInterval interval) {
		if (type.arity() != 1) {
			throw new TranslationException(type, "Only types with arity 1 may have scopes, got %s with arity %d"
					.formatted(type, type.arity()));
		}
		var newValue = scopes.compute(type, (ignoredKey, oldValue) ->
				oldValue == null ? interval : oldValue.meet(interval));
		if (newValue.isEmpty()) {
			throw new TranslationException(type, "Unsatisfiable scope for type %s".formatted(type));
		}
		return this;
	}

	public ScopePropagator scopes(Map<PartialRelation, CardinalityInterval> scopes) {
		return scopes(scopes.entrySet());
	}

	public ScopePropagator scopes(Collection<Map.Entry<PartialRelation, CardinalityInterval>> scopes) {
		for (var entry : scopes) {
			scope(entry.getKey(), entry.getValue());
		}
		return this;
	}

	@Override
	public void apply(ModelStoreBuilder storeBuilder) {
		createTypeScopePropagatorFactories();
		Loader.loadNativeLibraries();
		for (var factory : typeScopePropagatorFactories) {
			factory.configure(storeBuilder);
		}
		storeBuilder.getAdapter(PropagationBuilder.class)
				.propagator(model -> new BoundScopePropagator(model, this));
	}

	private void createTypeScopePropagatorFactories() {
		for (var entry : scopes.entrySet()) {
			var type = entry.getKey();
			var bounds = entry.getValue();
			if (bounds.lowerBound() > 0) {
				var lowerFactory = new LowerTypeScopePropagator.Factory(type, bounds.lowerBound());
				typeScopePropagatorFactories.add(lowerFactory);
			}
			if (bounds.upperBound() instanceof FiniteUpperCardinality finiteUpperCardinality) {
				var upperFactory = new UpperTypeScopePropagator.Factory(type,
						finiteUpperCardinality.finiteUpperBound());
				typeScopePropagatorFactories.add(upperFactory);
			}
		}
	}

	Symbol<CardinalityInterval> getCountSymbol() {
		return countSymbol;
	}

	List<TypeScopePropagator.Factory> getTypeScopePropagatorFactories() {
		return typeScopePropagatorFactories;
	}
}