Cookbook
From OpenCog
This guide indicates how to do specific tasks that one often comes across while programming with OpenCog. Of course there are usually multiple ways of doing the same thing, and the examples should display the best practice to use unless the situation specifically requires an alternative approach.
The tasks here are things that don't impact on the framework itself, instead they manipulate the data with an OpenCog instance, or describe how to use OpenCog as part of another system. To extend OpenCog (such as adding MindAgents, adding types of atoms, etc.) please look at the Framework Cookbook.
Contents |
CogServer
If one wants to create their own application one should inherit from CogServer::CogServer. Or access OpenCog through the CogServer.
(is this correct? Joel Pitt)
Creating a CogServer instance
The AtomSpace
Adding an atom
You can create an atom explicitly and then add it to the AtomSpace using the addRealAtom(Atom& a) method.
AtomSpace* atomSpace = CogServer::getAtomSpace(); Node a(CONCEPT_NODE, "ConceptNode1"); atomSpace->addRealAtom(a);
Adding a node
You can get the AtomSpace to directly create a Node and return a Handle.
AtomSpace* atomSpace = CogServer::getAtomSpace(); SimpleTruthValue tv1(0.5f, 0.99f); Handle h1 = atomSpace->addNode(CONCEPT_NODE, "ConceptNode1", tv1);
Adding a link
Similarly, you can get the AtomSpace to directly create a link and return a Handle to it.
vector<Handle> outgoingSet; AtomSpace* atomSpace = CogServer::getAtomSpace(); SimpleTruthValue tv1(0.5f, 0.99f); Handle h1 = atomSpace->addNode(CONCEPT_NODE, "ConceptNode1", tv1); outgoingSet.push_back(h1); Handle h2 = atomSpace->addNode(CONCEPT_NODE, "ConceptNode2", tv1); outgoingSet.push_back(h2); Handle link = atomSpace->addLink(INHERITANCE_LINK, outgoingSet, tv1);
Importing and exporting data
Scheme
from src/guile/Example
Below follows some examples for using scheme within OpenCog.
The scheme shell itself is reachable through the OpenCog shell.
Thus:
./cogserver & # start the opencog server telnet localhost 17001 # to get to the opencog shell opencog> scm # to enter the scheme shell
At this point, generic scheme expressions can be entered.
Consider the following OpenCog XML:
<InheritanceLink strength=".98" confidence=".99">
<Element class="ConceptNode" name="famous"/>
<Element class="ConceptNode" name="artist"/>
</InheritanceLink>
<ExtensionalImplicationLink strength=".99" confidence=".99">
<Element class="ConceptNode" name="Muhammad"/>
<Element class="ConceptNode" name="artist"/>
</ExtensionalImplicationLink>
<ExtensionalImplicationLink strength=".99" confidence=".99">
<Element class="ConceptNode" name="Britney"/>
<Element class="ConceptNode" name="artist"/>
</ExtensionalImplicationLink>
This can be represented in scheme several different ways. The most direct (but not the prettiest) way is as follows:
(cog-new-link 'InheritanceLink (cog-new-stv 0.98 0.99)
(cog-new-node 'ConceptNode "famous")
(cog-new-node 'ConceptNode "artist"))
(cog-new-link 'ExtensionalImplicationLink (cog-new-stv 0.99 0.99)
(cog-new-node 'ConceptNode "Muhammad")
(cog-new-node 'ConceptNode "artist"))
(cog-new-link 'ExtensionalImplicationLink (cog-new-stv 0.99 0.99)
(cog-new-node 'ConceptNode "Britney")
(cog-new-node 'ConceptNode "artist"))
Enter the above at the scheme shell prompt, and then exit the shell (with a single '.' followed by newline). Then, at the opencog shell, type 'ls'; this will reveal what was created.
The above can be prettied up with some "syntactic sugar." First, define some wrapper functions (in the future, similar wrapper functions might be automatically included; however, these need to be manually entered at this time):
(define (stv x y) (cog-new-stv x y))
(define (ConceptNode . x)
(apply cog-new-node (append (list 'ConceptNode) x)))
(define (InheritanceLink . x)
(apply cog-new-link (append (list 'InheritanceLink) x)))
(define (ExtensionalImplicationLink . x)
(apply cog-new-link (append (list 'ExtensionalImplicationLink) x)))
After the above, the rather sweeter form is possible:
(InheritanceLink (stv 0.98 0.99)
(ConceptNode "famous")
(ConceptNode "artist"))
(ExtensionalImplicationLink (stv 0.99 0.99)
(ConceptNode "Muhammad")
(ConceptNode "artist"))
(ExtensionalImplicationLink (stv 0.99 0.99)
(ConceptNode "Britney")
(ConceptNode "artist"))
The simple truth values can appear either before, or after the nodes, thus:
(ExtensionalImplicationLink (ConceptNode "Britney") (ConceptNode "artist") (stv 0.99 0.99))
will also work.
Note: OpenCog shell uses generic TCP/IP sockets for its I/O, and so you can issues commands from a file, or from a process. So, for example, if you create a file, say, "some-atoms.scm", which contains something like this:
scm (cog-new-node 'ConceptNode "blah-blah-blah")
and then, you go:
cat some-atoms.scm | telnet localhost 17001
then the atom will be created. Of course, anything that spits out scheme to stdout will work -- so for example, if you have a file stuff.c which contains:
#include <stdio.h>
main () {
printf ("scm\n");
printf ("(cog-new-node 'ConceptNode \"blah-blah-blah\")\n");
printf(".\n");
}
and then
cc stuff.c -o stuff ./stuff | telnet localhost 17001
will also create the atom(s).
If you're picky, use netcat or socat instead of telnet.
OpenCog XML (formerly NM-XML)
- Importing
You can send opencog xml to the opencog shell. Just issue 'data' first, and you'll get a shell that accepts only nmxml. Use ^D to leave the shell.
- Exporting

