[rules-users] accessing a drools variable

Wolfgang Laun wolfgang.laun at gmail.com
Thu Nov 4 16:33:20 EDT 2010


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 at 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-variable-tp1843203p1844248.html
> Sent from the Drools - User mailing list archive at Nabble.com.
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20101104/b5882819/attachment.html 


More information about the rules-users mailing list