<commit_before>#include "globals.h"
#include "operator.h"
#include "module.h"
#include "best_first_search.h"
#include "plannerParameters.h"
#include "ros_printouts.h"
#include "tfd_modules/opl/stringutil.h"

#include <iostream>
using namespace std;

Condition::Condition(istream &in)
{
    in >> var >> prev;
}

bool Condition::is_applicable(const TimeStampedState &state, const Operator* op, bool allowRelaxed) const
{
    assert(var >= 0 && var < g_variable_name.size());
    assert((prev >= 0 && prev < g_variable_domain[var]) || (g_variable_types[var] == module));
    if(g_variable_types[var] == module) {
        g_setModuleCallbackState(&state);
        predicateCallbackType pct = getPreds;
        numericalFluentCallbackType nct = getFuncs;

        modules::ParameterList parameters = g_condition_modules[var]->params;
        op->addGroundParameters(parameters);
        double cost = g_condition_modules[var]->checkCondition(
                parameters, pct, nct, allowRelaxed);
        return cost < INFINITE_COST;
    } else {
        return state_equals(state[var], prev);
    }
}

Effect::Effect(istream &in)
{
    int cond_count;
    in >> cond_count;
    for(int i = 0; i < cond_count; i++)
        cond_start.push_back(Condition(in));
    in >> cond_count;
    for(int i = 0; i < cond_count; i++)
        cond_overall.push_back(Condition(in));
    in >> cond_count;
    for(int i = 0; i < cond_count; i++)
        cond_end.push_back(Condition(in));
    in >> var;
    if(is_functional(var)) {
        in >> fop >> var_post;
        // HACK: just use some arbitrary values for pre and post
        // s.t. they do not remain uninitialized
        pre = post = -1;
    } else {
        in >> pre >> post;
        // HACK: just use some arbitrary values for var_post and fop
        // s.t. they do not remain uninitialized
        var_post = -1;
        fop = assign;
    }
}

ModuleEffect::ModuleEffect(istream &in)
{
    int cond_count;
    in >> cond_count;
    for(int i = 0; i < cond_count; i++)
        cond_start.push_back(Condition(in));
    in >> cond_count;
    for(int i = 0; i < cond_count; i++)
        cond_overall.push_back(Condition(in));
    in >> cond_count;
    for(int i = 0; i < cond_count; i++)
        cond_end.push_back(Condition(in));
    string name;
    in >> name;
    name = name.substr(3);
    module = g_effect_modules[atoi(name.c_str())];
    ROS_ASSERT(module != NULL);
}

ModuleGrounding::ModuleGrounding(std::istream &in)
{
    string name;
    in >> name;
    name = name.substr(3);
    module = g_grounding_modules[atoi(name.c_str())];
    ROS_ASSERT(module != NULL);
}

void ModuleGrounding::dump() const
{
    module->dump();
}

bool Effect::is_applicable(const TimeStampedState &state) const
{
    assert(var >= 0 && var < g_variable_name.size());
    assert(pre == -1 || (pre >= 0 && pre < g_variable_domain[var]));
    return pre == -1 || (state_equals(state[var], pre));
}

void Operator::sort_prevails(vector<Condition> &prevails)
{
    int swapIndex = prevails.size() - 1;
    for(int i = 0; i <= swapIndex; ++i) {
        if(g_variable_types[prevails[i].var] == module) {
            std::swap(prevails[i], prevails[swapIndex]);
            i--;
            swapIndex--;
        }
    }
}

