Skip to content

Commit e49255f

Browse files
author
Jill Grant
authored
Merge pull request #277235 from ecfan/rules-engine
New docs for Azure Logic Apps Rules Engine (Integrate 2024, , 6/7 3 PM)
2 parents fa7fb8d + 6d831ce commit e49255f

36 files changed

+3200
-4
lines changed

articles/logic-apps/rules-engine/add-rules-control-functions.md

Lines changed: 553 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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)
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
---
2+
title: Build fact creators and retrievers
3+
description: Learn how to build fact creators and retrievers to develop and test rules in 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 to build fact creators and retrievers so that I can test my rules and rulesets in Microsoft Rules Composer and use those rulesets with my Azure Logic Apps Rules Engine project.
13+
---
14+
15+
# Build fact creators and retrievers to use with rules and rulesets (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+
To create facts for your rules engine to use during business rules development and testing, you can build a *fact creator*, which provides your engine with an array of .NET objects. You can also build a *fact retriever* that inserts long-term or slowly changing facts into your rules for evaluation during execution.
24+
25+
This how-to guide shows how to build a fact creator and fact retriever for your Azure Logic Apps Rules Engine project to use.
26+
27+
## Build a fact creator
28+
29+
To create your fact instances, implement the **IFactCreator** interface and its methods, **CreateFacts** and **GetFactTypes**. After you build the .NET assembly (DLL file) for your first fact creator, you can select the assembly from the ruleset testing capability in Microsoft Rules Composer. For more information, see [Test rulesets](test-rulesets.md).
30+
31+
The following example shows a sample fact creator implementation:
32+
33+
```csharp
34+
public class MyFactCreator : IFactCreator
35+
{
36+
private object[] myFacts;
37+
public MyFactCreator()
38+
{
39+
}
40+
41+
public object[] CreateFacts ( RuleSetInfo rulesetInfo )
42+
{
43+
myFacts = new object[1];
44+
myFacts.SetValue(new MySampleBusinessObject(),0);
45+
return myFacts;
46+
}
47+
48+
public Type[] GetFactTypes (RuleSetInfo rulesetInfo)
49+
{
50+
return null;
51+
}
52+
}
53+
```
54+
55+
## Build a fact retriever
56+
57+
A fact retriever is an .NET object that implements standard methods and typically uses them to supply long-term and slowly changing facts to the rules engine before the engine executes the ruleset. The engine caches these facts and uses them over multiple execution cycles. The fact retriever submits the fact the first time and then updates the fact in memory only when necessary. Rather than submit a fact each time that you invoke the rules engine, create fact retriever that submits the fact the first time, and then updates the fact in memory only when necessary.
58+
59+
To supply fact instances to your rules engine, implement the **IFactRetriever** interface and the **UpdateFacts** method. You can then set up your ruleset version to use this implementation to bring in facts at run time. Your ruleset version then calls the **UpdateFacts** method at every execution cycle.
60+
61+
Optionally, you can implement the **IFactRemover** interface on a fact retriever component. The rules engine can then call the method named **UpdateFactsAfterExecution** from the **IFactRemover** interface when the ruleset is disposed. That way, you can do any post-execution work, such as committing any database changes or retracting any object instances from the rules engine's working memory.
62+
63+
You can design your fact retriever with the required application-specific logic to perform the following tasks:
64+
65+
1. Connect to the required data sources.
66+
67+
1. Assert the data as long-term facts into the engine.
68+
69+
1. Specify the logic to refresh or assert new long-term fact instances into the engine.
70+
71+
The engine uses the initially asserted and cached values on subsequent execution cycles until those values are updated.
72+
73+
The fact retriever implementation returns an object that is analogous to a token that the retriever can use with the **factsHandleIn** object to determine whether to update existing facts or assert new facts. When a ruleset version calls the fact retriever for the first time, the **factsHandleIn** object is always set to null, but takes on the return object's value after the fact retriever completes execution.
74+
75+
The following sample code shows how to assert .NET and XML facts using a fact retriever implementation:
76+
77+
```csharp
78+
using System;
79+
using System.Xml;
80+
using System.Collections;
81+
using Microsoft.Azure.Workflows.RuleEngine;
82+
using System.IO;
83+
using System.Data;
84+
using System.Data.SqlClient;
85+
namespace MyApplication.FactRetriever
86+
{
87+
public class myFactRetriever:IFactRetriever
88+
{
89+
public object UpdateFacts(RuleSetInfo rulesetInfo, RuleEngine engine, object factsHandleIn)
90+
{
91+
object factsHandleOut;
92+
if (factsHandleIn == null)
93+
{
94+
// Create .NET object instances.
95+
bookInstance = new Book();
96+
magazineInstance = new Magazine();
97+
98+
// Create XML object instance.
99+
XmlDocument xd = new XmlDocument();
100+
101+
// Load the XML document.
102+
xd.Load(@"..\myXMLInstance.xml");
103+
104+
// Create and instantiate a TypedXmlDocument class instance.
105+
TypedXmlDocument doc = new TypedXmlDocument("mySchema",xd1);
106+
107+
engine.Assert(bookInstance);
108+
engine.Assert(magazineInstance);
109+
engine.Assert(doc);
110+
factsHandleOut = doc;
111+
}
112+
else
113+
factsHandleOut = factsHandleIn;
114+
return factsHandleOut;
115+
}
116+
}
117+
}
118+
```
119+
120+
To include the following capabilities, write your own code implementation:
121+
122+
- Determine when to update the long-term facts.
123+
124+
- Track whichever rules engine instance uses whichever long-term facts.
125+
126+
## Specify a fact retriever for a ruleset
127+
128+
To set up fact retriever for your ruleset version, you can either [set the Fact Retriever property in the Microsoft Rules Composer](perform-advanced-ruleset-tasks.md#set-up-fact-retriever), or write your own code as shown in the following example, which uses a class named "MyFactRetriever" in the assembly named "MyAssembly":
129+
130+
```csharp
131+
RuleEngineComponentConfiguration fr = new RuleEngineComponentConfiguration("MyAssembly", "MyFactRetriever");
132+
RuleSet rs = new RuleSet("ruleset");
133+
134+
// Associate the execution configuration with a ruleset version.
135+
RuleSetExecutionConfiguration rsCfg = rs.ExecutionConfiguration;
136+
rsCfg.FactRetriever = factRetriever;
137+
```
138+
139+
> [!NOTE]
140+
>
141+
> If you use a simple generic assembly name, such as "MyAssembly", as the
142+
> first parameter for the **RuleEngineComponentConfiguration** constructor,
143+
> the rules engine looks for the assembly in the application folder.
144+
145+
## Related content
146+
147+
- [Create rules with the Microsoft Rules Composer?](create-rules.md)
148+
- [Test rulesets](test-rulesets.md)
149+
- [Create an Azure Logic Apps Rules Engine project](create-rules-engine-project.md)

0 commit comments

Comments
 (0)