views:

526

answers:

7

I have to read business rules stored in XML file (I am using VC++/MFC/MSXML). XML consists of rules. Each rule has a set of conditions and actions. What would be an elegant OOP design for such system? What design patterns would you use if you where a designer of such system?

update:
Rules are put in sequence and executed one buy one if all conditions of rule are met.

example of xml:


   <rules>
      <!--  rule no. 1 -->
      <rule name="rule no. 1">
         <conditions>
            <condition name="condition no. 1" type="ATTR_EMPTY">
               <parameters>
                  <parameter name="attribute_name">ttt</parameter>
                  <parameter name="workflow_id">3</parameter>
                  <parameter name="workflow_state_id">5</parameter>
               </parameters>
            </condition>
            <condition name="condition no. 2" type="ATTR_EMPTY">
               <parameters>
                  <parameter name="attribute_name">ttt</parameter>
                  <parameter name="enviroment_id">3</parameter>
               </parameters>
            </condition>
         </conditions>
         <actions>
            <action name="action no. 1" type="ATTR_CLEAR">
               <parameters>
                  <parameter name="attribute_name">ttt</parameter>
               </parameters>
            </action>
         </actions>
      </rule>
   </rules>
A: 

XML is really just the low level transport layer, is the any higher level format on top of it you using? That way you can get recommendations to specific libraries.

Edit:

OK a downvote so this wasn't the answer you were looking for.

You could probably get an oo design but you will need to post a snippet of the xml or a detailed specification.

Tim Matthews
Down vote was not from me, I am interesting in all opinions equally...
Darius Kucinskas
+1  A: 

How do you intend to model these rules in memory?

One way of representing these rules in memory is using a higher-level programming language (e.g. python). Translate your XML rules into python code with the equivalent flow of control that invokes your C++ objects when it is executed. I've actually done that in the past to implement a rule system. I used JavaScript as both the serialization format of the rules (much easier to define rules in JS than in XML, and much more readable) and also as the actual executed code (I used SpiderMonkey to do C++<->JS). But you can do it basically in any popular script language that has C++ binding.

Assaf Lavie
+1  A: 

Difficult to tell from such a brief description, but I would make each rule an object which manages its own conditions and actions. The rules would be created by a factory from the XML and stored in some sort of dictionary.

anon
+1  A: 

There are two designs I'd consider, one is the one shot evaluator (your application runs once with a set of rules versus data and then exits) in this case I'd use an XML SAX parser with callbacks that handle the data

The other is a DOM parser that caches rules in memory using a Rule Class that's a composite of a Condition(Check) class and an Action class.

Robert Gould
+1  A: 

The rules part looks a little like Schematron:

http://www.schematron.com/elements.html

which is an XML validation language written in XML and an ISO standard.

Assuming the source data (that is being validated) is also in XML, one approach would be to transform your set of rules into Schematron (which would not be hard in XSLT) and then use the Schematron "infrastructure" (ie, stylesheets) to validate your content against the Schematron file.

This would produce a result XML file showing which rule has been satisfied and which has not; you may then perform the "actions" (transformations?) associated with each verified rule.

+1  A: 

Also check out RulesML...

jm04469
A: 

Writing business rules in XML is clunky. Besides, C++ is also not a good language for XML processing.

Your application can expose its properties through Active Scripting, and write rules in VBScript/JScript. In this way you can evaluate rules at run time.

If XML is required for defining rules, I would write the processor separately in a high level language such as Java, JavaScript or XSLT. Run the processor at background and grabs the return value in your C++ application.

Sherwood Hu