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();
}
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);
}
}
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);
}
Aggregations