From Icbwiki

Jump to: navigation, search

This example models a text language, that is a language made of sequences of words interspersed with punctuation signs.


The logical View

The logical view, on the top left, shows a tree:

  • Project
    • sandbox
    • jetbrains.textLanguage
    • modules pool

  1. Click to expand jetbrains.textLanguage to show the structure of the language:
    • Project
      • jetbrains.textLanguage
        • structure:jetbrains.textLanguage.structure
          • Colon
          • Comma
          • ...
          • Word
          • WordValue
        • editor

Concepts of the language

Colon, Comma, Word are concepts of the language. Click on a concept to see its definition in the right panel. For instance, if you select Word, the following is shown in the right panel:

 concept definition Word
   extends: TextElement
   is root: false
   value: WordValue

The Word concept is defined as extending the TextElement concept and having a value property of type WordValue. Moving the caret on TextElement and pressing CTRL-B (^B) navigates to the definition of the concept under teh caret TextElement. The back arrow (one of the icons under the menu bar, shown as an arrow to the left from a rectangle) allows to navigate back to the previous concept visited.

Note that Word is a concept of the jetbrains.textLanguage language. The definition of Word, however, is given in a different language. That language is jetbrains.mps.bootstrap.structureLanguage, the Structure Language for short. You can discover this fact by right clicking on "concept declaration" and selecting "Go to concept declaration" (or pressing Ctrl-Shift-S on top of concept declaration).

The text language structure

Find the concept Text. This concept has attribute isRoot=true. This indicates that this concept can appear at the top-level of "documents" expressed in the textLanguage. The Text concept declaration includes the following lines:

 element aggregation 0..n target: TextElement

This indicates that Text instances can contain zero or more element(s) of type TextElement.

Creating instances of the TextLanguage

Since the TextLanguage is pre-defined, we can create documents in this language. In MPS, a document is called a model. Creating a model is done in the sandbox.

  1. Right-click the Sandbox (under Logical View).
  2. Select New Model
  3. A pop-up is shown to request parameters.
  4. Move the cursor caret to <no root> and press Ctrl-space. This should auto-complete to a reasonable path.
  5. Move the cursor caret to <no name> and enter sentences. Our document/model will be called "sentences". This is appropriate since it will contain instances of Text.
  6. Move the curor caret to << ... >> under 'languages:' and use Ctrl-space again to bring a list of available languages. You want to find and select jetbrains.textLanguage (could be last on the list).
  7. You can see the sentences model in the Logical View:
    • Project
      • sandbox
        • <.>
          • sentences
  1. Right click on sentences and press Create Root Node->jetbrains TextLanguage->Text (notice that Text is the only choice because it is the only concept of the TextLanguage with isRootNode=true).
  2. The last action created a Text instance (shown under sentences in the LogicalView). An editor window is shown on the right to allow editing the new instance.
  3. At the location where the cursor carret blinks, starting typing the characters 'Hello' (omit the quotes). As you type, MPS populates the Text instance that you edit with a Word (in the field element) and enters the characters typed into the new Word instance just created.
  4. Press return. This ends the current word. If you continue typing, a new word is created.
  5. Try typing the following ', how are you?'. You will need to press return after each punctuation sign and word. As you type, the Logical View is updated to reflct the state of the Text instance.
  6. When you are done typing, the Text instance should have the following elements:
 element: Word
 element: Comma
 element: Word
 element: Word
 element: Word
 element: Question
 element: Word (possibly to allow entering a new word)

Document/model transformation, the Generators

When we have defined a model/document, we would like to do something with it. To do this, we need a generator. The generator transforms a document expressed in a source language into a document expressed in a target language.

  1. Locate the Logical View entry generators, right click and select 'New Generator'.
  2. A pop-up prompts you for parameter about the generator that you want to create.
  3. Enter the target language: jetbrains.TextLanguage (we will generate from TextLanguage to TextLanguage).
  4. Accept the default path for template path. This is where the generator templates will be stored for this target language.

The Logical View should now look like this:

  • generators
    • generator -> jetbrains.textLanguage
      • <.>
      • <templates>
        • jetbrains.textLanguage.generator.textLanguage.template.main@templates
          • main

The node main represents the main template for transforming TextLanguage into TextLanguage.

  1. Double click the main node to show its content in the editor pane. Something similar to the following should be shown:
 mapping configuration: main
   mapping rules: 
      << ... >>
   weaving rules: 
      << ... >>
   reduction rules:
      << ... >>

When you clik on the << ... >> symbol below mapping rules, the symbol turns blue. When the symbol is blue, press Enter. Pressing Enter replaces the << ... >> symbol with the following line:

 for each <no source query> --> <no rule name> : <no template>

When I reach this stage, I do not know how to proceed. How do I define the query and template parts? There is some mention of query and template on [[1]], but that blog entry is too imprecise to be much help.

Personal tools