Пример #1
0
    def manual_constructs(self):
        p1 = c_const("p1")
        p2 = c_const("p2")
        p3 = c_const("p3")

        parent = c_pred("parent", 2)
        grandparent = c_pred("grandparent", 2)

        f1 = parent(p1, p2)
        f2 = parent(p2, p3)

        v1 = c_var("X")
        v2 = c_var("Y")
        v3 = c_var("Z")

        cl = grandparent(v1, v3) <= parent(v1, v2) & parent(v2, v3)

        assert isinstance(p1, Constant)
        assert isinstance(p2, Constant)
        assert isinstance(p3, Constant)

        assert isinstance(parent, Predicate)
        assert isinstance(grandparent, Predicate)

        assert isinstance(v1, Variable)
        assert isinstance(v2, Variable)
        assert isinstance(v2, Variable)

        assert isinstance(cl, Clause)

        assert isinstance(f1, Atom)
        assert isinstance(f2, Atom)
Пример #2
0
    def graph_connectivity(self):
        v1 = c_const("v1")
        v2 = c_const("v2")
        v3 = c_const("v3")
        v4 = c_const("v4")

        edge = c_pred("edge", 2)
        path = c_pred("path", 2)

        f1 = edge(v1, v2)
        f2 = edge(v1, v3)
        f3 = edge(v2, v4)

        X = c_var("X")
        Y = c_var("Y")
        Z = c_var("Z")

        cl1 = path(X, Y) <= edge(X, Y)
        cl2 = path(X, Y) <= path(X, Z) & edge(Z, Y)

        solver = MuZ()

        solver.assert_fact(f1)
        solver.assert_fact(f2)
        solver.assert_fact(f3)

        solver.assert_rule(cl1)
        solver.assert_rule(cl2)

        # assert solver.has_solution(path(v1, v2))
        # assert solver.has_solution(path(v1, v4))
        # assert not solver.has_solution(path(v3, v4))
        #
        # assert len(solver.one_solution(path(v1, X))) == 1
        # assert len(solver.one_solution(path(X, v4))) == 1
        # assert len(solver.one_solution(path(X, Y))) == 2
        #
        # assert len(solver.all_solutions(path(v1, X))) == 3
        # assert len(solver.all_solutions(path(X, Y))) == 4

        assert solver.has_solution(path(v1, v2))
        assert solver.has_solution(path(v1, v4))
        assert not solver.has_solution(path(v3, v4))

        assert len(solver.query(path(v1, X), max_solutions=1)[0]) == 1
        assert len(solver.query(path(X, v4), max_solutions=1)[0]) == 1
        assert len(solver.query(path(X, Y), max_solutions=1)[0]) == 2

        assert len(solver.query(path(v1, X))) == 3
        assert len(solver.query(path(X, Y))) == 4
Пример #3
0
def _pyxsb_string_to_const_or_var(term: str):
    if term[0].islower():
        return c_const(term)
    elif term.isnumeric():
        if '.' in term:
            return float(term)
        else:
            return int(term)
    else:
        return c_var(term)
Пример #4
0
def _swipy_to_string(term):
    return c_const(swipy.swipy_get_string_chars(term))
Пример #5
0
def _swipy_to_const(term):
    name = swipy.swipy_get_atom_chars(term)
    return c_const(name if name[0].islower() else f"\"{name}\"")
Пример #6
0
    def simple_grandparent(self):
        p1 = c_const("p1")
        p2 = c_const("p2")
        p3 = c_const("p3")

        parent = c_pred("parent", 2)
        grandparent = c_pred("grandparent", 2)

        f1 = parent(p1, p2)
        f2 = parent(p2, p3)

        v1 = c_var("X")
        v2 = c_var("Y")
        v3 = c_var("Z")

        cl = (grandparent(v1, v3) <= parent(v1, v2) & parent(v2, v3))

        solver = MuZ()

        solver.assert_fact(f1)
        solver.assert_fact(f2)
        solver.assert_rule(cl)

        # assert solver.has_solution(parent(v1, v2))
        # assert not solver.has_solution(parent(v1, v1))
        # assert len(solver.all_solutions(parent(v1, v2))) == 2
        # assert len(solver.all_solutions(parent(p1, v1))) == 1
        # assert solver.has_solution(parent(p1, p2))
        # assert not solver.has_solution(parent(p2, p1))
        # assert len(solver.one_solution(parent(p1, v1))) == 1
        #
        # assert solver.has_solution(grandparent(v1, v2))
        # assert solver.has_solution(grandparent(p1, v1))
        # assert len(solver.one_solution(grandparent(p1, v1))) == 1
        # assert solver.has_solution(grandparent(p1, p3))
        # assert not solver.has_solution(grandparent(p2, v1))
        # assert len(solver.one_solution(grandparent(p1, v1))) == 1
        # ans = solver.one_solution(grandparent(p1, v1))
        # assert ans[v1] == p3
        # ans = solver.one_solution(grandparent(v1, v2))
        # assert ans[v1] == p1 and ans[v2] == p3
        #
        # assert solver.has_solution(cl)
        # ans = solver.one_solution(cl)
        # assert ans[v1] == p1 and ans[v3] == p3
        # assert len(solver.all_solutions(cl)) == 1

        assert solver.has_solution(parent(v1, v2))
        assert not solver.has_solution(parent(v1, v1))
        assert len(solver.query(parent(v1, v2))) == 2
        assert len(solver.query(parent(p1, v1))) == 1
        assert solver.has_solution(parent(p1, p2))
        assert not solver.has_solution(parent(p2, p1))
        assert len(solver.query(parent(p1, v1), max_solutions=1)) == 1

        assert solver.has_solution(grandparent(v1, v2))
        assert solver.has_solution(grandparent(p1, v1))
        assert len(solver.query(grandparent(p1, v1), max_solutions=1)) == 1
        assert solver.has_solution(grandparent(p1, p3))
        assert not solver.has_solution(grandparent(p2, v1))
        assert len(solver.query(grandparent(p1, v1), max_solutions=1)) == 1
        ans = solver.query(grandparent(p1, v1), max_solutions=1)[0]
        assert ans[v1] == p3
        ans = solver.query(grandparent(v1, v2), max_solutions=1)[0]
        assert ans[v1] == p1 and ans[v2] == p3

        assert solver.has_solution(*cl.get_literals())
        ans = solver.query(*cl.get_literals(), max_solutions=1)[0]
        assert ans[v1] == p1 and ans[v3] == p3
        assert len(solver.query(*cl.get_literals())) == 1
Пример #7
0
def _pygp_to_const(term):
    # global global_context
    return c_const(pygprolog.pygp_Rd_String(term))