From 513d3e54a8646f5541c5e4a189a50a82be529b11 Mon Sep 17 00:00:00 2001 From: Linas Vepstas Date: Tue, 23 Dec 2014 11:30:12 -0600 Subject: [PATCH] provide a simpler, better solution for wrapping --- opencog/query/PatternSCM.cc | 84 +++--------------------------------- opencog/query/PatternSCM.h | 17 ++++---- opencog/query/QueryModule.cc | 23 +++++++--- opencog/query/QueryModule.h | 3 +- 4 files changed, 34 insertions(+), 93 deletions(-) diff --git a/opencog/query/PatternSCM.cc b/opencog/query/PatternSCM.cc index 49558585360..ea61c333e51 100644 --- a/opencog/query/PatternSCM.cc +++ b/opencog/query/PatternSCM.cc @@ -9,94 +9,22 @@ #include #include -#include "BindLink.h" -#include "PatternMatch.h" #include "PatternSCM.h" using namespace opencog; -PatternSCM* PatternSCM::_inst = NULL; - -PatternSCM::PatternSCM() -{ - if (NULL == _inst) { - _inst = this; - init(); - } -} - -PatternSCM::~PatternSCM() -{ - if (_inst == this) _inst = NULL; -} - -void PatternSCM::init(void) -{ - _inst = new PatternSCM(); -#ifdef HAVE_GUILE - // XXX FIXME .. what we really should do here is to make sure - // that SchemeSmob is initialized first ... and, for that, we - // would need to get our hands on an atomspace ... Ugh. Yuck. - define_scheme_primitive("cog-bind", &PatternSCM::do_bindlink, _inst); - define_scheme_primitive("cog-bind-single", &PatternSCM::do_single_bindlink, _inst); - define_scheme_primitive("cog-bind-crisp", &PatternSCM::do_crisp_bindlink, _inst); - define_scheme_primitive("cog-bind-pln", &PatternSCM::do_pln_bindlink, _inst); -#endif -} - -/** - * Run implication, assuming that the argument is a handle to - * an BindLink containing variables and an ImplicationLink - */ -Handle PatternSCM::do_bindlink(Handle h) +PatternWrap::PatternWrap(Handle (f)(AtomSpace*, Handle), const char* n) + : _func(f), _name(n) { -#ifdef HAVE_GUILE - // XXX we should also allow opt-args to be a list of handles - AtomSpace *as = SchemeSmob::ss_get_env_as("cog-bind"); - Handle grounded_expressions = bindlink(as, h); - return grounded_expressions; -#else - return Handle::UNDEFINED; -#endif -} - -/** - * Identical to do_bindlink above, except that it only returns the first match - */ -Handle PatternSCM::do_single_bindlink(Handle h) -{ -#ifdef HAVE_GUILE - // XXX we should also allow opt-args to be a list of handles - AtomSpace *as = SchemeSmob::ss_get_env_as("cog-bind-single"); - Handle grounded_expressions = single_bindlink(as, h); - return grounded_expressions; -#else - return Handle::UNDEFINED; -#endif -} - -/** - * Run implication, assuming that the argument is a handle to - * an BindLink containing variables and an ImplicationLink - */ -Handle PatternSCM::do_crisp_bindlink(Handle h) -{ -#ifdef HAVE_GUILE - // XXX we should also allow opt-args to be a list of handles - AtomSpace *as = SchemeSmob::ss_get_env_as("cog-bind-crisp"); - Handle grounded_expressions = crisp_logic_bindlink(as, h); - return grounded_expressions; -#else - return Handle::UNDEFINED; -#endif + define_scheme_primitive(_name, &PatternWrap::wrapper, this); } -Handle PatternSCM::do_pln_bindlink(Handle h) +Handle PatternWrap::wrapper(Handle h) { #ifdef HAVE_GUILE // XXX we should also allow opt-args to be a list of handles - AtomSpace *as = SchemeSmob::ss_get_env_as("cog-bind-pln"); - Handle grounded_expressions = pln_bindlink(as, h); + AtomSpace *as = SchemeSmob::ss_get_env_as(_name); + Handle grounded_expressions = _func(as, h); return grounded_expressions; #else return Handle::UNDEFINED; diff --git a/opencog/query/PatternSCM.h b/opencog/query/PatternSCM.h index 82a12005597..b0894ecfe64 100644 --- a/opencog/query/PatternSCM.h +++ b/opencog/query/PatternSCM.h @@ -12,18 +12,17 @@ namespace opencog { -class PatternSCM +class AtomSpace; + +/// Wrapper class, to invoke pattern matcher from guile. +class PatternWrap { private: - Handle do_bindlink(Handle); - Handle do_single_bindlink(Handle); - Handle do_crisp_bindlink(Handle); - Handle do_pln_bindlink(Handle); - static PatternSCM* _inst; - void init(void); + Handle wrapper(Handle); + Handle (*_func)(AtomSpace*, Handle); + const char *_name; // scheme name of the c++ function. public: - PatternSCM(); - ~PatternSCM(); + PatternWrap(Handle (*)(AtomSpace*, Handle), const char*); }; } diff --git a/opencog/query/QueryModule.cc b/opencog/query/QueryModule.cc index aeb5e6804c3..c3e5861b819 100644 --- a/opencog/query/QueryModule.cc +++ b/opencog/query/QueryModule.cc @@ -5,6 +5,8 @@ * Copyright (c) 2008 Linas Vepstas */ +#include "BindLink.h" +#include "PatternMatch.h" #include "PatternSCM.h" #include "QueryModule.h" @@ -14,17 +16,28 @@ DECLARE_MODULE(QueryModule); QueryModule::QueryModule(CogServer& cs) : Module(cs) { - pat = NULL; } QueryModule::~QueryModule() { - delete pat; + foreach(PatternWrap *pw: _binders) + delete pw; } void QueryModule::init(void) { - // Force the constructor to run, so that the scheme initialization - // happens. - pat = new PatternSCM(); + // Run implication, assuming that the argument is a handle to + // an BindLink containing variables and an ImplicationLink. + _binders.push_back(new PatternWrap(bindlink, "cog-bind")); + + // Identical to do_bindlink above, except that it only returns the + // first match. + _binders.push_back(new PatternWrap(single_bindlink, "cog-bind-single")); + + // Run implication, assuming that the argument is a handle to + // an BindLink containing variables and an ImplicationLink + _binders.push_back(new PatternWrap(crisp_logic_bindlink, "cog-bind-crisp")); + + // Mystery function + _binders.push_back(new PatternWrap(pln_bindlink, "cog-bind-pln")); } diff --git a/opencog/query/QueryModule.h b/opencog/query/QueryModule.h index a2a81f7e0e8..18b962270d0 100644 --- a/opencog/query/QueryModule.h +++ b/opencog/query/QueryModule.h @@ -8,6 +8,7 @@ #ifndef _OPENCOG_QUERY_MODULE_H #define _OPENCOG_QUERY_MODULE_H +#include #include #include @@ -16,7 +17,7 @@ namespace opencog { class QueryModule : public Module { private: - PatternSCM* pat; + std::vector _binders; public: QueryModule(CogServer&); virtual ~QueryModule();