|
| 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