Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » how to call OCL constraints( generated java source code of model ) from the main code of java(want to validate OCL constraints generated in source code from java code)
how to call OCL constraints( generated java source code of model ) from the main code of java [message #976833] Thu, 08 November 2012 22:07 Go to next message
Ekansh Missing name is currently offline Ekansh Missing nameFriend
Messages: 3
Registered: November 2012
Junior Member
am using OCL constraints on ecore model and generating code. So the thing is as simple as this I have a class Task having attribute startDate and endDate it has an ocl constraint :- invariant which states startDate < endDate.

I have generated java code for this project and there is a java class :
public class TmsValidator extends EObjectValidator {

  //what is diagnosticChain and context in the below method

  public boolean validateTask_C2(Task task, DiagnosticChain diagnostics, Map<Object, Object context) {
    return validate(TmsPackage.Literals.TASK,
         task,
         diagnostics,
         context,
         "http://www.eclipse.org/emf/2002/Ecore/OCL/Pivot",
         "C2",
         TASK__C2__EEXPRESSION,
         Diagnostic.ERROR,
         DIAGNOSTIC_SOURCE,
         0);    
  }
}



So from my main class, how do I validate my task here . I want to call the OCL constraint to check if startDate(Start Date) is less than endDate(End Date)

public static void main(String[] args) {
 // How do I validate my task here  . I want to call the 
    //OCL constraint to check if startDate is less than endDate 
// this is what I am doing
		Map<Object, Object> context = new HashMap<Object, Object>();
		BasicDiagnostic diag = new BasicDiagnostic();
		Task t=  TmsFactory.eINSTANCE.createTask();
		boolean flag =TmsValidator.INSTANCE.validateTask_C2(t, diag, context);
		System.out.println(flag); 
	
	}

Re: how to call OCL constraints( generated java source code of model ) from the main code of java [message #977315 is a reply to message #976833] Fri, 09 November 2012 07:15 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

From the EMF Java

/**
* Validates the object in the given context, optionally producing
diagnostics.
* @param diagnostics a place to accumulate diagnostics; if it's
<code>null</code>, no diagnostics should be produced.
* @param context a place to cache information, if it's
<code>null</code>, no cache is supported.
* @return whether the object is valid.
*/
boolean validate(EObject eObject, DiagnosticChain diagnostics,
Map<Object, Object> context);

In practice the 'context' is pre-initialized by the normal EMF
invocation with usefuil helpers such as a SubstitutionLabelProvider.

Regards

Ed Willink

On 08/11/2012 22:13, Ekansh Mising name wrote:
> am using OCL constraints on ecore model and generating code. So the
> thing is as simple as this I have a class Task having attribute
> startDate and endDate it has an ocl constraint :- invariant which
> states startDate < endDate.
>
> I have generated java code for this project and there is a java class :
> public class TmsValidator extends EObjectValidator {
>
> //what is diagnosticChain and context in the below method
>
> public boolean validateTask_C2(Task task, DiagnosticChain
> diagnostics, Map<Object, Object context) {
> return validate(TmsPackage.Literals.TASK,
> task,
> diagnostics,
> context,
> "http://www.eclipse.org/emf/2002/Ecore/OCL/Pivot",
> "C2",
> TASK__C2__EEXPRESSION,
> Diagnostic.ERROR,
> DIAGNOSTIC_SOURCE,
> 0); }
> }
>
>
> So from my main class, how do I validate my task here . I want to call
> the OCL constraint to check if startDate(Start Date) is less than
> endDate(End Date)
>
> public static void main(String[] args) {
> // How do I validate my task here . I want to call the //OCL
> constraint to check if startDate is less than endDate // this is what
> I am doing
> Map<Object, Object> context = new HashMap<Object, Object>();
> BasicDiagnostic diag = new BasicDiagnostic();
> Task t= TmsFactory.eINSTANCE.createTask();
> boolean flag =TmsValidator.INSTANCE.validateTask_C2(t, diag,
> context);
> System.out.println(flag);
> }
>
Previous Topic:enriching code with ocl
Next Topic:OCL Interpreter in Java (Helios)
Goto Forum:
  


Current Time: Tue Apr 16 04:40:36 GMT 2024

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

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

Back to the top