f:ddlZddlmZmZddlmZGddZGddeZd Zd Z d Z d Z d Z dZ GddZGddeZGddZGddZGddZGddZdZdZdZdZdZy) N)Atom BindingsSet)SyntaxNodeTypec:eZdZdZdZdZdZdZdZdZ dZ y ) AbstractSpacezM A virtual base class upon which Spaces can be implemented in Python cy)z=Initialiize the AbstractSpace. Does nothing in the base classNselfs U/var/lib/jenkins/workspace/mettalog/venv/lib/python3.12/site-packages/hyperon/base.py__init__zAbstractSpace.__init__ sctd)z Performs the specified query on the Space. Should be overridden to return a BindingsSet as the result of the query. z!Space::query() is not implemented RuntimeErrorr query_atoms r queryzAbstractSpace.querys >??rctd)zY Adds an Atom to the atom space. Must be implemented in derived classes. zSpace::add() is not implementedrr atoms r addzAbstractSpace.adds<==rctd)z^ Removes an Atom from the atom space. Must be implemented in derived classes. z"Space::remove() is not implementedrrs r removezAbstractSpace.remove s?@@rctd)z_ Replaces an Atom from the atom space. Must be implemented in derived classes. z#Space::replace() is not implementedrr r replacements r replacezAbstractSpace.replace&s@AArcy)z] Counts the number of atoms in the atom space. Optional for derived classes. Nr r s r atom_countzAbstractSpace.atom_count, rcy)z\ Returns an iterator over atoms in the Space. Optional for derived classes. Nr r s r atoms_iterzAbstractSpace.atoms_iter2r"rN) __name__ __module__ __qualname____doc__rrrrrr!r$r rr rrs-@> A B   rrcFeZdZdZfdZdZdZdZdZdZ dZ xZ S) GroundingSpacezy A wrapper over the native GroundingSpace implementation, which can be subclassed and extended within Python c@t|t|_y)zCInitialize GroundingSpace and its underlying native implementation.N)superrGroundingSpaceRefgspace)r __class__s r rzGroundingSpace.__init__=s ') rc8|jj|S)zx Delegates the query to the underlying native GroundingSpace and returns the result BindingsSet )r.rrs r rzGroundingSpace.queryCs {{  ,,rc:|jj|y)z1 Adds an Atom to the atom space. N)r.add_atomrs r rzGroundingSpace.addMs T"rc8|jj|S)z6 Removes an Atom from the atom space. )r. remove_atomrs r rzGroundingSpace.removeSs{{&&t,,rc:|jj||S)z5 Replaces an Atom in the atom space. )r. replace_atom)r from_atomto_atoms r rzGroundingSpace.replaceYs{{'' 7;;rc6|jjS)z? Counts the number of Atoms in the atom space. )r.r!r s r r!zGroundingSpace.atom_count_s{{%%''rcHt|jjS)zC Returns an iterator over atoms in the atom space. )iterr. get_atomsr s r r$zGroundingSpace.atoms_iteresDKK))+,,r) r%r&r'r(rrrrrr!r$ __classcell__)r/s@r r*r*8s+* -# - < ( -rr*cNtj|}|j|S)z Private glue for Hyperonpy implementation. Translates a native 'catom' into an Atom object, and then delegates the query to the provided 'space' object. )r _from_catomr)space query_catomrs r _priv_call_query_on_python_spacerBks# !!+.J ;;z ""rcPtj|}|j|y)z Private glue for Hyperonpy implementation. Translates a native 'catom' into an Atom object, and then adds it to the provided 'space' object. N)rr?rr@catomrs r _priv_call_add_on_python_spacerFts   E "D IIdOrcNtj|}|j|S)z Private glue for Hyperonpy implementation. Translates a native 'catom' into an Atom object, and then removes it from the provided 'space' object. )rr?rrDs r !_priv_call_remove_on_python_spacerH}s#   E "D << rcztj|}tj|}|j||S)z Private glue for Hyperonpy implementation. Translates native 'catom' objects into Atom objects, and then replaces the first with the second in the provided 'space' object. )rr?r)r@cfromctor7r8s r "_priv_call_replace_on_python_spacerLs5   'Is#G ==G ,,rcFt|dr|j}||Syy)zt Private glue for Hyperonpy implementation. Returns the number of Atoms in the provided 'space' object. r!)hasattrr!)r@counts r %_priv_call_atom_count_on_python_spacerQs- ul#  "  Lrc<t|dr|jSy)zw Private glue for Hyperonpy implementation. Returns an iterator over Atoms in the provided 'space' object. r$N)rOr$)r@s r )_priv_call_new_iter_state_on_python_spacerSs ul#!!rcheZdZdZdZdZdZedZdZ dZ dZ d Z d Z d Zd Zd ZdZy)SpaceRefz A reference to a Space, which may be accessed directly, wrapped in a grounded atom, or passed to a MeTTa interpreter. c~t|tjur||_ytj||_y)z Initialize a new SpaceRef based on the given space object, either a CSpace or a custom Python object. N)typehpCSpacecspacespace_new_custom)r space_objs r rzSpaceRef.__init__s-  ?bii '#DK--i8DKrcBtj|jy)z"Free the underlying CSpace object N)rX space_freerZr s r __del__zSpaceRef.__del__s dkk"rcVtj|j|jS)zLCompare two SpaceRef objects for equality, based on their underlying spaces.)rXspace_eqrZ)r others r __eq__zSpaceRef.__eq__s{{4;; 55rct|S)zI Create a new SpaceRef based on the given CSpace object. )rUrZs r _from_cspacezSpaceRef._from_cspaces rc|S)z\ Returns a new copy of the SpaceRef, referencing the same underlying Space. r r s r copyz SpaceRef.copys  rcXtj|j|jy)z+ Add an Atom to the Space. N)rX space_addrZrErs r r2zSpaceRef.add_atoms T[[$**-rcVtj|j|jS)z; Delete the specified Atom from the Space. )rX space_removerZrErs r r4zSpaceRef.remove_atomst{{DJJ77rcltj|j|j|jS)zh Replaces the specified Atom, if it exists in the Space, with the supplied replacement. )rX space_replacerZrErs r r6zSpaceRef.replace_atoms' TZZ9J9JKKrc@tj|jS)zc Returns the number of Atoms in the Space, or -1 if it cannot be readily computed. )rXspace_atom_countrZr s r r!zSpaceRef.atom_counts""4;;//rctj|j}|dk(ryg}|D]&}|jt j |(|S)zZ Returns a list of all Atoms in the Space, or None if that is impossible. N)rX space_listrZappendrr?)r resresultrs r r<zSpaceRef.get_atomssKmmDKK( $;A MM$**1- . rc@tj|jS)z Returns the Space object referenced by the SpaceRef, or None if the object does not have a direct Python interface. )rXspace_get_payloadrZr s r get_payloadzSpaceRef.get_payloads ##DKK00rcltj|j|j}t |S)ze Performs the specified query on the Space, and returns the result as a BindingsSet. )rX space_queryrZrEr)r patternrus r rzSpaceRef.querys' W]];6""rctj|j|j|jDcgc]}t j |c}Scc}w)z^ Performs a substitution within the Space, based on a pattern and a template. )rX space_substrZrErr?)r r|templrEs r substzSpaceRef.substsG t{{GMM).67E  '7 77sAN)r%r&r'r(rr_rc staticmethodrfrhr2r4r6r!r<ryrrr rr rUrUsX 9#6   . 8 L 0 1#7rrUc(eZdZdZddZedZy)r-zX A reference to a native GroundingSpace, implemented by the MeTTa core library. NcJ|tj|_y||_y)z Initialize a new GroundingSpaceRef. If a CSpace object is provided, use it; otherwise create a new GroundingSpace. N)rXspace_new_groundingrZ)r rZs r rzGroundingSpaceRef.__init__ s >002DK DKrct|S)zC Creates a GroundingSpaceRef from a CSpace object. )r-res r rfzGroundingSpaceRef._from_cspaces !((rN)r%r&r'r(rrrfr rr r-r-s !))rr-c4eZdZdZddZedZdZdZy) Tokenizerz A class responsible for text tokenization in the context of Hyperon. This class wraps around a Tokenizer object from the core library. NcJ|tj|_y||_yz- Initialize a new Tokenizer. N)rX tokenizer_new ctokenizer)r rs r rzTokenizer.__init__#s!   ..0DO(DOrct|S)z? Creates a Tokenizer from a CTokenizer object. )r)rs r _from_ctokenizerzTokenizer._from_ctokenizer,s $$rcBtj|jy)zj Destructor that frees the underlying resources when the Tokenizer instance is destroyed. N)rXtokenizer_freerr s r r_zTokenizer.__del__3s $//*rcFtj|j||y)a Registers a new custom Token in the Tokenizer based on a regular expression. Parameters: ---------- regex: A string representing the regular expression to match incoming text. Hyperon uses the Rust RegEx engine and syntax. constr: A constructor function for generating a new atom when the regex is triggered. N)rXtokenizer_register_tokenr)r regexconstrs r register_tokenzTokenizer.register_token9s ##DOOUFCrr) r%r&r'r(rrrr_rr rr rrs+ )%% + Drrc.eZdZdZdZdZdZdZdZy) SyntaxNodez= A class representing a node in a parsed syntax tree c||_yr)cnoder rs r rzSyntaxNode.__init__Ls  rcBtj|jy)z/ Destructor for the SyntaxNode N)rXsyntax_node_freerr s r r_zSyntaxNode.__del__Rs DJJ'rc@tj|jS)z2 Returns the type of a SyntaxNode )rXsyntax_node_typerr s r get_typezSyntaxNode.get_typeXs""4::..rcdtj|j}t|d|dS)zm Returns the range of offsets into the source code of the text represented by the SyntaxNode rr)rXsyntax_node_src_rangerrange)r range_tuples r src_rangezSyntaxNode.src_range^s-..tzz: [^[^44rcg}tj|jD]}|jt ||S)z\ Returns a list of all leaf nodes recursively contained within a SyntaxNode )rXsyntax_node_unrollrrsr)r syntax_nodesrs r unrollzSyntaxNode.unrolles; **4::6E    5 1 27rN) r%r&r'r(rr_rrrr rr rrGs  ( / 5rrc"eZdZdZdZdZdZy) SExprParserz A class responsible for parsing S-expressions (Symbolic Expressions). This class wraps around a SExprParser object from the core library. c8tj||_y)z$Initialize a new SExprParser object.N)rX CSExprParsercparser)r texts r rzSExprParser.__init__tst, rc|jj|j}|(|jj}|yt |t j |S)zG Parses the S-expression using the provided Tokenizer. N)rparsersexpr_parser_err_str SyntaxErrorrr?)r tokenizerrEerr_strs r rzSExprParser.parsexsY ""9#7#78 Mll779G!'**##E* *rcT|jj}| t|SdS)zh Parses the S-expression into a SyntaxNode representing the top-level of a syntax tree. N)rparse_to_syntax_treerrs r rz SExprParser.parse_to_syntax_trees+ 113$)$5z% ?4?rN)r%r&r'r(rrrr rr rrns - +@rrc.eZdZdZdZdZdZdZdZy) Interpreterz A wrapper class for the MeTTa interpreter that handles the interpretation of expressions in a given grounding space. NOTE: This is a low-level API, and most applications would be better served by a `MeTTa` runner object cbtj|j|j|_y)z\ Initializes the interpreter with the given grounding space and expression. N)rXinterpret_initrZrE step_result)r gnd_spaceexprs r rzInterpreter.__init__s#,,Y-=-=tzzJrc@tj|jS)zW Checks if there are more steps to execute in the interpretation plan. )rX step_has_nextrr s r has_nextzInterpreter.has_nexts 0 011rc|js ttj|j|_y)zD Executes the next step in the interpretation plan. N)r StopIterationrXinterpret_steprr s r nextzInterpreter.nexts.}}/ !,,T-=-=>rcv|jr tdtj|jS)zI Retrieves the final outcome of the interpretation plan. zPlan execution is not finished)rrrXstep_get_resultrr s r get_resultzInterpreter.get_results0 ==??@ @!!$"2"233rc|jS)zE Gets the current result of the interpretation plan. )rr s r get_step_resultzInterpreter.get_step_resultsrN) r%r&r'r(rrrrrr rr rrs! K 2 ?4 rrct||}|jr!|j|jr!|jDcgc]}t j |c}Scc}w)zG Parses the given expression in the specified grounding space. )rrrrrr?)rr interpreterrEs r interpretrs^i.K        1<1G1G1I JD  U # JJ JsA/cltj|j|j|jS)a Checks whether the given Atom has the specified type in the given space context. Parameters ---------- gnd_space: A pointer to the space_t representing the space context in which to perform the check atom: A pointer to the atom_t or atom_ref_t representing the atom whose Type the function will check type: A pointer to the atom_t or atom_ref_t representing the type to check against )rX check_typerZrE)rrrWs r rrs% ==))4::tzz BBrcVtj|j|jS)a Checks whether the given Atom is correctly typed. Parameters ---------- gnd_space: A pointer to the space_t representing the space context in which to perform the check atom: A pointer to the atom_t or atom_ref_t representing the atom whose Type the function will check Returns ------- True if the Atom is correctly typed, otherwise false )rX validate_atomrZrE)rrs r rrs!"   I,,djj 99rctj|j|j}|Dcgc]}t j |c}Scc}w)zHProvides all types for the given Atom in the context of the given Space.)rXget_atom_typesrZrErr?)rrrurEs r rrs=   y// rs$$0 0 d1-]1-f#- ]7]7~)),(D(DT%%N@@>' ' TKC$:&9 (r