Skip to content

Commit 7432572

Browse files
committed
Not compatible with prior versions.
1 parent 1966c89 commit 7432572

4 files changed

Lines changed: 359 additions & 178 deletions

File tree

examples/example1.scm

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
(use-modules (gexsys gexsys0))
4343

4444

45+
; Welcome.
46+
(ptit "=" 60 2 "Example1 - One single iteration of a full reasoning process.")
47+
48+
4549
; Vars and initial stuff.
4650
(define dbms "sqlite3")
4751
(define kb1 "example1.db")
@@ -52,11 +56,12 @@
5256
(define tb1 "sde_facts")
5357
(define tb2 "sde_mem_facts")
5458
(define it " ")
59+
(define f3 2)
5560

5661

5762
; Creation of the knowledge base. Note that this function also adds some
5863
; records in various data tables by default.
59-
(kb-create dbms kb1)
64+
(kb-create dbms kb1 f3)
6065

6166

6267
; Insertion of fact records. Notice that all values v for facts contained in
@@ -68,26 +73,26 @@
6873

6974
;Insert fact.
7075
(set! it "counter2")
71-
(kb-insert-facts dbms kb1 tb1 co st it v p)
72-
(kb-insert-facts dbms kb1 tb2 co st it v p)
76+
(kb-insert-facts dbms kb1 tb1 co st it v p f3)
77+
(kb-insert-facts dbms kb1 tb2 co st it v p f3)
7378

7479

7580
;Insert fact.
7681
(set! it "item-a")
77-
(kb-insert-facts dbms kb1 tb1 co st it v p)
78-
(kb-insert-facts dbms kb1 tb2 co st it v p)
82+
(kb-insert-facts dbms kb1 tb1 co st it v p f3)
83+
(kb-insert-facts dbms kb1 tb2 co st it v p f3)
7984

8085

8186
;Insert fact.
8287
(set! it "item-b")
83-
(kb-insert-facts dbms kb1 tb1 co st it v p)
84-
(kb-insert-facts dbms kb1 tb2 co st it v p)
88+
(kb-insert-facts dbms kb1 tb1 co st it v p f3)
89+
(kb-insert-facts dbms kb1 tb2 co st it v p f3)
8590

8691

8792
;Insert fact.
8893
(set! it "item-c")
89-
(kb-insert-facts dbms kb1 tb1 co st it v p)
90-
(kb-insert-facts dbms kb1 tb2 co st it v p)
94+
(kb-insert-facts dbms kb1 tb1 co st it v p f3)
95+
(kb-insert-facts dbms kb1 tb2 co st it v p f3)
9196

9297

9398
; Insertion of rules.
@@ -99,42 +104,42 @@
99104
(define c "SELECT Value FROM sde_facts WHERE Item = `item-a` AND Value = 0;")
100105
(define a "UPDATE sde_facts SET Value = ( ( SELECT Value FROM sde_facts WHERE Item = `counter2` ) + 1 ) WHERE Status = `applykbrules` AND Item = `counter2`;")
101106
(define d "2- If item-a = zero, then increment counter2.")
102-
(kb-insert-rules dbms kb1 tb1 co st c a d p)
107+
(kb-insert-rules dbms kb1 tb1 co st c a d p f3)
103108

104109

105110
; Insert rule #3.
106111
(define c "SELECT Value FROM sde_facts WHERE Item = `item-a` AND Value = 0;")
107112
(define a "UPDATE sde_facts SET Value = 1 WHERE Item = `item-a` AND Status = `applykbrules`;")
108113
(define d "3- If item-a = zero, then set its value to 1.")
109-
(kb-insert-rules dbms kb1 tb1 co st c a d p)
114+
(kb-insert-rules dbms kb1 tb1 co st c a d p f3)
110115

111116

112117
; Insert rule #4.
113118
(define c "SELECT Value FROM sde_facts WHERE Item = `item-a` AND Value = 1;")
114119
(define a "UPDATE sde_facts SET Value = 1 WHERE Item = `item-b` AND Status = `applykbrules`;")
115120
(define d "4- If item-a = 1, then set item-b value to 1.")
116-
(kb-insert-rules dbms kb1 tb1 co st c a d p)
121+
(kb-insert-rules dbms kb1 tb1 co st c a d p f3)
117122

118123

119124
; Insert rule #5.
120125
(define c "SELECT Value FROM sde_facts WHERE Item = `item-a` AND Value >= 1;")
121126
(define a "UPDATE sde_facts SET Value = ( ( SELECT Value FROM sde_facts WHERE Item = `item-c` ) * (-2) ) WHERE Item = `item-c` AND Status = `applykbrules`;")
122127
(define d "5- If item-a >= 1, then set item-c value to item-c * (-2).")
123-
(kb-insert-rules dbms kb1 tb1 co st c a d p)
128+
(kb-insert-rules dbms kb1 tb1 co st c a d p f3)
124129

