Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » enabling OCL to parse user definded functions
enabling OCL to parse user definded functions [message #1083851] Sat, 10 August 2013 16:04 Go to next message
felix Kurth is currently offline felix KurthFriend
Messages: 5
Registered: July 2013
Junior Member
Hey,

I could use some more detailed advice on how to add additional Arithmetic functions to the Eclipse ocl. Currently i am parsing OCL Expressions with some arithmetic constraints and i want OCL also to parse the Expression x^y or sin(y). As i already saw the easyest way seems to be an completeOCL document defining those functios and then loading the complete ocl document in addition to the model. But how do i have to specify such globaly available functions can somebody provide an example?

thanks in advance
Felix
Re: enabling OCL to parse user definded functions [message #1083877 is a reply to message #1083851] Sat, 10 August 2013 16:55 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 4154
Registered: July 2009
Senior Member
Hi

The lack of a maths library for OCL is longstanding, and the
difficulties of providing domain libraries is one of my motivations for
modeling the OCL Standard Library so that extensions can reuse its
modeling capabilities.

Clean library extensions are potentially straightforward

context Real
def: sin() : Real ....

supporting usage as

myNumber.sin()

If you want to avoid the need for prefixes, you could have a bogus self

context OclAny
def: sin(x : Real) : Real ....

supporting usage as

sin(myNumber) or rather self.sin(myNumber)


If you want additional lexical operators, that is much harder. "^" is
particularly hard since it has already been allocated to an OclMessage
operation.


If you are seriously interested in pursuing this have a look at

GIT\org.eclipse.ocl\examples\org.eclipse.ocl.examples.library\model\OCL-2.4.oclstdlib

where you can see the "=>" redirections to Java operations that I'm sure
you will want for sin/pow/....

Regards

Ed Willink


On 10/08/2013 17:04, felix Kurth wrote:
> Hey,
>
> I could use some more detailed advice on how to add additional
> Arithmetic functions to the Eclipse ocl. Currently i am parsing OCL
> Expressions with some arithmetic constraints and i want OCL also to
> parse the Expression x^y or sin(y). As i already saw the easyest way
> seems to be an completeOCL document defining those functios and then
> loading the complete ocl document in addition to the model. But how do
> i have to specify such globaly available functions can somebody
> provide an example?
>
> thanks in advance
> Felix
Re: enabling OCL to parse user definded functions [message #1084412 is a reply to message #1083877] Sun, 11 August 2013 13:48 Go to previous messageGo to next message
felix Kurth is currently offline felix KurthFriend
Messages: 5
Registered: July 2013
Junior Member
Thank you very much Ed. I am still having some troubles. Can you give some more advice on them?

I had produced a file named "math.ocl" with the content:

context OclAny
def: sin(x : Real) : Real

The editor reported a Syntaxerror "no viable alternative at input 'context'" and when loading it into the UML Model via load Complete OCL Resource I get the Message no viable alternative at input 'OclAny'.
then i tried adding the org.eclipse.ocl\examples\org.eclipse.ocl.examples.library to my workspace and added the declaration
	operation "pow"(object2 : OclSelf) : Real precedence=UNARY ;
	operation "sin()"() : Real precedence=UNARY;

directly after
type Real : PrimitiveType conformsTo OclComparable, OclSummable {
	annotation '...'(ClassGroup='Primitive');

In another Plugin Project in my Workspace I have got the follwing peace of Code:
import org.eclipse.ocl.uml.OCL;
import org.eclipse.ocl.uml.OCL.Helper;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Operation;

...
OCL ocl;
ocl = OCL.newInstance();
Helper helper = ocl.createOCLHelper();

ResourceSet resourceSet = new ResourceSetImpl();
UMLResourcesUtil.init(resourceSet); // MDT/UML2 4.0.0 (Juno)
Resource resource = resourceSet.createResource(URI
				.createFileURI("../umlModels/AcademicProblems/model.uml"));
try {
     resource.load(null);
} catch (IOException ioe) {}
...
Class umlClas = ...;
Operation umlOp = ...;
helper.setOperationContext(umlClas, umlOp);
try {
   helper.createPostcondition("x=y pow z");
} catch (ParserException e) {}


what else will i now have to do in order to make the call to createPostcondition succeed? Am I on the right Track?
Re: enabling OCL to parse user definded functions [message #1085707 is a reply to message #1084412] Tue, 13 August 2013 08:52 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 4154
Registered: July 2009
Senior Member
Hi

You need a package, and since OclAny is a built-in name you need to
escape it.

package ocl

context _'OclAny'
def: sin(x : Real) : Real = 0

endpackage

Regards

Ed Willink


On 11/08/2013 14:48, felix Kurth wrote:
> context OclAny
> def: sin(x : Real) : Real
Previous Topic:Visiting expression from SpecificationCS
Next Topic:Error messages of the Audit Rule not shown!!!!
Goto Forum:
  


Current Time: Wed Nov 26 20:52:12 GMT 2014

Powered by FUDForum. Page generated in 0.01759 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software