/*
 * Copyright 2010 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.drools.examples.numberguess

dialect "mvel"

import org.drools.examples.numberguess.NumberGuessExample.RandomNumber
import org.drools.examples.numberguess.NumberGuessExample.Guess
import org.drools.examples.numberguess.NumberGuessExample.Game
import org.drools.examples.numberguess.NumberGuessExample.GameRules

import java.io.InputStreamReader;
import java.io.BufferedReader;
 
rule "Get user Guess"
    ruleflow-group "Guess"
    no-loop
    when
        $r : RandomNumber()
        rules : GameRules( allowed : allowedGuesses )
        game : Game( guessCount < allowed )
        not ( Guess() )
    then
        System.out.println( "You have " + ( rules.allowedGuesses - game.guessCount ) + " out of " + rules.allowedGuesses + " guesses left.\nPlease enter your guess from 0 to " + rules.maxRange );
        br = new BufferedReader( new InputStreamReader( System.in ) );
        modify ( game ) { guessCount += 1 }
        i = br.readLine();        
        insert( new Guess( new Integer(i) ) );
end

rule "Record the biggest Guess"
    ruleflow-group "Guess"
    no-loop
    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
    when
        game : Game( smallestGuess : smallest )
        Guess( $value : value < smallestGuess )
    then
        modify ( game ) { smallest = $value };
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
