Skip to content

Authoring Rules

Gabe Stocco edited this page Jul 20, 2020 · 24 revisions

OAT operates by checking if Rules apply to targeted objects. This page documents the format for specifying those rules.

Rule Basics

A Rule contains

  • Name string
  • Severity int
  • Description string (optional)
  • Clauses List<Clause> (optional)
  • Expression string (optional)
  • Target string - Name of the object to apply to (optional)

Rule matching

A Rule matches an Object when:

  • The Object's type maps to the specified Target OR Target is null
  • AND any of
    1. The rule has no Clauses
    2. The rule has no Expression AND all its Clauses are true
    3. The evaluation of the Expression is true

Clause Basics

A Clause contains:

  • An Operation - see Operations
  • A Field that the Clause applies to. This can be any property or field, or any sub-property or sub-field. If null the object itself is used.
  • A Label (required if using an Expression in the associated Rule). The Label may contain characters other than whitespace and parentheses.
  • List, Data as appropriate depending on the Operation.
  • Dictionary<string,string>, DictData as appropriate depending on the Operation.

Expressions

The Expression in a rule is a user specified boolean expression across the defined Clauses' Labels in the Rule.

These are some examples of Valid Expressions:

FOO
FOO AND BAR
FOO OR BAR NAND NOT BAZ OR (BAT123 XOR $B@A%N)

These are some examples of Invalid Expressions

FOO BAR OR     // Each clause must be separated by an operator
((FOO OR BAR ) // Parenthesis must be balanced
F(OO OR BA)R   // Clause Labels may not contain parenthesis

Expressions can be created using any of the standard boolean operators in addition to parenthesis.

OR
AND
NOR
NAND
XOR
NOT

Verifying Your Rules

To see if your rules are valid you can check if they have any violations. Each violation includes a message explaining the issue and where it is occurring.

var analyzer = new Analyzer();
var violations = analyzer.EnumerateRuleIssues(rules)

Rule Examples

Simple Rule

This is an example of a simple rule from the toll system demo in the walkthrough.

new VehicleRule("Normal Car"){
    Cost = 3,
    Severity = 1,
    Target = "Vehicle",
    Clauses = new List<Clause>()
    {
        new Clause("Weight", OPERATION.GT)
        {
            Data = new List<string>()
            {
                "1000"
            }
        }
    }
}

Rule With Expression

This is an example of a more complicated rule which uses an OR based Expression in order to match either a weight limit or a large number of axles.

new VehicleRule("Heavy or long")
{
    Cost = 10,
    Severity = 3,
    Expression = "Weight OR Axles",
    Target = "Vehicle",
    Clauses = new List<Clause>()
    {
        new Clause("Weight", OPERATION.GT)
        {
            Label = "Weight",
            Data = new List<string>()
            {
                "4000"
            }
        },
        new Clause("Axles", OPERATION.GT)
        {
            Label = "Axles",
            Data = new List<string>()
            {
                "2"
            }
        }
    }
},
Clone this wiki locally