OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
FindUtils.h
Go to the documentation of this file.
1 
27 #ifndef _OPENCOG_FIND_UTILS_H
28 #define _OPENCOG_FIND_UTILS_H
29 
30 #include <set>
31 #include <vector>
32 
33 #include <opencog/util/functional.h>
34 #include <opencog/atomspace/Atom.h>
37 #include <opencog/atomspace/Link.h>
39 
40 namespace opencog {
41 
47 
48 //================================================================
84 class FindAtoms
85 {
86  public:
88  std::set<Handle> varset;
89  std::set<Handle> holders;
90  std::set<Handle> least_holders;
91 
92  inline FindAtoms(Type t, bool subclass = false)
93  : stop_at_quote(true), _target_types({t})
94  {
95  if (subclass)
96  {
98  }
99  }
100 
101  inline FindAtoms(Type ta, Type tb, bool subclass = false)
102  : stop_at_quote(true), _target_types({ta, tb})
103  {
104  if (subclass)
105  {
108  }
109  }
110 
111  inline FindAtoms(const Handle& atom)
112  : stop_at_quote(true),
113  _target_types(),
115  { _target_atoms.insert(atom); }
116 
117  inline FindAtoms(const std::set<Handle>& selection)
118  : stop_at_quote(true),
119  _target_types(),
120  _target_atoms(selection)
121  {}
122 
128  inline void search_set(const Handle& h)
129  {
130  find_rec(h);
131  }
132 
133  inline void search_set(const std::vector<Handle>& hlist)
134  {
135  for (const Handle& h : hlist) find_rec(h);
136  }
137  private:
138  typedef enum
139  {
140  NOPE, // Does not contain.
141  YEP, // Contains, but not immediately so.
142  IMM // Contains immediately below.
143  } Loco;
144 
145  inline Loco find_rec(const Handle& h)
146  {
147  Type t = h->getType();
148  if (1 == _target_types.count(t) or _target_atoms.count(h) == 1)
149  {
150  varset.insert(h);
151  return IMM;
152  }
153 
154  if (stop_at_quote and QUOTE_LINK == t) return NOPE;
155 
156  LinkPtr l(LinkCast(h));
157  if (l)
158  {
159  bool held = false;
160  bool imm = false;
161  for (const Handle& oh : l->getOutgoingSet())
162  {
163  Loco where = find_rec(oh);
164  if (NOPE != where) held = true;
165  if (IMM == where) imm = true;
166  }
167  if (imm) least_holders.insert(h);
168  if (held)
169  {
170  holders.insert(h);
171  return YEP;
172  }
173  }
174  return NOPE;
175  }
176 
177  private:
178  std::set<Type> _target_types;
179  std::set<Handle> _target_atoms;
180 };
181 
186 static inline bool is_atom_in_tree(const Handle& tree, const Handle& atom)
187 {
188  if (tree == Handle::UNDEFINED) return false;
189  if (tree == atom) return true;
190  LinkPtr ltree(LinkCast(tree));
191  if (NULL == ltree) return false;
192 
193  // Recurse downwards...
194  for (const Handle h: ltree->getOutgoingSet()) {
195  if (is_atom_in_tree(h, atom)) return true;
196  }
197  return false;
198 }
199 
205 static inline bool is_quoted_in_tree(const Handle& tree, const Handle& atom)
206 {
207  if (tree == Handle::UNDEFINED) return false;
208  if (tree == atom) return false; // not quoted, so false.
209  LinkPtr ltree(LinkCast(tree));
210  if (NULL == ltree) return false;
211 
212  if (tree->getType() == QUOTE_LINK)
213  {
214  if (is_atom_in_tree(tree, atom)) return true;
215  return false;
216  }
217 
218  // Recurse downwards...
219  for (const Handle& h: ltree->getOutgoingSet()) {
220  if (is_quoted_in_tree(h, atom)) return true;
221  }
222  return false;
223 }
224 
232 static inline bool is_unquoted_in_tree(const Handle& tree, const Handle& atom)
233 {
234  if (tree == Handle::UNDEFINED) return false;
235  if (tree == atom) return true;
236  LinkPtr ltree(LinkCast(tree));
237  if (NULL == ltree) return false;
238 
239  if (tree->getType() == QUOTE_LINK) return false;
240 
241  // Recurse downwards...
242  for (const Handle& h : ltree->getOutgoingSet()) {
243  if (is_unquoted_in_tree(h, atom)) return true;
244  }
245  return false;
246 }
247 
252 static inline bool any_atom_in_tree(const Handle& tree,
253  const std::set<Handle>& atoms)
254 {
255  for (const Handle& n: atoms)
256  {
257  if (is_atom_in_tree(tree, n)) return true;
258  }
259  return false;
260 }
261 
269 static inline bool any_unquoted_in_tree(const Handle& tree,
270  const std::set<Handle>& atoms)
271 {
272  for (const Handle& n: atoms)
273  {
274  if (is_unquoted_in_tree(tree, n)) return true;
275  }
276  return false;
277 }
278 
286 static inline unsigned int num_unquoted_in_tree(const Handle& tree,
287  const std::set<Handle>& atoms)
288 {
289  unsigned int count = 0;
290  for (const Handle& n: atoms)
291  {
292  if (is_unquoted_in_tree(tree, n)) count++;
293  }
294  return count;
295 }
296 
300 static inline bool is_atom_in_any_tree(const std::vector<Handle>& trees,
301  const Handle& atom)
302 {
303  for (const Handle& tree: trees)
304  {
305  if (is_atom_in_tree(tree, atom)) return true;
306  }
307  return false;
308 }
309 
315 static inline bool is_unquoted_in_any_tree(const std::vector<Handle>& trees,
316  const Handle& atom)
317 {
318  for (const Handle& tree: trees)
319  {
320  if (is_unquoted_in_tree(tree, atom)) return true;
321  }
322  return false;
323 }
324 
329 static inline bool contains_atomtype(const Handle& clause, Type atom_type)
330 {
331  Type clause_type = clause->getType();
332  if (QUOTE_LINK == clause_type) return false;
333  if (classserver().isA(clause_type, atom_type)) return true;
334 
335  LinkPtr lc(LinkCast(clause));
336  if (not lc) return false;
337 
338  for (const Handle& subclause: lc->getOutgoingSet())
339  {
340  if (contains_atomtype(subclause, atom_type)) return true;
341  }
342  return false;
343 }
344 
345 } // namespace opencog
346 
347 #endif // _OPENCOG_FIND_UTILS_H
static bool contains_atomtype(const Handle &clause, Type atom_type)
Definition: FindUtils.h:329
std::set< Handle > varset
Definition: FindUtils.h:88
static bool is_atom_in_tree(const Handle &tree, const Handle &atom)
Definition: FindUtils.h:186
static bool any_atom_in_tree(const Handle &tree, const std::set< Handle > &atoms)
Definition: FindUtils.h:252
FindAtoms(Type ta, Type tb, bool subclass=false)
Definition: FindUtils.h:101
static bool is_quoted_in_tree(const Handle &tree, const Handle &atom)
Definition: FindUtils.h:205
std::set< Handle > holders
Definition: FindUtils.h:89
static unsigned int num_unquoted_in_tree(const Handle &tree, const std::set< Handle > &atoms)
Definition: FindUtils.h:286
Type getType() const
Definition: Atom.h:197
std::set< Type > _target_types
Definition: FindUtils.h:178
unsigned long getChildrenRecursive(Type type, OutputIterator result)
Definition: ClassServer.h:109
static bool any_unquoted_in_tree(const Handle &tree, const std::set< Handle > &atoms)
Definition: FindUtils.h:269
std::shared_ptr< Link > LinkPtr
Definition: Atom.h:53
ClassServer & classserver(ClassServerFactory *=ClassServer::createInstance)
Definition: ClassServer.cc:159
void search_set(const std::vector< Handle > &hlist)
Definition: FindUtils.h:133
std::set< Handle > _target_atoms
Definition: FindUtils.h:179
static const Handle UNDEFINED
Definition: Handle.h:77
std::set< Handle > least_holders
Definition: FindUtils.h:90
FindAtoms(const std::set< Handle > &selection)
Definition: FindUtils.h:117
static LinkPtr LinkCast(const Handle &h)
Definition: Link.h:263
static bool is_unquoted_in_any_tree(const std::vector< Handle > &trees, const Handle &atom)
Definition: FindUtils.h:315
Loco find_rec(const Handle &h)
Definition: FindUtils.h:145
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40
FindAtoms(Type t, bool subclass=false)
Definition: FindUtils.h:92
void search_set(const Handle &h)
Definition: FindUtils.h:128
static bool is_unquoted_in_tree(const Handle &tree, const Handle &atom)
Definition: FindUtils.h:232
static bool is_atom_in_any_tree(const std::vector< Handle > &trees, const Handle &atom)
Definition: FindUtils.h:300