package org.example;
import java.util.Date;
declare Thing
end
declare IDProperty
subject : Thing
object : int
end
declare StatusEvent
@role(event)
@timestamp(timestamp)
thingId : int
timestamp : Date
end
rule init
when
then
Thing t = new Thing();
insert (t);
insert (new IDProperty(t, 1));
insert (new StatusEvent(1, new Date(0)));
insert (new StatusEvent(1, new Date(1)));
end
rule notLast1
when
$t : Thing()
IDProperty(subject == $t, $id : object)
// Constraint
$ev : StatusEvent(thingId == $id)
exists(
// Constraint
StatusEvent(this after $ev, $id == thingId)
)
then
System.out.println("(1) not last " + $ev);
end
rule notLast2
when
$t : Thing()
IDProperty(subject == $t, $id : object)
// Constraint
$ev : StatusEvent(thingId == $id)
exists(
// Eval
StatusEvent(this after $ev, $evId : thingId)
and
eval($evId == $id)
)
then
System.out.println("(2) not last " + $ev);
end
rule notLast3
when
$t : Thing()
IDProperty(subject == $t, $id : object)
// Eval
$ev : StatusEvent()
eval($ev.getThingId() == $id)
exists(
// Constraint
StatusEvent(this after $ev, $id == thingId)
)
then
System.out.println("(3) not last " + $ev);
end
rule notLast4
when
$t : Thing()
IDProperty(subject == $t, $id : object)
// Eval
$ev : StatusEvent()
eval($ev.getThingId() == $id)
exists(
// Eval
StatusEvent(this after $ev, $evId : thingId)
and
eval($evId == $id)
)
then
System.out.println("(4) not last " + $ev);
end
Output:
(1) not last StatusEvent( thingId=1, timestamp=Thu Jan 01 01:00:00 CET 1970 )
(2) not last StatusEvent( thingId=1, timestamp=Thu Jan 01 01:00:00 CET 1970 )
(3) not last StatusEvent( thingId=1, timestamp=Thu Jan 01 01:00:00 CET 1970 )
Rule "notLast4" doesn't fire