def use_module(self, module_name: str, **kwargs): if module_name.startswith('library'): # create library functor library_term = swipy.swipy_new_atom("library") library_funct = swipy.swipy_new_functor(library_term, 1) # create inner module name: library([name]) module_inner_name = module_name[:-1].replace('library(', '') inner_name = swipy.swipy_new_term_ref() swipy.swipy_put_atom_chars(inner_name, module_inner_name) # construct library(name) full_module = swipy.swipy_new_term_ref() swipy.swipy_cons_functor(full_module, library_funct, inner_name) else: full_module = swipy.swipy_new_term_ref() swipy.swipy_put_atom_chars(full_module, module_name) # load module use_module = swipy.swipy_predicate("use_module", 1, None) query = swipy.swipy_open_query(use_module, full_module) r = swipy.swipy_next_solution(query) swipy.swipy_close_query(query) if r == 0: raise Exception(f"could not load module {module_name}") return r
def _neg_to_swipy(clause: Not, lit_var_store: Dict[Variable, int]): lit = _lit_to_swipy(clause.get_atom(), lit_var_store) neg_atom = swipy.swipy_new_atom("\\+") neg_functor = swipy.swipy_new_functor(neg_atom, 1) entire_negation = swipy.swipy_new_term_ref() swipy.swipy_cons_functor(entire_negation, neg_functor, lit) return entire_negation
def _conjoin_literals(lits: Sequence[int]): if len(lits) == 1: return lits[0] else: f_atm = swipy.swipy_new_atom(",") conj_functor = swipy.swipy_new_functor(f_atm, 2) compound_arg = swipy.swipy_new_term_refs(2) conj = swipy.swipy_new_term_ref() swipy.swipy_put_term(compound_arg, lits[0]) swipy.swipy_put_term(compound_arg + 1, _conjoin_literals(lits[1:])) swipy.swipy_cons_functor(conj, conj_functor, compound_arg) return conj
def _wrap_in_call_time(self, query, time): """ Wraps the query in call_with_time_limit. Assumes that query is already translated to swipy :param query: :param time: :return: """ func_atm = swipy.swipy_new_atom("call_with_time_limit") func = swipy.swipy_new_functor(func_atm, 2) compound_arg = swipy.swipy_new_term_refs(2) _num_to_swipy_ref(time, compound_arg) swipy.swipy_put_term(compound_arg + 1, query) final_term = swipy.swipy_new_term_ref() swipy.swipy_cons_functor(final_term, func, compound_arg) return final_term
def _cl_to_swipy(clause: Clause, lit_var_store: Dict[Variable, int]): body: typing.Sequence[Union[Atom, Not]] = clause.get_body().get_literals() head: Atom = clause.get_head() body: typing.List[int] = [_lit_to_swipy(x, lit_var_store) if isinstance(x, Atom) else _neg_to_swipy(x, lit_var_store) for x in body] head = _lit_to_swipy(head, lit_var_store) body = _conjoin_literals(body) clause_atom = swipy.swipy_new_atom(":-") clause_functor = swipy.swipy_new_functor(clause_atom, 2) entire_clause = swipy.swipy_new_term_ref() compound_arg = swipy.swipy_new_term_refs(2) swipy.swipy_put_term(compound_arg, head) swipy.swipy_put_term(compound_arg + 1, body) swipy.swipy_cons_functor(entire_clause, clause_functor, compound_arg) return entire_clause
def _functor_to_swipy(functor: Union[Functor, Predicate]): func_atm = swipy.swipy_new_atom(functor.get_name()) func = swipy.swipy_new_functor(func_atm, functor.get_arity()) return func