diff options
author | Kristóf Marussy <kristof@marussy.com> | 2021-12-12 17:48:47 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2021-12-12 17:48:47 +0100 |
commit | fc7e9312d00e60171ed77c477ed91231d3dbfff9 (patch) | |
tree | cc185dd088b5fa6e9357aab3c9062a70626d1953 /subprojects/language-model/src/testFixtures | |
parent | build: refactor java-application conventions (diff) | |
download | refinery-fc7e9312d00e60171ed77c477ed91231d3dbfff9.tar.gz refinery-fc7e9312d00e60171ed77c477ed91231d3dbfff9.tar.zst refinery-fc7e9312d00e60171ed77c477ed91231d3dbfff9.zip |
build: move modules into subproject directory
Diffstat (limited to 'subprojects/language-model/src/testFixtures')
-rw-r--r-- | subprojects/language-model/src/testFixtures/java/tools/refinery/language/model/tests/ProblemTestUtil.java | 197 |
1 files changed, 197 insertions, 0 deletions
diff --git a/subprojects/language-model/src/testFixtures/java/tools/refinery/language/model/tests/ProblemTestUtil.java b/subprojects/language-model/src/testFixtures/java/tools/refinery/language/model/tests/ProblemTestUtil.java new file mode 100644 index 00000000..d0990dc0 --- /dev/null +++ b/subprojects/language-model/src/testFixtures/java/tools/refinery/language/model/tests/ProblemTestUtil.java | |||
@@ -0,0 +1,197 @@ | |||
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.ActionLiteral; | ||
11 | import tools.refinery.language.model.problem.Argument; | ||
12 | import tools.refinery.language.model.problem.Assertion; | ||
13 | import tools.refinery.language.model.problem.AssertionArgument; | ||
14 | import tools.refinery.language.model.problem.Atom; | ||
15 | import tools.refinery.language.model.problem.ClassDeclaration; | ||
16 | import tools.refinery.language.model.problem.Conjunction; | ||
17 | import tools.refinery.language.model.problem.DeleteActionLiteral; | ||
18 | import tools.refinery.language.model.problem.EnumDeclaration; | ||
19 | import tools.refinery.language.model.problem.IndividualDeclaration; | ||
20 | import tools.refinery.language.model.problem.Literal; | ||
21 | import tools.refinery.language.model.problem.NamedElement; | ||
22 | import tools.refinery.language.model.problem.NegativeLiteral; | ||
23 | import tools.refinery.language.model.problem.NewActionLiteral; | ||
24 | import tools.refinery.language.model.problem.Node; | ||
25 | import tools.refinery.language.model.problem.NodeAssertionArgument; | ||
26 | import tools.refinery.language.model.problem.NodeValueAssertion; | ||
27 | import tools.refinery.language.model.problem.Parameter; | ||
28 | import tools.refinery.language.model.problem.ParametricDefinition; | ||
29 | import tools.refinery.language.model.problem.PredicateDefinition; | ||
30 | import tools.refinery.language.model.problem.Problem; | ||
31 | import tools.refinery.language.model.problem.ReferenceDeclaration; | ||
32 | import tools.refinery.language.model.problem.Relation; | ||
33 | import tools.refinery.language.model.problem.RuleDefinition; | ||
34 | import tools.refinery.language.model.problem.Statement; | ||
35 | import tools.refinery.language.model.problem.ValueActionLiteral; | ||
36 | import tools.refinery.language.model.problem.ValueLiteral; | ||
37 | import tools.refinery.language.model.problem.Variable; | ||
38 | import tools.refinery.language.model.problem.VariableOrNode; | ||
39 | import tools.refinery.language.model.problem.VariableOrNodeArgument; | ||
40 | |||
41 | public class ProblemTestUtil { | ||
42 | public Problem builtin(Problem problem) { | ||
43 | return ProblemUtil.getBuiltInLibrary(problem).get(); | ||
44 | } | ||
45 | |||
46 | public List<Diagnostic> errors(Problem problem) { | ||
47 | EcoreUtil.resolveAll(problem); | ||
48 | return problem.eResource().getErrors(); | ||
49 | } | ||
50 | |||
51 | public List<String> nodeNames(Problem problem) { | ||
52 | return problem.getNodes().stream().map(node -> node.getName()).toList(); | ||
53 | } | ||
54 | |||
55 | public PredicateDefinition pred(Problem problem, String name) { | ||
56 | return namedStatementOfType(problem, PredicateDefinition.class, name); | ||
57 | } | ||
58 | |||
59 | public RuleDefinition rule(Problem problem, String name) { | ||
60 | return namedStatementOfType(problem, RuleDefinition.class, name); | ||
61 | } | ||
62 | |||
63 | public Parameter param(ParametricDefinition definition, int i) { | ||
64 | return definition.getParameters().get(i); | ||
65 | } | ||
66 | |||
67 | public Conjunction conj(ParametricDefinition definition, int i) { | ||
68 | return definition.getBodies().get(i); | ||
69 | } | ||
70 | |||
71 | public Literal lit(Conjunction conjunction, int i) { | ||
72 | return conjunction.getLiterals().get(i); | ||
73 | } | ||
74 | |||
75 | public ActionLiteral actionLit(RuleDefinition rule, int i) { | ||
76 | return rule.getAction().getActionLiterals().get(i); | ||
77 | } | ||
78 | |||
79 | public Atom valueAtom(Literal literal) { | ||
80 | return ((ValueLiteral) literal).getAtom(); | ||
81 | } | ||
82 | |||
83 | public Atom negated(Literal literal) { | ||
84 | return ((NegativeLiteral) literal).getAtom(); | ||
85 | } | ||
86 | |||
87 | public Relation relation(Literal literal) { | ||
88 | return ((Atom) literal).getRelation(); | ||
89 | } | ||
90 | |||
91 | public Argument arg(Atom atom, int i) { | ||
92 | return atom.getArguments().get(i); | ||
93 | } | ||
94 | |||
95 | public Argument arg(Literal literal, int i) { | ||
96 | return arg((Atom) literal, i); | ||
97 | } | ||
98 | |||
99 | public VariableOrNode variableOrNode(Argument argument) { | ||
100 | return ((VariableOrNodeArgument) argument).getVariableOrNode(); | ||
101 | } | ||
102 | |||
103 | public Variable variable(Argument argument) { | ||
104 | return (Variable) variableOrNode(argument); | ||
105 | } | ||
106 | |||
107 | public Variable variable(ValueActionLiteral valueActionLiteral, int i) { | ||
108 | return variable(arg(valueActionLiteral.getAtom(), i)); | ||
109 | } | ||
110 | |||
111 | public Variable variable(NewActionLiteral newActionLiteral) { | ||
112 | return newActionLiteral.getVariable(); | ||
113 | } | ||
114 | |||
115 | public VariableOrNode deleteVar(ActionLiteral actionLiteral) { | ||
116 | return ((DeleteActionLiteral) actionLiteral).getVariableOrNode(); | ||
117 | } | ||
118 | |||
119 | public VariableOrNode newVar(ActionLiteral actionLiteral) { | ||
120 | return ((NewActionLiteral) actionLiteral).getVariable(); | ||
121 | } | ||
122 | |||
123 | public Atom valueAtom(ActionLiteral actionLiteral) { | ||
124 | return ((ValueActionLiteral) actionLiteral).getAtom(); | ||
125 | } | ||
126 | |||
127 | public Variable variable(DeleteActionLiteral deleteActionLiteral) { | ||
128 | return (Variable) deleteActionLiteral.getVariableOrNode(); | ||
129 | } | ||
130 | |||
131 | public Node node(Argument argument) { | ||
132 | return (Node) variableOrNode(argument); | ||
133 | } | ||
134 | |||
135 | public Assertion assertion(Problem problem, int i) { | ||
136 | return nthStatementOfType(problem, Assertion.class, i); | ||
137 | } | ||
138 | |||
139 | public AssertionArgument arg(Assertion assertion, int i) { | ||
140 | return assertion.getArguments().get(i); | ||
141 | } | ||
142 | |||
143 | public Node node(AssertionArgument argument) { | ||
144 | return ((NodeAssertionArgument) argument).getNode(); | ||
145 | } | ||
146 | |||
147 | public Node node(Problem problem, String name) { | ||
148 | return named(problem.getNodes(), name); | ||
149 | } | ||
150 | |||
151 | public Node individualNode(Problem problem, String name) { | ||
152 | var uniqueNodes = statementsOfType(problem, IndividualDeclaration.class) | ||
153 | .flatMap(declaration -> declaration.getNodes().stream()); | ||
154 | return named(uniqueNodes, name); | ||
155 | } | ||
156 | |||
157 | public NodeValueAssertion nodeValueAssertion(Problem problem, int i) { | ||
158 | return nthStatementOfType(problem, NodeValueAssertion.class, i); | ||
159 | } | ||
160 | |||
161 | public ClassDeclaration findClass(Problem problem, String name) { | ||
162 | return namedStatementOfType(problem, ClassDeclaration.class, name); | ||
163 | } | ||
164 | |||
165 | public ReferenceDeclaration reference(ClassDeclaration declaration, String name) { | ||
166 | return named(declaration.getReferenceDeclarations(), name); | ||
167 | } | ||
168 | |||
169 | public EnumDeclaration findEnum(Problem problem, String name) { | ||
170 | return namedStatementOfType(problem, EnumDeclaration.class, name); | ||
171 | } | ||
172 | |||
173 | public Node literal(EnumDeclaration declaration, String name) { | ||
174 | return named(declaration.getLiterals(), name); | ||
175 | } | ||
176 | |||
177 | private <T extends NamedElement> T named(Stream<? extends T> stream, String name) { | ||
178 | return stream.filter(statement -> name.equals(statement.getName())).findAny().get(); | ||
179 | } | ||
180 | |||
181 | private <T extends NamedElement> T named(List<? extends T> list, String name) { | ||
182 | return named(list.stream(), name); | ||
183 | } | ||
184 | |||
185 | private <T extends Statement> Stream<T> statementsOfType(Problem problem, Class<? extends T> type) { | ||
186 | return problem.getStatements().stream().filter(type::isInstance).map(type::cast); | ||
187 | } | ||
188 | |||
189 | private <T extends Statement & NamedElement> T namedStatementOfType(Problem problem, Class<? extends T> type, | ||
190 | String name) { | ||
191 | return named(statementsOfType(problem, type), name); | ||
192 | } | ||
193 | |||
194 | private <T extends Statement> T nthStatementOfType(Problem problem, Class<? extends T> type, int n) { | ||
195 | return statementsOfType(problem, type).skip(n).findFirst().get(); | ||
196 | } | ||
197 | } | ||