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);
}
}
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();
}
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());
}
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")));
}
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());
}
Aggregations