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
def _prepare_query(self, var_store, *query, max_time=None, max_depth=None, max_inference=None): """ Prepares the query Return swipl-transformed predicate and arguments needed to posed the query to the engine :param var_store: :param query: :param max_time: :param max_depth: :param max_inference: :return: """ if len(query) == 1: query = query[0] if not (max_time or max_depth or max_inference): predicate_name = query.get_predicate().get_name() query_args = swipy.swipy_new_term_refs(query.get_predicate().get_arity()) for ind, arg in enumerate(query.get_arguments()): _to_swipy_ref(arg, query_args + ind, var_store) pred = swipy.swipy_predicate(predicate_name, query.get_predicate().get_arity(), None) return pred, query_args else: query_term = _lit_to_swipy(query, var_store) if max_time: # if time limit should be imposed on the query pred = swipy.swipy_predicate("call_with_time_limit", 2, None) query_args = swipy.swipy_new_term_refs(2) swipy.swipy_put_integer(query_args, max_time) swipy.swipy_put_term(query_args+1, query_term) return pred, query_args elif max_depth or max_inference: pred = swipy.swipy_predicate("call_with_depth_limit", 3, None) if max_depth else swipy.swipy_predicate("call_with_inference_limit", 3, None) query_args = swipy.swipy_new_term_refs(3) swipy.swipy_put_term(query_args, query_term) if max_depth: _num_to_swipy_ref(max_depth, query_args + 1) else: _num_to_swipy_ref(max_inference, query_args + 1) swipy.swipy_put_variable(query_args + 2) return pred, query_args else: swipy_objs = [_lit_to_swipy(x, var_store) if isinstance(x, Atom) else _neg_to_swipy(x, var_store) for x in query] if not (max_time or max_depth or max_inference): first = swipy_objs[0] rest = _conjoin_literals(swipy_objs[1:]) compound_arg = swipy.swipy_new_term_refs(2) swipy.swipy_put_term(compound_arg, first) swipy.swipy_put_term(compound_arg + 1, rest) predicate = swipy.swipy_predicate(",", 2, None) return predicate, compound_arg else: query_term = _conjoin_literals(swipy_objs) if max_time: # if time limit should be imposed on the query wrapped_term = self._wrap_in_call_time(query_term, max_time) exception = swipy.swipy_new_atom('time_limit_exceeded') fail = swipy.swipy_new_atom('fail') pred = swipy.swipy_predicate("catch", 3, None) query_args = swipy.swipy_new_term_refs(3) swipy.swipy_put_term(query_args, wrapped_term) swipy.swipy_put_term(query_args + 1, exception) swipy.swipy_put_term(query_args + 2, fail) return pred, query_args elif max_depth or max_inference: pred = swipy.swipy_predicate("call_with_depth_limit", 3, None) if max_depth else swipy.swipy_predicate("call_with_inference_limit", 3, None) query_args = swipy.swipy_new_term_refs(3) swipy.swipy_put_term(query_args, query_term) if max_depth: _num_to_swipy_ref(max_depth, query_args + 1) else: _num_to_swipy_ref(max_inference, query_args + 1) swipy.swipy_put_variable(query_args + 2) return pred, query_args