drools5工作流实例--猜数字

*.java

package com.sample;

public class NumGuessMain
{

public static void main(String[] args)
{
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("NumGuess.drl"), ResourceType.DRL);
kbuilder.add(ResourceFactory.newClassPathResource("NumGuess.rf"), ResourceType.DRF);
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
GameRules gr = new GameRules();
gr.setMaxRange(100);
gr.setAllowedGuesses(6);
ksession.insert(gr);
ksession.insert(new RandomNumber());
ksession.insert(new Game());
ksession.startProcess("Number Guess");
ksession.fireAllRules();
ksession.dispose();
}

}

public class Game
{
private int biggest;
private int smallest;
private int guessCount;
public Game()
{
this.biggest = 0;
this.smallest = 100;
this.guessCount = 0;
}
public void incrementGuessCount()
{
this.guessCount++;
}
public int getBiggest()
{
return biggest;
}
public void setBiggest(int biggest)
{
this.biggest = biggest;
}
public int getSmallest()
{
return smallest;
}
public void setSmallest(int smallest)
{
this.smallest = smallest;
}
public int getGuessCount()
{
return guessCount;
}
public void setGuessCount(int guessCount)
{
this.guessCount = guessCount;
}
}

public class GameRules
{
private int maxRange;
private int allowedGuesses;
public int getMaxRange()
{
return maxRange;
}
public void setMaxRange(int maxRange)
{
this.maxRange = maxRange;
}
public int getAllowedGuesses()
{
return allowedGuesses;
}
public void setAllowedGuesses(int allowedGuesses)
{
this.allowedGuesses = allowedGuesses;
}
public static boolean isNumber(String s)
{
try {
Integer.parseInt(s);
return true;
} catch (Exception e) {
return false;
}
}
}

public class Guess
{
private int value;
public Guess(int value)
{
this.value = value;
}
public int getValue()
{
return value;
}
public void setValue(int value)
{
this.value = value;
}
public String toString()
{
return "Guess " + this.value;
}
}

public class RandomNumber
{
private int value;
public RandomNumber()
{
this.value = new Random().nextInt(100);
}
public int getValue()
{
return value;
}
public void setValue(int value)
{
this.value = value;
}
}

NumGuess.drl

package com.sample
dialect "mvel"
import java.io.InputStreamReader;
import java.io.BufferedReader;
rule "Get User Guess"
ruleflow-group "Guess"
no-loop true
when
$r:RandomNumber()
rules:GameRules(allowed:allowedGuesses)
game:Game(guessCount<allowed)
not (Guess())
then
System.out.println("you have "+(rules.getAllowedGuesses()-game.getGuessCount())+" out of "+rules.getAllowedGuesses()+" guesses left.\nplease enter your guess from 0 to "+rules.getMaxRange());
br=new BufferedReader(new InputStreamReader(System.in));
s=br.readLine();
i=GameRules.isNumber(s)?(new Integer(s)):0;
modify(game){guessCount+=1}
insert(new Guess(i));
end
rule "Record the biggest Guess"
ruleflow-group "Guess"
no-loop true
when
game:Game(biggestGuess:biggest)
Guess($value:value>biggestGuess)
then
modify(game){biggest=$value}
end
rule "Record the smallest Guess"
ruleflow-group "Guess"
no-loop true
when
game:Game(small:smallest)
Guess($val:value<small)
then
modify(game){smallest=$val}
end
rule "Guess incorrect, retract Guess"
ruleflow-group "Guess incorrect"
when
guess:Guess()
then
retract(guess);
end
rule "No more Guesses notification"
ruleflow-group "No more guesses"
when
r:RandomNumber()
game:Game()
then
System.out.println("\nyou have no more guesses\nthe correct guess was "+r.value);
System.out.println("your smallest guess was "+game.smallest+"\nyour biggest guess was "+game.biggest);
end

NumGuess.rf

<?xml version="1.0" encoding="UTF-8"?>
<process xmlns="http://drools.org/drools-5.0/process"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://drools.org/drools-5.0/process drools-processes-5.0.xsd"
type="RuleFlow" name="Number Guess" id="Number Guess" package-name="com.sample" >

