use of org.kie.api.runtime.rule.RuleUnit in project drools by kiegroup.
the class UnitCompilationTest method testSingleFileUnit.
@Test
public void testSingleFileUnit() throws Exception {
CompiledUnit unit = DrlxCompiler.compileSingleSource(getClass().getClassLoader().getResourceAsStream("unit1/AdultUnit.java"));
RuleUnitExecutor executor = unit.createExecutor();
Constructor<?> constructor = unit.getConstructorFor("org.unit1.AdultUnit$Person", String.class, int.class);
DataSource<?> persons = executor.newDataSource("persons", constructor.newInstance("Mario", 43), constructor.newInstance("Marilena", 44), constructor.newInstance("Sofia", 5));
RuleUnit ruleUnit = unit.getOrCreateRuleUnit();
assertEquals(2, executor.run(ruleUnit));
}
use of org.kie.api.runtime.rule.RuleUnit in project drools by kiegroup.
the class SegmentExecution method applySegmentModel.
public void applySegmentModel(PMMLRequestData requestData, DataSource<PMMLRequestData> data, DataSource<PMML4Result> results, DataSource<SegmentExecution> segmentExecutions, KnowledgeHelper helper) throws IllegalStateException {
if (ruleUnitClassName == null || ruleUnitClassName.trim().isEmpty()) {
throw new IllegalStateException("Unable to apply segment model: No rule unit class name is available");
}
Class<? extends RuleUnit> ruleUnitClass = null;
RuleUnitRegistry rur = ((KnowledgeBaseImpl) helper.getKieRuntime().getKieBase()).getRuleUnitRegistry();
RuleUnit ruOld = ((WrappedStatefulKnowledgeSessionForRHS) helper.getKieRuntime()).getRuleUnitExecutor().getCurrentRuleUnit();
RuleUnitDescr rud = rur.getNamedRuleUnit(ruleUnitClassName).orElse(null);
if (rud != null) {
ruleUnitClass = rud.getRuleUnitClass();
} else {
throw new IllegalStateException("Unable to apply segment model: Unable to find rule unit");
}
// if (ruleUnitClass != null) {
// helper.run(ruleUnitClass);
// PMML4Result result = new PMML4Result(this);
// FactHandle requestFH = data.insert(this.requestData);
// FactHandle resultsFH = results.insert(result);
//
// helper.run(ruleUnitClass);
// RuleUnit ruNew = ((WrappedStatefulKnowledgeSessionForRHS)helper.getKieRuntime()).getRuleUnitExecutor().getCurrentRuleUnit();
// System.out.println("** Result **");
// System.out.println(result);
//
// // Update the state and let the Mining session know
// this.state = SegmentExecutionState.EXECUTING;
// FactHandle handle = ((InternalDataSource)segmentExecutions).getFactHandleForObject(this);
// segmentExecutions.update(handle, this);
//
// helper.run(ruleUnitClass);
// System.out.println("Sub-Model Results");
// results.forEach(r -> {System.out.println(r);});
// }
}
use of org.kie.api.runtime.rule.RuleUnit in project drools by kiegroup.
the class UnitCompilationTest method testOr.
@Test
public void testOr() throws Exception {
CompiledUnit unit = DrlxCompiler.compileFolders("src/test/resources/model", "src/test/resources/unit4");
RuleUnitExecutor executor = unit.createExecutor();
Constructor<?> personConstructor = unit.getConstructorFor("org.model.Person", String.class, int.class);
Constructor<?> childConstructor = unit.getConstructorFor("org.model.Child", String.class, int.class, int.class);
DataSource<?> persons = executor.newDataSource("persons", personConstructor.newInstance("Mario", 43), personConstructor.newInstance("Marilena", 44), childConstructor.newInstance("Sofia", 5, 10));
RuleUnit ruleUnit = unit.getOrCreateRuleUnit();
assertEquals(1, executor.run(ruleUnit));
}
use of org.kie.api.runtime.rule.RuleUnit in project drools by kiegroup.
the class RuleUnitTest method testWithOOPathAndNotNoMatch.
@Test
public void testWithOOPathAndNotNoMatch() throws Exception {
String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "rule Adult @Unit( AdultUnit.class ) when\n" + " not /persons[age >= 18]\n" + "then\n" + " System.out.println(\"No adults\");\n" + "end";
KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 44), new Person("Marilena", 170), new Person("Sofia", 18));
RuleUnit adultUnit = new AdultUnit(persons);
assertEquals(0, executor.run(adultUnit));
}
use of org.kie.api.runtime.rule.RuleUnit in project drools by kiegroup.
the class RuleUnitTest method testWithOOPathOnList.
@Test
public void testWithOOPathOnList() throws Exception {
// DROOLS-1646
String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnitWithList.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);
List<Person> persons = asList(new Person("Mario", 42), new Person("Marilena", 44), new Person("Sofia", 4));
RuleUnit adultUnit = new AdultUnitWithList(persons);
assertEquals(2, executor.run(adultUnit));
}
Aggregations