Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to use reference and enumerated list in xtext grammar and parser(How to use reference and enumerated list in xtext grammar and parser)
How to use reference and enumerated list in xtext grammar and parser [message #743042] Fri, 21 October 2011 04:13 Go to next message
AHOT Mising name is currently offline AHOT Mising nameFriend
Messages: 13
Registered: September 2011
Junior Member
I am trying to use reference to some already created entities in an xtext project,
someone please help.
Thank you
Re: How to use reference and enumerated list in xtext grammar and parser [message #743387 is a reply to message #743042] Fri, 21 October 2011 12:21 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
Without providing more information, we can only repeat sections from the User Guide here. If you post your grammar (and .mwe2) files, then we can provide more specialized help.

In general, this is done through scoping, which behavior is configured through the .mwe2 file and customized through the Java custom scope provider. The configuration of qualified naming also plays a role.


Re: How to use reference and enumerated list in xtext grammar and parser [message #743906 is a reply to message #743387] Fri, 21 October 2011 20:26 Go to previous messageGo to next message
AHOT Mising name is currently offline AHOT Mising nameFriend
Messages: 13
Registered: September 2011
Junior Member
Thank you Meinte,
below are my grammar and xtend parser code.
I have added comments to the right on lines where i am having problems.

I have been trying for a while to do type casting in xtend but not successful.

Model:
	Atomic|Network
	name = ID
	author = ID
	description = STRING
	'port_list_open{'
				ports += Port*
	'}port_list_close'
;

Atomic:
	'atomic_pen {'
		'state_list_open{'
				states += State+
		'}state_list_close'
		'transitions_open{'
				transitions += Transition*
		'}transitions_close'
		'
	'}atomic_close'
;

N:etwork:
	'coupled_model_open{'
		sub_Models += [Atomic]+  //NOTE: //NOTE: I WANT TO MAKE REFERENCE TO SOME ALREADY CREATED "ATOMIC" HERE 
		couplings += Coupling+
	'}coupled_model_close'
;

Port:                  
	'port_open {'
		name = ID
		description = STRING
		port_type = ID  //input or output, NOTE: I want to use enumerated list here {input, output}
		port_dataType = ID  
	'} port_close'
;
Transition:
	Internal_transition|Exteral_transition
	computation = STRING
	description = STRING
;

Exteral_transition:
	trigger = STRING	
;

Internal_transition:
	'int_trans_open{'
		current_state = ID
		next_state = ID
		(lambda = STRING
		output_port = [Port])?  //NOTE: I WANT TO MAKE REFERENCE TO SOME ALREADY CREATED PORTS HERE 
	'}int_trans_close'
;

State:
	'state_desc_open {'
		stateID = ID
		time = INT     //NOTE: I want to use real number here instead of integer, how do I specify real?
		(next_state = [State])? //NOTE: I WANT TO MAKE REFERENCE TO A STATE HERE
	'}state_desc_close'
;




def atomic_Parser(Atomic atom)'''
		//«atom.name».java
		//«atom.description»
		
		public class "«atom.name.toFirstUpper»" extends AtomicModel{
			/**
	 		  * I/O port names
	 		  */
			«FOR port: atom.ports»
				private String «port.name.toLowerCase»;
			«ENDFOR»
			
			/**
	 		  * State variables
	 		  */
			private String PHASE;
			private double SIGMA;
			
			
			/*
			 *Constructor
			 */
			public «atom.name.toFirstUpper»(){
				super("«atom.name.toFirstUpper»","«atom.description»");
				
				/**
		 		  * Registering the input and output ports
		 		  */
				«FOR port: atom.ports»
					«IF("«port.port_type»".equalsIgnoreCase("input"))»  				//THIS IF EXPRESSION IS NOT WORING AS NOTHING 
						addIPort(new «port.port_dataType.toFirstUpper»(),«port.name»);//WAS GENERATED FOR THIS SEGMENT
					«ENDIF»
					«IF("«port.port_type»".equalsIgnoreCase("output"))»
							addOPort(new "«port.port_dataType.toFirstUpper»"+(),«port.name»);
					«ENDIF»
				«ENDFOR»
								
				/*
				 *Internal transition function
				 */
				public void deltaInt(){
					«FOR state: atom.states»
						«IF state.next_state_on_deltaInt != null»
							if (PHASE.equals("«state.current_state»"))
								PHASE = "«state.next_state_on_deltaInt»";
						«ENDIF»											
					«ENDFOR»
				}
				
				
				/*
				 *Output function
				 */
				public void output() throws Exception {
					«FOR trans: atom.transitions»
						«IF trans instanceof Internal_transition»
						 «var newtran = trans as Internal_transition»  //I WANT TO CAST "trans" to a type of Internal_transition but there is problem on next line
						 if (PHASE.equals("«newtran.current_state»")){
						 //Please fill in the action
						};
						«ENDIF»
					«ENDFOR»
				}
		}
		
	'''
Re: How to use reference and enumerated list in xtext grammar and parser [message #743940 is a reply to message #743906] Fri, 21 October 2011 20:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

all of your problems should be mentioned in the docs

(0) am not quite sure about your model rule
you grammar in general may suffer by the low precedence of |
do you mean something like

Model:
(Atomic|Network)
name = ID
author = ID
description = STRING
'port_list_open{'
ports += Port*
'}port_list_close'
;

same for

Transition:
Internal_transition|Exteral_transition
computation = STRING
description = STRING
;

what do you mean by that
currently this is the same a

Transition:
Internal_transition|(Exteral_transition
computation = STRING
description = STRING
);

(1) sub_Models += [Atomic]+

you can only reference stuff that has a name (direct or via IQualifiedNameProvider)
your Atomic has (without the fix mentioned above) not a name

(2) port_type = ID

you want to have a enum so use one

port_type = PortType

enum PortType : input| output;

(3) output_port = [Port])?

the ports may have a FQN (name of Atomic|Network) . name of port
=> customize IQualifiedNameProvider

or change grammar to output_port = [Port|FQN])?
FQN: ID ("." ID)*;

(4)time = INT

time=Real

Real: INT "." INT;

with real being a string in the model you can change this using a valueconverter

(5) the states is the same than the ports

(6) the xtend class looks ok
besides some strage stuff like «IF("«port.port_type»".equalsIgnoreCase("input"))»
which is by definion false maybe you mean port.port_type.equalsIgnoreCase("input")

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Fri, 21 October 2011 21:13]

Report message to a moderator

Re: How to use reference and enumerated list in xtext grammar and parser [message #744708 is a reply to message #743940] Sat, 22 October 2011 09:25 Go to previous messageGo to next message
AHOT Mising name is currently offline AHOT Mising nameFriend
Messages: 13
Registered: September 2011
Junior Member
Quote:
(1) sub_Models += [Atomic]+

you can only reference stuff that has a name (direct or via IQualifiedNameProvider)
your Atomic has (without the fix mentioned above) not a name


I want to be able to re-use an instance of Atomic in multiple Networks without having to re-create it many times when the specification is the same.So I think that can be achieved by making reference to the existing one.
I don't really understand what you mean by the reference not having a name.
can you please give me a small example to illustrate that?.
I have implemented all other suggestions in your previous reply successfully though I still have one problem testing for the values of my enum elements in xtend, for example I have these snippets in my grammar rule:
Model:
	(Atomic|Networked)
	 //some lines removed here for clarity

	'port_list_open{'
	   ports += Port*
	'}port_list_close'
;

Port:                  
	'port_open {'
	  'port_name:'	name = ID
	  'description:'description = STRING
	  [color=darkblue]'port_type:'	port_type = PORT_TYPE //enumerated list {INPUT,OUTPUT}[/color]
	'} port_close'
;

[color=darkblue]enum PORT_TYPE:
     INPUT|OUTPUT;[/color]



And in my xtend code, I am trying to test the value of "port_type" and take some decisions based on the result as follows:

def atomic_Model_Parser(Atomic atom)'''
		//«atom.name.toFirstUpper».java
		//«atom.description»
		
		import model.Atomic;
		import java.util.ArrayList;
		
		
  public class "«atom.name.toFirstUpper»" extends Atomic{
     /**
       * I/O port names
       */
	«FOR port: atom.ports»
	  private String «port.name.toLowerCase»;
	«ENDFOR»
			 
	/**
          * Registering the input and output ports
	  */
     «FOR port: atom.ports»
	[color=red] «IF port.port_type == INPUT»[/color]//PLEASE NOTE THIS LINE
	  addInputPort(«port.name», «port.description»);
	«ENDIF»
	[color=red]«IF port.port_type == OUTPUT»[/color]    //PLEASE NOTE THIS LINE
	  addOutputPort(«port.name», «port.description»);
	«ENDIF»
     «ENDFOR»
		


However, it's like the if conditions are NOT satisfied as I'm not getting anythin from there

Thank you for your anticipated assistance
Re: How to use reference and enumerated list in xtext grammar and parser [message #744745 is a reply to message #744708] Sat, 22 October 2011 09:59 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

with the change to the grammar i suggested you the atomic now has a name. so let us have a
a look at the port problem / the problem in general.

let us take this grammar:

Model:
	packages+=Package+
	;
	
Package:
	"package" name=ID "{"
		elements+=Element*
	"}"
;

Element:
	notname=ID ("refs" refs=[Element])? 
;


as you can see the Entity has no name. so out of the box the referencing will never ever work

you can fix by changing the grammar to
Element:
	name=ID ("refs" refs=[Element])? 
;


or use a IQualifiedNameProvider like this one (dont forget to bind it in the runtime module)

public class MyQNP extends DefaultDeclarativeQualifiedNameProvider {
	
	QualifiedName qualifiedName(Element e) {
		return QualifiedName.create(e.getNotname());
	}

}


we choose to do the first variant as you did.

now the problem is how Xtext build the Qualified Names by default:
it will qn=be packagename.elementname
but the qn contains a dot.

in the grammar we have refs=[Element] which is shot
for refs=[Element|ID] wich means: refernce to an Element and therefore parse an ID

this is why i sugested the FQN Stuff

Element:
	notname=ID ("refs" refs=[Element|FQN])? 
;

FQN: ID ("." ID)*;


of course the custom qualfiedNameProvider as shown above
would do the name trick -> but then you would not be
able to have the name element name in different packages.

so this will help you to solve the referecing problem.
reaching the web/form for IQualifiedNameProvider / Scoping
will give you 1000 more examples / blog posts etc

the enum problem is easier to solve «IF(port.port_type == PortType::INPUT)»

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to use reference and enumerated list in xtext grammar and parser [message #744757 is a reply to message #744745] Sat, 22 October 2011 10:10 Go to previous message
AHOT Mising name is currently offline AHOT Mising nameFriend
Messages: 13
Registered: September 2011
Junior Member
I'm very grateful.
Thank you
Previous Topic:how do I write to the console?
Next Topic:Lookahead in Xtext?
Goto Forum:
  


Current Time: Thu Apr 25 19:51:11 GMT 2024

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

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

Back to the top