Skip to content

Commit

Permalink
File / Class renaming and splitting for the io directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ptheywood committed Jun 24, 2021
1 parent 0384124 commit bd59a48
Show file tree
Hide file tree
Showing 27 changed files with 325 additions and 279 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class RunPlan;
/**
* JSON format Logger
*/
class jsonLogger : public Logger{
class JSONLogger : public Logger{
public:
jsonLogger(const std::string &outPath, bool prettyPrint, bool truncateFile);
JSONLogger(const std::string &outPath, bool prettyPrint, bool truncateFile);
/**
* Log a runlog to file, using a RunPlan in place of config
* @throws May throw exceptions if logging to file failed for any reason
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#ifndef INCLUDE_FLAMEGPU_IO_JSONREADER_H_
#define INCLUDE_FLAMEGPU_IO_JSONREADER_H_
#ifndef INCLUDE_FLAMEGPU_IO_JSONSTATEREADER_H_
#define INCLUDE_FLAMEGPU_IO_JSONSTATEREADER_H_

#include <memory>
#include <string>
#include <unordered_map>
#include <utility>

#include "flamegpu/io/statereader.h"
#include "flamegpu/io/StateReader.h"
#include "flamegpu/model/ModelDescription.h"
#include "flamegpu/util/StringPair.h"

Expand All @@ -15,7 +15,7 @@ namespace flamegpu {
/**
* JSON format StateReader
*/
class jsonReader : public StateReader {
class JSONStateReader : public StateReader {
public:
/**
* Constructs a reader capable of reading model state from JSON files
Expand All @@ -28,7 +28,7 @@ class jsonReader : public StateReader {
* @param input_file Filename of the input file (This will be used to determine which reader to return)
* @param sim_instance Instance of the Simulation object (This is used for setting/getting config)
*/
jsonReader(
JSONStateReader(
const std::string &model_name,
const std::unordered_map<std::string, EnvironmentDescription::PropData> &env_desc,
std::unordered_map<std::pair<std::string, unsigned int>, util::Any> &env_init,
Expand All @@ -45,4 +45,4 @@ class jsonReader : public StateReader {

} // namespace flamegpu

#endif // INCLUDE_FLAMEGPU_IO_JSONREADER_H_
#endif // INCLUDE_FLAMEGPU_IO_JSONSTATEREADER_H_
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef INCLUDE_FLAMEGPU_IO_JSONWRITER_H_
#define INCLUDE_FLAMEGPU_IO_JSONWRITER_H_
#ifndef INCLUDE_FLAMEGPU_IO_JSONSTATEWRITER_H_
#define INCLUDE_FLAMEGPU_IO_JSONSTATEWRITER_H_

#include <memory>
#include <string>
#include <unordered_map>

#include "flamegpu/io/statewriter.h"
#include "flamegpu/io/StateWriter.h"
#include "flamegpu/model/ModelDescription.h"
#include "flamegpu/util/StringPair.h"

Expand All @@ -14,7 +14,7 @@ namespace flamegpu {
/**
* JSON format StateWriter
*/
class jsonWriter : public StateWriter {
class JSONStateWriter : public StateWriter {
public:
/**
* Returns a writer capable of writing model state to a JSON file
Expand All @@ -27,7 +27,7 @@ class jsonWriter : public StateWriter {
* @param output_file Filename of the input file (This will be used to determine which reader to return)
* @param sim_instance Instance of the Simulation object (This is used for setting/getting config)
*/
jsonWriter(
JSONStateWriter(
const std::string &model_name,
const unsigned int &sim_instance_id,
const util::StringPairUnorderedMap<std::shared_ptr<AgentVector>> &model_state,
Expand All @@ -53,4 +53,4 @@ class jsonWriter : public StateWriter {

} // namespace flamegpu

#endif // INCLUDE_FLAMEGPU_IO_JSONWRITER_H_
#endif // INCLUDE_FLAMEGPU_IO_JSONSTATEWRITER_H_
43 changes: 43 additions & 0 deletions include/flamegpu/io/LoggerFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef INCLUDE_FLAMEGPU_IO_LOGGERFACTORY_H_
#define INCLUDE_FLAMEGPU_IO_LOGGERFACTORY_H_

#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <algorithm>

#include "flamegpu/io/Logger.h"
#include "flamegpu/io/JSONLogger.h"
#include "flamegpu/io/XMLLogger.h"
#include "flamegpu/util/filesystem.h"

namespace flamegpu {

/**
* Factory for creating instances of flamegpu::Logger
*/
class LoggerFactory {
public:
/**
* @param output_path File for the log to be output to, this will be used to determine the logger type
* @param prettyPrint If false, the output data will be in a compact/minified format which may not be very readable
* @param truncateFile If true and output file already exists, it will be truncated
*/
static std::unique_ptr<Logger> createLogger(const std::string &output_path, bool prettyPrint, bool truncateFile = true) {
const std::string extension = util::filesystem::getFileExt(output_path);

if (extension == "xml") {
return std::make_unique<XMLLogger>(output_path, prettyPrint, truncateFile);
} else if (extension == "json") {
return std::make_unique<JSONLogger>(output_path, prettyPrint, truncateFile);
}
THROW UnsupportedFileType("File '%s' is not a type which can be written "
"by StateWriterFactory::createLogger().",
output_path.c_str());
}
};

} // namespace flamegpu

#endif // INCLUDE_FLAMEGPU_IO_LOGGERFACTORY_H_
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class AgentVector;

/**
* Abstract representation of a class for importing model data (agent population data, environment properties, run configuration) from file
* @see XMLReader The XML implementation of a StateReader
* @see JSONReader The JSON implementation of a StateReader
* @see XMLStateReader The XML implementation of a StateReader
* @see JSONStateReader The JSON implementation of a StateReader
*/
class StateReader {
public:
Expand Down
58 changes: 58 additions & 0 deletions include/flamegpu/io/StateReaderFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#ifndef INCLUDE_FLAMEGPU_IO_STATEREADERFACTORY_H_
#define INCLUDE_FLAMEGPU_IO_STATEREADERFACTORY_H_

#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <algorithm>

#include "flamegpu/io/StateReader.h"
#include "flamegpu/io/XMLStateReader.h"
#include "flamegpu/io/JSONStateReader.h"
#include "flamegpu/util/StringPair.h"
#include "flamegpu/util/filesystem.h"

namespace flamegpu {

class AgentVector;

/**
* Factory for creating instances of StateReader
*/
class StateReaderFactory {
public:
/**
* Returns a reader capable of reading 'input'
* Environment properties will be read into the Simulation instance pointed to by 'sim_instance_id'
* Agent data will be read into 'model_state'
* @param model_name Name from the model description hierarchy of the model to be loaded
* @param env_desc Environment description for validating property data on load
* @param env_init Dictionary of loaded values map:<{name, index}, value>
* @param model_state Map of AgentVector to load the agent data into per agent, key should be agent name
* @param input Filename of the input file (This will be used to determine which reader to return)
* @param sim_instance Instance of the Simulation object (This is used for setting/getting config)
* @throws UnsupportedFileType If the file extension does not match an appropriate reader
*/
static StateReader* createReader(
const std::string& model_name,
const std::unordered_map<std::string, EnvironmentDescription::PropData>& env_desc,
std::unordered_map<std::pair<std::string, unsigned int>, util::Any>& env_init,
util::StringPairUnorderedMap<std::shared_ptr<AgentVector>>& model_state,
const std::string& input,
Simulation* sim_instance) {
const std::string extension = util::filesystem::getFileExt(input);

if (extension == "xml") {
return new XMLStateReader(model_name, env_desc, env_init, model_state, input, sim_instance);
} else if (extension == "json") {
return new JSONStateReader(model_name, env_desc, env_init, model_state, input, sim_instance);
}
THROW UnsupportedFileType("File '%s' is not a type which can be read "
"by StateReaderFactory::createReader().",
input.c_str());
}
};
} // namespace flamegpu

#endif // INCLUDE_FLAMEGPU_IO_STATEREADERFACTORY_H_
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class AgentVector;

/**
* Abstract representation of a class for exporting model data (agent population data, environment properties, run configuration) to file
* @see XMLWriter The XML implementation of a StateWriter
* @see JSONWriter The JSON implementation of a StateWriter
* @see XMLStateWriter The XML implementation of a StateWriter
* @see JSONStateWriter The JSON implementation of a StateWriter
*/
class StateWriter {
public:
Expand Down
79 changes: 79 additions & 0 deletions include/flamegpu/io/StateWriterFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#ifndef INCLUDE_FLAMEGPU_IO_STATEWRITERFACTORY_H_
#define INCLUDE_FLAMEGPU_IO_STATEWRITERFACTORY_H_

#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <algorithm>

#include "flamegpu/io/StateWriter.h"
#include "flamegpu/io/XMLStateWriter.h"
#include "flamegpu/io/JSONStateWriter.h"
#include "flamegpu/io/JSONLogger.h"
#include "flamegpu/io/XMLLogger.h"
#include "flamegpu/util/StringPair.h"
#include "flamegpu/util/filesystem.h"

namespace flamegpu {

class AgentVector;

/**
* Factory for creating instances of StateWriter
*/
class StateWriterFactory {
public:
/**
* Returns a writer capable of writing model state to 'output_file'
* Environment properties from the Simulation instance pointed to by 'sim_instance_id' will be used
* Agent data will be read from 'model_state'
* @param model_name Name from the model description hierarchy of the model to be exported
* @param sim_instance_id Instance is from the Simulation instance to export the environment properties from
* @param model_state Map of AgentVector to read the agent data from per agent, key should be agent name
* @param iterations The value from the step counter at the time of export.
* @param output_file Filename of the input file (This will be used to determine which reader to return)
* @param sim_instance Instance of the Simulation object (This is used for setting/getting config)
* @throws UnsupportedFileType If the file extension does not match an appropriate reader
*/
static StateWriter* createWriter(
const std::string& model_name,
const unsigned int& sim_instance_id,
const util::StringPairUnorderedMap<std::shared_ptr<AgentVector>>& model_state,
const unsigned int& iterations,
const std::string& output_file,
const Simulation* sim_instance) {
const std::string extension = util::filesystem::getFileExt(output_file);

if (extension == "xml") {
return new XMLStateWriter(model_name, sim_instance_id, model_state, iterations, output_file, sim_instance);
} else if (extension == "json") {
return new JSONStateWriter(model_name, sim_instance_id, model_state, iterations, output_file, sim_instance);
}
THROW UnsupportedFileType("File '%s' is not a type which can be written "
"by StateWriterFactory::createWriter().",
output_file.c_str());
}
/**
* Return a clean file extension from the provided string
* If the file extension is not supported empty string is returned instead
*/
static std::string detectSupportedFileExt(const std::string &user_file_ext) {
std::string rtn = user_file_ext;
// Move entire string to lower case
std::transform(rtn.begin(), rtn.end(), rtn.begin(), [](unsigned char c) { return std::use_facet< std::ctype<char>>(std::locale()).tolower(c); });
// Strip first character if it is '.'
if (rtn[0] == '.')
rtn = rtn.substr(1);
// Compare against supported formats
if (rtn == "xml" ||
rtn == "json") {
return rtn;
}
return "";
}
};

} // namespace flamegpu

#endif // INCLUDE_FLAMEGPU_IO_STATEWRITERFACTORY_H_
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class RunPlan;
/**
* XML format Logger
*/
class xmlLogger : public Logger{
class XMLLogger : public Logger{
public:
xmlLogger(const std::string &outPath, bool prettyPrint, bool truncateFile);
XMLLogger(const std::string &outPath, bool prettyPrint, bool truncateFile);
/**
* Log a runlog to file, using a RunPlan in place of config
* @throws May throw exceptions if logging to file failed for any reason
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#ifndef INCLUDE_FLAMEGPU_IO_XMLREADER_H_
#define INCLUDE_FLAMEGPU_IO_XMLREADER_H_
#ifndef INCLUDE_FLAMEGPU_IO_XMLSTATEREADER_H_
#define INCLUDE_FLAMEGPU_IO_XMLSTATEREADER_H_

#include <memory>
#include <string>
#include <unordered_map>
#include <utility>

#include "flamegpu/io/statereader.h"
#include "flamegpu/io/StateReader.h"
#include "flamegpu/model/ModelDescription.h"
#include "flamegpu/util/StringPair.h"

Expand All @@ -15,7 +15,7 @@ namespace flamegpu {
/**
* XML format StateReader
*/
class xmlReader : public StateReader {
class XMLStateReader : public StateReader {
public:
/**
* Constructs a reader capable of reading model state from XML files
Expand All @@ -28,7 +28,7 @@ class xmlReader : public StateReader {
* @param input_file Filename of the input file (This will be used to determine which reader to return)
* @param sim_instance Instance of the Simulation object (This is used for setting/getting config)
*/
xmlReader(
XMLStateReader(
const std::string &model_name,
const std::unordered_map<std::string, EnvironmentDescription::PropData> &env_desc,
std::unordered_map<std::pair<std::string, unsigned int>, util::Any> &env_init,
Expand All @@ -52,4 +52,4 @@ class xmlReader : public StateReader {

} // namespace flamegpu

#endif // INCLUDE_FLAMEGPU_IO_XMLREADER_H_
#endif // INCLUDE_FLAMEGPU_IO_XMLSTATEREADER_H_
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef INCLUDE_FLAMEGPU_IO_XMLWRITER_H_
#define INCLUDE_FLAMEGPU_IO_XMLWRITER_H_
#ifndef INCLUDE_FLAMEGPU_IO_XMLSTATEWRITER_H_
#define INCLUDE_FLAMEGPU_IO_XMLSTATEWRITER_H_

#include <memory>
#include <string>
#include <unordered_map>

#include "flamegpu/io/statewriter.h"
#include "flamegpu/io/StateWriter.h"
#include "flamegpu/model/ModelDescription.h"
#include "flamegpu/util/StringPair.h"

Expand All @@ -14,7 +14,7 @@ namespace flamegpu {
/**
* XML format StateWriter
*/
class xmlWriter : public StateWriter {
class XMLStateWriter : public StateWriter {
public:
/**
* Returns a writer capable of writing model state to an XML file
Expand All @@ -27,7 +27,7 @@ class xmlWriter : public StateWriter {
* @param output_file Filename of the input file (This will be used to determine which reader to return)
* @param sim_instance Instance of the Simulation object (This is used for setting/getting config)
*/
xmlWriter(
XMLStateWriter(
const std::string &model_name,
const unsigned int &sim_instance_id,
const util::StringPairUnorderedMap<std::shared_ptr<AgentVector>> &model_state,
Expand All @@ -45,4 +45,4 @@ class xmlWriter : public StateWriter {

} // namespace flamegpu

#endif // INCLUDE_FLAMEGPU_IO_XMLWRITER_H_
#endif // INCLUDE_FLAMEGPU_IO_XMLSTATEWRITER_H_
Loading

0 comments on commit bd59a48

Please sign in to comment.