Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
StereotypeOCLwithEcore
An example of an editor action that can be contributed against the UML Editor, which checks the OCL-defined constraints of the stereotypes applied to the selected element. This example uses the Ecore environment instead of the UML environment.
public class NewAction2 implements IEditorActionDelegate { protected Shell shell = null; protected UMLEditor editor = null; protected IStructuredSelection sel; public NewAction2() { super(); } public void setActiveEditor(IAction action,IEditorPart targetEditor) { this.editor = (UMLEditor) targetEditor; if ( targetEditor != null ) { this.shell = targetEditor.getSite().getShell(); } } public void setActivePart(IAction action, IWorkbenchPart targetPart) { } public Object[] getElements(Object inputElement) { return new Object[0]; } public void run(IAction action) { Shell shell = new Shell(); EObject context=null; if (sel != null && !sel.isEmpty()) { Object selected = sel.getFirstElement(); if (selected instanceof EObject) { context = (EObject) selected; } else if (selected instanceof IAdaptable) { context = (EObject) ((IAdaptable) selected).getAdapter(EObject.class); } } OCL ocl = OCL.newInstance(new EcoreEnvironmentFactory( new DelegatingPackageRegistry( context.eResource().getResourceSet().getPackageRegistry(), EPackage.Registry.INSTANCE))); OCL.Helper helper = ocl.createOCLHelper(); helper.setContext(context.eClass()); if (helper.getContextClassifier() == context) { helper.setContext(context.eClass()); } ocl.setParseTracingEnabled(true); OCLExpression<EClassifier> parsed=null; Element objectselected= (Element) sel.getFirstElement(); EList<Constraint> constraints = objectselected.getAppliedStereotypes().get(0).getOwnedRules(); Constraint myconstraint=constraints.get(0); String body=myconstraint.getSpecification().stringValue(); try { parsed = helper.createQuery(body); } catch (ParserException e1) { e1.printStackTrace(); } try { Object o=ocl.evaluate(context, parsed); if (o.toString().equals("true")) { MessageDialog.openInformation(shell,"Validerprofile Plug-in","validation passed"); } else { MessageDialog.openInformation(shell,"Validerprofile Plug-in","constraints violated!"); } } catch (Exception e) { MessageDialog.openInformation(shell,"voici le probleme",e.getLocalizedMessage()); e.printStackTrace(); } } public void selectionChanged(IAction action, final ISelection selection) { try{ if (selection instanceof IStructuredSelection) { sel = (IStructuredSelection) selection; } } catch (Exception e) { throw new RuntimeException(e); } finally { action.setEnabled((null != sel)); } } }