1f|ddlZddlGddeZGddeZdZeddlmZGd d Zd Z y) N)*c eZdZy) PrologErrorN)__name__ __module__ __qualname__W/var/lib/jenkins/workspace/metta-wam/venv/lib/python3.12/site-packages/pyswip/prolog.pyrrsr rceZdZdZy)NestedQueryErrorz SWI-Prolog does not accept nested queries, that is, opening a query while the previous one was not closed. As this error may be somewhat difficult to debug in foreign code, it is automatically treated inside pySWIP N)rrr__doc__r r r r r #s  r r c@g}|jd|jdt|jdtztt||}|st d|zt }t }td|t|dt|y)Nz./z-qz --home=%szECould not initialize the Prolog environment.PL_initialise returned %dzqasserta(pyrun(GoalString,BindingList) :- (atom_chars(A,GoalString),atom_to_term(A,Goal,BindingList),call(Goal))).) append SWI_HOME_DIR PL_initialiselenrPL_open_foreign_framePL_new_term_refPL_chars_to_termPL_callPL_discard_foreign_frame)argsresult swipl_fid swipl_loads r _initializer.s DKKKK K,./ 3t9T *F 68>?@ @&'I "J%'12 JY'r )getTermceZdZdZdZGddeZedZeddZ eddZ eddZ edd Z edd Z edd Zedd Zy )Prologz;Easily query SWI-Prolog. This is a singleton class FceZdZdZdZy)Prolog._QueryWrapperc:tjr tdy)NzThe last query was not closed)r _queryIsOpenr )selfs r __init__zProlog._QueryWrapper.__init__Vs""&'FGG#r c #Ktjt}t}t d}|}|dz} t |t tzd|jdtddd} |xr ttzxst} td| | |} dt_ |rNt| rC|dz}g} t!| }t#|}|r |j$}|n||r t| rCt-| r=t#t-| }t/dj1d|d d t3|d g t5| t7|d t_y#t&$rFi}|Dcgc]}|j$ncc}wc}D]}t)|}|j+| YwxYw#t5| t7|d t_wxYww) Nzutf-8pyrunTz Caused by: 'z'. z Returned: 'z'.F)r _init_prolog_threadrrPL_new_term_refs PL_put_chars PL_STRINGREP_UTF8encode PL_predicate PL_Q_NODEBUGPL_Q_CATCH_EXCEPTION PL_Q_NORMAL PL_open_queryr$PL_next_solutionPL_copy_term_refrvalueAttributeErrornormalize_valuesupdate PL_exceptionrjoinstr PL_cut_queryr)r%query maxresult catcherrors normalizer swipl_head swipl_argsswipl_goalCharListswipl_bindingListswipl_predicateplq swipl_qidbindings swipl_listtvxrterms r __call__zProlog._QueryWrapper.__call__Zs  & & (-/I(*J)!,J!+  *Q  +Yx-?U\\RYEZ [*7At"G F//F*F/,F2.F//F22#GGN)rrrr&rTr r r _QueryWrapperr"Ts  H, ,r rUczt}|dk(r td}|dk(r td|dk(r tdyy)Nr*z0Unable to attach new Prolog engine to the threadz+{WARN} Single-threaded swipl build, beware!)PL_thread_selfPL_thread_attach_enginerprint)cls pengine_ids r r-zProlog._init_prolog_threadsF#%  06J  PQ Q 2  ? @r c^t|j|jddg|y)Nz asserta(()).rDnextrBr?r[ assertionrDs r assertazProlog.asserta% SYYy~~{E&:;Y UVr c^t|j|jddg|y)Nz assertz((r^r_r`rbs r assertzzProlog.assertzrer c^t|j|jddg|y)Nz dynamic((r^r_r`r[rSrDs r dynamiczProlog.dynamic% SYYtyy+u!56KY PQr c^t|j|jddg|y)Nz retract((r^r_r`ris r retractzProlog.retractrkr c^t|j|jddg|y)Nz retractall((r^r_r`ris r retractallzProlog.retractalls% SYYtyy.%!89{Y STr c^t|j|jddg|y)Nz consult('z')r_r`)r[filenamerDs r consultzProlog.consults% SYYx}}k4%89{Y STr c4|j||||S)aLRun a prolog query and return a generator. If the query is a yes/no question, returns {} for yes, and nothing for no. Otherwise returns a generator of dicts with variables as keys. >>> prolog = Prolog() >>> prolog.assertz("father(michael,john)") >>> prolog.assertz("father(michael,gina)") >>> bool(list(prolog.query("father(michael,john)"))) True >>> bool(list(prolog.query("father(michael,olivia)"))) False >>> print sorted(prolog.query("father(michael,X)")) [{'X': 'gina'}, {'X': 'john'}] )rU)r[rBrCrDrEs r rBz Prolog.querys #s  "5)[)LLr N)F)r*TT)rrrrr$objectrU classmethodr-rdrgrjrmrorrrBr r r r r Ls L2,2,hAAWWWWRRRRUUUUMMr r c:ddlm}m}t||r |jSt||rrt |j j}|jrE|jDcgc]}t t|}}|dzdj|zdz}|St|tr-|jDcic]\}}|t|c}}St|ttfr|Dcgc] }t|c}S|Scc}wcc}}wcc}w)Nr)AtomFunctor(z, )) pyswip.easyrwrx isinstancer:r@namearityrr<r?dictitemslisttuple)valuesrwrx normalizedargnormalized_argskeyrPs r r<r<s)&$||&'"**+ <rsJ4  )  { (.  nMnMbr