OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PatternUtils.cc
Go to the documentation of this file.
1 /*
2  * PatternUtils.cc
3  *
4  * Copyright (C) 2008,2009,2011,2014 Linas Vepstas
5  *
6  * Author: Linas Vepstas <linasvepstas@gmail.com> February 2008
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 
25 #include "PatternUtils.h"
26 
27 using namespace opencog;
28 
29 namespace opencog {
30 
51 bool remove_constants(const std::set<Handle> &vars,
52  std::vector<Handle> &clauses)
53 {
54  bool modified = false;
55 
56  // Caution: this loop modifies the clauses list!
57  std::vector<Handle>::iterator i;
58  for (i = clauses.begin(); i != clauses.end(); )
59  {
60  Handle clause(*i);
61  if (any_unquoted_in_tree(clause, vars)
62  or contains_atomtype(clause, GROUNDED_PREDICATE_NODE)
63  or contains_atomtype(clause, GROUNDED_SCHEMA_NODE))
64  {
65  ++i;
66  }
67  else
68  {
69  i = clauses.erase(i);
70  modified = true;
71  }
72  }
73 
74  return modified;
75 }
76 
77 /* ======================================================== */
109 void get_connected_components(const std::set<Handle>& vars,
110  const HandleSeq& clauses,
111  std::vector<HandleSeq>& components,
112  std::vector<std::set<Handle>>& component_vars)
113 {
114  std::vector<Handle> todo(clauses);
115 
116  while (0 < todo.size())
117  {
118  // no_con_yet == clauses that failed to connect to any existing
119  // component.
120  std::vector<Handle> no_con_yet;
121  bool did_at_least_one = false;
122 
123  for (const Handle& cl: todo)
124  {
125  bool extended = false;
126 
127  // Which component might this possibly belong to??? Try them all.
128  size_t nc = components.size();
129  for (size_t i = 0; i<nc; i++)
130  {
131  std::set<Handle>& cur_vars(component_vars[i]);
132  // If clause cl is connected to this component, then add it
133  // to this component.
134  if (any_unquoted_in_tree(cl, cur_vars))
135  {
136  // Extend the component
137  components[i].push_back(cl);
138 
139  // Add to the varset cache for that component.
140  FindAtoms fv(vars);
141  fv.search_set(cl);
142  for (const Handle& v : fv.varset) cur_vars.insert(v);
143 
144  extended = true;
145  did_at_least_one = true;
146  break;
147  }
148  }
149 
150  if (not extended)
151  no_con_yet.push_back(cl);
152  }
153 
154  if (did_at_least_one)
155  {
156  todo = no_con_yet;
157  continue;
158  }
159 
160  // Grab the first clause that failed to attach to something,
161  // and use it to start a new component.
162  Handle ncl(no_con_yet.back());
163  no_con_yet.pop_back();
164  todo = no_con_yet;
165 
166  // If we are here, we found a disconnected clause.
167  // Start a new component
168  components.push_back({ncl});
169 
170  FindAtoms fv(vars);
171  fv.search_set(ncl);
172  component_vars.push_back(fv.varset);
173  }
174 }
175 
187 {
188  std::set<Handle> varset;
189 
190  std::function<void (const Handle&)> find_rec = [&](const Handle& h)
191  {
192  Type t = h->getType();
193  if (t == VARIABLE_NODE)
194  {
195  varset.insert(h);
196  return;
197  }
198 
199  if (classserver().isA(t, LAMBDA_LINK))
200  return;
201 
202  LinkPtr l(LinkCast(h));
203  if (l)
204  {
205  for (const Handle& oh : l->getOutgoingSet())
206  find_rec(oh);
207  }
208 
209  return;
210  };
211 
212  find_rec(tree);
213 
214  return HandleSeq(varset.begin(), varset.end());
215 }
216 
217 } // namespace opencog
218 
219 /* ===================== END OF FILE ===================== */
static bool contains_atomtype(const Handle &clause, Type atom_type)
Definition: FindUtils.h:329
std::set< Handle > varset
Definition: FindUtils.h:88
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
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
bool remove_constants(const std::set< Handle > &vars, std::vector< Handle > &clauses)
Definition: PatternUtils.cc:51
HandleSeq get_free_vars_in_tree(const Handle &tree)
void get_connected_components(const std::set< Handle > &vars, const HandleSeq &clauses, std::vector< HandleSeq > &components, std::vector< std::set< Handle >> &component_vars)
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
void search_set(const Handle &h)
Definition: FindUtils.h:128