Skip to content

Commit

Permalink
[Issue 1146] Simplify InputFileParser, fix usage in RootTask.
Browse files Browse the repository at this point in the history
  • Loading branch information
tanjaschindler committed Jul 18, 2024
1 parent 5c189e7 commit 53c5096
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 185 deletions.
93 changes: 40 additions & 53 deletions src/search/tasks/root_task.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,43 +141,31 @@ static void check_facts(const ExplicitOperator &action, const vector<ExplicitVar
}
}

static vector<FactPair> read_facts(utils::InputFileParser &in) {
int count = in.parse_single_token_line().parse_int("number of conditions");
static vector<FactPair> read_facts(utils::InputFileParser &in, bool read_from_single_line) {
int count = read_from_single_line ? in.read_int("number of conditions")
: in.read_line_int("number of conditions");
vector<FactPair> conditions;
conditions.reserve(count);
for (int i = 0; i < count; ++i) {
utils::InputFileLineParser line = in.parse_line();
FactPair condition = FactPair::no_fact;
condition.var = line.read_token().parse_int("condition variable");
condition.value = line.read_token().parse_int("condition value");
line.check_last_token();
conditions.push_back(condition);
}
return conditions;
}

static vector<FactPair> read_facts(utils::InputFileLineParser &line) {
int count = line.read_token().parse_int("number of conditions");
vector<FactPair> conditions;
conditions.reserve(count);
for (int i = 0; i < count; ++i) {
FactPair condition = FactPair::no_fact;
condition.var = line.read_token().parse_int("condition variable");
condition.value = line.read_token().parse_int("condition value");
condition.var = in.read_int("condition variable");
condition.value = in.read_int("condition value");
if (!read_from_single_line) {
in.confirm_end_of_line();
}
conditions.push_back(condition);
}
return conditions;
}

ExplicitVariable::ExplicitVariable(utils::InputFileParser &in) {
in.read_magic_line("begin_variable");
name = in.read_line().get_line();
axiom_layer = in.parse_single_token_line().parse_int("variable axiom layer");
domain_size = in.parse_single_token_line().parse_int("variable domain size");
// in >> ws;
name = in.read_line("variable name");
axiom_layer = in.read_line_int("variable axiom layer");
domain_size = in.read_line_int("variable domain size");
fact_names.resize(domain_size);
for (int i = 0; i < domain_size; ++i)
fact_names[i] = in.read_line().get_line();
fact_names[i] = in.read_line("fact name");
in.read_magic_line("end_variable");
}

Expand All @@ -188,24 +176,23 @@ ExplicitEffect::ExplicitEffect(
}

void ExplicitOperator::read_pre_post(utils::InputFileParser &in) {
utils::InputFileLineParser line = in.parse_line();
vector<FactPair> conditions = read_facts(line);
int var = line.read_token().parse_int("variable affected by effect");
int value_pre = line.read_token().parse_int("variable value precondition");
int value_post = line.read_token().parse_int("variable value postcondition");
line.check_last_token();
vector<FactPair> conditions = read_facts(in, true);
int var = in.read_int("variable affected by effect");
int value_pre = in.read_int("variable value precondition");
int value_post = in.read_int("variable value postcondition");
if (value_pre != -1) {
preconditions.emplace_back(var, value_pre);
}
in.confirm_end_of_line();
effects.emplace_back(var, value_post, move(conditions));
}

void ExplicitOperator::read_axiom(utils::InputFileParser &in) {
vector<FactPair> conditions = read_facts(in);
utils::InputFileLineParser line = in.parse_line();
int var = line.read_token().parse_int("variable affected by axiom");
int value_pre = line.read_token().parse_int("variable value precondition");
int value_post = line.read_token().parse_int("variable value postcondition");
vector<FactPair> conditions = read_facts(in, false);
int var = in.read_int("variable affected by axiom");
int value_pre = in.read_int("variable value precondition");
int value_post = in.read_int("variable value postcondition");
in.confirm_end_of_line();
if (value_pre != -1) {
preconditions.emplace_back(var, value_pre);
}
Expand All @@ -216,14 +203,14 @@ ExplicitOperator::ExplicitOperator(utils::InputFileParser &in, bool is_an_axiom,
: is_an_axiom(is_an_axiom) {
if (!is_an_axiom) {
in.read_magic_line("begin_operator");
name = in.read_line().get_line();
preconditions = read_facts(in);
int count = in.parse_single_token_line().parse_int("number of operator effects");
name = in.read_line("operator name");
preconditions = read_facts(in, false);
int count = in.read_line_int("number of operator effects");
effects.reserve(count);
for (int i = 0; i < count; ++i) {
read_pre_post(in);
}
int op_cost = in.parse_single_token_line().parse_int("operator cost");
int op_cost = in.read_line_int("operator cost");
cost = use_metric ? op_cost : 1;
in.read_magic_line("end_operator");
} else {
Expand All @@ -239,7 +226,7 @@ ExplicitOperator::ExplicitOperator(utils::InputFileParser &in, bool is_an_axiom,
static void read_and_verify_version(utils::InputFileParser &in) {
in.set_context("version section");
in.read_magic_line("begin_version");
int version = in.parse_line().read_token().parse_int("version number");
int version = in.read_line_int("version number");
if (version != PRE_FILE_VERSION) {
cerr << "Expected translator output file version " << PRE_FILE_VERSION
<< ", got " << version << "." << endl
Expand All @@ -252,7 +239,7 @@ static void read_and_verify_version(utils::InputFileParser &in) {
static bool read_metric(utils::InputFileParser &in) {
in.set_context("metric_section");
in.read_magic_line("begin_metric");
string use_metric_string = in.read_line().get_line();
string use_metric_string = in.read_line("use metric");
bool use_metric = false;
if (use_metric_string == "1") {
use_metric = true;
Expand All @@ -267,7 +254,7 @@ static bool read_metric(utils::InputFileParser &in) {

static vector<ExplicitVariable> read_variables(utils::InputFileParser &in) {
in.set_context("variable_section");
int count = in.parse_single_token_line().parse_int("variable count");
int count = in.read_line_int("variable count");
vector<ExplicitVariable> variables;
variables.reserve(count);
for (int i = 0; i < count; ++i) {
Expand All @@ -282,7 +269,7 @@ static vector<vector<set<FactPair>>> read_mutexes(utils::InputFileParser &in, co
for (size_t i = 0; i < variables.size(); ++i)
inconsistent_facts[i].resize(variables[i].domain_size);

int num_mutex_groups = in.parse_single_token_line().parse_int("number of mutex groups");
int num_mutex_groups = in.read_line_int("number of mutex groups");

/*
NOTE: Mutex groups can overlap, in which case the same mutex
Expand All @@ -293,14 +280,13 @@ static vector<vector<set<FactPair>>> read_mutexes(utils::InputFileParser &in, co
*/
for (int i = 0; i < num_mutex_groups; ++i) {
in.read_magic_line("begin_mutex_group");
int num_facts = in.parse_single_token_line().parse_int("number of facts in mutex group");
int num_facts = in.read_line_int("number of facts in mutex group");
vector<FactPair> invariant_group;
invariant_group.reserve(num_facts);
for (int j = 0; j < num_facts; ++j) {
utils::InputFileLineParser line = in.parse_line();
int var = line.read_token().parse_int("variable number of mutex atom");
int value = line.read_token().parse_int("value of mutex atom");
line.check_last_token();
int var = in.read_int("variable number of mutex atom");
int value = in.read_int("value of mutex atom");
in.confirm_end_of_line();
invariant_group.emplace_back(var, value);
}
in.read_magic_line("end_mutex_group");
Expand Down Expand Up @@ -328,7 +314,7 @@ static vector<vector<set<FactPair>>> read_mutexes(utils::InputFileParser &in, co
static vector<FactPair> read_goal(utils::InputFileParser &in) {
in.set_context("goal section");
in.read_magic_line("begin_goal");
vector<FactPair> goals = read_facts(in);
vector<FactPair> goals = read_facts(in, false);
in.read_magic_line("end_goal");
if (goals.empty()) {
cerr << "Task has no goal condition!" << endl;
Expand All @@ -341,7 +327,7 @@ static vector<ExplicitOperator> read_actions(
utils::InputFileParser &in, bool is_axiom, bool use_metric,
const vector<ExplicitVariable> &variables) {
in.set_context(is_axiom ? "axiom section" : "operator section");
int count = in.parse_single_token_line().parse_int(is_axiom ? "number of axioms" : "number of operators");
int count = in.read_line_int(is_axiom ? "number of axioms" : "number of operators");
vector<ExplicitOperator> actions;
actions.reserve(count);
for (int i = 0; i < count; ++i) {
Expand All @@ -359,8 +345,8 @@ RootTask::RootTask(istream &in) {
int num_variables = variables.size();

mutexes = read_mutexes(file, variables);
for (int i = 0; i < mutexes.size(); ++i) {
for (int j = 0; j < mutexes[i].size(); ++j) {
for (size_t i = 0; i < mutexes.size(); ++i) {
for (size_t j = 0; j < mutexes[i].size(); ++j) {
check_facts(mutexes[i][j], variables);
}
}
Expand All @@ -370,7 +356,7 @@ RootTask::RootTask(istream &in) {
initial_state_values.resize(num_variables);
file.read_magic_line("begin_state");
for (int i = 0; i < num_variables; ++i) {
initial_state_values[i] = file.parse_single_token_line().parse_int("initial state variable value");
initial_state_values[i] = file.read_line_int("initial state variable value");
}
file.read_magic_line("end_state");

Expand All @@ -383,6 +369,7 @@ RootTask::RootTask(istream &in) {
check_facts(goals, variables);
operators = read_actions(file, false, use_metric, variables);
axioms = read_actions(file, true, use_metric, variables);
file.confirm_end_of_file();
/* TODO: We should be stricter here and verify that we
have reached the end of "in". */

Expand Down
Loading

0 comments on commit 53c5096

Please sign in to comment.