[rules-users] re: NumberGuessExample

Tak-po Li takpo.li at gmail.com
Wed Jun 10 15:20:46 EDT 2009


Mauricio,

I would like to take this example as the base to play around.  That is what
I did:

1. Make a project "try" that generates the HellowWorld for me in Eclipse.

2. add directory "log"

3. Move file Guess.java to the Java/com/sample directory.  Move Guess.drl
and Guss.rf to rule directory.

 4. Change Package and Implort location from org/drools/examples to
com/sample in the above three files.

Eclipse shows compiler error pointing to ShoppingExample.*class.  *
**
*I did the same to Shopping.java and Shopping.drl and the compiler error is
removed, and the following is the run time error messages:*
**
*

Exception in thread "main" java.lang.IllegalArgumentException: Unknown
process ID: Number Guess

at org.drools.common.AbstractWorkingMemory.startProcess(
AbstractWorkingMemory.java:1615)

at org.drools.common.AbstractWorkingMemory.startProcess(
AbstractWorkingMemory.java:1604)

at org.drools.impl.StatefulKnowledgeSessionImpl.startProcess(
StatefulKnowledgeSessionImpl.java:267)

at com.sample.NumberGuessExample.main(NumberGuessExample.java:38)
*
Could you please point me the right direction?

Thanks,

Tak
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**





2009/6/10 Mauricio Salatino <salaboy at gmail.com>

> This is only a reference to know in which class loader is the resource...
> The idea is to know that the resource (in this case: NumberGuess.drl and
> NumberGuess.rf) is in the same class loader that the ShoppingExample.*
> class*
>
> 2009/6/10 Tak-po Li <takpo.li at gmail.com>
>
>>
>> I am a newbie with Drools rules.  I just download the examples.  I notice
>> in line19 of  NumberGuessExample.java, there are two statement that
>> referring to ShoppingExample.*class.*
>>
>> It seems this is different example.  Could someone help me out?
>>
>> Thanks,
>>
>> Tak
>>
>>
>>
>> kbuilder.add( ResourceFactory.*newClassPathResource*(
>> "NumberGuess.drl",
>>
>> ShoppingExample.
>> *class* ),
>>
>> ResourceType.
>> *DRL* );
>>
>> kbuilder.add( ResourceFactory.*newClassPathResource*(
>> "NumberGuess.rf",
>>
>> ShoppingExample.
>> *class* ),
>>
>> ResourceType.
>> *DRF* );
>>
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>>
>
>
> --
> - http://salaboy.wordpress.com
> - http://www.jbug.com.ar
> - Salatino "Salaboy" Mauricio -
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20090610/92f16ffa/attachment.html 
-------------- next part --------------
package com.sample

dialect "mvel"

import com.sample.NumberGuessExample.RandomNumber
import com.sample.NumberGuessExample.Guess
import com.sample.NumberGuessExample.Game
import com.sample.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( 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	
-------------- next part --------------
A non-text attachment was scrubbed...
Name: NumberGuessExample.java
Type: application/octet-stream
Size: 3799 bytes
Desc: not available
Url : http://lists.jboss.org/pipermail/rules-users/attachments/20090610/92f16ffa/attachment.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ShoppingExample.java
Type: application/octet-stream
Size: 3635 bytes
Desc: not available
Url : http://lists.jboss.org/pipermail/rules-users/attachments/20090610/92f16ffa/attachment-0001.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: NumberGuess.rf
Type: text/xml
Size: 3984 bytes
Desc: not available
Url : http://lists.jboss.org/pipermail/rules-users/attachments/20090610/92f16ffa/attachment.xml 
-------------- next part --------------
package com.sample
 
dialect "mvel"

import com.sample.ShoppingExample.Customer
import com.sample.ShoppingExample.Product
import com.sample.ShoppingExample.Purchase
import com.sample.ShoppingExample.Discount
 
rule "Purchase notification"
    salience 10

	when
		$c : Customer()
		$p : Purchase( customer == $c)	    
	then
	    System.out.println( "Customer " + $c.name + " just purchased " + $p.product.name );
end	 
 
rule "Discount removed notification"
	when
	    $c : Customer()
		not Discount( customer == $c )
	then
		$c.discount = 0;
		System.out.println( "Customer " + $c.name + " now has a discount of " + $c.discount );
end

rule "Discount awarded notification"
	when
	    $c : Customer()
		$d : Discount( customer == $c )
	then
		System.out.println( "Customer " + $c.name + " now has a discount of " + $d.amount );
end

rule "Apply 10% discount if total purcahses is over 100"			
	no-loop true	
	dialect "java"
    when
		$c : Customer()
		$i : Double(doubleValue  > 100) from accumulate ( Purchase( customer == $c, $price : product.price ), 
		                                                            sum( $price ) )
    then
  		$c.setDiscount( 10 );
		insertLogical( new Discount($c, 10) );	
		System.out.println( "Customer " + $c.getName() + " now has a shopping total of " + $i );
end


More information about the rules-users mailing list