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);
}
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));
}
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"));
}
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));
}
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();
}
Aggregations