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

import tools.refinery.store.reasoning.representation.PartialRelation;
import tools.refinery.store.reasoning.translator.TranslationException;

import java.util.*;

@SuppressWarnings("UnusedReturnValue")
public class TypeHierarchyBuilder {
	protected final Map<PartialRelation, TypeInfo> typeInfoMap = new LinkedHashMap<>();

	protected TypeHierarchyBuilder() {
	}

	public TypeHierarchyBuilder type(PartialRelation partialRelation, TypeInfo typeInfo) {
		if (partialRelation.arity() != 1) {
			throw new TranslationException(partialRelation,
					"Only types of arity 1 are supported, got %s with %d instead"
							.formatted(partialRelation, partialRelation.arity()));
		}
		var putResult = typeInfoMap.put(partialRelation, typeInfo);
		if (putResult != null && !putResult.equals(typeInfo)) {
			throw new TranslationException(partialRelation,
					"Duplicate type info for partial relation: " + partialRelation);
		}
		return this;
	}

	public TypeHierarchyBuilder type(PartialRelation partialRelation, boolean abstractType,
									 PartialRelation... supertypes) {
		return type(partialRelation, abstractType, Set.of(supertypes));
	}

	public TypeHierarchyBuilder type(PartialRelation partialRelation, boolean abstractType,
									 Collection<PartialRelation> supertypes) {
		return type(partialRelation, new TypeInfo(supertypes, abstractType));
	}

	public TypeHierarchyBuilder type(PartialRelation partialRelation, PartialRelation... supertypes) {
		return type(partialRelation, List.of(supertypes));
	}

	public TypeHierarchyBuilder type(PartialRelation partialRelation, Collection<PartialRelation> supertypes) {
		return type(partialRelation, false, supertypes);
	}

	public TypeHierarchyBuilder types(Collection<Map.Entry<PartialRelation, TypeInfo>> entries) {
		for (var entry : entries) {
			type(entry.getKey(), entry.getValue());
		}
		return this;
	}

	public TypeHierarchyBuilder types(Map<PartialRelation, TypeInfo> map) {
		return types(map.entrySet());
	}

	public TypeHierarchy build() {
		return new TypeHierarchy(typeInfoMap);
	}
}