[rules-users] Looping through a vector object in a rule

Michael Anstis michael.anstis at gmail.com
Thu May 5 09:47:45 EDT 2011


Hi,

You have to "let go" of the need to explicitly iterate collections within
rules.

You wouldn't normally think about iterating all records yourself in a
database when performing a SQL statement like this:-

select sum(ch.claimAmount) as total from claim c, claimHistory ch where
c.claimId = ch.fk_claimId;

So your rules can work in much the same way:-

global Long total;

when
    $c : Claim()
    $h : ClaimHistory( claimId == $c.claimId )
then
    total = total + $h.getClaimAmount();
end

This rule will activate for every combination of Claim and ClaimHistory
(where claim ID's match) in Working Memory.

Relating this to your example you should be able to do the following:-

when
    $c : DURClaim()
    $ch : ClaimsHistory()
    $h : DURClaim() from $ch.getHistory()
then
    //Do some computation involving $c and $h
end

This will match on every combination of DURClaim and ClaimsHistory's
DURClaim in Working Memory. AFAIK Drools does not support Java's generics in
pattern matching so I have shown use of "ClaimsHistory" rather than using
"List" to prevent matching on any other List you may have in WM. Obviously
you'd need to insert "claims" into WM not "historyClaims".

If ClaimsHistory.getHistory returned a List of objects of a class other than
DURClaim you could have used the option to explode the list first using a
higher salience rule (as in my earlier email) however, in your case, it'd be
difficult to prevent matching between different instances of DURClaim that
represent different "inClaim" objects if the rule simply contained two
"DURClaim" patterns.

With kind regards,

Mike

On 5 May 2011 13:03, sdinoo <sdinoo at gmail.com> wrote:

> Hi Michael
>
> Thank you for helping me out - but I need some more help
>
> Pardon my ignorance, but I am still not clear how this can be done
> I am looking for something in the language that will allow me to iterate
> through the entire List/Vector object by object and then access each object
> instances values like Object.getClaimAmount
>
> Here is my calling program
>        public static final void main(String[] args) {
>                DURUtils rc = new DURUtils();
>
>                //Load the claims from CSV into History
>                ClaimsHistory claims = new ClaimsHistory();
>                List<DurClaim> historyClaims = claims.getHistory();
>                System.out.println("List Size = " + historyClaims.size());
>
>                //Create a New Incoming object
>                IncomingClaim ic = new IncomingClaim();
>                DurClaim inClaim = ic.getIncomingClaim();
>
>        try {
>                        // load up the knowledge base
>                        KnowledgeBase kbase = readKnowledgeBase();
>                        StatefulKnowledgeSession ksession =
> kbase.newStatefulKnowledgeSession();
>                        KnowledgeRuntimeLogger logger =
> KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
>                        // start a new process instance
>                        ksession.insert(inClaim);
>                        ksession.insert(historyClaims);
>                        ksession.startProcess("com.drughistory");
>                        ksession.fireAllRules();
>                        logger.close();
>                } catch (Throwable t) {
>                        t.printStackTrace();
>                }
>
>                }
>
>        private static KnowledgeBase readKnowledgeBase() throws Exception {
>                KnowledgeBuilder kbuilder =
> KnowledgeBuilderFactory.newKnowledgeBuilder();
>
>
> kbuilder.add(ResourceFactory.newClassPathResource("trials/ClaimProcessing.bpmn"),
> ResourceType.BPMN2);
>
>  //kbuilder.add(ResourceFactory.newClassPathResource("DurClaimRules.drl"),
> ResourceType.DRL);
>
>  kbuilder.add(ResourceFactory.newClassPathResource("trials/History.drl"),
> ResourceType.DRL);
>                return kbuilder.newKnowledgeBase();
>        }
>
> All I want to do is some computation using the
> incomingClaim with a List of the historyClaims
> The incoming claim and the historyclaims are of the same model claim called
> DURClaim
>
> Again, this may be a very elemental question but I thought there would be
> an
> easier way of doing this as I would like to keep the iteration processing
> in
> the rule rather than outside in a Java Class
>
>
>
>
>
>
>
>
>
> --
> View this message in context:
> http://drools.46999.n3.nabble.com/Looping-through-a-vector-object-in-a-rule-tp2897872p2903115.html
> Sent from the Drools: User forum 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/20110505/65f6ebb0/attachment.html 


More information about the rules-users mailing list