[rules-users] Infinite loop occurs even though no-loop is used

ST super_thunder2 at hotmail.com
Wed Feb 27 01:44:20 EST 2008


Hi,

I have encountered an infinite loop in the following example.  Where
TestVariable is has three fields, "context", "name", and "value".  Context and
name are Strings.  Value is an Object.  To work around the infinite loop, I need
to do some ugly type casting.

---------------------------
package testobjects;

public class TestVariable {

  private String context;
  private String name;
  private Object value;
  
  public TestVariable() {
  }
  public TestVariable(String context, String name, Object value) {
    setContext(context);
    setName(name);
    setValue(value);
  }
  public String getContext() {
    return context;
  }
  public void setContext(String context) {
    this.context = context;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public Object getValue() {
    return value;
  }
  public void setValue(Object value) {
    this.value = value;
  }
  public String toString() {
    StringBuffer sbResult = new StringBuffer(17);
    sbResult.append("");
    sbResult
        .append("context: ").append(getContext()).append(", ")
        .append("name: ").append(getName()).append(", ")
        .append("value: ").append(getValue());
    return sbResult.toString();
  }
  
}

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

package com.sample

import testobjects.TestVariable
import java.util.List

rule "init"
salience 1000
	when
		eval(true);
	then
		insert (new TestVariable("Price Items Rules Context" , "Unit Price", null));
		insert (new TestVariable("Price Items Rules Context", "Pricing Category",
"INTERNATIONAL"));
end

rule "test 1"
	
	no-loop true
	when
$unitPriceVariable : TestVariable(context == "Price Items Rules Context" && name
== "Unit Price")
		TestVariable(context == "Price Items Rules Context" && name == "Pricing
Category" && $pricingCategoryValue : value)
$pricingCategory : String() from $pricingCategoryValue
eval($pricingCategory.equals("INTERNATIONAL"))
	then
		$unitPriceVariable.setValue("542.00");
update($unitPriceVariable);
end


------------
The problem goes away if I change the "test 1" rule into the following:

rule "test 1"
	
	no-loop true
	when
$unitPriceVariable : TestVariable(context == "Price Items Rules Context" && name
== "Unit Price")
		TestVariable(context == "Price Items Rules Context" && name == "Pricing
Category" && $pricingCategoryValue : value)
  eval(((String)$pricingCategoryValue).equals("INTERNATIONAL"))
	then
//		$unitPriceVariable.setValue("542.00");
update($unitPriceVariable);
end





More information about the rules-users mailing list