Search in sources :

Example 1 with KieSession

use of org.kie.api.runtime.KieSession in project graylog2-server by Graylog2.

the class DroolsEngine method stop.

public void stop() {
    LOG.debug("Stopping drools session and removing all rules.");
    final KieSession activeSession = session.getAndSet(null);
    if (activeSession != null) {
        activeSession.dispose();
    }
    if (currentReleaseId != null) {
        kieServices.getRepository().removeKieModule(currentReleaseId);
    }
}
Also used : KieSession(org.kie.api.runtime.KieSession)

Example 2 with KieSession

use of org.kie.api.runtime.KieSession in project opennms by OpenNMS.

the class CorrelationExample method main.

/**
 * <p>main</p>
 *
 * @param args an array of {@link java.lang.String} objects.
 * @throws java.lang.Exception if any.
 */
public static void main(final String[] args) throws Exception {
    final KieSession session = new KieHelper().addResource(new ClassPathResource("CorrelationExample.drl")).build().newKieSession();
    KieRuntimeLogger logger = KieServices.Factory.get().getLoggers().newFileLogger(session, "log/correlation");
    try (InputStream in = CorrelationExample.class.getResourceAsStream("simulation")) {
        final Simulation simulation = new Simulation();
        System.out.println("Loading Simulation");
        simulation.load(in);
        System.out.println("Executing Simulation");
        simulation.simulate(session);
    }
    logger.close();
}
Also used : KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) InputStream(java.io.InputStream) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) ClassPathResource(org.drools.core.io.impl.ClassPathResource)

Example 3 with KieSession

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

the class CompilerTest method testNullSafeDereferncing.

@Test
public void testNullSafeDereferncing() {
    String str = "import " + Result.class.getCanonicalName() + ";" + "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + "  $r : Result()\n" + "  $p : Person( name!.length == 4 )\n" + "then\n" + "  $r.setValue(\"Found: \" + $p);\n" + "end";
    KieSession ksession = getKieSession(str);
    Result result = new Result();
    ksession.insert(result);
    ksession.insert(new Person("Mark", 37));
    ksession.insert(new Person("Mario", 40));
    ksession.insert(new Person(null, 40));
    ksession.fireAllRules();
    assertEquals("Found: Mark", result.getValue());
}
Also used : KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Example 4 with KieSession

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

the class CompilerTest method testNamedConsequence.

@Test
public void testNamedConsequence() {
    String str = "import " + Result.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "rule R when\n" + "  $r : Result()\n" + "  $p1 : Person(name == \"Mark\")\n" + "  do[FoundMark]\n" + "  $p2 : Person(name != \"Mark\", age > $p1.age)\n" + "then\n" + "  $r.addValue($p2.getName() + \" is older than \" + $p1.getName());\n" + "then[FoundMark]\n" + "  $r.addValue(\"Found \" + $p1.getName());\n" + "end";
    KieSession ksession = getKieSession(str);
    Result result = new Result();
    ksession.insert(result);
    ksession.insert(new Person("Mark", 37));
    ksession.insert(new Person("Edson", 35));
    ksession.insert(new Person("Mario", 40));
    ksession.fireAllRules();
    Collection results = (Collection) result.getValue();
    assertEquals(2, results.size());
    assertTrue(results.containsAll(asList("Found Mark", "Mario is older than Mark")));
}
Also used : Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Result(org.drools.modelcompiler.domain.Result) Test(org.junit.Test)

Example 5 with KieSession

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

the class FlowTest method testAccumulate1.

@Test
public void testAccumulate1() {
    Result result = new Result();
    Variable<Person> person = declarationOf(Person.class);
    Variable<Integer> resultSum = declarationOf(Integer.class);
    Variable<Integer> age = declarationOf(Integer.class);
    Rule rule = rule("accumulate").build(bind(age).as(person, Person::getAge), accumulate(expr(person, p -> p.getName().startsWith("M")), accFunction(org.drools.core.base.accumulators.IntegerSumAccumulateFunction.class, age).as(resultSum)), on(resultSum).execute(sum -> result.setValue("total = " + sum)));
    Model model = new ModelImpl().addRule(rule);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model);
    KieSession ksession = kieBase.newKieSession();
    ksession.insert(new Person("Mark", 37));
    ksession.insert(new Person("Edson", 35));
    ksession.insert(new Person("Mario", 40));
    ksession.fireAllRules();
    assertEquals("total = 77", result.getValue());
}
Also used : Arrays(java.util.Arrays) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) FlowDSL.eval(org.drools.model.FlowDSL.eval) ObjectOutput(java.io.ObjectOutput) Man(org.drools.modelcompiler.domain.Man) Global(org.drools.model.Global) FlowDSL.not(org.drools.model.FlowDSL.not) Toy(org.drools.modelcompiler.domain.Toy) FlowDSL.and(org.drools.model.FlowDSL.and) FlowDSL.accFunction(org.drools.model.FlowDSL.accFunction) Relationship(org.drools.modelcompiler.domain.Relationship) BaseModelTest.getObjectsIntoList(org.drools.modelcompiler.BaseModelTest.getObjectsIntoList) Assert.assertThat(org.junit.Assert.assertThat) Query2Def(org.drools.model.Query2Def) Child(org.drools.modelcompiler.domain.Child) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) FlowDSL.from(org.drools.model.FlowDSL.from) QueryResults(org.kie.api.runtime.rule.QueryResults) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) FlowDSL.reactiveFrom(org.drools.model.FlowDSL.reactiveFrom) Assertions(org.assertj.core.api.Assertions) KieSession(org.kie.api.runtime.KieSession) FlowDSL.declarationOf(org.drools.model.FlowDSL.declarationOf) TargetPolicy(org.drools.modelcompiler.domain.TargetPolicy) FlowDSL.or(org.drools.model.FlowDSL.or) EventProcessingOption(org.kie.api.conf.EventProcessingOption) Collection(java.util.Collection) FlowDSL.on(org.drools.model.FlowDSL.on) Customer(org.drools.modelcompiler.domain.Customer) Serializable(java.io.Serializable) ConstraintType(org.drools.model.Index.ConstraintType) List(java.util.List) Query(org.drools.model.Query) FlowDSL.executeScript(org.drools.model.FlowDSL.executeScript) InternationalAddress(org.drools.modelcompiler.oopathdtables.InternationalAddress) Assert.assertFalse(org.junit.Assert.assertFalse) KnowledgeBaseFactory(org.drools.core.impl.KnowledgeBaseFactory) FlowDSL.bind(org.drools.model.FlowDSL.bind) FlowDSL.valueOf(org.drools.model.FlowDSL.valueOf) Person(org.drools.modelcompiler.domain.Person) ObjectInput(java.io.ObjectInput) InOperator(org.drools.model.operators.InOperator) FlowDSL.when(org.drools.model.FlowDSL.when) ModelImpl(org.drools.model.impl.ModelImpl) FlowDSL.globalOf(org.drools.model.FlowDSL.globalOf) StockTick(org.drools.modelcompiler.domain.StockTick) Employee.createEmployee(org.drools.modelcompiler.domain.Employee.createEmployee) ClockType(org.drools.core.ClockType) FlowDSL.accumulate(org.drools.model.FlowDSL.accumulate) ArrayList(java.util.ArrayList) Result(org.drools.modelcompiler.domain.Result) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Adult(org.drools.modelcompiler.domain.Adult) Employee(org.drools.modelcompiler.domain.Employee) FlowDSL.execute(org.drools.model.FlowDSL.execute) Woman(org.drools.modelcompiler.domain.Woman) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) FlowDSL.rule(org.drools.model.FlowDSL.rule) FlowDSL.expr(org.drools.model.FlowDSL.expr) Variable(org.drools.model.Variable) FlowDSL.query(org.drools.model.FlowDSL.query) Address(org.drools.modelcompiler.domain.Address) FlowDSL.window(org.drools.model.FlowDSL.window) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) FlowDSL.input(org.drools.model.FlowDSL.input) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) TimeUnit(java.util.concurrent.TimeUnit) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) Assert.assertNull(org.junit.Assert.assertNull) FlowDSL.forall(org.drools.model.FlowDSL.forall) Rule(org.drools.model.Rule) Query1Def(org.drools.model.Query1Def) Assert.assertEquals(org.junit.Assert.assertEquals) Result(org.drools.modelcompiler.domain.Result) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) KieSession(org.kie.api.runtime.KieSession) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Aggregations

KieSession (org.kie.api.runtime.KieSession)5328 Test (org.junit.Test)4824 KieBase (org.kie.api.KieBase)2414 ArrayList (java.util.ArrayList)2317 List (java.util.List)1105 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)769 FactHandle (org.kie.api.runtime.rule.FactHandle)598 Person (org.drools.modelcompiler.domain.Person)519 HashMap (java.util.HashMap)416 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)415 KieServices (org.kie.api.KieServices)382 KieHelper (org.kie.internal.utils.KieHelper)355 KieContainer (org.kie.api.runtime.KieContainer)298 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)265 InternalFactHandle (org.drools.core.common.InternalFactHandle)259 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)234 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)234 ReleaseId (org.kie.api.builder.ReleaseId)232 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)229 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)207