Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » EMF + Xtext 2.4.3: Error in serialization of EObject
EMF + Xtext 2.4.3: Error in serialization of EObject [message #1219237] Mon, 02 December 2013 20:17 Go to next message
J A is currently offline J A
Messages: 31
Registered: July 2013
Member
Dear all,

I have got one working DSL Text Editor, thanks to XText 2.4.3.

Now, when I want to add GUI support to this text editor, so that the user could use SWT/JFace components to edit the model, I have the following error while serializing an EObject back to the text editor resource. This EObject is the root of the modified tree after cloning a node EObject and inserting that clone back to the tree.

// Code that copies one EObject

AnnotatedElement original = new AnnotatedElement();
AnnotatedElement clone = EcoreUtil2.cloneWithProxies(original );
org.eclipse.emf.common.command.Command theCommand = AddCommand.createcreate(org.eclipse.emf.edit.domain.EditingDomain domain, Object original.getOwner(), Object feature, Object clone);
org.eclipse.emf.edit.domain.CommandStack.execute(theCommand);


// Code that serializes the EObject that is the root of the AST Tree

org.eclipse.emf.edit.domain.EditingDomain editingDomain = new EditingDomain();
final XtextDocument doc = (XtextDocument) xtextEditor.getDocument();
doc.modify(new IUnitOfWork.Void<XtextResource>()
{
	@Override
	public void process(XtextResource resource) throws Exception
	{
		Injector injector = Guice.createInjector(new ExampleRuntimeModule());
		org.eclipse.xtext.serializer.impl.Serializer serializer = injector.getInstance(org.eclipse.xtext.serializer.impl.Serializer.class);
	        EObject rootEObj = editingDomain.getResourceSet().getResources().get(0).getContents().get(0);
		String serializedDoc = serializer.serialize(eObj);
		doc.set(serializedDoc);
        }
}


The code above compiles (and what you see is the simplified version) and now for this model:

import BaseMath.*;
import BaseModel.*;


model Example uses BaseModel
{
	version : "00.00.000.000";
	root
	{
		variable TREEROOT
		{
			variable VARIABLE_1
			{
				datatype : string;
				formula : If(1, 2, 3);
				title : "Var title 1";
			}
		}
	}
}


The error occurs when I copy VARIABLE_1 in that model

--------------------------------

No EObjectDescription could be found in Scope IdentifierExpression.declaration for FinancialModel.elements[0]->AnnotatedElement.element->LibraryDeclaration'BaseMath'.elements[117]->AnnotatedElement.element->FunctionDeclaration'If'
Semantic Object: FinancialModel.elements[0]->AnnotatedElement.element->Model'Example'.root->Root'Root'.elements[0]->AnnotatedElement.element->Variable'TREEROOT'.elements[1]->AnnotatedElement.element->Variable'VARIABLE_1_COPY'.statements[1]->AssignmentStatement.value->ValueList.valueItems[0]->ValueItem.value->IdentifierExpression'If'
--------------------------------

[Updated on: Tue, 03 December 2013 10:36]

Report message to a moderator

Re: The grammar, domain specific file [message #1219239 is a reply to message #1219237] Mon, 02 December 2013 20:22 Go to previous messageGo to next message
J A is currently offline J A
Messages: 31
Registered: July 2013
Member
grammar com.mydomain.ide.example.Example with com.mydomain.ide.expression.Expression

generate example "http://www.mydomain.com/ide/example/Example"
import "http://www.mydomain.com/ide/expression/Expression" as expression

ExampleModel:
	(	expression=Expression |
		(
			imports+=ImportStatement* 
			elements+=AnnotatedElement*
		)
	)		
;

ImportStatement:
	'import' importedNamespace=FqnWithWildCard ';'
;
 
AnnotatedElement:
	annotations+=Annotation*
	element=AbstractElement
;

AbstractElement:
	AbstractDeclaration
	| AbstractContainer
	| Model
	| EnumItem
	| Parameter
; 

AbstractContainer:
	AbstractVariable
	| Module
;

AbstractVariable:
	Root
	| Variable
	| Tuple
;

AbstractDeclaration:
	BlockDeclaration
	| ParameterTypeDeclaration
	| PropertyTypeDeclaration
	| AnnotationDeclaration
	| ConstantDeclaration
	| PropertyDeclaration
	| FunctionDeclaration
	| LibraryDeclaration
	| ContextDeclaration
;

AnnotationDeclaration:	
	'annotation' name=Fqn ';'
;

BlockDeclaration:
	'block' name=Fqn ';'
;

PropertyTypeDeclaration:
	'propertytype' name=Fqn ';'
;

ParameterTypeDeclaration:
	'parametertype' name=Fqn ';'
;

PropertyDeclaration: 	
 	'property' name=Fqn ':' propertyType=[PropertyTypeDeclaration|Fqn] enumList=EnumList? ';'
;

EnumList:
	'(' items+=EnumItem (',' items+=EnumItem)* ')'
;

EnumItem:
	name=Fqn
;

ConstantDeclaration:
	'constant' name=Fqn ':' returnType=[ParameterTypeDeclaration] ';' (external?='external' ';')?
;

FunctionDeclaration:	
	'function' name=Fqn '(' (parameters+=Parameter (';' parameters+=Parameter)*)? ')' 
	':' returnType=[ParameterTypeDeclaration] ';' ((external?='external' ';') | body=FunctionBody )
;

Parameter:
	name=ID  ':' parameterType=[ParameterTypeDeclaration]
;

FunctionBody:
	'{' expression=Expression '}'
;

LibraryDeclaration:
	'library' name=Fqn
	'{' 
		elements+=AnnotatedElement*
	'}'
;

ContextDeclaration:
	'context' name=Fqn
	'{'
		elements+=AnnotatedElement*
	'}'
;

Model:
	'model' name=Fqn 
		'uses' contexts+=[ContextDeclaration|Fqn] (',' contexts+=[ContextDeclaration|Fqn])*  
	'{'
		statements+=AssignmentStatement*
		root=Root
	'}'
;

Module:
	'module' name=Fqn
		'uses' contexts+=[ContextDeclaration|Fqn] (',' contexts+=[ContextDeclaration|Fqn])*
	'{'
		(elements+=AnnotatedElement 
		| propertyGroups+=PropertyGroup 
		| elements+=IncludeStatement)*
	'}'
;

//If all sub classes implement the same property then Xtext 
//expects the property to be in the super class of the sub classes. 
//That's the reason why root has an optional element 'name'; to 
//enable us to move the name property to AbstractElement. 
Root: {Root}
	'root' name=Fqn?
	'{'
		(elements+=AnnotatedElement 
		| propertyGroups+=PropertyGroup 
		| elements+=IncludeStatement)*
	'}'
;

enum SummationOperator:
	ADD='+' | SUBTRACT='-' | RESULT='='
;

Tuple:
	'tuple' summationOperator=SummationOperator? name=Fqn 
	'{'
		statements+=AssignmentStatement*
		(elements+=AnnotatedElement
		| propertyGroups+=PropertyGroup
		| elements+=IncludeStatement)*
	'}'
;

Variable:
	'variable' summationOperator=SummationOperator? name=Fqn
		('refers' 'to' refersTo=[AbstractElement|Fqn])?
	'{'
		statements+=AssignmentStatement*
		propertyGroups+=PropertyGroup*
		(elements+=AnnotatedElement		
		| elements+=IncludeStatement)*
	'}'
;

Annotation:
	'@' declaration=[AnnotationDeclaration|Fqn] 
		'(' (arguments+=ValueItem (',' arguments+=ValueItem)*)? ')' ';'
;

PropertyGroup:
	'group' declaration=[PropertyDeclaration|Fqn] 
	'{'
		statements+=AssignmentStatement*
	'}'
;

IncludeStatement:
	'include' module=[Module|Fqn] ';'
;

AssignmentStatement:
	element=[AbstractElement|Fqn] ':' (value=ValueList  | linked?='grouped') ';'
;

ValueList:
	valueItems+=ValueItem (',' valueItems+=ValueItem)*
;

ValueItem:
	value=Expression | value=Validator	
;

enum ValidatorType:	
	MINEXCLUSIVE='>' | MININCLUSIVE='>=' | MAXEXCLUSIVE='<' | MAXINCLUSIVE='<='	 
;

Validator:
	(type1=ValidatorType number1=UnaryExpression)
	('and' type2=ValidatorType number2=UnaryExpression)?
;

FqnWithWildCard:
	Fqn ('.*')?
;

Fqn:
	ID ('.' ID)*
;

/* Overrides IdentifierExpression from Expression.xtext */ 
IdentifierExpression:
	hasAmpersand?='&'? declaration=[AbstractElement|Fqn] extension=IdentifierExtension?
;

[Updated on: Tue, 03 December 2013 07:28]

Report message to a moderator

Re: The grammar, domain generic file [message #1219240 is a reply to message #1219239] Mon, 02 December 2013 20:24 Go to previous message
J A is currently offline J A
Messages: 31
Registered: July 2013
Member
grammar com.mydomain.ide.expression.Expression with org.eclipse.xtext.common.Terminals
  hidden(WS, ML_COMMENT, SL_COMMENT)

generate expression "http://www.mydomain.com/ide/expression/Expression"

import "http://www.eclipse.org/emf/2002/Ecore" as ecore

Expression:		
	ImpliesExpression
;

/*
 * Operator precedence from lowest to highest:
 *  
 *  implies
 *  > | < | >= | <=
 *  = | <>
 *	+ | - | &
 *  or
 *  * | / | mod
 *  and
 *  ^
 *  not | - | +  << unary operators
 *  literals & function calls
 *  
 */
enum ImpliesOperator returns BinaryOperator: 
	IMPLIES='implies'
;

ImpliesExpression returns Expression:
	RelationalExpression (
		{BinaryExpression.left = current}
		operator=ImpliesOperator
		right=RelationalExpression
	)*
;

enum RelationalOperator returns BinaryOperator: 
	GREATER='>' | SMALLER='<' | GREATEREQUAL='>=' | SMALLEREQUAL='<='
;

RelationalExpression returns Expression:
	EqualityExpression (
		{BinaryExpression.left=current}		
		operator=RelationalOperator 
		right=EqualityExpression
	)*
;
	

enum EqualityOperator returns BinaryOperator: 
	EQUAL='=' | NOTEQUAL='<>'
;

EqualityExpression returns Expression:
	AdditiveExpression (
		{BinaryExpression.left=current} 
		operator=EqualityOperator
		right=AdditiveExpression
	)*
;
	
enum AdditiveOperator returns BinaryOperator: 
	PLUS='+' | MINUS='-' | CONCAT='&'
;

AdditiveExpression returns Expression:
	OrExpression (
		{BinaryExpression.left=current}
		operator=AdditiveOperator
		right=OrExpression
	)*
;

enum OrOperator returns BinaryOperator: 
	OR='or'
;

OrExpression returns Expression:
	MultiplicativeExpression (
		{BinaryExpression.left=current}
		operator=OrOperator
		right=MultiplicativeExpression
	)*
;

enum MultiplicativeOperator returns BinaryOperator: 
	MULTIPLY='*' | DIVIDE='/' | MODULO='mod'
;

MultiplicativeExpression returns Expression:
	AndExpression (
		{BinaryExpression.left=current}
		operator=MultiplicativeOperator
		right=AndExpression
	)*
;

enum AndOperator returns BinaryOperator:
	AND='and'
;

AndExpression returns Expression:
	PowerExpression (
		{BinaryExpression.left=current}
		operator=AndOperator
		right=PowerExpression
	)*
;

enum PowerOperator returns BinaryOperator:
	POWER='^'
;

PowerExpression returns Expression:
	UnaryExpression (
		{BinaryExpression.left=current}		
		operator=PowerOperator
		right=UnaryExpression
	)*
;

enum UnaryOperator: 
	NOT='not' | NEGATIVE='-' | POSITIVE='+'
;

UnaryExpression returns Expression:
	({UnaryExpression}
		operator=UnaryOperator expression=PrimaryExpression
	)
	| PrimaryExpression
;

PrimaryExpression returns Expression:
	LiteralExpression
	| IdentifierExpression
	| ParenthesizedExpression
;

ParenthesizedExpression returns Expression:
	{ParenthesizedExpression}
	'(' innerExpression=Expression ')'
;

LiteralExpression returns Expression:
	LiteralNumber 
	| LiteralPercentage 
	| LiteralString
	| LiteralKeyword
;

LiteralPercentage:
	value=Double '%'
;

LiteralNumber:
	value=Double
;

LiteralString:
	hasAmpersand?='&'? value=STRING
;

enum KeywordValue:
	X='X'
	| Y='Y'
	| Z='Z'
	| ROOT='ROOT'
;

LiteralKeyword:
	(keyword=KeywordValue)
;

//An identifier expression can be a variable, function or a constant.
//It's not possible to define these in separate production rules,
//because the parser can't decide between rules that start with the 
//same terminal or production rule; in this case the terminal 'ID', 
//because the ampersand is optional.
IdentifierExpression:
	hasAmpersand?='&'? name=ID extension=IdentifierExtension?
;

IdentifierExtension:
	CellSpecifier | ArgumentList
;

CellSpecifier:
	'[' column=Expression (',' tsy=Expression)?	']'
;

ArgumentList:
	'(' arguments+=Argument (',' arguments+=Argument)* ')'
;

Argument:
	itemList=CaseItemList | expression=Expression	
;

CaseItemList:
	'[' items+=CaseItem (( separators+=CaseItemSeparator ) items+=CaseItem)* ']'
;

enum CaseItemSeparator: 
	PIPELINE='|' | COLON=':'
;

enum CaseItemOperator returns BinaryOperator: 
	GREATER='>' | SMALLER='<' | GREATEREQUAL='>=' | SMALLEREQUAL='<=' | EQUAL='=' | NOTEQUAL='<>'
;

CaseItem:
	operator=CaseItemOperator? expression=Expression
;

Double returns ecore::EDouble:
	INT+('.'INT)?
;

//The standard ID terminal rule is overridden here, because of the 
//power operator defined in the original ID terminal rule.
terminal ID	: ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
Previous Topic:newbie: how to generate multiple files from one model element
Next Topic:Is there a memory leak of xtext editors opened by debugger?
Goto Forum:
  


Current Time: Wed Oct 01 12:31:29 GMT 2014

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

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