ConditionRoot.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarX::Core
19 * @author Kai Welke (welke _at_ kit _dot_ edu)
20 * @date 2012
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #include "LiteralImpl.h"
26 
30 
31 #include <cstdarg>
32 #include <list>
33 
34 namespace armarx
35 {
36  void ConditionRoot::update(const Ice::Current& c)
37  {
38  std::unique_lock lock(updateMutex);
39 
40  if (childs.size() != 1)
41  {
42  return;
43  }
44 
45  // empty terms is always evaluated to true
46  if ((!fired || !onlyFireOnce) && (!childs.at(0) || childs.at(0)->getValue()))
47  {
48  value = true;
49  ARMARX_DEBUG_S << "Condition fulfilled, sending event " << event->eventName << std::endl;
50  try
51  {
52  event->properties.clear();
53  for (const DatafieldRefBasePtr& ref : refs)
54  {
55  DatafieldRefPtr r = DatafieldRefPtr::dynamicCast(ref);
56  const Variant& data = *r->getDataField();
57  event->properties[r->getDataFieldIdentifier()->getIdentifierStr()] = new SingleVariant(data);
58  }
59  //needs to be async otherwise it can lead to deadlock if the listener uses a datafield of an observer
60  listener->begin_reportEvent(event);
61  }
62  catch (std::exception& e)
63  {
64  ARMARX_ERROR_S << "Could not report event: " << e.what();
65  }
66 
67  fired = true;
68  }
69  }
70 
71  void ConditionRoot::updateWithData(const Ice::Current& c)
72  {
73  std::unique_lock lock(updateMutex);
74 
75  if (childs.size() != 1)
76  {
77  return;
78  }
79 
80  // empty terms is always evaluated to true
81  if ((!fired || !onlyFireOnce) && (!childs.at(0) || childs.at(0)->getValue()))
82  {
83  value = true;
84  ARMARX_VERBOSE_S << "Condition fulfilled, sending event with data " << event->eventName << std::endl;
85  try
86  {
87  StringVariantBaseMap values = childs.at(0)->getDatafields();
88  event->properties.clear();
89  ARMARX_INFO_S << VAROUT(event->eventName);
90  for (auto& v : values)
91  {
92  const Variant& data = *VariantPtr::dynamicCast(v.second);
93  event->properties[v.first] = new SingleVariant(data);
94  ARMARX_INFO_S << v.first << " cond val: " << VariantPtr::dynamicCast(v.second)->getOutputValueOnly();
95  }
96  for (const DatafieldRefBasePtr& ref : refs)
97  {
98  DatafieldRefPtr r = DatafieldRefPtr::dynamicCast(ref);
99  auto id = r->getDataFieldIdentifier()->getIdentifierStr();
100  if (event->properties.count(id) > 0)
101  {
102  continue;
103  }
104  const Variant& data = *r->getDataField();
105  event->properties[id] = new SingleVariant(data);
106  ARMARX_INFO_S << r->getDataFieldIdentifier()->getIdentifierStr() << " val: " << r->getDataField()->getOutputValueOnly();
107  }
108 
109  //needs to be async otherwise it can lead to deadlock if the listener uses a datafield of an observer
110  listener->begin_reportEvent(event);
111  }
112  catch (std::exception& e)
113  {
114  ARMARX_ERROR_S << "Could not report event: " << e.what();
115  }
116 
117  fired = true;
118  }
119  }
120 
121  void ConditionRoot::output(std::ostream& out) const
122  {
123  out << "Event: " << event->eventName << " - ";
124 
125  if (childs.size() > 0)
126  {
127  out << "Expression: " << TermImplPtr::dynamicCast(childs.at(0));
128  }
129  }
130 
132  {
133  type = eConditionRoot;
134  }
135 
136  ConditionRoot::ConditionRoot(const EventListenerInterfacePrx& listener, const EventBasePtr& event, const std::string& description, bool onlyFireOnce, const DatafieldRefList& refs)
137  {
138  type = eConditionRoot;
139  this->listener = listener->ice_collocationOptimized(false); // events are reported with begin_reportEvent -> this does not work if the event receiver is in the same process and collocation is used
140  this->event = event;
141  this->description = description;
142  this->onlyFireOnce = onlyFireOnce;
143  this->refs = refs;
144  }
145 
146  std::vector<LiteralImplPtr> ConditionRoot::ExtractLiterals(const TermImplBasePtr& expression)
147  {
148  std::list<TermImplPtr> terms;
149  std::vector<LiteralImplPtr> literals;
150 
151  terms.push_back(TermImplPtr::dynamicCast(expression));
152 
153  while (terms.size() != 0)
154  {
155  // retrieve front of list
156  TermImplPtr term = terms.front();
157  terms.pop_front();
158 
159  // test type
160  if (term->getType() == eLiteral)
161  {
162  literals.push_back(LiteralImplPtr::dynamicCast(term));
163  }
164  else
165  {
166  // add childs to list
167  TermImplSequence childs = term->getChilds();
168  TermImplSequence::iterator iterChilds = childs.begin();
169 
170  while (iterChilds != childs.end())
171  {
172  terms.push_back(TermImplPtr::dynamicCast(*iterChilds));
173  iterChilds++;
174  }
175  }
176 
177  }
178 
179 
180 
181  return literals;
182  }
183 }
LiteralImpl.h
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:111
armarx::ConditionRoot::ConditionRoot
ConditionRoot()
Creates an empty ConditionRoot.
Definition: ConditionRoot.cpp:131
VariantContainer.h
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::ConditionRoot::updateWithData
void updateWithData(const Ice::Current &c=Ice::emptyCurrent) override
Definition: ConditionRoot.cpp:71
armarx::ConditionRoot::update
void update(const Ice::Current &c=Ice::emptyCurrent) override
Update state f the ConditionRoot object based on the child.
Definition: ConditionRoot.cpp:36
IceInternal::Handle< DatafieldRef >
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
ARMARX_DEBUG_S
#define ARMARX_DEBUG_S
Definition: Logging.h:198
armarx::SingleVariant
The SingleVariant class is required to store single Variant instances in VariantContainer subclasses.
Definition: VariantContainer.h:109
ARMARX_ERROR_S
#define ARMARX_ERROR_S
Definition: Logging.h:209
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
ConditionRoot.h
armarx::ConditionRoot::output
void output(std::ostream &out) const override
output to stream.
Definition: ConditionRoot.cpp:121
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
ARMARX_VERBOSE_S
#define ARMARX_VERBOSE_S
Definition: Logging.h:200
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:195
Logging.h
armarx::ConditionRoot::ExtractLiterals
static std::vector< LiteralImplPtr > ExtractLiterals(const TermImplBasePtr &expression)
Definition: ConditionRoot.cpp:146
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28