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"
(validate stereotypes' constraints) |
(→Examples) |
||
Line 14: | Line 14: | ||
public class NewAction2 implements IEditorActionDelegate | public class NewAction2 implements IEditorActionDelegate | ||
{ | { | ||
− | |||
− | |||
− | |||
protected Shell shell = null; | protected Shell shell = null; | ||
− | |||
− | |||
− | |||
− | |||
protected UMLEditor editor = null; | protected UMLEditor editor = null; | ||
protected IStructuredSelection sel; | protected IStructuredSelection sel; | ||
− | |||
− | |||
− | |||
− | |||
public NewAction2() | public NewAction2() | ||
{ | { | ||
Line 41: | Line 30: | ||
} | } | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
public void setActivePart(IAction action, IWorkbenchPart targetPart) | public void setActivePart(IAction action, IWorkbenchPart targetPart) | ||
{ | { | ||
Line 54: | Line 39: | ||
return new Object[0]; | return new Object[0]; | ||
} | } | ||
− | |||
− | |||
− | |||
public void run(IAction action) | public void run(IAction action) | ||
{ | { | ||
Shell shell = new Shell(); | Shell shell = new Shell(); | ||
EObject context=null; | EObject context=null; | ||
− | |||
− | |||
if (sel != null && !sel.isEmpty()) | if (sel != null && !sel.isEmpty()) | ||
{ | { | ||
Line 76: | Line 56: | ||
} | } | ||
} | } | ||
− | |||
− | |||
OCL ocl = OCL.newInstance(new EcoreEnvironmentFactory( | OCL ocl = OCL.newInstance(new EcoreEnvironmentFactory( | ||
new DelegatingPackageRegistry( | new DelegatingPackageRegistry( | ||
Line 90: | Line 68: | ||
ocl.setParseTracingEnabled(true); | ocl.setParseTracingEnabled(true); | ||
OCLExpression<EClassifier> parsed=null; | OCLExpression<EClassifier> parsed=null; | ||
− | |||
Element objectselected= (Element) sel.getFirstElement(); | Element objectselected= (Element) sel.getFirstElement(); | ||
EList<Constraint> constraints = objectselected.getAppliedStereotypes().get(0).getOwnedRules(); | EList<Constraint> constraints = objectselected.getAppliedStereotypes().get(0).getOwnedRules(); | ||
Constraint myconstraint=constraints.get(0); | Constraint myconstraint=constraints.get(0); | ||
− | String body=myconstraint.getSpecification().stringValue(); | + | String body=myconstraint.getSpecification().stringValue(); |
− | + | ||
− | + | ||
try | try | ||
{ | { | ||
Line 105: | Line 80: | ||
e1.printStackTrace(); | e1.printStackTrace(); | ||
} | } | ||
− | + | try { | |
− | + | ||
− | try | + | |
− | + | ||
− | + | ||
Object o=ocl.evaluate(context, parsed); | Object o=ocl.evaluate(context, parsed); | ||
if (o.toString().equals("true")) | if (o.toString().equals("true")) | ||
Line 119: | Line 90: | ||
MessageDialog.openInformation(shell,"Validerprofile Plug-in","constraints violated!"); | MessageDialog.openInformation(shell,"Validerprofile Plug-in","constraints violated!"); | ||
} | } | ||
− | } | + | } catch (Exception e) { |
− | + | ||
− | + | ||
MessageDialog.openInformation(shell,"voici le probleme",e.getLocalizedMessage()); | MessageDialog.openInformation(shell,"voici le probleme",e.getLocalizedMessage()); | ||
e.printStackTrace(); | e.printStackTrace(); | ||
} | } | ||
− | + | } | |
− | } | + | |
− | + | ||
− | + | ||
− | + | ||
public void selectionChanged(IAction action, final ISelection selection) | public void selectionChanged(IAction action, final ISelection selection) | ||
{ | { | ||
− | try | + | try{ |
− | + | if (selection instanceof IStructuredSelection) | |
− | + | ||
− | + | ||
{ | { | ||
sel = (IStructuredSelection) selection; | sel = (IStructuredSelection) selection; | ||
Line 142: | Line 105: | ||
} | } | ||
− | catch (Exception e) | + | catch (Exception e) { |
− | + | ||
throw new RuntimeException(e); | throw new RuntimeException(e); | ||
} | } | ||
− | finally | + | finally { |
− | + | ||
action.setEnabled((null != sel)); | action.setEnabled((null != sel)); | ||
} | } |
Revision as of 14:07, 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 { 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.