Using Rule Editor

Rule Editor is the client-side tool that is used by rule authors to create new business rules or load existing ones for modification, validation, or deletion. Both the Asp.NET and MVC components of Code Effects have their own RuleEditor class that can be used programmatically or declaratively to add Rule Editor to the page or view.

UI Elements

Rule Editor consists of three elements:

  • Toolbar is an optional UI element that sits on top of Rule Editor and helps to manage rules, and is enabled by default. Set the value of RuleEditor.ShowToolBar to False to remove it from the UI. This is an important piece of Code Effects' UI that deserves its own documentation topic.

  • Help String is an optional line of text that appears between the Toolbar and Rule Area, which helps the rule author to navigate through the rule elements as (s)he creates or modifies the rule. It also warns users about any kind of failure or exception. By default, this element is turned on and displays default English messages. Set the value of RuleEditor.ShowHelpString to False to remove it from the UI. All messages displayed by the Help Sting can be modified or even translated into other languages by utilizing the Help XML feature of Code Effects.

  • Rule Area is the panel at the bottom of Rule Editor that holds all elements of the business rule that the rule author is currently working on. Unless noted otherwise, the rest of this topic discusses the use of this Code Effects element and its features.

You can change all default UI labels and messages to your own values by using the Help XML feature of the Rule Editor.

Rule Editor can operate in one of four modes, and it is very important that you understand what the modes are and how they operate.

Keyboard Support

The easiest way to add rule elements, select menu items, or navigate through an existing rule is to use your keyboard. Here is the list of supported keys and descriptions of what they do:

  • Space bar: Brings up a context menu related to the currently selected rule element.
  • Left, Right, Up, and Down Arrow keys: If any context menu is displayed, the Up and Down keys move the selection from the current item to its sibling. If no menu is shown, the arrow keys move the selection from the currently selected element to its left, right, upper, or lower sibling.
  • Enter key:
    • If any menu is displayed, selects the currently highlighted item.
    • If a value is being edited, finalizes the input.
    • Otherwise, inserts a new line.
  • Home key: Moves the selection to the most left rule element in the current line.
  • End key: Moves the selection to the most right rule element in the current line.
  • Page Up key: Moves the selection to the If flow element, which is always the first rule element.
  • Page Down key: Moves the selection to the very last element of the currently displayed rule.
  • Delete key: Deletes the currently selected rule element and moves the selection to its previous sibling.
  • Backspace key: Deletes the previous sibling of the currently selected rule element.
  • Esc key: Hides context menu, date picker, or time picker if any of these tools are visible.
  • Tab key: If the very first empty element of a line is selected, inserts a new empty "tab" and moves the selection to this new element.

Usage

When you first load a page with Rule Editor on it, the Rule Area will contain no rule elements, displaying only a default instructional message. Click inside the Rule Area, and select any field from the context menu. Note that Rule Editor now contains the initial if flow element and the field that you just selected. Now, select an operator from the next menu, enter the value of the condition, select a clause, and so on, and so forth. If enabled, the Help String displays dynamic instructions on what to do next.

As you can see, the Rule Editor allows you to build business rules of any complexity with no predefined UI controls to deal with or complicated formats to follow. To create a new rule, select the corresponding option from the Toolbar's Rules menu, add rule conditions to the Rule Area, name the rule, and save it. To load the existing rule for editing, select its name from the same Rules menu (assuming that your code loaded that rule in the menu when the page was loading.) (Re)name the rule, optionally describe it, and click the Save button to save the changes. Click the Delete button to delete the currently loaded rule. (The Delete button appears in the Toolbar only when an existing rule is loaded)

Parentheses

One of the most popular features of Code Effects' UI is its use of parentheses to set the order in which rule conditions are evaluated. Most traditional business rules engines require rule conditions to be manually combined into a large ruleset, with evaluation order/priority/salience being set for each individual condition. Code Effects component changes all that; its parentheses work intuitively:

Parentheses appear as the first item in relevant context menus. Code Effects component knows when to offer the opening parenthesis and where the closing one could be.

Note that the built-in rule validation (described in the next section) enforces the logical consistency of or and and clauses that "connect" conditions inside of a single rule level. This sounds complicated, but, in reality, it's not. You can get details by reading the Code Effects Basics topic (look for the definition of Clauses there), but in simple terms, you cannot have different clauses on the same level (a level is a group of conditions separated by a single pair of parentheses). Examples:

NO:    Check if ( A is B and C is not D or E is less than F )
YES:    Check if ( A is B and C is not D and E is less than F )
YES:    Check if  (A is B or C is not D or E is less than F )

Most modern compilers and script interpreters evaluate the line if ( A == B && C != D || E < F ) to either True or False depending on the rules that they enforce internally, while Code Effects component prefers not to confuse rule authors. Logically speaking, levels with different clauses are invalid, therefore, the rule validation treats such levels as invalid. Obviously, if a rule must have conditions joined by different clauses, the rule author can simply place each clause in its own level:

YES:    Check if ( A is B and ( C is not D or E is less than F ) )

Automatic Rule Validation

Code Effects component comes with automatic rule validation. Every time a rule author tries to save or update a rule, Code Effects component forces it through its internal validation process. All your code has to do is to check if the value of the RuleEditor.IsValid property is True before continuing to save it:

if (ruleControl.IsEmpty || !ruleControl.IsValid)
{
	// You don't really need to do anything if the rule is invalid, the Help String
	// will display a proper error message demonstrated in the screenshot below

	return;
}

If the current rule is invalid, the Help String displays a warning message and the Rule Editor highlights each invalid element, allowing the rule author to hover the mouse over it to see the description of that particular problem.

Post your support requests on Stackoverflow.com. You can also post your comments and product feedback using the form at the bottom of this page.
Comments: 0
Name (optional):
Comment (URLs are allowed and must start with http:// or https://; all tags will be encoded):
Remaining character count: