Calling the Rules Engine - In Process

  • Updated

This article contains the following sections:

Basic Example of Calling the Rule Engine

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

Specific InRule exceptions, including Compiler, Runtime, and several others, can be caught. See Handling Exceptions for samples.

Opening a Rule Application for Execution

Prerequisites None
Namespaces InRule.Runtime
Classes  FileSystemRuleApplicationReference, InMemoryRuleApplicationReference, CatalogRuleApplicationReference

InRule allows users to execute rules from the file system, catalog, or a rule application created in memory.


FileSystemRuleApplicationReference

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

CatalogRuleApplicationReference

// The URI for the catalog service.
string Uri = "http://localhost/InRuleCatalogService/Service.svc";

// Get the ruleapp from the repository using the URI, Username, Password, and Rule Application name.
CatalogRuleApplicationReference ruleAppRef = new CatalogRuleApplicationReference(Uri, "CustomerInvoice", "Admin", "password");

// If you are using active directory,  you only need the Uri and application name
ruleAppRef = new CatalogRuleApplicationReference(Uri, "CustomerInvoice");

// you can also specify a version e.g 2
ruleAppRef = new CatalogRuleApplicationReference(Uri, "CustomerInvoice", "Admin", "password",2);

// Using a label for example PRODUCTION
ruleAppRef = new CatalogRuleApplicationReference(Uri, "CustomerInvoice", "Admin", "password", "PRODUCTION");

// Using background version polling and recompilation after the refresh interval has expired
ruleAppRef = new CatalogRuleApplicationReference(Uri, "CustomerInvoice", "Admin", "password", "PRODUCTION", true);

InMemoryRuleApplicationReference

// Open an InMemoryRuleApplicationReference  using the RuleApplicationDef object

// Used when you have a rule RuleApplicationDef object available in memory,

// for example when dynamically generating rule applications via the Repository SDK.
RuleApplicationReference ruleAppRef = new InMemoryRuleApplicationReference (ruleApplicationDef)

Creating a RuleSession

Prerequisites A valid RuleApplicationReference
Namespaces  InRule.Runtime
Classes  RuleSession, RuleApplicationReference
See Also Retrieving a Rule Application, Creating a RuleSession with Cache Retention

The rule session object object that manages all of the rules engine request directives and execution results.  The rule session is used to load state, execute rules, and retrieve notifications, validations, and the execution log.

// Create a session to the rules engine, passing in a RuleApplicationReference
RuleSession ruleSession = new RuleSession(ruleAppRef);

// Use of cache retention
RuleSession ruleSession = new RuleSession(ruleAppRef, CacheRetention.AlwaysRetain);

// Using  Rule Application Definitions  reference to create a rule session
RuleSession ruleSession = new RuleSession(ruleAppDef);

// Using Rule Application Definition that has been saved to file to create a rule session
RuleSession ruleSession = new RuleSession(@"C:\temp\ruleAppDef");

Creating a RuleSession with Cache Retention

Prerequisites A valid RuleApplicationReference
Namespaces  InRule.Runtime
Classes RuleSession, RuleApplicationReference, CacheRetention
See Also  Retrieving a Rule Application, Adding items into the Cache, Controlling Compilation and Cache Retention, Iterating items in the cache , Working with the Rule Application Cache

The rule session object manages all of the rules engine request directives and execution results. It is used to load the state, execute rules, retrieve notifications, validations, and the execution log.

Cache retention controls how a session is stored in the cache

// Create a rule session with a rule application reference and

// specifying the cache retention
RuleSession ruleSession = new RuleSession(ruleAppRef, CacheRetention.AlwaysRetain);

// Create a rule session with a rule application definition reference and

// specifying the cache retention
RuleSession ruleSession = new RuleSession(ruleAppDef, CacheRetention.AlwaysRetain);

// Using Rule Application Definition that has been saved to file to create

// a rule session and specifying the cache retention
RuleSession ruleSession = new RuleSession(@"C:\temp\ruleAppDef", CacheRetention.AlwaysRetain);

// Using the cache retention weight to control how an rule application will be kept in the cache

// Higher values are more likely to be retained; Lower valies are less likely to be retained

// and therefore expired from the cache. 1000 is the default weight.
RuleSession ruleSession = new RuleSession(ruleAppRef, CacheRetention.FromWeight(5000));

Creating Entities

