aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/translator/typehierarchy/InferredType.java
blob: 9a0c2b0f8bb905e99a4925645c5fab5ed76e3659 (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
/*
 * SPDX-FileCopyrightText: 2021-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 java.util.Collections;
import java.util.Objects;
import java.util.Set;

public final class InferredType {
	public static final InferredType UNTYPED = new InferredType(Set.of(), Set.of(), null);
	private final Set<PartialRelation> mustTypes;
	private final Set<PartialRelation> mayConcreteTypes;
	private final PartialRelation candidateType;
	private final int hashCode;

	public InferredType(Set<PartialRelation> mustTypes, Set<PartialRelation> mayConcreteTypes,
						PartialRelation candidateType) {
		this.mustTypes = Collections.unmodifiableSet(mustTypes);
		this.mayConcreteTypes = Collections.unmodifiableSet(mayConcreteTypes);
		this.candidateType = candidateType;
		hashCode = Objects.hash(mustTypes, mayConcreteTypes, candidateType);
	}

	public boolean isConsistent() {
		return candidateType != null || mustTypes.isEmpty();
	}

	public boolean isMust(PartialRelation partialRelation) {
		return mustTypes.contains(partialRelation);
	}

	public boolean isMayConcrete(PartialRelation partialRelation) {
		return mayConcreteTypes.contains(partialRelation);
	}


	public Set<PartialRelation> mustTypes() {
		return mustTypes;
	}

	public Set<PartialRelation> mayConcreteTypes() {
		return mayConcreteTypes;
	}

	public PartialRelation candidateType() {
		return candidateType;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		InferredType that = (InferredType) o;
		return Objects.equals(mustTypes, that.mustTypes) &&
				Objects.equals(mayConcreteTypes, that.mayConcreteTypes) &&
				Objects.equals(candidateType, that.candidateType);
	}

	@Override
	public int hashCode() {
		return hashCode;
	}

	@Override
	public String toString() {
		return "InferredType[" +
				"mustTypes=" + mustTypes + ", " +
				"mayConcreteTypes=" + mayConcreteTypes + ", " +
				"candidateType=" + candidateType + ']';
	}
}