hello everybody,<br>I have some trouble in solving the &#39;<a href="http://en.wikipedia.org/wiki/Zebra_Puzzle">Zebra Puzzle</a>&#39; using drools5.<br>I defined a class named &#39;House&#39; to represent the data, and the corresponding enums as well : Color.java, Nation.java, Pet.java, Drink.java, Cigarette.java, here are those sources:<br>
<br>House.java:<br>public class House {<br><br>    // 颜色<br>    public Color color;<br>    // 房子从左数的位置 1-5<br>    public int position;<br>    public Nation nation;<br>    public Pet pet;<br>    public Drink drink;<br>    public Cigarette cigarette;<br>
......<br><br>Color.java:<br><br>public enum Color {<br>    red, yellow, blue, green, ivory<br>}<br><br>Nation.java:<br><br>public enum Nation {<br>    englishman, spaniard, norwegian, ukrainian, japanese<br>}<br><br>Pet.java:<br>
<br>public enum Pet {<br>    dog, fox, snails, horse, zebra<br>}<br><br>Drink.java:<br><br>public enum Drink {<br>    orangeJuice, tea, coffee, milk, water<br>}<br><br>Cigarette.java:<br><br>public enum Cigarette {<br>    kools, chesterfields, winston, luckyStrike, parliaments<br>
}<br><br>And this is my drl file:<br><br>package com.sample<br> <br>import com.sample.fh.*<br><br>rule &quot;英国人住在红色的房子里&quot;<br>    when<br>        $h1:House(nation == Nation.englishman, color != Color.red)<br>        $h2:House(color == Color.red)<br>
    then<br>        System.out.println(&quot;英国人(englishman)住在红色(red)的房子里&quot;);<br>        modify($h2){setColor($h1.color)};<br>        modify($h1){setColor(Color.red)};<br>end<br><br>rule &quot;西班牙人养了一条狗&quot;<br>    when<br>
        $h1:House(nation == Nation.spaniard)<br>        $h2:House(pet == Pet.dog, nation != Nation.spaniard)<br>    then<br>        System.out.println(&quot;西班牙人(spaniard)养了一条狗(dog)&quot;);<br>        modify($h2){setPet($h1.pet)};<br>
        modify($h1){setPet(Pet.dog)};<br>end<br><br>rule &quot;挪威人住在左边的第一个房子里&quot;<br>    when<br>        $h1:House(position == 1, nation != Nation.norwegian)<br>        $h2:House(nation == Nation.norwegian)<br>    then<br>
        System.out.println(&quot;挪威人(norwegian)住在左边的第一个房子里&quot;);<br>        modify($h2){setNation($h1.nation)};<br>        modify($h1){setNation(Nation.norwegian)};<br>end<br><br>rule &quot;黄房子里的人喜欢抽kools牌的香烟&quot;<br>    when<br>
        $h1:House(color == Color.yellow, cigarette != Cigarette.kools)<br>        $h2:House(cigarette == Cigarette.kools)<br>    then<br>        System.out.println(&quot;黄房子(yellow)里的人喜欢抽kools牌的香烟&quot;);<br>        modify($h2){setCigarette($h1.cigarette)};<br>
        modify($h1){setCigarette(Cigarette.kools)};<br>end<br><br>rule &quot;抽chesterfields牌香烟的人与养狐狸的人是邻居&quot;<br>    when<br>        $h1:House(cigarette == Cigarette.chesterfields, pet != Pet.fox)<br>        $h2:House(pet == Pet.fox)<br>
        $h3:House(eval(position - $h2.position == 1) || eval(position - $h2.position == -1))<br>        eval($h1.position - $h2.position != 1 &amp;&amp; $h1.position - $h2.position != -1)<br>    then<br>        System.out.println(&quot;抽chesterfields牌香烟的人与养狐狸(fox)的人是邻居&quot;);<br>
        int tmp = $h3.position;<br>        modify($h3){setPosition($h1.position)};<br>        modify($h1){setPosition(tmp)};<br>end<br>rule &quot;抽chesterfields牌香烟的人与养狐狸的人是同一人&quot;<br>    when<br>        $h1:House(cigarette == Cigarette.chesterfields, pet == Pet.fox)<br>
        $h2:House(eval(position - $h1.position == 1) || eval(position - $h1.position == -1))<br>    then<br>        System.out.println(&quot;抽chesterfields牌香烟的人与养狐狸的人是同一人&quot;);<br>        modify($h1){setPet($h2.pet)};<br>
        modify($h2){setPet(Pet.fox)};<br>end<br><br>rule &quot;挪威人住在蓝色的房子旁边&quot;<br>    when<br>        $h1:House(nation == Nation.norwegian)<br>        $h2:House(color == Color.blue)<br>        $h3:House(eval(position - $h2.position == 1) || eval(position - $h2.position == -1))<br>
        eval($h1.position - $h2.position != 1 &amp;&amp; $h1.position - $h2.position != -1)<br>    then<br>        System.out.println(&quot;挪威人(norwegian)住在蓝色(blue)的房子旁边&quot;);<br>        modify($h1){setNation($h3.nation)};<br>
        modify($h3){setNation(Nation.norwegian)};<br>end<br><br>rule &quot;抽winston牌香烟的人养了一只蜗牛&quot;<br>    when<br>        $h1:House(cigarette == Cigarette.winston)<br>        $h2:House(pet == Pet.snails, cigarette != Cigarette.winston)<br>
    then<br>        System.out.println(&quot;抽winston牌香烟的人养了一只蜗牛(Snails)&quot;);<br>        modify($h2){setPet($h1.pet)};<br>        modify($h1){setPet(Pet.snails)};<br>end<br><br>rule &quot;抽LuckyStrike牌香烟的人喜欢喝桔子汁&quot;<br>
    when<br>        $h1:House(cigarette == Cigarette.luckyStrike)<br>        $h2:House(drink == Drink.orangeJuice, cigarette != Cigarette.luckyStrike)        <br>    then<br>        System.out.println(&quot;抽Lucky Strike牌香烟的人喜欢喝桔子汁(orange juice)&quot;);<br>
        modify($h2){setDrink($h1.drink)};<br>        modify($h1){setDrink(Drink.orangeJuice)};<br>end<br><br>rule &quot;乌克兰人喜欢喝茶&quot;<br>    when<br>        $h1:House(nation == Nation.ukrainian)<br>        $h2:House(drink == Drink.tea, nation != Nation.ukrainian)<br>
    then<br>        System.out.println(&quot;乌克兰人(ukrainian)喜欢喝茶(tea)&quot;);<br>        modify($h2){setDrink($h1.drink)};<br>        modify($h1){setDrink(Drink.tea)};<br>end<br><br>rule &quot;日本人抽parliaments牌的烟&quot;<br>
    when<br>        $h1:House(nation == Nation.japanese)<br>        $h2:House(cigarette == Cigarette.parliaments, nation != Nation.japanese)<br>    then<br>        System.out.println(&quot;日本人(japanese)抽parliaments牌的烟&quot;);<br>
        modify($h2){setCigarette($h1.cigarette)};<br>        modify($h1){setCigarette(Cigarette.parliaments)}<br>end<br><br>rule &quot;抽kools牌的香烟的人与养马的人是邻居&quot;<br>    when<br>        $h1:House(cigarette == Cigarette.kools)<br>
        $h2:House(pet == Pet.horse)<br>        $h3:House(eval(position - $h2.position == 1) || eval(position - $h2.position == -1))<br>        eval($h1.position - $h2.position != 1 &amp;&amp; $h1.position - $h2.position != -1)<br>
    then<br>        System.out.println(&quot;抽kools牌的香烟的人与养马(horse)的人是邻居&quot;);<br>        modify($h1){setCigarette($h3.cigarette)};<br>        modify($h3){setCigarette(Cigarette.kools)};<br>end<br><br>rule &quot;喜欢喝咖啡的人住在绿房子里&quot;<br>
    when<br>        $h1:House(drink == Drink.coffee)<br>        $h2:House(color == Color.green, drink != Drink.coffee)<br>    then<br>        System.out.println(&quot;喜欢喝咖啡(coffee)的人住在绿(green)房子里&quot;);<br>        modify($h2){setColor($h1.color)};<br>
        modify($h1){setColor(Color.green)};<br>end<br><br>rule &quot;绿房子在象牙白房子的右边&quot;<br>    when<br>        $h1:House(color == Color.green, position != 1)<br>        $h2:House(color == Color.ivory, position != 5)<br>        $h3:House(eval(position == $h2.position + 1), color != Color.green)<br>
    then<br>        System.out.println(&quot;绿(green)房子在象牙白(ivory)房子的右边(图中的右边)&quot;);<br>        modify($h1){setColor($h3.color)};<br>        modify($h3){setColor(Color.green)};<br>end<br>rule &quot;白房子已经在最右边了&quot;<br>    when<br>
        $h1:House(color == Color.ivory, position == 5)<br>        $h2:House(color == Color.green, position != 1)<br>        $h3:House(eval(position == $h2.position -1))<br>    then<br>        System.out.println(&quot;白房子已经在最右边了&quot;);<br>
        $h3.setPosition($h1.position);<br>        $h1.setPosition($h2.position - 1);<br>        update($h3);<br>        update($h1);<br>end<br>rule &quot;绿房子已经在最左边了&quot;<br>    when<br>        $h1:House(color == Color.green, position == 1)<br>
        $h2:House(color == Color.ivory, position != 5)<br>        $h3:House(eval(position == $h2.position + 1))<br>    then<br>        System.out.println(&quot;绿房子已经在最左边了&quot;);<br>        modify($h3){setPosition($h1.position)};<br>
        modify($h1){setPosition($h2.position + 1)};<br>end<br>rule &quot;绿房子在最左边且白房子在最右边&quot;<br>    when<br>        $h1:House(color == Color.green, position == 1)<br>        $h2:House(color == Color.ivory, position == 5)<br>
        $h3:House()<br>        $h4:House(eval(position - $h3.position == 1))<br>    then<br>        System.out.println(&quot;绿房子在最左边且白房子在最右边&quot;);<br>        modify($h2){setPosition($h3.position)};<br>        modify($h3){setPosition(5)};<br>
        modify($h1){setPosition($h4.position)};<br>        modify($h4){setPosition(1)};<br>end<br><br>rule &quot;中间那个房子里的人喜欢喝牛奶&quot;<br>    when<br>        $h1:House(position == 3, drink != Drink.milk)<br>        $h2:House(drink == Drink.milk)<br>
    then<br>        System.out.println(&quot;中间那个房子里的人喜欢喝牛奶&quot;);<br>        modify($h2){setDrink($h1.drink))};<br>        modify($h1){setDrink(Drink.milk)};<br>end<br><br>rule &quot;output&quot;<br>    when<br>        $h1:House(position == 1)<br>
        $h2:House(position == 2)<br>        $h3:House(position == 3)<br>        $h4:House(position == 4)<br>        $h5:House(position == 5)<br>        <br>        House(nation == Nation.englishman, color == Color.red)<br>
        <br>        House(nation == Nation.spaniard, pet == Pet.dog)<br>        <br>        House(position == 1, nation == Nation.norwegian)<br>        <br>        House(color == Color.yellow, cigarette == Cigarette.kools)<br>
        <br>        $h6:House(cigarette == Cigarette.chesterfields)<br>        $h7:House(pet == Pet.fox)<br>        eval($h6.position - $h7.position == 1 || $h6.position - $h7.position == -1)<br>        <br>        $h8:House(nation == Nation.norwegian)<br>
        $h9:House(color == Color.blue)<br>        eval($h8.position - $h9.position == 1 || $h8.position - $h9.position == -1)<br>        <br>        House(cigarette == Cigarette.winston, pet == Pet.snails)<br>        <br>
        House(cigarette == Cigarette.luckyStrike, drink == Drink.orangeJuice)<br>        <br>        House(nation == Nation.ukrainian, drink == Drink.tea)<br>        <br>        House(cigarette == Cigarette.parliaments, nation == Nation.japanese)<br>
        <br>        $h10:House(cigarette == Cigarette.kools)<br>        $h11:House(pet == Pet.horse)<br>        eval($h10.position - $h11.position == 1 || $h10.position - $h11.position == -1)<br>        <br>        House(drink == Drink.coffee, color == Color.green)<br>
        <br>        $h12:House(color == Color.green)<br>        $h13:House(color == Color.ivory)<br>        eval($h12.position - $h13.position == 1)<br>        <br>        House(position == 3, drink == Drink.milk)<br>    then<br>
        System.out.println(&quot;从左到右:&quot;);<br>        System.out.println(&quot;第一间房子是&quot;+$h1.color+&quot;色的;住着&quot;+$h1.nation+&quot;人;养&quot;+$h1.pet+&quot;宠物;抽&quot;+$h1.cigarette+&quot;牌香烟;喝&quot;+$h1.drink+&quot;饮料;&quot;);<br>
        System.out.println(&quot;第二间房子是&quot;+$h2.color+&quot;色的;住着&quot;+$h2.nation+&quot;人;养&quot;+$h2.pet+&quot;宠物;抽&quot;+$h2.cigarette+&quot;牌香烟;喝&quot;+$h2.drink+&quot;饮料;&quot;);<br>        System.out.println(&quot;第三间房子是&quot;+$h3.color+&quot;色的;住着&quot;+$h3.nation+&quot;人;养&quot;+$h3.pet+&quot;宠物;抽&quot;+$h3.cigarette+&quot;牌香烟;喝&quot;+$h3.drink+&quot;饮料;&quot;);<br>
        System.out.println(&quot;第四间房子是&quot;+$h4.color+&quot;色的;住着&quot;+$h4.nation+&quot;人;养&quot;+$h4.pet+&quot;宠物;抽&quot;+$h4.cigarette+&quot;牌香烟;喝&quot;+$h4.drink+&quot;饮料;&quot;);<br>        System.out.println(&quot;第五间房子是&quot;+$h5.color+&quot;色的;住着&quot;+$h5.nation+&quot;人;养&quot;+$h5.pet+&quot;宠物;抽&quot;+$h5.cigarette+&quot;牌香烟;喝&quot;+$h5.drink+&quot;饮料;&quot;);<br>
