public static MetaModelManager getMetaModelManager(Resource resource) { MetaModelManagerResourceAdapter adapter = MetaModelManagerResourceAdapter.getAdapter(resource, null); MetaModelManager metaModelManager = adapter.getMetaModelManager(); assert metaModelManager != null; return metaModelManager; }
/** * Create an Xtext resource containing the parsed expression within a typeContext and supervised * by a metaModelManager. * * <p>Provided an EssentialOCL resource registration has been made the created resource named * internal.essentialocl may be cast to an XtextResource. Semantic errors may be found at the * Resource.errors and may be converted to ParseExceptions by invoking checkResourceErrors. * * @param metaModelManager the overall type / meta-model domain * @param typeContext the type that provides the scope of expression * @param expression to be parsed * @return the Xtext resource which may be cast to XtextResource * @throws IOException if resource loading fails */ public static Resource createXtextResource( MetaModelManager metaModelManager, URI uri, NamedElement typeContext, String expression) throws IOException { InputStream inputStream = new ByteArrayInputStream(expression.getBytes()); ResourceSetImpl resourceSet = new ResourceSetImpl(); Resource resource = resourceSet.createResource(uri); MetaModelManagerResourceAdapter.getAdapter(resource, metaModelManager); if (resource instanceof EvaluationContext) { ((EvaluationContext) resource).setContext(typeContext, null); } resource.load(inputStream, null); return resource; }
public static ExpressionInOcl resolveSpecification( MetaModelManager metaModelManager, URI uri, NamedElement contextClassifier, String expression) throws ParserException { Resource resource = null; try { resource = createXtextResource(metaModelManager, uri, contextClassifier, expression); checkResourceErrors(NLS.bind(OCLMessages.ErrorsInResource, expression), resource); return getExpressionInOcl(resource); } catch (IOException e) { // throw new ParserException("Failed to load expression", e); ExpressionInOcl specification = PivotFactory.eINSTANCE.createExpressionInOcl(); OclExpression invalidValueBody = metaModelManager.createInvalidExpression(); specification.setBodyExpression(invalidValueBody); return specification; } finally { if (resource != null) { MetaModelManagerResourceAdapter adapter = MetaModelManagerResourceAdapter.findAdapter(resource); if (adapter != null) { adapter.dispose(); } } } }