Search in sources :

Example 1 with ExternalSpreadsheetCompiler

use of org.drools.decisiontable.ExternalSpreadsheetCompiler in project drools by kiegroup.

the class RuleTemplateTest method testSampleCheese.

@Test
public void testSampleCheese() {
    // first we compile the decision table into a whole lot of rules.
    final ExternalSpreadsheetCompiler converter = new ExternalSpreadsheetCompiler();
    final KieServices kieServices = KieServices.Factory.get();
    final Resource table = kieServices.getResources().newClassPathResource("sample_cheese.xls", getClass());
    final Resource template = kieServices.getResources().newClassPathResource("sample_cheese.drt", getClass());
    String drl = null;
    try {
        // the data we are interested in starts at row 2, column 2 (i.e. B2)
        drl = converter.compile(table.getInputStream(), template.getInputStream(), 2, 2);
    } catch (IOException e) {
        throw new IllegalArgumentException("Could not read spreadsheet or rules stream.", e);
    }
    // compile the drl
    final Resource drlResource = kieServices.getResources().newReaderResource(new StringReader(drl));
    drlResource.setTargetPath(TestConstants.DRL_TEST_TARGET_PATH);
    final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, drlResource);
    final Collection<KiePackage> pkgs = kbase.getKiePackages();
    Assertions.assertThat(pkgs.size()).isEqualTo(2);
    final ArrayList<String> names = new ArrayList<String>();
    for (KiePackage kp : pkgs) {
        names.add(kp.getName());
    }
    Assertions.assertThat(names.contains(TestConstants.PACKAGE_FUNCTIONAL)).isTrue();
    Assertions.assertThat(names.contains(TestConstants.PACKAGE_TESTCOVERAGE_MODEL)).isTrue();
    final KiePackage kiePackage = (KiePackage) pkgs.toArray()[names.indexOf(TestConstants.PACKAGE_FUNCTIONAL)];
    Assertions.assertThat(kiePackage.getRules().size()).isEqualTo(2);
    final KieSession ksession = kbase.newKieSession();
    ksession.insert(new Cheese("stilton", 42));
    ksession.insert(new Person("michael", "stilton", 42));
    final List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    LOGGER.debug(list.toString());
    ksession.dispose();
}
Also used : ExternalSpreadsheetCompiler(org.drools.decisiontable.ExternalSpreadsheetCompiler) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) Cheese(org.drools.testcoverage.common.model.Cheese) IOException(java.io.IOException) KiePackage(org.kie.api.definition.KiePackage) KieBase(org.kie.api.KieBase) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.testcoverage.common.model.Person) Test(org.junit.Test)

Example 2 with ExternalSpreadsheetCompiler

use of org.drools.decisiontable.ExternalSpreadsheetCompiler in project drools by kiegroup.

the class TemplatesTest method loadingFromDLRSpreadsheetCorrectnessCheck.

@Test
public void loadingFromDLRSpreadsheetCorrectnessCheck() throws Exception {
    final ExternalSpreadsheetCompiler converter = new ExternalSpreadsheetCompiler();
    final KieServices kieServices = KieServices.Factory.get();
    // the data we are interested in starts at row 1, column 1 (e.g. A1)
    try (InputStream spreadSheetStream = kieServices.getResources().newClassPathResource("template1_spreadsheet.xls", getClass()).getInputStream();
        InputStream templateStream = kieServices.getResources().newClassPathResource("template_1.drl", getClass()).getInputStream()) {
        final String drl = converter.compile(spreadSheetStream, templateStream, 1, 1);
        // prints rules generated from template
        LOGGER.debug(drl);
        assertEqualsIgnoreWhitespace(EXPECTED_RULES.toString(), drl);
        testCorrectnessCheck(drl);
    }
}
Also used : ExternalSpreadsheetCompiler(org.drools.decisiontable.ExternalSpreadsheetCompiler) InputStream(java.io.InputStream) KieServices(org.kie.api.KieServices) Test(org.junit.Test)

Example 3 with ExternalSpreadsheetCompiler

use of org.drools.decisiontable.ExternalSpreadsheetCompiler in project drools by kiegroup.

the class ResourcesTest method testRuleTemplate.

@Test
public void testRuleTemplate() {
    // first we compile the decision table into a whole lot of rules.
    final ExternalSpreadsheetCompiler converter = new ExternalSpreadsheetCompiler();
    // the data we are interested in starts at row 2, column 2 (e.g. B2)
    final String drl = converter.compile(getClass().getResourceAsStream("sample_cheese.xls"), getClass().getResourceAsStream("sample_cheese.drt"), 2, 2);
    // compile the drl
    final Resource res = KieServices.Factory.get().getResources().newReaderResource(new StringReader(drl));
    res.setTargetPath(TestConstants.DRL_TEST_TARGET_PATH);
    final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, res);
    Assertions.assertThat((long) kbase.getKiePackages().size()).as("Unexpected number of packages in kbase").isEqualTo((long) 2);
    verifyPackageWithRules(kbase, TestConstants.PACKAGE_FUNCTIONAL, 2);
    verifyPackageWithImports(kbase, TestConstants.PACKAGE_TESTCOVERAGE_MODEL);
}
Also used : ExternalSpreadsheetCompiler(org.drools.decisiontable.ExternalSpreadsheetCompiler) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) StringReader(java.io.StringReader) Test(org.junit.Test)

Aggregations

ExternalSpreadsheetCompiler (org.drools.decisiontable.ExternalSpreadsheetCompiler)3 Test (org.junit.Test)3 StringReader (java.io.StringReader)2 KieBase (org.kie.api.KieBase)2 KieServices (org.kie.api.KieServices)2 Resource (org.kie.api.io.Resource)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Cheese (org.drools.testcoverage.common.model.Cheese)1 Person (org.drools.testcoverage.common.model.Person)1 KiePackage (org.kie.api.definition.KiePackage)1 KieSession (org.kie.api.runtime.KieSession)1