Skip to: Site menu | Main content

Drools

Java Rules Engine

Prime Factors Example Print

Introduction

The Prime Factors example demonstrates the dynamic runtime creation of an arbitrary number of rules, and the subsequent assertion of an arbitrary large number of facts. The asserted facts are integer numbers and the rules perform a prime factorization of the numbers. For example, 819 is factored into [13, 7, 3, 3], because 13, 7, and 3 are all prime numbers and 13 * 7 * 3 * 3 = 819.

Instructions for Running the Prime Factors Example

Verbose Example

With Maven installed, the source code checked out of CVS, and the entire project compiled, change into the drools-examples directory and run the Prime Factors example with the command "maven primefactors-java-verbose". This will run the Prime Factors example with 100 rules, 10 random facts, and verbose output.

[drools/drools-examples]% maven primefactors-java-verbose
 __  __
|  \/  |__ _Apache__ ___
| |\/| / _` \ V / -_) ' \  ~ intelligent projects ~
|_|  |_\__,_|\_/\___|_||_|  v. 1.0

primefactors-java-verbose:
    [java] Number of Rules: 100
    [java] Number of Facts: 10
    [java] Random Seed: 1091553523298
    [java] Verbose Output: true
    [java] 
    [java] Reading 100 primes...
    [java] Read 100 primes [0 secs]
    [java] Creating 100 rules...
    [java] Created 100 rules [0 secs]
    [java] Building RuleBase with 100 rules...
    [java] Built RuleBase with 100 rules [0 secs]
    [java] Generating 10 random numbers to assert...
    [java] Generated 10 random numbers to assert [0 secs]
    [java] 
    [java] == Example #1 ========================================
    [java] foreach Number {
    [java]     new WorkingMemory();
    [java]     assertObject();
    [java]     fireAllRules();
    [java] }
    [java] ======================================================
    [java] Number(394 -> [197, 2])@d16fc1: [0 secs]
    [java] Number(331 -> [331])@697b67: [0 secs]
    [java] Number(288 -> [3, 3, 2, 2, 2, 2, 2])@554d32: [0 secs]
    [java] Number(262 -> [131, 2])@29b0e1: [0 secs]
    [java] Number(221 -> [13, 17])@4d5bc9: [0 secs]
    [java] Number(33 -> [3, 11])@7cd37a: [0 secs]
    [java] Number(280 -> [7, 5, 2, 2, 2])@a89ce3: [0 secs]
    [java] Number(323 -> [19, 17])@48f8c8: [0 secs]
    [java] Number(400 -> [5, 5, 2, 2, 2, 2])@815338: [0 secs]
    [java] Number(33 -> [3, 11])@ba0f36: [0 secs]
    [java] Total time: [0 secs]
    [java] 
    [java] == Example #2 ========================================
    [java] new WorkingMemory();
    [java] foreach Number {
    [java]     assertObject();
    [java]     fireAllRules();
    [java] }
    [java] ======================================================
    [java] Number(394 -> [197, 2])@aae86e: [0 secs]
    [java] Number(331 -> [331])@81a197: [0 secs]
    [java] Number(288 -> [3, 3, 2, 2, 2, 2, 2])@97d026: [0 secs]
    [java] Number(262 -> [131, 2])@e29820: [0 secs]
    [java] Number(221 -> [13, 17])@716cb7: [0 secs]
    [java] Number(33 -> [3, 11])@987a33: [0 secs]
    [java] Number(280 -> [7, 5, 2, 2, 2])@7eb6e2: [0 secs]
    [java] Number(323 -> [19, 17])@18d189: [0 secs]
    [java] Number(400 -> [5, 5, 2, 2, 2, 2])@648016: [0 secs]
    [java] Number(33 -> [3, 11])@1551b0: [0 secs]
    [java] Total time: [0 secs]
    [java] 
    [java] == Example #3 ========================================
    [java] new WorkingMemory();
    [java] foreach Number {
    [java]     assertObject();
    [java] }
    [java] fireAllRules()
    [java] ======================================================
    [java] Asserting 10 facts...
    [java] Firing all rules...
    [java] Number(331 -> [331])@e67e6a
    [java] Number(262 -> [131, 2])@6ed322
    [java] Number(400 -> [5, 5, 2, 2, 2, 2])@f54c5f
    [java] Number(323 -> [19, 17])@8330bf
    [java] Number(33 -> [3, 11])@bf053f
    [java] Number(394 -> [197, 2])@969c29
    [java] Number(288 -> [3, 3, 2, 2, 2, 2, 2])@758500
    [java] Number(33 -> [3, 11])@6917ee
    [java] Number(280 -> [7, 5, 2, 2, 2])@f1916f
    [java] Number(221 -> [13, 17])@e78c96
    [java] Total time: [0 secs]
BUILD SUCCESSFUL
Total time: 9 seconds
Finished at: Tue Aug 03 12:18:44 EST 2004

Larger Example

To see a longer run of the Prime Factors example using many more rules and facts, use the command "maven primefactors-java" instead. This will run the Prime Factors example with 10,000 rules, 300 random facts, and minimal output.

[drools/drools-examples]% maven primefactors-java
 __  __
|  \/  |__ _Apache__ ___
| |\/| / _` \ V / -_) ' \  ~ intelligent projects ~
|_|  |_\__,_|\_/\___|_||_|  v. 1.0

