NL101

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 validating:

  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 create the NL execution plan as well to ensure the XML structure is valid.

To validate the XML structure (i.e. Declaration, Define...) 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 engine for the NL
var engine = RuntimeEngine.FromXml(nlContent);
 
// Force engine to create the execution plan
engine.EnsureLoaded()

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);