Skip to content

Example 3. SampleTransform

Daniel Balasubramanian edited this page Oct 17, 2022 · 1 revision

Models can be transformed to another model with similar structures using the “transform” operation. Here is a simple example demonstrating how the transformation works:

domain TransformDomain
{
  A ::= new (id: Integer).
  B ::= new (id: Integer).
}

transform SampleTransform(in:: TransformDomain)
		returns (out:: TransformDomain)
{
  out.B(x) :- in.A(x).
}

model m of TransformDomain
{
  A(1).
  A(2).
}

The “transform” module has name “SampleTransform” and takes an input model “in” of TransformDomain and returns the output model of TransformDomain. Each A(x) in the model in will be transformed into B(x) in the output model. Let’s say we want to transform the model m in the above example, the first thing to do is to apply the SampleTransform to the model m and give the return model a name, which is “output” in this case.

 []> apply output = SampleTransform(m)

Then extract and install a result by running

 []> extract 0 output

Here 0 is the task id of applying transform and “output” is the name of the result we are trying to install.

To see what the installed result is like, this step allows us to print it to the screen

 []> print output

 model output of TransformDomain at "/Users/liuxinran/work/formula-examples/TransformExample.4ml"
 {
      output__B__0x0 is B(1),
      output__B__0x1 is B(2).
 }

As expected, A(1) and A(2) in the model m are transformed into B(1) and B(2) in the model output.

Finally, save the result to a path you want (e.g. d:\work\output.4ml) by running

 []> save output d:\work\output.4ml
Clone this wiki locally