Skip to content

rALF Snippet Compiler Usage Examples

lunkpeter edited this page Sep 29, 2015 · 2 revisions

rALF Snippet Compiler Usage Example

The rALF snippet compiler can be accessed using the rALF generator API, however it must be instantiated before use. This can be done in the following way: (Examples shown here are implemented using the Xtend language)

//Snippet compiler that creates a snippet template tree based on the parsed code
//It needs an UML valueDescriptor factory which is used for determining which CPP element can be traced 
//back to what UML element. It also need an UML context which will be used to determine the type of the 'this' object
val compiler = new ReducedAlfSnippetTemplateCompiler(new DummyUmlValueDescriptorFactory())

Note, that the descriptor factory and UML context provider objects used in the example are mocked ones, do not use them in live code.

Create snippet directly from action code

The recommended method for accessing the rALF snippet compiler is via the rALF generator API. This way, after the rALF action code is parsed, the generator checks for any parsing and validation errors before running the snippet generation.

val context =  new TestModelUMLContextProvider("Test_Model_Location");
val input = '''Integer x = 1;'''
val parser = new ReducedAlfParser
val compiler = new ReducedAlfSnippetTemplateCompiler(new DummyUmlValueDescriptorFactory())
val generator = new ReducedAlfGenerator
generator.createSnippet(input , context , parser , compiler)

As it can be seen on the code fragment above, firstly rALF parser, snippet compiler and generator objects are instantiated. After this, following the design pattern of dependency injection, the parser and snippet compiler objects are passed to the generator, which uses them to process the given input.

Create snippet from parsing results:

It is also possible to generate C++ snippets from already-existing rALF parsing results:

val context =  new TestModelUMLContextProvider("Test_Model_Location");
val input = '''Integer x = 1;'''
val parser = new ReducedAlfParser
val compiler = new ReducedAlfSnippetTemplateCompiler(new DummyUmlValueDescriptorFactory())
val generator = new ReducedAlfGenerator
// Parse the action code
val result = parser.parse(input, context)
// Generate snippet from parsing results
val snippet = generator.createSnippet(result, context, compiler)

Serializing snippets

In order to access the generated snippet in string format, the created snippets need to be serialized. This can be done in the following way:

val serializer = new ReducedAlfSnippetTemplateSerializer
val serializedSnippet = serializer.serialize(snippet)
Clone this wiki locally