Thursday, February 24, 2005

Rules Engine investigations

This is a copy of my recent posting to the newsgroups, which I'm posting here in case anyone has a simple answer to what I believe is a simple question...

------------------------------------

I have created a simple policy of two rules, that operate on a single TypedXmlDocument:

<ns0:Root xmlns:ns0="http://RulesEngine.Schema1">
<Fact1>Y</Fact1>
<Fact2>N</Fact2>
<Fact3>N</Fact3>
</ns0:Root>

I want this to work in such a fashion that when testing the rule with the aforementioned sample, I end up with a message with all facts = Y.

My rules are:
1. If Fact1 = Y then set Fact2 = Y
2. If Fact2 = Y then set Fact3 = Y


Stephon Mohr's chapter on the subject states:
"When the available facts exist to evaluate a rule's condition and it evaluates to true, the rule is fired. As rules fire, new facts are asserted into memory. For example, firing a rule might cause a field to be set in a message. This becomes a new fact in memory, which may cause another rule to fire. This continues until no more rules can fire."

I tested the policy, and got the following results:

RULE ENGINE TRACE for RULESET: Policy1 24/02/2005 15:12:56

FACT ACTIVITY 24/02/2005 15:12:56
Rule Engine Instance Identifier: d54d5da0-b88a-4e94-ba09-88932c880c92
Ruleset Name: Policy1
Operation: Assert
Object Type: TypedXmlDocument:Schema1
Object Instance Identifier: 735

FACT ACTIVITY 24/02/2005 15:12:56
Rule Engine Instance Identifier: d54d5da0-b88a-4e94-ba09-88932c880c92
Ruleset Name: Policy1
Operation: Assert
Object Type: TypedXmlDocument:Schema1:/Root
Object Instance Identifier: 725

CONDITION EVALUATION TEST (MATCH) 24/02/2005 15:12:56
Rule Engine Instance Identifier: d54d5da0-b88a-4e94-ba09-88932c880c92
Ruleset Name: Policy1
Test Expression: TypedXmlDocument:Schema1:/Root.Fact1 == Y
Left Operand Value: Y
Right Operand Value: Y
Test Result: True

AGENDA UPDATE 24/02/2005 15:12:56
Rule Engine Instance Identifier: d54d5da0-b88a-4e94-ba09-88932c880c92
Ruleset Name: Policy1
Operation: Add
Rule Name: Rule1
Conflict Resolution Criteria: 0

CONDITION EVALUATION TEST (MATCH) 24/02/2005 15:12:56
Rule Engine Instance Identifier: d54d5da0-b88a-4e94-ba09-88932c880c92
Ruleset Name: Policy1
Test Expression: TypedXmlDocument:Schema1:/Root.Fact2 == Y
Left Operand Value: N
Right Operand Value: Y
Test Result: False

RULE FIRED 24/02/2005 15:12:56
Rule Engine Instance Identifier: d54d5da0-b88a-4e94-ba09-88932c880c92
Ruleset Name: Policy1
Rule Name: Rule1
Conflict Resolution Criteria: 0

FACT ACTIVITY 24/02/2005 15:12:56
Rule Engine Instance Identifier: d54d5da0-b88a-4e94-ba09-88932c880c92
Ruleset Name: Policy1
Operation: Retract
Object Type: TypedXmlDocument:Schema1
Object Instance Identifier: 735

FACT ACTIVITY 24/02/2005 15:12:56
Rule Engine Instance Identifier: d54d5da0-b88a-4e94-ba09-88932c880c92
Ruleset Name: Policy1
Operation: Retract
Object Type: TypedXmlDocument:Schema1:/Root
Object Instance Identifier: 725

-------------------------------------------------------

According to my reading of forward chaining, Rule 1 should have been fired, changing the value of Fact2 to 'Y', which should have caused Fact2 to be reasserted, which in turn would cause Rule 2 to be fired on the second pass, resulting in Fact 3 being set to Y.

I also don't understand why the output shows the facts that are asserted and retracted are given as:

Object Type: TypedXmlDocument:Schema1
Object Type: TypedXmlDocument:Schema1:/Root

What about the actual elements Fact1 and Fact2 - aren't they are what I'm evaluating?

No comments: