1

I took the Fibanocci example and modified it little bit. It still seems to work, but I don't know how. Here is my rule.

rule "Recurse"
salience 10
    when
       f : Fibanocci(value == 0)
       not Fibanocci(sequence == 0)
    then
       System.out.println(f.sequence + "/" + f.value);
       insert(new Fibanocci(f.sequence - 1));
end

I get the following output:

50/0 49/0 48/0 47/0 46/0 45/0 .. and so on

It seems to me every time a new instance of Fibanocci is inserted, it should start from 50 again, but it doesn't.

// here is my class, getter/setter skipped
public class Fibonacci {

    private int sequence;
    private long value;

    public Fibonacci(final int sequence) {
        this.sequence = sequence;
    }
}
4

1 回答 1

2

Here is a short explanation, why the rules fire like they do:

  • Every rule fires exactly once for any combination of matching working memory elements.
  • The first match is generated by "a Fibonacci-Object with value==0" and "the non-existance of a Fibonacci-Object with sequence==0".
  • For this combination, the rule will not fire again until
    • the particular Fibonacci-Object is modified and updated.
    • a Fibonacci-Object with sequence==0 appears and then disappears (*)
  • The consequence of the rule creates another Fibonacci-Object with value==0. This creates only one more match of "a Fibonacci-Object with value==0" and "the non-existance of a Fibonacci-Object with sequence==0". (The "non-existance" fact is not changed.)

*) only 99% sure about that

于 2012-07-06T10:46:19.530 回答