Reputation: 75
Given the following drools rules:
import java.lang.Double;
import java.util.ArrayList;
declare SensorMessage
@role(event)
@timestamp(timestamp)
@expires(10s)
end
rule "temperature monitoring"
when
$meta: SensorMetadata()
$acc: Double () from accumulate(
$message: SensorMessage() over window:time(1s),
average ($message.getTemperature())
)
then
$meta.setTemperature($acc);
update($meta);
end
rule "temperature activated"
when
$meta: SensorMetadata(highTemperature == false, temperature >= $meta.targetTemperature)
$lastMessage: SensorMessage($lastTimestamp: timestamp)
not (SensorMessage(timestamp > $lastMessage.timestamp))
then
$meta.setLastActivated($lastMessage.getTimestamp());
$meta.setHighTemperature(true);
update($meta);
System.out.printf("temperature activated %.2f%n", $meta.getTemperature());
end
How would I be able to test whether the rule "temperature activated" has been fired x
times exactly? The issue here is, that these rules are over a window, as well as that the message itself expires. So i would like to "simulate" time as well...
Is this possible?
Kind regards and happy holidays!
Upvotes: 1
Views: 102
Reputation: 6322
The best way to test this is by using a Pseudo Clock: https://docs.drools.org/8.31.0.Final/drools-docs/docs-website/drools/rule-engine/index.html#rule-execution-modes-con_rule-engine
The idea is to have a clock that you can advance when you want it and how you want it:
KieSessionConfiguration config = KieServices.Factory.get().newKieSessionConfiguration();
config.setOption( ClockTypeOption.get("pseudo") );
KieSession session = kbase.newKieSession( conf, null );
SessionPseudoClock clock = session.getSessionClock();
session.insert( tick1 );
session.fireAllRules();
clock.advanceTime(1, TimeUnit.SECONDS);
session.insert( tick2 );
session.fireAllRules();
Note that the Kie Session you are using is configured with clockType=pseudo
: https://docs.drools.org/8.31.0.Final/drools-docs/docs-website/drools/KIE/index.html
Hope it helps,
Upvotes: 1