From e4821f82249bddb443a1f6a6e403087cab659c6d Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 1 Jan 2021 20:35:28 +0100 Subject: Migrating to Drools. --- src/main/java/io/trygvis/rules/acme/AcmeIo.java | 81 ++++++++++++++++++++++ src/main/java/io/trygvis/rules/acme/AcmeMyApp.java | 6 ++ .../java/io/trygvis/rules/acme/AcmeObject.java | 16 +++++ src/main/java/io/trygvis/rules/acme/AcmeOps.java | 4 ++ 4 files changed, 107 insertions(+) create mode 100644 src/main/java/io/trygvis/rules/acme/AcmeIo.java create mode 100644 src/main/java/io/trygvis/rules/acme/AcmeMyApp.java create mode 100644 src/main/java/io/trygvis/rules/acme/AcmeObject.java create mode 100644 src/main/java/io/trygvis/rules/acme/AcmeOps.java (limited to 'src/main/java/io/trygvis/rules/acme') diff --git a/src/main/java/io/trygvis/rules/acme/AcmeIo.java b/src/main/java/io/trygvis/rules/acme/AcmeIo.java new file mode 100644 index 0000000..67dd4cb --- /dev/null +++ b/src/main/java/io/trygvis/rules/acme/AcmeIo.java @@ -0,0 +1,81 @@ +package io.trygvis.rules.acme; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; +import org.drools.core.common.DefaultFactHandle; +import org.kie.api.runtime.rule.FactHandle; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Map; + +public class AcmeIo { + private final ObjectMapper mapper; + + public AcmeIo() { + var factory = new YAMLFactory(); + factory.enable(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID); + factory.enable(YAMLGenerator.Feature.USE_NATIVE_OBJECT_ID); + mapper = new ObjectMapper(factory); + mapper.findAndRegisterModules(); + } + + public List load(String file) throws IOException { + var parser = mapper.getFactory().createParser(new File(file)); + + var objects = mapper.readValues(parser, AcmeObject.class).readAll(new ArrayList<>()); + + List items = new ArrayList<>(objects.size()); + for (AcmeObject object : objects) { + try { + var type = Class.forName(object.type); + var x = mapper.treeToValue(object.data, type); + items.add(x); + } catch (ClassNotFoundException e) { + throw new IOException(e); + } + } + + return items; + } + + public void dump(String s, Collection factHandles) throws IOException { + var out = new File("out"); + + if (!out.isDirectory()) { + if (!out.mkdirs()) { + throw new IOException("Could not create directory: " + out); + } + } + + List> facts = new ArrayList<>(factHandles.size()); + for (var handle : factHandles) { + if (handle instanceof DefaultFactHandle h) { + facts.add(new AbstractMap.SimpleImmutableEntry<>( + h.getObjectClassName(), + h.getObject())); + } + } + + facts.sort(Map.Entry.comparingByKey()); + + var factory = mapper.getFactory(); + try (var writer = new FileWriter(new File(out, s + ".yaml")); + var g = factory.createGenerator(writer)) { + for (Map.Entry fact : facts) { + g.writeObject(new AcmeObject( + fact.getKey(), + mapper.valueToTree(fact.getValue()) + )); + } + } + } +} diff --git a/src/main/java/io/trygvis/rules/acme/AcmeMyApp.java b/src/main/java/io/trygvis/rules/acme/AcmeMyApp.java new file mode 100644 index 0000000..a1340e7 --- /dev/null +++ b/src/main/java/io/trygvis/rules/acme/AcmeMyApp.java @@ -0,0 +1,6 @@ +package io.trygvis.rules.acme; + +public class AcmeMyApp { + public String environment; + public String dockerTag; +} diff --git a/src/main/java/io/trygvis/rules/acme/AcmeObject.java b/src/main/java/io/trygvis/rules/acme/AcmeObject.java new file mode 100644 index 0000000..a75c4ba --- /dev/null +++ b/src/main/java/io/trygvis/rules/acme/AcmeObject.java @@ -0,0 +1,16 @@ +package io.trygvis.rules.acme; + +import com.fasterxml.jackson.databind.node.ObjectNode; + +public final class AcmeObject { + public String type; + public ObjectNode data; + + public AcmeObject() { + } + + public AcmeObject(String type, ObjectNode data) { + this.type = type; + this.data = data; + } +} diff --git a/src/main/java/io/trygvis/rules/acme/AcmeOps.java b/src/main/java/io/trygvis/rules/acme/AcmeOps.java new file mode 100644 index 0000000..147fcfa --- /dev/null +++ b/src/main/java/io/trygvis/rules/acme/AcmeOps.java @@ -0,0 +1,4 @@ +package io.trygvis.rules.acme; + +public class AcmeOps { +} -- cgit v1.2.3