Esempio n. 1
0
    def query(self, *query, **kwargs):
        if 'max_solutions' in kwargs:
            max_solutions = kwargs['max_solutions']
        else:
            max_solutions = -1

        vars_of_interest = [[
            y for y in x.get_arguments() if isinstance(y, Variable)
        ] for x in query]
        vars_of_interest = reduce(lambda x, y: x + y, vars_of_interest, [])
        vars_of_interest = reduce(lambda x, y: x + [y]
                                  if y not in x else x, vars_of_interest, [])

        string_repr = ','.join([str(x) for x in query])
        res = pyxsb.pyxsb_query_string(f"{string_repr}.")

        all_solutions = []
        while res and max_solutions != 0:
            vals = [x for x in res.strip().split(";")]
            var_assignments = [_pyxsb_string_to_pylo(x) for x in vals]
            all_solutions.append(
                dict([(v, s)
                      for v, s in zip(vars_of_interest, var_assignments)]))

            res = pyxsb.pyxsb_next_string()
            max_solutions -= 1

        pyxsb.pyxsb_close_query()

        return all_solutions
Esempio n. 2
0
    def has_solution(self, *query: Atom):
        #assert not all([x.is_ground() for x in query]), "XSB Prolog currently cannot query ground queries"
        string_repr = ','.join([str(x) for x in query])
        res = pyxsb.pyxsb_query_string(f"{string_repr}.")

        if res:
            pyxsb.pyxsb_close_query()

        return True if res else False