Pregunta

There is a container class which has a collection of integers. My objective is to calculate the sum of all the integers. I am using the workflow rule engine here. The two rules written are somewhat like follows:

RuleName: Initializer
Priority: 2
Condition : 1==1
ThenActions: 
    this.i = 0;
ReEvaluationBehavior: Always

----

RuleName: Looping
Priority: 1
Condition: this.i < this.Items.Count
ThenActions:
    this.total = this.total + this.Items[this.i];
    this.i = this.i + 1;
ReEvaluationBehavior: Always

This is just an abstract representation. The Container class is defined as:

public class NumberContainer
{
    public int i;
    public int ItemCount 
    {
        get
        {
            return this.Items.Length;
        }

        private double total;

        public double Total
        {
            get { return total; }
            set { total = value; }
        }

        private double[] items;

        public double[] Items
        {
            get { return items; }
            set { items = value; }
        }

        //Other stuff...
    }
}

I've only shown the important fields here. All of this (i.e the populated container object and the ruleset) is fed to the rule engine to execute.

Why does the Looping rule execute multiple times whereas Initializer rule only executes once? Is it something to do with Chaining? Is it the fact that we are setting the value of i in the Looping rule, and the same variable happens to be a part of the corresponding rule's condition?

¿Fue útil?

Solución

Typically RETE-based/forward-chaining rules engines execute a rule whenever its condition becomes "newly true". Newly true in this context means that the condition is true and that one of its input variables has been changed since the last time the condition was checked.

In your case, the first rule has no input variables and hence is fired only once. The second rule has two input variables, this.i and this.Items.Count. After it is fired first, the second rule modifies this.i, causing its condition to be rechecked after firing the rule. The condition is then newly true again (repeating then until this.i>=this.Items.Count).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top