All accesses to a global must be done via setGlobal/getGlobal:
drools.getWorkingMemory().setGlobal( "prodCat", getName(nameList) );
It's not a Java variable.
-W
On 4 November 2010 20:59, rajivharris <rajivharris(a)gmail.com> wrote:
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-va...
Sent from the Drools - User mailing list archive at
Nabble.com.
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users