MPS enables language developers to modify models programmatically via its Open API. The API is quite straightforward and clear. What’s not so clear and where newcomers to MPS often get lost is where to put the code and how to get MPS to execute it. Oh, and how to get the project reference, since this is often the first step to be able to actually write some Open API code.

The Simplest Solution: Use MPS Console

If you want to execute a piece of code inside MPS with as little ceremony as possible, use MPS Console.

MPS Console is a powerful tool for ad hoc model manipulation. It can be used to refactor nodes programmatically or to find all nodes that satisfy a certain condition. But you can also use it to run arbitrary code: just type a left brace { in the prompt and choose baseLanguage statements in the completion menu:

Completion menu after typing left brace

The console offers several constructs starting with # to access the environment. Among them, #project gives the current project.

Completion menu with #project

For more information about all the capabilities of MPS Console I recommend consulting the corresponding section of the MPS documentation.

PRO TIP: Don’t Lose Your Console Snippets

All code you write in the MPS console is stored inside the .mps/workspace.xml file which is NOT supposed to be checked in to version control as it contains user-specific data. Its contents are therefore easily lost after a git clean or a new checkout.

To preserve your code, place it into a static method in a class in your project. You can then import the class into MPS Console using the standard Cmd/Ctrl+R shortcut and invoke the method from the console.