Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » Creating constraints with opaque expressions in a programmtic way
Creating constraints with opaque expressions in a programmtic way [message #70010] Sun, 26 April 2009 14:57 Go to next message
Eclipse User
Originally posted by: Gregor.Trefs.googlemail.com

This is a multi-part message in MIME format.
--------------080202060007070600090900
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Hi,

I already posted this to eclipse.modeling.mdt.uml2. I'm not sure if it
was the right group, so I post it here, too.

Greetings
Gregor Trefs

--------------080202060007070600090900
Content-Type: message/rfc822;
name="Creating constraints with opaque expressions in a programmtic way.eml"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename*0="Creating constraints with opaque expressions in a programmti";
filename*1="c way.eml"

X-Mozilla-Keys:
Date: Sun, 26 Apr 2009 18:00:39 +0200
From: Gregor Trefs <Gregor.Trefs@googlemail.com>
User-Agent: Thunderbird 2.0.0.17 (Windows/20080914)
MIME-Version: 1.0
Newsgroups: eclipse.modeling.mdt.uml2
Subject: Creating constraints with opaque expressions in a programmtic way
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Hi There,

My name is Gregor Trefs and I'm currently working on my bachelor thesis,
which got me in touch with emf, ecore, uml and ocl. I'm asked to do a
little Eclipse Plug-In which displays a uml-model in a treeviewer (like
the standard uml-editor). The intention is to create an editor which
only shows classes with correspondig operations. Further on, it should
be possible to click on an operation to define, modify or delete
constraints. I actually managed to solve the first part with the help of
the book "Eclipse Modeling Framework 2nd Edition" and some papers.
Now I'm stucked in the second part. My solution so far: I created a view
to display pre-, body-, and postconstraints. This works fine. To have
the possibility to add constraints, I tried to find a programmtic way to
do so. Here is my code:

// Create the new Constraint, depending on the type;
Constraint newConstraint=null;
switch(type){
case precondition:
newConstraint = operation.createPrecondition(name);
case bodycondition:
newConstraint = operation.createBodyCondition(name);
case postcondition:
newConstraint = operation.createPostcondition(name);
}
// Create a new OpaqueExpression
OpaqueExpression expression = UMLFactory.eINSTANCE.createOpaqueExpression();
// Set the expression
expression.getBodies().add(oclString);
newConstraint.setSpecification(expression);

The String oclString is OCL stored in a String (as the name says ;) ).
The type in the switch statement is an enum, which I created by my own.
The code doesn't throw an error, but the resulting constraint does not
behave as intended. I can't deal with it as I can with an constraint
created by the default UML-Editor. I think this malfunction depends on
the way I create the constraint. I deleted the codeline where I added
the language-String "OCL" to the corresponding list, because I read some
of the migration guide where it was mentioned, that OCL is the standard
language for now. Is this the right way ? After searching the net for
hours, I hope you can help me.


The used OCL String has been parsed, to look for errors. Here is the
code (for completion):

if(editor == null) return;
// This returns the actual OCL used by the editor
// This one has UML as Enviroment and is used to parse the constraints
on the M1 Level.
// For further description look into the eclipse help.
OCL ocl = editor.getOCL();
// Get the OCLHelper
OCLHelper<Classifier, Operation, Property, Constraint> oclHelper =
ocl.createOCLHelper();
// Where to apply the constraint ?
oclHelper.setOperationContext(operation.getClass_(), operation);
// Parse the ocl
switch(type){
case precondition:
oclHelper.createPrecondition(oclString);
case bodycondition:
oclHelper.createBodyCondition(oclString);
case postcondition:
oclHelper.createPostcondition(oclString);
}

Greetings
Gregor Trefs


