Notification

From FlexRule Wiki
Jump to: navigation, search

Introduction

Notification is a way of collecting some feedback from the engine while it is running. There might be different reasons for collecting feedback:

  • 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, etc.). However, in a validation engine, direct access is not possible, but you can use the 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, it 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 UI response notices from Audit messages by putting them in different Notifications.

Cross-engine notification

In an execution, Notification will be shared across all of the engines. The rule that has initiative would push its Notifications tray to all of 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 in turn they can create and maintain their own Notifications.

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

Formatting messages

Notices in notification can be formatted as required. 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 replace those placeholders.

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

Accessing Notification

In your code, once you execute 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 Notifications across your logic. To learn more, please check rule api.

Notification in a Decision Table

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

Notification in Decision Tables works based on an action column with a Notice type. What you need to do is to set the type of action to Notice. If you need to define the type of notice you can set notice attribute values. The template of the 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 an error notification. The decision table shown below writes the notification using a message template:

Duplicate-notice-DT.png

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

Duplicate-notice-action-DT.png

Here is an 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 a notice property for generating an error, warning or information type of message.

Please note, because we want to apply this action to 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