Using OCLHelper and OCL API... Simple working example !? [message #1224383] |
Thu, 26 December 2013 09:51  |
Eclipse User |
|
|
|
Hi,
I follow OCL Pivot programmer guid to create standalone java code that uses OCLHelper and OCL API's to parse and evaluate OCL query on a UML model (State machine). Simply we pass OCL query as sting and the model URI then we load the model and parse OCL query and return the result .! since I do not find a simple start-up and complete working example, I struggle with the following code (project attached as well) and I keep getting the following error :
Failed to load '_WseC8G46EeO2DthnKXWrzA.essentialocl'
Please any advice to fix this error in order to get OCL result. Is their any simple and complete working example do such functionality.
I appreciate your help
@SuppressWarnings("unchecked")
public List<String> exeOcl(String ModelPath, String OclString) {
List<String> result = null;
try{
// ****** Load UML model ******
baseModelURL = URI.createFileURI(ModelPath);
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
resourceSet.createResource(baseModelURL);
Resource resource = resourceSet.getResource(baseModelURL, true);
Model umlModel = (Model) EcoreUtil.getObjectByType(resource.getContents(), UMLPackage.Literals.MODEL);
System.out.println("The model is successfully loaded. " + "Model Name is : " + umlModel.getName());
//**** OCL parsing OCL *******
OCL ocl = OCL.newInstance(new PivotEnvironmentFactory());
//how to set the context !??
OCLHelper oclHelper = ocl.createOCLHelper(umlModel);
//OCLHelper oclHelper = ocl.createOCLHelper(UMLPackage.Literals.MODEL);
//oclHelper.setInstanceContext(UMLPackage.Literals.MODEL);
ExpressionInOCL OclQuery = oclHelper.createQuery(OclString);
List<String> evaluateResult = (List<String>) ocl.evaluate(umlModel, OclQuery);
// loop in the list to printout the result
while (!evaluateResult.isEmpty()){
System.out.printf("Rsults : " + "%s",evaluateResult);
}
result = evaluateResult;
}
catch (ParserException e1) {
System.out.println("Error ....");
Diagnostic diagnostic = e1.getDiagnostic();
System.out.println(diagnostic.getMessage());
}
catch(Exception e){
System.out.println(e.getMessage());
}
return result;
}
thank you
[Updated on: Thu, 26 December 2013 15:47] by Moderator
|
|
|
Re: Using OCLHelper and OCL API... Simple working example !? [message #1224460 is a reply to message #1224383] |
Thu, 26 December 2013 15:30   |
Eclipse User |
|
|
|
Hi
The attached code has no imports.
http://wiki.eclipse.org/OCL/ForumNetiquette
Regards
Ed Willink
On 26/12/2013 14:51, Naif Mokhayesh wrote:
> Hi,
>
> I follow OCL Pivot programmer guid to create standalone java code that
> uses OCLHelper and OCL API's to parse and evaluate OCL query on a UML
> model (State machine). Simply we pass OCL query as sting and the model
> URI then we load the model and parse OCL query and return the result
> .! since I do not find a simple start-up and complete working example,
> I struggle with the following code (project attached as well) and I
> keep getting the following error :
> Failed to load '_WseC8G46EeO2DthnKXWrzA.essentialocl'
>
> Please any advice to fix this error in order to get OCL result. Is
> their any simple and complete working example do such functionality.
> I appreciate your help
>
>
> @SuppressWarnings("unchecked")
> public List<String> exeOcl(String ModelPath, String OclString) {
>
> List<String> result = null;
>
> try{
>
> // ****** Load UML model ******
> baseModelURL = URI.createFileURI(ModelPath);
>
> ResourceSet resourceSet = new ResourceSetImpl();
> resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI,
> UMLPackage.eINSTANCE);
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION,
> UMLResource.Factory.INSTANCE);
> resourceSet.createResource(baseModelURL);
>
> Resource resource = resourceSet.getResource(baseModelURL,
> true);
>
> Model umlModel = (Model)
> EcoreUtil.getObjectByType(resource.getContents(),
> UMLPackage.Literals.MODEL);
>
> System.out.println("The model is successfully loaded. " +
> "Model Name is : " + umlModel.getName());
>
>
> //**** OCL parsing OCL *******
>
> OCL ocl = OCL.newInstance(new PivotEnvironmentFactory());
>
> //how to set the context !??
> OCLHelper oclHelper = ocl.createOCLHelper(umlModel);
> //OCLHelper oclHelper =
> ocl.createOCLHelper(UMLPackage.Literals.MODEL);
> //oclHelper.setInstanceContext(UMLPackage.Literals.MODEL);
> ExpressionInOCL OclQuery =
> oclHelper.createQuery(OclString);
> List<String> evaluateResult = (List<String>)
> ocl.evaluate(umlModel, OclQuery);
>
> // loop in the list to printout the result
> while (!evaluateResult.isEmpty()){
> System.out.printf("Rsults : " + "%s",evaluateResult);
> }
> result = evaluateResult;
> }
>
> catch (ParserException e1) {
> System.out.println("Error ....");
> Diagnostic diagnostic = e1.getDiagnostic();
> System.out.println(diagnostic.getMessage());
> }
> catch(Exception e){
> System.out.println(e.getMessage());
> }
> return result;
> }
>
>
> thank you
|
|
|
Re: Using OCLHelper and OCL API... Simple working example !? [message #1224465 is a reply to message #1224460] |
Thu, 26 December 2013 15:48   |
Eclipse User |
|
|
|
Hi Ed,
thank you for your reply. sorry for missing imports. I add it now and a complete test project is attached as well
package testocl;
import java.util.List;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.resource.UMLResource;
import org.eclipse.emf.common.util.Diagnostic;
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.util.EcoreUtil;
import org.eclipse.ocl.examples.pivot.ExpressionInOCL;
import org.eclipse.ocl.examples.pivot.OCL;
import org.eclipse.ocl.examples.pivot.ParserException;
import org.eclipse.ocl.examples.pivot.helper.OCLHelper;
import org.eclipse.ocl.examples.pivot.utilities.PivotEnvironmentFactory;
public class UtilitiesLibrary {
private static URI baseModelURL = null;
@SuppressWarnings("unchecked")
public List<String> exeOcl(String ModelPath, String OclString) {
List<String> result = null;
try{
// ****** Load UML model ******
baseModelURL = URI.createFileURI(ModelPath);
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
resourceSet.createResource(baseModelURL);
Resource resource = resourceSet.getResource(baseModelURL, true);
Model umlModel = (Model) EcoreUtil.getObjectByType(resource.getContents(), UMLPackage.Literals.MODEL);
System.out.println("The model is successfully loaded. " + "Model Name is : " + umlModel.getName());
//**** OCL parsing OCL *******
OCL ocl = OCL.newInstance(new PivotEnvironmentFactory());
//how to set the context !??
OCLHelper oclHelper = ocl.createOCLHelper(umlModel);
//OCLHelper oclHelper = ocl.createOCLHelper(UMLPackage.Literals.MODEL);
//oclHelper.setInstanceContext(UMLPackage.Literals.MODEL);
ExpressionInOCL OclQuery = oclHelper.createQuery(OclString);
List<String> evaluateResult = (List<String>) ocl.evaluate(umlModel, OclQuery);
// loop in the list to printout the result
while (!evaluateResult.isEmpty()){
System.out.printf("Rsults : " + "%s",evaluateResult);
}
result = evaluateResult;
}
catch (ParserException e1) {
System.out.println("Error ....");
Diagnostic diagnostic = e1.getDiagnostic();
System.out.println(diagnostic.getMessage());
}
catch(Exception e){
System.out.println(e.getMessage());
}
return result;
}
}
thanks again for help
[Updated on: Thu, 26 December 2013 15:51] by Moderator
|
|
|
Re: Using OCLHelper and OCL API... Simple working example !? [message #1224654 is a reply to message #1224465] |
Fri, 27 December 2013 06:08   |
Eclipse User |
|
|
|
Hi
I see no test project, but the imports show that you are using the pivot
code but seem to have ignored the "Pivot Standalone Configuration"
section of the documentation.
Regards
Ed Willink
On 26/12/2013 20:48, Naif Mokhayesh wrote:
> Hi Ed,
>
> thanks you for your reply. sorry for missing imports. I ad it now and
> complete test project is attached as well
>
>
>
> package testocl;
>
> import java.util.List;
> import org.eclipse.uml2.uml.Model;
> import org.eclipse.uml2.uml.UMLPackage;
> import org.eclipse.uml2.uml.resource.UMLResource;
> import org.eclipse.emf.common.util.Diagnostic;
> 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.util.EcoreUtil;
> import org.eclipse.ocl.examples.pivot.ExpressionInOCL;
> import org.eclipse.ocl.examples.pivot.OCL;
> import org.eclipse.ocl.examples.pivot.ParserException;
> import org.eclipse.ocl.examples.pivot.helper.OCLHelper;
> import org.eclipse.ocl.examples.pivot.utilities.PivotEnvironmentFactory;
>
>
> public class UtilitiesLibrary {
>
> private static URI baseModelURL = null;
>
> @SuppressWarnings("unchecked")
> public List<String> exeOcl(String ModelPath, String OclString) {
>
> List<String> result = null;
>
> try{
>
> // ****** Load UML model ******
> baseModelURL = URI.createFileURI(ModelPath);
>
> ResourceSet resourceSet = new ResourceSetImpl();
> resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI,
> UMLPackage.eINSTANCE);
>
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION,
> UMLResource.Factory.INSTANCE);
> resourceSet.createResource(baseModelURL);
>
> Resource resource = resourceSet.getResource(baseModelURL,
> true);
>
> Model umlModel = (Model)
> EcoreUtil.getObjectByType(resource.getContents(),
> UMLPackage.Literals.MODEL);
>
> System.out.println("The model is successfully loaded. " +
> "Model Name is : " + umlModel.getName());
>
>
> //**** OCL parsing OCL *******
>
> OCL ocl = OCL.newInstance(new PivotEnvironmentFactory());
>
> //how to set the context !??
> OCLHelper oclHelper = ocl.createOCLHelper(umlModel);
> //OCLHelper oclHelper =
> ocl.createOCLHelper(UMLPackage.Literals.MODEL);
> //oclHelper.setInstanceContext(UMLPackage.Literals.MODEL);
> ExpressionInOCL OclQuery = oclHelper.createQuery(OclString);
> List<String> evaluateResult = (List<String>)
> ocl.evaluate(umlModel, OclQuery);
>
> // loop in the list to printout the result
> while (!evaluateResult.isEmpty()){
> System.out.printf("Rsults : " + "%s",evaluateResult);
> }
> result = evaluateResult;
> }
>
> catch (ParserException e1) {
> System.out.println("Error ....");
> Diagnostic diagnostic = e1.getDiagnostic();
> System.out.println(diagnostic.getMessage());
> }
> catch(Exception e){
> System.out.println(e.getMessage());
> }
> return result;
> }
> }
>
>
>
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.08341 seconds