[rules-users] Drools-Fusion: Problem with loading data
Wolfgang Laun
wolfgang.laun at gmail.com
Fri Mar 30 01:30:47 EDT 2012
Indeed, now you have avoided creating the full Cartesian product POS x Customer.
-W
On 30/03/2012, Luciano Molinari <lucmolinari at gmail.com> wrote:
> Hi Wolfgang,
>
> Do you mean something like this?
>
> rule "rule_suspiciousTransactionInDifferentsPOSOfSameCustomer"
> dialect "mvel"
> when
> $trans1 : Event($logicNumber : logicNumber, $customer : customer)
> from entry-point StreamTransactions
> $trans2 : Event(logicNumber != $logicNumber, customer == $customer,
> this after[0, 10s] $trans1) from entry-point StreamTransactions
> then
> System.out.println("Suspect fraud between the transactions " + $
> trans1.id + " and " + $trans2.id);
> end
>
> Is this the best way?
>
> Thanks for your help.
>
> Cheers,
> Luciano
>
>
> 2012/3/29 Wolfgang Laun <wolfgang.laun at gmail.com>
>
>> Rule "rule_suspiciousTransactionInDifferentsPOSOfSameCustomer" in
>> combination with many POS and many Customer facts is an anti-pattern. The
>> rule starts looking for POS facts: you insert plenty. Then, it looks for
>> Customers: you insert a bundle. And so it combines the first POS with all
>> Customers, the second POS with all Customers,... and so on, and on and on.
>>
>> Start with an Event, match another Event close to the first one, and *then
>> pull in the suspects. *(The other way round isn't popular in real life
>> either ;-) )
>>
>> Cheers
>> -W
>>
>>
>>
>> 2012/3/29 Luciano Molinari <lucmolinari at gmail.com>
>>
>>> Hi guys,
>>>
>>> I'm trying drools-fusion's cep capabilities but I'm having some troubles
>>> on loading the data to the Working Memory. I have three types defined in
>>> the DRL File: Customer, POS (Point of sale) and Transaction (event). I
>>> have
>>> a rule to discover suspicious transactions (customers that have made two
>>> transactions in ten seconds and from diferents POS).
>>>
>>> Besides, I have other 3 helper rules that receive the fact (Customer or
>>> POS) or the event (Transaction) in JSON format, convert it and store it
>>> in
>>> the session (Customer or POS) or forward the event to another
>>> entry-point.
>>>
>>> My drools file:
>>>
>>> global org.codehaus.jackson.map.ObjectMapper jsonConverter;
>>>
>>> declare Event
>>> @role(event)
>>> id : String
>>> logicNumber : String
>>> customer : String
>>> end
>>>
>>> declare POS
>>> logicNumber : String
>>> end
>>>
>>> declare Customer
>>> id : String
>>> end
>>>
>>> rule "rule_convertAndInsertPOS"
>>> when
>>> $json : String() from entry-point convertAndInsertPOS
>>> then
>>> POS pos = jsonConverter.readValue($json, POS.class);
>>> retract($json);
>>> insert(pos);
>>> end
>>>
>>> rule "rule_convertAndInsertCustomer"
>>> when
>>> $json : String() from entry-point convertAndInsertCustomer
>>> then
>>> Customer customer = jsonConverter.readValue($json,
>>> Customer.class);
>>> retract($json);
>>> insert(customer);
>>> end
>>>
>>> rule "rule_convertAndSendEvent"
>>> when
>>> $json : String() from entry-point convertAndSendTransaction
>>> then
>>> Event evt = jsonConverter.readValue($json, Event.class);
>>> System.out.println("Repassando evento " + evt);
>>> entryPoints["StreamTransactions"].insert(evt);
>>> retract($json);
>>> end
>>>
>>> rule "rule_suspiciousTransactionInDifferentsPOSOfSameCustomer"
>>> dialect "mvel"
>>> when
>>> $pos : POS($posNumber : logicNumber)
>>> $customer : Customer($idCustomer : id)
>>> $trans1 : Event(logicNumber == $posNumber, customer ==
>>> $idCustomer) from entry-point StreamTransactions
>>> $trans2 : Event(logicNumber != $posNumber, customer ==
>>> $idCustomer, this after[0, 10s] $trans1) from entry-point
>>> StreamTransactions
>>> then
>>> System.out.println("Suspect fraud between the transactions " + $
>>> trans1.id + " and " + $trans2.id);
>>> end
>>>
>>> In my test case, before starting sending events to the entry-point
>>> "convertAndSendTransaction", I try to load some POS and Customers.
>>>
>>> private void loadPOS() {
>>> WorkingMemoryEntryPoint entryPointConvertAndInsertPOS = kSession
>>> .getWorkingMemoryEntryPoint("convertAndInsertPOS");
>>> log("Loading POS");
>>> for (int i = 1; i <= 20000; i++) {
>>> if ((i % 1000) == 0) {
>>> log("Inserting POST " + i);
>>> kSession.fireAllRules();
>>> }
>>> entryPointConvertAndInsertPOS.insert(getPOSInJson("POS" +
>>> i));
>>> }
>>> kSession.fireAllRules();
>>> log("POS loaded");
>>> }
>>>
>>> private void loadCustomer() {
>>> WorkingMemoryEntryPoint entryPointConvertAndInsertCustomer =
>>> kSession
>>> .getWorkingMemoryEntryPoint("convertAndInsertCustomer");
>>> for (int i = 1; i <= 10000; i++) {
>>> if ((i % 1000) == 0) {
>>> log("Inserting customer " + i);
>>> kSession.fireAllRules();
>>> }
>>>
>>> entryPointConvertAndInsertCustomer.insert(getCustomerInJson(String.valueOf(i)));
>>> }
>>> kSession.fireAllRules();
>>> log("Customers loadaed");
>>> }
>>>
>>> No matter if I call loadPOS() or loadCustomer() first, the first one I
>>> call is executed correctly, but when I call the second one it stops on
>>> fireAllRules() and the memory increases untill OME.If I remove the rule
>>> "rule_suspiciousTransactionInDifferentsPOSOfSameCustomer" from the DRL
>>> file, both the loading executes correctly. It seems that the when the
>>> data
>>> is inserted into the session, the begin of this rule is being executed or
>>> something like that.
>>>
>>> How can I solve this issue?Is there any way of just executing the rules
>>> of loading data at this moment?Is there a better way to achieve what I'm
>>> trying?
>>>
>>>
>>> The full project (maven) is attached to this email.
>>>
>>>
>>> Thanks and sorry for the English.
>>> --
>>> Luciano Davoglio Molinari
>>> http://lucianomolinari.wordpress.com/
>>>
>>> _______________________________________________
>>> rules-users mailing list
>>> rules-users at lists.jboss.org
>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>
>>>
>>
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>>
>
>
> --
> Luciano Davoglio Molinari
>
More information about the rules-users
mailing list