Home » Modeling » EMF » [Xcore] Invariant code referencing XXXValidator constants
[Xcore] Invariant code referencing XXXValidator constants [message #1736413] |
Wed, 29 June 2016 06:32  |
Eclipse User |
|
|
|
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 07:36   |
Eclipse User |
|
|
|
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 #1736430 is a reply to message #1736424] |
Wed, 29 June 2016 07:56   |
Eclipse User |
|
|
|
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
>
|
|
| |
Goto Forum:
Current Time: Sun Jul 27 10:05:06 EDT 2025
Powered by FUDForum. Page generated in 0.04416 seconds
|