0

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.

What am I doing wrong?

Here is the complete implementation of my code (The main part for moving the agents is the MOVE class):

InterpretMaster.java

commandMaster.java

Command.java

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");


            }
        }
    }
}

Create.java

Freelancer
  • 836
  • 1
  • 14
  • 47
  • This is far too much code for this site. I have not looked at it. But from your problem statement, the node movement is **sequential**, and requests to move nodes should be processed **sequentially**. If this is the case, I would suggest a very simple producer/consumer pattern, with a single "node mover" thread that takes movement events from a queue. Any number of other threads deposit these events in the queue. Some sort of concurrent, FIFO, queue should work fine - consider a [`LinkedBlockingDeque`](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html). – Boris the Spider Jan 17 '15 at 12:54
  • @BoristheSpider I have added the complete implementation just in case it's needed for understanding the code. – Freelancer Jan 17 '15 at 12:56
  • 1
    No, that is not how this site works. You need to create an SSCCE that **illustrates your problem**. That we can analyse, and if necessary, run. Posting massive amounts of code is unhelpful, and will likely lead to downvotes for you. – Boris the Spider Jan 17 '15 at 12:57
  • @BoristheSpider I know what you are mentioning about the consumer and producer problem I have Implemented such code before but this code has different implementation and I don't know how to create and manage threads for it. – Freelancer Jan 17 '15 at 19:05
  • and also I don't want to limit the program in moving multiple agents at the same time. – Freelancer Jan 17 '15 at 19:15

0 Answers0