Skip to content

base

AbstractSpace

AbstractSpace()

A virtual base class upon which Spaces can be implemented in Python

Source code in python/hyperon/base.py
def __init__(self):
    """Initialiize the AbstractSpace. Does nothing in the base class"""
    return

add

add(atom)

Adds an Atom to the atom space. Must be implemented in derived classes.

Source code in python/hyperon/base.py
def add(self, atom):
    """
    Adds an Atom to the atom space. Must be implemented in derived classes.
    """
    raise RuntimeError("Space::add() is not implemented")

atom_count

atom_count()

Counts the number of atoms in the atom space. Optional for derived classes.

Source code in python/hyperon/base.py
def atom_count(self):
    """
    Counts the number of atoms in the atom space. Optional for derived classes.
    """
    None

atoms_iter

atoms_iter()

Returns an iterator over atoms in the Space. Optional for derived classes.

Source code in python/hyperon/base.py
def atoms_iter(self):
    """
    Returns an iterator over atoms in the Space. Optional for derived classes.
    """
    None

query

query(query_atom)

Performs the specified query on the Space. Should be overridden to return a BindingsSet as the result of the query.

Source code in python/hyperon/base.py
def query(self, query_atom):
    """
    Performs the specified query on the Space.
    Should be overridden to return a BindingsSet as the result of the query.
    """
    raise RuntimeError("Space::query() is not implemented")

remove

remove(atom)

Removes an Atom from the atom space. Must be implemented in derived classes.

Source code in python/hyperon/base.py
def remove(self, atom):
    """
    Removes an Atom from the atom space. Must be implemented in derived classes.
    """
    raise RuntimeError("Space::remove() is not implemented")

replace

replace(atom, replacement)

Replaces an Atom from the atom space. Must be implemented in derived classes.

Source code in python/hyperon/base.py
def replace(self, atom, replacement):
    """
    Replaces an Atom from the atom space. Must be implemented in derived classes.
    """
    raise RuntimeError("Space::replace() is not implemented")

GroundingSpace

GroundingSpace()

Bases: AbstractSpace

A wrapper over the native GroundingSpace implementation, which can be subclassed and extended within Python

Source code in python/hyperon/base.py
def __init__(self):
    """Initialize GroundingSpace and its underlying native implementation."""
    super().__init__()
    # self.cspace = hp.space_new_grounding()
    self.gspace = GroundingSpaceRef()

add

add(atom)

Adds an Atom to the atom space.

Source code in python/hyperon/base.py
def add(self, atom):
    """
    Adds an Atom to the atom space.
    """
    self.gspace.add_atom(atom)

atom_count

atom_count()

Counts the number of Atoms in the atom space.

Source code in python/hyperon/base.py
def atom_count(self):
    """
    Counts the number of Atoms in the atom space.
    """
    return self.gspace.atom_count()

atoms_iter

atoms_iter()

Returns an iterator over atoms in the atom space.

Source code in python/hyperon/base.py
def atoms_iter(self):
    """
    Returns an iterator over atoms in the atom space.
    """
    return iter(self.gspace.get_atoms())

query

query(query_atom)

Delegates the query to the underlying native GroundingSpace and returns the result BindingsSet

Source code in python/hyperon/base.py
def query(self, query_atom):
    """
    Delegates the query to the underlying native GroundingSpace
    and returns the result BindingsSet
    """
    return self.gspace.query(query_atom)

remove

remove(atom)

Removes an Atom from the atom space.

Source code in python/hyperon/base.py
def remove(self, atom):
    """
    Removes an Atom from the atom space.
    """
    return self.gspace.remove_atom(atom)

replace

replace(from_atom, to_atom)

Replaces an Atom in the atom space.

Source code in python/hyperon/base.py
def replace(self, from_atom, to_atom):
    """
    Replaces an Atom in the atom space.
    """
    return self.gspace.replace_atom(from_atom, to_atom)

GroundingSpaceRef

GroundingSpaceRef(cspace=None)

Bases: SpaceRef

A reference to a native GroundingSpace, implemented by the MeTTa core library.

Source code in python/hyperon/base.py
def __init__(self, cspace = None):
    """
    Initialize a new GroundingSpaceRef.
    If a CSpace object is provided, use it; otherwise create a new GroundingSpace.
    """
    if cspace is None:
        self.cspace = hp.space_new_grounding()
    else:
        self.cspace = cspace

__del__

__del__()

Free the underlying CSpace object

Source code in python/hyperon/base.py
def __del__(self):
    """Free the underlying CSpace object """
    hp.space_free(self.cspace)

