Navigation

Categories
Show Navigation Next Topic  »

Storing Rules with Rule XML

As we mentioned in the Code Effects Basics topic, Code Effects business rules engine is not a system or environment - it's a component. As such, it doesn't manage your rules. It helps rule authors to create, update, delete business rules, and provides developers with a way to evaluate them.

Code Effects generates business rules in XML format, called Rule XML. Its schema is located here.

Because it's an XML document, a rule can be stored pretty much anywhere. The most common way - in the .NET world, at least - is to store them in a SqlServer database.

You have two options to store Rule XML:

  • You can simply store each rule as a separate XML document, the way you received it from the Rule Editor:

    <?xml version="1.0" encoding="utf-8"?>
    <codeeffects
    		xmlns="https://CodeEffects.com/schemas/rule/41"
    		xmlns:ui="https://CodeEffects.com/schemas/ui/4">
    	<rule
    		id="f480a6ce-deb0-4622-b7a3-b87fcca316ff"
    		webrule="3.0.0.151"
    		utc="2012-08-04T13:26:10.3429"
    		type="CodeEffects.Rule.Site.Common.Soap.Objects.Patient,
    			CodeEffects.Rule.Site, Version=2.0.0.0, Culture=neutral,
    			PublicKeyToken=null"
    		eval="false">
    			<name>Sample Rule</name>
    			<definition>
    				<!-- Definition nodes are omitted -->
    			</definition>
    			<format>
    				<!-- Formatting nodes are omitted -->
    			</format>
    	</rule>
    </codeeffects>
  • You can also store multiple rules in a single XML document. Such file is known as ruleset:

    <?xml version="1.0" encoding="utf-8"?>
    <codeeffects
    		xmlns="https://CodeEffects.com/schemas/rule/41"
    		xmlns:ui="https://CodeEffects.com/schemas/ui/4">
    	<rule
    		id="f480a6ce-deb0-4622-b7a3-b87fcca316ff"
    		webrule="3.0.0.151"
    		utc="2012-08-04T13:26:10.3429"
    		type="CodeEffects.Rule.Site.Common.Soap.Objects.Patient,
    			CodeEffects.Rule.Site, Version=2.0.0.0, Culture=neutral,
    			PublicKeyToken=null"
    		eval="false">
    			<name>Sample Rule 1</name>
    			<definition>
    				<!-- Definition nodes are omitted -->
    			</definition>
    			<format>
    				<!-- Formatting nodes are omitted -->
    			</format>
    	</rule>
    	<!-- There can be any number of rules in a single ruleset -->
    	<rule
    		id="0D1484FB-1757-4855-871C-90539A3569EB"
    		webrule="3.0.0.151"
    		utc="2012-08-05T16:23:26.0945"
    		type="CodeEffects.Rule.Site.Common.Soap.Objects.Patient,
    			CodeEffects.Rule.Site, Version=2.0.0.0, Culture=neutral,
    			PublicKeyToken=null"
    		eval="false">
    		<name>Sample Rule 2</name>
    		<definition>
    			<!-- Definition nodes are omitted -->
    		</definition>
    		<format>
    			<!-- Formatting nodes are omitted -->
    		</format>
    	</rule>
    </codeeffects>

    Its schema is the same as for a single Rule XML. The only difference is that its root <codeeffects> node can contains multiple <rule> nodes (business rules).

    All public constructors of all evaluator classes have a string XML parameter called rulesetXml. This parameter can take either the entire ruleset, or a single Rule XML document, or even an outer XML of a single <rule> node (this is convenient if you loop through the rules in a ruleset.)

    It's easy to create an empty ruleset:

    System.Xml.XmlDocument ruleset =
    		CodeEffects.Rule.Common.Xml.GetEmptyRuleDocument();

    You can add rules to its DocumentElement node and save its OuterXml as an XML file.

When a rule author creates a new business rule on the client using the Rule Editor, and clicks the Save button on the Toolbar, Rule Editor simply tells the server: "The user wants to create or update a business rule. Here is that rule as an XML string, and here is some additional information about it, such as its auto-generated ID, a list of optional reusable rules that it may reference, as well as some other useful data." At this point, all the server has to do is store the received Rule XML in your rule storage, whether it’s a database, a local file system, or something as complicated as network storage or remote web services.

You must handle all the rule management logic yourself by calling the RuleEditor.GetRuleXml() method to get Rule XML and handling its management if your project doesn't use Toolbar.

Please refer to our demo projects for code examples and details on storing and managing Rule XML documents.


Post your questions on Stackoverflow and become a part of our growing community

Comments: 0