OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Substitutor.h
Go to the documentation of this file.
1 /*
2  * Substitutor.h
3  *
4  * Copyright (C) 2015 OpenCog Foundation
5  *
6  * Author: William Ma <https://github.com/williampma>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License v3 as
10  * published by the Free Software Foundation and including the exceptions
11  * at http://opencog.org/wiki/Licenses
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program; if not, write to:
20  * Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #ifndef _OPENCOG_SUBSTITUTOR
25 #define _OPENCOG_SUBSTITUTOR
26 
27 
29 
30 
31 namespace opencog
32 {
33 
45 {
46 private:
48  const std::map<Handle, Handle> *_vmap;
49 
50  Handle walk_tree(const Handle& expr)
51  {
52  std::map<Handle,Handle>::const_iterator it = _vmap->find(expr);
53  if (_vmap->end() != it )
54  return it->second;
55 
56  LinkPtr lexpr(LinkCast(expr));
57 
58  // if not a link, and not mapped, just return it
59  if (not lexpr)
60  return Handle(expr);
61 
62  HandleSeq oset_results;
63  for (const Handle& h : lexpr->getOutgoingSet())
64  {
65  Handle hg = walk_tree(h);
66  oset_results.push_back(hg);
67  }
68 
69  // Now create a duplicate link with the substitution
70  return Handle(createLink(expr->getType(), oset_results, expr->getTruthValue()));
71  }
72 
73 public:
74  Substitutor(AtomSpace* as) : _as(as) {}
75 
83  Handle substitute(const Handle& expr, const std::map<Handle, Handle> &vars)
84  {
85  // throw, not assert, because this is a user error ...
86  if (Handle::UNDEFINED == expr)
87  throw InvalidParamException(TRACE_INFO,
88  "Asked to substitute a null expression");
89 
90  _vmap = &vars;
91 
92  // The returned handle is not yet in the atomspace. Add it now.
93  Handle hn = walk_tree(expr);
94  if (NULL != hn)
95  return _as->add_atom(hn);
96  return hn;
97  }
98 };
99 
100 }
101 
102 #endif // _OPENCOG_SUBSTITUTOR
103 
const std::map< Handle, Handle > * _vmap
Definition: Substitutor.h:48
#define createLink
Definition: Link.h:269
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
Substitutor(AtomSpace *as)
Definition: Substitutor.h:74
Type getType() const
Definition: Atom.h:197
std::shared_ptr< Link > LinkPtr
Definition: Atom.h:53
static const Handle UNDEFINED
Definition: Handle.h:77
Handle substitute(const Handle &expr, const std::map< Handle, Handle > &vars)
Definition: Substitutor.h:83
static LinkPtr LinkCast(const Handle &h)
Definition: Link.h:263
TruthValuePtr getTruthValue()
Definition: Atom.cc:104
Handle walk_tree(const Handle &expr)
Definition: Substitutor.h:50
Handle add_atom(AtomPtr atom, bool async=false)
Definition: AtomSpace.cc:100