From: yoriy <yo...@us...> - 2012-07-20 08:09:05
|
Update of /cvsroot/arianne/stendhal/src/games/stendhal/server/entity/npc/fsm In directory vz-cvs-4.sog:/tmp/cvs-serv28645/src/games/stendhal/server/entity/npc/fsm Modified Files: Engine.java Transition.java Log Message: adding base functionality of handling transitions in NPC's fsm tables. Index: Transition.java =================================================================== RCS file: /cvsroot/arianne/stendhal/src/games/stendhal/server/entity/npc/fsm/Transition.java,v retrieving revision 1.24 retrieving revision 1.25 diff -C2 -d -r1.24 -r1.25 *** Transition.java 1 May 2011 19:50:05 -0000 1.24 --- Transition.java 20 Jul 2012 08:09:03 -0000 1.25 *************** *** 53,56 **** --- 53,93 ---- /** The action that will take place when the transition is triggered. */ private final PostTransitionAction action; + + /** Label for search through installed transitions */ + private final String label; + + + /** + * Creates a new transition. + * + * @param currentState + * old state + * @param triggers + * input triggers + * @param condition + * additional precondition + * @param secondary + * flag to mark secondary transitions to be taken into account after preferred transitions + * @param nextState + * state after the transition + * @param reply + * output + * @param action + * additional action after the condition + * @param label + * string label to handle transitions in NPC's FSM table + */ + public Transition(final ConversationStates currentState, final Collection<Expression> triggers, + final PreTransitionCondition condition, final boolean secondary, final ConversationStates nextState, + final String reply, final PostTransitionAction action, final String label) { + this.state = currentState; + this.condition = condition; + this.secondary = secondary; + this.nextState = nextState; + this.triggers = triggers; + this.reply = reply; + this.action = action; + this.label = label; + } /** *************** *** 82,85 **** --- 119,140 ---- this.reply = reply; this.action = action; + this.label = ""; + } + + /** + * Create transition and copy values from existing transition + * + * @param tr - source transition, must not be null + * @throws + */ + public Transition(Transition tr) { + this.state = tr.state; + this.condition = tr.condition; + this.secondary = tr.secondary; + this.nextState = tr.nextState; + this.triggers = tr.triggers; + this.reply = tr.reply; + this.action = tr.action; + this.label = tr.label; } *************** *** 290,295 **** --- 345,374 ---- return false; } + + /** + * Checks for labels equality + * + * @param label - label to compare + * @return - check result + */ + public boolean checkLabel(final String label) { + if (this.label.equals(label)) { + return true; + } + return false; + } /** + * check if label is empty string + * @return - check result + */ + public boolean isEmptyLabel() { + if (this.label.equals("")) { + return true; + } + return false; + } + + /** * Checks whether this transition's condition is fulfilled. * Index: Engine.java =================================================================== RCS file: /cvsroot/arianne/stendhal/src/games/stendhal/server/entity/npc/fsm/Engine.java,v retrieving revision 1.66 retrieving revision 1.67 diff -C2 -d -r1.66 -r1.67 *** Engine.java 16 May 2011 14:18:53 -0000 1.66 --- Engine.java 20 Jul 2012 08:09:03 -0000 1.67 *************** *** 48,52 **** public Engine(final SpeakerNPC speakerNPC) { if (speakerNPC == null) { ! throw new IllegalArgumentException("speakerNpc must not be null"); } --- 48,52 ---- public Engine(final SpeakerNPC speakerNPC) { if (speakerNPC == null) { ! throw new IllegalArgumentException("speakerNPC must not be null"); } *************** *** 71,74 **** --- 71,92 ---- return null; } + + /** + * Looks for an already registered exactly matching transition. + * + * @param label - label to search + * @return - transition or null + */ + private Transition get(final String label) { + if (label.equals("")) { + return null; + } + for (final Transition transition : stateTransitionTable) { + if (transition.checkLabel(label)) { + return transition; + } + } + return null; + } /** *************** *** 98,101 **** --- 116,147 ---- } + + /** + * Adds a new transition to FSM. + * + * @param state + * old state + * @param triggerStrings + * input trigger + * @param condition + * additional precondition + * @param secondary + * flag to mark secondary transitions to be taken into account after preferred transitions + * @param nextState + * state after the transition + * @param reply + * output + * @param action + * additional action after the condition + */ + public void add(final ConversationStates state, final Collection<String> triggerStrings, final ChatCondition condition, + boolean secondary, final ConversationStates nextState, final String reply, final ChatAction action, + final String label) { + Collection<Expression> triggerExpressions = createUniqueTriggerExpressions( + state, triggerStrings, null, condition, reply, action); + + add(triggerExpressions, state, condition, secondary, nextState, reply, action, label); + } + /** * Adds a new set of transitions to the FSM. *************** *** 200,203 **** --- 246,276 ---- */ public void add(Collection<Expression> triggerExpressions, final ConversationStates state, final ChatCondition condition, + boolean secondary, final ConversationStates nextState, final String reply, final ChatAction action, final String label) { + if (triggerExpressions!=null && !triggerExpressions.isEmpty()) { + if(label.equals("") || (get(label)==null)) { + stateTransitionTable.add(new Transition(state, triggerExpressions, condition, secondary, nextState, reply, action, label)); + } + } + } + + /** + * Adds a new set of transitions to the FSM. + * @param triggerExpressions + * a list of trigger expressions for this transition, must not be null + * @param state + * the starting state of the FSM + * @param condition + * null or condition that has to return true for this transition + * to be considered + * @param secondary + * flag to mark secondary transitions to be taken into account after preferred transitions + * @param nextState + * the new state of the FSM + * @param reply + * a simple sentence reply (may be null for no reply) + * @param action + * a special action to be taken (may be null) + */ + public void add(Collection<Expression> triggerExpressions, final ConversationStates state, final ChatCondition condition, boolean secondary, final ConversationStates nextState, final String reply, final ChatAction action) { if (triggerExpressions!=null && !triggerExpressions.isEmpty()) { *************** *** 205,208 **** --- 278,295 ---- } } + + /** + * remove matches transition + * + * @param label + * @return + */ + public boolean remove(final String label) { + final Transition tr = get(label); + if (tr==null) { + return false; + } + return(stateTransitionTable.remove(tr)); + } /** *************** *** 490,492 **** --- 577,594 ---- } + /** + * Returns a copy of transition that match label + * @param label + * @return + */ + public Transition getTransition(final String label) { + Transition tr = get(label); + if(tr==null) { + return null; + } + return new Transition(tr); + } + + + } |