I'm not sure if it is possible or not but personally I wouldn't do it.
I would have both Order and CustomerOrder as facts within your knowledge base (and
probably orderItem as well).
I would add a "total" field to Order and write a rule to maintain this value by
accumulating the itemAmount of all the orderitems. Similarly I would have another rule on
CustomerOrder to accumulate the value of the "total" of each of the orders. When
an Order is updated this would automatically trigger the re-calculation of the
CustomerOrder total.
Seperately I would have rules written against these fields which are then trivial to write
and will make it much easier to cater for future requirements (eg in the future if
customer name is "Mr Rich" then allow a max order value of XYZ).
If you can't modify your core objects then you can also implement it by having a
separate fact which links the object (CustomerOrder or order) to the order value) eg
ValueFact (source, value).
Thomas
Ps.
It is a really bad idea to try and use doubles to store currency amounts. Doubles do not
store decimal numbers precisely, this means £0.30 may actually be stored as
0.30000000000000004, and if you keep add £0.10 100 times you may end up with £9.99 not
£10.00. You should either use integer numbers (store pence not fractions of pounds) or use
big decimal.
http://www.velocityreviews.com/forums/t139008-java-double-precision.html
shows the problem in action.
-----Original Message-----
From: rules-users-bounces(a)lists.jboss.org [mailto:rules-users-
bounces(a)lists.jboss.org] On Behalf Of Dinesh kumar
Sent: 06 August 2010 11:58
To: rules-users(a)lists.jboss.org
Subject: [rules-users] Is nested loops possible ?
Hi,
I am having a problem in iterating a list inside a list.
I am having the following business scenario.
Public Class CustomerOrder {
private String customerName;
private ArrayList<Order> orderList;
}
Public Class Order {
private String orderId;
private String orderTime;
private ArrayList<OrderItem> orderList;
}
Public Class OrderItem {
private String itemName;
private Double itemAmount;
}
I need to find the total order amount for each order and the sum of all
order.
My rule is like individual orders should not exceed a 10000 and total order
amount of all customers should not exceed 100000.
For this, I need to iterate ArrayList<Order> orderList and during each
iteration , I need to iterate ArrayList<OrderItem> orderList. Is this
possible in Drools ?
I tried something like this. But it doesn't seem to be working.
when
ClaimRegistration($orderList: orderList)
Number( $count : intValue > 0)
from accumulate(Order(orderId== "order1"
&&
Number( doubleValue > 10000 )
from accumulate( OrderItem( $value : itemAmount),
init( double total = 0; ),
action( total += $value; ),
reverse( total -= $value; ),
result( total ) )
) from $orderList,count(1))
then
Sysout(",,,,,");
It would be of great help if any one can give suggestions to solve this.
Is nested loops possible in Drools ?
Regards,
Dinesh
--
View this message in context:
http://drools-java-rules-
engine.46999.n3.nabble.com/Is-nested-loops-possible-tp1029366p1029366.html
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
**************************************************************************************
This message is confidential and intended only for the addressee. If you have received
this message in error, please immediately notify the postmaster(a)nds.com and delete it from
your system as well as any copies. The content of e-mails as well as traffic data may be
monitored by NDS for employment and security purposes. To protect the environment please
do not print this e-mail unless necessary.
NDS Limited. Registered Office: One London Road, Staines, Middlesex, TW18 4EX, United
Kingdom. A company registered in England and Wales. Registered no. 3080780. VAT no. GB 603
8808 40-00
**************************************************************************************