Skip to content

Commit

Permalink
remove unused action parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
drexlerd committed May 27, 2024
1 parent 6470401 commit bcd5cc5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
5 changes: 4 additions & 1 deletion include/loki/details/pddl/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,11 +46,12 @@ struct Context
Requirements requirements;
std::unordered_set<Predicate> 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)
Expand Down
4 changes: 2 additions & 2 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ DomainParser::DomainParser(const fs::path& file_path, bool strict, bool quiet) :
m_position_cache = std::make_unique<PDDLPositionCache>(x3_error_handler, file_path);
m_scopes = std::make_unique<ScopeStack>(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();

Expand Down Expand Up @@ -125,7 +125,7 @@ ProblemParser::ProblemParser(const fs::path& file_path, DomainParser& domain_par
m_position_cache = std::make_unique<PDDLPositionCache>(x3_error_handler, file_path);
m_scopes = std::make_unique<ScopeStack>(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();
Expand Down
19 changes: 18 additions & 1 deletion src/pddl/parser/structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit bcd5cc5

Please sign in to comment.