⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loop.java

📁 移动Agent编程工具Naplet
💻 JAVA
字号:

/*
 * @<#>Loop.java version 0.0.1, 1/1/2000
 *
 * THIS PROGRAM IS FREE SOFTWARE; YOU CAN DISTRIBUTED IT AND/OR
 * MODIFY IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE 
 * AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.
 *
 * THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
 * BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE 
 * GNU GENERAL PUBLIC LICENSE FOR MORE DETAILS.
 *
 * Copyright (c) 2000 Wayne State University. All Rights Reserved.
 */
package naplet.itinerary;

import naplet.*;

/**
 * <code>Loop</code> class defines an repetitive 
 * itinerary, along which naplet is passed. 
 *
 * @version 0.0.1, 1/1/2000
 * @author C. Xu
 */

public class Loop implements ItineraryPattern {

	private Checkable guard;
	private ItineraryPattern itin;

	/**
	 * In theory, loop can be a repetition of any itinerary, including
     	 * ParPattern and followed by a post-action as well.
	 * In practice, we restrict it to be loop over SeqPattern or Singleton
	 * Any desired post-action can be integrated into the iterative 
	 * sequence.
	 */
   
	public Loop(Checkable guard, ItineraryPattern itin) 
			throws InvalidItineraryException {
		if (itin instanceof SingletonPattern || itin instanceof SeqPattern) {
			this.guard = guard;
			this.itin = itin;
		} else {
			throw new InvalidItineraryException("Loop is restricted to repetition of SeqPattern or SingletonPattern");
		}
	}

	public URN first() {
		return itin.first();
	}

	public void reset(){};

	public String toString() 
	{
		StringBuffer out = new StringBuffer();
		out.append( "LOOP(" + itin.toString() + ")" );
		return out.toString();
	}

	public synchronized Object clone() {
		try {
	
			Loop lp = (Loop)super.clone();
			lp.guard =  guard;
			lp.itin = (ItineraryPattern)itin.clone();

			return lp;

		} catch (CloneNotSupportedException e){
			throw new NapletInternalError("Loop itinerary clone error!");
		} 
	}


	public void go( Naplet nap )
		throws java.rmi.RemoteException, UnableDispatchException {

		// Conditional visit of the sequence	

		if (guard==null || guard.check(nap) ) {	

			nap.getItinerary().pushRoute( this ); 
			nap.getItinerary().setRoute( itin );
			itin.go(nap);
			

		} else  { 		// skip the sequence
			ItineraryPattern itin = nap.getItinerary().popRoute();
			if (itin==null) {
				System.out.println("Loop itinerary exit");
			} else {		
				nap.getItinerary().setRoute( itin );
				itin.go(nap);
			}
		}	
	}  

}
		

⌨️ 快捷键说明

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