<header>
<imports>
<import name="com.sample.Game" />
<import name="com.sample.GameRules" />
<import name="com.sample.Guess" />
<import name="com.sample.RandomNumber" />
</imports>
</header>

<nodes>
<start id="1" name="Start" x="67" y="88" width="49" height="49" />
<end id="2" name="End" x="476" y="462" width="48" height="48" />
<join id="3" name="More guesses Join" x="69" y="174" width="145" height="49" type="2" />
<ruleSet id="4" name="Guess" ruleFlowGroup="Guess" x="48" y="285" width="80" height="49" />
<actionNode id="5" name="Guess correct" x="209" y="368" width="121" height="48" >
<action type="expression" dialect="mvel" >System.out.println( "You guessed correctly" );</action>
</actionNode>
<actionNode id="6" name="Too high" x="33" y="515" width="121" height="48" >
<action type="expression" dialect="mvel" >System.out.println( "Your guess was too high" );</action>
</actionNode>
<actionNode id="7" name="Too low" x="210" y="454" width="121" height="48" >
<action type="expression" dialect="mvel" >System.out.println( "Your guess was too low" );</action>
</actionNode>
<ruleSet id="8" name="No more guesses" ruleFlowGroup="No more guesses" x="425" y="292" width="145" height="49" />
<ruleSet id="9" name="Guess incorrect" ruleFlowGroup="Guess incorrect" x="606" y="514" width="125" height="48" />
<split id="10" name="Guess correct?" x="64" y="376" width="49" height="49" type="2" >
<constraints>
<constraint toNodeId="7" toType="DROOLS_DEFAULT" name="too low" priority="1" type="rule" dialect="mvel">
RandomNumber(val:value)
Guess(value &lt; val)
</constraint>
<constraint toNodeId="6" toType="DROOLS_DEFAULT" name="too high" priority="1" type="rule" dialect="mvel">
RandomNumber(val:value)
Guess(value &gt; val)
</constraint>
<constraint toNodeId="5" toType="DROOLS_DEFAULT" name="correct" priority="1" type="rule" dialect="mvel">
RandomNumber(val:value)
Guess(value == val)
</constraint>
</constraints>
</split>
<join id="11" name="Gateway" x="427" y="513" width="49" height="49" type="2" />
<split id="12" name="More Guesses?" x="634" y="159" width="49" height="49" type="2" >
<constraints>
<constraint toNodeId="8" toType="DROOLS_DEFAULT" name="no more guesses" priority="1" type="rule" dialect="mvel">
GameRules(allowed:allowedGuesses)
Game(guessCount &gt;= allowed)
</constraint>
<constraint toNodeId="3" toType="DROOLS_DEFAULT" name="more guesses" priority="1" type="rule" dialect="mvel">
GameRules(allowed:allowedGuesses)
Game(guessCount &lt; allowed)
</constraint>
</constraints>
</split>
<join id="13" name="Gateway" x="474" y="381" width="49" height="49" type="2" />
</nodes>

<connections>
<connection from="13" to="2" />
<connection from="1" to="3" />
<connection from="12" to="3" />
<connection from="3" to="4" />
<connection from="10" to="5" />
<connection from="10" to="6" />
<connection from="10" to="7" />
<connection from="12" to="8" />
<connection from="11" to="9" />
<connection from="4" to="10" />
<connection from="6" to="11" />
<connection from="7" to="11" />
<connection from="9" to="12" />
<connection from="5" to="13" />
<connection from="8" to="13" />
</connections>

</process>

时间: 2024-11-10 20:54:28

drools5工作流实例--猜数字的相关文章

Python3基础学习-while循环实例- 猜数字游戏

