Link

From OpenCog

Jump to: navigation, search

Links are Atom types that connect other Atoms. A Link is uniquely defined by its type and its outgoing set, i.e., the set of Atoms linked by it. If a Link with the same type and outgoing set of a previously inserted Link is inserted in the AtomSpace, they are merged.

The type and outgoing set of the link cannot be changed after the node has been inserted into the AtomSpace: OpenCog atoms are immutable.

Scheme interface

Links can be created, manipulated and destroyed at the scheme shell command prompt. See scheme for details; cookbook for examples.

C++ programming interface

   class Link : public Atom {
       public:
          Link(Type, Arity, Handle*);
          Trail* getTrail();
          void setTrail(Trail *);
          
          vector<Handle> getOutgoingSet();
          void setOutgoingSet(vector<Handle>);
          string toString();
          string toShortString();
          bool isSource(Handle);
          bool isSource(int);
          bool isTarget(Handle);
          bool isTarget(int);
          bool equals(Link *);
   };

setOutgoingSet() should be called only in newly created Links. Changing the outgoing set of an Atom after it is inserted in the AtomTable will throw an exception. Once declared, the outgoing set is not meant to be changeable.

getTrail() and setTrail() are used by the reasoning module.

getWeight() returns a float representation for its TruthValue.

toString() and toShortString() return string representations of the link.

_isSource() _ and isTarget() are convenience methods which answer if a given Atom (or the Atom in position 'n' of Link's outgoing set) is source/target in the relationship represented by the Link.

equals() returns true if both links have the same type and the same elements in their outgoing sets.

getArity() returns the arity of the Link, i.e. the number of Atoms which participate on the relationship it represents. By definition, Nodes have arity = 0 while Links have arity > 0.

getOutgoingSet() returns the outgoing set of the Atom.

Incoming and Outgoing Sets

These are easiest to understand by example. Consider an AtomSpace formed by four Nodes A, B, C and D and four Links:

L1 A -> B
L2 A -> C
L3 D -> B
L4 L1 -> A

In the following table we have the incoming and the outgoing sets of each Atom:

Atom Incoming set Outgoing set
A {L1,L2,L4} null
B {L1,L3} null
C {L2} null
D {L3} null
L1 {L4} {A,B}
L2 null {A,C}
L3 null {D,B}
L4 null {L1,A}

The incoming set is stored inside the Atom in a single linked list while the outgoing set is stored in an array of Handles with 'arity' elements. The array and the linked list store the Handles and not the Atoms themselves.

Another way to explain this is by imagining an InheritanceLink between 'cat' and 'animal. In this link, the outgoing set is defined as the nodes that are connected by the link, so 'cat' and 'animal'. If you would then imagine another link to define the fact that 'Bob knows a cat is an animal', that link would be in the incoming set of the previously defined 'cat' inherits from 'animal' link.