MADARA  3.2.3
KnowledgeBase.cpp
Go to the documentation of this file.
1 
4 #include "madara/logger/Logger.h"
6 #include "ContextGuard.h"
8 
9 
10 #include <sstream>
11 #include <iostream>
12 
13 namespace utility = madara::utility;
14 
16 
17 namespace madara { namespace knowledge {
18 
19 #ifndef _MADARA_NO_KARL_
20 
23  CompiledExpression & expression,
24  const WaitSettings & settings)
25 {
26  KnowledgeRecord result;
27 
28  if (context_)
29  {
40  EpochEnforcer enforcer (settings.poll_frequency, settings.max_wait_time);
41 
42  KnowledgeRecord last_value;
43 
44  // print the post statement at highest log level (cannot be masked)
45  if (settings.pre_print_statement != "")
47 
48  {
49  ContextGuard context_guard (*context_);
50 
52  "KnowledgeBase::wait:" \
53  " waiting on %s\n", expression.logic.c_str ());
54 
55  last_value = expression.expression.evaluate (settings);
56 
58  "KnowledgeBase::wait:" \
59  " completed first eval to get %s\n",
60  last_value.to_string ().c_str ());
61 
62  send_modifieds ("KnowledgeBase:wait", settings);
63  }
64 
65  // wait for expression to be true
66  while (!last_value.to_integer () &&
67  (settings.max_wait_time < 0 || !enforcer.is_done ()))
68  {
70  "KnowledgeBase::wait:" \
71  " last value didn't result in success\n");
72 
73  // Unlike the other wait statements, we allow for a time based wait.
74  // To do this, we allow a user to specify a
75  if (settings.poll_frequency > 0)
76  {
77  enforcer.sleep_until_next ();
78  }
79  else
80  context_->wait_for_change (true);
81 
82  // relock - basically we need to evaluate the tree again, and
83  // we can't have a bunch of people changing the variables as
84  // while we're evaluating the tree.
85  {
86  ContextGuard context_guard (*context_);
87 
89  "KnowledgeBase::wait:" \
90  " waiting on %s\n", expression.logic.c_str ());
91 
92  last_value = expression.expression.evaluate (settings);
93 
95  "KnowledgeBase::wait:" \
96  " completed eval to get %s\n",
97  last_value.to_string ().c_str ());
98 
99  send_modifieds ("KnowledgeBase:wait", settings);
100  }
101 
102  context_->signal ();
103  } // end while (!last)
104 
105  if (enforcer.is_done ())
106  {
108  "KnowledgeBase::wait:" \
109  " Evaluate did not succeed. Timeout occurred\n");
110  }
111 
112  // print the post statement at highest log level (cannot be masked)
113  if (settings.post_print_statement != "")
115 
116  return last_value;
117  }
118  else if (impl_.get ())
119  {
120  result = impl_->wait (expression, settings);
121  }
122 
123  return result;
124 }
125 #endif // _MADARA_NO_KARL_
126 
127 } }
This class encapsulates an entry in a KnowledgeBase.
double max_wait_time
Maximum time to wait for an expression to become true (in seconds)
Definition: WaitSettings.h:113
std::string pre_print_statement
Statement to print before evaluations.
Definition: EvalSettings.h:120
int send_modifieds(const std::string &prefix="KnowledgeBase::send_modifieds", const EvalSettings &settings=EvalSettings())
Sends all modified variables through the attached transports.
void signal(bool lock=true) const
Signals that this thread is done with the context.
void print(unsigned int level) const
Atomically prints all variables and values in the context.
std::string logic
the logic that was compiled
Compiled, optimized KaRL logic.
#define madara_logger_log(logger, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
std::string post_print_statement
Statement to print after evaluations.
Definition: EvalSettings.h:125
std::shared_ptr< KnowledgeBaseImpl > impl_
Pointer to actual implementation, i.e., the "bridge", which is reference counted to automate memory m...
bool is_done(void) const
Checks to see if max duration is finished.
madara::expression::ExpressionTree expression
the expression tree
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:23
ThreadSafeContext * context_
A knowledge base can also be a facade for another knowledge base.
madara::knowledge::KnowledgeRecord wait(const std::string &expression, const WaitSettings &settings=WaitSettings())
Waits for an expression to be non-zero.
Enforces a periodic epoch.
Definition: EpochEnforcer.h:17
double poll_frequency
Frequency to poll an expression for truth (in seconds)
Definition: WaitSettings.h:108
Integer to_integer(void) const
converts the value to an integer.
void wait_for_change(bool extra_release=false)
Wait for a change to happen to the context.
utility::EpochEnforcer< std::chrono::steady_clock > EpochEnforcer
Provides utility functions and classes for common tasks and needs.
Definition: IteratorImpl.h:14
Provides functions and classes for the distributed knowledge base.
Copyright (c) 2015 Carnegie Mellon University.
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
Encapsulates settings for a wait statement.
Definition: WaitSettings.h:23
madara::knowledge::KnowledgeRecord evaluate(const madara::knowledge::KnowledgeUpdateSettings &settings=knowledge::KnowledgeUpdateSettings())
Evaluates the expression tree.
void sleep_until_next(void)
Sleeps until the next epoch.
Definition: EpochEnforcer.h:92