Working with Code Libraries - Implementing the engine
(Page 5 of 5 )
Using NxBRE in a project typically involves the following steps:
- Instantiating the engine
- Loading a rule base
- Storing data in memory
- Running the engine
- Analyzing the results

Figure 4-4. Mixing rules and other schema elements
It is not possible to programmatically define rules in NxBRE.
As shown in Figure 4-6, the memory where data is loaded is called context for the Flow Engine and is called working memory for the Inference Engine. Context and working memory contain references to object instances and bits of knowledge called facts, respectively.
The following code shows a typical usage of the Flow Engine:
IFlowEngine bre = new BREImpl();
bre.Init(new XBusinessRulesFileDriver(ruleFile))
bre.RuleContext.SetObject("TestObject", tobj);
bre.Process();
// check the modifications made on tobj

Figure 4-5. Testing a rule base with the Inference Engine Console

Figure 4-6. Architecture of NxBRE
And the following code shows a typical usage of the Inference Engine:
IInferenceEngine ie = new IEImpl();
ie.LoadRuleBase(new RuleML09NafDatalogAdapter(
ruleFile, FileAccess.Read));
ie.Assert(new Fact("is the father of", new Individual("John Smith"),
new Individual("Kyle Doe"));
ie.Process();
// query the fact base for deduced facts
The Inference Engine can leverage a binder, which is a file that plays a role similar to ASP.NET code-behind files. The binder, which can be either an on-the-fly compiled C# class or a flow control rule file, is used to retrieve data from enterprise sources or business objects, to analyze complex expressions, and to perform data changes based on the engine deductions.
Using a binder contributes to a versatile implementation because it is dynamically evaluated and can therefore be altered at runtime.
The binder is also able to consume events raised by the engine when deductions are made: these events can be leveraged to implement reaction code that directly modifies business objects or enterprise data sources, instead of analyzing the memory of the engine after process time and mapping its state to the said objects and data sources.
While the Flow and Inference Engines are not thread-safe objects, NxBRE’s documentation details recommend implementation strategies for multithreaded environments such as ASP.NET or Enterprise Services.
A registry that facilitates the implementation of the Inference Engine in a multithreaded environment, with support for hot reloading of rule bases and binders, has recently been contributed as an extra package for NxBRE.
Getting Support Support for NxBRE is available either through the online SourceForge forums or via email by writing to support@nxbre.org.
NxBRE in a Nutshell
NxBRE’s interest lies first in its simplicity, and second in the possibility of easily extending its features by delegating to custom code in the Flow Engine or by writing custom RuleBase binders in the Inference Engine.
NxBRE can be really useful for projects that have to deal with:
Complex business rules that cannot be expressed in one uniform, structured manner but require the ability to have free logical expressions
Changing business rules that force recompilation if the new rules must meet unexpected requirements
Implementing such an engine has a price, though, as data must be adapted in both ways between the application and the engine. This can impair performance and also requires adopting a different mindset for the data manipulated by the engine. Software programmers and analysts might find the learning curve too steep (and the available documentation too thin).
Management of large rule bases can be discouraging, as NxBRE does not offer the rule-management utilities its paying counterparts do. Performance has also been reported to degrade with big rule bases (more than a thousand rules), while it remains pretty insensitive to the volume of data loaded in memory.
That said, NxBRE has proven satisfactory for many implementations in the finance and health-care industries and in government. Recent releases of NxBRE have focused on improving its performance.
NxBRE lets projects whose cost constraints would not allow purchasing a commercial solution externalize business rules. It fits easily in any kind of application and starts giving results within minutes.
—David Dossot, creator of NxBRE
Please check back next week for the continuation of this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of Windows Developer Power Tools, written by James Avery and Jim Holmes (O'Reilly, 2006; ISBN: 0596527543). Check it out today at your favorite bookstore. Buy this book now.
|
|