OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
opencog::PatternLink Class Reference

#include <PatternLink.h>

+ Inheritance diagram for opencog::PatternLink:
+ Collaboration diagram for opencog::PatternLink:

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 Variablesget_variables (void) const
 
const Handleget_body (void) const
 
const Patternget_pattern (void)
 
bool satisfy (PatternMatchCallback &) const
 
void debug_print (void) const
 
- Public Member Functions inherited from opencog::Atom
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())
 
- Protected Member Functions inherited from opencog::Atom
 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
 
- Protected Attributes inherited from opencog::Atom
UUID _uuid
 
AtomTable_atomTable
 
Type _type
 
char _flags
 
TruthValuePtr _truthValue
 
AttentionValuePtr _attentionValue
 
std::mutex _mtx
 
InSetPtr _incoming_set
 

Additional Inherited Members

- Protected Types inherited from opencog::Atom
typedef std::shared_ptr< InSetInSetPtr
 

Detailed Description

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.

Constructor & Destructor Documentation

PatternLink::PatternLink ( Type  t,
const HandleSeq hseq,
TruthValuePtr  tv = TruthValue::DEFAULT_TV(),
AttentionValuePtr  av = AttentionValue::DEFAULT_AV() 
)
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)
PatternLink::PatternLink ( const std::set< Handle > &  vars,
const VariableTypeMap typemap,
const HandleSeq compo,
const std::set< Handle > &  opts 
)
PatternLink::PatternLink ( const std::set< Handle > &  vars,
const HandleSeq clauses 
)

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.

Member Function Documentation

void PatternLink::check_connectivity ( const std::vector< HandleSeq > &  components)
protected

Check that all clauses are connected

Definition at line 679 of file PatternLink.cc.

void PatternLink::debug_print ( void  ) const
void PatternLink::extract_optionals ( const std::set< Handle > &  vars,
const std::vector< Handle > &  component 
)
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.

+ Here is the caller graph for this function:

void PatternLink::extract_variables ( const HandleSeq oset)
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().

+ Here is the caller graph for this function:

const Handle& opencog::PatternLink::get_body ( void  ) const
inline

Definition at line 170 of file PatternLink.h.

References _body.

const Pattern& opencog::PatternLink::get_pattern ( void  )
inline

Definition at line 174 of file PatternLink.h.

References _pat.

const Variables& opencog::PatternLink::get_variables ( void  ) const
inline

Definition at line 168 of file PatternLink.h.

References _varlist.

void PatternLink::init ( void  )
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().

+ Here is the caller graph for this function:

void PatternLink::init_scoped_variables ( const Handle hvar)
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.

+ Here is the caller graph for this function:

void PatternLink::make_connectivity_map ( const HandleSeq component)
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().

+ Here is the caller graph for this function:

void PatternLink::make_map_recursive ( const Handle root,
const Handle h 
)
protected

Definition at line 634 of file PatternLink.cc.

References _pat, opencog::Pattern::connectivity_map, and opencog::LinkCast().

+ Here is the caller graph for this function:

void PatternLink::make_term_tree_recursive ( const Handle root,
const Handle h,
PatternTermPtr parent 
)
protected

Definition at line 660 of file PatternLink.cc.

References _pat, opencog::Pattern::connected_terms_map, and opencog::LinkCast().

+ Here is the caller graph for this function:

void PatternLink::make_term_trees ( )
protected

Definition at line 651 of file PatternLink.cc.

References _pat, opencog::Pattern::cnf_clauses, and make_term_tree_recursive().

+ Here is the caller graph for this function:

static void opencog::PatternLink::prt ( const Handle h)
inlinestaticprotected

Definition at line 136 of file PatternLink.h.

References opencog::Atom::toShortString().

+ Here is the caller graph for this function:

void PatternLink::setup_components ( void  )
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.

+ Here is the caller graph for this function:

void PatternLink::trace_connectives ( const std::set< Type > &  connectives,
const HandleSeq oset 
)
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.

+ Here is the caller graph for this function:

void PatternLink::unbundle_clauses ( const Handle hbody)
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.

+ Here is the caller graph for this function:

void PatternLink::unbundle_virtual ( const std::set< Handle > &  vars,
const HandleSeq clauses,
HandleSeq fixed_clauses,
HandleSeq virtual_clauses,
std::set< Handle > &  black_clauses 
)
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.

+ Here is the caller graph for this function:

void PatternLink::validate_clauses ( std::set< Handle > &  vars,
HandleSeq clauses 
)
protected

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().

+ Here is the caller graph for this function:

Member Data Documentation

Handle opencog::PatternLink::_body
protected

Definition at line 78 of file PatternLink.h.

HandleSeq opencog::PatternLink::_component_patterns
protected

Definition at line 98 of file PatternLink.h.

std::vector<std::set<Handle> > opencog::PatternLink::_component_vars
protected

Definition at line 97 of file PatternLink.h.

std::vector<HandleSeq> opencog::PatternLink::_components
protected

Definition at line 96 of file PatternLink.h.

HandleSeq opencog::PatternLink::_fixed
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.

size_t opencog::PatternLink::_num_comps
protected

Definition at line 95 of file PatternLink.h.

size_t opencog::PatternLink::_num_virts
protected

Definition at line 92 of file PatternLink.h.

Pattern opencog::PatternLink::_pat
protected

Definition at line 84 of file PatternLink.h.

Variables opencog::PatternLink::_varlist
protected

Definition at line 81 of file PatternLink.h.

HandleSeq opencog::PatternLink::_virtual
protected

Definition at line 93 of file PatternLink.h.


The documentation for this class was generated from the following files: