There has to be something I do not understand about using rules.
Take this simple class and rules and I don't understand the output.
First, when the rule constructor is executed it appears as though only the
value 1 was put into the map, then it runs rule test1 and then it sees only
values 2 and 3.
Secondly, look at rule test2 right now the RHS displays true, BUT if you
comment out the eval(true) and uncomment either of the evals you get a
nullpointer at runtime. I do not understand why there is a null pointer. Any
insight would be wonderful, I have been banging my head against this for a
bit and it's probably something stupid.
Thanks!
---------------------------------
package test.map;
import java.util.HashMap;
import java.util.Map;
public class Test
{
public Test()
{
values = new HashMap<String, Object>();
}
private Map<String, Object> values;
public Map<String, Object> getValues()
{
return values;
}
public void setValues(Map<String, Object> values)
{
this.values = values;
}
}
-----------------------------------------
package test.map
import java.math.BigDecimal;
import java.util.Map;
rule "Constructor"
no-loop true
when
eval(true);
$t : Test(values:values)
then
drools.setFocus("test2");
drools.setFocus("test1");
values.put("one", new BigDecimal("1"));
addValue($t, "two", new BigDecimal("2"));
$t.getValues().put("three", new BigDecimal("3"));
update($t);
System.out.println("in constructor");
System.out.println(values);
System.out.println();
end
rule "test1"
no-loop true
agenda-group "test1"
when
eval(true);
$t : Test(values:values)
then
System.out.println("in test1");
System.out.println(values);
System.out.println();
end
rule "test2"
no-loop true
agenda-group "test2"
when
eval(true);
$t : Test(values:values)
//eval(((BigDecimal)values.get("two")).equals(new
BigDecimal("2")));
//eval(((BigDecimal)$t.getValues().get("two")).equals(new
BigDecimal("2")));
then
System.out.println(((BigDecimal)$t.getValues().get("two")).equals(new
BigDecimal("2")));
end
function void addValue(Test t, String key, BigDecimal value)
{
t.getValues().put(key, value);
}
---------------------------------------
This is a simple class to start it up.
---------------------------------------
package test.map;
import java.io.InputStreamReader;
import java.io.Reader;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.WorkingMemory;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;
public class Mainclass
{
public static void main(String[] args)
{
try
{
//load up the rulebase
RuleBase ruleBase = readRule();
WorkingMemory workingMemory =
ruleBase.newStatefulSession();
Test test = new Test();
workingMemory.insert(test);
workingMemory.fireAllRules();
}
catch (Throwable t)
{
t.printStackTrace();
}
}
/**
* Please note that this is the "low level" rule assembly API.
*/
private static RuleBase readRule() throws Exception
{
Reader source = new
InputStreamReader(Mainclass.class.getResourceAsStream("/Nulltest.drl"));
System.setProperty("drools.compiler",
"JANINO");
PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl( source );
Package pkg = builder.getPackage();
RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage( pkg );
return ruleBase;
}
}
Jeff Shutt
Software Developer
SOS Staffing Services
(801) 924-0429
shuttj(a)sosstaffing.com