Home » Modeling » OCL » External OCL document
External OCL document [message #68361] |
Wed, 04 March 2009 12:07 |
Timothy Marc Messages: 547 Registered: July 2009 |
Senior Member |
|
|
Hey all,
these are my very first trials with the MDT OCL. What i want to do is
the following:
1. Define an UML model
2. Define external constraint wihtin an OCL document
3. Parse the external OCL document
4. Process the generated AST of this OCL document with specific Visitors
I've read the documentation and the detailed article about how to
process the OCL AST. But i ran into problems, before i was able to
start, in fact:
When i try to parse the OCL document, i got a SemanticException, because
the package was not founded. Here is my shnippet
URI uri = URI.createFileURI("the path");
final ResourceSet set = new ResourceSetImpl();
UMLEnvironmentFactory umlEnv = new UMLEnvironmentFactory();
umlEnv.loadEnvironment(set.getResource(uri,true));
OCL ocl = OCL.newInstance(umlEnv);
OCLInput document = new OCLInput(in);
List<Constraint> constraints = ocl.parse(document);
My document looks like this:
package AccountExample::account
context AccountManager::store(currentBalance:Integer,
money:Integer):Integer
pre: currentBalance > -1 and currentBalance < 1000000 and money > 0
and money < 100000
post: result = currentBalance + money
endpackage
This structure fit exactly to the UML model.
So, why is the package didn't found? And by the way, is the scenario,
i've mentioned above, realizable with MDT OCL?
Thx for any help
Timothy
|
|
|
Re: External OCL document [message #68372 is a reply to message #68361] |
Wed, 04 March 2009 15:01 |
Eclipse User |
|
|
|
Originally posted by: cdamus.zeligsoft.com
Hi, Timothy,
To answer your last question first, Yes, MDT OCL can realize your scenario.
I noticed a few points amiss in your code snippet, if indeed I
understand what it is trying to do.
1. The resource set that you create should be passed to the
UMLEnvironmentFactory's constructor. This will ensure that
any environment that it creates will be able to find your UML
model.
2. You are loading from a URI a Resource that I assume is the
UML model on which your OCL document defines constraints (the
AccountExample). This is not a persisted environment, which
is what the EnvironmentFactory::loadEnvironment(...) API expects.
An environment comprises contstraint defininitions, variable
definitions, additional operation/attribute definitions,
demand-created collection/tuple/message types, and the like.
Let me try to emend the snippet:
// load the UML model in a resource set
URI uri = URI.createFileURI("the path");
final ResourceSet set = new ResourceSetImpl();
set.getResource(uri, true);
// initialize a UML environment factory on that resource set
UMLEnvironmentFactory umlEnv = new UMLEnvironmentFactory(set);
// and an OCL façade on that environment factory (and resource set)
OCL ocl = OCL.newInstance(umlEnv);
// parse the constraints in the document
OCLInput document = new OCLInput(in);
List<Constraint> constraints = ocl.parse(document);
You will also find a working example of parsing an OCL document in the
UML context in the automated tests. Look at the
org.eclipse.ocl.uml.tests.OCLDocumentTest class.
HTH,
Christian
Timothy Marc wrote:
> Hey all,
>
> these are my very first trials with the MDT OCL. What i want to do is
> the following:
>
> 1. Define an UML model
> 2. Define external constraint wihtin an OCL document
> 3. Parse the external OCL document
> 4. Process the generated AST of this OCL document with specific Visitors
>
> I've read the documentation and the detailed article about how to
> process the OCL AST. But i ran into problems, before i was able to
> start, in fact:
>
> When i try to parse the OCL document, i got a SemanticException, because
> the package was not founded. Here is my shnippet
>
> URI uri = URI.createFileURI("the path");
> final ResourceSet set = new ResourceSetImpl();
> UMLEnvironmentFactory umlEnv = new UMLEnvironmentFactory();
> umlEnv.loadEnvironment(set.getResource(uri,true));
> OCL ocl = OCL.newInstance(umlEnv);
> OCLInput document = new OCLInput(in);
> List<Constraint> constraints = ocl.parse(document);
>
> My document looks like this:
>
> package AccountExample::account
> context AccountManager::store(currentBalance:Integer,
> money:Integer):Integer
> pre: currentBalance > -1 and currentBalance < 1000000
> and money > 0 and money < 100000
> post: result = currentBalance + money
> endpackage
>
> This structure fit exactly to the UML model.
>
> So, why is the package didn't found? And by the way, is the scenario,
> i've mentioned above, realizable with MDT OCL?
>
> Thx for any help
> Timothy
|
|
|
Re: External OCL document [message #68401 is a reply to message #68372] |
Wed, 04 March 2009 23:29 |
Timothy Marc Messages: 547 Registered: July 2009 |
Senior Member |
|
|
Christian,
thanks, that works brilliant, but know i got another strange problem,
which i can't solve, even not with the OCLDocumentTest file.
In my class AccountManager there are 3 operations (they are really there
- believe me), and a simple integer property a. I have specified the
following ocl:
################################
context AccountManager::a:Integer
init: self.a = 500
context AccountManager::store(currentBalance:Integer, money:Integer) :
Integer
pre: currentBalance > -1 and currentBalance < 1000000
pre: money > 0 and money < 100000
post: result = currentBalance + money
endpackage
################################
The first constraint works good, but the second onw throws me a
SemanticException, because of an unrecognized contecx
org.eclipse.ocl.SemanticException: Unrecognized context: (
AccountManager store)
at org.eclipse.ocl.util.OCLUtil.checkForErrors(OCLUtil.java:309 )
at org.eclipse.ocl.OCL.parse(OCL.java:274)
at ocltest.OCLInputDocument.testReadDoc(OCLInputDocument.java:7 2)
I've tried to reset the context via OCLHelper.setContext(operation), but
that throws me an ClassCastException (of course). But why isn't the
operation as context recognized???
Thx again.
Timothy
Christian W. Damus schrieb:
> Hi, Timothy,
>
> To answer your last question first, Yes, MDT OCL can realize your scenario.
>
> I noticed a few points amiss in your code snippet, if indeed I
> understand what it is trying to do.
>
> 1. The resource set that you create should be passed to the
> UMLEnvironmentFactory's constructor. This will ensure that
> any environment that it creates will be able to find your UML
> model.
>
> 2. You are loading from a URI a Resource that I assume is the
> UML model on which your OCL document defines constraints (the
> AccountExample). This is not a persisted environment, which
> is what the EnvironmentFactory::loadEnvironment(...) API expects.
> An environment comprises contstraint defininitions, variable
> definitions, additional operation/attribute definitions,
> demand-created collection/tuple/message types, and the like.
>
> Let me try to emend the snippet:
>
> // load the UML model in a resource set
> URI uri = URI.createFileURI("the path");
> final ResourceSet set = new ResourceSetImpl();
> set.getResource(uri, true);
>
> // initialize a UML environment factory on that resource set
> UMLEnvironmentFactory umlEnv = new UMLEnvironmentFactory(set);
>
> // and an OCL façade on that environment factory (and resource set)
> OCL ocl = OCL.newInstance(umlEnv);
>
> // parse the constraints in the document
> OCLInput document = new OCLInput(in);
> List<Constraint> constraints = ocl.parse(document);
>
>
> You will also find a working example of parsing an OCL document in the
> UML context in the automated tests. Look at the
> org.eclipse.ocl.uml.tests.OCLDocumentTest class.
>
> HTH,
>
> Christian
>
>
> Timothy Marc wrote:
>> Hey all,
>>
>> these are my very first trials with the MDT OCL. What i want to do is
>> the following:
>>
>> 1. Define an UML model
>> 2. Define external constraint wihtin an OCL document
>> 3. Parse the external OCL document
>> 4. Process the generated AST of this OCL document with specific Visitors
>>
>> I've read the documentation and the detailed article about how to
>> process the OCL AST. But i ran into problems, before i was able to
>> start, in fact:
>>
>> When i try to parse the OCL document, i got a SemanticException,
>> because the package was not founded. Here is my shnippet
>>
>> URI uri = URI.createFileURI("the path");
>> final ResourceSet set = new ResourceSetImpl();
>> UMLEnvironmentFactory umlEnv = new UMLEnvironmentFactory();
>> umlEnv.loadEnvironment(set.getResource(uri,true));
>> OCL ocl = OCL.newInstance(umlEnv);
>> OCLInput document = new OCLInput(in);
>> List<Constraint> constraints = ocl.parse(document);
>>
>> My document looks like this:
>>
>> package AccountExample::account
>> context AccountManager::store(currentBalance:Integer,
>> money:Integer):Integer
>> pre: currentBalance > -1 and currentBalance < 1000000
>> and money > 0 and money < 100000
>> post: result = currentBalance + money
>> endpackage
>>
>> This structure fit exactly to the UML model.
>>
>> So, why is the package didn't found? And by the way, is the scenario,
>> i've mentioned above, realizable with MDT OCL?
>>
>> Thx for any help
>> Timothy
|
|
|
Re: External OCL document [message #68434 is a reply to message #68361] |
Thu, 05 March 2009 11:36 |
Timothy Marc Messages: 547 Registered: July 2009 |
Senior Member |
|
|
Okay,
don't care for this question. I've found the solution, a quite
interesting, because it deals with an export problem of MagicDraw. I use
MS 16.0 (personal edition) to create my model and export it to EMF. MD
converts the UML Primitive Type Integer to Int, and not to Integer.
After changing this manually in the *.uml file, it works as expected.
FYI
Timothy
Timothy Marc schrieb:
> Hey all,
>
> these are my very first trials with the MDT OCL. What i want to do is
> the following:
>
> 1. Define an UML model
> 2. Define external constraint wihtin an OCL document
> 3. Parse the external OCL document
> 4. Process the generated AST of this OCL document with specific Visitors
>
> I've read the documentation and the detailed article about how to
> process the OCL AST. But i ran into problems, before i was able to
> start, in fact:
>
> When i try to parse the OCL document, i got a SemanticException, because
> the package was not founded. Here is my shnippet
>
> URI uri = URI.createFileURI("the path");
> final ResourceSet set = new ResourceSetImpl();
> UMLEnvironmentFactory umlEnv = new UMLEnvironmentFactory();
> umlEnv.loadEnvironment(set.getResource(uri,true));
> OCL ocl = OCL.newInstance(umlEnv);
> OCLInput document = new OCLInput(in);
> List<Constraint> constraints = ocl.parse(document);
>
> My document looks like this:
>
> package AccountExample::account
> context AccountManager::store(currentBalance:Integer,
> money:Integer):Integer
> pre: currentBalance > -1 and currentBalance < 1000000
> and money > 0 and money < 100000
> post: result = currentBalance + money
> endpackage
>
> This structure fit exactly to the UML model.
>
> So, why is the package didn't found? And by the way, is the scenario,
> i've mentioned above, realizable with MDT OCL?
>
> Thx for any help
> Timothy
|
|
|
Re: External OCL document [message #718666 is a reply to message #68434] |
Thu, 25 August 2011 01:42 |
John Guerson Messages: 51 Registered: August 2011 |
Member |
|
|
Hey guys,
I need the same as Timothy but rather than a UML model i have a profile of UML. I nedd to add some constraints that is related to this profile. So, i've tried to do the same as Thimothy but making some modifications.I was not sucessful. What I have to do to get the same result?
I've used a EcoreEnvironmentFactory (after i finished i realized that the constraints were applied in the metamodel level and not in the model level). I've used a UMLEnvironmentFactory (posted here) and still does not work.Is there some problem with this approach? I need to create my own Environment for that? Here is my code:
Thanks,
John
public static void main (String[] args) throws IOException, ParserException {
ResourceSet resourceSet = new ResourceSetImpl();
File sourceFileOntoUML = new File("src/ProfileModel.ontouml");
URI uri = URI.createFileURI(sourceFileOntoUML.getAbsolutePath());
resourceSet.getPackageRegistry().
put(OntoUMLPackage.eNS_URI, OntoUMLPackage.eINSTANCE);
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().
put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
Resource resource = resourceSet.getResource(uri, true);
UMLEnvironmentFactory envFactory =
new UMLEnvironmentFactory(resourceSet);
OCL ocl = OCL.newInstance(envFactory);
InputStream in = new FileInputStream("src/MyConstraints.ocl");
OCLInput document = new OCLInput(in);
List<Constraint> constraints = ocl.parse(document);
in.close();
}
[Updated on: Mon, 29 August 2011 14:44] Report message to a moderator
|
|
|
Re: External OCL document [message #718667 is a reply to message #718666] |
Thu, 25 August 2011 01:49 |
John Guerson Messages: 51 Registered: August 2011 |
Member |
|
|
Here is the code that i've tried utilizing the EcoreEnvironmentFactory:
ResourceSet resourceSet = new ResourceSetImpl();
File sourceFileOntoUML = new File("src/ProfileModel.ontouml");
URI uri = URI.createFileURI(sourceFileOntoUML.getAbsolutePath());
resourceSet.getPackageRegistry().
put(OntoUMLPackage.eNS_URI,OntoUMLPackage.eINSTANCE);
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().
put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());
Resource resource = resourceSet.getResource(uri, true);
EcoreEnvironmentFactory envFactory =
new EcoreEnvironmentFactory(resourceSet.getPackageRegistry());
envFactory.loadEnvironment(resource);
OCL ocl = OCL.newInstance(envFactory);
InputStream in = new FileInputStream("src/MyConstraints.ocl");
OCLInput document = new OCLInput(in);
List<Constraint> constraints = ocl.parse(document);
in.close();
[Updated on: Mon, 29 August 2011 14:45] Report message to a moderator
|
|
| |
Re: External OCL document [message #718841 is a reply to message #718694] |
Thu, 25 August 2011 13:30 |
John Guerson Messages: 51 Registered: August 2011 |
Member |
|
|
Hi Edward,
When i use the EcoreEnvironmentFactory:
Exception in thread "main" org.eclipse.ocl.SemanticException: [b]Unrecognized context: (School)[/b]
at org.eclipse.ocl.util.OCLUtil.checkForErrors(OCLUtil.java:352)
at org.eclipse.ocl.OCL.parse(OCL.java:323)
at pkg.Test.main(Test.java:59)
If i use the context: "context Classifier" (a classifier of profile metamodel in the OCL context) then it pass...
Exception in thread "main" org.eclipse.ocl.SemanticException: [b]Unrecognized variable: (schoolOf)[/b]
at org.eclipse.ocl.util.OCLUtil.checkForErrors(OCLUtil.java:352)
at org.eclipse.ocl.OCL.parse(OCL.java:323)
at pkg.Test.main(Test.java:59)
What is wrong Edward?
It seems that when i use the ecore environment he looks at the metamodel level (probably in the package registry) rather than a model level (in the resource).
i don't know, i'm confused. Maybe i have to create my own Environment for this kind of binding.
I really appreciate your help. And by the way, sorry for my english..
[Updated on: Mon, 29 August 2011 14:47] Report message to a moderator
|
|
| |
Re: External OCL document [message #719260 is a reply to message #719113] |
Fri, 26 August 2011 14:31 |
John Guerson Messages: 51 Registered: August 2011 |
Member |
|
|
Okay Edward, sorry ... let me explain :
Well, like i said before, i have a model that is a profile of UML and an OCL document with constraints about this model.
I need of the elements, the constructs, of the OCL document that are related to the profile model for doing some transformation of these constraints OCL for another language. But note that the constraints have to be related to the OntoUML model and not with the OntoUML metamodel.
For example, a context have to be a name of a Class of my OntoUML model,an navigation through the association end name have to be a relationship name of my OntoUML model, and so on...
So, basically, this is my problem. I need to write the constraints in an OCL document(stand alone), parse them according to the model and read the constructs of the OCL document for doing transformation of the constructs for another language.
I believe, until now, i have to do the following steps:
- Parse the external OCL document.
- Process the generated AST of this OCL document with specific Visitors.
So Edward, i need of the constructs in the OCL constraints and their contexts for doing my transformation. But first, i think that i have to parse the document according to my profile model.
I'm wasting so much time with that and maybe it's better process the generated AST (if possible, to get the constructs in the OCL document for transforming the constraints in another language) without parsing them with my model. But, again, the constraints will be related to my model, they just not will be parsed.
Thank's for your attention and quickly reply.
Regards,
John.
[Updated on: Mon, 29 August 2011 14:42] Report message to a moderator
|
|
| |
Re: External OCL document [message #719567 is a reply to message #719285] |
Sat, 27 August 2011 22:12 |
John Guerson Messages: 51 Registered: August 2011 |
Member |
|
|
Hi Edward, i had not understood very well, sorry...
So far, i have not figured out what is wrong...
I thought about creating a new topic but I think it's better to post right here.
The plugin of my profile model is not installed in my eclipse... you think that could be that?
The model and the OCL document is on my home/Desktop, separate of my workspace of eclipse.
Thank's for your help.
Regards
John
[Updated on: Mon, 29 August 2011 14:48] Report message to a moderator
|
|
|
Re: External OCL document [message #719634 is a reply to message #719567] |
Sun, 28 August 2011 05:45 |
Ed Willink Messages: 7655 Registered: July 2009 |
Senior Member |
|
|
Hi John
I'm sorry. I have only a limited amount of time that I can devote to
answering queries. If you want help, you must provide something that
helps me to help.
You at least provide a Java file, but the OntoUML package is missing, so
I must invent something. Your code has
/** #### I'm note sure what have to be done here #### */
although the surrounding code looks plausible, so I could investigate
that, but your earlier comments concerned
Exception in thread "main" org.eclipse.ocl.SemanticException:
Unrecognized context: (School)
at org.eclipse.ocl.util.OCLUtil.checkForErrors(OCLUtil.java:352)
at org.eclipse.ocl.OCL.parse(OCL.java:323)
at pkg.OCL2Alloy.main(OCL2Alloy.java:59)
suggesting a meta-model anomally.
I'm afraid that I cannot help further unless you provide a complete
executable demonstration of your query, and the instructions on how to
reproduce your problem. (ZIP your project tree and optionally delete the
bin tree to save space).
Regards
Ed Willink
On 27/08/2011 23:12, John wrote:
>
>
> package pkg;
> import java.io.FileInputStream;
> import java.io.InputStream;
> import java.util.List;
>
> import org.eclipse.emf.common.util.URI;
>
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.ResourceSet;
> import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
> import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
> import org.eclipse.emf.ecore.EClass;
> import org.eclipse.emf.ecore.EObject;
> import org.eclipse.emf.ecore.EClassifier;
>
> import org.eclipse.ocl.OCLInput;
> import org.eclipse.ocl.OCL;
>
> import org.eclipse.ocl.ecore.EcoreEnvironmentFactory;
> import org.eclipse.ocl.ecore.Constraint;
>
> import OntoUML.*;
>
>
> public class Parser {
> public static void main(String[] args) throws {
>
> /** Load an OntoUML model to a Resource */
> // Obtain a new Resource Set
> ResourceSet resourceSet = new ResourceSetImpl();
> // Register the XMI Resource Factory to handle all file
> extensions.
>
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ontouml",
> new XMIResourceFactoryImpl());
> // Register the Package Registry to create new Resources of
> this type
>
> resourceSet.getPackageRegistry().put(OntoUMLPackage.eNS_URI,OntoUMLPackage.eINSTANCE);
>
> // Get the Resource
> URI modelURI =
> URI.createFileURI("/home/john/Desktop/Enrollment.ontouml");
> Resource resource = resourceSet.getResource(modelURI, true);
> System.out.println("Loaded " + resource);
>
> /** Load an OCL model to a OCLInput document */
> InputStream in = new
> FileInputStream("/home/john/Desktop/Enrollment.ocl");
> OCLInput document = new OCLInput(in);
>
> /** Parse the document according to the OntoUML model */
>
> /** #### I'm note sure what have to be done here #### */
> EcoreEnvironmentFactory envFactory = new
> EcoreEnvironmentFactory(resourceSet.getPackageRegistry());
>
> OCL <?, EClassifier, ?, ?, ?, ?, ?, ?, ?, Constraint, EClass,
> EObject> myOcl;
> myOcl = OCL.newInstance(envFactory);
> List<Constraint> constraints = myOcl.parse(document);
>
> System.out.println("completely parsed.");
>
> }catch (Exception e) {
> e.printStackTrace();
> }
> }
> }
>
>
>
> Here is the message:
>
>
>
> Loaded org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl@6d7f11fb
> uri='file:/home/john/Desktop/Enrollment.ontouml'
> Exception in thread "main" org.eclipse.ocl.SemanticException:
> Unrecognized context: (School)
> at org.eclipse.ocl.util.OCLUtil.checkForErrors(OCLUtil.java:352)
> at org.eclipse.ocl.OCL.parse(Parser.java:323)
> at pkg.Parser.main(OCL.java:56)
>
>
> So far, i have not figured out what is wrong...
> I thought about creating a new topic but I think it's better to post
> right here.
> The plugin of the OntoUML model is not installed in my eclipse... you
> think that could be that?
> The OntoUML model and the OCL document is on my home/Desktop, separate
> of my workspace of eclipse.
> Or maybe i have to do something like this: OCL < OntoUMLPackage,
> OntoUML.Classifier, .... , OntoUML.Class ...> ;
>
> Thank's for your help.
>
> Regards
> John
|
|
| |
Goto Forum:
Current Time: Sat Apr 27 00:56:17 GMT 2024
Powered by FUDForum. Page generated in 0.03957 seconds
|