OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AtomTable.h
Go to the documentation of this file.
1 /*
2  * opencog/atomspace/AtomTable.h
3  *
4  * Copyright (C) 2008-2010 OpenCog Foundation
5  * Copyright (C) 2002-2007 Novamente LLC
6  * Copyright (C) 2013,2015 Linas Vepstas <linasvepstas@gmail.com>
7  * All Rights Reserved
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License v3 as
11  * published by the Free Software Foundation and including the exceptions
12  * at http://opencog.org/wiki/Licenses
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program; if not, write to:
21  * Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 
25 #ifndef _OPENCOG_ATOMTABLE_H
26 #define _OPENCOG_ATOMTABLE_H
27 
28 #include <iostream>
29 #include <set>
30 #include <vector>
31 
32 #include <boost/signals2.hpp>
33 
34 #include <opencog/util/async_method_caller.h>
35 #include <opencog/util/exceptions.h>
36 #include <opencog/util/Logger.h>
37 #include <opencog/util/RandGen.h>
38 
39 #include <opencog/atomspace/atom_types.h>
45 #include <opencog/atomspace/Link.h>
47 #include <opencog/atomspace/Node.h>
51 
52 class AtomTableUTest;
53 
54 namespace opencog
55 {
60 typedef std::set<AtomPtr> AtomPtrSet;
61 
62 typedef boost::signals2::signal<void (const Handle&)> AtomSignal;
63 typedef boost::signals2::signal<void (const AtomPtr&)> AtomPtrSignal;
64 typedef boost::signals2::signal<void (const Handle&,
65  const AttentionValuePtr&,
67 typedef boost::signals2::signal<void (const Handle&,
68  const TruthValuePtr&,
70 
71 class AtomSpace;
72 
79 class AtomTable
80 {
81  friend class SavingLoading;
82  friend class ::AtomTableUTest;
83 
84 private:
85 
86  // Single, global mutex for locking the indexes.
87  // Its recursive because we need to lock twice during atom insertion
88  // and removal: we need to keep the indexes stable while we search
89  // them during add/remove.
90  static std::recursive_mutex _mtx;
91 
92  size_t size;
93 
94  // Holds all atoms in the table. Provides lookup between numeric
95  // handle uuid and the actual atom pointer (since they are stored
96  // together). To some degree, this info is duplicated in the Node
97  // and LinkIndex below; we have this here for convenience.
98  //
99  // This also plays a critical role for memory management: this is
100  // the only index that actually holds the atom shared_ptr, and thus
101  // increments the atom use count in a guaranteed fashion. This is
102  // the one true guaranteee that the atom will not be deleted while
103  // it is in the atom table.
104  std::unordered_set<Handle, handle_hash> _atom_set;
105 
112 
113  async_caller<AtomTable, AtomPtr> _index_queue;
116 
121  boost::signals2::connection addedTypeConnection;
122 
124  void typeAdded(Type);
125 
129 
132 
135 
136  // JUST FOR TESTS:
137  bool isCleared() const;
138 
146  bool inEnviron(AtomPtr);
148 
149  // The AtomSpace that is holding us (if any). Needed for DeleteLink operation
155  AtomTable& operator=(const AtomTable&);
156  AtomTable(const AtomTable&);
157 
158 public:
159 
163  AtomTable(AtomTable* parent = NULL, AtomSpace* holder = NULL);
164  ~AtomTable();
165  UUID get_uuid(void) { return _uuid; }
166  AtomSpace* getAtomSpace(void) { return _as; }
167 
171  size_t getSize() const;
172  size_t getNumNodes() const;
173  size_t getNumLinks() const;
174  size_t getNumAtomsOfType(Type type, bool subclass = true) const;
175 
185  Handle getHandle(Type, std::string) const;
186  Handle getHandle(const NodePtr&) const;
187 
188  Handle getHandle(Type, const HandleSeq&) const;
189  Handle getHandle(const LinkPtr&) const;
190  Handle getHandle(const AtomPtr&) const;
191  Handle getHandle(Handle&) const;
192 
193  static AtomPtr factory(Type atom_type, AtomPtr atom);
194 
195 public:
203  template <typename OutputIterator> OutputIterator
204  getHandlesByType(OutputIterator result,
205  Type type,
206  bool subclass = false,
207  bool parent = true) const
208  {
209  std::lock_guard<std::recursive_mutex> lck(_mtx);
210  if (parent && _environ)
211  _environ->getHandlesByType(result, type, subclass, parent);
212  return std::copy(typeIndex.begin(type, subclass),
213  typeIndex.end(),
214  result);
215  }
216 
218  template <typename Function> void
219  foreachHandleByType(Function func,
220  Type type,
221  bool subclass = false,
222  bool parent = true) const
223  {
224  std::lock_guard<std::recursive_mutex> lck(_mtx);
225  if (parent && _environ)
226  _environ->foreachHandleByType(func, type, subclass);
227  std::for_each(typeIndex.begin(type, subclass),
228  typeIndex.end(),
229  [&](Handle h)->void {
230  (func)(h);
231  });
232  }
233 
243  {
244  std::lock_guard<std::recursive_mutex> lck(_mtx);
245  return importanceIndex.getHandleSet(this, lowerBound, upperBound);
246  }
247 
256  {
257  if (a->_atomTable != this) return;
258  std::lock_guard<std::recursive_mutex> lck(_mtx);
259  importanceIndex.updateImportance(a.operator->(), bin);
260  }
261 
283  Handle add(AtomPtr, bool async);
284 
290  void barrier(void);
291 
295  bool holds(Handle& h) const {
296  return (NULL != h) and h->getAtomTable() == this;
297  }
298 
305  inline NodePtr getNode(Handle& h) const {
306  h = getHandle(h); // force resolution of uuid into atom pointer.
307  return NodeCast(h);
308  }
309 
316  inline LinkPtr getLink(Handle& h) const {
317  h = getHandle(h); // force resolution of uuid into atom pointer.
318  return LinkCast(h);
319  }
320 
336  AtomPtrSet extract(Handle& handle, bool recursive = true);
337 
341  Handle getRandom(RandGen* rng) const;
342 
345 
348 
351 };
352 
354 } //namespace opencog
355 
356 #endif // _OPENCOG_ATOMTABLE_H
boost::signals2::connection addedTypeConnection
Definition: AtomTable.h:121
NodeIndex nodeIndex
Definition: AtomTable.h:109
TVCHSigl & TVChangedSignal()
Definition: AtomTable.h:350
UnorderedHandleSet getHandlesByAV(AttentionValue::sti_t lowerBound, AttentionValue::sti_t upperBound=AttentionValue::MAXSTI) const
Definition: AtomTable.h:241
AtomSpace * getAtomSpace(void)
Definition: AtomTable.h:166
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
std::shared_ptr< Atom > AtomPtr
Definition: Handle.h:48
AtomTable * getAtomTable() const
Returns the AtomTable in which this Atom is inserted.
Definition: Atom.h:90
boost::signals2::signal< void(const Handle &, const TruthValuePtr &, const TruthValuePtr &)> TVCHSigl
Definition: AtomTable.h:69
std::shared_ptr< TruthValue > TruthValuePtr
Definition: TruthValue.h:85
std::shared_ptr< AttentionValue > AttentionValuePtr
std::unordered_set< Handle, handle_hash > _atom_set
Definition: AtomTable.h:104
AtomPtrSignal & removeAtomSignal()
Definition: AtomTable.h:344
static std::recursive_mutex _mtx
Definition: AtomTable.h:90
AtomTable(const AtomTable &)
Definition: AtomTable.cc:123
void barrier(void)
Definition: AtomTable.cc:615
Handle add(AtomPtr, bool async)
Definition: AtomTable.cc:394
bool holds(Handle &h) const
Definition: AtomTable.h:295
iterator begin(Type, bool) const
Definition: TypeIndex.cc:66
LinkIndex linkIndex
Definition: AtomTable.h:110
std::shared_ptr< Link > LinkPtr
Definition: Atom.h:53
TypeIndex typeIndex
Definition: AtomTable.h:108
AtomPtrSet extract(Handle &handle, bool recursive=true)
Definition: AtomTable.cc:667
AVCHSigl _AVChangedSignal
Definition: AtomTable.h:134
Handle getRandom(RandGen *rng) const
Definition: AtomTable.cc:648
static NodePtr NodeCast(const Handle &h)
Definition: Node.h:113
async_caller< AtomTable, AtomPtr > _index_queue
Definition: AtomTable.h:113
AVCHSigl & AVChangedSignal()
Definition: AtomTable.h:347
void updateImportanceIndex(AtomPtr a, int bin)
Definition: AtomTable.h:255
Handle getHandle(Type, std::string) const
Definition: AtomTable.cc:130
size_t getNumNodes() const
Definition: AtomTable.cc:625
boost::signals2::signal< void(const AtomPtr &)> AtomPtrSignal
Definition: AtomTable.h:63
short sti_t
short-term importance type
unsigned long UUID
UUID == Universally Unique Identifier.
Definition: Handle.h:46
boost::signals2::signal< void(const Handle &, const AttentionValuePtr &, const AttentionValuePtr &)> AVCHSigl
Definition: AtomTable.h:66
bool isCleared() const
Definition: AtomTable.cc:99
void foreachHandleByType(Function func, Type type, bool subclass=false, bool parent=true) const
Definition: AtomTable.h:219
LinkPtr getLink(Handle &h) const
Definition: AtomTable.h:316
size_t getNumAtomsOfType(Type type, bool subclass=true) const
Definition: AtomTable.cc:637
size_t getNumLinks() const
Definition: AtomTable.cc:631
static LinkPtr LinkCast(const Handle &h)
Definition: Link.h:263
AtomSpace * _as
Definition: AtomTable.h:150
AtomTable & operator=(const AtomTable &)
Definition: AtomTable.cc:117
boost::signals2::signal< void(const Handle &)> AtomSignal
Definition: AtomTable.h:62
AtomSignal _addAtomSignal
Definition: AtomTable.h:127
AtomSignal & addAtomSignal()
Definition: AtomTable.h:343
void updateImportance(Atom *, int)
std::shared_ptr< Node > NodePtr
Definition: Node.h:112
void put_atom_into_index(AtomPtr &)
Definition: AtomTable.cc:605
static AtomPtr factory(Type atom_type, AtomPtr atom)
Definition: AtomTable.cc:269
friend class SavingLoading
Definition: AtomTable.h:81
TVCHSigl _TVChangedSignal
Definition: AtomTable.h:131
bool inEnviron(AtomPtr)
Definition: AtomTable.cc:256
AtomTable * _environ
Definition: AtomTable.h:145
void typeAdded(Type)
Definition: AtomTable.cc:829
std::set< AtomPtr > AtomPtrSet
Definition: AtomTable.h:60
ImportanceIndex importanceIndex
Definition: AtomTable.h:111
iterator end(void) const
Definition: TypeIndex.cc:89
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40
std::unordered_set< Handle, handle_hash > UnorderedHandleSet
a hash that associates the handle to its unique identificator
Definition: Handle.h:250
UnorderedHandleSet getHandleSet(const AtomTable *, AttentionValue::sti_t, AttentionValue::sti_t) const
size_t getSize() const
Definition: AtomTable.cc:620
AtomPtrSignal _removeAtomSignal
Definition: AtomTable.h:128
OutputIterator getHandlesByType(OutputIterator result, Type type, bool subclass=false, bool parent=true) const
Definition: AtomTable.h:204
UUID get_uuid(void)
Definition: AtomTable.h:165
NodePtr getNode(Handle &h) const
Definition: AtomTable.h:305
static const sti_t MAXSTI