Working example for OCL as UML query language [message #795552] |
Fri, 10 February 2012 15:54 |
Michael Bob Messages: 29 Registered: February 2012 |
Junior Member |
|
|
Hello,
I want to use OCL as query language for UML models. I.e. for example I want to get the number of classes within my model that begin with a "T".
For learning UML2 and OCL, I have created an example. Here is the code to create a small UML2 model:
public class Test {
public void createModel(String name) {
Model model = UMLFactoryImpl.eINSTANCE.createModel();
model.setName(name);
ResourceSet rs = new ResourceSetImpl();
rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi",
new XMIResourceFactoryImpl());
org.eclipse.uml2.uml.Package p = model.createNestedPackage("Test");
URI saveURI = URI.createFileURI("C:/UML/test.xmi");
p.createOwnedClass("Test", false);
System.out.println(saveURI.toString());
Resource resource = rs.createResource(saveURI);
resource.getContents().add(model);
try {
resource.save(null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OCL ocl;
ocl = OCL.newInstance(EcoreEnvironmentFactory.INSTANCE);
OCLHelper helper = ocl.createOCLHelper();
OCLExpression<EClassifier> query;
try {
query = (OCLExpression<EClassifier>)helper.createQuery("self");
System.out.println(ocl.evaluate(model, query));
} catch (ParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The creation of the model works. However, at the end of my example, I want to access the model using OCL. But, even though I have read some examples, I do not get it. What is the right way to access UML models? In this example, I try to get only the first element of the model. I do not understand, how to specify the model the expression should be evaluated on.
Does anybody have a small example that shows how to use OCL on UML models?
Regards,
Michael
|
|
|
Re: Working example for OCL as UML query language [message #795617 is a reply to message #795552] |
Fri, 10 February 2012 17:32 |
Ed Willink Messages: 7655 Registered: July 2009 |
Senior Member |
|
|
Hi Michael
It used to be necessary to use the Java API to exploit OCL.
The OCL Console was added to facilitate experimenting with short queries
and evaluations.
Helios/Indigo: The OCL editors were edited to facilitate editing OCL in
the large, whether embedded in Ecore or as an independent document.
Helios: OCLinEcore allows interpreted execution of OCL without ever
touching a line of Java.
Indigo: The EssentialOCL editor is embedded within Papyrus to support
OCL in UML.
Juno: The OCL to Java code generator supports compiled execution of OCL
without ever touching a line of Java.
Juno: The Complete OCL Load menu action enables Complete OCL complements
to Ecore or UML or ... meta-models
It will be touch and go for the OCLinUML Xtext editor to make it in time
for Juno.
I suggest you learn to use the interactive facilities before struggling
with the Java APIs which are awkward for Ecore and more so for UML.
Regards
Ed Willink
On 10/02/2012 15:54, Michael Gebhart wrote:
> Hello,
>
> I want to use OCL as query language for UML models. I.e. for example I
> want to get the number of classes within my model that begin with a "T".
> For learning UML2 and OCL, I have created an example. Here is the code
> to create a small UML2 model:
>
> public class Test {
> public void createModel(String name) {
> Model model = UMLFactoryImpl.eINSTANCE.createModel();
> model.setName(name);
>
> ResourceSet rs = new ResourceSetImpl();
>
> rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi",
> new XMIResourceFactoryImpl());
> org.eclipse.uml2.uml.Package p =
> model.createNestedPackage("Test");
>
> URI saveURI = URI.createFileURI("C:/UML/test.xmi");
>
> p.createOwnedClass("Test", false);
>
> System.out.println(saveURI.toString());
> Resource resource = rs.createResource(saveURI);
> resource.getContents().add(model);
> try {
> resource.save(null);
> } catch (IOException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
>
> OCL ocl;
> ocl = OCL.newInstance(EcoreEnvironmentFactory.INSTANCE);
> OCLHelper helper = ocl.createOCLHelper();
>
>
> OCLExpression<EClassifier> query;
> try {
> query =
> (OCLExpression<EClassifier>)helper.createQuery("self");
> System.out.println(ocl.evaluate(model, query));
> } catch (ParserException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
>
> }
> }
>
> The creation of the model works. However, at the end of my example, I
> want to access the model using OCL. But, even though I have read some
> examples, I do not get it. What is the right way to access UML models?
> In this example, I try to get only the first element of the model. I
> do not understand, how to specify the model the expression should be
> evaluated on.
>
> Does anybody have a small example that shows how to use OCL on UML
> models?
>
> Regards,
>
> Michael
|
|
|
|
Re: Working example for OCL as UML query language [message #796107 is a reply to message #796006] |
Sat, 11 February 2012 12:01 |
Ed Willink Messages: 7655 Registered: July 2009 |
Senior Member |
|
|
Hi Michael
Ok, if you're determined to go for the Java API.
There are limited examples in the (Help Contents) OCL Documentation.
PLease file a Bugzilla for any topics that you would like to see; it is
much easier for an impatient user than an rushed developer to spot the
omissions.
There are many examples in the tests plugins:
- org.eclipse.ocl.uml.tests for the legacy UML binding
- org.eclipse.ocl.examples.xtext.tests for the new Pivot binding
But beware that tests are written for testing and so sometimes they are
deliberately obtuse, other times they just didn't get upgraded to
exploit a smoother API.
If you're happy to use the new experimental code,
org.eclipse.ocl.examples.pivot.tests.EvaluateNameVisibilityTest uses a
UML model.
Regards
Ed Willink
On 11/02/2012 08:05, Michael Gebhart wrote:
> Hi Ed,
>
> thank you. However, I am familiar with OCL and I have working queries.
> But now, I want to write an application that evaluates these queries
> on a UML model. And for this purpose, I do not find good examples.
>
> Regards,
>
> Michael
|
|
|
Powered by
FUDForum. Page generated in 0.28858 seconds