Statemachine

This commit is contained in:
FlightControl 2016-06-30 10:52:37 +02:00
parent 7ab52025fd
commit e2250dc92f
38 changed files with 468 additions and 46879 deletions

View File

@ -2,33 +2,33 @@
--
-- ===
--
-- 1) @{Fac#FAC_BASE} class, extends @{Base#BASE}
-- 1) @{Fac#DETECTION_MANAGER} class, extends @{Base#BASE}
-- ==============================================
-- The @{Fac#FAC_BASE} class defines the core functions to report detected objects to clients.
-- Reportings can be done in several manners, and it is up to the derived classes if FAC_BASE to model the reporting behaviour.
-- The @{Fac#DETECTION_MANAGER} class defines the core functions to report detected objects to clients.
-- Reportings can be done in several manners, and it is up to the derived classes if DETECTION_MANAGER to model the reporting behaviour.
--
-- 1.1) FAC_BASE constructor:
-- 1.1) DETECTION_MANAGER constructor:
-- ----------------------------
-- * @{Fac#FAC_BASE.New}(): Create a new FAC_BASE instance.
-- * @{Fac#DETECTION_MANAGER.New}(): Create a new DETECTION_MANAGER instance.
--
-- 1.2) FAC_BASE reporting:
-- 1.2) DETECTION_MANAGER reporting:
-- ------------------------
-- Derived FAC_BASE classes will reports detected units using the method @{Fac#FAC_BASE.ReportDetected}(). This method implements polymorphic behaviour.
-- Derived DETECTION_MANAGER classes will reports detected units using the method @{Fac#DETECTION_MANAGER.ReportDetected}(). This method implements polymorphic behaviour.
--
-- The time interval in seconds of the reporting can be changed using the methods @{Fac#FAC_BASE.SetReportInterval}().
-- To control how long a reporting message is displayed, use @{Fac#FAC_BASE.SetReportDisplayTime}().
-- Derived classes need to implement the method @{Fac#FAC_BASE.GetReportDisplayTime}() to use the correct display time for displayed messages during a report.
-- The time interval in seconds of the reporting can be changed using the methods @{Fac#DETECTION_MANAGER.SetReportInterval}().
-- To control how long a reporting message is displayed, use @{Fac#DETECTION_MANAGER.SetReportDisplayTime}().
-- Derived classes need to implement the method @{Fac#DETECTION_MANAGER.GetReportDisplayTime}() to use the correct display time for displayed messages during a report.
--
-- Reporting can be started and stopped using the methods @{Fac#FAC_BASE.StartReporting}() and @{Fac#FAC_BASE.StopReporting}() respectively.
-- If an ad-hoc report is requested, use the method @{Fac#FAC_BASE#ReportNow}().
-- Reporting can be started and stopped using the methods @{Fac#DETECTION_MANAGER.StartReporting}() and @{Fac#DETECTION_MANAGER.StopReporting}() respectively.
-- If an ad-hoc report is requested, use the method @{Fac#DETECTION_MANAGER#ReportNow}().
--
-- The default reporting interval is every 60 seconds. The reporting messages are displayed 15 seconds.
--
-- ===
--
-- 2) @{Fac#FAC_REPORTING} class, extends @{Fac#FAC_BASE}
-- 2) @{Fac#FAC_REPORTING} class, extends @{Fac#DETECTION_MANAGER}
-- ======================================================
-- The @{Fac#FAC_REPORTING} class implements detected units reporting. Reporting can be controlled using the reporting methods available in the @{Fac#FAC_BASE} class.
-- The @{Fac#FAC_REPORTING} class implements detected units reporting. Reporting can be controlled using the reporting methods available in the @{Fac#DETECTION_MANAGER} class.
--
-- 2.1) FAC_REPORTING constructor:
-- -------------------------------
@ -42,26 +42,26 @@
--- FAC_BASE class.
-- @type FAC_BASE
--- DETECTION_MANAGER class.
-- @type DETECTION_MANAGER
-- @field Set#SET_CLIENT ClientSet The clients to which the FAC will report to.
-- @field Detection#DETECTION_BASE Detection The DETECTION_BASE object that is used to report the detected objects.
-- @extends Base#BASE
FAC_BASE = {
ClassName = "FAC_BASE",
DETECTION_MANAGER = {
ClassName = "DETECTION_MANAGER",
ClientSet = nil,
Detection = nil,
}
--- FAC constructor.
-- @param #FAC_BASE self
-- @param #DETECTION_MANAGER self
-- @param Set#SET_CLIENT ClientSet
-- @param Detection#DETECTION_BASE Detection
-- @return #FAC_BASE self
function FAC_BASE:New( ClientSet, Detection )
-- @return #DETECTION_MANAGER self
function DETECTION_MANAGER:New( ClientSet, Detection )
-- Inherits from BASE
local self = BASE:Inherit( self, BASE:New() ) -- Fac#FAC_BASE
local self = BASE:Inherit( self, BASE:New() ) -- Fac#DETECTION_MANAGER
self.ClientSet = ClientSet
self.Detection = Detection
@ -73,10 +73,10 @@ function FAC_BASE:New( ClientSet, Detection )
end
--- Set the reporting time interval.
-- @param #FAC_BASE self
-- @param #DETECTION_MANAGER self
-- @param #number ReportInterval The interval in seconds when a report needs to be done.
-- @return #FAC_BASE self
function FAC_BASE:SetReportInterval( ReportInterval )
-- @return #DETECTION_MANAGER self
function DETECTION_MANAGER:SetReportInterval( ReportInterval )
self:F2()
self._ReportInterval = ReportInterval
@ -84,29 +84,29 @@ end
--- Set the reporting message display time.
-- @param #FAC_BASE self
-- @param #DETECTION_MANAGER self
-- @param #number ReportDisplayTime The display time in seconds when a report needs to be done.
-- @return #FAC_BASE self
function FAC_BASE:SetReportDisplayTime( ReportDisplayTime )
-- @return #DETECTION_MANAGER self
function DETECTION_MANAGER:SetReportDisplayTime( ReportDisplayTime )
self:F2()
self._ReportDisplayTime = ReportDisplayTime
end
--- Get the reporting message display time.
-- @param #FAC_BASE self
-- @param #DETECTION_MANAGER self
-- @return #number ReportDisplayTime The display time in seconds when a report needs to be done.
function FAC_BASE:GetReportDisplayTime()
function DETECTION_MANAGER:GetReportDisplayTime()
self:F2()
return self._ReportDisplayTime
end
--- Reports the detected items to the @{Set#SET_CLIENT}.
-- @param #FAC_BASE self
-- @param #DETECTION_MANAGER self
-- @param Set#SET_BASE DetectedSets The detected Sets created by the @{Detection#DETECTION_BASE} object.
-- @return #FAC_BASE self
function FAC_BASE:ReportDetected( DetectedSets )
-- @return #DETECTION_MANAGER self
function DETECTION_MANAGER:ReportDetected( DetectedSets )
self:F2()
@ -114,11 +114,11 @@ function FAC_BASE:ReportDetected( DetectedSets )
end
--- Schedule the FAC reporting.
-- @param #FAC_BASE self
-- @param #DETECTION_MANAGER self
-- @param #number DelayTime The delay in seconds to wait the reporting.
-- @param #number ReportInterval The repeat interval in seconds for the reporting to happen repeatedly.
-- @return #FAC_BASE self
function FAC_BASE:Schedule( DelayTime, ReportInterval )
-- @return #DETECTION_MANAGER self
function DETECTION_MANAGER:Schedule( DelayTime, ReportInterval )
self:F2()
self._ScheduleDelayTime = DelayTime
@ -130,8 +130,8 @@ function FAC_BASE:Schedule( DelayTime, ReportInterval )
end
--- Report the detected @{Unit#UNIT}s detected within the @{DetectION#DETECTION_BASE} object to the @{Set#SET_CLIENT}s.
-- @param #FAC_BASE self
function FAC_BASE:_FacScheduler( SchedulerName )
-- @param #DETECTION_MANAGER self
function DETECTION_MANAGER:_FacScheduler( SchedulerName )
self:F2( { SchedulerName } )
self.ClientSet:ForEachClient(
@ -139,7 +139,7 @@ function FAC_BASE:_FacScheduler( SchedulerName )
function( Client )
if Client:IsAlive() then
local DetectedSets = self.Detection:GetDetectedSets()
return self:ReportDetected( Client, DetectedSets )
return self:ProcessDetected( Client, DetectedSets )
end
end
)
@ -153,7 +153,7 @@ end
-- @type FAC_REPORTING
-- @field Set#SET_CLIENT ClientSet The clients to which the FAC will report to.
-- @field Detection#DETECTION_BASE Detection The DETECTION_BASE object that is used to report the detected objects.
-- @extends #FAC_BASE
-- @extends #DETECTION_MANAGER
FAC_REPORTING = {
ClassName = "FAC_REPORTING",
}
@ -166,8 +166,8 @@ FAC_REPORTING = {
-- @return #FAC_REPORTING self
function FAC_REPORTING:New( ClientSet, Detection )
-- Inherits from FAC_BASE
local self = BASE:Inherit( self, FAC_BASE:New( ClientSet, Detection ) ) -- #FAC_REPORTING
-- Inherits from DETECTION_MANAGER
local self = BASE:Inherit( self, DETECTION_MANAGER:New( ClientSet, Detection ) ) -- #FAC_REPORTING
self:Schedule( 5, 60 )
return self
@ -179,7 +179,7 @@ end
-- @param Client#CLIENT Client The @{Client} object to where the report needs to go.
-- @param Set#SET_BASE DetectedSets The detected Sets created by the @{Detection#DETECTION_BASE} object.
-- @return #boolean Return true if you want the reporting to continue... false will cancel the reporting loop.
function FAC_REPORTING:ReportDetected( Client, DetectedSets )
function FAC_REPORTING:ProcessDetected( Client, DetectedSets )
self:F2( Client )
local DetectedMsg = {}
@ -208,3 +208,85 @@ function FAC_REPORTING:ReportDetected( Client, DetectedSets )
return true
end
--- TASK_DISPATCHER
--- TASK_DISPATCHER class.
-- @type TASK_DISPATCHER
-- @field Set#SET_CLIENT ClientSet The clients to which the FAC will report to.
-- @field Detection#DETECTION_BASE Detection The DETECTION_BASE object that is used to report the detected objects.
-- @extends #DETECTION_MANAGER
TASK_DISPATCHER = {
ClassName = "TASK_DISPATCHER",
}
--- TASK_DISPATCHER constructor.
-- @param #TASK_DISPATCHER self
-- @param Set#SET_CLIENT ClientSet
-- @param Detection#DETECTION_BASE Detection
-- @return #TASK_DISPATCHER self
function TASK_DISPATCHER:New( ClientSet, Detection, TaskType, Priority )
-- Inherits from DETECTION_MANAGER
local self = BASE:Inherit( self, DETECTION_MANAGER:New( ClientSet, Detection ) ) -- #TASK_DISPATCHER
self:Schedule( 5, 60 )
return self
end
--- Assigns tasks in relation to the detected items to the @{Set#SET_CLIENT}.
-- @param #FAC_REPORTING self
-- @param Client#CLIENT Client The @{Client} object to where the report needs to go.
-- @param Set#SET_BASE DetectedSets The detected Sets created by the @{Detection#DETECTION_BASE} object.
-- @param Mission#MISSIONSCHEDULER MissionScheduler
-- @param #string TaskID The task to be executed.
-- @return #boolean Return true if you want the task assigning to continue... false will cancel the loop.
function TASK_DISPATCHER:ProcessDetected( Client, DetectedSets, MissionScheduler, Targets )
self:F2( Client )
local DetectedMsg = {}
local FACGroup = self.Detection:GetFACGroup()
local FACGroupName = FACGroup:GetName()
for DetectedUnitSetID, DetectedUnitSet in pairs( DetectedSets ) do
local UnitSet = DetectedUnitSet -- Set#SET_UNIT
local MT = {} -- Message Text
local UnitTypes = {}
if not MissionScheduler.FindMission( FACGroupName ) then
local Mission = MISSION:New()
MissionScheduler.AddMission(Mission)
end
for DetectedUnitID, DetectedUnitData in pairs( UnitSet:GetSet() ) do
local DetectedUnit = DetectedUnitData -- Unit#UNIT
local UnitType = DetectedUnit:GetTypeName()
local DetectedUnitName = DetectedUnit:GetName()
if Task:GetTarget( DetectedUnitName ) then
if not UnitTypes[UnitType] then
UnitTypes[UnitType] = 1
else
UnitTypes[UnitType] = UnitTypes[UnitType] + 1
end
Task:AddTarget( DetectedUnit )
end
end
for UnitTypeID, UnitType in pairs( UnitTypes ) do
MT[#MT+1] = Task:GetCommand() .. " " .. UnitType .. " of " .. UnitTypeID
end
local MessageText = table.concat( MT, ", " )
DetectedMsg[#DetectedMsg+1] = " - Group #" .. DetectedUnitSetID .. ": " .. MessageText
end
Task:Assign( Client )
local FACGroup = self.Detection:GetFACGroup()
FACGroup:MessageToClient( "Reporting detected target groups:\n" .. table.concat( DetectedMsg, "\n" ), self:GetReportDisplayTime(), Client )
return true
end

View File

@ -45,6 +45,9 @@ Include.File( "AIBalancer" )
Include.File( "AirbasePolice" )
Include.File( "Detection" )
Include.File( "FAC" )
Include.File( "Task2" )
Include.File( "TaskSead" )
Include.File( "StateMachine" )
-- The order of the declarations is important here. Don't touch it.

View File

@ -0,0 +1,128 @@
local machine = {}
machine.__index = machine
--- This module contains the STATEMACHINE class.
--
-- ===
--
-- 1) @{Workflow#STATEMACHINE} class, extends @{Base#BASE}
-- ==============================================
--
-- 1.1) Add or remove objects from the STATEMACHINE
-- --------------------------------------------
-- @module StateMachine
-- @author FlightControl
--- STATEMACHINE class
-- @type STATEMACHINE
STATEMACHINE = {
ClassName = "STATEMACHINE",
}
--- Creates a new STATEMACHINE object.
-- @param #STATEMACHINE self
-- @return #STATEMACHINE
function STATEMACHINE:New(options)
local self = routines.utils.deepCopy( self ) -- Create a new self instance
assert(options.events)
local FiniteStateMachine = {}
setmetatable( FiniteStateMachine, self )
self.__index = self
FiniteStateMachine.options = options
FiniteStateMachine.current = options.initial or 'none'
FiniteStateMachine.events = {}
for _, event in ipairs(options.events or {}) do
local name = event.name
FiniteStateMachine[name] = FiniteStateMachine[name] or self:create_transition(name)
FiniteStateMachine.events[name] = FiniteStateMachine.events[name] or { map = {} }
self:add_to_map(FiniteStateMachine.events[name].map, event)
end
for name, callback in pairs(options.callbacks or {}) do
FiniteStateMachine[name] = callback
end
return FiniteStateMachine
end
function STATEMACHINE:call_handler(handler, params)
if handler then
return handler(unpack(params))
end
end
function STATEMACHINE:create_transition(name)
return function(self, ...)
local can, to = self:can(name)
if can then
local from = self.current
local params = { self, name, from, to, ... }
if self:call_handler(self["onbefore" .. name], params) == false
or self:call_handler(self["onleave" .. from], params) == false then
return false
end
self.current = to
self:call_handler(self["onenter" .. to] or self["on" .. to], params)
self:call_handler(self["onafter" .. name] or self["on" .. name], params)
self:call_handler(self["onstatechange"], params)
return true
end
return false
end
end
function STATEMACHINE:add_to_map(map, event)
if type(event.from) == 'string' then
map[event.from] = event.to
else
for _, from in ipairs(event.from) do
map[from] = event.to
end
end
end
function STATEMACHINE:is(state)
return self.current == state
end
function STATEMACHINE:can(e)
local event = self.events[e]
local to = event and event.map[self.current] or event.map['*']
return to ~= nil, to
end
function STATEMACHINE:cannot(e)
return not self:can(e)
end
function STATEMACHINE:todot(filename)
local dotfile = io.open(filename,'w')
dotfile:write('digraph {\n')
local transition = function(event,from,to)
dotfile:write(string.format('%s -> %s [label=%s];\n',from,to,event))
end
for _, event in pairs(self.options.events) do
if type(event.from) == 'table' then
for _, from in ipairs(event.from) do
transition(event.name,from,event.to)
end
else
transition(event.name,event.from,event.to)
end
end
dotfile:write('}\n')
dotfile:close()
end

View File

@ -1,12 +1,6 @@
--- The TASK Classes define major end-to-end activities within a MISSION. The TASK Class is the Master Class to orchestrate these activities. From this class, many concrete TASK classes are inherited.
-- @module TASK
--- The TASK class
-- @type TASK
-- @extends Base#BASE

View File

@ -0,0 +1,37 @@
--- @module TASK2
--- The TASK2 class
-- @type TASK2
-- @field Scheduler#SCHEDULER TaskScheduler
-- @extends Base#BASE
TASK2 = {
ClassName = "TASK",
TaskScheduler = nil,
NextEvent = nil,
}
--- Instantiates a new TASK Base. Should never be used. Interface Class.
-- @return #TASK2 self
function TASK2:New( Client )
local self = BASE:Inherit( self, BASE:New() )
self:F()
self.Client = Client
return self
end
--- @param #TASK2 self
function TASK2:Schedule()
self.TaskScheduler = SCHEDULER:New( self.StateMachine, self.StateMachine.Assign, { self, self.Client }, 1)
end
--- @param #TASK2 self
function TASK2:NextEvent( NextEvent, ... )
self:E( NextEvent )
self.TaskScheduler = SCHEDULER:New( self.StateMachine, NextEvent, { self, self.Client }, 1 )
end

View File

@ -0,0 +1,106 @@
--- @module Task2
--- TASK2_SEAD class
-- @type TASK2_SEAD
-- @extends Task2#TASK2
TASK2_SEAD = {
ClassName = "TASK2_SEAD",
StateMachine = {},
}
function TASK2_SEAD:New( Client )
-- Inherits from BASE
local self = BASE:Inherit( self, TASK2:New( Client ) ) -- #TASK2_SEAD
--- @param #TASK2_SEAD self
-- @param #string Event
-- @param #string From
-- @param #string To
-- @param Client#CLIENT Client
local function OnAssign( self, Event, From, To, Task, Client )
Task:E( { Event, From, To, Client.ClientName} )
Client:Message( "Assigned", 15 )
Task:NextEvent( self.Await )
end
--- @param #string Event
-- @param #string From
-- @param #string To
local function OnAwait( self, Event, From, To, Task, Client )
Task:E( { Event, From, To, Client.ClientName} )
Client:Message( "Waiting", 15 )
Task:NextEvent( self.Await )
end
--- @param #string Event
-- @param #string From
-- @param #string To
local function OnHitTarget( self, Event, From, To )
end
--- @param #string Event
-- @param #string From
-- @param #string To
local function OnMoreTargets( self, Event, From, To )
end
local function OnKilled( self, Event, From, To )
end
local function OnFailed( self, Event, From, To )
end
local function OnDestroyed( self, Event, From, To )
end
self.StateMachine = STATEMACHINE:New( {
initial = 'Unassigned',
events = {
{ name = 'Assign', from = 'Unassigned', to = 'Assigned' },
{ name = 'Await', from = 'Assigned', to = 'Waiting' },
{ name = 'Await', from = 'Waiting', to = 'Waiting' },
{ name = 'HitTarget', from = 'Waiting', to = 'Destroyed' },
{ name = 'MoreTargets', from = 'Destroyed', to = 'Waiting' },
{ name = 'Killed', from = 'Waiting', to = 'Killed' },
{ name = 'Failed', from = 'Killed', to = 'Unassigned' },
{ name = 'Destroyed', from = 'Destroyed', to = 'Finished' }
},
callbacks = {
onAssign = OnAssign,
onAwait = OnAwait,
onHitTarget = OnHitTarget,
onMoreTargets = OnMoreTargets,
onKilled = OnKilled,
onFailed = OnFailed,
onDestroyed = OnDestroyed,
}
} )
_EVENTDISPATCHER:OnHit( self.OnHit, self )
self:Schedule()
end
--- @param #TASK2_SEAD self
-- @param Event#EVENTDATA Event
function TASK2_SEAD:OnHit( Event )
if Event.IniUnit then
self:NextEvent( self.StateMachine.OnHitTarget )
end
end

View File

@ -0,0 +1,17 @@
--- @module Task2Workflow
--- The TASK2_WORKFLOW class
-- @type TASK2_WORKFLOW
-- @extends Base#BASE
TASK2_WORKFLOW = {
ClassName = "TASK2_WORKFLOW",
}
function TASK2_WORKFLOW:New( Client, Task )
-- Inherits from BASE
local self = BASE:Inherit( self, BASE:New() )
Task:Assign( Client )
end

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -3,6 +3,7 @@
local FACGroup = GROUP:FindByName( "FAC Group" )
local FACDetection = DETECTION_UNITGROUPS:New( FACGroup, 1000, 250 )
local FACClientSet = SET_CLIENT:New():FilterCoalitions( "blue" ):FilterStart()
local SeadClientSet = SET_CLIENT:New():FilterCoalitions( "blue" ):FilterStart()
local DestroyClientSet = SET_CLIENT:New():FilterCoalitions( "blue" ):FilterStart()
local FACReporting = FAC_REPORTING:New( FACClientSet, FACDetection )
local FACReporting = FAC_REPORTING:New( FACClientSet, FACDetection )

View File

@ -0,0 +1,13 @@
local FACGroup = GROUP:FindByName( "FAC Group" )
local FACDetection = DETECTION_UNITGROUPS:New( FACGroup, 1000, 250 )
local SeadClientSet = SET_CLIENT:New():FilterCoalitions( "blue" ):FilterStart()
local DestroyClientSet = SET_CLIENT:New():FilterCoalitions( "blue" ):FilterStart()
local Targets_SEAD_Set = SET_UNIT:New():FilterCoalitions( "red" ):FilterPrefixes( "RU Patriot" ):FilterStart()
local Targets_SEAD = TARGETS:New( "SEAD", Targets_SEAD_Set )
local TaskAssign = TASK_DISPATCHER:New( CommmandCenter, SeadClientSet, FACDetection, Targets_SEAD )

View File

@ -0,0 +1,4 @@
local Client = CLIENT:FindByName( "Test SEAD" )
local Task_SEAD = TASK2_SEAD:New( Client )