Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [Xcore] Invariant code referencing XXXValidator constants
[Xcore] Invariant code referencing XXXValidator constants [message #1736413] Wed, 29 June 2016 10:32 Go to next message
Alexander Aumann is currently offline Alexander AumannFriend
Messages: 8
Registered: October 2015
Junior Member
For the following simplified model (MyModel) with an invariant operation:
class FancyNumber {
  String number;
  op boolean isValid(EDiagnosticChain diagnostics, Map<Object,Object> context) {
     if (!canParseAsNumber(number)) {
         EStructuralFeature feature = ....
         diagnostics.add(new BasicDiagnostic(Diagnostic.ERROR, 
              MyModelValidtor.DIAGNOSTIC_SOURCE,
              MyModelValidator.FANCY_NUMBER__VALIDATE, 
              "Not a number", #[this, feature])) 
         false
     } else {
        true
     }
  }
}

Xcore cannot generate the model from a clean state anymore. It complains that MyModelValidator cannot be found (which is true, it needs to be generated first) and thus does not generate anything.

If I remove the reference to the validator, let xcore generate, and re-add the reference it works fine as the validtor has already been generated now.

Is there a better way of working around the issue than extracting the code for generating the diagnostic object into a static java helper class?

Kind regards,
Alexander
Re: [Xcore] Invariant code referencing XXXValidator constants [message #1736424 is a reply to message #1736413] Wed, 29 June 2016 11:36 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 31961
Registered: July 2009
Senior Member
Alexander,

Yes, there appears to be a difficult problem. Recognizing that an
operation is an invariant involves looking at the signature. But JVM
inference involves two phases:

Phase 1 infers the shallow structure, i.e., it generates stubs for
all the classes that the generator will generate.
Phase 2 infers the deep structure, i.e., all the methods and fields
in those classes.

Linking/resolving of types happens between phase 1 and phase2. But
during phase 1 the inferrer needs to determine if a GenPackage has
constraints, i.e., whether a validator class will be generated, but it
can't know that until after phase 1 is finished.

I'm not sure how to fix that chicken-egg problem, but please open a
Bugzilla.

In the meantime, you can add an Ecore annotation to ensure that the
invariant operation is recognized as an invariant during phase 1. E.g.,
this example works:

package org.example.test

import org.eclipse.emf.ecore.EDiagnosticChain
import org.eclipse.emf.ecore.EMap
import org.example.test.util.TestValidator

class Example{
@Ecore(invariant="true")
op boolean isValid(EDiagnosticChain diagnostics,
EMap<Object,Object> context) {
val x = TestValidator.EXAMPLE__IS_VALID
true
}
}

Perhaps that's the only solution that will ever work, i.e., perhaps this
is a limitation.

On 29.06.2016 06:33, Alexander Aumann wrote:
> For the following simplified model (MyModel) with an invariant operation:
>
> class FancyNumber {
> String number;
> op boolean isValid(EDiagnosticChain diagnostics, Map<Object,Object>
> context) {
> if (!canParseAsNumber(number)) {
> EStructuralFeature feature = ....
> diagnostics.add(new BasicDiagnostic(Diagnostic.ERROR,
> MyModelValidtor.DIAGNOSTIC_SOURCE,
> MyModelValidator.FANCY_NUMBER__VALIDATE, "Not a number",
> #[this, feature])) false
> } else {
> true
> }
> }
> }
>
> Xcore cannot generate the model from a clean state anymore. It
> complains that MyModelValidator cannot be found (which is true, it
> needs to be generated first) and thus does not generate anything.
>
> If I remove the reference to the validator, let xcore generate, and
> re-add the reference it works fine as the validtor has already been
> generated now.
>
> Is there a better way of working around the issue than extracting the
> code for generating the diagnostic object into a static java helper
> class?
>
> Kind regards,
> Alexander


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: [Xcore] Invariant code referencing XXXValidator constants [message #1736430 is a reply to message #1736424] Wed, 29 June 2016 11:56 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7224
Registered: July 2009
Senior Member
Hi Ed

I hesitate to suggest that you have forgotten how genmodel works; helper
methods generate useful names of future literals without needing to
reflect. The only minor problem is that sometimes loading a stale
*Impl.java file fails as a consequence of serious errors. The
constraints can all be predicted from the "constraints" EAnnotation.

Regards

Ed Willink


On 29/06/2016 12:36, Ed Merks wrote:
> Alexander,
>
> Yes, there appears to be a difficult problem. Recognizing that an
> operation is an invariant involves looking at the signature. But JVM
> inference involves two phases:
>
> Phase 1 infers the shallow structure, i.e., it generates stubs for
> all the classes that the generator will generate.
> Phase 2 infers the deep structure, i.e., all the methods and fields
> in those classes.
>
> Linking/resolving of types happens between phase 1 and phase2. But
> during phase 1 the inferrer needs to determine if a GenPackage has
> constraints, i.e., whether a validator class will be generated, but it
> can't know that until after phase 1 is finished.
>
> I'm not sure how to fix that chicken-egg problem, but please open a
> Bugzilla.
>
> In the meantime, you can add an Ecore annotation to ensure that the
> invariant operation is recognized as an invariant during phase 1.
> E.g., this example works:
>
> package org.example.test
>
> import org.eclipse.emf.ecore.EDiagnosticChain
> import org.eclipse.emf.ecore.EMap
> import org.example.test.util.TestValidator
>
> class Example{
> @Ecore(invariant="true")
> op boolean isValid(EDiagnosticChain diagnostics,
> EMap<Object,Object> context) {
> val x = TestValidator.EXAMPLE__IS_VALID
> true
> }
> }
>
> Perhaps that's the only solution that will ever work, i.e., perhaps
> this is a limitation.
>
> On 29.06.2016 06:33, Alexander Aumann wrote:
>> For the following simplified model (MyModel) with an invariant
>> operation:
>>
>> class FancyNumber {
>> String number;
>> op boolean isValid(EDiagnosticChain diagnostics, Map<Object,Object>
>> context) {
>> if (!canParseAsNumber(number)) {
>> EStructuralFeature feature = ....
>> diagnostics.add(new BasicDiagnostic(Diagnostic.ERROR,
>> MyModelValidtor.DIAGNOSTIC_SOURCE,
>> MyModelValidator.FANCY_NUMBER__VALIDATE, "Not a number",
>> #[this, feature])) false
>> } else {
>> true
>> }
>> }
>> }
>>
>> Xcore cannot generate the model from a clean state anymore. It
>> complains that MyModelValidator cannot be found (which is true, it
>> needs to be generated first) and thus does not generate anything.
>>
>> If I remove the reference to the validator, let xcore generate, and
>> re-add the reference it works fine as the validtor has already been
>> generated now.
>>
>> Is there a better way of working around the issue than extracting the
>> code for generating the diagnostic object into a static java helper
>> class?
>>
>> Kind regards,
>> Alexander
>
Re: [Xcore] Invariant code referencing XXXValidator constants [message #1736431 is a reply to message #1736424] Wed, 29 June 2016 12:03 Go to previous message
Alexander Aumann is currently offline Alexander AumannFriend
Messages: 8
Registered: October 2015
Junior Member
Thanks Ed,

https://bugs.eclipse.org/bugs/show_bug.cgi?id=496998

I did not include your reply in the bug yet, I don't know exactly which part is relevant to you for future reference.

I will try the annotation and report back to confirm the solution.
Previous Topic:[Xcore] Proposed way of adding adapters to new instances
Next Topic:Invalid thread access while updating UI
Goto Forum:
  


Current Time: Tue Aug 03 21:53:17 GMT 2021

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

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

Back to the top