[rules-users] How can I iterate through the ArrayList

Wolfgang Laun wolfgang.laun at gmail.com
Thu Nov 14 06:44:18 EST 2013


rule "fire for each AwaitingAction"
when
   $aal: AwaitingActionList()
   $aa: AwaitingAction() from $aal.getAwaitingActionsList()
then
    System.out.println( $aa.getStatus() );
end

Or you can itrerate over the list on the RHS:

rule "fire for each AwaitingActionList"
when
   $aal: AwaitingActionList()
then
    for( Object obj: $aal.getAwaitingActionsList() ){
        AwaitingAction aa = (AwaitingAction)obj;
        System.out.println( aa.getStatus() );
    }
end

-W


On 14/11/2013, Govind J. Parashar <Govind.Parashar at mastek.com> wrote:
> Hi
>
>
> Below have my java code and main method I want  to fire rule for all value
> of "STATUS"   how to write drl for this
>
> I am passing  awaitingActionList to rule
>
> public static void main(String args[]) throws Exception {
> 	
>     KnowledgeBase kbase = readKnowledgeBase();
>     StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
>
>     AwaitingAction action = new AwaitingAction();
>     AwaitingActionList awaitingActionList = new AwaitingActionList();
>     List<AwaitingAction> list = new ArrayList<AwaitingAction>();
>     action.setStatus("CU");
>     action.setValue("Create User");
>     list.add(action);
>     action = new AwaitingAction();
>     action.setStatus("CLU");
>     action.setValue("Complete Request");
>     list.add(action);
>     action = new AwaitingAction();
>     action.setStatus("CP");
>     action.setValue("Create Position");
>     list.add(action);
>     awaitingActionList.setAwaitingActionsList(list);
>     for(AwaitingAction actionVO :
> awaitingActionList.getAwaitingActionsList())
> 	{
> 		System.out.println("VALUE:-- "+actionVO.getValue()+"    STATUS:--
> "+actionVO.getStatus()+"   REDIRECT TO:-- "+actionVO.getRedirect());
> 	}	
>
>     ksession.insert(awaitingActionList);
>     ksession.fireAllRules();
>     ksession.dispose();
>
>   }
>
>
>
> private static KnowledgeBase readKnowledgeBase() throws Exception {
>     KnowledgeBuilder kbuilder =
> KnowledgeBuilderFactory.newKnowledgeBuilder();
>     kbuilder.add(ResourceFactory.newClassPathResource("AwaitingAction.drl"),
> ResourceType.DRL);
>     KnowledgeBuilderErrors errors = kbuilder.getErrors();
>     if (errors.size() > 0) {
>       for (KnowledgeBuilderError error : errors) {
>         System.err.println(error);
>       }
>       throw new IllegalArgumentException("Could not parse knowledge.");
>     }
>     KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
>     kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
>     return kbase;
>   }
> }
>
>
>
>
> public class AwaitingAction {
>
> 	private String status;
> 	private String value;
> 	private String redirect;
> 	public String getStatus() {
> 		return status;
> 	}
> 	public void setStatus(String status) {
> 		this.status = status;
> 	}
> 	public String getValue() {
> 		return value;
> 	}
> 	public void setValue(String value) {
> 		this.value = value;
> 	}
> 	public String getRedirect() {
> 		return redirect;
> 	}
> 	public void setRedirect(String redirect) {
> 		this.redirect = redirect;
> 	}
>
>
>
> public class AwaitingActionList {
>
> 	public List<AwaitingAction> awaitingActionsList =  new
> ArrayList<AwaitingAction>();
>
> 	public List<AwaitingAction> getAwaitingActionsList() {
> 		return awaitingActionsList;
> 	}
>
> 	public void setAwaitingActionsList(List<AwaitingAction>
> awaitingActionsList) {
> 		this.awaitingActionsList = awaitingActionsList;
> 	}
>
> 	
> }
>
>
>
>
> -----Original Message-----
> From: rules-users-bounces at lists.jboss.org
> [mailto:rules-users-bounces at lists.jboss.org] On Behalf Of Wolfgang Laun
> Sent: 14 November 2013 05:08
> To: Rules Users List
> Subject: Re: [rules-users] How can I iterate through the ArrayList
>
> You may have to phrase your question more accurately in order to get an
> answer.
>
> Do you want the rule to fire if some, or if all, "value" fields of the
> AwaitingAction have some value? Once or for each AwaitingAction? Or do you
> want it to fire once if at least one, or if all, AwaitingActions of all
> AwaitingActionLists have a field "value" with some specific value?
>
> Also, would you consider inserting also the AwaitingAction objects as facts,
> not only the AwaitingActionList?
>
> -W
>
>
>
> On 13/11/2013, Govind J. Parashar <Govind.Parashar at mastek.com> wrote:
>> HI,
>>
>> I have a AwaitingActionList Object which has a awaitingActionsList
>> Object which has a ArrayList of AwaitingAction Objects. How can I
>> iterate through the ArrayList to get to check each object
>> AwaitingAction .value in the drl file.
>>
>> Java refrence file attached
>>
>> Thanks
>> Govind
>> MASTEK LTD.
>> In the US, we're called MAJESCOMASTEK
>>
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> Opinions expressed in this e-mail are those of the individual and not
>> that of Mastek Limited, unless specifically indicated to that effect.
>> Mastek Limited does not accept any responsibility or liability for it.
>> This e-mail and attachments (if any) transmitted with it are
>> confidential and/or privileged and solely for the use of the intended
>> person or entity to which it is addressed. Any review,
>> re-transmission, dissemination or other use of or taking of any action
>> in reliance upon this information by persons or entities other than
>> the intended recipient is prohibited. This e-mail and its attachments
>> have been scanned for the presence of computer viruses. It is the
>> responsibility of the recipient to run the virus check on e-mails and
>> attachments before opening them. If you have received this e-mail in
>> error, kindly delete this e-mail from desktop and server.
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
> MASTEK LTD.
> In the US, we're called MAJESCOMASTEK
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Opinions expressed in this e-mail are those of the individual and not that
> of Mastek Limited, unless specifically indicated to that effect. Mastek
> Limited does not accept any responsibility or liability for it. This e-mail
> and attachments (if any) transmitted with it are confidential and/or
> privileged and solely for the use of the intended person or entity to which
> it is addressed. Any review, re-transmission, dissemination or other use of
> or taking of any action in reliance upon this information by persons or
> entities other than the intended recipient is prohibited. This e-mail and
> its attachments have been scanned for the presence of computer viruses. It
> is the responsibility of the recipient to run the virus check on e-mails and
> attachments before opening them. If you have received this e-mail in error,
> kindly delete this e-mail from desktop and server.
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>


More information about the rules-users mailing list