From 663274763e56b228efe07363b8ede4ce7bebc251 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Tue, 19 Oct 2021 03:36:26 +0200 Subject: chore: remove builtin library xtext dependency --- .../refinery/language/model/ProblemEMFSetup.java | 34 +++++++ .../tools/refinery/language/model/ProblemUtil.java | 102 +++++++++++++++++++++ .../src/main/resources/model/builtin.problem_xmi | 67 ++++++++++++++ .../src/main/resources/model/problem.ecore | 3 +- .../src/main/resources/model/problem.genmodel | 4 +- 5 files changed, 206 insertions(+), 4 deletions(-) create mode 100644 language-model/src/main/java/tools/refinery/language/model/ProblemEMFSetup.java create mode 100644 language-model/src/main/java/tools/refinery/language/model/ProblemUtil.java create mode 100644 language-model/src/main/resources/model/builtin.problem_xmi (limited to 'language-model/src/main') diff --git a/language-model/src/main/java/tools/refinery/language/model/ProblemEMFSetup.java b/language-model/src/main/java/tools/refinery/language/model/ProblemEMFSetup.java new file mode 100644 index 00000000..9383098b --- /dev/null +++ b/language-model/src/main/java/tools/refinery/language/model/ProblemEMFSetup.java @@ -0,0 +1,34 @@ +package tools.refinery.language.model; + +import org.eclipse.emf.ecore.EPackage; +import org.eclipse.emf.ecore.resource.Resource; + +import tools.refinery.language.model.problem.ProblemPackage; +import tools.refinery.language.model.problem.impl.ProblemFactoryImpl; + +public class ProblemEMFSetup { + public static final String XMI_RESOURCE_EXTENSION = "problem_xmi"; + + private ProblemEMFSetup() { + throw new IllegalStateException("This is a static utility class and should not be instantiated directly"); + } + + // Here we can't rely on java.util.HashMap#putIfAbsent, because + // org.eclipse.emf.ecore.impl.EPackageRegistryImpl#containsKey is overridden + // without also overriding putIfAbsent. We must make sure to call the + // overridden containsKey implementation. + @SuppressWarnings("squid:S3824") + public static void doEMFRegistration() { + if (!EPackage.Registry.INSTANCE.containsKey(ProblemPackage.eNS_URI)) { + EPackage.Registry.INSTANCE.put(ProblemPackage.eNS_URI, ProblemPackage.eINSTANCE); + } + + // This Resource.Factory is not actually used once + // tools.refinery.language.ProblemStandaloneSetup.createInjectorAndDoEMFRegistration() + // is called, because if will be replaced by + // tools.refinery.language.resource.ProblemXmiResourceFactory, which implements + // org.eclipse.xtext.resource.IResourceFactory as required by Xtext. + Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().putIfAbsent(XMI_RESOURCE_EXTENSION, + new ProblemFactoryImpl()); + } +} diff --git a/language-model/src/main/java/tools/refinery/language/model/ProblemUtil.java b/language-model/src/main/java/tools/refinery/language/model/ProblemUtil.java new file mode 100644 index 00000000..b6b199f8 --- /dev/null +++ b/language-model/src/main/java/tools/refinery/language/model/ProblemUtil.java @@ -0,0 +1,102 @@ +package tools.refinery.language.model; + +import java.util.ArrayDeque; +import java.util.Collection; +import java.util.Deque; +import java.util.HashSet; +import java.util.Optional; +import java.util.Set; + +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource; + +import tools.refinery.language.model.problem.ClassDeclaration; +import tools.refinery.language.model.problem.Node; +import tools.refinery.language.model.problem.Problem; +import tools.refinery.language.model.problem.ProblemPackage; +import tools.refinery.language.model.problem.ReferenceDeclaration; +import tools.refinery.language.model.problem.Relation; +import tools.refinery.language.model.problem.Variable; + +public final class ProblemUtil { + public static final String BUILTIN_LIBRARY_NAME = "builtin"; + + public static final URI BUILTIN_LIBRARY_URI = getLibraryUri(BUILTIN_LIBRARY_NAME); + + public static final String NODE_CLASS_NAME = "node"; + + private ProblemUtil() { + throw new IllegalStateException("This is a static utility class and should not be instantiated directly"); + } + + public static boolean isSingletonVariable(Variable variable) { + return variable.eContainingFeature() == ProblemPackage.Literals.VARIABLE_OR_NODE_ARGUMENT__SINGLETON_VARIABLE; + } + + public static boolean isUniqueNode(Node node) { + var containingFeature = node.eContainingFeature(); + return containingFeature == ProblemPackage.Literals.UNIQUE_DECLARATION__NODES + || containingFeature == ProblemPackage.Literals.ENUM_DECLARATION__LITERALS; + } + + public static boolean isNewNode(Node node) { + return node.eContainingFeature() == ProblemPackage.Literals.CLASS_DECLARATION__NEW_NODE; + } + + public static Optional getBuiltInLibrary(EObject context) { + return Optional.ofNullable(context.eResource()).map(Resource::getResourceSet) + .map(resourceSet -> resourceSet.getResource(BUILTIN_LIBRARY_URI, true)).map(Resource::getContents) + .filter(contents -> !contents.isEmpty()).map(contents -> contents.get(0)) + .filter(Problem.class::isInstance).map(Problem.class::cast); + } + + public static boolean isBuiltIn(EObject eObject) { + if (eObject != null) { + var eResource = eObject.eResource(); + if (eResource != null) { + return BUILTIN_LIBRARY_URI.equals(eResource.getURI()); + } + } + return false; + } + + public static Optional getNodeClassDeclaration(EObject context) { + return getBuiltInLibrary(context).flatMap(problem -> problem.getStatements().stream() + .filter(ClassDeclaration.class::isInstance).map(ClassDeclaration.class::cast) + .filter(declaration -> NODE_CLASS_NAME.equals(declaration.getName())).findFirst()); + } + + public static Collection getSuperclassesAndSelf(ClassDeclaration classDeclaration) { + Set found = new HashSet<>(); + getNodeClassDeclaration(classDeclaration).ifPresent(found::add); + Deque queue = new ArrayDeque<>(); + queue.addLast(classDeclaration); + while (!queue.isEmpty()) { + ClassDeclaration current = queue.removeFirst(); + if (!found.contains(current)) { + found.add(current); + for (Relation superType : current.getSuperTypes()) { + if (superType instanceof ClassDeclaration superDeclaration) { + queue.addLast(superDeclaration); + } + } + } + } + return found; + } + + public static Collection getAllReferenceDeclarations(ClassDeclaration classDeclaration) { + Set referenceDeclarations = new HashSet<>(); + for (ClassDeclaration superclass : getSuperclassesAndSelf(classDeclaration)) { + referenceDeclarations.addAll(superclass.getReferenceDeclarations()); + } + return referenceDeclarations; + } + + private static URI getLibraryUri(String libraryName) { + return URI.createURI(ProblemUtil.class.getClassLoader() + .getResource("model/" + libraryName + "." + ProblemEMFSetup.XMI_RESOURCE_EXTENSION) + .toString()); + } +} diff --git a/language-model/src/main/resources/model/builtin.problem_xmi b/language-model/src/main/resources/model/builtin.problem_xmi new file mode 100644 index 00000000..9255ab66 --- /dev/null +++ b/language-model/src/main/resources/model/builtin.problem_xmi @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/language-model/src/main/resources/model/problem.ecore b/language-model/src/main/resources/model/problem.ecore index a62590ac..e86f0afd 100644 --- a/language-model/src/main/resources/model/problem.ecore +++ b/language-model/src/main/resources/model/problem.ecore @@ -1,7 +1,6 @@ + xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="problem" nsURI="https://refinery.tools/emf/2021/Problem" nsPrefix="problem"> diff --git a/language-model/src/main/resources/model/problem.genmodel b/language-model/src/main/resources/model/problem.genmodel index e529977f..057b4917 100644 --- a/language-model/src/main/resources/model/problem.genmodel +++ b/language-model/src/main/resources/model/problem.genmodel @@ -8,8 +8,8 @@ copyrightFields="false" operationReflection="true" importOrganizing="true"> problem.ecore - + -- cgit v1.2.3-70-g09d2