Skip to content

Commit

Permalink
Fdariasm/#415 generalized csv reader 4ci (#443)
Browse files Browse the repository at this point in the history
* Copy ouput directory in parkingSpec to avoid ovewriting issue #396

* Merge branch 'master' into fdariasm/#396-parkingSpec

* Merge branch 'master' into fdariasm/#396-parkingSpec

* Generalized CSV reader #415. Remove CsvUtils.scala class

Comes from comments in PR #384:
 - https://reviewable.io/reviews/lbnl-ucb-sti/beam/384#-LJjLS_57OHlUOBHBBTY:-LJjLS_57OHlUOBHBBTZ:b-jim72m
 - https://reviewable.io/reviews/lbnl-ucb-sti/beam/384#-LJjLhxCAIkG4ITt7GBh:-LJjLhxCAIkG4ITt7GBi:b-b9estn

* Merge branch 'master' into fdariasm/#415-generalizedCsvReader-4ci

* Merge branch 'master' into fdariasm/#415-generalizedCsvReader-4ci
  • Loading branch information
fdariasm authored and colinsheppard committed Aug 28, 2018
1 parent c6addb0 commit d2481b3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import beam.agentsim.infrastructure.TAZTreeMap.TAZ
import beam.agentsim.infrastructure.ZonalParkingManager.ParkingAlternative
import beam.router.BeamRouter.Location
import beam.sim.{BeamServices, HasServices}
import beam.utils.CsvUtils
import beam.utils.{FileUtils}
import org.matsim.api.core.v01.Id
import org.supercsv.cellprocessor.ift.CellProcessor
import org.supercsv.io.{CsvMapReader, CsvMapWriter, ICsvMapReader, ICsvMapWriter}
Expand Down Expand Up @@ -321,11 +321,9 @@ class ZonalParkingManager(
def selectStallWithCharger(inquiry: ParkingInquiry, startRadius: Double): ParkingStall = ???

def readCsvFile(filePath: String): mutable.Map[StallAttributes, StallValues] = {
var mapReader: ICsvMapReader = null
val res: mutable.Map[StallAttributes, StallValues] = mutable.Map()
try {
mapReader =
new CsvMapReader(CsvUtils.readerFromFile(filePath), CsvPreference.STANDARD_PREFERENCE)

FileUtils.using(new CsvMapReader(FileUtils.readerFromFile(filePath), CsvPreference.STANDARD_PREFERENCE)) { mapReader =>
val header = mapReader.getHeader(true)
var line: java.util.Map[String, String] = mapReader.read(header: _*)
while (null != line) {
Expand All @@ -347,10 +345,6 @@ class ZonalParkingManager(

line = mapReader.read(header: _*)
}

} finally {
if (null != mapReader)
mapReader.close()
}
res
}
Expand Down
71 changes: 0 additions & 71 deletions src/main/scala/beam/utils/CsvUtils.scala

This file was deleted.

11 changes: 10 additions & 1 deletion src/main/scala/beam/utils/FileUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ object FileUtils extends LazyLogging {
}

def safeLines(fileLoc: String): stream.Stream[String] = {
using(CsvUtils.readerFromFile(fileLoc))(_.lines)
using(readerFromFile(fileLoc))(_.lines)
}

def readerFromFile(filePath: String): java.io.BufferedReader = {
IOUtils.getBufferedReader(filePath)
}

def downloadFile(source: String): Unit = {
Expand All @@ -97,4 +101,9 @@ object FileUtils extends LazyLogging {
assert(target != null)
copyURLToFile(new URL(source), Paths.get(target).toFile)
}

def getHash(concatParams: Any*): Int = {
val concatString = concatParams.foldLeft("")(_ + _)
concatString.hashCode
}
}
27 changes: 22 additions & 5 deletions src/test/scala/beam/integration/ParkingSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@ import beam.agentsim.events.{
ParkEventAttrs,
PathTraversalEvent
}
import java.io.File

import beam.agentsim.events.{LeavingParkingEventAttrs, ModeChoiceEvent, ParkEventAttrs, PathTraversalEvent}
import beam.sim.BeamHelper
import com.typesafe.config.ConfigValueFactory
import org.apache.commons.io.FileUtils
import org.matsim.api.core.v01.events.Event
import org.matsim.core.events.{EventsUtils, MatsimEventsReader}
import org.matsim.core.events.handler.BasicEventHandler
import org.scalatest.{BeforeAndAfterAll, Matchers, WordSpecLike}

import scala.collection.immutable.Queue
import scala.collection.mutable.ArrayBuffer

class ParkingSpec
extends WordSpecLike
Expand Down Expand Up @@ -71,19 +83,24 @@ class ParkingSpec
)
.resolve()

val matsimConfig = runBeamWithConfig(config)._1
val (matsimConfig, outputDirectory) = runBeamWithConfig(config)

val queueEvents = ArrayBuffer[Queue[Event]]()
for (i <- 0 until iterations) {
val filePath = getEventsFilePath(matsimConfig, "xml", i).getAbsolutePath
queueEvents.append(collectEvents(filePath))
}

val outputDirectoryFile = new File(outputDirectory)
FileUtils.copyDirectory(outputDirectoryFile, new File(s"${outputDirectory}_$parkingScenario"))

queueEvents
}

lazy val limitedEvents = runAndCollectForIterations("limited", 10)
lazy val defaultEvents = runAndCollectForIterations("default", 10)
lazy val expensiveEvents = runAndCollectForIterations("expensive", 10)
lazy val emptyEvents = runAndCollectForIterations("empty", 10)
val limitedEvents = runAndCollectForIterations("limited", 10)
val defaultEvents = runAndCollectForIterations("default", 10)
val expensiveEvents = runAndCollectForIterations("expensive", 10)
val emptyEvents = runAndCollectForIterations("empty", 10)

val filterForCarMode: Seq[Event] => Int = { events =>
events.count { e =>
Expand Down

0 comments on commit d2481b3

Please sign in to comment.