Skip to main content



      Home
Home » Modeling » OCL » OCL pre conditions and specific parsing
OCL pre conditions and specific parsing [message #16856] Wed, 11 April 2007 06:05 Go to next message
Eclipse UserFriend
Hello,

I would like to parse a string like this :

"context MyClass::aMethod(arg1 : in integer) : AType pre
arg1 <> self.arg1 and arg1 > AnotherStaticClass.getSomeValue()

context MyClass::otherMethod(arg : String) : void pre
arg <> mySpecialMethod()
"

There are some requirements :
- all pre conditions are defined in the same string (or stream)

- there is no package definition since I already know with which package
I work

- I would like to check the syntax and the variable usages against an
EMF model (not a UML model, just against my model that use classes but
is not UML) to be sure for instance that the class AnotherStaticClass
exists and has a static method called getSomeValue() or to check that
self.arg1 exists.

- I would like to have some "magic" variables and methods that are
already defined internally, like mySpecialMethod(), wich are not in the
model and are just provided to the user for simplicity. An example of
such a variable could be "className" that would be a string describing
the name of the class.

- the validation will not be done against the instance of the model but
with some Java source code that express these constraints. For this
part, like stated in a previous post, I have the answer : I just have to
work with the AST to generate Java source code.

But for the others requirements, I would really appreciate your help,

Thanks in advance.
Re: OCL pre conditions and specific parsing [message #16882 is a reply to message #16856] Wed, 11 April 2007 08:41 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cdamus.ca.ibm.com

Hi, Youmm,

See some replies in-line, below.

HTH,

Christian


Youmm P. wrote:

> Hello,
>
> I would like to parse a string like this :
>
> "context MyClass::aMethod(arg1 : in integer) : AType pre
> arg1 <> self.arg1 and arg1 > AnotherStaticClass.getSomeValue()
>
> context MyClass::otherMethod(arg : String) : void pre
> arg <> mySpecialMethod()
> "
>
> There are some requirements :
> - all pre conditions are defined in the same string (or stream)
> - there is no package definition since I already know with which package
> I work

The org.eclipse.ocl.OCL class has a parse(OCLInput) method that parses text
from a stream or a string, returning a list of the constraints that it
parsed. The package context declaration is not required, but in that case,
in order for the parser to know the namespace of the classes that your text
references, you will need (in code) to set the context package of your OCL
instance's Environment. Have a look at OCL.newInstance(Environment) and
org.eclipse.ocl.EnvironmentFactory.createPackageContext(...) .


> - I would like to check the syntax and the variable usages against an
> EMF model (not a UML model, just against my model that use classes but
> is not UML) to be sure for instance that the class AnotherStaticClass
> exists and has a static method called getSomeValue() or to check that
> self.arg1 exists.

MDT OCL supports both Ecore and UML metamodels, so that should be OK.
However, Ecore doesn't have a notion of static operations as UML does. I'm
not sure what you mean, here.


> - I would like to have some "magic" variables and methods that are
> already defined internally, like mySpecialMethod(), wich are not in the
> model and are just provided to the user for simplicity. An example of
> such a variable could be "className" that would be a string describing
> the name of the class.

You can add variables to the environment using the
Environment.addElement(...) API on the environment of your OCL instance.
Operations are different; they may be defined via "def:" expression
constraints, or may be provided by a custom Environment implementation.
You can extend either of the EcoreEnvironment and UMLEnvironment
implementations to make a "global" type, attribute, operation, or anything
that the parser "looks up" in the environment available to the parser. An
example to get you started is available on the MDT Wiki, courtesy of Chris
Lenz:

http://wiki.eclipse.org/index.php/CustomizingOclEnvironments

This is an example of looking up a "global" class name, and it uses the
deprecated 1.0 API, but the idea is the same in the 1.1 API.


> - the validation will not be done against the instance of the model but
> with some Java source code that express these constraints. For this
> part, like stated in a previous post, I have the answer : I just have to
> work with the AST to generate Java source code.
>
> But for the others requirements, I would really appreciate your help,
>
> Thanks in advance.
Re: OCL pre conditions and specific parsing [message #16912 is a reply to message #16882] Wed, 11 April 2007 09:04 Go to previous messageGo to next message
Eclipse UserFriend
Hi Christian,

First, thanks a lot for this detailed answer.

>> - I would like to check the syntax and the variable usages against an
>> EMF model (not a UML model, just against my model that use classes but
>> is not UML) to be sure for instance that the class AnotherStaticClass
>> exists and has a static method called getSomeValue() or to check that
>> self.arg1 exists.
>
> MDT OCL supports both Ecore and UML metamodels, so that should be OK.
> However, Ecore doesn't have a notion of static operations as UML does. I'm
> not sure what you mean, here.

I'm working with an Ecore model. I really only need a subset of UML
(classes, attributes, methods and that's all : no need for interfaces,
inheritance, composition etc) and some extensions (my own particular
nodes for particular needs, for instance my personal "note" node).

Do you think I should work with the UML model instead of my own model ?
Re: OCL pre conditions and specific parsing [message #16940 is a reply to message #16912] Wed, 11 April 2007 11:00 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cdamus.ca.ibm.com

Hi, Youmm,

I don't know that I can recommend any particular metamodel over any other;
presumably your own extensions to Ecore are already working for you, and
abandoning it just so that working with OCL is easier would not make sense.

It sounds to me like you should be able, with relative ease, to extend the
EcoreEnvironmentFactory and EcoreEnvironment implementations in the
org.eclipse.ocl.ecore plug-in to add your specific semantics. Perhaps the
org.eclipse.ocl.uml.UMLEnvironment can be a guide in supporting your
UML-ish constructs such as static operations. This is assuming that you
are actually fashioning your own metamodel by extending Ecore (in the
literal sense).

Cheers,

Christian


Youmm P. wrote:

> Hi Christian,
>
> First, thanks a lot for this detailed answer.
>
>>> - I would like to check the syntax and the variable usages against an
>>> EMF model (not a UML model, just against my model that use classes but
>>> is not UML) to be sure for instance that the class AnotherStaticClass
>>> exists and has a static method called getSomeValue() or to check that
>>> self.arg1 exists.
>>
>> MDT OCL supports both Ecore and UML metamodels, so that should be OK.
>> However, Ecore doesn't have a notion of static operations as UML does.
>> I'm not sure what you mean, here.
>
> I'm working with an Ecore model. I really only need a subset of UML
> (classes, attributes, methods and that's all : no need for interfaces,
> inheritance, composition etc) and some extensions (my own particular
> nodes for particular needs, for instance my personal "note" node).
>
> Do you think I should work with the UML model instead of my own model ?
Re: OCL pre conditions and specific parsing [message #16969 is a reply to message #16940] Wed, 11 April 2007 11:52 Go to previous messageGo to next message
Eclipse UserFriend
> UML-ish constructs such as static operations. This is assuming that you
> are actually fashioning your own metamodel by extending Ecore (in the
> literal sense).
Well... I suppose it's not, I'm working with GMF. Anyway this solution
seems more complexed that what I first thought.

The problem is that the OCL parser seems to mix syntax checks and
semantic checks.

Is there a way to parse an OCL expression (like defined in my previous
post) to check if it's valid (syntax check) but without checking that
the package, method, variables exists (semantic checks) ?

If yes I could just use the AST (once the syntax is OK) to do this
verifications myself with my model. It should not be too hard for simple
OCL expressions.
Re: OCL pre conditions and specific parsing [message #17028 is a reply to message #16969] Thu, 12 April 2007 06:00 Go to previous messageGo to next message
Eclipse UserFriend
>
> Is there a way to parse an OCL expression (like defined in my previous
> post) to check if it's valid (syntax check) but without checking that
> the package, method, variables exists (semantic checks) ?
>
"public interface OCLHelper<C,O,P,CT>

A utility object that provides OCL syntax completion suggestions for OCL
expressions and convenient API for parsing OCL constraint expressions
without the encumbrance of context declarations. "

I'll take a look to this :)
Re: OCL pre conditions and specific parsing [message #17148 is a reply to message #16969] Thu, 12 April 2007 12:25 Go to previous message
Eclipse UserFriend
Originally posted by: cdamus.ca.ibm.com

Hi, Youmm,

The only separation of syntax parsing from semantic parsing is implemented
internally by the OCLLPGParser/OCLParser classes, which are not API. The
former produces a representation of the concrete syntax from in the input
text and the latter parses this (by validation against the model) into the
abstract syntax.

The problem with attempting a solely syntactic parse is that the concrete
syntax cannot distinguish between:

- operations and iterators (in the case where the iterator variables are
implied)
- variables and attributes (in the case where the source of the attribute
call is implied)
- attributes and association classes
- enumeration literals and qualified class names

so I'm not sure how useful it would be, anyway. If you actually want to do
something meaningful with your parsed OCL expression, the semantic checking
is essential.

Cheers,

Christian


Youmm P. wrote:

>
>> UML-ish constructs such as static operations. This is assuming that you
>> are actually fashioning your own metamodel by extending Ecore (in the
>> literal sense).
> Well... I suppose it's not, I'm working with GMF. Anyway this solution
> seems more complexed that what I first thought.
>
> The problem is that the OCL parser seems to mix syntax checks and
> semantic checks.
>
> Is there a way to parse an OCL expression (like defined in my previous
> post) to check if it's valid (syntax check) but without checking that
> the package, method, variables exists (semantic checks) ?
>
> If yes I could just use the AST (once the syntax is OK) to do this
> verifications myself with my model. It should not be too hard for simple
> OCL expressions.
Previous Topic:Playing with setContext
Next Topic:[Announce] MDT OCL 1.1.0 I200704121504 is available
Goto Forum:
  


Current Time: Sun Jun 08 01:35:58 EDT 2025

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

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

Back to the top