reactive_function.cpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5  */
6 
8 #include <sol/sol.hpp>
10 #include <QMessageBox>
11 
12 namespace PJ
13 {
15 {
16  _lua_function = {};
17  _lua_engine = {};
18 
23 
25 
26  prepareLua();
27 
29  if (!result.valid())
30  {
31  sol::error err = result;
32  throw std::runtime_error(std::string("Error in Library part:\n") + err.what());
33  }
34 
36  if (!result.valid())
37  {
38  sol::error err = result;
39  throw std::runtime_error(std::string("Error in Global part:\n") + err.what());
40  }
41 
42  auto calcFunction = fmt::format("function calc(tracker_time)\n{}\nend", _function_code);
43  result = _lua_engine.script(calcFunction);
44  if (!result.valid())
45  {
46  sol::error err = result;
47  throw std::runtime_error(std::string("Error in Function part:\n") + err.what());
48  }
49  _lua_function = _lua_engine["calc"];
50 }
51 
53  QString lua_function, QString lua_library)
54  : _global_code(lua_global.toStdString())
55  , _function_code(lua_function.toStdString())
56  , _library_code(lua_library.toStdString())
57 {
58  _data = data_map;
59  init();
60 }
61 
63 {
64 }
65 
66 void ReactiveLuaFunction::setTimeTracker(double time_tracker_value)
67 {
68  _tracker_value = time_tracker_value;
69 }
70 
72 {
73  try
74  {
75  auto result = _lua_function(_tracker_value);
76  if (!result.valid())
77  {
78  sol::error err = result;
79  throw std::runtime_error(err.what());
80  }
81  }
82  catch (std::exception& err)
83  {
84  QMessageBox::warning(nullptr, "Error in Reactive Script", QString(err.what()),
85  QMessageBox::Cancel);
86  }
87 }
88 
89 bool ReactiveLuaFunction::xmlSaveState(QDomDocument&, QDomElement&) const
90 {
91  return false;
92 }
93 
94 bool ReactiveLuaFunction::xmlLoadState(const QDomElement&)
95 {
96  return false;
97 }
98 
100 {
102 
103  _timeseries_ref["find"] = [this](sol::object name) {
104  auto str = name.as<std::string>();
105  auto it = plotData()->numeric.find(str);
106  if (it == plotData()->numeric.end())
107  {
109  }
110  auto series = std::make_unique<TimeseriesRef>(&(it->second));
112  };
118 
119  //---------------------------------------
121 
122  _created_timeseries["new"] = [this](sol::object name) {
123  if (name.is<std::string>() == false)
124  {
126  }
127  auto str_name = name.as<std::string>();
128  auto series = CreatedSeriesTime(plotData(), str_name);
129  series.clear();
130  _created_curves.push_back(str_name);
131  return sol::object(_lua_engine, sol::in_place, series);
132  };
133 
138 
139  //---------------------------------------
141 
142  _created_scatter["new"] = [this](sol::object name) {
143  if (name.is<std::string>() == false)
144  {
146  }
147  auto str_name = name.as<std::string>();
148  auto series = CreatedSeriesXY(plotData(), str_name);
149  series.clear();
150  _created_curves.push_back(str_name);
151  return sol::object(_lua_engine, sol::in_place, series);
152  };
153 
158 
159  //---------------------------------------
160  auto GetSeriesNames = [this]() {
161  std::vector<std::string> names;
162  for (const auto& it : plotData()->numeric)
163  {
164  names.push_back(it.first);
165  }
166  return names;
167  };
168  _lua_engine.set_function("GetSeriesNames", GetSeriesNames);
169 }
170 
172 {
173 }
174 
175 std::pair<double, double> TimeseriesRef::at(unsigned i) const
176 {
177  const auto& p = _plot_data->at(i);
178  return { p.x, p.y };
179 }
180 
181 void TimeseriesRef::set(unsigned index, double x, double y)
182 {
183  auto& p = _plot_data->at(index);
184  p = { x, y };
185 }
186 
187 double TimeseriesRef::atTime(double t) const
188 {
189  int i = _plot_data->getIndexFromX(t);
190  return _plot_data->at(i).y;
191 }
192 
193 unsigned TimeseriesRef::size() const
194 {
195  return _plot_data->size();
196 }
197 
199 {
200  _plot_data->clear();
201 }
202 
203 CreatedSeriesBase::CreatedSeriesBase(PlotDataMapRef* data_map, const std::string& name,
204  bool timeseries)
205 {
206  if (timeseries)
207  {
208  _plot_data = &(data_map->getOrCreateNumeric(name));
209  }
210  else
211  {
212  _plot_data = &(data_map->getOrCreateScatterXY(name));
213  }
214 }
215 
216 std::pair<double, double> CreatedSeriesBase::at(unsigned i) const
217 {
218  const auto& p = _plot_data->at(i);
219  return { p.x, p.y };
220 }
221 
223 {
224  _plot_data->clear();
225 }
226 
227 void CreatedSeriesBase::push_back(double x, double y)
228 {
229  _plot_data->pushBack({ x, y });
230 }
231 
232 unsigned CreatedSeriesBase::size() const
233 {
234  return _plot_data->size();
235 }
236 
237 CreatedSeriesTime::CreatedSeriesTime(PlotDataMapRef* data_map, const std::string& name)
238  : CreatedSeriesBase(data_map, name, true)
239 {
240 }
241 
242 CreatedSeriesXY::CreatedSeriesXY(PlotDataMapRef* data_map, const std::string& name)
243  : CreatedSeriesBase(data_map, name, false)
244 {
245 }
246 
247 } // namespace PJ
sol::error::what
virtual const char * what() const noexcept override
Definition: sol.hpp:4593
PJ::ReactiveLuaFunction::xmlSaveState
bool xmlSaveState(QDomDocument &doc, QDomElement &parent_element) const override
Override this method to save the status of the plugin to XML.
Definition: reactive_function.cpp:89
PJ::TimeseriesBase
Definition: timeseries.h:16
sol::state_view::set_function
state_view & set_function(Key &&key, Args &&... args)
Definition: sol.hpp:28015
sol::protected_function_result
Definition: sol.hpp:17228
PJ::ReactiveLuaFunction::_created_curves
std::vector< std::string > _created_curves
Definition: reactive_function.h:120
PJ::ReactiveLuaFunction::_function_code
std::string _function_code
Definition: reactive_function.h:117
PJ::ReactiveLuaFunction::reset
void reset() override
Definition: reactive_function.cpp:62
sol::object
basic_object< reference > object
Definition: forward.hpp:1209
PJ::ReactiveLuaFunction::_library_code
std::string _library_code
Definition: reactive_function.h:118
PJ::ReactiveLuaFunction::_created_scatter
sol::usertype< CreatedSeriesXY > _created_scatter
Definition: reactive_function.h:127
sol::lib::base
@ base
PJ::ReactiveLuaFunction::_global_code
std::string _global_code
Definition: reactive_function.h:116
PJ::ReactiveLuaFunction::_lua_function
sol::protected_function _lua_function
Definition: reactive_function.h:123
PJ::TimeseriesRef::clear
void clear() const
Definition: reactive_function.cpp:198
PJ::CreatedSeriesBase::CreatedSeriesBase
CreatedSeriesBase(PlotDataMapRef *data_map, const std::string &name, bool timeseries)
Definition: reactive_function.cpp:203
PJ::CreatedSeriesTime::CreatedSeriesTime
CreatedSeriesTime(PlotDataMapRef *data_map, const std::string &name)
Definition: reactive_function.cpp:237
PJ::ReactiveLuaFunction::_lua_engine
sol::state _lua_engine
Definition: reactive_function.h:122
sol::lib::string
@ string
format.h
PJ::CreatedSeriesTime
Definition: reactive_function.h:54
PJ::CreatedSeriesBase::at
std::pair< double, double > at(unsigned i) const
Definition: reactive_function.cpp:216
PJ::CreatedSeriesXY
Definition: reactive_function.h:59
PJ::ReactiveLuaFunction::init
void init()
Definition: reactive_function.cpp:14
PJ::PlotDataBase::Point::x
TypeX x
Definition: plotdatabase.h:128
sol::in_place
constexpr std::in_place_t in_place
Definition: sol.hpp:4612
PJ::ReactiveLuaFunction::_created_timeseries
sol::usertype< CreatedSeriesTime > _created_timeseries
Definition: reactive_function.h:126
mqtt_test_proto.x
x
Definition: mqtt_test_proto.py:34
PJ::ReactiveLuaFunction::name
const char * name() const override
Name of the plugin type, NOT the particular instance.
Definition: reactive_function.h:72
PJ::ReactiveLuaFunction::prepareLua
void prepareLua()
Definition: reactive_function.cpp:99
sol::state_view::safe_script
protected_function_result safe_script(lua_Reader reader, void *data, Fx &&on_error, const std::string &chunkname=detail::default_chunk_name(), load_mode mode=load_mode::any)
Definition: sol.hpp:27564
sol::state_view::script
protected_function_result script(const string_view &code, Fx &&on_error, const std::string &chunkname=detail::default_chunk_name(), load_mode mode=load_mode::any)
Definition: sol.hpp:27721
mqtt_test_proto.y
y
Definition: mqtt_test_proto.py:35
PJ::ReactiveLuaFunction::_timeseries_ref
sol::usertype< TimeseriesRef > _timeseries_ref
Definition: reactive_function.h:125
PJ::PlotDataBase::clear
virtual void clear()
Definition: plotdatabase.h:213
PJ::CreatedSeriesXY::CreatedSeriesXY
CreatedSeriesXY(PlotDataMapRef *data_map, const std::string &name)
Definition: reactive_function.cpp:242
sol::protected_function_result::valid
bool valid() const noexcept
Definition: sol.hpp:17288
PJ::ReactiveLuaFunction::calculate
void calculate() override
Definition: reactive_function.cpp:71
PJ::TimeseriesRef
Definition: reactive_function.h:20
PJ::PlotDataMapRef::numeric
TimeseriesMap numeric
Numerical timeseries.
Definition: plotdata.h:39
reactive_function.h
PJ::ReactiveLuaFunction::_tracker_value
double _tracker_value
Definition: reactive_function.h:115
PJ::CreatedSeriesBase::_plot_data
PJ::PlotDataXY * _plot_data
Definition: reactive_function.h:51
PJ::TimeseriesRef::TimeseriesRef
TimeseriesRef(PlotData *data)
Definition: reactive_function.cpp:171
backward::details::move
const T & move(const T &v)
Definition: backward.hpp:394
PJ::TimeseriesRef::_plot_data
PJ::PlotData * _plot_data
Definition: reactive_function.h:34
sol.hpp
sol::state_view::new_usertype
usertype< Class > new_usertype(Args &&... args)
Definition: sol.hpp:27983
PJ::CreatedSeriesBase::size
unsigned size() const
Definition: reactive_function.cpp:232
PJ::PlotDataMapRef::getOrCreateScatterXY
PlotDataXY & getOrCreateScatterXY(const std::string &name, PlotGroup::Ptr group={})
Definition: plotdata.cpp:69
sol::lib::table
@ table
sol::lua_nil
constexpr lua_nil_t lua_nil
Definition: sol.hpp:7314
PJ::ReactiveLuaFunction::setTimeTracker
void setTimeTracker(double time_tracker_value)
Definition: reactive_function.cpp:66
PJ::CreatedSeriesBase::push_back
void push_back(double x, double y)
Definition: reactive_function.cpp:227
sol::error
Definition: sol.hpp:4573
PJ::PlotDataMapRef::getOrCreateNumeric
PlotData & getOrCreateNumeric(const std::string &name, PlotGroup::Ptr group={})
Definition: plotdata.cpp:75
format
auto format(const text_style &ts, const S &format_str, const Args &... args) -> std::basic_string< Char >
Definition: color.h:543
PJ::TimeseriesBase::getIndexFromX
int getIndexFromX(double x) const
Definition: timeseries.h:106
PJ::TransformFunction::_data
PlotDataMapRef * _data
Definition: transform_function.h:80
sol::state_view::open_libraries
void open_libraries(Args &&... args)
Definition: sol.hpp:27320
PJ::PlotDataBase::size
virtual size_t size() const
Definition: plotdatabase.h:183
PJ::PlotDataBase::pushBack
virtual void pushBack(const Point &p)
Definition: plotdatabase.h:325
PJ::ReactiveLuaFunction::ReactiveLuaFunction
ReactiveLuaFunction(PlotDataMapRef *data_map, QString lua_global, QString lua_function, QString lua_library)
Definition: reactive_function.cpp:52
PJ::CreatedSeriesBase::clear
void clear()
Definition: reactive_function.cpp:222
PJ::ReactiveLuaFunction::xmlLoadState
bool xmlLoadState(const QDomElement &parent_element) override
Override this method to load the status of the plugin from XML.
Definition: reactive_function.cpp:94
PJ
Definition: dataloader_base.h:16
mqtt_test.data
dictionary data
Definition: mqtt_test.py:22
PJ::TransformFunction::plotData
PlotDataMapRef * plotData()
Definition: transform_function.h:57
PJ::PlotDataBase::at
const Point & at(size_t index) const
Definition: plotdatabase.h:193
sol::make_object
object make_object(lua_State *L_, T &&value)
Definition: sol.hpp:16916
PJ::PlotDataMapRef
Definition: plotdata.h:34
PJ::TimeseriesRef::set
void set(unsigned index, double x, double y)
Definition: reactive_function.cpp:181
PJ::TimeseriesRef::at
std::pair< double, double > at(unsigned i) const
Definition: reactive_function.cpp:175
PJ::CreatedSeriesBase
Definition: reactive_function.h:39
sol::lib::math
@ math
PJ::TimeseriesRef::size
unsigned size() const
Definition: reactive_function.cpp:193
PJ::TimeseriesRef::atTime
double atTime(double t) const
Definition: reactive_function.cpp:187
sol::basic_object
Definition: forward.hpp:1197


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:46