diff options
author | 2024-02-11 01:13:13 +0100 | |
---|---|---|
committer | 2024-02-11 19:55:41 +0100 | |
commit | b64100256d399128fc2373195351d259bd2ab1fb (patch) | |
tree | 06341db872c75acaedaf6028c83223669ba12058 /subprojects/language/src | |
parent | feat(language): automatic problem kind inference (diff) | |
download | refinery-b64100256d399128fc2373195351d259bd2ab1fb.tar.gz refinery-b64100256d399128fc2373195351d259bd2ab1fb.tar.zst refinery-b64100256d399128fc2373195351d259bd2ab1fb.zip |
refactor(language): simplify default module kinds
Use a transient attribute instead of an adapter.
Diffstat (limited to 'subprojects/language/src')
3 files changed, 7 insertions, 29 deletions
diff --git a/subprojects/language/src/main/java/tools/refinery/language/parser/ProblemEcoreElementFactory.java b/subprojects/language/src/main/java/tools/refinery/language/parser/ProblemEcoreElementFactory.java index b54c6970..7d246117 100644 --- a/subprojects/language/src/main/java/tools/refinery/language/parser/ProblemEcoreElementFactory.java +++ b/subprojects/language/src/main/java/tools/refinery/language/parser/ProblemEcoreElementFactory.java | |||
@@ -5,9 +5,7 @@ | |||
5 | */ | 5 | */ |
6 | package tools.refinery.language.parser; | 6 | package tools.refinery.language.parser; |
7 | 7 | ||
8 | import org.eclipse.emf.common.notify.impl.AdapterImpl; | ||
9 | import org.eclipse.emf.ecore.EObject; | 8 | import org.eclipse.emf.ecore.EObject; |
10 | import org.eclipse.emf.ecore.util.EcoreUtil; | ||
11 | import org.eclipse.xtext.conversion.ValueConverterException; | 9 | import org.eclipse.xtext.conversion.ValueConverterException; |
12 | import org.eclipse.xtext.nodemodel.INode; | 10 | import org.eclipse.xtext.nodemodel.INode; |
13 | import org.eclipse.xtext.parser.DefaultEcoreElementFactory; | 11 | import org.eclipse.xtext.parser.DefaultEcoreElementFactory; |
@@ -20,29 +18,7 @@ public class ProblemEcoreElementFactory extends DefaultEcoreElementFactory { | |||
20 | EObject object, String feature, Object value, String ruleName, INode node) throws ValueConverterException { | 18 | EObject object, String feature, Object value, String ruleName, INode node) throws ValueConverterException { |
21 | super.set(object, feature, value, ruleName, node); | 19 | super.set(object, feature, value, ruleName, node); |
22 | if (object instanceof Problem problem && ProblemPackage.Literals.PROBLEM__KIND.getName().equals(feature)) { | 20 | if (object instanceof Problem problem && ProblemPackage.Literals.PROBLEM__KIND.getName().equals(feature)) { |
23 | ExplicitAssignmentTracker.install(problem); | 21 | problem.setExplicitKind(true); |
24 | } | ||
25 | } | ||
26 | |||
27 | public static boolean hasExplicitlySetProblemKind(Problem problem) { | ||
28 | return ExplicitAssignmentTracker.hasAdapter(problem); | ||
29 | } | ||
30 | |||
31 | private static class ExplicitAssignmentTracker extends AdapterImpl { | ||
32 | @Override | ||
33 | public boolean isAdapterForType(Object type) { | ||
34 | return type == ExplicitAssignmentTracker.class; | ||
35 | } | ||
36 | |||
37 | public static boolean hasAdapter(Problem problem) { | ||
38 | return EcoreUtil.getAdapter(problem.eAdapters(), ExplicitAssignmentTracker.class) != null; | ||
39 | } | ||
40 | |||
41 | public static void install(Problem problem) { | ||
42 | if (hasAdapter(problem)) { | ||
43 | throw new IllegalStateException("Duplicate explicit assignment of module kind"); | ||
44 | } | ||
45 | problem.eAdapters().add(new ExplicitAssignmentTracker()); | ||
46 | } | 22 | } |
47 | } | 23 | } |
48 | } | 24 | } |
diff --git a/subprojects/language/src/main/java/tools/refinery/language/resource/ProblemResource.java b/subprojects/language/src/main/java/tools/refinery/language/resource/ProblemResource.java index 57b7bb45..440a238e 100644 --- a/subprojects/language/src/main/java/tools/refinery/language/resource/ProblemResource.java +++ b/subprojects/language/src/main/java/tools/refinery/language/resource/ProblemResource.java | |||
@@ -25,7 +25,6 @@ import org.eclipse.xtext.resource.DerivedStateAwareResource; | |||
25 | import org.eclipse.xtext.util.Triple; | 25 | import org.eclipse.xtext.util.Triple; |
26 | import org.jetbrains.annotations.Nullable; | 26 | import org.jetbrains.annotations.Nullable; |
27 | import tools.refinery.language.model.problem.Problem; | 27 | import tools.refinery.language.model.problem.Problem; |
28 | import tools.refinery.language.parser.ProblemEcoreElementFactory; | ||
29 | import tools.refinery.language.utils.ProblemUtil; | 28 | import tools.refinery.language.utils.ProblemUtil; |
30 | 29 | ||
31 | import java.util.Arrays; | 30 | import java.util.Arrays; |
@@ -48,7 +47,11 @@ public class ProblemResource extends DerivedStateAwareResource { | |||
48 | protected void updateInternalState(IParseResult oldParseResult, IParseResult newParseResult) { | 47 | protected void updateInternalState(IParseResult oldParseResult, IParseResult newParseResult) { |
49 | if (isNewRootElement(oldParseResult, newParseResult) && | 48 | if (isNewRootElement(oldParseResult, newParseResult) && |
50 | newParseResult.getRootASTElement() instanceof Problem newRootProblem && | 49 | newParseResult.getRootASTElement() instanceof Problem newRootProblem && |
51 | !ProblemEcoreElementFactory.hasExplicitlySetProblemKind(newRootProblem)) { | 50 | !newRootProblem.isExplicitKind()) { |
51 | // Post-process the parsed model to set its URI-dependent module kind. | ||
52 | // We can't set the default module kind in {@link tools.refinery.language.serializer | ||
53 | // .ProblemTransientValueService}, because the {@link Problem} does not get added into the EMF resource | ||
54 | // before parsing is fully completed. | ||
52 | var defaultModuleKind = ProblemUtil.getDefaultModuleKind(getURI()); | 55 | var defaultModuleKind = ProblemUtil.getDefaultModuleKind(getURI()); |
53 | newRootProblem.setKind(defaultModuleKind); | 56 | newRootProblem.setKind(defaultModuleKind); |
54 | } | 57 | } |
diff --git a/subprojects/language/src/main/java/tools/refinery/language/serializer/ProblemTransientValueService.java b/subprojects/language/src/main/java/tools/refinery/language/serializer/ProblemTransientValueService.java index c364b30b..269e1243 100644 --- a/subprojects/language/src/main/java/tools/refinery/language/serializer/ProblemTransientValueService.java +++ b/subprojects/language/src/main/java/tools/refinery/language/serializer/ProblemTransientValueService.java | |||
@@ -10,7 +10,6 @@ import org.eclipse.emf.ecore.EStructuralFeature; | |||
10 | import org.eclipse.xtext.parsetree.reconstr.impl.DefaultTransientValueService; | 10 | import org.eclipse.xtext.parsetree.reconstr.impl.DefaultTransientValueService; |
11 | import tools.refinery.language.model.problem.Problem; | 11 | import tools.refinery.language.model.problem.Problem; |
12 | import tools.refinery.language.model.problem.ProblemPackage; | 12 | import tools.refinery.language.model.problem.ProblemPackage; |
13 | import tools.refinery.language.parser.ProblemEcoreElementFactory; | ||
14 | import tools.refinery.language.utils.ProblemUtil; | 13 | import tools.refinery.language.utils.ProblemUtil; |
15 | 14 | ||
16 | public class ProblemTransientValueService extends DefaultTransientValueService { | 15 | public class ProblemTransientValueService extends DefaultTransientValueService { |
@@ -18,7 +17,7 @@ public class ProblemTransientValueService extends DefaultTransientValueService { | |||
18 | public boolean isTransient(EObject owner, EStructuralFeature feature, int index) { | 17 | public boolean isTransient(EObject owner, EStructuralFeature feature, int index) { |
19 | if (owner instanceof Problem problem && feature == ProblemPackage.Literals.PROBLEM__KIND) { | 18 | if (owner instanceof Problem problem && feature == ProblemPackage.Literals.PROBLEM__KIND) { |
20 | return problem.getName() == null && problem.getKind() == ProblemUtil.getDefaultModuleKind(problem) && | 19 | return problem.getName() == null && problem.getKind() == ProblemUtil.getDefaultModuleKind(problem) && |
21 | !ProblemEcoreElementFactory.hasExplicitlySetProblemKind(problem); | 20 | !problem.isExplicitKind(); |
22 | } | 21 | } |
23 | return super.isTransient(owner, feature, index); | 22 | return super.isTransient(owner, feature, index); |
24 | } | 23 | } |