analyzer.cpp
1 /***************************************************************************
2  Copyright (C) 2002-2015 Kentaro Kitagawa
3  kitagawa@phys.s.u-tokyo.ac.jp
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  You should have received a copy of the GNU Library General
11  Public License and a list of authors along with this program;
12  see the files COPYING and AUTHORS.
13 ***************************************************************************/
14 //---------------------------------------------------------------------------
15 #include "ui_graphform.h"
16 #include "graphwidget.h"
17 #include "analyzer.h"
18 #include "graph.h"
19 #include "driver.h"
20 #include "measure.h"
21 
22 #include <QStatusBar>
23 
24 //---------------------------------------------------------------------------
25 XScalarEntry::XScalarEntry(const char *name, bool runtime, const shared_ptr<XDriver> &driver,
26  const char *format)
27  : XNode(name, runtime),
28  m_driver(driver),
29  m_delta(create<XDoubleNode>("Delta", false)),
30  m_store(create<XBoolNode>("Store", false)),
31  m_value(create<XDoubleNode>("Value", true)),
32  m_storedValue(create<XDoubleNode>("StoredValue", true)) {
33 
34  m_delta->setFormat(format);
35  m_storedValue->setFormat(format);
36  m_value->setFormat(format);
37  trans( *store()) = false;
38 }
39 void
40 XScalarEntry::storeValue(Transaction &tr) {
41  tr[ *storedValue()] = (double)tr[ *value()];
42  tr[ *this].m_bTriggered = false;
43 }
44 
45 XString
47  return driver()->getLabel() + "-" + XNode::getLabel();
48 }
49 
50 void
51 XScalarEntry::value(Transaction &tr, double val) {
52  Snapshot &shot(tr);
53  if((shot[ *delta()] != 0) && (fabs(val - shot[ *storedValue()]) > shot[ *delta()])) {
54  tr[ *this].m_bTriggered = true;
55  }
56  tr[ *value()] = val;
57 }
58 
59 XValChart::XValChart(const char *name, bool runtime,
60  const shared_ptr<XScalarEntry> &entry)
61  : XNode(name, runtime),
62  m_entry(entry),
63  m_graph(create<XGraph>(name, false)),
64  m_graphForm(new FrmGraph(g_pFrmMain, Qt::Window)) {
65 
66  m_graphForm->m_graphwidget->setGraph(m_graph);
67 
68  m_graph->iterate_commit([=](Transaction &tr){
69  m_chart= m_graph->plots()->create<XXYPlot>(tr, entry->getLabel().c_str(), true, ref(tr), m_graph);
70  tr[ *m_graph->persistence()] = 0.0;
71  tr[ *m_chart->label()] = entry->getLabel();
72  const XNode::NodeList &axes_list( *tr.list(m_graph->axes()));
73  shared_ptr<XAxis> axisx = static_pointer_cast<XAxis>(axes_list.at(0));
74  shared_ptr<XAxis> axisy = static_pointer_cast<XAxis>(axes_list.at(1));
75 
76  tr[ *m_chart->axisX()] = axisx;
77  tr[ *m_chart->axisY()] = axisy;
78  tr[ *m_chart->maxCount()] = 600;
79  tr[ *axisx->length()] = 0.95 - tr[ *axisx->x()];
80  tr[ *axisy->length()] = 0.90 - tr[ *axisy->y()];
81  tr[ *axisx->label()] = "Time";
82  tr[ *axisx->ticLabelFormat()] = "TIME:%H:%M:%S";
83  tr[ *axisy->label()] = entry->getLabel();
84  tr[ *axisy->ticLabelFormat()] = entry->value()->format();
85  tr[ *axisx->minValue()].setUIEnabled(false);
86  tr[ *axisx->maxValue()].setUIEnabled(false);
87  tr[ *axisx->autoScale()].setUIEnabled(false);
88  tr[ *axisx->logScale()].setUIEnabled(false);
89  tr[ *m_graph->label()] = entry->getLabel();
90  });
91 
92  entry->driver()->iterate_commit([=](Transaction &tr){
93  m_lsnOnRecord = tr[ *entry->driver()].onRecord().connectWeakly(
94  shared_from_this(), &XValChart::onRecord);
95  });
96 }
97 void
98 XValChart::onRecord(const Snapshot &shot, XDriver *driver) {
99  double val;
100  val = shot[ *m_entry->value()];
101  XTime time = shot[ *driver].time();
102  iterate_commit([=](Transaction &tr){
103  if(time)
104  m_chart->addPoint(tr, time.sec() + time.usec() * 1e-6, val);
105  });
106 }
107 void
108 XValChart::showChart(void) {
109  m_graphForm->setWindowTitle(i18n("Chart - ") + getLabel() );
110  m_graphForm->showNormal();
111 }
112 
113 XChartList::XChartList(const char *name, bool runtime, const shared_ptr<XScalarEntryList> &entries)
114  : XAliasListNode<XValChart>(name, runtime),
115  m_entries(entries) {
116  entries->iterate_commit([=](Transaction &tr){
117  m_lsnOnCatchEntry = tr[ *entries].onCatch().connectWeakly(shared_from_this(), &XChartList::onCatchEntry);
118  m_lsnOnReleaseEntry = tr[ *entries].onRelease().connectWeakly(shared_from_this(), &XChartList::onReleaseEntry);
119  });
120 }
121 
122 void
123 XChartList::onCatchEntry(const Snapshot &shot, const XListNodeBase::Payload::CatchEvent &e) {
124  shared_ptr<XScalarEntry> entry = static_pointer_cast<XScalarEntry>(e.caught);
125  create<XValChart>(entry->getLabel().c_str(), true, entry);
126 }
127 void
128 XChartList::onReleaseEntry(const Snapshot &shot, const XListNodeBase::Payload::ReleaseEvent &e) {
129  shared_ptr<XScalarEntry> entry = dynamic_pointer_cast<XScalarEntry>(e.released);
130  iterate_commit_while([=](Transaction &tr)->bool{
131  shared_ptr<XValChart> valchart;
132  if(tr.size()) {
133  const XNode::NodeList &list( *tr.list());
134  for(auto it = list.begin(); it != list.end(); it++) {
135  auto chart = dynamic_pointer_cast<XValChart>( *it);
136  if(chart->entry() == entry) valchart = chart;
137  }
138  }
139  if( !valchart)
140  return false;
141  if( !release(tr, valchart))
142  return true;//will fail.
143  return true;
144  });
145 }
146 
147 XValGraph::XValGraph(const char *name, bool runtime,
148  Transaction &tr_entries, const shared_ptr<XScalarEntryList> &entries)
149  : XNode(name, runtime),
150  m_graphForm(),
151  m_axisX(create<tAxis>("AxisX", false, ref(tr_entries), entries)),
152  m_axisY1(create<tAxis>("AxisY1", false, ref(tr_entries), entries)),
153  m_axisZ(create<tAxis>("AxisZ", false, ref(tr_entries), entries)),
154  m_entries(entries) {
155  iterate_commit([=](Transaction &tr){
156  m_lsnAxisChanged = tr[ *axisX()].onValueChanged().connectWeakly(
157  shared_from_this(), &XValGraph::onAxisChanged,
158  XListener::FLAG_MAIN_THREAD_CALL | XListener::FLAG_AVOID_DUP);
159  tr[ *axisY1()].onValueChanged().connect(m_lsnAxisChanged);
160  tr[ *axisZ()].onValueChanged().connect(m_lsnAxisChanged);
161  });
162 }
163 void
164 XValGraph::onAxisChanged(const Snapshot &shot, XValueNodeBase *) {
165  shared_ptr<XScalarEntry> entryx;
166  shared_ptr<XScalarEntry> entryy1;
167  shared_ptr<XScalarEntry> entryz;
168  shared_ptr<XGraph> graph;
169  iterate_commit([=, &entryx, &entryy1, &entryz, &graph](Transaction &tr){
170  const Snapshot &shot_this(tr);
171  entryx = shot_this[ *axisX()];
172  entryy1 = shot_this[ *axisY1()];
173  entryz = shot_this[ *axisZ()];
174 
175  if(tr[ *this].m_graph) release(tr, tr[ *this].m_graph);
176  tr[ *this].m_graph = create<XGraph>(tr, getName().c_str(), false);
177  graph = tr[ *this].m_graph;
178 
179  if( !entryx || !entryy1) return;
180 
181  tr[ *this].m_livePlot =
182  graph->plots()->create<XXYPlot>(tr,
183  (graph->getName() + "-Live").c_str(), false, ref(tr), graph);
184  tr[ *shot_this[ *this].m_livePlot->label()] = graph->getLabel() + " Live";
185  tr[ *this].m_storePlot =
186  graph->plots()->create<XXYPlot>(tr,
187  (graph->getName() + "-Stored").c_str(), false, ref(tr), graph);
188  tr[ *shot_this[ *this].m_storePlot->label()] = graph->getLabel() + " Stored";
189 
190  const XNode::NodeList &axes_list( *tr.list(graph->axes()));
191  auto axisx = static_pointer_cast<XAxis>(axes_list.at(0));
192  auto axisy = static_pointer_cast<XAxis>(axes_list.at(1));
193 
194  tr[ *axisx->ticLabelFormat()] = entryx->value()->format();
195  tr[ *axisy->ticLabelFormat()] = entryy1->value()->format();
196  tr[ *shot_this[ *this].m_livePlot->axisX()] = axisx;
197  tr[ *shot_this[ *this].m_livePlot->axisY()] = axisy;
198  tr[ *shot_this[ *this].m_storePlot->axisX()] = axisx;
199  tr[ *shot_this[ *this].m_storePlot->axisY()] = axisy;
200 
201  tr[ *axisx->length()] = 0.95 - shot_this[ *axisx->x()];
202  tr[ *axisy->length()] = 0.90 - shot_this[ *axisy->y()];
203  if(entryz) {
204  shared_ptr<XAxis> axisz = graph->axes()->create<XAxis>(
205  tr, "Z Axis", false, XAxis::DirAxisZ, false, ref(tr), graph);
206  tr[ *axisz->ticLabelFormat()] = entryz->value()->format();
207  tr[ *shot_this[ *this].m_livePlot->axisZ()] = axisz;
208  tr[ *shot_this[ *this].m_storePlot->axisZ()] = axisz;
209  // axisz->label()] = "Z Axis";
210  tr[ *axisz->label()] = entryz->getLabel();
211  }
212 
213  tr[ *shot_this[ *this].m_storePlot->pointColor()] = clGreen;
214  tr[ *shot_this[ *this].m_storePlot->lineColor()] = clGreen;
215  tr[ *shot_this[ *this].m_storePlot->barColor()] = clGreen;
216  tr[ *shot_this[ *this].m_storePlot->displayMajorGrid()] = false;
217  tr[ *shot_this[ *this].m_livePlot->maxCount()] = 10000;
218  tr[ *shot_this[ *this].m_storePlot->maxCount()] = 10000;
219  tr[ *axisx->label()] = entryx->getLabel();
220  tr[ *axisy->label()] = entryy1->getLabel();
221  tr[ *graph->label()] = getLabel();
222  });
223  m_graphForm.reset(new FrmGraph(g_pFrmMain, Qt::Window));
224  m_graphForm->m_graphwidget->setGraph(graph);
225  m_entries.lock()->iterate_commit([=](Transaction &tr){
226  if( !tr.isUpperOf( *entryx)) return;
227  if( !tr.isUpperOf( *entryy1)) return;
228  if(entryz && !tr.isUpperOf( *entryz)) return;
229  m_lsnLiveChanged = tr[ *entryx->value()].onValueChanged().connectWeakly(
230  shared_from_this(), &XValGraph::onLiveChanged);
231  tr[ *entryy1->value()].onValueChanged().connect(m_lsnLiveChanged);
232  if(entryz) tr[ *entryz->value()].onValueChanged().connect(m_lsnLiveChanged);
233 
234  m_lsnStoreChanged = tr[ *entryx->storedValue()].onValueChanged().connectWeakly(
235  shared_from_this(), &XValGraph::onStoreChanged);
236  tr[ *entryy1->storedValue()].onValueChanged().connect(m_lsnStoreChanged);
237  if(entryz) tr[ *entryz->storedValue()].onValueChanged().connect(m_lsnStoreChanged);
238  });
239 
240  showGraph();
241 }
242 
243 void
244 XValGraph::clearAllPoints() {
245  iterate_commit([=](Transaction &tr){
246  if( !tr[ *this].m_graph) return;
247  tr[ *this].m_storePlot->clearAllPoints(tr);
248  tr[ *this].m_livePlot->clearAllPoints(tr);
249  });
250 }
251 void
252 XValGraph::onLiveChanged(const Snapshot &shot, XValueNodeBase *) {
253  Snapshot shot_this( *this);
254  shared_ptr<XScalarEntry> entryx = shot_this[ *axisX()];
255  shared_ptr<XScalarEntry> entryy1 = shot_this[ *axisY1()];
256  shared_ptr<XScalarEntry> entryz = shot_this[ *axisZ()];
257  if( !entryx || !entryy1) return;
258  Snapshot shot_entries( *m_entries.lock());
259  if( !shot_entries.isUpperOf( *entryx)) return;
260  if( !shot_entries.isUpperOf( *entryy1)) return;
261  if(entryz && !shot_entries.isUpperOf( *entryz)) return;
262 
263  double x, y, z = 0.0;
264  x = shot_entries[ *entryx->value()];
265  y = shot_entries[ *entryy1->value()];
266  if(entryz) z = shot_entries[ *entryz->value()];
267 
268  iterate_commit([=](Transaction &tr){
269  tr[ *this].m_livePlot->addPoint(tr, x, y, z);
270  });
271 }
272 void
273 XValGraph::onStoreChanged(const Snapshot &shot, XValueNodeBase *) {
274  Snapshot shot_this( *this);
275  shared_ptr<XScalarEntry> entryx = shot_this[ *axisX()];
276  shared_ptr<XScalarEntry> entryy1 = shot_this[ *axisY1()];
277  shared_ptr<XScalarEntry> entryz = shot_this[ *axisZ()];
278  if( !entryx || !entryy1) return;
279  Snapshot shot_entries( *m_entries.lock());
280  if( !shot_entries.isUpperOf( *entryx)) return;
281  if( !shot_entries.isUpperOf( *entryy1)) return;
282  if(entryz && !shot_entries.isUpperOf( *entryz)) return;
283 
284  double x, y, z = 0.0;
285  x = shot_entries[ *entryx->storedValue()];
286  y = shot_entries[ *entryy1->storedValue()];
287  if(entryz) z = shot_entries[ *entryz->storedValue()];
288 
289  iterate_commit([=](Transaction &tr){
290  tr[ *this].m_storePlot->addPoint(tr, x, y, z);
291  });
292 }
293 void
294 XValGraph::showGraph() {
295  if(m_graphForm) {
296  m_graphForm->setWindowTitle(i18n("Graph - ") + getLabel() );
297  m_graphForm->showNormal();
298  }
299 }
300 
301 XGraphList::XGraphList(const char *name, bool runtime, const shared_ptr<XScalarEntryList> &entries)
302  : XCustomTypeListNode<XValGraph>(name, runtime),
303  m_entries(entries) {
304 }
305 
306 shared_ptr<XNode>
308  shared_ptr<XValGraph> x;
309  m_entries->iterate_commit([=, &x](Transaction &tr){
310  x = create<XValGraph>(name.c_str(), false, ref(tr), m_entries);
311  });
312  return x;
313 }

Generated for KAME4 by  doxygen 1.8.3