FSM Documentation

This commit is contained in:
FlightControl 2017-01-05 08:34:06 +01:00
parent 29401e0c95
commit 3017f10906
10 changed files with 337 additions and 59113 deletions

View File

@ -29,7 +29,7 @@
-- * To handle **State** moments, create methods starting with OnLeave or OnEnter concatenated with the State name.
-- * To handle **Event** moments, create methods starting with OnBefore or OnAfter concatenated with the Event name.
--
-- ** The OnLeave and OnBefore transition methods may return false to cancel the transition.**
-- **The OnLeave and OnBefore transition methods may return false, which will cancel the transition.**
--
-- ## 1.2) Event Triggers
--
@ -50,6 +50,85 @@
--
-- The initial state can be defined using the method @{#FSM.SetStartState}(). The default start state of an FSM is "None".
--
-- ### Example
--
-- This example creates a new FsmDemo object from class FSM.
-- It will set the start state of FsmDemo to Green.
-- 2 Transition Rules are created, where upon the event Switch,
-- the FsmDemo will transition from state Green to Red and vise versa.
--
-- local FsmDemo = FSM:New() -- #FsmDemo
-- FsmDemo:SetStartState( "Green" )
-- FsmDemo:AddTransition( "Green", "Switch", "Red" )
-- FsmDemo:AddTransition( "Red", "Switch", "Green" )
--
-- In the above example, the FsmDemo could flare every 5 seconds a Green or a Red flare into the air.
-- The next code implements this through the event handling method **OnAfterSwitch**.
--
-- function FsmDemo:OnAfterSwitch( From, Event, To, FsmUnit )
-- self:E( { From, Event, To, FsmUnit } )
--
-- if From == "Green" then
-- FsmUnit:Flare(FLARECOLOR.Green)
-- else
-- if From == "Red" then
-- FsmUnit:Flare(FLARECOLOR.Red)
-- end
-- end
-- FsmDemo:__Switch( 5, FsmUnit ) -- Trigger the next Switch event to happen in 5 seconds.
-- end
--
-- FsmDemo:__Switch( 5, FsmUnit ) -- Trigger the first Switch event to happen in 5 seconds.
--
-- The OnAfterSwitch implements a loop. The last line of the code fragment triggers the Switch Event within 5 seconds.
-- Upon the event execution (after 5 seconds), the OnAfterSwitch method is called of FsmDemo (cfr. the double point notation!!! ":").
-- The OnAfterSwitch method receives from the FSM the 3 transition parameter details ( From, Event, To ),
-- and one additional parameter that was given when the event was triggered, which is in this case the Unit that is used within OnSwitchAfter.
--
-- function FsmDemo:OnAfterSwitch( From, Event, To, FsmUnit )
--
-- For debugging reasons the received parameters are traced within the DCS.log.
--
-- self:E( { From, Event, To, FsmUnit } )
--
-- The method will check if the From state received is either "Green" or "Red" and will flare the respective color from the FsmUnit.
--
-- if From == "Green" then
-- FsmUnit:Flare(FLARECOLOR.Green)
-- else
-- if From == "Red" then
-- FsmUnit:Flare(FLARECOLOR.Red)
-- end
-- end
--
-- It is important that the Switch event is again triggered, otherwise, the FsmDemo would stop working after having the first Event being handled.
--
-- FsmDemo:__Switch( 5, FsmUnit ) -- Trigger the next Switch event to happen in 5 seconds.
--
-- This example is fully implemented in the following MOOSE test mission:
--
--
-- Note that transition rules can be declared with a few variations:
--
-- * The From states can be a table of strings, indicating that the transition rule will be valid if the current state of the FSM will be one of the given From states.
-- * The From state can be a "*", indicating that the transition rule will always be valid, regardless of the current state of the FSM.
--
-- This transition will create a new FsmDemo object from class FSM.
-- It will set the start state of FsmDemo to Green.
-- A new event is added in addition to the above example.
-- The new event Stop will cancel the Switching process.
-- So, the transtion for event Stop can be executed if the current state of the FSM is either "Red" or "Green".
--
-- local FsmDemo = FSM:New() -- #FsmDemo
-- FsmDemo:SetStartState( "Green" )
-- FsmDemo:AddTransition( "Green", "Switch", "Red" )
-- FsmDemo:AddTransition( "Red", "Switch", "Green" )
-- FsmDemo:AddTransition( { "Red", "Green" }, "Stop", "Stopped" )
--
-- The transition for event Stop can also be simplified, as any current state of the FSM is valid.
--
-- FsmDemo:AddTransition( "*", "Stop", "Stopped" )
--
-- ## 1.4) FSM Process Rules
--
-- The FSM can implement sub-processes that will execute and return multiple possible states.
@ -385,8 +464,8 @@ do -- FSM
self:T3( { onafter = "onafter" .. EventName, callback = self["onafter" .. EventName] } )
self:_call_handler("onafter" .. EventName, params)
self:T3( { On = "OnAfter" .. to, callback = self["OnAfter" .. to] } )
ReturnValues = self:_call_handler("OnAfter" .. to, params )
self:T3( { On = "OnAfter" .. EventName, callback = self["OnAfter" .. EventName] } )
ReturnValues = self:_call_handler("OnAfter" .. EventName, params )
end
self:_call_handler("onstatechange", params)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,122 @@
--- Transition Explanation
--
-- ===
--
-- Name: Transition Explanation
-- Author: FlightControl
-- Date Created: 05 Jan 2017
--
-- # Situation:
--
-- Create a simple FSM.
-- Add 2 transitions that will switch state from "Green" to "Red" upon event "Switch".
--
-- # Test cases:
--
-- # Status: TESTED 05 Jan 2017
local FsmDemo = FSM:New() -- #FsmDemo
local FsmUnit = UNIT:FindByName( "FlareUnit" )
FsmDemo:SetStartState( "Green" )
do FsmDemo:AddTransition( "Green", "Switch", "Red" ) -- FSM Transition for type #FsmDemo.
--- OnLeave State Transition for Green.
-- @function [parent=#FsmDemo] OnLeaveGreen
-- @param #FsmDemo self
-- @param #string From The From State string.
-- @param #string Event The Event string.
-- @param #string To The To State string.
-- @return #boolean Return false to cancel Transition.
--- OnEnter State Transition for Red.
-- @function [parent=#FsmDemo] OnEnterRed
-- @param #FsmDemo self
-- @param #string From The From State string.
-- @param #string Event The Event string.
-- @param #string To The To State string.
--- OnBefore State Transition for Switch.
-- @function [parent=#FsmDemo] OnBeforeSwitch
-- @param #FsmDemo self
-- @param #string From The From State string.
-- @param #string Event The Event string.
-- @param #string To The To State string.
-- @return #boolean Return false to cancel Transition.
--- OnAfter State Transition for Switch.
-- @function [parent=#FsmDemo] OnAfterSwitch
-- @param #FsmDemo self
-- @param #string From The From State string.
-- @param #string Event The Event string.
-- @param #string To The To State string.
--- Embedded Event Trigger for Switch.
-- @function [parent=#FsmDemo] Switch
-- @param #FsmDemo self
--- Delayed Event Trigger for Switch
-- @function [parent=#FsmDemo] __Switch
-- @param #FsmDemo self
-- @param #number Delay The delay in seconds.
end -- FsmDemo
do FsmDemo:AddTransition( "Red", "Switch", "Green" ) -- FSM Transition for type #FsmDemo.
--- OnLeave State Transition for Red.
-- @function [parent=#FsmDemo] OnLeaveRed
-- @param #FsmDemo self
-- @param #string From The From State string.
-- @param #string Event The Event string.
-- @param #string To The To State string.
-- @return #boolean Return false to cancel Transition.
--- OnEnter State Transition for Green.
-- @function [parent=#FsmDemo] OnEnterGreen
-- @param #FsmDemo self
-- @param #string From The From State string.
-- @param #string Event The Event string.
-- @param #string To The To State string.
--- OnBefore State Transition for Switch.
-- @function [parent=#FsmDemo] OnBeforeSwitch
-- @param #FsmDemo self
-- @param #string From The From State string.
-- @param #string Event The Event string.
-- @param #string To The To State string.
-- @return #boolean Return false to cancel Transition.
--- OnAfter State Transition for Switch.
-- @function [parent=#FsmDemo] OnAfterSwitch
-- @param #FsmDemo self
-- @param #string From The From State string.
-- @param #string Event The Event string.
-- @param #string To The To State string.
--- Embedded Event Trigger for Switch.
-- @function [parent=#FsmDemo] Switch
-- @param #FsmDemo self
--- Delayed Event Trigger for Switch
-- @function [parent=#FsmDemo] __Switch
-- @param #FsmDemo self
-- @param #number Delay The delay in seconds.
end -- FsmDemo
function FsmDemo:OnAfterSwitch( From, Event, To, FsmUnit )
self:E( { From, Event, To, FsmUnit } )
if From == "Green" then
FsmUnit:Flare(FLARECOLOR.Green)
else
if From == "Red" then
FsmUnit:Flare(FLARECOLOR.Red)
end
end
FsmDemo:__Switch( 5, FsmUnit )
end
FsmDemo:__Switch( 5, FsmUnit )

View File

@ -2411,7 +2411,6 @@ The UNIT carrying the package.</p>
<dl class="function">
<dt>
<em></em>
<a id="#(AI_CARGO_UNIT).CargoCarrier" >
<strong>AI_CARGO_UNIT.CargoCarrier</strong>
</a>

View File

@ -105,7 +105,7 @@ These methods define the flow of the FSM process; because in those methods the F
<li>To handle <strong>Event</strong> moments, create methods starting with OnBefore or OnAfter concatenated with the Event name.</li>
</ul>
<p><em>* The OnLeave and OnBefore transition methods may return false to cancel the transition.</em>*</p>
<p><strong>The OnLeave and OnBefore transition methods may return false, which will cancel the transition.</strong></p>
<h2>1.2) Event Triggers</h2>
@ -128,6 +128,95 @@ These rules define when an FSM can transition from a specific state towards an o
<p>The initial state can be defined using the method <a href="##(FSM).SetStartState">FSM.SetStartState</a>(). The default start state of an FSM is "None".</p>
<h3>Example</h3>
<p>This example creates a new FsmDemo object from class FSM.
It will set the start state of FsmDemo to Green.
2 Transition Rules are created, where upon the event Switch,
the FsmDemo will transition from state Green to Red and vise versa.</p>
<pre><code> local FsmDemo = FSM:New() -- #FsmDemo
FsmDemo:SetStartState( "Green" )
FsmDemo:AddTransition( "Green", "Switch", "Red" )
FsmDemo:AddTransition( "Red", "Switch", "Green" )
</code></pre>
<p>In the above example, the FsmDemo could flare every 5 seconds a Green or a Red flare into the air.
The next code implements this through the event handling method <strong>OnAfterSwitch</strong>.</p>
<pre><code> function FsmDemo:OnAfterSwitch( From, Event, To, FsmUnit )
self:E( { From, Event, To, FsmUnit } )
if From == "Green" then
FsmUnit:Flare(FLARECOLOR.Green)
else
if From == "Red" then
FsmUnit:Flare(FLARECOLOR.Red)
end
end
FsmDemo:__Switch( 5, FsmUnit ) -- Trigger the next Switch event to happen in 5 seconds.
end
FsmDemo:__Switch( 5, FsmUnit ) -- Trigger the first Switch event to happen in 5 seconds.
</code></pre>
<p>The OnAfterSwitch implements a loop. The last line of the code fragment triggers the Switch Event within 5 seconds.
Upon the event execution (after 5 seconds), the OnAfterSwitch method is called of FsmDemo (cfr. the double point notation!!! ":").
The OnAfterSwitch method receives from the FSM the 3 transition parameter details ( From, Event, To ),
and one additional parameter that was given when the event was triggered, which is in this case the Unit that is used within OnSwitchAfter.</p>
<pre><code> function FsmDemo:OnAfterSwitch( From, Event, To, FsmUnit )
</code></pre>
<p>For debugging reasons the received parameters are traced within the DCS.log.</p>
<pre><code> self:E( { From, Event, To, FsmUnit } )
</code></pre>
<p>The method will check if the From state received is either "Green" or "Red" and will flare the respective color from the FsmUnit.</p>
<pre><code> if From == "Green" then
FsmUnit:Flare(FLARECOLOR.Green)
else
if From == "Red" then
FsmUnit:Flare(FLARECOLOR.Red)
end
end
</code></pre>
<p>It is important that the Switch event is again triggered, otherwise, the FsmDemo would stop working after having the first Event being handled.</p>
<pre><code> FsmDemo:__Switch( 5, FsmUnit ) -- Trigger the next Switch event to happen in 5 seconds.
</code></pre>
<p>This example is fully implemented in the following MOOSE test mission:</p>
<p>Note that transition rules can be declared with a few variations:</p>
<ul>
<li>The From states can be a table of strings, indicating that the transition rule will be valid if the current state of the FSM will be one of the given From states.</li>
<li>The From state can be a "*", indicating that the transition rule will always be valid, regardless of the current state of the FSM.</li>
</ul>
<p>This transition will create a new FsmDemo object from class FSM.
It will set the start state of FsmDemo to Green.
A new event is added in addition to the above example.
The new event Stop will cancel the Switching process.
So, the transtion for event Stop can be executed if the current state of the FSM is either "Red" or "Green".</p>
<pre><code> local FsmDemo = FSM:New() -- #FsmDemo
FsmDemo:SetStartState( "Green" )
FsmDemo:AddTransition( "Green", "Switch", "Red" )
FsmDemo:AddTransition( "Red", "Switch", "Green" )
FsmDemo:AddTransition( { "Red", "Green" }, "Stop", "Stopped" )
</code></pre>
<p>The transition for event Stop can also be simplified, as any current state of the FSM is valid.</p>
<pre><code> FsmDemo:AddTransition( "*", "Stop", "Stopped" )
</code></pre>
<h2>1.4) FSM Process Rules</h2>
<p>The FSM can implement sub-processes that will execute and return multiple possible states. <br/>

View File

@ -1692,9 +1692,6 @@ The group that was spawned. You can use this group for further actions.</p>
<p> Don't repeat the group from Take-Off till Landing and back Take-Off by ReSpawning.</p>
</dd>
</dl>
<dl class="function">
@ -2169,7 +2166,7 @@ when nothing was spawned.</p>
<dl class="function">
<dt>
<em>#number</em>
<em></em>
<a id="#(SPAWN).SpawnMaxGroups" >
<strong>SPAWN.SpawnMaxGroups</strong>
</a>
@ -2186,7 +2183,7 @@ when nothing was spawned.</p>
<dl class="function">
<dt>
<em>#number</em>
<em></em>
<a id="#(SPAWN).SpawnMaxUnitsAlive" >
<strong>SPAWN.SpawnMaxUnitsAlive</strong>
</a>
@ -2489,7 +2486,7 @@ Spawn_BE_KA50 = SPAWN:New( 'BE KA-50@RAMP-Ground Defense' ):Schedule( 600, 0.5 )
<p> Flag that indicates if all the Groups of the SpawnGroup need to be visible when Spawned.</p>
<p> When the first Spawn executes, all the Groups need to be made visible before start.</p>
</dd>
</dl>

View File

@ -185,6 +185,8 @@ sub {
img {
border: 0;
max-width:100%;
height:auto;
}
/**
@ -500,6 +502,7 @@ h3 {
#main-content img {
max-width: 100%;
height: auto;
}
aside#sidebar {

View File

@ -185,6 +185,8 @@ sub {
img {
border: 0;
max-width:100%;
height:auto;
}
/**
@ -500,6 +502,7 @@ h3 {
#main-content img {
max-width: 100%;
height: auto;
}
aside#sidebar {