concepts.dsl.executors.function_domain_executor.FunctionDomainExecutor#

class FunctionDomainExecutor[source]#

Bases: DSLExecutorBase

The executor for simple function DSLs.

Methods

execute(expr[, grounding])

Execute an expression.

execute_function(function, *args[, grounding])

Execute a function with a list of arguments.

get_function_implementation(name)

Get the implementation of a function.

has_function_implementation(name)

Check whether the executor has an implementation for a function.

parse_expression(string)

Parse an expression from a string.

register_function(name, func)

Register an implementation for a function to the executor.

register_function_implementation(name, func)

Register an implementation for a function.

unwrap_values(func_or_ftype)

A function decorator that automatically unwraps the values of the arguments of the function.

with_grounding(grounding)

A context manager for setting the grounding of the executor.

Attributes

domain

The function domain of the executor.

function_implementations

The implementations of functions, which is a mapping from function names to implementations.

grounding

The grounding of the current execution.

parser

The parser of the DSL.

__init__(domain, parser=None)[source]#

Initialize the executor.

Parameters:
__new__(**kwargs)#
execute(expr, grounding=None)[source]#

Execute an expression.

Parameters:
  • expr (str | Expression) – the expression to execute.

  • grounding (Any | None) – the grounding of the expression.

Returns:

The result of the execution.

Return type:

Value

execute_function(function, *args, grounding=None)[source]#

Execute a function with a list of arguments.

Parameters:
  • function (Function) – the function to execute.

  • *args (Value) – the arguments of the function.

  • grounding (Any | None) – the grounding of the function.

Returns:

The result of the execution.

Return type:

Value

get_function_implementation(name)#

Get the implementation of a function. When the executor does not have an implementation for the function, the implementation of the function in the domain will be returned. If that is also None, a KeyError will be raised.

Parameters:

name (str) – the name of the function.

Returns:

the implementation of the function.

Return type:

Callable

has_function_implementation(name)#

Check whether the executor has an implementation for a function.

Parameters:

name (str) – the name of the function.

Returns:

whether the executor has an implementation for the function.

Return type:

bool

parse_expression(string)[source]#

Parse an expression from a string.

Parameters:

string (str) – the string to parse.

Returns:

The parsed expression.

Return type:

Expression

register_function(name, func)#

Register an implementation for a function to the executor. Alias for register_function_implementation().

Parameters:
  • name (str) – the name of the function.

  • func (Callable) – the implementation of the function.

register_function_implementation(name, func)#

Register an implementation for a function.

Parameters:
  • name (str) – the name of the function.

  • func (Callable) – the implementation of the function.

unwrap_values(func_or_ftype)[source]#

A function decorator that automatically unwraps the values of the arguments of the function. Basically, this decorator will unwrap the values of the arguments of the function, and then wrap the result with the concepts.dsl.value.Value class.

There are two ways to use this decorator. The first way is to use it as a function decorator: In this case, the wrapped function should have the same name as the DSL function it implements.

>>> domain = FunctionDomain()
>>> # Assume domain has a function named "add" with two arguments.
>>> executor = FunctionDomainExecutor(domain)
>>> @executor.unwrap_values
>>> def add(a, b):
>>>     return a + b
>>> executor.register_function('add', add)

The second way is to use it as function that generates a function decorator:

>>> domain = FunctionDomain()
>>> # Assume domain has a function named "add" with two arguments.
>>> executor = FunctionDomainExecutor(domain)
>>> @executor.unwrap_values(domain.functions['add'].ftype)
>>> def add(a, b):
>>>     return a + b
>>> executor.register_function('add', executor.unwrap_values(add))
Parameters:

func_or_ftype (Callable | FunctionType) – the function to wrap, or the function type of the function to wrap.

Returns:

The decorated function or a function decorator.

Return type:

Callable

with_grounding(grounding)[source]#

A context manager for setting the grounding of the executor.

Parameters:

grounding (Any) – the grounding to set.

property domain: FunctionDomain#

The function domain of the executor.

property function_implementations: Dict[str, Callable]#

The implementations of functions, which is a mapping from function names to implementations.

property grounding: Any#

The grounding of the current execution.

parser: ParserBase#

The parser of the DSL.