aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/language/src/main/java
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2022-07-29 21:57:50 +0200
committerLibravatar Kristóf Marussy <kristof@marussy.com>2022-07-29 21:57:50 +0200
commit68476e86f4c04212c7aae7950550869b67442c10 (patch)
treef45e43a89dc7fb8c65d977f18f51f4e19eead510 /subprojects/language/src/main/java
parentbuild: file encoding import into Eclipse (diff)
downloadrefinery-68476e86f4c04212c7aae7950550869b67442c10.tar.gz
refinery-68476e86f4c04212c7aae7950550869b67442c10.tar.zst
refinery-68476e86f4c04212c7aae7950550869b67442c10.zip
build: clean up buildSrc scripts
Diffstat (limited to 'subprojects/language/src/main/java')
-rw-r--r--subprojects/language/src/main/java/tools/refinery/language/ProblemUtil.java121
1 files changed, 121 insertions, 0 deletions
diff --git a/subprojects/language/src/main/java/tools/refinery/language/ProblemUtil.java b/subprojects/language/src/main/java/tools/refinery/language/ProblemUtil.java
new file mode 100644
index 00000000..d8958381
--- /dev/null
+++ b/subprojects/language/src/main/java/tools/refinery/language/ProblemUtil.java
@@ -0,0 +1,121 @@
1package tools.refinery.language.model;
2
3import java.util.ArrayDeque;
4import java.util.Collection;
5import java.util.Deque;
6import java.util.HashSet;
7import java.util.Optional;
8import java.util.Set;
9
10import org.eclipse.emf.common.util.URI;
11import org.eclipse.emf.ecore.EObject;
12import org.eclipse.emf.ecore.resource.Resource;
13
14import tools.refinery.language.model.problem.ClassDeclaration;
15import tools.refinery.language.model.problem.ImplicitVariable;
16import tools.refinery.language.model.problem.Node;
17import tools.refinery.language.model.problem.Problem;
18import tools.refinery.language.model.problem.ProblemPackage;
19import tools.refinery.language.model.problem.ReferenceDeclaration;
20import tools.refinery.language.model.problem.Relation;
21import tools.refinery.language.model.problem.Variable;
22
23public final class ProblemUtil {
24 public static final String BUILTIN_LIBRARY_NAME = "builtin";
25
26 public static final URI BUILTIN_LIBRARY_URI = getLibraryUri(BUILTIN_LIBRARY_NAME);
27
28 public static final String NODE_CLASS_NAME = "node";
29
30 private ProblemUtil() {
31 throw new IllegalStateException("This is a static utility class and should not be instantiated directly");
32 }
33
34 public static boolean isSingletonVariable(Variable variable) {
35 return variable.eContainingFeature() == ProblemPackage.Literals.VARIABLE_OR_NODE_ARGUMENT__SINGLETON_VARIABLE;
36 }
37
38 public static boolean isImplicitVariable(Variable variable) {
39 return variable instanceof ImplicitVariable;
40 }
41
42 public static boolean isImplicitNode(Node node) {
43 return node.eContainingFeature() == ProblemPackage.Literals.PROBLEM__NODES;
44 }
45
46 public static boolean isImplicit(EObject eObject) {
47 if (eObject instanceof Node node) {
48 return isImplicitNode(node);
49 } else if (eObject instanceof Variable variable) {
50 return isImplicitVariable(variable);
51 } else {
52 return false;
53 }
54 }
55
56 public static boolean isIndividualNode(Node node) {
57 var containingFeature = node.eContainingFeature();
58 return containingFeature == ProblemPackage.Literals.INDIVIDUAL_DECLARATION__NODES
59 || containingFeature == ProblemPackage.Literals.ENUM_DECLARATION__LITERALS;
60 }
61
62 public static boolean isNewNode(Node node) {
63 return node.eContainingFeature() == ProblemPackage.Literals.CLASS_DECLARATION__NEW_NODE;
64 }
65
66 public static Optional<Problem> getBuiltInLibrary(EObject context) {
67 return Optional.ofNullable(context.eResource()).map(Resource::getResourceSet)
68 .map(resourceSet -> resourceSet.getResource(BUILTIN_LIBRARY_URI, true)).map(Resource::getContents)
69 .filter(contents -> !contents.isEmpty()).map(contents -> contents.get(0))
70 .filter(Problem.class::isInstance).map(Problem.class::cast);
71 }
72
73 public static boolean isBuiltIn(EObject eObject) {
74 if (eObject != null) {
75 var eResource = eObject.eResource();
76 if (eResource != null) {
77 return BUILTIN_LIBRARY_URI.equals(eResource.getURI());
78 }
79 }
80 return false;
81 }
82
83 public static Optional<ClassDeclaration> getNodeClassDeclaration(EObject context) {
84 return getBuiltInLibrary(context).flatMap(problem -> problem.getStatements().stream()
85 .filter(ClassDeclaration.class::isInstance).map(ClassDeclaration.class::cast)
86 .filter(declaration -> NODE_CLASS_NAME.equals(declaration.getName())).findFirst());
87 }
88
89 public static Collection<ClassDeclaration> getSuperclassesAndSelf(ClassDeclaration classDeclaration) {
90 Set<ClassDeclaration> found = new HashSet<>();
91 getNodeClassDeclaration(classDeclaration).ifPresent(found::add);
92 Deque<ClassDeclaration> queue = new ArrayDeque<>();
93 queue.addLast(classDeclaration);
94 while (!queue.isEmpty()) {
95 ClassDeclaration current = queue.removeFirst();
96 if (!found.contains(current)) {
97 found.add(current);
98 for (Relation superType : current.getSuperTypes()) {
99 if (superType instanceof ClassDeclaration superDeclaration) {
100 queue.addLast(superDeclaration);
101 }
102 }
103 }
104 }
105 return found;
106 }
107
108 public static Collection<ReferenceDeclaration> getAllReferenceDeclarations(ClassDeclaration classDeclaration) {
109 Set<ReferenceDeclaration> referenceDeclarations = new HashSet<>();
110 for (ClassDeclaration superclass : getSuperclassesAndSelf(classDeclaration)) {
111 referenceDeclarations.addAll(superclass.getReferenceDeclarations());
112 }
113 return referenceDeclarations;
114 }
115
116 private static URI getLibraryUri(String libraryName) {
117 return URI.createURI(ProblemUtil.class.getClassLoader()
118 .getResource("model/" + libraryName + "." + ProblemEMFSetup.XMI_RESOURCE_EXTENSION)
119 .toString());
120 }
121}