125130

126131
; Insert rule #6.
127132
(define c "SELECT Value FROM sde_facts WHERE Item = `counter1` AND Value >= ( SELECT Value FROM sde_facts WHERE Item = `max-iter` );")
128133
(define a "UPDATE sde_facts SET Value = 0 WHERE Item = `mode-run` AND Status = `applykbrules`;")
129134
(define d "6- If counter1 reached the values specified for max-iter, then mode-run is set to zero in order to stop the cycle.")
130-
(kb-insert-rules dbms kb1 tb1 co st c a d p)
135+
(kb-insert-rules dbms kb1 tb1 co st c a d p f3)
131136

132137

133138
; Insert rule #4.
134139
(define c "SELECT Value FROM sde_facts WHERE Item = `item-a` AND Value >= 10;")
135140
(define a "UPDATE sde_facts SET Value = 2 WHERE Item = `max-iter` AND Status = `applykbrules`;")
136141
(define d "4- This rule controls dynamically the number of iterations.")
137-
(kb-insert-rules dbms kb1 tb1 co st c a d p)
142+
(kb-insert-rules dbms kb1 tb1 co st c a d p f3)
138143

139144

140145
; This function will increase by ten the values of item-* items. Its goal is to
@@ -153,9 +158,9 @@
153158
(let ((sql-sen "UPDATE sde_facts SET Value = ( ( SELECT Value FROM sde_facts WHERE Item LIKE 'item-%' ) + 10 ) WHERE Item LIKE 'item-%';"))
154159
(newline)
155160
(let ((db-obj (dbi-open "sqlite3" p_kb1)))
156-
(display sql-sen)
157-
(newline)
158-
(kb-query p_dbms p_kb1 sql-sen)
161+
;(display sql-sen)
162+
;(newline)
163+
(kb-query p_dbms p_kb1 sql-sen 0)
159164
(dbi-close db-obj)
160165
)
161166
)
@@ -221,19 +226,17 @@
221226
; a specific rule. However, notice that this does not imply that the whole
222227
; reasoning cycle implying data gathering, thinking and actuating is repeated.
223228
;
224-
(ptit "=" 60 2 "Example1 - One single iteration of a full reasoning process.")
225-
(kb-setup-session dbms kb1)
226-
(kb-read-sen dbms kb1 (item10 dbms kb1))
227-
(kb-read-mod dbms kb1 1)
228-
(kb-think dbms kb1 1)
229-
(kb-write-act dbms kb1 1)
230-
229+
(kb-setup-session dbms kb1 f3)
230+
(kb-read-sen dbms kb1 (item10 dbms kb1) f3)
231+
(kb-read-mod dbms kb1 1 f3)
232+
(kb-think dbms kb1 1 f3)
233+
(kb-write-act dbms kb1 1 f3)
231234

232235
; And then show all the columns or fields of sde_facts.
233-
(kb-display-table dbms kb1 "SELECT * FROM sde_facts" "q")
236+
(kb-display-table dbms kb1 "SELECT * FROM sde_facts" "sql")
234237

235238

236239
; Or we can see only some selected data.
237-
(kb-display-table dbms kb1 "SELECT Item, Value FROM sde_facts" "q")
240+
(kb-display-table dbms kb1 "SELECT Item, Value FROM sde_facts" "sql")
238241

239242

examples/example2.scm

Lines changed: 43 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
(use-modules (gexsys gexsys0))
4242

4343

44+
; Welcome.
45+
(ptit "=" 60 2 "Example2 - A loop that repeats a full reasoning process")
46+
47+
4448
; Vars and initial stuff.
4549
(define dbms "sqlite3")
4650
(define kb1 "example2.db")
@@ -51,11 +55,12 @@
5155
(define tb1 "sde_facts")
5256
(define tb2 "sde_mem_facts")
5357
(define it " ")
54-
58+
(define f3 2)
59+
(define i 0)
5560

5661
; Creation of the knowledge base. Note that this function also adds some
5762
; records in various data tables by default.
58-
(kb-create dbms kb1)
63+
(kb-create dbms kb1 f3)
5964

6065

6166
; Insertion of fact records. Notice that all values v for facts contained in
@@ -67,26 +72,26 @@
6772

6873
;Insert fact.
6974
(set! it "counter2")
70-
(kb-insert-facts dbms kb1 tb1 co st it v p)
71-
(kb-insert-facts dbms kb1 tb2 co st it v p)
75+
(kb-insert-facts dbms kb1 tb1 co st it v p f3)
76+
(kb-insert-facts dbms kb1 tb2 co st it v p f3)
7277

