How BizTalk Maps Work – The sequence of links (Part 3)

Posted: May 7, 2012 in BizTalk
Tags: , , , ,

“The order in which we perform the links between the elements from source to destination has a huge impact in the final result”. This statement is true and false at the same time!

In fact the order with which we associate links (Drag & Drop) from the source to different destination elements is irrelevant, since the compiler, as previously explained, will process them in the correct order… Except if we have to associate several links to the same destination element or functoid. In these two last cases the order in which the link association takes place is extremely important and can lead to unexpected results.

Impact of the order of links in functoids

The functoids require certain input parameters that can vary according to the functoid that we are using, in this case the order with which we associate the link is extremely important, a practical example is the Value Mapping Functoid.

This functoid returns the value of the second parameter if the value of the first parameter is “true”. If the value of the first parameter is not “true”, the corresponding element or attribute in the output instance message is not created. Therefore, it is necessary to respect the order in which we associate the links:

  • The first parameter must be a value “true” or “false”, generally from the output of some other Logical functoid or from a variable Boolean field in the input instance message.
  • The second is the value that is output if parameter 1 is “true”. This value can be from a link from a node in the source schema that represents simple content, the output from another functoid, or a constant input parameter.

If we change the order in which the links are associated to the functoid, it will lead to mapping errors or unexpected results, according to the functoid used.

The link reorganization in functoids is very easy to accomplish, you just open the functoid detail (double click) and use the sort buttons.

reorganizar-functoids

Impact of the order of links in elements of the destination schema

If we change the order in which we associate the links on the same element in the destination schema we can also have an impact on the desired final result.

Unfortunately, when we associated different links in the same element, there is no way or place in the graphical editor where you can check the order of the association, for example, similarly to what happens with the functoids. The only way to verify the order in these cases is to inspect the XSLT generated code or testing the map.

A good example of this scenario is when we associated two different Scripting functoid, both with custom inline XSLT scripts to the same destination element or record, once again, changing the order of the link association may have unexpected results.

Impact-order-links-in-elements-destination-schema

In this example the first Scripting functoid contain the following XSLT code:

<xsl:for-each select="Client">
   <Person>
      <Name>
         <xsl:value-of select="Name/text()" />
      </Name>
      <Sex>
         <xsl:value-of select="Sex/text()" />
      </Sex>
   </Person>
</xsl:for-each>

This code performs the mapping of all existing elements in the record “Client” from the source schema to the elements on the record “Person” in the destination schema.

The second Scripting functoid contain an identical XSLT code:

<xsl:for-each select="Employee">
   <Person>
      <Name>
         <xsl:value-of select="Name/text()" />
      </Name>
      <Sex>
         <xsl:value-of select="Sex/text()" />
      </Sex>
   </Person>
</xsl:for-each>

but this time it will map all existing elements in the record “Employee” from the source schema to the elements on the record “Person” in the destination schema.

The expected result is to appear in the final document all clients in the record “Person” and then all employees. If we change the order in which we association the links in the record “Person”, we will see that the result he also will be changed.

We can validate the result of this scenario in the following links:

The rule of Link Sequence

In brief, the mapping engine, process the rules by going through the destination schema from the beginning to the end, processing the links in the order that he finds and in case of multiple links in a particular element or functoid, they are processed in the order in which they were associated.

This means that the links associated with the parent nodes (records) are processed before links associated with the children (elements inside the record).

A good example of this scenario is the use of conditions in the parent node (record) when we want to influence the outcome according with a particular condition. So…let’s find all the names of female clients. To do this we will create a map with the following settings:

The-exception-rule-sequence-links

  • Open the Toolbox window and drag the Looping Functoid onto the grid;
    • Drag a link from the record “Client” from the source schema to the Looping Functoid;
    • Drag a link from the Looping Functoid to the record “Person” in the destination schema;
  • Drag the Equal Functoid from the Toolbox window onto the grid;
    • Drag a link from the element “Sex” from the source schema to the Equal Functoid;
    • Drag a link from the Equal Functoid to the record “Person” in the destination schema;
    • Configured the Equal Functoid, by double click in the functoid, and edit the second condition with the value “F” to build a condition equivalent to Sex = “F”;

sex-condition

The Equal Functoid will return the value “True” if the first input, in this case, the element “Sex” is equal to the second input parameter, ie, “F”. Otherwise it will return the value “False”. What will lead to: the record “Client” is only mapped if the condition returns the value “True”.

