f{AddlZddlmZddlZddlZddlZddlZddlm Z m Z m Z ddl m Z mZmZddlmZmZGddZGd d ZGd d ZGd dZGddZGddZdZddZy)N) import_module)AtomAtomType OperationAtom)GroundingSpaceRef Tokenizer SExprParser) EnvBuilderModuleIdc0eZdZdZdZdZdZdZddZy) RunnerStatez The state for an in-flight MeTTa interpreter handling the interpretation and evaluation of atoms in a given grounding space. ct|}||_tj|j|j |_y)zAInitialize a RunnerState with a MeTTa object and a program to runN)r parserhprunner_state_new_with_parsercmettacparsercstate)selfmettaprogramrs W/var/lib/jenkins/workspace/mettalog/venv/lib/python3.12/site-packages/hyperon/runner.py__init__zRunnerState.__init__s1W% 55ellFNNS cBtj|jy)z1Frees a RunnerState and all associated resources.N)rrunner_state_freerrs r__del__zRunnerState.__del__s T[[)rctj|jtj|j}| t |y)z Executes the next step in the interpretation plan, or begins interpretation of the next atom in the stream of MeTTa code. N)rrunner_state_steprrunner_state_err_str RuntimeErrorrerr_strs rrun_stepzRunnerState.run_steps@ T[[)))$++6  w' ' rc@tj|jS)zq Returns True if the runner has concluded, or False if there are more steps remaining to execute )rrunner_state_is_completerrs r is_completezRunnerState.is_complete%s**4;;77rc $tj|j}|r,|Dcgc]}|D]}tj| c}}S|Dcgc]%}|Dcgc]}tj|c}'c}}Scc}}wcc}wcc}}w)z^ Returns the current in-progress results from an in-flight program evaluation )rrunner_state_current_resultsrr _from_catom)rflatresultsresultcatoms rcurrent_resultszRunnerState.current_results+sw11$++> :AVvVeD$$U+V+V VPWXf&AT%%e,AX XWAXs#B B B 8B B NF) __name__ __module__ __qualname____doc__rrr&r)r1rrrr s"T*(8 YrrceZdZdZdZy)ModuleDescriptorzg An object that uniquely describes a module, including the module's name, optionally a version c||_y)z:Wraps the underlying ModuleDescriptor object from the coreN)c_module_descriptor)rr;s rrzModuleDescriptor.__init__9s #6 rN)r3r4r5r6rr7rrr9r95s 7rr9cFeZdZdZdZdZdZdZdZdZ dZ d Z d Z y ) RunContextz\ An accessor object for the API used by the executable atoms inside a MeTTa program c||_y)z4Wraps the underlying RunContext object from the coreN) c_run_context)rr?s rrzRunContext.__init__As *rcZtj|j|j|y)z]Must be called exactly once from within a module loader to initialize the module being loadedN)rrun_context_init_self_moduler?cspace)rspace resource_dirs rinit_self_modulezRunContext.init_self_moduleEs ''(:(:ELL,WrcTttj|jS)z=Access the MeTTa runner that the RunContext is running within)r)MeTTarrun_context_get_mettar?rs rrzRunContext.mettaJsb66t7I7IJKKrcftjtj|jS)z1Access the space for the currently running module)r _from_cspacerrun_context_get_spacer?rs rrCzRunContext.spaceNs$ --b.F.FtGYGY.Z[[rcftjtj|jS)z5Access the tokenizer for the currently running module)r _from_ctokenizerrrun_context_get_tokenizerr?rs r tokenizerzRunContext.tokenizerRs$))"*F*FtGYGY*Z[[rcBtj|j|S)z\Resolves a module by name in the context of the running module, and loads it into the runner)rrun_context_load_moduler?)rmod_names r load_modulezRunContext.load_moduleVs))$*<*z*RunContext.register_atom..`FrNrVrnamer`s `r register_atomzRunContext.register_atom^ D"23rcz|jr!tj|j|yt d)z=Imports a loaded module as a dependency of the running modulezInvalid ModuleIdN)is_validrrun_context_import_dependencyr?r#)rmod_ids rimport_dependencyzRunContext.import_dependencybs. ??   , ,T-?-? H12 2rN) r3r4r5r6rrErrCrOrSrVrfrlr7rrr=r==s9+X L\\H843rr=c|eZdZdZddZdZdZdZdZdZ d Z d Z d Z d Z d ZdZdZddZddZdZdZy)rGz:This class represents the runner to execute MeTTa programsNc|||_y| t}|tj}tj|t dt jjt jjtd}tj||tj}|D]}tj||tj|j||_y)Niexts)rrrenv_builder_start!env_builder_push_fs_module_format_PyFileMeTTaModFmtospathjoindirname__file__env_builder_push_include_pathsitegetsitepackages metta_newrB)rrrC env_builderbuiltin_mods_pathpy_site_packages_pathsrts rrzMeTTa.__init__ls   DK})+" 224  0 0>PRV W !# RWW__X-F O   , ,[:K L%)%9%9%; ".00dC/,,u||[ADKrcBtj|jyr])r metta_freerrs rrz MeTTa.__del__s dkk"rcVtj|j|jS)z.rbrNrcrds `rrfzMeTTa.register_atomrgrc#pKt|} |j|j}|y|'wr])r parserO)rrratoms r _parse_allzMeTTa._parse_alls:W%<< 01D|J s46c6t|j|S)z\Parse an entire program from text into atoms, using the Tokenizer of the runner's top module)listrrrs r parse_allzMeTTa.parse_allDOOG,--rc6t|j|S)z1Parse the next single token from the text program)nextrrs r parse_singlezMeTTa.parse_singlerrcfd}tj|j||j|}tj|j}| t ||S)zYLoads a module into the runner using a loader function, with the specified name and scopecDt|}t|}||yr])r=r9)r? c_descriptor run_context descriptorpy_loader_funcs r loader_funcz7MeTTa.load_module_direct_from_func..loader_funcs!$]3K),7J ; 3r)rmetta_load_module_directrr; metta_err_strr#)rrR private_torrrkr%s ` rload_module_direct_from_funcz"MeTTa.load_module_direct_from_funcsU 4,,T[[(JDbDbdop""4;;/  w' ' rctt|ts t|}t|}|j |||S)z_Loads a module into the runner directly from a Python module, with the specified name and scope) isinstancestrrepr'_priv_make_module_loader_func_for_pymodr)rrRr pymod_namers rload_module_direct_from_pymodz#MeTTa.load_module_direct_from_pymods5*c*j)J=jI 00:{SSrctj|j||}tj|j}| t ||S)z Loads a module into the runner directly from resource at a file system path, trying the formats from the runner's environment in succession )rmetta_load_module_at_pathrrr#)rrtrRrkr%s rload_module_at_pathzMeTTa.load_module_at_pathsF --dkk4J""4;;/  w' ' rc pt|}tj|j|j}|j |r,|Dcgc]}|D]}t j| c}}S|Dcgc]%}|Dcgc]}t j|c}'c}}Scc}}wcc}wcc}}w)zPRuns the MeTTa code from the program string containing S-Expression MeTTa syntax)r r metta_runrr_run_check_for_errorrr,)rrr-rr.r/r0s rrunz MeTTa.runsW%,,t{{FNN; !!# :AVvVeD$$U+V+V VPWXf&AT%%e,AX XWAXs #B'9 B2B- B2-B2ctj|j|j}|j |Dcgc]}t j |c}Scc}wr])rmetta_evaluate_atomrr0rrr,)rrr/r0s r evaluate_atomzMeTTa.evaluate_atomsG'' TZZ@ !!#5; U "  , ,We <   - -g 6   & &w 5!D  , ,Wd ;"r)r3r4r5r6rrrrr7rrrrsE_'+tfkw|OQ7 -"&D$afrwJLrrc"eZdZdZdZdZdZy)rrzThis private class implements the loader for Python modules that implement MeTTa modules. This logic covers both "*.py" files and directories that contain an "__init__.py".cr|jdr|n|dz}tjj||S)z6Construct a file path name based on the metta_mod_name.py)endswithrsrtru) parent_dirmetta_mod_name file_names r path_for_namez _PyFileMeTTaModFmt.path_for_name s1&4&=&=e&DN.[`J` ww||J 22rc|jdr|n$tjj|ddz}tjj |sctjj tjj|dd}tjj |r|}nyt jj||} t jj|}|tj|<|jj|||dS#t$r1}tj d|zdzt#|zYd}~yd}~wwxYw)z-Load the file as a Python module if it existsrrz __init__.pyN)rrtz#Python error loading MeTTa module 'z'. )rrsrtsplitextexistsru importlibutilspec_from_file_locationmodule_from_specsysmodulesloader exec_module Exceptionr log_errorr)rtrdir_pathspecmodulees rtry_pathz_PyFileMeTTaModFmt.try_paths}}U+t1A1A$1G1JU1Rww~~d#ww||BGG$4$4T$:1$=}MHww~~h'~~55ndK ^^44T:F*0CKK ' KK # #F + -   LL>ORWWZ^_`Zaa b sAD.. E(7'E##E(ct|}|d}|d}tjj|}t ||}||y)zHLoads the items from the python module into the runner as a MeTTa modulerrt)rDN)r=rsrtrvr)r?callback_contextrrrtrDrs r_load_called_from_cz&_PyFileMeTTaModFmt._load_called_from_c1sI / &l3 'wwt, =jWcd K rN)r3r4r5r6rrrr7rrrrrrsZ3 !F !rrrcDt|}tdd}||y)zj Private function called indirectly to load the Python stdlib during Python runner initialization zhyperon.stdlibT) load_corelibN)r=r)r?r stdlib_loaders r_priv_load_py_stdlibr?s$]+K;.loader_funcZs O +C&'E  ( ( =(44]C --j9Xc1oS)cll>N.N $  O> AN N Os$A2B6B B B, B''B,r7)rrrDrs``` rrrUs O, r)FN)rsrrimportlib.utilrry hyperonpyratomsrrrbaserr r r r rr9r=rGrrrrrr7rrrsu # 00;;*(Y(YT77*3*3Xu(u(n##J8!8!t,r