Home » Modeling » OCL » Information about processing OCL constraints(OCL contraints)
| | |
Re: Information about processing OCL constraints [message #666607 is a reply to message #666589] |
Thu, 21 April 2011 09:21 |
Ed Willink Messages: 7670 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 #667703 is a reply to message #666614] |
Sun, 01 May 2011 14:32 |
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):
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 14:51 |
Ed Willink Messages: 7670 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 16:10 |
Ed Willink Messages: 7670 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?
>
|
|
| | |
Goto Forum:
Current Time: Fri Sep 20 12:52:32 GMT 2024
Powered by FUDForum. Page generated in 0.05191 seconds
|