From 813d4edc970915de04591762e02b249d59f88fec Mon Sep 17 00:00:00 2001 From: Frank Date: Wed, 7 Dec 2022 18:35:12 +0100 Subject: [PATCH] CONDITION v0.3.0 - Added methods to remove condition functions - Added option to define output if no condition functions at all were given - --- Moose Development/Moose/Core/Condition.lua | 145 +++++++++++++++++---- 1 file changed, 122 insertions(+), 23 deletions(-) diff --git a/Moose Development/Moose/Core/Condition.lua b/Moose Development/Moose/Core/Condition.lua index 80f80cf52..7e9842a7b 100644 --- a/Moose Development/Moose/Core/Condition.lua +++ b/Moose Development/Moose/Core/Condition.lua @@ -25,10 +25,13 @@ -- @field #string lid Class id string for output to DCS log file. -- @field #string name Name of the condition. -- @field #boolean isAny General functions are evaluated as any condition. --- @field #boolean negateResult Negeate result of evaluation. +-- @field #boolean negateResult Negate result of evaluation. +-- @field #boolean noneResult Boolean that is returned if no condition functions at all were specified. -- @field #table functionsGen General condition functions. -- @field #table functionsAny Any condition functions. -- @field #table functionsAll All condition functions. +-- @field #number functionCounter Running number to determine the unique ID of condition functions. +-- @field #boolean defaultPersist Default persistence of condition functions. -- -- @extends Core.Base#BASE @@ -42,27 +45,34 @@ -- -- @field #CONDITION CONDITION = { - ClassName = "CONDITION", - lid = nil, - functionsGen = {}, - functionsAny = {}, - functionsAll = {}, + ClassName = "CONDITION", + lid = nil, + functionsGen = {}, + functionsAny = {}, + functionsAll = {}, + functionCounter = 0, + defaultPersist = false, } --- Condition function. -- @type CONDITION.Function --- @field #function func Callback function to check for a condition. Should return a `#boolean`. +-- @field #number uid Unique ID of the condition function. +-- @field #string type Type of the condition function: "gen", "any", "all". +-- @field #boolean persistence If `true`, this is persistent. +-- @field #function func Callback function to check for a condition. Must return a `#boolean`. -- @field #table arg (Optional) Arguments passed to the condition callback function if any. --- CONDITION class version. -- @field #string version -CONDITION.version="0.2.0" +CONDITION.version="0.3.0" ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -- TODO list ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --- TODO: Make FSM. +-- TODO: Make FSM. No sure if really necessary. +-- DONE: Option to remove condition functions. +-- DONE: Persistence option for condition functions. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -- Constructor @@ -79,6 +89,8 @@ function CONDITION:New(Name) self.name=Name or "Condition X" + self:SetNoneResult(false) + self.lid=string.format("%s | ", self.name) return self @@ -102,6 +114,28 @@ function CONDITION:SetNegateResult(Negate) return self end +--- Set whether `true` or `false` is returned, if no conditions at all were specified. By default `false` is returned. +-- @param #CONDITION self +-- @param #boolean ReturnValue Returns this boolean. +-- @return #CONDITION self +function CONDITION:SetNoneResult(ReturnValue) + if not ReturnValue then + self.noneResult=false + else + self.noneResult=true + end + return self +end + +--- Set whether condition functions are persistent, *i.e.* are removed. +-- @param #CONDITION self +-- @param #boolean IsPersistent If `true`, condition functions are persistent. +-- @return #CONDITION self +function CONDITION:SetDefaultPersistence(IsPersistent) + self.defaultPersist=IsPersistent + return self +end + --- Add a function that is evaluated. It must return a `#boolean` value, *i.e.* either `true` or `false` (or `nil`). -- @param #CONDITION self -- @param #function Function The function to call. @@ -114,47 +148,109 @@ end -- -- myCondition:AddFunction(isAequalB, a, b) -- --- @return #CONDITION self +-- @return #CONDITION.Function Condition function table. function CONDITION:AddFunction(Function, ...) -- Condition function. - local condition=self:_CreateCondition(Function, ...) + local condition=self:_CreateCondition(0, Function, ...) -- Add to table. table.insert(self.functionsGen, condition) - return self + return condition end --- Add a function that is evaluated. It must return a `#boolean` value, *i.e.* either `true` or `false` (or `nil`). -- @param #CONDITION self -- @param #function Function The function to call. -- @param ... (Optional) Parameters passed to the function (if any). --- @return #CONDITION self +-- @return #CONDITION.Function Condition function table. function CONDITION:AddFunctionAny(Function, ...) -- Condition function. - local condition=self:_CreateCondition(Function, ...) + local condition=self:_CreateCondition(1, Function, ...) -- Add to table. table.insert(self.functionsAny, condition) - return self + return condition end --- Add a function that is evaluated. It must return a `#boolean` value, *i.e.* either `true` or `false` (or `nil`). -- @param #CONDITION self -- @param #function Function The function to call. -- @param ... (Optional) Parameters passed to the function (if any). --- @return #CONDITION self +-- @return #CONDITION.Function Condition function table. function CONDITION:AddFunctionAll(Function, ...) -- Condition function. - local condition=self:_CreateCondition(Function, ...) + local condition=self:_CreateCondition(2, Function, ...) -- Add to table. table.insert(self.functionsAll, condition) + return condition +end + +--- Remove a condition function. +-- @param #CONDITION self +-- @param #CONDITION.Function ConditionFunction The condition function to be removed. +-- @return #CONDITION self +function CONDITION:RemoveFunction(ConditionFunction) + + if ConditionFunction then + + local data=nil + if ConditionFunction.type==0 then + data=self.functionsGen + elseif ConditionFunction.type==1 then + data=self.functionsAny + elseif ConditionFunction.type==2 then + data=self.functionsAll + end + + if data then + for i=#data,1,-1 do + local cf=data[i] --#CONDITION.Function + if cf.uid==ConditionFunction.uid then + self:T(self.lid..string.format("Removed ConditionFunction UID=%d", cf.uid)) + table.remove(data, i) + return self + end + end + end + + end + + return self +end + +--- Remove all non-persistant condition functions. +-- @param #CONDITION self +-- @return #CONDITION self +function CONDITION:RemoveNonPersistant() + + for i=#self.functionsGen,1,-1 do + local cf=self.functionsGen[i] --#CONDITION.Function + if not cf.persistence then + table.remove(self.functionsGen, i) + end + end + + for i=#self.functionsAll,1,-1 do + local cf=self.functionsAll[i] --#CONDITION.Function + if not cf.persistence then + table.remove(self.functionsAll, i) + end + end + + for i=#self.functionsAny,1,-1 do + local cf=self.functionsAny[i] --#CONDITION.Function + if not cf.persistence then + table.remove(self.functionsAny, i) + end + end + return self end @@ -167,11 +263,7 @@ function CONDITION:Evaluate(AnyTrue) -- Check if at least one function was given. if #self.functionsAll + #self.functionsAny + #self.functionsAll == 0 then - if self.negateResult then - return true - else - return false - end + return self.noneResult end -- Any condition for gen. @@ -280,13 +372,20 @@ end --- Create conditon function object. -- @param #CONDITION self +-- @param #number Ftype Function type: 0=Gen, 1=All, 2=Any. -- @param #function Function The function to call. -- @param ... (Optional) Parameters passed to the function (if any). -- @return #CONDITION.Function Condition function. -function CONDITION:_CreateCondition(Function, ...) +function CONDITION:_CreateCondition(Ftype, Function, ...) + + -- Increase counter. + self.functionCounter=self.functionCounter+1 local condition={} --#CONDITION.Function + condition.uid=self.functionCounter + condition.type=Ftype or 0 + condition.persistence=self.defaultPersist condition.func=Function condition.arg={} if arg then