OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AtomSpaceUtils.cc
Go to the documentation of this file.
1 /*
2  * AtomSpaceUtils.cc
3  *
4  * Copyright (C) 2014 OpenCog Foundation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License v3 as
8  * published by the Free Software Foundation and including the exceptions
9  * at http://opencog.org/wiki/Licenses
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program; if not, write to:
18  * Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #include <opencog/atomspace/atom_types.h>
23 #include <opencog/atomspace/Link.h>
24 #include "AtomSpaceUtils.h"
25 
26 namespace opencog {
27 
28 Handle add_prefixed_node(AtomSpace& as, Type t, const std::string& prefix)
29 {
30  static const char alphanum[] =
31  "0123456789"
32  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
33  "abcdefghijklmnopqrstuvwxyz";
34  static const int len = 8;
35  std::string name;
36  Handle result;
37  // Keep trying random suffixes until a non-existant name is generated.
38  do {
39  name = prefix;
40  for (int i = 0; i < len; ++i) {
41  name += alphanum[rand() % (sizeof(alphanum) - 1)];
42  }
43  result = as.get_handle(t, name);
44  } while (as.is_valid_handle(result));
45 
46  return as.add_node(t, name);
47 }
48 
50 {
51  LinkPtr link(LinkCast(h));
52 
53  // Recursive case
54  if (link) {
55  HandleSeq oset = link->getOutgoingSet();
56  bool success = as.remove_atom(h);
57  if (success)
58  for (const Handle& oh : oset)
59  success &= remove_hypergraph(as, oh);
60  return success;
61  }
62  // Base case
63  else {
64  return as.remove_atom(h);
65  }
66 }
67 
68 } // namespace opencog
Handle add_node(Type t, const std::string &name="", bool async=false)
Definition: AtomSpace.cc:135
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
Handle add_prefixed_node(AtomSpace &as, Type t, const std::string &prefix)
std::shared_ptr< Link > LinkPtr
Definition: Atom.h:53
bool remove_hypergraph(AtomSpace &as, Handle h)
bool remove_atom(Handle h, bool recursive=false)
Definition: AtomSpace.cc:344
Handle get_handle(Type t, const std::string &str)
Definition: AtomSpace.h:294
bool is_valid_handle(Handle h) const
Definition: AtomSpace.h:355
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