Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » default value for Boolean attribute and serialization
default value for Boolean attribute and serialization [message #1763633] Thu, 18 May 2017 08:31 Go to next message
Fa Za is currently offline Fa ZaFriend
Messages: 245
Registered: March 2010
Senior Member
Hi all,

I have this metamodel
package myDsl : myDsl = 'http://www.xtext.org/example/mydsl/MyDsl'
{
	class Model
	{
		property greetings : Greeting[*|1] { ordered composes };
	}
	class Greeting
	{
		attribute name : String[?];
		attribute condition : Boolean[1] = 'true';
	}
}

It contains a boolean attribute "condition" (lowerbound 1, upperbound 1 and default value "true")
I create an Xtext project from this existing metamodel
The Xtext grammar is
// automatically generated by Xtext
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals

import "http://www.xtext.org/example/mydsl/MyDsl"  
import "http://www.eclipse.org/emf/2002/Ecore" as ecore

Model returns Model:
	{Model}
	'Model'
	'{'
		('greetings' '{' greetings+=Greeting ( "," greetings+=Greeting)* '}' )?
	'}';


Greeting returns Greeting:
	'Greeting'
	name=ID
	(condition?='valid')?
		;


I create the following model
Model {
	greetings {
		Greeting test valid
	}
}

I save and I open the model with the MyDsl editor model (the emf editor for mydsl).
I change the condition attribute value to "false".
When I save the model, i have this error
java.lang.RuntimeException: Could not serialize EObject via backtracking.
Constraint: Greeting_Greeting returns Greeting: (name=ID condition?='valid'?);
Values: name(1), condition(1)
Semantic Object: Model.greetings[0]->Greeting'ss'
URI: platform:/resource/mydsl/My.mydsl
Context: Greeting returns Greeting
	at org.eclipse.xtext.serializer.diagnostic.ISerializationDiagnostic$ExceptionThrowingAcceptor.accept(ISerializationDiagnostic.java:131)
	at org.eclipse.xtext.serializer.sequencer.BacktrackingSemanticSequencer.createSequence(BacktrackingSemanticSequencer.java:480)
	at org.xtext.example.mydsl.serializer.MyDslSemanticSequencer.sequence_Greeting(MyDslSemanticSequencer.java:53)
	at org.xtext.example.mydsl.serializer.MyDslSemanticSequencer.sequence(MyDslSemanticSequencer.java:35)
	at org.eclipse.xtext.serializer.sequencer.AbstractSemanticSequencer.createSequence(AbstractSemanticSequencer.java:67)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.acceptEObjectRuleCall(SequenceFeeder.java:325)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.acceptRuleCall(SequenceFeeder.java:352)
	at org.eclipse.xtext.serializer.acceptor.SequenceFeeder.accept(SequenceFeeder.java:263)
	at org.eclipse.xtext.serializer.sequencer.BacktrackingSemanticSequencer.accept(BacktrackingSemanticSequencer.java:412)
	at org.eclipse.xtext.serializer.sequencer.BacktrackingSemanticSequencer.createSequence(BacktrackingSemanticSequencer.java:478)
	at org.xtext.example.mydsl.serializer.MyDslSemanticSequencer.sequence_Model(MyDslSemanticSequencer.java:65)
	at org.xtext.example.mydsl.serializer.MyDslSemanticSequencer.sequence(MyDslSemanticSequencer.java:38)
	at org.eclipse.xtext.serializer.sequencer.AbstractSemanticSequencer.createSequence(AbstractSemanticSequencer.java:67)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:118)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:142)
	at org.eclipse.xtext.serializer.impl.Serializer.serialize(Serializer.java:206)
	at org.eclipse.xtext.resource.XtextResource.doSave(XtextResource.java:369)
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(ResourceImpl.java:1430)
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.saveOnlyIfChangedWithMemoryBuffer(ResourceImpl.java:1144)
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.save(ResourceImpl.java:985)
	at myDsl.presentation.MyDslEditor$18.execute(MyDslEditor.java:1498)
	at org.eclipse.ui.actions.WorkspaceModifyOperation.lambda$0(WorkspaceModifyOperation.java:107)
	at org.eclipse.core.internal.resources.Workspace.run(Workspace.java:2240)
	at org.eclipse.core.internal.resources.Workspace.run(Workspace.java:2267)
	at org.eclipse.ui.actions.WorkspaceModifyOperation.run(WorkspaceModifyOperation.java:128)
	at org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:119)


