aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/logic/src/main/java/tools/refinery/logic/term/Parameter.java
blob: d4a651e11b8468df80eb5e4fb72e3f72e4b2296f (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
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.logic.term;

import java.util.Objects;
import java.util.Optional;

public class Parameter {
	public static final Parameter NODE_OUT = new Parameter(null);

	private final Class<?> dataType;
	private final ParameterDirection direction;

	public Parameter(Class<?> dataType) {
		this(dataType, ParameterDirection.OUT);
	}

	public Parameter(Class<?> dataType, ParameterDirection direction) {
		this.dataType = dataType;
		this.direction = direction;
	}

	public boolean isNodeVariable() {
		return dataType == null;
	}

	public boolean isDataVariable() {
		return !isNodeVariable();
	}

	public Optional<Class<?>> tryGetType() {
		return Optional.ofNullable(dataType);
	}

	public ParameterDirection getDirection() {
		return direction;
	}

	public boolean matches(Parameter other) {
		return Objects.equals(dataType, other.dataType) && direction == other.direction;
	}

	public boolean isAssignable(Variable variable) {
		if (variable instanceof AnyDataVariable dataVariable) {
			return dataVariable.getType().equals(dataType);
		} else if (variable instanceof NodeVariable) {
			return !isDataVariable();
		} else {
			throw new IllegalArgumentException("Unknown variable " + variable);
		}
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		Parameter parameter = (Parameter) o;
		return matches(parameter);
	}

	@Override
	public int hashCode() {
		return Objects.hash(dataType, direction);
	}
}