Skip to content
Gabe Stocco edited this page Aug 4, 2020 · 20 revisions

Object Anaylsis Toolkit (OAT) is a rules processing engine to apply provided rules against arbitrary objects

  • Rules contain a Target, a Severity, a boolean Expression and a List of Clauses which are applied to the targeted object.
  • Clauses perform a specified Operation on a specified Field of a Target. The Field can be any property or subproperty or field of the object specified with dot notation to separate levels.
  • The Analyzer has 4 Delegate extensibility points and the built-in operators can also be overridden with delegates.

Basic Usage

The basic usage of OAT is applying rules to targets using the Analyze function.

object target;
IEnumerable<Rule> rules;
var analyzer = new Analyzer();
var rulesWhichApply = analyzer.Analyze(rules,target);

Capturing

OAT also supports capturing the results of clauses.

object target;
IEnumerable<Rule> rules;
var analyzer = new Analyzer();
var res = analyzer.GetCaptures(rules, target);

Example

This sample from the the Walkthrough captures the weight of Overweight vehicles

var overweightTruck = new Vehicle()
{
    Weight = 30000,
    Capacity = 20000,
};

var analyzer = new Analyzer();
analyzer.SetOperation(OverWeightOperation);
var res = analyzer.GetCaptures(rules, overweightTruck);
// We get the First Rule capture and the First Clause Capture
// Since this is our custom operation on an int we know we should get back a TypedClauseCapture<int>
var weight = ((TypedClauseCapture<int>)res.First().Captures[0]).Result;

Assert.IsTrue(weight == 30000);

var overweightRule = new VehicleRule("Overweight Truck")
{
    Expression = "Overweight",
    Target = "Vehicle",
    Clauses = new List<Clause>()
    {
        new Clause(Operation.Custom)
        {
            Label = "Overweight",
            CustomOperation = "OVERWEIGHT",
            Capture = true
        }
    }
}

OatOperation OverweightOperation = new OatOperation(Operation.Custom, analyzer)
{
    CustomOperation = "OVERWEIGHT",
    OperationDelegate = OverweightOperationDelegate,
    ValidationDelegate = OverweightOperationValidationDelegate
};

public OperationResult OverweightOperationDelegate(Clause clause, object? state1, object? state2, IEnumerable<ClauseCapture>? captures)
{
    if (state1 is Vehicle vehicle)
    {
        var res = vehicle.Weight > vehicle.Capacity;
        if ((res && !clause.Invert) || (clause.Invert && !res))
        {
            // The rule applies and is true and the capture is available if capture is enabled
            return new OperationResult(true, clause.Capture ? new TypedClauseCapture<int>(clause, vehicle.Weight, state1, state2) : null);
        }
    }
    return new OperationResult(false, null);
}

public IEnumerable<Violation> OverweightOperationValidationDelegate(Rule r, Clause c)
{
    var violations = new List<Violation>();
    if (r.Target != "Vehicle")
    {
        violations.Add(new Violation("Overweight operation requires a Vehicle object", r, c));
    }

    if (c.Data != null || c.DictData != null)
    {
        violations.Add(new Violation("Overweight operation takes no data.", r, c));
    }
    return violations;
}

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Clone this wiki locally