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.
Difference between revisions of "OCL"
m (→Javadoc) |
(validate stereotypes' constraints) |
||
Line 11: | Line 11: | ||
* [[CustomizingOclEnvironments | Customizing OCL Environments]] | * [[CustomizingOclEnvironments | Customizing OCL Environments]] | ||
+ | |||
+ | public class NewAction2 implements IEditorActionDelegate | ||
+ | { | ||
+ | /** | ||
+ | * The shell this action is hosted in | ||
+ | */ | ||
+ | protected Shell shell = null; | ||
+ | |||
+ | /** | ||
+ | * The active editor | ||
+ | */ | ||
+ | protected UMLEditor editor = null; | ||
+ | protected IStructuredSelection sel; | ||
+ | |||
+ | /** | ||
+ | * Constructor for Action1. | ||
+ | */ | ||
+ | public NewAction2() | ||
+ | { | ||
+ | super(); | ||
+ | } | ||
+ | |||
+ | public void setActiveEditor(IAction action,IEditorPart targetEditor) | ||
+ | { | ||
+ | this.editor = (UMLEditor) targetEditor; | ||
+ | if ( targetEditor != null ) | ||
+ | { | ||
+ | this.shell = targetEditor.getSite().getShell(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart) | ||
+ | */ | ||
+ | public void setActivePart(IAction action, IWorkbenchPart targetPart) | ||
+ | { | ||
+ | } | ||
+ | |||
+ | public Object[] getElements(Object inputElement) | ||
+ | { | ||
+ | |||
+ | return new Object[0]; | ||
+ | } | ||
+ | /** | ||
+ | * @see IActionDelegate#run(IAction) | ||
+ | */ | ||
+ | public void run(IAction action) | ||
+ | { | ||
+ | Shell shell = new Shell(); | ||
+ | EObject context=null; | ||
+ | |||
+ | /***********************preparation du contexte a partir de la selection**************/ | ||
+ | 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; | ||
+ | /****************************get the constraint from stereotype***********************/ | ||
+ | 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(); | ||
+ | } | ||
+ | |||
+ | /**************************constraint evaluation*******************************/ | ||
+ | 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(); | ||
+ | } | ||
+ | /**************************************************************************************/ | ||
+ | } | ||
+ | /** | ||
+ | * @see IActionDelegate#selectionChanged(IAction, ISelection) | ||
+ | */ | ||
+ | |||
+ | 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 == | == Javadoc == |
Revision as of 14:03, 30 May 2007
Contents
General Information
- New and Noteworthy features in the 1.1 release, by milestone
Articles
- Implementing Model Integrity in EMF with OCL by Christian W. Damus
- EclipseCon 2007 submission "Effective Use of the Eclipse Modeling Framework" by Ed Merks et al.
Examples
public class NewAction2 implements IEditorActionDelegate { /** * The shell this action is hosted in */ protected Shell shell = null;
/** * The active editor */ protected UMLEditor editor = null; protected IStructuredSelection sel;
/** * Constructor for Action1. */ public NewAction2() { super(); }
public void setActiveEditor(IAction action,IEditorPart targetEditor) { this.editor = (UMLEditor) targetEditor; if ( targetEditor != null ) { this.shell = targetEditor.getSite().getShell(); } }
/** * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart) */ public void setActivePart(IAction action, IWorkbenchPart targetPart) { }
public Object[] getElements(Object inputElement) {
return new Object[0]; } /** * @see IActionDelegate#run(IAction) */ public void run(IAction action) { Shell shell = new Shell(); EObject context=null;
/***********************preparation du contexte a partir de la selection**************/ 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; /****************************get the constraint from stereotype***********************/ 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(); }
/**************************constraint evaluation*******************************/ 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(); }
/**************************************************************************************/
} /** * @see IActionDelegate#selectionChanged(IAction, ISelection) */
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.