Search code examples
javaoopobjectsimulationtraffic-simulation

Referring to Objects by an ID? (Java)


i'm not a Java-Newbie but I can't get my head around a problem occured recently.

I have to simulate a road system in Java. For sake of proper OOP, I've got a class Car and a class Street (and several others to manage the whole simulation of course^^). I already managed to simulate a jam on one road and have had no problem doing so.

OK, here comes the Question: I want to extend my simulation from one lonely street to a road system. So i thought of a class called something like "RoadSystem" which might have an array of streets and some sort of connection (I thought of 'knots') allowing cars to know where they can drive as they reach the end of the street they are driving on.

The problem is that I have no idea how to implement these knots. The car has to be able to ask the street "hey bro, I'm at the end of you, where can I drive now?" and the street should somehow know which knot has a reference to it and ask it for the streets that are also connected to this particular knot. How do I do this reference? I thought of an ID but this might get extremely slow for bigger road systems if the street has to search through the street-IDs of every knot in order to find its own ID there. Or am I missing an obvious solution to my problem?

Every help highly appreciated!

Greetings from Germany,

Ruffy


Solution

  • You should have a Look at the SourceCode of LinkedList and maybe adapt this principle. A road has 2 connection points, while a intersection maybe 2 to 4?

    Abstract class RoadElement{
      //abstract for simulation purpose, maybe randomized
      //calculation of next direction, etc.
    }
    
    class Road extends RoadElement{
      private RoadElement previous = null;
      private RoadElement next = null;
    }
    
    class Intersection extends RoadElement{
        private RoadElement northernConnection = null;
        private RoadElement easternConnection = null;
        private RoadElement southernConnection = null;
        private RoadElement westernConnection = null;
    }
    

    Finally, you can design your Road-Network and linking up RoadElements as requried. During the simulation you don't have to care about concrete instaces, cause they will be connected logically.

    (You could later on improve this with additional RoadElements, such as "Curves" with limited velocity, person-crossings with stopping time etc.)

    Example:

       List<RoadElement> RoadMap = new LinkedList<RoadElement>();
       Road r1 = new Road();
       Intersection i1 = new Intersection();
       r1.setPrevious(i1);
       i1.setNorthernConnection(r1);
       ....
    

    Then, during the simulation, you just can do something like:

    Car currentCar = getCurrentCar();
    RoadElement re = currentCar.getLocation();
    if (re instanceof Road){
      //can we drive "forward and backward?"
      if ((Road)re).getPrevious() != null){
    
      }
    
      if ((Road)re).getNext() != null){
    
      }
    }else if (re instanceof Intersection){
       //check available outgoing roads
       if ((Intersection)re).getNorthernConnection() != null){
    
       }
       ...
    }