我正在通过 JUNG 库来强制接口,以便在节点之间移动agents
(即JUNG节点)。
当我命令agent
从节点 1 移动到节点 2 时,以及代理人前往节点 2之前已完成我命令agent
移至节点 1 。
我希望agent
在到达节点 2 后转移到节点 1 但是代理获取slowed down
(因为新命令减少了它的速度)当达到节点 2 时,它会以相同的降低速度返回到节点1。
当有一个Third
节点时,代理被命令移动到(当它在旅行表单节点 1 到节点 2 时)代理looses its path
到节点 2 ,并且无法访问任何节点 2 或 3 。
我知道发生这种情况是因为当某些thread
为moving the agent
时,执行新命令的另一个线程应以某种方式变为paused
并且在另一个thread
完成其工作之后它应该是resumed
。
我试过通过睡觉线程来做这样的事情,但它确实无效。
我做错了什么?
以下是我的代码(The main part for moving the agents is the MOVE class)
的完整实现:
Move.java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package interpreter.command;
import GraphHandling.BehGraphUndirected;
import edu.uci.ics.jung.algorithms.layout.AbstractLayout;
import edu.uci.ics.jung.algorithms.util.IterativeProcess;
import edu.uci.ics.jung.visualization.VisualizationImageServer;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.algorithms.layout.ISOMLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.visualization.util.Animator;
import interpreter.Command;
import interpreter.CommandMaster;
import interpreter.InterpretMaster;
import java.awt.geom.Point2D;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Administrator
*/
public class Move extends Command{
private CommandMaster cm;
private BehGraphUndirected behGraph;
private static String lastAgent = "";
@Override
public Object run(BehGraphUndirected behGraph, VisualizationImageServer panel, InterpretMaster interpretMaster,LinkedList<String> nodeList,LinkedList<InterpretMaster.nodeAttribute> nodeAttributes,AbstractLayout <String, String> layout, String... args) {
System.out.print("move Runs\n");
this.cm = new CommandMaster();
this.behGraph = behGraph;
//AbstractLayout <String, String> moveLayout;
if(cm.exists(args[0]))
{
//got to another command
}
else
{
switch (args[0])
{
case "agent":
int size=nodeAttributes.size();
int i;
for(i=0;i<size;i++)
{
if(args[1].equals(nodeAttributes.get(i).nodeName))
{
if(nodeAttributes.get(i).isMoving)
{
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Logger.getLogger(Move.class.getName()).log(Level.SEVERE, null, ex);
}
}
VertexCollider vtxCol = new VertexCollider(layout, panel,args[1], args[2] , args[1] , nodeAttributes.get(i));
vtxCol.setMaximumIterations(1000);
vtxCol.setDesiredPrecision(1);
vtxCol.initialize();
Animator animator = new Animator(vtxCol);
animator.start();
nodeAttributes.get(i).isMoving = true;
break;
}
}
break;
}
}
interpretMaster.repaint();
return null;
}
class VertexCollider extends IterativeProcess
{
private String COLLIDER;
private AbstractLayout<String, String> layout;
private VisualizationImageServer<String, String> vv;
private Point2D startLocation;
private Point2D endLocation;
private Double moveX;
private Double moveY;
private InterpretMaster.nodeAttribute agentAttributes;
public VertexCollider(AbstractLayout<String, String> layout, VisualizationImageServer <String, String> vv, String vertexA, String vertexB , String collider , InterpretMaster.nodeAttribute nodeAttributes) {
this.layout = layout;
this.vv = vv;
startLocation = layout.transform(vertexA);
endLocation = layout.transform(vertexB);
COLLIDER = collider;
agentAttributes = nodeAttributes;
}
public void initialize() {
setPrecision(Double.MAX_VALUE);
//layout.setLocation(COLLIDER, startLocation);
moveX = (endLocation.getX() - startLocation.getX()) / getMaximumIterations();
moveY = (endLocation.getY() - startLocation.getY()) / getMaximumIterations();
}
@Override
public void step() {
layout.setLocation(COLLIDER, layout.getX(COLLIDER) + moveX, layout.getY(COLLIDER) + moveY);
vv.repaint();
setPrecision(Math.max(Math.abs(endLocation.getX() - layout.transform(COLLIDER).getX()),
Math.abs(endLocation.getY() - layout.transform(COLLIDER).getY())));
if (hasConverged()){
//layout.getGraph().removeVertex(COLLIDER);
agentAttributes.isMoving = false;
System.out.println("reached");
}
}
}
}