From d167fc9b40a83187c657f3a048f19ac56f529af3 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 28 Jan 2021 15:52:17 +0100 Subject: Better main(). * More than one input database. * Only creating templating engine if the rules require it. --- module/ri-engine/pom.xml | 2 +- .../main/java/io/trygvis/rules/engine/Engine.java | 27 ++++++++++++++-------- .../io/trygvis/rules/engine/cli/RunCommand.java | 2 +- .../io/trygvis/rules/engine/AcmeAppsTestMain.java | 2 +- .../rules/engine/AcmeWireguardTestMain.java | 2 +- 5 files changed, 22 insertions(+), 13 deletions(-) (limited to 'module') diff --git a/module/ri-engine/pom.xml b/module/ri-engine/pom.xml index c667145..01a72cc 100644 --- a/module/ri-engine/pom.xml +++ b/module/ri-engine/pom.xml @@ -169,7 +169,7 @@ java -Dpicocli.autocomplete.systemExitOnError - --enable-preview + ${java.preview} -cp picocli.AutoComplete diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/engine/Engine.java b/module/ri-engine/src/main/java/io/trygvis/rules/engine/Engine.java index 4a49ca2..fbb79aa 100644 --- a/module/ri-engine/src/main/java/io/trygvis/rules/engine/Engine.java +++ b/module/ri-engine/src/main/java/io/trygvis/rules/engine/Engine.java @@ -29,7 +29,7 @@ public class Engine implements Closeable { public final DbIo io; public final KieSession session; - public Engine(String name, File database, File output, String[] agendaGroups, File[] modules) throws IOException { + public Engine(String name, File[] databases, File output, String[] agendaGroups, File[] modules) throws IOException { logger.info("Getting KieServices"); var services = KieServices.Factory.get(); @@ -85,20 +85,29 @@ public class Engine implements Closeable { session.addEventListener((AgendaEventListener) l); session.addEventListener((RuleRuntimeEventListener) l); - session.setGlobal("te", new JinjavaTemplateEngine(templateLoader, output)); + var te = session.getGlobals().get("te"); + if (te != null) { + session.setGlobal("te", new JinjavaTemplateEngine(templateLoader, output)); + } logger.info("Loading data"); io = new DbIo(container, kieBase); - var objects = io.load(database); - if (objects.isEmpty()) { - logger.warn("Did not load any objects, something is wrong"); - return; - } + List allObjects = new ArrayList<>(); + for (File database : databases) { + var objects = io.load(database); - logger.info("Loaded {} objects", objects.size()); + if (objects.isEmpty()) { + logger.warn("Did not load any objects, something is wrong"); + return; + } + + logger.info("Loaded {} objects from {}", objects.size(), objects.size()); + allObjects.addAll(objects); + } + logger.info("Loaded {} objects", allObjects.size()); - for (var object : objects) { + for (var object : allObjects) { logger.info("object = " + object); session.insert(object); } diff --git a/module/ri-engine/src/main/java/io/trygvis/rules/engine/cli/RunCommand.java b/module/ri-engine/src/main/java/io/trygvis/rules/engine/cli/RunCommand.java index 203a9b0..f61acbe 100644 --- a/module/ri-engine/src/main/java/io/trygvis/rules/engine/cli/RunCommand.java +++ b/module/ri-engine/src/main/java/io/trygvis/rules/engine/cli/RunCommand.java @@ -15,7 +15,7 @@ public class RunCommand implements Callable { public String name; @Option(names = {"-i", "--input"}) - public File input; + public File[] input; @Option(names = {"--output-state"}) public File outputState; diff --git a/module/ri-engine/src/test/java/io/trygvis/rules/engine/AcmeAppsTestMain.java b/module/ri-engine/src/test/java/io/trygvis/rules/engine/AcmeAppsTestMain.java index 17b7950..37e8787 100644 --- a/module/ri-engine/src/test/java/io/trygvis/rules/engine/AcmeAppsTestMain.java +++ b/module/ri-engine/src/test/java/io/trygvis/rules/engine/AcmeAppsTestMain.java @@ -10,7 +10,7 @@ class AcmeAppsTestMain { public static void main(String[] args) throws Exception { var c = new RunCommand(); c.name = "acme-apps"; - c.input = new File("acme.yaml"); + c.input = new File[]{new File("acme.yaml")}; c.outputState = new File("out/acme/apps.yaml"); c.agendaGroups = new String[]{"init", "generate"}; c.generatedOutput = new File("acme-apps"); diff --git a/module/ri-engine/src/test/java/io/trygvis/rules/engine/AcmeWireguardTestMain.java b/module/ri-engine/src/test/java/io/trygvis/rules/engine/AcmeWireguardTestMain.java index 2bb5513..4d88e39 100644 --- a/module/ri-engine/src/test/java/io/trygvis/rules/engine/AcmeWireguardTestMain.java +++ b/module/ri-engine/src/test/java/io/trygvis/rules/engine/AcmeWireguardTestMain.java @@ -10,7 +10,7 @@ class AcmeWireguardTestMain { public static void main(String[] args) throws Exception { var c = new RunCommand(); c.name = "acme-wireguard"; - c.input = new File("acme.yaml"); + c.input = new File[]{new File("acme.yaml")}; c.outputState = new File("out/acme/wireguard.yaml"); c.agendaGroups = new String[]{"init", "generate"}; c.generatedOutput = new File("acme-wireguard"); -- cgit v1.2.3