Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » OCL in Xtext editor: Quick fixes for constraints
OCL in Xtext editor: Quick fixes for constraints [message #989407] Thu, 06 December 2012 06:34 Go to next message
Daniel Strueber is currently offline Daniel StrueberFriend
Messages: 14
Registered: March 2011
Junior Member
Hi,

I have written an Xtext DSL and now I want to provide some OCL constraints for the generated meta-model in order to support validation and quick fixes based on these constraints.

The nice presentation provided at [1] showed me how to refer to OCL constraints in the Xtext validator class using CompleteOCL. This was very intuitive and works well as for the validation part. However, as for the quick fix part I am not sure: is there any way to let many programmatically access the constraints to define quick fixes (@fix annotation)?

As I did not have an answer for this question, I thought of another way of doing it by defining the constraints by individual methods in the Xtext validator class and calling the validation API. However, from the small examples given in the documentation I ran into some runtime errors that looked like something was not properly initialised (could provide you with details later). The complete example source code was a bit cumbersome and hard to understand. So my question would be, has someone figured this out and could provide a minimal example for the right way to call the API?

Regards,
Daniel

[1] http://www.slideshare.net/EdWillink/enrich-your-models-with-ocl

[Updated on: Thu, 06 December 2012 06:38]

Report message to a moderator

Re: OCL in Xtext editor: Quick fixes for constraints [message #989420 is a reply to message #989407] Thu, 06 December 2012 07:48 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
HI

I'm glad you had some success.

Quickfixes have no extensibility at present, and indeed I have fixed a
few bugs in them since Juno.

Without any detail on your errors, it is of course very difficult to
comment.

Any suggestions you have in this area are welcome; I feel the whole
severity/diagnostic/reaction deserves something better.

Regards

Ed Willink


On 06/12/2012 06:34, Daniel Strueber wrote:
> Hi,
>
> I have written an Xtext DSL and now I want to provide some OCL
> constraints for the generated meta-model in order to support
> validation and quick fixes based on these constraints.
>
> The nice presentationg provided at [1] showed me how to refer to OCL
> constraints in the Xtext validator class using CompleteOCL. This was
> very intuitive and works well as for the validation part. However, as
> for the quick fix part I am not sure: is there any way to let many
> programmatically access the constraints to define quick fixes (@fix
> annotation)?
>
> As I did not have an answer for this question, I thought of another
> way of doing it by defining the constraints by individual methods in
> the Xtext validator class and calling the validation API. However,
> from the small examples given in the documentation I ran into some
> runtime errors that looked like something was not properly initialised
> (could provide you with details only later). The complete example
> source code was a bit cumbersome and hard to understand. So my
> question would be, can someone provide a minimal example for the right
> way to call the API?
>
> Regards,
> Daniel
>
> [1] http://www.slideshare.net/EdWillink/enrich-your-models-with-ocl
Re: OCL in Xtext editor: Quick fixes for constraints [message #989506 is a reply to message #989420] Thu, 06 December 2012 14:34 Go to previous messageGo to next message
Daniel Strueber is currently offline Daniel StrueberFriend
Messages: 14
Registered: March 2011
Junior Member
Hi Ed,

thank you very much for your reply and sorry for messing up the language in my posting - it was pretty early this morning. Wink

As I couldn't reproduce the runtime errors in my second try, it seems they were caused by wrong API usage. Here's the code that finally works for me. Defining the checks as OCL constraints embedded in Java source code like that, it is possible to define quick fixes the default Xtext way. Using CompleteOCL would still be nicer, of course, but I have no idea how this could be done conveniently.

Regards,
Daniel

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.ocl.OCL;
import org.eclipse.ocl.ParserException;
import org.eclipse.ocl.Query;
import org.eclipse.ocl.ecore.Constraint;
import org.eclipse.ocl.ecore.EcoreEnvironmentFactory;
import org.eclipse.ocl.expressions.OCLExpression;
import org.eclipse.ocl.helper.OCLHelper;
import org.eclipse.xtext.validation.Check;

public class SwalJavaValidator extends AbstractSwalJavaValidator {
    // OCL instance for Ecore
    OCL<?, EClassifier, ?, ?, ?, ?, ?, ?, ?, Constraint, EClass, EObject> ocl = OCL.newInstance(EcoreEnvironmentFactory.INSTANCE);
    // OCL helper object
    OCLHelper<EClassifier, ?, ?, Constraint> helper = ocl.createOCLHelper();
    
  @Check
  public void checkNameNotEmpty(WebApplicationModel wm) {
    try {
        helper.setContext(WebModelPackage.Literals.WEBAPPLICATION);
        OCLExpression<EClassifier> nameNotEmptyOCL = helper.createQuery("name <> ''");
        Query<EClassifier, ?, ?> query = ocl.createQuery(nameNotEmptyOCL);
        Boolean valid = (Boolean) query.evaluate(wm);
        ...	
    } catch (ParserException e) {
        ...
    }
  }
}
Re: OCL in Xtext editor: Quick fixes for constraints [message #989889 is a reply to message #989506] Sun, 09 December 2012 14:32 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

On reflection, a quick fix is just a contextually parameterized refactoring.

Once transformation-defined refactorings are supported by Xtext, perhaps
by EMF Refactor, we can look at how to exploit them.

Regards

Ed Willink


On 06/12/2012 14:34, Daniel Strueber wrote:
> Hi Ed,
>
> thank you very much for your reply and sorry for messing up the
> language in my posting - it was pretty early this morning. ;)
>
> As I couldn't reproduce the runtime errors in my second try, it seems
> they were caused by wrong API usage. Here's the code that finally
> works for me. Defining the checks as OCL constraints embedded in Java
> source code like that, it is possible to define quick fixes the
> default Xtext way. Using CompleteOCL would still be nicer, of course,
> but I have no idea how this could be done conveniently.
>
> Regards,
> Daniel
>
> import org.eclipse.emf.ecore.EClass;
> import org.eclipse.emf.ecore.EClassifier;
> import org.eclipse.emf.ecore.EObject;
> import org.eclipse.ocl.OCL;
> import org.eclipse.ocl.ParserException;
> import org.eclipse.ocl.Query;
> import org.eclipse.ocl.ecore.Constraint;
> import org.eclipse.ocl.ecore.EcoreEnvironmentFactory;
> import org.eclipse.ocl.expressions.OCLExpression;
> import org.eclipse.ocl.helper.OCLHelper;
> import org.eclipse.xtext.validation.Check;
>
> public class SwalJavaValidator extends AbstractSwalJavaValidator {
> // OCL instance for Ecore
> OCL<?, EClassifier, ?, ?, ?, ?, ?, ?, ?, Constraint, EClass,
> EObject> ocl = OCL.newInstance(EcoreEnvironmentFactory.INSTANCE);
> // OCL helper object
> OCLHelper<EClassifier, ?, ?, Constraint> helper =
> ocl.createOCLHelper();
> @Check
> public void checkNameNotEmpty(WebApplicationModel wm) {
> try {
> helper.setContext(WebModelPackage.Literals.WEBAPPLICATION);
> OCLExpression<EClassifier> nameNotEmptyOCL =
> helper.createQuery("name <> ''");
> Query<EClassifier, ?, ?> query = ocl.createQuery(nameNotEmptyOCL);
> Boolean valid = (Boolean) query.evaluate(wm);
> ...
> } catch (ParserException e) {
> ...
> }
> }
> }
>
Previous Topic:extending OCL MDT with "just" a temporal operator always
Next Topic:serialization of CompleteOCL document to XMI format
Goto Forum:
  


Current Time: Thu Mar 28 19:08:52 GMT 2024

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

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

Back to the top