Skip to content
Draft
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
14 changes: 14 additions & 0 deletions include/vara/Sampling/SampleSetWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ class SampleSetWriter {
&Configurations);
};

class SampleSetWriterCSV {
public:
/// This method writes the given configurations to a yml file.
///
/// \param FM the corresponding feature model
/// \param Configurations the configurations to print into the file
///
/// \returns the string containing the output in YAML format
[[nodiscard]] static std::string
writeConfigurations(const vara::feature::FeatureModel &FM,
std::vector<std::unique_ptr<vara::feature::Configuration>>
&Configurations);
};

} // namespace vara::sampling

#endif // VARA_SAMPLING_SAMPLESETWRITER_H
52 changes: 52 additions & 0 deletions lib/Sampling/SampleSetWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,56 @@ std::string vara::sampling::SampleSetWriter::writeConfigurations(
Output << ConfigurationStringMap;
return Str;
}

std::string vara::sampling::SampleSetWriterCSV::writeConfigurations(
const vara::feature::FeatureModel &FM,
std::vector<std::unique_ptr<vara::feature::Configuration>>
&Configurations) {

std::string HeaderStr = "id";

for (auto *F : FM.features()) {
HeaderStr.append(",");
HeaderStr.append(F->getName());
}

HeaderStr.append("\n");

std::vector<std::string> ConfigRows;
ConfigRows.reserve(Configurations.size());

for (size_t ConfigurationCount = 0;
ConfigurationCount < Configurations.size(); ConfigurationCount++) {

auto &Configuration = Configurations.at(ConfigurationCount);
std::string ConfigRow = std::to_string(ConfigurationCount);

for (auto *F : FM.features()) {
auto FeatureName = F->getName();

ConfigRow.append(",");

auto Value = Configuration->configurationOptionValue(F->getName());

assert(Value.has_value() &&
"Could not retrieve option value, broken configuration option.");

ConfigRow.append(Value.value());
}
ConfigRow.append("\n");
ConfigRows.push_back(ConfigRow);
}

// Write configurations to a string in YAML format
std::string Str;
llvm::raw_string_ostream OutputString(Str);

OutputString << HeaderStr;

for (auto &Config : ConfigRows) {
OutputString << Config;
}

return Str;
}
} // namespace vara::sampling
26 changes: 23 additions & 3 deletions tools/config-generator/ConfigurationGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ enum class ConfigurationGenerationChoice : unsigned {
SAMPLE_SET,
};

enum class OutputFormat : unsigned { YAML, CSV };

static llvm::cl::opt<ConfigurationGenerationChoice, false>
ConfigurationGenerationOption(
"type",
Expand All @@ -36,6 +38,14 @@ static llvm::cl::opt<ConfigurationGenerationChoice, false>
llvm::cl::init(ConfigurationGenerationChoice::ALL), llvm::cl::Optional,
llvm::cl::cat(ConfigCreatorCategory));

static llvm::cl::opt<OutputFormat, false> OutputFormatOption(
"format", llvm::cl::desc("The way how the configurations should output."),
llvm::cl::values(
clEnumValN(OutputFormat::YAML, "yaml", "Generate as YAML format"),
clEnumValN(OutputFormat::CSV, "csv", "Generate as CSV format")),
llvm::cl::init(OutputFormat::YAML), llvm::cl::Optional,
llvm::cl::cat(ConfigCreatorCategory));

static llvm::cl::opt<std::string>
CsvInputFilePath("csv", llvm::cl::desc("Path to the csv input file."),
llvm::cl::value_desc("filename"), llvm::cl::init(""),
Expand Down Expand Up @@ -101,9 +111,19 @@ int main(int Argc, char **Argv) {
}

if (!OutputFilePath.empty() && !Configurations.empty()) {
const std::string Str =
vara::sampling::SampleSetWriter::writeConfigurations(*FM,
Configurations);
std::string Str;

switch (OutputFormatOption.getValue()) {
case OutputFormat::YAML:
Str = vara::sampling::SampleSetWriter::writeConfigurations(
*FM, Configurations);
break;
case OutputFormat::CSV:
Str = vara::sampling::SampleSetWriterCSV::writeConfigurations(
*FM, Configurations);
break;
}

std::error_code EC;
auto Out = llvm::raw_fd_ostream(OutputFilePath.getValue(), EC);
if (EC) {
Expand Down
Loading