Question

How can I print in my board an object (character # object I) in proper coordinates?

(deftemplate cenario
(slot min-line)
(slot max-line)
(slot min-column)
(slot max-column))

(deftemplate line
(slot index))

(deftemplate column
(slot index))

(deftemplate coordinate
(slot line)
(slot column))

(deftemplate object
(multislot coordinate))

(deffacts cenario
(cenario
(min-line 1)
(max-line 24)
(min-column 1)
(max-column 12)))

(deffacts line
(line (index 1))
(line (index 2))
(line (index 3))
(line (index 4))
(line (index 5))
(line (index 6))
(line (index 7))
(line (index 8))
(line (index 9))
(line (index 10))
(line (index 11))
(line (index 12))
(line (index 13))
(line (index 14))
(line (index 15))
(line (index 16))
(line (index 17))
(line (index 18))
(line (index 19))
(line (index 20))
(line (index 21))
(line (index 22))
(line (index 23))
(line (index 24)))

(deffacts column
(column (index 1))
(column (index 2))
(column (index 3))
(column (index 4))
(column (index 5))
(column (index 6))
(column (index 7))
(column (index 8))
(column (index 9))
(column (index 10))
(column (index 11))
(column (index 12)))

(deffacts I
(object (coordinate 5 24) (coordinate 6 24) (coordinate 7 24) (coordinate 8 24))))

(defrule startcolumn
(not(columnCurrent))
(cenario (min-column ?x))
=>
(assert(columnCurrent ?x)))

(defrule startline
(not(lineCurrent))
(cenario (max-line ?x))
=>
(assert(lineCurrent ?x)))

(defrule print-board
(cenario (max-column ?maxcol))
?f <- (line (index ?i))
?g <- (columnCurrent ?ca&:(<= ?ca ?maxcol))
(not (object (coordinate ?i ?ca)))
(lineCurrent ?i)
=>
(retract ?g)
(assert (columnCurrent (+ ?ca 1)))
(printout t "?"))

(defrule print-object
(lineCurrent ?i)
(columnCurrent ?ca)
(object (coordinate ?i ?ca ))
=>
(printout t ?i " " ?ca ))

(defrule change-line
(cenario (max-column ?maxcol))
?f <- (line (index ?i))
?g <- (columnCurrent 13)
(lineCurrent ?i)
=>
(retract ?f)
(assert (columnCurrent 1))
(assert (lineCurrent (- ?i 1)))
(printout t crlf))

I want this final result:

????####????

????????????

????????????

????????????

????????????

????????????

????????????

????????????

????????????

????????????

????????????

????????????

????????????

????????????

????????????

????????????

????????????

????????????

????????????

????????????

????????????

????????????

????????????

????????????
Was it helpful?

Solution

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

OTHER TIPS

A multislot contains objects, but you cannot match objects within the list items. This will not work:

(object $? (coordinate ?i ?ca ) $?)

It should be possible to store a coordinate pair as a string (or folded integer):

(deffacts I
(object (coordinate "5.23"  "6.23" "7.2" "8.2")))

And now rewrite the patterns according to

(object (coordinate $? ?x&=(str-cat ?i "." ?ca) $?)))

(Your loop over the lines doesn't work very well, but I don't want to waste my time with this absolutely non-declarative rule code. Sorry.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top