OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
FollowLink.h
Go to the documentation of this file.
1 
16 #ifndef _OPENCOG_FOLLOW_LINK_H
17 #define _OPENCOG_FOLLOW_LINK_H
18 
19 #include <opencog/atomspace/Link.h>
20 
21 namespace opencog
22 {
28 {
29 public:
30 
41  inline Handle follow_binary_link(const Handle& h, Type ltype)
42  {
43  return follow_link(h, ltype, 0, 1);
44  }
45 
46  inline Handle backtrack_binary_link(const Handle& h, Type ltype)
47  {
48  return follow_link(h, ltype, 1, 0);
49  }
50 
51  inline Handle follow_link(const Handle& h, Type ltype, int from, int to)
52  {
53  // Look for incoming links that are of the given type.
54  // Then grab the thing that they link to.
55  link_type = ltype;
56  from_atom = h;
58  position_from = from;
59  position_to = to;
61  return to_atom;
62  }
63 
64 private:
70  int cnt;
71 
75  inline bool find_link_type(const Handle& h)
76  {
77  // Make sure that the link is of the desired type.
78  if (link_type != h->getType()) return false;
79 
80  cnt = -1;
82  LinkPtr l(LinkCast(h));
83  if (l) l->foreach_outgoing(&FollowLink::pursue_link, this);
84  if (to_atom != Handle::UNDEFINED) return true;
85  return false;
86  }
87 
88  inline bool pursue_link(const Handle& h)
89  {
90  cnt ++;
91 
92  // The from-slot should be occupied by the node itself.
93  if (position_from == cnt) {
94  if (from_atom != h) {
96  return true; // Bad match, stop now.
97  }
98  return false;
99  }
100 
101  // The to-slot is the one we're looking for.
102  if (position_to == cnt) {
103  to_atom = h;
104  }
105 
106  return false;
107  }
108 };
109 
111 } // namespace opencog
112 
113 #endif // _OPENCOG_FOLLOW_LINK_H
bool foreach_incoming(bool(T::*cb)(const Handle &), T *data)
Definition: Atom.h:329
Type getType() const
Definition: Atom.h:197
std::shared_ptr< Link > LinkPtr
Definition: Atom.h:53
static const Handle UNDEFINED
Definition: Handle.h:77
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