Adding New Atom Types

From OpenCog
Jump to: navigation, search

Custom Atom Types Example

This provides an example of how to add new custom atom types to the cogserver. Currently, the only way to add new custom types is to define them in an atom_types.script file, and then write some C++ code that causes the script file to be compiled, and the new types to be loaded. Fortunately, the C++ code is very easy to create: a simple cut-n-paste of the code here is enough.

The example files are here: ~/opencog/opencog/examples/atomtypes

The new atom types are loaded into the cogserver when the example module is loaded. To load this module and use the atom types, do the following:

Start the cogserver

First, navigate the opencog build directory in your opencog install ($ cd ~/opencog/opencog/build/) then run the cogserver (in the background by adding & to the end of the command) ./opencog/cogserver/server/cogserver -c ../lib/development.conf & - there should be some output (showing the cogserver process number and saying that it is listening on port 17001)

Listening on port 17001

Sanity check: See if the cogserver is running $ pgrep cogserver - the process number should be displayed (same as the one shown then the cogserver is started)

Connect to the server

We can connect to the cogserver by telnetting to it on port 17001 telnet localhost 17001 - output as such:
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

Load the modules

At the cogserver prompt ('opencog>'), issue the command: opencog> loadmodule examples/atomtypes/libcustomatomtypes.so You should see done printed on a line by itself.

At the same prompt issue the command listmodules - to verify that the module was loaded, you should see the following line in the output:

Filename: libcustomatomtypes.so, ID: opencog::CustomAtomTypesModule

Load the scheme file

Sanity check: During compilation, a scheme wrapper for the new atom types should have been created, in the build directory

./examples/atomtypes/example_types.scm


Now it's time to load the example_types scheme file. From a shell prompt (not from the cogserver prompt), pipe the example_types.scm into the cogserver: $ cat examples/atomtypes/example_types.scm | telnet localhost 17001

Use the new atom types. From the cogserver prompt, start the scheme shell opencog> scm (now you should have a 'guile>' prompt), and create some atoms:

   guile> (FooNode "asdf")
   (FooNode "asdf")

   guile> (define xxx (ListLink (FooNode "hee") (BarNode "haw")))

   guile> xxx
   (ListLink
      (FooNode "hee")
      (BarNode "haw")
   )

   guile>

That's all folks .. the rest is up to you.

Quiz

Issues

Unable to find libcustomtypes.so

Note that if libcustomtypes.io doesn't exist then its likely the examples haven't been built yet. Note the examples are not built by default when you build opencog. To build the examples, in the directory above execute: make examples or if you are in the examples directory simply execute: make.

Unable to telnet to cogserver

~/opencog/opencog/build$ telnet localhost 17001
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused

Most likely because of a build issue

Sanity Check: try lsof -i tcp:17001 - if working correctly it should return something like:
COMMAND    PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
cogserver <yourpid> <user>    9u  IPv4 16986332      0t0  TCP *:17001 (LISTEN)

if this returns no result, then there is no process running which has this port open (17001) Most likely it is because of an issue when building opencog.

Notes

Contact:(Yenat)

GIt https://github.com/opencog/opencog/tree/master/examples/atomtypes

  • We need a better example. Maybe, say, a ComplexNumberNode would be an OK toy example…