All product information in wiki.bizagi.com is only valid for Bizagi BPM Suite 9.1.X.
For newer Bizagi BPM Suite versions (10.X and up) please visit the User Guide.
 

Iterate over Fact or XPath

From Business Process Management, BPM and Workflow Automation Wiki | BizAgi BPMS

Jump to: navigation, search

<keywords content="keywords"> iterate, iterate over, iterateover, iterateoverfact, iterateoverXpath, relations, count records, modify records, relationship, relationships </keywords> 

Contents

Iterate over Xpath

This module allows you to carry out iterations (or cycles) over an XPath expression that has a collection, that is, a one-to-many relationship.

This module allows to access each record of the collection individually. You access one by one each record, and you can manipulate (get or set data) then individually.

For example, there is a purchase request that has many products. Then, the entity Request has a collection of Products. With Iterate over Xpath you go through each and every product included in the request.

This module not only allows you to access each of the entries in the relationship to evaluate them, but it also allows you to carry out operations such as modifying entries, or performing calculations and validations with them.


Image:Business_Rules8_Image021.jpg



Image:Bulletrojo.gif Edit Description: allows the user to give a name to the Iterate Module.

Image:Bulletrojo.gif Properties: allows the user to configure the cycle.

Image:Bulletrojo.gif Remove: this option allows the user to delete the entire module.


Add an Iterate over an XPath

1. First you need to create a variable (in the grren button on top of the expression). Create a variable to use for the iteration, in the green button. Through this variable you will have access to each record of the collection.

Right click over the button and create a variable. You should name it in such a way that you can know that is the collection you are accessing.

Use an Object type and initialize with null.


2. Then, add the Itrate over XPath module.

Right click on the vertical line of the rule and select the Add Iterate Over XPath option; the Iterate Over XPath – Description window appears to name it.




3.Right click (or double click) on the Iteration module and select the Properties option to configure it. The Iterate Over XPath window will be displayed.

4.The following parameters are included in the Iterate Over XPath window:


Image:Business_Rules8_Image022.jpg


Variable: In this field select the variable created in step 1. Remember this is how you will access each entry in the XPath collection.

XPath Base. This will be autofilled with Me.Context

Xpath: enter the Xpath expression with the collection. That is, select the Xpath from the business entity to the collection. In this field you can enter a filter to restrict the values that will be accessed.




5.Click on Ok when all parameters are included.

Get or set values to the collection

Once the Iterate over XPath has been configured you can access each record through the variable declared.


To get a value of an attribute, you can use:

Variable.getXPath(“Attribute”)

Variable: The name of the variable used for the Iterate Over XPath cycle

Attribute: Name of the attribute in the collection entity.


To set a value to a record, you can use:

Variable.setXPath(“Attribute”,value)

Variable: The name of the variable used for the Iterate Over XPath cycle

Attribute: Name of the attribute in the collection entity.

Value: The value you want to assign to the attribute

Iterate over XPath example

Description: In a disbursement sub-process of a Loan Request, all the approved loans that require disbursement from the same Request are disbursed together. Each loan has an approved value and an expense value that must be deducted at the time of disbursement. A rule must be created to calculate the value to be disbursed for each loan.


This is how the data model for the process looks like:


Image:Business_Rules8_Image023.jpg


This is how the expression should look like, including the variables that should be declared. The iterate over XPath module will go over each of the loans that were approved in the case where the expression is executed.



The Iterate Over XPath window in the expression described above will contain the following properties.

- The Variable: Products, declared in the green button on the top of the rule.

- XPath Base: Included by default

- XPath: the collection, in this case all the products of the request filtered by those who need disbursement. Request.Products[RequiresDisbursement=true]



The expression Calculate Disbursement Value is added inside the iteration, to access the record's information. In it we will calculate the values for each loan as shown below:

Each product is accessed using the Variable, in this case the variable Products. Using getXPath("Attribute") or setXPath("Attribute",Value)  the user can manipulate each record. The first expression will substract the Value approved in the product from the expenses.

The second expression will take this subtraction and set it in the ValueDisbursement of that product.



This is how the results will look like in the web application.



Validation Example

Description: In a Loans request case, a message is shown in the user interface when the value approved for each product is greater than the value requested.


