aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/language-model
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2022-07-29 21:58:47 +0200
committerLibravatar Kristóf Marussy <kristof@marussy.com>2022-07-29 21:58:47 +0200
commit1d43c802f853b08f03c4b3954af242ad005c8afd (patch)
tree9a98da19c07fc587b6a3e2283aca707ad345e252 /subprojects/language-model
parentbuild: clean up buildSrc scripts (diff)
downloadrefinery-1d43c802f853b08f03c4b3954af242ad005c8afd.tar.gz
refinery-1d43c802f853b08f03c4b3954af242ad005c8afd.tar.zst
refinery-1d43c802f853b08f03c4b3954af242ad005c8afd.zip
refactor: simplify language project
* Move all utilities for language-model to language, since they do not make sense on their own * Convert xtend code into java
Diffstat (limited to 'subprojects/language-model')
-rw-r--r--subprojects/language-model/build.gradle1
-rw-r--r--subprojects/language-model/src/main/java/tools/refinery/language/model/ProblemEMFSetup.java34
-rw-r--r--subprojects/language-model/src/main/resources/model/builtin.problem_xmi67
-rw-r--r--subprojects/language-model/src/testFixtures/java/tools/refinery/language/model/tests/ProblemTestUtil.java197
4 files changed, 0 insertions, 299 deletions
diff --git a/subprojects/language-model/build.gradle b/subprojects/language-model/build.gradle
index 67883a3d..275db188 100644
--- a/subprojects/language-model/build.gradle
+++ b/subprojects/language-model/build.gradle
@@ -1,6 +1,5 @@
1plugins { 1plugins {
2 id 'refinery-java-library' 2 id 'refinery-java-library'
3 id 'refinery-java-test-fixtures'
4 id 'refinery-mwe2' 3 id 'refinery-mwe2'
5 id 'refinery-sonarqube' 4 id 'refinery-sonarqube'
6} 5}
diff --git a/subprojects/language-model/src/main/java/tools/refinery/language/model/ProblemEMFSetup.java b/subprojects/language-model/src/main/java/tools/refinery/language/model/ProblemEMFSetup.java
deleted file mode 100644
index 9383098b..00000000
--- a/subprojects/language-model/src/main/java/tools/refinery/language/model/ProblemEMFSetup.java
+++ /dev/null
@@ -1,34 +0,0 @@
1package tools.refinery.language.model;
2
3import org.eclipse.emf.ecore.EPackage;
4import org.eclipse.emf.ecore.resource.Resource;
5
6import tools.refinery.language.model.problem.ProblemPackage;
7import tools.refinery.language.model.problem.impl.ProblemFactoryImpl;
8
9public class ProblemEMFSetup {
10 public static final String XMI_RESOURCE_EXTENSION = "problem_xmi";
11
12 private ProblemEMFSetup() {
13 throw new IllegalStateException("This is a static utility class and should not be instantiated directly");
14 }
15
16 // Here we can't rely on java.util.HashMap#putIfAbsent, because
17 // org.eclipse.emf.ecore.impl.EPackageRegistryImpl#containsKey is overridden
18 // without also overriding putIfAbsent. We must make sure to call the
19 // overridden containsKey implementation.
20 @SuppressWarnings("squid:S3824")
21 public static void doEMFRegistration() {
22 if (!EPackage.Registry.INSTANCE.containsKey(ProblemPackage.eNS_URI)) {
23 EPackage.Registry.INSTANCE.put(ProblemPackage.eNS_URI, ProblemPackage.eINSTANCE);
24 }
25
26 // This Resource.Factory is not actually used once
27 // tools.refinery.language.ProblemStandaloneSetup.createInjectorAndDoEMFRegistration()
28 // is called, because if will be replaced by
29 // tools.refinery.language.resource.ProblemXmiResourceFactory, which implements
30 // org.eclipse.xtext.resource.IResourceFactory as required by Xtext.
31 Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().putIfAbsent(XMI_RESOURCE_EXTENSION,
32 new ProblemFactoryImpl());
33 }
34}
diff --git a/subprojects/language-model/src/main/resources/model/builtin.problem_xmi b/subprojects/language-model/src/main/resources/model/builtin.problem_xmi
deleted file mode 100644
index 9255ab66..00000000
--- a/subprojects/language-model/src/main/resources/model/builtin.problem_xmi
+++ /dev/null
@@ -1,67 +0,0 @@
1<?xml version="1.0" encoding="UTF-8"?>
2<problem:Problem
3 xmi:version="2.0"
4 xmlns:xmi="http://www.omg.org/XMI"
5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6 xmlns:problem="https://refinery.tools/emf/2021/Problem"
7 xsi:schemaLocation="https://refinery.tools/emf/2021/Problem problem.ecore"
8 name="builtin">
9 <statements
10 xsi:type="problem:ClassDeclaration"
11 name="node"
12 abstract="true">
13 <referenceDeclarations
14 name="equals"
15 referenceType="//@statements.0"
16 opposite="//@statements.0/@referenceDeclarations.0">
17 <multiplicity
18 xsi:type="problem:UnboundedMultiplicity"/>
19 </referenceDeclarations>
20 </statements>
21 <statements
22 xsi:type="problem:PredicateDefinition"
23 name="exists">
24 <parameters
25 name="node"
26 parameterType="//@statements.0"/>
27 </statements>
28 <statements
29 xsi:type="problem:ClassDeclaration"
30 name="domain"
31 abstract="true"
32 superTypes="//@statements.0"/>
33 <statements
34 xsi:type="problem:ClassDeclaration"
35 name="data"
36 abstract="true"
37 superTypes="//@statements.0"/>
38 <statements
39 xsi:type="problem:EnumDeclaration"
40 name="bool">
41 <literals
42 name="false"/>
43 <literals
44 name="true"/>
45 </statements>
46 <statements
47 xsi:type="problem:ClassDeclaration"
48 name="real"
49 superTypes="//@statements.3">
50 <newNode
51 name="new"/>
52 </statements>
53 <statements
54 xsi:type="problem:ClassDeclaration"
55 name="int"
56 superTypes="//@statements.3">
57 <newNode
58 name="new"/>
59 </statements>
60 <statements
61 xsi:type="problem:ClassDeclaration"
62 name="string"
63 superTypes="//@statements.3">
64 <newNode
65 name="new"/>
66 </statements>
67</problem:Problem>
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
deleted file mode 100644
index d0990dc0..00000000
--- a/subprojects/language-model/src/testFixtures/java/tools/refinery/language/model/tests/ProblemTestUtil.java
+++ /dev/null
@@ -1,197 +0,0 @@
1package tools.refinery.language.model.tests;
2
3import java.util.List;
4import java.util.stream.Stream;
5
6import org.eclipse.emf.ecore.resource.Resource.Diagnostic;
7import org.eclipse.emf.ecore.util.EcoreUtil;
8
9import tools.refinery.language.model.ProblemUtil;
10import tools.refinery.language.model.problem.ActionLiteral;
11import tools.refinery.language.model.problem.Argument;
12import tools.refinery.language.model.problem.Assertion;
13import tools.refinery.language.model.problem.AssertionArgument;
14import tools.refinery.language.model.problem.Atom;
15import tools.refinery.language.model.problem.ClassDeclaration;
16import tools.refinery.language.model.problem.Conjunction;
17import tools.refinery.language.model.problem.DeleteActionLiteral;
18import tools.refinery.language.model.problem.EnumDeclaration;
19import tools.refinery.language.model.problem.IndividualDeclaration;
20import tools.refinery.language.model.problem.Literal;
21import tools.refinery.language.model.problem.NamedElement;
22import tools.refinery.language.model.problem.NegativeLiteral;
23import tools.refinery.language.model.problem.NewActionLiteral;
24import tools.refinery.language.model.problem.Node;
25import tools.refinery.language.model.problem.NodeAssertionArgument;
26import tools.refinery.language.model.problem.NodeValueAssertion;
27import tools.refinery.language.model.problem.Parameter;
28import tools.refinery.language.model.problem.ParametricDefinition;
29import tools.refinery.language.model.problem.PredicateDefinition;
30import tools.refinery.language.model.problem.Problem;
31import tools.refinery.language.model.problem.ReferenceDeclaration;
32import tools.refinery.language.model.problem.Relation;
33import tools.refinery.language.model.problem.RuleDefinition;
34import tools.refinery.language.model.problem.Statement;
35import tools.refinery.language.model.problem.ValueActionLiteral;
36import tools.refinery.language.model.problem.ValueLiteral;
37import tools.refinery.language.model.problem.Variable;
38import tools.refinery.language.model.problem.VariableOrNode;
39import tools.refinery.language.model.problem.VariableOrNodeArgument;
40
41public 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}