use of org.kie.api.io.Resource in project drools by kiegroup.
the class BuildFromKJarTest method createKieModule.
private InternalKieModule createKieModule(KieServices ks, ReleaseId releaseId) {
KieFileSystem kfs = ks.newKieFileSystem();
kfs.writeKModuleXML(getDefaultKieModuleModel(ks).toXML());
kfs.writePomXML(KJARUtils.getPom(releaseId));
String javaSrc = Person.class.getCanonicalName().replace('.', '/') + ".java";
Resource javaResource = ks.getResources().newFileSystemResource("src/test/java/" + javaSrc);
kfs.write("src/main/java/" + javaSrc, javaResource);
kfs.write("src/main/resources/rule.drl", getRule());
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
List<Message> messages = ((KieBuilderImpl) kieBuilder).buildAll(ExecutableModelFlowProject.class).getResults().getMessages();
if (!messages.isEmpty()) {
fail(messages.toString());
}
return (InternalKieModule) kieBuilder.getKieModule();
}
use of org.kie.api.io.Resource 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.kie.api.io.Resource in project drools by kiegroup.
the class TemplatesTest method testManyRows.
private void testManyRows(final String drl, final int expectedResultListSize, final int expectedRulesCount) {
final Resource drlResource = KieServices.Factory.get().getResources().newReaderResource(new StringReader(drl));
drlResource.setTargetPath(TestConstants.DRL_TEST_TARGET_PATH);
final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, drlResource);
final KieSession session = kbase.newKieSession();
final List<String> list = new ArrayList<String>();
session.setGlobal("list", list);
for (int i = 0; i < 500; i++) {
session.insert(new Vegetable("tomato", 350, 8, 3, Taste.BAD));
}
session.fireAllRules();
// check of size of satisfying items
Assertions.assertThat(list.size()).isEqualTo(expectedResultListSize);
final Collection<KiePackage> pkgs = kbase.getKiePackages();
Assertions.assertThat(pkgs.size()).isEqualTo(1);
final KiePackage pkg = pkgs.iterator().next();
// check of generated rules size from template
Assertions.assertThat(pkg.getRules().size()).isEqualTo(expectedRulesCount);
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class UnicodeTest method testCzech.
@Test
public void testCzech() {
final KieServices kieServices = KieServices.Factory.get();
final Resource resource = kieServices.getResources().newClassPathResource("unicode.drl", getClass());
final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, resource);
final KieSession ksession = kbase.newKieSession();
final List<Command<?>> commands = new ArrayList<Command<?>>();
final List<Člověk> lidé = new ArrayList<Člověk>();
commands.add(kieServices.getCommands().newSetGlobal("lidé", lidé));
Člověk Řehoř = new Člověk();
Řehoř.setVěk(30);
Řehoř.setJméno("Řehoř");
commands.add(kieServices.getCommands().newInsert(Řehoř));
commands.add(kieServices.getCommands().newFireAllRules());
ksession.execute(kieServices.getCommands().newBatchExecution(commands, null));
Assertions.assertThat(kbase.getRule(TestConstants.PACKAGE_FUNCTIONAL, "příliš žluťoučký kůň úpěl ďábelské ódy")).isNotNull();
Map<String, Object> metaData = kbase.getRule(TestConstants.PACKAGE_FUNCTIONAL, "příliš žluťoučký kůň úpěl ďábelské ódy").getMetaData();
Assertions.assertThat(metaData.get("PrávníPožadavek")).isEqualTo("Osoba starší osmnácti let");
Assertions.assertThat(lidé.size()).isEqualTo(2);
Assertions.assertThat(lidé.get(0).getJméno()).isEqualTo("Řehoř");
Assertions.assertThat(lidé.get(1).getJméno()).isEqualTo("Oldřiška");
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class UnicodeTest method testCzechCSVDecisionTable.
@Test
public void testCzechCSVDecisionTable() throws FileNotFoundException {
final KieServices kieServices = KieServices.Factory.get();
final Resource decisionTable = ResourceUtil.getDecisionTableResourceFromClasspath("unicode.csv", getClass(), DecisionTableInputType.CSV);
KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, decisionTable);
KieSession ksession = kbase.newKieSession();
List<Command<?>> commands = new ArrayList<Command<?>>();
List<Člověk> dospělí = new ArrayList<Člověk>();
commands.add(kieServices.getCommands().newSetGlobal("dospělí", dospělí));
Člověk Řehoř = new Člověk();
Řehoř.setVěk(30);
Řehoř.setJméno("Řehoř");
commands.add(kieServices.getCommands().newInsert(Řehoř));
commands.add(kieServices.getCommands().newFireAllRules());
ksession.execute(kieServices.getCommands().newBatchExecution(commands, null));
Assertions.assertThat(kbase.getRule(TestConstants.PACKAGE_FUNCTIONAL, "pokusné pravidlo rozhodovací tabulky")).isNotNull();
Assertions.assertThat(dospělí.size()).isEqualTo(1);
Assertions.assertThat(dospělí.iterator().next().getJméno()).isEqualTo("Řehoř");
}
Aggregations