OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
InferenceSCM.cc
Go to the documentation of this file.
1 /*
2  * InferenceSCM.cc
3  *
4  * Copyright (C) 2014 Misgana Bayetta
5  * Copyright (C) 2015 OpenCog Foundation
6  *
7  * Author: Misgana Bayetta <misgana.bayetta@gmail.com> Sept 2014
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License v3 as
11  * published by the Free Software Foundation and including the exceptions
12  * at http://opencog.org/wiki/Licenses
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program; if not, write to:
21  * Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 
25 #include "InferenceSCM.h"
26 
33 
34 using namespace opencog;
35 
37 {
38  static bool is_init = false;
39  if (is_init) return;
40  is_init = true;
41  scm_with_guile(init_in_guile, this);
42 }
43 
52 void* InferenceSCM::init_in_guile(void* self)
53 {
54  scm_c_define_module("opencog rule-engine", init_in_module, self);
55  scm_c_use_module("opencog rule-engine");
56  return NULL;
57 }
58 
65 {
66  InferenceSCM* self = (InferenceSCM*) data;
67  self->init();
68 }
69 
70 
72 {
73 #ifdef HAVE_GUILE
74  // All commands for invoking the rule engine from scm shell should
75  // be declared here
76  define_scheme_primitive("cog-fc", &InferenceSCM::do_forward_chaining,
77  this, "rule-engine");
78  define_scheme_primitive("cog-bc", &InferenceSCM::do_backward_chaining,
79  this, "rule-engine");
80 #endif
81 }
82 
84 {
85  if (Handle::UNDEFINED == rbs)
86  throw RuntimeException(TRACE_INFO,
87  "InferenceSCM::do_forward_chaining - invalid rulebase!");
88 
89 #ifdef HAVE_GUILE
90  AtomSpace *as = SchemeSmob::ss_get_env_as("cog-fc");
91  DefaultForwardChainerCB dfc(*as);
92  ForwardChainer fc(*as, rbs);
99  if (h->getType() == LIST_LINK and as->get_outgoing(h).empty())
100  fc.do_chain(dfc, Handle::UNDEFINED);
101  else
110  fc.do_chain(dfc, h);
111 
113  return as->add_link(LIST_LINK, result);
114 #else
115  return Handle::UNDEFINED;
116 #endif
117 }
118 
120 {
121  if (Handle::UNDEFINED == rbs)
122  throw RuntimeException(TRACE_INFO,
123  "InferenceSCM::do_backward_chaining - invalid rulebase!");
124 
125 #ifdef HAVE_GUILE
126  AtomSpace *as = SchemeSmob::ss_get_env_as("cog-bc");
127 
128  BackwardChainer bc(*as, rbs);
129  bc.set_target(h);
130 
131  logger().debug("[BackwardChainer] Before do_chain");
132 
133  bc.do_chain();
134 
135  logger().debug("[BackwardChainer] After do_chain");
136  map<Handle, UnorderedHandleSet> soln = bc.get_chaining_result();
137 
138  HandleSeq soln_list_link;
139  for (auto it = soln.begin(); it != soln.end(); ++it) {
140  HandleSeq hs;
141  hs.push_back(it->first);
142  hs.insert(hs.end(), it->second.begin(), it->second.end());
143 
144  soln_list_link.push_back(as->add_link(LIST_LINK, hs));
145  }
146 
147  return as->add_link(LIST_LINK, soln_list_link);
148 #else
149  return Handle::UNDEFINED;
150 #endif
151 }
152 
153 
155 {
156  static InferenceSCM inference;
157 }
void set_target(Handle init_target)
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
Handle do_forward_chaining(Handle h, Handle rbs)
Definition: InferenceSCM.cc:83
Type getType() const
Definition: Atom.h:197
const VarMultimap & get_chaining_result()
static const Handle UNDEFINED
Definition: Handle.h:77
static void * init_in_guile(void *)
Definition: InferenceSCM.cc:52
static void init_in_module(void *)
Definition: InferenceSCM.cc:64
Handle do_backward_chaining(Handle h, Handle rbs)
void opencog_ruleengine_init(void)
Handle add_link(Type t, const HandleSeq &outgoing, bool async=false)
Definition: AtomSpace.cc:175
HandleSeq get_chaining_result(void)
static AtomSpace * ss_get_env_as(const char *)
const HandleSeq & get_outgoing(Handle h) const
Definition: AtomSpace.h:735
void do_chain(ForwardChainerCallBack &fcb, Handle hsource=Handle::UNDEFINED)