-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdecentralized.scm
79 lines (65 loc) · 2.48 KB
/
decentralized.scm
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
;
; decentralized.scm
;
; Demo of decentralized operation, where the local AtomSpace is
; connected to two or more cogservers, trading data with each of them.
; This demo requires you to configure at least two cogservers on
; different machines, or to run two cogservers on the same machine,
; with different port numbers. See the cogserver docs for how to do
; this.
;
; ----------------------------------------------
; Getting started, making the initial connection.
;
(use-modules (opencog) (opencog persist))
(use-modules (opencog persist-cog))
(use-modules (opencog persist-cog-simple))
; Let's talk to the two machines in two different ways.
; The CogSimpleStorageNode uses the simple, single-threaded
; serialized communications channel; the CogStorageNode uses
; the parallelized, asynchronous channel.
(define csna (CogStorageNode "cog://192.168.1.1"))
(define csnb (CogSimpleStorageNode "cog://192.168.1.2"))
; Open the channel to each; by default, storage nodes are created
; with closed channels. This allows storage nodes to be created,
; when the remote end is not actually available, yet.
(cog-open csna)
(cog-open csnb)
; --------------
; Moving Atoms
;
; On server A, create an atom `(Concept "foo" (stv 0.9 0.2))`,
; then fetch it to here:
(fetch-atom (Concept "foo") csna)
; Push it out to server B:
(store-atom (Concept "foo") csnb)
; Verify, on server B, that the (Concept "foo") showed up, and has the
; correct TruthValue on it.
; --------------
; That's it! You've created a distributed AtomSpace! All of the other
; storage commands work the same way. For example, you can copy the
; entire atomspace from A to here, and from here to B:
(load-atomspace csna)
(store-atomspace csnb)
; --------------
; When database people say "eventual consistency", they mean there is
; no "universal truth". We can demo this explicitly.
; On server A, create an atom `(Concept "bar" (stv 0.1111 0.87777))`
; On server B, create an atom `(Concept "bar" (stv 0.67 0.99))`
; Ask each one in turn:
(fetch-atom (Concept "bar") csna)
(fetch-atom (Concept "bar") csnb)
(fetch-atom (Concept "bar") csna)
(fetch-atom (Concept "bar") csnb)
(fetch-atom (Concept "bar") csna)
(fetch-atom (Concept "bar") csnb)
; Note how the TV toggled with each fetch. Lets make things
; "eventually consistent":
(Concept "bar" (stv 0.3 0.4))
(store-atom (Concept "bar") csna)
(store-atom (Concept "bar") csnb)
; --------------
; We can be nice and shutdown cleanly:
(cog-close csna)
(cog-close csnb)
; --- The end. That's all folks! ----