Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[core][util][sre][docs] Add filter for registerBehavior.
close #252

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Feb 9, 2017
1 parent 8af5850 commit 9f1fb91
Show file tree
Hide file tree
Showing 12 changed files with 806 additions and 221 deletions.
Expand Up @@ -1223,6 +1223,16 @@ describe "Built-in Capacity Reference" {
* When a behavior is registered, it is receiving the events
* in the default space of the inner context of the agent, or
* received by the agent itself.
*
* <p>An example of call to the registration function is:
*
* var beh = new MyBehavior
* registerBehavior(beh)
*
* <p>According to the SARL syntax reference, the example could be also written as:
*
* var beh = new MyBehavior
* beh.registerBehavior
*
* @filter(.*)
*/
Expand Down Expand Up @@ -1277,6 +1287,55 @@ describe "Built-in Capacity Reference" {
}".parseSuccessfully
}

/* Assuming that a behavior was already defined,
* it is possible for an agent to register this behavior that may received only the events
* matching a specific filtering function. For registering such a behavior with its filter,
* the following function could be used:
*
* def registerBehavior(attitude : Behavior, filter : (Event) -> boolean) : Behavior
*
*
* <p>This function takes the behavior to be registered, and replies the
* same behavior.
* When a behavior is registered, it is receiving the events that are matching the given
* filter in the default space of the inner context of the agent, or
* received by the agent itself.
* The filtering function is invoked for each event that should be given to the behavior.
* If the filtering function replies {@code true}, the event is really dispatching into the
* behavior. If the function replies {@code false}, the event is discarded to the behavior.
*
* <p>An example of call to the registration function is:
*
* var beh = new MyBehavior
* registerBehavior(beh, [event | event instanceof MyEvent])
*
* <p>According to the SARL syntax reference, the example could be also written as:
*
* var beh = new MyBehavior
* beh.registerBehavior [event | event instanceof MyEvent]
*
* @filter(.*)
*/
fact "Registering a Behavior with an event filter"{
// Test the URL in the introduction of this section
"BehaviorReferenceSpec.html" should beAccessibleFrom this
//
" package io.sarl.docs.reference.bic
import io.sarl.core.Behaviors
import io.sarl.lang.core.Behavior
behavior B {
}
agent A {
uses Behaviors
def myaction {
var b : B
var c : Behavior
b = new B(this)
c = registerBehavior(b) [true]
}
}".parseSuccessfully
}

/* A behavior is executed through its event handlers.
* Consequently, for running a behavior, it is mandatory
* to wake it with an event. This particular feature is
Expand Down
8 changes: 7 additions & 1 deletion eclipse-sarl/plugins/io.sarl.core/src/io/sarl/core/bic.sarl
Expand Up @@ -233,10 +233,16 @@ capacity Behaviors {
* Register a Behavior for the owner agent.
* The new Behavior will react to all declared events received by the agent.
*
* <p>If the filter is provided, it will be used for determining if the given behavior accepts a specific event.
* If the filter function replies {@code true} for a specific event as argument, the event is fired in the
* behavior context. If the filter function replies {@code false}, the event is not fired in the behavior context.
*
* @param attitude the behavior to add to the agent.
* @param filter the filter that enables to indicates if an event is accepted by a behavior.
* @return the given behavior.
* @since 0.5 for the {@code filter} parameter.
*/
def registerBehavior(attitude : Behavior) : Behavior
def registerBehavior(attitude : Behavior, filter : (Event) => boolean = null) : Behavior

/**
* Unregisters a behavior for the owner agent.
Expand Down

0 comments on commit 9f1fb91

Please sign in to comment.