I am impelementing an interface by JUNG library for moving agents
(that are JUNG nodes) between nodes.
When I command the agent
to move from node 1 to node 2 , and before the agent's trip to node 2 has finished I command the agent
to move to node 1.
I want the agent
to move to node 1 after reaching node 2 But Instead the agent gets slowed down
( Because the new command decreases its speed) and when reaches node 2 by that decreased speed it returns to node 1 by the same decreased speed.
And when there is a Third
node That the agent is commanded to move to (when it is on the trip form node 1 to node 2) the agent looses its path
to node 2 and doesn't reach any of the nodes 2 or 3.
I know this happens because when some thread
is moving the agent
the other thread that is performing the new command should somehow become paused
and after the other thread
finishes its job it should be resumed
.
I have tried doing such thing by sleeping the thread but it does't work.
It seems even when I Sleep the thread the vertex collider keeps changing the node location.
I have Also tried using the semaphores
but the same thing happens.
How can I stop it completely?
Here is the complete implementation of my code (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");
}
}
}
}
来源:https://stackoverflow.com/questions/28003792/how-to-pause-jung-animator-given-an-iterativeprocess-by-using-thread-sleep