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.java51
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
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;
11import org.eclipse.emf.ecore.EClass;
10import org.eclipse.emf.ecore.EObject; 12import org.eclipse.emf.ecore.EObject;
11import org.eclipse.emf.ecore.util.EcoreUtil; 13import org.eclipse.emf.ecore.util.EcoreUtil;
12import org.eclipse.xtext.naming.IQualifiedNameConverter; 14import org.eclipse.xtext.naming.IQualifiedNameConverter;
13import org.eclipse.xtext.naming.IQualifiedNameProvider; 15import org.eclipse.xtext.naming.IQualifiedNameProvider;
14import org.eclipse.xtext.naming.QualifiedName; 16import org.eclipse.xtext.naming.QualifiedName;
17import org.eclipse.xtext.resource.IEObjectDescription;
18import org.eclipse.xtext.resource.IResourceDescriptionsProvider;
15import org.eclipse.xtext.scoping.IScope; 19import org.eclipse.xtext.scoping.IScope;
16import org.jetbrains.annotations.NotNull; 20import org.jetbrains.annotations.NotNull;
17import org.jetbrains.annotations.Nullable; 21import org.jetbrains.annotations.Nullable;
18import tools.refinery.language.model.problem.Problem; 22import tools.refinery.language.model.problem.Problem;
23import tools.refinery.language.naming.ProblemQualifiedNameProvider;
19 24
20import java.util.Optional; 25import 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"