diff options
Diffstat (limited to 'subprojects/language/src/main/java/tools/refinery/language/utils/ProblemUtil.java')
-rw-r--r-- | subprojects/language/src/main/java/tools/refinery/language/utils/ProblemUtil.java | 51 |
1 files changed, 37 insertions, 14 deletions
diff --git a/subprojects/language/src/main/java/tools/refinery/language/utils/ProblemUtil.java b/subprojects/language/src/main/java/tools/refinery/language/utils/ProblemUtil.java index 7b6407e1..f70893e0 100644 --- a/subprojects/language/src/main/java/tools/refinery/language/utils/ProblemUtil.java +++ b/subprojects/language/src/main/java/tools/refinery/language/utils/ProblemUtil.java | |||
@@ -8,11 +8,12 @@ package tools.refinery.language.utils; | |||
8 | import org.eclipse.emf.common.util.URI; | 8 | import org.eclipse.emf.common.util.URI; |
9 | import org.eclipse.emf.ecore.EObject; | 9 | import org.eclipse.emf.ecore.EObject; |
10 | import org.eclipse.emf.ecore.util.EcoreUtil; | 10 | import org.eclipse.emf.ecore.util.EcoreUtil; |
11 | import org.eclipse.xtext.EcoreUtil2; | ||
12 | import tools.refinery.language.library.BuiltinLibrary; | ||
11 | import tools.refinery.language.model.problem.*; | 13 | import tools.refinery.language.model.problem.*; |
12 | 14 | ||
13 | public final class ProblemUtil { | 15 | public final class ProblemUtil { |
14 | public static final String BUILTIN_LIBRARY_NAME = "builtin"; | 16 | public static final String MODULE_EXTENSION = "refinery"; |
15 | public static final URI BUILTIN_LIBRARY_URI = getLibraryUri(); | ||
16 | 17 | ||
17 | private ProblemUtil() { | 18 | private ProblemUtil() { |
18 | throw new IllegalStateException("This is a static utility class and should not be instantiated directly"); | 19 | throw new IllegalStateException("This is a static utility class and should not be instantiated directly"); |
@@ -22,7 +23,7 @@ public final class ProblemUtil { | |||
22 | if (eObject != null) { | 23 | if (eObject != null) { |
23 | var eResource = eObject.eResource(); | 24 | var eResource = eObject.eResource(); |
24 | if (eResource != null) { | 25 | if (eResource != null) { |
25 | return ProblemUtil.BUILTIN_LIBRARY_URI.equals(eResource.getURI()); | 26 | return BuiltinLibrary.BUILTIN_LIBRARY_URI.equals(eResource.getURI()); |
26 | } | 27 | } |
27 | } | 28 | } |
28 | return false; | 29 | return false; |
@@ -54,14 +55,24 @@ public final class ProblemUtil { | |||
54 | return eObject instanceof PredicateDefinition predicateDefinition && predicateDefinition.isError(); | 55 | return eObject instanceof PredicateDefinition predicateDefinition && predicateDefinition.isError(); |
55 | } | 56 | } |
56 | 57 | ||
57 | public static boolean isIndividualNode(Node node) { | 58 | public static boolean isAtomNode(Node node) { |
58 | var containingFeature = node.eContainingFeature(); | 59 | var containingFeature = node.eContainingFeature(); |
59 | return containingFeature == ProblemPackage.Literals.INDIVIDUAL_DECLARATION__NODES | 60 | if (containingFeature == ProblemPackage.Literals.NODE_DECLARATION__NODES) { |
60 | || containingFeature == ProblemPackage.Literals.ENUM_DECLARATION__LITERALS; | 61 | return ((NodeDeclaration) node.eContainer()).getKind() == NodeKind.ATOM; |
62 | } | ||
63 | return containingFeature == ProblemPackage.Literals.ENUM_DECLARATION__LITERALS; | ||
64 | } | ||
65 | |||
66 | public static boolean isMultiNode(Node node) { | ||
67 | var containingFeature = node.eContainingFeature(); | ||
68 | if (containingFeature == ProblemPackage.Literals.NODE_DECLARATION__NODES) { | ||
69 | return ((NodeDeclaration) node.eContainer()).getKind() == NodeKind.MULTI; | ||
70 | } | ||
71 | return containingFeature == ProblemPackage.Literals.CLASS_DECLARATION__NEW_NODE; | ||
61 | } | 72 | } |
62 | 73 | ||
63 | public static boolean isNewNode(Node node) { | 74 | public static boolean isDeclaredNode(Node node) { |
64 | return node.eContainingFeature() == ProblemPackage.Literals.CLASS_DECLARATION__NEW_NODE; | 75 | return node.eContainingFeature() == ProblemPackage.Literals.NODE_DECLARATION__NODES; |
65 | } | 76 | } |
66 | 77 | ||
67 | public static boolean isInvalidMultiplicityConstraint(PredicateDefinition predicateDefinition) { | 78 | public static boolean isInvalidMultiplicityConstraint(PredicateDefinition predicateDefinition) { |
@@ -116,12 +127,24 @@ public final class ProblemUtil { | |||
116 | }; | 127 | }; |
117 | } | 128 | } |
118 | 129 | ||
119 | private static URI getLibraryUri() { | 130 | public static ModuleKind getDefaultModuleKind(Problem problem) { |
120 | var libraryResource = ProblemUtil.class.getClassLoader() | 131 | var resource = problem.eResource(); |
121 | .getResource("tools/refinery/language/%s.problem".formatted(BUILTIN_LIBRARY_NAME)); | 132 | if (resource == null) { |
122 | if (libraryResource == null) { | 133 | return ModuleKind.PROBLEM; |
123 | throw new AssertionError("Library '%s' was not found".formatted(BUILTIN_LIBRARY_NAME)); | ||
124 | } | 134 | } |
125 | return URI.createURI(libraryResource.toString()); | 135 | return getDefaultModuleKind(resource.getURI()); |
136 | } | ||
137 | |||
138 | public static ModuleKind getDefaultModuleKind(URI uri) { | ||
139 | return MODULE_EXTENSION.equals(uri.fileExtension()) ? ModuleKind.MODULE : ModuleKind.PROBLEM; | ||
140 | } | ||
141 | |||
142 | public static boolean isModule(Problem problem) { | ||
143 | return problem.getKind() == ModuleKind.MODULE; | ||
144 | } | ||
145 | |||
146 | public static boolean isInModule(EObject eObject) { | ||
147 | var problem = EcoreUtil2.getContainerOfType(eObject, Problem.class); | ||
148 | return problem != null && isModule(problem); | ||
126 | } | 149 | } |
127 | } | 150 | } |