Search in sources :

Example 36 with Resource

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();
}
Also used : KieBuilderImpl(org.drools.compiler.kie.builder.impl.KieBuilderImpl) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.kie.api.builder.Message) Resource(org.kie.api.io.Resource) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 37 with Resource

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();
}
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 38 with Resource

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);
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession)

Example 39 with Resource

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");
}
Also used : Command(org.kie.api.command.Command) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 40 with Resource

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ř");
}
Also used : Command(org.kie.api.command.Command) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

Resource (org.kie.api.io.Resource)162 Test (org.junit.Test)100 KieSession (org.kie.api.runtime.KieSession)50 KieServices (org.kie.api.KieServices)43 KieBase (org.kie.api.KieBase)42 ArrayList (java.util.ArrayList)30 KieContainer (org.kie.api.runtime.KieContainer)27 StringReader (java.io.StringReader)23 ReleaseId (org.kie.api.builder.ReleaseId)23 KieBuilder (org.kie.api.builder.KieBuilder)20 KieModule (org.kie.api.builder.KieModule)20 KieFileSystem (org.kie.api.builder.KieFileSystem)19 ClassPathResource (org.drools.core.io.impl.ClassPathResource)18 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)17 FactType (org.kie.api.definition.type.FactType)17 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)16 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)14 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)14 IOException (java.io.IOException)13 PMMLResource (org.drools.compiler.compiler.PMMLResource)11