OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Pattern.h
Go to the documentation of this file.
1 /*
2  * Pattern.h
3  *
4  * Author: Linas Vepstas April 2015
5  *
6  * Copyright (C) 2015 Linas Vepstas <linasvepstas@gmail.com>
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_PATTERN_H
25 #define _OPENCOG_PATTERN_H
26 
27 #include <map>
28 #include <set>
29 #include <stack>
30 #include <unordered_map>
31 #include <vector>
32 
35 #include <opencog/atomspace/types.h> // for typedef Type
36 
37 namespace opencog {
38 
43 typedef std::map<Handle, const std::set<Type> > VariableTypeMap;
44 
50 struct Variables
51 {
65  std::set<Handle> varset;
67  std::map<Handle, unsigned int> index;
68 };
69 
75 struct Pattern
76 {
77  // Private, locally scoped typedefs, not used outside of this class.
78  // XXX TODO Replace by unordered multimap...
79  typedef std::vector<Handle> RootList;
80  typedef std::map<Handle, RootList> ConnectMap;
81  typedef std::pair<Handle, RootList> ConnectPair;
82 
83  // Each atom of the pattern as well as their corresponding pattern terms
84  // may appear in many clauses. Moreover the same atom may be replicated
85  // under the same clause root in many instances. Each occurence has its
86  // own unique PatternTermPtr. We need to keep the mapping beetwen atoms
87  // and clause roots to the list of atoms occurences. Typically the list
88  // of PatternTermPtr has single element, but as said above when given atom
89  // is replicated under one root then the list has many elements.
90  typedef std::map<std::pair<Handle,Handle>, PatternTermSeq> ConnectTermMap;
91 
92  // -------------------------------------------
93  // The current set of clauses (beta redex context) being grounded.
94  std::string redex_name; // for debugging only!
95 
98 
99  // The cnf_clauses are the clauses, but with the AbsentLink removed.
100  // This simplifies graph discovery, so that when they are found,
101  // they can be rejected (e.g. are not absent).
102  HandleSeq cnf_clauses; // AbsentLink removed!
103 
104  // The mandatory clauses must be grounded.
106 
107  // The optional clauses don't have to be grounded, but they might be.
108  // This is where the absent clauses are held, so e.g. if these do get
109  // grounded, they might be rejected (depending on the callback).
110  std::set<Handle> optionals; // Optional clauses
111 
112  // Black-box clauses. These are clauses that contain GPN's. These
113  // have to drop into scheme or python to get evaluated, which means
114  // that they will be slow. So, we leave these for last, so that the
115  // faster clauses can run first, and rule out un-needed evaluations.
116  std::set<Handle> black; // Black-box clauses
117 
118  // Evaluatable terms are those that hold a GroundedPredicateNode
119  // (GPN) in them, or are stand-ins (e.g. GreaterThanLink, EqualLink).
120  std::set<Handle> evaluatable_terms; // smallest term that is evaluatable
121  std::set<Handle> evaluatable_holders; // holds something evaluatable.
122 
123  // Execuatable terms are those that hold a GroundedSchemaNode (GSN)
124  // in them.
125  std::set<Handle> executable_terms; // smallest term that is executable
126  std::set<Handle> executable_holders; // holds something executable.
127 
128  // Maps; the value is the largest (evaluatable or executable)
129  // term containing the variable. Its a multimap, because
130  // a variable may appear in several different evaluatables.
131  std::unordered_multimap<Handle,Handle> in_evaluatable;
132  std::unordered_multimap<Handle,Handle> in_executable;
133 
134  // Any given atom may appear in one or more clauses. Given an atom,
135  // the connectivy map tells you what clauses it appears in. It
136  // captures how the clauses are connected to one-another, so that,
137  // after one clause is solved, we know what parts of the unsolved
138  // clauses already have a solution.
139  ConnectMap connectivity_map; // setup by make_connectivity_map()
140 
141  ConnectTermMap connected_terms_map; // setup by make_term_trees()
142 };
143 
145 } // namespace opencog
146 
147 #endif // OPENCOG_PATTERN_H
HandleSeq varseq
Definition: Pattern.h:64
std::string redex_name
Definition: Pattern.h:94
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
std::map< Handle, const std::set< Type > > VariableTypeMap
Definition: Pattern.h:43
std::set< Handle > evaluatable_holders
Definition: Pattern.h:121
std::set< Handle > varset
Definition: Pattern.h:65
std::set< Handle > evaluatable_terms
Definition: Pattern.h:120
std::map< Handle, RootList > ConnectMap
Definition: Pattern.h:80
HandleSeq cnf_clauses
Definition: Pattern.h:102
std::map< std::pair< Handle, Handle >, PatternTermSeq > ConnectTermMap
Definition: Pattern.h:90
std::vector< PatternTermPtr > PatternTermSeq
Definition: PatternTerm.h:37
std::map< Handle, unsigned int > index
Definition: Pattern.h:67
std::unordered_multimap< Handle, Handle > in_evaluatable
Definition: Pattern.h:131
HandleSeq mandatory
Definition: Pattern.h:105
std::set< Handle > executable_holders
Definition: Pattern.h:126
ConnectMap connectivity_map
Definition: Pattern.h:139
HandleSeq clauses
The actual clauses. Set by validate_clauses()
Definition: Pattern.h:97
std::set< Handle > black
Definition: Pattern.h:116
std::set< Handle > executable_terms
Definition: Pattern.h:125
std::set< Handle > optionals
Definition: Pattern.h:110
std::unordered_multimap< Handle, Handle > in_executable
Definition: Pattern.h:132
std::pair< Handle, RootList > ConnectPair
Definition: Pattern.h:81
VariableTypeMap typemap
Definition: Pattern.h:66
ConnectTermMap connected_terms_map
Definition: Pattern.h:141
std::vector< Handle > RootList
Definition: Pattern.h:79