Home » Modeling » TMF (Xtext) » How to get an inverse boolean assignment operator?
How to get an inverse boolean assignment operator? [message #1736535] |
Thu, 30 June 2016 09:10  |
Dennis Hendriks Messages: 73 Registered: January 2010 Location: The Netherlands |
Member |
|
|
Consider a class 'A' with an attribute 'b' of type 'EBoolean'. In the textual syntax, there is a 'notb' keyword, that is optional. If it is specified, attribute 'b' should be 'false'. If it is not specified, it should be 'true'. How can this be specified in Xtext?
I tried using the 'boolean assignment operator', but in this case it needs to be inverted from the default behavior that if the keyword is present, the attribute becomes 'true'.
That is:
A: ... some other attributes and stuff... b?='notb'
Doesn't work, as this way attrbute 'b' gets the wrong boolean value. I couldn't find a way to invert the boolean value.
I also tried:
A: ... some other attributes and stuff... (b?=|'notb')
Here I want to recognize no input to assign 'true' using the boolean assignment operator. Alternatively, 'notb' is parsed, and the 'b' feature keeps its value of 'false'. Xtext doesn't allow this.
I tried the same with a terminal that can be either empty or the 'notb' keyword, but that was not allowed either.
Note that I can't change the syntax, as it is in production already (C code, yacc generated parser). I use an imported metamodel. I can change it to rename the 'b' attribute to 'notb'. That works for now. But I don't like the negation being part of the name.
Is there any solution to this problem?
|
|
|
Re: How to get an inverse boolean assignment operator? [message #1736549 is a reply to message #1736535] |
Thu, 30 June 2016 09:59   |
|
you can try something like
package org.xtext.example.mydsl8
import org.eclipse.xtext.parser.DefaultEcoreElementFactory
import org.eclipse.emf.ecore.EClassifier
import org.xtext.example.mydsl8.myDsl.Greeting
import org.eclipse.emf.ecore.EObject
import org.eclipse.xtext.nodemodel.INode
import org.eclipse.xtext.conversion.ValueConverterException
import org.xtext.example.mydsl8.myDsl.MyDslPackage
class MyDslEcoreElementFactory extends DefaultEcoreElementFactory {
override create(EClassifier classifier) {
val result = super.create(classifier)
if (result instanceof Greeting) {
result.flag = true
}
result
}
override set(EObject object, String feature, Object value, String ruleName, INode node) throws ValueConverterException {
if (object instanceof Greeting && feature == MyDslPackage.Literals.GREETING__FLAG.name) {
super.set(object, feature, false, ruleName, node)
} else {
super.set(object, feature, value, ruleName, node)
}
}
}
class MyDslRuntimeModule extends AbstractMyDslRuntimeModule {
override bindIAstFactory() {
MyDslEcoreElementFactory
}
}
or switch to a manually maintained metamodel with default value = true and a grammar like (flag="flag")? with a valueConverter turning "flag" into false
Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
Re: How to get an inverse boolean assignment operator? [message #1749013 is a reply to message #1736549] |
Thu, 01 December 2016 07:17   |
Niels Brouwers Messages: 80 Registered: July 2009 |
Member |
|
|
Christian Dietrich wrote on Thu, 30 June 2016 11:59you can try something like
package org.xtext.example.mydsl8
import org.eclipse.xtext.parser.DefaultEcoreElementFactory
import org.eclipse.emf.ecore.EClassifier
import org.xtext.example.mydsl8.myDsl.Greeting
import org.eclipse.emf.ecore.EObject
import org.eclipse.xtext.nodemodel.INode
import org.eclipse.xtext.conversion.ValueConverterException
import org.xtext.example.mydsl8.myDsl.MyDslPackage
class MyDslEcoreElementFactory extends DefaultEcoreElementFactory {
override create(EClassifier classifier) {
val result = super.create(classifier)
if (result instanceof Greeting) {
result.flag = true
}
result
}
override set(EObject object, String feature, Object value, String ruleName, INode node) throws ValueConverterException {
if (object instanceof Greeting && feature == MyDslPackage.Literals.GREETING__FLAG.name) {
super.set(object, feature, false, ruleName, node)
} else {
super.set(object, feature, value, ruleName, node)
}
}
}
class MyDslRuntimeModule extends AbstractMyDslRuntimeModule {
override bindIAstFactory() {
MyDslEcoreElementFactory
}
}
or switch to a manually maintained metamodel with default value = true and a grammar like (flag="flag")? with a valueConverter turning "flag" into false
Hi Dennis, we have pursued the second approach proposed by Christian. We could share our experiences if you want.
Kind regards,
Niels Brouwers (Altran)
Kind regards,
Niels Brouwers.
|
|
|
Re: How to get an inverse boolean assignment operator? [message #1749023 is a reply to message #1749013] |
Thu, 01 December 2016 08:57  |
Dennis Hendriks Messages: 73 Registered: January 2010 Location: The Netherlands |
Member |
|
|
Niels Brouwers wrote on Thu, 01 December 2016 08:17
...
Hi Dennis, we have pursued the second approach proposed by Christian. We could share our experiences if you want.
Kind regards,
Niels Brouwers (Altran)
We ended up simply changing the metamodel, inverting the meaning of the feature. Not ideal, but it works and is simple.
But if you have some code example of how to do it properly, by all means, share it. Also for others who might stumble upon this forum post in the future.
|
|
|
Goto Forum:
Current Time: Thu Jun 08 18:59:56 GMT 2023
Powered by FUDForum. Page generated in 0.01942 seconds
|