OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ExecSCM.cc
Go to the documentation of this file.
1 /*
2  * ExecSCM.cc
3  *
4  * Guile Scheme bindings for the execution links
5  * Copyright (c) 2008, 2014, 2015 Linas Vepstas <linas@linas.org>
6  */
7 
8 
9 #include <cstddef>
10 #include <opencog/atomspace/Link.h>
16 
17 #include "ExecSCM.h"
18 
19 
20 // ========================================================
21 
22 using namespace opencog;
23 
28 {
29  Instantiator inst(atomspace);
30  Handle rh(inst.execute(h));
31  if (NULL != rh)
32  rh = atomspace->add_atom(rh);
33  return rh;
34 }
35 
40 {
41  return EvaluationLink::do_evaluate(atomspace, h);
42 }
43 
48 {
49  Type t = h->getType();
50  if (NUMBER_NODE == t) return Handle(h);
51 
52  if (not classserver().isA(t, FREE_LINK))
53  {
54  throw InvalidParamException(TRACE_INFO,
55  "Expecteing a FreeLink (PlusLink, TimesLink, etc");
56  }
57 
58  // Arghh. The cast should have been enough, but we currently
59  // can't store these in the atomsapce, due to circular shared
60  // lib dependencies.
61  FreeLinkPtr fff(FreeLinkCast(h));
62  if (NULL == fff)
64  Handle hr(fff->reduce());
65 
66  if (DELETE_LINK == hr->getType())
67  {
68  for (const Handle& ho : LinkCast(hr)->getOutgoingSet())
69  atomspace->remove_atom(ho, true);
70  return Handle::UNDEFINED;
71  }
72 
73  return atomspace->add_atom(hr);
74 }
75 
76 // ========================================================
77 
78 // XXX HACK ALERT This needs to be static, in order for python to
79 // work correctly. The problem is that python keeps creating and
80 // destroying this class, but it expects things to stick around.
81 // Oh well. I guess that's OK, since the definition is meant to be
82 // for the lifetime of the server, anyway.
83 std::vector<FunctionWrap*> ExecSCM::_binders;
84 
86  ModuleWrap("opencog exec")
87 {}
88 
91 void ExecSCM::init(void)
92 {
93  _binders.push_back(new FunctionWrap(ss_execute,
94  "cog-execute!", "exec"));
95 
96  _binders.push_back(new FunctionWrap(ss_evaluate,
97  "cog-evaluate!", "exec"));
98 
99  _binders.push_back(new FunctionWrap(ss_reduce,
100  "cog-reduce!", "exec"));
101 }
102 
104 {
105 #if PYTHON_BUG_IS_FIXED
106  for (FunctionWrap* pw : _binders)
107  delete pw;
108 #endif
109 }
110 
111 
113 {
114  static ExecSCM exy;
115  exy.module_init();
116 }
void opencog_exec_init(void)
Definition: ExecSCM.cc:112
static std::vector< FunctionWrap * > _binders
Definition: ExecSCM.h:19
void module_init(void)
Definition: SchemeModule.cc:62
std::shared_ptr< TruthValue > TruthValuePtr
Definition: TruthValue.h:85
virtual void init(void)
Definition: ExecSCM.cc:91
Wrapper class, to invoke misc extension code from guile.
Definition: SchemeModule.h:20
Type getType() const
Definition: Atom.h:197
static TruthValuePtr ss_evaluate(AtomSpace *atomspace, const Handle &h)
Definition: ExecSCM.cc:39
ClassServer & classserver(ClassServerFactory *=ClassServer::createInstance)
Definition: ClassServer.cc:159
static const Handle UNDEFINED
Definition: Handle.h:77
static FreeLinkPtr FreeLinkCast(const Handle &h)
Definition: FreeLink.h:97
std::shared_ptr< FreeLink > FreeLinkPtr
Definition: FreeLink.h:96
bool remove_atom(Handle h, bool recursive=false)
Definition: AtomSpace.cc:344
static LinkPtr LinkCast(const Handle &h)
Definition: Link.h:263
Handle execute(const Handle &expr)
Definition: Instantiator.h:70
static Handle ss_reduce(AtomSpace *atomspace, const Handle &h)
Definition: ExecSCM.cc:47
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40
static Handle ss_execute(AtomSpace *atomspace, const Handle &h)
Definition: ExecSCM.cc:27
Handle add_atom(AtomPtr atom, bool async=false)
Definition: AtomSpace.cc:100