Basic Example of Calling the Rule Engine

  • Updated

Prerequisites: None
Namespaces: InRule.Runtime, InRule.Common.Exceptions
Classes:   FileSystemRuleApplicationReference, RuleSession, Entity, RuleException
See Also: Retrieving a Rule Application, Creating a RuleSession, Creating Entities, Applying Rules, Handling Exceptions
References: InRule.Runtime.dll, InRule.Common.dll, InRule.Repository.dll

The following basic example demonstrates calling the rules engine to calculate the area of a rectangle.

// Get the ruleapp from a file.
RuleApplicationReference ruleAppRef =
new FileSystemRuleApplicationReference(@"C:\RuleApps\Rectangle.ruleapp");

// Create a session to manage the rule engine request and response.

// The RuleSession should be disposed after use. This can be done with a "using" statement or by explicitly calling Dispose
using (RuleSession session = new RuleSession(ruleAppRef))
{
 try
 {

         // Create the "Rectangle" Entity defined in the rule application schema and set the values using xml.

         Entity rectangle = session.CreateEntity("Rectangle", @"<Rectangle><Height>2</Height><Width>3</Width></Rectangle>");

         // Apply the rules to calculate the area of the rectangle.
         session.ApplyRules();

         // Get the state and results as XML
         string resultsXml = rectangle.GetXml();
 }

// catch RuleException, base class for all InRule Exceptions

 catch (RuleException ex)
 {
         Console.WriteLine(ex.ToString());
 }
}

Note: There are specific InRule exceptions that can be caught, including Compiler, Runtime and several other exceptions.  See Handling Exceptions for samples.  See RuleException Class for a complete list of InRule exceptions.

Was this article helpful?

0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.