mirror of
https://github.com/Jaxan/hybrid-ads.git
synced 2025-04-27 15:07:45 +02:00
Merges the read_mealy files
This commit is contained in:
parent
0ab23f5ec2
commit
713b5fb48c
13 changed files with 137 additions and 141 deletions
|
@ -1,9 +1,11 @@
|
|||
#include "read_mealy_from_dot.hpp"
|
||||
#include "read_mealy.hpp"
|
||||
#include "mealy.hpp"
|
||||
|
||||
#include <boost/algorithm/string/trim.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
|
@ -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<mealy, translation> read_mealy_from_dot(const string & filename){
|
|||
const auto m = read_mealy_from_dot(filename, t);
|
||||
return {move(m), move(t)};
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
std::vector<std::string> create_reverse_map_impl(std::map<std::string, T> const & indices) {
|
||||
std::vector<std::string> ret(indices.size());
|
||||
for (auto && p : indices) {
|
||||
ret[p.second] = p.first;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::vector<string> create_reverse_map(const std::map<string, input> & indices) {
|
||||
return create_reverse_map_impl(indices);
|
||||
}
|
||||
|
||||
#if 0 // Note: input and output are equal types, so this would be a redecl
|
||||
std::vector<string> create_reverse_map(const std::map<string, output> & 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;
|
||||
}
|
42
lib/read_mealy.hpp
Normal file
42
lib/read_mealy.hpp
Normal file
|
@ -0,0 +1,42 @@
|
|||
#pragma once
|
||||
|
||||
#include "types.hpp"
|
||||
|
||||
#include <iosfwd>
|
||||
#include <map>
|
||||
#include <utility>
|
||||
|
||||
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<mealy, translation> read_mealy_from_dot(std::istream & in);
|
||||
std::pair<mealy, translation> read_mealy_from_dot(std::string const & filename);
|
||||
|
||||
|
||||
/// \brief For non-integral formats we use a translation to integers
|
||||
struct translation {
|
||||
std::map<std::string, input> input_indices;
|
||||
input max_input = 0;
|
||||
|
||||
std::map<std::string, output> output_indices;
|
||||
output max_output = 0;
|
||||
};
|
||||
|
||||
/// \brief inverts the input_indices and output_indices maps
|
||||
std::vector<std::string> create_reverse_map(std::map<std::string, input> const & indices);
|
||||
std::vector<std::string> create_reverse_map(std::map<std::string, output> const & indices);
|
||||
|
||||
/// \brief defines trivial translation (the string represent integers directly)
|
||||
translation create_translation_for_mealy(mealy const & m);
|
|
@ -1,38 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include "types.hpp"
|
||||
|
||||
#include <iosfwd>
|
||||
#include <map>
|
||||
#include <utility>
|
||||
|
||||
/*
|
||||
* These maps record the translation used while reading.
|
||||
*/
|
||||
struct translation {
|
||||
std::map<std::string, input> input_indices;
|
||||
input max_input = 0;
|
||||
|
||||
std::map<std::string, output> 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<mealy, translation> read_mealy_from_dot(std::istream & in);
|
||||
std::pair<mealy, translation> read_mealy_from_dot(const std::string & filename);
|
||||
|
||||
// Used to invert the input_indices and output_indices maps
|
||||
template <typename T>
|
||||
std::vector<std::string> create_reverse_map(std::map<std::string, T> const & indices){
|
||||
std::vector<std::string> ret(indices.size());
|
||||
for(auto&& p : indices){
|
||||
ret[p.second] = p.first;
|
||||
}
|
||||
return ret;
|
||||
}
|
|
@ -1,76 +0,0 @@
|
|||
#include "read_mealy_from_txt.hpp"
|
||||
#include "mealy.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include "types.hpp"
|
||||
#include "read_mealy_from_dot.hpp"
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
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);
|
|
@ -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"
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include <mealy.hpp>
|
||||
#include <read_mealy_from_dot.hpp>
|
||||
#include <read_mealy.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
|
|
@ -2,8 +2,7 @@
|
|||
#include <logging.hpp>
|
||||
#include <mealy.hpp>
|
||||
#include <reachability.hpp>
|
||||
#include <read_mealy_from_dot.hpp>
|
||||
#include <read_mealy_from_txt.hpp>
|
||||
#include <read_mealy.hpp>
|
||||
#include <characterization_family.hpp>
|
||||
#include <separating_matrix.hpp>
|
||||
#include <splitting_tree.hpp>
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
#include <adaptive_distinguishing_sequence.hpp>
|
||||
#include <read_mealy_from_dot.hpp>
|
||||
#include <read_mealy_from_txt.hpp>
|
||||
#include <read_mealy.hpp>
|
||||
#include <characterization_family.hpp>
|
||||
#include <separating_matrix.hpp>
|
||||
#include <trie.hpp>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include <mealy.hpp>
|
||||
#include <read_mealy_from_dot.hpp>
|
||||
#include <read_mealy.hpp>
|
||||
#include <transfer_sequences.hpp>
|
||||
|
||||
#include <boost/optional.hpp>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include <mealy.hpp>
|
||||
#include <read_mealy_from_dot.hpp>
|
||||
#include <read_mealy.hpp>
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
#include <mealy.hpp>
|
||||
#include <read_mealy_from_dot.hpp>
|
||||
#include <read_mealy_from_txt.hpp>
|
||||
#include <read_mealy.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
|
|
|
@ -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>
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue