Generating list of variables

Hello,

I try to create reduction rules that reduce a model structure like this,

root: Root
|_
    switchCollection: SwitchCollection[1]
    |_
        switches: Switch[1..n]

(where a Switch implements INamedConcept and has a value property which is either "on" or "off")
into a Java class with a list of boolean variables (where on means true, and off means false).

So, for a model like this (just imagine the editor projecting like this):

Switches:
  foo is on
  bar is off
  baz is on

I'd like to get this:

public class Foo {
  private boolean foo = true;
  private boolean bar = false;
  private boolean baz = true;
}

Seems simple enough, but I fail.
Here's what I have:

  • a root mapping rule for "Root" that creates the "public class Foo { ... }" frame

public class Foo {
  $COPY_SRC[]$
}

Where the COPY_SRC macro reads:

(genContext, node, operationContext)->node<> {
  node.switchCollection;
}

  • a reduction rule for the Switch concept:


public class Foo {
  <TF [private final boolean $[name] = $[false];] TF>
}

The first property template fills in the Switch's name, the second either "true" or "false", depending on the state of the value property (on/off).

  • and finally a reduction rule for the SwitchCollection concept. However, no matter how I slice it, I only ever get one of the many switches put out.
For example, I tried it like so:


public class Foo {
  <TF [$LOOP$[$COPY_SRC$[]]] TF>
}

Where the LOOP iteration sequence runs over the "switches" child of the SwitchCollection and COPY_SRC is supposed to just fill in there whatever the reduce_Switch rule creates (for each Switch, that is!).

So, for above example, only the "foo" variable ends up being in the final model, while "bar" and "baz" are dropped.

public class Foo {
  private boolean foo = true;
}

When I look at the "transient model stack", I see that they are already dropped in the very first reduction step:

Switches:
  private boolean foo = true;

Interestingly enough, though: when I remove the root mapping rule and the reduction rule for SwitchCollection from my mapping configuration, all switches are reduced to boolean variables just fine (of course, the class frame is missing).

Switches:
  private boolean foo = true;
  private boolean bar = false;
  private boolean baz = true;

So, what is the trick I'm unaware of?

Thanks,
Robert
3 comments
Comment actions Permalink
Okay, a workaround that just occured to me is this:

  • get rid of the reduction rule for SwitchCollection altogether
  • rewrite the root mapping rule like so:

public class Script {
  $COPY_SRCL$
}

where COPY_SRCL does the following:
(genContext, node, operationContext)->sequence<node<>> {
  node.switchCollection : SwitchCollection.switches;
}

This works. Still, I wonder why my other approach failed, since it seems more natural.
0
Comment actions Permalink
Your first approach should work. If only one child is generated always make sure that $COPY_SRCL surrounds a Statement and not a StatementList .

Consider the following example:

/*package*/ void test() { 
  $COPY_SRCL$[<no statements>]
} 
 
/*package*/ void test2() { 
  $COPY_SRCL$[] 
}


The first one generates only one child, the second one all of them. I think this should definitely be in the official documentation because it is a bit confusing.
0
Comment actions Permalink
Ahhh, that was it. Thanks! Not sure how I can mark your answer as "the answer" and close this thread, though :(
0

Please sign in to leave a comment.