需求: 猜数字游戏, 要求如下: 给个数字, 比如是66. 让用户一直猜,直到猜对退出. 中间输入q或Q也能退出 如果猜大了,提示用户猜小一点; 如果猜小了,提示用户猜大一点; likely = 66 while True: guest_num = input("请输入你猜测的数字[q|Q退出]: ") if guest_num.upper() == "Q": break elif int(guest_num) > likely: print("你猜

实验三 Java猜数字游戏开发

课程:Java实验   班级:201352     姓名:程涵  学号:20135210 成绩:             指导教师:娄佳鹏   实验日期:15.06.03 实验密级:         预习程度:             实验时间: 仪器组次:          必修/选修:选修          实验序号:3 实验名称:     Java猜数字游戏开发 (姬梦馨.程涵小组) 一.实验目的与要求: 通过编写Java的应用系统综合实例——猜数字游戏,总结.回顾和实践面向对象的编程思想

通过游戏学python 3.6 第一季 第二章 实例项目 猜数字游戏 可复制使用 可封装

本文实例讲述了python实现的简单猜数字游戏.分享给大家供大家参考.具体如下: 给定一个1-99之间的数,让用户猜数字,当用户猜错时会提示用户猜的数字是过大还是过小,知道用户猜对数字为止,猜对数字用的次数越少成绩越好. 1 #猜数字 2 3 number=33 4 5 conversion=input('请猜数字') 6 guess=int(conversion) 7 8 9 if guess == number: 10 print('猜对了') 11 elif guess > number:

通过游戏学python 3.6 第一季 第九章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号--锁定次数--菜单功能&#39;menufile

通过游戏学python 3.6 第一季 第九章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号--锁定次数--菜单功能'menufile 1 #猜数字--核心代码--猜测次数--随机函数和屏蔽错误代码---优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号--锁定次数--菜单功能'menufile' 2 #!usr/bin/env python 3 #-*-c

通过游戏学python 3.6 第一季 第三章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释 可复制直接使用 娱乐 可封装 函数

1 #猜数字--核心代码--猜测次数--随机函数和屏蔽错误代码---优化代码及注释 2 3 import random 4 number = random.randint(1,99)#设定答案(可以假设成年龄吧) 5 amount = random.randint(3,8) #设定猜测次数 6 print('本次游戏次数为',amount,'次') 7 8 count = 0 #设定初始次数 9 while count <= amount: #条件成立无限循环 10 try: 11 guess

通过游戏学python 3.6 第一季 第四章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码 可复制直接使用 娱乐 可封装 函数

1 #猜数字--核心代码--猜测次数--随机函数和屏蔽错误代码 2 3 import random 4 secrst = random.randint(1,99) 5 6 number = secrst 7 8 amount = random.randint(3,8) 9 print('本次游戏次数为',amount,'次') 10 11 count=0 12 while count<=amount: 13 try: 14 conversion=input('请猜数字') 15 guess=in

通过游戏学python 3.6 第一季 第七章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号

#猜数字--核心代码--猜测次数--随机函数和屏蔽错误代码---优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号 1 #猜数字--核心代码--猜测次数--随机函数和屏蔽错误代码---优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号 2 #!usr/bin/env python 3 #-*-coding:utf-8-*- 4 #QQ124111294 5 6 7 import random 8 number = random.rand

原创Android游戏--猜数字游戏V1.1 --数据存储,Intent,SimpleAdapter的学习与应用

--------------------------------------------------------------- V0.1版本 上次做完第一个版本后,发现还有一些漏洞,并且还有一些可以添加的功能,以及一些可改进的地方,于是准备继续完善此游戏,顺便学Android了. 本次更新信息如下: 1.改正了随机数生成算法,更正了不能产生数字'9'的bug 2.增加了数据存储与IO的内容,使用了SharedPreferences保存数据 3.保存数据为: 总盘数,猜中的盘数 4.使用了Simp

python3.3使用tkinter实现猜数字游戏代码

发布时间:2014-06-18   编辑:www.jbxue.com 原文地址:http://www.jbxue.com/article/python/22152.html python3.3使用tkinter开发猜数字游戏的例子,python3.3中tkinter的使用方法. 本节脚本小编分享一例ython3.3中tkinter开发猜数字小游戏的代码. 例子,python3.3中tkinter实例. 复制代码代码示例: # -*- coding: utf-8 -*-import tkinter