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  {
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  };
113  _timeseries_ref["size"] = &TimeseriesRef::size;
114  _timeseries_ref["at"] = &TimeseriesRef::at;
115  _timeseries_ref["set"] = &TimeseriesRef::set;
116  _timeseries_ref["atTime"] = &TimeseriesRef::atTime;
117 
118  //---------------------------------------
120 
121  _created_timeseries["new"] = [this](sol::object name) {
122  if (name.is<std::string>() == false)
123  {
125  }
126  auto str_name = name.as<std::string>();
127  auto series = CreatedSeriesTime(plotData(), str_name);
128  series.clear();
129  _created_curves.push_back(str_name);
130  return sol::object(_lua_engine, sol::in_place, series);
131  };
132 
133  _created_timeseries["at"] = &CreatedSeriesTime::at;
134  _created_timeseries["size"] = &CreatedSeriesTime::size;
135  _created_timeseries["clear"] = &CreatedSeriesTime::clear;
136  _created_timeseries["push_back"] = &CreatedSeriesTime::push_back;
137 
138  //---------------------------------------
140 
141  _created_scatter["new"] = [this](sol::object name) {
142  if (name.is<std::string>() == false)
143  {
145  }
146  auto str_name = name.as<std::string>();
147  auto series = CreatedSeriesXY(plotData(), str_name);
148  series.clear();
149  _created_curves.push_back(str_name);
150  return sol::object(_lua_engine, sol::in_place, series);
151  };
152 
153  _created_scatter["at"] = &CreatedSeriesXY::at;
154  _created_scatter["size"] = &CreatedSeriesXY::size;
155  _created_scatter["clear"] = &CreatedSeriesXY::clear;
156  _created_scatter["push_back"] = &CreatedSeriesXY::push_back;
157 
158  //---------------------------------------
159  auto GetSeriesNames = [this]() {
160  std::vector<std::string> names;
161  for (const auto& it : plotData()->numeric)
162  {
163  names.push_back(it.first);
164  }
165  return names;
166  };
167  _lua_engine.set_function("GetSeriesNames", GetSeriesNames);
168 }
169 
171 {
172 }
173 
174 std::pair<double, double> TimeseriesRef::at(unsigned i) const
175 {
176  const auto& p = _plot_data->at(i);
177  return { p.x, p.y };
178 }
179 
180 void TimeseriesRef::set(unsigned index, double x, double y)
181 {
182  auto& p = _plot_data->at(index);
183  p = { x, y };
184 }
185 
186 double TimeseriesRef::atTime(double t) const
187 {
188  int i = _plot_data->getIndexFromX(t);
189  return _plot_data->at(i).y;
190 }
191 
192 unsigned TimeseriesRef::size() const
193 {
194  return _plot_data->size();
195 }
196 
197 CreatedSeriesBase::CreatedSeriesBase(PlotDataMapRef* data_map, const std::string& name,
198  bool timeseries)
199 {
200  if (timeseries)
201  {
202  _plot_data = &(data_map->getOrCreateNumeric(name));
203  }
204  else
205  {
206  _plot_data = &(data_map->getOrCreateScatterXY(name));
207  }
208 }
209 
210 std::pair<double, double> CreatedSeriesBase::at(unsigned i) const
211 {
212  const auto& p = _plot_data->at(i);
213  return { p.x, p.y };
214 }
215 
217 {
218  _plot_data->clear();
219 }
220 
221 void CreatedSeriesBase::push_back(double x, double y)
222 {
223  _plot_data->pushBack({ x, y });
224 }
225 
226 unsigned CreatedSeriesBase::size() const
227 {
228  return _plot_data->size();
229 }
230 
231 CreatedSeriesTime::CreatedSeriesTime(PlotDataMapRef* data_map, const std::string& name)
232  : CreatedSeriesBase(data_map, name, true)
233 {
234 }
235 
236 CreatedSeriesXY::CreatedSeriesXY(PlotDataMapRef* data_map, const std::string& name)
237  : CreatedSeriesBase(data_map, name, false)
238 {
239 }
240 
241 } // namespace PJ
PlotData & getOrCreateNumeric(const std::string &name, PlotGroup::Ptr group={})
Definition: plotdata.cpp:75
constexpr std::in_place_t in_place
Definition: sol.hpp:4612
sol::usertype< TimeseriesRef > _timeseries_ref
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
PJ::PlotData * _plot_data
basic_object< reference > object
Definition: forward.hpp:1209
sol::protected_function _lua_function
int getIndexFromX(double x) const
Definition: timeseries.h:107
bool xmlSaveState(QDomDocument &doc, QDomElement &parent_element) const override
Override this method to save the status of the plugin to XML.
CreatedSeriesBase(PlotDataMapRef *data_map, const std::string &name, bool timeseries)
object make_object(lua_State *L_, T &&value)
Definition: sol.hpp:16916
bool valid() const noexcept
Definition: sol.hpp:17288
double atTime(double t) const
CreatedSeriesTime(PlotDataMapRef *data_map, const std::string &name)
void set(unsigned index, double x, double y)
TimeseriesMap numeric
Numerical timeseries.
Definition: plotdata.h:38
virtual size_t size() const
Definition: plotdatabase.h:182
std::pair< double, double > at(unsigned i) const
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
PlotDataXY & getOrCreateScatterXY(const std::string &name, PlotGroup::Ptr group={})
Definition: plotdata.cpp:69
std::vector< std::string > _created_curves
PlotDataMapRef * _data
ReactiveLuaFunction(PlotDataMapRef *data_map, QString lua_global, QString lua_function, QString lua_library)
unsigned size() const
constexpr lua_nil_t lua_nil
Definition: sol.hpp:7314
sol::usertype< CreatedSeriesXY > _created_scatter
CreatedSeriesXY(PlotDataMapRef *data_map, const std::string &name)
bool xmlLoadState(const QDomElement &parent_element) override
Override this method to load the status of the plugin from XML.
const T & move(const T &v)
Definition: backward.hpp:394
void pushBack(const Point &p) override
Definition: timeseries.h:61
const Point & at(size_t index) const
Definition: plotdatabase.h:192
usertype< Class > new_usertype(Args &&... args)
Definition: sol.hpp:27983
const char * name() const override
Name of the plugin type, NOT the particular instance.
void push_back(double x, double y)
virtual const char * what() const noexcept override
Definition: sol.hpp:4593
PlotDataMapRef * plotData()
void setTimeTracker(double time_tracker_value)
state_view & set_function(Key &&key, Args &&... args)
Definition: sol.hpp:28015
TimeseriesRef(PlotData *data)
void open_libraries(Args &&... args)
Definition: sol.hpp:27320
virtual void clear()
Definition: plotdatabase.h:212
std::pair< double, double > at(unsigned i) const
sol::usertype< CreatedSeriesTime > _created_timeseries
Definition: format.h:895
std::basic_string< Char > format(const text_style &ts, const S &format_str, const Args &... args)
Definition: color.h:583


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Jun 19 2023 03:01:39