mirror of
https://github.com/MarginaliaSearch/MarginaliaSearch.git
synced 2025-02-23 21:18:58 +00:00
(actor) Code cleanup
This commit is contained in:
parent
6c1ca10be7
commit
98d742d634
@ -54,11 +54,12 @@ public abstract class RecordActorPrototype implements ActorPrototype {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public List<ActorStateInstance> asStateList() {
|
public List<ActorStateInstance> asStateList() {
|
||||||
|
|
||||||
List<ActorStateInstance> steps = new ArrayList<>();
|
List<ActorStateInstance> steps = new ArrayList<>();
|
||||||
|
|
||||||
// Look for member classes that instantiate ActorStep in this class and all parent classes up until
|
// Look for member classes that implement ActorStep in this class and all parent classes up until
|
||||||
// RecordActorPrototype
|
// RecordActorPrototype
|
||||||
|
|
||||||
for (Class<?> clazz = getClass();
|
for (Class<?> clazz = getClass();
|
||||||
@ -68,48 +69,63 @@ public abstract class RecordActorPrototype implements ActorPrototype {
|
|||||||
for (var stepClass : clazz.getDeclaredClasses()) {
|
for (var stepClass : clazz.getDeclaredClasses()) {
|
||||||
if (!ActorStep.class.isAssignableFrom(stepClass))
|
if (!ActorStep.class.isAssignableFrom(stepClass))
|
||||||
continue;
|
continue;
|
||||||
steps.add(new StepInstance((Class<? extends ActorStep>) stepClass));
|
|
||||||
|
steps.add(new StateInstance((Class<? extends ActorStep>) stepClass));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return steps;
|
return steps;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class StepInstance implements ActorStateInstance {
|
private class StateInstance implements ActorStateInstance {
|
||||||
private final Class<? extends ActorStep> stepClass;
|
private final Class<? extends ActorStep> stepClass;
|
||||||
|
|
||||||
public StepInstance(Class<? extends ActorStep> stepClass) {
|
public StateInstance(Class<? extends ActorStep> stepClass) {
|
||||||
this.stepClass = stepClass;
|
this.stepClass = stepClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String name() {
|
public String name() {
|
||||||
return stepClass.getSimpleName().toUpperCase();
|
return functionName(stepClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ActorStateTransition next(String message) {
|
public ActorStateTransition next(String message) {
|
||||||
|
ActorStep nextState;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ActorStep dest;
|
var currentState = constructState(message);
|
||||||
if (null == message || message.isBlank()) {
|
|
||||||
dest = stepClass.getDeclaredConstructor().newInstance();
|
nextState = transition(currentState);
|
||||||
} else {
|
|
||||||
dest = gson.fromJson(message, stepClass);
|
|
||||||
}
|
|
||||||
dest = transition(dest);
|
|
||||||
return new ActorStateTransition(
|
|
||||||
dest.getClass().getSimpleName().toUpperCase(),
|
|
||||||
gson.toJson(dest)
|
|
||||||
);
|
|
||||||
} catch (ActorControlFlowException cfe) {
|
} catch (ActorControlFlowException cfe) {
|
||||||
return new ActorStateTransition(
|
// This exception allows custom error messages
|
||||||
Error.class.getSimpleName(),
|
nextState = new Error(cfe.getMessage());
|
||||||
gson.toJson(new Error(cfe.getMessage()))
|
|
||||||
);
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
logger.error("Error in transition handler, decoding {}:'{}'", stepClass.getSimpleName(), message);
|
logger.error("Error in transition handler, decoding {}:'{}'", stepClass.getSimpleName(), message);
|
||||||
logger.error("Exception was", ex);
|
logger.error("Exception was", ex);
|
||||||
|
|
||||||
return new ActorStateTransition("ERROR", ex.getMessage());
|
nextState = new Error(ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return encodeTransition(nextState);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ActorStateTransition encodeTransition(ActorStep nextState) {
|
||||||
|
return new ActorStateTransition(
|
||||||
|
functionName(nextState.getClass()),
|
||||||
|
gson.toJson(nextState)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String functionName(Class<? extends ActorStep> functionClass) {
|
||||||
|
return functionClass.getSimpleName().toUpperCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
private ActorStep constructState(String message) throws ReflectiveOperationException {
|
||||||
|
if (null == message || message.isBlank()) {
|
||||||
|
return stepClass.getDeclaredConstructor().newInstance();
|
||||||
|
} else {
|
||||||
|
return gson.fromJson(message, stepClass);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user