KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
//NEW WORKING MEMORY
StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
FactType scorecardType = kbase.getFactType( "org.drools.scorecards.example","SampleScore" );
DroolsScorecard scorecard = (DroolsScorecard) scorecardType.newInstance();
scorecardType.set(scorecard, "age", 10);
session.insert(scorecard);
session.fireAllRules();
session.dispose();
//age = 30, validLicence -1, initialScore = 100;
assertTrue(129 == scorecard.getCalculatedScore());
//age-reasoncode=AGE02, license-reasoncode=VL002
assertEquals(2, scorecard.getReasonCodes().size());
assertTrue(scorecard.getReasonCodes().contains("AGE02"));
assertTrue(scorecard.getReasonCodes().contains("VL002"));
session = kbase.newStatefulKnowledgeSession();
scorecard = (DroolsScorecard) scorecardType.newInstance();
scorecardType.set(scorecard, "age", 0);
scorecardType.set(scorecard, "occupation", "SKYDIVER");
session.insert(scorecard);
session.fireAllRules();
session.dispose();
//occupation = -10, age = +10, validLicense = -1, initialScore = 100;
assertEquals(99.0, scorecard.getCalculatedScore());
assertEquals(3, scorecard.getReasonCodes().size());
//[AGE01, VL002, OCC01]
assertTrue(scorecard.getReasonCodes().contains("AGE01"));
assertTrue(scorecard.getReasonCodes().contains("VL002"));
assertTrue(scorecard.getReasonCodes().contains("OCC01"));
session = kbase.newStatefulKnowledgeSession();
scorecard = (DroolsScorecard) scorecardType.newInstance();
scorecardType.set(scorecard, "age", 20);
scorecardType.set(scorecard, "occupation", "TEACHER");
scorecardType.set(scorecard, "residenceState", "AP");
scorecardType.set(scorecard, "validLicense", true);
session.insert( scorecard );
session.fireAllRules();
session.dispose();
//occupation = +10, age = +40, state = -10, validLicense = 1, initialScore = 100;
assertEquals(141.0,scorecard.getCalculatedScore());