Ashik’s IT Thoughts

February 9, 2007

Jess & Rule Based Systems

Filed under: Java — ashikuzzaman @ 3:25 am

Recently I worked on JESS, stands for Java Expert System Shell, in Philips Motiva project which is a remote patient monitoring platform. We use Jess for decision making support in Motiva to make it an intelligent system. I can’t think of learning JESS without reading its creator Ernest Friedman-Hill‘s book JESS In Action. Ernest is also a moderator of JavaRanch. There are alternate rule engines available – Mandarax, QuickRules, Drools, ofbiz, JRules, Haley Rules Products, Common Knowledge.

While working with JESS, I have developed a sample prototype to use the basic language syntaxes of JESS. The program is as follows. It shows usage of basic Jess language sytanxes, if/then/else, while consturcts, bind, list etc. The programs gives an overview of chess borad game and its pieces.

JESS ON CHESS

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Jess On Chess
;; Developed By: Ashik Uzzaman
;; Aug 22, 2006
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; this is jess
;; working on chess
;; i hope its
;; not quite a mess
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(printout t ”
=========== Start of Jess On Chess. ===========” crlf)

;; what is chess?
(printout t crlf
“**** What is Chess?
Chess is an abstract strategy board game and mental sport for two players.
The object of the game is to checkmate the opponent’s king.
This occurs when the king is under immediate attack (in check) and
there is no way to prevent it from being captured on the next move.” crlf)

;; declare squares
(printout t crlf
“**** What is Chess Borad?
Chess is played on a square board of 8 rows (called ranks) and 8 columns (called files), giving (* 8 8) = ” (* 8 8) ” squares of alternating light and dark color, which are referred to as ‘light squares’ and ‘dark squares’.” crlf crlf)

(bind ?row# 1)
(bind ?column# 1)
(bind ?maxrows 8)
(bind ?maxcolumns 8)
(bind ?files (create$ A B C D E F G H))
;; ranks in chess are 1 to 8 so not declaring any list for them

(while (<= ?column# ?maxcolumns) do
(while (<= ?row# ?maxrows) do
(printout t (nth$ ?column# ?files) ?row# ” “)
(bind ?row# (+ ?row# 1)))
(printout t crlf)
(bind ?row# 1)
(bind ?column# (+ ?column# 1)))

(printout t crlf
“**** How many light squares are there?” crlf)
(printout t “(/ (* 8 8) 2) = ” (/ (* 8 8) 2) crlf “The light squares of a chess board are….” crlf crlf)

(bind ?row# 1)
(bind ?column# 1)
(while (<= ?column# ?maxcolumns) do
(while (<= ?row# ?maxrows) do
(if (= (mod ?row# 2) 1) then
(if (= (mod ?column# 2) 0) then
(printout t (nth$ ?column# ?files) ?row# ” “))
else
(if (= (mod ?column# 2) 1) then
(printout t (nth$ ?column# ?files) ?row# ” “)))
(bind ?row# (+ ?row# 1)))
(printout t crlf)
(bind ?row# 1)
(bind ?column# (+ ?column# 1)))

(printout t crlf
“**** How many dark squares are there?” crlf)
(printout t “(- (* 8 8) (/ (* 8 8) 2)) = ” (- (* 8 8) (/ (* 8 8) 2)) crlf “The dark squares of a chess board are….” crlf crlf)

(bind ?row# 1)
(bind ?column# 1)
(while (<= ?column# ?maxcolumns) do
(while (<= ?row# ?maxrows) do
(if (= (mod ?row# 2) 0) then
(if (= (mod ?column# 2) 0) then
(printout t (nth$ ?column# ?files) ?row# ” “))
else
(if (= (mod ?column# 2) 1) then
(printout t (nth$ ?column# ?files) ?row# ” “)))
(bind ?row# (+ ?row# 1)))
(printout t crlf)
(bind ?row# 1)
(bind ?column# (+ ?column# 1)))

;; declare team
(bind ?teams (create$ white black))
(printout t crlf
“Two players represent 2 teams in a game ” ?teams ” and tries to win
by killing the opponent king which is know as “checkmate”. Depending on
success to checkmate the opponent king or saving one’s own king from
checkmate by opponent, a game may result in win, loss or draw.” crlf)

;; define there are 2 types of team represented by color – white and black
(bind ?team-members (create$ king queen rook bishop knight pawn))
(bind ?member-numbers (create$ 1 1 2 2 2 8))
(bind ?member-descriptions (create$ “the invaluable member of team” “the most powerful” “other major pieces that move straight” “minor pieces that move diagonally” “other minor pieces that have interesting movement” “the cheapest members of the team”))

(printout t crlf
“Each team has the following members ” ?team-members crlf)

(bind ?count 1)
(while (<= ?count 6) do
(printout t crlf (nth$ ?count ?member-numbers) ” ” (nth$ ?count ?team-members) ” – ” (nth$ ?count ?member-descriptions) crlf)
(bind ?count (+ ?count 1)))

;; define that there might be empty squares or occupied squares
(printout t crlf
“A square may be occupied by any one member of the 2 teams.” crlf)

;; define that no square can be occupied by more than 1 member
(printout t crlf
“But no square can be occupied by more than 1 member.” crlf)

;; define that no member can occupy more than 1 square
(printout t crlf
“Also no member can occupy more than 1 sqaure.” crlf)

(printout t crlf
“**** So what is the total number of memebrs occupying the chess board?
The total number of sqaures – (* 8 8) = ” (* 8 8) crlf)

(printout t
“The total number of white pices – (+ (+ (+ (+ (+ 1 1) 2) 2) 2) 8) = ” (+ (+ (+ (+ (+ 1 1) 2) 2) 2) 8) crlf)

(printout t
“The total number of black pices – (+ (+ (+ (+ (+ 1 1) 2) 2) 2) 8) = ” (+ (+ (+ (+ (+ 1 1) 2) 2) 2) 8) crlf)

(printout t
“The total number of pices combinedly (black and white) – (* (+ (+ (+ (+ (+ 1 1) 2) 2) 2) 8) 2) = ” (* (+ (+ (+ (+ (+ 1 1) 2) 2) 2) 8) 2) crlf)

(printout t
“The total number of empty squares at the start of the game – (- (* 8 8) (* (+ (+ (+ (+ (+ 1 1) 2) 2) 2) 8) 2)) = ” (- (* 8 8) (* (+ (+ (+ (+ (+ 1 1) 2) 2) 2) 8) 2)) crlf)

(printout t “

=========== End of Jess On Chess. ===========

this is jess
working on chess
i hope its
not quite a mess” crlf crlf)

Advertisements

2 Comments »

  1. very insightful read, thankyou.

    Comment by garyM — February 21, 2007 @ 9:08 pm

  2. ehh… funny )

    Comment by drunk sex videos — July 23, 2009 @ 7:45 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: