Skip to content

Commit

Permalink
added optional filepath to domain and problem
Browse files Browse the repository at this point in the history
  • Loading branch information
drexlerd committed Jul 8, 2024
1 parent b2079cd commit d70a7f9
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 28 deletions.
4 changes: 2 additions & 2 deletions include/loki/details/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace loki
class DomainParser
{
private:
fs::path m_file_path;
fs::path m_filepath;
// We need to keep the source in memory for error reporting.
std::string m_source;

Expand Down Expand Up @@ -64,7 +64,7 @@ class DomainParser
class ProblemParser
{
private:
fs::path m_file_path;
fs::path m_filepath;
// We need to keep the source in memory for error reporting.
std::string m_source;

Expand Down
5 changes: 5 additions & 0 deletions include/loki/details/pddl/domain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#include "loki/details/pddl/declarations.hpp"
#include "loki/details/pddl/factory.hpp"
#include "loki/details/pddl/requirements.hpp"
#include "loki/details/utils/filesystem.hpp"

#include <optional>
#include <string>

namespace loki
Expand All @@ -31,6 +33,7 @@ namespace loki
class DomainImpl : public Base<DomainImpl>
{
private:
std::optional<fs::path> m_filepath;
std::string m_name;
Requirements m_requirements;
TypeList m_types;
Expand All @@ -41,6 +44,7 @@ class DomainImpl : public Base<DomainImpl>
AxiomList m_axioms;

DomainImpl(size_t identifier,
std::optional<fs::path> filepath,
std::string name,
Requirements requirements,
TypeList types,
Expand All @@ -62,6 +66,7 @@ class DomainImpl : public Base<DomainImpl>
friend class Base<DomainImpl>;

public:
const std::optional<fs::path>& get_filepath() const;
const std::string& get_name() const;
const Requirements& get_requirements() const;
const TypeList& get_types() const;
Expand Down
12 changes: 8 additions & 4 deletions include/loki/details/pddl/factories.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ class PDDLFactories
return m_factories.get<NumericFluentImpl>().get_or_create<NumericFluentImpl>(std::move(function), std::move(number));
}

Domain get_or_create_domain(std::string name,
Domain get_or_create_domain(std::optional<fs::path> filepath,
std::string name,
Requirements requirements,
TypeList types,
ObjectList constants,
Expand All @@ -267,7 +268,8 @@ class PDDLFactories
ActionList actions,
AxiomList axioms)
{
return m_factories.get<DomainImpl>().get_or_create<DomainImpl>(std::move(name),
return m_factories.get<DomainImpl>().get_or_create<DomainImpl>(std::move(filepath),
std::move(name),
std::move(requirements),
std::move(types),
std::move(constants),
Expand All @@ -277,7 +279,8 @@ class PDDLFactories
std::move(axioms));
}

Problem get_or_create_problem(Domain domain,
Problem get_or_create_problem(std::optional<fs::path> filepath,
Domain domain,
std::string name,
Requirements requirements,
ObjectList objects,
Expand All @@ -288,7 +291,8 @@ class PDDLFactories
std::optional<OptimizationMetric> optimization_metric,
AxiomList axioms)
{
return m_factories.get<ProblemImpl>().get_or_create<ProblemImpl>(std::move(domain),
return m_factories.get<ProblemImpl>().get_or_create<ProblemImpl>(std::move(filepath),
std::move(domain),
std::move(name),
std::move(requirements),
std::move(objects),
Expand Down
2 changes: 1 addition & 1 deletion include/loki/details/pddl/factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class PDDLFactory
}

public:
explicit PDDLFactory(size_t initial_num_element_per_segment = 16, size_t maximum_num_elements_per_segment = 16 * 1024) :
PDDLFactory(size_t initial_num_element_per_segment = 16, size_t maximum_num_elements_per_segment = 16 * 1024) :
m_persistent_vector(SegmentedVector<HolderType>(initial_num_element_per_segment, maximum_num_elements_per_segment))
{
}
Expand Down
4 changes: 2 additions & 2 deletions include/loki/details/pddl/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
namespace loki
{

extern Domain parse(const ast::Domain& domain_node, Context& context);
extern Problem parse(const ast::Problem& problem_node, Context& context, const Domain& domain);
extern Domain parse(const fs::path& filepath, const ast::Domain& domain_node, Context& context);
extern Problem parse(const fs::path& filepath, const ast::Problem& problem_node, Context& context, const Domain& domain);

}

Expand Down
4 changes: 4 additions & 0 deletions include/loki/details/pddl/problem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "loki/details/pddl/base.hpp"
#include "loki/details/pddl/declarations.hpp"
#include "loki/details/pddl/factory.hpp"
#include "loki/details/utils/filesystem.hpp"

#include <optional>
#include <string>
Expand All @@ -30,6 +31,7 @@ namespace loki
class ProblemImpl : public Base<ProblemImpl>
{
private:
std::optional<fs::path> m_filepath;
Domain m_domain;
std::string m_name;
Requirements m_requirements;
Expand All @@ -42,6 +44,7 @@ class ProblemImpl : public Base<ProblemImpl>
AxiomList m_axioms;

ProblemImpl(size_t identifier,
std::optional<fs::path> filepath,
Domain domain,
std::string name,
Requirements requirements,
Expand All @@ -65,6 +68,7 @@ class ProblemImpl : public Base<ProblemImpl>
friend class Base<ProblemImpl>;

public:
const std::optional<fs::path>& get_filepath() const;
const Domain& get_domain() const;
const std::string& get_name() const;
const Requirements& get_requirements() const;
Expand Down
5 changes: 4 additions & 1 deletion include/loki/details/utils/segmented_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class SegmentedVector

void increase_capacity()
{
assert(m_num_element_per_segment > 0);

// Use doubling strategy to make future insertions cheaper.
m_num_element_per_segment = std::min(2 * m_num_element_per_segment, m_maximum_num_elements_per_segment);

Expand All @@ -68,9 +70,10 @@ class SegmentedVector
}

public:
explicit SegmentedVector(size_t initial_num_element_per_segment = 16, size_t maximum_num_elements_per_segment = 16 * 1024) :
SegmentedVector(size_t initial_num_element_per_segment = 16, size_t maximum_num_elements_per_segment = 16 * 1024) :
m_num_element_per_segment(initial_num_element_per_segment),
m_maximum_num_elements_per_segment(maximum_num_elements_per_segment),
m_segments(),
m_size(0),
m_capacity(0)
{
Expand Down
28 changes: 14 additions & 14 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,28 @@
namespace loki
{

DomainParser::DomainParser(const fs::path& file_path, bool strict, bool quiet) :
m_file_path(file_path),
m_source(loki::read_file(file_path)),
DomainParser::DomainParser(const fs::path& filepath, bool strict, bool quiet) :
m_filepath(filepath),
m_source(loki::read_file(filepath)),
m_position_cache(nullptr),
m_scopes(nullptr)
{
const auto start = std::chrono::high_resolution_clock::now();
if (!quiet)
{
std::cout << "Started parsing domain file: " << file_path << std::endl;
std::cout << "Started parsing domain file: " << filepath << std::endl;
}

/* Parse the AST */
auto node = ast::Domain();
auto x3_error_handler = X3ErrorHandler(m_source.begin(), m_source.end(), file_path);
auto x3_error_handler = X3ErrorHandler(m_source.begin(), m_source.end(), filepath);
bool success = parse_ast(m_source, domain(), node, x3_error_handler.get_error_handler());
if (!success)
{
throw SyntaxParserError("", x3_error_handler.get_error_stream().str());
}

m_position_cache = std::make_unique<PDDLPositionCache>(x3_error_handler, file_path);
m_position_cache = std::make_unique<PDDLPositionCache>(x3_error_handler, filepath);
m_scopes = std::make_unique<ScopeStack>(m_position_cache->get_error_handler());

auto context = Context(m_factories, *m_position_cache, *m_scopes, strict, quiet);
Expand All @@ -79,7 +79,7 @@ DomainParser::DomainParser(const fs::path& file_path, bool strict, bool quiet) :
const auto equal_predicate = context.factories.get_or_create_predicate("=", binary_parameterlist);
context.scopes.top().insert_predicate("=", equal_predicate, {});

m_domain = parse(node, context);
m_domain = parse(filepath, node, context);

// Only the global scope remains
assert(context.scopes.get_stack().size() == 1);
Expand All @@ -101,36 +101,36 @@ const PDDLPositionCache& DomainParser::get_position_cache() const { return *m_po

const Domain& DomainParser::get_domain() const { return m_domain; }

ProblemParser::ProblemParser(const fs::path& file_path, DomainParser& domain_parser, bool strict, bool quiet) :
m_file_path(file_path),
m_source(loki::read_file(file_path)),
ProblemParser::ProblemParser(const fs::path& filepath, DomainParser& domain_parser, bool strict, bool quiet) :
m_filepath(filepath),
m_source(loki::read_file(filepath)),
m_position_cache(nullptr),
m_scopes(nullptr)
{
const auto start = std::chrono::high_resolution_clock::now();
if (!quiet)
{
std::cout << "Started parsing problem file: " << file_path << std::endl;
std::cout << "Started parsing problem file: " << filepath << std::endl;
}

/* Parse the AST */
auto problem_node = ast::Problem();
auto x3_error_handler = X3ErrorHandler(m_source.begin(), m_source.end(), file_path);
auto x3_error_handler = X3ErrorHandler(m_source.begin(), m_source.end(), filepath);
bool success = parse_ast(m_source, problem(), problem_node, x3_error_handler.get_error_handler());
if (!success)
{
throw SyntaxParserError("", x3_error_handler.get_error_stream().str());
}

m_position_cache = std::make_unique<PDDLPositionCache>(x3_error_handler, file_path);
m_position_cache = std::make_unique<PDDLPositionCache>(x3_error_handler, filepath);
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, quiet);

// Initialize global scope
context.scopes.open_scope();

m_problem = parse(problem_node, context, domain_parser.get_domain());
m_problem = parse(filepath, problem_node, context, domain_parser.get_domain());

// Only the global scope remains
assert(context.scopes.get_stack().size() == 1);
Expand Down
4 changes: 4 additions & 0 deletions src/pddl/domain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ using namespace std;
namespace loki
{
DomainImpl::DomainImpl(size_t identifier,
std::optional<fs::path> filepath,
std::string name,
Requirements requirements,
TypeList types,
Expand All @@ -42,6 +43,7 @@ DomainImpl::DomainImpl(size_t identifier,
ActionList actions,
AxiomList axioms) :
Base(identifier),
m_filepath(std::move(filepath)),
m_name(std::move(name)),
m_requirements(std::move(requirements)),
m_types(std::move(types)),
Expand Down Expand Up @@ -207,6 +209,8 @@ std::ostream& operator<<(std::ostream& os, const DomainImpl& domain)
return os;
}

const std::optional<fs::path>& DomainImpl::get_filepath() const { return m_filepath; }

const std::string& DomainImpl::get_name() const { return m_name; }

const Requirements& DomainImpl::get_requirements() const { return m_requirements; }
Expand Down
9 changes: 5 additions & 4 deletions src/pddl/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ using namespace std;
namespace loki
{

Domain parse(const ast::Domain& domain_node, Context& context)
Domain parse(const fs::path& filepath, const ast::Domain& domain_node, Context& context)
{
const auto domain_name = parse(domain_node.domain_name.name);
/* Requirements section */
Expand Down Expand Up @@ -105,12 +105,12 @@ Domain parse(const ast::Domain& domain_node, Context& context)
test_function_skeleton_references(function_skeletons, context);

const auto domain =
context.factories.get_or_create_domain(domain_name, requirements, types, constants, predicates, function_skeletons, action_list, axiom_list);
context.factories.get_or_create_domain(filepath, domain_name, requirements, types, constants, predicates, function_skeletons, action_list, axiom_list);
context.positions.push_back(domain, domain_node);
return domain;
}

Problem parse(const ast::Problem& problem_node, Context& context, const Domain& domain)
Problem parse(const fs::path& filepath, const ast::Problem& problem_node, Context& context, const Domain& domain)
{
/* Domain name section */
const auto domain_name = parse(problem_node.domain_name.name);
Expand Down Expand Up @@ -198,7 +198,8 @@ Problem parse(const ast::Problem& problem_node, Context& context, const Domain&
}
}

const auto problem = context.factories.get_or_create_problem(domain,
const auto problem = context.factories.get_or_create_problem(filepath,
domain,
problem_name,
requirements,
objects,
Expand Down
4 changes: 4 additions & 0 deletions src/pddl/problem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ using namespace std;
namespace loki
{
ProblemImpl::ProblemImpl(size_t identifier,
std::optional<fs::path> filepath,
Domain domain,
std::string name,
Requirements requirements,
Expand All @@ -49,6 +50,7 @@ ProblemImpl::ProblemImpl(size_t identifier,
std::optional<OptimizationMetric> optimization_metric,
AxiomList axioms) :
Base(identifier),
m_filepath(std::move(filepath)),
m_domain(std::move(domain)),
m_name(std::move(name)),
m_requirements(std::move(requirements)),
Expand Down Expand Up @@ -205,6 +207,8 @@ std::ostream& operator<<(std::ostream& os, const ProblemImpl& problem)
return os;
}

const std::optional<fs::path>& ProblemImpl::get_filepath() const { return m_filepath; }

const Domain& ProblemImpl::get_domain() const { return m_domain; }

const std::string& ProblemImpl::get_name() const { return m_name; }
Expand Down

0 comments on commit d70a7f9

Please sign in to comment.