How to debug in MPS — September 21, 2017

How to debug in MPS

The quickest way to start debugging in MPS is the following:

We are using com.dslfoundry.plaintextgen as an example.

Please be advised that in (possibly earlier) generic distribution versions of MPS, the debugger doesn’t work. You will have to really use one of the dedicated distributions (Mac, Linux, or Windows).

  • Choose your breakpoint by clicking in the gutter:

mps-debugging-1

  • Select Run –> Edit Configurations from the main menu:

mps-debugging-2

  • Press + to choose from the list of new configuration types to add:

mps-debugging-3

  • Choose MPS –> MPS Instance:

mps-debugging-4

  • Check Open current project:

mps-debugging-5

  • Type your own name for the run configuration (I chose MPSInstance):

mps-debugging-6

  • Press OK to close the Run configuration screen and then select Run –> Debug MPSInstance from the main menu:

mps-debugging-7

Now a second instance of MPS will be started (click away all the startup screens that come in).

With the given breakpoint, let’s inspect variables:

  • Trigger the code with the breakpoint by executing the Surround with Horizontal Collection intention:mps-debugging-8
  • Now the MPS instance is going to freeze, because the debugger paused it at the breakpoint. Go back to the original MPS window from where you started the debug session and view variables:mps-debugging-9

Tip: enable toolbar for having convenient debug buttons. You can do this by selecting View –> Toolbar from the main menu.

Advertisements
How to write a dump of an MPS model to XML based on its structure — October 27, 2016

How to write a dump of an MPS model to XML based on its structure

This post has been written using material developed in JetBrains MPS 3.3.5. The example project can be found at DSLFoundry’s MPS example github repository under StructureDumpToXML. It is recommended to have this project opened while going through this howto.

Suppose you want to dump a model to some simple XML format without wanting to write a generator or wanting any specific knowledge about the structure of the model. A use case for this could be that you don’t want to have to make changes in the generator for a simple XML dump, just because you change the structure. Now of course this can be solved using custom persistance, but let’s say you just want something quick and simple without having to deal with too much of the more complicated stuff like references. This howto shows such a simple case. So here we go:

An example model:

xmlstructuredump-0

Let’s use the generator mechanism and the xml language from JetBrains to make an XML dump out of this:

  • make a root mapping rule, by double clicking on the generator’s main, then click on root mapping rules and press enter:

xmlstructuredump-1

  • choose MyConcept in the input concept:

xmlstructuredump-2

  • import the xml language by pressing Ctrl+L+L (yes, you have to press L a second time) and then choosing it:

2016-10-27-13_03_21-structuredumptoxml-d__repo_dslfoundry_mps-examples_structuredumptoxml-_

  • stand on the template of the root mapping rule and press Alt+Enter to invoke the intention menu and choose New Root Template, then choose directly xml file:

2016-10-27-13_04_43-structuredumptoxml-d__repo_dslfoundry_mps-examples_structuredumptoxml-_ and then:

2016-10-27-13_05_56

  • navigate to map_MyConcept by Ctrl + clicking it:

2016-10-27-13_07_20-structuredumptoxml-d__repo_dslfoundry_mps-examples_structuredumptoxml-_

  • add an XML element using the completion menu:

2016-10-27-14_32_53-structuredumptoxml-d__repo_dslfoundry_mps-examples_structuredumptoxml-_

  • call the element something (e.g. bla) and create a MAP_SRC macro which makes it possible to build any wild mapping construct you’d like:

2016-10-27-14_34_45-structuredumptoxml-d__repo_dslfoundry_mps-examples_structuredumptoxml-_ and then:

2016-10-27-14_35_26-structuredumptoxml-d__repo_dslfoundry_mps-examples_structuredumptoxml-_

  • now go to the inspector of this MAP_SRC macro by clicking the macro and pressing ALT+2:

2016-10-27-14_37_08-structuredumptoxml-d__repo_dslfoundry_mps-examples_structuredumptoxml-_

  • go to the <none> part of the mapping func and then press Enter to create a mapping function:

2016-10-27-14_38_52-structuredumptoxml-d__repo_dslfoundry_mps-examples_structuredumptoxml-_

  • from this point on, you can type baseLanguage code, which is pretty much the same as typing java. We replace the XML element <bla></bla> with a new XmlElement node that we create, using some regular programming. See the next step first to see how to create the dumpStructure funciton so that you can call it here in the macro. The code you see is a combination of baseLanguage and smodel language. The MPS smodel language makes it possible to query a model and build nodes. For more information on this, see the MPS smodel reference:

2016-10-27-14_40_15-structuredumptoxml-d__repo_dslfoundry_mps-examples_structuredumptoxml-_

  • in the above piece of code, we use a piece of behavior that belongs to a helper aspect called StructureDumper. This code can be created by making a concept called structure helper and then making a new behavior for it:

2016-10-27-14_44_40-structuredumptoxml-d__repo_dslfoundry_mps-examples_structuredumptoxml-_

  • now we can type the code for the dumpStructure function. This is just a recursive function reading the model and dumping everything to XML. Note: in the statement mpsNode/.getProperties(), there is this strange notation mpsNode/. This is a so-called semantic downcast operation. It makes it possible to reach directly the java class that encode the node mpsNode (needed here for reflectively reaching its properties).

2016-10-27-14_46_49-structuredumptoxml-d__repo_dslfoundry_mps-examples_structuredumptoxml-stru

  • since the XML language has a text generator and we hooked the transformation from our model to XML in a MAP_SRC macro of a generator, we have text generation. You can preview the text by right-clicking the model called myconcept in the StructureDumpToXML solution and selecting Preview Generated Text:

2016-10-27-14_52_36-structuredumptoxml-d__repo_dslfoundry_mps-examples_structuredumptoxml-_

  • and the text will look like this:

2016-10-27-14_53_29-structuredumptoxml-d__repo_dslfoundry_mps-examples_structuredumptoxml-map_

  • note that nothing smart has been done yet with references. This is tricky, since we may get (even indirect) circular references.

This howto has demonstrated the use of MAP_SRC macro to do a dump of an arbitrary (at least for simple cases where it’s a tree) MPS model to an XML format of your own choosing. Feel free to extend and improve for your own needs. We may one day make a language that will just do this automatically.

If this post helped you, gave you ideas or has motivated you to help, please feel free to contact us on github.

Extra tips:

  • if you need special characters properly escaped as XML entities in MPS 3.3.5, it is recommended to use mbeddr’s com.mbeddr.mpsutil.xml.fix language.
  • you may need to use light quotations instead of the manually written code for node instantiations in the above described dump function (see JetBrains docsite on light quotations). The reason that you cannot use full quotations is, I think, because you may not have all the editors built before building behavior, which makes it impossible to have the editor to make a full quotation.
How to add default to a property in a concept — October 12, 2016

How to add default to a property in a concept

This post assumes that you have an MPS with mbeddr preloaded.

Go to the constraints aspect for the concept you want to add a default to a property:

hdp-01

Create a new constraint for this concept:

hdp-02

Press Ctrl+L and import the language com.mbeddr.mpsutil.propertydefault:

hdp-03

hdp-04

Go to the property constraints and press Enter to make a new one:

hdp-05

Choose the property to which you want to apply a default value:

hdp-06

Select the entire constraint by pressing ctrl + uparrow twice:

hdp-07

Press Ctrl + Space and choose default to convert the constraint to a default specification:

hdp-08

Choose your default value:

hdp-09

If you now make an instance of MyConcept, you will automatically get the myProperty filled in with the default value:

hdp-10

 

How to rename an MPS project — October 29, 2015