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