Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fdariasm/#415 generalized csv reader 4ci #443

Merged
merged 6 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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