For the sake of completeness, here is also the RuleRunner class used in
this example:
package org.drools.tutorials.banking;
import java.util.Collection;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.definition.KnowledgePackage;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
public class RuleRunner {
public RuleRunner() {
}
public void runRules(String[] rules,
Object[] facts) {
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
KnowledgeBuilder kbuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder();
for ( int i = 0; i < rules.length; i++ ) {
String ruleFile = rules[i];
System.out.println( "Loading file: " + ruleFile );
kbuilder.add( ResourceFactory.newClassPathResource( ruleFile,
RuleRunner.class ),
ResourceType.DRL );
}
Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages();
kbase.addKnowledgePackages( pkgs );
StatefulKnowledgeSession ksession =
kbase.newStatefulKnowledgeSession();
for ( int i = 0; i < facts.length; i++ ) {
Object fact = facts[i];
System.out.println( "Inserting fact: " + fact );
ksession.insert( fact );
}
ksession.fireAllRules();
}
}
2013/4/18 Stefan Schuster <stefan.m.schuster(a)gmail.com>
Hello,
I have a problem with the banking tutorial number 3, which is part of the
examples of drools expert.
It simply adds some Numbers as facts, and retracts them in an increasing
order.
It is very short, therefore I post the whole Code:
BankingExample3.java
_________________________________________________
package org.drools.tutorials.banking;
public class BankingExample3 {
public static void main(String[] args) {
Number[] numbers = new Number[] {wrap(3), wrap(1), wrap(4),
wrap(1), wrap(5)};
new RuleRunner().runRules( new String[] { "Example3.drl" },
numbers );
}
private static Integer wrap(int i) {
return new Integer(i);
}
}
_________________________________________________
Example3.drl:
_________________________________________________
package org.drools.tutorials.banking
rule "Rule 01"
when
$number : Number()
not Number( intValue < $number.intValue )
then
System.out.println("Number found with value: " +
$number.intValue() );
retract( $number );
end
_________________________________________________
Output:
_________________________________________________
Loading file: Example3.drl
Inserting fact: 3
Inserting fact: 1
Inserting fact: 4
Inserting fact: 1
Inserting fact: 5
Number found with value: 1
Number found with value: 1
Number found with value: 3
Number found with value: 4
Number found with value: 5
_________________________________________________
that seams absolute logically to me.
But now I alter the Numbers in the Java-Part:
Number[] numbers = new Number[] {wrap(3), wrap(1), wrap(2)};
and the output destroys everything I thought I understood:
Loading file: Example3.drl
Inserting fact: 3
Inserting fact: 1
Inserting fact: 2
Number found with value: 1
Number found with value: 3
Number found with value: 2
Can someone reproduce this behavior?? It seams absolutely strange to me.
I would have expected the order 1,2,3. Any explanations?
Thanks in advance for helping me!