Skip to content

Commit

Permalink
Inactionware#99 Add code to handle action input wire to output
Browse files Browse the repository at this point in the history
  • Loading branch information
minjing committed Mar 3, 2019
1 parent 1d05f30 commit ed69a74
Show file tree
Hide file tree
Showing 14 changed files with 383 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,56 @@

package uapi.behavior;

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

public final class ActionInputReference {

private static final int NO_INDEX = -1;
private static final String SEP = ".";

public static final String generateKey(
final String label,
final String name
) {
return StringHelper.makeString("{}.{}", label, name);
return generateKey(label, name, NO_INDEX);
}

public static final String generateKey(
final String label,
final String name,
final int index
) {
verify(label);
verify(name);
if (index > NO_INDEX) {
return StringHelper.makeString("{}{}{}{}{}", label, SEP, name, SEP, index);
} else {
return StringHelper.makeString("{}{}{}", label, SEP, name);
}
}

private final String _label;
private final String _name;
private final int _idx;

public ActionInputReference(
final String label,
final String name
) {
this(label, name, NO_INDEX);
}

public ActionInputReference(
final String label,
final String name,
final int index
) {
verify(label);
verify(name);
this._label = label;
this._name = name;
this._idx = index;
}

public String label() {
Expand All @@ -40,6 +70,17 @@ public String name() {
}

public String toKey() {
return generateKey(this._label, this._name);
return generateKey(this._label, this._name, this._idx);
}

/**
* Verify specific string has invalid char or not
*
* @param str
* The string
*/
private static void verify(final String str) {
ArgumentChecker.required(str, "str");
ArgumentChecker.notContains(str, "str", SEP);
}
}
28 changes: 16 additions & 12 deletions uapi.behavior/src/main/java/uapi/behavior/ActionOutputMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
public final class ActionOutputMeta {

private static final String ANONYMOUS = "_anonymous_";
// private static final String ANONYMOUS = "_anonymous_";

private final Class<?> _type;
private final String _name;
Expand All @@ -27,27 +27,27 @@ public final class ActionOutputMeta {
* @param type
* Action output type
*/
ActionOutputMeta(
public ActionOutputMeta(
final Class<?> type
) {
ArgumentChecker.required(type, "type");
this._type = type;
this._name = ANONYMOUS;
this._name = null;
}

public ActionOutputMeta(
final Class<?> type,
final String name
) {
ArgumentChecker.required(type, "type");
ArgumentChecker.required(name, "name");
if (name.charAt(0) == '_') {
throw BehaviorException.builder()
.errorCode(BehaviorErrors.RESERVED_ACTION_OUTPUT_NAME)
.variables(new BehaviorErrors.ReservedActionOutputName()
.name(name))
.build();
}
// ArgumentChecker.required(name, "name");
// if (name.charAt(0) == '_') {
// throw BehaviorException.builder()
// .errorCode(BehaviorErrors.RESERVED_ACTION_OUTPUT_NAME)
// .variables(new BehaviorErrors.ReservedActionOutputName()
// .name(name))
// .build();
// }
this._type = type;
this._name = name;
}
Expand All @@ -70,6 +70,10 @@ public String name() {
return this._name;
}

public boolean isAnonymous() {
return this._name == null;
}

@Override
public boolean equals(Object other) {
if (other == null) {
Expand All @@ -79,6 +83,6 @@ public boolean equals(Object other) {
return false;
}
ActionOutputMeta otherMeta = (ActionOutputMeta) other;
return this._name.equals(otherMeta._name) || this._type.equals(otherMeta._type);
return this._name.equals(otherMeta._name) && this._type.equals(otherMeta._type);
}
}
65 changes: 47 additions & 18 deletions uapi.behavior/src/main/java/uapi/behavior/BehaviorErrors.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ public class BehaviorErrors extends FileBasedExceptionErrors<BehaviorException>
public static final int INPUT_OUTPUT_COUNT_MISMATCH = 33;
public static final int INPUT_OUTPUT_TYPE_MISMATCH = 34;
public static final int INPUT_OBJECT_TYPE_MISMATCH = 35;
public static final int RESERVED_ACTION_OUTPUT_NAME = 36;
// 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;
public static final int INCONSISTENT_INTERCEPTOR_INPUT_METAS = 39;
public static final int INTERCEPTOR_HAS_OUTPU_META = 40;
public static final int NO_DEFAULT_NEXT_ACTION = 41;

private static final Map<Integer, String> keyCodeMapping;

Expand Down Expand Up @@ -104,11 +105,13 @@ public class BehaviorErrors extends FileBasedExceptionErrors<BehaviorException>
keyCodeMapping.put(INPUT_OUTPUT_COUNT_MISMATCH, InputOutputCountMismatch.KEY);
keyCodeMapping.put(INPUT_OUTPUT_TYPE_MISMATCH, InputOutputTypeMismatch.KEY);
keyCodeMapping.put(INPUT_OBJECT_TYPE_MISMATCH, InputObjectTypeMismatch.KEY);
keyCodeMapping.put(RESERVED_ACTION_OUTPUT_NAME, ReservedActionOutputName.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);
keyCodeMapping.put(INCONSISTENT_INTERCEPTOR_INPUT_METAS, InconsistentInterceptorInputMetas.KEY);
keyCodeMapping.put(INTERCEPTOR_HAS_OUTPU_META, InterceptorHasOutputMeta.KEY);
keyCodeMapping.put(NO_DEFAULT_NEXT_ACTION, NoDefaultNextAction.KEY);

}

public BehaviorErrors() {
Expand Down Expand Up @@ -1075,22 +1078,22 @@ public Object[] get() {
* Error string template:
* The action output name is reserved by system - {}
*/
public static final class ReservedActionOutputName extends IndexedParameters<ReservedActionOutputName> {

public static final String KEY = "ReservedActionOutputName";

private String _name;

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

@Override
public Object[] get() {
return new Object[] { this._name };
}
}
// public static final class ReservedActionOutputName extends IndexedParameters<ReservedActionOutputName> {
//
// public static final String KEY = "ReservedActionOutputName";
//
// private String _name;
//
// public ReservedActionOutputName name(final String name) {
// this._name = name;
// return this;
// }
//
// @Override
// public Object[] get() {
// return new Object[] { this._name };
// }
// }

/**
* Error string template:
Expand Down Expand Up @@ -1201,4 +1204,30 @@ public Object[] get() {
return new Object[] { this._interceptorId, CollectionHelper.asString(this._outMetas) };
}
}

/**
* Error String template:
* The action [{}] in behavior [{}] has next action but it does not define a default next action
*/
public static final class NoDefaultNextAction extends IndexedParameters<NoDefaultNextAction> {

public static final String KEY = "ActionHasNoDefaultNext";

private ActionIdentify _actionId;
private ActionIdentify _behaviorId;

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

public NoDefaultNextAction behaviorId(final ActionIdentify actionId) {
this._behaviorId = actionId;
return this;
}
@Override
public Object[] get() {
return new Object[] { this._actionId, this._behaviorId };
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ public interface IBehaviorBuilder {
*/
INavigator navigator();

/**
* Get reference object which can used to wired action input and output
*
* @return A reference
*/
IWired wired();

/**
* Build behavior
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public interface IExecutionContext {
String KEY_BEHA_NAME = "BehaviorName";
String KEY_ORI_EVENT = "OriginalEvent";
String KEY_BEHA_INPUTS = "BehaviorInputs";
String KEY_BEHA_IN_1 = "BehaviorInputs.1";
String KEY_BEHA_IN_2 = "BehaviorInputs.2";

/**
* Put single k/v data under behavior scope
Expand Down
13 changes: 13 additions & 0 deletions uapi.behavior/src/main/java/uapi/behavior/IReference.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright (c) 2019. The UAPI Authors
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at the LICENSE file.
*
* You must gained the permission from the authors if you want to
* use the project into a commercial product.
*/

package uapi.behavior;

public interface IReference {
}
17 changes: 17 additions & 0 deletions uapi.behavior/src/main/java/uapi/behavior/IWired.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) 2019. The UAPI Authors
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at the LICENSE file.
*
* You must gained the permission from the authors if you want to
* use the project into a commercial product.
*/

package uapi.behavior;

public interface IWired {

IReference toOutput(String actionLabel, String outputName);

IReference toOutput(String actionLabel, int actionIndex);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ActionHolder {
private final List<ActionHolder> _nextActions;
private final ActionHolder _previousAction;
private final Object[] _inputs;
private boolean _hasDefaultNext = false;

ActionHolder(
final IAction action,
Expand Down Expand Up @@ -110,6 +111,9 @@ void next(
) throws BehaviorException {
ArgumentChecker.required(actionHolder, "actionHolder");
this._nextActions.add(actionHolder);
if (actionHolder._evaluator == ALWAYS_MATCHED) {
this._hasDefaultNext = true;
}
}

boolean hasNext() {
Expand Down Expand Up @@ -170,6 +174,17 @@ ActionHolder findNext(final Object data) throws BehaviorException {
}

void verify() {
// The action must have a default next action if it has next action
if (hasNext() && ! this._hasDefaultNext) {
throw BehaviorException.builder()
.errorCode(BehaviorErrors.NO_DEFAULT_NEXT_ACTION)
.variables(new BehaviorErrors.NoDefaultNextAction()
.actionId(this._action.getId())
.behaviorId(this._behavior.getId()))
.build();
}

// Check action input
ActionInputMeta[] inputMetas = this._action.inputMetas();
if (inputMetas.length != this._inputs.length) {
throw BehaviorException.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2019. The UAPI Authors
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at the LICENSE file.
*
* You must gained the permission from the authors if you want to
* use the project into a commercial product.
*/

package uapi.behavior.internal;

import uapi.behavior.ActionInputReference;
import uapi.behavior.ActionOutputMeta;
import uapi.rx.Looper;

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

/**
* A class is used to holder action output data
*/
public class ActionOutputHolder {

private final ActionOutputMeta[] _outMetas;
private final Map<String, Object> _namedDatas;
private final Object[] _outDatas;

ActionOutputHolder(
final ActionOutputMeta[] outputMetas,
final Object[] outputDatas
) {
this._outMetas = outputMetas;
this._outDatas = outputDatas;
this._namedDatas = new HashMap<>();
Looper.on(outputMetas).foreachWithIndex((idx, meta) -> {
if (! meta.isAnonymous()) {
this._namedDatas.put(meta.name(), outputDatas[idx]);
}
});
}

Object getData(final ActionInputReference inRef) {
Object data = null;
if (inRef.name() != null) {
data = this._namedDatas.get(inRef.name());
}
if (data != null) {
return data;
}
return data;
}
}
Loading

0 comments on commit ed69a74

Please sign in to comment.