Skip to content

Commit

Permalink
provide a simpler, better solution for wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
linas committed Dec 23, 2014
1 parent a900bc2 commit 513d3e5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 93 deletions.
84 changes: 6 additions & 78 deletions opencog/query/PatternSCM.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,94 +9,22 @@
#include <opencog/guile/SchemePrimitive.h>
#include <opencog/guile/SchemeSmob.h>

#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;
Expand Down
17 changes: 8 additions & 9 deletions opencog/query/PatternSCM.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*);
};

}
Expand Down
23 changes: 18 additions & 5 deletions opencog/query/QueryModule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Copyright (c) 2008 Linas Vepstas <[email protected]>
*/

#include "BindLink.h"
#include "PatternMatch.h"
#include "PatternSCM.h"
#include "QueryModule.h"

Expand All @@ -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"));
}
3 changes: 2 additions & 1 deletion opencog/query/QueryModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#ifndef _OPENCOG_QUERY_MODULE_H
#define _OPENCOG_QUERY_MODULE_H

#include <vector>
#include <opencog/server/Module.h>
#include <opencog/query/PatternSCM.h>

Expand All @@ -16,7 +17,7 @@ namespace opencog {
class QueryModule : public Module
{
private:
PatternSCM* pat;
std::vector<PatternWrap*> _binders;
public:
QueryModule(CogServer&);
virtual ~QueryModule();
Expand Down

0 comments on commit 513d3e5

Please sign in to comment.