Skip to content

Commit

Permalink
Added grsp dep.
Browse files Browse the repository at this point in the history
  • Loading branch information
PESchoenberg committed Dec 28, 2018
1 parent 3df0a68 commit c9b3045
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 22 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ constructing rule based systems using relational databases.

* Sqlite3 - ( https://www.sqlite.org/index.html )

* grsp - https://github.com/PESchoenberg/grsp.git


## Installation:

Expand Down
66 changes: 53 additions & 13 deletions examples/example1.scm
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@


(use-modules (dbi dbi))
(use-modules (grsp grsp0))
(use-modules (gexsys gexsys0))


Expand Down Expand Up @@ -161,27 +162,66 @@
; MAIN PROGRAM ----------------------------------------------------------------


; Start with a heading.
(newline)
(display "------------------------------------------------------------")
(newline)
(display "Example of a full reasoning iteration.")
(newline)


; These steps constitute a full reasoning iteration. If you loop them, you
; can obtain several different variants of reasoning processes.
; can obtain several different variants of reasoning processes. You may need
; to create functions to replace the last parameter p_f1 by function calls
; like in the case of kb-read-sen in order to get meaningful results on
; each step.
;
; Gexsys is essentially the Scheme version of an expert system designed for
; a semi - autonomous spacecraft. That system - called exsys - runs on an
; unique thread, separated from other processes In this thread, exsys works
; on a continupus loop in which several things take place:
;
; 1 - Data is gathered from the onboard sensors and stored into sde_facts; this
; is what function kb-read-sen does. i.e. current speed, current altitude ;
; as measured from a reference celestial body.

; 2 - Then, data coming from modules related to the user interface - human or
; machine are gathered and stored in sde_facts, on specific fact records, such as
; target or desired speed, target altitude, etc. with respect to a reference
; body that is usually the same as in paraghraph 1. This is achieved via
; external modules and then, kb-read-mode finishes the process by changing the
; Status flag on the recently-added item values.
;
; 3 - Then, rules are checked and executed if applicable. for example, once the
; kb has received a different value for target speed with respect to the current
; speed, rules concerning reference celestial bodies and thrust maneuvering
; might come into action in order to achieve the desired goals. This is the job
; of function kb-think, which reads the Condition field of each rule, sees if
; its conditions are applicable, and if so, then reads the Action field and
; executes it. This meas that values of items that correspond to actuators
; will be updated so that in the next step, data will be send to them and
; processes such as maneuvers will be performed. kb-think does not actually
; actuate the decisions taken, it just passes the relevant parameters to the
; data items that will be passed to the actuators.

; 4 - Finally. kb-write-act passes the data to the actuators. In the case of
; Gexsys, little is done at this stage, but nevertheless, the function updates
; the status of the kb.
;
; You may not need to perform all those steps, depending on the architecture of
; your particular system, or you may have to add additional ones, but now you
; know where does this particular architecture comes from.
;
; Also, notice that Gexsys uses SQL statement both as data stored in the kb and
; as program instructions. That is, data and and program statements are
; essentially the same thing, and as data can be modified on the fly, this
; means that you can add, delete or even modify the rules of your system while
; it is running, or the system can modify them by itself.
;
(ptit "=" 60 2 "Example1 - One single iteration of a full reasoning process.")
(kb-read-sen dbms kb1 (item10 dbms kb1))
(kb-read-mod dbms kb1 1)
(kb-think dbms kb1 1)
(kb-write-act dbms kb1 1)


; And then finish with a message.
(newline)
(display "Okay then...")
(newline)
; And then show all the columns or fields of sde_facts.
(kb-display-table dbms kb1 "SELECT * FROM sde_facts" "q")


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


74 changes: 66 additions & 8 deletions examples/example2.scm
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@


(use-modules (dbi dbi))
(use-modules (grsp grsp0))
(use-modules (gexsys gexsys0))


Expand Down Expand Up @@ -136,25 +137,82 @@
(kb-insert-rules dbms kb1 tb1 co st c a d p)


; Run a full cycle as king as mode-run equals 1. A change of the value of this
; variable depends entirely on the rules contained in the kb.


;/////////////////////
; This function will increase by ten the values of item-* items. Its goal is to
; show how you can provide a helper function to update table sde_facts with
; sensorial data. Of course, real functions might be far more complex than this one.
; For each step of the full reasining cycle you can create one such function to
; deal with specific issues related to the step in question. The only condition is
; that each such function must return the value one.
;
; Arguments:
; - p_dbms: database management system to be used.
; - p_kb1: knowledge base name.
;
(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-%'"))
(newline)
(let ((db-obj (dbi-open "sqlite3" p_kb1)))
(display sql-sen)
(newline)
(kb-query p_dbms p_kb1 sql-sen)
(dbi-close db-obj)
)
)
; Return the value one.
(* ret 1)
)
)


; MAIN PROGRAM ----------------------------------------------------------------


; Here we will run a loop with the same functions used on example1.scm, but
; with slightly different runes letting full control to the kb. The system
; assigns value 1 to fact mode-run, then that fact value passed from the kb
; to a variable named mode-run on the program, and that finally stops
; the loop and finishes the program. Consider that a change in mode-run depends
; entirely on a decision made by the system
;
; Please see the notes for example1.scm for additional information on the
; rationale for each step and function in a complete reasoning cycle.
;
(define mode-run 1)


(ptit "=" 60 2 "Example2 - A loop that repeats a full reasoning process")
(kb-setup-session dbms kb1)
(while (= mode-run 1)
(display "Working...")
(newline)
(ptit " " 1 1 "Working... mode-run still equals one.")

; First get thata from any sensors you might have (i.e. peripherals)
(kb-read-sen dbms kb1 1)

; Now exchange data with any modules, users, etc.
(kb-read-mod dbms kb1 1)

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

; Now data from sde_facts should be passed back to any actuators you
; might have
(kb-write-act dbms kb1 1)

; And finally we get the value of item mode-run and pass it to a
; of the same name.
(set! mode-run (kb-get-value-from-item dbms kb1 "sde_facts" "mode-run"))
)


; Fihish the program with a message.
(newline)
(display "Okay then...")
(newline)
; And then show all the facts and their values at the end of the loop.
(kb-display-table dbms kb1 "SELECT Item, Value FROM sde_facts" "Results of reasoning process: ")




Expand Down
81 changes: 80 additions & 1 deletion gexsys0.scm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

(define-module (gexsys gexsys0)
#:use-module (dbi dbi)
#:use-module (grsp grsp0)
#:export (kb-create
kb-create-sde-edges
kb-create-sde-facts
Expand All @@ -44,7 +45,8 @@
kb-think
kb-write-act
kb-setup-session-wr
kb-get-value-from-item))
kb-get-value-from-item
kb-display-table))


; kb-create - creates knowledge base.
Expand Down Expand Up @@ -479,6 +481,44 @@
; - 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 ((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))

; 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)
)
(dbi-close db-obj2)
)
(set! sql-res2 #f)
)
)
)
)
)
)
)
)

; 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 @@ -547,6 +587,45 @@
)


; kb-display-table - Displays the content of a table as defined by p_sql.
;
; Arguments:
; - p_dbms: database management system to be used.
; - p_kb1: knowledge base name.
; - p_sql: SQL query.
; - p_tit: title or header for listing.
; - "q": to use p_sql as value for p_tit.
;
(define (kb-display-table p_dbms p_kb1 p_sql p_tit)
(let ((sql-res #f))
(if (equal? p_tit "q")(set! p_tit p_sql))
(pline "-" 60)
(display p_tit)
(newline)
(newline)
(let ((db-obj (dbi-open "sqlite3" p_kb1)))
(dbi-query db-obj p_sql)
(display db-obj)
(newline)
(write (dbi-get_row db-obj))
(newline)
(set! sql-res (dbi-get_row db-obj))
(while (not (equal? sql-res #f))
(display sql-res)(newline)
(set! sql-res (dbi-get_row db-obj))
)
(display sql-res)
(newline)
(dbi-close db-obj)
(write (dbi-get_row db-obj))
(newline)
)
)
)







0 comments on commit c9b3045

Please sign in to comment.