-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
File / Class renaming and splitting for the io directory
- Loading branch information
Showing
27 changed files
with
325 additions
and
279 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.