Search in sources :

Example 66 with KieBase

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

the class RuleUnitTest method testDeleteFromDataSource.

@Test
public void testDeleteFromDataSource() throws Exception {
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + FlowUnit.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "import " + FilterUnit.class.getCanonicalName() + "\n" + "rule Flow @Unit( FlowUnit.class ) when\n" + "then\n" + "    drools.run( FilterUnit.class );\n" + "    drools.run( AdultUnit.class );\n" + "end\n" + "rule filter @Unit( FilterUnit.class ) when\n" + "    $p:Person(name str[startsWith] \"D\") from persons\n" + "then\n" + "    System.out.println(\"Deleting person: \" + $p.getName() + \". Sorry man, your name starts with a 'D' ....\");\n" + "    persons.delete( $p );\n" + "end\n" + "rule AdultUnit @Unit( AdultUnit.class ) when\n" + "    Person($age : age > 18, $name : name) from persons\n" + "then\n" + "    results.add($name);\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", 43), new Person("Duncan", 34), new Person("Sofia", 6));
    List<String> results = new ArrayList<>();
    executor.bindVariable("results", results);
    assertEquals(3, executor.run(FlowUnit.class));
    assertEquals(1, results.size());
    assertEquals("Mario", results.get(0));
}
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 67 with KieBase

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

the class RuleUnitTest method testUnitDeclaration.

@Test
public void testUnitDeclaration() throws Exception {
    String drl1 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnit.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult when\n" + "    Person(age >= adultAge, $name : name) from persons\n" + "then\n" + "    System.out.println($name + \" is adult\");\n" + "end";
    String drl2 = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(NotAdultUnit.class) + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule NotAdult 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 );\n" + "end";
    KieBase kbase = new KieHelper().addContent(drl1, ResourceType.DRL).addContent(drl2, 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).bindVariable("adultAge", 18);
    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)

Example 68 with KieBase

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

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

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

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