aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/language-semantics/src/main/java/tools/refinery/language/semantics/SemanticsUtils.java
diff options
context:
space:
mode:
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.java41
1 files changed, 39 insertions, 2 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 110295b2..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 */
@@ -8,11 +8,14 @@ package tools.refinery.language.semantics;
8import com.google.inject.Inject; 8import com.google.inject.Inject;
9import com.google.inject.Singleton; 9import com.google.inject.Singleton;
10import com.google.inject.name.Named; 10import com.google.inject.name.Named;
11import org.eclipse.emf.ecore.EClass;
11import org.eclipse.emf.ecore.EObject; 12import org.eclipse.emf.ecore.EObject;
12import org.eclipse.emf.ecore.util.EcoreUtil; 13import org.eclipse.emf.ecore.util.EcoreUtil;
13import org.eclipse.xtext.naming.IQualifiedNameConverter; 14import org.eclipse.xtext.naming.IQualifiedNameConverter;
14import org.eclipse.xtext.naming.IQualifiedNameProvider; 15import org.eclipse.xtext.naming.IQualifiedNameProvider;
15import org.eclipse.xtext.naming.QualifiedName; 16import org.eclipse.xtext.naming.QualifiedName;
17import org.eclipse.xtext.resource.IEObjectDescription;
18import org.eclipse.xtext.resource.IResourceDescriptionsProvider;
16import org.eclipse.xtext.scoping.IScope; 19import org.eclipse.xtext.scoping.IScope;
17import org.jetbrains.annotations.NotNull; 20import org.jetbrains.annotations.NotNull;
18import org.jetbrains.annotations.Nullable; 21import org.jetbrains.annotations.Nullable;
@@ -33,6 +36,9 @@ public class SemanticsUtils {
33 @Inject 36 @Inject
34 private IQualifiedNameConverter qualifiedNameConverter; 37 private IQualifiedNameConverter qualifiedNameConverter;
35 38
39 @Inject
40 private IResourceDescriptionsProvider resourceDescriptionsProvider;
41
36 public Optional<String> getNameWithoutRootPrefix(EObject eObject) { 42 public Optional<String> getNameWithoutRootPrefix(EObject eObject) {
37 var qualifiedName = delegateQualifiedNameProvider.getFullyQualifiedName(eObject); 43 var qualifiedName = delegateQualifiedNameProvider.getFullyQualifiedName(eObject);
38 if (qualifiedName == null) { 44 if (qualifiedName == null) {
@@ -42,11 +48,31 @@ public class SemanticsUtils {
42 } 48 }
43 49
44 @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
45 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) {
46 if (qualifiedName == null) { 65 if (qualifiedName == null) {
47 throw new IllegalArgumentException("Element name must not be null"); 66 throw new IllegalArgumentException("Element name must not be null");
48 } 67 }
49 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();
50 if (!iterator.hasNext()) { 76 if (!iterator.hasNext()) {
51 return null; 77 return null;
52 } 78 }
@@ -66,8 +92,19 @@ public class SemanticsUtils {
66 } 92 }
67 93
68 @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
69 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) {
70 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) {
71 if (element == null) { 108 if (element == null) {
72 var qualifiedNameString = qualifiedNameConverter.toString(qualifiedName); 109 var qualifiedNameString = qualifiedNameConverter.toString(qualifiedName);
73 throw new IllegalArgumentException("No such %s: %s" 110 throw new IllegalArgumentException("No such %s: %s"