Skip to content

Commit 79af849

Browse files
committed
Add a new, improved python example.
1 parent 5c3260b commit 79af849

File tree

2 files changed

+122
-11
lines changed

2 files changed

+122
-11
lines changed

examples/python/README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# Python Examples
22

3-
It is assumed Python 3 is the default version on your system. If you
4-
use Python 2 you will have to modify the commands and scripts
5-
accordingly.
63

74
* From the python prompt, the following should list the opencog python modules
85
```
@@ -18,9 +15,13 @@ accordingly.
1815

1916
* You can run the examples from your shell. For example,
2017
```
21-
python create_atoms_by_type.py
18+
python3 storage_tutorial.py
2219
```
23-
20+
21+
## storage_tutorial.py
22+
A relatively simple all-in-one tutorial introducing basic concepts,
23+
a practical example, and the use of the store-to-disk backend.
24+
2425
## create_atoms_by_type.py, create_atoms_simple.py
2526
Simple examples of how to create atoms in an atomspace. These
2627
demonstrate two different ways in which the API can be used; one
@@ -31,16 +32,17 @@ directly.
3132
Simple example to show how to access the outgoing set of a link.
3233

3334
## stop_go.py
34-
A simple example of a "behavior tree" implemented in the AtomSpace.
35+
A simple example of a "behavior tree" implemented in
36+
[Atomese](https://wiki.opencog.org/w/Atomese), the API for manipulating
37+
AtomSpace contents.
3538

36-
## Scheme through python.
39+
## Scheme through python
3740
The following examples show how to use the scheme bindings through python
3841

3942
### scheme/atom_type_names.py
40-
Example of how to obtain atom type names and atom type IDs.
41-
42-
### scheme/bindlink.py
43-
Example of how to use the pattern matcher BindLink functionality.
43+
The scheme Atomese bindings can be accessed from python, and vice-versa;
44+
the two languages can be freely intermixed. The examples below show how
45+
this can be done.
4446

4547
### scheme/scheme_timer.py
4648
Simple measurement of the performance of invoking the scheme (guile)
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#! /usr/bin/env python3
2+
#
3+
# storage_tutorial.py
4+
#
5+
"""
6+
A basic tutorial showing some of the basic concepts from the OpenCog
7+
AtomSpace.
8+
"""
9+
10+
from opencog.atomspace import AtomSpace
11+
from opencog.type_constructors import *
12+
from opencog.storage import *
13+
from opencog.storage_rocks import *
14+
15+
space = AtomSpace()
16+
set_default_atomspace(space)
17+
18+
# Record a photograph stored in a directory.
19+
#
20+
# This has the form of a labelled directed graph edge.
21+
# In ASCII graphics:
22+
#
23+
# "some edge label"
24+
# "from vertex" ------------------------> "to vertex"
25+
#
26+
# which in Atomese, becomes
27+
#
28+
# (Edge (Predicate "some edge label")
29+
# (List (Item "from vertex") (Item "to vertex")))
30+
#
31+
# and for python, the parens get re-arranged and commas are inserted:
32+
#
33+
# Edge (Predicate ("some edge label"),
34+
# List (Item ("from vertex"), Item ("to vertex")))
35+
#
36+
# Photographs are "stored" (or can be found at) URL locations.
37+
# The relationship between the URL location and the file name can
38+
# be indicated with an arrow. (This is one of many ways.)
39+
e = EdgeLink(
40+
# Here, "URL" is just some string. Any string will do.
41+
PredicateNode("URL"),
42+
ListLink(
43+
# The name of the directory with photos in it.
44+
ItemNode("file:///Home Computer/folders/My photo album"),
45+
46+
# The photo itself.
47+
ItemNode("Fantastic Sunset on Sunday.jpg")))
48+
49+
print("Here's your data:", e)
50+
51+
# -------------------------------------------
52+
53+
# AtomSpace contents can be loaded, stored and send over the net.
54+
# Below, a StorageNode is used save selected AtomSpace contents
55+
# to a RocksDB database. Rocks is nice, because it requires no
56+
# config to use.
57+
58+
# Create a RocksDB StorageNode, and open a connection to it.
59+
# The rocks:// URL specifies a directory in the local filesystem.
60+
storage = RocksStorageNode("rocks:///tmp/foo")
61+
cog_open(storage)
62+
63+
# Store the one and only edge created above.
64+
store_atom(e)
65+
66+
# Close the connection to storage.
67+
cog_close(storage)
68+
print("Closed the connection to storage")
69+
70+
# The rest of this demo is about restoring the Atom that was just saved.
71+
# To prove that this works, the AtomSpace will be cleared. To prove that
72+
# it was cleared, look at the contents before and after.
73+
74+
# Define a utility printer
75+
def prt_atomspace_contents(asp) :
76+
print("AtomSpace contains a total of " + str(len(space)) + " Atoms")
77+
if 0 < len(asp) :
78+
print("These are:")
79+
count = 0
80+
for atom in asp:
81+
count += 1
82+
print("Atom " + str(count) + ".... " + str(atom))
83+
84+
print("The AtomSpace before clearing:")
85+
prt_atomspace_contents(space)
86+
87+
print("\nWill now clear the AtomSpace.")
88+
space.clear()
89+
print("The AtomSpace size after clearing: ", len(space))
90+
prt_atomspace_contents(space)
91+
92+
# The clear clobbers the StorageNode; create it again.
93+
storage = RocksStorageNode("rocks:///tmp/foo")
94+
cog_open(storage)
95+
96+
print("Restore one atom: the file, whose name we magically know already.")
97+
fetch_atom(PredicateNode("URL"))
98+
prt_atomspace_contents(space)
99+
100+
print("Restore all Edges in storage.")
101+
fetch_incoming_set(PredicateNode("URL"))
102+
103+
# Close the connection to storage.
104+
cog_close(storage)
105+
106+
print("After restoring, the AtomSpace size is " + str(len(space)))
107+
prt_atomspace_contents(space)
108+
109+
print("Good bye!")

0 commit comments

Comments
 (0)