Skip to content

Commit

Permalink
Updated code.
Browse files Browse the repository at this point in the history
  • Loading branch information
PESchoenberg committed Jan 5, 2019
1 parent c9b3045 commit f76712e
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 66 deletions.
33 changes: 17 additions & 16 deletions examples/example1.scm
Original file line number Diff line number Diff line change
Expand Up @@ -97,37 +97,37 @@


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


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


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


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


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


Expand All @@ -144,7 +144,7 @@
;
(define (item10 p_dbms p_kb1)
(let ((ret 1))
(let ((sql-sen "UPDATE sde_facts SET Value = ( ( SELECT Value FROM sde_facts WHERE Item LIKE 'item-%' ) + 10 ) WHERE Item LIKE 'item-%'"))
(let ((sql-sen "UPDATE sde_facts SET Value = ( ( SELECT Value FROM sde_facts WHERE Item LIKE 'item-%' ) + 10 ) WHERE Item LIKE 'item-%';"))
(newline)
(let ((db-obj (dbi-open "sqlite3" p_kb1)))
(display sql-sen)
Expand Down Expand Up @@ -211,6 +211,7 @@
; it is running, or the system can modify them by itself.
;
(ptit "=" 60 2 "Example1 - One single iteration of a full reasoning process.")
(kb-setup-session dbms kb1)
(kb-read-sen dbms kb1 (item10 dbms kb1))
(kb-read-mod dbms kb1 1)
(kb-think dbms kb1 1)
Expand Down
34 changes: 16 additions & 18 deletions examples/example2.scm
Original file line number Diff line number Diff line change
Expand Up @@ -90,53 +90,51 @@


; Insertion of rules.
(define tb1 "sde_rules")
(define tb3 "sde_rules")
; (define tb2 "sde_mem_rules") ; We would need this only if rules are stored as programs.
(define co "prg0_0") ; Standard context value to indicate that a rule always resides on sde_rules and not sde_meme_rules.


; Insert rule.
(define c "SELECT Value FROM sde_facts WHERE Item = `counter1` AND Value = 0")
(define a "UPDATE sde_facts SET Value = 2 WHERE Status = `applykbrules` AND Item = `max-iter`")
(define d "On initial iteration, set max-iter to a specified value.")
(kb-insert-rules dbms kb1 tb1 co st c a d p)
(define a "UPDATE sde_facts SET Value = 3 WHERE Status = `applykbrules` AND Item = `max-iter`")
(define d "1 On initial iteration, set max-iter to a specified value.")
(kb-insert-rules dbms kb1 tb3 co st c a d p)


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


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


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


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


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


(define c "SELECT Value FROM sde_facts WHERE Item = `counter1` AND Value >= 0")
(define a "UPDATE sde_facts SET Value = ( ( SELECT Value FROM sde_facts WHERE Item = `counter1` ) + 1 ) WHERE Status = `applykbrules` AND Item = `counter1`")
(define d "6 If counter1 >= zero, then increment counter1 (essentially, always increment counter1).")
(kb-insert-rules dbms kb1 tb3 co st c a d p)


;/////////////////////
Expand Down
106 changes: 74 additions & 32 deletions gexsys0.scm
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
kb-write-act
kb-setup-session-wr
kb-get-value-from-item
kb-display-table))
kb-display-table
;kb-setup-session
))


; kb-create - creates knowledge base.
Expand Down Expand Up @@ -106,8 +108,8 @@
(kb-insert-facts p_dbms p_kb1 tb2 co st "max-iter" 1.0 1.0)

; Default records for sde_rules.
(set! c "SELECT Value FROM sde_facts WHERE Item = `counter1`")
(set! a "UPDATE sde_facts SET Value = ( ( SELECT Value FROM sde_facts WHERE Item = `counter1` ) + 1 ) WHERE Status = `applykbrules` AND Item = `counter1`")
(set! c "SELECT Value FROM sde_facts WHERE Item = `counter1`;")
(set! a "UPDATE sde_facts SET Value = ( ( SELECT Value FROM sde_facts WHERE Item = `counter1` ) + 1 ) WHERE Status = `applykbrules` AND Item = `counter1`;")
(set! d "Increase counter in one unit on each iteration.")
(kb-insert-rules p_dbms p_kb1 tb3 co st c a d 1.0)
)
Expand Down Expand Up @@ -431,11 +433,25 @@
; Delete left-overs from past sessions i.e. rules loaded from sde_prg_rules.
(kb-query p_dbms p_kb1 "DELETE FROM sde_rules WHERE Context NOT LIKE 'prg0_0';")

; These two updates are requred to replace some characters that are used
; to input SQL as data itself into an SQLite table, since both Scheme and
; SQLIte do a bit of a mess with special characters such as " ' and `. Thus
; after geetting SQL queries as data into the database using ` in some cases
; because " and ' cause conflicts, those ` characters must be replaced by
; ' characters, and that is what this section achieves. Remember that data
; introduced in fields Condition and Action in sde*rules tables using SQL
; statements are SQL statements themselves.
;
(kb-query p_dbms p_kb1 "UPDATE sde_rules SET Condition = REPLACE ( Condition, \"`\", \"'\");")
(kb-query p_dbms p_kb1 "UPDATE sde_rules SET Action = REPLACE ( Action, \"`\", \"'\");")
(kb-query p_dbms p_kb1 "UPDATE sde_prg_rules SET Condition = REPLACE ( Condition, \"`\", \"'\");")
(kb-query p_dbms p_kb1 "UPDATE sde_prg_rules SET Action = REPLACE ( Action, \"`\", \"'\");")

