Skip to main content

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.

Jump to: navigation, search

Difference between revisions of "OCL"

(Examples)
(Restored deleted examples section)
Line 8: Line 8:
 
* [http://eclipsezilla.eclipsecon.org/show_bug.cgi?id=3619 EclipseCon 2007 submission "Effective Use of the Eclipse Modeling Framework"] by Ed Merks et al.
 
* [http://eclipsezilla.eclipsecon.org/show_bug.cgi?id=3619 EclipseCon 2007 submission "Effective Use of the Eclipse Modeling Framework"] by Ed Merks et al.
  
 +
== Examples ==
  
 
+
* [[CustomizingOclEnvironments | Customizing OCL Environments]]
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));
+
  }
+
}
+
}
+
[[http://www.example.com link title][[http://www.example.com link title][[http://www.example.com link title][[http://www.example.com link title][[Link title]]]]]]
+
  
 
== Javadoc ==
 
== Javadoc ==

Revision as of 15:02, 30 May 2007

General Information

Articles

Examples

Javadoc

Development

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

Planning

Related Links

Back to the top