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

📄 modelswarm.java

📁 自己用swarm开发的一个车辆跟驰的模型
💻 JAVA
字号:
// ModelSwarm.java
// The top-level ModelSwarm.

import swarm.Globals;
import swarm.Selector;
import swarm.defobj.Zone;

import swarm.objectbase.Swarm;
import swarm.objectbase.SwarmImpl;

import swarm.activity.ActionGroupImpl;
import swarm.activity.ScheduleImpl;
import swarm.activity.Activity;


public class ModelSwarm extends SwarmImpl
{
    public Node from;
    public Road road;
    public Veh veh,veh1,veh2,veh3;
    public int endTime = 1000;

    
    ScheduleImpl modelSchedule;

    public ModelSwarm(Zone aZone)
    {
	super(aZone);
    }

   public Object buildObjects()
    {
	
  
	super.buildObjects();

	from=new Node(Globals.env.globalZone);
	from.X=from.Y=0;
	road=new Road(Globals.env.globalZone,from);
	veh=new Veh(Globals.env.globalZone,from,road,1);
	veh1=new Veh(Globals.env.globalZone,from,road,2);
	veh2=new Veh(Globals.env.globalZone,from,road,3);
	veh3=new Veh(Globals.env.globalZone,from,road,4);
	veh1.maxAcc=2;
	veh1.maxDcc=-2;
	veh2.maxAcc=12;
	veh2.maxDcc=-12;
	veh3.maxAcc=20;
	veh3.maxDcc=-20;
	
	



	return this;
    }

  public Object buildActions()
    {
    	
	Selector sel,sel1,sel2,sel3;
	ActionGroupImpl modelActions;

	super.buildActions();

	modelActions = new ActionGroupImpl(getZone());
	
	
	
	
		try
	    {
	    	
	    sel = new Selector(veh.getClass(),"follow",false);
	    sel1 = new Selector(veh1.getClass(),"follow",false);
	    sel2 = new Selector(veh2.getClass(),"follow",false);
	    sel3 = new Selector(veh3.getClass(),"follow",false);
	   
	    modelActions.createActionTo$message(veh, sel);
	    modelActions.createActionTo$message(veh1, sel1);
	    modelActions.createActionTo$message(veh2, sel2);
	    modelActions.createActionTo$message(veh3, sel3);
	     
	    } catch (Exception e)
		{
		System.err.println("Exception checkTime " + e.getMessage ());
		System.exit(1);
		}
System.out.println("di");
	
	try
	    {
	    
	    sel = new Selector(this.getClass(), "checkTime", false);
	   
	    modelActions.createActionTo$message(this, sel);
	    
	    } catch (Exception e)
		{
		System.err.println("Exception checkTime " + e.getMessage ());
		System.exit(1);
		}
		
		
		
		


	modelSchedule = new ScheduleImpl(getZone(), 1);

	modelSchedule.at$createAction(0, modelActions);

	return this;
    }

    public Activity activateIn(Swarm swarmContext)
    {
	super.activateIn(swarmContext);

	modelSchedule.activateIn(this);

	return getActivity();
    }

   public void checkTime()
    {
	int i, t;
	int interval = 5;
    
	if (Globals.env.getCurrentTime() >= endTime)
	    {
	    System.out.println("We've reached our endTime at period " + 
			       Globals.env.getCurrentTime());
	    Globals.env.getCurrentSwarmActivity().terminate();
	    }
	else
	    {
		for (i = 0; (t = (int)Math.pow(interval, (double)i)) 
			                               <= endTime; i++)
		    if (t == Globals.env.getCurrentTime())
			System.out.println("The time is " + t);
	    }

        return;
    }
}

⌨️ 快捷键说明

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