diff --git a/lib/read_mealy_from_dot.cpp b/lib/read_mealy.cpp similarity index 55% rename from lib/read_mealy_from_dot.cpp rename to lib/read_mealy.cpp index c94fccc..c0dfd72 100644 --- a/lib/read_mealy_from_dot.cpp +++ b/lib/read_mealy.cpp @@ -1,9 +1,11 @@ -#include "read_mealy_from_dot.hpp" +#include "read_mealy.hpp" #include "mealy.hpp" #include +#include #include +#include #include #include @@ -13,6 +15,54 @@ static string easy_substr(string const & s, size_t begin, size_t end){ return s.substr(begin, end - begin); } +mealy read_mealy_from_txt(std::istream & in) { + mealy m; + + state max_state = 0; + input max_input = 0; + output max_output = 0; + + string line; + while (getline(in, line)) { + state from, to; + input i; + output o; + string seperator; + + stringstream ss(line); + ss >> from >> seperator >> i >> seperator >> o >> seperator >> to; + + if (from >= max_state) max_state = from + 1; + if (to >= max_state) max_state = to + 1; + if (i >= max_input) max_input = i + 1; + if (o >= max_output) max_output = o + 1; + + if (defined(m, from, i)) throw runtime_error("Nondeterministic machine"); + + m.graph.resize(max_state); + auto & v = m.graph[from]; + v.resize(max_input); + v[i] = mealy::edge(to, o); + + assert(defined(m, to, i)); + } + + m.graph_size = max_state; + m.input_size = max_input; + m.output_size = max_output; + + if (m.graph_size == 0) throw runtime_error("Empty state set"); + if (m.input_size == 0) throw runtime_error("Empty input set"); + if (m.output_size == 0) throw runtime_error("Empty output set"); + if (!is_complete(m)) throw runtime_error("Partial machine"); + return m; +} + +mealy read_mealy_from_txt(const std::string & filename) { + std::ifstream file(filename); + return read_mealy_from_txt(file); +} + mealy read_mealy_from_dot(std::istream & in, translation & t){ mealy m; @@ -89,3 +139,39 @@ std::pair read_mealy_from_dot(const string & filename){ const auto m = read_mealy_from_dot(filename, t); return {move(m), move(t)}; } + + +template +std::vector create_reverse_map_impl(std::map const & indices) { + std::vector ret(indices.size()); + for (auto && p : indices) { + ret[p.second] = p.first; + } + return ret; +} + +std::vector create_reverse_map(const std::map & indices) { + return create_reverse_map_impl(indices); +} + +#if 0 // Note: input and output are equal types, so this would be a redecl +std::vector create_reverse_map(const std::map & indices) { + return create_reverse_map_impl(indices); +} +#endif + +translation create_translation_for_mealy(const mealy & m) { + translation t; + t.max_input = m.input_size; + t.max_output = m.output_size; + + for (input i = 0; i < t.max_input; ++i) { + t.input_indices[to_string(i)] = i; + } + + for (output o = 0; o < t.max_output; ++o) { + t.output_indices[to_string(o)] = o; + } + + return t; +} diff --git a/lib/read_mealy.hpp b/lib/read_mealy.hpp new file mode 100644 index 0000000..845311a --- /dev/null +++ b/lib/read_mealy.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include "types.hpp" + +#include +#include +#include + +struct mealy; +struct translation; + +/// \brief reads a mealy machine from plain txt file as provided by A. T. Endo +/// States, inputs and outputs in these files are already integral, so no need for translation +mealy read_mealy_from_txt(std::istream & in); +mealy read_mealy_from_txt(std::string const & filename); + +/// \brief reads a mealy machine from dot files as generated by learnlib +/// Here we need a translation, which is extended during parsing +mealy read_mealy_from_dot(std::istream & in, translation & t); +mealy read_mealy_from_dot(std::string const & filename, translation & t); + +/// \brief reads a mealy machine from dot files as generated by learnlib +/// Here the translation starts out empty and is returned in the end +std::pair read_mealy_from_dot(std::istream & in); +std::pair read_mealy_from_dot(std::string const & filename); + + +/// \brief For non-integral formats we use a translation to integers +struct translation { + std::map input_indices; + input max_input = 0; + + std::map output_indices; + output max_output = 0; +}; + +/// \brief inverts the input_indices and output_indices maps +std::vector create_reverse_map(std::map const & indices); +std::vector create_reverse_map(std::map const & indices); + +/// \brief defines trivial translation (the string represent integers directly) +translation create_translation_for_mealy(mealy const & m); diff --git a/lib/read_mealy_from_dot.hpp b/lib/read_mealy_from_dot.hpp deleted file mode 100644 index 653d7cb..0000000 --- a/lib/read_mealy_from_dot.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#include "types.hpp" - -#include -#include -#include - -/* - * These maps record the translation used while reading. - */ -struct translation { - std::map input_indices; - input max_input = 0; - - std::map output_indices; - output max_output = 0; -}; - -struct mealy; - -// Read a mealy machine while extending the translation -mealy read_mealy_from_dot(std::istream & in, translation & t); -mealy read_mealy_from_dot(const std::string & filename, translation & t); - -// Read a mealy machine, starting with the empty translation -std::pair read_mealy_from_dot(std::istream & in); -std::pair read_mealy_from_dot(const std::string & filename); - -// Used to invert the input_indices and output_indices maps -template -std::vector create_reverse_map(std::map const & indices){ - std::vector ret(indices.size()); - for(auto&& p : indices){ - ret[p.second] = p.first; - } - return ret; -} diff --git a/lib/read_mealy_from_txt.cpp b/lib/read_mealy_from_txt.cpp deleted file mode 100644 index 6a1d75a..0000000 --- a/lib/read_mealy_from_txt.cpp +++ /dev/null @@ -1,76 +0,0 @@ -#include "read_mealy_from_txt.hpp" -#include "mealy.hpp" - -#include -#include -#include -#include - -#include - -using namespace std; - -mealy read_mealy_from_txt(std::istream & in) { - mealy m; - - state max_state = 0; - input max_input = 0; - output max_output = 0; - - string line; - while (getline(in, line)) { - state from, to; - input i; - output o; - string seperator; - - stringstream ss(line); - ss >> from >> seperator >> i >> seperator >> o >> seperator >> to; - - if (from >= max_state) max_state = from + 1; - if (to >= max_state) max_state = to + 1; - if (i >= max_input) max_input = i + 1; - if (o >= max_output) max_output = o + 1; - - if (defined(m, from, i)) throw runtime_error("Nondeterministic machine"); - - m.graph.resize(max_state); - auto & v = m.graph[from]; - v.resize(max_input); - v[i] = mealy::edge(to, o); - - assert(defined(m, to, i)); - } - - m.graph_size = max_state; - m.input_size = max_input; - m.output_size = max_output; - - if (m.graph_size == 0) throw runtime_error("Empty state set"); - if (m.input_size == 0) throw runtime_error("Empty input set"); - if (m.output_size == 0) throw runtime_error("Empty output set"); - if (!is_complete(m)) throw runtime_error("Partial machine"); - return m; -} - -mealy read_mealy_from_txt(const std::string & filename) { - std::ifstream file(filename); - return read_mealy_from_txt(file); -} - - -translation create_translation_for_mealy(const mealy & m) { - translation t; - t.max_input = m.input_size; - t.max_output = m.output_size; - - for (input i = 0; i < t.max_input; ++i) { - t.input_indices[to_string(i)] = i; - } - - for (output o = 0; o < t.max_output; ++o) { - t.output_indices[to_string(o)] = o; - } - - return t; -} diff --git a/lib/read_mealy_from_txt.hpp b/lib/read_mealy_from_txt.hpp deleted file mode 100644 index cb8e3d0..0000000 --- a/lib/read_mealy_from_txt.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include "types.hpp" -#include "read_mealy_from_dot.hpp" - -#include - -struct mealy; - -/// \brief reads a mealy machine from plain txt file (as provided by A. T. Endo) -/// States, inputs and outputs in these files are already integral (and contiguous) -mealy read_mealy_from_txt(std::istream & in); -mealy read_mealy_from_txt(std::string const & filename); - -translation create_translation_for_mealy(mealy const & m); diff --git a/lib/write_tree_to_dot.cpp b/lib/write_tree_to_dot.cpp index 567a3c5..aefdaa0 100644 --- a/lib/write_tree_to_dot.cpp +++ b/lib/write_tree_to_dot.cpp @@ -1,5 +1,5 @@ #include "adaptive_distinguishing_sequence.hpp" -#include "read_mealy_from_dot.hpp" +#include "read_mealy.hpp" #include "splitting_tree.hpp" #include "write_tree_to_dot.hpp" diff --git a/src/distance.cpp b/src/distance.cpp index 428ec81..ea34de2 100644 --- a/src/distance.cpp +++ b/src/distance.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include diff --git a/src/main.cpp b/src/main.cpp index 1a20d6c..ecc3370 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,8 +2,7 @@ #include #include #include -#include -#include +#include #include #include #include diff --git a/src/methods.cpp b/src/methods.cpp index 349d36f..667eac8 100644 --- a/src/methods.cpp +++ b/src/methods.cpp @@ -1,6 +1,5 @@ #include -#include -#include +#include #include #include #include diff --git a/src/pre_gephi_tool.cpp b/src/pre_gephi_tool.cpp index 4209166..d8f46b6 100644 --- a/src/pre_gephi_tool.cpp +++ b/src/pre_gephi_tool.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include diff --git a/src/reachability.cpp b/src/reachability.cpp index 633e507..0ff3a92 100644 --- a/src/reachability.cpp +++ b/src/reachability.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include diff --git a/src/stats.cpp b/src/stats.cpp index c18836c..90ec6e4 100644 --- a/src/stats.cpp +++ b/src/stats.cpp @@ -1,6 +1,5 @@ #include -#include -#include +#include #include #include diff --git a/src/trees.cpp b/src/trees.cpp index f89e643..cd91faa 100644 --- a/src/trees.cpp +++ b/src/trees.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include