Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » Information about processing OCL constraints(OCL contraints)
Information about processing OCL constraints [message #666545] Wed, 20 April 2011 16:41 Go to next message
miguelafr  is currently offline miguelafr
Messages: 9
Registered: April 2011
Junior Member
Hello,

I am trying to process OCL contraints from a Java program in order to transforming the contraints into Java objects that can be processed to perform actions with those constraints. I am looking for information about that, and I think this is possible with Eclipse OCL.

I found some links such as http://www.eclipse.org/articles/article.php?file=Article-How ToProcessOCLAbstractSyntaxTrees/index.html , but all the information I am finding is very confusing.

Can anybody help me? Does a tuturial about that exist?

Thank you
Re: Information about processing OCL constraints [message #666550 is a reply to message #666545] Wed, 20 April 2011 16:57 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 3844
Registered: July 2009
Senior Member
Hi

That article was a forerunner for the OCL Tools project that never got
past the proposal stage.

Much of the proposed functionality for the OCL Tools project is now in
the Examples and Editors of the main OCL project, which will be
splitting into Core+Tools builds after Indigo.

The editors have an associated pivot model that provides uniform
visitors throughout rather than just OCL. The editors provide an outline
view.

You can find discussion on Java Code Generation in
https://bugs.eclipse.org/bugs/show_bug.cgi?id=279638 Something may
happen soon after Indigo.

Regards

Ed Willink

On 20/04/2011 21:41, miguelafr wrote:
> Hello,
>
> I am trying to process OCL contraints from a Java program in order to
> transforming the contraints into Java objects that can be processed to
> perform actions with those constraints. I am looking for information
> about that, and I think this is possible with Eclipse OCL.
>
> I found some links such as
> http://www.eclipse.org/articles/article.php?file=Article-How ToProcessOCLAbstractSyntaxTrees/index.html
> , but all the information I am finding is very confusing.
>
> Can anybody help me? Does a tuturial about that exist?
>
> Thank you
Re: Information about processing OCL constraints [message #666589 is a reply to message #666550] Thu, 21 April 2011 04:22 Go to previous messageGo to next message
miguelafr  is currently offline miguelafr
Messages: 9
Registered: April 2011
Junior Member
Thank you very much for your answer. I read the discussion and it seems very interesting, but I think it is very advanced for my current knowledge about this topic.

It would be very useful an example which transforms an OCL expression (a String) into Java objects (OCLExpression, IteratorExp, LetExp, ...). Where I can find examples of this?

I was trying to build these objects by hand (new ...()) but the classes that implement these interfaces (IteratorExpImpl, VariableExpImpl, ...) have protected constructors.

Sorry for my ignorance. I am too lost, and getting crazy...
Re: Information about processing OCL constraints [message #666607 is a reply to message #666589] Thu, 21 April 2011 05:21 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 3844
Registered: July 2009
Senior Member
Hi

Sorry, I misunderstood. I though you wanted generated Java, rather than
just a parsed model.

The Programmers's Guide in the OCL Developer Guide accessible via Help
Contents gives basic introductions.

There are 2 choices for OCLExpression.

org.eclipse.ocl.ecore.OCLExpression which is specialized for use with Ecore
org.eclipse.ocl.uml.OCLExpression which is specialized for use with UML2

Indigo introduces a third choice:

org.eclipse.ocl.examples.pivot.OclExpression which prototypes a new
flexible highly OMG conformant implementation

You can find numerous examples of very varying complexity in the JUnit
tests.

Regards

Ed Willink

On 21/04/2011 09:22, miguelafr wrote:
> Thank you very much for your answer. I read the discussion and it
> seems very interesting, but I think it is very advanced for my current
> knowledge about this topic.
>
> It would be very useful an example which transforms an OCL expression
> (a String) into Java objects (OCLExpression, IteratorExp, LetExp,
> ...). Where I can find examples of this?
>
> I was trying to build these objects by hand (new ...()) but the
> classes that implement these interfaces (IteratorExpImpl,
> VariableExpImpl, ...) have protected constructors.
>
> Sorry for my ignorance. I am too lost, and getting crazy...
Re: Information about processing OCL constraints [message #666610 is a reply to message #666607] Thu, 21 April 2011 06:08 Go to previous messageGo to next message
miguelafr  is currently offline miguelafr
Messages: 9
Registered: April 2011
Junior Member
Thank you very much again Edward.

Could you tell me where can I download the JUnit tests from? I am not finding them...

I think I am answering the same as in http://www.eclipse.org/forums/index.php?t=msg&goto=66660 2&S=ae16c16f82444bbc18e9a28a94404e0b#msg_666602

Regards
Re: Information about processing OCL constraints [message #666614 is a reply to message #666610] Thu, 21 April 2011 06:12 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 3844
Registered: July 2009
Senior Member
Hi

CVS host: anonymous@dev.eclipse.org
repository: /cvsroot/modeling
path:
org.eclipse.mdt/org.eclipse.ocl/tests/org.eclipse.ocl.exampl es.xtext.tests

Regards

Ed Willink

On 21/04/2011 11:08, miguelafr wrote:
> Thank you very much again Edward.
>
> Could you tell me where can I download the JUnit tests from? I am not
> finding them...
>
> I think I am answering the same as in
> http://www.eclipse.org/forums/index.php?t=msg&goto=66660 2&S=ae16c16f82444bbc18e9a28a94404e0b#msg_666602
>
> Regards
Re: Information about processing OCL constraints [message #667703 is a reply to message #666614] Sun, 01 May 2011 10:32 Go to previous messageGo to next message
miguelafr  is currently offline miguelafr
Messages: 9
Registered: April 2011
Junior Member
Hello again,

After examining the test examples that you recommended, I was not able to write the code that I need.

Is it possible to read an OCL file in order to have the constraints in Java objects?

I have the following OCL file (the most file OCL file I can imagine):

context Book
inv: true


and I have the following Java code to read the constraints:

OCL<?, EClassifier, ?, ?, ?, ?, ?, ?, ?, Constraint, EClass, EObject> ocl;
ocl = OCL.newInstance(EcoreEnvironmentFactory.INSTANCE);

File file = new File("contraints.ocl");

InputStream in = new FileInputStream(file);

Map<String, Constraint> constraintMap = new HashMap<String, Constraint>();

try {
OCLInput document = new OCLInput(in, "UTF-8");

List<Constraint> constraints = ocl.parse(document);
for (Constraint next : constraints) {
    constraintMap.put(next.getName(), next);

    OCLExpression<EClassifier> body = next.getSpecification().getBodyExpression();
    System.out.printf("Name: " + next.getName() + " | body: " + body);
}
} finally {
    in.close();
}


but I get the error "org.eclipse.ocl.SemanticException: Unrecognized context: (Book)".

I also try to parse a constraint stored in a String variable, with the following code:

try {

    OCL<?, EClassifier, ?, ?, ?, ?, ?, ?, ?, Constraint, EClass, EObject> ocl;
    ocl = OCL.newInstance(EcoreEnvironmentFactory.INSTANCE);

    OCLHelper<EClassifier, ?, ?, Constraint> helper = ocl.createOCLHelper();

    helper.setContext(EcorePackage.Literals.EINVOCATION_TARGET_EXCEPTION);

    ConstraintImpl c = (ConstraintImpl) helper.createInvariant("x > 9");
    
    System.out.println(((ExpressionInOCLImpl)c.getSpecification()).getBodyExpression());

} catch(Exception e) {
    e.printStackTrace();
}


but the error is "org.eclipse.ocl.SemanticException: Unrecognized variable: (x)".

Maybe I am trying to do something that is not possible, or maybe I need more knowlegde. Can anybody help me please?
Re: Information about processing OCL constraints [message #667711 is a reply to message #667703] Sun, 01 May 2011 10:51 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 3844
Registered: July 2009
Senior Member
Hi

Thanks for pointing this out.

The example that you have used for your first attempt unhelpfully
assumes that you have installed your model as a plugin.

If not, rather than using the EcoreEnvironmentFactory.INSTANCE, you need
to construct a new EcoreEnvironmentFactory with a package registry
argument that identifies your Ecore models. I'll reply again when I have
a successful JUnit test of an improved contribution to the documentation.

Your second attempt is your mistake. You have attempted to access for
"x" an EInvocationTargetException, which has no "x" so you get the
resulting error message.

Regards

Ed Willink



On 01/05/2011 15:32, miguelafr wrote:
> Hello again,
>
> After examining the test examples that you recommended, I was not able
> to write the code that I need.
>
> Is it possible to read an OCL file in order to have the constraints in
> Java objects?
>
> I have the following OCL file (the most file OCL file I can imagine):
>
>
> context Book
> inv: true
>
>
> and I have the following Java code to read the constraints:
>
>
> OCL<?, EClassifier, ?, ?, ?, ?, ?, ?, ?, Constraint, EClass, EObject>
> ocl;
> ocl = OCL.newInstance(EcoreEnvironmentFactory.INSTANCE);
>
> File file = new File("contraints.ocl");
>
> InputStream in = new FileInputStream(file);
>
> Map<String, Constraint> constraintMap = new HashMap<String,
> Constraint>();
>
> try {
> OCLInput document = new OCLInput(in, "UTF-8");
>
> List<Constraint> constraints = ocl.parse(document);
> for (Constraint next : constraints) {
> constraintMap.put(next.getName(), next);
>
> OCLExpression<EClassifier> body =
> next.getSpecification().getBodyExpression();
> System.out.printf("Name: " + next.getName() + " | body: " + body);
> }
> } finally {
> in.close();
> }
>
>
> but I get the error "org.eclipse.ocl.SemanticException: Unrecognized
> context: (Book)".
>
> I also try to parse a constraint stored in a String variable, with the
> following code:
>
>
> try {
>
> OCL<?, EClassifier, ?, ?, ?, ?, ?, ?, ?, Constraint, EClass,
> EObject> ocl;
> ocl = OCL.newInstance(EcoreEnvironmentFactory.INSTANCE);
>
> OCLHelper<EClassifier, ?, ?, Constraint> helper =
> ocl.createOCLHelper();
>
> helper.setContext(EcorePackage.Literals.EINVOCATION_TARGET_E XCEPTION);
>
> ConstraintImpl c = (ConstraintImpl) helper.createInvariant("x > 9");
>
> System.out.println(((ExpressionInOCLImpl)c.getSpecification( )).getBodyExpression());
>
> } catch(Exception e) {
> e.printStackTrace();
> }
>
>
> but the error is "org.eclipse.ocl.SemanticException: Unrecognized
> variable: (x)".
>
> Maybe I am trying to do something that is not possible, or maybe I
> need more knowlegde. Can anybody help me please?
Re: Information about processing OCL constraints [message #667719 is a reply to message #667711] Sun, 01 May 2011 12:10 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 3844
Registered: July 2009
Senior Member
Hi

I have now recast the example as a JUnit test and fixed four typos. The
modification to access a specific model is the first few lines. Replace
EXTLibraryPackage by your favourite model. The full working code is:

public void test_parsingDocumentsExample() throws IOException,
ParserException {

//---------------------------------------------------------- ---------------
// The OCL Input

//---------------------------------------------------------- ---------------
EPackage.Registry registry = new EPackageRegistryImpl();
registry.put(EXTLibraryPackage.eNS_URI,
EXTLibraryPackage.eINSTANCE);
EcoreEnvironmentFactory environmentFactory = new
EcoreEnvironmentFactory(registry);
OCL ocl = OCL.newInstance(environmentFactory);

// get an OCL text file via some hypothetical API
InputStream in =
getInputStream("/model/parsingDocumentsExample.ocl");

Map<String, Constraint> constraintMap = new HashMap<String,
Constraint>();

// parse the contents as an OCL document
try {
OCLInput document = new OCLInput(in);

List<Constraint> constraints = ocl.parse(document);
for (Constraint next : constraints) {
constraintMap.put(next.getName(), next);

OCLExpression<EClassifier> body =
next.getSpecification().getBodyExpression();
System.out.printf("%s: %s%n", next.getName(), body);
}
} finally {
in.close();
}

//---------------------------------------------------------- ---------------
// Accessing the Constraints

//---------------------------------------------------------- ---------------
Library library = getLibrary(); // get library from a
hypothetical source

OCLHelper<EClassifier, ?, ?, Constraint> helper =
ocl.createOCLHelper();

// use the constraints defined in the OCL document

// use the getBooks() additional operation to find a book
helper.setContext(EXTLibraryPackage.Literals.LIBRARY);
OCLExpression<EClassifier> query = helper.createQuery(
"getBooks('Bleak House')->asSequence()->first()");

Book book = (Book) ocl.evaluate(library, query);
System.out.printf("Got book: %s%n", book);

// use the unique_title constraint to validate the book
System.out.printf("Validate book: %b%n",
ocl.check(book, constraintMap.get("unique_title")));
}

test implementations of the helper functions are

public InputStream getInputStream(String fileName) throws
MalformedURLException, IOException {
URI uri = getTestModelURI(fileName);
URL url = new URL(uri.toString());
return url.openStream();
}

public Library getLibrary() {
Library library = EXTLibraryFactory.eINSTANCE.createLibrary();
Book aBook = EXTLibraryFactory.eINSTANCE.createBook();
aBook.setTitle("Bleak House");
library.getBooks().add(aBook);
return library;
}



and the valid OCL document is

package extlibrary

context Library
-- get all books with a title in a library and its branches (recursively)
def: getBooks(title : String) : Set(Book) =
books->select(b | b.title = title)->asSet()->union(
branches.getBooks(title)->asSet())

context Book
-- the library containing a book
def: library : Library = Library.allInstances()->any(books->includes(self))

-- book titles are unique within their library branch (and its sub-branches)
inv unique_title: not library.oclIsUndefined() implies
library.getBooks(title) = Set{self}

endpackage

Regards

Ed Willink

On 01/05/2011 15:51, Ed Willink wrote:
> Hi
>
> Thanks for pointing this out.
>
> The example that you have used for your first attempt unhelpfully
> assumes that you have installed your model as a plugin.
>
> If not, rather than using the EcoreEnvironmentFactory.INSTANCE, you
> need to construct a new EcoreEnvironmentFactory with a package
> registry argument that identifies your Ecore models. I'll reply again
> when I have a successful JUnit test of an improved contribution to the
> documentation.
>
> Your second attempt is your mistake. You have attempted to access for
> "x" an EInvocationTargetException, which has no "x" so you get the
> resulting error message.
>
> Regards
>
> Ed Willink
>
>
>
> On 01/05/2011 15:32, miguelafr wrote:
>> Hello again,
>>
>> After examining the test examples that you recommended, I was not
>> able to write the code that I need.
>>
>> Is it possible to read an OCL file in order to have the constraints
>> in Java objects?
>>
>> I have the following OCL file (the most file OCL file I can imagine):
>>
>>
>> context Book
>> inv: true
>>
>>
>> and I have the following Java code to read the constraints:
>>
>>
>> OCL<?, EClassifier, ?, ?, ?, ?, ?, ?, ?, Constraint, EClass, EObject>
>> ocl;
>> ocl = OCL.newInstance(EcoreEnvironmentFactory.INSTANCE);
>>
>> File file = new File("contraints.ocl");
>>
>> InputStream in = new FileInputStream(file);
>>
>> Map<String, Constraint> constraintMap = new HashMap<String,
>> Constraint>();
>>
>> try {
>> OCLInput document = new OCLInput(in, "UTF-8");
>>
>> List<Constraint> constraints = ocl.parse(document);
>> for (Constraint next : constraints) {
>> constraintMap.put(next.getName(), next);
>>
>> OCLExpression<EClassifier> body =
>> next.getSpecification().getBodyExpression();
>> System.out.printf("Name: " + next.getName() + " | body: " + body);
>> }
>> } finally {
>> in.close();
>> }
>>
>>
>> but I get the error "org.eclipse.ocl.SemanticException: Unrecognized
>> context: (Book)".
>>
>> I also try to parse a constraint stored in a String variable, with
>> the following code:
>>
>>
>> try {
>>
>> OCL<?, EClassifier, ?, ?, ?, ?, ?, ?, ?, Constraint, EClass,
>> EObject> ocl;
>> ocl = OCL.newInstance(EcoreEnvironmentFactory.INSTANCE);
>>
>> OCLHelper<EClassifier, ?, ?, Constraint> helper =
>> ocl.createOCLHelper();
>>
>>
>> helper.setContext(EcorePackage.Literals.EINVOCATION_TARGET_E XCEPTION);
>>
>> ConstraintImpl c = (ConstraintImpl) helper.createInvariant("x > 9");
>>
>> System.out.println(((ExpressionInOCLImpl)c.getSpecification( )).getBodyExpression());
>>
>> } catch(Exception e) {
>> e.printStackTrace();
>> }
>>
>>
>> but the error is "org.eclipse.ocl.SemanticException: Unrecognized
>> variable: (x)".
>>
>> Maybe I am trying to do something that is not possible, or maybe I
>> need more knowlegde. Can anybody help me please?
>
Re: Information about processing OCL constraints [message #667735 is a reply to message #667719] Sun, 01 May 2011 16:35 Go to previous messageGo to next message
miguelafr  is currently offline miguelafr
Messages: 9
Registered: April 2011
Junior Member
Thank you for your answers Edward. I have already seen the example you suggest (or something very similar) in the tutorial you recommended, but I would like to parse the constraints without specifying a context.

I think I have the same problem as Robert in http://www.eclipse.org/forums/index.php?t=msg&th=203358& amp;start=0&S=382fa9def7e5a848309c2808ccc717be because I only would like to use Eclipse OCL to read the OCL constraints from a file and to store it into Java objects.
Re: Information about processing OCL constraints [message #667737 is a reply to message #667735] Sun, 01 May 2011 16:59 Go to previous message
Ed Willink is currently offline Ed Willink
Messages: 3844
Registered: July 2009
Senior Member
Hi

Yes it is the same example just corrected to work.

It would be nice to specify null as the context, but the old code does
not allow it. But to specify null it means you have no model references,
in which case you can specify anything you like since it isnt't used.

Regards

Ed Willink

On 01/05/2011 21:35, miguelafr wrote:
> Thank you for your answers Edward. I have already seen the example you
> suggest (or something very similar) in the tutorial you recommended,
> but I would like to parse the constraints without specifying a context.
>
> I think I have the same problem as Robert in
> http://www.eclipse.org/forums/index.php?t=msg&th=203358& amp;start=0&S=382fa9def7e5a848309c2808ccc717be
> because I only would like to use Eclipse OCL to read the OCL
> constraints from a file and to store it into Java objects.
Previous Topic:Parallelized execution of OCL queries
Next Topic:[Announce] MDT/OCL 3.1.0 M7 is now available
Goto Forum:
  


Current Time: Sun Apr 20 16:07:54 EDT 2014

Powered by FUDForum. Page generated in 0.01750 seconds