Over view on Clips By: Mohsen Faghihi. Clips view.

Post on 16-Dec-2015

232 views 0 download

Tags:

Transcript of Over view on Clips By: Mohsen Faghihi. Clips view.

Over view on Clips

By: Mohsen Faghihi

Clips view

Facts (cat) (animal cat) (animal dog) (brothers ali mohammad) (polygon (name rectangle) (edge 4))

assert modify retract << fact(s) >>

)

(assert (phase start))

Managing facts

(A)(phase)(phase start) (phase start)

(phase end)

)

(assert

( phase choose-player)>=

( printout t "Who moves first (Compute: c Human: h))" ?( assert (player-select (read)))

)

(defrule Main::player-select

Define rules(defrule rule_name "optional_comment"

(patern_1)..(patern_N)=>(action_1)..(atcion_N)

)

(defrule player-select (phase choose-player) => (printout t "Who moves first (Compute: c Human: h)? ") (assert (player-select (read)) ))

Sample:

(salience N)

reset and clear Clear Reset

(reset)

(clear)

Define facts(deffacts <name>

<fact-1> . . <fact-n>

)

(deffacts initial-phase (phase choose-pile-size))

Sample:

(deffacts take-sticks-information (take-sticks (how-many 1) (for-remainder 1)) (take-sticks (how-many 1) (for-remainder 2)) (take-sticks (how-many 2) (for-remainder 3)) (take-sticks (how-many 3) (for-remainder 0)))

deftemplate(deftemplate <name>

(slot-1) . . (slot-n)

)

(deftemplate take-sticks (slot how-many) (slot for-remainder))

Sample:

(take-sticks (how-many 1) (for-remainder 1) )

Variable ?size ?choice ?pile ?whose-turn

(defrule MAIN::human-lost ?player-turn <- (player-move h) ?pile <- (pile-size ?size) (test (<= ?size 1)) => (retract ?player-turn ?pile) (printout t "You lost !" crlf))

Sample:

Math operators (+ (- (* (/

(+ 1 2 6 98 ?size)

(+ (* 6 7) 3)

Sample:

Logical operators (and (or (not

& | ~

(or (not (integerp ?choice)) (< ?choice 1) (> ?choice 3) (> ?choice ?size))

(player-select ?player&~c&~h)

Sample:

Test and bind Test Bind

(test <boolean-st>)(bind <variable> <value>)

(defrule human-lost ?player-turn <- (player-move h) ?pile <- (pile-size ?size) (test (<= ?size 1)) => (retract ?player-turn ?pile) (printout t "You lost !" crlf))

Sample:

(defrule addition (numbers ?x ?y) => (assert (answer (+ ?x ?y))) (bind ?answer (+ ?x ?y)) (printout t "answer is " ?answer crlf))

Sample:

Run time fact managing assert modify retract(modify

fact-place fact)

(retract fact-place1 . . fact-placeN)

(defrule rain (rain true) ?x <- (floor dry) => (modify ?x (floor wet)))

Sample:

(defrule avg ?student <- (student (std-id ?std)

(average ?avg) ) (lesson (mark ?mark) (std-id ?std) ) => (modify ?student

(average (+ ?avg ?mark)) ))

Sample:

(defrule r-1 ?x <-(student (mark 2) ) => (modify ?x (student (mark 1) (std-id 0)) ))

Sample:

(defrule MAIN::good-player-choice ?phase <- (phase choose-player) ?choice <- (player-select ?player&c|h) => (retract ?phase ?choice) (assert (player-move ?player)))

Sample:

I/O statements (read) (printout t “text”)

Boolean operators (< (> (<= (>= (eq (neq

Example Rule Move-Directly

IF The goal is to move block ?upper on top of block ?lower AND Block ?upper is the top block in its stack AND Block ?lower is the top block in its stack THEN Move block ?upper on top of block ?lower

Rule Clear-Upper-Block IF The goal is to move block ?x AND Block ?x is not the top block in its stack AND Block ?above is on top of block ?x THEN The goal is to move block ?above to the floor

Example Rule Clear-Lower-Block

IF The goal is to move another block on top of block ?x AND Block ?x is not the top block in its stack AND Block ?above is on top of block ?x THEN The goal is to move block ?above to the floor

Rule Move-To-Floor IF The goal is to move block ?upper on top of the floor AND Block ?upper is the top block in its stack THEN Move block ?upper on top of the floor

Example: Facts (on-top-of (upper nothing)(lower A)) (on-top-of (upper A)(lower B)) (on-top-of (upper B)(lower C)) (on-top-of (upper C)(lower floor))

(on-top-of (upper nothing)(lower D)) (on-top-of (upper D)(lower E)) (on-top-of (upper E)(lower F)) (on-top-of (upper F)(lower floor))

(deftemplate on-top-of(slot upper)(slot lower)

)

Example: Facts (block A) (block B) (block C) (block D) (block E) (block F)

Example: Facts (goal (move C)(on-top-of E))

(deftemplate goal(slot move)(slot on-top-of)

)

Example: Rules (defrule Move-Directly

?goal <- (goal (move ?block1)(on-top-of ?block2))(block ?block1)(block ?block2)(on-top-of (upper nothing)(lower ?block1))

?stack-1 <- (on-top-of (upper ?block1)(lower ?block3))?stack-2 <- (on-top-of (upper nothing)(lower ?block2))=>(retract ?goal ?stack-1 ?stack-2)(assert

(on-top-of (upper ?block1)(lower ?block2))(on-top-of (upper nothing)(lower ?block3))

)(printout t ?block1 " moved on top of " ?block2 "." crlf)

)

Rule Move-Directly IF The goal is to move block ?upper on yop of block ?lower AND Block ?upper is the top block in its stack AND Block ?lower is the top block in its stack THEN Move block ?upper on top of block ?lower

Example: Rules (defrule Clear-Upper-Block

(goal (move ?block1))(block ?block1)(on-top-of (upper ?block2)(lower ?block1))(block ?block2)=>(assert

(goal (move ?block2)(on-top-of floor)))

)

Rule Clear-Upper-Block IF The goal is to move block ?x AND Block ?x is not the top block in its stack AND Block ?above is on top of block ?x THEN The goal is to move block ?above to the floor

Example: Rules (defrule Clear-Lower-Block

(goal (on-top-of ?block1))(block ?block1)(on-top-of (upper ?block2)(lower ?block1))=>(assert

(goal (move ?block2)(on-top-of floor)))

)

Rule Clear-Lower-Block IF The goal is to move another block on top of block ?x AND Block ?x is not the top block in its stack AND Block ?above is on top of block ?x THEN The goal is to move block ?above to the floor

Example: Rules (defrule move-to-floor

?goal <- (goal (move ?block1)(on-top-of floor))(block ?block1)(on-top-of (upper nothing)(lower ?block1))?stack <- (on-top-of (upper ?block1)(lower ?block2))=>(retract ?goal ?stack)(assert

(on-top-of (upper ?block1)(lower floor))(on-top-of (upper nothing)(lower ?block2))

)(printout t ?block1 " moved to floor." crlf)

)

Rule Move-To-Floor IF The goal is to move block ?upper on top of the floor AND Block ?upper is the top block in its stack THEN Move block ?upper on top of the floor

Thanks

با تشکر فراوان ازخودم که خیلی زحمت کشیدم

درهمی استاد