# Path: stripstream/utils.py
# def write(filename, string):
#     with open(filename, 'w') as f:
#         f.write(string)
# 
# def read(filename):
#     with open(filename, 'r') as f:
#         return f.read()
# 
# def ensure_dir(f):
#     d = os.path.dirname(f)
#     if not os.path.exists(d):
#         os.makedirs(d)
# 
# def safe_remove(p):
#     if os.path.exists(p):
#         os.remove(p)
# 
# def remove_dir(d):
#     if os.path.exists(d):
#         shutil.rmtree(d)
# 
# Path: stripstream/algorithms/search/utils.py
# TEMP_DIRECTORY = 'temp/'
# 
# DOMAIN_PATH = os.path.join(TEMP_DIRECTORY, 'domain.pddl')
# 
# PROBLEM_PATH = os.path.join(TEMP_DIRECTORY, 'problem.pddl')
# 
# Path: stripstream/algorithms/search/fast_downward.py
# def lookup_action(universe, action, parameters):
#     return universe.get_action(action), tuple(universe.get_object(param) for param in parameters)


import os
from stripstream.utils import write, read, ensure_dir, safe_remove, remove_dir
from stripstream.algorithms.search.utils import TEMP_DIRECTORY, DOMAIN_PATH, PROBLEM_PATH
from stripstream.algorithms.search.fast_downward import lookup_action
def has_lapkt():
    return ENV_VAR in os.environ


def get_lapkt_root():
    if not has_lapkt():
        raise RuntimeError('Environment variable %s is not defined.' % ENV_VAR)
    return os.environ[ENV_VAR]


def lapkt(universe, search, verbose):
    ensure_dir(TEMP_DIRECTORY)
    write(DOMAIN_PATH, universe.domain_pddl(True, False))
    write(PROBLEM_PATH, universe.problem_pddl(True))
    safe_remove(PLANNER_OUTPUT)

    planner_path = os.path.join(get_lapkt_root(), LAPKT_PLANNERS[search])
    command = planner_path + \
        COMMAND % (DOMAIN_PATH, PROBLEM_PATH, PLANNER_OUTPUT)
    if verbose:
        print command
    p = os.popen(command)
    output = p.read()
    if verbose:
        print output

    SUCCESS = 'Plan found'
    if output.find(SUCCESS) == -1:
        return None
    plan = []

# Path: stripstream/utils.py
# def write(filename, string):
#     with open(filename, 'w') as f:
#         f.write(string)
# 
# def read(filename):
#     with open(filename, 'r') as f:
#         return f.read()
# 
# def ensure_dir(f):
#     d = os.path.dirname(f)
#     if not os.path.exists(d):
#         os.makedirs(d)
# 
# def safe_remove(p):
#     if os.path.exists(p):
#         os.remove(p)
# 
# def remove_dir(d):
#     if os.path.exists(d):
#         shutil.rmtree(d)
# 
# Path: stripstream/algorithms/search/utils.py
# TEMP_DIRECTORY = 'temp/'
# 
# DOMAIN_PATH = os.path.join(TEMP_DIRECTORY, 'domain.pddl')
# 
# PROBLEM_PATH = os.path.join(TEMP_DIRECTORY, 'problem.pddl')
# 
# Path: stripstream/algorithms/search/fast_downward.py
# def lookup_action(universe, action, parameters):
#     return universe.get_action(action), tuple(universe.get_object(param) for param in parameters)


# Path: stripstream/algorithms/search/lapkt.py
import os
from stripstream.utils import write, read, ensure_dir, safe_remove, remove_dir
from stripstream.algorithms.search.utils import TEMP_DIRECTORY, DOMAIN_PATH, PROBLEM_PATH
from stripstream.algorithms.search.fast_downward import lookup_action

def has_lapkt():
    return ENV_VAR in os.environ


def get_lapkt_root():
    if not has_lapkt():
        raise RuntimeError('Environment variable %s is not defined.' % ENV_VAR)
    return os.environ[ENV_VAR]


def lapkt(universe, search, verbose):
    ensure_dir(TEMP_DIRECTORY)
    write(DOMAIN_PATH, universe.domain_pddl(True, False))
    write(PROBLEM_PATH, universe.problem_pddl(True))
    safe_remove(PLANNER_OUTPUT)

    planner_path = os.path.join(get_lapkt_root(), LAPKT_PLANNERS[search])
    command = planner_path + \
        COMMAND % (DOMAIN_PATH, PROBLEM_PATH, PLANNER_OUTPUT)
    if verbose:
        print command
    p = os.popen(command)
    output = p.read()
    if verbose:
        print output

    SUCCESS = 'Plan found'
    if output.find(SUCCESS) == -1:
        return None
    plan = []

    for line in read(PLANNER_OUTPUT).split('\n')[:-1]: