Expand description
Trigger criteria that emit user-defined events.
A trigger is a way to say: when some simulation criterion is met, emit a
concrete user-defined IxaEvent. Trigger-emitted events
are ordinary Ixa events, so any number of subscribers can listen for them
with Context::subscribe_to_event.
A trigger criterion is not itself registered on a context. A criterion, such
as PropertyChangeTrigger or TimeTrigger, defines what should be
monitored. A complete trigger is created only after binding that criterion
to a concrete event with one of the emit_* methods. The value returned by
emit_with, emit_value, or emit_default is the value passed to
ContextTriggersExt::register_trigger.
The usual flow is:
- Choose one of the built-in trigger criteria.
- Bind it to the event you want emitted with
TriggerCriterion::emit_with,TriggerCriterion::emit_value, orTriggerCriterion::emit_default. - Register the complete trigger with
ContextTriggersExt::register_trigger. - Subscribe to the emitted user event as usual.
TogglingTrigger is a composite trigger for stateful on/off behavior. Instead of binding a
single criterion to a single event, it combines an activation criterion and a deactivation
criterion, each with its own emitted event (of possibly distinct types).
The usual flow for a toggling trigger is:
- Choose the activation and deactivation criteria.
- Pair them with
TogglingTriggerCriteria::new. - Bind the pair to activation and deactivation events with one of the
TogglingTriggerCriteria::emit_*methods. - Register the complete trigger with
ContextTriggersExt::register_trigger. - Subscribe to the activation and deactivation events as usual.
§Construct an event from observation data
Each trigger criterion has its own observation data type, available as the criterion’s
TriggerCriterion::Observation associated type. For example, PropertyChangeTrigger
observations use PropertyChangeTriggerEvent containing the entity ID and the previous and
current property values. EntityCountTrigger, PropertyValueCountTrigger, TimeTrigger,
and PeriodicTimeTrigger use their corresponding *TriggerEvent types.
For events that do not need observation data, use TriggerCriterion::emit_value to emit
a constant event value, or TriggerCriterion::emit_default when the event type implements
Default.
Use TriggerCriterion::emit_with when the emitted event should contain data from the trigger
observation. When the criterion is met, this observation value is passed to the event
constructor (typically a closure or static constructor method) supplied to emit_with, and that
constructor returns the concrete user-defined IxaEvent that subscribers
will receive.
use ixa::{Context, define_entity, define_property, IxaEvent};
use ixa::entity::EntityId;
use ixa::triggers::{ContextTriggersExt, PropertyChangeTrigger, TriggerCriterion};
define_entity!(Person);
define_property!(struct Alive(bool), Person, default_const = Alive(true));
#[derive(IxaEvent)]
struct FirstDeath {
person: EntityId<Person>,
}
let mut context = Context::new();
context.register_trigger(
PropertyChangeTrigger::to(Alive(false))
.once()
.emit_with(|event| FirstDeath {
person: event.entity_id,
}),
);
context.subscribe_to_event(|_context, _event: FirstDeath| {
// perform cleanup tasks
});Structs§
- Entity
Count Trigger - Entity
Count Trigger Event - Periodic
Time Trigger - Periodic
Time Trigger Event - Property
Change Trigger - Property
Change Trigger Event - Property
Value Count Trigger - Property
Value Count Trigger Event - Time
Trigger - Time
Trigger Event - Toggling
Trigger - A complete installable trigger specification that emits activation and deactivation events when its paired criteria cause state changes.
- Toggling
Trigger Criteria - A pair of activation and deactivation criteria that can be bound to emitted events.
- Trigger
- A criterion bound to a user event constructor. Values of this type are not constructed directly
but rather are returned by the
emit_*methods on trigger criterion types. These values are “complete” triggers than can be “installed” on a context withcontext.register_trigger.
Enums§
- Direction
- Direction in which a count changed to reach a threshold.
- Trigger
Mode - Whether a trigger emits once or every time its criterion is satisfied.
Traits§
- Context
Triggers Ext - Extension trait for registering triggers on a
Context. - Trigger
Criterion - A bare trigger criterion: the condition that can be monitored. This module provides a collection of types that implement this trait.
- Trigger
Spec - A complete installable trigger specification that can be passed to
context.register_trigger. This is automatically implemented by theTriggertypes returned by theemit_*methods on trigger criterion types. Client code should not implement this themselves.