Getting Started with Atoms and the Scheme Shell
Contents
Theory
- OpenCog shell basics
- Scheme shell basics
- The basic ideas about Atoms from OpenCog Basics
The links above provide detailed descriptions of all topics in this chapter. What follows is a severely restricted selection from these pages. One should go back and explore those links to gain a deeper understanding of the topics introduced here.
Atomspace
Atomspace is a Knowledge Representation (KR) database and the associated query engine that is used to manipulate the data it stores. Data is stored in the form of hypergraphs. A graph is a collection of vertices (nodes) and edges (links), in which each edge connects two vertices. A hypergraph is a graph in which each edge can connect an arbitrary number of vertices. In Atomspace the vertices and edges are collectively called Atoms. So the Atomspace has hypergraphs that are made up of Atoms.
Atoms, Links and Nodes
Each Atom has a set of properties associated with it. Based on these atoms are classified into Links and Nodes.
Nodes: These are uniquely identified by the combination of name and type. So no two Nodes in an Atomspace can have both the same name and the same type. These atoms correspond the the vertices in the hypergraph.
Links: Links are uniquely identified by the combination of outgoing set(the atoms it points to) and type. These correspond to the edges in the hypergraph.
Practise
Making some atoms (scheme)
Scheme is a dialect of lisp. Lisp is a programming language from the days of yore. Now before you pack up and move to the python section, it is a really fun language. And learning it --at least for what you need to do in this section-- is easier than even python.
1. Starting the scheme shell: If you installed everything correctly then you should be able to open a scheme shell by typing guile in your terminal.
$ guile GNU Guile 2.0.9 Copyright (C) 1995-2013 Free Software Foundation, Inc. Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'. This program is free software, and you are welcome to redistribute it under certain conditions; type `,show c' for details. Enter `,help' for help. scheme@(guile-user)>
2. Loading opencog: Before we can start playing with Atoms we need to load the opencog modules in the scheme shell we just opened. To do this you need to type the following:
scheme@(guile-user)> (use-modules (ice-9 readline)) (activate-readline)
scheme@(guile-user)> (add-to-load-path "/usr/local/share/opencog/scm")
scheme@(guile-user)> (add-to-load-path ".")
scheme@(guile-user)> (use-modules (opencog))
scheme@(guile-user)> (use-modules (opencog query))
scheme@(guile-user)> (use-modules (opencog exec))
You can put this in your ~/.guile file (create one if none exists) to avoid typing this every time.
$ vim ~/.guile
(use-modules (ice-9 readline)) (activate-readline)
(add-to-load-path "/usr/local/share/opencog/scm")
(add-to-load-path ".")
(use-modules (opencog))
(use-modules (opencog query))
(use-modules (opencog exec))
You can also put these commands at the top of any .scm file and then run it by typing:
guile filename.scm
3. Creating Atoms(Nodes): At the scheme prompt you can create a ConceptNode by typing:
scheme@(guile-user)> (ConceptNode "HelloWorld")
$12 = (ConceptNode "HelloWorld")
There are several types of Nodes that you can create using a similar syntax. For an exhaustive list see this page. All nodes can be created by specifying a name and a type. When you create a node this way it is added to the atomspace that is automatically created when we load opencog in the scheme shell.
4. Creating Atoms(Links): Since Links are identified by their type and their outgoing set, we create them by specifying those two attributes.
scheme@(guile-user)> (InheritanceLink (ConceptNode "Fox") (ConceptNode "Animal"))
$13 = (InheritanceLink
(ConceptNode "Fox")
(ConceptNode "Animal")
)
Here the type of the link is InheritanceLink and the outgoing set consists of two ConceptNodes named "Fox" and "Animal" respectively. Again there are several types of links and you can look at the exhaustive list at the same page.
5. Querying the atomspace: Till now we have added Atoms to the Atomspace. This amounts to adding knowledge/data in the KR database. Now we will take the next step and do something with that knowledge/data. Let us start by just adding two numbers.
First we add two numbers to the Atomspace.
scheme@(guile-user)> (define num1 (NumberNode 3))
scheme@(guile-user)> (define num2 (NumberNode 5))
Now, we add a procedure in Atomspace that knows how to add the two numbers.
scheme@(guile-user)> (define link (PlusLink num1 num2))
Finally, we use the function cog-execute! to run this procedure. (More about this in the next section).
scheme@(guile-user)> (cog-execute! link)
$19 = (NumberNode "8.000000")