Traversing all nodes of every model

Hello again,

I would like to ask that is there a way to traverse all the nodes of every model that are present in the project?

I know I can access nodes of the modules with jetbrains.mps.lang.smodel by doing:
list<node<>> nodes = model/name_of_model/.roots();

But I would like to know if I am able to loop through all the models without providing their names explicitly.

Thanks.
0
3 comments
list<SModel> models = new arraylist<SModel>; 
models.addAll(new ProjectScope(ProjectHelper.getProject(editorContext.getRepository())).getModels()); 
models.forEach({~it => 
  nlist<> rootNodes = new arraylist<SNode>; 
  rootNodes.addAll(it.getRootNodes()); 
});

I don't know where you execute your code but you need a reference to Project. In my example you can access it with an editorContext. Unfortunately this method also contains nodes from stubs, languages and other nodes that you may want to filter. The next approach is maybe a bit better. You can try it in the console:

{
list<SModule> modules = new arraylist<SModule>; 
modules.addAll(new ProjectScope(#project).getModules()); 
modules.forEach({~module => 
  if (module instanceof Solution) { 
    list<SModel> models = new arraylist<SModel>; 
    models.addAll(module.getModels()); 
    models.forEach({~model => 
      nlist<> rootNodes = new arraylist<SNode>; 
      rootNodes.addAll(model.getRootNodes()); 
      #print rootNodes; 
    }); 
  } 
});
}
1
Thank you very much, the second solution was what I was looking for actually.
0

Hi! FYI, I just want to say thanks to fxlex. His code helped me in my project also! 

0

Please sign in to leave a comment.