Search in sources :

Example 61 with KieBase

use of org.kie.api.KieBase in project drools by kiegroup.

the class RuleUnitTest method testModifyGlobalFact.

@Test
public void testModifyGlobalFact() throws Exception {
    String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnit.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult when\n" + "    $p : Person(age < 18, $name : name)\n" + "then\n" + "    System.out.println($name + \" is NOT adult\");\n" + "    modify($p) { setAge(18) }\n" + "end";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    executor.getKieSession().insert(new Person("Sofia", 4));
    assertEquals(1, executor.run(AdultUnit.class));
}
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) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 62 with KieBase

use of org.kie.api.KieBase 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 63 with KieBase

use of org.kie.api.KieBase in project drools by kiegroup.

the class RuleUnitTest method testRuleWithoutUnitsIsNotExecutor.

@Test
public void testRuleWithoutUnitsIsNotExecutor() throws Exception {
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "import " + NotAdultUnit.class.getCanonicalName() + "\n" + "rule Adult when\n" + "    Person(age >= 18, $name : name)\n" + "then\n" + "    System.out.println($name + \" is adult\");\n" + "end\n" + "rule NotAdult when\n" + "    Person(age < 18, $name : name)\n" + "then\n" + "    System.out.println($name + \" is NOT adult\");\n" + "end";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
    try {
        RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
        fail("It should throw IllegalStateException");
    } catch (IllegalStateException e) {
    // expected
    }
}
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) Test(org.junit.Test)

Example 64 with KieBase

use of org.kie.api.KieBase in project drools by kiegroup.

the class RuleUnitTest method testMultiLevelGuards.

@Test
public void testMultiLevelGuards() throws Exception {
    String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(Unit0.class) + "\n" + "import " + UnitA.class.getCanonicalName() + "\n" + "rule X when\n" + "    $b: /ds#Boolean\n" + "then\n" + "    Boolean b = $b;\n" + "    drools.guard( UnitA.class );\n" + "end";
    String drl2 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(UnitA.class) + "\n" + "import " + UnitB.class.getCanonicalName() + "\n" + "rule A when\n" + "    $s: /ds#String\n" + "then\n" + "    drools.guard( UnitB.class );" + "end";
    String drl3 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(UnitB.class) + "\n" + "import " + UnitB.class.getCanonicalName() + "\n" + "rule B when\n" + "    $i: /ds#Integer\n" + "then\n" + "    list.add($i);" + "end";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).addContent(drl2, ResourceType.DRL).addContent(drl3, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    DataSource<Object> ds = executor.newDataSource("ds");
    List<Integer> list = new ArrayList<>();
    executor.bindVariable("list", list);
    ds.insert(1);
    executor.run(Unit0.class);
    // all units are inactive
    assertEquals(0, list.size());
    FactHandle guardA = ds.insert(true);
    executor.run(Unit0.class);
    // UnitB still inactive
    assertEquals(0, list.size());
    FactHandle guardB = ds.insert("test");
    executor.run(Unit0.class);
    // all units are active
    assertEquals(1, list.size());
    // all units are active
    assertEquals(1, (int) list.get(0));
    list.clear();
    ds.insert(2);
    executor.run(Unit0.class);
    // all units are inactive
    assertEquals(1, list.size());
    // all units are active
    assertEquals(2, (int) list.get(0));
    list.clear();
    // retracting guard A deactivate unitA and in cascade unit B
    ds.delete(guardA);
    ds.insert(3);
    executor.run(Unit0.class);
    // all units are inactive
    assertEquals(0, list.size());
    // activating guard A reactivate unitA and in cascade unit B
    guardA = ds.insert(true);
    executor.run(Unit0.class);
    // all units are active
    assertEquals(1, list.size());
    list.clear();
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) Test(org.junit.Test)

Example 65 with KieBase

use of org.kie.api.KieBase in project drools by kiegroup.

the class RuleUnitTest method testTwoPartsOOPath.

@Test
public void testTwoPartsOOPath() throws Exception {
    // DROOLS-1539
    String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnit.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + LongAddress.class.getCanonicalName() + "\n" + "rule Adult when\n" + "    $a: /persons[ age > 18 ]/addresses#LongAddress[ country == \"it\" ]\n" + "then\n" + "    System.out.println($a.getCountry());\n" + "end";
    KieBase kbase = new KieHelper(PropertySpecificOption.ALWAYS).addContent(drl1, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    Person mario = new Person("Mario", 43);
    mario.setAddresses(asList(new LongAddress("street", "suburb", "zipCode", "it")));
    Person mark = new Person("Mark", 40);
    mark.setAddresses(asList(new LongAddress("street", "suburb", "zipCode", "uk")));
    DataSource<Person> persons = executor.newDataSource("persons", mario, mark);
    assertEquals(1, executor.run(AdultUnit.class));
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) LongAddress(org.drools.compiler.LongAddress) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) Person(org.drools.compiler.Person) Test(org.junit.Test)

Aggregations

KieBase (org.kie.api.KieBase)1272 Test (org.junit.Test)1191 KieSession (org.kie.api.runtime.KieSession)1011 ArrayList (java.util.ArrayList)592 List (java.util.List)392 Person (org.drools.compiler.Person)214 FactHandle (org.kie.api.runtime.rule.FactHandle)176 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)168 KieHelper (org.kie.internal.utils.KieHelper)156 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)154 Cheese (org.drools.compiler.Cheese)139 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)99 Arrays.asList (java.util.Arrays.asList)87 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)86 QueryResults (org.kie.api.runtime.rule.QueryResults)78 KieServices (org.kie.api.KieServices)74 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)67 Model (org.drools.model.Model)64 Rule (org.drools.model.Rule)64 ModelImpl (org.drools.model.impl.ModelImpl)64