OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
opencog::SchemeEval Class Reference

#include <SchemeEval.h>

+ Inheritance diagram for opencog::SchemeEval:
+ Collaboration diagram for opencog::SchemeEval:

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)
 
- Public Member Functions inherited from opencog::GenericEval
 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 SchemeEvalget_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
 
AtomSpaceatomspace
 
int _gc_ctr
 
bool _in_eval
 

Additional Inherited Members

- Protected Attributes inherited from opencog::GenericEval
std::string _input_line
 
bool _pending_input
 
bool _caught_error
 

Detailed Description

Definition at line 63 of file SchemeEval.h.

Constructor & Destructor Documentation

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().

Member Function Documentation

Handle SchemeEval::apply ( const std::string &  func,
Handle  varargs 
)

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().

+ Here is the caller graph for this function:

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.

+ Here is the caller graph for this function:

void SchemeEval::begin_eval ( )
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.

+ Here is the caller graph for this function:

void * SchemeEval::c_wrap_apply ( void *  p)
staticprivate

Definition at line 991 of file SchemeEval.cc.

References hargs.

+ Here is the caller graph for this function:

void * SchemeEval::c_wrap_apply_tv ( void *  p)
staticprivate

Definition at line 1088 of file SchemeEval.cc.

References do_apply_scm(), and opencog::SchemeSmob::to_tv().

+ Here is the caller graph for this function:

void * SchemeEval::c_wrap_eval ( void *  p)
staticprivate

Definition at line 529 of file SchemeEval.cc.

+ Here is the caller graph for this function:

void * SchemeEval::c_wrap_eval_h ( void *  p)
staticprivate

Definition at line 890 of file SchemeEval.cc.

References do_scm_eval(), recast_scm_eval_string(), and opencog::SchemeSmob::scm_to_handle().

+ Here is the caller graph for this function:

void * SchemeEval::c_wrap_eval_tv ( void *  p)
staticprivate

Definition at line 940 of file SchemeEval.cc.

References do_scm_eval(), recast_scm_eval_string(), and opencog::SchemeSmob::to_tv().

+ Here is the caller graph for this function:

void * SchemeEval::c_wrap_finish ( void *  p)
staticprivate

Definition at line 180 of file SchemeEval.cc.

References finish().

+ Here is the caller graph for this function:

void * SchemeEval::c_wrap_init ( void *  p)
staticprivate

Definition at line 150 of file SchemeEval.cc.

References init().

+ Here is the caller graph for this function:

void * SchemeEval::c_wrap_poll ( void *  p)
staticprivate

Definition at line 516 of file SchemeEval.cc.

References answer.

+ Here is the caller graph for this function:

void * SchemeEval::c_wrap_set_atomspace ( void *  vas)
staticprivate

Definition at line 1180 of file SchemeEval.cc.

References opencog::SchemeSmob::ss_set_env_as().

+ Here is the caller graph for this function:

void SchemeEval::capture_port ( void  )
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.

+ Here is the caller graph for this function:

SCM SchemeEval::catch_handler ( SCM  tag,
SCM  throw_args 
)
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().

+ Here is the caller graph for this function:

SCM SchemeEval::catch_handler_wrapper ( void *  data,
SCM  tag,
SCM  throw_args 
)
staticprivate

Definition at line 377 of file SchemeEval.cc.

References catch_handler().

+ Here is the caller graph for this function:

Handle SchemeEval::do_apply ( const std::string &  func,
Handle varargs 
)
private

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().

+ Here is the caller graph for this function:

SCM SchemeEval::do_apply_scm ( const std::string &  func,
Handle varargs 
)
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().

+ Here is the caller graph for this function:

void SchemeEval::do_eval ( const std::string &  expr)
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().

+ Here is the caller graph for this function:

std::string SchemeEval::do_poll_result ( )
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().

SCM SchemeEval::do_scm_eval ( SCM  sexpr,
SCM(*)(void *)  evo 
)
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().

+ Here is the caller graph for this function:

void SchemeEval::drain_output ( void  )
private

Discard all chars in the outport.

Definition at line 144 of file SchemeEval.cc.

References poll_port().

+ Here is the caller graph for this function:

std::string opencog::SchemeEval::eval ( const std::string &  expr)
inline

Definition at line 155 of file SchemeEval.h.

References begin_eval(), eval_expr(), and poll_result().

+ Here is the caller graph for this function:

std::string opencog::SchemeEval::eval ( const std::stringstream &  ss)
inline

Definition at line 157 of file SchemeEval.h.

References eval().

void SchemeEval::eval_expr ( const std::string &  expr)
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().

+ Here is the caller graph for this function:

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().

+ Here is the caller graph for this function:

Handle opencog::SchemeEval::eval_h ( const std::stringstream &  ss)
inline

Definition at line 162 of file SchemeEval.h.

References eval_h().

+ Here is the caller graph for this function:

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.

TruthValuePtr opencog::SchemeEval::eval_tv ( const std::stringstream &  ss)
inline

Definition at line 166 of file SchemeEval.h.

References eval_tv().

+ Here is the caller graph for this function:

void SchemeEval::finish ( void  )
private

Definition at line 157 of file SchemeEval.cc.

