Refactoring Altova MapForce Functions

By | November 20, 2014

In this post I want to share a Python script I have created that refactors a user-defined function in an Altova MapForce mapping to a library (mapping) file while retaining all the instances of the function in the mapping and their connections to other parts.
The use case for this is when you have several mappings which make use of the same function(s) and would prefer to move them to a library of mappings, possibly to be shared among several developers.

Pre-Code Words

Before we begin, I want to say the following:

  • I am in no way associated with Altova, except for being a happy customer of theirs.
  • I have not received any information on the file-format used to save MapForce mappings.
    Altova being the Masters of XML, the file-format is of course XML so it is readable but I cannot claim to understand it completely.
  • You use this script at your own risk.
    It may possibly chew your mappings and fart afterwards. Make a backup and you will be safe.
  • Feel free to modify the script but please make any improvements available.
    The script is distributed under the GPL v3 license.
  • I have made limited tests with the script and Altova MapForce 2015.

Given my limited knowledge about Altova MapForce, I would accomplish what the script does by manually copying and pasting the function from the mapping to the library, then replace (and rewire) all the instances of the original function in the mapping with instances of the function from the library.
It is reasonable procedure the first few times, but I suspect that it will become tedious after a while and it is also slightly error-prone, given that you have to re-wire the function connections.

Code!

Enough chit-chat, here is the code:

After having saved the script, you may need to perform a “cmod +x [script filename]” in order to make it executable.

As stated in the initial comment, the script is to be executed with the following parameters:

  • Function name.
    This is the name of the user-defined MapForce function that you want to move to your library file. The name of the user-defined function will not change as part of the refactoring.
  • Current library name.
    The name of the library in which the function is located prior to the mapping.
    This name can be found in the MapForce Library pane and is the name of the parent-node of the function. Usually, the name of the current library is “user” for user-defined functions.
  • Destination library name.
    The name of the library to which the function will be moved.
    The library may contain other user-defined functions.
    If you want the function to remain in the same library as it was located prior to the refactoring, enter the same value as for the previous parameter.
  • Mapping file.
    The path to the mapping file containing the user-defined function to move.
    The function will be removed from this file and the mapping file will be updated by the script.
  • Library file.
    The path to the mapping file to which the user-defined function will be moved.
    The function, possibly with its library name updated, will be written to this file.

Recommended Approach

I recommend the following approach in MapForce when refactoring user-defined functions using the above script:

  • Open the mapping containing the function(s) that you want to refactor in MapForce.
  • Create a new mapping in MapForce and save it as your library file.
    If you already have a library file you want to use, skip this step.
  • Go back to the mapping containing the function, click the button Add/Remove Libraries… in the Library pane on the right in MapForce and add the library file of your choice, if it is not already added as a library.
  • Save the mapping containing the function.
  • Use the script above to refactor the function.
  • Allow MapForce to reload the modified files.
    If MapForce does not properly discover the modifications, you may need to close the files and open them again.

Leave a Reply

Your email address will not be published. Required fields are marked *