primefactors-java:
    [java] Number of Rules: 10000
    [java] Number of Facts: 300
    [java] Random Seed: 0
    [java] Verbose Output: false
    [java] 
    [java] == Example #1 ========================================
    [java] foreach Number {
    [java]     new WorkingMemory();
    [java]     assertObject();
    [java]     fireAllRules();
    [java] }
    [java] ======================================================
    [java] Total time: [13 secs]
    [java] 
    [java] == Example #2 ========================================
    [java] new WorkingMemory();
    [java] foreach Number {
    [java]     assertObject();
    [java]     fireAllRules();
    [java] }
    [java] ======================================================
    [java] Total time: [12 secs]
    [java] 
    [java] == Example #3 ========================================
    [java] new WorkingMemory();
    [java] foreach Number {
    [java]     assertObject();
    [java] }
    [java] fireAllRules()
    [java] ======================================================
    [java] Total time: [14 secs]
BUILD SUCCESSFUL
Total time: 50 seconds
Finished at: Tue Aug 03 12:23:24 EST 2004

Custom Example

You can also run the Prime Factors example with any arbitrary number of facts and rules. The easiest way to do this is to edit the maven.xml file. Just find the section where the primefactors-java goals are defined and change the parameters to the example to any values you desire.

Note: the number of rules you can use with this example is contrained by the number of primes contained in the primes.txt.gz file located in the drools/drools-examples/src/main/org/drools/examples/primefactors/ directory. If you want to run this example with more rules then allowed with the default primes.txt.gz file, you will need to replace the default primes.txt.gz file with a file containing more prime numbers. Such files can be found attached at the bottom of this page. Just download the desired attached file, and overwrite the existing primes.txt.gz with the one you downloaded. Make sure you rename the downloaded file to primes.txt.gz. If your browser automatically unzips the attached file, you will need to zip if back up again (using gzip) for it to work with the Prime Factors example.

One drawback to using Maven to run the example is that you can't set the JVM memory options, which may be required if you want to test with very large numbers of rules and facts without getting OutOfMemory exceptions. The alternative is to run the example directly from the Drools JAR files with the java command. Be sure to get all the required Drool JAR files on your classpath. The command-line should be of the form:

java -Xms{MinMem} -Xmx{MaxMem} -classpath {drools-smf jar}:{drools-io jar}:{drools-java jar}:{drools-core jar}:{drools-examples jar} org.drools.examples.primefactors.PrimeFactors {#ofRules} {#ofFacts} {RandomSeed} {Verbose?}

This is a command-line that worked for me (executed from the main drools directory):

[drools]% java -Xms256m -Xmx256m -classpath
    drools-smf/target/drools-smf-2.0-beta-18-dev.jar:
    drools-io/target/drools-io-2.0-beta-18-dev.jar:
    drools-java/target/drools-java-2.0-beta-18-dev.jar:
    drools-core/target/drools-core-2.0-beta-18-dev.jar:
    drools-examples/target/drools-examples-2.0-beta-18-dev.jar 
    org.drools.examples.primefactors.PrimeFactors 100000 100 0 false

Number of Rules: 100000
Number of Facts: 100
Random Seed: 0
Verbose Output: false

== Example #1 ========================================
foreach Number {
    new WorkingMemory();
    assertObject();
    fireAllRules();
}
======================================================
Total time: [37 secs]

== Example #2 ========================================
new WorkingMemory();
foreach Number {
    assertObject();
    fireAllRules();
}
======================================================
Total time: [36 secs]

== Example #3 ========================================
new WorkingMemory();
foreach Number {
    assertObject();
}
fireAllRules()
======================================================
Total time: [54 secs]

Understanding the Prime Factors Example

The Three Methods of Execution

Note that a single run of the Prime Factors example asserts the facts against the rules in three different ways. Method #1 creates a new WorkingMemory, asserts a single fact, then calls firesAllRules(), for each fact to be asserted. Method #2 creates a new WorkingMemory only once at the start, then asserts a single fact, and then calls firesAllRules() as each fact is asserted. Method #3 creates a new WorkingMemory only once at the start, then asserts all the facts, then calles firesAllRules() only once after all the facts are asserted.

The Rules

[TODO]

The Facts

[TODO]

Attachments