From ae0cad47c422f454a5ee0ecb4e79ce1cc6c4885a Mon Sep 17 00:00:00 2001 From: Viktor Lofgren Date: Wed, 29 Jan 2025 15:20:25 +0100 Subject: [PATCH] (actor) Utility method for getting a json prototype for actor states If we can hook this into the control gui somehow, it'll make for a nice QOL upgrade when manually interacting with the actors. --- .../actor/prototype/RecordActorPrototype.java | 45 +++++++++++++++++-- .../nu/marginalia/actor/state/ActorStep.java | 6 ++- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/code/libraries/message-queue/java/nu/marginalia/actor/prototype/RecordActorPrototype.java b/code/libraries/message-queue/java/nu/marginalia/actor/prototype/RecordActorPrototype.java index ea8a5ac3..0394d7ca 100644 --- a/code/libraries/message-queue/java/nu/marginalia/actor/prototype/RecordActorPrototype.java +++ b/code/libraries/message-queue/java/nu/marginalia/actor/prototype/RecordActorPrototype.java @@ -5,9 +5,7 @@ import nu.marginalia.actor.state.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.util.*; public abstract class RecordActorPrototype implements ActorPrototype { @@ -118,7 +116,7 @@ public abstract class RecordActorPrototype implements ActorPrototype { } private String functionName(Class functionClass) { - return functionClass.getSimpleName().toUpperCase(); + return ActorStep.functionName(functionClass); } private ActorStep constructState(String message) throws ReflectiveOperationException { @@ -145,4 +143,43 @@ public abstract class RecordActorPrototype implements ActorPrototype { } } + /** Get a list of JSON prototypes for each actor step declared by this actor */ + @SuppressWarnings("unchecked") + public Map getMessagePrototypes() { + Map messagePrototypes = new HashMap<>(); + + for (var clazz : getClass().getDeclaredClasses()) { + if (!clazz.isRecord() || !ActorStep.class.isAssignableFrom(clazz)) + continue; + + StringJoiner sj = new StringJoiner(",\n\t", "{\n\t", "\n}"); + + renderToJsonPrototype(sj, (Class) clazz); + + messagePrototypes.put(ActorStep.functionName((Class) clazz), sj.toString()); + } + + return messagePrototypes; + } + + @SuppressWarnings("unchecked") + private void renderToJsonPrototype(StringJoiner sj, Class recordType) { + for (var field : recordType.getDeclaredFields()) { + String typeName = field.getType().getSimpleName(); + + if ("List".equals(typeName)) { + sj.add(String.format("\"%s\": [ ]", field.getName())); + } + else if (field.getType().isRecord()) { + var innerSj = new StringJoiner(",", "{", "}"); + renderToJsonPrototype(innerSj, (Class) field.getType()); + sj.add(String.format("\"%s\": %s", field.getName(), sj)); + } + else { + sj.add(String.format("\"%s\": \"%s\"", field.getName(), typeName)); + } + } + + } + } diff --git a/code/libraries/message-queue/java/nu/marginalia/actor/state/ActorStep.java b/code/libraries/message-queue/java/nu/marginalia/actor/state/ActorStep.java index 36a7fa2b..c41dd668 100644 --- a/code/libraries/message-queue/java/nu/marginalia/actor/state/ActorStep.java +++ b/code/libraries/message-queue/java/nu/marginalia/actor/state/ActorStep.java @@ -1,3 +1,7 @@ package nu.marginalia.actor.state; -public interface ActorStep {} +public interface ActorStep { + static String functionName(Class type) { + return type.getSimpleName().toUpperCase(); + } +}