pub struct Action {
pub name: String,
pub action_type: ActionType,
/* private fields */
}Expand description
An action that can be executed during state transitions
Fields§
§name: StringThe name of this action
action_type: ActionTypeThe type of action execution
Implementations§
Source§impl Action
impl Action
Sourcepub fn new<F>(
name: impl Into<String>,
action_type: ActionType,
executor: F,
) -> Self
pub fn new<F>( name: impl Into<String>, action_type: ActionType, executor: F, ) -> Self
Create a new action with a name and executor function
Examples found in repository?
examples/traffic_light.rs (lines 30-32)
18fn create_traffic_light() -> rustate::Result<Machine> {
19 // Create the states
20 let green = State::new("green");
21 let yellow = State::new("yellow");
22 let red = State::new("red");
23
24 // Create the transitions
25 let green_to_yellow = Transition::new("green", "TIMER", "yellow");
26 let yellow_to_red = Transition::new("yellow", "TIMER", "red");
27 let red_to_green = Transition::new("red", "TIMER", "green");
28
29 // Define some actions
30 let log_green = Action::new("logGreen", ActionType::Entry, |_ctx, _evt| {
31 println!("Entering GREEN state - Go!")
32 });
33
34 let log_yellow = Action::new("logYellow", ActionType::Entry, |_ctx, _evt| {
35 println!("Entering YELLOW state - Slow down!")
36 });
37
38 let log_red = Action::new("logRed", ActionType::Entry, |_ctx, _evt| {
39 println!("Entering RED state - Stop!")
40 });
41
42 // Build the machine
43 let machine = MachineBuilder::new("trafficLight")
44 .state(green)
45 .state(yellow)
46 .state(red)
47 .initial("green")
48 .transition(green_to_yellow)
49 .transition(yellow_to_red)
50 .transition(red_to_green)
51 .on_entry("green", log_green)
52 .on_entry("yellow", log_yellow)
53 .on_entry("red", log_red)
54 .build()?;
55
56 Ok(machine)
57}More examples
examples/model_based_testing.rs (lines 143-145)
131fn create_test_machine() -> Machine {
132 // 状態の作成
133 let green = State::new("green");
134 let yellow = State::new("yellow");
135 let red = State::new("red");
136
137 // 遷移の作成
138 let green_to_yellow = Transition::new("green", "TIMER", "yellow");
139 let yellow_to_red = Transition::new("yellow", "TIMER", "red");
140 let red_to_green = Transition::new("red", "TIMER", "green");
141
142 // アクションの定義
143 let log_green = Action::new("logGreen", ActionType::Entry, |_ctx, _evt| {
144 println!("緑信号になりました - 進んでください")
145 });
146
147 let log_yellow = Action::new("logYellow", ActionType::Entry, |_ctx, _evt| {
148 println!("黄信号になりました - 注意してください")
149 });
150
151 let log_red = Action::new("logRed", ActionType::Entry, |_ctx, _evt| {
152 println!("赤信号になりました - 停止してください")
153 });
154
155 // マシンの構築
156 MachineBuilder::new("trafficLight")
157 .state(green)
158 .state(yellow)
159 .state(red)
160 .initial("green")
161 .transition(green_to_yellow)
162 .transition(yellow_to_red)
163 .transition(red_to_green)
164 .on_entry("green", log_green)
165 .on_entry("yellow", log_yellow)
166 .on_entry("red", log_red)
167 .build()
168 .unwrap()
169}examples/hierarchical.rs (lines 74-76)
36fn create_player() -> rustate::Result<Machine> {
37 // Create states
38 let power_off = State::new("powerOff");
39
40 let mut player = State::new_compound("player", "stopped");
41 player.parent = Some("root".to_string());
42
43 let mut stopped = State::new("stopped");
44 stopped.parent = Some("player".to_string());
45
46 let mut playing = State::new_compound("playing", "normal");
47 playing.parent = Some("player".to_string());
48
49 let mut normal = State::new("normal");
50 normal.parent = Some("playing".to_string());
51
52 let mut double_speed = State::new("doubleSpeed");
53 double_speed.parent = Some("playing".to_string());
54
55 let mut paused = State::new("paused");
56 paused.parent = Some("player".to_string());
57
58 // Create transitions
59 let power_toggle = Transition::new("powerOff", "POWER", "player");
60 let power_off_transition = Transition::new("player", "POWER", "powerOff");
61
62 let play = Transition::new("stopped", "PLAY", "playing");
63 let stop = Transition::new("playing", "STOP", "stopped");
64 let pause = Transition::new("playing", "PAUSE", "paused");
65 let resume = Transition::new("paused", "PLAY", "playing");
66
67 let speed_up = Transition::new("normal", "SPEED_UP", "doubleSpeed");
68 let speed_normal = Transition::new("doubleSpeed", "SPEED_NORMAL", "normal");
69
70 let next_track = Transition::internal_transition("playing", "NEXT");
71 let prev_track = Transition::internal_transition("playing", "PREV");
72
73 // Create guards and actions
74 let log_power_on = Action::new("logPowerOn", ActionType::Entry, |_ctx, _evt| {
75 println!("Power ON - Player ready")
76 });
77
78 let log_power_off = Action::new("logPowerOff", ActionType::Entry, |_ctx, _evt| {
79 println!("Power OFF")
80 });
81
82 let log_playing = Action::new("logPlaying", ActionType::Entry, |_ctx, _evt| {
83 println!("Playing track")
84 });
85
86 let log_stopped = Action::new("logStopped", ActionType::Entry, |_ctx, _evt| {
87 println!("Stopped")
88 });
89
90 let log_paused = Action::new("logPaused", ActionType::Entry, |_ctx, _evt| {
91 println!("Paused")
92 });
93
94 let log_double_speed = Action::new("logDoubleSpeed", ActionType::Entry, |_ctx, _evt| {
95 println!("Playing at double speed")
96 });
97
98 let log_normal_speed = Action::new("logNormalSpeed", ActionType::Entry, |_ctx, _evt| {
99 println!("Playing at normal speed")
100 });
101
102 let next_track_action = Action::new("nextTrack", ActionType::Transition, |ctx, _evt| {
103 let current_track = ctx.get::<usize>("track").unwrap_or(0);
104 let next_track = current_track + 1;
105 println!("Changing to track {}", next_track);
106 let _ = ctx.set("track", next_track);
107 });
108
109 let prev_track_action = Action::new("prevTrack", ActionType::Transition, |ctx, _evt| {
110 let current_track = ctx.get::<usize>("track").unwrap_or(0);
111 let prev_track = if current_track > 0 {
112 current_track - 1
113 } else {
114 0
115 };
116 println!("Changing to track {}", prev_track);
117 let _ = ctx.set("track", prev_track);
118 });
119
120 // Create context with initial track
121 let mut context = Context::new();
122 let _ = context.set("track", 0);
123
124 // Create and configure the state machine
125 let mut next_track = next_track;
126 next_track.with_action(next_track_action);
127
128 let mut prev_track = prev_track;
129 prev_track.with_action(prev_track_action);
130
131 let machine = MachineBuilder::new("musicPlayer")
132 .initial("powerOff")
133 .state(power_off)
134 .state(player)
135 .state(stopped)
136 .state(playing)
137 .state(normal)
138 .state(double_speed)
139 .state(paused)
140 .transition(power_toggle)
141 .transition(power_off_transition)
142 .transition(play)
143 .transition(stop)
144 .transition(pause)
145 .transition(resume)
146 .transition(speed_up)
147 .transition(speed_normal)
148 .transition(next_track)
149 .transition(prev_track)
150 .on_entry("player", log_power_on)
151 .on_entry("powerOff", log_power_off)
152 .on_entry("playing", log_playing)
153 .on_entry("stopped", log_stopped)
154 .on_entry("paused", log_paused)
155 .on_entry("doubleSpeed", log_double_speed)
156 .on_entry("normal", log_normal_speed)
157 .context(context)
158 .build()?;
159
160 Ok(machine)
161}Sourcepub fn transition<F>(name: impl Into<String>, executor: F) -> Self
pub fn transition<F>(name: impl Into<String>, executor: F) -> Self
Create a new transition action
Sourcepub fn named(name: impl Into<String>, action_type: ActionType) -> Self
pub fn named(name: impl Into<String>, action_type: ActionType) -> Self
Create a new action with a name only (for serialization)
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Action
impl<'de> Deserialize<'de> for Action
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl IntoAction for Action
impl IntoAction for Action
Source§fn into_action(self, _action_type: ActionType) -> Action
fn into_action(self, _action_type: ActionType) -> Action
Convert into an action
Auto Trait Implementations§
impl Freeze for Action
impl !RefUnwindSafe for Action
impl Send for Action
impl Sync for Action
impl Unpin for Action
impl !UnwindSafe for Action
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more