diff options
author | Kristóf Marussy <kristof@marussy.com> | 2021-10-19 03:36:26 +0200 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2021-10-19 03:36:26 +0200 |
commit | 663274763e56b228efe07363b8ede4ce7bebc251 (patch) | |
tree | da6f8e0404cc03152cad84d8b9dfc8c63f7a6dbf /language-model/src/testFixtures/java/tools | |
parent | nodeIter moved from private field inside the method (diff) | |
download | refinery-663274763e56b228efe07363b8ede4ce7bebc251.tar.gz refinery-663274763e56b228efe07363b8ede4ce7bebc251.tar.zst refinery-663274763e56b228efe07363b8ede4ce7bebc251.zip |
chore: remove builtin library xtext dependency
Diffstat (limited to 'language-model/src/testFixtures/java/tools')
-rw-r--r-- | language-model/src/testFixtures/java/tools/refinery/language/model/tests/ProblemTestUtil.java | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/language-model/src/testFixtures/java/tools/refinery/language/model/tests/ProblemTestUtil.java b/language-model/src/testFixtures/java/tools/refinery/language/model/tests/ProblemTestUtil.java new file mode 100644 index 00000000..b412ed1f --- /dev/null +++ b/language-model/src/testFixtures/java/tools/refinery/language/model/tests/ProblemTestUtil.java | |||
@@ -0,0 +1,154 @@ | |||
1 | package tools.refinery.language.model.tests; | ||
2 | |||
3 | import java.util.List; | ||
4 | import java.util.stream.Stream; | ||
5 | |||
6 | import org.eclipse.emf.ecore.resource.Resource.Diagnostic; | ||
7 | import org.eclipse.emf.ecore.util.EcoreUtil; | ||
8 | |||
9 | import tools.refinery.language.model.ProblemUtil; | ||
10 | import tools.refinery.language.model.problem.Argument; | ||
11 | import tools.refinery.language.model.problem.Assertion; | ||
12 | import tools.refinery.language.model.problem.AssertionArgument; | ||
13 | import tools.refinery.language.model.problem.Atom; | ||
14 | import tools.refinery.language.model.problem.ClassDeclaration; | ||
15 | import tools.refinery.language.model.problem.Conjunction; | ||
16 | import tools.refinery.language.model.problem.EnumDeclaration; | ||
17 | import tools.refinery.language.model.problem.Literal; | ||
18 | import tools.refinery.language.model.problem.NamedElement; | ||
19 | import tools.refinery.language.model.problem.NegativeLiteral; | ||
20 | import tools.refinery.language.model.problem.Node; | ||
21 | import tools.refinery.language.model.problem.NodeAssertionArgument; | ||
22 | import tools.refinery.language.model.problem.NodeValueAssertion; | ||
23 | import tools.refinery.language.model.problem.Parameter; | ||
24 | import tools.refinery.language.model.problem.PredicateDefinition; | ||
25 | import tools.refinery.language.model.problem.Problem; | ||
26 | import tools.refinery.language.model.problem.ReferenceDeclaration; | ||
27 | import tools.refinery.language.model.problem.Relation; | ||
28 | import tools.refinery.language.model.problem.Statement; | ||
29 | import tools.refinery.language.model.problem.UniqueDeclaration; | ||
30 | import tools.refinery.language.model.problem.Variable; | ||
31 | import tools.refinery.language.model.problem.VariableOrNode; | ||
32 | import tools.refinery.language.model.problem.VariableOrNodeArgument; | ||
33 | |||
34 | public class ProblemTestUtil { | ||
35 | public Problem builtin(Problem problem) { | ||
36 | return ProblemUtil.getBuiltInLibrary(problem).get(); | ||
37 | } | ||
38 | |||
39 | public List<Diagnostic> errors(Problem problem) { | ||
40 | EcoreUtil.resolveAll(problem); | ||
41 | return problem.eResource().getErrors(); | ||
42 | } | ||
43 | |||
44 | public List<String> nodeNames(Problem problem) { | ||
45 | return problem.getNodes().stream().map(node -> node.getName()).toList(); | ||
46 | } | ||
47 | |||
48 | public PredicateDefinition pred(Problem problem, String name) { | ||
49 | return namedStatementOfType(problem, PredicateDefinition.class, name); | ||
50 | } | ||
51 | |||
52 | public Parameter param(PredicateDefinition definition, int i) { | ||
53 | return definition.getParameters().get(i); | ||
54 | } | ||
55 | |||
56 | public Conjunction conj(PredicateDefinition definition, int i) { | ||
57 | return definition.getBodies().get(i); | ||
58 | } | ||
59 | |||
60 | public Literal lit(Conjunction conjunction, int i) { | ||
61 | return conjunction.getLiterals().get(i); | ||
62 | } | ||
63 | |||
64 | public Atom negated(Literal literal) { | ||
65 | return ((NegativeLiteral) literal).getAtom(); | ||
66 | } | ||
67 | |||
68 | public Relation relation(Literal literal) { | ||
69 | return ((Atom) literal).getRelation(); | ||
70 | } | ||
71 | |||
72 | public Argument arg(Atom atom, int i) { | ||
73 | return atom.getArguments().get(i); | ||
74 | } | ||
75 | |||
76 | public Argument arg(Literal literal, int i) { | ||
77 | return arg((Atom) literal, i); | ||
78 | } | ||
79 | |||
80 | public VariableOrNode variableOrNode(Argument argument) { | ||
81 | return ((VariableOrNodeArgument) argument).getVariableOrNode(); | ||
82 | } | ||
83 | |||
84 | public Variable variable(Argument argument) { | ||
85 | return (Variable) variableOrNode(argument); | ||
86 | } | ||
87 | |||
88 | public Node node(Argument argument) { | ||
89 | return (Node) variableOrNode(argument); | ||
90 | } | ||
91 | |||
92 | public Assertion assertion(Problem problem, int i) { | ||
93 | return nthStatementOfType(problem, Assertion.class, i); | ||
94 | } | ||
95 | |||
96 | public AssertionArgument arg(Assertion assertion, int i) { | ||
97 | return assertion.getArguments().get(i); | ||
98 | } | ||
99 | |||
100 | public Node node(AssertionArgument argument) { | ||
101 | return ((NodeAssertionArgument) argument).getNode(); | ||
102 | } | ||
103 | |||
104 | public Node node(Problem problem, String name) { | ||
105 | return named(problem.getNodes(), name); | ||
106 | } | ||
107 | |||
108 | public Node uniqueNode(Problem problem, String name) { | ||
109 | var uniqueNodes = statementsOfType(problem, UniqueDeclaration.class) | ||
110 | .flatMap(declaration -> declaration.getNodes().stream()); | ||
111 | return named(uniqueNodes, name); | ||
112 | } | ||
113 | |||
114 | public NodeValueAssertion nodeValueAssertion(Problem problem, int i) { | ||
115 | return nthStatementOfType(problem, NodeValueAssertion.class, i); | ||
116 | } | ||
117 | |||
118 | public ClassDeclaration findClass(Problem problem, String name) { | ||
119 | return namedStatementOfType(problem, ClassDeclaration.class, name); | ||
120 | } | ||
121 | |||
122 | public ReferenceDeclaration reference(ClassDeclaration declaration, String name) { | ||
123 | return named(declaration.getReferenceDeclarations(), name); | ||
124 | } | ||
125 | |||
126 | public EnumDeclaration findEnum(Problem problem, String name) { | ||
127 | return namedStatementOfType(problem, EnumDeclaration.class, name); | ||
128 | } | ||
129 | |||
130 | public Node literal(EnumDeclaration declaration, String name) { | ||
131 | return named(declaration.getLiterals(), name); | ||
132 | } | ||
133 | |||
134 | private <T extends NamedElement> T named(Stream<? extends T> stream, String name) { | ||
135 | return stream.filter(statement -> name.equals(statement.getName())).findAny().get(); | ||
136 | } | ||
137 | |||
138 | private <T extends NamedElement> T named(List<? extends T> list, String name) { | ||
139 | return named(list.stream(), name); | ||
140 | } | ||
141 | |||
142 | private <T extends Statement> Stream<T> statementsOfType(Problem problem, Class<? extends T> type) { | ||
143 | return problem.getStatements().stream().filter(type::isInstance).map(type::cast); | ||
144 | } | ||
145 | |||
146 | private <T extends Statement & NamedElement> T namedStatementOfType(Problem problem, Class<? extends T> type, | ||
147 | String name) { | ||
148 | return named(statementsOfType(problem, type), name); | ||
149 | } | ||
150 | |||
151 | private <T extends Statement> T nthStatementOfType(Problem problem, Class<? extends T> type, int n) { | ||
152 | return statementsOfType(problem, type).skip(n).findFirst().get(); | ||
153 | } | ||
154 | } | ||