From FlexRule Wiki
Jump to: navigation, search

Validate Natural Language

When you have an NL model you can validate the NL structure to ensure it is a valid NL. There are two ways to validate:

  1. Create an instance of an engine using RuntimeEngine
  2. Build the execution plan of NL using NaturalParser parser
Info2.png If your Natural Language is not valid structurally, the execution plan cannot be built and an exception will be thrown. If you go with the second approach, you need to create the NL execution plan as well to ensure the XML structure is valid.

To validate the XML structure (i.e., Declaration, Define, etc.), you can check the XML against our Document Commands.

Using RuntimeEngine

When you create an instance of a IRuntimeEngine for a Natural Language, the execution plan will be created for NL immediately.

// get the binary of your NL model
var nlContent = Encoding.UTF8.GetBytes(rule);
// create an instance of an engine for the NL
var engine = RuntimeEngine.FromXml(nlContent);
// Force the engine to create the execution plan

Using Natural Language Parser

In this approach, you can validate the NL format and structure:

// get the binary of your NL model
var nlContent = Encoding.UTF8.GetBytes(rule);
// Use table parser to build the execution plan for your NL
var nlPlanModel = TableParser.CreateParser().Parse(nlContent).First();

And then create an execution plan to ensure the whole XML document is valid:

// Create an Execution Plan for NL
var engine = RuntimeEngine.FromXml(nlContent);

Extract Expressions

When a Natural Language is compiled (parsed) to its execution plan, the execution steps become a Check elements. So the application can traverse the Check elements and inspect value attribute for expressions.

For example consider below NL:

input car
when test price range
    car.Price is [2; 33]

The result of NL to its execution plan is below Validation logic:

  1. <Validation enable="true">
  2.   <Declaration>
  3.     <Define name="car" direction="Local" />
  4.   </Declaration>
  5.   <Logic name="test price range">
  6.     <Group>
  7.       <Check value="((car.Price &gt;= 2) &amp;&amp; (car.Price &lt;=  33))" />
  8.     </Group>
  9.   </Logic>
  10. </Validation>

Extracting expressions from Execution Plan:

  • Build execution plan
  • Traverse execution plan and extract Check/value expressions
  • Register parameters in Variable Container
  • Build expression tree using ExpressionEval.Default.Compile
  1. var parser = NaturalParser.CreateParser();
  2. IElementModel model = parser.Parse(rule).First();
  4. // Traverse the tree to extract the expressions to parse
  5. var logicTestPriceRange = model.Childs.First(x => x.Name == "Logic");
  6. var check = logicTestPriceRange.Childs[0].Childs[0];
  7. var expression = check.Parameters["value"];
  9. // create a Variable Container and register parameters
  10. var parameters = model.GetDeclaration().Childs.Where(x => x.Name == "Define").Select(x => x.Parameters["name"]);
  11. var vc = new VariableContainer();
  12. foreach (var param in parameters)
  13.     vc.RegisterVariable(param);
  15. // Build the expression tree for the Check in execution plan
  16. var tree = ExpressionEval.Default.Compile(vc, expression);

Expression Tree:

 compare.Greater, Equal:
 compare.Equal, Less: