use of org.kie.api.runtime.rule.RuleUnit in project drools by kiegroup.
the class RuleContext method findUnitClass.
private void findUnitClass() {
if (descr == null) {
return;
}
String unitName = null;
AnnotationDescr unitAnn = descr.getAnnotation("Unit");
if (unitAnn != null) {
unitName = (String) unitAnn.getValue();
unitName = unitName.substring(0, unitName.length() - ".class".length());
} else if (descr.getUnit() != null) {
unitName = descr.getUnit().getTarget();
}
if (unitName != null) {
try {
Class<? extends RuleUnit> unitClass = (Class<? extends RuleUnit>) getTypeResolver().resolveType(unitName);
ruleUnitDescr = ruleUnitDescrCache.computeIfAbsent(unitClass, RuleUnitDescr::new);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
use of org.kie.api.runtime.rule.RuleUnit in project drools by kiegroup.
the class RuleUnitGuardSystem method fireActiveUnits.
private int fireActiveUnits(RuleUnit ruleUnit, Set<RuleUnit> firedUnits) {
Set<Guard> guards = guardsByActivatingUnit.get(ruleUnit);
if (guards == null) {
return 0;
}
int result = 0;
while (true) {
Optional<RuleUnit> unit = guards.stream().map(Guard::getGuardedUnit).filter(u -> !firedUnits.contains(u)).findFirst();
if (!unit.isPresent()) {
break;
}
RuleUnit firingUnit = unit.get();
result += session.internalExecuteUnit(firingUnit);
firedUnits.add(firingUnit);
result += fireActiveUnits(firingUnit, firedUnits);
}
return result;
}
use of org.kie.api.runtime.rule.RuleUnit in project drools by kiegroup.
the class RuleUnitTest method testRuleUnitWithVarBinding.
@Test
public void testRuleUnitWithVarBinding() {
Variable<AdultUnit> unit = declarationOf(AdultUnit.class);
Variable<Person> adult = declarationOf(Person.class, unitData("persons"));
Rule rule = rule("org.drools.retebuilder", "Adult").unit(RuleUnitTest.AdultUnit.class).build(expr("$expr$1$", adult, unit, (p, u) -> p.getAge() > u.getAdultAge()), on(adult, unitData(List.class, "results")).execute((p, r) -> {
System.out.println(p.getName());
r.add(p.getName());
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kieBase);
executor.newDataSource("persons", new Person("Mario", 43), new Person("Marilena", 44), new Person("Sofia", 5));
AdultUnit ruleUnit = new AdultUnit();
executor.run(ruleUnit);
assertTrue(ruleUnit.getResults().containsAll(asList("Mario", "Marilena")));
}
use of org.kie.api.runtime.rule.RuleUnit in project drools by kiegroup.
the class UnitCompilationTest method testMultipleConsequences.
@Test
public void testMultipleConsequences() throws Exception {
CompiledUnit unit = DrlxCompiler.compileFolders("src/test/resources/model", "src/test/resources/unit3");
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(3, executor.run(ruleUnit));
}
use of org.kie.api.runtime.rule.RuleUnit in project drools by kiegroup.
the class UnitCompilationTest method testFolderUnit.
@Test
public void testFolderUnit() throws Exception {
CompiledUnit unit = DrlxCompiler.compileFolders("src/test/resources/model", "src/test/resources/unit2");
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));
}
Aggregations