Search in sources :

Example 91 with KieSessionConfiguration

use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.

the class KieSessionUtil method getKieSessionConfigurationWithClock.

public static KieSessionConfiguration getKieSessionConfigurationWithClock(final ClockTypeOption clockType, final Properties sessionProperties) {
    final KieSessionConfiguration conf = KieServices.Factory.get().newKieSessionConfiguration(sessionProperties);
    conf.setOption(clockType);
    return conf;
}
Also used : KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration)

Example 92 with KieSessionConfiguration

use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.

the class EventDeserializationInPastTest method testSerializationWithEventInPastBZ1205666.

@Test
public void testSerializationWithEventInPastBZ1205666() {
    // DROOLS-749
    final String drl = "import " + Event1.class.getCanonicalName() + "\n" + "declare Event1\n" + " @role( event )\n" + " @timestamp( timestamp )\n" + " @expires( 3h )\n" + "end\n" + "\n" + "rule R\n" + " when\n" + " $evt: Event1()\n" + " not Event1(this != $evt, this after[0, 1h] $evt)\n" + " then\n" + " System.out.println($evt.getCode());\n" + "end\n";
    final KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    final KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    final KieBase kbase = helper.build(EventProcessingOption.STREAM);
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    ksession.insert(new Event1("id1", 0));
    final PseudoClockScheduler clock = ksession.getSessionClock();
    clock.advanceTime(2, TimeUnit.HOURS);
    ksession.fireAllRules();
    ksession = marshallAndUnmarshall(KieServices.Factory.get(), kbase, ksession, sessionConfig);
    ksession.insert(new Event1("id2", 0));
    ksession.fireAllRules();
}
Also used : KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 93 with KieSessionConfiguration

use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.

the class FusionAfterBeforeTest method testAfterBeforeOperators.

@Test
public void testAfterBeforeOperators() {
    final Resource drlResource = KieServices.Factory.get().getResources().newClassPathResource("fusionAfterBeforeTest.drl", getClass());
    final KieBase kieBase = KieBaseUtil.getKieBaseAndBuildInstallModule(TestConstants.PACKAGE_REGRESSION, kieBaseTestConfiguration, drlResource);
    final KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get("pseudo"));
    final KieSession ksession = kieBase.newKieSession(ksconf, null);
    final TrackingAgendaEventListener listener = new TrackingAgendaEventListener();
    ksession.addEventListener(listener);
    final EntryPoint stream = ksession.getEntryPoint("EventStream");
    SessionPseudoClock clock = ksession.getSessionClock();
    try {
        for (int i = 0; i < 3; i++) {
            final MessageEvent tc = new MessageEvent(MessageEvent.Type.received, new Message());
            stream.insert(tc);
            ksession.fireAllRules();
            clock.advanceTime(8, TimeUnit.SECONDS);
        }
        ksession.fireAllRules();
    } finally {
        ksession.dispose();
    }
    Assertions.assertThat(listener.isRuleFired("AfterMessageEvent")).as("Rule 'AfterMessageEvent' was no fired!").isTrue();
    Assertions.assertThat(listener.isRuleFired("BeforeMessageEvent")).as("Rule 'BeforeMessageEvent' was no fired!").isTrue();
    // each rules should be fired 2 times
    int firedCount = 2;
    int actuallyFired = listener.ruleFiredCount("AfterMessageEvent");
    Assertions.assertThat(firedCount).as("Rule 'AfterMessageEvent' should be fired 2 times, but was fired " + firedCount + " time(s)!").isEqualTo(actuallyFired);
    firedCount = listener.ruleFiredCount("BeforeMessageEvent");
    Assertions.assertThat(firedCount).as("Rule 'BeforeMessageEvent' should be fired 2 times, but was fired " + firedCount + " time(s)!").isEqualTo(actuallyFired);
}
Also used : Message(org.drools.testcoverage.common.model.Message) TrackingAgendaEventListener(org.drools.testcoverage.common.listener.TrackingAgendaEventListener) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) MessageEvent(org.drools.testcoverage.common.model.MessageEvent) Resource(org.kie.api.io.Resource) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 94 with KieSessionConfiguration

use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.

the class KnowledgeStoreServiceImpl method mergeConfig.

private KieSessionConfiguration mergeConfig(KieSessionConfiguration configuration) {
    KieSessionConfiguration merged = ((SessionConfiguration) configuration).addDefaultProperties(configProps);
    merged.setOption(TimerJobFactoryOption.get("jpa"));
    return merged;
}
Also used : KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration)

Example 95 with KieSessionConfiguration

use of org.kie.api.runtime.KieSessionConfiguration in project ff4j by ff4j.

the class FF4jDroolsService method initFromRulesFiles.

/**
 * Initialisation of Drools stateful session without convention and kodmule files.
 *
 * @param ruleFiles
 *      DRL files
 */
public static synchronized void initFromRulesFiles(Set<String> ruleFiles) {
    if (isInitialized()) {
        throw new IllegalStateException("This Factory has already be initialized once");
    }
    _instance = new FF4jDroolsService();
    _instance.ruleFiles = ruleFiles;
    KieHelper helper = new KieHelper();
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    // ruleFiles.stream().forEach(drlFile -> helper.addContent(loadResourceAsString(drlFile), ResourceType.determineResourceType(drlFile)));
    for (String drlFile : ruleFiles) {
        String fileContent = loadResourceAsString(drlFile);
        ResourceType typeFile = ResourceType.determineResourceType(drlFile);
        helper.addContent(fileContent, typeFile);
    }
    _instance.ksession = helper.build(EventProcessingOption.STREAM).newKieSession(sessionConfig, null);
}
Also used : KieHelper(org.kie.internal.utils.KieHelper) ResourceType(org.kie.api.io.ResourceType) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration)

Aggregations

KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)204 KieSession (org.kie.api.runtime.KieSession)168 Test (org.junit.Test)160 KieBase (org.kie.api.KieBase)126 ArrayList (java.util.ArrayList)98 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)71 KieHelper (org.kie.internal.utils.KieHelper)69 List (java.util.List)59 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)57 StockTick (org.drools.compiler.StockTick)40 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)39 EntryPoint (org.kie.api.runtime.rule.EntryPoint)28 Arrays.asList (java.util.Arrays.asList)27 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)26 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)22 Date (java.util.Date)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 StockTickInterface (org.drools.compiler.StockTickInterface)18 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)16 SessionConfiguration (org.drools.core.SessionConfiguration)15