I’ve seen a few questions lately asking whether one can access the data/code one has in MPS from outside of MPS. The
simplest way to make MPS models available to external tools is to export a root node or an entire model in XML format
using NodeSerializer
from the MPS-extensions project.
Here is how you can try out NodeSerializer
on your model:
- If you are not using MPS-extensions or mbeddr platform in your project install the de.itemis.mps.utils MPS plugin.
- Open the MPS Console tool, import the
NodeSerializer
class (MPS link) by pressingCtrl+R
and searching for it in the popup. - Type the following code into the console:
where
new NodeSerializer(nodeRef@123456, false, "").getXMLAsString()
nodeRef@123456
is a reference to a node in your model. You write it by copying and pasting a node into the console. - Execute it by pressing Ctrl+Enter (Cmd+Enter on a Mac).
You should then see the output directly in the console, it will look something like this (with extra line breaks for clarity):
<root nodeID="98682967484619562"
conceptFQN="jetbrains.mps.baseLanguage.structure.ClassConcept"
conceptID="f3061a53-9226-4cc5-a443-f952ceaf5816/1068390468198"
modelID="r:5bc020f2-590a-4818-ae68-fa483b92486f"
modelFQN="com.mbeddr.mpsutil.serializer.xml.serializer"
isStatic="true"
nestedName="NodeSerializer"
shortDescription="class (c.m.m.s.xml.serializer)"
name="NodeSerializer"
resolveInfo="NodeSerializer">
<superclass nodeID="98682967488494074"
conceptFQN="jetbrains.mps.baseLanguage.structure.ClassifierType"
conceptID="f3061a53-9226-4cc5-a443-f952ceaf5816/1107535904670"
linktype="child">
<classifier linktype="ref"
targetNodeID="98682967488480204"
targetModelID="r:5bc020f2-590a-4818-ae68-fa483b92486f"
targetModelFQN="com.mbeddr.mpsutil.serializer.xml.serializer"
targetConceptFQN="jetbrains.mps.baseLanguage.structure.ClassConcept"
targetName="XMLConstants" />
</superclass>
<member nodeID="98682967484619621"
conceptFQN="jetbrains.mps.baseLanguage.structure.PlaceholderMember"
conceptID="f3061a53-9226-4cc5-a443-f952ceaf5816/1465982738277781862"
linktype="child"
shortDescription="member (c.m.m.s.xml.serializer.NodeSerializer)" />
...
(This is an excerpt from the export of the NodeSerializer
class itself.)
You can also get the output as an org.jdom2.Element
rather than a string, and you can customize the output via a
few flags or using a SerializerProcessor
implementation to alter each node after it is built by the serializer.
You may have noticed that MPS already stores its models in XML files. I don’t recommend using these files directly because the format they use is adapted to MPS’ needs. It is normalized, meaning that it tries to store a given piece of information only once, and it also encodes node IDs in a particular way to save space.
When using NodeSerializer
to export nodes you get a different XML structure that is not as space-efficient because it
contains a lot of redundancy but this redundancy makes it much easier to process.