Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [Xtext] Default values for optional assignments?
[Xtext] Default values for optional assignments? [message #468198] Tue, 04 August 2009 13:44 Go to next message
Hauke Fuhrmann is currently offline Hauke FuhrmannFriend
Messages: 333
Registered: July 2009
Senior Member
Dear xtext folks,

is there a way to specify default values for attribute assignments that
are optional?

Like in:

Node:
(attr=INT)? name=ID

If one omits the INT token, then attr will be initialized with 0 as I
understand. Is there a way to set this default value to some other value?

Cheers,
HAuke
Re: [Xtext] Default values for optional assignments? [message #468219 is a reply to message #468198] Tue, 04 August 2009 14:56 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Hauke,

please have a look at the post processing hook for the meta model
generation. The documentation can be found at http://www.xtext.org. The
FAQ provides an entry point for this one, too:
http://wiki.eclipse.org/Xtext/FAQ#How_can_I_control_the_Xtex t_meta_model_inference_.3F

Hope that helps,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 04.08.2009 15:44 Uhr, schrieb Hauke Fuhrmann:
> Dear xtext folks,
>
> is there a way to specify default values for attribute assignments that
> are optional?
>
> Like in:
>
> Node:
> (attr=INT)? name=ID
>
> If one omits the INT token, then attr will be initialized with 0 as I
> understand. Is there a way to set this default value to some other value?
>
> Cheers,
> HAuke
Re: [Xtext] Default values for optional assignments? [message #985906 is a reply to message #468219] Fri, 16 November 2012 16:00 Go to previous messageGo to next message
Christopher Brind is currently offline Christopher BrindFriend
Messages: 9
Registered: October 2011
Junior Member
Hi,

I am trying to work this out as well. I see this post is quite old now so I wonder if that FAQ is still accurate? I can't find the things it is referring to in my IDE.

Any help assigning default values to optional grammar would be appreciated.

Cheers,
Chris
Re: [Xtext] Default values for optional assignments? [message #985922 is a reply to message #985906] Fri, 16 November 2012 16:56 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
hi,

IXtext2EcorePostProcessor is still there. it ships with one impl (XtendXtext2EcorePostProcessor)
you may need to install a complete xpand sdk the get full useage of the ide features described in the faq.
or you implement it yourself e.g. as i described it here http://christiandietrich.wordpress.com/2011/07/22/customizing-xtext-metamodel-inference-using-xtend2/

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [Xtext] Default values for optional assignments? [message #985923 is a reply to message #985922] Fri, 16 November 2012 17:03 Go to previous messageGo to next message
Christopher Brind is currently offline Christopher BrindFriend
Messages: 9
Registered: October 2011
Junior Member
Thanks Christian, I had just started reading your blog post.
Re: [Xtext] Default values for optional assignments? [message #986747 is a reply to message #985923] Wed, 21 November 2012 14:19 Go to previous messageGo to next message
Christopher Brind is currently offline Christopher BrindFriend
Messages: 9
Registered: October 2011
Junior Member
Hi again,

Your blog post helped in that I got to the point where I realised that for that particular problem I could just set the default value literal of the attribute.

The problem I have now is that I need to override the getter of an attribute so that if the attribute is null i return a value derived from one of the other attributes.

Obviously I can't just add an operation to override the getter as that invalidates the genmodel, so I'm a little stuck... any suggestions?

Thanks in advance.
Re: [Xtext] Default values for optional assignments? [message #986803 is a reply to message #986747] Wed, 21 November 2012 17:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Hmmm i have actually no idea for that. Maybe you have to switch to a
manually maintained metamodel

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [Xtext] Default values for optional assignments? [message #986806 is a reply to message #468198] Wed, 21 November 2012 17:22 Go to previous messageGo to next message
Christopher Brind is currently offline Christopher BrindFriend
Messages: 9
Registered: October 2011
Junior Member
Drat, that's a shame. Thanks for your reply anyway.
Re: [Xtext] Default values for optional assignments? [message #1863231 is a reply to message #986806] Thu, 18 January 2024 17:33 Go to previous messageGo to next message
Steve Hickman is currently offline Steve HickmanFriend
Messages: 56
Registered: August 2017
Member
Christian Dietrich's Wordpress blog has been replaced by this: https://www.dietrich-it.de. All articles from the Wordpress blog appear to be copied over with similar URLs. The post referenced above can be found at https://dietrich-it.de/xtext/2011/07/22/customizing-xtext-metamodel-inference-using-xtend2/. Having said that, per the FAQ, post processing has been removed.

[Updated on: Thu, 18 January 2024 17:43]

Report message to a moderator

Re: [Xtext] Default values for optional assignments? [message #1863563 is a reply to message #1863231] Fri, 09 February 2024 17:12 Go to previous message
Steve Hickman is currently offline Steve HickmanFriend
Messages: 56
Registered: August 2017
Member
This can now be handled using a ValueConverter. In the example below, note that the `toValue()` methods check to see if the string is empty - this is how you handle the default value. This class must also be registered in the <DSL>RuntimeModule - see second code snippet

/**
 * 
 */
package com.epistimis.uddl;

import org.eclipse.xtext.common.services.DefaultTerminalConverters;
import org.eclipse.xtext.conversion.IValueConverter;
import org.eclipse.xtext.conversion.ValueConverter;
import org.eclipse.xtext.conversion.ValueConverterException;
import org.eclipse.xtext.nodemodel.INode;

/**
 * 
 */
public class UddlValueConverters extends DefaultTerminalConverters {

    IValueConverter<Boolean> realRangeLowerValueConverter = new IValueConverter<Boolean>() {

        @Override
        public Boolean toValue(String string, INode node) throws ValueConverterException {
        	if ((string.startsWith("[")) || (string.isEmpty())) {
        		return Boolean.TRUE;
        	}
        	if (string.startsWith("(")) {
        		return Boolean.FALSE;
        	}
           // default
        	return Boolean.TRUE;
        }

        @Override
        public String toString(Boolean value) throws ValueConverterException {
            return value.booleanValue()? "[" : "(" ;
        }
    };

    IValueConverter<Boolean> realRangeUpperValueConverter = new IValueConverter<Boolean>() {

        @Override
        public Boolean toValue(String string, INode node) throws ValueConverterException {
        	if ((string.startsWith("]")) || (string.isEmpty())) {
        		return Boolean.TRUE;
        	}
        	if (string.startsWith(")")) {
        		return Boolean.FALSE;
        	}
           // default
        	return Boolean.TRUE;
        }

        @Override
        public String toString(Boolean value) throws ValueConverterException {

            return value.booleanValue()? "]" : ")" ;
        }
    };
    

    @ValueConverter(rule = "RRLOWER")
    public IValueConverter<Boolean> RRLOWER() {
        return realRangeLowerValueConverter;
    }

    @ValueConverter(rule = "RRUPPER")
    public IValueConverter<Boolean> RRUPPER() {
        return realRangeUpperValueConverter;
    }

}



Register it in the <DSL>RuntimeModule like this:
    public Class<? extends IValueConverterService> bindIValueConverterService() {
        return UddlValueConverters.class ;
    }
Previous Topic:junit: NamesAreUniqueValidator does not work as eclipse junit but does work in Maven build?
Next Topic:How to change the QualifiedNameProvider from another language
Goto Forum:
  


Current Time: Mon Sep 23 10:51:13 GMT 2024

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

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

Back to the top