BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
Recorder.cpp
Go to the documentation of this file.
1#include "Recorder.h"
4
5using namespace BPMNOS::Execution;
6
7Recorder::Recorder(size_t maxSize) : objective(0), os(std::nullopt), maxSize(maxSize)
8{
9 log = nlohmann::ordered_json::array();
10}
11
12Recorder::Recorder(std::ostream &os, size_t maxSize) : objective(0), os(os), maxSize(maxSize)
13{
14 log = nlohmann::ordered_json::array();
15 if (this->os.has_value()) {
16 this->os.value().get() << Color::Modifier(Color::FG_LIGHT_GRAY) << "[" << Color::Modifier(Color::FG_DEFAULT);
17 }
18 isFirst = true;
19}
20
22{
23 if (os.has_value()) {
24 os.value().get() << Color::Modifier(Color::FG_LIGHT_GRAY) << "]" << Color::Modifier(Color::FG_DEFAULT) << std::endl;
25 os.value().get() << "Objective (maximization): " << (float)objective << std::endl;
26 os.value().get() << "Objective (minimization): " << -(float)objective << std::endl;
27 }
28}
29
37
38void Recorder::notice(const Observable* observable) {
40 auto token = static_cast<const Token*>(observable);
42 auto json = token->jsonify();
43
44 if (os.has_value()) {
45 if ( !isFirst ) {
47 }
48 os.value().get() << Color::Modifier(Color::FG_LIGHT_GRAY) <<json.dump() << Color::Modifier(Color::FG_DEFAULT);
49 isFirst = false;
50 }
51
52 log.push_back( json );
53
54 if ( log.size() > maxSize) {
55 log.erase(log.begin());
56 }
57 }
58 else if ( observable->getObservableType() == Execution::Observable::Type::Event ) {
59 auto event = static_cast<const Event*>(observable);
60 auto json = event->jsonify();
61
62 if (os.has_value()) {
63 if ( !isFirst ) {
65 }
66 os.value().get() << Color::Modifier(Color::FG_LIGHT_CYAN) <<json.dump() << Color::Modifier(Color::FG_DEFAULT);
67 isFirst = false;
68 }
69
70 log.push_back( json );
71
72 if ( log.size() > maxSize) {
73 log.erase(log.begin());
74 }
75 }
76 else if ( observable->getObservableType() == Execution::Observable::Type::Message ) {
77 auto message = static_cast<const Message*>(observable);
78 auto json = message->jsonify();
79
80 if (os.has_value()) {
81 if ( !isFirst ) {
83 }
84 os.value().get() << Color::Modifier(Color::FG_LIGHT_YELLOW) << json.dump() << Color::Modifier(Color::FG_DEFAULT);
85 isFirst = false;
86 }
87
88 log.push_back( json );
89
90 if ( log.size() > maxSize) {
91 log.erase(log.begin());
92 }
93 }
94
95};
96
97nlohmann::ordered_json Recorder::find(nlohmann::json include, nlohmann::json exclude) const {
98 nlohmann::ordered_json result = nlohmann::ordered_json::array();
99 std::copy_if(
100 log.begin(),
101 log.end(),
102 std::back_inserter(result), [&include,&exclude](const nlohmann::json& item) {
103 for ( auto& [key,value] : include.items() ) {
104 if ( !item.contains(key) || !(value.is_null() || item[key] == value) ) {
105 return false;
106 }
107 }
108 for ( auto& [key,value] : exclude.items() ) {
109 if ( item.contains(key) && (value.is_null() || item[key] == value) ) {
110 return false;
111 }
112 }
113 return true;
114 }
115 );
116 return result;
117}
118
nlohmann::ordered_json jsonify() const
Definition Message.cpp:48
void addSubscriber(Observer *subscriber, ObservableTypes... observableTypes)
Definition Notifier.h:16
nlohmann::ordered_json find(nlohmann::json include, nlohmann::json exclude=nlohmann::json()) const
Returns a json array containing all log entries matching the include object and not matching the excl...
Definition Recorder.cpp:97
nlohmann::ordered_json log
A json object of the entire log.
Definition Recorder.h:60
void subscribe(Engine *engine)
Definition Recorder.cpp:30
void notice(const Observable *observable) override
Definition Recorder.cpp:38
Recorder(size_t maxSize=std::numeric_limits< size_t >::max())
Definition Recorder.cpp:7
BPMNOS::number objective
The global objective.
Definition Recorder.h:59
const SystemState * systemState
BPMNOS::number getObjective() const
Returns the total objective value (assuming maximization) accumulated during execution.
Represents a token running through a (sub)process.
Definition Token.h:35
const StateMachine * owner
State machine owning the token.
Definition Token.h:44
STL namespace.
virtual nlohmann::ordered_json jsonify() const =0
virtual constexpr Type getObservableType() const =0