def export_ground_problem(folder, domain_name, silent=True):
    """Parse problem, ground problem, export problem to temp location.
    Load the problem back, ground it again, and check if they are the same."""

    p_fnames, d_fname = get_files(folder)
    p_fnames = [os.path.join(folder, n) for n in p_fnames]
    d_fname = os.path.join(folder, d_fname)

    if p_fnames is None or len(p_fnames) == 0:
        return False

    for _, p_fname in enumerate(p_fnames):
        gp = grounder.GroundProblem(d_fname, p_fname)
        fakes = _export_problem(gp)

        if not silent:
            print("* Contents of domain file *")
            fp = open(fakes["domain"], "r")
            for line in fp:
                print(line.rstrip())
            fp.close()
            if fakes["problem"] is not None:
                print("* Contents of problem file *")
                fp = open(fakes["problem"], "r")
                for line in fp:
                    print(line.rstrip())
                fp.close()

        # now read back the domain file
        fake_gp = grounder.GroundProblem(
            fakes["domain"], fakes["problem"], no_ground=True
        )

        if fake_gp.is_equal(gp):
            if not silent:
                print("Domains are equivalent")
            result = True
        else:
            if not silent:
                print("Domains are not equivalent")
            result = False

        # remove temporary file
        os.remove(fakes["domain"])
        if fakes["problem"] is not None:
            os.remove(fakes["problem"])

        return result
def test_ktm(folder: str, parent_name: str):

    # find location of domain and problem files
    p_fnames, d_fname = get_files(folder)
    p_fnames = [os.path.join(folder, f) for f in p_fnames]
    d_fname = os.path.join(folder, d_fname)
    assert len(p_fnames) == 1, "There should only be 1 problem"
    p_fname = p_fnames[0]

    # check import works
    from pond import ktm, grounder

    # ground the problem
    gp = grounder.GroundProblem(d_fname, p_fname)

    # sample tags
    # take the first literal from the problem
    t0 = sorted(list(gp.fluents))[0]
    T = [ktm.Literal(t0, False)]

    # sample merges
    M = []

    # sample initial literals
    init_TL = []

    # somewhere, where stuff can be dumped without consequence
    fakes = _create_temp_files()

    fond_prob = ktm.compile(gp, T, M, init_TL)
    fond_prob.write(fakes["domain"], fakes["problem"])
def ground_problem(folder: str, domain_name: str, silent: bool = False):
    """Ground the given problem. Show the result."""

    p_fnames, d_fname = get_files(folder)
    p_fnames = [os.path.join(folder, f) for f in p_fnames]
    d_fname = os.path.join(folder, d_fname)

    est_tot = len(p_fnames)
    tot = 0

    if not silent:
        s = "problems" if est_tot > 1 else "problem"
        print("Grounding %d %s in domain %s" % (est_tot, s, domain_name))

    for p_fname in p_fnames:
        tot += 1
        gp = grounder.GroundProblem(d_fname, p_fname)
        if not silent:
            # gp.dump ()

            print(gp.initial_states)
            # sys.stdout.write("Progress: %.0f%% problems checked\r" % (tot / est_tot * 100))
            # sys.stdout.flush()

    if not silent:
        print("Successfully ground all problems")
    return True