use of org.drools.compiler.StockTick in project drools by kiegroup.
the class DeserializationWithCompositeTriggerTest method testSerializationAndDeserialization.
/**
* Verifies that serialization of a rule with composite trigger does not fail on
* org.drools.core.time.impl.CompositeMaxDurationTrigger class serialization.
*/
@Test
public void testSerializationAndDeserialization() throws Exception {
this.ksession.insert(new StockTick(2, "AAA", 1.0, 0));
this.ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
Assertions.assertThat(this.ksession).isNotNull();
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class LifecycleTest method testExpires.
@Test
public void testExpires() throws Exception {
EntryPoint entryPoint = kieSession.getEntryPoint("EventStream");
StockTick event = new StockTick();
FactHandle handle = entryPoint.insert(event);
assertTrue(entryPoint.getFactHandles().contains(handle));
kieSession.fireAllRules();
assertTrue(entryPoint.getFactHandles().contains(handle));
advanceTime(5, TimeUnit.SECONDS);
kieSession.fireAllRules();
assertFalse(entryPoint.getFactHandles().contains(handle));
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class FirstOrderLogicTest method testForallWithSlidingWindow.
@Test
public void testForallWithSlidingWindow() throws Exception {
final KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieBase kbase = loadKnowledgeBase("test_ForallSlidingWindow.drl");
KieSession ksession = createKnowledgeSession(kbase, conf);
final SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
List<String> results = new ArrayList<String>();
ksession.setGlobal("results", results);
// advance time... no events, so forall should fire
clock.advanceTime(60, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, results.size());
int seq = 1;
// advance time... there are matching events now, but forall still not fire
ksession.insert(new StockTick(seq++, "RHT", 10, // 60
clock.getCurrentTime()));
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, results.size());
ksession.insert(new StockTick(seq++, "RHT", 10, // 65
clock.getCurrentTime()));
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, results.size());
// advance time... there are non-matching events now, so forall de-activates
ksession.insert(new StockTick(seq++, "IBM", 10, // 70
clock.getCurrentTime()));
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, results.size());
// advance time... there are non-matching events now, so forall is still deactivated
ksession.insert(new StockTick(seq++, "RHT", 10, // 80
clock.getCurrentTime()));
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(1, results.size());
// advance time... non-matching event expires now, so forall should fire
ksession.insert(new StockTick(seq++, "RHT", 10, // 90
clock.getCurrentTime()));
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, results.size());
// advance time... forall still matches and should not fire
ksession.insert(new StockTick(seq++, "RHT", 10, // 100
clock.getCurrentTime()));
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, results.size());
// advance time... forall still matches and should not fire
clock.advanceTime(60, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(2, results.size());
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class LengthSlidingWindowTest method checkPrice.
private void checkPrice(String drl, double expectedPrice) {
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build(EventProcessingOption.STREAM).newKieSession(sessionConfig, null);
List<Double> list = new ArrayList<Double>();
ksession.setGlobal("list", list);
ksession.insert("RHT");
ksession.insert(new StockTick(1L, "RHT", 10.0));
ksession.insert(new StockTick(2L, "RHT", 10.0));
ksession.insert(new StockTick(3L, "ABC", 20.0));
ksession.insert(new StockTick(4L, "RHT", 10.0));
ksession.insert(new StockTick(5L, "XYZ", 20.0));
ksession.insert(new StockTick(6L, "XYZ", 20.0));
ksession.insert(new StockTick(7L, "RHT", 10.0));
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals(expectedPrice, (double) list.get(0), 0.01);
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class LengthSlidingWindowTest method testCompilationFailureWithUnknownWindow.
@Test
public void testCompilationFailureWithUnknownWindow() {
// DROOLS-841
String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "global java.util.List list;\n" + "declare StockTick @role( event ) end\n" + "declare window RhtStocksWindow\n" + " StockTick() over window:length( 3 )\n" + "end\n" + "rule R\n" + "when \n" + " accumulate( StockTick( company == \"RHT\", $price : price ) from window AbcStocksWindow; $total : sum($price) )\n" + "then \n" + " list.add($total);\n" + "end \n";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
Results results = ks.newKieBuilder(kfs).buildAll().getResults();
assertEquals(1, results.getMessages().size());
}
Aggregations