aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/language/src/testFixtures/java/tools/refinery/language/model/tests/utils/WrappedProblem.java
blob: fc51ff57456a6ea1d40729cab29bcbdd4ce84f68 (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
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.language.model.tests.utils;

import org.eclipse.emf.ecore.resource.Resource.Diagnostic;
import org.eclipse.emf.ecore.util.Diagnostician;
import tools.refinery.language.model.problem.*;
import tools.refinery.language.utils.BuiltinSymbols;
import tools.refinery.language.utils.ProblemDesugarer;

import java.util.List;
import java.util.stream.Stream;

public record WrappedProblem(Problem problem) {
	public Problem get() {
		return problem;
	}

	public List<Diagnostic> getResourceErrors() {
		return problem.eResource().getErrors();
	}

	public List<Diagnostic> getResourceWarnings() {
		return problem.eResource().getWarnings();
	}

	public List<org.eclipse.emf.common.util.Diagnostic> validate() {
		return Diagnostician.INSTANCE.validate(problem).getChildren();
	}

	public WrappedProblem builtin() {
		return new WrappedProblem(new ProblemDesugarer().getBuiltinProblem(problem).orElseThrow());
	}

	public BuiltinSymbols builtinSymbols() {
		return new ProblemDesugarer().getBuiltinSymbols(problem).orElseThrow();
	}

	public List<String> nodeNames() {
		return problem.getNodes().stream().map(Node::getName).toList();
	}

	public WrappedPredicateDefinition pred(String name) {
		return new WrappedPredicateDefinition(namedStatementOfType(PredicateDefinition.class, name));
	}

	public WrappedRuleDefinition rule(String name) {
		return new WrappedRuleDefinition(namedStatementOfType(RuleDefinition.class, name));
	}

	public WrappedClassDeclaration findClass(String name) {
		return new WrappedClassDeclaration(namedStatementOfType(ClassDeclaration.class, name));
	}

	public WrappedEnumDeclaration findEnum(String name) {
		return new WrappedEnumDeclaration(namedStatementOfType(EnumDeclaration.class, name));
	}

	public WrappedAssertion assertion(int i) {
		return new WrappedAssertion(nthStatementOfType(Assertion.class, i));
	}

	public Node node(String name) {
		return ProblemNavigationUtil.named(problem.getNodes(), name);
	}

	public Node individualNode(String name) {
		var uniqueNodes = statementsOfType(IndividualDeclaration.class)
				.flatMap(declaration -> declaration.getNodes().stream());
		return ProblemNavigationUtil.named(uniqueNodes, name);
	}

	private <T extends Statement> Stream<T> statementsOfType(Class<? extends T> type) {
		return problem.getStatements().stream().filter(type::isInstance).map(type::cast);
	}

	private <T extends Statement & NamedElement> T namedStatementOfType(Class<? extends T> type, String name) {
		return ProblemNavigationUtil.named(statementsOfType(type), name);
	}

	private <T extends Statement> T nthStatementOfType(Class<? extends T> type, int n) {
		return statementsOfType(type).skip(n).findFirst().orElseThrow();
	}
}