diff options
Diffstat (limited to 'subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SemanticsUtils.java')
-rw-r--r-- | subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SemanticsUtils.java | 51 |
1 files changed, 47 insertions, 4 deletions
diff --git a/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SemanticsUtils.java b/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SemanticsUtils.java index b72ba697..9c40e6df 100644 --- a/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SemanticsUtils.java +++ b/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SemanticsUtils.java | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | 2 | * SPDX-FileCopyrightText: 2023-2024 The Refinery Authors <https://refinery.tools/> |
3 | * | 3 | * |
4 | * SPDX-License-Identifier: EPL-2.0 | 4 | * SPDX-License-Identifier: EPL-2.0 |
5 | */ | 5 | */ |
@@ -7,15 +7,20 @@ package tools.refinery.language.semantics; | |||
7 | 7 | ||
8 | import com.google.inject.Inject; | 8 | import com.google.inject.Inject; |
9 | import com.google.inject.Singleton; | 9 | import com.google.inject.Singleton; |
10 | import com.google.inject.name.Named; | ||
11 | import org.eclipse.emf.ecore.EClass; | ||
10 | import org.eclipse.emf.ecore.EObject; | 12 | import org.eclipse.emf.ecore.EObject; |
11 | import org.eclipse.emf.ecore.util.EcoreUtil; | 13 | import org.eclipse.emf.ecore.util.EcoreUtil; |
12 | import org.eclipse.xtext.naming.IQualifiedNameConverter; | 14 | import org.eclipse.xtext.naming.IQualifiedNameConverter; |
13 | import org.eclipse.xtext.naming.IQualifiedNameProvider; | 15 | import org.eclipse.xtext.naming.IQualifiedNameProvider; |
14 | import org.eclipse.xtext.naming.QualifiedName; | 16 | import org.eclipse.xtext.naming.QualifiedName; |
17 | import org.eclipse.xtext.resource.IEObjectDescription; | ||
18 | import org.eclipse.xtext.resource.IResourceDescriptionsProvider; | ||
15 | import org.eclipse.xtext.scoping.IScope; | 19 | import org.eclipse.xtext.scoping.IScope; |
16 | import org.jetbrains.annotations.NotNull; | 20 | import org.jetbrains.annotations.NotNull; |
17 | import org.jetbrains.annotations.Nullable; | 21 | import org.jetbrains.annotations.Nullable; |
18 | import tools.refinery.language.model.problem.Problem; | 22 | import tools.refinery.language.model.problem.Problem; |
23 | import tools.refinery.language.naming.ProblemQualifiedNameProvider; | ||
19 | 24 | ||
20 | import java.util.Optional; | 25 | import java.util.Optional; |
21 | 26 | ||
@@ -25,10 +30,17 @@ public class SemanticsUtils { | |||
25 | private IQualifiedNameProvider qualifiedNameProvider; | 30 | private IQualifiedNameProvider qualifiedNameProvider; |
26 | 31 | ||
27 | @Inject | 32 | @Inject |
33 | @Named(ProblemQualifiedNameProvider.NAMED_DELEGATE) | ||
34 | private IQualifiedNameProvider delegateQualifiedNameProvider; | ||
35 | |||
36 | @Inject | ||
28 | private IQualifiedNameConverter qualifiedNameConverter; | 37 | private IQualifiedNameConverter qualifiedNameConverter; |
29 | 38 | ||
30 | public Optional<String> getName(EObject eObject) { | 39 | @Inject |
31 | var qualifiedName = qualifiedNameProvider.getFullyQualifiedName(eObject); | 40 | private IResourceDescriptionsProvider resourceDescriptionsProvider; |
41 | |||
42 | public Optional<String> getNameWithoutRootPrefix(EObject eObject) { | ||
43 | var qualifiedName = delegateQualifiedNameProvider.getFullyQualifiedName(eObject); | ||
32 | if (qualifiedName == null) { | 44 | if (qualifiedName == null) { |
33 | return Optional.empty(); | 45 | return Optional.empty(); |
34 | } | 46 | } |
@@ -36,11 +48,31 @@ public class SemanticsUtils { | |||
36 | } | 48 | } |
37 | 49 | ||
38 | @Nullable | 50 | @Nullable |
51 | public <T> T maybeGetLocalElement(Problem problem, QualifiedName qualifiedName, Class<T> type, EClass eClass) { | ||
52 | var resource = problem.eResource(); | ||
53 | var resourceSet = resource.getResourceSet(); | ||
54 | var resourceDescriptions = resourceDescriptionsProvider.getResourceDescriptions(resourceSet); | ||
55 | var resourceDescription = resourceDescriptions.getResourceDescription(resource.getURI()); | ||
56 | if (resourceDescription == null) { | ||
57 | return null; | ||
58 | } | ||
59 | var eObjectDescriptions = resourceDescription.getExportedObjects(eClass, qualifiedName, false); | ||
60 | return maybeGet(problem, eObjectDescriptions, qualifiedName, type); | ||
61 | } | ||
62 | |||
63 | @Nullable | ||
39 | public <T> T maybeGetElement(Problem problem, IScope scope, QualifiedName qualifiedName, Class<T> type) { | 64 | public <T> T maybeGetElement(Problem problem, IScope scope, QualifiedName qualifiedName, Class<T> type) { |
40 | if (qualifiedName == null) { | 65 | if (qualifiedName == null) { |
41 | throw new IllegalArgumentException("Element name must not be null"); | 66 | throw new IllegalArgumentException("Element name must not be null"); |
42 | } | 67 | } |
43 | var iterator = scope.getElements(qualifiedName).iterator(); | 68 | var eObjectDescriptions = scope.getElements(qualifiedName); |
69 | return maybeGet(problem, eObjectDescriptions, qualifiedName, type); | ||
70 | } | ||
71 | |||
72 | @Nullable | ||
73 | private <T> T maybeGet(Problem problem, Iterable<IEObjectDescription> eObjectDescriptions, | ||
74 | QualifiedName qualifiedName, Class<T> type) { | ||
75 | var iterator = eObjectDescriptions.iterator(); | ||
44 | if (!iterator.hasNext()) { | 76 | if (!iterator.hasNext()) { |
45 | return null; | 77 | return null; |
46 | } | 78 | } |
@@ -60,8 +92,19 @@ public class SemanticsUtils { | |||
60 | } | 92 | } |
61 | 93 | ||
62 | @NotNull | 94 | @NotNull |
95 | public <T> T getLocalElement(Problem problem, QualifiedName qualifiedName, Class<T> type, EClass eClass) { | ||
96 | var element = maybeGetLocalElement(problem, qualifiedName, type, eClass); | ||
97 | return getOrThrow(element, qualifiedName, type); | ||
98 | } | ||
99 | |||
100 | @NotNull | ||
63 | public <T> T getElement(Problem problem, IScope scope, QualifiedName qualifiedName, Class<T> type) { | 101 | public <T> T getElement(Problem problem, IScope scope, QualifiedName qualifiedName, Class<T> type) { |
64 | var element = maybeGetElement(problem, scope, qualifiedName, type); | 102 | var element = maybeGetElement(problem, scope, qualifiedName, type); |
103 | return getOrThrow(element, qualifiedName, type); | ||
104 | } | ||
105 | |||
106 | @NotNull | ||
107 | private <T> T getOrThrow(@Nullable T element, QualifiedName qualifiedName, Class<T> type) { | ||
65 | if (element == null) { | 108 | if (element == null) { |
66 | var qualifiedNameString = qualifiedNameConverter.toString(qualifiedName); | 109 | var qualifiedNameString = qualifiedNameConverter.toString(qualifiedName); |
67 | throw new IllegalArgumentException("No such %s: %s" | 110 | throw new IllegalArgumentException("No such %s: %s" |