|
41 | 41 | (use-modules (gexsys gexsys0)) |
42 | 42 |
|
43 | 43 |
|
| 44 | +; Welcome. |
| 45 | +(ptit "=" 60 2 "Example2 - A loop that repeats a full reasoning process") |
| 46 | + |
| 47 | + |
44 | 48 | ; Vars and initial stuff. |
45 | 49 | (define dbms "sqlite3") |
46 | 50 | (define kb1 "example2.db") |
|
51 | 55 | (define tb1 "sde_facts") |
52 | 56 | (define tb2 "sde_mem_facts") |
53 | 57 | (define it " ") |
54 | | - |
| 58 | +(define f3 2) |
| 59 | +(define i 0) |
55 | 60 |
|
56 | 61 | ; Creation of the knowledge base. Note that this function also adds some |
57 | 62 | ; records in various data tables by default. |
58 | | -(kb-create dbms kb1) |
| 63 | +(kb-create dbms kb1 f3) |
59 | 64 |
|
60 | 65 |
|
61 | 66 | ; Insertion of fact records. Notice that all values v for facts contained in |
|
67 | 72 |
|
68 | 73 | ;Insert fact. |
69 | 74 | (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) |
72 | 77 |
|
73 | 78 |
|
74 | 79 | ;Insert fact. |
75 | 80 | (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) |
78 | 83 |
|
79 | 84 |
|
80 | 85 | ;Insert fact. |
81 | 86 | (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) |
84 | 89 |
|
85 | 90 |
|
86 | 91 | ;Insert fact. |
87 | 92 | (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) |
90 | 95 |
|
91 | 96 |
|
92 | 97 | ; Insertion of rules. |
|
99 | 104 | (define c "SELECT Value FROM sde_facts WHERE Item = `counter1` AND Value = 0") |
100 | 105 | (define a "UPDATE sde_facts SET Value = 1 WHERE Status = `applykbrules` AND Item = `max-iter`") |
101 | 106 | (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) |
103 | 108 |
|
104 | 109 |
|
105 | 110 | ; Insert rule. |
106 | 111 | (define c "SELECT Value FROM sde_facts WHERE Item = `item-a` AND Value = 0") |
107 | 112 | (define a "UPDATE sde_facts SET Value = ( ( SELECT Value FROM sde_facts WHERE Item = `counter2` ) + 1 ) WHERE Status = `applykbrules` AND Item = `counter2`") |
108 | 113 | (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) |
110 | 115 |
|
111 | 116 |
|
112 | 117 | ; Insert rule. |
113 | 118 | (define c "SELECT Value FROM sde_facts WHERE Item = `item-a` AND Value = 1") |
114 | 119 | (define a "UPDATE sde_facts SET Value = 1 WHERE Item = `item-b` AND Status = `applykbrules`") |
115 | 120 | (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) |
117 | 122 |
|
118 | 123 |
|
119 | 124 | ; Insert rule. |
120 | 125 | (define c "SELECT Value FROM sde_facts WHERE Item = `item-a` AND Value >= 1") |
121 | 126 | (define a "UPDATE sde_facts SET Value = ( ( SELECT Value FROM sde_facts WHERE Item = `item-c` ) * (-2) ) WHERE Item = `item-c` AND Status = `applykbrules`") |
122 | 127 | (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) |
131 | 129 |
|
132 | 130 |
|
133 | 131 | ; Insert rule. |
134 | 132 | (define c "SELECT Value FROM sde_facts WHERE Item = `counter2` AND Value > 2") |
135 | 133 | (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) |
167 | 136 |
|
168 | 137 |
|
169 | 138 | ; MAIN PROGRAM ---------------------------------------------------------------- |
|
183 | 152 | ; by item max-iter, like in example1.scm, and another one that depends on the |
184 | 153 | ; value of certain items - counter2 in this case. |
185 | 154 | ; |
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 |
187 | 156 | ; like in the case of example1.scm, there is one that sets a value for max-iter |
188 | 157 | ; but there is also a rule that establishes that the "outer" loop that defines |
189 | 158 | ; the full reasoning cycle depends on the value of variable mode-run |
190 | 159 | ; as extracted from item mode-run (see below). |
191 | 160 | ; |
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, |
193 | 162 | ; variable mode-run, as you desire. For example, the system may continue its |
194 | 163 | ; reasoning cycles until a sensor detects something, passes a value to the kb |
195 | 164 | ; and then the rules determine that mode-run now equals zero. You can of course |
196 | 165 | ; extract any number of values coming from any number of item records at any |
197 | 166 | ; time, like it is done here with mode-run. |
198 | 167 | ; |
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 |
200 | 169 | ; interact, but fairly complex reasoning systems can be built in this way. We |
201 | 170 | ; will leave that for other example programs. |
202 | 171 | ; |
| 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 | +; |
203 | 181 | (define mode-run 1) |
204 | 182 |
|
205 | 183 |
|
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) |
208 | 185 | (while (= mode-run 1) |
209 | | - (ptit " " 1 1 "Working... mode-run still equals one.") |
210 | 186 |
|
| 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 | + |
211 | 192 | ; 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) |
213 | 194 |
|
214 | 195 | ; Now exchange data with any modules, users, etc. |
215 | | - (kb-read-mod dbms kb1 1) |
| 196 | + (kb-read-mod dbms kb1 1 f3) |
216 | 197 |
|
217 | 198 | ; Now the system reads each rule contained in sde_rules and if the |
218 | 199 | ; SQL code foun in the Condition field delivers a valid result, then |
219 | 200 | ; 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) |
221 | 202 |
|
222 | 203 | ; Now data from sde_facts should be passed back to any actuators you |
223 | 204 | ; might have |
224 | | - (kb-write-act dbms kb1 1) |
| 205 | + (kb-write-act dbms kb1 1 f3) |
225 | 206 |
|
226 | 207 | ; And finally we get the value of item mode-run and pass it to a |
227 | 208 | ; of the same name. |
|
0 commit comments