summaryrefslogtreecommitdiff
path: root/src/main/java/io/trygvis/rules/engine
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/io/trygvis/rules/engine')
-rw-r--r--src/main/java/io/trygvis/rules/engine/KeyValue.java14
-rw-r--r--src/main/java/io/trygvis/rules/engine/Main.java6
-rw-r--r--src/main/java/io/trygvis/rules/engine/TemplateEngine.java30
3 files changed, 49 insertions, 1 deletions
diff --git a/src/main/java/io/trygvis/rules/engine/KeyValue.java b/src/main/java/io/trygvis/rules/engine/KeyValue.java
new file mode 100644
index 0000000..5046169
--- /dev/null
+++ b/src/main/java/io/trygvis/rules/engine/KeyValue.java
@@ -0,0 +1,14 @@
+package io.trygvis.rules.engine;
+
+public class KeyValue {
+ public String key;
+ public String value;
+
+ public KeyValue() {
+ }
+
+ public KeyValue(String key, String value) {
+ this.key = key;
+ this.value = value;
+ }
+}
diff --git a/src/main/java/io/trygvis/rules/engine/Main.java b/src/main/java/io/trygvis/rules/engine/Main.java
index 3730c28..d469f3a 100644
--- a/src/main/java/io/trygvis/rules/engine/Main.java
+++ b/src/main/java/io/trygvis/rules/engine/Main.java
@@ -2,7 +2,6 @@ package io.trygvis.rules.engine;
import io.trygvis.rules.acme.AcmeIo;
import org.drools.core.audit.WorkingMemoryConsoleLogger;
-import org.drools.core.common.DefaultFactHandle;
import org.kie.api.KieServices;
import org.kie.api.event.rule.AgendaEventListener;
import org.kie.api.event.rule.RuleRuntimeEventListener;
@@ -19,6 +18,8 @@ public class Main {
var container = services.getKieClasspathContainer();
var session = container.newKieSession();
+ session.setGlobal("te", new TemplateEngine());
+
for (var object : objects) {
System.out.println("object = " + object);
session.insert(object);
@@ -27,6 +28,9 @@ public class Main {
var logger = new WorkingMemoryConsoleLogger(session);
session.addEventListener((AgendaEventListener) logger);
session.addEventListener((RuleRuntimeEventListener) logger);
+
+ session.getAgenda().getAgendaGroup("init").setFocus();
+
session.fireAllRules();
io.dump("phase-1", session.getFactHandles());
diff --git a/src/main/java/io/trygvis/rules/engine/TemplateEngine.java b/src/main/java/io/trygvis/rules/engine/TemplateEngine.java
new file mode 100644
index 0000000..ace7aaf
--- /dev/null
+++ b/src/main/java/io/trygvis/rules/engine/TemplateEngine.java
@@ -0,0 +1,30 @@
+package io.trygvis.rules.engine;
+
+import ch.qos.logback.core.util.FileUtil;
+import com.hubspot.jinjava.Jinjava;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Map;
+
+public class TemplateEngine {
+ private final Jinjava jinjava = new Jinjava();
+
+ public void template(String name, String output, Map<String, Object> params) throws IOException {
+ var template = Files.readString(Path.of("j2", name + ".j2"));
+ String renderedTemplate = jinjava.render(template, params);
+// System.out.println("-----------------------------------");
+// for (var item : params.entrySet()) {
+// System.out.printf(" %s : %s%n", item.getKey(), item.getValue());
+// }
+ System.out.println("-----------------------------------");
+ System.out.println(renderedTemplate);
+ System.out.println("-----------------------------------");
+
+ var f = new File("gen", output);
+ FileUtil.createMissingParentDirectories(f);
+ Files.writeString(f.toPath(), renderedTemplate);
+ }
+}