Search in sources :

Example 16 with RuleUnit

use of org.kie.api.runtime.rule.RuleUnit in project drools by kiegroup.

the class RuleUnitTest method testNamingConventionOnDrlFile.

@Test
public void testNamingConventionOnDrlFile() throws Exception {
    String drl1 = "package org.kie.test;\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult when\n" + "    $p : /persons[age >= 18]\n" + "then\n" + "    System.out.println($p.getName() + \" is adult\");\n" + "end";
    String javaRuleUnit = "package org.kie.test;\n" + "\n" + "import " + Person.class.getCanonicalName() + ";\n" + "import " + RuleUnit.class.getCanonicalName() + ";\n" + "import " + DataSource.class.getCanonicalName() + ";\n" + "\n" + "public class MyRuleUnit implements RuleUnit {\n" + "    private DataSource<Person> persons;\n" + "\n" + "    public DataSource<Person> getPersons() {\n" + "        return persons;\n" + "    }\n" + "}\n";
    String path = "org/kie/test/MyRuleUnit";
    KieServices ks = KieServices.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writeKModuleXML(ks.newKieModuleModel().toXML()).write("src/main/resources/" + path + ".drl", drl1).write("src/main/java/" + path + ".java", javaRuleUnit);
    ks.newKieBuilder(kfs).buildAll();
    KieContainer kcontainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    KieBase kbase = kcontainer.getKieBase();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42));
    RuleUnit ruleUnit = new RuleUnitFactory().bindVariable("persons", persons).getOrCreateRuleUnit(((InternalRuleUnitExecutor) executor), "org.kie.test.MyRuleUnit", kcontainer.getClassLoader());
    assertEquals(1, executor.run(ruleUnit));
    persons.insert(new Person("Sofia", 4));
    assertEquals(0, executor.run(ruleUnit));
    persons.insert(new Person("Marilena", 44));
    assertEquals(1, executor.run(ruleUnit));
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) KieFileSystem(org.kie.api.builder.KieFileSystem) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) RuleUnitFactory(org.drools.core.ruleunit.RuleUnitFactory) KieServices(org.kie.api.KieServices) RuleUnit(org.kie.api.runtime.rule.RuleUnit) Person(org.drools.compiler.Person) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 17 with RuleUnit

use of org.kie.api.runtime.rule.RuleUnit in project drools by kiegroup.

the class RuleUnitTest method testWithOOPathOnSingleItem.

@Test
public void testWithOOPathOnSingleItem() throws Exception {
    // DROOLS-1646
    String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnitWithSingleItem.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult when\n" + "    $p : /person[age >= adultAge]\n" + "then\n" + "    System.out.println($p.getName() + \" is adult\");\n" + "end";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    RuleUnit adultUnit = new AdultUnitWithSingleItem(new Person("Mario", 42));
    assertEquals(1, executor.run(adultUnit));
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) RuleUnit(org.kie.api.runtime.rule.RuleUnit) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 18 with RuleUnit

use of org.kie.api.runtime.rule.RuleUnit in project drools by kiegroup.

the class RuleUnitTest method testWithOOPathOnArray.

@Test
public void testWithOOPathOnArray() throws Exception {
    // DROOLS-1646
    String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnitWithArray.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult when\n" + "    $p : /persons[age >= adultAge]\n" + "then\n" + "    System.out.println($p.getName() + \" is adult\");\n" + "end";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    RuleUnit adultUnit = new AdultUnitWithArray(new Person("Mario", 42), new Person("Marilena", 44), new Person("Sofia", 4));
    assertEquals(2, executor.run(adultUnit));
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) RuleUnit(org.kie.api.runtime.rule.RuleUnit) Person(org.drools.compiler.Person) Test(org.junit.Test)

Aggregations

RuleUnit (org.kie.api.runtime.rule.RuleUnit)18 Test (org.junit.Test)14 RuleUnitExecutor (org.kie.api.runtime.rule.RuleUnitExecutor)14 KieBase (org.kie.api.KieBase)9 Person (org.drools.compiler.Person)8 InternalRuleUnitExecutor (org.drools.core.impl.InternalRuleUnitExecutor)8 KieHelper (org.kie.internal.utils.KieHelper)7 ArrayList (java.util.ArrayList)2 List (java.util.List)2 KieServices (org.kie.api.KieServices)2 KieFileSystem (org.kie.api.builder.KieFileSystem)2 KieContainer (org.kie.api.runtime.KieContainer)2 Arrays.asList (java.util.Arrays.asList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 RuleBuildError (org.drools.compiler.compiler.RuleBuildError)1 AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)1