__eq__

__eq__(other)

Compare two SpaceRef objects for equality, based on their underlying spaces.

Source code in python/hyperon/base.py
def __eq__(self, other):
    """Compare two SpaceRef objects for equality, based on their underlying spaces."""
    return hp.space_eq(self.cspace, other.cspace)

add_atom

add_atom(atom)

Add an Atom to the Space.

Source code in python/hyperon/base.py
def add_atom(self, atom):
    """
    Add an Atom to the Space.
    """
    hp.space_add(self.cspace, atom.catom)

atom_count

atom_count()

Returns the number of Atoms in the Space, or -1 if it cannot be readily computed.

Source code in python/hyperon/base.py
def atom_count(self):
    """
    Returns the number of Atoms in the Space, or -1 if it cannot be readily computed.
    """
    return hp.space_atom_count(self.cspace)

copy

copy()

Returns a new copy of the SpaceRef, referencing the same underlying Space.

Source code in python/hyperon/base.py
def copy(self):
    """
    Returns a new copy of the SpaceRef, referencing the same underlying Space.
    """
    return self

get_atoms

get_atoms()

Returns a list of all Atoms in the Space, or None if that is impossible.

Source code in python/hyperon/base.py
def get_atoms(self):
    """
    Returns a list of all Atoms in the Space, or None if that is impossible.
    """
    res = hp.space_list(self.cspace)
    if res == None:
        return None
    result = []
    for r in res:
        result.append(Atom._from_catom(r))
    return result

get_payload

get_payload()

Returns the Space object referenced by the SpaceRef, or None if the object does not have a direct Python interface.

Source code in python/hyperon/base.py
def get_payload(self):
    """
    Returns the Space object referenced by the SpaceRef, or None if the object does not have a
    direct Python interface.
    """
    return hp.space_get_payload(self.cspace)

query

query(pattern)

Performs the specified query on the Space, and returns the result as a BindingsSet.

Source code in python/hyperon/base.py
def query(self, pattern):
    """
    Performs the specified query on the Space, and returns the result as a BindingsSet.
    """
    result = hp.space_query(self.cspace, pattern.catom)
    return BindingsSet(result)

remove_atom

remove_atom(atom)

Delete the specified Atom from the Space.

Source code in python/hyperon/base.py
def remove_atom(self, atom):
    """
    Delete the specified Atom from the Space.
    """
    return hp.space_remove(self.cspace, atom.catom)

replace_atom

replace_atom(atom, replacement)

Replaces the specified Atom, if it exists in the Space, with the supplied replacement.

Source code in python/hyperon/base.py
def replace_atom(self, atom, replacement):
    """
    Replaces the specified Atom, if it exists in the Space, with the supplied replacement.
    """
    return hp.space_replace(self.cspace, atom.catom, replacement.catom)

subst

subst(pattern, templ)

Performs a substitution within the Space, based on a pattern and a template.

Source code in python/hyperon/base.py
def subst(self, pattern, templ):
    """
    Performs a substitution within the Space, based on a pattern and a template.
    """
    return [Atom._from_catom(catom) for catom in
            hp.space_subst(self.cspace, pattern.catom,
                                     templ.catom)]

Interpreter

Interpreter(gnd_space, expr)

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

Source code in python/hyperon/base.py
def __init__(self, gnd_space, expr):
    """
    Initializes the interpreter with the given grounding space and expression.
    """
    self.step_result = hp.interpret_init(gnd_space.cspace, expr.catom)

get_result

get_result()

Retrieves the final outcome of the interpretation plan.

Source code in python/hyperon/base.py
def get_result(self):
    """
    Retrieves the final outcome of the interpretation plan.
    """
    if self.has_next():
        raise RuntimeError("Plan execution is not finished")
    return hp.step_get_result(self.step_result)

get_step_result

get_step_result()

Gets the current result of the interpretation plan.

Source code in python/hyperon/base.py
def get_step_result(self):
    """
    Gets the current result of the interpretation plan.
    """
    return self.step_result

has_next

has_next()

Checks if there are more steps to execute in the interpretation plan.

Source code in python/hyperon/base.py
def has_next(self):
    """
    Checks if there are more steps to execute in the interpretation plan.
    """
    return hp.step_has_next(self.step_result)

next

next()

Executes the next step in the interpretation plan.

Source code in python/hyperon/base.py
def next(self):
    """
    Executes the next step in the interpretation plan.
    """
    if not self.has_next():
        raise StopIteration()
    self.step_result = hp.interpret_step(self.step_result)

