OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Rule.cc
Go to the documentation of this file.
1 /*
2  * Rule.cc
3  *
4  * Copyright (C) 2015 Misgana Bayetta
5  *
6  * Author: Misgana Bayetta <misgana.bayetta@gmail.com> 2015
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License v3 as
10  * published by the Free Software Foundation and including the exceptions
11  * at http://opencog.org/wiki/Licenses
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program; if not, write to:
20  * Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #include <queue>
25 
26 #include <boost/uuid/uuid_io.hpp>
27 #include <boost/uuid/uuid_generators.hpp>
28 
32 
33 #include "Rule.h"
34 
35 using namespace opencog;
36 
38 {
39  if (!rule->isType(MEMBER_LINK, true))
40  throw InvalidParamException(TRACE_INFO,
41  "Rule '%s' is expected to be a MemberLink",
42  rule->toString().c_str());
43 
44  Handle name_h = LinkCast(rule)->getOutgoingAtom(0),
45  rbs_h = LinkCast(rule)->getOutgoingAtom(1);
46 
48  name_ = NodeCast(name_h)->getName();
49  category_ = NodeCast(rbs_h)->getName();
50  weight_ = rule->getTruthValue()->getMean();
51 }
52 
54 {
55  return weight_;
56 }
57 
58 void Rule::set_category(const string& name)
59 {
60  category_ = name;
61 }
62 
64 {
65  return category_;
66 }
67 
68 const string& Rule::get_category() const
69 {
70  return category_;
71 }
72 
73 void Rule::set_name(const string& name)
74 {
75  name_ = name;
76 }
77 
78 string& Rule::get_name()
79 {
80  return name_;
81 }
82 
83 const string& Rule::get_name() const
84 {
85  return name_;
86 }
87 
88 void Rule::set_handle(Handle h) throw (InvalidParamException)
89 {
90  rule_handle_ = h;
91 }
92 
94 {
95  return rule_handle_;
96 }
97 
104 {
105  return LinkCast(rule_handle_)->getOutgoingAtom(0);
106 }
107 
114 {
115  // if the rule's handle has not been set yet
117  return Handle::UNDEFINED;
118 
119  return BindLinkCast(rule_handle_)->get_body();
120 }
121 
129 {
130  Handle implicant= get_implicant();
131  Type t = implicant->getType();
132  HandleSeq hs;
133 
134  if (t == AND_LINK or t == OR_LINK)
135  hs = LinkCast(implicant)->getOutgoingSet();
136  else
137  hs.push_back(implicant);
138 
139  return hs;
140 }
147 {
148  // if the rule's handle has not been set yet
150  return Handle::UNDEFINED;
151 
152  return BindLinkCast(rule_handle_)->get_implicand();
153 }
154 
164 {
165  // if the rule's handle has not been set yet
167  return HandleSeq();
168 
169  Handle implicand = BindLinkCast(rule_handle_)->get_implicand();
170 
171  std::queue<Handle> pre_output;
172  HandleSeq final_output;
173 
174  // skip the top level ListLink
175  if (implicand->getType() == LIST_LINK)
176  {
177  for (Handle h : LinkCast(implicand)->getOutgoingSet())
178  pre_output.push(h);
179  }
180  else
181  {
182  pre_output.push(implicand);
183  }
184 
185  // check all output of ExecutionOutputLink
186  while (not pre_output.empty())
187  {
188  Handle hfront = pre_output.front();
189  pre_output.pop();
190 
191  if (hfront->getType() == EXECUTION_OUTPUT_LINK)
192  {
193  // get the ListLink containing the arguments of the ExecutionOutputLink
194  Handle harg = LinkCast(hfront)->getOutgoingSet()[1];
195 
196  for (Handle h : LinkCast(harg)->getOutgoingSet())
197  pre_output.push(h);
198 
199  continue;
200  }
201 
202  // if not an ExecutionOutputLink, it is a final output
203  final_output.push_back(hfront);
204  }
205 
206  return final_output;
207 }
208 
209 void Rule::set_weight(float p)
210 {
211  weight_ = p;
212 }
213 
221 {
222  // clone the Rule
223  Rule st_ver = *this;
224  std::map<Handle, Handle> dict;
225 
226  Handle st_bindlink = standardize_helper(as, rule_handle_, dict);
227  st_ver.set_handle(st_bindlink);
228 
229  return st_ver;
230 }
231 
240 Handle Rule::standardize_helper(AtomSpace* as, const Handle h, std::map<Handle, Handle>& dict)
241 {
242  if (LinkCast(h))
243  {
244  HandleSeq old_outgoing = LinkCast(h)->getOutgoingSet();
245  HandleSeq new_outgoing;
246 
247  for (auto ho : old_outgoing)
248  new_outgoing.push_back(standardize_helper(as, ho, dict));
249 
250  return as->add_atom(createLink(h->getType(), new_outgoing, h->getTruthValue()));
251  }
252 
253  // normal node does not need to be changed
254  if (h->getType() != VARIABLE_NODE)
255  return h;
256 
257  // use existing mapping if the VariableNode is already mapped
258  if (dict.count(h) == 1)
259  return dict[h];
260 
261  std::stringstream ss;
262  ss << NodeCast(h)->getName() << "-rule_uuid_" << rule_handle_ << "-standardize_apart";
263 
264  Handle hcpy = as->add_atom(createNode(h->getType(), ss.str(), h->getTruthValue()));
265  dict[h] = hcpy;
266 
267  return hcpy;
268 }
#define createLink
Definition: Link.h:269
Handle get_implicand()
Definition: Rule.cc:146
Rule gen_standardize_apart(AtomSpace *as)
Definition: Rule.cc:220
Handle standardize_helper(AtomSpace *as, Handle, std::map< Handle, Handle > &)
Definition: Rule.cc:240
HandleSeq get_implicant_seq()
Definition: Rule.cc:128
void set_weight(float p)
Definition: Rule.cc:209
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
Handle get_vardecl()
Definition: Rule.cc:103
void set_category(const string &name)
Definition: Rule.cc:58
static BindLinkPtr BindLinkCast(const Handle &h)
Definition: BindLink.h:62
virtual std::string toString(std::string indent="")=0
Type getType() const
Definition: Atom.h:197
bool isType(Type t, bool subclass) const
Definition: Atom.h:200
static NodePtr NodeCast(const Handle &h)
Definition: Node.h:113
static const Handle UNDEFINED
Definition: Handle.h:77
string name_
Definition: Rule.h:84
HandleSeq get_implicand_seq()
Definition: Rule.cc:163
#define createNode
Definition: Node.h:119
static LinkPtr LinkCast(const Handle &h)
Definition: Link.h:263
Rule(Handle rule)
Definition: Rule.cc:37
void set_name(const string &name)
Definition: Rule.cc:73
TruthValuePtr getTruthValue()
Definition: Atom.cc:104
string & get_category()
Definition: Rule.cc:63
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40
string category_
Definition: Rule.h:87
float weight_
Definition: Rule.h:91
string & get_name()
Definition: Rule.cc:78
Handle rule_handle_
Definition: Rule.h:81
Handle get_handle()
Definition: Rule.cc:93
void set_handle(Handle h)
Definition: Rule.cc:88
float get_weight()
Definition: Rule.cc:53
Handle get_implicant()
Definition: Rule.cc:113
Handle add_atom(AtomPtr atom, bool async=false)
Definition: AtomSpace.cc:100