Search in sources :

Example 6 with RuleUnit

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);
        }
    }
}
Also used : RuleUnit(org.kie.api.runtime.rule.RuleUnit) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr)

Example 7 with RuleUnit

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;
}
Also used : HashSet(java.util.HashSet) RuleUnitExecutorSession(org.drools.core.impl.RuleUnitExecutorSession) List(java.util.List) Map(java.util.Map) Activation(org.drools.core.spi.Activation) Optional(java.util.Optional) Set(java.util.Set) HashMap(java.util.HashMap) RuleUnit(org.kie.api.runtime.rule.RuleUnit) ArrayList(java.util.ArrayList) RuleUnit(org.kie.api.runtime.rule.RuleUnit)

Example 8 with RuleUnit

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")));
}
Also used : DataSource(org.kie.api.runtime.rule.DataSource) FlowDSL.rule(org.drools.model.FlowDSL.rule) FlowDSL.expr(org.drools.model.FlowDSL.expr) FlowDSL.unitData(org.drools.model.FlowDSL.unitData) Variable(org.drools.model.Variable) FlowDSL.on(org.drools.model.FlowDSL.on) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) ArrayList(java.util.ArrayList) RuleUnit(org.kie.api.runtime.rule.RuleUnit) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) List(java.util.List) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) Arrays.asList(java.util.Arrays.asList) Rule(org.drools.model.Rule) Person(org.drools.modelcompiler.domain.Person) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) ModelImpl(org.drools.model.impl.ModelImpl) FlowDSL.declarationOf(org.drools.model.FlowDSL.declarationOf) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) ArrayList(java.util.ArrayList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Example 9 with RuleUnit

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));
}
Also used : RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) RuleUnit(org.kie.api.runtime.rule.RuleUnit) Test(org.junit.Test)

Example 10 with 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));
}
Also used : RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) RuleUnit(org.kie.api.runtime.rule.RuleUnit) 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