Updating child of other node

Hello everyone

I have a concept Party that has as a child a list "inputs" of InputDeclaration, which is basically a VariableDeclaration with an added name.

I also have a concept Computation that has as a child a StatementList "body" as well as a list "resultParties" of Party.

I created a checking rule that makes sure the StatementList always has a non-empty return statement that can be traced back to a name (like when returning a variable reference it would be the variable name). So each return statement has a name and an expression.

For each Party in the "resultParties", I would now like to automatically add that return statement as an InputDeclaration to the list "inputs" of the party.

I tried to do it in the same checking rule as follows:

  foreach party in computation.resultParties {
    party.ref.party.input.add(retInput);
  }

where retInput is the new InputDeclaration. But this does not seem to do anything. I also tried to do the same thing in a node factory for the computation, but it again does not seem to work. 

Is there a way to programmatically do this? 

Thanks in advance for any answers!

 

2 comments
Comment actions Permalink

Luckily, you cannot manipulate the AST in a checking rule. A checking rule runs every time a (sub-)tree gets invalidated, so this would add a LOT of nodes if this would work!

For models that already exist and where you need to add these nodes due to a language evolution (your language has evolved and has gotten this new feature you describe, but there are already models out there that don't satisfy the requirements of that new feature), you should use a migration script to add the "returns".

For nodes that the user creates anew, you can potentially go down different routes: 

  • Node Factories
  • Checking rule with auto quickfix: This recent thread might be useful to learn about this approach, I also created an example for a different use case that shows how this mechanism can be used to manipulate the AST as a "side-effect" of a user interaction
  • Maybe model listeners is what you want to apply: I think they are still par tof the mbeddr platform, and I don't know what state they are in. Basically, they allow you to execute code when children are added/removed by a user. But you should be careful with those.

 

0
Comment actions Permalink

Hello Robert, thank you for your answer! 

You are right, a checking rule like this would just add infinite nodes. I didn't even think about that.

I only need it for nodes that the user creates anew, and so I looked at your example of using a quick fix and that did the trick! 

Again thanks for the help!

 

0

Please sign in to leave a comment.