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

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import tools.refinery.store.reasoning.representation.PartialRelation;
import tools.refinery.store.reasoning.translator.multiplicity.ConstrainedMultiplicity;
import tools.refinery.store.reasoning.translator.multiplicity.Multiplicity;
import tools.refinery.store.reasoning.translator.multiplicity.UnconstrainedMultiplicity;
import tools.refinery.store.representation.TruthValue;
import tools.refinery.store.representation.cardinality.CardinalityInterval;

public final class ReferenceInfoBuilder {
	private boolean containment;
	private PartialRelation sourceType;
	private Multiplicity multiplicity = UnconstrainedMultiplicity.INSTANCE;
	private PartialRelation targetType;
	private PartialRelation opposite;
	private TruthValue defaultValue = TruthValue.UNKNOWN;

	ReferenceInfoBuilder() {
	}

	public ReferenceInfoBuilder source(@NotNull PartialRelation sourceType) {
		if (sourceType.arity() != 1) {
			throw new IllegalArgumentException("Source type %s must be of arity 1".formatted(sourceType));
		}
		this.sourceType = sourceType;
		return this;
	}

	public ReferenceInfoBuilder target(@NotNull PartialRelation targetType) {
		if (targetType.arity() != 1) {
			throw new IllegalArgumentException("Target type %s must be of arity 1".formatted(targetType));
		}
		this.targetType = targetType;
		return this;
	}

	public ReferenceInfoBuilder containment(boolean containment) {
		this.containment = containment;
		return this;
	}

	public ReferenceInfoBuilder multiplicity(@NotNull Multiplicity multiplicity) {
		this.multiplicity = multiplicity;
		return this;
	}

	public ReferenceInfoBuilder multiplicity(@NotNull CardinalityInterval multiplicityInterval,
											 @NotNull PartialRelation errorSymbol) {
		return multiplicity(ConstrainedMultiplicity.of(multiplicityInterval, errorSymbol));
	}

	public ReferenceInfoBuilder opposite(@Nullable PartialRelation opposite) {
		if (opposite != null && opposite.arity() != 2) {
			throw new IllegalArgumentException("Opposite %s must be of arity 2".formatted(targetType));
		}
		this.opposite = opposite;
		return this;
	}

	public ReferenceInfoBuilder defaultValue(@NotNull TruthValue defaultValue) {
		if (defaultValue.must()) {
			throw new IllegalArgumentException("Unsupported default value");
		}
		this.defaultValue = defaultValue;
		return this;
	}

	public ReferenceInfo build() {
		if (sourceType == null) {
			throw new IllegalStateException("Source type is required");
		}
		if (targetType == null) {
			throw new IllegalStateException("Target type is required");
		}
		return new ReferenceInfo(containment, sourceType, multiplicity, targetType, opposite, defaultValue);
	}
}