Commit e183ca6e by amir

first

parent 2a178998
Pipeline #75 skipped in 0 seconds
/build/
/bin/
.gradle
.settings
.idea
.lck
.project
.classpath
gradle/
group 'com.ipgallery.common'
version '1.0.0'
apply plugin: 'java'
apply plugin: 'maven-publish'
sourceCompatibility = 1.8
repositories {
//mavenCentral()
maven { url "http://172.16.1.132:8081/repository/internal" }
}
dependencies {
compile 'com.fasterxml.jackson.core:jackson-databind:2.2.3'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
publishing {
publications {
repositories.maven {
url 'http://172.16.1.132:8081/repository/internal'
credentials {
username "admin"
password "giptmgr1"
}
}
mavenJava(MavenPublication) {
//artifactId 'group-service'
from components.java
}
}
}
\ No newline at end of file
#!/usr/bin/env bash
##############################################################################
##
## Gradle start up script for UN*X
##
##############################################################################
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
warn ( ) {
echo "$*"
}
die ( ) {
echo
echo "$*"
echo
exit 1
}
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
esac
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD="java"
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi
# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi
# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
function splitJvmOpts() {
JVM_OPTS=("$@")
}
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:init
@rem Get command-line arguments, handling Windowz variants
if not "%OS%" == "Windows_NT" goto win9xME_args
if "%@eval[2+2]" == "4" goto 4NT_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
goto execute
:4NT_args
@rem Get arguments from the 4NT Shell from JP Software
set CMD_LINE_ARGS=%$
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
rootProject.name = 'seh'
package seh.SEH;
import java.lang.invoke.CallSite;
import java.lang.reflect.Method;
import seh.Types.RetStatSupplier;
/**
* this is the interface of all the classes
* that use the GenericSEH engine
* @author amir
*
*/
public interface BaseSEH
{
public Method resolveAction(String actionName);
public int resolveEvent(String eventName);
public String resolveEventName(int event);
public SEHParam getNewSEHParam();
public void setAppData(Object appData);
public RetStatSupplier invokeFunc(CallSite callSite, SEHParam sehParam) throws Throwable;
}
package seh.SEH;
public class Constants
{
public static final int C_NO_ENTRY = -1;
public static final int C_MAX_SEH_STATE_NAME_LEN = 256;
public static final int C_MAX_SEH_CALL_FLOW_FILE_NAME_LEN = 128;
public static final int C_MAX_SEH_EVENT_NAME_LEN = 128;
public static final int C_MAX_SEH_FULL_EVENT_NAME_LEN = 128;
public static final int C_MAX_SEH_FULL_ACTION_NAME = 128;
public static final int C_MAX_SEH_ERROR_MSG = 256;
public static final int C_ACTION_PARAMS_LEN = 6;
public static final int C_ACTION_PRIORITY_LEN = 8;
public static final int C_ACTION_PRIORITY_MANDATORY_LEN = 9;
public static final int C_ACTION_PRIORITY_CRITICAL_LEN = 8;
public static final String C_ACTION_PARAMS ="Params";
public static final String C_ACTION_PRIORITY = "Priority";
public static final String C_ACTION_PRIORITY_MANDATORY = "Mandatory";
public static final String C_ACTION_PRIORITY_CRITICAL = "Critical";
public static final String C_BASE_STATE = "BaseState";
public static final String C_NAME = "Name";
public static final String C_NEXT_STATE = "NextState";
public static final String C_WRONG_SEH_STATE = "Wrong State";
public static final String C_PARAM_COND_ATTRIBUTE = "Cond";
public static final String C_NULL_STATE = "NULL";
public static final String C_STATES = "States";
public static final String C_EVENTS = "Events";
public static final String C_ACTIONS = "Actions";
public static final String C_TYPE = "Type";
public static final String C_IF_THEN = "Then";
public static final String C_IF_ELSE = "Else";
public static final String C_CASES = "Cases";
}
package seh.SEH;
/**
* all the enums are here
* @author amir
*
*/
public class Enums
{
public enum EnumRetStat
{
E_FAIL,
E_SUCCESS
}
public enum EnumsEventActionType
{
E_ActionType_Regular("Action"),
E_ActionType_If("If"),
E_ActionType_While("While"),
E_ActionType_Switch("Switch");
private String name;
private EnumsEventActionType(String type)
{
name = type;
}
public String getName() { return name; }
public static EnumsEventActionType create(String type)
{
for ( EnumsEventActionType eventType : EnumsEventActionType.values()) {
if (eventType.getName().equals(type))
return eventType;
}
return null;
}
};
}
package seh.SEH;
import seh.Types.CallFlow;
import seh.Types.RetStat;
public interface ICallFlowBuilder {
RetStat buildFlowTableFromFile(String flowFileName, CallFlow callFlow,BaseSEH baseSEHObject);
RetStat buildFlowTableFromString(String flowFSM, CallFlow callFlow,BaseSEH baseSEHObject);
}
package seh.SEH;
import java.io.File;
import java.lang.invoke.CallSite;
import java.lang.invoke.LambdaMetafactory;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.logging.Logger;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import seh.SEH.Enums.EnumRetStat;
import seh.SEH.Enums.EnumsEventActionType;
import seh.Types.CallFlow;
import seh.Types.EventAction;
import seh.Types.EventData;
import seh.Types.EventIfAction;
import seh.Types.EventSwitchAction;
import seh.Types.EventWhileAction;
import seh.Types.NextState;
import seh.Types.RetStat;
import seh.Types.RetStatSupplier;
import seh.Types.State;
public class JsonCallFlowBuilder implements ICallFlowBuilder {
ObjectMapper objMapper = new ObjectMapper();
JsonNode flowNode;
Logger logger = Logger.getLogger(getClass().getName());
public EventData eventData = new EventData();
public EventAction eventAction = new EventAction();
public EventAction eventActionPtr;
public CallFlow callFlow;
public BaseSEH baseSEHObject;
String flowFileName;
// SEHParam defaultSEHParam;
@Override
public RetStat buildFlowTableFromFile(String flowFileName, CallFlow callFlow,
BaseSEH baseSEHObject) {
RetStat retStat = new RetStat(EnumRetStat.E_SUCCESS, 0, null);
int iCurrentState = 0;
this.callFlow = callFlow;
this.baseSEHObject = baseSEHObject;
this.flowFileName = flowFileName;
//defaultSEHParam = this.baseSEHObject.getNewSEHParam();
/*********************************************
* copying the file name and reading the file
********************************************/
this.callFlow.flowFileName = flowFileName;
try
{
flowNode = objMapper.readTree(new File(flowFileName));
if (flowNode != null)
{
retStat = createCallFlow(flowFileName, callFlow, retStat,
iCurrentState);
}
else
{
retStat.setFail("JsonCallFlowBuilder::BuildFlowTable-\tFailed to read file: "
+ flowFileName);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
retStat.setError(e.toString());
}
return retStat;
}
@Override
public RetStat buildFlowTableFromString(String flowFSM, CallFlow callFlow,
BaseSEH baseSEHObject) {
RetStat retStat = new RetStat(EnumRetStat.E_SUCCESS, 0, null);
int iCurrentState = 0;
this.callFlow = callFlow;
this.baseSEHObject = baseSEHObject;
/*********************************************
* copying the file name and reading the file
********************************************/
this.callFlow.flowFileName = flowFileName;
try
{
flowNode = objMapper.readTree(flowFSM);
if (flowNode != null)
{
retStat = createCallFlow(flowFileName, callFlow, retStat,
iCurrentState);
}
else
{
retStat.setFail("JsonCallFlowBuilder::BuildFlowTable-\tFailed to read file: "
+ flowFileName);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
retStat.setError(e.toString());
}
return retStat;
}
private RetStat createCallFlow(String flowFileName, CallFlow callFlow,
RetStat retStat, int iCurrentState) {
State statePtr;
JsonNode currentStateNodePtr;
/*************************************************
* Going over the tree: - Counting the number of states - Allocating
* the states - going over each state and reading it's data
************************************************/
ArrayNode statesArrayNode = (ArrayNode)flowNode.path(Constants.C_STATES);
/************************************
* - Counting the number of states and setting the node to the first
* child
*************************************/
if (statesArrayNode.isArray() )
callFlow.iNumOfStates = statesArrayNode.size();
/***************************
* - Allocating the states
***************************/
if (callFlow.iNumOfStates > 0)
{
callFlow.stateArray = new State[callFlow.iNumOfStates];
/*
* allocating each cell
*/
for (int i = 0; i < callFlow.iNumOfStates; i++)
callFlow.stateArray[i] = new State();
/*************************************
* Going over the States and Setting the State Names
************************************/
for (JsonNode stateNode : statesArrayNode)
{
statePtr = callFlow.stateArray[iCurrentState];
currentStateNodePtr = stateNode;
/*
* Getting the <Name> tag - first child
*/
statePtr.stateName = currentStateNodePtr.path(Constants.C_NAME).asText();
/*
* Getting the State data
*/
retStat = GetState(currentStateNodePtr, statePtr );
if (retStat.isSuccess())
{
/****************************
* Going to the next state
****************************/
iCurrentState++;
}
else
{
if (retStat.getError() == null)
retStat.setError("JsonCallFlowBuilder.BuildFlowTable-\tFailed in State: "
+ statePtr.stateName);
break;
}
}
} else
{
retStat.setFail("JsonCallFlowBuilder.BuildFlowTable-\tno States for "
+ flowFileName);
}
return retStat;
}
/**
* Getting the state and all it's data
* @param currentStateNodePtr
* @param statePtr
* @return
*/
private RetStat GetState(JsonNode stateNode, State statePtr) {
RetStat retStat = new RetStat(EnumRetStat.E_SUCCESS, 0, null);
/***************************
* Getting the State Events
**************************/
/*
* checking for BaseState tags
*/
JsonNode baseDtateNode = stateNode.path(Constants.C_BASE_STATE);
if (baseDtateNode.isTextual())
{
statePtr.baseState = new NextState();
// Getting the base state name
statePtr.baseState.stateName = baseDtateNode.asText();
}
ArrayNode eventsArray = (ArrayNode)stateNode.path(Constants.C_EVENTS);
if (eventsArray.isArray())
{
for (JsonNode eventNode : eventsArray)
{
/****************************
* Getting the Event
****************************/
retStat = GetEvent(eventNode );
if (retStat.isFail())
{
if (retStat.getError() == null)
retStat.setError("JsonCallFlowBuilder.GetState-\tFailed Getting the event");
return retStat;
}
/*
* adding the event to map
*/
try
{
statePtr.eventsMap.put(this.eventData.eventName,(EventData)this.eventData.clone());
} catch (CloneNotSupportedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
retStat.setFail("JsonCallFlowBuilder.GetState-\tFailed in cloning event "
+ this.eventData.eventName);
return retStat;
}
}
}
else
{
retStat.setFail("JsonCallFlowBuilder.GetState-\tno events node for state: " +
stateNode.path(Constants.C_NAME).asText() +
", file: " + this.flowFileName);
}
return retStat;
}
/**
* Getting the event data
* @param eventNode
* @return
*/
private RetStat GetEvent(JsonNode eventNode) {
RetStat retStat = new RetStat();
this.eventData.Reset();
/*
* Going over the Event children
*/
/*
* checking for leaf : Event name / Next state
*/
/*******************************
* Getting the event name
******************************/
JsonNode eventNameNode = eventNode.path(Constants.C_NAME);
if (eventNameNode.isTextual())
this.eventData.eventName = new String(eventNameNode.asText());
/*******************************
* Getting the event next state
******************************/
JsonNode eventNextStateNode = eventNode.path(Constants.C_NEXT_STATE);
if (eventNextStateNode.isTextual())
{
String nextStateStr = eventNextStateNode.asText();
if (nextStateStr.equalsIgnoreCase(Constants.C_NEXT_STATE))
{
this.eventData.nextState.Reset();
}
else
{
this.eventData.nextState.stateName = nextStateStr;
}
}
/*****************************
* Getting the Event Actions
*****************************/
JsonNode eventActionsNode = eventNode.path(Constants.C_ACTIONS);
retStat = GetEventActions(eventActionsNode);
if (retStat.isFail())
{
if (retStat.getError() == null)
retStat.setError("JsonCallFlowBuilder.GetEvent-\tFailed to get event actions for event: "
+ this.eventData.eventName);
return retStat;
}
return retStat;
}
/**
* Getting the event actions, resolving their names to functions pointers
* and storing them
* @param eventActionsNode
* @return
*/
private RetStat GetEventActions(JsonNode eventActionsNode) {
RetStat retStat = new RetStat();
for (JsonNode actionNode : eventActionsNode)
/*
* Going over the Event actions
*/
{
/*******************************
* Resolving the action
*******************************/
retStat = GetAction(actionNode );
if (retStat.isFail())
return retStat;
/*****************************
* Storing the action
****************************/
this.eventData.AddAction(this.eventActionPtr);
}
return retStat;
}
/**
* Getting the Action
* example:
* [
{
"Type":"Action",
"Name":"SimpleAction"
},
{
"Type":"Action",
"Name": "ActionWithParamInt",
"Params": { "TO": "20" }
}
]
* @param actionNode
* @return
*/
private RetStat GetAction(JsonNode actionNode) {
RetStat retStat = new RetStat();
this.eventAction.Reset();
/*
* getting the type
*/
JsonNode actionTypeNode = actionNode.path(Constants.C_TYPE);
if (actionTypeNode.isTextual())
{
EnumsEventActionType eventActonType = EnumsEventActionType.create(actionTypeNode.asText());
if (eventActonType != null)
{
switch (eventActonType) {
case E_ActionType_Regular:
// Handle action
this.eventActionPtr = new EventAction();
this.eventActionPtr.Reset();
retStat = ResolveSimpleAction(this.eventActionPtr, actionNode );
break;
case E_ActionType_If:
this.eventActionPtr = new EventIfAction();
this.eventActionPtr.Reset();
/*******************************
* Getting the condition action
********************************/
retStat = HandleEventIfAction((EventIfAction) this.eventActionPtr,actionNode );
break;
case E_ActionType_Switch:
this.eventActionPtr = new EventSwitchAction();
this.eventActionPtr.Reset();
/*******************************
* Getting the condition action
********************************/
retStat = HandleEventSwitchAction((EventSwitchAction) this.eventActionPtr, actionNode);
break;
case E_ActionType_While:
this.eventActionPtr = new EventWhileAction();
this.eventActionPtr.Reset();
/*******************************
* Getting the condition action
********************************/
retStat = HandleEventWhileAction((EventWhileAction) this.eventActionPtr, actionNode);
break;
default:
break;
}
};
}
else
{
retStat = new RetStat();
retStat.setFail("JsonCallFlowBuilder.GetAction-\tno type for action");
}
return retStat;
}
/**
* Getting the method of an action with no parameters:
*
* <pre>
* {@code
* <Action>EndSession</Action>
* }
* </pre>
*
* @param actionPtr
* @param action
* @return
*/
RetStat ResolveAction(EventAction actionPtr, String action )
{
RetStat retStat = new RetStat();
/*********************************
* Resolve action
********************************/
actionPtr.action = this.baseSEHObject.resolveAction(action);
if (actionPtr.action == null)
{
retStat.setFail("JsonCallFlowBuilder.ResolveAction-\tFailed to Resolve the action"
+ action);
}
return retStat;
}
RetStat ResolveActionFunc(EventAction actionPtr, String action )
{
RetStat retStat = new RetStat();
/*********************************
* Resolve action
********************************/
try {
actionPtr.actionFunc = createRetStatFunc(actionPtr,action);
if (actionPtr.actionFunc == null)
{
retStat.setFail("JsonCallFlowBuilder.ResolveAction-\tFailed to Resolve the action"
+ action);
}
} catch (Throwable e) {
retStat.setError(e.toString());
}
return retStat;
}
public RetStatSupplier createRetStatFunc(EventAction eventAction, String action ) throws Throwable {
Class<? extends SEHParam> paramClass = SEHParam.class;
Class<? extends BaseSEH> sehClass = this.baseSEHObject.getClass();
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodType methodType = MethodType.methodType(RetStat.class, paramClass),
functionType = MethodType.methodType(RetStat.class),
invokedType = MethodType.methodType(RetStatSupplier.class, sehClass, paramClass);
MethodHandle methodHandle=lookup.findVirtual(sehClass,action,methodType);
CallSite callSite = LambdaMetafactory.metafactory(lookup, "call",
invokedType, functionType, methodHandle, functionType);
return this.baseSEHObject.invokeFunc(callSite,eventAction.param);//(RetStatSupplier) callSite.getTarget().invokeExact(this.baseSEHObject.getClass().cast(this.baseSEHObject), defaultSEHParam);
}
private String getParamString(JsonNode jsonNode, String name) {
if (jsonNode != null) {
JsonNode jn = jsonNode.path(name);
if (jn.isTextual()) {
return jn.asText();
}
}
return null;
}
/**
* resolving a simple action - not a condition like if/while/switch
* @param actionPtr
* @param actionNode
* @return
*/
private RetStat ResolveSimpleAction(EventAction actionPtr, JsonNode actionNode) {
RetStat retStat = new RetStat();
/*
* get action params
*/
JsonNode actionParamsNode = actionNode.path(Constants.C_ACTION_PARAMS);
if (actionParamsNode.isObject())
GetActionParams(actionPtr, actionParamsNode );
JsonNode actionNameNode = actionNode.path(Constants.C_NAME);
if (actionNameNode.isTextual())
{
retStat = ResolveActionFunc(actionPtr,actionNameNode.asText());
//retStat = ResolveAction(actionPtr,actionNameNode.asText());
if (retStat.isSuccess())
{
/*
* checking for action priority
*/
Optional<String> actionPriority = Optional.ofNullable(getParamString(actionNode, Constants.C_ACTION_PRIORITY));
actionPriority.ifPresent(ap -> {
if (ap.equalsIgnoreCase(
Constants.C_ACTION_PRIORITY_MANDATORY))
actionPtr.bIsMandatory = true;
else if (ap.equalsIgnoreCase(
Constants.C_ACTION_PRIORITY_CRITICAL))
actionPtr.bIsCritical = true;
});
}
}
else
{
retStat.setFail("JsonCallFlowBuilder.ResolveSimpleAction-\tno action name");
}
return retStat;
}
/**
* Getting the Action parameters
* @param actionPtr
* @param actionParamsNode
*/
private void GetActionParams(EventAction actionPtr,
JsonNode actionParamsNode) {
/*****************************
* Allocating the action parmas class and Going over the params
******************************/
actionPtr.param = this.baseSEHObject.getNewSEHParam();
if (actionPtr.param != null)
{
/*
* Going over the Parameters
*/
Iterator<Entry<String, JsonNode>> fields = actionParamsNode.fields();
while (fields.hasNext()) {
Entry<String, JsonNode> nextParam = fields.next();
/*
* Setting the parameter in the param object
*/
actionPtr.param.SetParam(nextParam.getKey(),nextParam.getValue().asText());
}
}
}
/**
* {
"Type":"If",
"Name": "IfElseAction",
"Then": {
"Actions": [
{
"Type":"Action",
"Name":"SimpleAction"
},
{
"Type":"Action",
"Name": "ActionWithParamInt",
"Params": { "TO": "30000" }
},
{
"Type":"Action",
"Name":"SimpleAction"
}
],
"NextState": "Idle_IfThenState"
},
"Else": {
"Actions": [
{
"Type":"Action",
"Name": "ActionWithParamInt",
"Params": { "TO": "5000" }
},
{
"Type":"Action",
"Name":"SimpleAction"
},
{
"Type":"Action",
"Name":"SimpleAction"
}
],
"NextState": "Idle_IfElseState"
}
}
* @param eventAction
* @param actionNode
* @return
*/
private RetStat HandleEventIfAction(EventIfAction eventAction, JsonNode actionNode) {
RetStat retStat = new RetStat();
/*******************************
* Getting the condition action
*******************************/
retStat = ResolveSimpleAction(eventAction, actionNode );
if (retStat.isFail())
{
if (retStat.getError() == null)
retStat.setError("JsonCallFlowBuilder.HandleEventIfAction-\tFailed to Resolve the action"
+ actionNode.toString());
logger.severe(retStat.getError());
return retStat;
}
/*
* get then/else actions
*/
JsonNode thenNode = actionNode.path(Constants.C_IF_THEN);
JsonNode elseNode = actionNode.path(Constants.C_IF_ELSE);
if (thenNode.isObject() && elseNode.isObject())
{
retStat = GetEventData(eventAction.trueActions, thenNode );
if (retStat.isSuccess())
{
retStat = GetEventData(eventAction.falseActions, elseNode );
}
}
else
{
retStat.setFail("JsonCallFlowBuilder.HandleEventIfAction-\tNo true actions in: "
+ actionNode.toString());
}
return retStat;
}
/**
* Getting all the event data , including it's next state Calling this
* method when node is pointing to <Actions> tag
* @param eventData
* @param jsonNode
* @return
*/
private RetStat GetEventData(EventData eventData, JsonNode jsonNode) {
RetStat retStat = new RetStat();
EventAction actionPtr = null;
eventData.Reset();
/*****************************
* Going over the actions
****************************/
JsonNode actionArrayNode = jsonNode.path(Constants.C_ACTIONS);
if (actionArrayNode.isArray())
{
for (JsonNode actionNode : actionArrayNode) {
/*******************************
* Resolving the action
*******************************/
actionPtr = new EventAction();
actionPtr.Reset();
retStat = ResolveSimpleAction(actionPtr, actionNode);
if (retStat.isFail())
{
actionPtr = null;
return retStat;
}
/*****************************
* Storing the action
****************************/
eventData.AddAction(actionPtr);
}
} else
{
retStat.setFail("JsonCallFlowBuilder.GetEventData-\tNo Event Actions");
return retStat;
}
/***********************************
* Getting the next state - optional sometimes there won't be next
* state
**********************************/
Optional<String> nextStateOpt = Optional.ofNullable(getParamString(jsonNode,Constants.C_NEXT_STATE));
nextStateOpt.ifPresent(ns -> {
if (!ns.equalsIgnoreCase(Constants.C_NULL_STATE))
eventData.nextState.setStateName(ns);
else
eventData.nextState.Reset();
});
return retStat;
}
/**
* Handling Event If actions, the format is:
*
* <pre>
* {@code
* <While>
* <Action>condAction</Action>
* <Actions>
* <Action>...</Action>
* <Action>...</Action>
* <Action>...</Action>
* </Actions>
* <NextState>nextState</NextState>
* </While>
* }
* </pre>
*
* @param eventWhileAction
* @param actionNode
* @return
*/
private RetStat HandleEventWhileAction(EventWhileAction eventWhileAction, JsonNode actionNode) {
RetStat retStat = new RetStat();
/*******************************
* Getting the condition action
*******************************/
retStat = ResolveSimpleAction(eventAction, actionNode );
if (retStat.isSuccess())
{
/************************************
* Getting the while actions
************************************/
retStat = GetEventData(eventWhileAction.whileActions, actionNode);
if (retStat.isFail())
{
retStat.setFail("JsonCallFlowBuilder.HandleEventWhileAction-\tNo while actions after: "
+ actionNode.toString());
}
}
else
{
if (retStat.getError() == null)
retStat.setError("JsonCallFlowBuilder.HandleEventIfAction-\tFailed to Resolve the action"
+ actionNode.toString());
logger.severe(retStat.getError());
return retStat;
}
return retStat;
}
/**
* handling the switch action :
*
* <pre>
* {@code
*{
"Type": "Switch",
"Name": "SwitchAction",
"Cases": [{
"Val1": {
"Actions": [{
"Type": "Action",
"Name": "SimpleAction"
}, {
"Type": "Action",
"Name": "ActionWithParamInt",
"Params": {
"TDCause": "8"
}
}, {
"Type": "Action",
"Name": "ActionWithParamString",
"Params": {
"Party": "ICP"
}
}],
"NextState": "Idle"
}
}, {
"Val2": {
"Actions": [{
"Type": "Action",
"Name": "ActionWithParamInt",
"Params": {
"TDCause": "8"
}
}, {
"Type": "Action",
"Name": "ActionWithParamString",
"Params": {
"Party": "ICP"
}
}],
"NextState": "Idle_NestState1"
}
}]
}
*
* @param eventSwitchAction
* @param actionNode
* @return
*/
private RetStat HandleEventSwitchAction(EventSwitchAction eventSwitchAction,
JsonNode actionNode) {
RetStat retStat = new RetStat();
/******************************************************
* Getting the condition action - the resolving here is different
* because the return status also holds the value as integer
*****************************************************/
retStat = ResolveSimpleAction(eventSwitchAction,actionNode );
if (retStat.isSuccess())
{
/************************************
* Getting the switch actions
************************************/
JsonNode casesArrayNode = actionNode.path(Constants.C_CASES);
if (casesArrayNode.isArray())
{
/************************************
* now going on every case
************************************/
for (JsonNode caseNode : casesArrayNode)
{
Iterator<Entry<String, JsonNode>> casefields = caseNode.fields();
while (casefields.hasNext())
{
Entry<String, JsonNode> nextCase = casefields.next();
if (nextCase.getValue().isObject()) {
EventData eventData = new EventData();
retStat = GetEventData(eventData, nextCase.getValue() );
if (retStat.isFail())
break;
/*
* add the event data
*/
eventSwitchAction.switchActions.add(eventData);
} else {
retStat.setFail("JsonCallFlowBuilder.HandleEventSwitchAction-\tno case object: " + actionNode.toString());
}
}
}
}
else
{
retStat.setFail("JsonCallFlowBuilder.HandleEventSwitchAction-\tNo switch cases after: " + actionNode.toString());
}
}
else
{
retStat.setFail("JsonCallFlowBuilder.HandleEventSwitchAction-\tFailed to Resolve the action :"+ actionNode.toString());
}
return retStat;
}
}
package seh.SEH;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.logging.Logger;
import javax.swing.tree.TreeModel;
import seh.Types.ActivateActionData;
import seh.Types.CallFlow;
import seh.Types.EventAction;
import seh.Types.EventData;
import seh.Types.EventIfAction;
import seh.Types.EventSwitchAction;
import seh.Types.EventWhileAction;
import seh.Types.NextState;
import seh.Types.RetStat;
import seh.Types.State;
import seh.Types.StateIndex;
/**
* This is the State/Event handler engine for XML file describing Finite State
* Machine (FSM) in the form of State/Event handling
*
* @author amir
*
*/
public class SEHEngine
{
Logger logger = Logger.getLogger(getClass().getName());
/*
* Inner Classes
*/
/**********************
* Members Private
**********************/
private ICallFlowBuilder callFlowBuilder;
private String flowsFileName = null;
private CallFlow[] callFlowArray = null;
private Map<String, CallFlow> callFlowMap = null;
private Map<String, String> flowFilesMap = null;
private int currentCallFlowIndex = 0;
private int maxNumberOfCallFlows = 0;
private int startChildCallFlows;
private State currentFirstStatePtr;
private EventAction eventAction;
private EventData eventData = new EventData();;
// acts as pointers in C++
private StateIndex stateIndexPtr;
private State statePtr;
private EventData eventDataPtr;
private ActivateActionData activateActionData;
private TreeModel xmlTreeModel;
private String errorMsg;
private int currentFlowNumOfStates;
private String actionName;
private String eventName;
private BaseSEH baseSEHObject = null;
private Map<String, BaseSEH> baseSEHObjectMap = null;
private Method actionFunc;
protected EventAction eventActionPtr;
/************************
* Methods Private
***********************/
void InitMembers()
{
activateActionData = new ActivateActionData();
activateActionData.object = baseSEHObject;
eventAction = new EventAction();
}
void InitMultiFlowsMembers()
{
callFlowMap = new HashMap<String, CallFlow>();
flowFilesMap = new HashMap<String, String>();
activateActionData = new ActivateActionData();
activateActionData.object = baseSEHObject;
eventAction = new EventAction();
}
/**
* Reading the flows from the file and parsing each flow
*
* @return RetStat
*/
RetStat BuildMultiCallFlowTables(Map<String, String> flowFilesMap)
{
RetStat retStat = new RetStat();
/*
* go over the flows and parse them
*/
Set<Entry<String, String>> entrySet = flowFilesMap.entrySet();
for (Entry<String, String> entry : entrySet)
{
this.baseSEHObject = this.baseSEHObjectMap.get(entry.getKey());
if (this.baseSEHObject != null)
{
retStat = BuildSingleFlowTable(entry.getKey(), entry.getValue());
if (retStat.isFail())
{
if (retStat.getError() == null)
logger.severe("BuildMultiCallFlowTables-\tFailed to parse flow");
else
logger.severe(retStat.getError());
break;
}
}
else
{
logger.severe("BuildMultiCallFlowTables-\tFailed to get baseSEHObject for: " + entry.getKey());
}
}
if (retStat.isSuccess())
{
retStat = ResolveNextStateIndexes();
if (retStat.isFail())
logger.severe("BuildMultiCallFlowTables-\tFailed in ResolveNextStateIndexes");
}
return retStat;
}
/**
* Reading and parsing a single flow file
*
* @return
*/
RetStat BuildSingleFlowTable(String flowPrefix, String flowFile)
{
RetStat retStat = new RetStat();
CallFlow cf = this.callFlowMap.get(flowPrefix);
if (cf != null)
{
callFlowBuilder = createCallFlowBuilder(flowFile);
if (callFlowBuilder != null)
retStat = callFlowBuilder.buildFlowTableFromFile(flowFile, cf,baseSEHObject);
}
else
retStat.setError("BuildSingleFlowTable-\tdid not find call flow for: "
+ flowPrefix);
return retStat;
}
private ICallFlowBuilder createCallFlowBuilder(String flowFile) {
int lastIndexOf = flowFile.lastIndexOf('.');
if (lastIndexOf != -1)
{
String fileExt = flowFile.substring(lastIndexOf + 1);
switch(fileExt)
{
case "xml":
return new XMLCallFlowBuilder();
case "json":
return new JsonCallFlowBuilder();
}
}
return null;
}
RetStat ReadAndCreateFlowsFromFile()
{
RetStat retStat = new RetStat();
try
{
FileInputStream fstream = new FileInputStream(this.flowsFileName);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
// Read File Line By Line
while ((strLine = br.readLine()) != null)
{
if (strLine.startsWith("#"))
continue;
String[] values = strLine.split(" ");
if (values.length == 2)
{
CallFlow cf = new CallFlow();
this.callFlowMap.put(values[0], cf);
this.flowFilesMap.put(values[0], values[1]);
}
}
} catch (FileNotFoundException e)
{
retStat.setError(e.getLocalizedMessage());
e.printStackTrace();
} catch (IOException e)
{
retStat.setError(e.getLocalizedMessage());
e.printStackTrace();
}
return retStat;
}
//
// // Resolving
// UINT ResolveFlowName(String ba_FlowName);
// UINT ResolveFlowPrefix(String ba_FlowPrefix);
// UINT ResolveParty(String ba_PartyName);
// UINT ResolveEventEnum(String ba_EventName);
/**
* Resolving the next state index from the state name here there is only one
* call flow so wre don't need to resolve which call flow (for now)
*/
RetStat ResolveNextStateIndex(NextState nextStatePtr)
{
RetStat retStat = new RetStat();
nextStatePtr.stateIndex.uiStateFlowIndex = 0; // "there can be only one"
/*************************
* Resolve the State index
*************************/
for (CallFlow callFlow : this.callFlowArray)
{
int iFlowIndex = 0;
if (callFlow.iNumOfStates > 0 && callFlow.stateArray != null)
{
int iStateIndex = 0;
for (State statePtr : callFlow.stateArray)
{
if (statePtr.stateName.equalsIgnoreCase(nextStatePtr.stateName))
{
nextStatePtr.stateIndex.uiStateFlowIndex = iFlowIndex;
nextStatePtr.stateIndex.uiStateIndex = iStateIndex;
return retStat;
}
iStateIndex++;
}
iFlowIndex++;
}
}
retStat.setFail();
return retStat;
}
/**
* Resolving the state index from the state name + flow name : Going over
* the valid call-flows, going over each state and its events and resolving
* the actions next state
*/
RetStat ResolveNextStateIndexes()
{
RetStat retStat = new RetStat();
// tCallFlow* pc_CallFlow;
NextState nextStatePtr = null;
for (CallFlow callFlow : this.callFlowArray)
{
if (callFlow.iNumOfStates > 0 && callFlow.stateArray != null)
{
/*****************************
* going over the flow states
*****************************/
for (State statePtr : callFlow.stateArray)
{
/**************************
* Scanning the map events
*************************/
Collection<EventData> eventsPtr = statePtr.eventsMap.values();
for (EventData eventPtr : eventsPtr)
{
/*****************************
* checking for valid event
****************************/
if (eventPtr != null && !eventPtr.isEmpty())
{
/********************************************
* Resolve complex actions next state if any
*******************************************/
for (EventAction action : eventPtr.listActions)
{
retStat = ResolveComplexActionNextState(action);
if (retStat.isFail())
return retStat;
}
/*****************************************
* Resolving the NextState Name into index if the next
* state is empty then it means that we want to stay in
* the same state
****************************************/
nextStatePtr = eventPtr.nextState;
if (nextStatePtr.IsValid())
{
retStat = ResolveNextStateIndex(nextStatePtr);
if (retStat.isFail())
{
retStat.setError("SEHEngine::ResolveNextStateIndexes-\tFailed to Resolve NextState : "
+ nextStatePtr.stateName);
return retStat;
}
}
}
}
/******************************
* Resolve Base State if exists
*****************************/
if (statePtr.baseState != null && statePtr.baseState.IsValid())
{
retStat = ResolveNextStateIndex(statePtr.baseState);
if (retStat.isFail())
{
if (retStat.getError() == null)
retStat.setError("SEHEngine::ResolveNextStateIndexes-\tFailed to Resolve BaseState : "
+ statePtr.baseState.stateName);
return retStat;
}
}
}
}
}
return retStat;
}
/**
* Resolves complex actions that have next state inside them like
* switch/if/while actions
*
* @param actionPtr
* @return
*/
RetStat ResolveComplexActionNextState(EventAction actionPtr)
{
RetStat retStat = new RetStat();
EventIfAction eventIfActionPtr;
EventWhileAction eventWhileActionPtr;
EventSwitchAction eventSwitchActionPtr;
NextState nextState;
switch (actionPtr.actionType)
{
case E_ActionType_Regular:
break;
case E_ActionType_If:
eventIfActionPtr = (EventIfAction) actionPtr;
/*
* checking the true actions
*/
nextState = eventIfActionPtr.trueActions.nextState;
if (nextState.IsValid())
{
retStat = ResolveNextStateIndex(nextState);
if (retStat.isFail())
{
retStat.setError("SEHEngine::ResolveNextStateIndexes-\tFailed to Resolve true actions NextState : "
+ nextState.stateName);
return retStat;
}
}
/*
* checking the false actions
*/
nextState = eventIfActionPtr.falseActions.nextState;
if (nextState.IsValid())
{
retStat = ResolveNextStateIndex(nextState);
if (retStat.isFail())
{
retStat.setError("SEHEngine::ResolveNextStateIndexes-\tFailed to Resolve true actions NextState : "
+ nextState.stateName);
return retStat;
}
}
break;
case E_ActionType_While:
eventWhileActionPtr = (EventWhileAction) actionPtr;
nextState = eventWhileActionPtr.whileActions.nextState;
if (nextState.IsValid())
{
retStat = ResolveNextStateIndex(nextState);
if (retStat.isFail())
{
retStat.setError("SEHEngine::ResolveNextStateIndexes-\tFailed to Resolve NextState in the While: "
+ nextState);
return retStat;
}
}
break;
case E_ActionType_Switch:
eventSwitchActionPtr = (EventSwitchAction) actionPtr;
/*
* going over the cases
*/
for (EventData eventData : eventSwitchActionPtr.switchActions)
{
nextState = eventData.nextState;
if (nextState.IsValid())
{
retStat = ResolveNextStateIndex(nextState);
if (retStat.isFail())
{
retStat.setError("SEHEngine::ResolveNextStateIndexes-\tFailed to Resolve NextState in the Switch: "
+ nextState.stateName);
return retStat;
}
}
}
break;
}
return retStat;
}
// RetStat ReplaceParentStates(UINT ui_FlowIndex);
//
/**
* activating the actions and moving to the next state
*
* @param nextStateIndex
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
RetStat ActivateActions(StateIndex nextStateIndex)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
RetStat retStat = new RetStat();
// boolean b_WasStateChanged = false;
boolean b_DidLastActionFailed = false;
int iNumOfActions = this.eventDataPtr.getNumOfActions();
activateActionData.nextStateIndex = nextStateIndex;
activateActionData.boolWasStateChanged = false;
for (EventAction eventAction : this.eventDataPtr.listActions)
{
this.eventActionPtr = eventAction;
/************************************************************
* activating the action. checking if the last action failed, if so
* then activating only mandatory actions
***********************************************************/
if (b_DidLastActionFailed)
{
if (this.eventActionPtr.isActionMandatory())
{
retStat = this.eventActionPtr
.ActivateAction(activateActionData);
if (retStat.isFail())
{
logger.severe("SEHEngine::ActivateActions-\tFailed in Action: "
+ this.eventActionPtr.toString());
b_DidLastActionFailed = true;
}
}
} else
// everything is o.k.
{
retStat = this.eventActionPtr
.ActivateAction(activateActionData);
if (retStat.isFail())
{
if (!this.eventActionPtr.isActionCritical())
{
logger.severe("SEHEngine::ActivateActions-\tFailed in Action"
+ this.eventActionPtr.toString());
b_DidLastActionFailed = true;
}
}
}
// If state was moved to next state in the called object then we
// must not continue
if (activateActionData.boolWasStateChanged)
break;
}
/***************************
* Move to next state
**************************/
if (!activateActionData.boolWasStateChanged)
{
if (iNumOfActions > 0)
{
/*************************************
* Go to next State check if the state need to change
************************************/
if (this.eventDataPtr.nextState.IsValid())
{
logger.info("SEHEngine-\tChanging to State: "
+ this.eventDataPtr.nextState.stateName);
/*
* Setting next state
*/
nextStateIndex.uiStateFlowIndex = this.eventDataPtr.nextState.stateIndex.uiStateFlowIndex;
nextStateIndex.uiStateIndex = this.eventDataPtr.nextState.stateIndex.uiStateIndex;
} else
{
logger.info("SEHEngine-\tStaying in the same State: "
+ this.statePtr.stateName);
}
} else
{
retStat.setFail("SEHEngine::ActivateActions-\tNo actions");
}
}
return retStat;
}
// RetStat ActivateAction();
/************************
* Methods Public
***********************/
public SEHEngine()
{
}
/**
* Initialize the engine with a list of flows , each flow is attached to a
* BaseSEH class
*
* @param flowsFileName
* @param baseSehMap
* @param iMaxNumOfCallFlows
* @return
* @throws SecurityException
* @throws NoSuchMethodException
*/
public RetStat InitMultiFlows(String flowsFileName, Map<String, BaseSEH> baseSehMap)
throws SecurityException, NoSuchMethodException
{
this.flowsFileName = new String(flowsFileName);
maxNumberOfCallFlows = 1; // will be resolved from the flow file
this.baseSEHObjectMap = baseSehMap;
InitMultiFlowsMembers();
RetStat retStat = ReadAndCreateFlowsFromFile();
if (retStat.isSuccess())
{
/*
* set the max flows
* and current and init the callflow array
*/
maxNumberOfCallFlows = flowFilesMap.size();
callFlowArray = new CallFlow[maxNumberOfCallFlows];
/*
* setting the flows in the array
* and the flow index in the call flow
*/
int index = 0;
Set<Entry<String, CallFlow>> entrySet = callFlowMap.entrySet();
for (Entry<String, CallFlow> entry : entrySet)
{
CallFlow cf = entry.getValue();
cf.Reset();
cf.flowIndex = index;
cf.baseSEHObject = baseSEHObjectMap.get(entry.getKey());
callFlowArray[index++] = cf;
}
retStat = BuildMultiCallFlowTables(flowFilesMap);
}
return retStat;
}
public RetStat InitMultiFlows(Map<String, String> flowFilesMap, Map<String, BaseSEH> baseSehMap)
throws SecurityException, NoSuchMethodException
{
maxNumberOfCallFlows = 1; // will be resolved from the flow file
this.baseSEHObjectMap = baseSehMap;
InitMultiFlowsMembers();
/*
* set the max flows
* and current and init the callflow array
*/
maxNumberOfCallFlows = flowFilesMap.size();
callFlowArray = new CallFlow[maxNumberOfCallFlows];
/*
* Init Call flow map
*/
int index = 0;
Set<Entry<String, String>> entrySet = flowFilesMap.entrySet();
for (Entry<String, String> entry : entrySet)
{
CallFlow cf = new CallFlow();
cf.Reset();
this.callFlowMap.put(entry.getKey(), cf);
cf.flowIndex = index;
cf.baseSEHObject = baseSEHObjectMap.get(entry.getKey());
callFlowArray[index++] = cf;
}
return BuildMultiCallFlowTables(flowFilesMap);
}
/**
* Handling the State/Event : This method is what this is all about, getting
* the State/Event and activating the resolved actions and setting the next
* state
*
* @param stateIndex
* @param iEvent
* @return
*/
public RetStat HandleEvent(StateIndex stateIndex, String eventStr /*int iEvent */)
{
RetStat retStatus = new RetStat();
CallFlow callFlow = null;
/********************
* Validating
********************/
/******************************
* Reseting the working params Getting the Session State
*****************************/
this.stateIndexPtr = stateIndex;
/*************************
* Validate State indexes
************************/
if (this.stateIndexPtr.uiStateFlowIndex < this.maxNumberOfCallFlows)
{
callFlow = this.callFlowArray[this.stateIndexPtr.uiStateFlowIndex];
if (this.stateIndexPtr.uiStateIndex < callFlow.iNumOfStates)
{
/*************************************************
* Getting the EventData from State/Event/Party
* and setting the BaseSEH object
***********************************************/
this.statePtr = callFlow.stateArray[this.stateIndexPtr.uiStateIndex];
this.eventDataPtr = this.statePtr.eventsMap.get(eventStr); //eventCellsArray[iEvent];
this.baseSEHObject = callFlow.baseSEHObject;
this.activateActionData.object = callFlow.baseSEHObject;;
String eventName = eventStr; //this.baseSEHObject.resolveEventName(iEvent);
if (eventName != null)
logger.info("SEHEngine::HandleEvent-\tState/Event: " + this.statePtr.stateName + '/' + eventName);
/**********************************************
* if no implementation for this State/Event then looking if
* there is a BaseState
*********************************************/
if ((this.eventDataPtr == null || this.eventDataPtr
.getNumOfActions() == 0)
&& (this.statePtr.baseState != null && this.statePtr.baseState
.IsValid()))
{
this.stateIndexPtr = this.statePtr.baseState.stateIndex;
this.statePtr = callFlow.stateArray[this.stateIndexPtr.uiStateIndex];
this.eventDataPtr = this.statePtr.eventsMap.get(eventStr); // eventCellsArray[iEvent];
}
if (this.eventDataPtr != null)
{
// logger.severe("SEHEngine::HandleEvent-\tEvent: "
// + eventName + " Not Implemented for State: "
// + this.statePtr.stateName);
// retStatus.setFail();
// return retStatus;
// }
/*************************
* Get & Activate Actions
************************/
try
{
retStatus = ActivateActions(this.stateIndexPtr);
} catch (IllegalArgumentException e)
{
retStatus
.setFail("SEHEngine::HandleEvent-\tFailed in ActivateActions: "
+ e.getLocalizedMessage());
} catch (IllegalAccessException e)
{
retStatus
.setFail("SEHEngine::HandleEvent-\tFailed in ActivateActions: "
+ e.getLocalizedMessage());
} catch (InvocationTargetException e)
{
retStatus
.setFail("SEHEngine::HandleEvent-\tFailed in ActivateActions: "
+ e.getLocalizedMessage());
}
}
else
{
logger.severe("SEHEngine::HandleEvent-\tEvent: "
+ eventName + " Not Implemented for State: "
+ this.statePtr.stateName);
retStatus.setFail();
}
} else
{
logger.severe("SEHEngine::HandleEvent-\tWrong State index");
retStatus.setFail();
//return retStatus;
}
} else
{
logger.severe("SEHEngine::HandleEvent-\tWrong Flow index");
retStatus.setFail();
//return retStatus;
}
return retStatus;
}
public RetStat HandleEvent(StateIndex stateIndex, String eventStr /*int iEvent */, Object appData)
{
this.baseSEHObject.setAppData(appData);
return HandleEvent(stateIndex, eventStr); //iEvent);
}
/**
* Getting the state name from the stateIndex
*
* @param stateIndex
* @return
*/
public String GetStateName(StateIndex stateIndex)
{
if (stateIndex.uiStateFlowIndex < maxNumberOfCallFlows)
{
if (stateIndex.uiStateIndex < callFlowArray[stateIndex.uiStateFlowIndex].iNumOfStates)
return callFlowArray[stateIndex.uiStateFlowIndex].stateArray[stateIndex.uiStateIndex].stateName;
else
return Constants.C_WRONG_SEH_STATE;
}
return Constants.C_WRONG_SEH_STATE;
}
public int GetCurrentNumOfCallFlows()
{
return maxNumberOfCallFlows;
}
/**
* Getting the stateIndex from the State Name and flow file
*
* @param stateIndex
* @param flowFileName
* @param ba_StateName
* @return
*/
public RetStat GetStateIndex(StateIndex stateIndex, String stateName)
{
RetStat retStatus = new RetStat();
int iStateIndex = 0;
if (stateName != null)
{
for (CallFlow flow : this.callFlowMap.values())
{
/**************************
* finding the state
*************************/
for (State statePtr : flow.stateArray)
{
if (statePtr.stateName.equals(stateName))
{
stateIndex.uiStateFlowIndex = flow.flowIndex;
stateIndex.uiStateIndex = iStateIndex;
return retStatus;
}
iStateIndex++;
}
}
logger.severe("SEHEngine::GetStateIndex-\tThe state: " + stateName
+ " does not exists");
retStatus.setFail();
}
else
{
stateIndex.uiStateFlowIndex = 0;
stateIndex.uiStateIndex = 0;
}
return retStatus;
}
}
package seh.SEH;
/**
* interface for the parameter passed to the action method
* @author amir
*
*/
public interface SEHParam
{
void SetParam(String name, String value);
}
package seh.SEH;
import java.util.Enumeration;
import java.util.logging.Logger;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import seh.SEH.Enums.EnumRetStat;
import seh.SEH.Enums.EnumsEventActionType;
import seh.Types.CallFlow;
import seh.Types.EventAction;
import seh.Types.EventData;
import seh.Types.EventIfAction;
import seh.Types.EventSwitchAction;
import seh.Types.EventWhileAction;
import seh.Types.NextState;
import seh.Types.RetStat;
import seh.Types.State;
import seh.Util.XMLTree;
import seh.Util.XMLTree.Tag;
public class XMLCallFlowBuilder implements ICallFlowBuilder {
Logger logger = Logger.getLogger(getClass().getName());
public EventData eventData = new EventData();
public EventAction eventAction = new EventAction();
public EventAction eventActionPtr;
public CallFlow callFlow;
public BaseSEH baseSEHObject;
@Override
public RetStat buildFlowTableFromFile(String flowFileName, CallFlow callFlow,BaseSEH baseSEHObject) {
RetStat retStat = new RetStat(EnumRetStat.E_SUCCESS, 0, null);
Tag xmlItemPtr = null;
DefaultMutableTreeNode nodePtr;
DefaultMutableTreeNode rootNode;
DefaultMutableTreeNode currentStateNodePtr;
int iCurrentState = 0;
State statePtr;
State currentFirstStatePtr = null;
int currentFlowNumOfStates = 0;
this.callFlow = callFlow;
this.baseSEHObject = baseSEHObject;
//callFlow.Reset();
/*********************************************
* copying the file name and reading the file
********************************************/
this.callFlow.flowFileName = flowFileName;
/************************************
* Calling the XMLTree to parse the file and return a Tree from the xml
* file
***********************************/
XMLTree parser = new XMLTree();
TreeModel xmlTreeModel = parser.parse(flowFileName);
if (xmlTreeModel != null)
{
/*************************************************
* Going over the tree: - Counting the number of states - Allocating
* the states - going over each state and reading it's data
************************************************/
rootNode = (DefaultMutableTreeNode) xmlTreeModel.getRoot();
/************************************
* - Counting the number of states and setting the node to the first
* child
*************************************/
callFlow.iNumOfStates = xmlTreeModel.getChildCount(rootNode);
/***************************
* - Allocating the states
***************************/
if (callFlow.iNumOfStates > 0)
{
callFlow.stateArray = new State[callFlow.iNumOfStates];
if (callFlow.stateArray == null)
{
retStat.setFail("SEHEngine::BuildFlowTable-\tFailed to allocate memory");
return retStat;
}
/*
* allocating each cell
*/
for (int i = 0; i < callFlow.iNumOfStates; i++)
callFlow.stateArray[i] = new State();
} else
{
retStat.setFail("SEHEngine::BuildFlowTable-\tno States for "
+ flowFileName);
return retStat;
}
/*******************************************************
* Setting the current first state in the current state array, this
* will be useful later for searching
*******************************************************/
currentFirstStatePtr = callFlow.stateArray[0];
currentFlowNumOfStates = callFlow.iNumOfStates;
/*************************************
* Going over the States and Setting the State Names
************************************/
Enumeration<?> stateNodes = rootNode.children();
iCurrentState = 0;
while (stateNodes.hasMoreElements())
{
statePtr = callFlow.stateArray[iCurrentState];
currentStateNodePtr = (DefaultMutableTreeNode) stateNodes
.nextElement();
/*
* Getting the <Name> tag - first child
*/
nodePtr = (DefaultMutableTreeNode) xmlTreeModel.getChild(
currentStateNodePtr, 0);
xmlItemPtr = (Tag) nodePtr.getUserObject();
statePtr.stateName = new String(xmlItemPtr.getData());
/*
* Getting the State data
*/
retStat = GetState(currentStateNodePtr, statePtr );
if (retStat.enumRetStat.equals(EnumRetStat.E_FAIL))
{
if (retStat.getError() == null)
retStat.setError("SEHEngine::BuildFlowTable-\tFailed in State: "
+ statePtr.stateName);
return retStat;
}
/****************************
* Going to the next state
****************************/
iCurrentState++;
}
} else
{
retStat.setFail("SEHEngine::BuildFlowTable-\tFailed to read file: "
+ flowFileName);
}
return retStat;
}
@Override
public RetStat buildFlowTableFromString(String flowFSM, CallFlow callFlow,
BaseSEH baseSEHObject) {
// TODO Auto-generated method stub
return new RetStat(EnumRetStat.E_FAIL, 0, "Not implemented yet");
}
/**
* Getting the state and all it's data
*
* @param nodePtr
* @param statePtr
* @return RetStat
*/
RetStat GetState(DefaultMutableTreeNode stateNodePtr, State statePtr )
{
RetStat retStat = new RetStat(EnumRetStat.E_SUCCESS, 0, null);
DefaultMutableTreeNode nodePtr = null;
Tag xmlItemPtr = null;
/***************************
* Getting the State Events
**************************/
Enumeration<?> nodeEnumeration = stateNodePtr.children();
while (nodeEnumeration.hasMoreElements())
{
nodePtr = (DefaultMutableTreeNode) nodeEnumeration.nextElement();
/*
* checking for <Name> or <BaseState> tags
*/
if (nodePtr.isLeaf())
{
xmlItemPtr = (Tag) nodePtr.getUserObject();
if (xmlItemPtr.getName().equals(Constants.C_BASE_STATE))
{
statePtr.baseState = new NextState();
// Getting the base state name
statePtr.baseState.stateName = new String(
xmlItemPtr.getData());
}
continue;
}
/*
* All the nodes which are not leafs are considered as Event nodes
*/
/****************************
* Getting the Event
****************************/
retStat = GetEvent(nodePtr );
if (retStat.isFail())
{
if (retStat.getError() == null)
retStat.setError("SEHEngine::GetState-\tFailed Getting the event");
return retStat;
}
/*
* adding the event to map
*/
try
{
statePtr.eventsMap.put(this.eventData.eventName,(EventData)this.eventData.clone());
} catch (CloneNotSupportedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
retStat.setFail("SEHEngine::GetState-\tFailed in cloning event "
+ this.eventData.eventName);
return retStat;
}
}
return retStat;
}
/**
* Getting the event data
*
* @param eventNodePtr
* @return
*/
RetStat GetEvent(DefaultMutableTreeNode eventNodePtr )
{
RetStat retStat = new RetStat();
DefaultMutableTreeNode nodePtr = null;
this.eventData.Reset();
Tag xmlItemPtr = null;
/*
* Going over the Event children
*/
Enumeration<?> nodeEnumeration = eventNodePtr.children();
while (nodeEnumeration.hasMoreElements())
{
nodePtr = (DefaultMutableTreeNode) nodeEnumeration.nextElement();
/*
* checking for leaf : Event name / Next state
*/
if (nodePtr.isLeaf())
{
xmlItemPtr = (Tag) nodePtr.getUserObject();
if (xmlItemPtr.getName().equals(Constants.C_NAME))
{
/*******************************
* Getting the event name
******************************/
this.eventData.eventName = new String(xmlItemPtr.getData());
} else if (xmlItemPtr.getName().equals(Constants.C_NEXT_STATE))
{
String nextStateString = xmlItemPtr.getData();
if (nextStateString == null
|| nextStateString
.equalsIgnoreCase(Constants.C_NULL_STATE))
{
this.eventData.nextState.Reset();
} else
{
this.eventData.nextState.stateName = new String(
nextStateString);
}
}
continue;
}
/*****************************
* Getting the Event Actions
*****************************/
retStat = GetEventActions(nodePtr );
if (retStat.isFail())
{
if (retStat.getError() == null)
retStat.setError("SEHEngine::GetEvent-\tFailed to get event actions for event: "
+ this.eventData.eventName);
return retStat;
}
}
return retStat;
}
/**
* Getting the event actions, resolving their names to functions pointers
* and storing them
*
* @param actionsNodePtr
* @return
*/
RetStat GetEventActions(DefaultMutableTreeNode actionsNodePtr )
{
RetStat retStat = new RetStat();
DefaultMutableTreeNode nodePtr = null;
/*
* Going over the Event actions
*/
Enumeration<?> nodeEnumeration = actionsNodePtr.children();
while (nodeEnumeration.hasMoreElements())
{
nodePtr = (DefaultMutableTreeNode) nodeEnumeration.nextElement();
/*******************************
* Resolving the action
*******************************/
retStat = GetAction(nodePtr );
if (retStat.isFail())
return retStat;
/*****************************
* Storing the action
****************************/
this.eventData.AddAction(this.eventActionPtr);
}
return retStat;
}
/**
* Getting the Action
*
* @param actionNodePtr
* @return
*/
RetStat GetAction(DefaultMutableTreeNode actionNodePtr )
{
RetStat retStat = new RetStat();
Tag xmlItemPtr = (Tag) actionNodePtr.getUserObject();
// char* pba_Space = null;
this.eventAction.Reset();
/*******************************
* Resolving the type of action
******************************/
if (xmlItemPtr.getName().equalsIgnoreCase(
EnumsEventActionType.E_ActionType_Regular.getName()))
{
// Handle action
this.eventActionPtr = new EventAction();
this.eventActionPtr.Reset();
return ResolveSimpleAction(this.eventActionPtr, actionNodePtr );
} else if (xmlItemPtr.getName().equalsIgnoreCase(
EnumsEventActionType.E_ActionType_If.getName()))
{
this.eventActionPtr = new EventIfAction();
this.eventActionPtr.Reset();
/*******************************
* Getting the condition action
********************************/
return HandleEventIfAction((EventIfAction) this.eventActionPtr,actionNodePtr );
} else if (xmlItemPtr.getName().equalsIgnoreCase(
EnumsEventActionType.E_ActionType_While.getName()))
{
this.eventActionPtr = new EventWhileAction();
this.eventActionPtr.Reset();
/*******************************
* Getting the condition action
********************************/
return HandleEventWhileAction(
(EventWhileAction) this.eventActionPtr, actionNodePtr);
} else if (xmlItemPtr.getName().equalsIgnoreCase(
EnumsEventActionType.E_ActionType_Switch.getName()))
{
this.eventActionPtr = new EventSwitchAction();
this.eventActionPtr.Reset();
/*******************************
* Getting the condition action
********************************/
return HandleEventSwitchAction(
(EventSwitchAction) this.eventActionPtr, actionNodePtr);
}
/*
* else send fail
*/
retStat.setFail();
return retStat;
}
/**
* Getting the Action parameters
*
* @param actionPtr
* @param actionNodePtr
* @return
*/
void GetActionParams(EventAction actionPtr, DefaultMutableTreeNode paramsNodePtr )
{
Tag xmlItemPtr = null; // (Tag)actionNodePtr.getUserObject();
DefaultMutableTreeNode nodePtr = null;
/*****************************
* Allocating the action parmas class and Going over the params
******************************/
actionPtr.param = this.baseSEHObject.getNewSEHParam();
if (actionPtr.param != null)
{
/*
* Going over the Parameters
*/
Enumeration<?> nodeEnumeration = paramsNodePtr.children();
while (nodeEnumeration.hasMoreElements())
{
nodePtr = (DefaultMutableTreeNode) nodeEnumeration
.nextElement();
xmlItemPtr = (Tag) nodePtr.getUserObject();
/*
* Setting the parameter in the param object
*/
actionPtr.param.SetParam(xmlItemPtr.getName(),
xmlItemPtr.getData());
}
} else
{
logger.info("SEHEngine::GetActionParams-\tNo param in BaseSEH but the XML file has <Param> tags!!!");
}
}
//
// // Child/Parent handling
// RetStat GetParentState(String ba_StateName,tState** ppt_ParentState);
// RetStat GetParentEvent(tState* pt_ParentState,String
// ba_EventName,tEventData** ppt_ParentEvent);
//
// // Special handling
/**
* Handling Event If actions, the format is:
*
* <pre>
* {@code
* <If Cond="condAction">
* <Then>
* <Actions>
* <Action>...</Action>
* <Action>...</Action>
* <Action>...</Action>
* </Actions>
* <NextState>nextState</NextState>
* </Then>
* <Else>
* <Actions>
* <Action>...</Action>
* </Actions>
* <NextState>nextState</NextState>
* </Else>
* </If>
* }
* </pre>
*
* @param actionPtr
* @param actionNodePtr
* @return
*/
RetStat HandleEventIfAction(EventIfAction actionPtr,
DefaultMutableTreeNode actionNodePtr )
{
RetStat retStat = new RetStat();
Tag xmlItemPtr = (Tag) actionNodePtr.getUserObject();
DefaultMutableTreeNode nodePtr = null;
DefaultMutableTreeNode condNodePtr = null; // then/else
/*
* the condition action is in it's own <Name> tag : <if>
* <Action>condAction</Action> Getting the Name tag
*/
nodePtr = (DefaultMutableTreeNode) actionNodePtr.getFirstChild();
xmlItemPtr = (Tag) nodePtr.getUserObject();
/*******************************
* Getting the condition action
*******************************/
retStat = ResolveSimpleAction(actionPtr, nodePtr );
if (retStat.isFail())
{
if (retStat.getError() == null)
retStat.setError("SEHEngine::HandleEventIfAction-\tFailed to Resolve the action"
+ xmlItemPtr.getName());
logger.severe(retStat.getError());
return retStat;
}
/*
* move to the next node
*/
nodePtr = (DefaultMutableTreeNode) nodePtr.getNextSibling();
condNodePtr = nodePtr;
if (nodePtr != null)
{
// True actions
nodePtr = (DefaultMutableTreeNode) nodePtr.getFirstChild();
retStat = GetEventData(actionPtr.trueActions, nodePtr );
if (retStat.isSuccess())
{
/*
* move to the next node - False actions
*/
nodePtr = (DefaultMutableTreeNode) condNodePtr.getNextSibling();
if (nodePtr != null)
{
nodePtr = (DefaultMutableTreeNode) nodePtr.getFirstChild();
retStat = GetEventData(actionPtr.falseActions, nodePtr );
if (retStat.isSuccess())
{
return retStat;
}
} else
{
retStat.setFail("SEHEngine::HandleEventIfAction-\tNo false actions in: "
+ actionNodePtr.toString());
}
}
} else
{
retStat.setFail("SEHEngine::HandleEventIfAction-\tNo true actions in: "
+ actionNodePtr.toString());
}
retStat.setFail();
return retStat;
}
/**
* handling the switch action :
*
* <pre>
* {@code
* <Switch>
* <Action>condAction</Action>
* <Case>
* <Value>value1</Value>
* <Actions>
* <Action>...</Action>
* <Action>...</Action>
* <Action>...</Action>
* </Actions>
* <NextState>nextState1</NextState>
* </Case>
* <Case>
* <Value>value2</Value>
* <Actions>
* <Action>...</Action>
* <Action>...</Action>
* <Action>...</Action>
* </Actions>
* <NextState>nextState2</NextState>
* </Case>
* </Switch>
* }
* </pre>
*
* @param actionPtr
* @param actionNodePtr
* @return
*/
RetStat HandleEventSwitchAction(EventSwitchAction actionPtr,
DefaultMutableTreeNode actionNodePtr )
{
RetStat retStat = new RetStat();
DefaultMutableTreeNode nodePtr = null;
DefaultMutableTreeNode caseNodePtr = null;
Tag xmlItemPtr = null; // (Tag)actionNodePtr.getUserObject();
nodePtr = (DefaultMutableTreeNode) actionNodePtr.getFirstChild();
if (nodePtr != null)
{
xmlItemPtr = (Tag) nodePtr.getUserObject();
/******************************************************
* Getting the condition action - the resolving here is different
* because the return status also holds the value as integer
*****************************************************/
retStat = ResolveSimpleAction(actionPtr, nodePtr );
if (retStat.isSuccess())
{
/************************************
* Getting the switch actions
************************************/
nodePtr = nodePtr.getNextSibling();
if (nodePtr != null)
{
caseNodePtr = nodePtr;
/************************************
* now going on every case
************************************/
while (caseNodePtr != null)
{
nodePtr = (DefaultMutableTreeNode) caseNodePtr
.getFirstChild();
while (nodePtr != null)
{
if (!nodePtr.isLeaf()) // leaf if the <Value> tag
{
EventData eventData = new EventData();
retStat = GetEventData(eventData, nodePtr );
if (retStat.isFail())
break;
/*
* add the event data
*/
actionPtr.switchActions.add(eventData);
}
nodePtr = nodePtr.getNextSibling();
}
/*
* move to the next case
*/
caseNodePtr = caseNodePtr.getNextSibling();
}
// check for error
if (retStat.isFail() && retStat.getError() == null)
retStat.setFail("SEHEngine::HandleEventSwitchAction-\tNo switch actions after: "
+ xmlItemPtr.toString());
} else
retStat.setFail("SEHEngine::HandleEventSwitchAction-\tNo switch cases after: "
+ xmlItemPtr.toString());
} else
retStat.setFail("SEHEngine::HandleEventSwitchAction-\tFailed to Resolve the action :"
+ xmlItemPtr.toString());
} else
retStat.setFail("SEHEngine::HandleEventSwitchAction-\tNo children !!! :");
return retStat;
}
/**
* Getting the method of an action with no parameters:
*
* <pre>
* {@code
* <Action>EndSession</Action>
* }
* </pre>
*
* @param actionPtr
* @param action
* @return
*/
RetStat ResolveAction(EventAction actionPtr, String action )
{
RetStat retStat = new RetStat();
/*********************************
* Resolve action
********************************/
actionPtr.action = this.baseSEHObject.resolveAction(action);
if (actionPtr.action == null)
{
retStat.setFail("SEHEngine::ResolveAction-\tFailed to Resolve the action"
+ action);
}
return retStat;
}
/**
* resolving a simple action - not a condition like if/while/switch
*
* @param actionPtr
* @param actionNodePtr
* @param workObjs
* @param bValueAction
* @return
*/
RetStat ResolveSimpleAction(EventAction actionPtr,
DefaultMutableTreeNode actionNodePtr)
{
Tag xmlItemPtr = (Tag) actionNodePtr.getUserObject();
/*****************************************
* checking if the action is leaf or node
****************************************/
if (actionNodePtr.isLeaf())
{
return ResolveAction(actionPtr, xmlItemPtr.getData() );
} else
{
return ResolveActionWithParams(actionPtr, actionNodePtr );
}
}
/**
* resolving an action with parameters e.g:
*
* <pre>
* {@code
* <Action>
* <Name>PartyClear</Name>
* <Priority>Mandatory</Priority>
* <Params>
* <Party>ISP</Party>
* </Params>
* </Action>
* }
* </pre>
*
* @param actionPtr
* @param actionNodePtr
* @param workObjs
* @return
*/
@SuppressWarnings("unchecked")
RetStat ResolveActionWithParams(EventAction actionPtr,
DefaultMutableTreeNode actionNodePtr)
{
RetStat retStat = new RetStat();
DefaultMutableTreeNode nodePtr = null;
Tag xmlItemPtr = null; // (Tag)actionNodePtr.getUserObject();
/*
* Going over the action tags (children)
*/
/*
* Going over the Event actions
*/
Enumeration<?> nodeEnumeration = actionNodePtr.children();
while (nodeEnumeration.hasMoreElements())
{
nodePtr = (DefaultMutableTreeNode) nodeEnumeration.nextElement();
xmlItemPtr = (Tag) nodePtr.getUserObject();
if (xmlItemPtr != null)
{
if (nodePtr.isLeaf())
{
/*
* a Leaf can be <Name> / <Priority>
*/
if (xmlItemPtr.getName().equalsIgnoreCase(Constants.C_NAME))
{
// <Name>
retStat = ResolveAction(actionPtr, xmlItemPtr.getData() );
if (retStat.isFail())
{
if (retStat.getError() == null)
retStat.setError("SEHEngine::ResolveActionWithParams-\tFailed to Resolve the action: "
+ xmlItemPtr.getData());
return retStat;
}
} else if (xmlItemPtr.getName().equalsIgnoreCase(
Constants.C_ACTION_PRIORITY))
{
// <Priority>
if (xmlItemPtr.getData().equalsIgnoreCase(
Constants.C_ACTION_PRIORITY_MANDATORY))
actionPtr.bIsMandatory = true;
else if (xmlItemPtr.getData().equalsIgnoreCase(
Constants.C_ACTION_PRIORITY_CRITICAL))
actionPtr.bIsCritical = true;
}
} else
{
/*
* a node represents <Param> tag
*/
if (xmlItemPtr.getName().equalsIgnoreCase(
Constants.C_ACTION_PARAMS))
{
GetActionParams(actionPtr, nodePtr );
} else
{
retStat.setFail("SEHEngine::ResolveActionWithParams-\tThe Node is not <Params>: "
+ nodePtr.toString());
return retStat;
}
}
} else
{
retStat.setFail("SEHEngine::ResolveActionWithParams-\tReceived empty node: "
+ nodePtr.toString());
return retStat;
}
}
return retStat;
}
/**
* Handling Event If actions, the format is:
*
* <pre>
* {@code
* <While>
* <Action>condAction</Action>
* <Actions>
* <Action>...</Action>
* <Action>...</Action>
* <Action>...</Action>
* </Actions>
* <NextState>nextState</NextState>
* </While>
* }
* </pre>
*
* @param actionPtr
* @param actionNodePtr
* @return
*/
RetStat HandleEventWhileAction(EventWhileAction actionPtr,
DefaultMutableTreeNode actionNodePtr)
{
RetStat retStat = new RetStat();
Tag xmlItemPtr = (Tag) actionNodePtr.getUserObject();
DefaultMutableTreeNode nodePtr = null;
nodePtr = (DefaultMutableTreeNode) actionNodePtr.getFirstChild();
if (nodePtr != null)
{
/*******************************
* Getting the condition action
*******************************/
retStat = ResolveSimpleAction(actionPtr, nodePtr);
if (retStat.isSuccess())
{
/************************************
* Getting the while actions
************************************/
nodePtr = (DefaultMutableTreeNode) nodePtr.getNextNode();
if (nodePtr != null)
{
retStat = GetEventData(actionPtr.whileActions, nodePtr);
if (retStat.isSuccess())
return retStat;
} else
{
retStat.setFail("SEHEngine::HandleEventWhileAction-\tNo while actions after: "
+ xmlItemPtr.toString());
}
} else
{
retStat.setFail("SEHEngine::HandleEventWhileAction-\tFailed to Resolve the action : "
+ xmlItemPtr.toString());
}
} else
{
retStat.setFail("SEHEngine::HandleEventWhileAction-\tNo child nodes: "
+ xmlItemPtr.toString());
}
return retStat;
}
/**
* Getting all the event data , including it's next state Calling this
* method when node is pointing to <Actions> tag
*
* @param eventDataPtr
* the event data
* @param nodePtr
* the current node
* @return RetStat (SUCCESS/FAIL) with error text/code
*/
RetStat GetEventData(EventData eventDataPtr, DefaultMutableTreeNode nodePtr)
{
RetStat retStat = new RetStat();
DefaultMutableTreeNode firstNodePtr = null;
Tag xmlItemPtr = null;
EventAction actionPtr = null;
eventDataPtr.Reset();
if (nodePtr != null) // Actions
{
firstNodePtr = nodePtr;
if (!nodePtr.isLeaf())
{
/*****************************
* Going over the actions
****************************/
Enumeration<?> nodeEnumeration = nodePtr.children();
while (nodeEnumeration.hasMoreElements())
{
nodePtr = (DefaultMutableTreeNode) nodeEnumeration
.nextElement();
/*******************************
* Resolving the action
*******************************/
actionPtr = new EventAction();
actionPtr.Reset();
xmlItemPtr = (Tag) nodePtr.getUserObject();
retStat = ResolveSimpleAction(actionPtr, nodePtr);
if (retStat.isFail())
{
actionPtr = null;
return retStat;
}
/*****************************
* Storing the action
****************************/
eventDataPtr.AddAction(actionPtr);
}
} else
{
retStat.setFail("SEHEngine::GetEventData-\tNo Event Actions");
return retStat;
}
/***********************************
* Getting the next state - optional sometimes there won't be next
* state
**********************************/
nodePtr = (DefaultMutableTreeNode) firstNodePtr.getNextLeaf();
if (nodePtr != null)
{
/********************************
* Set the Next State
*******************************/
xmlItemPtr = (Tag) nodePtr.getUserObject();
if (xmlItemPtr.getData() != null)
{
if (!(xmlItemPtr.getData()
.equalsIgnoreCase(Constants.C_NULL_STATE)))
eventDataPtr.nextState.stateName = new String(
xmlItemPtr.getData());
else
eventDataPtr.nextState.Reset();
}
}
} else
{
retStat.setFail("SEHEngine::GetEventData-\tNo Actions");
}
return retStat;
}
}
package seh.Types;
import seh.SEH.BaseSEH;
/**
* Data needed to activate the actions and move to the next state
*/
public class ActivateActionData
{
public BaseSEH object;
public StateIndex nextStateIndex;
public boolean boolWasStateChanged; // if true means that the called object
// change the state - moved to the next
// state
}
\ No newline at end of file
package seh.Types;
import seh.SEH.BaseSEH;
/**
* a complete call flow (FSM) file with all its states
*
* @author amir
*
*/
public class CallFlow
{
public int flowIndex = 0;
public State[] stateArray;
public int iNumOfStates;
public String flowFileName;
public BaseSEH baseSEHObject = null;
public void Reset()
{
flowIndex = 0;
stateArray = null;
flowFileName = null;
iNumOfStates = 0;
baseSEHObject = null;
}
public String GetFlowFileName()
{
return flowFileName;
}
}
\ No newline at end of file
package seh.Types;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Logger;
import seh.SEH.SEHEngine;
import seh.SEH.Enums.EnumRetStat;
/**
* contains basic handling for activating complex actions
*
* @author amir
*
*/
public class ComplexEventAction extends EventAction
{
Logger logger = Logger.getLogger(getClass().getName());
/**
*
*/
public ComplexEventAction() {
super();
}
RetStat retStat = new RetStat(EnumRetStat.E_SUCCESS, 0, null);
public RetStat ActivateActions(EventData eventData,
ActivateActionData activateActionData, Boolean bMoveToNextState)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
int iNumOfActions = eventData.getNumOfActions();
for (EventAction eventAction : eventData.listActions)
{
/*
* activating the action
*/
retStat = (RetStat) eventAction.action.invoke(
activateActionData.object, eventAction.param);
if (retStat.enumRetStat.equals(EnumRetStat.E_FAIL))
{
logger.severe("ComplexEventAction::ActivateActions-\tFailed in Action");
break;
}
}
/******************************************
* check whether to move to the next state
******************************************/
if (bMoveToNextState)
{
if (iNumOfActions > 0)
{
if (eventData.isNextStateValid())
{
activateActionData.boolWasStateChanged = true;
activateActionData.nextStateIndex.uiStateFlowIndex = eventData.nextState.stateIndex.uiStateFlowIndex;
activateActionData.nextStateIndex.uiStateIndex = eventData.nextState.stateIndex.uiStateIndex;
logger.info("SEHEngine-\tChanging to State: "
+ eventData.nextState.stateName);
} else
{
activateActionData.boolWasStateChanged = false;
}
} else
{
retStat.enumRetStat = EnumRetStat.E_FAIL;
return retStat;
}
}
retStat.enumRetStat = EnumRetStat.E_SUCCESS;
return retStat;
}
}
\ No newline at end of file
package seh.Types;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Logger;
import seh.SEH.SEHEngine;
import seh.SEH.Enums.EnumRetStat;
/**
* same as {@link ComplexEventAction} but for value actions
*
* @author amir
*
*/
public class ComplexValueEventAction extends EventAction
{
/**
*
*/
Logger logger = Logger.getLogger(getClass().getName());
/**
* @param sehEngine
*/
public ComplexValueEventAction() {
super();
}
RetStat retStat = new RetStat(EnumRetStat.E_SUCCESS, 0, null);
public RetStat ActivateActions(EventData eventData,
ActivateActionData activateActionData, Boolean bMoveToNextState)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
int iNumOfActions = eventData.getNumOfActions();
for (EventAction eventAction : eventData.listActions)
{
retStat = (RetStat) eventAction.action.invoke(
activateActionData.object, eventAction.param);
if (retStat.enumRetStat.equals(EnumRetStat.E_FAIL))
{
logger.severe("ComplexValueEventAction::ActivateActions-\tFailed in Action");
break;
}
}
/******************************************
* check whether to move to the next state
******************************************/
if (bMoveToNextState)
{
if (iNumOfActions > 0)
{
if (eventData.isNextStateValid())
{
activateActionData.boolWasStateChanged = true;
activateActionData.nextStateIndex.uiStateFlowIndex = eventData.nextState.stateIndex.uiStateFlowIndex;
activateActionData.nextStateIndex.uiStateIndex = eventData.nextState.stateIndex.uiStateIndex;
logger.info("SEHEngine-\tChanging to State: "
+ eventData.nextState.stateName);
} else
{
activateActionData.boolWasStateChanged = false;
}
} else
{
retStat.enumRetStat = EnumRetStat.E_FAIL;
return retStat;
}
}
retStat.enumRetStat = EnumRetStat.E_SUCCESS;
return retStat;
}
}
\ No newline at end of file
package seh.Types;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import seh.SEH.SEHParam;
import seh.SEH.Enums.EnumsEventActionType;
/**
* contains the data for the action such as the Method (function pointer),
* the param to pass to the method is this a mandatory action or critical.
* This is also a base class for other complex actions
*
* @author amir
*
*/
public class EventAction
{
public RetStatSupplier actionFunc;
public Method action;
public SEHParam param;
public EnumsEventActionType actionType;
public boolean bIsMandatory = false; // mandatory means that it have to be
// execute , even if the former action
// failed
public boolean bIsCritical = false; // Critical means that if this action
// failes then none of the following
// actions should be
// executed, unless they have 'mandatory' attribute
public EventAction()
{
actionType = EnumsEventActionType.E_ActionType_Regular;
}
public void Reset()
{
action = null;
param = null;
bIsMandatory = false;
bIsCritical = false;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#clone()
*/
@Override
public Object clone() throws CloneNotSupportedException
{
EventAction eventAction = new EventAction();
eventAction.action = this.action;
if (this.param != null)
{
eventAction.param = this.param;
}
return eventAction;
}
public EnumsEventActionType getActionType()
{
return actionType;
}
/**
* activating the action
*
* @param activateActionData
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public RetStat ActivateAction(ActivateActionData activateActionData)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
activateActionData.boolWasStateChanged = false;
if (this.actionFunc != null)
return actionFunc.call();
else // backward support for xml callflow builder
return (RetStat) this.action.invoke(activateActionData.object, param);
}
public boolean isActionMandatory()
{
return bIsMandatory;
}
public boolean isActionCritical()
{
return bIsCritical;
}
}
\ No newline at end of file
package seh.Types;
import java.util.LinkedList;
import java.util.List;
/**
* Contains the event data: the list of actions to activate, the next state
* and the event name
*
* @author amir
*
*/
public class EventData
{
// vector < tEventAction*> ta_Actions;
// test - amir
public List<EventAction> listActions = new LinkedList<EventAction>();
public NextState nextState = new NextState();
public String eventName;
public void Reset()
{
listActions.clear();
nextState.Reset();
eventName = null;
}
public int getNumOfActions()
{
return listActions.size();
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#clone()
*/
@Override
public Object clone() throws CloneNotSupportedException
{
EventData ed = new EventData();
ed.listActions.addAll(this.listActions);
ed.nextState = (NextState) this.nextState.clone();
ed.eventName = new String(this.eventName);
return ed;
}
public void AddAction(EventAction action)
{
listActions.add(action);
}
public boolean isEmpty()
{
return (listActions.size() == 0); /* ta_Actions.empty(); */
}
public boolean isNextStateValid()
{
return nextState.IsValid();
}
}
\ No newline at end of file
package seh.Types;
import java.lang.reflect.InvocationTargetException;
import seh.SEH.Enums.EnumRetStat;
import seh.SEH.Enums.EnumsEventActionType;
/**
* implements If/Else event actions
*
* @author amir
*
*/
public class EventIfAction extends ComplexEventAction
{
public EventData trueActions;
public EventData falseActions;
public EventIfAction()
{
super();
this.actionType = EnumsEventActionType.E_ActionType_If;
trueActions = new EventData();
falseActions = new EventData();
}
public void Reset()
{
super.Reset();
trueActions.Reset();
falseActions.Reset();
}
@Override
public RetStat ActivateAction(ActivateActionData activateActionData)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
/***********************************
* Activating the Condition action
**********************************/
retStat = (RetStat) this.action.invoke(activateActionData.object,
this.param);
if (retStat.enumRetStat.equals(EnumRetStat.E_SUCCESS))
return ActivateActions(trueActions, activateActionData, true);
else
return ActivateActions(falseActions, activateActionData, true);
}
/*
* (non-Javadoc)
*
* @see SEH.SEHEngine.EventAction#clone()
*/
@Override
public Object clone() throws CloneNotSupportedException
{
// TODO Auto-generated method stub
EventIfAction eia = (EventIfAction) super.clone();
eia.trueActions = (EventData) this.trueActions.clone();
eia.falseActions = (EventData) this.falseActions.clone();
return eia;
}
}
\ No newline at end of file
package seh.Types;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import seh.SEH.SEHEngine;
import seh.SEH.Enums.EnumRetStat;
import seh.SEH.Enums.EnumsEventActionType;
/**
* implements switch case event actions the case is of numbers e.g:<br />
* switch (action)<br />
* {<br />
* case 1:<br />
* &nbsp;&nbsp;break;<br />
* case 2:<br />
* &nbsp;&nbsp;break;<br />
* }
*
* @author amir
*
*/
public class EventSwitchAction extends ComplexValueEventAction
{
Logger logger = Logger.getLogger(getClass().getName());
/**
*
*/
public List<EventData> switchActions = new ArrayList<EventData>();
public EventSwitchAction()
{
this.actionType = EnumsEventActionType.E_ActionType_Switch;
}
public void Reset()
{
super.Reset();
switchActions.clear();
}
@Override
public RetStat ActivateAction(ActivateActionData activateActionData)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
EventData eventData;
/*
* activating the switch condition action
*/
retStat = (RetStat) this.action.invoke(activateActionData.object,
this.param);
if (retStat.enumRetStat.equals(EnumRetStat.E_FAIL))
return retStat;
/************************************************************
* Activating the appropriate case according to the ret_code
***********************************************************/
if (retStat.retCode < switchActions.size())
{
eventData = switchActions.get(retStat.retCode);
retStat = (RetStat) ActivateActions(eventData,
activateActionData, true);
if (retStat.enumRetStat.equals(EnumRetStat.E_FAIL))
return retStat;
} else
{
logger.severe("SEHEngine::EventSwitchAction::ActivateAction-\tReturn value is bigger the num of switch cases");
retStat.enumRetStat = EnumRetStat.E_FAIL;
return retStat;
}
/******************************************
* check whether to move to the next state
******************************************/
if (eventData.isNextStateValid())
{
activateActionData.boolWasStateChanged = true;
activateActionData.nextStateIndex.uiStateFlowIndex = eventData.nextState.stateIndex.uiStateFlowIndex;
activateActionData.nextStateIndex.uiStateIndex = eventData.nextState.stateIndex.uiStateIndex;
logger.info("SEHEngine-\tChanging to State: "
+ eventData.nextState.stateName);
} else
{
activateActionData.boolWasStateChanged = false;
}
retStat.enumRetStat = EnumRetStat.E_SUCCESS;
return retStat;
}
/*
* (non-Javadoc)
*
* @see SEH.SEHEngine.EventAction#clone()
*/
@Override
public Object clone() throws CloneNotSupportedException
{
EventSwitchAction esa = (EventSwitchAction) super.clone();
esa.switchActions.addAll(switchActions);
return esa;
}
}
\ No newline at end of file
package seh.Types;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Logger;
import seh.SEH.Enums.EnumRetStat;
import seh.SEH.Enums.EnumsEventActionType;
/**
* implements while event actions
*
* @author amir
*
*/
public class EventWhileAction extends ComplexEventAction
{
Logger logger = Logger.getLogger(getClass().getName());
/**
*
*/
public EventData whileActions;
public EventWhileAction()
{
super();
this.actionType = EnumsEventActionType.E_ActionType_While;
}
public void Reset()
{
super.Reset();
whileActions.Reset();
}
@Override
public RetStat ActivateAction(ActivateActionData activateActionData)
throws IllegalArgumentException, IllegalAccessException,
InvocationTargetException
{
/*
* activating the while condition if success then activating the
* while actions
*/
while (((RetStat) this.action.invoke(activateActionData.object,
this.param)).enumRetStat.equals(EnumRetStat.E_SUCCESS))
{
retStat = (RetStat) ActivateActions(whileActions,
activateActionData, true);
if (retStat.enumRetStat.equals(EnumRetStat.E_FAIL))
return retStat;
}
/******************************************
* check whether to move to the next state
******************************************/
if (whileActions.isNextStateValid())
{
activateActionData.boolWasStateChanged = true;
activateActionData.nextStateIndex.uiStateFlowIndex = whileActions.nextState.stateIndex.uiStateFlowIndex;
activateActionData.nextStateIndex.uiStateIndex = whileActions.nextState.stateIndex.uiStateIndex;
logger.info("SEHEngine-\tChanging to State: "
+ whileActions.nextState.stateName);
} else
{
activateActionData.boolWasStateChanged = false;
}
retStat.enumRetStat = EnumRetStat.E_SUCCESS;
return retStat;
}
/*
* (non-Javadoc)
*
* @see SEH.SEHEngine.EventAction#clone()
*/
@Override
public Object clone() throws CloneNotSupportedException
{
EventWhileAction ewa = (EventWhileAction) super.clone();
ewa.whileActions = this.whileActions;
return ewa;
}
}
\ No newline at end of file
package seh.Types;
/**
* data for the next state
*
* @author amir
*
*/
public class NextState
{
public StateIndex stateIndex = new StateIndex();
public String stateName = null;
public void Reset()
{
stateIndex.Reset();
stateName = null;
}
@Override
public Object clone() throws CloneNotSupportedException
{
NextState ns = new NextState();
ns.stateName = new String(stateName);
return ns;
}
public boolean IsValid()
{
return stateName != null;
}
public String getStateName() {
return stateName;
}
public void setStateName(String stateName) {
this.stateName = stateName;
}
}
\ No newline at end of file
package seh.Types;
import seh.SEH.Enums.EnumRetStat;
public class RetStat
{
public EnumRetStat enumRetStat;
public int retCode;
public String errorText;
public RetStat()
{
enumRetStat = EnumRetStat.E_SUCCESS;
retCode = 0;
errorText = null;
}
public RetStat(EnumRetStat eRetStat, int retCode,String errorText)
{
this.enumRetStat = eRetStat;
this.retCode = retCode;
this.errorText = errorText;
}
public void setSuccess() { enumRetStat = EnumRetStat.E_SUCCESS; }
public void setFail() { enumRetStat = EnumRetStat.E_FAIL; }
public void setFail(String error)
{
enumRetStat = EnumRetStat.E_FAIL;
errorText = new String(error);
}
public void setError(String error)
{
errorText = new String(error);
}
public boolean isSuccess() { return enumRetStat.equals(EnumRetStat.E_SUCCESS); }
public boolean isFail() { return enumRetStat.equals(EnumRetStat.E_FAIL); }
public String getError() { return errorText; }
}
package seh.Types;
@FunctionalInterface
public interface RetStatSupplier {
RetStat call();
}
package seh.Types;
import java.util.HashMap;
import java.util.Map;
/**
* holds that data structure for a State in the xml FSM
*
* @author amir
*
*/
public class State
{
public Map<String,EventData> eventsMap;
public String stateName;
public NextState baseState; // the base state for this state (optional)
public State()
{
Reset();
eventsMap = new HashMap<String,EventData>();
}
public void Reset()
{
eventsMap = null;
stateName = null;
baseState = null;
}
}
\ No newline at end of file
package seh.Types;
import seh.SEH.Constants;
public class StateIndex
{
public int uiStateIndex;
public int uiStateFlowIndex;
public StateIndex()
{
}
public StateIndex(int uiStateIndex,int uiFlowIndex)
{
this.uiStateIndex = uiStateIndex;
this.uiStateFlowIndex = uiFlowIndex;
}
public void Reset()
{
uiStateIndex = Constants.C_NO_ENTRY;
uiStateFlowIndex = Constants.C_NO_ENTRY;
}
public boolean isValid()
{
return (uiStateIndex != Constants.C_NO_ENTRY &&
uiStateFlowIndex != Constants.C_NO_ENTRY);
}
}
package seh.Util;
import java.io.File;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XMLTree
{
public TreeModel parse(String filename) {
SAXParserFactory factory = SAXParserFactory.newInstance();
XMLTreeHandler handler = new XMLTreeHandler();
try {
// Parse the input.
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(new File(filename), handler);
} catch (Exception e) {
System.err.println("File Read Error: " + e);
e.printStackTrace();
return new DefaultTreeModel(new DefaultMutableTreeNode("error"));
}
return new DefaultTreeModel(handler.getRoot());
}
public static class XMLTreeHandler extends DefaultHandler {
private DefaultMutableTreeNode root, currentNode;
public DefaultMutableTreeNode getRoot() {
return root;
}
// SAX Parser Handler methods...
public void startElement(String namespaceURI, String lName,
String qName, Attributes attrs) throws SAXException {
String eName = lName; // Element name
if ("".equals(eName))
eName = qName;
Tag t = new Tag(eName, attrs);
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(t);
if (currentNode == null) {
root = newNode;
} else {
// Must not be the root node...
currentNode.add(newNode);
}
currentNode = newNode;
}
public void endElement(String namespaceURI, String sName, String qName)
throws SAXException {
currentNode = (DefaultMutableTreeNode) currentNode.getParent();
}
public void characters(char buf[], int offset, int len)
throws SAXException {
String s = new String(buf, offset, len).trim();
((Tag) currentNode.getUserObject()).addData(s);
}
}
public static class Tag {
private String name;
private String data;
private Attributes attr;
public Tag(String n, Attributes a) {
name = n;
attr = a;
}
public String getName() {
return name;
}
public Attributes getAttributes() {
return attr;
}
public void setData(String d) {
data = d;
}
public String getData() {
return data;
}
public void addData(String d) {
if (data == null) {
setData(d);
} else {
data += d;
}
}
public String getAttributesAsString() {
StringBuffer buf = new StringBuffer(256);
for (int i = 0; i < attr.getLength(); i++) {
buf.append(attr.getQName(i));
buf.append("=\"");
buf.append(attr.getValue(i));
buf.append("\"");
}
return buf.toString();
}
public String toString() {
String a = getAttributesAsString();
return name + ": " + a + (data == null ? "" : " (" + data + ")");
}
}
// public static void main(String args[]) {
// JFrame frame = new JFrame("VSX Test");
// VSX parser = new VSX();
// JTree tree = new JTree(parser.parse("build.xml"));
// frame.getContentPane().add(new JScrollPane(tree));
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// frame.setSize(300, 400);
// frame.setVisible(true);
// }
}
package seh;
import java.lang.invoke.CallSite;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import org.junit.Test;
import seh.SEH.BaseSEH;
import seh.SEH.SEHEngine;
import seh.SEH.SEHParam;
import seh.Types.RetStat;
import seh.Types.RetStatSupplier;
import seh.Types.StateIndex;
public class TestSEH implements seh.SEH.BaseSEH
{
private Logger logger = Logger.getLogger(getClass().getName());
public class TestSEHParam implements seh.SEH.SEHParam
{
int intValue = 0;
String strVal = null;
@Override
public void SetParam(String name, String value)
{
if (name.equalsIgnoreCase("TO") ||
name.equalsIgnoreCase("TDCause"))
intValue = Integer.parseInt(value);
else if (name.equalsIgnoreCase("Party") ||
name.equalsIgnoreCase("CallType"))
strVal = new String(value);
}
/**
* @return the intValue
*/
public int getIntValue()
{
return intValue;
}
/**
* @return the strVal
*/
public String getStrVal()
{
return strVal;
}
}
enum EnumEvents
{
Event1,
Event2,
Event3,
Event4
}
enum EnumSwitchValues
{
Val1,
Val2,
Val3
}
RetStat retStat = new RetStat();
@Override
public RetStatSupplier invokeFunc(CallSite callSite, SEHParam sehParam) throws Throwable {
// TODO Auto-generated method stub
return (RetStatSupplier) callSite.getTarget().invokeExact(this, sehParam);
}
@Override
public Method resolveAction(String actionName)
{
try
{
return TestSEH.class.getMethod(actionName,SEHParam.class);
} catch (SecurityException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (NoSuchMethodException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
@Override
public int resolveEvent(String eventName)
{
try
{
return EnumEvents.valueOf(eventName).ordinal();
}
catch (Exception e)
{
return EnumEvents.values().length; // max events
}
}
@Override
public String resolveEventName(int event)
{
try
{
return EnumEvents.values()[event].toString();
}
catch (Exception e)
{
return null;
}
}
@Override
public SEHParam getNewSEHParam()
{
return new TestSEHParam();
}
/*
* ACTIONS
*/
public RetStat SimpleAction(SEHParam param)
{
retStat.setSuccess();
logger.info("Action: \tSimpleAction");
return retStat;
}
public RetStat ActionWithParamInt(SEHParam param)
{
retStat.setSuccess();
logger.info("Action: \tActionWithParamInt: " + String.valueOf(((TestSEHParam)param).intValue));
return retStat;
}
public RetStat ActionWithParamString(SEHParam param)
{
retStat.setSuccess();
logger.info("Action: \tActionWithParamString: " + ((TestSEHParam)param).getStrVal());
return retStat;
}
public RetStat IfElseAction(SEHParam param)
{
retStat.setFail();
logger.info("Action: \tIfElseAction");
return retStat;
}
public RetStat SwitchAction(SEHParam param)
{
retStat.setSuccess();
logger.info("Action: \tSwitchAction");
retStat.retCode = EnumSwitchValues.Val1.ordinal();
return retStat;
}
@Test
public void TestEngine()
{
RetStat stat = new RetStat();
SEHEngine fsmEngine = new SEHEngine();
StateIndex state = new StateIndex();
EnumEvents event = EnumEvents.Event1;
try
{
Map<String,String> flowFilesMap = new HashMap<String, String>();
Map<String,BaseSEH> flowsSEHMap = new HashMap<String, BaseSEH>();
//flowFilesMap.put("Test", "src/test/resources/seh/TestSEH.xml");
flowFilesMap.put("Test", "src/test/resources/seh/TestSEH.json");
flowsSEHMap.put("Test", this);
stat = fsmEngine.InitMultiFlows(flowFilesMap, flowsSEHMap);
state.uiStateFlowIndex = 0;
state.uiStateIndex = 0; // idle
fsmEngine.HandleEvent(state, event.toString());
event = EnumEvents.Event3;
fsmEngine.HandleEvent(state, event.toString());
event = EnumEvents.Event2;
fsmEngine.HandleEvent(state, event.toString());
event = EnumEvents.Event2;
fsmEngine.HandleEvent(state, event.toString());
event = EnumEvents.Event4;
fsmEngine.HandleEvent(state, event.toString());
} catch (SecurityException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void setAppData(Object appData)
{
// TODO Auto-generated method stub
}
}
{
"States": [{
"Name": "Idle",
"Events": [{
"Name": "Event1",
"Actions": [{
"Type": "Action",
"Name": "SimpleAction"
}, {
"Type": "Action",
"Name": "ActionWithParamInt",
"Params": {
"TO": "20"
}
}, {
"Type": "Action",
"Name": "ActionWithParamString",
"Params": {
"CallType": "IncOut"
}
}],
"NextState": "Idle_NestState1"
}, {
"Name": "Event2",
"Actions": [{
"Type": "If",
"Name": "IfElseAction",
"Then": {
"Actions": [{
"Type": "Action",
"Name": "SimpleAction"
}, {
"Type": "Action",
"Name": "ActionWithParamInt",
"Params": {
"TO": "30000"
}
}, {
"Type": "Action",
"Name": "SimpleAction"
}],
"NextState": "Idle_IfThenState"
},
"Else": {
"Actions": [{
"Type": "Action",
"Name": "ActionWithParamInt",
"Params": {
"TO": "5000"
}
}, {
"Type": "Action",
"Name": "SimpleAction"
}, {
"Type": "Action",
"Name": "SimpleAction"
}],
"NextState": "Idle_IfElseState"
}
}],
"NextState": "Idle_IfElseDefaultState"
}]
}, {
"Name": "Idle_NestState1",
"Events": [{
"Name": "Event3",
"Actions": [{
"Type": "Action",
"Name": "SimpleAction"
}, {
"Type": "Action",
"Name": "SimpleAction"
}, {
"Type": "Switch",
"Name": "SwitchAction",
"Cases": [{
"Val1": {
"Actions": [{
"Type": "Action",
"Name": "SimpleAction"
}, {
"Type": "Action",
"Name": "ActionWithParamInt",
"Params": {
"TDCause": "8"
}
}, {
"Type": "Action",
"Name": "ActionWithParamString",
"Params": {
"Party": "ICP"
}
}],
"NextState": "Idle"
}
}, {
"Val2": {
"Actions": [{
"Type": "Action",
"Name": "ActionWithParamInt",
"Params": {
"TDCause": "8"
}
}, {
"Type": "Action",
"Name": "ActionWithParamString",
"Params": {
"Party": "ICP"
}
}],
"NextState": "Idle_NestState1"
}
}]
}],
"NextState": "Idle_SwitchDefaultState"
}]
}, {
"Name": "Idle_IfThenState",
"Events": [{
"Name": "Event4",
"Actions": [
{
"Type": "Action",
"Name": "SimpleAction"
}
],
"NextState": "Idle"
}]
}, {
"Name": "Idle_IfElseState",
"Events": [{
"Name": "Event4",
"Actions": [
{
"Type": "Action",
"Name": "SimpleAction"
}
],
"NextState": "Idle"
}]
}, {
"Name": "Idle_IfElseDefaultState",
"Events": [{
"Name": "Event4",
"Actions": [
{
"Type": "Action",
"Name": "SimpleAction"
}
],
"NextState": "Idle"
}]
}, {
"Name": "Idle_SwitchDefaultState",
"Events": [{
"Name": "Event4",
"Actions": [
{
"Type": "Action",
"Name": "SimpleAction"
}
],
"NextState": "Idle"
}]
}]
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<State>
<Name>Idle</Name>
<Event>
<Name>Event1</Name>
<Actions>
<Action>SimpleAction</Action>
<Action>
<Name>ActionWithParamInt</Name>
<Params>
<TO>20</TO>
</Params>
</Action>
<Action>
<Name>ActionWithParamString</Name>
<Params>
<CallType>IncOut</CallType>
</Params>
</Action>
</Actions>
<NextState>Idle_NestState1</NextState>
</Event>
<Event>
<Name>Event2</Name>
<Actions>
<If>
<Action>IfElseAction</Action>
<Then>
<Actions>
<Action>SimpleAction</Action>
<Action>
<Name>ActionWithParamInt</Name>
<Params>
<TO>30000</TO>
</Params>
</Action>
<Action>SimpleAction</Action>
</Actions>
<NextState>Idle_IfThenState</NextState>
</Then>
<Else>
<Actions>
<Action>
<Name>ActionWithParamInt</Name>
<Params>
<TO>5000</TO>
</Params>
</Action>
<Action>SimpleAction</Action>
<Action>SimpleAction</Action>
</Actions>
<NextState>Idle_IfElseState</NextState>
</Else>
</If>
</Actions>
<NextState>Idle_IfElseDefaultState</NextState>
</Event>
</State>
<State>
<Name>Idle_NestState1</Name>
<Event>
<Name>Event3</Name>
<Actions>
<Action>SimpleAction</Action>
<Action>SimpleAction</Action>
<Switch>
<Action>SwitchAction</Action>
<Case>
<Value>Val1</Value>
<Actions>
<Action>SimpleAction</Action>
<Action>
<Name>ActionWithParamInt</Name>
<Params>
<TDCause>8</TDCause><!-- SetUp TO -->
</Params>
</Action>
<Action>
<Name>ActionWithParamString</Name>
<Params>
<Party>ICP</Party>
</Params>
</Action>
</Actions>
<NextState>Idle</NextState>
</Case>
<Case>
<Value>Val2</Value>
<Actions>
<Action>
<Name>ActionWithParamInt</Name>
<Params>
<TDCause>8</TDCause><!-- SetUp TO -->
</Params>
</Action>
<Action>
<Name>ActionWithParamString</Name>
<Params>
<Party>ICP</Party>
</Params>
</Action>
</Actions>
<NextState>Idle_NestState1</NextState>
</Case>
<Case>
<Value>Val3</Value>
<Actions>
<Action>SimpleAction</Action>
</Actions>
<NextState>Idle_SwitchDefaultState</NextState>
</Case>
</Switch>
</Actions>
<NextState>Idle_SwitchDefaultState</NextState>
</Event>
</State>
<State>
<Name>Idle_IfThenState</Name>
<Event>
<Name>Event4</Name>
<Actions>
<Action>SimpleAction</Action>
</Actions>
<NextState>Idle</NextState>
</Event>
</State>
<State>
<Name>Idle_IfElseState</Name>
<Event>
<Name>Event4</Name>
<Actions>
<Action>SimpleAction</Action>
</Actions>
<NextState>Idle</NextState>
</Event>
</State>
<State>
<Name>Idle_IfElseDefaultState</Name>
<Event>
<Name>Event4</Name>
<Actions>
<Action>SimpleAction</Action>
</Actions>
<NextState>Idle</NextState>
</Event>
</State>
<State>
<Name>Idle_SwitchDefaultState</Name>
<Event>
<Name>Event4</Name>
<Actions>
<Action>SimpleAction</Action>
</Actions>
<NextState>Idle</NextState>
</Event>
</State>
</xml>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment