Automating Code-to-Model Synchronization

If you frequently modify generated C++ code outside Model RealTime it can be convenient to automate the process of performing code-to-model synchronization, to propagate the changes made in generated files back to the model. In the Build Active Transformation Configuration button menu there is a command Synchronize Workspace for doing this:

It performs code-to-model synchronization for all TCs in the entire workspace. Of course, you can be more selective and only do it for a particular TC.

Sometimes it's useful to invoke code-to-model synchronization automatically. For example, if you have scripts that make changes to generated code, and want the script to invoke the code-to-model synchronization afterwards. In this case you can use an Ant task called com.ibm.xtools.umldt.rt.transform.cpp.codeSync. And the first step is to define an Ant file.

Creating Ant task file for code-to-model synchronization

The Ant task com.ibm.xtools.umldt.rt.transform.cpp.codeSync supports the following attributes:

transformConfig

A path to the TC that you want to synchronize. It can be specified as:

<project name="CodeToModelSynch" default="synch">
  <target name="synch">
    <com.ibm.xtools.umldt.rt.transform.cpp.codeSync transformConfig="/HW/HelloWorld.tcjs" />
  </target>
</project>
<project name="CodeToModelSynch" default="synch">
  <target name="synch">
    <com.ibm.xtools.umldt.rt.transform.cpp.codeSync transformConfig="file:/C:/_Work/CodeSyncTask/HW/HelloWorld.tcjs" />
  </target>
</project>

mapFile

An absolute path to a map file that defines list of projects in the workspace and list of all source files that should be synchronized, for example,

<project name="CodeToModelSynch" default="synch">
  <target name="synch">
    <com.ibm.xtools.umldt.rt.transform.cpp.codeSync mapFile="C:/_Work/CodeSyncTask/syncMap.map" />
  </target>
</project>

This attribute is useful when code is generated by the model compiler outside of Model RealTime. Map file defines project locations and a list of source files for synchronization. Here is an example of the map file syntax:

[projects]
MyProject1=C:\workspaces\MyWorkspace\MyProject1
MyProject2=C:\workspaces\MyWorkspace\MyProject2
[sources]
D:\generated\folder1
D:\generated\folder2\MyFile.cpp

You can use Java system properties or environment variables in the map file using the syntax ${VAR} or $(VAR).

Model compiler can generate such map file automatically during code generation. This can be enabled by command-line option --syncMap:

java -jar modelcompiler.jar ... --syncMap C:/_Work/CodeSyncTask/syncMap.map

debug

Set this attribute to true to enable additional logging during the code-to-model synchronization.

All attributes are optional but one of transformConfig or mapFile must be set (but not both at the same time).

Running Ant task from Model RealTime UI

Running Ant task from command-line

To run the code-to-model synchronization Ant task outside of Model RealTime, make sure to use the org.eclipse.ant.ui.antRunner application. The command-line should look like this:

java -classpath <eclipse-installation>/plugins/org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar org.eclipse.equinox.launcher.Main -application org.eclipse.ant.ui.antRunner -data <workspace> -buildfile <Ant file> synch

where,

Note that the version of the plugin org.eclipse.equinox.launcher may be newer in your Model RealTime installation.

Important: The workspace must not be opened in the product UI (eclipse) when running code-to-model synchronization Ant task from command line on the same workspace.

If you use a map file that specifies projects, the workspace can be empty or even some temporary folder can be used as workspace. The projects will then be imported into the workspace automatically.