Search in sources :

Example 26 with KieBase

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

the class DRLTest method testEventsInDifferentPackages.

@Test
public void testEventsInDifferentPackages() {
    final String str = "package org.drools.compiler.test\n" + "import org.drools.compiler.*\n" + "declare StockTick\n" + "    @role( event )\n" + "end\n" + "rule r1\n" + "when\n" + "then\n" + "    StockTick st = new StockTick();\n" + "    st.setCompany(\"RHT\");\n" + "end\n";
    final KieBase kbase = loadKnowledgeBaseFromString(str);
    final KieSession ksession = createKnowledgeSession(kbase);
    final int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 27 with KieBase

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

the class DRLTest method testDRLWithoutPackageDeclaration.

@Test
public void testDRLWithoutPackageDeclaration() throws Exception {
    final KieBase kbase = loadKnowledgeBase("test_NoPackageDeclaration.drl");
    // no package defined, so it is set to the default
    final FactType factType = kbase.getFactType("defaultpkg", "Person");
    assertNotNull(factType);
    final Object bob = factType.newInstance();
    factType.set(bob, "name", "Bob");
    factType.set(bob, "age", Integer.valueOf(30));
    final KieSession session = createKnowledgeSession(kbase);
    final List results = new ArrayList();
    session.setGlobal("results", results);
    session.insert(bob);
    session.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(bob, results.get(0));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 28 with KieBase

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

the class DRLTest method testRuleMetaAttributes.

@Test
public void testRuleMetaAttributes() throws Exception {
    String drl = "";
    drl += "package test\n";
    drl += "rule \"test meta attributes\"\n";
    drl += "    @id(1234 ) @author(  john_doe  ) @text(\"It's an escaped\\\" string\"  )\n";
    drl += "when\n";
    drl += "then\n";
    drl += "    // some comment\n";
    drl += "end\n";
    final KieBase kbase = loadKnowledgeBaseFromString(drl);
    final Rule rule = kbase.getRule("test", "test meta attributes");
    assertNotNull(rule);
    assertThat(rule.getMetaData().get("id"), is(1234));
    assertThat(rule.getMetaData().get("author"), is("john_doe"));
    assertThat(rule.getMetaData().get("text"), is("It's an escaped\" string"));
}
Also used : KieBase(org.kie.api.KieBase) Rule(org.kie.api.definition.rule.Rule) Test(org.junit.Test)

Example 29 with KieBase

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

the class DeclareTest method testDeclaredFactAndFunction.

@Test
public void testDeclaredFactAndFunction() throws Exception {
    String rule = "package com.jboss.qa;\n";
    rule += "global java.util.List list\n";
    rule += "declare Address\n";
    rule += "    street: String\n";
    rule += "end\n";
    rule += "function void myFunction() {\n";
    rule += "}\n";
    rule += "rule \"r1\"\n";
    rule += "    dialect \"mvel\"\n";
    rule += "when\n";
    rule += "    Address()\n";
    rule += "then\n";
    rule += "    list.add(\"r1\");\n";
    rule += "end\n";
    final KieBase kbase = loadKnowledgeBaseFromString(rule);
    final KieSession session = createKnowledgeSession(kbase);
    List list = new ArrayList();
    session.setGlobal("list", list);
    final FactType addressFact = kbase.getFactType("com.jboss.qa", "Address");
    final Object address = addressFact.newInstance();
    session.insert(address);
    session.fireAllRules();
    list = (List) session.getGlobal("list");
    assertEquals(1, list.size());
    assertEquals("r1", list.get(0));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 30 with KieBase

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

the class DeclareTest method testConstructorWithOtherDefaults.

@Test
public void testConstructorWithOtherDefaults() {
    final String str = "" + "\n" + "global java.util.List list;\n" + "\n" + "declare Bean\n" + "   kField : String     @key\n" + "   sField : String     = \"a\"\n" + "   iField : int        = 10\n" + "   dField : double     = 4.32\n" + "   aField : Long[]     = new Long[] { 100L, 1000L }\n" + "end" + "\n" + "rule \"Trig\"\n" + "when\n" + "    Bean( kField == \"key\", sField == \"a\", iField == 10, dField == 4.32, aField[1] == 1000L ) \n" + "then\n" + "    list.add( \"OK\" );\n" + "end\n" + "\n" + "rule \"Exec\"\n" + "when\n" + "then\n" + "    insert( new Bean( \"key\") ); \n" + "end";
    final KieBase kbase = loadKnowledgeBaseFromString(str);
    final KieSession ksession = kbase.newKieSession();
    final java.util.List list = new java.util.ArrayList();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertTrue(list.contains("OK"));
    ksession.dispose();
}
Also used : List(java.util.List) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) 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