Drools Firing Rules for all event when loading persisted session - drools

I am storing drools session using KieMarshler. When I am loading the persisted session. Drools is firing all the rule for all the event that is there in the loaded session. Below is the rule:
rule "1"
when
TestEvent()
then
System.out.println("Rule 1 Fired");
end

Related

How to guard/lock entites in reactive pipelines between each subscription

How do I lock an entity in a Reactive context between each subscription?
I have an endpoint to cancel an order. In the flow, I call some other services to cancel promotion and so on, and at last, I will mark Order in cancelled status and persist it.
... // order is retrieved by some repository
return Flux.concat(
cartService.defrost(order.getCartId()), // 1
promotionsService.cancel(order.getRedemption()), // 2
Mono.fromRunnable(() -> orderService.cancelOrder(order)), // 3
).then();
I want to make sure that when concurrent calls come in(another subscription in the Reactive pipeline)and another thread is launched to check and modify the status of the same order, it would not call external services again. I think dirty writes is OK when I already completed step 3, but when transaction 2 comes in when transaction 1 only reaches step 1 and 2, it's still too early.
Do I have some operators in Reactor 3, or I can only rely on pessimistic locking in Postgres? And BTW, if I really want to lock on row level, setting the isolation level will only be a hint for Postgresql 11 or will lock it indeed?

Drools and Multithreading

I am executing Drools in a multi-threaded application. Often times, I am seeing this warning in the console:
2018-01-24 09:17:16.223 AM [drools-worker-4] WARN o.d.c.rule.constraint.MvelConstraint - Exception jitting: driveable == 'Y' This is NOT an error and NOT prevent the correct execution since the constraint will be evaluated in intrepreted mode.
Does it mean the constraint order for evaluation needs to be changed? Any insights on this will be helpful.
I am using Drools 6.5.0.Final.
Application: Spring Boot with Thread pool task executor.
KSession is not thread safe. Behavior is unpredictable in multi-threaded app.
So option # 1: Make it thread local. Do not forget to retract all facts after fireAllRules()
Option # 2: Create new KSession each time in order to fireAllRules(). Do not forget to dispose it afterwards.

Drools Fusion Event Processing LRU eviction

I am using Drools Fusion for processing large scale events of millions of users.
Since volume of events is huge , it will lead to out of memory error if I keep on adding incoming events in kie session.
Each event is associated with User. Is it possible to evict events from session based on user's LRU policy ?
Any sample code will be helpful.
Thanks
I think you could write some rules that will delete the events from working memory based on user's LRU policy.
e.g. something like if event has the policy in it
rule "retract event"
when
User($userPolicy: policy == somePolicy)
$event: Event(policy == $userPolicy)
then
delete($event);
end

#expires vs. retract and Memory Management

1) I have setup a small program inserting a event to my key session running in STREAM mode. I have created a timer rule causing halt() after 5 seconds. Running this program with fireUntilHalt() and printing kieSession.getFactCount() shows me that there are still 1 fact in the working memory.
2) Next i declare the event with #expires(4s) and run the program again this time there is no facts left in the working memory as expected.
3) Next i created a rule using the temporal condition that no other events has been inserted within 2 seconds causing the rule to retract the event. Again as expected there 0 facts in the working memory as expected.
4) Next i created a rule inserting an event 1 second after the first event. This time only one is retracted because the second event is within 2 seconds of the first one, but Memory Management apparently retracts the first event because it can no longer match any rule due to its temporal constraints. Again there are 0 facts left in the working memory.
One of the benefits of running the engine in STREAM mode is that the engine can detect when an event can no longer match any rule due to its temporal c onstraints. When that happens, the engine can safely delete the event from the session without side effects and release any resources used by that event.
Through out these 4 tests i added an eventListener(), reporting insertions and retractions and i also set up a logger for the audit view in eclipse.
My question is: Why does both the eventListener() and the Audit view not report when a fact is expired or handled by the Memory Management system.
1) In the first test obviously nothing was retracted thus the reports are concise.
2) The second test did not report any retraction, but 0 facts was left in working memory although one fact was inserted.
3) The third test reported a retraction, thus the report are concise.
4) The fourth test reported a retraction but 2 facts was actually "retracted" thus the report was in-concise.
Does another solution exist to report expired facts and memory management actions??

Error with pseudo clock in drools when I have two rules matching different events

I want to test drools 6.3 with a scenario, But I have a problem in a special situation.
Here is my scenario in a simple form:
I have two systems, A and B, in a simulated Network that generate events. I want to write two rules to find out patterns in these events. Two rules for testing this scenario is:
declare A
#timestamp(timestampA)
end
declare B
#timestamp(timestampB)
end
Rule “1”
When
accumulate( A() over window:time( 10s ) ; s:count(1) ; s>1)
Then
System.out.println( " Rule 1 matched " );
Rule “2”
When
B()
Then
System.out.println( " Rule 2 matched " );
Timestamp of each event is the timestamp from log generated in each system on when received by drools and inserted in working memory.
I’m using STREAM mode with pseudo clock, because events from System B receives with 25min delay due to network congestion and I should adjust session clock manually. Session clock set with the timestamp of every event inserted into the session. And All rules fire when every event inserted.
When order of receiving and inserting events are like below matched correctly.
Event A received at 10:31:21 – Session clock : 10:31:21 – insert A and fire
Event A received at 10:31:23 - Session clock : 10:31:23 – insert A and fire
Rule 1 matched
Event B received at 10:06:41 - Session clock : 10:06:41 – insert B and fire
Rule 2 matched
But when order of receiving and inserting events are like below matched incorrectly:
Event A received at 10:31:21 – Session clock : 10:31:21 – insert A and fire
Event B received at 10:06:41 - Session clock : 10:06:41 – insert B and fire
Rule 2 matched
Event A received at 10:31:23 - Session clock : 10:31:23 – insert A and fire
When second A event inserted tow A events in last 10s are in working memory but rule 1 does not match. Why?
What you are doing is somewhat in conflict with the assumptions underlying the CEP (Continuous Event Processing) of Drools. STREAM mode implies that events should be inserted in the order of their timestamps, irrespective of their origin. Setting the pseudo clock back and forth in big jumps is another good way to confuse the Engine.
Don't use STREAM mode, window:time and forget about session clocks.
You have facts containing time stamps, and you can easily write your rules by referring to these time stamps, either using plain old arithmetic or by applying the temporal operators (which are nothing but syntactic sugar for testing the relation of long (as in java.lang) values.