Skip to content

Commit 2ec94ea

Browse files
committed
add enum arguments for reduce()
1 parent c71302d commit 2ec94ea

File tree

5 files changed

+38
-102
lines changed

5 files changed

+38
-102
lines changed

include/mata/nfa/algorithms.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Nfa reduce_simulation(const Nfa& nfa, StateRenaming &state_renaming);
148148
* @param[in] direction Direction of the residual construction (values: "forward", "backward").
149149
*/
150150
Nfa reduce_residual(const Nfa& nfa, StateRenaming &state_renaming,
151-
const std::string& type, const std::string& direction);
151+
const std::string& type, ReductionDirection direction);
152152

153153
/**
154154
* @brief Reduce NFA using residual construction.

include/mata/nfa/nfa.hh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,9 @@ Nfa determinize(
788788
const Nfa& aut, std::unordered_map<StateSet, State> *subset_map = nullptr,
789789
std::optional<std::function<bool(const Nfa&, const State, const StateSet&)>> macrostate_discover = std::nullopt);
790790

791+
enum class ReductionAlgorithm { SIMULATION, RESIDUAL_AFTER, RESIDUAL_WITH };
792+
enum class ReductionDirection { FORWARD, BACKWARD };
793+
791794
/**
792795
* @brief Reduce the size of the automaton.
793796
*
@@ -801,7 +804,8 @@ Nfa determinize(
801804
* @return Reduced automaton.
802805
*/
803806
Nfa reduce(const Nfa &aut, StateRenaming *state_renaming = nullptr,
804-
const ParameterMap& params = {{ "algorithm", "simulation" }, { "type", "after" }, { "direction", "forward" } });
807+
ReductionAlgorithm reduction_algorithm = ReductionAlgorithm::SIMULATION,
808+
ReductionDirection direction = ReductionDirection::FORWARD);
805809

