Search in sources :

Example 1 with KieHelper

use of org.kie.internal.utils.KieHelper in project opennms by OpenNMS.

the class CorrelationExample method main.

/**
 * <p>main</p>
 *
 * @param args an array of {@link java.lang.String} objects.
 * @throws java.lang.Exception if any.
 */
public static void main(final String[] args) throws Exception {
    final KieSession session = new KieHelper().addResource(new ClassPathResource("CorrelationExample.drl")).build().newKieSession();
    KieRuntimeLogger logger = KieServices.Factory.get().getLoggers().newFileLogger(session, "log/correlation");
    try (InputStream in = CorrelationExample.class.getResourceAsStream("simulation")) {
        final Simulation simulation = new Simulation();
        System.out.println("Loading Simulation");
        simulation.load(in);
        System.out.println("Executing Simulation");
        simulation.simulate(session);
    }
    logger.close();
}
Also used : KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) InputStream(java.io.InputStream) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) ClassPathResource(org.drools.core.io.impl.ClassPathResource)

Example 2 with KieHelper

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

the class TemplateIntegrationTest method test.

@Test
public void test() {
    String drl = "global java.util.List list\n" + "rule \"String detector\"\n" + "    when\n" + "        $s : String( )\n" + "    then\n" + "        list.add($s);\n" + "end";
    final KieSession ksession = new KieHelper().addContent(template, ResourceType.TEMPLATE).build().newKieSession();
    Company myCompany = new Company(123, "myCompany");
    Company yourCompany = new Company(456, "yourCompany");
    ksession.insert(myCompany);
    ksession.insert(yourCompany);
    ksession.fireAllRules();
    assertTrue(myCompany.isFound());
    assertFalse(yourCompany.isFound());
}
Also used : KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 3 with KieHelper

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

the class TemplateWithDSLIntegrationTest method testCompileTemplateWithDSL.

@Test
public void testCompileTemplateWithDSL() {
    String template = "<rule>\n" + "  <name>guided-template-with-dsl</name>\n" + "  <modelVersion>1.0</modelVersion>\n" + "  <attributes/>\n" + "  <metadataList/>\n" + "  <lhs>\n" + "    <dslSentence>\n" + "      <drl>applicant:" + Applicant.class.getCanonicalName() + "(approved=={bool})</drl>\n" + "      <definition>When the applicant approval is {bool:BOOLEAN:checked}</definition>\n" + "      <values>\n" + "        <org.drools.workbench.models.datamodel.rule.DSLVariableValue>\n" + "          <value>false</value>\n" + "        </org.drools.workbench.models.datamodel.rule.DSLVariableValue>\n" + "        <org.drools.workbench.models.datamodel.rule.DSLComplexVariableValue>\n" + "          <value>bool</value>\n" + "          <id>BOOLEAN:checked</id>\n" + "        </org.drools.workbench.models.datamodel.rule.DSLComplexVariableValue>\n" + "      </values>\n" + "    </dslSentence>\n" + "  </lhs>\n" + "  <rhs>\n" + "    <dslSentence>\n" + "      <drl>applicant.setApproved(true)</drl>\n" + "      <definition>Approve the loan</definition>\n" + "      <values/>\n" + "    </dslSentence>\n" + "  </rhs>\n" + "  <imports>\n" + "    <imports/>\n" + "  </imports>\n" + "  <packageName>org.mortgages</packageName>\n" + "  <isNegated>false</isNegated>\n" + "  <table>\n" + "    <entry>\n" + "      <string>__ID_KOL_NAME__</string>\n" + "      <list>\n" + "        <string>1</string>\n" + "      </list>\n" + "    </entry>\n" + "  </table>\n" + "  <idCol>1</idCol>\n" + "  <rowsCount>1</rowsCount>\n" + "</rule>";
    String dsl = "[when]When the applicant approval is {bool:BOOLEAN:checked} = applicant:" + Applicant.class.getCanonicalName() + "(approved=={bool})\n" + "[then]Approve the loan = applicant.setApproved(true)";
    KieHelper kieHelper = new KieHelper();
    KieSession kieSession = kieHelper.addContent(template, ResourceType.TEMPLATE).addContent(dsl, ResourceType.DSL).build().newKieSession();
    Applicant applicant = new Applicant();
    applicant.setApproved(false);
    kieSession.insert(applicant);
    int rulesFired = kieSession.fireAllRules();
    Assert.assertEquals("Incorrect number of rules fired!", 1, rulesFired);
    Assert.assertEquals("Rule RHS wasn't triggered!", true, applicant.isApproved());
}
Also used : KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 4 with KieHelper

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

the class TruthMaintenanceTest method testDeleteLogicalAssertionFromRule.

@Test
public void testDeleteLogicalAssertionFromRule() {
    // BZ-1317026
    String drl = "global java.util.List list;\n" + "rule R1 when\n" + "then\n" + "    insertLogical( \"test\" ); \n" + "end\n" + "rule R2 when\n" + "    $s : String()\n" + "then\n" + "    list.add( $s ); \n" + "    delete( $s ); \n" + "end\n";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("test", list.get(0));
    Collection<FactHandle> fhs = ksession.getFactHandles(new ClassObjectFilter(String.class));
    assertEquals(0, fhs.size());
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 5 with KieHelper

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

the class TruthMaintenanceTest method testStatedDeleteLogicalAssertion.

@Test
public void testStatedDeleteLogicalAssertion() {
    // BZ-1317026
    String drl = "rule R1 when\n" + "then\n" + "    insertLogical( \"test\" ); \n" + "end\n";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    ksession.fireAllRules();
    Collection<FactHandle> fhs = ksession.getFactHandles(new ClassObjectFilter(String.class));
    assertEquals(1, fhs.size());
    for (FactHandle fh : fhs) {
        ksession.delete(fh, FactHandle.State.STATED);
    }
    fhs = ksession.getFactHandles(new ClassObjectFilter(String.class));
    assertEquals(1, fhs.size());
    for (FactHandle fh : fhs) {
        ksession.delete(fh, FactHandle.State.LOGICAL);
    }
    fhs = ksession.getFactHandles(new ClassObjectFilter(String.class));
    assertEquals(0, fhs.size());
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) 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