helics  2.8.1
ForwardingTimeCoordinator.hpp
1 /*
2 Copyright (c) 2017-2021,
3 Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable
4 Energy, LLC. See the top-level NOTICE for additional details. All rights reserved.
5 SPDX-License-Identifier: BSD-3-Clause
6 */
7 #pragma once
8 
9 #include "ActionMessage.hpp"
10 #include "CoreFederateInfo.hpp"
11 #include "TimeDependencies.hpp"
12 
13 #include "json/forwards.h"
14 #include <atomic>
15 #include <functional>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 namespace helics {
21 
27  private:
28  // the variables for time coordination
29  DependencyInfo upstream;
30  DependencyInfo downstream;
31 
32  // Core::local_federate_id parent = invalid_fed_id; //!< the id for the parent object which
33  // should also be a ForwardingTimeCoordinator
34  TimeDependencies dependencies;
35  std::function<void(const ActionMessage&)> sendMessageFunction;
37 
38  public:
41  global_federate_id delayedFederate{};
43  bool checkingExec{false};
44  bool executionMode{false};
45  bool iterating{false};
46  bool ignoreMinFed{false};
47  bool restrictive_time_policy{false};
49  bool noParent{false};
51  private:
52  bool federatesOnly{false};
53  public:
54  ForwardingTimeCoordinator() = default;
55 
57  void setMessageSender(std::function<void(const ActionMessage&)> userSendMessageFunction)
58  {
59  sendMessageFunction = std::move(userSendMessageFunction);
60  }
61 
63  std::vector<global_federate_id> getDependencies() const;
65  std::vector<global_federate_id> getDependents() const;
66 
70  void updateTimeFactors();
71 
77  bool isDependency(global_federate_id ofed) const;
79  bool empty() const { return dependencies.empty(); }
80 
81  private:
82  void transmitTimingMessagesUpstream(ActionMessage& msg) const;
83  void transmitTimingMessagesDownstream(ActionMessage& msg,
84  global_federate_id skipFed = global_federate_id{}) const;
86  ActionMessage generateTimeRequest(const DependencyInfo& dep, global_federate_id fed) const;
87 
88  public:
92  bool processTimeMessage(const ActionMessage& cmd);
93 
95  void processDependencyUpdateMessage(const ActionMessage& cmd);
99  bool addDependency(global_federate_id fedID);
103  bool addDependent(global_federate_id fedID);
106  void removeDependency(global_federate_id fedID);
109  void removeDependent(global_federate_id fedID);
110 
111  void setAsChild(global_federate_id fedID);
112  void setAsParent(global_federate_id fedID);
113 
115  void disconnect();
118 
121  void enteringExecMode();
122 
124  std::string printTimeStatus() const;
126  void generateDebuggingTimeInfo(Json::Value& base) const;
127 
129  bool hasActiveTimeDependencies() const;
131  Time getNextTime() const { return downstream.next; }
133  int dependencyCount() const;
136 };
137 } // namespace helics
helics::timeZero
constexpr Time timeZero
Definition: helics-time.hpp:31
helics::ForwardingTimeCoordinator::getNextTime
Time getNextTime() const
Definition: ForwardingTimeCoordinator.hpp:131
helics::ActionMessage::source_id
global_federate_id source_id
12 – for federate_id or route_id
Definition: ActionMessage.hpp:36
helics::TimeDependencies::removeDependency
void removeDependency(global_federate_id id)
Definition: TimeDependencies.cpp:264
helics::ForwardingTimeCoordinator::restrictive_time_policy
bool restrictive_time_policy
flag indicating that a restrictive time policy should be used
Definition: ForwardingTimeCoordinator.hpp:48
helics::TimeDependencies::isDependency
bool isDependency(global_federate_id ofed) const
Definition: TimeDependencies.cpp:201
helics::ForwardingTimeCoordinator::removeDependency
void removeDependency(global_federate_id fedID)
Definition: ForwardingTimeCoordinator.cpp:194
helics::TimeDependencies::getMinDependency
global_federate_id getMinDependency() const
Definition: TimeDependencies.cpp:362
helics::TimeDependencies::addDependency
bool addDependency(global_federate_id id)
Definition: TimeDependencies.cpp:239
helics::ForwardingTimeCoordinator::updateTimeFactors
void updateTimeFactors()
Definition: ForwardingTimeCoordinator.cpp:83
helics::ForwardingTimeCoordinator::generateDebuggingTimeInfo
void generateDebuggingTimeInfo(Json::Value &base) const
Definition: ForwardingTimeCoordinator.cpp:130
helics::TimeDependencies
Definition: TimeDependencies.hpp:75
helics_definitions.hpp
base helics enumerations for C++ API's, a namespace wrapper for the definitions defined in helics_enu...
helics::global_federate_id
Definition: global_federate_id.hpp:68
helics::ForwardingTimeCoordinator::source_id
global_federate_id source_id
the identifier for inserting into the source id field of any generated messages;
Definition: ForwardingTimeCoordinator.hpp:40
helics::DependencyInfo::fedID
global_federate_id fedID
identifier for the dependency
Definition: TimeDependencies.hpp:55
helics::parent_broker_id
constexpr global_broker_id parent_broker_id
Definition: global_federate_id.hpp:60
helics::DependencyInfo::dependent
bool dependent
indicator the dependency is a dependent object
Definition: TimeDependencies.hpp:60
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helics-time.hpp:27
helics::TimeData::Te
Time Te
the next currently scheduled event
Definition: TimeDependencies.hpp:39
helics::ActionMessage
Definition: ActionMessage.hpp:29
helics::message_processing_result::error
@ error
indicator that an error has occurred
helics::ForwardingTimeCoordinator::ignoreMinFed
bool ignoreMinFed
Definition: ForwardingTimeCoordinator.hpp:46
helics::ForwardingTimeCoordinator::addDependency
bool addDependency(global_federate_id fedID)
Definition: ForwardingTimeCoordinator.cpp:168
iteration_requested_flag
@ iteration_requested_flag
indicator that an iteration has been requested
Definition: flagOperations.hpp:16
helics::ForwardingTimeCoordinator
Definition: ForwardingTimeCoordinator.hpp:26
helics::ForwardingTimeCoordinator::processTimeMessage
bool processTimeMessage(const ActionMessage &cmd)
Definition: ForwardingTimeCoordinator.cpp:364
helics::appendMessage
int appendMessage(ActionMessage &m, const ActionMessage &newMessage)
Definition: ActionMessage.cpp:939
checkActionFlag
bool checkActionFlag(uint16_t flags, FlagIndex flag)
Definition: flagOperations.hpp:75
helics::message_processing_result
message_processing_result
Definition: core-types.hpp:65
helics::TimeDependencies::size
auto size() const
Definition: TimeDependencies.hpp:100
helics::ForwardingTimeCoordinator::executionMode
bool executionMode
flag that the coordinator has entered the execution Mode
Definition: ForwardingTimeCoordinator.hpp:44
helics::ForwardingTimeCoordinator::processDependencyUpdateMessage
void processDependencyUpdateMessage(const ActionMessage &cmd)
Definition: ForwardingTimeCoordinator.cpp:380
helics::message_processing_result::continue_processing
@ continue_processing
the current loop should continue
child_flag
constexpr uint16_t child_flag
overload of extra_flag4 indicating a message is from a child object
Definition: flagOperations.hpp:46
helics::ForwardingTimeCoordinator::getMinDependency
global_federate_id getMinDependency() const
Definition: ForwardingTimeCoordinator.cpp:241
helics::TimeData::next
Time next
next possible message or value
Definition: TimeDependencies.hpp:38
helics::TimeDependencies::activeDependencyCount
int activeDependencyCount() const
Definition: TimeDependencies.cpp:355
helics::ActionMessage::payload
std::string payload
Definition: ActionMessage.hpp:44
helics::ForwardingTimeCoordinator::empty
bool empty() const
Definition: ForwardingTimeCoordinator.hpp:79
helics::global_federate_id::baseValue
constexpr base_type baseValue() const
Definition: global_federate_id.hpp:83
helics::ActionMessage::messageID
int32_t messageID
8 – message ID for a variety of purposes
Definition: ActionMessage.hpp:35
helics::ForwardingTimeCoordinator::setMessageSender
void setMessageSender(std::function< void(const ActionMessage &)> userSendMessageFunction)
Definition: ForwardingTimeCoordinator.hpp:57
helics::TimeDependencies::begin
auto begin()
Definition: TimeDependencies.hpp:102
helics::ForwardingTimeCoordinator::enteringExecMode
void enteringExecMode()
Definition: ForwardingTimeCoordinator.cpp:22
fed
@ fed
special logging command for message coming from a fed
Definition: loggingHelper.hpp:32
helics::ForwardingTimeCoordinator::getDependencies
std::vector< global_federate_id > getDependencies() const
Definition: ForwardingTimeCoordinator.cpp:209
helics::ForwardingTimeCoordinator::hasActiveTimeDependencies
bool hasActiveTimeDependencies() const
Definition: ForwardingTimeCoordinator.cpp:231
setActionFlag
void setActionFlag(FlagContainer &M, FlagIndex flag)
Definition: flagOperations.hpp:67
helics::TimeDependencies::addDependent
bool addDependent(global_federate_id id)
Definition: TimeDependencies.cpp:277
helics::DependencyInfo
Definition: TimeDependencies.hpp:53
helics::ForwardingTimeCoordinator::isDependency
bool isDependency(global_federate_id ofed) const
Definition: ForwardingTimeCoordinator.cpp:163
parent_flag
constexpr uint16_t parent_flag
overload of extra_flag3 indicating the message is from a parent object
Definition: flagOperations.hpp:43
helics::ForwardingTimeCoordinator::checkExecEntry
message_processing_result checkExecEntry()
Definition: ForwardingTimeCoordinator.cpp:246
helics::ForwardingTimeCoordinator::disconnect
void disconnect()
Definition: ForwardingTimeCoordinator.cpp:47
helics::ForwardingTimeCoordinator::hasDelayedTimingFederate
bool hasDelayedTimingFederate
indicator that federate is using delayed timing
Definition: ForwardingTimeCoordinator.hpp:50
helics::ForwardingTimeCoordinator::noParent
bool noParent
indicator that the coordinator does not have parents
Definition: ForwardingTimeCoordinator.hpp:49
helics::TimeDependencies::checkIfReadyForExecEntry
bool checkIfReadyForExecEntry(bool iterating) const
Definition: TimeDependencies.cpp:336
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::ForwardingTimeCoordinator::checkingExec
bool checkingExec
flag indicating that the coordinator is trying to enter the exec mode
Definition: ForwardingTimeCoordinator.hpp:43
helics::ForwardingTimeCoordinator::getDependencyInfo
const DependencyInfo * getDependencyInfo(global_federate_id ofed) const
Definition: ForwardingTimeCoordinator.cpp:204
helics::TimeDependencies::hasActiveTimeDependencies
bool hasActiveTimeDependencies() const
Definition: TimeDependencies.cpp:348
helics::ForwardingTimeCoordinator::iterating
bool iterating
flag indicating that the min dependency is iterating
Definition: ForwardingTimeCoordinator.hpp:45
helics::TimeData::update
bool update(const TimeData &update)
Definition: TimeDependencies.cpp:95
helics::ForwardingTimeCoordinator::addDependent
bool addDependent(global_federate_id fedID)
Definition: ForwardingTimeCoordinator.cpp:173
helics::TimeData::minDe
Time minDe
min dependency event time
Definition: TimeDependencies.hpp:40
helics::DependencyInfo::dependency
bool dependency
indicator that the dependency is an actual dependency
Definition: TimeDependencies.hpp:61
helics::ForwardingTimeCoordinator::removeDependent
void removeDependent(global_federate_id fedID)
Definition: ForwardingTimeCoordinator.cpp:199
helics::ForwardingTimeCoordinator::dependencyCount
int dependencyCount() const
Definition: ForwardingTimeCoordinator.cpp:236
helics::ForwardingTimeCoordinator::getDependents
std::vector< global_federate_id > getDependents() const
Definition: ForwardingTimeCoordinator.cpp:220
helics::ActionMessage::dest_id
global_federate_id dest_id
20 fed_id for a targeted message
Definition: ActionMessage.hpp:38
helics::TimeDependencies::empty
bool empty() const
Definition: TimeDependencies.hpp:115
flagOperations.hpp
helics::ForwardingTimeCoordinator::printTimeStatus
std::string printTimeStatus() const
Definition: ForwardingTimeCoordinator.cpp:155
helics::message_processing_result::next_step
@ next_step
indicator that the iterations have completed
helics::TimeData::minFed
global_federate_id minFed
identifier for the min dependency
Definition: TimeDependencies.hpp:42
helics::TimeDependencies::getDependencyInfo
const DependencyInfo * getDependencyInfo(global_federate_id id) const
Definition: TimeDependencies.cpp:219
helics::TimeDependencies::removeDependent
void removeDependent(global_federate_id id)
Definition: TimeDependencies.cpp:302