Skip to content

Commit

Permalink
before more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
drexlerd committed Aug 14, 2024
1 parent 420bd84 commit 9a0c04d
Show file tree
Hide file tree
Showing 11 changed files with 557 additions and 307 deletions.
6 changes: 3 additions & 3 deletions examples/multiple_problems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ int main()
// Parse the domain
auto domain_parser = loki::DomainParser(std::string(DATA_DIR) + "gripper/domain.pddl");
const auto domain = domain_parser.get_domain();
std::cout << loki::StreamWriter(*domain) << std::endl;
std::cout << *domain << std::endl;

// Parse first problem
const auto problem_parser = loki::ProblemParser(std::string(DATA_DIR) + "gripper/p-2-0.pddl", domain_parser);
const auto problem1 = problem_parser.get_problem();
std::cout << loki::StreamWriter(*problem1) << std::endl;
std::cout << *problem1 << std::endl;

// Parse second problem where the constants are reordered
const auto problem_parser2 = loki::ProblemParser(std::string(DATA_DIR) + "gripper/p-2-1.pddl", domain_parser);
const auto problem2 = problem_parser2.get_problem();
std::cout << loki::StreamWriter(*problem2) << std::endl;
std::cout << *problem2 << std::endl;

/* Both problems are structurally equivalent */
assert(problem1 == problem2);
Expand Down
2 changes: 1 addition & 1 deletion examples/position_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ int main()
// Parse the domain
auto domain_parser = loki::DomainParser(std::string(DATA_DIR) + "gripper/domain.pddl");
const auto domain = domain_parser.get_domain();
std::cout << loki::StreamWriter(*domain) << std::endl << std::endl;
std::cout << *domain << std::endl << std::endl;

const loki::PDDLPositionCache& position_cache = domain_parser.get_position_cache();
const loki::PDDLErrorHandler& error_handler = position_cache.get_error_handler();
Expand Down
4 changes: 2 additions & 2 deletions examples/single_problem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ int main()
// Parse the domain
auto domain_parser = loki::DomainParser(std::string(DATA_DIR) + "gripper/domain.pddl");
const auto domain = domain_parser.get_domain();
std::cout << loki::StreamWriter(*domain) << std::endl;
std::cout << *domain << std::endl;

// Parse the problem
const auto problem_parser = loki::ProblemParser(std::string(DATA_DIR) + "gripper/p-2-0.pddl", domain_parser);
const auto problem = problem_parser.get_problem();
std::cout << loki::StreamWriter(*problem) << std::endl;
std::cout << *problem << std::endl;

return 0;
}
4 changes: 2 additions & 2 deletions examples/undefined_behavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ int main()
// Parse the domain
auto domain_parser = loki::DomainParser(std::string(DATA_DIR) + "gripper/domain.pddl");
domain = domain_parser.get_domain();
std::cout << loki::StreamWriter(*domain) << std::endl;
std::cout << *domain << std::endl;

/* Destructor of DomainParser is called and all domain and problem specific PDDL objects are destroyed. */
}

std::cout << "Bye ;(" << std::endl;

/* Undefined behavior when accessing the domain, usually the program crashes because memory was overwritten */
std::cout << loki::StreamWriter(*domain) << std::endl;
std::cout << *domain << std::endl;

return 0;
}
2 changes: 1 addition & 1 deletion exe/domain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int main(int argc, char** argv)
// 1. Parse the domain
const auto domain_parser = loki::DomainParser(domain_file);
const auto domain = domain_parser.get_domain();
std::cout << loki::StreamWriter(*domain) << std::endl;
std::cout << *domain << std::endl;

return 0;
}
4 changes: 2 additions & 2 deletions exe/problem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ int main(int argc, char** argv)
// 1. Parse the domain
auto domain_parser = loki::DomainParser(domain_file);
const auto domain = domain_parser.get_domain();
std::cout << loki::StreamWriter(*domain) << std::endl;
std::cout << *domain << std::endl;

// 2. Parse the problem
const auto problem_parser = loki::ProblemParser(problem_file, domain_parser);
const auto problem = problem_parser.get_problem();
std::cout << loki::StreamWriter(*problem) << std::endl;
std::cout << *problem << std::endl;

