OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
FCMemory.cc
Go to the documentation of this file.
1 /*
2  * FCMemory.cc
3  *
4  * Copyright (C) 2014 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 #include "FCMemory.h"
24 
25 #include <boost/range/algorithm/find.hpp>
26 
27 using namespace opencog;
28 
30 {
31  _as = as;
32 }
33 
35 {
36 }
37 
39 {
40  for (Handle i : input) {
41  if (boost::find(_potential_sources, i) == _potential_sources.end())
42  _potential_sources.push_back(i);
43  }
44 }
45 
46 vector<Rule*>& FCMemory::get_rules()
47 {
48  return _rules;
49 }
50 
51 void FCMemory::set_rules(const vector<Rule*>& rules)
52 {
53  _rules = rules;
54 }
55 
56 void FCMemory::set_rules(vector<Rule>& rules)
57 {
58  _rules.clear();
59  for (Rule& rule : rules)
60  _rules.push_back(&rule);
61 }
62 
64 {
65  _cur_source = source;
66  _selected_sources.push_back(_cur_source);
67 }
68 
70 {
71  return _selected_sources;
72 }
73 
75 {
76  return _potential_sources;
77 }
78 
80 {
81  _search_in_af = val;
82 }
83 
85 {
86  return _search_in_af;
87 }
88 
90 {
91  return _cur_rule;
92 }
93 
95 {
96  _cur_rule = r;
97 }
98 
99 void FCMemory::add_rules_product(int iteration, HandleSeq product)
100 {
101  for (Handle p : product) {
102  Inference inf;
103  inf.iter_step = iteration;
104  inf.applied_rule = _cur_rule;
105  inf.inf_product.push_back(p);
106  _inf_history.push_back(inf);
107  }
108 }
109 
110 void FCMemory::add_inference(int iter_step, HandleSeq product,
111  HandleSeq matched_nodes)
112 {
113  Inference inf;
114  inf.applied_rule = _cur_rule;
115  inf.iter_step = iter_step;
116  for (Handle p : product)
117  inf.inf_product.push_back(p);
118  for (Handle mn : matched_nodes)
119  inf.matched_nodes.push_back(mn);
120  _inf_history.push_back(inf);
121 }
122 
124 {
125  return _cur_source;
126 }
127 
129 {
130  return (boost::find(_selected_sources, h) != _selected_sources.end());
131 }
132 
134 {
135  for (Handle hi : _potential_sources) {
136  if (hi.value() == h.value())
137  return true;
138  //recursive lookup
139  else if (LinkCast(hi)) {
140  HandleSeqSeq hseqs;
141  hseqs.push_back(_as->get_outgoing(hi));
142  do {
143  HandleSeq iset = hseqs[hseqs.size() - 1];
144  hseqs.pop_back();
145  for (Handle i : iset) {
146  if (i.value() == h.value())
147  return true;
148  else if (LinkCast(i))
149  hseqs.push_back(_as->get_outgoing(i));
150  }
151  } while (not hseqs.empty());
152  }
153  }
154  return false;
155 }
156 
158 {
160  for (Inference i : _inf_history)
161  result.insert(result.end(), i.inf_product.begin(), i.inf_product.end());
162  return result;
163 }
164 
165 vector<Inference>& FCMemory::get_inf_history()
166 {
167  return _inf_history;
168 }
169 
171 {
172  vector<Rule*> applied_rules;
173  for (Inference i : _inf_history) {
174  if (boost::find(applied_rules, i.applied_rule) == applied_rules.end())
175  applied_rules.push_back(i.applied_rule);
176  }
177  return applied_rules;
178 }
HandleSeq get_potential_sources(void)
Definition: FCMemory.cc:74
void add_rules_product(int iteration, HandleSeq product)
Definition: FCMemory.cc:99
FCMemory(AtomSpace *as)
Definition: FCMemory.cc:29
HandleSeq matched_nodes
Definition: FCMemory.h:35
bool is_search_in_af()
Definition: FCMemory.cc:84
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
HandleSeq _selected_sources
Definition: FCMemory.h:45
vector< Rule * > get_applied_rules(void)
Definition: FCMemory.cc:170
Handle get_cur_source(void)
Definition: FCMemory.cc:123
Rule * applied_rule
Definition: FCMemory.h:33
std::vector< HandleSeq > HandleSeqSeq
a list of lists of handles
Definition: Handle.h:248
Handle _cur_source
Definition: FCMemory.h:43
vector< Inference > _inf_history
Definition: FCMemory.h:47
Rule * _cur_rule
Definition: FCMemory.h:42
bool _search_in_af
Definition: FCMemory.h:40
vector< Inference > & get_inf_history()
Definition: FCMemory.cc:165
void set_rules(const vector< Rule * > &rules)
Definition: FCMemory.cc:51
bool isin_potential_sources(Handle h)
Definition: FCMemory.cc:133
bool isin_selected_sources(Handle h)
Definition: FCMemory.cc:128
HandleSeq get_selected_sources(void)
Definition: FCMemory.cc:69
vector< Rule * > _rules
Definition: FCMemory.h:41
static LinkPtr LinkCast(const Handle &h)
Definition: Link.h:263
void set_search_in_af(bool val)
Definition: FCMemory.cc:79
HandleSeq _potential_sources
Definition: FCMemory.h:46
void set_cur_rule(Rule *r)
Definition: FCMemory.cc:94
UUID value(void) const
Definition: Handle.h:85
vector< Rule * > & get_rules()
Definition: FCMemory.cc:46
AtomSpace * _as
Definition: FCMemory.h:49
void update_potential_sources(HandleSeq input)
Definition: FCMemory.cc:38
Rule * get_cur_rule()
Definition: FCMemory.cc:89
void set_source(Handle source)
Definition: FCMemory.cc:63
HandleSeq inf_product
Definition: FCMemory.h:34
void add_inference(int iteration, HandleSeq product, HandleSeq matched_nodes)
Definition: FCMemory.cc:110
const HandleSeq & get_outgoing(Handle h) const
Definition: AtomSpace.h:735
HandleSeq get_result()
Definition: FCMemory.cc:157