end<br> <br>There&#39;s some unicode character in the file.But it shouldn&#39;t bother the compilation&gt;...&lt;<br>The rule named &#39;output&#39; describes when the computation should stop. The other rules are absorbed from the original &#39;Zebra puzzle&#39; problem and each making a little change to the working memory in order to &#39;approach&#39; the solution...(I don&#39;t know if I&#39;m doing this wrong..)<br>
<br>No matter how I modify this program, it always result in an infinite loop...<br>For example, the console prints these messages repeatedly:<br><br>白房子已经在最右边了<br>抽chesterfields牌香烟的人与养狐狸的人是同一人<br>抽kools牌的香烟的人与养马(horse)的人是邻居<br>
黄房子(yellow)里的人喜欢抽kools牌的香烟<br>抽kools牌的香烟的人与养马(horse)的人是邻居<br>黄房子(yellow)里的人喜欢抽kools牌的香烟<br>抽kools牌的香烟的人与养马(horse)的人是邻居<br>黄房子(yellow)里的人喜欢抽kools牌的香烟<br>抽kools牌的香烟的人与养马(horse)的人是邻居<br>黄房子(yellow)里的人喜欢抽kools牌的香烟<br>抽kools牌的香烟的人与养马(horse)的人是邻居<br>
黄房子(yellow)里的人喜欢抽kools牌的香烟<br>......................<br><br>I worked quite much time on this problem till now, and I referenced another solution which is programmed in prolog, but still I didn&#39;t figured out the drools solution to this problem.<br>
Could somebody give a hint? Any help is appreciated,<br>Thanks!<br clear="all"><br>-- <br>Regards.<br>Miles. Wen<br><br>