Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » Using OCLHelper and OCL API... Simple working example !?(using OCLHelper and OCL in standalone Java code for parsing and evaluating OCL query.)
Using OCLHelper and OCL API... Simple working example !? [message #1224383] Thu, 26 December 2013 14:51 Go to next message
Naif Mokhayesh is currently offline Naif MokhayeshFriend
Messages: 31
Registered: November 2011
Member
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 20:47]

Report message to a moderator

Re: Using OCLHelper and OCL API... Simple working example !? [message #1224460 is a reply to message #1224383] Thu, 26 December 2013 20:30 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
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 20:48 Go to previous messageGo to next message
Naif Mokhayesh is currently offline Naif MokhayeshFriend
Messages: 31
Registered: November 2011
Member
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 20:51]

Report message to a moderator

Re: Using OCLHelper and OCL API... Simple working example !? [message #1224654 is a reply to message #1224465] Fri, 27 December 2013 11:08 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
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;
> }
> }
>
>
>
Re: Using OCLHelper and OCL API... Simple working example !? [message #1224831 is a reply to message #1224654] Fri, 27 December 2013 22:45 Go to previous messageGo to next message
Naif Mokhayesh is currently offline Naif MokhayeshFriend
Messages: 31
Registered: November 2011
Member
Hi,

I'm using OCL within eclipse as plugin project and according to the documentation no need for registration as explained in "Pivot Standalone Configuration".

Anyway, I add the following by as explained in documentation, but still keep getting " failed to load _-cKzAG9FEeOw1708PUAavA.essentialocl" exception!
by the way, the test project was uploaded in the original message and in this message as well.

is the problem in setting OCLHelper context or in the query or what? I did toooooo many trails for more than a week now and still getting same exception message !

any advice please

thank you for your help

//  **** registration and settings 
			org.eclipse.ocl.examples.pivot.OCL.initialize(resourceSet);
			org.eclipse.ocl.examples.pivot.uml.UML2Pivot.initialize(resourceSet);
			org.eclipse.ocl.examples.pivot.model.OCLstdlib.install();
			org.eclipse.uml2.uml.resources.util.UMLResourcesUtil.init(resourceSet);
			org.eclipse.ocl.examples.domain.utilities.StandaloneProjectMap.getAdapter(resourceSet);
Re: Using OCLHelper and OCL API... Simple working example !? [message #1224980 is a reply to message #1224831] Sat, 28 December 2013 10:00 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Apologies. Your original message did indeed have a project attachment.
(I don't seem to be able to get Thunderbird to behave in a familiar way
on my new PC.)

You do not appear to be using OCL as a plugin poject, you are just using
a plugin manifest to assist in defining your standalone classpath. To
run your code as an Eclipse project you would need to provide some UI
action to invoke 'main', or a JUnit plugin test that invokes 'main'.

You therefore need the standalone setup steps. In particular
EssentialOCLSetup.doSetup() that will associate the Xtext editor/parser
with the *.essentialocl extension and so resolve your loading failure.

Regards

Ed Willink




On 27/12/2013 22:45, Naif Mokhayesh wrote:
> Hi,
>
> I'm using OCL within eclipse as plugin project and according to the documentation no need for registration as explained in "Pivot Standalone Configuration".
>
> Anyway, I add the following by as explained in documentation, but still keep getting " failed to load _-cKzAG9FEeOw1708PUAavA.essentialocl" exception!
> by the way, the test project was uploaded in the original message and in this message as well.
>
> is the problem in setting OCLHelper context or in the query or what? I did toooooo many trails for more than a week now and still getting same exception message !
>
> any advice please
>
> thank you for your help
>
>
> // **** registration and settings
> org.eclipse.ocl.examples.pivot.OCL.initialize(resourceSet);
> org.eclipse.ocl.examples.pivot.uml.UML2Pivot.initialize(resourceSet);
> org.eclipse.ocl.examples.pivot.model.OCLstdlib.install();
> org.eclipse.uml2.uml.resources.util.UMLResourcesUtil.init(resourceSet);
> org.eclipse.ocl.examples.domain.utilities.StandaloneProjectMap.getAdapter(resourceSet);
>
Re: Using OCLHelper and OCL API... Simple working example !? [message #1226080 is a reply to message #1224980] Tue, 31 December 2013 16:29 Go to previous message
Naif Mokhayesh is currently offline Naif MokhayeshFriend
Messages: 31
Registered: November 2011
Member
Hi Ed,

Really I appreciate your reply, it works now!. and your reply in this post was also valuable

http://www.eclipse.org/forums/index.php/t/515271/

Happy new year to you and to every member of this forum Smile
Previous Topic:oclxmi with ocl:///-URLs fails to load
Next Topic:addHelperOperation/callOperation in pivot
Goto Forum:
  


Current Time: Thu Apr 25 05:20:01 GMT 2024

Powered by FUDForum. Page generated in 0.03792 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top