806810
/**
807811
* @brief Checks inclusion of languages of two NFAs: @p smaller and @p bigger (smaller <= bigger).

include/mata/nfa/plumbing.hh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ inline void determinize(Nfa* result, const Nfa& aut, std::unordered_map<StateSet
4444
}
4545

4646
inline void reduce(Nfa* result, const Nfa &aut, StateRenaming *state_renaming = nullptr,
47-
const ParameterMap& params = {{ "algorithm", "simulation"}}) {
48-
*result = reduce(aut, state_renaming, params);
47+
ReductionAlgorithm reduction_algorithm = ReductionAlgorithm::SIMULATION,
48+
ReductionDirection direction = ReductionDirection::FORWARD) {
49+
*result = reduce(aut, state_renaming, reduction_algorithm, direction);
4950
}
5051

5152
inline void revert(Nfa* result, const Nfa& aut) { *result = revert(aut); }

src/nfa/operations.cc

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,40 +1073,23 @@ Simlib::Util::BinaryRelation mata::nfa::algorithms::compute_relation(const Nfa&
10731073
}
10741074
}
10751075

1076-
Nfa mata::nfa::reduce(const Nfa &aut, StateRenaming *state_renaming, const ParameterMap& params) {
1077-
if (!haskey(params, "algorithm")) {
1078-
throw std::runtime_error(std::to_string(__func__) +
1079-
" requires setting the \"algorithm\" key in the \"params\" argument; "
1080-
"received: " + std::to_string(params));
1081-
}
1082-
1076+
Nfa mata::nfa::reduce(const Nfa &aut, StateRenaming *state_renaming, ReductionAlgorithm reduction_algorithm, ReductionDirection direction) {
10831077
Nfa result;
10841078
std::unordered_map<State,State> reduced_state_map;
1085-
const std::string& algorithm = params.at("algorithm");
1086-
if ("simulation" == algorithm) {
1087-
result = algorithms::reduce_simulation(aut, reduced_state_map);
1088-
}
1089-
else if ("residual" == algorithm) {
1090-
// reduce type either 'after' or 'with' creation of residual automaton
1091-
if (!haskey(params, "type")) {
1092-
throw std::runtime_error(std::to_string(__func__) +
1093-
" requires setting the \"type\" key in the \"params\" argument; "
1094-
"received: " + std::to_string(params));
1095-
}
1096-
// forward or backward canonical residual automaton
1097-
if (!haskey(params, "direction")) {
1098-
throw std::runtime_error(std::to_string(__func__) +
1099-
" requires setting the \"direction\" key in the \"params\" argument; "
1100-
"received: " + std::to_string(params));
1101-
}
11021079

1103-
const std::string& residual_type = params.at("type");
1104-
const std::string& residual_direction = params.at("direction");
1105-
1106-
result = algorithms::reduce_residual(aut, reduced_state_map, residual_type, residual_direction);
1107-
} else {
1108-
throw std::runtime_error(std::to_string(__func__) +
1109-
" received an unknown value of the \"algorithm\" key: " + algorithm);
1080+
switch(reduction_algorithm) {
1081+
case ReductionAlgorithm::SIMULATION:
1082+
result = algorithms::reduce_simulation(aut, reduced_state_map);
1083+
break;
1084+
case ReductionAlgorithm::RESIDUAL_AFTER:
1085+
result = algorithms::reduce_residual(aut, reduced_state_map, "after", direction);
1086+
break;
1087+
case ReductionAlgorithm::RESIDUAL_WITH:
1088+
result = algorithms::reduce_residual(aut, reduced_state_map, "with", direction);
1089+
break;
1090+
default:
1091+
throw std::runtime_error(std::to_string(__func__) +
1092+
" received an unknown value of the \"reduction_algorithm\" key");
11101093
}
11111094

11121095
if (state_renaming) {
@@ -1617,20 +1600,15 @@ Nfa mata::nfa::algorithms::reduce_simulation(const Nfa& aut, StateRenaming &stat
16171600
return result;
16181601
}
16191602

1620-
Nfa mata::nfa::algorithms::reduce_residual(const Nfa& nfa, StateRenaming &state_renaming, const std::string& type, const std::string& direction) {
1603+
Nfa mata::nfa::algorithms::reduce_residual(const Nfa& nfa, StateRenaming &state_renaming, const std::string& type, ReductionDirection direction) {
16211604
Nfa back_determinized = nfa;
16221605
Nfa result;
16231606

1624-
if (direction != "forward" && direction != "backward"){
1625-
throw std::runtime_error(std::to_string(__func__) +
1626-
" received an unknown value of the \"direction\" key: " + direction);
1627-
}
1628-
16291607
// forward canonical residual automaton is firstly backward determinized and
16301608
// then the residual construction is done forward, for backward residual automaton
16311609
// is it the opposite, so the automaton is reverted once more before and after
16321610
// construction, however the first two reversion negate each other out
1633-
if (direction == "forward")
1611+
if (direction == ReductionDirection::FORWARD)
16341612
back_determinized = revert(back_determinized);
16351613
back_determinized = revert(determinize(back_determinized)); // backward deteminization
16361614

@@ -1653,7 +1631,7 @@ Nfa mata::nfa::algorithms::reduce_residual(const Nfa& nfa, StateRenaming &state_
16531631
" received an unknown value of the \"type\" key: " + type);
16541632
}
16551633

1656-
if (direction == "backward")
1634+
if (direction == ReductionDirection::BACKWARD)
16571635
result = revert(result);
16581636

16591637
return result.trim();

tests/nfa/nfa.cc

Lines changed: 12 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3288,19 +3288,11 @@ TEST_CASE("mata::nfa::algorithms::minimize_hopcroft()") {
32883288
TEST_CASE("mata::nfa::reduce_size_by_residual()") {
32893289
Nfa aut;
32903290
StateRenaming state_renaming;
3291-
ParameterMap params_after, params_with;
3292-
params_after["algorithm"] = "residual";
3293-
params_with["algorithm"] = "residual";
32943291

32953292
SECTION("empty automaton")
32963293
{
3297-
params_after["type"] = "after";
3298-
params_after["direction"] = "forward";
3299-
params_with["type"] = "with";
3300-
params_with["direction"] = "forward";
3301-
3302-
Nfa result_after = reduce(aut, &state_renaming, params_after);
3303-
Nfa result_with = reduce(aut, &state_renaming, params_with);
3294+
Nfa result_after = reduce(aut, &state_renaming, ReductionAlgorithm::RESIDUAL_AFTER);
3295+
Nfa result_with = reduce(aut, &state_renaming, ReductionAlgorithm::RESIDUAL_WITH);
33043296

33053297
REQUIRE(result_after.delta.empty());
33063298
REQUIRE(result_after.initial.empty());
@@ -3311,16 +3303,12 @@ TEST_CASE("mata::nfa::reduce_size_by_residual()") {
33113303

33123304
SECTION("simple automaton")
33133305
{
3314-
params_after["type"] = "after";
3315-
params_after["direction"] = "forward";
3316-
params_with["type"] = "with";
3317-
params_with["direction"] = "forward";
33183306
aut.add_state(2);
33193307
aut.initial.insert(1);
33203308

33213309
aut.final.insert(2);
3322-
Nfa result_after = reduce(aut, &state_renaming, params_after);
3323-
Nfa result_with = reduce(aut, &state_renaming, params_with);
3310+
Nfa result_after = reduce(aut, &state_renaming, ReductionAlgorithm::RESIDUAL_AFTER);
3311+
Nfa result_with = reduce(aut, &state_renaming, ReductionAlgorithm::RESIDUAL_WITH);
33243312

33253313
REQUIRE(result_after.num_of_states() == 0);
33263314
REQUIRE(result_after.initial.empty());
@@ -3330,8 +3318,8 @@ TEST_CASE("mata::nfa::reduce_size_by_residual()") {
33303318
REQUIRE(are_equivalent(aut, result_after));
33313319

33323320
aut.delta.add(1, 'a', 2);
3333-
result_after = reduce(aut, &state_renaming, params_after);
3334-
result_with = reduce(aut, &state_renaming, params_with);
3321+
result_after = reduce(aut, &state_renaming, ReductionAlgorithm::RESIDUAL_AFTER);
3322+
result_with = reduce(aut, &state_renaming, ReductionAlgorithm::RESIDUAL_WITH);
33353323

33363324
REQUIRE(result_after.num_of_states() == 2);
33373325
REQUIRE(result_after.initial[0]);
@@ -3343,10 +3331,6 @@ TEST_CASE("mata::nfa::reduce_size_by_residual()") {
33433331

33443332
SECTION("medium automaton")
33453333
{
3346-
params_after["type"] = "after";
3347-
params_after["direction"] = "forward";
3348-
params_with["type"] = "with";
3349-
params_with["direction"] = "forward";
33503334
aut.add_state(4);
33513335

33523336
aut.initial = { 1 };
@@ -3359,8 +3343,8 @@ TEST_CASE("mata::nfa::reduce_size_by_residual()") {
33593343
aut.delta.add(3, 'a', 3);
33603344
aut.delta.add(2, 'a', 1);
33613345

3362-
Nfa result_after = reduce(aut, &state_renaming, params_after);
3363-
Nfa result_with = reduce(aut, &state_renaming, params_with);
3346+
Nfa result_after = reduce(aut, &state_renaming, ReductionAlgorithm::RESIDUAL_AFTER);
3347+
Nfa result_with = reduce(aut, &state_renaming, ReductionAlgorithm::RESIDUAL_WITH);
33643348

33653349
REQUIRE(result_after.num_of_states() == 4);
33663350
REQUIRE(result_after.initial[0]);
@@ -3380,10 +3364,6 @@ TEST_CASE("mata::nfa::reduce_size_by_residual()") {
33803364

33813365
SECTION("big automaton")
33823366
{
3383-
params_after["type"] = "after";
3384-
params_after["direction"] = "forward";
3385-
params_with["type"] = "with";
3386-
params_with["direction"] = "forward";
33873367
aut.add_state(7);
33883368

33893369
aut.initial = { 0 };
@@ -3423,8 +3403,8 @@ TEST_CASE("mata::nfa::reduce_size_by_residual()") {
34233403
aut.delta.add(6, 'c', 1);
34243404
aut.delta.add(6, 'd', 1);
34253405

3426-
Nfa result_after = reduce(aut, &state_renaming, params_after);
3427-
Nfa result_with = reduce(aut, &state_renaming, params_with);
3406+
Nfa result_after = reduce(aut, &state_renaming, ReductionAlgorithm::RESIDUAL_AFTER);
3407+
Nfa result_with = reduce(aut, &state_renaming, ReductionAlgorithm::RESIDUAL_WITH);
34283408

34293409
REQUIRE(result_after.num_of_states() == 5);
34303410
REQUIRE(result_after.initial[0]);
@@ -3485,11 +3465,6 @@ TEST_CASE("mata::nfa::reduce_size_by_residual()") {
34853465

34863466
SECTION("backward residual big automaton")
34873467
{
3488-
params_after["type"] = "after";
3489-
params_after["direction"] = "backward";
3490-
params_with["type"] = "with";
3491-
params_with["direction"] = "backward";
3492-
34933468
aut.add_state(7);
34943469

34953470
aut.initial = { 0 };
@@ -3529,8 +3504,8 @@ TEST_CASE("mata::nfa::reduce_size_by_residual()") {
35293504
aut.delta.add(6, 'c', 1);
35303505
aut.delta.add(6, 'd', 1);
35313506

3532-
Nfa result_after = reduce(aut, &state_renaming, params_after);
3533-
Nfa result_with = reduce(aut, &state_renaming, params_with);
3507+
Nfa result_after = reduce(aut, &state_renaming, ReductionAlgorithm::RESIDUAL_AFTER, ReductionDirection::BACKWARD);
3508+
Nfa result_with = reduce(aut, &state_renaming, ReductionAlgorithm::RESIDUAL_WITH, ReductionDirection::BACKWARD);
35343509

35353510
REQUIRE(result_after.num_of_states() == 6);
35363511
REQUIRE(result_after.initial[0]);
@@ -3562,28 +3537,6 @@ TEST_CASE("mata::nfa::reduce_size_by_residual()") {
35623537
REQUIRE(are_equivalent(aut, result_after));
35633538

35643539
}
3565-
3566-
SECTION("error checking")
3567-
{
3568-
CHECK_THROWS_WITH(reduce(aut, &state_renaming, params_after),
3569-
Catch::Matchers::ContainsSubstring("requires setting the \"type\" key in the \"params\" argument;"));
3570-
3571-
params_after["type"] = "bad_type";
3572-
CHECK_THROWS_WITH(reduce(aut, &state_renaming, params_after),
3573-
Catch::Matchers::ContainsSubstring("requires setting the \"direction\" key in the \"params\" argument;"));
3574-
3575-
params_after["direction"] = "unknown_direction";
3576-
CHECK_THROWS_WITH(reduce(aut, &state_renaming, params_after),
3577-
Catch::Matchers::ContainsSubstring("received an unknown value of the \"direction\" key"));
3578-
3579-
params_after["direction"] = "forward";
3580-
CHECK_THROWS_WITH(reduce(aut, &state_renaming, params_after),
3581-
Catch::Matchers::ContainsSubstring("received an unknown value of the \"type\" key"));
3582-
3583-
params_after["type"] = "after";
3584-
CHECK_NOTHROW(reduce(aut, &state_renaming, params_after));
3585-
3586-
}
35873540
}
35883541

35893542
TEST_CASE("mata::nfa::union_norename()") {

0 commit comments

Comments
 (0)