fsmbehaviour.java

来自「java实现的P2P多agent中间件」· Java 代码 · 共 688 行 · 第 1/2 页

JAVA
688
字号
	protected void scheduleNext(boolean currentDone, int currentResult) {
		if (currentDone) {
			try {
				previousName = currentName;
				if (transitionForced) {
					currentName = forcedTransitionDest;
					transitionForced = false;
				}
				else {
					// Normal case: use the TransitionTable to select the next state
					Transition t = theTransitionTable.getTransition(currentName, currentResult);
					resetStates(t.toBeReset);
					currentName = t.dest;
				}
				current = getState(currentName);
				if (current == null) {
					throw new NullPointerException();
				}
				else {
					handleStateEntered(current);
				}
			}
			catch (NullPointerException npe) {
				current = null;
				handleInconsistentFSM(previousName, currentResult);
			}
			// DEBUG
			//System.out.println(myAgent.getLocalName()+ " is Executing state "+currentName);
		}
	}
	
	protected void handleInconsistentFSM(String current, int event) {
		throw new RuntimeException("Inconsistent FSM. State: "+current+" event: "+event);
	}
	
	protected void handleStateEntered(Behaviour state) {
	}
	
	/**
	 Check whether this <code>FSMBehaviour</code> must terminate.
	 @return true when the last child has terminated and it 
	 represents a final state. false otherwise
	 @see jade.core.behaviours.CompositeBehaviour#checkTermination
	 */
	protected boolean checkTermination(boolean currentDone, int currentResult) {
		boolean ret = false;
		if (currentDone) {
			lastExitValue = currentResult;
			ret = lastStates.contains(currentName);
		}
		//#J2ME_EXCLUDE_BEGIN
		if (myLogger.isLoggable(Logger.FINE)) {
			myLogger.log(Logger.FINE, "FSM-Behaviour "+getBehaviourName()+": checkTermination() returning "+ret);
		}
		//#J2ME_EXCLUDE_END
		return ret;
	}  		
	
	/** 
	 Get the current child
	 @see jade.core.behaviours.CompositeBehaviour#getCurrent
	 */
	protected Behaviour getCurrent() {
		return current;
	}
	
	/**
	 Return a Collection view of the children of 
	 this <code>SequentialBehaviour</code> 
	 @see jade.core.behaviours.CompositeBehaviour#getChildren
	 */
	public Collection getChildren() {
		return states.values();
	}
	
	/**
	 Temporarily disregards the FSM structure, and jumps to the given
	 state. This method acts as a sort of <code>GOTO</code> statement
	 between states, and replaces the currently active state without
	 considering the trigger event or whether a transition was
	 registered. It should be used only to handle exceptional
	 conditions, if default transitions are not effective enough.
	 
	 @param next The name of the state to jump to at the next FSM
	 cheduling quantum. If the FSM has no state with the given name,
	 this method does nothing.
	 */
	protected void forceTransitionTo(String next) {
		// Just check that the forced transition leads into a valid state
		Behaviour b = getState(next);
		if (b != null) {
			transitionForced = true;
			forcedTransitionDest = next;
		}
	}
	
	/** 
	 Get the previously executed child
	 @see jade.core.behaviours.CompositeBehaviour#getCurrent
	 */
	protected Behaviour getPrevious() {
		return getState(previousName);
	}
	
	/**
	 Put this FSMBehaviour back in the initial condition.
	 */ 
	public void reset() {
		super.reset();
		transitionForced = false;
		forcedTransitionDest = null;
	}
	
	/**
	 Reset the children behaviours registered in the states indicated in
	 the <code>states</code> parameter.
	 @param states the names of the states that have to be reset
	 */
	public void resetStates(String[] states) {	
		if (states != null) {
			for(int i=0; i < states.length; i++){
				Behaviour b = getState(states[i]);
				b.reset();
			}
		}
	}
	
	
	/** 
	 * Inner class implementing the FSM transition table
	 */
	class TransitionTable implements Serializable {
		private Hashtable transitions = new Hashtable();
		private static final long serialVersionUID = 3487495895819003L;
		
		void clear() {
			transitions.clear();
		}
		
		void addTransition(Transition t) {
			String key1 = t.getFromState();
			
			TransitionsFromState tfs = (TransitionsFromState) transitions.get(key1);
			
			if (tfs == null) {
				tfs = new TransitionsFromState();
				transitions.put(key1, tfs);
			}
			
			if (t.isDefault()) {
				tfs.setDefaultTransition(t);
			}
			else {
				Integer key2 = new Integer(t.getTrigger());
				tfs.put(key2, t);
			}
		}
		
		void removeTransition(String s1, int event) {
			TransitionsFromState tfs = (TransitionsFromState)transitions.get(s1);
			if(tfs != null) {
				Transition t = (Transition)tfs.remove(new Integer(event));
				if(t != null) {
					
					if((tfs.isEmpty() && (tfs.getDefaultTransition() == null))) {
						transitions.remove(s1);
					}
				}
			}
		}
		
		void removeTransition(String s1) {
			TransitionsFromState tfs = (TransitionsFromState)transitions.get(s1);
			if(tfs != null) {
				tfs.setDefaultTransition(null);
				
				if (tfs.isEmpty()) {
					transitions.remove(s1);
				}
			}
		}
		
		Transition getTransition(String s, int event) {
			TransitionsFromState tfs = (TransitionsFromState) transitions.get(s);
			if(tfs != null) {
				Transition t = (Transition) tfs.get(new Integer(event));
				return t;
			}
			else {
				return null;
			}
		}
		
		Transition getTransition(String s) {
			TransitionsFromState tfs = (TransitionsFromState) transitions.get(s);
			if(tfs != null) {
				return tfs.getDefaultTransition();
			}
			else {
				return null;
			}
		}
		
		void removeTransitionsFromState(String stateName) {
			transitions.remove(stateName);
		}
	}
	
	
	/**
	 * Inner class Transition
	 */
	static class Transition implements Serializable {
		
		private FSMBehaviour fsm;
		private String src;
		private String dest;
		private int trigger;
		private boolean def;
		private String[] toBeReset;
		private static final long     serialVersionUID = 3487495895819004L;
		
		public Transition() {
		}
		
		public Transition(FSMBehaviour f, String s, String d, int t, String[] rs) {
			fsm = f;
			src = s;
			dest = d;
			trigger = t;
			def = false;
			toBeReset = rs;
		}
		
		public Transition(FSMBehaviour f, String s, String d, String[] rs) {
			fsm = f;
			src = s;
			dest = d;
			trigger = 0;
			def = true;
			toBeReset = rs;
		}
		
		public FSMBehaviour getFSM() {
			return fsm;
		}
		
		public void setFSM(FSMBehaviour f) {
			fsm = f;
		}
		
		public String getFromState() {
			return src;
		}
		
		public void setFromState(String f) {
			src = f;
		}
		
		public String getToState() {
			return dest;
		}
		
		public void setToState(String t) {
			dest = t;
		}
		
		public int getTrigger() {
			return trigger;
		}
		
		public void setTrigger(int t) {
			trigger = t;
		}
		
		public boolean isDefault() {
			return def;
		}
		
		public void setDefault(boolean d) {
			def = d;
		}
		
		public String[] getStatesToReset() {
			return toBeReset;
		}
		
		public void setStatesToReset(String[] ss) {
			toBeReset = ss;
		}	
		
		//#MIDP_EXCLUDE_BEGIN
		public String toString() {
			StringBuffer sb = new StringBuffer();
			sb.append("(TRANSITION trigger=").append(trigger).append(", source=").append(src).append(", dest=").append(dest).append(")");
			return sb.toString();
		}
		//#MIDP_EXCLUDE_END
	} // END of inner class Transition
	
	
	/**
	 * Inner class TransitionsFromState
	 */
	class TransitionsFromState extends Hashtable {
		private Transition defaultTransition = null;
		private static final long     serialVersionUID = 3487495895819005L;
		
		void setDefaultTransition(Transition dt) {
			defaultTransition = dt;
		}
		
		Transition getDefaultTransition() {
			return defaultTransition;
		}
		
		public Object get(Object key) {
			Transition t = (Transition) super.get(key);
			if (t == null) {
				t = defaultTransition;
			}
			return t;
		}
		
		//#MIDP_EXCLUDE_BEGIN
		public String toString() {
			StringBuffer sb = new StringBuffer();
			sb.append("Transitions: ");
			sb.append(super.toString());
			if (defaultTransition != null) {
				sb.append(" defaultTransition: "+defaultTransition);
			}
			return sb.toString();
		}
		//#MIDP_EXCLUDE_END
	} // END of inner class TransitionsFromState
	
	//#MIDP_EXCLUDE_BEGIN
	public String stringifyTransitionTable() {
		return theTransitionTable.transitions.toString();
	}
	//#MIDP_EXCLUDE_END
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?