use of org.drools.core.ruleunit.RuleUnitFactory 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));
}
Aggregations