7378

7479
;Insert fact.
7580
(set! it "item-a")
76-
(kb-insert-facts dbms kb1 tb1 co st it v p)
77-
(kb-insert-facts dbms kb1 tb2 co st it v p)
81+
(kb-insert-facts dbms kb1 tb1 co st it v p f3)
82+
(kb-insert-facts dbms kb1 tb2 co st it v p f3)
7883

7984

8085
;Insert fact.
8186
(set! it "item-b")
82-
(kb-insert-facts dbms kb1 tb1 co st it v p)
83-
(kb-insert-facts dbms kb1 tb2 co st it v p)
87+
(kb-insert-facts dbms kb1 tb1 co st it v p f3)
88+
(kb-insert-facts dbms kb1 tb2 co st it v p f3)
8489

8590

8691
;Insert fact.
8792
(set! it "item-c")
88-
(kb-insert-facts dbms kb1 tb1 co st it v p)
89-
(kb-insert-facts dbms kb1 tb2 co st it v p)
93+
(kb-insert-facts dbms kb1 tb1 co st it v p f3)
94+
(kb-insert-facts dbms kb1 tb2 co st it v p f3)
9095

9196

9297
; Insertion of rules.
@@ -99,71 +104,35 @@
99104
(define c "SELECT Value FROM sde_facts WHERE Item = `counter1` AND Value = 0")
100105
(define a "UPDATE sde_facts SET Value = 1 WHERE Status = `applykbrules` AND Item = `max-iter`")
101106
(define d "1- On a certain value for counter1, set max-iter to a specified value.")
102-
(kb-insert-rules dbms kb1 tb3 co st c a d p)
107+
(kb-insert-rules dbms kb1 tb3 co st c a d p f3)
103108

104109

105110
; Insert rule.
106111
(define c "SELECT Value FROM sde_facts WHERE Item = `item-a` AND Value = 0")
107112
(define a "UPDATE sde_facts SET Value = ( ( SELECT Value FROM sde_facts WHERE Item = `counter2` ) + 1 ) WHERE Status = `applykbrules` AND Item = `counter2`")
108113
(define d "2- If item-a = zero, then increment counter2.")
109-
(kb-insert-rules dbms kb1 tb3 co st c a d p)
114+
(kb-insert-rules dbms kb1 tb3 co st c a d p f3)
110115

111116

112117
; Insert rule.
113118
(define c "SELECT Value FROM sde_facts WHERE Item = `item-a` AND Value = 1")
114119
(define a "UPDATE sde_facts SET Value = 1 WHERE Item = `item-b` AND Status = `applykbrules`")
115120
(define d "3- If item-a = 1, then set item-b value to 1.")
116-
(kb-insert-rules dbms kb1 tb3 co st c a d p)
121+
(kb-insert-rules dbms kb1 tb3 co st c a d p f3)
117122

118123

119124
; Insert rule.
120125
(define c "SELECT Value FROM sde_facts WHERE Item = `item-a` AND Value >= 1")
121126
(define a "UPDATE sde_facts SET Value = ( ( SELECT Value FROM sde_facts WHERE Item = `item-c` ) * (-2) ) WHERE Item = `item-c` AND Status = `applykbrules`")
122127
(define d "4- If item-a >= 1, then set item-c value to item-c * (-2).")
123-
(kb-insert-rules dbms kb1 tb3 co st c a d p)
124-
125-
126-
; Insert rule.
127-
(define c "SELECT Value FROM sde_facts WHERE Item = `counter1` AND Value >= 0")
128-
(define a "UPDATE sde_facts SET Value = ( ( SELECT Value FROM sde_facts WHERE Item = `counter1` ) + 1 ) WHERE Status = `applykbrules` AND Item = `counter1`")
129-
(define d "5- If counter1 >= zero, then increment counter1 (essentially, always increment counter1).")
130-
(kb-insert-rules dbms kb1 tb3 co st c a d p)
128+
(kb-insert-rules dbms kb1 tb3 co st c a d p f3)
131129

132130

