Skip to content
Snippets Groups Projects
Commit d03e5d8b authored by Jaime Arias's avatar Jaime Arias
Browse files

wip: main.cpp

parent f9c2bf6c
No related branches found
No related tags found
No related merge requests found
Pipeline #9938 failed with stage
......@@ -8,6 +8,7 @@
#include "Net.hpp"
#include "bdd.h"
#include "bvec.h"
#include "petri_net_sog.hpp"
// BDD initial values
......@@ -37,11 +38,10 @@ bool ValidTransitionName(const std::string& str) {
* @param output_file Path to the file where the output will be saved
* @param obs_paths Set of observable paths
*/
void SaveObservablePaths(const Paths& obs_paths,
const std::string& output_file) {
std::cout << "\nSaving results in " << output_file << '\n';
void SaveObservablePaths(const Paths& obs_paths, const string& output_file) {
cout << "\nSaving results in " << output_file << '\n';
std::ofstream my_file(output_file);
ofstream my_file(output_file);
my_file << "# paths: " << obs_paths.size() << '\n';
int path_id = 1;
......@@ -58,7 +58,7 @@ void SaveObservablePaths(const Paths& obs_paths,
*/
void PrintStats(const Paths& abstract_paths) {
size_t sum_transitions = 0;
std::set<int> transitions;
set<int> transitions;
for (const Path& path : abstract_paths) {
for (int transition_id : path) {
......@@ -76,11 +76,10 @@ void PrintStats(const Paths& abstract_paths) {
}
std_deviation = sqrt(std_deviation / nb_paths);
std::cout << "# abstract paths: " << nb_paths << '\n';
std::cout << "average # of transitions per abstract path: " << average
<< '\n';
std::cout << "standard deviation: " << std_deviation << '\n';
std::cout << "# covered transitions: " << transitions.size() << '\n';
cout << "# abstract paths: " << nb_paths << '\n';
cout << "average # of transitions per abstract path: " << average << '\n';
cout << "standard deviation: " << std_deviation << '\n';
cout << "# covered transitions: " << transitions.size() << '\n';
}
/**
......@@ -88,8 +87,8 @@ void PrintStats(const Paths& abstract_paths) {
* @param sog SOG graph
* @param sog_file Path to the file where the SOG will be saved
*/
void SaveSOG(const SOG& sog, const std::string& sog_file) {
std::cout << "Saving generated SOG in " << sog_file << '\n';
void SaveSOG(const SOG& sog, const string& sog_file) {
cout << "Saving generated SOG in " << sog_file << '\n';
sog.ExportGraphToDotFile(sog_file);
}
......@@ -103,14 +102,14 @@ void SaveSOG(const SOG& sog, const std::string& sog_file) {
* @param output_file_prefix file where the information will be saved
*/
void PrintOutput(const RdP::net& model, SOG& sog,
const std::map<int, int>& obs_transitions,
const Paths& obs_paths, const Paths& abstract_paths,
const std::string& output_file_prefix) {
const map<int, int>& obs_transitions, const Paths& obs_paths,
const Paths& abstract_paths,
const string& output_file_prefix) {
// print SOG information
sog.PrintCompleteInformation();
std::cout << "\n# transition: " << model.transitions.size() << '\n';
std::cout << "# places: " << model.places.size() << '\n';
std::cout << "# observable transitions: " << obs_transitions.size() << "\n\n";
cout << "\n# transition: " << model.transitions.size() << '\n';
cout << "# places: " << model.places.size() << '\n';
cout << "# observable transitions: " << obs_transitions.size() << "\n\n";
// print stats
PrintStats(abstract_paths);
......@@ -127,10 +126,10 @@ void PrintOutput(const RdP::net& model, SOG& sog,
* @param filename Path to the petri net model
* @return name of the model
*/
std::string GetModelName(const std::string& filename) {
string GetModelName(const string& filename) {
const auto end_pos = filename.find(".n") + 1;
const auto start_pos = filename.rfind('/') + 1;
std::string name = filename.substr(start_pos, end_pos - start_pos - 1);
string name = filename.substr(start_pos, end_pos - start_pos - 1);
return name;
}
......@@ -139,10 +138,10 @@ std::string GetModelName(const std::string& filename) {
* @param filename Path to the petri net model
* @return Petri net model
*/
RdP::net LoadPetriNet(const std::string& filename) {
std::cout << "Parsing net: " << filename << " ... ";
RdP::net LoadPetriNet(const string& filename) {
cout << "Parsing net: " << filename << " ... ";
RdP::net model(filename.c_str());
std::cout << "done\n";
cout << "done\n";
return model;
}
......@@ -154,13 +153,12 @@ RdP::net LoadPetriNet(const std::string& filename) {
* @param obs_trans set of observable transitions
* @param unobs_trans set of unobservable transitions
*/
void LoadTransitions(const RdP::net& model, const std::string& file,
std::map<int, int>& obs_trans,
std::set<int>& unobs_trans) {
void LoadTransitions(const RdP::net& model, const string& file,
map<int, int>& obs_trans, set<int>& unobs_trans) {
ifstream my_file(file);
if (my_file) {
std::string line;
string line;
while (getline(my_file, line)) {
// check if the transition name is valid
if (!ValidTransitionName(line)) {
......@@ -188,8 +186,8 @@ void LoadTransitions(const RdP::net& model, const std::string& file,
* @param[out] obs_trans set of observable transitions
* @param[out] unobs_trans set of unobservable transitions
*/
void FindObservableTransitions(RdP::net& model, std::map<int, int>& obs_trans,
std::set<int>& unobs_trans) {
void FindObservableTransitions(RdP::net& model, map<int, int>& obs_trans,
set<int>& unobs_trans) {
// compute the unobservable transitions of the model using the pattern
unobs_trans = model.calcul1();
......@@ -317,6 +315,103 @@ void ComputeAbstractPaths(const std::string& net_file, int bound, bool only_sog,
output_file_prefix);
}
void SaveObsPathsAndWeights(const string& net_file, int bound, bool only_sog,
const string& transitions_file,
const string& output_folder) {
SOG sog;
Paths obs_paths;
std::vector<int> weight_obs_paths;
Paths abstract_paths;
set<int> unobs_trans;
map<int, int> obs_trans;
// output file
string model_name = GetModelName(net_file);
string output_file_prefix = output_folder + "/" + model_name;
// load the petri net model
RdP::net model = LoadPetriNet(net_file);
// BDD initialization.
// See https://buddy.sourceforge.net/manual/group__kernel.html
bdd_init(BDD_INITIAL_NUM_NODES, BDD_SIZE_CACHES);
auto start_time = GetTime();
// if a path with transitions is not given, then we apply the algorithm to
// find the needed observable transitions to cover all the behaviors
if (transitions_file.empty()) {
cout << "\nComputing observable transitions ...";
auto start_obs_time = GetTime();
FindObservableTransitions(model, obs_trans, unobs_trans);
auto obs_time = GetTime() - start_obs_time;
cout << " done\nTime for computing observable transitions: " << obs_time
<< " seconds\n";
} else {
LoadTransitions(model, transitions_file, obs_trans, unobs_trans);
}
// build the net
cout << "\nBuilding net ...";
auto start_net_time = GetTime();
PetriNetSOG pn_sog(model, obs_trans, unobs_trans, bound, false);
auto net_time = GetTime() - start_net_time;
cout << " done\nTime for computing the net: " << net_time << " seconds\n";
// compute the observable paths
cout << "\nComputing the SOG ...";
auto start_sog_time = GetTime();
pn_sog.GenerateSOG(sog);
auto sog_time = GetTime() - start_sog_time;
cout << " done\nTime for computing SOG: " << sog_time << " seconds\n";
std::cout << "\nGenerating regex from pn_sog ... ";
const Lg languages = pn_sog.GenerateRegExp(&sog);
auto regex_time = GetTime() - (start_sog_time + sog_time);
cout << "\nTime for computing Regex: " << regex_time << " seconds\n";
// vector of generated paths
std::vector<std::vector<std::string>> generated_paths;
std::cout << "\n\nGenerating paths ... ";
for (const LgElement& e : languages) {
for (const std::string& path : PathGenerator::ExtractPaths(e.first)) {
if (!path.empty()) {
generated_paths.push_back(path);
}
}
}
const float paths_time = GetTime() - (start_sog_time + sog_time + regex_time);
std::cout << "\n Elapsed time for generating paths: " << paths_time
<< " seconds";
std::cout << "\n # of no null paths: " << generated_paths.size();
weight_obs_paths = pn_sog.ComputeWeightObsPaths(generated_paths, sog);
const float obs_paths_time =
GetTime() - (paths_time + start_sog_time + sog_time);
std::cout << "\n Elapsed time for generating obs paths: " << paths_time
<< " seconds";
// time for generating the paths
auto elapsed_time = GetTime() - start_time;
cout << "\nTotal time: " << elapsed_time << " seconds\n";
// print output
PrintOutput(model, sog, obs_trans, obs_paths, abstract_paths,
output_file_prefix);
std::string output_file = output_file_prefix + "_weights.txt";
cout << "\nSaving weights in " << output_file << '\n';
ofstream my_file(output_file);
my_file << "# path weights: ";
for (auto w : weight_obs_paths) {
my_file << w << ' ';
}
my_file << '\n';
}
/******************************************************************************
* Main function
******************************************************************************/
......@@ -324,13 +419,13 @@ int main(const int argc, char** argv) {
CLI::App app{
"sogMBT: Symbolic Observation Graph-Based Generation of Test Paths"};
std::string input_file;
string input_file;
app.add_option("--input-net", input_file, "Petri net file")
->type_name("Path")
->required()
->check(CLI::ExistingFile);
std::string output_folder;
string output_folder;
app.add_option("--output-folder", output_folder, "output folder")
->type_name("Path")
->required()
......@@ -355,7 +450,7 @@ int main(const int argc, char** argv) {
// parse arguments
CLI11_PARSE(app, argc, argv);
ComputeAbstractPaths(input_file, bound, only_sog, obs_file, output_folder);
// ComputeAbstractPaths(input_file, bound, only_sog, obs_file, output_folder);
SaveObsPathsAndWeights(input_file, bound, only_sog, obs_file, output_folder);
return 0;
}
......@@ -104,6 +104,7 @@ class PetriNetSOG {
* Computes the weight of each observable path
* @param obsPaths : a list of paths each represented by a vector of string
* (transitions composing the path)
* @param g SOG
* @return a vector containing the corresponding weights
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment