Howto generate nested structures of arbitrary depth?
I would like to generate a nested structure, from a given linear structure.
Structure in Source-Language: i_1, i_2, ... , i_n
Desired Structure in Target Language: foreach i_1 ( foreach i_2 ( ... foreach i_n ( do_sth() ) ... ) )
where n is arbitrary.
How can I do this in MPS?
Can I use weaving rules recursively ?
Please sign in to leave a comment.
There is advanced technique which allows to achieve such a recursion.
It involves the 'reduction rule' and the 'COPY_SRCL' macro.
The COPY_SRCL macro takes list of nodes (from src model) as the input and try to find and apply reduction rule to each node in the input. If the reduction rule wasn't found the macro will just copy the source node to the target model.
I've made simple language which defines 3 concepts: I1, I2, I3, and solution model with 3 roots - each root is instance of one of concept in the 'simple' language.
I'm going to generate XML where I1 nodes are mapped to 1st-level elements, I2-nodes to 2nd-level and I3-nodes to 3d level.
The generator 'simple' --> 'XML' is defined as follows:
- one mapping rule to create document and one reduction rule applicable to any node which is instance of a concept from the 'simple' language. (mapping_config.png)
- the 'doc' template contains xml document with root node. The content of the root node will be generated. The source query for the COPY_SRCL macro returns list of I1-nodes in source model. (doc_template.png)
- the 'create_element_and_proceed_with_next_level' template is similar but in this case the source query will return list of 'next level nodes' or empty list to return from recursion. (create_element_and_proceed_with_next_level_template.png)
The generation yields xml such as:
-
Igor.
Message was edited by:
alshan
Message was edited by:
alshan
Attachment(s):
mapping_config.PNG
doc_template.PNG
create_element_and_proceed_with_next_level_template.PNG
Thank you very much for the detailed explanation. Great support!
Matt