; Set the Value field of sde_facts to the default values contained in sde_mem_facts.
(kb-query p_dbms p_kb1"UPDATE sde_facts SET Value = COALESCE( ( SELECT sde_mem_facts.Value FROM sde_mem_facts WHERE ( sde_facts.Item = sde_mem_facts.Item AND sde_mem_facts.Status = 'enabled' AND sde_mem_facts.Context = 'prg0_0' ) ), 0);")
(kb-query p_dbms p_kb1 "UPDATE sde_facts SET Value = COALESCE( ( SELECT sde_mem_facts.Value FROM sde_mem_facts WHERE ( sde_facts.Item = sde_mem_facts.Item AND sde_mem_facts.Status = 'enabled' AND sde_mem_facts.Context = 'prg0_0' ) ), 0);")

; Set the Prob field of sde_facts to the default values contained in sde_mem_facts.
(kb-query p_dbms p_kb1"UPDATE sde_facts SET Prob = COALESCE( ( SELECT sde_mem_facts.Prob FROM sde_mem_facts WHERE ( sde_facts.Item = sde_mem_facts.Item AND sde_mem_facts.Status = 'enabled' AND sde_mem_facts.Context = 'prg0_0' ) ), 0);")
(kb-query p_dbms p_kb1 "UPDATE sde_facts SET Prob = COALESCE( ( SELECT sde_mem_facts.Prob FROM sde_mem_facts WHERE ( sde_facts.Item = sde_mem_facts.Item AND sde_mem_facts.Status = 'enabled' AND sde_mem_facts.Context = 'prg0_0' ) ), 0);")

; Initialize status for first cycle.
(kb-query p_dbms p_kb1 "UPDATE sde_facts SET Status = 'sentodb' WHERE Status != 'disabled';")
Expand Down Expand Up @@ -481,43 +497,70 @@
; - p_f3: control value for reasoning data function.
;
(define (kb-think p_dbms p_kb1 p_f3)
(let (( db-obj1 (dbi-open "sqlite3" "DGIIIAI.db")))
(let (( db-obj (dbi-open "sqlite3" p_kb1)))
(let ((sql-res1 #t))
(let ((sql-res2 #f))
(let ((condition " "))
(let ((action " "))
(let ((con " "))
(let ((act " "))
(dbi-query db-obj1 "SELECT Condition, Action from sde_rules WHERE Status = 'enabled'")
(set! sql-res1 (dbi-get_row db-obj1))
(while (not (equal? sql-res1 #f))
(set! sql-res1 (dbi-get_row db-obj1))
(let ((condition "c"))
(let ((action "a"))
(let ((con "c"))
(let ((act "a"))
(let ((i 0))
(dbi-query db-obj "SELECT Condition, Action from sde_rules WHERE Status = 'enabled'")
(set! sql-res1 (dbi-get_row db-obj))
(while (not (equal? sql-res1 #f))
; Get Condition and action into different string variables.
(set! con (list-ref sql-res1 0))
(set! act (list-ref sql-res1 1))
(set! condition (cdr con))
(set! action (cdr act))
(set! i (+ i 1))

; Get Condition and action into different string variables.
(set! con (list-ref sql-res1 0))
(set! act (list-ref sql-res1 1))
(set! condition (cdr (con)))
(set! action (cdr (act)))

; Test if condition applies. If it does, apply action.
(let ((db-obj2 (dbi-open "sqlite3" "DGIIIAI.db")))
(dbi-query db-obj2 condition)
(set! sql-res2 (dbi-get_row db-obj2))
(if (not (equal? sql-res2 #f))
(dbi-query db-obj2 action)
; Test if condition applies. If it does, apply action.
(newline)
(display "Step 1.4......................")
(newline)
(dbi-query db-obj condition)
(set! sql-res2 (dbi-get_row db-obj))

; Equiv to if.
(while (not (equal? sql-res2 #f))
(display "Step 1.4.1....................")
(newline)
(display condition)
(newline)
(write sql-res2)
(newline)
(display action)
(dbi-query db-obj action)
(newline)
(set! sql-res2 #f)
)
(newline)
(display "Step 1.5......................")
(set! sql-res2 #f)
(dbi-query db-obj "SELECT Condition, Action from sde_rules WHERE Status = 'enabled'")
(let ((j 0))
(while (not (equal? i j))
(set! sql-res1 (dbi-get_row db-obj))
(set! j (+ j 1))
)
)
(dbi-close db-obj2)
)
(set! sql-res2 #f)
)
)
(newlines 3)
)
)
)
)
)
)
)
)
(dbi-close db-obj)
)

;
(display "Step 2.......................")
(newline)

; Once all rules in sde_rules have been reviewed, change status.
(if (= p_f3 1)(kb-query p_dbms p_kb1 "UPDATE sde_facts SET Status = 'sentodb' WHERE Status = 'applykbrules';"))
)
Expand Down Expand Up @@ -628,4 +671,3 @@




0 comments on commit f76712e

Please sign in to comment.