It seems that it is caused by the default value.

Any suggestion how resolve it?

best regards,

[Updated on: Thu, 18 May 2017 08:33]

Report message to a moderator

Re: default value for Boolean attribute and serialization [message #1763635 is a reply to message #1763633] Thu, 18 May 2017 09:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14062
Registered: July 2009
Senior Member
so you have an opposite default value that differs from the default value in the concrete syntax right?

you could play arround with the transient value service.

public class MyDslTransientValueService extends DefaultTransientValueService {

@Override
public boolean isTransient(EObject owner, EStructuralFeature feature, int index) {
if (feature == TestPackage.Literals.GREETING__FLAG) {
Boolean value = (Boolean)owner.eGet(feature);
if (value == null) {
return false;
} else if (value == Boolean.FALSE) {
return true;
}
}
return super.isTransient(owner, feature, index);
}

}


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: default value for Boolean attribute and serialization [message #1763641 is a reply to message #1763635] Thu, 18 May 2017 09:35 Go to previous messageGo to next message
Fa Za is currently offline Fa ZaFriend
Messages: 245
Registered: March 2010
Senior Member
Thanks Christian for the explanation. It works perfectly.

I share the complete solution

I created this class

package org.xtext.example.mydsl.services;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.xtext.parsetree.reconstr.impl.DefaultTransientValueService;

import myDsl.MyDslPackage;

public class MyDslTransientValueService extends DefaultTransientValueService {

	@Override
	public boolean isTransient(EObject owner, EStructuralFeature feature, int index) {
	if (feature == MyDslPackage.Literals.GREETING__CONDITION) {
	Boolean value = (Boolean)owner.eGet(feature);
	if (value == null) {
	return false;
	} else if (value == Boolean.FALSE) {
	return true;
	}
	}
	return super.isTransient(owner, feature, index);
	}
	
}


I updated my MyDslRuntimeModule class by adding this method

		override Class<? extends ITransientValueService> bindITransientValueService() {
			MyDslTransientValueService
	}


Re: default value for Boolean attribute and serialization [message #1763649 is a reply to message #1763641] Thu, 18 May 2017 11:04 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7265
Registered: July 2009
Senior Member
Hi

It seems wrong to have to play around with such internals to make a model work.

Presumably you specified true as the default in order to give a statistical saving on the XMI size since true is much more common than false. Otherwise there is no point.

But if that is a saving for your XMI surely it is also true in your DSL, so why are DSL and XMI inconsistent?

I suggest that you change the XMI default to 'false' and the artificial problem goes away and a fancy hack is unnecessary.

Regards

Ed Willink
Re: default value for Boolean attribute and serialization [message #1763650 is a reply to message #1763649] Thu, 18 May 2017 11:15 Go to previous message
Fa Za is currently offline Fa ZaFriend
Messages: 245
Registered: March 2010
Senior Member
Ed Willink wrote on Thu, 18 May 2017 13:04
Hi
I suggest that you change the XMI default to 'false' and the artificial problem goes away and a fancy hack is unnecessary.


Where can I change it? in the mydsl.genmodel?
Previous Topic:Building for Eclipse and LSP
Next Topic:Hide parts of the textual representation in the editor
Goto Forum:
  


Current Time: Mon Sep 20 00:04:41 GMT 2021

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

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

Back to the top