Ejemplo n.º 1
0
  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand() == locale.btOpen()) {
      final JFileChooser fc = new JFileChooser();
      fc.setFileFilter(
          new FileFilter() {

            @Override
            public String getDescription() {
              return "MATSim config file";
            }

            @Override
            public boolean accept(File f) {
              if (f.isDirectory()) {
                return true;
              }
              if (f.getName().endsWith("xml")) {
                return true;
              }
              return false;
            }
          });

    } else if (e.getActionCommand() == locale.btSave()) {
      if (this.roadClosures.size() > 0) {
        boolean saved = ConfigIO.saveRoadClosures(controller, roadClosures);
        // TODO add confirmation dialog?

      }
    }
  }
  @Test
  public void testScenarioGenerator() {

    ArrayList<Id<Link>> closedRoadIDs = new ArrayList<>();
    closedRoadIDs.add(Id.create(156, Link.class));
    closedRoadIDs.add(Id.create(316, Link.class));
    closedRoadIDs.add(Id.create(263, Link.class));

    String inputDir = getInputDirectory();
    String outputDir = getOutputDirectory();

    String gripsFileString = inputDir + "/grips_config.xml";
    String matsimConfigFileString = outputDir + "/config.xml";

    System.out.println("grips file:" + gripsFileString);
    System.out.println("matsim config file:" + matsimConfigFileString);

    //		File file = new File("oloberg.jpg");
    //		File file2 = new File("C:/HTW_Logo_rgb.jpg");
    //		File file3 = new File("/lol/HTW_Logo_rgb.jpg");
    //		File file4 = new
    // File("./test/input/org/matsim/contrib/grips/scenariogenerator/ScenarioGeneratorTest/testScenarioGenerator/lenzen.osm");
    //
    //		System.out.println("file1 absolute path:" + file.isAbsolute());
    //		System.out.println("file2 absolute path:" + file2.isAbsolute());
    //		System.out.println("file3 absolute path:" + file3.isAbsolute());
    //
    //		System.out.println(":" + file4.isAbsolute());
    //		System.out.println("file4 absolute path:" + file4.exists());

    //		System.exit(0);

    File gripsConfigFile = new File(gripsFileString);
    File matsimConfigFile;

    Controller controller = new Controller();
    GripsConfigModule gcm;
    Config mc;

    // check for files
    assertTrue("grips config file is missing", gripsConfigFile.exists());
    assertTrue(
        "evacuation area shape file is missing",
        (new File(inputDir + "/evacuation_area.shp")).exists());
    assertTrue(
        "population area shape file is missing", (new File(inputDir + "/population.shp")).exists());
    assertTrue("open street map file is missing", (new File(inputDir + "/lenzen.osm")).exists());
    assertTrue("could not open grips config.", controller.openGripsConfig(gripsConfigFile));

    gcm = controller.getGripsConfigModule();

    // generate and check matsim network/config
    boolean generateScenario = true;
    try {
      ScenarioGenerator scengen =
          new org.matsim.contrib.grips.scenariogenerator.ScenarioGenerator(gripsFileString);
      scengen.run();
    } catch (Exception e) {
      generateScenario = false;
      e.printStackTrace();
    }
    assertTrue("scenario was not generated", generateScenario);

    // check and open matsim scenario config file
    System.out.println("string:" + matsimConfigFileString);
    matsimConfigFile = new File(matsimConfigFileString);
    assertTrue("scenario config file is missing", matsimConfigFile.exists());
    assertTrue("could not open matsim config", controller.openMastimConfig(matsimConfigFile));

    // open matsim config, set first and last iteration
    mc = controller.getScenario().getConfig();
    mc.setParam("controler", "firstIteration", "0");
    mc.setParam("controler", "lastIteration", "10");
    new ConfigWriter(mc).write(matsimConfigFileString);

    // save road closures
    HashMap<Id<Link>, String> roadClosures = new HashMap<Id<Link>, String>();
    for (Id<Link> id : closedRoadIDs) roadClosures.put(id, "00:00");
    boolean saved = ConfigIO.saveRoadClosures(controller, roadClosures);
    assertTrue("could not save road closures", saved);

    // simulate and check scenario
    boolean simulateScenario = true;
    try {
      Controler matsimController = new Controler(mc);
      matsimController.setOverwriteFiles(true);
      matsimController.run();
    } catch (Exception e) {
      simulateScenario = false;
      e.printStackTrace();
    }
    assertTrue("scenario was not simulated", simulateScenario);

    // parse events, check if closed roads are not being visited
    LinkEnterEventHandler eventHandler = null;
    EventsManager e = EventsUtils.createEventsManager();
    EventsReaderXMLv1 reader = new EventsReaderXMLv1(e);
    Thread readerThread =
        new Thread(
            new EventReaderThread(reader, outputDir + "output/ITERS/it.10/10.events.xml.gz"),
            "readerthread");
    final ArrayList<Id<Link>> usedIDs = new ArrayList<Id<Link>>();
    eventHandler =
        new LinkEnterEventHandler() {
          @Override
          public void reset(int iteration) {}

          @Override
          public void handleEvent(LinkEnterEvent event) {
            if (usedIDs.contains(event.getLinkId())) usedIDs.add(event.getLinkId());
          }
        };
    e.addHandler(eventHandler);
    readerThread.run();

    for (Id<Link> id : closedRoadIDs)
      assertTrue("a closed road is crossed (id: " + id.toString() + ")", !usedIDs.contains(id));

    //		assertEquals("different config-files.", CRCChecksum.getCRCFromFile(inputDir +
    // "/config.xml"), CRCChecksum.getCRCFromFile(outputDir + "/config.xml"));
    assertEquals(
        "different network-files.",
        CRCChecksum.getCRCFromFile(inputDir + "/network.xml.gz"),
        CRCChecksum.getCRCFromFile(outputDir + "/network.xml.gz"));
    //		assertEquals("different plans-files.", CRCChecksum.getCRCFromFile(inputDir +
    // "/population.xml.gz"), CRCChecksum.getCRCFromFile(outputDir + "/population.xml.gz"));
  }