WSF
WsfMultiresolutionPlatformComponentImpl.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 (C) 2021 Stellar Science; U.S. Government has Unlimited Rights.
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#include <algorithm>
13#include <iterator>
14#include <type_traits>
15
17#include "UtInput.hpp"
18#include "UtInputBlock.hpp"
19#include "UtLog.hpp"
20#include "UtMemory.hpp"
21#include "WsfComponentRoles.hpp"
24#include "WsfPlatform.hpp"
25#include "WsfScenario.hpp"
26
27namespace
28{
32template<typename T>
33struct ObjectTypeTraits
34{
35 static_assert(std::is_base_of<WsfObject, T>::value,
36 "ObjectTypeTraits may only be instantiated with types deriving from WsfObject.");
37
38 static constexpr bool cIS_NAMED = wsf::multiresolution::ComponentName<T>::cREQUIRES_USER_INPUT_NAME;
39 static constexpr bool cIS_UNNAMED = !wsf::multiresolution::ComponentName<T>::cREQUIRES_USER_INPUT_NAME;
40
41 static constexpr bool cIS_PLATFORM_COMPONENT = std::is_base_of<WsfPlatformComponent, T>::value;
42 static constexpr bool cIS_MOVER = std::is_base_of<WsfMover, T>::value;
43 static constexpr bool cIS_NON_MOVER_COMPONENT = cIS_PLATFORM_COMPONENT && !cIS_MOVER;
44 static constexpr bool cIS_NAMED_NON_MOVER_COMPONENT = cIS_NON_MOVER_COMPONENT && cIS_NAMED;
45 static constexpr bool cIS_UNNAMED_NON_MOVER_COMPONENT = cIS_NON_MOVER_COMPONENT && cIS_UNNAMED;
46 static constexpr bool cIS_SIGNATURE = std::is_base_of<WsfSignature, T>::value;
47};
48
49template<typename DerivedComponent>
50typename std::enable_if<ObjectTypeTraits<DerivedComponent>::cIS_PLATFORM_COMPONENT, std::string>::type
51NameForWarning(const DerivedComponent& derivedComponent)
52{
53 return "component " + derivedComponent.GetComponentName();
54}
55
56template<typename DerivedComponent>
57typename std::enable_if<ObjectTypeTraits<DerivedComponent>::cIS_SIGNATURE, std::string>::type
58NameForWarning(const DerivedComponent& derivedComponent)
59{
60 return "signature " + derivedComponent.GetName();
61}
62
64// This (with the below function) can be `if constexpr` in C++17
65template<typename DerivedComponent>
66typename std::enable_if<ObjectTypeTraits<DerivedComponent>::cIS_MOVER, bool>::type
67SetComponentOnPlatform(WsfPlatform& aPlatform, DerivedComponent* aComponent, const std::string& /*aComponentName*/)
68{
69 aPlatform.SetMover(aComponent);
70 return true;
71}
72
74// This can be `if constexpr` in C++17
75template<typename DerivedComponent>
76typename std::enable_if<ObjectTypeTraits<DerivedComponent>::cIS_NAMED_NON_MOVER_COMPONENT, bool>::type
77SetComponentOnPlatform(WsfPlatform& aPlatform, DerivedComponent* aComponent, const std::string& aComponentName)
78{
79 aComponent->SetName(aComponentName);
80 return aPlatform.AddComponent(aComponent);
81}
82
84// This (with the above) can be `if constexpr` in C++17
85template<typename DerivedComponent>
86typename std::enable_if<ObjectTypeTraits<DerivedComponent>::cIS_UNNAMED_NON_MOVER_COMPONENT, bool>::type
87SetComponentOnPlatform(WsfPlatform& aPlatform, DerivedComponent* aComponent, const std::string& /*aComponentName*/)
88{
89 return aPlatform.AddComponent(aComponent);
90}
91
93// This can be `if constexpr` in C++17
94template<typename DerivedComponent>
95typename std::enable_if<ObjectTypeTraits<DerivedComponent>::cIS_SIGNATURE, bool>::type
96SetComponentOnPlatform(WsfPlatform& aPlatform, DerivedComponent* aComponent, const std::string& /*aComponentName*/)
97{
98 WsfSignatureInterface* interfacePtr = aPlatform.GetSignatureList().GetInterface(DerivedComponent::cSIGNATURE_INDEX);
99 if (interfacePtr)
100 {
101 interfacePtr->SetSignature(aComponent);
102 }
103 return interfacePtr != nullptr;
104}
105
107// (Can be replaced with if constexpr in C++17)
108template<typename DerivedComponent>
109typename std::enable_if<ObjectTypeTraits<DerivedComponent>::cIS_PLATFORM_COMPONENT, bool>::type
110PreInitializeComponent(WsfPlatform* const /*aPlatform*/, DerivedComponent* const aComponent, const double aSimTime)
111{
112 return aComponent->PreInitialize(aSimTime);
113}
114
115template<typename DerivedComponent>
116typename std::enable_if<ObjectTypeTraits<DerivedComponent>::cIS_SIGNATURE, bool>::type
117PreInitializeComponent(WsfPlatform* const aPlatform, DerivedComponent* const aComponent, const double aSimTime)
118{
119 return aComponent->Initialize(aSimTime, aPlatform);
120}
121
122} // namespace
123
124template<typename DerivedComponent>
131
132template<typename DerivedComponent>
134{
135 static int roles[] = {cMULTIRESOLUTION_COMPONENT_ROLE, cWSF_COMPONENT_NULL};
136 return roles;
137}
138
139template<typename DerivedComponent>
141{
142 if (aRole == cMULTIRESOLUTION_COMPONENT_ROLE)
143 {
144 return this;
145 }
146 return nullptr;
147}
148
149template<typename DerivedComponent>
152{
153 WsfMultiresolutionPlatformComponent* componentPtr = nullptr;
155 return componentPtr;
156}
157
158template<typename DerivedComponent>
160{
161 const std::string command(aInput.GetCommand());
162 if (command == "fidelity")
163 {
164 aInput.ReadValue(mFidelity);
165 aInput.ValueInClosedRange(mFidelity, 0.0, 1.0);
166 return true;
167 }
168 else if (WsfPlatformComponent::ProcessInput(aInput))
169 {
170 return true;
171 }
172 else if (WsfObject::ProcessInput(aInput))
173 {
174 return true;
175 }
176 else
177 {
178 throw UtInput::BadValue(aInput,
179 "Unexpected command " + command + " encountered in " +
181 }
182}
183
184template<typename DerivedComponent>
186{
187 assert(GetComponentParent());
188
189 const WsfMultiresolutionMultirunTable* const multirunTable =
191
192 std::string componentName = wsf::multiresolution::RequiresUserInputName<DerivedComponent>() ? GetName() : "";
193 return multirunTable
194 ->GetFidelity({GetComponentParent()->GetName(),
196 std::move(componentName)})
197 .value_or(mFidelity);
198}
199
200template<typename DerivedComponent>
202{
203 bool ok = WsfComponent::PreInitialize(aSimTime);
204 WsfPlatform* const parentPlatform = GetComponentParent();
205 ok &= parentPlatform != nullptr;
206 if (ok)
207 {
208 std::unique_ptr<DerivedComponent> componentModel = GetModel();
209 ok &= componentModel != nullptr;
210 if (ok)
211 {
212 ok &= SetComponentOnPlatform(*parentPlatform, componentModel.get(), GetName());
213 if (ok)
214 {
215 // Finally release pointer. Since SetComponentOnPlatform was successful, the platform takes ownership
216 ok &= PreInitializeComponent(parentPlatform, componentModel.release(), aSimTime);
217 }
218 else
219 {
220 ut::log::error() << "In " << wsf::multiresolution::CommandName<DerivedComponent>() << ": Unable to add "
221 << NameForWarning(*componentModel) << " to platform with name " << parentPlatform->GetName();
222 // Documentation of AddComponentP states that if AddComponent returns false, it doesn't assume ownership.
223 // If this branch is reached, then componentModel will be destroyed on exit
224 }
225 }
226 }
227 return ok;
228}
@ cWSF_COMPONENT_NULL
A special entry to indicate the end of the list of roles assumed by a component.
Definition WsfComponentRoles.hpp:92
WsfComponentT< WsfPlatform > WsfPlatformComponent
A convenient typedef for a platform component.
Definition WsfComponent.hpp:171
bool ok
Definition WsfScriptWeaponClass.cpp:211
bool AddComponent(ComponentType *aComponentPtr)
Definition WsfComponentList.hpp:464
bool FindByRole(T *&aPtr) const
Definition WsfComponentList.hpp:170
ParentType * GetComponentParent() const
Definition WsfComponent.hpp:154
virtual bool PreInitialize(double aSimTime)
Definition WsfComponent.cpp:24
virtual bool ProcessInput(UtInput &aInput)
Definition WsfComponent.cpp:100
Definition WsfMultiresolutionMultirunTable.hpp:39
static WsfMultiresolutionMultirunTable * Find(const WsfScenario &aScenario)
Definition WsfMultiresolutionMultirunTable.cpp:193
ut::optional< double > GetFidelity(const wsf::multiresolution::PlatformComponentName &aPlatformComponentName) const
Definition WsfMultiresolutionMultirunTable.cpp:178
A base class template for defining multiresolution WsfPlatformComponent types.
Definition WsfMultiresolutionPlatformComponent.hpp:40
bool PreInitialize(double aSimTime) override
Definition WsfMultiresolutionPlatformComponentImpl.hpp:201
WsfMultiresolutionPlatformComponent()
Definition WsfMultiresolutionPlatformComponentImpl.hpp:125
bool ProcessInput(UtInput &aInput) override
Definition WsfMultiresolutionPlatformComponentImpl.hpp:159
static WsfMultiresolutionPlatformComponent< DerivedComponent > * Find(const WsfPlatform &aParent)
Definition WsfMultiresolutionPlatformComponentImpl.hpp:151
double GetFidelity() const
Definition WsfMultiresolutionPlatformComponentImpl.hpp:185
virtual std::unique_ptr< DerivedComponent > GetModel() const =0
void * QueryInterface(int aRole) override
Definition WsfMultiresolutionPlatformComponentImpl.hpp:140
const int * GetComponentRoles() const override
Definition WsfMultiresolutionPlatformComponentImpl.hpp:133
const std::string & GetName() const
Definition WsfObject.cpp:235
WsfObject()
This is the constructor for the WsfObject class.
Definition WsfObject.cpp:88
void SetName(WsfStringId aName)
Definition WsfObject.hpp:70
virtual bool ProcessInput(UtInput &aInput)
Definition WsfObject.cpp:140
Platforms represent a entity within the simulation.
Definition WsfPlatform.hpp:115
void SetMover(WsfMover *aMoverPtr)
Definition WsfPlatform.cpp:1167
const WsfPlatformComponentList & GetComponents() const
Return a reference to the raw component list.
Definition WsfPlatform.hpp:398
WsfSignatureList & GetSignatureList()
Return a reference to the 'signature list' object that encapsulates all the signature processing.
Definition WsfPlatform.hpp:471
Definition WsfSignatureInterface.hpp:32
virtual bool Initialize(double aSimTime, WsfPlatform *aPlatformPtr)
Definition WsfSignatureInterface.cpp:47
void SetSignature(WsfSignature *aSignaturePtr)
Definition WsfSignatureInterface.cpp:89
Interface * GetInterface(size_t aIndex)
Return the pointer to the interface with the specified index.
Definition WsfSignatureList.hpp:48
std::string CommandName()
This name, associated with DerivedComponent, is for use in the multiresolution framework....
Definition ComponentNameHelper.hpp:74
constexpr bool RequiresUserInputName()
Definition ComponentNameHelper.hpp:97
Helper struct for assigning names to platform components.
Definition ComponentNameHelper.hpp:54
Copyrights Multiple, All Rights Reserved