Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

OCL

Revision as of 14:07, 30 May 2007 by Asma.smaoui.cea.fr (Talk | contribs) (Examples)

General Information

Articles

Examples

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)); } } }

Javadoc

Development

This section has links to pages of interest primarily to those developing the OCL component.

Planning

Related Links

Back to the top