Operator::Operator(istream &in) : grounding_parent(NULL)
{
    check_magic(in, "begin_operator");
    in >> ws;
    getline(in, name);
    int groundingCount;
    in >> groundingCount;
    assert(groundingCount >= 0 && groundingCount < 2);  // exactly 0 or 1 grounding module
    for(int i = 0; i < groundingCount; ++i) {
        mod_groundings.push_back(ModuleGrounding(in));
    }
    int count;
    binary_op bop;
    in >> bop >> duration_var;
    if(bop != eq) {
        cout << "Error: The duration constraint must be of the form\n";
        cout << "       (= ?duration (arithmetic_term))" << endl;
        exit(1);
    }

    in >> count; //number of prevail at-start conditions
    for(int i = 0; i < count; i++)
        prevail_start.push_back(Condition(in));
    in >> count; //number of prevail overall conditions
    for(int i = 0; i < count; i++)
        prevail_overall.push_back(Condition(in));
    in >> count; //number of prevail at-end conditions
    for(int i = 0; i < count; i++)
        prevail_end.push_back(Condition(in));
    in >> count; //number of pre_post_start conditions (symbolical)
    for(int i = 0; i < count; i++)
        pre_post_start.push_back(Effect(in));
    in >> count; //number of pre_post_end conditions (symbolical)
    for(int i = 0; i < count; i++)
        pre_post_end.push_back(Effect(in));
    in >> count; //number of pre_post_start conditions (functional)
    for(int i = 0; i < count; i++)
        pre_post_start.push_back(Effect(in));
    in >> count; //number of pre_post_end conditions (functional)
    for(int i = 0; i < count; i++)
        pre_post_end.push_back(Effect(in));

    // sort prevails such that conditions on module variables come last
    sort_prevails(prevail_start);
    sort_prevails(prevail_overall);
    sort_prevails(prevail_end);

    in >> count; //numer of module start effects
    for(int i = 0; i < count; ++i) {
        mod_effs_start.push_back(ModuleEffect(in));
    }
    in >> count; //number of module end effects
    for(int i = 0; i < count; ++i) {
        mod_effs_end.push_back(ModuleEffect(in));
    }
    check_magic(in, "end_operator");

    numBranches = 0;

    dump();
}

Operator::Operator(bool uses_concrete_time_information) : grounding_parent(NULL)
{
    prevail_start   = vector<Condition>();
    prevail_overall = vector<Condition>();
    prevail_end     = vector<Condition>();
    pre_post_start  = vector<Effect>();
    pre_post_end    = vector<Effect>();
    if(!uses_concrete_time_information) {
        name = "let_time_pass";
        duration_var = -1;
    } else {
        name = "wait";
        duration_var = -2;<commit_msg>removed excessive debug printouts<commit_after>#include "globals.h"
#include "operator.h"
#include "module.h"
#include "best_first_search.h"
#include "plannerParameters.h"
#include "ros_printouts.h"
#include "tfd_modules/opl/stringutil.h"

#include <iostream>
using namespace std;

Condition::Condition(istream &in)
{
    in >> var >> prev;
}

bool Condition::is_applicable(const TimeStampedState &state, const Operator* op, bool allowRelaxed) const
{
    assert(var >= 0 && var < g_variable_name.size());
    assert((prev >= 0 && prev < g_variable_domain[var]) || (g_variable_types[var] == module));
    if(g_variable_types[var] == module) {
        g_setModuleCallbackState(&state);
        predicateCallbackType pct = getPreds;
        numericalFluentCallbackType nct = getFuncs;

        modules::ParameterList parameters = g_condition_modules[var]->params;
        op->addGroundParameters(parameters);
        double cost = g_condition_modules[var]->checkCondition(
                parameters, pct, nct, allowRelaxed);
        return cost < INFINITE_COST;
    } else {
        return state_equals(state[var], prev);
    }
}

Effect::Effect(istream &in)
{
    int cond_count;
    in >> cond_count;
    for(int i = 0; i < cond_count; i++)
        cond_start.push_back(Condition(in));
    in >> cond_count;
    for(int i = 0; i < cond_count; i++)
        cond_overall.push_back(Condition(in));
    in >> cond_count;
    for(int i = 0; i < cond_count; i++)
        cond_end.push_back(Condition(in));
    in >> var;
    if(is_functional(var)) {
        in >> fop >> var_post;
        // HACK: just use some arbitrary values for pre and post
        // s.t. they do not remain uninitialized
        pre = post = -1;
    } else {
        in >> pre >> post;
        // HACK: just use some arbitrary values for var_post and fop
        // s.t. they do not remain uninitialized
        var_post = -1;
        fop = assign;
    }
}

ModuleEffect::ModuleEffect(istream &in)
{
    int cond_count;
    in >> cond_count;
    for(int i = 0; i < cond_count; i++)
        cond_start.push_back(Condition(in));
    in >> cond_count;
    for(int i = 0; i < cond_count; i++)
        cond_overall.push_back(Condition(in));
    in >> cond_count;
    for(int i = 0; i < cond_count; i++)
        cond_end.push_back(Condition(in));
    string name;
    in >> name;
    name = name.substr(3);
    module = g_effect_modules[atoi(name.c_str())];
    ROS_ASSERT(module != NULL);
}

ModuleGrounding::ModuleGrounding(std::istream &in)
{
    string name;
    in >> name;
    name = name.substr(3);
    module = g_grounding_modules[atoi(name.c_str())];
    ROS_ASSERT(module != NULL);
}

void ModuleGrounding::dump() const
{
    module->dump();
}

bool Effect::is_applicable(const TimeStampedState &state) const
{
    assert(var >= 0 && var < g_variable_name.size());
    assert(pre == -1 || (pre >= 0 && pre < g_variable_domain[var]));
    return pre == -1 || (state_equals(state[var], pre));
}

void Operator::sort_prevails(vector<Condition> &prevails)
{
    int swapIndex = prevails.size() - 1;
    for(int i = 0; i <= swapIndex; ++i) {
        if(g_variable_types[prevails[i].var] == module) {
            std::swap(prevails[i], prevails[swapIndex]);
            i--;
            swapIndex--;
        }
    }
}

Operator::Operator(istream &in) : grounding_parent(NULL)
{
    check_magic(in, "begin_operator");
    in >> ws;
    getline(in, name);
    int groundingCount;
    in >> groundingCount;
    assert(groundingCount >= 0 && groundingCount < 2);  // exactly 0 or 1 grounding module
    for(int i = 0; i < groundingCount; ++i) {
        mod_groundings.push_back(ModuleGrounding(in));
    }
    int count;
    binary_op bop;
    in >> bop >> duration_var;
    if(bop != eq) {
        cout << "Error: The duration constraint must be of the form\n";
        cout << "       (= ?duration (arithmetic_term))" << endl;
        exit(1);
    }

    in >> count; //number of prevail at-start conditions
    for(int i = 0; i < count; i++)
        prevail_start.push_back(Condition(in));
    in >> count; //number of prevail overall conditions
    for(int i = 0; i < count; i++)
        prevail_overall.push_back(Condition(in));
    in >> count; //number of prevail at-end conditions
    for(int i = 0; i < count; i++)
        prevail_end.push_back(Condition(in));
    in >> count; //number of pre_post_start conditions (symbolical)
    for(int i = 0; i < count; i++)
        pre_post_start.push_back(Effect(in));
    in >> count; //number of pre_post_end conditions (symbolical)
    for(int i = 0; i < count; i++)
        pre_post_end.push_back(Effect(in));
    in >> count; //number of pre_post_start conditions (functional)
    for(int i = 0; i < count; i++)
        pre_post_start.push_back(Effect(in));
    in >> count; //number of pre_post_end conditions (functional)
    for(int i = 0; i < count; i++)
        pre_post_end.push_back(Effect(in));

    // sort prevails such that conditions on module variables come last
    sort_prevails(prevail_start);
    sort_prevails(prevail_overall);
    sort_prevails(prevail_end);

    in >> count; //numer of module start effects
    for(int i = 0; i < count; ++i) {
        mod_effs_start.push_back(ModuleEffect(in));
    }
    in >> count; //number of module end effects
    for(int i = 0; i < count; ++i) {
        mod_effs_end.push_back(ModuleEffect(in));
    }
    check_magic(in, "end_operator");

    numBranches = 0;

    //dump();
}

Operator::Operator(bool uses_concrete_time_information) : grounding_parent(NULL)
{
    prevail_start   = vector<Condition>();
    prevail_overall = vector<Condition>();
    prevail_end     = vector<Condition>();
    pre_post_start  = vector<Effect>();
    pre_post_end    = vector<Effect>();
    if(!uses_concrete_time_information) {
        name = "let_time_pass";
        duration_var = -1;
    } else {
        name = "wait";
        duration_var = -2;
{'fraction_non_alphanumeric': 0.13959003896323904, 'fraction_numerical': 0.006098593935287142, 'mean_word_length': 2.943553774215097, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 147, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3300140', 'n_tokens_mistral': 4014, 'n_tokens_neox': 3909, 'n_words': 938}