From b7d0da791505ec08bc5e87dc1f5245078c8b3d42 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 3 Jan 2021 23:31:47 +0100 Subject: Better sorted output files. --- src/main/java/io/trygvis/rules/acme/AcmeIo.java | 102 +++++++++++++++++++++--- 1 file changed, 89 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/main/java/io/trygvis/rules/acme/AcmeIo.java b/src/main/java/io/trygvis/rules/acme/AcmeIo.java index 9235992..488c93a 100644 --- a/src/main/java/io/trygvis/rules/acme/AcmeIo.java +++ b/src/main/java/io/trygvis/rules/acme/AcmeIo.java @@ -11,13 +11,15 @@ 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.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; +import java.util.Comparator; +import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.function.Function; +@SuppressWarnings("unchecked") public class AcmeIo { private final ObjectMapper mapper; @@ -55,6 +57,73 @@ public class AcmeIo { dump(s, factHandles, (o) -> true); } + static class FactCollection { + public final Class type; + public final List values; + + public FactCollection(Class type) { + this.type = type; + this.values = new ArrayList<>(); + } + + public void sort() { + var comparator = comparable(type, "key"); + + if (comparator == null) { + comparator = comparable(type, "name"); + } + + if (comparator == null) { + comparator = Comparator.comparingInt(System::identityHashCode); + } + + this.values.sort(comparator); + } + } + + private static > Comparator comparable(Class klass, String name) { + + if (klass.getName().contains("Wg")) { + System.out.println("AcmeIo.invoker"); + } + + try { + var method = klass.getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1)); + if (!method.isAccessible()) { + if (!method.trySetAccessible()) + return null; + } + + return (a, b) -> { + try { + var x = (T) method.invoke(a); + var y = (T) method.invoke(b); + return x.compareTo(y); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + }; + } catch (NoSuchMethodException ignored) { + } + + try { + var field = klass.getField(name); + + return (a, b) -> { + try { + var x = (T) field.get(a); + var y = (T) field.get(b); + return x.compareTo(y); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + }; + } catch (NoSuchFieldException ignored) { + } + + return null; + } + public void dump(String s, Collection factHandles, Function filter) throws IOException { var out = new File("out"); @@ -64,7 +133,7 @@ public class AcmeIo { } } - List> facts = new ArrayList<>(factHandles.size()); + var facts = new HashMap, FactCollection>(factHandles.size()); for (var handle : factHandles) { if (handle instanceof DefaultFactHandle h) { var obj = h.getObject(); @@ -72,22 +141,29 @@ public class AcmeIo { continue; } - facts.add(new AbstractMap.SimpleImmutableEntry<>( - h.getObjectClassName(), - obj)); + Class type = obj.getClass(); + var collection = facts.get(type); + + if (collection == null) { + collection = new FactCollection(type); + facts.put(type, collection); + } + + collection.values.add(obj); } } - 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()) - )); + for (var e : facts.entrySet()) { + var name = e.getKey().getName(); + + var collection = e.getValue(); + collection.sort(); + for (var fact : collection.values) { + g.writeObject(new AcmeObject(name, mapper.valueToTree(fact))); + } } } } -- cgit v1.2.3