All accesses to a global must be done via setGlobal/getGlobal:
drools.getWorkingMemory().setGlobal( "prodCat", getName(nameList) );
It's not a Java variable.
-W
Hi ,
Thanks Bruno and Wolfgang for replying.
The getname() is a drools function which i want to invoke at runtime and set
the global variable.
Code:
global String prodCat;
rule "Your First Rule"
when
#conditions
nameList : ArrayList()
//prodCat : String()
eval (nameList.size() > 0)
then
#actions
prodCat = getName(nameList);
//modify (prodCat);
System.out.println( "ArrayListSize:" +nameList.size());
System.out.println( "ProductCategory:" +prodCat);
end
function String getName(List lstName)
{
String name= "";
String prodCat = "";
for (Iterator iterator = lstName.iterator();iterator.hasNext();) {
Map dsName= (HashMap)iterator.next();
prodCat = dsName.get("name").toString();
if(prodCat.equalsIgnoreCase("Rajiv"))
{
name= "Rajiv";
break;
}
else if(prodCat.equalsIgnoreCase("Robin"))
{
name= "Robin";
}
}
return name;
}
JAVA:
String prodCat = "dummy";
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
ksession.insert(lstName);
ksession.setGlobal("prodCat", prodCat);
ksession.fireAllRules();
System.out.println("Output:"+ksession.getGlobal("prodCat"));
--Rajiv
--
View this message in context: http://drools-java-rules-engine.46999.n3.nabble.com/accessing-a-drools-variable-tp1843203p1844248.html
Sent from the Drools - User mailing list archive at Nabble.com.
_______________________________________________
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users