SExprParser

SExprParser(text)

A class responsible for parsing S-expressions (Symbolic Expressions). This class wraps around a SExprParser object from the core library.

Source code in python/hyperon/base.py
def __init__(self, text):
    """Initialize a new SExprParser object."""
    self.cparser = hp.CSExprParser(text)

parse

parse(tokenizer)

Parses the S-expression using the provided Tokenizer.

Source code in python/hyperon/base.py
def parse(self, tokenizer):
    """
    Parses the S-expression using the provided Tokenizer.
    """
    catom = self.cparser.parse(tokenizer.ctokenizer)
    if (catom is None):
        err_str = self.cparser.sexpr_parser_err_str()
        if (err_str is None):
            return None
        else:
            raise SyntaxError(err_str)
    else:
        return Atom._from_catom(catom)

parse_to_syntax_tree

parse_to_syntax_tree()

Parses the S-expression into a SyntaxNode representing the top-level of a syntax tree.

Source code in python/hyperon/base.py
def parse_to_syntax_tree(self):
    """
    Parses the S-expression into a SyntaxNode representing the top-level of a syntax tree.
    """
    cnode = self.cparser.parse_to_syntax_tree()
    return SyntaxNode(cnode) if cnode is not None else None

SpaceRef

SpaceRef(space_obj)

A reference to a Space, which may be accessed directly, wrapped in a grounded atom, or passed to a MeTTa interpreter.

Source code in python/hyperon/base.py
def __init__(self, space_obj):
    """
    Initialize a new SpaceRef based on the given space object, either a CSpace 
    or a custom Python object.
    """
    if type(space_obj) is hp.CSpace:
        self.cspace = space_obj
    else:
        self.cspace = hp.space_new_custom(space_obj)

__del__

__del__()

Free the underlying CSpace object

Source code in python/hyperon/base.py
def __del__(self):
    """Free the underlying CSpace object """
    hp.space_free(self.cspace)

__eq__

__eq__(other)

Compare two SpaceRef objects for equality, based on their underlying spaces.

Source code in python/hyperon/base.py
def __eq__(self, other):
    """Compare two SpaceRef objects for equality, based on their underlying spaces."""
    return hp.space_eq(self.cspace, other.cspace)

add_atom

add_atom(atom)

Add an Atom to the Space.

Source code in python/hyperon/base.py
def add_atom(self, atom):
    """
    Add an Atom to the Space.
    """
    hp.space_add(self.cspace, atom.catom)

atom_count

atom_count()

Returns the number of Atoms in the Space, or -1 if it cannot be readily computed.

Source code in python/hyperon/base.py
def atom_count(self):
    """
    Returns the number of Atoms in the Space, or -1 if it cannot be readily computed.
    """
    return hp.space_atom_count(self.cspace)

copy

copy()

Returns a new copy of the SpaceRef, referencing the same underlying Space.

Source code in python/hyperon/base.py
def copy(self):
    """
    Returns a new copy of the SpaceRef, referencing the same underlying Space.
    """
    return self

get_atoms

get_atoms()

Returns a list of all Atoms in the Space, or None if that is impossible.

Source code in python/hyperon/base.py
def get_atoms(self):
    """
    Returns a list of all Atoms in the Space, or None if that is impossible.
    """
    res = hp.space_list(self.cspace)
    if res == None:
        return None
    result = []
    for r in res:
        result.append(Atom._from_catom(r))
    return result

get_payload

get_payload()

Returns the Space object referenced by the SpaceRef, or None if the object does not have a direct Python interface.

Source code in python/hyperon/base.py
def get_payload(self):
    """
    Returns the Space object referenced by the SpaceRef, or None if the object does not have a
    direct Python interface.
    """
    return hp.space_get_payload(self.cspace)

query

query(pattern)

Performs the specified query on the Space, and returns the result as a BindingsSet.

Source code in python/hyperon/base.py
def query(self, pattern):
    """
    Performs the specified query on the Space, and returns the result as a BindingsSet.
    """
    result = hp.space_query(self.cspace, pattern.catom)
    return BindingsSet(result)

remove_atom

remove_atom(atom)

Delete the specified Atom from the Space.

Source code in python/hyperon/base.py
def remove_atom(self, atom):
    """
    Delete the specified Atom from the Space.
    """
    return hp.space_remove(self.cspace, atom.catom)

replace_atom

replace_atom(atom, replacement)

Replaces the specified Atom, if it exists in the Space, with the supplied replacement.

