summaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2021-01-03 23:31:47 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2021-01-03 23:31:47 +0100
commitb7d0da791505ec08bc5e87dc1f5245078c8b3d42 (patch)
tree8d43a87b526dcf875e930f5990bb258d7ad0e4ca /src/main/java
parent09ba202f73a519355f86b845b210b5126ea1dacf (diff)
downloadrules-sandbox-b7d0da791505ec08bc5e87dc1f5245078c8b3d42.tar.gz
rules-sandbox-b7d0da791505ec08bc5e87dc1f5245078c8b3d42.tar.bz2
rules-sandbox-b7d0da791505ec08bc5e87dc1f5245078c8b3d42.tar.xz
rules-sandbox-b7d0da791505ec08bc5e87dc1f5245078c8b3d42.zip
Better sorted output files.
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/io/trygvis/rules/acme/AcmeIo.java102
1 files changed, 89 insertions, 13 deletions
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<T> {
+ public final Class<T> type;
+ public final List<T> values;
+
+ public FactCollection(Class<T> 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 <A, T extends Comparable<T>> Comparator comparable(Class<A> 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<FactHandle> factHandles, Function<Object, Boolean> filter) throws IOException {
var out = new File("out");
@@ -64,7 +133,7 @@ public class AcmeIo {
}
}
- List<Map.Entry<String, Object>> facts = new ArrayList<>(factHandles.size());
+ var facts = new HashMap<Class<?>, FactCollection<Object>>(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<String, Object> 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)));
+ }
}
}
}