OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ForeachChaseLink.h
Go to the documentation of this file.
1 
37 #ifndef _OPENCOG_LINK_CHASE_H
38 #define _OPENCOG_LINK_CHASE_H
39 
40 #include <opencog/atomspace/Link.h>
41 
42 namespace opencog
43 {
90 template <typename T>
92 {
93 public:
94 
109  inline bool follow_link(const Handle& h, Type ltype,
110  int from, int to, bool (T::*cb)(const Handle&), T *data)
111  {
112  user_callback = cb;
113  user_callback_lh = NULL;
114  return do_follow_link (h, ltype, from, to, data);
115  }
116 
121  inline bool follow_link_lh(const Handle& h, Type ltype, int from, int to,
122  bool (T::*cb)(const Handle&, const Handle&), T *data)
123  {
124  user_callback = NULL;
125  user_callback_lh = cb;
126  return do_follow_link (h, ltype, from, to, data);
127  }
128 
129  inline bool follow_unordered_binary_link(const Handle& h, Type ltype,
130  bool (T::*cb)(const Handle&), T *data)
131  {
132  user_callback = cb;
133  user_callback_lh = NULL;
134  return do_follow_unordered_binary_link(h, ltype, data);
135  }
136 
137  inline bool follow_unordered_binary_link(const Handle& h, Type ltype,
138  bool (T::*cb)(const Handle&, const Handle&), T *data)
139  {
140  user_callback = NULL;
141  user_callback_lh = cb;
142  return do_follow_unordered_binary_link(h, ltype, data);
143  }
144 
145 private:
151  int cnt;
153  bool (T::*user_callback)(const Handle&);
154  bool (T::*user_callback_lh)(const Handle&, const Handle&);
156 
157  inline bool do_follow_link(const Handle& h, Type ltype, int from, int to, T *data)
158  {
159  // Look for incoming links that are of the given type.
160  // Then grab the thing that they link to.
161  link_type = ltype;
162  from_atom = h;
164  position_from = from;
165  position_to = to;
166  user_data = data;
169  return rc;
170  }
171 
172  inline bool do_follow_unordered_binary_link(const Handle& h, Type ltype, T *data)
173  {
174  // Look for incoming links that are of the given type.
175  // Then grab the thing that they link to.
176  link_type = ltype;
177  from_atom = h;
179  user_data = data;
182  return rc;
183  }
184 
188  inline bool find_link_type(const Handle& link_h)
189  {
190  // Make sure the link is of the specified link type
191  if (link_type != link_h->getType()) return false;
192 
193  cnt = -1;
195  // foreach_outgoing_handle(link_h, PrivateUseOnlyChaseLink::endpoint_matcher, this);
196  LinkCast(link_h)->foreach_outgoing(endpoint_matcher, this);
197 
198  bool rc = false;
199  if (Handle::UNDEFINED != to_atom)
200  {
201  if (user_callback)
202  rc = (user_data->*user_callback)(to_atom);
203  else
204  rc = (user_data->*user_callback_lh)(to_atom, link_h);
205  }
206  return rc;
207  }
208 
209  inline bool pursue_link(const Handle& h)
210  {
211  cnt ++;
212 
213  // The from-slot should be occupied by the node itself.
214  if (position_from == cnt)
215  {
216  if (from_atom != h)
217  {
219  return true; // bad match, stop now.
220  }
221  return false;
222  }
223 
224  // The to-slot is the one we're looking for.
225  if (position_to == cnt)
226  {
227  to_atom = h;
228  }
229 
230  return false;
231  }
232 
233  inline bool pursue_unordered_link(const Handle& h)
234  {
235  // There are only two atoms in a binary link. The one that is
236  // not the from_atom is the one we are looking for.
237  if (from_atom != h)
238  {
239  to_atom = h; // found it!
240  return true;
241  }
242  return false;
243  }
244 };
245 
257 template <typename T>
258 inline bool foreach_binary_link(const Handle& h, Type ltype,
259  bool (T::*cb)(const Handle&), T *data)
260 {
262  return cl.follow_link(h, ltype, 0, 1, cb, data);
263 }
264 
269 template <typename T>
270 inline bool foreach_binary_link(const Handle& h, Type ltype,
271  bool (T::*cb)(const Handle&, const Handle&), T *data)
272 {
274  return cl.follow_link_lh(h, ltype, 0, 1, cb, data);
275 }
276 
283 template <typename T>
284 inline bool foreach_reverse_binary_link(const Handle& h, Type ltype,
285  bool (T::*cb)(const Handle&), T *data)
286 {
288  return cl.follow_link(h, ltype, 1, 0, cb, data);
289 }
290 
295 template <typename T>
296 inline bool foreach_reverse_binary_link(const Handle& h, Type ltype,
297  bool (T::*cb)(const Handle&, const Handle&), T *data)
298 {
300  return cl.follow_link_lh(h, ltype, 1, 0, cb, data);
301 }
302 
317 template <typename T>
318 inline bool foreach_link(const Handle& h, Type ltype, int from, int to,
319  bool (T::*cb)(const Handle&), T *data)
320 {
322  return cl.follow_link(h, ltype, from, to, cb, data);
323 }
324 
329 template <typename T>
330 inline bool foreach_link(const Handle& h, Type ltype, int from, int to,
331  bool (T::*cb)(const Handle&, const Handle&), T *data)
332 {
334  return cl.follow_link_lh(h, ltype, from, to, cb, data);
335 }
336 
340 template <typename T>
341 inline bool foreach_unordered_binary_link(const Handle& h, Type ltype,
342  bool (T::*cb)(const Handle&), T *data)
343 {
345  return cl.follow_unordered_binary_link(h, ltype, cb, data);
346 }
347 
348 template <typename T>
349 inline bool foreach_unordered_binary_link(const Handle& h, Type ltype,
350  bool (T::*cb)(const Handle&, const Handle&), T *data)
351 {
353  return cl.follow_unordered_binary_link(h, ltype, cb, data);
354 }
355 
357 } // namespace opencog
358 
359 #endif // _OPENCOG_LINK_CHASE_H
bool foreach_unordered_binary_link(const Handle &h, Type ltype, bool(T::*cb)(const Handle &), T *data)
bool foreach_reverse_binary_link(const Handle &h, Type ltype, bool(T::*cb)(const Handle &), T *data)
bool foreach_link(const Handle &h, Type ltype, int from, int to, bool(T::*cb)(const Handle &), T *data)
bool foreach_incoming(bool(T::*cb)(const Handle &), T *data)
Definition: Atom.h:329
Type getType() const
Definition: Atom.h:197
static const Handle UNDEFINED
Definition: Handle.h:77
bool foreach_binary_link(const Handle &h, Type ltype, bool(T::*cb)(const Handle &), T *data)
static LinkPtr LinkCast(const Handle &h)
Definition: Link.h:263
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40