Repository navigation
Expand file tree
/
Copy pathstorage_tutorial.py
More file actions
executable file
·281 lines (245 loc) · 10.6 KB
/
Copy pathstorage_tutorial.py
File metadata and controls
executable file
·281 lines (245 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#! /usr/bin/env python3
#
# storage_tutorial.py
#
"""
Tutorial on how to use RocksStorageNode to save selectively save
individual Atoms or entire AtomSpaces to disk. This is a fairly basic
demo; the scheme files in this directory provide a larger variety of
examples. As the scheme examples are written in a direct fashion, it
should be fairly obvious how to port those examples to Python, as
desired.
"""
# Boilerplate
from opencog.atomspace import AtomSpace
from opencog.type_constructors import *
from opencog.storage_rocks import *
# AtomSpace cne be given a name; if not supplied, one is autogenerated.
space = AtomSpace("my basic space")
set_thread_atomspace(space)
# Basic Atomese tutorial: a labelled directed graph edge.
# In ASCII graphics:
#
# "some edge label"
# "from vertex" ------------------------> "to vertex"
#
# which in Atomese, becomes
#
# (Edge (Predicate "some edge label")
# (List (Item "from vertex") (Item "to vertex")))
#
# and for python, the parens get re-arranged and commas are inserted:
#
# Edge (Predicate ("some edge label"),
# List (Item ("from vertex"), Item ("to vertex")))
#
p = Predicate("My collection of URLs")
e = Edge(p,
List(
Item("file:///Home Computer/folders/My photo album"),
Item("Fantastic Sunset on Sunday.jpg")))
print("Here's your data:", e)
# To make the demo more fun, we'll also attach some static data to the
# Predicate. This is done by using set_value to specify a triplet:
# an Atom, a key, and the Value to attach at that key. The Atom can be
# any Atom, and so can the key. By convention, PredicateNodes are used
# for simple keys with simple string names. The Values can be any Value,
# which includes Atoms as a special case.
space.set_value(p, Predicate("floaty things"), FloatValue([1,2,3]))
space.set_value(p, Predicate("stringy thing"), StringValue(["a","bb","king"]))
space.set_value(p, Predicate("atomic thing"), Concept("gadget"))
# The list of keys is readily available:
KeysOf(p).execute()
# Values can be fetched individually:
ValueOf(p, Predicate("atomic thing")).execute()
ValueOf(p, Predicate("stringy thing")).execute()
ValueOf(p, Predicate("floaty things")).execute()
# Note that Values can be gotten directly, while sets must go through
# the space. This requirement comes into play when multiple AtomSpaces
# are in use. Such multiple spaces can be stacked on one-another, or
# kept disjoint; dispatching the setter through the space is required
# for proper copy-on-write semantics, atom hiding and other more
# advanced multi-space operations.
# -------------------------------------------
# AtomSpace contents can be stored and loaded from disk, and sent over
# the net (to other AtomSpaces). All such operations use exactly the
# same API, given by the StorageNode. This demo illustrates the API by
# using it with RocksStorageNode, which is built on top of the RocksDB
# disk database. Rocks is nice, because it requires no config to use.
# Create a RocksDB StorageNode.
# The rocks:// URL specifies a directory in the local filesystem.
storage = RocksStorage("rocks:///tmp/foo")
# Open a connection to storage using message-passing.
# The StorageNode API works by sending messages to StorageNodes via
# set_value(). All messages are PredicateNodes naming the operation.
space.set_value(storage, Predicate("*-open-*"), VoidValue())
# Store the one and only edge created above.
# (This uses pure Atomese style; see immediately below.)
SetValue(storage, Predicate("*-store-atom-*"), e).execute()
# This could have been done in the same way as before. However, pure
# Atomese style is an important part of working with Atomese, and so
# was illustrated above.)
space.set_value(storage, Predicate("*-store-atom-*"), e)
# Also store the predicate. Although it is implicitly stored, when the
# edge above is stored, it is stored without the attached key-value
# pairs. To store these, the Atom must be explicitly named.
SetValue(storage, Predicate("*-store-atom-*"), p).execute()
# Close the connection to storage.
space.set_value(storage, Predicate("*-close-*"), VoidValue())
# The entire contents of the AtomSpace could have been stored with
# just one simple command:
# space.set_value(storage, PredicateNode("*-store-atomspace-*"), space)
# This was not done, mostly just to show that one can also do granular
# save and restore.
# -------------------------------------------
# The rest of this demo is about restoring the Atom that was just saved.
# To prove that this works, the AtomSpace will be cleared. To prove that
# it was cleared, look at the contents before and after.
# Define a utility printer
def prt_atomspace_contents(asp) :
print("AtomSpace contains a total of " + str(len(asp)) + " Atoms")
if 0 < len(asp) :
print("These are:")
count = 0
for atom in asp:
count += 1
print("Atom " + str(count) + ".... " + str(atom))
print("The AtomSpace before clearing:")
prt_atomspace_contents(space)
print("\nWill now clear the AtomSpace.")
space.clear()
print("The AtomSpace size after clearing: ", len(space))
prt_atomspace_contents(space)
# -------------------------------------------
# The clear clobbers the StorageNode; create it again.
storage = RocksStorageNode("rocks:///tmp/foo")
space.set_value(storage, Predicate("*-open-*"), VoidValue())
# Bulk restore: load the entire AtomSpace from storage in one step.
# This restores everything, and is the easiest way to load data.
# space.set_value(storage, Predicate("*-load-atomspace-*"), space)
SetValue(storage, Predicate("*-load-atomspace-*"), space).execute()
# Close storage for now.
space.set_value(storage, Predicate("*-close-*"), VoidValue())
print("After bulk restore:")
prt_atomspace_contents(space)
# -------------------------------------------
# Bulk restore of everything can be impractical, if the dataset is
# large. In this case, individual Atoms, and even Values, can be loaded,
# one at a time, or in groups. Storae can even be queried (!); see the
# scheme examples for details; they port over easily to python.
#
# Clear and start over.
space.clear()
print("Cleared AtomSpace again, size is now:", len(space))
# Reopen storage
storage = RocksStorage("rocks:///tmp/foo")
space.set_value(storage, Predicate("*-open-*"), VoidValue())
# Restore one atom: the predicate, whose name "My collection" we already
# know, as if by magic. The fetch-atom message argument must be either
# a ListLink or a LinkValue containing the AtomSpace and the atom to
# fetch. Since Links are *always* stored in the AtomSpace, and Values
# are *never* stored in the AtomSpace, ths gives you some flexibility
# in how to represent the arguments. As a general rule, one should not
# pollute the AtomSpace with suprious transient temporary junk, and
# for this, the LinkValue is the obvious choice.
print("\nSelective fetch: restore just one atom by name.")
hurly = Predicate("My collection of URLs")
space.set_value(storage, Predicate("*-fetch-atom-*"),
LinkValue([space, hurly]))
# The requested Atom, and all of the Values attached to it, have been
# placed directly into the AtomSpace. That is why there is no need to
# say 'my_stuff = space.set_value(storage, ...)' -- everything goes
# straight into the current AtomSpace.
# Verify that all the key-value pairs were fetched:
KeysOf(hurly).execute()
for key in KeysOf(hurly).execute().to_list():
val = ValueOf(hurly, key).execute()
print(f"{key} points at {val}")
# Note that the keyes were also automatically placed in the AtomSpace:
prt_atomspace_contents(space)
# Restore all Edges held in storage, by fetching the incoming set.
space.set_value(storage, Predicate("*-fetch-incoming-set-*"),
LinkValue([space, url_pred]))
# Close the connection to storage.
space.set_value(storage, Predicate("*-close-*"), VoidValue())
print("\nAfter restorint the Incoming Set:")
prt_atomspace_contents(space)
print("Good bye!")
#
# The StorageNode API works via message-passing. Messages that have
# mandatory arugments are sent by saying
#
# `atomspace.set_value(storage, message, args)`.
#
# A small handful of messages do not require arguments; these work by
# saying
#
# result = `atomspace.get_value(storage, message)`.
#
# Messages are always PredicateNodes naming the operation.
#
# The StorageNode API is a message-passing API. It uses exactly the
# same generic 'atomspace.set_value(atom, key, value)' interface
# available for all Atoms. However, for StorageNodes (and more
# generally, for ObjectNodes, and more narrowly, for SensoryNodes)
# certain keys are interpreted as messages, requesting that some action
# be taken, rather than just static keys specifying a location.
#
# Such message-passing API's can be understood as a form of dynamic
# object-oriented programming: the messages can be thought of as being
# methods on a class instance, which are "called" when the message is
# is sent, whereas a plain key corresponds to a member or field: a named
# location on the class instance, where things can be stored.
#
# Common messages:
#
# * `*-open-*` and `*-close-*` to open/close storage connections.
# Args: VoidValue()
#
# * `*-store-atom-*` to store an atom and all its values.
# Args: the atom to store
#
# * `*-fetch-atom-*` to fetch an atom and all its values.
# Args: LinkValue([atomspace, atom])
#
# * `*-store-atomspace-*` and `*-load-atomspace-*` for bulk operations.
# Args: the AtomSpace
#
# * `*-fetch-incoming-set-*` to fetch all links containing an atom.
# Args: LinkValue([atomspace, atom])
#
# * `*-fetch-incoming-by-type-*` to fetch links of a specific type.
# Args: LinkValue([atomspace, atom, TypeNode("EdgeLink")])
#
# * `*-store-value-*` and `*-fetch-value-*` for individual values.
# Args: LinkValue([atom, key])
#
# * `*-update-value-*` performs an atomic read-modify-write (increment).
# Args: LinkValue([atom, key, delta])
#
# * `*-delete-*` and `*-delete-recursive-*` to remove atoms from storage.
# Args: LinkValue([atomspace, atom])
#
# * `*-barrier-*` for multi-thread synchronization.
# Args: atomspace
#
# * `*-connected?-*` to check connection status.
# Use: atomspace.get_value(storage, PredicateNode("*-connected?-*"))
#
# * `*-monitor-*` to get performance/debugging info.
# Use: atomspace.get_value(storage, PredicateNode("*-monitor-*"))
#
# See https://wiki.opencog.org/w/StorageNode for API documentation and
# longer description of the messages. A complete list of messages for
# a given StorageNode can be optained by saying
#
# MessagesOf(storage).execute()
#
# The validity of a message can be tested by saying
#
# IsMessage(Predicate("*-open-*")).execute()
# IsMessage(Predicate("foobar")).execute()
# IsMessage(Concept("*-dingdong-*")).execute()
#
# THE END.
# ---------------------------------------------------------------------