OpenCog Framework
Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
|
#include <SchemeEval.h>
Public Member Functions | |
SchemeEval (AtomSpace *=NULL) | |
~SchemeEval () | |
void | begin_eval () |
void | eval_expr (const std::string &) |
std::string | poll_result () |
std::string | eval (const std::string &expr) |
std::string | eval (const std::stringstream &ss) |
Handle | eval_h (const std::string &) |
Handle | eval_h (const std::stringstream &ss) |
TruthValuePtr | eval_tv (const std::string &) |
TruthValuePtr | eval_tv (const std::stringstream &ss) |
Handle | apply (const std::string &func, Handle varargs) |
TruthValuePtr | apply_tv (const std::string &func, Handle varargs) |
bool | recursing (void) |
![]() | |
GenericEval (void) | |
virtual | ~GenericEval () |
virtual bool | input_pending () |
virtual void | clear_pending () |
virtual bool | eval_error (void) |
Static Public Member Functions | |
static void | init_scheme (void) |
static void | set_scheme_as (AtomSpace *) |
static SchemeEval * | get_evaluator (AtomSpace *=NULL) |
Private Member Functions | |
void | init (void) |
void | per_thread_init (void) |
void | thread_lock (void) |
void | thread_unlock (void) |
void | finish (void) |
void | do_eval (const std::string &) |
std::string | do_poll_result () |
std::string | poll_port () |
void | capture_port () |
void | redirect_output () |
void | restore_output () |
void | drain_output () |
Discard all chars in the outport. More... | |
SCM | do_scm_eval (SCM, SCM(*)(void *)) |
Handle | do_apply (const std::string &func, Handle &varargs) |
SCM | do_apply_scm (const std::string &func, Handle &varargs) |
void | set_error_string (SCM) |
void | set_captured_stack (SCM) |
SCM | preunwind_handler (SCM, SCM) |
SCM | catch_handler (SCM, SCM) |
Static Private Member Functions | |
static void * | c_wrap_init (void *) |
static void * | c_wrap_finish (void *) |
static void * | c_wrap_eval (void *) |
static void * | c_wrap_poll (void *) |
static void * | c_wrap_eval_h (void *) |
static void * | c_wrap_eval_tv (void *) |
static void * | c_wrap_apply (void *) |
static void * | c_wrap_apply_tv (void *) |
static SCM | preunwind_handler_wrapper (void *, SCM, SCM) |
static SCM | catch_handler_wrapper (void *, SCM, SCM) |
static std::string | prt (SCM) |
static void * | c_wrap_set_atomspace (void *) |
Private Attributes | |
const std::string * | pexpr |
std::string | answer |
SCM | _rc |
bool | _eval_done |
bool | _poll_done |
std::mutex | _poll_mtx |
std::condition_variable | _wait_done |
SCM | _pipe |
int | _pipeno |
SCM | _outport |
SCM | _saved_outport |
bool | _in_shell |
bool | _in_server |
int | _in_redirect |
Handle | hargs |
TruthValuePtr | tvp |
SCM | error_string |
std::string | error_msg |
SCM | captured_stack |
AtomSpace * | atomspace |
int | _gc_ctr |
bool | _in_eval |
Additional Inherited Members | |
![]() | |
std::string | _input_line |
bool | _pending_input |
bool | _caught_error |
Definition at line 63 of file SchemeEval.h.
SchemeEval::SchemeEval | ( | AtomSpace * | as = NULL | ) |
Definition at line 304 of file SchemeEval.cc.
References atomspace, c_wrap_init(), init_only_once(), thread_lock(), and thread_unlock().
SchemeEval::~SchemeEval | ( | ) |
Definition at line 336 of file SchemeEval.cc.
References c_wrap_finish().
apply – apply named function func to arguments in ListLink It is assumed that varargs is a ListLink, containing a list of atom handles. This list is unpacked, and then the function func is applied to them. If the function returns an atom handle, then this is returned. If the function does not return a handle, or if an error ocurred during evaluation, then a C++ exception is thrown.
Definition at line 963 of file SchemeEval.cc.
References _in_eval, c_wrap_apply(), do_apply(), error_msg, opencog::GenericEval::eval_error(), hargs, pexpr, thread_lock(), and thread_unlock().
TruthValuePtr SchemeEval::apply_tv | ( | const std::string & | func, |
Handle | varargs | ||
) |
apply_tv – apply named function func to arguments in ListLink. Return an OpenCog TruthValuePtr. It is assumed that varargs is a ListLink, containing a list of atom handles. This list is unpacked, and then the fuction func is applied to them. The function is presumed to return pointer to a TruthValue object.
Definition at line 1053 of file SchemeEval.cc.
References _in_eval, c_wrap_apply_tv(), do_apply_scm(), error_msg, opencog::GenericEval::eval_error(), hargs, opencog::TruthValue::NULL_TV(), pexpr, thread_lock(), thread_unlock(), opencog::SchemeSmob::to_tv(), and tvp.
|
virtual |
begin_eval() must be called in the same thread as poll_result() an it must be called before eval_expr(). The eval_expr() method may be called in the same thread, or a different one. The poll_result() method can be called at any time after begin_eval(). The poll_result() method might block, until results are available. It must return the empty string when there are no more results.
Implements opencog::GenericEval.
Definition at line 625 of file SchemeEval.cc.
References _eval_done, and _poll_done.
|
staticprivate |
Definition at line 991 of file SchemeEval.cc.
References hargs.
|
staticprivate |
Definition at line 1088 of file SchemeEval.cc.
References do_apply_scm(), and opencog::SchemeSmob::to_tv().
|
staticprivate |
|
staticprivate |
Definition at line 890 of file SchemeEval.cc.
References do_scm_eval(), recast_scm_eval_string(), and opencog::SchemeSmob::scm_to_handle().
|
staticprivate |
Definition at line 940 of file SchemeEval.cc.
References do_scm_eval(), recast_scm_eval_string(), and opencog::SchemeSmob::to_tv().
|
staticprivate |
Definition at line 180 of file SchemeEval.cc.
References finish().
|
staticprivate |
Definition at line 150 of file SchemeEval.cc.
References init().
|
staticprivate |
Definition at line 516 of file SchemeEval.cc.
References answer.
|
staticprivate |
Definition at line 1180 of file SchemeEval.cc.
References opencog::SchemeSmob::ss_set_env_as().
|
private |
When the user is using the guile shell from within the cogserver, We need to capture the output port. It is used by the cogserver shell to provide an async I/O mechanism, so that long-running scheme expressions will have the output captured and displayed as they run, instead of getting it all saved up and only displayed at the very end. But if we are not running in the cogserver, non of this is needed.
Definition at line 78 of file SchemeEval.cc.
References _in_redirect, _in_server, _outport, _pipe, _pipeno, and init_mtx.
|
private |
Definition at line 392 of file SchemeEval.cc.
References opencog::GenericEval::_caught_error, opencog::GenericEval::_pending_input, captured_stack, set_captured_stack(), and set_error_string().
|
staticprivate |
Definition at line 377 of file SchemeEval.cc.
References catch_handler().
do_apply – apply named function func to arguments in ListLink It is assumed that varargs is a ListLink, containing a list of atom handles. This list is unpacked, and then the fuction func is applied to them. If the function returns an atom handle, then this is returned.
Definition at line 1005 of file SchemeEval.cc.
References do_apply_scm(), and opencog::SchemeSmob::scm_to_handle().
|
private |
do_apply_scm – apply named function func to arguments in ListLink It is assumed that varargs is a ListLink, containing a list of atom handles. This list is unpacked, and then the fuction func is applied to them. The SCM value returned by the function is returned.
Definition at line 1025 of file SchemeEval.cc.
References atomspace, do_scm_eval(), opencog::AtomSpace::get_outgoing(), opencog::SchemeSmob::handle_to_scm(), and thunk_scm_eval().
|
private |
do_eval – evaluate a scheme expression string. This implements the working guts of the shell-friendly evaluator.
This method must be called in guile mode, in order for garbage collection, etc. to work correctly!
Definition at line 583 of file SchemeEval.cc.
References opencog::GenericEval::_caught_error, _eval_done, _gc_ctr, opencog::GenericEval::_input_line, opencog::GenericEval::_pending_input, _rc, _wait_done, atomspace, catch_handler_wrapper(), do_gc(), error_msg, per_thread_init(), preunwind_handler_wrapper(), redirect_output(), restore_output(), set_captured_stack(), opencog::SchemeSmob::ss_get_env_as(), and opencog::SchemeSmob::ss_set_env_as().
|
private |
Get output from evaluator, if any; block otherwise.
This method is meant to be called from a different thread than the one that the scheme evaluator is running in. It will try to get any pending output, and will return that. If there's no pending output, then it will block until (1) there's output, or (2) the evaluation has completed, in which case, it will return all remaining output generated by the eval.
The entire goal of this function is to allow the use of the guile 'display' can other stdio output calls from the interactive environment, even if the actual scheme eval is very long-running. Without this, what would happen is that all the output would get buffered up, until the eval() finished, and then all this output would get dumped. For anything that runs more than a few seconds this would be confusing and undesirable. So, this method allows some other thread to see if eval() is generating output, and, if it is, to print it to stdout.
Definition at line 675 of file SchemeEval.cc.
References opencog::GenericEval::_caught_error, _eval_done, opencog::GenericEval::_input_line, opencog::GenericEval::_pending_input, _poll_done, _poll_mtx, _rc, _wait_done, error_string, per_thread_init(), poll_port(), prt(), set_captured_stack(), and set_error_string().
|
private |
do_scm_eval – evaluate a scheme expression
Similar to do_eval(), with several important differences: 1) The argument must be an SCM expression. 2) The expression is evaluated by "evo" 3) No shell-friendly string and output management is performed. 4) Evaluation errors are logged to the log file.
This method must be called in guile mode, in order for garbage collection, etc. to work correctly!
Definition at line 764 of file SchemeEval.cc.
References opencog::GenericEval::_caught_error, _eval_done, _in_server, _in_shell, atomspace, catch_handler_wrapper(), drain_output(), error_msg, error_string, per_thread_init(), poll_port(), preunwind_handler_wrapper(), prt(), redirect_output(), restore_output(), set_captured_stack(), opencog::SchemeSmob::ss_get_env_as(), and opencog::SchemeSmob::ss_set_env_as().
|
private |
Discard all chars in the outport.
Definition at line 144 of file SchemeEval.cc.
References poll_port().
|
inline |
Definition at line 155 of file SchemeEval.h.
References begin_eval(), eval_expr(), and poll_result().
|
inline |
Definition at line 157 of file SchemeEval.h.
References eval().
|
virtual |
Evaluate a scheme expression.
This evaluator is tailored for being invoked by a shell, in several different ways:
1) It buffers up incomplete, line-by-line input, until there's been enough input received to evaluate without error. 2) It catches errors, and prints the catch in a reasonably nicely formatted way. 3) It converts any returned scheme expressions to a string, for easy printing. 4) It concatenates any data sent to the scheme output port (e.g. printed output from the scheme (display) function) to the returned string.
An "unforgiving" evaluator, with none of these amenities, can be found in eval_h(), below.
Implements opencog::GenericEval.
Definition at line 490 of file SchemeEval.cc.
References _in_eval, _in_shell, c_wrap_eval(), do_eval(), pexpr, thread_lock(), and thread_unlock().
Handle SchemeEval::eval_h | ( | const std::string & | expr | ) |
Evaluate a string containing a scheme expression, returning a Handle. If an evaluation error occurs, an exception is thrown, and the stack trace is logged to the log file.
Definition at line 858 of file SchemeEval.cc.
References _in_eval, c_wrap_eval_h(), do_scm_eval(), error_msg, opencog::GenericEval::eval_error(), hargs, pexpr, recast_scm_eval_string(), opencog::SchemeSmob::scm_to_handle(), thread_lock(), and thread_unlock().
|
inline |
Definition at line 162 of file SchemeEval.h.
References eval_h().
TruthValuePtr SchemeEval::eval_tv | ( | const std::string & | expr | ) |
Evaluate a string containing a scheme expression, returning a TV. If an evaluation error occurs, an exception is thrown, and the stack trace is logged to the log file.
Definition at line 905 of file SchemeEval.cc.
References _in_eval, c_wrap_eval_tv(), do_scm_eval(), error_msg, opencog::GenericEval::eval_error(), opencog::TruthValue::NULL_TV(), pexpr, recast_scm_eval_string(), thread_lock(), thread_unlock(), opencog::SchemeSmob::to_tv(), and tvp.
|
inline |
Definition at line 166 of file SchemeEval.h.
References eval_tv().
|
private |
Definition at line 157 of file SchemeEval.cc.
References _in_server, _outport, _pipe, _rc, captured_stack, error_string, and init_mtx.
|
static |
Return evaluator, for this thread and atomspace combination. If called with NULL, it will use the current atomspace for this thread.
Use thread-local storage (TLS) in order to avoid repeatedly creating and destroying the evaluator.
This will throw an error if used recursively. Viz, if the evaluator evaluates something that causes another evaluator to be needed for this thread (e.g. an ExecutionOutputLink), then this very same evaluator would be re-entered, corrupting its own internal state. If that happened, the result would be a hard-to-find & fix bug. So instead, we throw.
Definition at line 1135 of file SchemeEval.cc.
References atomspace, get_from_pool(), and return_to_pool().
|
private |
This init is called once for every time that this class is instantiated – i.e. it is a per-instance initializer.
Definition at line 41 of file SchemeEval.cc.
References _eval_done, _gc_ctr, _in_eval, _in_redirect, _in_server, _in_shell, _poll_done, _rc, captured_stack, error_string, opencog::PrimitiveEnviron::init(), opencog::SchemeSmob::init(), and pexpr.
|
static |
Definition at line 1197 of file SchemeEval.cc.
|
private |
Definition at line 322 of file SchemeEval.cc.
References guile_user_module, and thread_is_inited.
|
private |
Read one end of a pipe. The other end of the pipe is attached to guile's default output port. We use standard posix to read, as that will be faster than mucking with guile's one-char-at-a-time API. The below assumes that the pipe is non-blcoking; if it blocks, then things might get wonky. The below is also very simple; it does not check for any standard unix errors, closed pipes, etc. I think that simplicity is just fine, here.
Definition at line 638 of file SchemeEval.cc.
References _in_server, _pipeno, and BUFSZ.
|
virtual |
Implements opencog::GenericEval.
Definition at line 523 of file SchemeEval.cc.
References answer, and c_wrap_poll().
|
private |
Definition at line 383 of file SchemeEval.cc.
References set_captured_stack().
|
staticprivate |
Definition at line 371 of file SchemeEval.cc.
References preunwind_handler().
|
staticprivate |
Definition at line 343 of file SchemeEval.cc.
References opencog::SchemeSmob::cog_misc_tag, and opencog::SchemeSmob::misc_to_string().
|
inline |
Definition at line 173 of file SchemeEval.h.
References _in_eval.
|
private |
Use the async I/O mechanism, if we are in the cogserver.
Note, by the way, that Guile implements the current port as a fluid on each thread. So this save an restore gives us exactly the right per-thread semantics.
Definition at line 119 of file SchemeEval.cc.
References _in_redirect, _outport, _saved_outport, and capture_port().
|
private |
Definition at line 132 of file SchemeEval.cc.
References _in_redirect, and _saved_outport.
|
private |
Definition at line 189 of file SchemeEval.cc.
References captured_stack.
|
private |
Definition at line 197 of file SchemeEval.cc.
References error_string.
|
static |
Set the current atomspace for this thread. From this point on, all scheme code executing in this thread will use this atomspace (unless it is changed in the course of execution...)
Definition at line 1192 of file SchemeEval.cc.
References c_wrap_set_atomspace().
|
private |
This lock primitive allow nested locks within one thread, but prevents concurrent threads from running.
Definition at line 281 of file SchemeEval.cc.
References ser_key, and serialize_lock.
|
private |
Definition at line 292 of file SchemeEval.cc.
References ser_key, and serialize_lock.
|
private |
Definition at line 85 of file SchemeEval.h.
|
private |
Definition at line 133 of file SchemeEval.h.
|
private |
Definition at line 134 of file SchemeEval.h.
|
private |
Definition at line 98 of file SchemeEval.h.
|
private |
Definition at line 97 of file SchemeEval.h.
|
private |
Definition at line 96 of file SchemeEval.h.
|
private |
Definition at line 94 of file SchemeEval.h.
|
private |
Definition at line 90 of file SchemeEval.h.
|
private |
Definition at line 91 of file SchemeEval.h.
|
private |
Definition at line 86 of file SchemeEval.h.
|
private |
Definition at line 87 of file SchemeEval.h.
|
private |
Definition at line 84 of file SchemeEval.h.
|
private |
Definition at line 95 of file SchemeEval.h.
|
private |
Definition at line 88 of file SchemeEval.h.
|
private |
Definition at line 83 of file SchemeEval.h.
|
private |
Definition at line 132 of file SchemeEval.h.
|
private |
Definition at line 121 of file SchemeEval.h.
|
private |
Definition at line 119 of file SchemeEval.h.
|
private |
Definition at line 118 of file SchemeEval.h.
|
private |
Definition at line 112 of file SchemeEval.h.
|
private |
Definition at line 82 of file SchemeEval.h.
|
private |
Definition at line 113 of file SchemeEval.h.