You can start with something like this.
(deftemplate point
(slot i(type INTEGER))
(slot j(type INTEGER))
)
(defglobal ?*ROWS* = 5)
(defglobal ?*COLS* = 5)
(deffacts initial
(currentRow 0)
(currentColumn 0)
(point(i 1)(j 1))
(point(i 1)(j 2))
(point(i 1)(j 3))
(point(i 2)(j 1))
(point(i 2)(j 3))
(point(i 3)(j 1))
(point(i 3)(j 2))
(point(i 3)(j 3))
)
(defrule print_1
"Prints 1 if there's a point to print"
?r<-(currentRow ?i)
?c<-(currentColumn ?j)
(point(i ?i)(j ?j))
=>
(printout t "1 ")
(retract ?c)
(assert (currentColumn (+ ?j 1)))
)
(defrule print_0
"Prints 0 if there's not any point to print"
?r<-(currentRow ?i)
?c<-(currentColumn ?j&:(<= ?j ?*COLS*))
(not(point(i ?i)(j ?j)))
=>
(printout t "0 ")
(retract ?c)
(assert (currentColumn (+ ?j 1)))
)
(defrule printNextRow
"If we have reached the limit"
?c<-(currentColumn ?j&:(> ?j ?*COLS*))
?r<-(currentRow ?i&:(< ?i ?*ROWS*))
=>
(printout t "" crlf)
(retract ?c)
(retract ?r)
(assert (currentColumn 0))
(assert (currentRow (+ ?i 1)))
)
(reset)
(run)
The idea is to emulate a for loop
using only asserts/retracts
.
Output:
Jess, the Rule Engine for the Java Platform
Copyright (C) 2008 Sandia Corporation
Jess Version 7.1p2 11/5/2008
0 0 0 0 0 0
0 1 1 1 0 0
0 1 0 1 0 0
0 1 1 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0
Hope this helps