⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eventrouting.java

📁 JAVA版的蚂蚁算法(Ant Colony Optimization Algorithms)
💻 JAVA
字号:
package dk.itu.nulx30.eventSimulation;

import dk.itu.nulx30.eventSimulation.exceptions.IllegalTimeException;

import dk.itu.nulx30.networkModel.NetworkPackage;
import dk.itu.nulx30.networkModel.Router;
import dk.itu.nulx30.networkModel.RouterQueue;
import dk.itu.nulx30.networkModel.Wire;

import dk.itu.nulx30.networkModel.exceptions.PackageTTLException;
import dk.itu.nulx30.networkModel.exceptions.RouterNotFoundException;
import dk.itu.nulx30.networkModel.exceptions.RouterQueueEmptyException;
import dk.itu.nulx30.networkModel.exceptions.RouterQueueFullException;
import dk.itu.nulx30.networkModel.exceptions.NoPossibleRoutingException;

import dk.itu.nulx30.util.BinaryHeap;

/**
 * This class represents the event, that a package has been removed from an
 * input queue and is about to be "routed" to a correct output queue. When this
 * event occurs the package is removed from the the input queue of the router
 * and the router then calculates the next router and with that the next
 * output queue for the package. An <code>EventStartSending</code> event
 * is put into the "global" <code>eventHolder</code> to simulate this.
 *
 * @author Jacob Wahl Winther
 * @author Mikkel Bundgaard
 * @author Troels C. Damgaard
 * @author Federico Decara
 *
 * @see EventStartSending
 */
public class EventRouting extends Event {
  /** this router contains the output queue, which receives the package */
  private Router router = null;
  /** the package to add to the output queue of the router*/  
  private NetworkPackage aPackage = null;
  /** binary heap, which handles all the events in the whole system*/
  private BinaryHeap eventHolder = null;
  /** the simulation which in this event has occurred*/
  private EventSimulation simulation;

  /**
   * Class constructor for <code>EventRouting</code> which takes a time, a
   * <code>Router</code>, a <code>NetworkPackage</code> and a
   * <code>BinaryHeap</code> as arguments.
   *
   * @param time the time when this event is about to occur
   * @param router the router, which contains the output queue, which receives
   * the package given as argument
   * @param pack the networkpackage to add to the output queue of the router
   * given as argument
   * @param bh the "global" binary heap, which handles all the events in the
   * system
   * @param sim the simulation in which this event has occurred
   */
  public EventRouting( double time, Router router, NetworkPackage pack,
                                          BinaryHeap bh, EventSimulation sim ) {
    super( time );
    this.router = router;
    aPackage = pack;
    eventHolder = bh;
    simulation = sim;
  }

  /**
   * This method is called when this event occurs.
   */
  public void doEvent() {
    aPackage.setTime( getTime() ); // Update the timecounter in ANT (package)
    EventStartSending newE;
    RouterQueue outputQueue;
    Wire outputWire;
    Router nextR = null;

    try {
      // Take element out of the input queue
      router.removePackageFromInputQueue();

      // If the package has reached the destination
      if ( aPackage.getDestination().equals( router ) ) {
        simulation.incrementAccumulatedPackageTime(
                                                aPackage.getTimeInNetwork(), false );
        aPackage.addRouterToPathList( null, router );
        simulation.getRoutingAlgorithm().packageArrived( aPackage );
      }
      else {
        // Find the next router on the path
        nextR = simulation.getRoutingAlgorithm().routePackage( router, aPackage );
        outputQueue = router.addPackageToOutputQueue( nextR, aPackage );
        outputWire = outputQueue.getWire();
        if ( getTime() >= outputWire.getNextEventTime() ) { // The wire is ready
          // The new Event can be processed without waiting
          newE = new EventStartSending( getTime(), outputQueue, aPackage,
                                                        eventHolder, simulation );
          // Increment next time event to be after current package is done being sent
          // Ensures that the next event on Wire cannot happen until current event
          // is complete
          outputWire.setNextEventTime( getTime() + outputWire.getDelay() );
        }
        else { // Wire is busy and new event must wait
          newE = new EventStartSending( outputWire.getNextEventTime(), outputQueue,
                                              aPackage, eventHolder, simulation );
          outputWire.setNextEventTime( outputWire.getNextEventTime() +
                                                          outputWire.getDelay() );
        }
        // Insert new event into the priority Queue
        eventHolder.add( newE );
      }
    }
    catch ( RouterQueueEmptyException RQEe ) {
      // ----------------------------------------
      // Hmm we are removing a package that is not there. This could be used
      // to catch events from dead routers. By setting the queues as empty
      // this event will occur whenever a package is being processed on
      // a "dead" router. But then we wouldn't know weather it was because
      // the router was dead or an actual error had occured!
      System.err.println( "The queue is empty therefore no package " +
                                                           "could be removed" );
      System.exit( 1 );      
    }
    catch ( RouterQueueFullException RQFe ) { // Package lost due to full queue 
      simulation.incOutputQueueDeaths( 1 );
      simulation.incrementLostPackages();
      simulation.incrementAccumulatedPackageTime(
                                                 aPackage.getTimeInNetwork(), true );
      simulation.getRoutingAlgorithm().packageLost( aPackage );
    }
    catch ( PackageTTLException TTLe ) {  // Package lost due to TTL
      simulation.incTTLDeaths( 1 );
      simulation.incrementLostPackages();
      simulation.incrementAccumulatedPackageTime(
                                                 aPackage.getTimeInNetwork(), true );
      simulation.getRoutingAlgorithm().packageLost( aPackage );
    }
    catch ( NoPossibleRoutingException NPRe ) { // Package lost due to NPR
      simulation.incNPRDeaths( 1 );
      simulation.incrementLostPackages();
      simulation.incrementAccumulatedPackageTime(
                                                 aPackage.getTimeInNetwork(), true );
      simulation.getRoutingAlgorithm().packageLost( aPackage );
    }
    catch ( IllegalTimeException ITe ) {
      ITe.printStackTrace();
    }
    catch ( RouterNotFoundException RNFe ) {
      RNFe.printStackTrace();
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -