Search in sources :

Example 26 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class RuleUnitTest method testNestedOOPath.

@Test
public void testNestedOOPath() 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" + "    $p: /persons[ age > 18, $a: /addresses#LongAddress[ country == \"it\" ] ]\n" + "then\n" + "    System.out.println($p.getName() + \" is in \" + $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)

Example 27 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class RuleUnitTest method testUnboundDataSource.

@Test
public void testUnboundDataSource() throws Exception {
    // DROOLS-1533
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "import " + NotAdultUnit.class.getCanonicalName() + "\n" + "rule Adult @Unit( AdultUnit.class ) when\n" + "    Person(age >= 18, $name : name) from persons\n" + "then\n" + "    System.out.println($name + \" is adult\");\n" + "end\n" + "rule NotAdult @Unit( NotAdultUnit.class ) when\n" + "    Person(age < 18, $name : name) from persons\n" + "then\n" + "    System.out.println($name + \" is NOT adult\");\n" + "end";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    DataSource<Person> persons = DataSource.create(new Person("Mario", 42), new Person("Marilena", 44), new Person("Sofia", 4));
    // explicitly create unit
    assertEquals(2, executor.run(new AdultUnit(persons)));
    // let RuleUnitExecutor internally create and wire the unit instance
    assertEquals(1, executor.run(new NotAdultUnit(persons)));
}
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 28 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class RuleUnitTest method testComplexData.

@Test
public void testComplexData() {
    String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(ComplexDataUnit.class) + "\n" + "import " + RequestData.class.getCanonicalName() + "\n" + "import " + ParameterHolder.class.getCanonicalName() + "\n" + "import " + InternalData.class.getCanonicalName() + "\n" + "import " + InternalDataFactory.class.getCanonicalName() + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "\n" + "\n" + "rule 'Extract Named Parameter out of Request - Name'\n" + "when\n" + "   $request: RequestData( $id: requestId != null, modelId != null, $parameters: parameters != null ) from requestData\n" + "   $param: ParameterHolder( capitalizedName == \"Name\" ) from $parameters\n" + "then\n" + "   System.out.printf(\"Parameter found: %s with value of %s%n\",$param.getCapitalizedName(),$param.getValue());\n" + "   InternalData d = InternalDataFactory.get().createInternalData($param);\n" + "   insert(d); \n" + "end\n" + "\n" + "\n" + "rule 'Extract Named Parameter out of Request - LikesBeets'\n" + "when\n" + "   $request: RequestData( $id: requestId != null, modelId != null, $parameters: parameters != null ) from requestData\n" + "   $param: ParameterHolder( capitalizedName == \"LikesBeets\" ) from $parameters\n" + "then\n" + "   System.out.printf(\"Parameter found: %s with value of %s%n\",$param.getCapitalizedName(),$param.getValue());\n" + "   InternalData d = InternalDataFactory.get().createInternalData($param);\n" + "   insert(d); \n" + "end\n" + "\n" + "\n" + "rule 'Extract Named Parameter out of Request - HairColor'\n" + "when\n" + "   $request: RequestData( $id: requestId != null, modelId != null, $parameters: parameters != null ) from requestData\n" + "   $param: ParameterHolder( capitalizedName == \"HairColor\" ) from $parameters\n" + "then\n" + "   System.out.printf(\"Parameter found: %s with value of %s%n\",$param.getCapitalizedName(),$param.getValue());\n" + "   InternalData d = InternalDataFactory.get().createInternalData($param);\n" + "   insert(d); \n" + "end\n" + "\n" + "rule 'Extract Named Parameter out of Request - Age'\n" + "when\n" + "   $request: RequestData( $id: requestId != null, modelId != null, $parameters: parameters != null ) from requestData\n" + "   $param: ParameterHolder( capitalizedName == \"Age\" ) from $parameters\n" + "then\n" + "   System.out.printf(\"Parameter found: %s with value of %f%n\",$param.getCapitalizedName(),$param.getValue());\n" + "   InternalData d = InternalDataFactory.get().createInternalData($param);\n" + "   insert(d); \n" + "end\n" + "\n" + "rule 'Check InternalData is inserted - Age' \n" + "when\n" + "   $i: InternalData( capitalizedName == \"Age\" )\n" + "then\n" + "   System.out.printf(\"Actual Age: %f   Factored Age: %f%n\",$i.getValue(),$i.getTransformed()); \n" + "end\n" + "";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    RequestData request = new RequestData("123", "simple");
    request.addParameter("name", "Lance", String.class);
    request.addParameter("age", 54.5, Double.class);
    request.addParameter("hairColor", "bald", String.class);
    request.addParameter("likesBeets", false, Boolean.class);
    DataSource<RequestData> requestData = executor.newDataSource("requestData", request);
    assertEquals(5, executor.run(ComplexDataUnit.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 29 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class RuleUnitTest method testModifyOnDataSource.

@Test
public void testModifyOnDataSource() throws Exception {
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "rule Adult @Unit( AdultUnit.class ) when\n" + "    Person(age >= 18, $name : name) from persons\n" + "then\n" + "    System.out.println($name + \" is adult\");\n" + "    results.add($name);\n" + "end\n" + "rule NotAdult @Unit( AdultUnit.class ) when\n" + "    $p : Person(age < 18, $name : name) from persons\n" + "then\n" + "    System.out.println($name + \" is NOT adult\");\n" + "    modify($p) { setAge(18); }\n" + "    drools.run( AdultUnit.class );" + "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));
    List<String> results = new ArrayList<>();
    executor.bindVariable("results", results);
    assertEquals(4, executor.run(AdultUnit.class));
    assertEquals(3, results.size());
    assertTrue(results.containsAll(asList("Mario", "Marilena", "Sofia")));
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 30 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class RuleUnitTest method testRuleUnitInvocationFromConsequence.

@Test
public void testRuleUnitInvocationFromConsequence() throws Exception {
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "import " + NotAdultUnit.class.getCanonicalName() + "\n" + "rule Adult @Unit( AdultUnit.class ) when\n" + "    Person(age >= 18, $name : name) from persons\n" + "then\n" + "    System.out.println($name + \" is adult\");\n" + "end\n" + "rule NotAdult @Unit( NotAdultUnit.class ) when\n" + "    $p : Person(age < 18, $name : name) from persons\n" + "then\n" + "    System.out.println($name + \" is NOT adult\");\n" + "    modify($p) { setAge(18); }\n" + "    drools.run( AdultUnit.class );" + "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));
    List<String> log = new ArrayList<>();
    executor.bindVariable("log", log);
    assertEquals(4, executor.run(NotAdultUnit.class));
    List<String> expectedLogs = asList("org.drools.compiler.integrationtests.RuleUnitTest$NotAdultUnit started.", "org.drools.compiler.integrationtests.RuleUnitTest$NotAdultUnit yielded to org.drools.compiler.integrationtests.RuleUnitTest$AdultUnit", "org.drools.compiler.integrationtests.RuleUnitTest$AdultUnit started.", "org.drools.compiler.integrationtests.RuleUnitTest$AdultUnit ended.", "org.drools.compiler.integrationtests.RuleUnitTest$NotAdultUnit started.", "org.drools.compiler.integrationtests.RuleUnitTest$NotAdultUnit ended.");
    assertEquals(expectedLogs, log);
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) Person(org.drools.compiler.Person) Test(org.junit.Test)

Aggregations

KieHelper (org.kie.internal.utils.KieHelper)467 Test (org.junit.Test)427 KieSession (org.kie.api.runtime.KieSession)355 ArrayList (java.util.ArrayList)194 KieBase (org.kie.api.KieBase)152 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)77 Person (org.drools.compiler.Person)61 FactHandle (org.kie.api.runtime.rule.FactHandle)55 List (java.util.List)44 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)41 RuleUnitExecutor (org.kie.api.runtime.rule.RuleUnitExecutor)38 InternalRuleUnitExecutor (org.drools.core.impl.InternalRuleUnitExecutor)37 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)34 Man (org.drools.compiler.oopath.model.Man)29 Child (org.drools.compiler.oopath.model.Child)24 Woman (org.drools.compiler.oopath.model.Woman)23 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)23 InternalFactHandle (org.drools.core.common.InternalFactHandle)18 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)18 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)18