|
| 1 | +--- |
| 2 | +title: Add arithmetic and logical operators to rules |
| 3 | +description: Learn how to add arithmetic and logical operators to the rules in your ruleset using the Microsoft Rules Composer. |
| 4 | +ms.service: logic-apps |
| 5 | +ms.suite: integration |
| 6 | +author: haroldcampos |
| 7 | +ms.author: hcampos |
| 8 | +ms.reviewer: estfan, azla |
| 9 | +ms.topic: how-to |
| 10 | +ms.date: 06/10/2024 |
| 11 | + |
| 12 | +#CustomerIntent: As a developer, I want to understand how use arithmetic and logic operators in the rules that I create for my Azure Logic Apps Rules Engine project. |
| 13 | +--- |
| 14 | + |
| 15 | +# Add arithmetic and logical operators to rules using Microsoft Rules Composer (Preview) |
| 16 | + |
| 17 | +[!INCLUDE [logic-apps-sku-standard](~/reusable-content/ce-skilling/azure/includes/logic-apps-sku-standard.md)] |
| 18 | + |
| 19 | +> [!IMPORTANT] |
| 20 | +> This capability is in preview and is subject to the |
| 21 | +> [Supplemental Terms of Use for Microsoft Azure Previews](https://azure.microsoft.com/support/legal/preview-supplemental-terms/). |
| 22 | +
|
| 23 | +This guide describes how to add arithmetic and logical operators to the rules in your ruleset using the Microsoft Rules Composer. |
| 24 | + |
| 25 | +## Prerequisites |
| 26 | + |
| 27 | +- Download and install the [Microsoft Rules Composer](https://go.microsoft.com/fwlink/?linkid=2274238). |
| 28 | + |
| 29 | +- The XML file that contains the ruleset that you want to work on. |
| 30 | + |
| 31 | +<a name="add-arithmetic-operator"></a> |
| 32 | + |
| 33 | +## Add an arithmetic operator to a rule |
| 34 | + |
| 35 | +You can add an arithmetic operator to a condition or action in a rule. The following table describes the available arithmetic operators: |
| 36 | + |
| 37 | +| Arithmetic operator | Description | |
| 38 | +|---------------------|-------------| |
| 39 | +| **Add** | The addition operator that adds *arg1* to *arg2*. | |
| 40 | +| **Subtract** | The subtraction operator that subtracts *arg1* from *arg2*. | |
| 41 | +| **Multiply** | The multiplication operator that multiplies *arg1* by *arg2*. | |
| 42 | +| **Divide** | The division operator that divides *arg1* by *arg2*. | |
| 43 | +| **Remainder** | The remainder operator that performs *arg1* modulo *arg2*. | |
| 44 | + |
| 45 | +1. In the **Microsoft Rules Composer**, load the XML file that contains the rule store you want to work on. |
| 46 | + |
| 47 | +1. In the **RuleSet Explorer** window, find and select the rule that you want. |
| 48 | + |
| 49 | +1. In the **Facts Explorer** window, select the **Vocabularies** tab. |
| 50 | + |
| 51 | +1. Under **Vocabularies** > **Functions** > **Version 1.0**, drag the arithmetic operator that you want to an argument in a condition in the conditions editor or an action in the actions editor. |
| 52 | + |
| 53 | +1. In the condition or action arguments, specify the values for left and right operands. |
| 54 | + |
| 55 | + - If the operands have different types, the rules engine performs automatic numeric promotion where the engine converts the smaller operand type to the larger operand type. |
| 56 | + |
| 57 | + For example, suppose you use the **Add** operator on an operand that has **int** type and an operand that has **long** type. Before the engine performs the **Add** operation, the engine converts the **int** type to the **long** type. |
| 58 | + |
| 59 | + - If the engine can promote both operands to a common type, the engine supports double promotion. |
| 60 | + |
| 61 | + For example, suppose you use the **Add** operator on an operand that has **int** type and an operand that has **uint** type. Before the engine performs the **Add** operation, the engine converts both operand types to the **long** type. |
| 62 | + |
| 63 | +<a name="add-logical-operator"></a> |
| 64 | + |
| 65 | +## Add a logical operator to a rule |
| 66 | + |
| 67 | +You can add a logical operator to a predicate in a condition. The following table describes the available logical operators: |
| 68 | + |
| 69 | +| Logical operator | Description| |
| 70 | +|------------------|------------| |
| 71 | +| **AND** | Combine two or more predicates to form a logical **AND** expression. Returns **true** if both predicates evaluate to **true**. Otherwise, returns **false**. | |
| 72 | +| **OR** | Combine two or more predicates to form a logical **OR** expression. Returns **true** if one predicate evaluates to **true**. Otherwise, returns **false**. | |
| 73 | +| **NOT** | Negate a logical expression or predicate. Returns **true** if the predicate evaluates to **false**. Otherwise, returns **false**. | |
| 74 | + |
| 75 | +1. In the **Microsoft Rules Composer**, load the XML file that contains the rule store you want to work on. |
| 76 | + |
| 77 | +1. In the **RuleSet Explorer** window, find and select the rule that you want. |
| 78 | + |
| 79 | +1. In the **IF** pane, which is the conditions editor, from the **Conditions** shortcut menu, select one of the following commands: |
| 80 | + |
| 81 | + | Logical operator | Description | |
| 82 | + |------------------|-------------| |
| 83 | + | **Add logical AND** | Combine two or more predicates to form a logical **AND** expression. | |
| 84 | + | **Add logical OR** | Combine two or more predicates to form a logical **OR** expression. | |
| 85 | + | **Add logical NOT** | Negate a logical expression or predicate. | |
| 86 | + |
| 87 | +1. In the conditions editor, open the operator's shortcut menu, and add the predicates or nested logical operators that you want. |
| 88 | + |
| 89 | + If the operands have different types, the rules engine converts the type for one operand to match the type for the other operand, or converts the types for both operands to a common type before evaluating the expression. |
| 90 | + |
| 91 | +## Handle null values |
| 92 | + |
| 93 | +The following section describes the expected behaviors for null values associated with different types and provides options for checking null or the existence of a specific field or member. |
| 94 | + |
| 95 | +### .NET classes |
| 96 | + |
| 97 | +- For types derived from the **Object** type, you can set their fields to null. |
| 98 | + |
| 99 | +- You can pass null as an argument for parameters that aren't value types, but you might get a runtime error, based on the member's implementation. |
| 100 | + |
| 101 | +- You can't use null for comparison if the return type isn't an **Object** type. |
| 102 | + |
| 103 | +### XML elements |
| 104 | + |
| 105 | +- An XML document never returns an XML value as null. Instead, this value is either an empty string or a "doesn't exist" error. For an empty string, an error might occur for the conversion of certain types, such as fields specified as an integer type when you build a rule. |
| 106 | + |
| 107 | +- The Microsoft Rules Composer doesn't allow you to set a field to null or to set a field type to **Object**. |
| 108 | + |
| 109 | +- Through the object model, you can set the type to **Object**. In this case, the returned value has the type to which the XPath evaluates, such as **Float**, **Boolean**, or **String**, based on the XPath expression. |
| 110 | + |
| 111 | +### Check for null or existence |
| 112 | + |
| 113 | +When you write rules, you naturally want to check that a field exists before you compare its value. However, if the field is null or doesn't exist, comparing the value causes an error. |
| 114 | + |
| 115 | +For example, suppose you have the following rule: |
| 116 | + |
| 117 | +`IF Product/Quantity Exists AND Product/Quantity > 1` |
| 118 | + |
| 119 | +If **Product/Quantity** doesn't exist, the rule throws an error. To work around this problem, you can pass a parent node to a helper method that returns the **Product/Quantity** value if that element exists, or return something else if that element doesn't exist. |
| 120 | + |
| 121 | +The following example shows the updated and new helper method rule: |
| 122 | + |
| 123 | +**Rule 1** |
| 124 | + |
| 125 | +`IF Exists(Product/Quantity) THEN Assert(CreateObject(typeof(Helper), Product/Quantity))` |
| 126 | + |
| 127 | +**Rule 2** |
| 128 | + |
| 129 | +`IF Helper.Value == X THEN...` |
| 130 | + |
| 131 | +As another possible solution, you can create a rule such as the following example: |
| 132 | + |
| 133 | +`IF Product/Quantity Exists THEN CheckQuantityAndDoSomething(Product/Quantity)` |
| 134 | + |
| 135 | +In the preceding example, the `<CheckQuantityAndDoSomething>` function checks the parameter value and executes if the condition is met. |
| 136 | + |
| 137 | +> [!NOTE] |
| 138 | +> |
| 139 | +> Alternatively, you can modify the **XPath Field** property for the |
| 140 | +> XML fact to catch any errors, but this approach isn't recommended. |
| 141 | +
|
| 142 | +## Related content |
| 143 | + |
| 144 | +- [Create rules with the Microsoft Rules Composer](create-rules.md) |
| 145 | +- [Add control functions to actions for optimizing rules exection](add-rules-control-functions.md) |
| 146 | +- [Test your rulesets](test-rulesets.md) |
| 147 | +- [Create an Azure Logic Apps Rules Engine project](create-rules-engine-project.md) |
0 commit comments