References _in_server, _outport, _pipe, _rc, captured_stack, error_string, and init_mtx.

+ Here is the caller graph for this function:

SchemeEval * SchemeEval::get_evaluator ( AtomSpace as = NULL)
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().

+ Here is the caller graph for this function:

void SchemeEval::init ( void  )
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.

+ Here is the caller graph for this function:

void SchemeEval::init_scheme ( void  )
static

Definition at line 1197 of file SchemeEval.cc.

void SchemeEval::per_thread_init ( void  )
private

Definition at line 322 of file SchemeEval.cc.

References guile_user_module, and thread_is_inited.

+ Here is the caller graph for this function:

std::string SchemeEval::poll_port ( )
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.

+ Here is the caller graph for this function:

std::string SchemeEval::poll_result ( )
virtual

Implements opencog::GenericEval.

Definition at line 523 of file SchemeEval.cc.

References answer, and c_wrap_poll().

+ Here is the caller graph for this function:

SCM SchemeEval::preunwind_handler ( SCM  tag,
SCM  throw_args 
)
private

Definition at line 383 of file SchemeEval.cc.

References set_captured_stack().

+ Here is the caller graph for this function:

SCM SchemeEval::preunwind_handler_wrapper ( void *  data,
SCM  tag,
SCM  throw_args 
)
staticprivate

Definition at line 371 of file SchemeEval.cc.

References preunwind_handler().

+ Here is the caller graph for this function:

std::string SchemeEval::prt ( SCM  node)
staticprivate

Definition at line 343 of file SchemeEval.cc.

References opencog::SchemeSmob::cog_misc_tag, and opencog::SchemeSmob::misc_to_string().

+ Here is the caller graph for this function:

bool opencog::SchemeEval::recursing ( void  )
inline

Definition at line 173 of file SchemeEval.h.

References _in_eval.

void SchemeEval::redirect_output ( void  )
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().

+ Here is the caller graph for this function:

void SchemeEval::restore_output ( void  )
private

Definition at line 132 of file SchemeEval.cc.

References _in_redirect, and _saved_outport.

+ Here is the caller graph for this function:

void SchemeEval::set_captured_stack ( SCM  newstack)
private

Definition at line 189 of file SchemeEval.cc.

References captured_stack.

+ Here is the caller graph for this function:

void SchemeEval::set_error_string ( SCM  newerror)
private

Definition at line 197 of file SchemeEval.cc.

References error_string.

+ Here is the caller graph for this function:

void SchemeEval::set_scheme_as ( AtomSpace as)
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().

void SchemeEval::thread_lock ( void  )
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.

+ Here is the caller graph for this function:

void SchemeEval::thread_unlock ( void  )
private

Definition at line 292 of file SchemeEval.cc.

References ser_key, and serialize_lock.

+ Here is the caller graph for this function:

Member Data Documentation

bool opencog::SchemeEval::_eval_done
private

Definition at line 85 of file SchemeEval.h.

int opencog::SchemeEval::_gc_ctr
private

Definition at line 133 of file SchemeEval.h.

bool opencog::SchemeEval::_in_eval
private

Definition at line 134 of file SchemeEval.h.

int opencog::SchemeEval::_in_redirect
private

Definition at line 98 of file SchemeEval.h.

bool opencog::SchemeEval::_in_server
private

Definition at line 97 of file SchemeEval.h.

bool opencog::SchemeEval::_in_shell
private

Definition at line 96 of file SchemeEval.h.

SCM opencog::SchemeEval::_outport
private

Definition at line 94 of file SchemeEval.h.

SCM opencog::SchemeEval::_pipe
private

Definition at line 90 of file SchemeEval.h.

int opencog::SchemeEval::_pipeno
private

Definition at line 91 of file SchemeEval.h.

bool opencog::SchemeEval::_poll_done
private

Definition at line 86 of file SchemeEval.h.

std::mutex opencog::SchemeEval::_poll_mtx
private

Definition at line 87 of file SchemeEval.h.

SCM opencog::SchemeEval::_rc
private

Definition at line 84 of file SchemeEval.h.

SCM opencog::SchemeEval::_saved_outport
private

Definition at line 95 of file SchemeEval.h.

std::condition_variable opencog::SchemeEval::_wait_done
private

Definition at line 88 of file SchemeEval.h.

std::string opencog::SchemeEval::answer
private

Definition at line 83 of file SchemeEval.h.

AtomSpace* opencog::SchemeEval::atomspace
private

Definition at line 132 of file SchemeEval.h.

SCM opencog::SchemeEval::captured_stack
private

Definition at line 121 of file SchemeEval.h.

std::string opencog::SchemeEval::error_msg
private

Definition at line 119 of file SchemeEval.h.

SCM opencog::SchemeEval::error_string
private

Definition at line 118 of file SchemeEval.h.

Handle opencog::SchemeEval::hargs
private

Definition at line 112 of file SchemeEval.h.

const std::string* opencog::SchemeEval::pexpr
private

Definition at line 82 of file SchemeEval.h.

TruthValuePtr opencog::SchemeEval::tvp
private

Definition at line 113 of file SchemeEval.h.


The documentation for this class was generated from the following files: