Created
June 28, 2021 09:50
-
-
Save evacchi/4e4705cdb039b04cc3122d6655ec5f80 to your computer and use it in GitHub Desktop.
jbang drools.java (improved)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///usr/bin/env jbang "$0" "$@" ; exit $? | |
//JAVA 16 | |
//REPOS jitpack=https://jitpack.io/ | |
//DEPS com.github.evacchi:drools-jbang-starter:main-SNAPSHOT | |
// example | |
// jbang <url> '{ "name":"Mario", "age":40 }' '{ "name":"Mark", "age":37 }' | |
package org.kie.example; | |
import static java.lang.System.*; | |
import org.kie.api.*; | |
import org.kie.api.builder.*; | |
import org.kie.api.runtime.*; | |
import com.github.evacchi.*; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
public class drools { | |
public static record Person(String name, int age) {} | |
final static String drl = """ | |
package org.kie.example; | |
import org.kie.example.drools.*; | |
rule MyRule when | |
$mark: Person(name == "Mark") | |
$older: Person(name != "Mark" && age > $mark.age) | |
then | |
System.out.printf("%s is older than %s", $older.name(), $mark.name()); | |
System.out.println(); | |
end | |
"""; | |
public static void main(String... args) throws Exception { | |
var kieSession = | |
DroolsLauncher.create() | |
.withRules(drl) | |
.session(); | |
var mapper = new ObjectMapper(); | |
for (var s: args) { | |
var person = mapper.readValue(s, Person.class); | |
kieSession.insert(person); | |
} | |
kieSession.fireAllRules(); | |
kieSession.dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment