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 the implementation of a function.
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
The function domain of the executor.
The implementations of functions, which is a mapping from function names to implementations.
The grounding of the current execution.
The parser of the DSL.
- __init__(domain, parser=None)[source]#
Initialize the executor.
- Parameters:
domain (FunctionDomain) – the function domain.
parser (ParserBase | None) – the parser of the DSL. If not specified, the default
FunctionExpressionParser
will be used.
- __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:
- execute_function(function, *args, grounding=None)[source]#
Execute a function with a list of arguments.
- 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.
- has_function_implementation(name)#
Check whether the executor has an implementation for a function.
- parse_expression(string)[source]#
Parse an expression from a string.
- Parameters:
string (str) – the string to parse.
- Returns:
The parsed expression.
- Return type:
- register_function(name, func)#
Register an implementation for a function to the executor. Alias for
register_function_implementation()
.
- register_function_implementation(name, func)#
Register an implementation for a 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:
- 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.
- parser: ParserBase#
The parser of the DSL.