diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2021-01-01 20:35:28 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2021-01-01 20:35:28 +0100 |
commit | e4821f82249bddb443a1f6a6e403087cab659c6d (patch) | |
tree | ba6969c5bb04bc88476efa43a53c7c1ceb3a3ca6 /src/main/java/io/trygvis/rules/acme | |
parent | 866eafabd7398fd1485e7aa285345a7dcea480d0 (diff) | |
download | rules-sandbox-e4821f82249bddb443a1f6a6e403087cab659c6d.tar.gz rules-sandbox-e4821f82249bddb443a1f6a6e403087cab659c6d.tar.bz2 rules-sandbox-e4821f82249bddb443a1f6a6e403087cab659c6d.tar.xz rules-sandbox-e4821f82249bddb443a1f6a6e403087cab659c6d.zip |
Migrating to Drools.
Diffstat (limited to 'src/main/java/io/trygvis/rules/acme')
-rw-r--r-- | src/main/java/io/trygvis/rules/acme/AcmeIo.java | 81 | ||||
-rw-r--r-- | src/main/java/io/trygvis/rules/acme/AcmeMyApp.java | 6 | ||||
-rw-r--r-- | src/main/java/io/trygvis/rules/acme/AcmeObject.java | 16 | ||||
-rw-r--r-- | src/main/java/io/trygvis/rules/acme/AcmeOps.java | 4 |
4 files changed, 107 insertions, 0 deletions
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<Object> load(String file) throws IOException { + var parser = mapper.getFactory().createParser(new File(file)); + + var objects = mapper.readValues(parser, AcmeObject.class).readAll(new ArrayList<>()); + + List<Object> 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<FactHandle> factHandles) throws IOException { + var out = new File("out"); + + if (!out.isDirectory()) { + if (!out.mkdirs()) { + throw new IOException("Could not create directory: " + out); + } + } + + List<Map.Entry<String, Object>> 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<String, Object> 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 { +} |