133131
; Insert rule.
134132
(define c "SELECT Value FROM sde_facts WHERE Item = `counter2` AND Value > 2")
135133
(define a "UPDATE sde_facts SET Value = 0 WHERE Item = `mode-run` AND Status = `applykbrules`")
136-
(define d "6- Set mode-run = 0 if counter2 reaches a certain value.")
137-
(kb-insert-rules dbms kb1 tb3 co st c a d p)
138-
139-
140-
;/////////////////////
141-
; This function will increase by ten the values of item-* items. Its goal is to
142-
; show how you can provide a helper function to update table sde_facts with
143-
; sensorial data. Of course, real functions might be far more complex than this one.
144-
; For each step of the full reasining cycle you can create one such function to
145-
; deal with specific issues related to the step in question. The only condition is
146-
; that each such function must return the value one.
147-
;
148-
; Arguments:
149-
; - p_dbms: database management system to be used.
150-
; - p_kb1: knowledge base name.
151-
;
152-
(define (item10 p_dbms p_kb1)
153-
(let ((ret 1))
154-
(let ((sql-sen "UPDATE sde_facts SET Value = ( ( SELECT Value FROM sde_facts WHERE Item LIKE 'item-%' ) + 10 ) WHERE Item LIKE 'item-%'"))
155-
(newline)
156-
(let ((db-obj (dbi-open "sqlite3" p_kb1)))
157-
(display sql-sen)
158-
(newline)
159-
(kb-query p_dbms p_kb1 sql-sen)
160-
(dbi-close db-obj)
161-
)
162-
)
163-
; Return the value one.
164-
(* ret 1)
165-
)
166-
)
134+
(define d "5- Set mode-run = 0 if counter2 reaches a certain value.")
135+
(kb-insert-rules dbms kb1 tb3 co st c a d p f3)
167136

168137

169138
; MAIN PROGRAM ----------------------------------------------------------------
@@ -183,45 +152,57 @@
183152
; by item max-iter, like in example1.scm, and another one that depends on the
184153
; value of certain items - counter2 in this case.
185154
;
186-
; If you take a look at the rules for this example program, you will see that
155+
; If you take a look at the rules for this example program you will see that
187156
; like in the case of example1.scm, there is one that sets a value for max-iter
188157
; but there is also a rule that establishes that the "outer" loop that defines
189158
; the full reasoning cycle depends on the value of variable mode-run
190159
; as extracted from item mode-run (see below).
191160
;
192-
; You can define rules that modify the value of item mode run and consequently,
161+
; You can define rules that modify the value of item mode-run and consequently,
193162
; variable mode-run, as you desire. For example, the system may continue its
194163
; reasoning cycles until a sensor detects something, passes a value to the kb
195164
; and then the rules determine that mode-run now equals zero. You can of course
196165
; extract any number of values coming from any number of item records at any
197166
; time, like it is done here with mode-run.
198167
;
199-
; This is of course, a fairly trivial example thatt only shows how things may
168+
; This is of course, a fairly trivial example that only shows how things may
200169
; interact, but fairly complex reasoning systems can be built in this way. We
201170
; will leave that for other example programs.
202171
;
172+
; I have named Scheme variables that extract values from the kb using function
173+
; kb-get-value-from-item exaclty the same as the items from sde_facts from which
174+
; they extract the value - i.e. a Scheme variable mode-run for example would
175+
; extract the value of item mode-run contained in sde_facts, or a scheme
176+
; variable counter1 would extract the value from item counter1. You can of
177+
; of course use different names but in my opinion it is better to use the same
178+
; names because once you star extracting lots of values from items, it might
179+
; get confusing if the Scheme variables are named otherwise.
180+
;
203181
(define mode-run 1)
204182

205183

206-
(ptit "=" 60 2 "Example2 - A loop that repeats a full reasoning process")
207-
(kb-setup-session dbms kb1)
184+
(kb-setup-session dbms kb1 f3)
208185
(while (= mode-run 1)
209-
(ptit " " 1 1 "Working... mode-run still equals one.")
210186

187+
; This informs you about the iteration number. Can be useful.
188+
(set! i (+ i 1))
189+
(pline "+" 60)
190+
(pres "Iteration" (number->string i))
191+
211192
; First get data from any sensors you might have (i.e. peripherals)
212-
(kb-read-sen dbms kb1 1)
193+
(kb-read-sen dbms kb1 1 f3)
213194

214195
; Now exchange data with any modules, users, etc.
215-
(kb-read-mod dbms kb1 1)
196+
(kb-read-mod dbms kb1 1 f3)
216197

217198
; Now the system reads each rule contained in sde_rules and if the
218199
; SQL code foun in the Condition field delivers a valid result, then
219200
; the SQL code of the Action field will be applied as is.
220-
(kb-think dbms kb1 1)
201+
(kb-think dbms kb1 1 f3)
221202

222203
; Now data from sde_facts should be passed back to any actuators you
223204
; might have
224-
(kb-write-act dbms kb1 1)
205+
(kb-write-act dbms kb1 1 f3)
225206

226207
; And finally we get the value of item mode-run and pass it to a
227208
; of the same name.

0 commit comments

Comments
 (0)