Search in sources :

Example 11 with RuleUnit

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

Example 12 with 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);});
// }
}
Also used : RuleUnitRegistry(org.drools.core.ruleunit.RuleUnitRegistry) RuleUnitDescr(org.drools.core.ruleunit.RuleUnitDescr) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleUnit(org.kie.api.runtime.rule.RuleUnit)

Example 13 with RuleUnit

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

Example 14 with 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));
}
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 15 with RuleUnit

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));
}
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