Notification

From FlexRule Wiki
Jump to: navigation, search

Introduction

Notification is a way of collecting some feedback from engine while is running. There might be different reasons to collect feedbacks:

  • Visibility to execution
  • Log, trace...
  • Checking some results
  • Sending feedback to host

and so on...

A notification can be accessed via Notification commands in all types of engines (e.g. flow, procedural, workflow...). However in validation engine, direct access is not possible but you can use message setting of Validation commands to record the result inside notification.

Notice

Notice is a message that will be placed into a notification group. By default if the group is not defined will will be stored in a default group.

Grouping

Notifications can be grouped by a name. Grouping allows you to organize notices in different Notifications. Then each group of notices (Notification) may serve a particular purpose in your specific scenario. For example you can separate notices from UI response than from Audit messages with putting them in different Notification.

Cross-engine notification

In an execution, Notification will be shared across all the engines. The rule that has initiative, would pushes its Notifications tray to all the underlying rules. For example if you have a Flow that has a Validation and a Procedure, then NotificationSet from the parent (Flow) will be shared with those Validation and Procedural rules and they can create and maintain their own Notifications.

This behaviour allows your application to collect all the notices related to its execution.

Formatting messages

Notices in notification can be formatted in the way you need to. You can put placeholders to be filled by the values of the context as they are available. To define a placeholder you can put an expression between { and } and the value from context will be replacing those placeholders.

<Notice message="your age {person.Age} is less than 18" type="error"/>

Accessing Notification

In your code, once you executed your rule, you can collect notifications from your execution context or Runtime Result.

For example consider following Validation logic:

  1. <Validation name="product check">
  2.   <Declaration>
  3.     <Define name="product" direction="in"/>
  4.  
  5.     <Define name="total" direction="local"/>
  6.     <Define name="validPoint" direction="local"/>
  7.   </Declaration>
  8.   <Logic name="main">
  9.     <And>
  10.       <Null negate="true" value="product" message="product not exists"/>
  11.       <Var name="total" value="product.Quantity * product.Point"/>
  12.       <And processAll="true">
  13.         <Var name="validPoint" value="total>=20"/>
  14.         <Check value="validPoint" message="un-authorized"/>
  15.         <Check value="!validPoint" message="authorized" noticeFor="success" notice="information"/>
  16.       </And>
  17.     </And>
  18.   </Logic>
  19. </Validation>

Then the logic is executed, you can collect the notifications like the code bellow:

  1. [TestMethod]
  2. public void expect_unauthorized_when_point_lessOrEqual_than_20()
  3. {
  4.     var product = new Product() { };
  5.  
  6.     var content = File.ReadAllBytes("product_access.xml");
  7.     var engine = RuntimeEngine.FromXml(content);
  8.     var res = engine.Run(product);
  9.  
  10.     // Retrieve list of notification from the result
  11.     var list = res.Notifications.Default.Notices.Where(x => x.Type == NoticeType.Error).ToList();
  12.     Assert.AreEqual(1, list.Count);
  13.     Assert.AreEqual("un-authorized", list.First().Message);
  14. }

Notification API

You can define notification across your logic, to learn more please check its rule api.

Notification in Decision Table

In any Decision table you can write notices as part of success or failure to meet conditions. Because the way decision tables work, its API for notifications are different from other types of logic e.g. validation, flow, procedural...

Notification in decision tables work based on action column with Notice type. What you need to do is to set the type of action to Notice. And if you need to set the type of notice you can set notice attribute values. The template of message comes from the data cells. Check the API for more details

Example

Scenario: If there is a duplicate in a list of people , write error notification. The decision table bellow writes the notification with a template of a message:

Duplicate-notice-DT.png

In your FlexRule Designer the properties of that action column named Write Error Message is:

Duplicate-notice-action-DT.png

Here is the example of the table definition:

  1. <DecisionTable name="Identify duplicates in list">
  2.   <Declaration>
  3.     <Define name="persons" direction="In" />
  4.     <Define name="duplicates" direction="Local" />
  5.   </Declaration>
  6.   <Columns>
  7.     <Condition name="Has Duplication" expression="duplicates.Count gt 0" />
  8.     <Action name="Write Error Message" expression="$value" type="Notice" onArray="duplicates" elementName="$x" />
  9.     <Action name="Create Duplicate List" expression="duplicates = $value |groupBy(p, p.Name) |where (g, g.Count&gt;1) |select(d, d.Key)" />
  10.     <Name />
  11.   </Columns>
  12.   <Data>
  13.     <Row>
  14.       <Value/>
  15.       <Value/>
  16.       <Value>: persons</Value>
  17.       <Value>Building Duplicates</Value>
  18.     </Row>
  19.     <Row>
  20.       <Value>true</Value>
  21.       <Value>Person named '{$x}' is duplicated in list.</Value>
  22.       <Value/>
  23.       <Value>Duplicate Check</Value>
  24.     </Row>
  25.   </Data>
  26. </DecisionTable>

If you need multiple notices in your notification, then simply add multiple actions and you can set notice property for having error, warning or information type of message.

Please note, because we want to apply this action on all elements of the collection named people we set the onArray and elementName of the action.

Functions

There are also some functions dealing with notifications if you need them at the expression level.

For more details visit Notification functions