我正在通过JUNG库实现一个接口,用于在agents
节点之间移动(即 JUNG 节点)。
当我命令agent
从节点1移动到节点2时,并且在代理到节点2的行程完成之前,我命令agent
移动到节点1。
我希望在到达节点2agent
后移动到节点1 ,但是代理得到(因为新命令降低了它的速度),当到达节点2的速度降低时,它以相同的降低速度返回节点 1。slowed down
并且当有一个Third
节点 代理被命令移动到(当它在从节点1到节点2的行程中时)代理looses its path
到节点2并且没有到达任何节点2或3。
我知道会发生这种情况,因为当某个thread
正在moving the agent
执行新命令的另一个线程应该以某种方式变为paused
并且在另一个thread
完成其工作之后它应该是resumed
。
我试过通过休眠线程来做这样的事情,但它不起作用。
似乎即使当我让线程休眠时,顶点对撞机也会不断改变节点位置。我也尝试过使用,semaphores
但同样的事情发生了。
我怎样才能完全阻止它?
这是我的代码的完整实现(The main part for moving the agents is the MOVE class)
:
移动.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");
}
}
}
}