WSF
WsfAdvancedBehaviorTree.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 2021 Infoscitex, a DCS 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 WSFADVANCEDBEHAVIORTREE_HPP
13#define WSFADVANCEDBEHAVIORTREE_HPP
14
15#include "wsf_export.h"
16
17#include <map>
18#include <string>
19#include <vector>
20
22#include "WsfObject.hpp"
25
26class UtInput;
28class WsfPlatform;
29class WsfScenario;
32class WsfSimulation;
33
34
36{
37public:
38 WsfScriptAdvancedBehaviorTreeClass(const std::string& aClassName, UtScriptTypes* aScriptTypesPtr);
39
43 UtScriptContext* GetContext(void* aObjectPtr) override;
44
45 // Script methods
46 // Each of the exported methods are defined as function objects.
51};
52
54{
55 enum RootNodeType
56 {
57 PARALLEL,
58 SEQUENCE,
59 SEQUENCE_WITH_MEMORY,
60 SELECTOR,
61 SELECTOR_WITH_MEMORY,
62 PRIORITY_SELECTOR,
63 WEIGHTED_RANDOM
64 };
65
66public:
67 WsfAdvancedBehaviorTree(const WsfScenario& aScenario);
69 ~WsfAdvancedBehaviorTree() override = default;
71
76 void AddBlackboard(const std::string& aName, std::shared_ptr<WsfAttributeContainer> aBlackboardPtr);
77
79 WsfAdvancedBehaviorTree* Clone() const override;
80
84 WsfAttributeContainer* GetSharedBlackboard(const std::string& aName) const;
85
87 std::vector<std::string> GetSharedBlackboardNames() const;
88
91
97 bool Initialize(double aSimTime, WsfScriptProcessor* aParentPtr, WsfScriptContext* aParentContextPtr = nullptr);
98
100 const size_t NodeCount() const { return mBehaviorNodes.size(); }
101
103 const unsigned int& GetTreeId() const { return mId; }
104
106 const std::string& GetName() const { return mName; }
107
109 static unsigned int GetUniqueTreeId() { return ++mNextUniqueId; }
110
112 static void Reset() { mNextUniqueId = 0; }
113
117 WsfAdvancedBehaviorTreeActionNode* NodeEntry(size_t aIndex) const;
118
120 WsfScriptProcessor* GetParentProcessor() const { return mParentPtr; }
121
125 bool ProcessInput(UtInput& aInput) override;
126
130 void RegisterActionNode(WsfAdvancedBehaviorTreeActionNode* aNodePtr);
131
133 std::shared_ptr<WsfAdvancedBehaviorTreeCompositeNode> RootNode() const { return mRootNodePtr; }
134
138 const bool SharedBlackboardExists(const std::string& aName) const;
139
143 void SetShouldOutputNextTick(const bool aValue) { mShouldOutput = aValue; }
144
148 bool Tick(double aSimTime);
149
153 WsfAdvancedBehaviorTreeNode* FindNode(const std::string& aNodeName) const;
154
158 void FindLastExecuted(std::vector<WsfAdvancedBehaviorTreeNode*>& aLastExecuted) const;
159
161 WsfPlatform* GetOwningPlatform() const;
162
164 const char* GetScriptClassName() const override;
165
167 const WsfScriptContext& GetScriptContext() const { return *mContextPtr; }
168
172 void SetParent(WsfScriptProcessor* aParentPtr) { mParentPtr = aParentPtr; }
173
177 void SetParentContext(WsfScriptContext* aParentContextPtr) { mContextPtr->SetParent(aParentContextPtr); }
178
180 void SetRootNodeClass();
181
185 void OutputTreeStructure(const double aSimTime);
186
190 void OutputTreeState(const double aSimTime);
191
195 void SetSuccessPolicy(const BT::SuccessPolicy aPolicy) { mSuccessPolicy = aPolicy; }
196
200 void SetThreshold(const unsigned int aThreshold) { mThreshold = aThreshold; }
201
202protected:
204 UtScriptContext* GetScriptAccessibleContext() const override;
205
210 bool RegisterExternVariable(const std::string& aVariableType, const std::string& aVariableName);
211
212private:
216 std::unique_ptr<WsfAdvancedBehaviorTreeNode> CreateNode(UtInput& aInput);
217
221 bool ProcessTree(UtInput& aInput);
222
226 const std::string GetFullFilePath(UtInput& aInput) const;
227
228private:
229 std::unique_ptr<WsfScriptContext> mContextPtr{nullptr};
230 std::vector<WsfAdvancedBehaviorTreeActionNode*> mBehaviorNodes;
232 std::map<std::string, std::shared_ptr<WsfAttributeContainer>> mBlackboards;
234 WsfScriptProcessor* mParentPtr;
235 std::shared_ptr<WsfAdvancedBehaviorTreeCompositeNode> mRootNodePtr;
236 bool mShouldOutput;
237 bool mOutputForBTT{false};
238 unsigned int mId;
239 std::string mName;
240 RootNodeType mRootNodeType;
241 BT::SuccessPolicy mSuccessPolicy{BT::cTHRESHOLD};
242 unsigned int mThreshold{1};
243 static unsigned int mNextUniqueId;
244};
245
246#endif // WSFADVANCEDBEHAVIORTREE_HPP
#define WSF_EXPORT
Definition WsfXIO_Export.hpp:35
Definition WsfAdvancedBehaviorTreeNode.hpp:584
Definition WsfAdvancedBehaviorTreeNode.hpp:138
std::vector< std::string > GetSharedBlackboardNames() const
Get a list of all shared blackboard names.
Definition WsfAdvancedBehaviorTree.cpp:192
void AddBlackboard(const std::string &aName, std::shared_ptr< WsfAttributeContainer > aBlackboardPtr)
Add a blackboard to the blackboard list.
Definition WsfAdvancedBehaviorTree.cpp:117
bool Initialize(double aSimTime, WsfScriptProcessor *aParentPtr, WsfScriptContext *aParentContextPtr=nullptr)
Initialize this tree.
Definition WsfAdvancedBehaviorTree.cpp:208
WsfAdvancedBehaviorTree(const WsfScenario &aScenario)
Definition WsfAdvancedBehaviorTree.cpp:74
void SetSuccessPolicy(const BT::SuccessPolicy aPolicy)
Set the success policy for this node.
Definition WsfAdvancedBehaviorTree.hpp:195
WsfAdvancedBehaviorTree & operator=(const WsfAdvancedBehaviorTree &aRhs)=delete
std::shared_ptr< WsfAdvancedBehaviorTreeCompositeNode > RootNode() const
Return a shared pointer to the root node of this tree.
Definition WsfAdvancedBehaviorTree.hpp:133
void SetShouldOutputNextTick(const bool aValue)
Set if the tree should output node values to the event pipe next tick.
Definition WsfAdvancedBehaviorTree.hpp:143
WsfScriptProcessor * GetParentProcessor() const
Return a pointer to the parent processor of this tree.
Definition WsfAdvancedBehaviorTree.hpp:120
void SetParentContext(WsfScriptContext *aParentContextPtr)
Set the parent script context for this tree.
Definition WsfAdvancedBehaviorTree.hpp:177
WsfAttributeContainer * GetSharedBlackboard(const std::string &aName) const
Get a blackboard with the specified name from the shared list.
Definition WsfAdvancedBehaviorTree.cpp:183
const std::string & GetName() const
Get the name of this tree.
Definition WsfAdvancedBehaviorTree.hpp:106
~WsfAdvancedBehaviorTree() override=default
const size_t NodeCount() const
Return a count of all leaf nodes.
Definition WsfAdvancedBehaviorTree.hpp:100
void SetParent(WsfScriptProcessor *aParentPtr)
Set the parent processor.
Definition WsfAdvancedBehaviorTree.hpp:172
static unsigned int GetUniqueTreeId()
Get a unique int id for this tree.
Definition WsfAdvancedBehaviorTree.hpp:109
static void Reset()
Reset the unique int counter.
Definition WsfAdvancedBehaviorTree.hpp:112
WsfSimulation * GetSimulation()
Return a pointer to the simulation.
Definition WsfAdvancedBehaviorTree.cpp:202
const unsigned int & GetTreeId() const
Get the id of this tree.
Definition WsfAdvancedBehaviorTree.hpp:103
void SetThreshold(const unsigned int aThreshold)
Set the threshold(number) of children that must succeed for this node to succeed.
Definition WsfAdvancedBehaviorTree.hpp:200
const WsfScriptContext & GetScriptContext() const
Returns a reference to the script context for this tree.
Definition WsfAdvancedBehaviorTree.hpp:167
Definition WsfAttributeContainer.hpp:24
WsfObject()
This is the constructor for the WsfObject class.
Definition WsfObject.cpp:88
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
WsfScriptAdvancedBehaviorTreeClass(const std::string &aClassName, UtScriptTypes *aScriptTypesPtr)
Definition WsfAdvancedBehaviorTree.cpp:25
UtScriptContext * GetContext(void *aObjectPtr) override
Return a pointer to the script context for an object.
Definition WsfAdvancedBehaviorTree.cpp:36
Definition WsfScriptContext.hpp:71
void * Clone(void *aObjectPtr) override
Definition WsfScriptObjectClass.cpp:50
WsfScriptObjectClass(const std::string &aClassName, UtScriptTypes *aTypesPtr)
Definition WsfScriptObjectClass.cpp:24
Definition WsfScriptProcessor.hpp:43
The main controller for a simulation.
Definition WsfSimulation.hpp:109
SuccessPolicy
Success policy for a Parallel composite node.
Definition WsfAdvancedBehaviorTreeNode.hpp:64
@ cTHRESHOLD
Definition WsfAdvancedBehaviorTreeNode.hpp:67
Copyrights Multiple, All Rights Reserved