Search in sources :

Example 1 with RuleUnit

use of org.kie.api.runtime.rule.RuleUnit in project drools by kiegroup.

the class RuleUnitCoordinationTest method testCoordination.

@Test
public void testCoordination() throws Exception {
    String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(MasterModelUnit.class) + ";\n" + "import " + MasterModel.class.getCanonicalName() + "\n" + "import " + ApplicableModel.class.getCanonicalName() + "\n" + "import " + ApplyMathModel.class.getCanonicalName() + "\n" + "import " + ApplyStringModel.class.getCanonicalName() + "\n" + "import " + ScheduledModelApplicationUnit.class.getCanonicalName() + "\n" + "\n" + "rule FindModelToApply \n" + "when\n" + "   $mm: MasterModel( subModels != null ) from models\n" + "   $am: ApplicableModel( applied == false, $idx: index ) from $mm.subModels\n" + // "   not ApplicableModel( applied == false, index < $idx ) from $mm.subModels\n" +
    "then\n" + "   applicableModels.insert($am);\n" + "   drools.run(new ScheduledModelApplicationUnit(models,applicableModels));\n" + "end\n" + "";
    String drl2 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(ScheduledModelApplicationUnit.class) + ";\n" + "import " + MasterModel.class.getCanonicalName() + "\n" + "import " + ApplicableModel.class.getCanonicalName() + "\n" + "import " + ApplyMathModel.class.getCanonicalName() + "\n" + "import " + ApplyStringModel.class.getCanonicalName() + "\n" + "\n" + "rule Apply_ApplyMathModel_Addition \n" + "when\n" + "    $amm: ApplyMathModel( applied == false, inputValue1 != null, " + "                          inputValue2 != null, operation == \"add\" ) from applicableModels\n" + "    $v1: Integer() from $amm.inputValue1 \n" + "    $v2: Integer() from $amm.inputValue2 \n" + "then\n" + "    modify($amm) { \n" + "       setResult($v1.intValue() + $v2.intValue()), \n" + "       setApplied(true) \n" + "    };\n" + "    System.out.println(\"Result = \"+$amm.getResult());\n" + "end\n" + "\n" + "rule Apply_ApplyStringModel_Concat \n" + "when\n" + "    $asm: ApplyStringModel( applied == false, inputString1 != null, " + "                            inputString2 != null, operation == \"concat\" ) from applicableModels \n" + "    $v1: String() from $asm.inputString1 \n" + "    $v2: String() from $asm.inputString2 \n" + "then\n" + "    String result = $v1+\" \"+$v2; \n" + "    modify($asm) {\n" + "       setResult(result),\n" + "       setApplied(true)\n" + "    };\n" + "    System.out.println(\"Result = \"+$asm.getResult());\n" + "end\n" + "";
    MasterModel master = new MasterModel("TestMaster");
    ApplyMathModel mathModel = new ApplyMathModel(1, "Math1", "add", 10, 10);
    ApplyStringModel stringModel = new ApplyStringModel(2, "String1", "concat", "hello", "world");
    master.addSubModel(mathModel);
    master.addSubModel(stringModel);
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).addContent(drl2, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    DataSource<MasterModel> masterModels = executor.newDataSource("models");
    DataSource<ApplicableModel> applicableModels = executor.newDataSource("applicableModel");
    FactHandle masterFH = masterModels.insert(master);
    RuleUnit unit = new MasterModelUnit(masterModels, applicableModels);
    // int x = 1;
    // while (x > 0) {
    // x = executor.run( unit );
    // System.out.println( x );
    // // I'm reinserting the master model to reinforce its evaluation, as I said in our call this is necessary because
    // // the second level froms are made on plain lists which are not normally re-evaluated by from nodes (regardless of rule units).
    // // In theory also the update should be more correct and enough to reinforce this re-evaluation, but for some reason
    // // in this case is not working. I suspect we have a bug in our NotNode related to this specific case, but I'm still
    // // evaluating it. For now the insert should be good enough to allow you to make some progress on this.
    // // masterModels.update( masterFH, master, "subModels" );
    // masterModels.insert( master );
    // }
    executor.run(unit);
    assertEquals(20, (int) mathModel.getResult());
    assertEquals("hello world", stringModel.getResult());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieHelper(org.kie.internal.utils.KieHelper) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) RuleUnit(org.kie.api.runtime.rule.RuleUnit) Test(org.junit.Test)

Example 2 with RuleUnit

use of org.kie.api.runtime.rule.RuleUnit in project drools by kiegroup.

the class RuleUnitTest method testRuleUnitFromKieContainer.

@Test
public void testRuleUnitFromKieContainer() {
    String drl = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnitWithSingleItem.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult when\n" + "    $p : /person[age >= adultAge]\n" + "then\n" + "    System.out.println($p.getName() + \" is adult\");\n" + "end";
    String kmodule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kmodule xmlns=\"http://jboss.org/kie/6.0.0/kmodule\">\n" + "    <kbase name=\"unit\" equalsBehavior=\"equality\" eventProcessingMode=\"stream\">\n" + "        <ksession name=\"unit-rules\" default=\"true\" type=\"stateful\" clockType=\"pseudo\"/>\n" + "    </kbase>\n" + "</kmodule>";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl).writeKModuleXML(kmodule);
    ks.newKieBuilder(kfs).buildAll();
    KieContainer kcontainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    RuleUnitExecutor executor = kcontainer.newRuleUnitExecutor();
    assertTrue(executor.getKieSession().getSessionClock() instanceof SessionPseudoClock);
    RuleUnit adultUnit = new AdultUnitWithSingleItem(new Person("Mario", 42));
    assertEquals(1, executor.run(adultUnit));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieServices(org.kie.api.KieServices) RuleUnit(org.kie.api.runtime.rule.RuleUnit) Person(org.drools.compiler.Person) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 3 with RuleUnit

use of org.kie.api.runtime.rule.RuleUnit in project drools by kiegroup.

the class RuleUnitTest method testWithOOPathAndNot.

@Test
public void testWithOOPathAndNot() 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", 4), new Person("Marilena", 17), new Person("Sofia", 4));
    RuleUnit adultUnit = new AdultUnit(persons);
    assertEquals(1, 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 4 with RuleUnit

use of org.kie.api.runtime.rule.RuleUnit in project drools by kiegroup.

the class RuleUnitTest method testWithOOPath.

@Test
public void testWithOOPath() throws Exception {
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "rule Adult @Unit( AdultUnit.class ) when\n" + "    $p : /persons[age >= 18]\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);
    DataSource<Person> persons = executor.newDataSource("persons", new Person("Mario", 42), new Person("Marilena", 44), new Person("Sofia", 4));
    RuleUnit adultUnit = new AdultUnit(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)

Example 5 with RuleUnit

use of org.kie.api.runtime.rule.RuleUnit in project drools by kiegroup.

the class RuleBuildContext method initRuleUnitClassName.

private void initRuleUnitClassName() {
    String ruleUnitClassName = rule.getRuleUnitClassName();
    boolean nameInferredFromResource = false;
    if (ruleUnitClassName == null && rule.getResource() != null && rule.getResource().getSourcePath() != null) {
        // We cannot depend on splitting based on File.separator, because e.g. MemoryFileSystem is "/" based
        // also on Windows => We need to parse the classname based on Java classname allowed characters.
        ruleUnitClassName = extractClassNameFromSourcePath();
        nameInferredFromResource = true;
    }
    if (ruleUnitClassName != null) {
        TypeResolver typeResolver = getPkg().getTypeResolver();
        boolean unitFound = false;
        Class<?> ruleUnitClass = ClassUtils.safeLoadClass(typeResolver.getClassLoader(), ruleUnitClassName);
        if (ruleUnitClass != null) {
            unitFound = RuleUnit.class.isAssignableFrom(ruleUnitClass);
            if (unitFound && nameInferredFromResource) {
                rule.setRuleUnitClassName(ruleUnitClassName);
            }
        }
        if (!unitFound && !nameInferredFromResource) {
            addError(new RuleBuildError(rule, getParentDescr(), null, ruleUnitClassName + " is not a valid RuleUnit class name"));
        }
    }
}
Also used : TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) RuleBuildError(org.drools.compiler.compiler.RuleBuildError) RuleUnit(org.kie.api.runtime.rule.RuleUnit)

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