diff options
Diffstat (limited to 'org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/ProblemUtil.java')
-rw-r--r-- | org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/ProblemUtil.java | 58 |
1 files changed, 57 insertions, 1 deletions
diff --git a/org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/ProblemUtil.java b/org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/ProblemUtil.java index 0151e0c0..3e7d515f 100644 --- a/org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/ProblemUtil.java +++ b/org.eclipse.viatra.solver.language.parent/org.eclipse.viatra.solver.language/src/main/java/org/eclipse/viatra/solver/language/ProblemUtil.java | |||
@@ -1,17 +1,37 @@ | |||
1 | package org.eclipse.viatra.solver.language; | 1 | package org.eclipse.viatra.solver.language; |
2 | 2 | ||
3 | import java.util.ArrayDeque; | ||
4 | import java.util.Collection; | ||
5 | import java.util.Deque; | ||
6 | import java.util.HashSet; | ||
7 | import java.util.Optional; | ||
8 | import java.util.Set; | ||
9 | |||
10 | import org.eclipse.emf.ecore.EObject; | ||
11 | import org.eclipse.emf.ecore.resource.Resource; | ||
12 | import org.eclipse.viatra.solver.language.model.problem.ClassDeclaration; | ||
3 | import org.eclipse.viatra.solver.language.model.problem.Node; | 13 | import org.eclipse.viatra.solver.language.model.problem.Node; |
14 | import org.eclipse.viatra.solver.language.model.problem.Problem; | ||
4 | import org.eclipse.viatra.solver.language.model.problem.ProblemPackage; | 15 | import org.eclipse.viatra.solver.language.model.problem.ProblemPackage; |
16 | import org.eclipse.viatra.solver.language.model.problem.ReferenceDeclaration; | ||
5 | import org.eclipse.viatra.solver.language.model.problem.Variable; | 17 | import org.eclipse.viatra.solver.language.model.problem.Variable; |
18 | import org.eclipse.viatra.solver.language.scoping.ProblemGlobalScopeProvider; | ||
6 | 19 | ||
20 | import com.google.common.collect.ImmutableList; | ||
7 | import com.google.inject.Singleton; | 21 | import com.google.inject.Singleton; |
8 | 22 | ||
9 | @Singleton | 23 | @Singleton |
10 | public class ProblemUtil { | 24 | public final class ProblemUtil { |
25 | private ProblemUtil() { | ||
26 | throw new IllegalStateException("This is a static utility class and should not be instantiated directly"); | ||
27 | } | ||
28 | |||
11 | public static final String SINGLETON_VARIABLE_PREFIX = "_"; | 29 | public static final String SINGLETON_VARIABLE_PREFIX = "_"; |
12 | 30 | ||
13 | public static final String ENUM_NODE_NAME_QUOTE = "'"; | 31 | public static final String ENUM_NODE_NAME_QUOTE = "'"; |
14 | 32 | ||
33 | public static final String NODE_CLASS_NAME = "node"; | ||
34 | |||
15 | public static boolean isSingletonVariableName(String name) { | 35 | public static boolean isSingletonVariableName(String name) { |
16 | return name != null && name.startsWith(SINGLETON_VARIABLE_PREFIX); | 36 | return name != null && name.startsWith(SINGLETON_VARIABLE_PREFIX); |
17 | } | 37 | } |
@@ -28,4 +48,40 @@ public class ProblemUtil { | |||
28 | public static boolean isNewNode(Node node) { | 48 | public static boolean isNewNode(Node node) { |
29 | return node.eContainingFeature() == ProblemPackage.Literals.CLASS_DECLARATION__NEW_NODE; | 49 | return node.eContainingFeature() == ProblemPackage.Literals.CLASS_DECLARATION__NEW_NODE; |
30 | } | 50 | } |
51 | |||
52 | public static Optional<Problem> getBuiltInLibrary(EObject context) { | ||
53 | return Optional.ofNullable(context.eResource()).map(Resource::getResourceSet) | ||
54 | .map(resourceSet -> resourceSet.getResource(ProblemGlobalScopeProvider.BULTIN_LIBRARY_URI, true)) | ||
55 | .map(Resource::getContents).filter(contents -> !contents.isEmpty()).map(contents -> contents.get(0)) | ||
56 | .filter(Problem.class::isInstance).map(Problem.class::cast); | ||
57 | } | ||
58 | |||
59 | public static Optional<ClassDeclaration> getNodeClassDeclaration(EObject context) { | ||
60 | return getBuiltInLibrary(context).flatMap(problem -> problem.getStatements().stream() | ||
61 | .filter(ClassDeclaration.class::isInstance).map(ClassDeclaration.class::cast) | ||
62 | .filter(declaration -> NODE_CLASS_NAME.equals(declaration.getName())).findFirst()); | ||
63 | } | ||
64 | |||
65 | public static Collection<ClassDeclaration> getSuperclassesAndSelf(ClassDeclaration classDeclaration) { | ||
66 | Set<ClassDeclaration> found = new HashSet<>(); | ||
67 | getNodeClassDeclaration(classDeclaration).ifPresent(found::add); | ||
68 | Deque<ClassDeclaration> queue = new ArrayDeque<>(); | ||
69 | queue.addLast(classDeclaration); | ||
70 | while (!queue.isEmpty()) { | ||
71 | ClassDeclaration current = queue.removeFirst(); | ||
72 | if (!found.contains(current)) { | ||
73 | found.add(current); | ||
74 | queue.addAll(current.getSuperTypes()); | ||
75 | } | ||
76 | } | ||
77 | return found; | ||
78 | } | ||
79 | |||
80 | public static Collection<ReferenceDeclaration> getAllReferenceDeclarations(ClassDeclaration classDeclaration) { | ||
81 | ImmutableList.Builder<ReferenceDeclaration> builder = ImmutableList.builder(); | ||
82 | for (ClassDeclaration superclass : getSuperclassesAndSelf(classDeclaration)) { | ||
83 | builder.addAll(superclass.getReferenceDeclarations()); | ||
84 | } | ||
85 | return builder.build(); | ||
86 | } | ||
31 | } | 87 | } |