Final Defense (28 April 2009)































































Progress (April 20,2009)














Progress (April 3,2009)















Retract-string

(assert (Friends jack tui nguang pop))
<Fact-0>

Jess> (facts)
f-0   (MAIN::Friends jack tui nguang pop)
For a total of 1 facts in module MAIN.

Jess> (retract-string “(Friends jack tui nguang pop)”)
TRUE

Jess> (facts)
For a total of 0 facts in module MAIN.

JESS: Arrow Detection

(clear)
(reset)

(deftemplate Node     (slot id) (slot x (type integer)) (slot y (type integer)) (slot dot (type integer) (default 1))    )
(deftemplate Near    (multislot nodes))
(deftemplate Angle     (multislot lines) (slot angle (type float) (default 0)) )
(deftemplate Line     (slot id) (multislot nodes) (slot length (type integer) (default 0)) )
(deftemplate Curve    (slot id) (multislot nodes)    (slot length (type integer) (default 0)) )

; Functions
(deffunction longer (?lenline1 ?lenline2)
(if  (>(/ ?lenline1 ?lenline2) 1.5) then
(return TRUE)
else
(return FALSE)
)
)

(deffunction shorter (?lenline1 ?lenline2)
(if  (<(/ ?lenline1 ?lenline2) 1.5) then
(return TRUE)
else
(return FALSE)
)
)

(deffunction sameLength (?lenline1 ?lenline2)
(if (and (<(/ ?lenline1 ?lenline2) 2) (>(/ ?lenline1 ?lenline2) 0.5)) then
(return TRUE)
else
(return FALSE)
)
)

;; Define the rules

(assert (Node (id 21) (x 32) (y 85) ))
(assert (Line (id 4) (nodes 21 13) (length 212) ))
(assert (Node (id 13) (x -6) (y -124) ))
(assert (Line (id 5) (nodes 21 39) (length 98) ))
(assert (Node (id 39) (x 78) (y -2) ))
(assert (Line (id 6) (nodes 21 28) (length 79) ))
(assert (Node (id 28) (x -27) (y 35) ))
(assert (Near (nodes 28 7) ))
(assert (Node (id 7) (x 0) (y 1073741823) ))
(assert (Near (nodes 7 28) ))
(assert (Angle (lines 6 4) (angle 39.41529) ))
(assert (Angle (lines 4 5) (angle 38.171852) ))
(assert (Angle (lines 6 5) (angle 77.58714) ))

(defrule AnArrow
?n1 <- (Node (x ?x1) (y ?y1) (id ?a))
?n2 <- (Node (x ?x2) (y ?y2) (id ?b&~?a))
?n3 <- (Node (x ?x3) (y ?y3) (id ?c&~?b&~?a))
?n4 <- (Node (x ?x4) (y ?y4) (id ?d&~?c&~?b&~?a))
?f1 <- (Line (id ?l1) (nodes $? ?a $?)    (length ?len1) )        ; a — b
(Line (id ?l1) (nodes $? ?b $?))
?f2 <- (Line (id ?l2) (nodes $? ?a $?)    (length ?len2) )        ; a — c
(Line (id ?l2) (nodes $? ?c $?) )
?f3 <- (Line (id ?l3) (nodes $? ?a $?)    (length ?len3) )
(Line (id ?l3) (nodes $? ?d $?) )
(test (longer ?len3 ?len2))
;(test (shorter ?len3 ?len2))
(test (sameLength ?len1 ?len2))
=>
(bind ?str (str-cat “Found an arrow = ” ?a “, ” ?b “, ” ?c “, ” ?d ))
(printout t ?str crlf)
(assert (Arrow ?a ?b ?c ?d))
/*
(bind ?symbol (new sketcher.symbolrecognizer.Symbol))
(?symbol addLine ?x1 ?y1 ?x2 ?y2)
(?symbol addLine ?x1 ?y1 ?x3 ?y3)
(?symbol addLine ?x1 ?y1 ?x4 ?y4)
(?symbol addJunction ?a)
(?symbol addJunction ?b)
(?symbol addJunction ?c)
(?symbol addJunction ?d)
;(retract ?f1 ?f2 ?f3)
(add ?symbol)                                ;; return symbol to Java
*/
)

(facts)
(run)

JESS Example: Concatinate Lines -> the Longest Line

; JESS Example : Concatinate Lines -> the Longest Line
; Author: Rawin Viruchpintu
; 28/3/2552
(reset)
(clear)

(deftemplate Node (slot id) (slot link (type integer) (default 0))  (slot dot (type atom) (default TRUE)))
(deftemplate Line (slot id)    (multislot nodes) (slot length (type integer) (default 0)) )

(assert (Node (id 1) (link 3) (dot FALSE)))
(assert (Node (id 2) (link 1) (dot FALSE)))
(assert (Node (id 3) (link 1) (dot FALSE)))
(assert (Node (id 4) (link 2) (dot FALSE)))
(assert (Node (id 5) (link 1) (dot FALSE)))

(assert (Line (id 1) (nodes 1 4) (length 5)))            ; 1 — 4 or 4 — 6
(assert (Line (id 2) (nodes 2 1) (length 16)))
(assert (Line (id 3) (nodes 1 3) (length 8)))            ; 1 — 3 or 3 — 5
(assert (Line (id 4) (nodes 3 5) (length 2)))
(assert (Line (id 5) (nodes 4 6) (length 20)))
(facts)

