OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
URECommons.cc
Go to the documentation of this file.
1 /*
2  * URECommons.cc
3  *
4  * Copyright (C) 2014 Misgana Bayetta
5  *
6  * Author: Misgana Bayetta <misgana.bayetta@gmail.com> Oct 2014
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 #include "URECommons.h"
24 
25 #include <opencog/util/macros.h>
28 
29 using namespace opencog;
30 
32 
33 Handle URECommons::create_bindLink(Handle himplicant, bool vnode_is_typedv)
34  throw (opencog::InvalidParamException) {
35  if (!LinkCast(himplicant)) {
36  throw InvalidParamException(TRACE_INFO, "Input must be a link type ");
37  } //xxx why?
38 
39  //if(vnode_is_typedv)
40  himplicant = replace_nodes_with_varnode(himplicant);
41 
42  UnorderedHandleSet variable_nodes = get_outgoing_nodes(himplicant, {
43  VARIABLE_NODE });
44  HandleSeq list_link_elem;
45 
46  // For searching ImplicationLinks with variables.
47  if (vnode_is_typedv) {
48  Handle h = _as.add_node(TYPE_NODE, "VariableNode");
49  for (Handle hvn : variable_nodes) {
50  Handle hi = _as.add_link(TYPED_VARIABLE_LINK, hvn, h);
51  list_link_elem.push_back(hi);
52  }
53  } else
54  list_link_elem.insert(list_link_elem.end(), variable_nodes.begin(),
55  variable_nodes.end());
56 
57  Handle var_listLink = _as.add_link(VARIABLE_LIST, list_link_elem);
58 
59  return _as.add_link(BIND_LINK, var_listLink, himplicant, himplicant);
60 }
61 
63  Type t /*=VARIABLE_NODE*/) {
64  UnorderedHandleSet hvars;
65  if (t == NODE)
66  hvars = get_outgoing_nodes(handle); // Get every node
67  else
68  hvars = get_outgoing_nodes(handle, { t });
69  map<Handle, Handle> node_unique_var_map;
70  for (Handle h : hvars)
71  node_unique_var_map[h] = _as.add_node(VARIABLE_NODE,
72  get_unique_name(h)); //TODO get_uuid is not implemented
73  return change_node_types(handle, node_unique_var_map);
74 }
75 
77 //xxx temporary implementation. need to be replaced by uuid generation for making sure name is always unique
78  string name = _as.get_name(h);
79  HandleSeq hs = _as.get_incoming(h);
80  if (!hs.empty())
81  name.append(to_string(hs[0].value()));
82  name.append("-bcgen");
83  return name;
84 }
85 
87  if (hlink == h) {
88  return true;
89  } else {
90  if (not LinkCast(hlink))
91  throw InvalidParamException(TRACE_INFO,
92  "Need a LINK type to look in");
93  auto outg = _as.get_outgoing(hlink);
94  if (find(outg.begin(), outg.end(), h) != outg.end())
95  return true;
96  else {
97  for (Handle hi : outg) {
98  if (LinkCast(hi) and exists_in(hi, h))
99  return true;
100  }
101  }
102  return false;
103  }
104 }
105 
107  map<Handle, Handle>& replacement_map) {
108  Handle hcpy;
109  if (LinkCast(h)) {
110  HandleSeq hs_cpy;
111  HandleSeq hs = _as.get_outgoing(h);
112  for (Handle hi : hs) {
113  if (NodeCast(hi)) {
114  if (replacement_map.find(hi) != replacement_map.end())
115  hs_cpy.push_back(replacement_map[hi]);
116  else
117  hs_cpy.push_back(hi);
118  } else if (LinkCast(hi)) {
119  hs_cpy.push_back(change_node_types(hi, replacement_map));
120  }
121  }
122  hcpy = _as.add_link(_as.get_type(h), hs_cpy);
123  hcpy->setTruthValue(_as.get_TV(h));
124  } else if (NodeCast(h)) {
125  if (replacement_map.find(h) != replacement_map.end())
126  hcpy = replacement_map[h];
127  else
128  hcpy = h;
129  }
130 
131  return hcpy;
132 }
133 
135  auto incoming = _as.get_incoming(h);
136  if (incoming.empty())
137  return;
138  else {
139  for (Handle hi : incoming) {
140  auto i = _as.get_incoming(hi);
141  if (i.empty()) {
142  if (find(parents.begin(), parents.end(), hi) == parents.end())
143  parents.push_back(hi);
144  } else {
145  get_root_links(hi, parents);
146  }
147  }
148  }
149 }
150 
152  TruthValuePtr ptv = _as.get_TV(h);
153  confidence_t c = ptv->getConfidence();
154  strength_t s = ptv->getMean();
155  return (pow((1 - s), FITNESS_PARAM) * (pow(c, (2 - FITNESS_PARAM))));
156 }
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 change_node_types(Handle &h, map< Handle, Handle > &replacement_map)
Definition: URECommons.cc:106
std::shared_ptr< TruthValue > TruthValuePtr
Definition: TruthValue.h:85
bool exists_in(Handle &hlink, Handle &h)
Definition: URECommons.cc:86
Handle create_bindLink(Handle himplicant, bool is_quoted=false)
Definition: URECommons.cc:33
float strength_t
void setTruthValue(TruthValuePtr)
Sets the TruthValue object of the atom.
Definition: Atom.cc:81
static NodePtr NodeCast(const Handle &h)
Definition: Node.h:113
float confidence_t
UnorderedHandleSet get_outgoing_nodes(const Handle &hinput, const std::vector< Type > &types)
Definition: AtomUtils.cc:156
static LinkPtr LinkCast(const Handle &h)
Definition: Link.h:263
AtomSpace & _as
Definition: URECommons.h:41
TruthValuePtr get_TV(Handle h) const
Definition: AtomSpace.h:787
URECommons(AtomSpace &as)
Definition: URECommons.cc:31
HandleSeq get_incoming(Handle h) const
Definition: AtomSpace.h:668
string get_unique_name(Handle &h)
Definition: URECommons.cc:76
void get_root_links(Handle h, HandleSeq &parents)
Definition: URECommons.cc:134
const std::string & get_name(Handle h) const
Definition: AtomSpace.h:688
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40
const float FITNESS_PARAM
Definition: URECommons.h:44
std::unordered_set< Handle, handle_hash > UnorderedHandleSet
a hash that associates the handle to its unique identificator
Definition: Handle.h:250
float tv_fitness(Handle h)
Definition: URECommons.cc:151
Handle add_link(Type t, const HandleSeq &outgoing, bool async=false)
Definition: AtomSpace.cc:175
Handle replace_nodes_with_varnode(Handle &himplication_link, Type t=VARIABLE_NODE)
Definition: URECommons.cc:62
Type get_type(Handle h) const
Definition: AtomSpace.h:781
const HandleSeq & get_outgoing(Handle h) const
Definition: AtomSpace.h:735