1. Declare a variable to be used to go through each record (in this case Products). Also declare ValueApproved and ValueRequested to set some information in them and make calculations.

2. Add Iterate over an XPath as shown below.




3. Right click on the Iteration box. Select the variable declared and the XPath. In this case all the products of the request.




4. Create the expression to return a message error



Iterate Over XPath using an expression

In some cases you could need to handle Collections in expressions without using the Iterate over Xpath functionality to allow a complete manipulation of the code. It can be done by using the advanced function GetValueAsCollection of the CHelper class.

When you obtain an object from an Collection through the getXPath function, you will obtain a list of values stored as a text chain. You will no be able to go through those values and manipulate them until you obtain an array list. In order to do it you need to use the GetValueAsCollection function in the following manner:

//Obtain XPath
var List=<Process.Collection>
//Obtain array List by using CHelper
Array=CHelper.GetValueAsCollection(List)
//Iterate
for (var I=0; I < Array.size(); I++) 
{
//Manipulate information (Get, Set)
var TempVariable= Array.get(I); 			
var DesiredValue=TempVariable.getXPath("Object") 
}

The expression shown above obtains the values of a collection in a single variable, then that variable is converted to an array list by using the GetValueAsCollection function. Once it has been done you can go through the values of the collection by iterating with a FOR function.

Let´s take the Purchase Process as example. Suppose you have a collection called PRODUCT when you store the information of the products to be purchased, you wish to obtain the total price of the purchase without using the Iterate Over Xpath functionality. The information you have is as follow:



The expression you need is:

//Obtain XPath
var List=<Purchase.Product>
//Obtain array List by using CHelper
var Array=CHelper.GetValueAsCollection(List)
//Iterate
for (var I=0; I < Array.size(); I++) 
{
//Manipulate information (Get, Set)
var TempVariable= Array.get(I); 			
var Price=TempVariable.getXPath("Price") 
var Total=Total+Price
}
<Purchase.TotalPrice>=Total

And you will obtain:


Iterate over Fact

This module can be used to carry out iterations or cycles over a one-to-many relationship EXCLUSIVELY when the relationship cannot be accessed directly through the data model using XPath.

This module allows you to access each of the entries in the one-to-many relationship (also known as a fact) to evaluate them, to carry out operations such as modifying the entries, deleting entries from the relationship, searching for information, counting entries, etc.

The most common example of this is accessing a collection, or one-to-many relationship between Parameter entities. For example, the parameter entity Supplier has a collection of Products that it supplies. Then, if the user wants to iterate the Products of the Supplier, both being Parameter entities with no relation to the case's information, the user has to user Iterate Over Fact.




Image:Business_Rules8_Image002.jpg


Image:Bulletrojo.gif Edit Description: allows the user to change the name given to the diagram.

Image:Bulletrojo.gif Properties: allows the user to establish the cycle.

Image:Bulletrojo.gif Remove: This option allows the user to eliminate the entire module.


Add an Iterate Over Fact

1. Create an object variable to use for the iteration, in the green button. Right click over it and include a variable.

2. Right click on the vertical line of the rule and select the Add Iterate Over Fact option; the Iteration Over Fact-Description window will pop up for you to name the module




3. Right click on the Iteration module and select the Properties option to establish its characteristics. The Iteration Over Fact window will then pop up.



4.The Iterate Over Fact window includes the following parameters:

Image:Business_Rules8_Image003.jpg


Variable for iterate: In this field, select the variable created in step 1. Each record of the many entity that belongs to the relationship will be accessed.

Entity: In this field, select the Entity that contains the relationship (Entity 1).

Fact: Choose the relationship over which you would like to make the iteration or cycle. All the relationships that belong to the entity selected in the previous field are available.

Key: The foreign key from the one entity of the relationship. It must always be an integer: it could be <Attribute.id> or a variable containing the id.


5.Click on Accept or Apply as necessary.


Note: Starting from Bizagi V.9.0, this module should only be used to go through relationships that cannot be accessed directly with XPath expressions.



Get or Set the value of an attribute

Image:Bulletazul.gif To get or set the value of an attribute, you can use:


Variable.Attributes[“Attribute”].Value

Variable: The name of the variable used for the Iterate Over Fact cycle

Attribute: Name of the attribute in the collection entity.



Itarate over fact example


Related Topics


<comments />