Search in sources :

Example 1 with Driver

use of org.acme.insurance.Driver in project drools by kiegroup.

the class PricingRuleLauncher method executeExample.

public int executeExample() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    DecisionTableConfiguration dtconf = KnowledgeBuilderFactory.newDecisionTableConfiguration();
    dtconf.setInputType(DecisionTableInputType.XLS);
    kbuilder.add(ResourceFactory.newClassPathResource("/data/ExamplePolicyPricing.xls", getClass()), ResourceType.DTABLE, dtconf);
    if (kbuilder.hasErrors()) {
        throw new RuntimeException(kbuilder.getErrors().toString());
    }
    // BUILD RULEBASE
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    // NEW WORKING MEMORY
    final KieSession session = kbase.newKieSession();
    // now create some test data
    Driver driver = new Driver();
    Policy policy = new Policy();
    session.insert(driver);
    session.insert(policy);
    session.fireAllRules();
    System.out.println("BASE PRICE IS: " + policy.getBasePrice());
    System.out.println("DISCOUNT IS: " + policy.getDiscountPercent());
    return policy.getBasePrice();
}
Also used : Policy(org.acme.insurance.Policy) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) DecisionTableConfiguration(org.kie.internal.builder.DecisionTableConfiguration) Driver(org.acme.insurance.Driver) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 2 with Driver

use of org.acme.insurance.Driver in project drools by kiegroup.

the class ExternalSpreadsheetCompilerTest method testPricing.

@Test
public void testPricing() throws Exception {
    final ExternalSpreadsheetCompiler converter = new ExternalSpreadsheetCompiler();
    final List<DataListener> listeners = new ArrayList<DataListener>();
    TemplateDataListener l1 = new TemplateDataListener(10, 3, "/templates/test_pricing1.drl");
    listeners.add(l1);
    TemplateDataListener l2 = new TemplateDataListener(30, 3, "/templates/test_pricing2.drl");
    listeners.add(l2);
    converter.compile("/data/ExamplePolicyPricing.xls", InputType.XLS, listeners);
    // COMPILE
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(l1.renderDRL().getBytes()), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newByteArrayResource(l2.renderDRL().getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession kSession = kbase.newKieSession();
    // now create some test data
    Driver driver = new Driver();
    Policy policy = new Policy();
    kSession.insert(driver);
    kSession.insert(policy);
    kSession.fireAllRules();
    System.out.println("BASE PRICE IS: " + policy.getBasePrice());
    System.out.println("DISCOUNT IS: " + policy.getDiscountPercent());
    int basePrice = policy.getBasePrice();
    assertEquals(120, basePrice);
}
Also used : Policy(org.acme.insurance.Policy) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) Driver(org.acme.insurance.Driver) TemplateDataListener(org.drools.template.parser.TemplateDataListener) DataListener(org.drools.template.parser.DataListener) KieSession(org.kie.api.runtime.KieSession) TemplateDataListener(org.drools.template.parser.TemplateDataListener) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

Driver (org.acme.insurance.Driver)2 Policy (org.acme.insurance.Policy)2 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2 KieSession (org.kie.api.runtime.KieSession)2 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)2 ArrayList (java.util.ArrayList)1 DataListener (org.drools.template.parser.DataListener)1 TemplateDataListener (org.drools.template.parser.TemplateDataListener)1 Test (org.junit.Test)1 DecisionTableConfiguration (org.kie.internal.builder.DecisionTableConfiguration)1