You are here: Jython Script Interfaces > Interface to engine for Jython scripts in the datamapper

Jython User API to MD Link engine for Datamapping scripts

 

The interface to the MDLink engine from Jython scripts in the datamapper is similar to the interface for the Custom Task and other area of the MDLink where you can enter Jython code, but there are some key differences.

 

One key difference is that here some fields and functions are available through simple names so that you don't need to prefix them with 'engine.' as you do in the Custom Task. For example the most often used variables in datamapping Jython scripts are called 'input' and 'output' - not 'engine.input' and 'engine.output'.

 

Jython scripts in the datamapper that wish to access 'util' functions will always need to use the prefix 'util.'.

 

See here for code samples of Jython in the datamapper.

Fields

input (type: string)

This is the input string for this node from the source document. If the selected mapping is a result of mapping with hard-coded cardinality, then this value will not be defined.

output (type: string)

Your script will need to set this variable to whatever you wish the output to be for the node in question. If your script does not set this variable, then the input value will be used as the output.

Functions

string input(string xpath)

Get the string value of the specified XML node from the source document. The xpath value can be absolute (such as '/ADT_A01/MSH/MSH.9-Message_Type/MSG.1-Message_Code') or relative (such as 'MSH.9-Message_Type/MSG.1-Message_Code' or '../MSG.1-Message_Code'.)

If more than one node matching the xpath is found, then the first one will be used. If none are found, then None will be returned.

<list of strings> inputs(string xpath)

Get the string value of all matching XML nodes from the source document, as a list of strings. The xpath value can be absolute or relative. If no nodes matching the xpath are found, then an empty list will be returned.

void outputxml(string xpath, string value)

Output an XML node, relative to the node that this script is defined on. For example: 

outputxml('HD.1-Namespace_ID', 'MDLINK')

string var(string variablename)

A shortcut for getVarVal(). For example, instead of writing:

output = engine.getVarVal('sender')

you can write:

output = var('sender')

 

Fields prefixed with 'engine.'

appVersion (type: integer)

Read-only property. This is the version of MD Link that is currently running (major and minor version numbers only) encoded as an integer such that, for example:

4.3.14.2 becomes 4030000

5.12.5 becomes 5120000

compId (type: string)

Read-only property. This is the ID of the enclosing task - for example, T20.

logdebug (type: python file-like object)

Read-only property. This is a Jython output file stream that is redirected to MD Link's debug-level logs. You can use it as part of a print statement like this:

print >> engine.logdebug, 'Some debug info.'

loginfo (type: python file-like object)

Read-only property. This is a Jython output file stream that is redirected to MD Link's info-level logs. You can use it as part of a print statement like this:

print >> engine.loginfo, 'Some info-level info.'

A simple print statement like this:

print 'Some info-level info'

will accomplish the same thing. This field is just provided for convenience in some programming scenarios.

logwarn (type: python file-like object)

Read-only property. This is a Jython output file stream that is redirected to MD Link's warning-level logs. You can use it as part of a print statement like this:

print >> engine.logwarn, 'Some warning info.'

logerror (type: python file-like object)

Read-only property. This is a Jython output file stream that is redirected to MD Link's error-level logs. You can use it as part of a print statement like this:

print >> engine.logerror, 'Some error info.'

inputDom (type: Java DOM object - i.e. org.w3c.dom.Document)

This is the source-side input message of the current task execution - that is, before any datamapping occurs. It is a DOM (Document Object Model) document object, so familiarity with that API is necessary to work with it. Please see the documentation for the Java DOM implementation.

The document tree provided by this field is not read-only. Your script may modify it as you wish. This may be useful if for example you are copying the input document to the output document with little or not modifications.

inputNode (type: DOM Node)

The node within the inputDom document that represents the source-side of this mapping. If this mapping has no associated source side (i.e. was mapped with hard-coded cardinality) then this will be the first source-side ancestor which does have an associated source side node.

solnId (type: string)

Read-only property. This is the ID of the solution of which this task is a part, as assigned by the engine. eg. SOLN3.

solutionFilename (type: string)

Read-only property. This is the absolute filename of the solution file of which this task is a part.

transId (type: string)

Read-only property. Available from the 'Run' script only. This is the ID of the current transaction, as assigned by the engine. eg. TRANS99. Generally, every firing of an event will create a new transaction ID, and all task executions caused (directly or indirectly) by that firing will have the same transaction ID.

qelemId (type: string)

Read-only property. Available from the 'Run' script only. This is the ID of the current task execution, as assigned by the engine. eg. QELEMID-TRANS7-E2-T3-1-T4-9-T6. This ID will be unique under each solution ID for each task execution, so it is useful for implementers that require such a unique key.

firstRun (type: boolean)

Read-only property. Available from the 'Run' script only. This is True during the first execution of this script since this solution was loaded, and False during all subsequent executions.

Functions prefixed with 'engine.'

void cancel(string message)

Available from the 'Run' script only. This causes no message to be passed to any downstream tasks as a result of the current task execution. The current task execution will cease immediately. Control will not return to the calling Jython code. Info message will be logged.

Parameters

message - optional. This message string will show up in the log message that results from this call.

void fail(string message)

An error message will be logged. Control will not return to the calling Jython code.

If this is called from the 'Load' script, then the solution will refuse to load. If it is called from the 'Run' script, then the current task execution will fail. If it is called from the 'Unload' script, nothing will happen except for the error message logged.

Parameters

message - optional. This message string will show up in the log message that results from this call.

void ret()

This method causes the Jython script to exit immediately. If this is called from the 'Run' script, then the current task execution will still succeed from the engine's perspective.

string getVarVal(string variablename)

Gets the value of a variable. This could be a solution or global variable. If there are both solution and global variables defined with the name supplied, then the value of the solution variable will be returned.

Do not include $ or ${} to reference the variable name, as you would when referencing variables in elsewhere in the product.

If the given variable name does not exist, an exception will be raised.

Example:

engine.getVarVal('sender')

might return "LAB".

string subVarVals(string str)

Given a string containing references to variables, this function substitutes the variable names with their values. Each variable name can be either a solution or global variable. If there are both solution and global variables defined with the name supplied, then the value of the solution variable will be returned.

Unlike getVarVal(), for this function you will need to include $ or ${} to reference the variable names, as you would when referencing variables in elsewhere in the product.

Also unlike getVarVal(), if the given variable name does not exist, then the original variable reference will appear in the return value unmodified. No exception will be raised by this case.

Example:

engine.subVarVals('sender: ${sender}, receiver: ${receiver}')

might return "sender: LAB, receiver: DATABASE"