Book a Demo

Author Topic: Transformation Template question  (Read 2871 times)

Bokkie

  • EA User
  • **
  • Posts: 80
  • Karma: +0/-0
  • Lima Bravo!
    • View Profile
Transformation Template question
« on: April 28, 2006, 04:09:02 am »
Hi there,

I have created a transformation that creates a complete state pattern from a statefull class (class with a statemachine). It works very well, from the transformation's result I am able to create 99% the code requered to implement the statemachine!

There are two minor obstacles however, perhaps somebody can give me a clue...

From a statemachine I create the standard state pattern where every state becomes a class. The transitions between the states become operations in the base-state-class (virtuals) and for the concrete state classes the applicable transitions are transformed to override operations. For the operation name I use the name of the transition's trigger.

Ok, here comes challenge #1: In most statemachines the same signals may occur in more than one state. In this case, my transformation results in duplicate operations in the base-state-class. Is there a way to filter duplicate operation names?

Challenge#2 is finding the initial state. Can anyone come up with a nice solution for this?

For your reference here is a stripped (incomplete) version of the transformation:

Class
Code: [Select]

Class
{
 $statecontext
 
 Operation
 {
   name="%className%"
   notes="constructor"
   scope="Public"
   type=""
   code="//TODO: Set initial state here
m_state = %className%State_INITIALSTATE.GetInstance();
m_state.OnEntry(this);"
 }
}

Class
{
 name="%className%State"
 notes="Base class for %className% states."
 $statebase
 $statebaseops=%list="Class__StateBaseOps" @separator="\n\n"%
 
 %REPLACE($statebaseops, "#CONTEXT#", className )%
}

Association
{
%TRANSFORM_REFERENCE("Assoc", classGUID)%
direction="Source -> Destination"
Source
{
$statecontext
aggregation="1"
access="Protected"
}
Target
{
$statebase
access="Protected"
role="m_state"
multiplicity="1"
containment="Reference"
}  
}


Class_StateBaseOps
Code: [Select]

%if elemType == "State"%

%list="Connector__StateBaseOps" @separator="\n"%  

%endIf%


Connector__StateBaseOps
Code: [Select]

%if connectorSourceElemGUID==classGUID%

Operation
{
 name="%connectorTrigger%"
 notes="Default handler for event %connectorTrigger%"
 Tag{name="virtual" value="true"}
Parameter
{
 kind="in"
 notes=""
 default="null"
 name="context"
 type="#CONTEXT#"
}
}

%endIf%


Thanks in advance,

M.
Lima Bravo!

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Transformation Template question
« Reply #1 on: May 03, 2006, 03:43:51 pm »
#1 There is a function macro that you may be able to use called REMOVE_DUPLICATES.  You could also use an add-in and the function call EXEC_ADD_IN if that makes things easier.

#2 This is a bit trickier.  To start with there is an extra substitution macro you'll need.  (It has managed to escape documentation so far)  %elemFullTypeName% will give the full type name of any element.  It should return something like "Initial State".

You can then either list over only initial states, or list over a template that only outputs the name for initial states.

Untested
Code: [Select]
%list="Class__Name" @separator="" elemFullTypeName=="Initial State"%
Untested
Code: [Select]
%if elemFullTypeName!="Initial State"%
%endTemplate%

%className%

Hope that helps.

Bokkie

  • EA User
  • **
  • Posts: 80
  • Karma: +0/-0
  • Lima Bravo!
    • View Profile
Re: Transformation Template question
« Reply #2 on: May 04, 2006, 01:05:08 am »
Thanks for the answers.

#2 is solved: I've managed to locate the start-state using %elemFullTypeName% as you suggested.

With respect to #1, How do you suggest to use the REMOVE_DUPLICATES marco? The Operations are created using a %list...%, I can assign the result of the list to an $variable but the structure in the variable is rather complex to filter out the duplicates.

Thanks,

M.
Lima Bravo!

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Transformation Template question
« Reply #3 on: May 04, 2006, 03:05:08 pm »
I've used REMOVE_DUPLICATES in similar situations by using some arbitrary separator string that is very unlikely to appear in any of the elements in the list.

eg.
Code: [Select]
$var=%list="Operation" @separator="#OPERATION_LIST_SEPARATOR#", @indent="\t"%
$var=%REMOVE_DUPLICATES($var,"#OPERATION_LIST_SEPARATOR#")%
%REPLACE($var,"#OPERATION_LIST_SEPARATOR#","\n")%

Bokkie

  • EA User
  • **
  • Posts: 80
  • Karma: +0/-0
  • Lima Bravo!
    • View Profile
Re: Transformation Template question
« Reply #4 on: May 05, 2006, 12:51:13 am »
Thanks! got it working now!

There are a few details to watch here: make sure to append the list-seperator to the variable or else the last operation will be different from any duplicate.

Code: [Select]

$var=%list="Class__ContextOps" @separator="\n"%
$var+="\n"
$var=%REMOVE_DUPLICATES($var,"#__SEPERATOR__#")%


Furthermore you must be very precise using spaces and newlines and the end and beginning of the templates.

Have fun!
Lima Bravo!