GetLink

From OpenCog
Jump to: navigation, search

The GetLink is used to specify a search pattern that can be satisfied with Atoms found in the AtomSpace. it is similar to BindLink, except that it does not perform any graph re-writing. it is similar to SatisfactionLink, except that, upon execution, it returns a set of atoms, instead of a truth value. It is similar to the SatisfyingSetLink, except that it is imperative: the GetLink commands: "go and search for all atoms that satisfy this predicate", whereas SatisfyingSetLink is declarative: "here is a set of atoms that satisfy this predicate".

GetLinks can be combined with PutLinks to implement graph rewriting. See link comparison for other related links.

Overview

The GetLink has the general form:

 GetLink
    <optional variable declarations and type restrictions>
    <clause containing variables>

Upon execution, with either the cog-execute! function or the cog-satisfying-set function, it returns the set of all possible groundings:

    SetLink
       <atoms that ground the variables>

When there is only one grounding, the SetLink is omitted, and the single grounding is returned. When there are multiple variables in the pattern, these are enclosed in a ListLink; if there is only one variable, the ListLink is omitted.

The GetLink is essentially identical to the SatisfyingSetLink, except that the SatisfyingSetLink is declarative, whereas the GetLink is imperative. That is, the GetLink is used to force a search, right now, of all of the things in the AtomSpace that satisfy it's conditions. By contrast, the SatisfyingSetLink is declarative, and can be used during (PLN) reasoning and/or Markov (Viterbi) parsing: viz: if something satisfies this, then I can conclude this other thing.

Type specifiers are allowed to explicitly bind the variables in a GetLink, such as TypedVariableLinks and VariableLists. Thus, for example, the following constrains variable A to be

  (GetLink
     (VariableList
         (TypedVariableLink
             (VariableNode "$A")
             (TypeNode "ConceptNode"))
         (VariableNode "$B"))
     (InheritanceLink
          (VariableNode "$A")
          (VariableNode "$B")))

By default, GetLink finds all free variables in an expression, and binds them. Thus, if you wish to specify type restrictions for one variable, then you must provide a variable binding for all of the relevant variables; else they will become unbound.

GetLink is fully implemented; the above example works today. Use cog-execute! to run it; see /examples/guile/simple.scm for an example.

Equivalence to BindLink

Observe that GetLink, as descried above, is completely equivalent to

 BindLink
     <clause containing free variables>
     ListLink
        <the variables>

Observe also that BindLink is completely equivalent to a GetLink and a PutLink combined. That is, the form

  BindLink
     <clauses containing variables>
     <re-write clauses>

is the exactly same thing as

  PutLink
     <re-write clauses>
     GetLink
        <clauses containing variables>

A concrete example should help to illustrate this. Consider the following BindLink:

BindLink
   WhizzyLink
       SomeAtom "widget"
       VariableNode "$x"
   FooBarLink
       SomeAtom "thing-a-ma-bob"
       VariableNode "$x"

When the above is executed, it will ind all possible groundings for the VariableNode $x, and it would then produce a SetLink containing a FooBarLink for each grounding. Exactly the same effect can be achieved by executing the following:

PutLink
   FooBarLink
       SomeAtom "thing-a-ma-bob"
       VariableNode "$x"
   GetLink
       WhizzyLink
           SomeAtom "widget"
           VariableNode "$x"
   

When the GetLink above is run, it will return a SetLink containing all possible groundings for the variable. The PutLink then takes this as an argument, substitutes those groundings for the variable in FooBarLink, and creates a bunch of FooBarLinks with the grounded values in them.