OpenCog Framework
Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
|
#include <PatternLink.h>
Public Member Functions | |
PatternLink (const HandleSeq &, TruthValuePtr tv=TruthValue::DEFAULT_TV(), AttentionValuePtr av=AttentionValue::DEFAULT_AV()) | |
PatternLink (const Handle &body, TruthValuePtr tv=TruthValue::DEFAULT_TV(), AttentionValuePtr av=AttentionValue::DEFAULT_AV()) | |
PatternLink (const Handle &varcdecls, const Handle &body, TruthValuePtr tv=TruthValue::DEFAULT_TV(), AttentionValuePtr av=AttentionValue::DEFAULT_AV()) | |
PatternLink (Link &l) | |
PatternLink (const std::set< Handle > &vars, const VariableTypeMap &typemap, const HandleSeq &component, const std::set< Handle > &optionals) | |
PatternLink (const std::set< Handle > &, const HandleSeq &) | |
const Variables & | get_variables (void) const |
const Handle & | get_body (void) const |
const Pattern & | get_pattern (void) |
bool | satisfy (PatternMatchCallback &) const |
void | debug_print (void) const |
![]() | |
Link (Type t, const HandleSeq &oset, TruthValuePtr tv=TruthValue::DEFAULT_TV(), AttentionValuePtr av=AttentionValue::DEFAULT_AV()) | |
Link (Type t, const Handle &h, TruthValuePtr tv=TruthValue::DEFAULT_TV(), AttentionValuePtr av=AttentionValue::DEFAULT_AV()) | |
Link (Type t, const Handle &ha, const Handle &hb, TruthValuePtr tv=TruthValue::DEFAULT_TV(), AttentionValuePtr av=AttentionValue::DEFAULT_AV()) | |
Link (Type t, const Handle &ha, const Handle &hb, const Handle &hc, TruthValuePtr tv=TruthValue::DEFAULT_TV(), AttentionValuePtr av=AttentionValue::DEFAULT_AV()) | |
Link (Type t, const Handle &ha, const Handle &hb, const Handle &hc, const Handle &hd, TruthValuePtr tv=TruthValue::DEFAULT_TV(), AttentionValuePtr av=AttentionValue::DEFAULT_AV()) | |
Link (Link &l) | |
~Link () | |
Arity | getArity () const |
const HandleSeq & | getOutgoingSet () const |
Handle | getOutgoingAtom (Arity pos) const throw (RuntimeException) |
template<class T > | |
bool | foreach_outgoing (bool(T::*cb)(const Handle &), T *data) |
std::string | toString (std::string indent="") |
std::string | toShortString (std::string indent="") |
bool | isSource (Handle) const throw (InvalidParamException) |
bool | isSource (size_t) const throw (IndexErrorException, InvalidParamException) |
bool | isTarget (Handle) const throw (InvalidParamException) |
bool | isTarget (size_t) const throw (IndexErrorException, InvalidParamException) |
virtual bool | operator== (const Atom &) const |
virtual bool | operator!= (const Atom &) const |
![]() | |
virtual | ~Atom () |
Type | getType () const |
bool | isType (Type t, bool subclass) const |
Handle | getHandle () |
AttentionValuePtr | getAttentionValue () |
void | setAttentionValue (AttentionValuePtr) |
Sets the AttentionValue object of the atom. More... | |
AttentionValue::sti_t | getSTI () |
Handy-dandy convenience getters for attention values. More... | |
AttentionValue::lti_t | getLTI () |
AttentionValue::vlti_t | getVLTI () |
void | setSTI (AttentionValue::sti_t stiValue) |
void | setLTI (AttentionValue::lti_t ltiValue) |
void | incVLTI () |
void | decVLTI () |
TruthValuePtr | getTruthValue () |
void | setTruthValue (TruthValuePtr) |
Sets the TruthValue object of the atom. More... | |
void | merge (TruthValuePtr) |
Handle | tvmerge (TruthValuePtr tv) |
size_t | getIncomingSetSize () |
Get the size of the incoming set. More... | |
IncomingSet | getIncomingSet () |
template<typename OutputIterator > | |
OutputIterator | getIncomingSet (OutputIterator result) |
template<class T > | |
bool | foreach_incoming (bool(T::*cb)(const Handle &), T *data) |
template<typename OutputIterator > | |
OutputIterator | getIncomingSetByType (OutputIterator result, Type type, bool subclass=false) |
IncomingSet | getIncomingSetByType (Type type, bool subclass=false) |
Protected Member Functions | |
void | extract_variables (const HandleSeq &oset) |
void | init_scoped_variables (const Handle &hvar) |
void | unbundle_clauses (const Handle &body) |
void | validate_clauses (std::set< Handle > &vars, HandleSeq &clauses) |
void | extract_optionals (const std::set< Handle > &vars, const std::vector< Handle > &component) |
void | unbundle_virtual (const std::set< Handle > &vars, const HandleSeq &clauses, HandleSeq &concrete_clauses, HandleSeq &virtual_clauses, std::set< Handle > &black_clauses) |
void | trace_connectives (const std::set< Type > &, const HandleSeq &clauses) |
void | make_connectivity_map (const HandleSeq &) |
void | check_connectivity (const std::vector< HandleSeq > &) |
void | make_map_recursive (const Handle &, const Handle &) |
void | make_term_trees () |
void | make_term_tree_recursive (const Handle &, const Handle &, PatternTermPtr &) |
void | init (void) |
void | common_init (void) |
void | setup_components (void) |
The second half of the common initialization sequence. More... | |
PatternLink (Type, const HandleSeq &, TruthValuePtr tv=TruthValue::DEFAULT_TV(), AttentionValuePtr av=AttentionValue::DEFAULT_AV()) | |
![]() | |
Atom (Type t, TruthValuePtr tv=TruthValue::DEFAULT_TV(), AttentionValuePtr av=AttentionValue::DEFAULT_AV()) | |
void | keep_incoming_set () |
void | drop_incoming_set () |
void | insert_atom (LinkPtr) |
Add an atom to the incoming set. More... | |
void | remove_atom (LinkPtr) |
Remove an atom from the incoming set. More... | |
Static Protected Member Functions | |
static void | prt (const Handle &h) |
Protected Attributes | |
Handle | _body |
Variables | _varlist |
Pattern | _pat |
HandleSeq | _fixed |
size_t | _num_virts |
HandleSeq | _virtual |
size_t | _num_comps |
std::vector< HandleSeq > | _components |
std::vector< std::set< Handle > > | _component_vars |
HandleSeq | _component_patterns |
![]() | |
HandleSeq | _outgoing |
![]() | |
UUID | _uuid |
AtomTable * | _atomTable |
Type | _type |
char | _flags |
TruthValuePtr | _truthValue |
AttentionValuePtr | _attentionValue |
std::mutex | _mtx |
InSetPtr | _incoming_set |
Additional Inherited Members | |
![]() | |
typedef std::shared_ptr< InSet > | InSetPtr |
The PatternLink specifies an (optional) list of variables, and a pattern (containing those variables) that is to be grounded (satisfied). If no list of variables is explicltly specified, then the pattner is searched for all (free) variables, and these then become implicitly bound. When processed by the pattern matcher, these at the variables that get grounded.
The body of the PatternLink is assumed to collection of clauses to be satsified. Thus, the body is typically an AndLink, ChoiceLink or a SequentialAnd, depending on how they are to be satsified.
The PatternLink is used as a base class for GetLink, BindLink and SatisfactionLink. It provides all the methods and tools needed to unpack the clauses, extract variables, check for connectivity, discover the evaluatable terms, and so on. The pattern matcher needs all of these different parts unpakced into C++ structures so that it can directly access them during traversal. Thus, this class is really a "utility class", designed to pre-process everything that the pattern matcher will need to have later on.
Given the initial list of variables and clauses, the constructors extract the optional clauses and the dynamically-evaluatable clauses. This also computes the connectivity diagram of the clauses. Two clauses are considered to be pair-wise connected if they both contain a common shared variable, AND that shared variable does not appear in a "virtual link" (e.g. a GreaterThanLink, or an EvaluationLink with a GroundedPredicateNode in it). Virtual links cannot be directly grounded by the pattern matcher (because they don't actually "exist" as "real" atoms in the atomspace). Thus, the code here splits up the pattern into multiple connected components; the components themselves are connected only by virtual links.
The (cog-satisfy) scheme call can ground this link, and return a truth value.
Definition at line 74 of file PatternLink.h.
|
protected |
Definition at line 209 of file PatternLink.cc.
References init().
PatternLink::PatternLink | ( | const HandleSeq & | hseq, |
TruthValuePtr | tv = TruthValue::DEFAULT_TV() , |
||
AttentionValuePtr | av = AttentionValue::DEFAULT_AV() |
||
) |
Definition at line 188 of file PatternLink.cc.
References init().
PatternLink::PatternLink | ( | const Handle & | body, |
TruthValuePtr | tv = TruthValue::DEFAULT_TV() , |
||
AttentionValuePtr | av = AttentionValue::DEFAULT_AV() |
||
) |
Definition at line 195 of file PatternLink.cc.
PatternLink::PatternLink | ( | const Handle & | varcdecls, |
const Handle & | body, | ||
TruthValuePtr | tv = TruthValue::DEFAULT_TV() , |
||
AttentionValuePtr | av = AttentionValue::DEFAULT_AV() |
||
) |
Definition at line 202 of file PatternLink.cc.
PatternLink::PatternLink | ( | Link & | l | ) |
Definition at line 218 of file PatternLink.cc.
References opencog::classserver(), opencog::Atom::getType(), opencog::ClassServer::getTypeName(), and init().
PatternLink::PatternLink | ( | const std::set< Handle > & | vars, |
const VariableTypeMap & | typemap, | ||
const HandleSeq & | compo, | ||
const std::set< Handle > & | opts | ||
) |
Special constructor used only to make single concrete pattern components. We are given the pre-computed components; we only have to store them.
Definition at line 113 of file PatternLink.cc.
References _components, _fixed, _num_comps, _num_virts, _pat, _varlist, _virtual, opencog::Pattern::black, opencog::Pattern::clauses, opencog::Pattern::cnf_clauses, opencog::is_atom_in_tree(), make_connectivity_map(), make_term_trees(), opencog::Pattern::mandatory, opencog::Pattern::optionals, opencog::Pattern::redex_name, opencog::Variables::typemap, unbundle_virtual(), and opencog::Variables::varset.
Constructor that takes a pre-determined set of variables, and a list of clauses to solve. This is currently kind-of crippled, since no variable type restricions are possible, and no optionals, either. This is used only for backwards-compatibility API's.
Definition at line 176 of file PatternLink.cc.
References _pat, _varlist, opencog::Pattern::clauses, common_init(), setup_components(), and opencog::Variables::varset.
|
protected |
Check that all clauses are connected
Definition at line 679 of file PatternLink.cc.
|
protected |
Definition at line 38 of file PatternLink.cc.
References _component_vars, _components, _fixed, _num_comps, _num_virts, _pat, _varlist, _virtual, opencog::Pattern::black, opencog::Pattern::clauses, opencog::Pattern::cnf_clauses, extract_optionals(), opencog::get_connected_components(), make_connectivity_map(), make_term_trees(), trace_connectives(), unbundle_virtual(), validate_clauses(), and opencog::Variables::varset.
void PatternLink::debug_print | ( | void | ) | const |
Definition at line 702 of file PatternLink.cc.
References _pat, _varlist, opencog::Pattern::evaluatable_holders, opencog::Pattern::executable_holders, opencog::Pattern::mandatory, opencog::NodeCast(), opencog::Pattern::optionals, prt(), opencog::Pattern::redex_name, and opencog::Variables::varset.
|
protected |
Given the initial list of variables and clauses, separate these into the mandatory and optional clauses.
Definition at line 394 of file PatternLink.cc.
References _pat, opencog::Pattern::cnf_clauses, opencog::LinkCast(), opencog::Pattern::mandatory, opencog::Pattern::optionals, and python.undocumented.blocksworld::t.
|
protected |
Find and unpack variable declarations, if any; otherwise, just find all free variables.
On top of that initialize _body with the clauses of the PatternLink.
Definition at line 245 of file PatternLink.cc.
References _body, _varlist, opencog::FreeLink::get_vars(), and init_scoped_variables().
|
inline |
Definition at line 170 of file PatternLink.h.
References _body.
|
inline |
Definition at line 174 of file PatternLink.h.
References _pat.
|
inline |
Definition at line 168 of file PatternLink.h.
References _varlist.
|
protected |
Definition at line 99 of file PatternLink.cc.
References _body, opencog::Link::_outgoing, _pat, common_init(), extract_variables(), opencog::Pattern::redex_name, setup_components(), and unbundle_clauses().
|
protected |
Initialize _varlist given a handle of either VariableList or a variable.
Definition at line 279 of file PatternLink.cc.
References _varlist, opencog::VariableList::get_variables(), opencog::Link::getOutgoingSet(), opencog::Atom::getType(), opencog::LinkCast(), and python.undocumented.blocksworld::t.
|
protected |
Create a map that holds all of the clauses that a given atom participates in. In other words, it indicates all the places where an atom is shared by multiple trees, and thus establishes how the trees are connected.
This is used for only one purpose: to find the next unsolved clause. Perhaps this could be simplied somehow ...
Definition at line 613 of file PatternLink.cc.
References _pat, opencog::Pattern::cnf_clauses, opencog::Pattern::connectivity_map, and make_map_recursive().
Definition at line 634 of file PatternLink.cc.
References _pat, opencog::Pattern::connectivity_map, and opencog::LinkCast().
|
protected |
Definition at line 660 of file PatternLink.cc.
References _pat, opencog::Pattern::connected_terms_map, and opencog::LinkCast().
|
protected |
Definition at line 651 of file PatternLink.cc.
References _pat, opencog::Pattern::cnf_clauses, and make_term_tree_recursive().
|
inlinestaticprotected |
Definition at line 136 of file PatternLink.h.
References opencog::Atom::toShortString().
bool PatternLink::satisfy | ( | PatternMatchCallback & | pmcb | ) | const |
Definition at line 327 of file PatternMatch.cc.
References _component_patterns, _num_comps, _num_virts, _pat, PMCGroundings::_term_groundings, PMCGroundings::_var_groundings, _varlist, _virtual, dbgprt, debug_print(), opencog::PatternMatchCallback::initiate_search(), opencog::PatternLinkCast(), opencog::PatternMatch::recursive_virtual(), and opencog::PatternMatchCallback::set_pattern().
|
protected |
The second half of the common initialization sequence.
Definition at line 80 of file PatternLink.cc.
References _component_patterns, _component_vars, _components, _num_comps, _pat, _varlist, createPatternLink, opencog::Pattern::optionals, and opencog::Variables::typemap.
|
protected |
Starting from the top of a clause, trace down through the tree of connectives. If a term appears under a connective, and there is a path of connectives all the way to the top, then we have to assume the term is evaluatable, as the whole point of connectives to to connect evaluatable terms. Thus, for example, for a clause having the form (AndLink stuff (OrLink more-stuff (NotLink not-stuff))) we have to assume that stuff, more-stuff and not-stuff are all evaluatable.
Definition at line 588 of file PatternLink.cc.
References _pat, add_to_map(), opencog::Pattern::evaluatable_holders, opencog::Pattern::in_evaluatable, opencog::LinkCast(), and python.undocumented.blocksworld::t.
|
protected |
Unpack the clauses.
The predicate is either an AndLink of clauses to be satisfied, or a single clause. Other link types, such as OrLink and SequentialAnd, are treated here as single clauses; unpacking them here would lead to confusion in the pattern matcher. This is partly because, after unpacking, clauses can be grounded in an arbitrary order; thus, SequentialAnd's must be griounded and evaluated sequentially, and thus, not unpacked. In the case of OrLinks, there is no flag to say that "these are disjoined", so again, that has to happen later.
Definition at line 309 of file PatternLink.cc.
References _pat, opencog::Pattern::clauses, opencog::Atom::getType(), opencog::LinkCast(), and python.undocumented.blocksworld::t.
|
protected |
Sort out the list of clauses into four classes: virtual, evaluatable, executable and concrete.
A term is "evalutable" if it contains a GroundedPredicate, or if it inherits from VirtualLink (such as the GreaterThanLink). Such terms need evaluation at grounding time, to determine thier truth values.
A term may also be evaluatable if consists of connectives (such as AndLink, OrLink, NotLink) used to join together evaluatable terms. Normally, the above search for GPN's and VirtualLinks should be enough, expect when an entire term is a variable. Thus, for example, a term such as (NotLink (VariableNode $x)) needs to be evaluated at grounding-time, even though it does not currently contain a GPN or a VirtualLink: the grounding might contain it; We don't know yet. However, there's a gotcha: we don't yet know what the connectives are. The actual connectives depend on the callback; the default callback uses AndLink, OrLink, NotLink, but other callbacks may pick something else. Thus, we cannot do this here. By contrast, SatisfactionLink and BindLink explicitly assume the default callback, so they do the additional unbundling there.
A term is "executable" if it is an ExecutionOutputLink or if it inherits from one (such as PlusLink, TimesLink). Such terms need execution at grounding time, to determine thier actual form. Note that executable terms may frequently occur underneath evaluatable terms, e.g. if something is greater than the sum of two other things.
A clause is "virtual" if it has an evaluatable term inside of it, and that term has two or more variables in it. Such virtual clauses not only require evaluation, but an evaluation must be performed for each different variable grounding. Virtual clauses get a very different (and more complex) treatment from the pattern matcher.
Virtual cluases are hard for the pattern matcher in two different ways: first, any variables in them must be grounded before they can be evaluated, and that grounding has to occur before evaluation. Thus, non-virtual clasues must be grounded first. Another problem arises when a virtual clause has two or more variables in it, and those variables are grounded by different disconnected graph components; the combinatoric explosino has to be handled...
Definition at line 481 of file PatternLink.cc.
References _pat, add_to_map(), opencog::Pattern::evaluatable_holders, opencog::Pattern::evaluatable_terms, opencog::Pattern::executable_holders, opencog::Pattern::executable_terms, opencog::FindAtoms::holders, opencog::Pattern::in_evaluatable, opencog::Pattern::in_executable, opencog::FindAtoms::least_holders, opencog::num_unquoted_in_tree(), opencog::FindAtoms::search_set(), and opencog::FindAtoms::varset.
A simple validatation a collection of clauses for correctness.
Every clause should contain at least one variable in it; clauses that are constants and can be trivially discarded.
XXX WTF, what about evaluatables? How did those not get removed??? I don't get it... fixme
Definition at line 333 of file PatternLink.cc.
References opencog::is_unquoted_in_any_tree(), and opencog::remove_constants().
|
protected |
Definition at line 78 of file PatternLink.h.
|
protected |
Definition at line 98 of file PatternLink.h.
|
protected |
Definition at line 97 of file PatternLink.h.
|
protected |
Definition at line 96 of file PatternLink.h.
|
protected |
The graph components. Set by validate_clauses() "virtual" clauses are those that contain virtual links. "fixed" clauses are those that do not. The list of component_vars are the variables that appear in the corresponding component.
Definition at line 91 of file PatternLink.h.
|
protected |
Definition at line 95 of file PatternLink.h.
|
protected |
Definition at line 92 of file PatternLink.h.
|
protected |
Definition at line 84 of file PatternLink.h.
|
protected |
Definition at line 81 of file PatternLink.h.
|
protected |
Definition at line 93 of file PatternLink.h.