65 lines
2.5 KiB
Java
65 lines
2.5 KiB
Java
package org.drools.examples.simple;
|
|
|
|
import java.io.File;
|
|
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.io.ResourceFactory;
|
|
import org.drools.runtime.StatefulKnowledgeSession;
|
|
import org.drools.runtime.StatelessKnowledgeSession;
|
|
|
|
public class DroolsExample {
|
|
/**
|
|
* @param args
|
|
*/
|
|
public static void main(String[] args) {
|
|
//Create KnowledgeBase...
|
|
KnowledgeBase knowledgeBase = createKnowledgeBase();
|
|
//Create a statefull session
|
|
StatefulKnowledgeSession session = knowledgeBase.newStatefulKnowledgeSession();
|
|
|
|
//Create Facts - two ban accounts
|
|
Account account = new Account();
|
|
account.setBalance(10);
|
|
account.setId("N1");
|
|
|
|
Account account2 = new Account();
|
|
account2.setBalance(120);
|
|
account2.setId("N2");
|
|
|
|
//Insert the bank account facts into a State full session
|
|
session.insert(account);
|
|
session.insert(account2);
|
|
|
|
//Note that at this point, afetr fact insertation the Agenda has one activation ready to be fired.
|
|
//Account1 is less than 100.
|
|
|
|
//Only now we will fire the rules which are already in the agenda
|
|
session.fireAllRules();
|
|
|
|
//A message of N1 is less than 100 will be printed to stdout.
|
|
}
|
|
|
|
/**
|
|
* Create new knowledge base
|
|
*/
|
|
private static KnowledgeBase createKnowledgeBase() {
|
|
KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
|
|
//Add drl file into builder
|
|
File accountRules = new File("rules/BasicAccountRule.drl"); //Where the account rule is.
|
|
builder.add(ResourceFactory.newFileResource(accountRules), ResourceType.DRL);
|
|
if (builder.hasErrors()) {
|
|
throw new RuntimeException(builder.getErrors().toString());
|
|
}
|
|
|
|
KnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
|
|
|
|
//Add to Knowledge Base packages from the builder which are actually the rules from the drl file.
|
|
knowledgeBase.addKnowledgePackages(builder.getKnowledgePackages());
|
|
|
|
return knowledgeBase;
|
|
}
|
|
|
|
} |