首页 > 代码库 > drools5工作流实例--猜数字
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 = http://www.mamicode.com/value;
}
public int getValue()
{
return value;
}
public void setValue(int value)
{
this.value = http://www.mamicode.com/value;
}
public String toString()
{
return "Guess " + this.value;
}
}
public class RandomNumber
{
private int value;
public RandomNumber()
{
this.value = http://www.mamicode.com/new Random().nextInt(100);
}
public int getValue()
{
return value;
}
public void setValue(int value)
{
this.value = http://www.mamicode.com/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 < val)
</constraint>
<constraint toNodeId="6" toType="DROOLS_DEFAULT" name="too high" priority="1" type="rule" dialect="mvel">
RandomNumber(val:value)
Guess(value > val)
</constraint>
<constraint toNodeId="5" toType="DROOLS_DEFAULT" name="correct" priority="1" type="rule" dialect="mvel">
RandomNumber(val:value)
Guess(value =http://www.mamicode.com/= 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 >= allowed)
</constraint>
<constraint toNodeId="3" toType="DROOLS_DEFAULT" name="more guesses" priority="1" type="rule" dialect="mvel">
GameRules(allowed:allowedGuesses)
Game(guessCount < 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>
drools5工作流实例--猜数字