Skip to content

Commit

Permalink
Inactionware#99 continue refactor code...
Browse files Browse the repository at this point in the history
  • Loading branch information
minjing committed Jan 31, 2019
1 parent 77c5548 commit b1e4ce9
Show file tree
Hide file tree
Showing 18 changed files with 333 additions and 279 deletions.
58 changes: 37 additions & 21 deletions uapi.behavior/src/main/java/uapi/behavior/ActionOutput.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,64 @@
package uapi.behavior;

import uapi.common.ArgumentChecker;
import uapi.common.Attributed;
import uapi.rx.Looper;

import java.util.HashMap;
import java.util.Map;

/**
* The class hold output of action
*/
public class ActionOutput {
public class ActionOutput extends Attributed {

private final ActionOutputMeta _meta;
private Object _output;
private final ActionIdentify _actionId;
private final Map<String, ActionOutputMeta> _metas;

public ActionOutput(
final ActionOutputMeta meta
final ActionIdentify actionId
) {
ArgumentChecker.required(meta, "meta");
this._meta = meta;
this(actionId, new ActionOutputMeta[0]);
}

public String name() {
return this._meta.name();
public ActionOutput(
final ActionIdentify actionId,
final ActionOutputMeta[] metas
) {
ArgumentChecker.required(actionId, "actionId");
ArgumentChecker.required(metas, "metas");
this._actionId = actionId;
this._metas = new HashMap<>();
// Duplicated action output meta was checked in ResponsibleRegistry::addAction method
Looper.on(metas).foreach(meta -> this._metas.put(meta.name(), meta));
}

public void set(final Object output) {
@Override
public Object set(
final Object name,
final Object output) {
ArgumentChecker.required(name, "name");
ArgumentChecker.required(output, "output");
if (this._output != null) {
ActionOutputMeta meta = this._metas.get(name);
if (meta == null) {
throw BehaviorException.builder()
.errorCode(BehaviorErrors.ACTION_OUTPUT_SET_TWICE)
.variables(new BehaviorErrors.ActionOutputSetTwice()
.name(this._meta.name()))
.errorCode(BehaviorErrors.INCORRECT_ACTION_OUTPUT_NAME)
.variables(new BehaviorErrors.IncorrectActionOutputName()
.outputName((String) name)
.actionId(this._actionId))
.build();
}
if (! this._meta.type().isInstance(output)) {
if (! meta.type().isInstance(output)) {
throw BehaviorException.builder()
.errorCode(BehaviorErrors.ACTION_OUTPUT_TYPE_NOT_MATCHED)
.variables(new BehaviorErrors.ActionOutputTypeNotMatched()
.output(output)
.type(this._meta.type()))
.outputType(output.getClass())
.actionId(this._actionId)
.outputName((String) name)
.requiredType(meta.type()))
.build();
}
this._output = output;
}

@SuppressWarnings("unchecked")
public <T> T get() {
return (T) this._output;
return super.set(name, output);
}
}
22 changes: 22 additions & 0 deletions uapi.behavior/src/main/java/uapi/behavior/ActionResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

import uapi.common.ArgumentChecker;
import uapi.common.Attributed;
import uapi.common.StringHelper;

public class ActionResult extends Attributed {

private static final String KEY_ACTION_ID = "ACTION";
private static final String KEY_SUCCESS = "SUCCESS";
private static final String KEY_CAUSE = "CAUSE";
private static final String KEY_MSG = "MSG";

public ActionResult(final ActionIdentify actionId) {
this(actionId, true);
Expand Down Expand Up @@ -46,6 +48,15 @@ public ActionResult(
super.set(KEY_CAUSE, cause);
}

public ActionResult(
final ActionIdentify actionId,
final String message
) {
this(actionId,false);
ArgumentChecker.required(message, "message");
super.set(KEY_MSG, message);
}

@Override
public Object set(
final Object key,
Expand Down Expand Up @@ -73,6 +84,17 @@ public Exception cause() {
return get(KEY_CAUSE);
}

public String message() {
String msg = get(KEY_MSG);
if (StringHelper.isNullOrEmpty(msg)) {
Exception ex = cause();
if (ex != null) {
msg = ex.getMessage();
}
}
return msg;
}

public ActionIdentify actionId() {
return get(KEY_ACTION_ID);
}
Expand Down
75 changes: 63 additions & 12 deletions uapi.behavior/src/main/java/uapi/behavior/BehaviorErrors.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import uapi.exception.FileBasedExceptionErrors;
import uapi.exception.IndexedParameters;

import javax.swing.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -59,6 +60,7 @@ public class BehaviorErrors extends FileBasedExceptionErrors<BehaviorException>
public static final int INPUT_OBJECT_TYPE_MISMATCH = 35;
public static final int RESERVED_ACTION_OUTPUT_NAME = 36;
public static final int UNKNOWN_FAILURE_ON_INTERCEPTOR = 37;
public static final int INCORRECT_ACTION_OUTPUT_NAME = 38;

private static final Map<Integer, String> keyCodeMapping;

Expand Down Expand Up @@ -101,6 +103,7 @@ public class BehaviorErrors extends FileBasedExceptionErrors<BehaviorException>
keyCodeMapping.put(INPUT_OBJECT_TYPE_MISMATCH, InputObjectTypeMismatch.KEY);
keyCodeMapping.put(RESERVED_ACTION_OUTPUT_NAME, ReservedActionOutputName.KEY);
keyCodeMapping.put(UNKNOWN_FAILURE_ON_INTERCEPTOR, UnknownFailureOnInterceptor.KEY);
keyCodeMapping.put(INCORRECT_ACTION_OUTPUT_NAME, IncorrectActionOutputName.KEY);
}

public BehaviorErrors() {
Expand Down Expand Up @@ -431,8 +434,8 @@ public static final class InconsistentLeafActions extends IndexedParameters<Inco

private ActionIdentify _leafAction1;
private ActionIdentify _leafAction2;
private Class _leafAction1Output;
private Class _leafAction2Output;
private ActionOutputMeta[] _leafAction1OutputMetas;
private ActionOutputMeta[] _leafAction2OutputMetas;

public InconsistentLeafActions leafAction1(ActionIdentify actionId) {
this._leafAction1 = actionId;
Expand All @@ -444,19 +447,19 @@ public InconsistentLeafActions leafAction2(ActionIdentify actionId) {
return this;
}

public InconsistentLeafActions leafAction1Output(Class type) {
this._leafAction1Output = type;
public InconsistentLeafActions leafAction1Output(ActionOutputMeta[] metas) {
this._leafAction1OutputMetas = metas;
return this;
}

public InconsistentLeafActions leafAction2Output(Class type) {
this._leafAction2Output = type;
public InconsistentLeafActions leafAction2Output(ActionOutputMeta[] metas) {
this._leafAction2OutputMetas = metas;
return this;
}

@Override
public Object[] get() {
return new Object[] { this._leafAction1Output, this._leafAction2Output, this._leafAction1, this._leafAction2 };
return new Object[] { this._leafAction1OutputMetas, this._leafAction2OutputMetas, this._leafAction1, this._leafAction2 };
}
}

Expand Down Expand Up @@ -702,28 +705,49 @@ public Object[] get() {

/**
* Error string template:
* The Action output {} is not instance of type - {}
* The output [{}] can not assign to Action [{}] output [{}] because its type incorrect - {} vs. {}
*/
public static final class ActionOutputTypeNotMatched extends IndexedParameters<ActionOutputTypeNotMatched> {

public static final String KEY = "ActionOutputTypeNotMatched";

private Object _output;
private Class<?> _type;
private Class<?> _outputType;
private ActionIdentify _actionId;
private String _name;
private Class<?> _requiredType;

public ActionOutputTypeNotMatched output(final Object output) {
this._output = output;
return this;
}

public ActionOutputTypeNotMatched type(final Class<?> type) {
this._type = type;
public ActionOutputTypeNotMatched outputType(final Class<?> type) {
this._outputType = type;
return this;
}

public ActionOutputTypeNotMatched actionId(final ActionIdentify actionId) {
this._actionId = actionId;
return this;
}

public ActionOutputTypeNotMatched outputName(final String name) {
this._name = name;
return this;
}

public ActionOutputTypeNotMatched requiredType(final Class<?> type) {
this._requiredType = type;
return this;
}

@Override
public Object[] get() {
return new Object[] { this._output, this._type };
return new Object[] {
this._output, this._outputType.getCanonicalName(),
this._actionId, this._name, this._requiredType.getCanonicalName()
};
}
}

Expand Down Expand Up @@ -1127,4 +1151,31 @@ public Object[] get() {
return new Object[] {this._interceptorId, this._actionId };
}
}

/**
* Error string template:
* Can not set output to output name [{}] on action [{}] - unsupported output name
*/
public static final class IncorrectActionOutputName extends IndexedParameters<IncorrectActionOutputName> {

public static final String KEY = "IncorrectActionOutputName";

private String _outputName;
private ActionIdentify _actionId;

public IncorrectActionOutputName outputName(final String name) {
this._outputName = name;
return this;
}

public IncorrectActionOutputName actionId(final ActionIdentify actionId) {
this._actionId = actionId;
return this;
}

@Override
public Object[] get() {
return new Object[] { this._outputName, this._actionId };
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ public class BehaviorExecutingEvent extends BehaviorTraceEvent {
public BehaviorExecutingEvent(
final ExecutionIdentify executionId,
final Object[] behaviorInputs,
final ActionResult result,
final ActionIdentify actionId,
final ActionOutput[] actionOutputs,
final ActionResult actionResult,
final String sourceName
) {
super(sourceName);
ArgumentChecker.required(executionId, "executionId");
ArgumentChecker.required(actionId, "actionId");
set(KEY_EXECUTION_ID, executionId);
set(KEY_ACTION_ID, actionId);
set(KEY_BEHAVIOR_INPUTS, behaviorInputs);
set(KEY_RESULT, result);
set(KEY_CURRENT_OUTPUTS, actionOutputs);
set(KEY_CURRENT_RESULT, actionResult);
set(KEY_SOURCE_NAME, sourceName);
}

public ActionIdentify actionId() {
return (ActionIdentify) get(KEY_ACTION_ID);
return ((ActionResult) get(KEY_CURRENT_RESULT)).actionId();
}
}
30 changes: 29 additions & 1 deletion uapi.behavior/src/main/java/uapi/behavior/BehaviorFailure.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,49 @@
package uapi.behavior;

import uapi.common.ArgumentChecker;
import uapi.common.StringHelper;

public class BehaviorFailure {

private final ActionIdentify _failureAction;
private final Object[] _failureInputs;
private final String _msg;
private final Exception _cause;

public BehaviorFailure(
final ActionIdentify failureAction,
final Object[] failureInputs,
final String message
) {
this(failureAction, failureInputs, message, null);
}

public BehaviorFailure(
final ActionIdentify failureAction,
final Object[] failureInputs,
final Exception cause
) {
this(failureAction, failureInputs, null, cause);
}

public BehaviorFailure(
final ActionIdentify failureAction,
final Object[] failureInputs,
final String message,
final Exception cause
) {
ArgumentChecker.required(failureAction, "failureAction");
ArgumentChecker.required(failureInputs, "failureInput");
ArgumentChecker.required(cause, "cause");
// ArgumentChecker.required(cause, "cause");

this._failureAction = failureAction;
this._failureInputs = failureInputs;
this._cause = cause;
if (StringHelper.isNullOrEmpty(message) && cause != null) {
this._msg = cause.getMessage();
} else {
this._msg = null;
}
}

public ActionIdentify failureAction() {
Expand All @@ -30,6 +54,10 @@ public Object[] failureInputs() {
return this._failureInputs;
}

public String message() {
return this._msg;
}

public Exception cause() {
return this._cause;
}
Expand Down
Loading

0 comments on commit b1e4ce9

Please sign in to comment.