def __call__(self, query, maxresult, catcherrors, normalize): Prolog._init_prolog_thread() swipl_fid = PL_open_foreign_frame() swipl_head = PL_new_term_ref() swipl_args = PL_new_term_refs(2) swipl_goalCharList = swipl_args swipl_bindingList = swipl_args + 1 PL_put_chars(swipl_goalCharList, PL_STRING | REP_UTF8, -1, query.encode("utf-8")) swipl_predicate = PL_predicate("pyrun", 2, None) plq = catcherrors and (PL_Q_NODEBUG | PL_Q_CATCH_EXCEPTION) or PL_Q_NORMAL swipl_qid = PL_open_query(None, plq, swipl_predicate, swipl_args) Prolog._queryIsOpen = True # From now on, the query will be considered open try: while maxresult and PL_next_solution(swipl_qid): maxresult -= 1 bindings = [] swipl_list = PL_copy_term_ref(swipl_bindingList) t = getTerm(swipl_list) if normalize: try: v = t.value except AttributeError: v = {} for r in [x.value for x in t]: # r = normalize_values(r) v.update(r) yield v else: yield t if PL_exception(swipl_qid): term = getTerm(PL_exception(swipl_qid)) if term.value == 'time_limit_exceeded': raise TimeoutError() if term.args[0].value == 'resource_error(stack)': raise MemoryError() if term.args[ 0].value == 'evaluation_error(float_overflow)': raise OverflowError raise PrologError("".join([ "Caused by: '", query, "'. ", "Returned: '", str(term), "'." ])) finally: # This ensures that, whatever happens, we close the query PL_cut_query(swipl_qid) PL_discard_foreign_frame(swipl_fid) Prolog._queryIsOpen = False
def __call__(self, query, maxresult, catcherrors, normalize, debug): Prolog._init_prolog_thread() swipl_fid = PL_open_foreign_frame() swipl_head = PL_new_term_ref() swipl_args = PL_new_term_refs(2) swipl_goalCharList = swipl_args swipl_bindingList = swipl_args + 1 PL_put_chars(swipl_goalCharList, PL_STRING | REP_UTF8, -1, query.encode("utf-8")) swipl_predicate = PL_predicate("pyrun", 2, None) # set flags for PL_open_query plq = PL_Q_NORMAL if catcherrors: plq |= PL_Q_CATCH_EXCEPTION if not debug: plq |= PL_Q_NODEBUG swipl_qid = PL_open_query(None, plq, swipl_predicate, swipl_args) Prolog._queryIsOpen = True # From now on, the query will be considered open try: while maxresult and PL_next_solution(swipl_qid): maxresult -= 1 bindings = [] swipl_list = PL_copy_term_ref(swipl_bindingList) t = getTerm(swipl_list) if normalize: try: v = t.value except AttributeError: v = {} for r in [x.value for x in t]: r = normalize_values(r) v.update(r) yield v else: yield t if PL_exception(swipl_qid): term = getTerm(PL_exception(swipl_qid)) raise PrologError("".join([ "Caused by: '", query, "'. ", "Returned: '", str(term), "'." ])) finally: # This ensures that, whatever happens, we close the query PL_cut_query(swipl_qid) PL_discard_foreign_frame(swipl_fid) Prolog._queryIsOpen = False
def __call__(self, query, maxresult, catcherrors, normalize): plq = catcherrors and (PL_Q_NODEBUG | PL_Q_CATCH_EXCEPTION) or PL_Q_NORMAL self.swipl_fid = PL_open_foreign_frame() swipl_head = PL_new_term_ref() swipl_args = PL_new_term_refs(2) swipl_goalCharList = swipl_args swipl_bindingList = swipl_args + 1 PL_put_list_chars(swipl_goalCharList, query) swipl_predicate = PL_predicate("pyrun", 2, None) self.swipl_qid = swipl_qid = PL_open_query(None, plq, swipl_predicate, swipl_args) while maxresult and PL_next_solution(swipl_qid): maxresult -= 1 bindings = [] swipl_list = PL_copy_term_ref(swipl_bindingList) t = getTerm(swipl_list) if normalize: try: v = t.value except AttributeError: v = {} for r in [x.value for x in t]: v.update(r) yield v else: yield t if PL_exception(self.swipl_qid): self.error = True PL_cut_query(self.swipl_qid) PL_discard_foreign_frame(self.swipl_fid) raise PrologError("".join(["Caused by: '", query, "'."]))
def __call__(self, query, maxresult, catcherrors, normalize): plq = catcherrors and (PL_Q_NODEBUG|PL_Q_CATCH_EXCEPTION) or PL_Q_NORMAL self.swipl_fid = PL_open_foreign_frame() swipl_head = PL_new_term_ref() swipl_args = PL_new_term_refs(2) swipl_goalCharList = swipl_args swipl_bindingList = swipl_args + 1 PL_put_list_chars(swipl_goalCharList, query) swipl_predicate = PL_predicate("pyrun", 2, None) self.swipl_qid = swipl_qid = PL_open_query(None, plq, swipl_predicate, swipl_args) while maxresult and PL_next_solution(swipl_qid): maxresult -= 1 bindings = [] swipl_list = PL_copy_term_ref(swipl_bindingList) t = getTerm(swipl_list) if normalize: try: v = t.value except AttributeError: v = {} for r in [x.value for x in t]: v.update(r) yield v else: yield t if PL_exception(self.swipl_qid): self.error = True PL_cut_query(self.swipl_qid) PL_discard_foreign_frame(self.swipl_fid) raise PrologError("".join(["Caused by: '", query, "'."]))
def __call__(self, query, maxresult, catcherrors, normalize): swipl_fid = PL_open_foreign_frame() swipl_head = PL_new_term_ref() swipl_args = PL_new_term_refs(2) swipl_goalCharList = swipl_args swipl_bindingList = swipl_args + 1 # PL_put_list_chars(swipl_goalCharList, query) PL_unify_wchars(swipl_goalCharList,query) swipl_predicate = PL_predicate("pyrun", 2, None) plq = catcherrors and (PL_Q_NODEBUG|PL_Q_CATCH_EXCEPTION) or PL_Q_NORMAL swipl_qid = PL_open_query(None, plq, swipl_predicate, swipl_args) Prolog._queryIsOpen = True # From now on, the query will be considered open try: while maxresult and PL_next_solution(swipl_qid): maxresult -= 1 bindings = [] swipl_list = PL_copy_term_ref(swipl_bindingList) t = getTerm(swipl_list) if normalize: try: v = t.value except AttributeError: v = {} for r in [x.value for x in t]: v.update(r) yield v else: yield t if PL_exception(swipl_qid): term = getTerm(PL_exception(swipl_qid)) raise PrologError("".join(["Caused by: '", query, "'. ", "Returned: '", str(term), "'."])) finally: # This ensures that, whatever happens, we close the query PL_cut_query(swipl_qid) PL_discard_foreign_frame(swipl_fid) Prolog._queryIsOpen = False
def __call__(self, query, maxresult, catcherrors, normalize): # When error occurs 'cut' query which does not free memory used by query. # Otherwise close query which frees memory used by query plq = catcherrors and (PL_Q_NODEBUG|PL_Q_CATCH_EXCEPTION) or PL_Q_NORMAL self.swipl_fid = PL_open_foreign_frame() swipl_head = PL_new_term_ref() swipl_args = PL_new_term_refs(2) swipl_goalCharList = swipl_args swipl_bindingList = swipl_args + 1 PL_put_list_chars(swipl_goalCharList, query) swipl_predicate = PL_predicate("pyrun", 2, None) self.swipl_qid = swipl_qid = PL_open_query(None, plq, swipl_predicate, swipl_args) while maxresult and PL_next_solution(swipl_qid): maxresult -= 1 swipl_list = PL_copy_term_ref(swipl_bindingList) t = getTerm(swipl_list) if normalize: try: v = t.value except AttributeError: v = {} for r in [x.value for x in t]: if isinstance(r.values()[0], Functor): v.update({r.keys()[0]:str(r.values()[0])}) else: v.update(r) yield v else: yield t if PL_exception(self.swipl_qid): self.error = True #PL_cut_query(self.swipl_qid) PL_close_query(self.swipl_qid) PL_discard_foreign_frame(self.swipl_fid) #apm = psutil.avail_phymem()#/(1024) #self.log.info(RBT + 'AVAIL_PHY_MEM: ' + BBB + repr(apm) + RESET) raise PrologError("".join(["Caused by: '", query, "'."]))