Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [XPand] Eliminate Double Entries
[XPand] Eliminate Double Entries [message #829495] Mon, 26 March 2012 07:17 Go to next message
Daniel Engelhardt is currently offline Daniel Engelhardt
Messages: 11
Registered: November 2010
Junior Member
Hello,

I have a problem concerning the elimination of double entries. I generate C++ Code from an XML file and I want to create enum entries inside of a C++ class, but unfortunately my input format does not describe this very well.

The input format has different submodules (called 'Functions') and inside each submodule, there is a declaration of several types. However, some types are declared identically multiple times in different submodules and there is a tacid understanding that these types actually are identical. Thus, it is forbidden to have different types by the same name in different subclasses.

In C++ I have to map these identical types from different submodules onto one common type in the more general class (this C++ class contains all declarations -- this is the way it was specified, I cannot change that).

So, in the beginning I had something straightforward like:
«DEFINE enums FOR FunctionType»
	«EXPAND enumHelper FOREACH submodule.subclass_a»
	«EXPAND enumHelper FOREACH submodule.subclass_b»
	«EXPAND enumHelper FOREACH submodule.subclass_c»
	
	«EXPAND enumHelper FOREACH submodule.subclass_d»
«ENDDEFINE»


Okay, but this produces double entries as in a different FunctionType (subclass), these types are declared again, as described above, and thus, I get a double entry in my C++ file.

So, I wanted to go a different route and create a list with all non-double entries and print this list afterwards. But my way got really ugly really quick:

«DEFINE enums FOR FunctionType»
    «LET (List[String]){} AS enums»
 // This is for the first subclass
	«LET (List[String]){} AS newenums»
		«FOREACH submodule.subclass_a AS p»
			«newenums.add(enumHelper(p)) -> ''»
		«ENDFOREACH»
			
		«LET (List[Integer]){} AS found»
			«FOREACH newenums AS n»
				«FOREACH enums AS e»
					«IF e.contains(n)==true»
						«found.add(1) -> ''»
					«ENDIF»
				«ENDFOREACH»
				
				«IF found.isEmpty == true»
					«enums.add(n) -> ''»
				«ENDIF»
			«ENDFOREACH»
		«ENDLET»
	«ENDLET»
        
        // TODO: the same code for subclass b...d [these subclasses have a different type, i.e. enumHelper is specified for each type specifically]

       «EXPAND printEnums FOREACH enums»
    «ENDLET»
«ENDDEFINE»


Not only is this really, really ugly. It also requires me to rewrite all my Xpand enumHelper macros in the extension format (ext), so that they can be called as in

«newenums.add(enumHelper(p)) -> ''»

I don't want to do this.

In short: I don't want to go down this route, if it is in any way avoidable.

Is there a better way to solve my problem, i.e. the elimination of multiple entries from different lists. Could I for example merge them together, or find out what I already printed out? Is there some kind of standard way or design pattern for this?

Or: in the very least, can I call Xpand macros in a way such that I can call them as in

«newenums.add(enumHelper(p)) -> ''»

and redirect the output, that would have otherwise went into the file into a String or Stream datatype which I can modify later?

Any other ideas?

Help would be much appreciated. If the problem is unclear in any way, I can go into more details and try to explain the problem more clearly.

Thank you very much in advance.

Best regards,
Daniel
Re: [XPand] Eliminate Double Entries [message #829734 is a reply to message #829495] Mon, 26 March 2012 14:01 Go to previous message
Karsten Thoms is currently offline Karsten Thoms
Messages: 353
Registered: July 2009
Location: Dortmund
Senior Member

Actually it is ugly to NOT program computation logic in Xtend. You should not do any non-trivial computations in Xpand code, but rather delegate to an Xtend function.

Did you ever consider using toSet() functions to eliminate duplicates? And a recursive Xtend call would also be good to collect the enums over all required types.

Regards,
~Karsten


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @kthoms
Blog : www.karsten-thoms.de
Previous Topic:Stereotype Problems
Next Topic:[SOLVED (+ or -)] JET context.getVariable returning AnyTypeImpl
Goto Forum:
  


Current Time: Thu Aug 21 14:15:13 EDT 2014

Powered by FUDForum. Page generated in 0.01611 seconds