--------------080202060007070600090900--
Re: Creating constraints with opaque expressions in a programmtic way [message #70032 is a reply to message #70010] Sun, 26 April 2009 18:02 Go to previous message
Eclipse User
Originally posted by: Gregor.Trefs.googlemail.com

Gregor Trefs schrieb:
> Hi,
>
> I already posted this to eclipse.modeling.mdt.uml2. I'm not sure if it
> was the right group, so I post it here, too.
>
> Greetings
> Gregor Trefs
>
> ------------------------------------------------------------ ------------
>
> Betreff:
> Creating constraints with opaque expressions in a programmtic way
> Von:
> Gregor Trefs <Gregor.Trefs@googlemail.com>
> Datum:
> Sun, 26 Apr 2009 18:00:39 +0200
>
> Newsgruppen:
> eclipse.modeling.mdt.uml2
>
>
> Hi There,
>
> My name is Gregor Trefs and I'm currently working on my bachelor thesis,
> which got me in touch with emf, ecore, uml and ocl. I'm asked to do a
> little Eclipse Plug-In which displays a uml-model in a treeviewer (like
> the standard uml-editor). The intention is to create an editor which
> only shows classes with correspondig operations. Further on, it should
> be possible to click on an operation to define, modify or delete
> constraints. I actually managed to solve the first part with the help of
> the book "Eclipse Modeling Framework 2nd Edition" and some papers.
> Now I'm stucked in the second part. My solution so far: I created a view
> to display pre-, body-, and postconstraints. This works fine. To have
> the possibility to add constraints, I tried to find a programmtic way to
> do so. Here is my code:
>
> // Create the new Constraint, depending on the type;
> Constraint newConstraint=null;
> switch(type){
> case precondition:
> newConstraint = operation.createPrecondition(name);
> case bodycondition:
> newConstraint = operation.createBodyCondition(name);
> case postcondition:
> newConstraint = operation.createPostcondition(name);
> }
> // Create a new OpaqueExpression
> OpaqueExpression expression =
> UMLFactory.eINSTANCE.createOpaqueExpression();
> // Set the expression
> expression.getBodies().add(oclString);
> newConstraint.setSpecification(expression);
>
> The String oclString is OCL stored in a String (as the name says ;) ).
> The type in the switch statement is an enum, which I created by my own.
> The code doesn't throw an error, but the resulting constraint does not
> behave as intended. I can't deal with it as I can with an constraint
> created by the default UML-Editor. I think this malfunction depends on
> the way I create the constraint. I deleted the codeline where I added
> the language-String "OCL" to the corresponding list, because I read some
> of the migration guide where it was mentioned, that OCL is the standard
> language for now. Is this the right way ? After searching the net for
> hours, I hope you can help me.
>
>
> The used OCL String has been parsed, to look for errors. Here is the
> code (for completion):
>
> if(editor == null) return;
> // This returns the actual OCL used by the editor
> // This one has UML as Enviroment and is used to parse the constraints
> on the M1 Level.
> // For further description look into the eclipse help.
> OCL ocl = editor.getOCL();
> // Get the OCLHelper
> OCLHelper<Classifier, Operation, Property, Constraint> oclHelper =
> ocl.createOCLHelper();
> // Where to apply the constraint ?
> oclHelper.setOperationContext(operation.getClass_(), operation);
> // Parse the ocl
> switch(type){
> case precondition:
> oclHelper.createPrecondition(oclString);
> case bodycondition:
> oclHelper.createBodyCondition(oclString);
> case postcondition:
> oclHelper.createPostcondition(oclString);
> }
>
> Greetings
> Gregor Trefs
>

Hello all,

I managed to figure out, what I did wrong. First of all, I forgot to add
break after each case of the switch statement. After realizing that, I
built up the code once again like the steps I would do if I create the
Constraint manually. Here is the resulting code:

public void saveOCL(Operation operation, Constraint cstr, String
oclString,String name,ExpressionType type) {
// If there is no active editor, give it up and leave
if(editor == null) return;
// Should the constraint be edited ?
boolean edit = cstr != null;
/* I. Build the Constraint
* Determine which type to create.
*/
Constraint newConstraint = null;
switch(type){
case precondition:
newConstraint = operation.createPrecondition(name);
break;
case bodycondition:
newConstraint = operation.createBodyCondition(name);
break;
case postcondition:
newConstraint = operation.createPostcondition(name);
break;
}
// Add the operation as constrained Element
newConstraint.getConstrainedElements().add(operation);
/*
* II. Create Opaque Expression and set values.
*/
OpaqueExpression value = UMLFactory.eINSTANCE.createOpaqueExpression();
value.getBodies().add(oclString);
value.getLanguages().add("OCL");
/*
* III. Add OpaqueExpression to the new constraint
*/
newConstraint.setSpecification(value);
/*
* IV. Edit ? Delete the old constraint.
*/
if(edit) operation.getOwnedRules().remove(cstr);
/*
* V. Save
*/
save();
}

This worked fine for me. I hope this is a help for someone looking for a
programmatic way.

Greetings
Gregor
Previous Topic:OCL and profiles
Next Topic:abstract derive function
Goto Forum:
  


Current Time: Tue Jul 22 09:28:31 EDT 2014

Powered by FUDForum. Page generated in 0.17615 seconds