-
Notifications
You must be signed in to change notification settings - Fork 2
/
filesys.scm
190 lines (161 loc) · 6.56 KB
/
filesys.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
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
;
; filesys.scm -- filesystem demo
;
; Demo the introspection (reflection) API for file system navigation.
; The goal is to demonstrate how an agent can find out what an API
; looks like, for some sensory environment, and what the motor commands
; are, to move around in that environment. The "environment" here
; happens to be the file system. The motor commands correspond to
; a small subset of the traditional unix filesystem commands. An agent
; can discover what these are, by puttering around in the API. This demo
; shows how.
;
; Under construction. Uses ideas sketched in the README in
; opencog/atoms/irc/README.md
;
; As you go through this demo, you will notice how horribly complicated
; it is. The description of the sensory environment uses Sections with
; a bunch of Connectors on them, describing the actions and perceptions
; that are possible in this environment. The demo below attempts to
; manually parse (by hand!) all of these connectors, and hook things up
; to them. Ouch! Parsing is the job of Link Grammar. What we really want
; to do here is to have Link Grammar to take a look at the sensory
; environment, say "a hah, this is really easy: I see one 'word' with
; four disjuncts on it, let me hook some stuff up to those connectors"
; where the stuff being hooked up will be various sensory-processing
; 'words' or various action-taking 'words'.
;
; Anyway, that's the general plan. The details remain obscure.
;
(use-modules (opencog) (opencog exec) (opencog sensory))
(use-modules (srfi srfi-1))
; -----------------------------------------------------------
; Preliminary setup.
; Open filesystem node, and anchor it.
(cog-execute!
(SetValue
(Anchor "xplor") (Predicate "fsys")
(Open (Type 'FileSysStream) (Sensory "file:///tmp"))))
; In principle, the agent should do the above after discovery.
; For now, for this demo, just hard-code it, as above.
; -----------------------------------------------------------
; Demo the basic commands as a "human" would use them.
; These are, of course, hopelessly verbose for a human, but that
; is beside the point; the goal is to have a self-describing
; interface. In some programming languages, this is called
; "introspection" or "reflection".
(cog-execute! (Write (ValueOf (Anchor "xplor") (Predicate "fsys"))
(Item "pwd")))
(cog-execute! (Write (ValueOf (Anchor "xplor") (Predicate "fsys"))
(Item "ls")))
(cog-execute! (Write (ValueOf (Anchor "xplor") (Predicate "fsys"))
(List (Item "cd") (Item "file:///home"))))
(cog-execute! (Write (ValueOf (Anchor "xplor") (Predicate "fsys"))
(Item "ls")))
; --------------------------------------------------------
; Now lets look at how the API for the above is represented.
; This builds a crude stimulus-reponse pipeline in scheme.
; Ultimately, we want such pipes to be in Atomese, not scheme,
; but it is easier to poke at bit bits and pieces with scheme.
; Get the description of what this sensory device provides.
(define fsys-descr
(cog-execute! (Lookat (Type 'FileSysStream))))
; Print it. It should be a ChoiceLink of a bunch of sections.
fsys-descr
; Brute-force; just get the first choice. It's a Section with
; a ConnectorSeq on it.
(define some-cmd-descr (cog-value-ref fsys-descr 0))
; Take a look at what it is.
some-cmd-descr
; Pull the connector sequence out of it.
(define conn-seq (cog-value-ref some-cmd-descr 1))
; Convert the connector sequence to a scheme list.
(define conn-list (cog-value->list conn-seq))
; Brute-force walk over the connectors, and extract the ones
; that encode a command. These are the ones having (Sex "command").
; The sensory data have (Sex "reply") and we ignore this, here.
(define cmd-seq
(filter-map
(lambda (cnctr)
(if (equal? (gar cnctr) (Sex "command"))
(gdr cnctr)
#f))
conn-list))
; Now that we have all of the pieces, assemble a runnable command.
; This should have the form of (Write (ValueOf ...) (Item "ls"))
; because the Section for "ls" just happens to be the first Section
; returned in the ChoiceLink, and that Section also stated that
; a WriteLink should be used to perform that action. The location
; of the sensory stream, that we have to insert ourselves, as the
; first argument to Write.
(define some-cmd
(cog-execute!
(LinkSignature
(car cmd-seq)
(DontExec (ValueOf (Anchor "xplor") (Predicate "fsys")))
(cdr cmd-seq))))
; Take a look at what we got. Make sure it is in the expected form.
some-cmd
; Run it. This assumes the sensory stream was opened, earlier, with
; the OpenLink (near top of this file).
(cog-execute! some-cmd)
; --------------------------------------------------------
; Repeat the above, but this time in Atomese.
; This builds a crude stimulus-reponse pipeline.
; Under construction, broken.
; Lets take a look at the landscape, again
(cog-execute! (Lookat (Type 'FileSysStream)))
; The landscape arrives as a ChoiceLink, of possible things to do.
; This makes it awkward for later processing stages, so unwrap the
; ChoiceLink and discard it.
(cog-execute!
(Filter
(LinkSignature (Type 'Choice) (Glob "$x"))
(Lookat (Type 'FileSysStream))))
; Extract the command, and it's argument, and then the rest.
; The result is a simplified form of the description. We need
; the simplified form to built the processing pipe.
(cog-execute!
(Filter
(Section
(Type 'Item) ; Ignore whatever text is here.
(ConnectorSeq
(Connector
(Sex "command")
(Variable "$cmd")) ; Get the actual command
(Connector
(Sex "command")
(Variable "$arg")) ; Get the argument to the command
(Glob "$rest"))) ; Get the rest.
; The unwrappped ChoiceLink, from above.
(Filter
(LinkSignature (Type 'Choice) (Glob "$x"))
(Lookat (Type 'FileSysStream)))))
; Look for commands that do not require any additional arguments.
(cog-execute!
(Filter
(Section
(Type 'Item)
(ConnectorSeq
(Connector
(Sex "command")
(Variable "$cmd"))
(Connector
(Sex "command")
(Variable "$arg"))
(Connector
(Sex "reply")
(TypeCoInh 'Atom)))) ; also (Variable "$foo")
(Filter
(LinkSignature (Type 'Choice) (Glob "$x"))
(Lookat (Type 'FileSysStream)))))
; XXX Notice how painfully complicated all of the above is. Ugh!
; What we *really* want to do is the use Link Grammar to perform
; all of the connector matching for us, automatically. All that
; the above demos are doing is reinventing Link Grammar, but in
; Atomese. Yikes!
; --------------------------------------------------------
; Throws because wrong number of args. XXX FIXME
(cog-execute! (Lookat (Type 'TextFileStream)))
; --------------------------------------------------------
; The End! That's All, Folks!