[rules-users] What's wrong with this rule

Wolfgang Laun wolfgang.laun at gmail.com
Mon Nov 1 06:19:11 EDT 2010


Absence of events can only be detected by checking the past.
This means that a trigger must be inserted whenever it's time
to check the last two minutes. This check cannot be done in
a uniform way for all players because the arrival of an EventB
is the start of another 2 minute interval. The solution is based
on a ticker which inserts the trigger (Checker) whenever 120
seconds have gone by. (There are also other techniques to do this.)

declare EventB
  @role( event )
end

declare Checker
  @role( event )
end

rule "ticker 120"
  no-loop true
  timer(int:0s 1s)
when
    $p: Player( $index: index, $ticks: ticks )
then
    if( $ticks == 120 ){
        modify( $p ){ setTicks( 1 ) }
        insert( new Checker( $index ) );
    } else {
        modify( $p ){ setTicks( $ticks + 1 ) }
    }
end

rule "no B for 2 minutes"
when
    $c : Checker( $index: index )
    not EventB( index == $index, this before[0s,2m] $c )
then
    System.out.println( "at " + Main.getClock() + ": no B:" + $index + " in
last 2 minutes" );
    retract( $c );
end

rule "B arrives"
when
    $p : Player( $index: index )
    $b : EventB( index == $index )
then
    System.out.println( "at " + Main.getClock() + ": B:" + $index + ", reset
ticks" );
    modify( $p ){ setTicks( 0 ) }
    retract( $b );
end

2010/10/31 赵侃侃 <kevin223 at gmail.com>

>      this.session = createSession();
>      SessionPseudoClock clock = session.getSessionClock();
>         session.insert( new Player( 1 ) );
>         session.insert( new Player( 2 ) );
>
>         clock.advanceTime( 3*60, TimeUnit.SECONDS );
>         session.insert(new EventB(1));
>         session.fireAllRules();
>
>         clock.advanceTime( 3*60, TimeUnit.SECONDS );
>         session.fireAllRules();
>
>         session.dispose();
>
> The above code gives the following output:
>
> no B:2 in last 2 minutes
> no B:1 in last 2 minutes
>
> what I expect is:
>
> no B:2 in last 2 minutes
> no B:2 in last 2 minutes
> no B:1 in last 2 minutes
>
> Any ideas?
>
> Best Regards,
> Kevin Zhao
>
> 2010/10/29 Wolfgang Laun <wolfgang.laun at gmail.com>
>
>>     System.out.println( "Test no B" );
>>     session = kBase.newStatefulKnowledgeSession( config, null );
>>     clock = session.getSessionClock();
>>     session.insert( new Player( 1 ) );
>>     clock.advanceTime( 3*60, TimeUnit.SECONDS );
>>     session.fireAllRules();
>>     session.dispose();
>>
>> The rule fires with this test, (kBase in STREAM mode).
>> -W
>>
>> 2010/10/29 赵侃侃 <kevin223 at gmail.com>
>>
>> The "no B:x for 2 minutes" will not fire if nothing comes in even after
>>> 2minutes. e.g. if no characters get killed then no events will come in.
>>> If I add a timer to this rule, say like timer (2m 1s), then the rule will
>>> fire every second after 2 minutes.
>>>
>>> Best Regards,
>>> Kevin
>>>
>>> 2010/10/29 Wolfgang Laun <wolfgang.laun at gmail.com>
>>>
>>>> OK; your original version somehow put me off the right track.
>>>> This rule will fire as soon as there is no B:n according to the
>>>> Player index for 2 minutes.
>>>>
>>>> rule "no B:x for 2 minutes"
>>>> when
>>>>     $b : Player( $index: index )
>>>>     not ( EventB( index == $index) over window:time( 2m ) )
>>>> then
>>>>     System.out.println( "no B:" + $index + " in last 2 minutes" );
>>>> end
>>>>
>>>> This would fire whenever there's no B at all for 2 minutes:
>>>>
>>>> rule "no B for 2 minutes"
>>>> when
>>>>     not ( EventB() over window:time( 2m ) )
>>>> then
>>>>     System.out.println( "no B:" + $index + " in last 2 minutes" );
>>>> end
>>>>
>>>>
>>>>
>>>> 2010/10/28 赵侃侃 <kevin223 at gmail.com>
>>>>
>>>>> My last mail should actually read:
>>>>>
>>>>> A player may have multiple characters and the rule should fire when *
>>>>> ALL* of its characters live longer than 2 minutes.  that's why
>>>>> "characters" do not have a unique identification and  there isn't any event
>>>>> representing a character birth.
>>>>>
>>>>> Best Regards,
>>>>> Kevin Zhao
>>>>>
>>>>> 在 2010年10月28日 下午9:50,赵侃侃 <kevin223 at gmail.com>写道:
>>>>>
>>>>> A player may have multiple characters and the rule should fire when any
>>>>>> of its characters live longer than 2 minutes.  that's why "characters" do
>>>>>> not have a unique identification and  there isn't any event representing a
>>>>>> character birth.
>>>>>>
>>>>>> Best Regards,
>>>>>> Kevin Zhao
>>>>>>
>>>>>> 2010/10/28 Wolfgang Laun <wolfgang.laun at gmail.com>
>>>>>>
>>>>>>> If EventB( index == 3 ) signifies that the "character" owned by
>>>>>>> player 3 has been killed: what is the event that this character has been
>>>>>>> born? Life is the time between birth and death; these two are well-defined
>>>>>>> (well, mostly) events, and they ought to be represented by clean-cut events
>>>>>>> in any application. Then it's no problem to write rules firing when a
>>>>>>> "character" lives longer or shorter than any time. Also, "characters" may
>>>>>>> have to have a unique identification beyond their player-owner.
>>>>>>>
>>>>>>> If you are constantly shifting your specs, we won't be getting any
>>>>>>> closer to a solution, though.
>>>>>>>
>>>>>>>
>>>>>>> -W
>>>>>>>
>>>>>>>
>>>>>>> 2010/10/28 赵侃侃 <kevin223 at gmail.com>
>>>>>>>
>>>>>>>> To be honest, I don't quite understand the rules you wrote. Let me
>>>>>>>> explain this a little bit with a real world scenario.
>>>>>>>> Assume this is an on-line game that EventB indicates a 'kill' event
>>>>>>>> that a player is losing its character who might be killed by some other
>>>>>>>> player.
>>>>>>>> The property index points to the player who owns this character.
>>>>>>>> What I'm looking for here is to find out when a player's character survives
>>>>>>>> in 2 minutes. The number of players in a game is at least 2 but can be up to
>>>>>>>> any number.
>>>>>>>>
>>>>>>>> Best Regards,
>>>>>>>> Kevin Zhao
>>>>>>>>
>>>>>>>> 2010/10/28 Wolfgang Laun <wolfgang.laun at gmail.com>
>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> 2010/10/28 赵侃侃 <kevin223 at gmail.com>
>>>>>>>>>
>>>>>>>>>> Hello Wolfgang,
>>>>>>>>>>
>>>>>>>>>> Index is just like the index in an array. the value can be any
>>>>>>>>>> from 0 to the array length.
>>>>>>>>>> for the previous example, the length is 1 so index can be either 0
>>>>>>>>>> or 1. but in the real case, the length can be an arbitrary number.
>>>>>>>>>>
>>>>>>>>>> another question, there are 2 rules here, do both of them have to
>>>>>>>>>> be applied?
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>> One creates and inserts the PatternConsumer which blocks repeated
>>>>>>>>> usage of the pair of EventB facts that have been successfully paired.
>>>>>>>>> Otherwise a sequence EventB:0, EventB:1, EventB:2 would fire 2 times.
>>>>>>>>>
>>>>>>>>> If the positive condition is more complex, e.g., you need *all*
>>>>>>>>> index values 0,...,L-1 within 2m, then other conditions will be required
>>>>>>>>> (and that's what I meant with "more precisesly"). If any pair a,b from
>>>>>>>>> [0..L-1] will do, then the modified version (!=) should be OK.
>>>>>>>>>
>>>>>>>>> -W
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> Best Regards,
>>>>>>>>>> Kevin Zhao
>>>>>>>>>>
>>>>>>>>>> 2010/10/28 Wolfgang Laun <wolfgang.laun at gmail.com>
>>>>>>>>>>
>>>>>>>>>> Kindly state your requirements precisely.
>>>>>>>>>>>
>>>>>>>>>>> Perhaps
>>>>>>>>>>>    not ( EventB ( index != $index,... )
>>>>>>>>>>> is what you need.
>>>>>>>>>>> -W
>>>>>>>>>>>
>>>>>>>>>>> 2010/10/28 赵侃侃 <kevin223 at gmail.com>
>>>>>>>>>>>
>>>>>>>>>>> I haven't tested this rule, but what about the case that property
>>>>>>>>>>>> index would have arbitrary number of possible values?
>>>>>>>>>>>>
>>>>>>>>>>>> 2010/10/27 Wolfgang Laun <wolfgang.laun at gmail.com>
>>>>>>>>>>>>
>>>>>>>>>>>>> Omitting the Entry  Points:
>>>>>>>>>>>>>
>>>>>>>>>>>>> rule "B-0-1-not OK"
>>>>>>>>>>>>> when
>>>>>>>>>>>>>    $b : EventB( $index: index )
>>>>>>>>>>>>>    not ( PatternConsumer( id == "B01", events contains $b ) )
>>>>>>>>>>>>>    not ( EventB( index == (1 - $index), this after[0s,2m] $b )
>>>>>>>>>>>>> )
>>>>>>>>>>>>> then
>>>>>>>>>>>>>    System.out.println( "B:" + $index + ", but no B:" +
>>>>>>>>>>>>> (1-$index) );
>>>>>>>>>>>>> end
>>>>>>>>>>>>>
>>>>>>>>>>>>> rule "B-0-1"
>>>>>>>>>>>>> when
>>>>>>>>>>>>>    $b1 : EventB( $index: index )
>>>>>>>>>>>>>    $b2 : EventB( index == (1 - $index), this after[0s,2m] $b1 )
>>>>>>>>>>>>> then
>>>>>>>>>>>>>    insert( new PatternConsumer( "B01", $b1, $b2 ) );
>>>>>>>>>>>>>    System.out.println( "B:" + $index + "+B:" + (1-$index) );
>>>>>>>>>>>>> end
>>>>>>>>>>>>>
>>>>>>>>>>>>> -W
>>>>>>>>>>>>>
>>>>>>>>>>>>> 2010/10/27 赵侃侃 <kevin223 at gmail.com>:
>>>>>>>>>>>>> > Hello,
>>>>>>>>>>>>> > With the help from the community I managed to get my first
>>>>>>>>>>>>> rule working, and
>>>>>>>>>>>>> > I'm trying to write my second rule on my own but it just
>>>>>>>>>>>>> doesn't seem to
>>>>>>>>>>>>> > work correctly.
>>>>>>>>>>>>> > Here is the scenario, what I want is to identify a pattern
>>>>>>>>>>>>> that there's no
>>>>>>>>>>>>> > EventB coming in within 2 minutes with a particular index
>>>>>>>>>>>>> value.
>>>>>>>>>>>>> > For example, EventB would have a property named index and
>>>>>>>>>>>>> assume the value
>>>>>>>>>>>>> > of index would be either 0 or 1.
>>>>>>>>>>>>> > Before firing the rules, I would manually insert facts of
>>>>>>>>>>>>> possibleIndex with
>>>>>>>>>>>>> > value 0 and 1 into the workingMemory.
>>>>>>>>>>>>> > Within 2 minutes, if there only comes one EventB with index
>>>>>>>>>>>>> valued 0 then
>>>>>>>>>>>>> > the system should report no EventB coming in with index value
>>>>>>>>>>>>> 1 in last 2
>>>>>>>>>>>>> > minutes.
>>>>>>>>>>>>> > Vice versa, in the case of only coming one EventB with index
>>>>>>>>>>>>> valued 1 then
>>>>>>>>>>>>> > the system should report no EventB coming in with index value
>>>>>>>>>>>>> 0 in last 2
>>>>>>>>>>>>> > minutes.
>>>>>>>>>>>>> > If within 2 minutes, there comes 2 EventB with both value 0
>>>>>>>>>>>>> and 1 then
>>>>>>>>>>>>> > nothing should report.
>>>>>>>>>>>>> > Here is what I wrote, but it doesn't seem to work correctly.
>>>>>>>>>>>>> > I used a timer to fire this rule every 10 seconds because I
>>>>>>>>>>>>> don't think the
>>>>>>>>>>>>> > rule would run automatically if I don't add that. (not too
>>>>>>>>>>>>> sure though)
>>>>>>>>>>>>> > rule "no B in 2 minutes"
>>>>>>>>>>>>> > timer (0 10s)
>>>>>>>>>>>>> > when
>>>>>>>>>>>>> > possibleIndex( $index : index ) from entry-point "Event
>>>>>>>>>>>>> stream"
>>>>>>>>>>>>> > $p : PatternConsumer ( name == 'no B' && index == $index )
>>>>>>>>>>>>> > not ( EventB( index == $index && this after[0ms,2m] $p) over
>>>>>>>>>>>>> window:time(2m)
>>>>>>>>>>>>> > from entry-point "Event stream" )
>>>>>>>>>>>>> > then
>>>>>>>>>>>>> > PatternConsumer pc = new PatternConsumer( "no B", $index );
>>>>>>>>>>>>> > insert(pc);
>>>>>>>>>>>>> > System.out.println("no B in 2 minutes " + $index);
>>>>>>>>>>>>> > end
>>>>>>>>>>>>> > Best Regards,
>>>>>>>>>>>>> > Kevin Zhao
>>>>>>>>>>>>> > _______________________________________________
>>>>>>>>>>>>> > 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
>>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>>
>>>>>>>>>>>> _______________________________________________
>>>>>>>>>>>> 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
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> _______________________________________________
>>>>>>>>>> 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
>>>>>>>>>
>>>>>>>>>
>>>>>>>>
>>>>>>>> _______________________________________________
>>>>>>>> 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
>>>>>>>
>>>>>>>
>>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> 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
>>>>
>>>>
>>>
>>> _______________________________________________
>>> 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
>>
>>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20101101/48670425/attachment.html 


More information about the rules-users mailing list