WSF
WsfBehaviorTreeNode.hpp
Go to the documentation of this file.
1// ****************************************************************************
2// CUI
3//
4// The Advanced Framework for Simulation, Integration, and Modeling (AFSIM)
5//
6// Copyright 2003-2015 The Boeing Company. All rights reserved.
7//
8// The use, dissemination or disclosure of data in this file is subject to
9// limitation or restriction. See accompanying README and LICENSE for details.
10// ****************************************************************************
11
12#ifndef WSFBEHAVIORTREENODE_HPP
13#define WSFBEHAVIORTREENODE_HPP
14
15#include <string>
16#include <vector>
17
18class UtInput;
19class WsfBehaviorTree;
20#include "wsf_export.h"
21class WsfMessage;
22#include "WsfObject.hpp"
23#include "WsfObjectTypeList.hpp"
24class WsfPlatform;
25class WsfSimulation;
26class WsfScript;
32
38{
45
46public:
53
59
60 WsfBehaviorTreeNode(const WsfScenario& aScenario);
63 ~WsfBehaviorTreeNode() override;
64 WsfObject* Clone() const override = 0;
65
66 bool ProcessInput(UtInput& aInput) override;
67 virtual void RegisterInput(UtInput& aInput) {}
68
69 static unsigned int GetUniqueNodeId(void) { return ++mNextUniqueId; }
70 static void Reset() { mNextUniqueId = 0; }
71
72 bool PreconditionsMet();
73 double PreconditionValue();
74
75 void AddChild(WsfBehaviorTreeNode* aNodePtr);
76 std::vector<WsfBehaviorTreeNode*>& Children() { return mChildren; }
77 int ChildCount() const { return (int)mChildren.size(); }
78 WsfBehaviorTreeNode* ChildEntry(int aIndex);
79 void SetParent(WsfBehaviorTreeNode* aNodePtr) { mParentPtr = aNodePtr; }
81
82 // to be done during standard initialization
83 virtual bool Initialize(double aSimTime, WsfScriptContext* aParentContext);
84
86
87 bool Execute(double aSimTime,
88 bool aSkipCheck = false); // does record keeping, calls LocalExecute()
89 void TurnOn() { mTurnedOn = true; }
90 void TurnOff() { mTurnedOn = false; }
91
92 void SetFailureReason(const std::string& aReason, bool aSendEvent = false);
93 std::string FailureReason() const { return mFailureReason; }
94
95 bool ExecutedLastRun() { return mLastExecute; }
96
97 void SetExecutedLastRun(bool aVal);
98 void SetExecutedLastRunRecursive(bool aVal);
99 void SetExecutedLastRun(bool aVal, const std::string& aReason);
100 void SetExecutedLastRunRecursive(bool aVal, const std::string& aReason);
101
102 WsfBehaviorTree* OwningTree();
103 virtual void SetTree(WsfBehaviorTree* aTreePtr);
104 WsfScriptProcessor* OwningProcessor();
105 WsfPlatform* OwningPlatform();
106 WsfSimulation* GetSimulation();
107
108 bool TryRunLast(double aSimTime);
109 bool ShouldRunNext(double aSimTime);
110 virtual bool LocalExecute(double aSimTime) = 0; // node type specific execution
111
112 unsigned int Id() const { return mId; }
113
114 void LogNodeStructure(double aSimTime, bool aRecurse);
115
116 std::string FilePath() const { return mFullPathFileName; }
117 time_t FileLastModTime() const { return mFileLastModTime; }
118 void SetFilePath(const std::string& aPath);
119 void SetFileInfo(const std::string& aPath, time_t aModTime);
120
121 WsfBehaviorTreeNode* FindNode(const std::string& aNodeName);
122 void FindLastExecuted(std::vector<WsfBehaviorTreeNode*>& aLastExecuted);
123
124 const char* GetScriptClassName() const override;
125 UtScriptContext* GetScriptAccessibleContext() const override;
126 const WsfScenario& GetScenario() const { return mScenario; }
127
128 virtual bool ProcessMessage(double aSimTime, const WsfMessage& aMessage);
129
130protected:
136 std::vector<WsfBehaviorTreeNode*> mChildren;
141 std::string mFailureReason;
144 unsigned int mId;
145 std::string mFullPathFileName;
159 bool mDebug;
160
161private:
162 static unsigned int mNextUniqueId;
163};
164
165class WsfBehaviorTreeNodeTypes : public WsfObjectTypeList<WsfBehaviorTreeNode>
166{
167public:
169
170 LoadResult LoadType(UtInput& aInput) override;
171};
172
174{
175public:
177 bool aRegisterPlatformVariable = true,
178 bool aRegisterProcessorVariable = true);
181 ~WsfBehaviorTreeLeafNode() override = default;
182
183 WsfObject* Clone() const override;
184 bool ProcessInput(UtInput& aInput) override;
185 void RegisterInput(UtInput& aInput) override; // allows behavior to know what file it was defined in
186 bool Initialize(double aSimTime, WsfScriptContext* aParentContextPtr) override;
187 bool LocalExecute(double aSimTime) override;
188 void SetTree(WsfBehaviorTree* aTreePtr) override;
189
190protected:
191 bool RegisterExternVariable(const std::string& aVariableType, const std::string& aVariableName);
192
193private:
194 UtScript* mOnInitScriptPtr;
195 UtScript* mExecuteScriptPtr;
196};
197
199{
200public:
201 WsfScriptBehaviorTreeNodeClass(const std::string& aClassName, UtScriptTypes* aScriptTypesPtr);
202 UtScriptContext* GetContext(void* aObjectPtr) override;
212};
213
214//############################################################################
215//#
216//# Sequence Nodes perform each of their children until one of them
217//# returns failure. Opposite of Selector Nodes.
218//#
219//############################################################################
221{
222public:
224 WsfObject* Clone() const override;
225 bool LocalExecute(double aSimTime) override;
226};
227
228//############################################################################
229//#
230//# Selector Nodes perform each of their children until one of them
231//# returns success. Opposite of Sequence Nodes.
232//#
233//############################################################################
235{
236public:
238 WsfObject* Clone() const override;
239 bool LocalExecute(double aSimTime) override;
240};
241
242//############################################################################
243//#
244//# Parallel Nodes perform each of their children, no matter what.
245//#
246//############################################################################
248{
249public:
251 WsfObject* Clone() const override;
252 bool LocalExecute(double aSimTime) override;
253};
254
255//############################################################################
256//#
257//# PrioritySelector Nodes perform their highest valued child.
258//# Ties are resolved by performing the 1st attached child in the tie.
259//#
260//############################################################################
262{
263public:
265 WsfObject* Clone() const override;
266 bool LocalExecute(double aSimTime) override;
267};
268
269//############################################################################
270//#
271//# WeightedRandom Nodes perform a randomly selected child.
272//# The random selection is weighted, according to the child values.
273//# If a child has value of zero, it is guaranteed not to run.
274//#
275//############################################################################
277{
278public:
280 WsfObject* Clone() const override;
281 bool LocalExecute(double aSimTime) override;
282};
283
284#endif
#define WSF_EXPORT
Definition WsfXIO_Export.hpp:35
bool RegisterExternVariable(const std::string &aVariableType, const std::string &aVariableName)
Definition WsfBehaviorTreeNode.cpp:928
void SetTree(WsfBehaviorTree *aTreePtr) override
Definition WsfBehaviorTreeNode.cpp:922
bool Initialize(double aSimTime, WsfScriptContext *aParentContextPtr) override
Definition WsfBehaviorTreeNode.cpp:864
void RegisterInput(UtInput &aInput) override
Definition WsfBehaviorTreeNode.cpp:852
WsfBehaviorTreeLeafNode & operator=(const WsfBehaviorTreeLeafNode &aRhs)=delete
bool LocalExecute(double aSimTime) override
Definition WsfBehaviorTreeNode.cpp:896
~WsfBehaviorTreeLeafNode() override=default
WsfBehaviorTreeLeafNode(WsfScenario &aScenario, bool aRegisterPlatformVariable=true, bool aRegisterProcessorVariable=true)
Definition WsfBehaviorTreeNode.cpp:762
LoadResult LoadType(UtInput &aInput) override
Definition WsfBehaviorTreeNode.cpp:58
WsfBehaviorTreeNodeTypes(WsfScenario &aScenario)
Definition WsfBehaviorTreeNode.cpp:45
Definition WsfBehaviorTreeNode.hpp:38
std::string mFullPathFileName
Definition WsfBehaviorTreeNode.hpp:145
bool mTurnedOn
Definition WsfBehaviorTreeNode.hpp:133
void TurnOff()
Definition WsfBehaviorTreeNode.hpp:90
friend class WsfBehaviorTreeLeafNode
Definition WsfBehaviorTreeNode.hpp:39
UtScript * mNewExecuteScriptPtr
Definition WsfBehaviorTreeNode.hpp:139
RunType
Definition WsfBehaviorTreeNode.hpp:48
@ ForTime
Definition WsfBehaviorTreeNode.hpp:50
@ Repeat
Definition WsfBehaviorTreeNode.hpp:49
@ UntilDone
Definition WsfBehaviorTreeNode.hpp:51
std::vector< WsfBehaviorTreeNode * > & Children()
Definition WsfBehaviorTreeNode.hpp:76
UtScript * mNewFailScriptPtr
Definition WsfBehaviorTreeNode.hpp:140
int mRepeats
Definition WsfBehaviorTreeNode.hpp:148
friend class WsfBehaviorTreeSequenceNode
Definition WsfBehaviorTreeNode.hpp:40
WsfBehaviorTreeNode * Parent()
Definition WsfBehaviorTreeNode.hpp:80
const WsfScenario & mScenario
Definition WsfBehaviorTreeNode.hpp:131
WsfBehaviorTreeNode * mParentPtr
Definition WsfBehaviorTreeNode.hpp:135
std::vector< WsfBehaviorTreeNode * > mChildren
Definition WsfBehaviorTreeNode.hpp:136
int mRepeatsLeft
Definition WsfBehaviorTreeNode.hpp:151
void SetParent(WsfBehaviorTreeNode *aNodePtr)
Definition WsfBehaviorTreeNode.hpp:79
static void Reset()
Definition WsfBehaviorTreeNode.hpp:70
int ChildCount() const
Definition WsfBehaviorTreeNode.hpp:77
WsfBehaviorTree * mParentTreePtr
Definition WsfBehaviorTreeNode.hpp:134
UtScript * mPreconditionScriptPtr
Definition WsfBehaviorTreeNode.hpp:138
WsfScriptMessageHandler * mMessageHandlerPtr
Definition WsfBehaviorTreeNode.hpp:158
SelectType
Definition WsfBehaviorTreeNode.hpp:55
@ Continous
Definition WsfBehaviorTreeNode.hpp:56
@ Finite
Definition WsfBehaviorTreeNode.hpp:57
const WsfScenario & GetScenario() const
Definition WsfBehaviorTreeNode.hpp:126
WsfBehaviorTreeNode(const WsfScenario &aScenario)
Definition WsfBehaviorTreeNode.cpp:101
unsigned int mId
Definition WsfBehaviorTreeNode.hpp:144
RunType mRunType
Definition WsfBehaviorTreeNode.hpp:147
bool mLastExecute
Definition WsfBehaviorTreeNode.hpp:143
double mRunDuration
Definition WsfBehaviorTreeNode.hpp:149
WsfBehaviorTreeNode & operator=(const WsfBehaviorTreeNode &)=delete
friend class WsfBehaviorTreePrioritySelectorNode
Definition WsfBehaviorTreeNode.hpp:43
void TurnOn()
Definition WsfBehaviorTreeNode.hpp:89
WsfScriptStateMachine * mFiniteStateMachinePtr
Definition WsfBehaviorTreeNode.hpp:156
bool mDebug
Definition WsfBehaviorTreeNode.hpp:159
WsfScriptContext & GetScriptContext()
Definition WsfBehaviorTreeNode.hpp:85
int mStateIndex
Definition WsfBehaviorTreeNode.hpp:157
static unsigned int GetUniqueNodeId(void)
Definition WsfBehaviorTreeNode.hpp:69
time_t mFileLastModTime
Definition WsfBehaviorTreeNode.hpp:146
friend class WsfBehaviorTreeWeightedRandomNode
Definition WsfBehaviorTreeNode.hpp:44
SelectType mSelectType
Definition WsfBehaviorTreeNode.hpp:153
std::string mFailureReason
Definition WsfBehaviorTreeNode.hpp:141
WsfObject * Clone() const override=0
friend class WsfBehaviorTreeParallelNode
Definition WsfBehaviorTreeNode.hpp:42
WsfScriptContext * mContextPtr
Definition WsfBehaviorTreeNode.hpp:137
WsfBehaviorTreeNode * mLastNodeSelectedPtr
Definition WsfBehaviorTreeNode.hpp:150
friend class WsfBehaviorTreeSelectorNode
Definition WsfBehaviorTreeNode.hpp:41
bool ExecutedLastRun()
Definition WsfBehaviorTreeNode.hpp:95
bool mInitialized
Definition WsfBehaviorTreeNode.hpp:132
double mRunStartTime
Definition WsfBehaviorTreeNode.hpp:152
double mFailureTime
Definition WsfBehaviorTreeNode.hpp:142
std::string FilePath() const
Definition WsfBehaviorTreeNode.hpp:116
std::string FailureReason() const
Definition WsfBehaviorTreeNode.hpp:93
time_t FileLastModTime() const
Definition WsfBehaviorTreeNode.hpp:117
virtual bool LocalExecute(double aSimTime)=0
int mSelectCount
Definition WsfBehaviorTreeNode.hpp:155
int mSelectLimit
Definition WsfBehaviorTreeNode.hpp:154
unsigned int Id() const
Definition WsfBehaviorTreeNode.hpp:112
virtual void RegisterInput(UtInput &aInput)
Definition WsfBehaviorTreeNode.hpp:67
WsfBehaviorTreeParallelNode(const WsfScenario &aScenario)
Definition WsfBehaviorTreeNode.cpp:1031
bool LocalExecute(double aSimTime) override
Definition WsfBehaviorTreeNode.cpp:1045
WsfBehaviorTreePrioritySelectorNode(WsfScenario &aScenario)
Definition WsfBehaviorTreeNode.cpp:1064
bool LocalExecute(double aSimTime) override
Definition WsfBehaviorTreeNode.cpp:1079
WsfBehaviorTreeSelectorNode(WsfScenario &aScenario)
Definition WsfBehaviorTreeNode.cpp:986
bool LocalExecute(double aSimTime) override
Definition WsfBehaviorTreeNode.cpp:1000
bool LocalExecute(double aSimTime) override
Definition WsfBehaviorTreeNode.cpp:956
WsfBehaviorTreeSequenceNode(WsfScenario &aScenario)
Definition WsfBehaviorTreeNode.cpp:941
WsfBehaviorTreeWeightedRandomNode(WsfScenario &aScenario)
Definition WsfBehaviorTreeNode.cpp:1144
bool LocalExecute(double aSimTime) override
Definition WsfBehaviorTreeNode.cpp:1159
Definition WsfBehaviorTree.hpp:31
A base class for messages sent among simulation communication objects, processors and sensors.
Definition WsfMessage.hpp:33
WsfObjectTypeList(WsfScenario &aScenario, unsigned int aFlags, const std::string &aBlockName)
Definition WsfObjectTypeList.hpp:76
WsfObject()
This is the constructor for the WsfObject class.
Definition WsfObject.cpp:88
virtual WsfObject * Clone() const =0
virtual bool ProcessInput(UtInput &aInput)
Definition WsfObject.cpp:140
const char * GetScriptClassName() const override
Definition WsfObject.cpp:115
Platforms represent a entity within the simulation.
Definition WsfPlatform.hpp:115
Contains the data required to create a simulation, and acts as the entry point for input file process...
Definition WsfScenario.hpp:111
WsfScriptBehaviorTreeNodeClass(const std::string &aClassName, UtScriptTypes *aScriptTypesPtr)
Definition WsfBehaviorTreeNode.cpp:691
UtScriptContext * GetContext(void *aObjectPtr) override
Definition WsfBehaviorTreeNode.cpp:705
Definition WsfScriptContext.hpp:71
Definition WsfScriptMessageHandler.hpp:42
WsfScriptObjectClass(const std::string &aClassName, UtScriptTypes *aTypesPtr)
Definition WsfScriptObjectClass.cpp:24
Definition WsfScriptProcessor.hpp:43
Definition WsfScriptStateMachine.hpp:47
The main controller for a simulation.
Definition WsfSimulation.hpp:109
Copyrights Multiple, All Rights Reserved