(defrule theLonggestLine
?f1 <- (or (Line (id ?l1) (nodes ?a $? ?b&~?a)        (length ?len1) )
(Line (id ?l1) (nodes ?b&~?a $? ?a)        (length ?len1) ))        ; a — b

?f2 <- (or (Line (id ?l2) (nodes ?b $? ?c&~?a&~?b)    (length ?len2) )
(Line (id ?l2) (nodes ?c&~?b&~?a $? ?b)    (length ?len2) ))        ; a — c
?n1 <- (Node (id ?a) )
?n2 <- (Node (id ?b) {link < 3}  )
(not (or (exists (Line (nodes ?a $? ?c)))   (exists (Line (nodes ?c $? ?a)))  ))
=>
(bind ?str (str-cat “Add longer line = ” ?a “, ” ?b “, ” ?c ” from ” ?f1 ?f2 ))
(printout t ?str crlf)
(assert (Line (id 55) (nodes ?a ?c ) (length (+ ?len1 ?len2))))
;(retract ?f1 ?f2)
)
(run)
(facts)

JESS Example: Passing parameters to function, return TRUE / FALSE, Checking condition

; JESS Example : Passing parameters to function, return TRUE / FALSE, Checking condition
; Author: Rawin Viruchpintu
; 28/3/2552
(reset)
(clear)

(deftemplate Line (slot id)    (multislot nodes) (slot length (type integer) (default 0)) )

(assert (Line (id 1) (nodes 1 4) (length 5)))
(assert (Line (id 2) (nodes 2 1) (length 16)))
(assert (Line (id 3) (nodes 3 1) (length 8)))
(facts)

(deffunction sameLength (?lenline1 ?lenline2)
(if (and (<(/ ?lenline1 ?lenline2) 2) (>(/ ?lenline1 ?lenline2) 0.5)) then
(return TRUE)
else
(return FALSE)
)
)

(defrule checkRule
?f1 <- (Line (id ?l1) (nodes $? ?a $?)    (length ?len1) )        ; a — b
(Line (id ?l1) (nodes $? ?b&~?a $?))
?f2 <- (Line (id ?l2) (nodes $? ?a $?)    (length ?len2) )        ; a — c
(Line (id ?l2) (nodes $? ?c&~?a&~?b $?) )
?f3 <- (Line (id ?l3) (nodes $? ?a $?)    (length ?len3) )
(Line (id ?l3) (nodes $? ?d&~?a&~?b&~?c $?) )
(test (sameLength ?len1 ?len2))
=>
(bind ?str (str-cat “Answer = ” ?a “, ” ?b “, ” ?c “, ” ?d ” in ” ?f1 ?f2 ?f3 ))
(printout t ?str crlf)
(assert (Arrow ?a ?b ?c ?d))
;(retract ?f1 ?f2 ?f3)
)
(run)
(facts)

JESS Example: Checking coincident in any order

; JESS Example : Checking coincident in any order.
; Author: Rawin Viruchpintu
; 27/3/2552

(reset)
(clear)

(deftemplate Line (slot id)    (multislot nodes) )

(assert (Line (id 1) (nodes 1 4)))
(assert (Line (id 2) (nodes 2 1)))
(assert (Line (id 3) (nodes 3 1)))
(facts)

(defrule checkRule
?f1 <- (Line (id ?l1) (nodes $? ?a $?) )
(Line (id ?l1) (nodes $? ?b&~?a $?))
?f2 <- (Line (id ?l2) (nodes $? ?a $?) )
(Line (id ?l2) (nodes $? ?c&~?a&~?b $?) )
?f3 <- (Line (id ?l3) (nodes $? ?a $?) )
(Line (id ?l3) (nodes $? ?d&~?a&~?b&~?c $?) )
=>
(bind ?str (str-cat “Answer = ” ?a “, ” ?b “, ” ?c “, ” ?d ” in ” ?f1 ?f2 ?f3 ))
(printout t ?str crlf)
(assert (Arrow ?a ?b ?c ?d))
(retract ?f1 ?f2 ?f3)
)
(run)
(facts)

;(nodes $? 2 $? 3 $?)            ; work only 2 before 3 in nodes,because order is important

Other Related Java Rule-based Engines

Drools & JBOSS
http://en.wikipedia.org/wiki/Drools
Java Rule Engine API (JSR 94)
http://www.java201.com/resources/browse/2004/java_rule_engine_api.html
JSR-000094 JavaTM Rule Engine API
http://jcp.org/aboutJava/communityprocess/final/jsr094/index.html
Getting Started With the Java Rule Engine API (JSR 94): Toward Rule-Based Applications
http://java.sun.com/developer/technicalArticles/J2SE/JavaRule.html
Microsoft’s Rule Engine Scalability Results - A comparison with Jess and Drools
http://geekswithblogs.net/cyoung/articles/54022.aspx

JESS: Making Your Own Rules

(clear)
(reset)

(assert (line 1 2 3 4))
(assert (line 1 2 3 5))
(assert (line 5 1))
(assert (line 5 1 2 3 5))

(defrule found-a-boundary
?f <- (line 5 $? 5)
=>
(printout t “Found a boundary in {” ?f “}.” crlf )
)

(defrule found-a-coincident
?f <- (line $? 5 $?)
=>
(printout t “Found 5 in {” ?f “}.” crlf )
)

(facts)
(run)

Reference: http://www.jessrules.com/jess/docs/71/rules.html
http://www.jessrules.com/jess/docs/71/table_of_contents.html