At first I solved a problem where I'd have an infinite loop, I fixed that by adding a rule to my CONSTRAIN module. I've taken all constrains into consideration, but it seems like all the facts are being deleted for some reason... This my code so far:
;;MAIN Module
(deftemplate state
(slot farmer-position)
(slot fox-position)
(slot goat-position)
(slot cabbage-position)
(slot id)
(slot prev-state (default nil))
(multislot move (default nil)))
(deftemplate finished
(slot value))
(deffacts initial-facts
(state (farmer-position s1)
(fox-position s1)
(goat-position s1)
(cabbage-position s1)
(id 0))
(opp s1 s2)
(opp s2 s1))
;;CONSTARIN Modle
(defmodule CONSTRAIN)
(defrule CONSTRAIN::fox-goat
(declare (auto-focus true))
?p<-(MAIN::state (fox-position ?f) (goat-position ?f) (farmer-position
~?f))
=>
(retract ?p))
(defrule CONSTRAIN::goat-cabbge
(declare (auto-focus true))
?p<-(MAIN::state (goat-position ?f) (cabbage-position ?f) (farmer-
position ~?f))
=>
(retract ?p))
(defrule CONSTRAIN::no-doubles
(declare (auto-focus true))
?p1<-(MAIN::state (farmer-position ?s1) (fox-position ?s2) (goat-
position ?s3) (cabbage-position ?s4) (id ?id1))
?p2<-(MAIN::state (farmer-position ?s1) (fox-position ?s2) (goat-
position ?s3) (cabbage-position ?s4) (id ?id2&:(> ?id2 ?id1)))
=>
(retract ?p2))
(defrule CONSTRAIN::stop-exc
(declare (auto-focus true))
?p1<-(MAIN::state (farmer-position s2) (fox-position s2) (goat-position
s2) (cabbage-position s2))
=>
(assert (MAIN::finished (value yes))))
;;MOVE Module
(defmodule MOVE)
(defrule MOVE::move-fox
?p<-(MAIN::state (farmer-position ?old) (fox-position ?old) (id ?id))
(not (MAIN::finished (value yes)))
(opp ?old ?new)
=>
(duplicate ?p (farmer-position ?new)
(fox-position ?new)
(prev-state ?p)
(id (+ ?id 1))
(move fox ?new)))
(defrule MOVE::move-goat
?p<-(MAIN::state (farmer-position ?old) (goat-position ?old) (id ?id))
(not (MAIN::finished (value yes)))
(opp ?old ?new)
=>
(duplicate ?p (farmer-position ?new)
(goat-position ?new)
(prev-state ?p)
(id (+ ?id 1))
(move goat ?new)))
(defrule MOVE::move-cabbage
?p<-(MAIN::state (farmer-position ?old) (cabbage-position ?old) (id ?
id))
(not (MAIN::finished (value yes)))
(opp ?old ?new)
=>
(duplicate ?p (farmer-position ?new)
(cabbage-position ?new)
(prev-state ?p)
(id (+ ?id 1))
(move cabbage ?new)))
;;RUN
(reset)
(watch all)
(focus MOVE)
(run)
(facts)
And here is my output:
<== Focus MAIN
==> Focus MOVE
FIRE 1 MOVE::move-fox f-1,, f-2
==> f-4 (MAIN::state (farmer-position s2) (fox-position s2) (goat-position
s1) (cabbage-position s1) (id 1) (prev-state <Fact-1>) (move fox s2))
==> Activation: CONSTRAIN::goat-cabbge : f-4
==> Activation: MOVE::move-fox : f-4,, f-3
<== Focus MOVE
==> Focus CONSTRAIN
FIRE 2 CONSTRAIN::goat-cabbge f-4
<== f-4 (MAIN::state (farmer-position s2) (fox-position s2) (goat-position
s1) (cabbage-position s1) (id 1) (prev-state <Fact-1>) (move fox s2))
<== Activation: MOVE::move-fox : f-4,, f-3
<== Focus CONSTRAIN
==> Focus MOVE
FIRE 3 MOVE::move-cabbage f-1,, f-2
==> f-5 (MAIN::state (farmer-position s2) (fox-position s1) (goat-position
s1) (cabbage-position s2) (id 1) (prev-state <Fact-1>) (move cabbage s2))
==> Activation: CONSTRAIN::fox-goat : f-5
==> Activation: MOVE::move-cabbage : f-5,, f-3
<== Focus MOVE
==> Focus CONSTRAIN
FIRE 4 CONSTRAIN::fox-goat f-5
<== f-5 (MAIN::state (farmer-position s2) (fox-position s1) (goat-position
s1) (cabbage-position s2) (id 1) (prev-state <Fact-1>) (move cabbage s2))
<== Activation: MOVE::move-cabbage : f-5,, f-3
<== Focus CONSTRAIN
==> Focus MOVE
FIRE 5 MOVE::move-goat f-1,, f-2
==> f-6 (MAIN::state (farmer-position s2) (fox-position s1) (goat-position
s2) (cabbage-position s1) (id 1) (prev-state <Fact-1>) (move goat s2))
==> Activation: MOVE::move-goat : f-6,, f-3
FIRE 6 MOVE::move-goat f-6,, f-3
==> f-7 (MAIN::state (farmer-position s1) (fox-position s1) (goat-position
s1) (cabbage-position s1) (id 2) (prev-state <Fact-6>) (move goat s1))
==> Activation: CONSTRAIN::no-doubles : f-1, f-7
==> Activation: MOVE::move-fox : f-7,, f-2
==> Activation: MOVE::move-goat : f-7,, f-2
==> Activation: MOVE::move-cabbage : f-7,, f-2
<== Focus MOVE
==> Focus CONSTRAIN
FIRE 7 CONSTRAIN::no-doubles f-1, f-7
<== f-7 (MAIN::state (farmer-position s1) (fox-position s1) (goat-position
s1) (cabbage-position s1) (id 2) (prev-state <Fact-6>) (move goat s1))
<== Activation: MOVE::move-fox : f-7,, f-2
<== Activation: MOVE::move-goat : f-7,, f-2
<== Activation: MOVE::move-cabbage : f-7,, f-2
<== Focus CONSTRAIN
==> Focus MOVE
<== Focus MOVE
==> Focus MAIN
<== Focus MAIN
For a total of 0 facts in module MOVE.
Fact f-6 was not retracted. The only valid first move is for the farmer to cross the river with the goat and that's the move represented by f-6. You don't have a rule for moving the farmer across the river alone, so the only valid move for the farmer from f-6 is to move back across the river with the goat which takes you back to the initial position. Since the initial position represented by f-1 is the same as f-7, the rule no-doubles retracts f-7 and there are no remaining valid moves.
If you download Jess from http://www.jessrules.com/jess/download.shtml, code for the river crossing example is available in the examples directory in the file dilemma.clp.