If you look the code generated:

…
<ns0:PeopleTarget>
   <xsl:for-each select="Client">
      <xsl:variable name="var:v1" select="userCSharp:LogicalEq(string(Sex/text()) , "F")" />
      <xsl:if test="$var:v1">
         <Person>
            <Name>
               <xsl:value-of select="Name/text()" />
            </Name>
            <Sex>
               <xsl:value-of select="Sex/text()" />
            </Sex>
         </Person>
      </xsl:if>
   </xsl:for-each>
</ns0:PeopleTarget>

We can check that the first action of the map, after the cycle that travels the various elements of the record is: get the value generated in the Equal Functoid, which is represented in the variable “v1″;

And the second action is to validate the condition (IF) with the value of the first operation (v1), ie, will test whether the value “v1″ is “True” or “False”. If the condition is true the code within the condition is executed, otherwise it will move to the next element without doing anything. Therefore thus we obtain the desired result:

<ns0:PeopleTarget xmlns:ns0="http://HowMapsWorks.PeopleTarget">
   <Person>
      <Name>Elsa Ligia</Name>
      <Sex>F</Sex>
   </Person>
</ns0:PeopleTarget>
The exception to the rule of Link Sequence: Process links out of order

However there is one important exception to this rule of Link Sequence, especially when using custom scripts in recursive records or elements.

Once again, a good example of this scenario is the use of custom scripts to increment counters. We can illustrate this scenario by adding two Scripting Functoids to the map:

  • The first containing the initialization and the function to increment the counter;
    int myCounter = 0;
    public void IncrementCounter()
    {
       myCounter += 1;
    }
    
  • The second getting the value of the counter
    public int ReturnCounter()
    {
       return myCounter;
    }
    

Note: This example will be associated with a loop, or with a recursive element.

The-exception-to-the-rule-of-Link-Sequence-Process-links-out of-order

We would expect that in the first cycle the result of the second script was the value “1”, in the second cycle we obtained the value “2” and so on. However, if we test the map, we will see that the reality is different:

<ns0:PeopleTarget xmlns:ns0="http://HowMapsWorks.PeopleTarget">
   <Person><LineNumber>0</LineNumber></Person>
   <Person><LineNumber>1</LineNumber></Person>
   <Person><LineNumber>2</LineNumber></Person>
</ns0:PeopleTarget>

As we can see in the result above, the sequence in which the links are executed is:

  • Create the record “PeopleTarget”;
  • Creating child elements, and carry out the link rules associated with them:
    • Execute the function “ReturnCounter” that will return the value “0” in the first iteration.
  • Execute the links associated with the parent node:
    • Execution of the function “IncrementCounter”

As we can validate by checking code produced by the map:

…
<ns0:PeopleTarget>
   <xsl:for-each select="Client">
      <Person>
         <xsl:variable name="var:v1" select="userCSharp:ReturnCounter()" />
         <LineNumber>
            <xsl:value-of select="$var:v1" />
         </LineNumber>
         <xsl:variable name="var:v2" select="userCSharp:IncrementCounter()" />
         <xsl:value-of select="$var:v2" />
      </Person>
   </xsl:for-each>
</ns0:PeopleTarget>

Of course we can change the existing code in the Scripting Functoids so we can get around to this behavior and thus obtain the desired result. However, this example serves to warn that in some scenarios, especially in the use of custom scripts in recursive records or elements, it is necessary to verify and validate the sequence in which the rules (links) are executed.

Conclusion

With this article, as we explore some of the common mappings scenarios, trying to dismantle the options that the BizTalk Map engine has taken to fulfill with the original intent of the visual map.

When you begin to explore the world of maps, there are two questions that should evaluate carefully

  • What is the best way to solve a problem: guaranteed there are several approaches to solve a common problem. Often deciding which the best way turns out to be the most difficult. Compile and analyze the code generated can be a good start to begin to understand the impact of certain options.
  • Incremental Testing: very often we are tempted to try to solve a mapping problem from start to finish and only then we test solution. Leave it to the end can make it extremely difficult to detect problems in complex mappings. Limit the scope of testing should be a continuous and incremental process during the creation of maps, tests must be carried out as soon as a significant block is completed.

I hope this kind of hacking can help you to understand the behavior and debugging techniques for this type of elementary problems.

Related links

BizTalk Server – Basics principles of Maps (318.0 KB)
Microsoft | MSDN Code Gallery

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s