OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PatternLink.h
Go to the documentation of this file.
1 /*
2  * opencog/atoms/PatternLink.h
3  *
4  * Copyright (C) 2015 Linas Vepstas
5  * All Rights Reserved
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License v3 as
9  * published by the Free Software Foundation and including the exceptions
10  * at http://opencog.org/wiki/Licenses
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program; if not, write to:
19  * Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #ifndef _OPENCOG_PATTERN_LINK_H
24 #define _OPENCOG_PATTERN_LINK_H
25 
26 #include <unordered_map>
27 
28 #include <opencog/query/Pattern.h>
31 
32 namespace opencog
33 {
34 
39 class PatternLink : public Link
75 {
76 protected:
77  // The link to be grounded.
79 
80  // The variables to be grounded
82 
83  // The pattern that is specified by this link.
85 
92  size_t _num_virts;
94 
95  size_t _num_comps;
96  std::vector<HandleSeq> _components;
97  std::vector<std::set<Handle>> _component_vars;
99 
100  void extract_variables(const HandleSeq& oset);
101  void init_scoped_variables(const Handle& hvar);
102  void unbundle_clauses(const Handle& body);
103  void validate_clauses(std::set<Handle>& vars,
104  HandleSeq& clauses);
105 
106  void extract_optionals(const std::set<Handle> &vars,
107  const std::vector<Handle> &component);
108 
109  void unbundle_virtual(const std::set<Handle>& vars,
110  const HandleSeq& clauses,
111  HandleSeq& concrete_clauses,
112  HandleSeq& virtual_clauses,
113  std::set<Handle>& black_clauses);
114 
115  void trace_connectives(const std::set<Type>&,
116  const HandleSeq& clauses);
117 
118  void make_connectivity_map(const HandleSeq&);
119  void check_connectivity(const std::vector<HandleSeq>&);
120  void make_map_recursive(const Handle&, const Handle&);
121 
122  void make_term_trees();
123  void make_term_tree_recursive(const Handle&, const Handle&,
124  PatternTermPtr&);
125 
126  void init(void);
127  void common_init(void);
128  void setup_components(void);
129 
130  // Only derived classes can call this
131  PatternLink(Type, const HandleSeq&,
134 
135  // utility debug print
136  static void prt(const Handle& h)
137  {
138  printf("%s\n", h->toShortString().c_str());
139  }
140 
141 public:
142  PatternLink(const HandleSeq&,
145 
146  PatternLink(const Handle& body,
149 
150  PatternLink(const Handle& varcdecls, const Handle& body,
153 
154  PatternLink(Link &l);
155 
156  // Used only to set up multi-component links.
157  // DO NOT call this!
158  PatternLink(const std::set<Handle>& vars,
159  const VariableTypeMap& typemap,
160  const HandleSeq& component,
161  const std::set<Handle>& optionals);
162 
163  // A backwards-compatibility constructor. Do not use.
164  PatternLink(const std::set<Handle>&,
165  const HandleSeq&);
166 
167  // Return the list of variables we are holding.
168  const Variables& get_variables(void) const { return _varlist; }
169 
170  const Handle& get_body(void) const { return _body; }
171 
172  // XXX temp hack till things get sorted out; remove this method
173  // later.
174  const Pattern& get_pattern(void) { return _pat; }
175 
176  bool satisfy(PatternMatchCallback&) const;
177 
178  void debug_print(void) const;
179 };
180 
181 typedef std::shared_ptr<PatternLink> PatternLinkPtr;
182 static inline PatternLinkPtr PatternLinkCast(const Handle& h)
183  { AtomPtr a(h); return std::dynamic_pointer_cast<PatternLink>(a); }
185  { return std::dynamic_pointer_cast<PatternLink>(a); }
186 
187 // XXX temporary hack ...
188 #define createPatternLink std::make_shared<PatternLink>
189 
191 }
192 
193 #endif // _OPENCOG_PATTERN_LINK_H
std::shared_ptr< PatternTerm > PatternTermPtr
Definition: PatternTerm.h:35
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
std::shared_ptr< Atom > AtomPtr
Definition: Handle.h:48
std::map< Handle, const std::set< Type > > VariableTypeMap
Definition: Pattern.h:43
std::shared_ptr< TruthValue > TruthValuePtr
Definition: TruthValue.h:85
std::shared_ptr< AttentionValue > AttentionValuePtr
virtual std::string toShortString(std::string indent="")=0
static TruthValuePtr DEFAULT_TV()
Definition: TruthValue.cc:52
static PatternLinkPtr PatternLinkCast(const Handle &h)
Definition: PatternLink.h:182
static AttentionValuePtr DEFAULT_AV()
to be used as default attention value
std::shared_ptr< PatternLink > PatternLinkPtr
Definition: PatternLink.h:181
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40