aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/language/src/main/java/tools/refinery/language/typesystem/SignatureProvider.java
blob: 3e25a0f55b250ad8a604ccd78c1920df85188f7d (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
103
104
105
106
107
/*
 * SPDX-FileCopyrightText: 2024 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.language.typesystem;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.xtext.naming.IQualifiedNameProvider;
import org.eclipse.xtext.util.IResourceScopeCache;
import tools.refinery.language.model.problem.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Singleton
public class SignatureProvider {
	private static final String PREFIX = "tools.refinery.language.typesystem.SignatureProvider.";
	private static final String SIGNATURE_CACHE = PREFIX + "SIGNATURE_CACHE";
	private static final String DATATYPE_CACHE = PREFIX + "DATATYPE_CACHE";
	private static final String AGGREGATOR_CACHE = PREFIX + "AGGREGATOR_CACHE";

	@Inject
	private IQualifiedNameProvider qualifiedNameProvider;

	@Inject
	private IResourceScopeCache cache;

	public Signature getSignature(Relation relation) {
		var signatures = cache.get(SIGNATURE_CACHE, relation.eResource(), () -> new HashMap<Relation, Signature>());
		return signatures.computeIfAbsent(relation, this::computeSignature);
	}

	public int getArity(Relation relation) {
		return getSignature(relation).parameterTypes().size();
	}

	private Signature computeSignature(Relation relation) {
		return new Signature(getParameterTypes(relation), getResultType(relation));
	}

	private List<FixedType> getParameterTypes(Relation relation) {
		return switch (relation) {
			case ClassDeclaration ignored -> List.of(ExprType.NODE);
			case EnumDeclaration ignored -> List.of(ExprType.NODE);
			case DatatypeDeclaration datatypeDeclaration -> List.of(getDataType(datatypeDeclaration));
			case ReferenceDeclaration referenceDeclaration -> {
				if (referenceDeclaration.getReferenceType() instanceof DatatypeDeclaration) {
					yield List.of(ExprType.NODE);
				}
				yield List.of(ExprType.NODE, ExprType.NODE);
			}
			case ParametricDefinition parametricDefinition -> {
				var parameters = parametricDefinition.getParameters();
				var exprTypes = new ArrayList<FixedType>(parameters.size());
				for (var parameter : parameters) {
					if (parameter.getParameterType() instanceof DatatypeDeclaration datatypeDeclaration) {
						exprTypes.add(getDataType(datatypeDeclaration));
					} else {
						exprTypes.add(ExprType.NODE);
					}
				}
				yield List.copyOf(exprTypes);
			}
			default -> throw new IllegalArgumentException("Unknown Relation: " + relation);
		};
	}

	private FixedType getResultType(Relation relation) {
		if (relation instanceof ReferenceDeclaration referenceDeclaration &&
				referenceDeclaration.getReferenceType() instanceof DatatypeDeclaration datatypeDeclaration) {
			return getDataType(datatypeDeclaration);
		}
		return ExprType.LITERAL;
	}

	public DataExprType getDataType(DatatypeDeclaration datatypeDeclaration) {
		var dataTypes = cache.get(DATATYPE_CACHE, datatypeDeclaration.eResource(),
				() -> new HashMap<DatatypeDeclaration, DataExprType>());
		return dataTypes.computeIfAbsent(datatypeDeclaration, this::computeDataType);
	}

	private DataExprType computeDataType(DatatypeDeclaration datatypeDeclaration) {
		var qualifiedName = qualifiedNameProvider.getFullyQualifiedName(datatypeDeclaration);
		if (qualifiedName == null) {
			throw new IllegalArgumentException("Datatype declaration has no qualified name: " + datatypeDeclaration);
		}
		return new DataExprType(qualifiedName);
	}

	public AggregatorName getAggregatorName(AggregatorDeclaration aggregatorDeclaration) {
		var dataTypes = cache.get(AGGREGATOR_CACHE, aggregatorDeclaration.eResource(),
				() -> new HashMap<AggregatorDeclaration, AggregatorName>());
		return dataTypes.computeIfAbsent(aggregatorDeclaration, this::computeAggregatorName);
	}

	private AggregatorName computeAggregatorName(AggregatorDeclaration aggregatorDeclaration) {
		var qualifiedName = qualifiedNameProvider.getFullyQualifiedName(aggregatorDeclaration);
		if (qualifiedName == null) {
			throw new IllegalArgumentException(
					"Aggregator declaration has no qualified name: " + aggregatorDeclaration);
		}
		return new AggregatorName(qualifiedName);
	}
}