return 0;
}
8 changes: 8 additions & 0 deletions include/loki/details/ast/printer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
namespace loki
{

struct DefaultFormatterOptions
{
// The indentation in the current level.
int indent = 0;
// The amount of indentation added per nesting
int add_indent = 0;
};

/**
* Domain
*/
Expand Down
155 changes: 82 additions & 73 deletions include/loki/details/pddl/formatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,94 +20,103 @@

#include "loki/details/pddl/declarations.hpp"

#include <concepts>
#include <cstddef>
#include <ostream>
#include <sstream>
#include <type_traits>

namespace loki
{

struct DefaultFormatterOptions
class PDDLFormatter
{
private:
// The indentation in the current level.
int indent = 0;
size_t m_indent = 0;
// The amount of indentation added per nesting
int add_indent = 0;
};

class DefaultFormatter
{
public:
void write(const ActionImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const AtomImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const AxiomImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const ConditionLiteralImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const ConditionAndImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const ConditionOrImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const ConditionNotImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const ConditionImplyImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const ConditionExistsImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const ConditionForallImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const ConditionImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const DomainImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const EffectLiteralImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const EffectAndImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const EffectNumericImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const EffectConditionalForallImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const EffectConditionalWhenImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const EffectImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const FunctionExpressionNumberImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const FunctionExpressionBinaryOperatorImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const FunctionExpressionMultiOperatorImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const FunctionExpressionMinusImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const FunctionExpressionFunctionImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const FunctionExpressionImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const FunctionSkeletonImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const FunctionImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const LiteralImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const OptimizationMetricImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const NumericFluentImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const ObjectImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const ParameterImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const PredicateImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const ProblemImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const RequirementsImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const TermObjectImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const TermVariableImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const TermImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const TypeImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
void write(const VariableImpl& element, const DefaultFormatterOptions& options, std::ostream& out) const;
};

/// @brief `StreamWriter` is a utility class to write to a stream using `operator<<`.
template<typename T, typename Formatter = DefaultFormatter, typename FormatterOptions = DefaultFormatterOptions>
requires std::is_default_constructible_v<Formatter> && std::is_default_constructible_v<FormatterOptions>
class StreamWriter
{
private:
const T& m_element;
const Formatter& m_formatter;
FormatterOptions m_options;
size_t m_add_indent = 0;

public:
StreamWriter(const T& element, const Formatter& formatter = Formatter(), const FormatterOptions& options = FormatterOptions());
PDDLFormatter(size_t indent = 0, size_t add_indent = 4);

std::string str() const;

const T& get_element() const;
const Formatter& get_formatter() const;
const FormatterOptions& get_options() const;
void write(const ActionImpl& element, std::ostream& out);
void write(const AtomImpl& element, std::ostream& out);
void write(const AxiomImpl& element, std::ostream& out);
void write(const ConditionLiteralImpl& element, std::ostream& out);
void write(const ConditionAndImpl& element, std::ostream& out);
void write(const ConditionOrImpl& element, std::ostream& out);
void write(const ConditionNotImpl& element, std::ostream& out);
void write(const ConditionImplyImpl& element, std::ostream& out);
void write(const ConditionExistsImpl& element, std::ostream& out);
void write(const ConditionForallImpl& element, std::ostream& out);
void write(const ConditionImpl& element, std::ostream& out);
void write(const DomainImpl& element, std::ostream& out);
void write(const EffectLiteralImpl& element, std::ostream& out);
void write(const EffectAndImpl& element, std::ostream& out);
void write(const EffectNumericImpl& element, std::ostream& out);
void write(const EffectConditionalForallImpl& element, std::ostream& out);
void write(const EffectConditionalWhenImpl& element, std::ostream& out);
void write(const EffectImpl& element, std::ostream& out);
void write(const FunctionExpressionNumberImpl& element, std::ostream& out);
void write(const FunctionExpressionBinaryOperatorImpl& element, std::ostream& out);
void write(const FunctionExpressionMultiOperatorImpl& element, std::ostream& out);
void write(const FunctionExpressionMinusImpl& element, std::ostream& out);
void write(const FunctionExpressionFunctionImpl& element, std::ostream& out);
void write(const FunctionExpressionImpl& element, std::ostream& out);
void write(const FunctionSkeletonImpl& element, std::ostream& out);
void write(const FunctionImpl& element, std::ostream& out);
void write(const LiteralImpl& element, std::ostream& out);
void write(const OptimizationMetricImpl& element, std::ostream& out);
void write(const NumericFluentImpl& element, std::ostream& out);
void write(const ObjectImpl& element, std::ostream& out);
void write(const ParameterImpl& element, std::ostream& out);
void write(const PredicateImpl& element, std::ostream& out);
void write(const ProblemImpl& element, std::ostream& out);
void write(const RequirementsImpl& element, std::ostream& out);
void write(const TermObjectImpl& element, std::ostream& out);
void write(const TermVariableImpl& element, std::ostream& out);
void write(const TermImpl& element, std::ostream& out);
void write(const TypeImpl& element, std::ostream& out);
void write(const VariableImpl& element, std::ostream& out);
};

/// @brief Write to a stream.
template<typename T, typename Formatter, typename FormatterOptions>
std::ostream& operator<<(std::ostream& out, const StreamWriter<T, Formatter, FormatterOptions>& writer)
{
writer.get_formatter().write(writer.get_element(), writer.get_options(), out);
return out;
}
extern std::ostream& operator<<(std::ostream& out, const ActionImpl& element);
extern std::ostream& operator<<(std::ostream& out, const AtomImpl& element);
extern std::ostream& operator<<(std::ostream& out, const AxiomImpl& element);
extern std::ostream& operator<<(std::ostream& out, const ConditionLiteralImpl& element);
extern std::ostream& operator<<(std::ostream& out, const ConditionAndImpl& element);
extern std::ostream& operator<<(std::ostream& out, const ConditionOrImpl& element);
extern std::ostream& operator<<(std::ostream& out, const ConditionNotImpl& element);
extern std::ostream& operator<<(std::ostream& out, const ConditionImplyImpl& element);
extern std::ostream& operator<<(std::ostream& out, const ConditionExistsImpl& element);
extern std::ostream& operator<<(std::ostream& out, const ConditionForallImpl& element);
extern std::ostream& operator<<(std::ostream& out, const ConditionImpl& element);
extern std::ostream& operator<<(std::ostream& out, const DomainImpl& element);
extern std::ostream& operator<<(std::ostream& out, const EffectLiteralImpl& element);
extern std::ostream& operator<<(std::ostream& out, const EffectAndImpl& element);
extern std::ostream& operator<<(std::ostream& out, const EffectNumericImpl& element);
extern std::ostream& operator<<(std::ostream& out, const EffectConditionalForallImpl& element);
extern std::ostream& operator<<(std::ostream& out, const EffectConditionalWhenImpl& element);
extern std::ostream& operator<<(std::ostream& out, const EffectImpl& element);
extern std::ostream& operator<<(std::ostream& out, const FunctionExpressionNumberImpl& element);
extern std::ostream& operator<<(std::ostream& out, const FunctionExpressionBinaryOperatorImpl& element);
extern std::ostream& operator<<(std::ostream& out, const FunctionExpressionMultiOperatorImpl& element);
extern std::ostream& operator<<(std::ostream& out, const FunctionExpressionMinusImpl& element);
extern std::ostream& operator<<(std::ostream& out, const FunctionExpressionFunctionImpl& element);
extern std::ostream& operator<<(std::ostream& out, const FunctionExpressionImpl& element);
extern std::ostream& operator<<(std::ostream& out, const FunctionSkeletonImpl& element);
extern std::ostream& operator<<(std::ostream& out, const FunctionImpl& element);
extern std::ostream& operator<<(std::ostream& out, const LiteralImpl& element);
extern std::ostream& operator<<(std::ostream& out, const OptimizationMetricImpl& element);
extern std::ostream& operator<<(std::ostream& out, const NumericFluentImpl& element);
extern std::ostream& operator<<(std::ostream& out, const ParameterImpl& element);
extern std::ostream& operator<<(std::ostream& out, const PredicateImpl& element);
extern std::ostream& operator<<(std::ostream& out, const ProblemImpl& element);
extern std::ostream& operator<<(std::ostream& out, const RequirementsImpl& element);
extern std::ostream& operator<<(std::ostream& out, const TermObjectImpl& element);
extern std::ostream& operator<<(std::ostream& out, const TermVariableImpl& element);
extern std::ostream& operator<<(std::ostream& out, const TermImpl& element);
extern std::ostream& operator<<(std::ostream& out, const TypeImpl& element);
extern std::ostream& operator<<(std::ostream& out, const VariableImpl& element);

}

Expand Down
Loading

0 comments on commit 9a0c04d

Please sign in to comment.