307 lines
12 KiB
C++
307 lines
12 KiB
C++
#include "hierarchical_fsm.hpp"
|
|
#include <charconv>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
using namespace hierarchical_fsm;
|
|
|
|
class Fields {
|
|
public:
|
|
explicit Fields(const std::string &line)
|
|
{
|
|
std::istringstream stream(line);
|
|
std::string token;
|
|
while (stream >> token) words_.push_back(token);
|
|
}
|
|
bool empty() const { return words_.empty(); }
|
|
const std::string &head() const { return words_.front(); }
|
|
template<class Number> bool number(Number &value)
|
|
{
|
|
if (position_ == words_.size()) return false;
|
|
const std::string &token = words_[position_++];
|
|
const auto result = std::from_chars(token.data(), token.data() + token.size(), value);
|
|
return result.ec == std::errc{} && result.ptr == token.data() + token.size();
|
|
}
|
|
bool done() const { return position_ == words_.size(); }
|
|
private:
|
|
std::vector<std::string> words_;
|
|
std::size_t position_ = 1;
|
|
};
|
|
|
|
template<class T, class Printer>
|
|
void list(std::ostream &stream, const std::vector<T> &values, Printer print)
|
|
{
|
|
stream << '[';
|
|
for (std::size_t i = 0; i < values.size(); ++i) {
|
|
if (i != 0) stream << ',';
|
|
print(stream, values[i]);
|
|
}
|
|
stream << ']';
|
|
}
|
|
|
|
void ids(std::ostream &stream, const std::vector<StateId> &values)
|
|
{
|
|
list(stream, values, [](std::ostream &out, StateId id) { out << id; });
|
|
}
|
|
|
|
void state(std::ostream &stream, const State &value)
|
|
{
|
|
stream << '[' << value.id << ',' << value.parent << ',' << value.initial
|
|
<< ',' << value.enter_tag << ',' << value.leave_tag << ']';
|
|
}
|
|
|
|
void transition(std::ostream &stream, const Transition &value)
|
|
{
|
|
stream << '[' << value.id << ',' << value.source << ',' << value.event
|
|
<< ',' << value.required << ',' << value.forbidden << ',' << value.priority
|
|
<< ',' << value.kind << ',' << value.target << ',' << value.target_mode
|
|
<< ',' << value.action_tag << ']';
|
|
}
|
|
|
|
void chart(std::ostream &stream, const Chart &value)
|
|
{
|
|
stream << '[' << value.root << ',';
|
|
list(stream, value.states, state);
|
|
stream << ',';
|
|
list(stream, value.transitions, transition);
|
|
stream << ']';
|
|
}
|
|
|
|
void analysis(std::ostream &stream, const Analysis &value)
|
|
{
|
|
stream << '[';
|
|
list(stream, value.states, [](std::ostream &out, const StateInfo &info) {
|
|
out << '[';
|
|
state(out, info.state);
|
|
out << ',';
|
|
ids(out, info.path);
|
|
out << ',';
|
|
ids(out, info.children);
|
|
out << ',' << info.initial_leaf << ']';
|
|
});
|
|
stream << ',';
|
|
list(stream, value.transitions, transition);
|
|
stream << ']';
|
|
}
|
|
|
|
void snapshot(std::ostream &stream, const Snapshot &value)
|
|
{
|
|
stream << '[' << (value.configured ? "true" : "false") << ',' << value.root << ',';
|
|
ids(stream, value.active);
|
|
stream << ',';
|
|
list(stream, value.history, [](std::ostream &out, const Memory &memory) {
|
|
out << '[' << memory.composite << ',' << memory.child << ']';
|
|
});
|
|
stream << ']';
|
|
}
|
|
|
|
void record(std::ostream &stream, const Record &value)
|
|
{
|
|
stream << '[' << static_cast<std::uint32_t>(value.phase)
|
|
<< ',' << value.state << ',' << value.tag << ']';
|
|
}
|
|
|
|
void selection(std::ostream &stream, const Selection &value)
|
|
{
|
|
stream << '[' << (value.found ? "true" : "false") << ',';
|
|
transition(stream, value.transition);
|
|
stream << ']';
|
|
}
|
|
|
|
void step(std::ostream &stream, const Step &value)
|
|
{
|
|
stream << '[';
|
|
selection(stream, value.selected);
|
|
stream << ',' << value.boundary << ',';
|
|
list(stream, value.trace, record);
|
|
stream << ']';
|
|
}
|
|
|
|
void event(std::ostream &stream, const RawEvent &value)
|
|
{
|
|
stream << '[' << value.event << ',' << value.flags << ']';
|
|
}
|
|
|
|
template<class T, class Printer>
|
|
std::string capture(const T &value, Printer print)
|
|
{
|
|
std::ostringstream stream;
|
|
print(stream, value);
|
|
return stream.str();
|
|
}
|
|
|
|
Selection dirty_selection()
|
|
{
|
|
Selection output;
|
|
output.found = true;
|
|
output.transition = Transition{60000, 60001, 250, 7, 9, -17, 1, 60002, 1, 60003};
|
|
return output;
|
|
}
|
|
|
|
Step dirty_step()
|
|
{
|
|
return Step{dirty_selection(), 60004, {{Phase::action, 60005, 60006}}};
|
|
}
|
|
|
|
bool next_payload(const char *required, Fields &fields)
|
|
{
|
|
std::string line;
|
|
if (!std::getline(std::cin, line)) return false;
|
|
fields = Fields(line);
|
|
return !fields.empty() && fields.head() == required;
|
|
}
|
|
|
|
bool read_chart(Fields &fields, Chart &output)
|
|
{
|
|
std::uint32_t count_states = 0;
|
|
std::uint32_t count_transitions = 0;
|
|
Chart replacement;
|
|
if (!fields.number(replacement.root) || !fields.number(count_states) ||
|
|
!fields.number(count_transitions) || !fields.done() ||
|
|
count_states > 65 || count_transitions > 513) return false;
|
|
for (std::uint32_t i = 0; i < count_states; ++i) {
|
|
Fields row("");
|
|
State value;
|
|
if (!next_payload("N", row) || !row.number(value.id) ||
|
|
!row.number(value.parent) || !row.number(value.initial) ||
|
|
!row.number(value.enter_tag) || !row.number(value.leave_tag) ||
|
|
!row.done()) return false;
|
|
replacement.states.push_back(value);
|
|
}
|
|
for (std::uint32_t i = 0; i < count_transitions; ++i) {
|
|
Fields row("");
|
|
Transition value;
|
|
if (!next_payload("T", row) || !row.number(value.id) ||
|
|
!row.number(value.source) || !row.number(value.event) ||
|
|
!row.number(value.required) || !row.number(value.forbidden) ||
|
|
!row.number(value.priority) || !row.number(value.kind) ||
|
|
!row.number(value.target) || !row.number(value.target_mode) ||
|
|
!row.number(value.action_tag) || !row.done()) return false;
|
|
replacement.transitions.push_back(value);
|
|
}
|
|
output = std::move(replacement);
|
|
return true;
|
|
}
|
|
|
|
int protocol_error()
|
|
{
|
|
std::cerr << "invalid observation command\n";
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
using namespace hierarchical_fsm;
|
|
Machine machines[2];
|
|
Snapshot saved[4];
|
|
Chart input;
|
|
unsigned bank = 0;
|
|
std::string line;
|
|
std::cout << std::boolalpha;
|
|
while (std::getline(std::cin, line)) {
|
|
Fields fields(line);
|
|
if (fields.empty() || fields.head()[0] == '#') continue;
|
|
const std::string op = fields.head();
|
|
Machine &machine = machines[bank];
|
|
if (op == "C") {
|
|
if (!read_chart(fields, input)) return protocol_error();
|
|
std::cout << "[\"C\"]\n";
|
|
} else if (op == "U") {
|
|
unsigned selected = 0;
|
|
if (!fields.number(selected) || !fields.done() || selected > 1) return protocol_error();
|
|
bank = selected;
|
|
std::cout << "[\"U\"," << bank << "]\n";
|
|
} else if (op == "Y") {
|
|
unsigned destination = 0;
|
|
if (!fields.number(destination) || !fields.done() || destination > 1) return protocol_error();
|
|
Snapshot before = machine.snapshot();
|
|
const std::string before_text = capture(before, snapshot);
|
|
Machine copy(machine);
|
|
machines[destination] = copy;
|
|
std::cout << "[\"Y\"," << destination << ',' << before_text << ',';
|
|
snapshot(std::cout, machines[destination].snapshot());
|
|
std::cout << ',' << (before_text == capture(before, snapshot)) << "]\n";
|
|
} else if (op == "V" || op == "A") {
|
|
if (!fields.done()) return protocol_error();
|
|
const std::string raw_before = capture(input, chart);
|
|
Analysis output{{StateInfo{State{60000, 0, 0, 1, 2}, {60000}, {60001}, 60002}},
|
|
{dirty_selection().transition}};
|
|
const Status status = op == "V" ? validate_chart(input) : analyze_chart(input, output);
|
|
std::cout << "[\"" << op << "\"," << static_cast<int>(status);
|
|
if (op == "A") { std::cout << ','; analysis(std::cout, output); }
|
|
std::cout << ',' << (raw_before == capture(input, chart)) << "]\n";
|
|
} else if (op == "R") {
|
|
if (!fields.done()) return protocol_error();
|
|
Snapshot before = machine.snapshot();
|
|
const std::string before_text = capture(before, snapshot);
|
|
const std::string raw_before = capture(input, chart);
|
|
std::vector<Record> output{{Phase::action, 60000, 60001}};
|
|
const Status status = machine.reset(input, output);
|
|
std::cout << "[\"R\"," << static_cast<int>(status) << ',' << before_text << ',';
|
|
snapshot(std::cout, machine.snapshot());
|
|
std::cout << ',';
|
|
list(std::cout, output, record);
|
|
std::cout << ',' << (raw_before == capture(input, chart))
|
|
<< ',' << (before_text == capture(before, snapshot)) << "]\n";
|
|
} else if (op == "Q" || op == "D") {
|
|
RawEvent raw;
|
|
if (!fields.number(raw.event) || !fields.number(raw.flags) || !fields.done()) return protocol_error();
|
|
const std::string raw_before = capture(raw, event);
|
|
Snapshot before = machine.snapshot();
|
|
const std::string before_text = capture(before, snapshot);
|
|
Selection selected = dirty_selection();
|
|
Step output = dirty_step();
|
|
const Status status = op == "Q" ? machine.select(raw, selected) : machine.dispatch(raw, output);
|
|
std::cout << "[\"" << op << "\"," << static_cast<int>(status) << ',' << before_text << ',';
|
|
snapshot(std::cout, machine.snapshot());
|
|
std::cout << ',';
|
|
if (op == "Q") selection(std::cout, selected); else step(std::cout, output);
|
|
std::cout << ',' << (raw_before == capture(raw, event))
|
|
<< ',' << (before_text == capture(before, snapshot)) << "]\n";
|
|
} else if (op == "B") {
|
|
unsigned count = 0;
|
|
if (!fields.number(count) || !fields.done() || count > 129) return protocol_error();
|
|
std::vector<RawEvent> events;
|
|
for (unsigned i = 0; i < count; ++i) {
|
|
Fields row("");
|
|
RawEvent raw;
|
|
if (!next_payload("E", row) || !row.number(raw.event) ||
|
|
!row.number(raw.flags) || !row.done()) return protocol_error();
|
|
events.push_back(raw);
|
|
}
|
|
const auto raw_text = [](std::ostream &out, const std::vector<RawEvent> &raw) {
|
|
list(out, raw, event);
|
|
};
|
|
const std::string raw_before = capture(events, raw_text);
|
|
Snapshot before = machine.snapshot();
|
|
const std::string before_text = capture(before, snapshot);
|
|
std::vector<Step> output{dirty_step()};
|
|
const Status status = machine.dispatch_many(events, output);
|
|
std::cout << "[\"B\"," << static_cast<int>(status) << ',' << before_text << ',';
|
|
snapshot(std::cout, machine.snapshot());
|
|
std::cout << ',';
|
|
list(std::cout, output, step);
|
|
std::cout << ',' << (raw_before == capture(events, raw_text))
|
|
<< ',' << (before_text == capture(before, snapshot)) << "]\n";
|
|
} else if (op == "P" || op == "K" || op == "O") {
|
|
unsigned slot = 0;
|
|
if (op != "P" && (!fields.number(slot) || slot > 3)) return protocol_error();
|
|
if (!fields.done()) return protocol_error();
|
|
if (op == "K") saved[slot] = machine.snapshot();
|
|
std::cout << "[\"" << op << "\",";
|
|
if (op == "P") snapshot(std::cout, machine.snapshot());
|
|
else { std::cout << slot << ','; snapshot(std::cout, saved[slot]); }
|
|
std::cout << "]\n";
|
|
} else {
|
|
return protocol_error();
|
|
}
|
|
}
|
|
if (std::cin.bad()) return protocol_error();
|
|
return 0;
|
|
}
|