Prerequisites A valid RuleSession
Namespaces InRule.Runtime
Classes Entity, RuleSession
See Also Retrieving a Rule Application, Creating a RuleSession

For typical entity models, there is a top-level entity that provides access to all fields, sub-entities, and collections defined in the rule application.  The alternative independent entity model approach requires the use of Independent Rulesets.


Create blank Entity

//create a blank mortgage entity
Entity mortgageEntity = ruleSession.CreateEntity("Mortgage");

Create Entities with XML
// Create a mortgage entity with XML
Entity mortgageEntity = ruleSession.CreateEntity("Mortgage",@"<Mortgage>
        <LoanInfo><PropertyId>1</PropertyId><Principal>500000</Principal>
        <APR>6.875</APR><TermInYears>30</TermInYears></LoanInfo><PaymentSummary/></Mortgage>");

Create the Entity model from a business object model
// Create a mortgage business object
Mortgage mortgage = new Mortgage();
LoanInfo loanInfo = new LoanInfo();
loanInfo.APR = 6.875m;
loanInfo.PropertyId = 1;
loanInfo.Principal = 500000;
loanInfo.TermInYears = 30;

mortgage.LoanInfo = loanInfo;
mortgage.PaymentSummary = new PaymentSummary();

Entity mortgageEntity = ruleSession.CreateEntity("Mortgage", mortgage);

In order to use an object-based state, the rule application must first be bound to a .NET assembly schema.  See Binding to a .NET Assembly Schema in the main InRule Help file.

Alternate Ways to Load State for Entities

Prerequisites A valid Entity
Namespaces InRule.Runtime
Classes Entity
See Also

Retrieving a Rule Application, Creating a RuleSession, Creating Entities


Loading state from a file
// Load the state by passing in the path & filename.
mortgageEntity.LoadXml("Mortgage.xml");

Loading state from an XML string
// Load state from an XML string.
mortgageEntity.ParseXml("<Mortgage><LoanInfo><Principal>500000</Principal><APR>7</APR><TermInYears>30</TermInYears></LoanInfo><PaymentSummary /></Mortgage>");

Manually wiring up an Entity
//create ruleapp entities
Entity mortgageEntity = ruleSession.CreateEntity("Mortgage");
Entity loanInfo = ruleSession.CreateEntity("LoanInfo");
Entity paymentSummary = ruleSession.CreateEntity("PaymentSummary");

//set loanInfo values

loanInfo.Fields["Principal"].Value = 250000;
loanInfo.Fields["APR"].Value = 5.25;
loanInfo.Fields["TermInYears"].Value = 30;

//associate loanInfo entity to an Mortgage entity field
mortgageEntity.Fields["LoanInfo"].Value = loanInfo;

//associate paymentSummary entity to Mortgage entity field
mortgageEntity.Fields["PaymentSummary"].Value = paymentSummary;

In order to use an object-based state, the rule application must first be bound to a .NET assembly schema.  See Binding to a .NET Assembly Schema in the main InRule Help file.

Retrieving Entity State

Prerequisites A valid Entity
Namespaces InRule.Runtime
Classes Entity
See Also Retrieving a Rule Application, Creating a RuleSession, Creating Entities

Saving the state to the file system
// Save the state by passing in the path & filename.
mortgageEntity.SaveXml("MortgageOutput.xml");

Retrieving entity state as object
// get a reference to the object model - provided an object was used to

// create (or load) the entity
Mortgage mortgageObject = mortgageEntity.BoundValue as Mortgage;

Retrieving entity state as XML
// Get the state as XML
string stateXml = mortgageEntity.GetXml();

Working with RuleSession State

Prerequisites A valid RuleSession
Namespaces InRule.Runtime
Classes Entity, RuleSession
See Also Retrieving a Rule Application, Creating a RuleSession, Creating Entities

The following demonstrates how to serialize the RuleSession state, also known as a Test Scenario, to the file system and then subsequently load the RuleSession state back into memory.  The persisted file on the file system can be loaded directly in irVerify by selecting File --> Load Test Scenario and selecting the file.


Saving RuleSession State to the File System
// Save RuleSession state
ruleSession.SaveState(@"c:\work\state.testscenario");

Loading RuleSession State from the File System

// Load RuleSession state
ruleSession.LoadState(@"c:\work\state.testscenario");

Was this article helpful?

0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.