use of org.drools.compiler.FactA in project drools by kiegroup.
the class ExecutionFlowControlTest method testEnabledExpressionWithOr.
@Test
public void testEnabledExpressionWithOr() throws Exception {
String text = "package org.kie.test\n" + "global java.util.List list\n" + "import " + FactA.class.getCanonicalName() + "\n" + "import " + Foo.class.getCanonicalName() + "\n" + "import " + Pet.class.getCanonicalName() + "\n" + "rule r1 salience(f1.field2) enabled(f1.field2 >= 20)\n" + "when\n" + " foo: Foo()\n" + " ( Pet() and f1 : FactA( field1 == 'f1') ) or \n" + " f1 : FactA(field1 == 'f2') \n" + "then\n" + " list.add( f1 );\n" + " foo.setId( 'xxx' );\n" + "end\n" + "\n";
KieBase kbase = loadKnowledgeBaseFromString(text);
KieSession ksession = kbase.newKieSession();
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert(new Foo(null, null));
ksession.insert(new Pet(null));
FactA fact1 = new FactA();
fact1.setField1("f1");
fact1.setField2(10);
FactA fact2 = new FactA();
fact2.setField1("f1");
fact2.setField2(30);
FactA fact3 = new FactA();
fact3.setField1("f2");
fact3.setField2(20);
ksession.insert(fact1);
ksession.insert(fact2);
ksession.insert(fact3);
ksession.fireAllRules();
assertEquals(2, list.size());
assertEquals(fact2, list.get(0));
assertEquals(fact3, list.get(1));
}
use of org.drools.compiler.FactA in project drools by kiegroup.
the class FirstOrderLogicTest method testOrWithVariableResolution.
// JBRULES-2482
@Test
public void testOrWithVariableResolution() throws Exception {
// KieBase kbase = loadKnowledgeBase( "test_OrCEFollowedByMultipleEval.drl");
// KieSession workingMemory = createKnowledgeSession(kbase);
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_OrCEFollowedByMultipleEval.drl", FirstOrderLogicTest.class), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
final KieSession ksession = createKnowledgeSession(kbase);
final AgendaEventListener al = mock(AgendaEventListener.class);
ksession.addEventListener(al);
ksession.insert(new FactA("a"));
ksession.insert(new FactB("b"));
ksession.insert(new FactC("c"));
ksession.fireAllRules();
verify(al, times(6)).afterMatchFired(any(AfterMatchFiredEvent.class));
}
use of org.drools.compiler.FactA in project drools by kiegroup.
the class IncrementalCompilationTest method testRemoveRuleAndThenFactInStreamMode.
@Test
public void testRemoveRuleAndThenFactInStreamMode() throws Exception {
// DROOLS-731
String header = "package org.some.test\n" + "import org.drools.compiler.FactA\n";
String declaration = "declare FactA\n" + "@role(event)" + "end\n";
String rule2 = "rule R when\n" + " $FactA : FactA ($FactA_field2 : field2 == 105742)\n" + " not FactA($FactA_field2 == 105742)\n" + "then\n" + "end\n";
String file2 = header + declaration + rule2;
KieServices ks = KieServices.Factory.get();
// Create an in-memory jar for version 1.0.0
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
KieModule km = createAndDeployJarInStreamMode(ks, releaseId1, file2);
// Create a session and fire rules
KieContainer kc = ks.newKieContainer(km.getReleaseId());
KieSession ksession = kc.newKieSession();
FactA factA = new FactA(105742);
factA.setField1("entry:" + 105742);
FactHandle fh = ksession.insert(factA);
// Create a new jar for version 1.1.0
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
km = createAndDeployJarInStreamMode(ks, releaseId2);
// try to update the container to version 1.1.0
kc.updateToVersion(releaseId2);
ksession.delete(fh);
}
use of org.drools.compiler.FactA in project drools by kiegroup.
the class TimerAndCalendarTest method testExprTimeRescheduled.
@Test(timeout = 10000)
public void testExprTimeRescheduled() throws Exception {
String text = "package org.kie.test\n" + "global java.util.List list\n" + "import " + FactA.class.getCanonicalName() + "\n" + "rule r1 timer (expr: f1.field2, f1.field4)\n" + "when\n" + " f1 : FactA() \n" + "then\n" + " list.add( f1 );\n" + "end\n" + "\n";
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
KieBase kbase = loadKnowledgeBaseFromString(text);
KieSession ksession = createKnowledgeSession(kbase, conf);
PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
timeService.advanceTime(new Date().getTime(), TimeUnit.MILLISECONDS);
List list = new ArrayList();
ksession.setGlobal("list", list);
FactA fact1 = new FactA();
fact1.setField1("f1");
fact1.setField2(500);
fact1.setField4(1000);
FactHandle fh = (FactHandle) ksession.insert(fact1);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(1100, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals(fact1, list.get(0));
timeService.advanceTime(1100, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(2, list.size());
assertEquals(fact1, list.get(1));
timeService.advanceTime(400, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(3, list.size());
assertEquals(fact1, list.get(2));
list.clear();
// the activation state of the rule is not changed so the timer isn't reset
// since the timer alredy fired it will only use only the period that now will be set to 2000
fact1.setField2(300);
fact1.setField4(2000);
ksession.update(fh, fact1);
ksession.fireAllRules();
// 100 has passed of the 1000, from the previous schedule
// so that should be deducted from the 2000 period above, meaning
// we only need to increment another 1950
timeService.advanceTime(1950, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals(fact1, list.get(0));
list.clear();
timeService.advanceTime(1000, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(700, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(0, list.size());
timeService.advanceTime(300, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(1, list.size());
}
use of org.drools.compiler.FactA in project drools by kiegroup.
the class PatternTest method testPatternOnClass.
@Test
public void testPatternOnClass() throws Exception {
final String rule = "import org.drools.core.reteoo.InitialFactImpl\n" + "import org.drools.compiler.FactB\n" + "rule \"Clear\" when\n" + " $f: Object(class != FactB.class)\n" + "then\n" + " if( ! ($f instanceof InitialFactImpl) ){\n" + " delete( $f );\n" + " }\n" + "end";
final KieBase kbase = loadKnowledgeBaseFromString(rule);
final KieSession ksession = kbase.newKieSession();
ksession.insert(new FactA());
ksession.insert(new FactA());
ksession.insert(new FactB());
ksession.insert(new FactB());
ksession.insert(new FactC());
ksession.insert(new FactC());
ksession.fireAllRules();
for (final FactHandle fact : ksession.getFactHandles()) {
final InternalFactHandle internalFact = (InternalFactHandle) fact;
assertTrue(internalFact.getObject() instanceof FactB);
}
}
Aggregations