diff --git a/include/loki/details/pddl/context.hpp b/include/loki/details/pddl/context.hpp index b1fc1eeb..8a4e7c34 100644 --- a/include/loki/details/pddl/context.hpp +++ b/include/loki/details/pddl/context.hpp @@ -36,6 +36,8 @@ struct Context ScopeStack& scopes; // For strict error checking bool strict; + // For printing warnings + bool quiet; // Toggle error checks during parsing bool allow_free_variables; // For checking that certain PDDL objects were referenced at least once @@ -44,11 +46,12 @@ struct Context Requirements requirements; std::unordered_set derived_predicates; - Context(PDDLFactories& factories_, PDDLPositionCache& positions_, ScopeStack& scopes_, bool strict_ = false) : + Context(PDDLFactories& factories_, PDDLPositionCache& positions_, ScopeStack& scopes_, bool strict_ = false, bool quiet_ = true) : factories(factories_), positions(positions_), scopes(scopes_), strict(strict_), + quiet(quiet_), allow_free_variables(false), references(ReferencedPDDLObjects()), requirements(nullptr) diff --git a/src/parser.cpp b/src/parser.cpp index b8f9d5d8..baf1d55a 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -60,7 +60,7 @@ DomainParser::DomainParser(const fs::path& file_path, bool strict, bool quiet) : m_position_cache = std::make_unique(x3_error_handler, file_path); m_scopes = std::make_unique(m_position_cache->get_error_handler()); - auto context = Context(m_factories, *m_position_cache, *m_scopes, strict); + auto context = Context(m_factories, *m_position_cache, *m_scopes, strict, quiet); // Initialize global scope context.scopes.open_scope(); @@ -125,7 +125,7 @@ ProblemParser::ProblemParser(const fs::path& file_path, DomainParser& domain_par m_position_cache = std::make_unique(x3_error_handler, file_path); m_scopes = std::make_unique(m_position_cache->get_error_handler(), domain_parser.m_scopes.get()); - auto context = Context(domain_parser.m_factories, *m_position_cache, *m_scopes, strict); + auto context = Context(domain_parser.m_factories, *m_position_cache, *m_scopes, strict, quiet); // Initialize global scope context.scopes.open_scope(); diff --git a/src/pddl/parser/structure.cpp b/src/pddl/parser/structure.cpp index e283d694..2b0db71a 100644 --- a/src/pddl/parser/structure.cpp +++ b/src/pddl/parser/structure.cpp @@ -54,8 +54,25 @@ Action parse(const ast::Action& node, Context& context) track_variable_references(parameter_list, context); auto [condition, effect] = parse(node.action_body, context); test_variable_references(parameter_list, context); + // Remove unused action parameters + auto used_parameters = ParameterList {}; + for (const auto& parameter : parameter_list) + { + if (!context.references.exists(parameter->get_variable())) + { + used_parameters.push_back(parameter); + } + else + { + if (!context.quiet) + { + std::cout << "Removed unused parameter " << *parameter << " from action " << name << std::endl; + } + } + } context.scopes.close_scope(); - const auto action = context.factories.get_or_create_action(name, parameter_list.size(), parameter_list, condition, effect); + + const auto action = context.factories.get_or_create_action(name, used_parameters.size(), used_parameters, condition, effect); context.positions.push_back(action, node); return action; }