use of org.kie.api.definition.type.FactType 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.definition.type.FactType 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.definition.type.FactType in project drools by kiegroup.
the class DeclareTest method testDeclareAndFrom.
@Test
public void testDeclareAndFrom() throws Exception {
final KieBase kbase = loadKnowledgeBase("test_DeclareWithFrom.drl");
final FactType profileType = kbase.getFactType("org.drools.compiler", "Profile");
final KieSession ksession = createKnowledgeSession(kbase);
final Object profile = profileType.newInstance();
final Map<String, Integer> map = new HashMap<String, Integer>();
map.put("internet", Integer.valueOf(2));
profileType.set(profile, "pageFreq", map);
ksession.insert(profile);
ksession.fireAllRules();
ksession.dispose();
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class IncrementalCompilationTest method doFire.
private void doFire(KieBase kbase, String reply) {
FactType ftype = kbase.getFactType("org.drools.example.api.kiedeclare", "Message");
KieSession kSession = kbase.newKieSession();
kSession.insert(createMessage(ftype, "Dave", "What's the problem?"));
assertEquals(1, kSession.fireAllRules());
assertEquals(1, kSession.getObjects().size());
Object fact = kSession.getObjects().iterator().next();
assertEquals("HAL", ftype.get(fact, "name"));
assertEquals(reply, ftype.get(fact, "text"));
kSession.dispose();
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class StreamsTest method testAtomicActivationFiring.
@Test(timeout = 10000)
public void testAtomicActivationFiring() throws Exception {
// JBRULES-3383
String str = "package org.drools.compiler.test\n" + "declare Event\n" + " @role(event)\n" + " name : String\n" + "end\n" + "declare Monitor\n" + " @role(event)\n" + " event : Event\n" + " name : String\n" + "end\n" + "\n" + "rule \"start monitoring\"\n" + "when\n" + " $e : Event( $in : name )\n" + " not Monitor( name == $in )\n" + "then\n" + " Monitor m = new Monitor( $e, $in );\n" + " insert( m );\n" + "end\n" + "\n" + "rule \"stop monitoring\"\n" + "timer( int: 1s )\n" + "when\n" + " $m : Monitor( $in : name )\n" + " $e : Event( name == $in )\n" + "then\n" + " retract( $m );\n" + " retract( $m.getEvent() );\n" + "end\n" + "rule \"halt\"\n" + "salience -1\n" + "when\n" + " not Event( )\n" + "then\n" + " drools.halt();\n" + "end\n";
KieBaseConfiguration kBaseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kBaseConfig.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(kBaseConfig, str);
KieSession ksession = createKnowledgeSession(kbase);
ksession.addEventListener(new org.kie.api.event.rule.DebugAgendaEventListener());
FactType eventType = kbase.getFactType("org.drools.compiler.test", "Event");
Object event = eventType.newInstance();
eventType.set(event, "name", "myName");
ksession.insert(event);
ksession.fireUntilHalt();
}
Aggregations