前几天在论坛讨论了Drools后,说到了Drools的问题,根据DaDa老兄的描述,找到了Drools 3.1M1,发现From Collect真的不错。已经试着成功集成到公司开发框架中(现在可以不重新编辑,只是改改文本文件就达到调整Control层的效果),昨晚突发奇想,试着花了几个小时用Drools写了一个Tank自动对打游戏,感觉还不错。打算后续完善一下,集成同事推荐的robocode玩玩。 相信Drools3.2正式版出来以后,用这个搞开发真的会很流行
Copy一段昨晚Rule上来抛砖引玉,帮助想了解M1却不想看E文的朋友。
规则代码
- package warrule
- import warunit.Tank;
- import work.Hit;
- import work.Exp;
- import work.Order;
- import java.util.ArrayList;
- rule "destroy"
- salience 1000
- when
- tk:Tank(life <= 0 , $id:id)
- ht:Hit(to == $id ,$s:soruce)
- xp:Exp(status ==0)
- then
- System.out.println("Tank:"+ $id +" 被"+ $s +"击毁!");
- xp.setCt(1);
- xp.setId($s);
- xp.setStatus(1);
- retract(tk);
- modify(xp);
- end
- rule "get exp"
- salience 950
- when
- xp:Exp(status == 1,$id:id,$ct:ct)
- tk:Tank(id==$id,$xp:exp)
- then
- xp.setStatus(0);
- modify(xp);
- int nxp =$xp + $ct;
- tk.setExp(nxp);
- modify(tk);
- System.out.println("Tank:"+ $id +" 击毁Tank,获取经验:1,现有经验:"+ nxp);
- end
- rule "hit by enemy"
- salience 900
- when
- ht:Hit(status ==1,$t:to,$ht:hit)
- tk:Tank(id == $t,$lf:life)
- then
- int rm = $lf - $ht;
- // System.out.println(" 剩余生命:" + rm);
- ht.setStatus(0);
- tk.setLife(rm);
- modify(tk);
- modify(ht);
- end
- rule "hit enemy right"
- salience 800
- when
- ht:Hit(status ==0 )
- $tk:Tank(run >0 , $x:x,$y:y,$id:id,$t:team,$r:run,$ht:hit)
- li:ArrayList(size >0) from collect( Tank(x ==($x+1),y==$y,team != $t))
- then
- Tank tk1 = (Tank)li.get(0);
- System.out.print("Tank:"+ $id +" 打击右侧敌人:" + tk1.getId() );
- ht.setSoruce($id);
- ht.setTo(tk1.getId());
- ht.setHit($ht);
- ht.setStatus(1);
- $tk.setRun($r-1);
- modify($tk);
- modify(ht);
- end
- rule "hit enemy left"
- salience 800
- when
- ht:Hit(status ==0 )
- $tk:Tank(run >0 , $x:x,$y:y,$id:id,$t:team,$r:run,$ht:hit)
- li:ArrayList(size >0) from collect( Tank(x ==($x-1),y==$y,team != $t))
- then
- Tank tk1 = (Tank)li.get(0);
- System.out.print("Tank:"+ $id +" 打击左侧敌人:" + tk1.getId() );
- ht.setSoruce($id);
- ht.setTo(tk1.getId());
- ht.setHit($ht);
- ht.setStatus(1);
- $tk.setRun($r-1);
- modify($tk);
- modify(ht);
- end
- rule "hit enemy down"
- salience 800
- when
- ht:Hit(status ==0 )
- $tk:Tank(run >0 , $x:x,$y:y,$id:id,$t:team,$r:run,$ht:hit)
- li:ArrayList(size >0) from collect( Tank(x ==$x,y==($y+1),team != $t))
- then
- Tank tk1 = (Tank)li.get(0);
- System.out.print("Tank:"+ $id +" 打击下侧敌人:" + tk1.getId() );
- ht.setSoruce($id);
- ht.setTo(tk1.getId());
- ht.setHit($ht);
- ht.setStatus(1);
- $tk.setRun($r-1);
- modify($tk);
- modify(ht);
- end
- rule "hit enemy up"
- salience 800
- when
- ht:Hit(status ==0 )
- $tk:Tank(run >0 , $x:x,$y:y,$id:id,$t:team,$r:run,$ht:hit)
- li:ArrayList(size >0) from collect( Tank(x ==$x,y==($y-1),team != $t))
- then
- Tank tk1 = (Tank)li.get(0);
- System.out.print("Tank:"+ $id +" 打击上侧敌人:" + tk1.getId() );
- ht.setSoruce($id);
- ht.setTo(tk1.getId());
- ht.setHit($ht);
- ht.setStatus(1);
- $tk.setRun($r-1);
- modify($tk);
- modify(ht);
- end
- rule "goRight"
- salience 100
- when
- od:Order(status ==1,px:px,py:py);
- tk:Tank(x < px, run > 0,$x:x,$y:y,$r:run,$id:id )
- ArrayList(size == 0) from collect( Tank(x ==($x+1),y==$y))
- then
- int $nx = $x +1;
- //System.out.println("Tank:["+ $id +"]向右移动: ["+ $x +","+ $y +"] => ["+ $nx +","+ $y +"]" );
- tk.setX($nx);
- tk.setRun($r-1);
- modify(tk);
- end
- rule "goRightDown"
- salience 150
- when
- od:Order(status ==1,px:px,py:py);
- tk:Tank(x < px, y < py, run > 0,$x:x,$y:y,$r:run,$id:id )
- ArrayList(size == 0) from collect( Tank(x ==($x+1),y==($y+1)))
- then
- int $nx = $x +1;
- int $ny = $y +1;
- //System.out.println("Tank:["+ $id +"]向右下移动: ["+ $x +","+ $y +"] => ["+ $nx +","+ $y +"]" );
- tk.setX($nx);
- tk.setY($ny);
- tk.setRun($r-1);
- modify(tk);
- end
- rule "goLeftDown"
- salience 150
- when
- od:Order(status ==1,px:px,py:py);
- tk:Tank(x > px, y < py, run > 0,$x:x,$y:y,$r:run,$id:id )
- ArrayList(size == 0) from collect( Tank(x ==($x-1),y==($y+1)))
- then
- int $nx = $x -1;
- int $ny = $y +1;
- //System.out.println("Tank:["+ $id +"]向右下移动: ["+ $x +","+ $y +"] => ["+ $nx +","+ $y +"]" );
- tk.setX($nx);
- tk.setY($ny);
- tk.setRun($r-1);
- modify(tk);
- end
- rule "goLeftUp"
- salience 150
- when
- od:Order(status ==1,px:px,py:py);
- tk:Tank(x > px, y > py, run > 0,$x:x,$y:y,$r:run,$id:id )
- ArrayList(size == 0) from collect( Tank(x ==($x-1),y==($y-1)))
- then
- int $nx = $x -1;
- int $ny = $y -1;
- //System.out.println("Tank:["+ $id +"]向右下移动: ["+ $x +","+ $y +"] => ["+ $nx +","+ $y +"]" );
- tk.setX($nx);
- tk.setY($ny);
- tk.setRun($r-1);
- modify(tk);
- end
- rule "goRightUp"
- salience 150
- when
- od:Order(status ==1,px:px,py:py);
- tk:Tank(x < px, y > py, run > 0,$x:x,$y:y,$r:run,$id:id )
- ArrayList(size == 0) from collect( Tank(x ==($x+1),y==($y-1)))
- then
- int $nx = $x + 1;
- int $ny = $y - 1;
- //System.out.println("Tank:["+ $id +"]向右移动: ["+ $x +","+ $y +"] => ["+ $nx +","+ $y +"]" );
- tk.setX($nx);
- tk.setY($ny);
- tk.setRun($r-1);
- modify(tk);
- end
- rule "goLeft"
- salience 100
- when
- od:Order(status ==1,px:px,py:py);
- tk:Tank(x > px , run > 0,$x:x,$y:y,$r:run,$id:id )
- ArrayList(size == 0) from collect( Tank(x == ($x-1),y==$y))
- then
- int $nx = $x - 1;
- //System.out.println("Tank:["+ $id +"]向右移动: ["+ $x +","+ $y +"] => ["+ $nx +","+ $y +"]" );
- tk.setX($nx);
- tk.setRun($r-1);
- modify(tk);
- end
- rule "goDown"
- salience 100
- activation-group "down"
- when
- od:Order(status ==1,px:px,py:py);
- tk:Tank(y < py, run > 0,$x:x,$y:y,$r:run,$id:id )
- ArrayList(size == 0) from collect( Tank(x == $x,y== ($y+1) ))
- then
- int $ny = $y +1;
- //System.out.println("Tank:["+ $id +"]向下移动: ["+ $x +","+ $y +"] => ["+ $x +","+ $ny +"]");
- tk.setY($ny);
- tk.setRun($r-1);
- modify(tk);
- end
- rule "goDownTarget"
- salience 200
- activation-group "down"
- when
- od:Order(status ==0,tm:team,px:px,py:py);
- tk:Tank(team ==tm, y < py, run > 0,$x:x,$y:y,$r:run,$id:id )
- ArrayList(size == 0) from collect( Tank(x == $x,y== ($y+1) ))
- then
- int $ny = $y +1;
- //System.out.println("Tank:["+ $id +"]向下移动: ["+ $x +","+ $y +"] => ["+ $x +","+ $ny +"]");
- tk.setY($ny);
- tk.setRun($r-1);
- modify(tk);
- end
- rule "goUp"
- salience 100
- when
- od:Order(status ==1,px:px,py:py);
- tk:Tank(y > py, run > 0,$x:x,$y:y,$r:run,$id:id )
- ArrayList(size == 0) from collect( Tank(x == $x,y== ($y-1)))
- then
- int $ny = $y - 1;
- //System.out.println("Tank:["+ $id +"]向上移动: ["+ $x +","+ $y +"] => ["+ $x +","+ $ny +"]");
- tk.setY($ny);
- tk.setRun($r-1);
- modify(tk);
- end
- rule "atTarget"
- salience 0
- when
- od:Order(status ==1,px:px,py:py);
- tk:Tank(x == px, y==py,$x:x,$y:y,$r:run,$id:id)
- then
- System.out.println("Tank:["+ $id +"]到达中心["+ $x +","+ $y +"],剩余:"+ $r);
- end