168 lines
6.0 KiB
C++
168 lines
6.0 KiB
C++
#include "hierarchical_fsm.hpp"
|
|
#include <algorithm>
|
|
#include <cstddef>
|
|
#include <limits>
|
|
#include <utility>
|
|
|
|
namespace hierarchical_fsm {
|
|
namespace {
|
|
constexpr std::size_t absent = std::numeric_limits<std::size_t>::max();
|
|
|
|
bool real_id(std::uint32_t value)
|
|
{
|
|
return value != 0 && value <= 65535;
|
|
}
|
|
|
|
class Index {
|
|
public:
|
|
explicit Index(const Chart &chart) : chart_(chart) {}
|
|
|
|
std::size_t find(StateId id) const
|
|
{
|
|
const auto found = std::lower_bound(nodes.begin(), nodes.end(), id,
|
|
[](const State *node, StateId key) { return node->id < key; });
|
|
if (found == nodes.end() || (*found)->id != id) return absent;
|
|
return static_cast<std::size_t>(found - nodes.begin());
|
|
}
|
|
|
|
bool build()
|
|
{
|
|
if (chart_.states.empty() || chart_.states.size() > 64 ||
|
|
chart_.transitions.size() > 512 || !real_id(chart_.root)) {
|
|
return false;
|
|
}
|
|
for (const State &node : chart_.states) {
|
|
if (!real_id(node.id) || node.parent > 65535 || node.initial > 65535 ||
|
|
node.enter_tag > 65535 || node.leave_tag > 65535) {
|
|
return false;
|
|
}
|
|
nodes.push_back(&node);
|
|
}
|
|
std::sort(nodes.begin(), nodes.end(),
|
|
[](const State *left, const State *right) { return left->id < right->id; });
|
|
for (std::size_t i = 1; i < nodes.size(); ++i) {
|
|
if (nodes[i - 1]->id == nodes[i]->id) return false;
|
|
}
|
|
const std::size_t root = find(chart_.root);
|
|
if (root == absent) return false;
|
|
parents.assign(nodes.size(), absent);
|
|
initials.assign(nodes.size(), absent);
|
|
std::vector<std::size_t> children(nodes.size(), 0);
|
|
for (std::size_t i = 0; i < nodes.size(); ++i) {
|
|
const State &node = *nodes[i];
|
|
if (node.parent == 0) {
|
|
if (i != root) return false;
|
|
} else {
|
|
if (i == root) return false;
|
|
const std::size_t parent = find(node.parent);
|
|
if (parent == absent || parent == i) return false;
|
|
parents[i] = parent;
|
|
++children[parent];
|
|
}
|
|
if (node.initial != 0) {
|
|
initials[i] = find(node.initial);
|
|
if (initials[i] == absent) return false;
|
|
}
|
|
}
|
|
std::vector<unsigned char> colors(nodes.size(), 0);
|
|
for (std::size_t i = 0; i < nodes.size(); ++i) {
|
|
if (!visit(i, colors)) return false;
|
|
if (children[i] == 0) {
|
|
if (initials[i] != absent) return false;
|
|
} else if (initials[i] == absent || parents[initials[i]] != i) {
|
|
return false;
|
|
}
|
|
}
|
|
std::vector<TransitionId> ids;
|
|
std::vector<std::size_t> counts(nodes.size(), 0);
|
|
for (const Transition &rule : chart_.transitions) {
|
|
const std::size_t source = find(rule.source);
|
|
if (!real_id(rule.id) || source == absent || rule.event > 255 ||
|
|
rule.required > 4294967295ULL || rule.forbidden > 4294967295ULL ||
|
|
rule.priority < -2147483648LL || rule.priority > 2147483647LL ||
|
|
rule.action_tag > 65535 || rule.kind > 1) {
|
|
return false;
|
|
}
|
|
if (++counts[source] > 8) return false;
|
|
ids.push_back(rule.id);
|
|
if (rule.kind == 0) {
|
|
if (rule.target != 0 || rule.target_mode != 0) return false;
|
|
} else {
|
|
const std::size_t target = find(rule.target);
|
|
if (target == absent || rule.target_mode > 1) return false;
|
|
if (rule.target_mode == 1 && children[target] == 0) return false;
|
|
}
|
|
}
|
|
std::sort(ids.begin(), ids.end());
|
|
return std::adjacent_find(ids.begin(), ids.end()) == ids.end();
|
|
}
|
|
|
|
std::vector<StateId> path(std::size_t index) const
|
|
{
|
|
std::vector<StateId> output;
|
|
for (std::size_t current = index; current != absent; current = parents[current]) {
|
|
output.push_back(nodes[current]->id);
|
|
}
|
|
std::reverse(output.begin(), output.end());
|
|
return output;
|
|
}
|
|
|
|
std::vector<const State *> nodes;
|
|
std::vector<std::size_t> parents;
|
|
std::vector<std::size_t> initials;
|
|
|
|
private:
|
|
bool visit(std::size_t index, std::vector<unsigned char> &colors) const
|
|
{
|
|
if (colors[index] == 2) return true;
|
|
if (colors[index] == 1) return false;
|
|
colors[index] = 1;
|
|
if (parents[index] != absent && !visit(parents[index], colors)) return false;
|
|
colors[index] = 2;
|
|
return true;
|
|
}
|
|
|
|
const Chart &chart_;
|
|
};
|
|
|
|
bool transition_order(const Transition &left, const Transition &right)
|
|
{
|
|
if (left.source != right.source) return left.source < right.source;
|
|
if (left.event != right.event) return left.event < right.event;
|
|
if (left.priority != right.priority) return left.priority > right.priority;
|
|
return left.id < right.id;
|
|
}
|
|
}
|
|
|
|
Status validate_chart(const Chart &chart)
|
|
{
|
|
Index index(chart);
|
|
return index.build() ? Status::ok : Status::invalid;
|
|
}
|
|
|
|
Status analyze_chart(const Chart &chart, Analysis &output)
|
|
{
|
|
output = Analysis{};
|
|
Index index(chart);
|
|
if (!index.build()) return Status::invalid;
|
|
Analysis result;
|
|
result.states.reserve(index.nodes.size());
|
|
for (std::size_t i = 0; i < index.nodes.size(); ++i) {
|
|
StateInfo info;
|
|
info.state = *index.nodes[i];
|
|
info.path = index.path(i);
|
|
for (std::size_t child = 0; child < index.nodes.size(); ++child) {
|
|
if (index.parents[child] == i) info.children.push_back(index.nodes[child]->id);
|
|
}
|
|
std::size_t leaf = i;
|
|
while (index.initials[leaf] != absent) leaf = index.initials[leaf];
|
|
info.initial_leaf = index.nodes[leaf]->id;
|
|
result.states.push_back(std::move(info));
|
|
}
|
|
result.transitions = chart.transitions;
|
|
std::sort(result.transitions.begin(), result.transitions.end(), transition_order);
|
|
output = std::move(result);
|
|
return Status::ok;
|
|
}
|
|
}
|