public class MyMortgageApplication {
public static void main(String[] args) throws Throwable {
KnowledgeBase knowledgeBase = createKnowledgeBase();
StatefulKnowledgeSession session = knowledgeBase.newStatefulKnowledgeSession();
FactType appType = knowledgeBase.getFactType("mortgages","LoanApplication");
FactType incomeType = knowledgeBase.getFactType("mortgages","IncomeSource");
session.addEventListener(new DebugAgendaEventListener());
session.addEventListener(new DebugWorkingMemoryEventListener());
Object application = appType.newInstance();
Object income = incomeType.newInstance();
appType.set(application, "amount", 25000);
appType.set(application, "deposit", 1500);
appType.set(application, "lengthYears", 20);
incomeType.set(income, "type", "Job");
incomeType.set(income, "amount", 65000);
appType.set(application, "amount", 25000);
appType.set(application, "deposit", 1);
appType.set(application, "lengthYears", 1);
incomeType.set(income, "type", "Job");
incomeType.set(income, "amount", 6);
System.out.println("FactCount: " + session.getFactCount());
session.insert(appType);
session.insert(incomeType);
System.out.println("FireAllRules: " + session.fireAllRules());
System.out.println("FactCount: " + session.getFactCount());
session.dispose();
System.exit(0);
}
private static KnowledgeBase createKnowledgeBase() {
KnowledgeAgentConfiguration kaconf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration();
kaconf.setProperty("drools.agent.scanDirectories", "false");
kaconf.setProperty("drools.agent.newInstance", "true");
KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent("MortgageAgent", kaconf);
kagent.applyChangeSet(ResourceFactory.newClassPathResource("my-guvnor.xml"));
return kagent.getKnowledgeBase();
}
}
my-guvnor.xml: