Sort children during generation

I want to sort children during the generation flow. At the moment I'm using the sort method to create a new sorted sequence (sorted = parentNode.children.sort({...}, asc)). Afterwards I clear the original sequence (parentNode.children.clear) and add all elements from the sorted sequence (parentNode.children.addAll(sorted)).
Even if this hack sounds quite dirty, it will do its job and the generated code is correct. Unfortunately references to the sorted elements break for a short period during generation. This results in flood of error messages like this:
[jetbrains.mps.smodel.StaticReference] couldn't resolve reference 'target' from  SignalReference ...
 -- target model 'r:50633910-27cb-4832-90d8-4ef14916a957(hml.sandbox.types@8_0)' doesn't contain node with id=3800155958210691946
Is there any way to sort the children in place without clearing the list?
0
5 comments
If I understood you aright, you change the "input model", right?
This is a bad practice, which will not work in future.

Instead, you can:
1) sort input nodes everywhere you need the sequence to be sorted
2) add an additional generation step, on which the collection will be sorted
3) make a "pre-process mapping script", which can change input model, and sort there
0
Thanks for your answer!
Do I understand you right that you list alternatives?
I use the third one at the moment. The script looks like this:
mapping script Sort Statements        
script kind : pre-process input model        
modifies model : true              
(genContext, model, operationContext)->void { 
  model.nodes(Module).where({~module => module.sortStatementInOutput; }).selectMany({~module => module.descendants<concept = ConcurrentBlock>; }).forEach({~block => 
    sequence<node<ConcurrentStatement>> sorted = block.statements.sort({~a,~b => a.getPresentation().compareTo(b.getPresentation()); }, asc); 
    block.statements.clear; 
    block.statements.addAll(sorted); 
  }); 
}



Is this what you would recommend?
0
yep. Does it fail or succeed?
0
It succeeds with the warning messages I post above.
0
I see. Imo this should work. I'll try to investigate what happens.
Filed: http://youtrack.jetbrains.com/issue/MPS-19767
0

Please sign in to leave a comment.