Source code in python/hyperon/base.py
def replace_atom(self, atom, replacement):
    """
    Replaces the specified Atom, if it exists in the Space, with the supplied replacement.
    """
    return hp.space_replace(self.cspace, atom.catom, replacement.catom)

subst

subst(pattern, templ)

Performs a substitution within the Space, based on a pattern and a template.

Source code in python/hyperon/base.py
def subst(self, pattern, templ):
    """
    Performs a substitution within the Space, based on a pattern and a template.
    """
    return [Atom._from_catom(catom) for catom in
            hp.space_subst(self.cspace, pattern.catom,
                                     templ.catom)]

SyntaxNode

SyntaxNode(cnode)

A class representing a node in a parsed syntax tree

Source code in python/hyperon/base.py
def __init__(self, cnode):
    """
    Initialize a new Tokenizer.
    """
    self.cnode = cnode

__del__

__del__()

Destructor for the SyntaxNode

Source code in python/hyperon/base.py
def __del__(self):
    """
    Destructor for the SyntaxNode
    """
    hp.syntax_node_free(self.cnode)

get_type

get_type()

Returns the type of a SyntaxNode

Source code in python/hyperon/base.py
def get_type(self):
    """
    Returns the type of a SyntaxNode
    """
    return hp.syntax_node_type(self.cnode)

src_range

src_range()

Returns the range of offsets into the source code of the text represented by the SyntaxNode

Source code in python/hyperon/base.py
def src_range(self):
    """
    Returns the range of offsets into the source code of the text represented by the SyntaxNode
    """
    range_tuple = hp.syntax_node_src_range(self.cnode)
    return range(range_tuple[0], range_tuple[1])

unroll

unroll()

Returns a list of all leaf nodes recursively contained within a SyntaxNode

Source code in python/hyperon/base.py
def unroll(self):
    """
    Returns a list of all leaf nodes recursively contained within a SyntaxNode
    """
    syntax_nodes = []
    for cnode in hp.syntax_node_unroll(self.cnode):
        syntax_nodes.append(SyntaxNode(cnode))
    return syntax_nodes

Tokenizer

Tokenizer(ctokenizer=None)

A class responsible for text tokenization in the context of Hyperon. This class wraps around a Tokenizer object from the core library.

Source code in python/hyperon/base.py
def __init__(self, ctokenizer = None):
    """
    Initialize a new Tokenizer.
    """
    if ctokenizer is None:
        self.ctokenizer = hp.tokenizer_new()
    else:
        self.ctokenizer = ctokenizer

__del__

__del__()

Destructor that frees the underlying resources when the Tokenizer instance is destroyed.

Source code in python/hyperon/base.py
def __del__(self):
    """
    Destructor that frees the underlying resources when the Tokenizer instance is destroyed.
    """
    hp.tokenizer_free(self.ctokenizer)

register_token

register_token(regex, constr)

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.

Source code in python/hyperon/base.py
def register_token(self, regex, constr):
    """
    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.
   """
    hp.tokenizer_register_token(self.ctokenizer, regex, constr)

atom_is_error

atom_is_error(atom)

Checks whether an Atom is an error expression

Source code in python/hyperon/base.py
def atom_is_error(atom):
    """Checks whether an Atom is an error expression"""
    return hp.atom_is_error(atom.catom)

check_type

check_type(gnd_space, atom, type)

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

Source code in python/hyperon/base.py
def check_type(gnd_space, atom, type):
    """
    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
    """

    return hp.check_type(gnd_space.cspace, atom.catom, type.catom)

get_atom_types

get_atom_types(gnd_space, atom)

Provides all types for the given Atom in the context of the given Space.

Source code in python/hyperon/base.py
def get_atom_types(gnd_space, atom):
    """Provides all types for the given Atom in the context of the given Space."""
    result = hp.get_atom_types(gnd_space.cspace, atom.catom)
    return [Atom._from_catom(catom) for catom in result]

interpret

interpret(gnd_space, expr)

Parses the given expression in the specified grounding space.

Source code in python/hyperon/base.py
def interpret(gnd_space, expr):
    """
    Parses the given expression in the specified grounding space.
    """
    interpreter = Interpreter(gnd_space, expr)
    while interpreter.has_next():
        interpreter.next()
    return [Atom._from_catom(catom) for catom in interpreter.get_result()]

validate_atom

validate_atom(gnd_space, atom)

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

Source code in python/hyperon/base.py
def validate_atom(gnd_space, atom):
    """
    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
    """
    return hp.validate_atom(gnd_space.cspace, atom.catom)