Source code for concepts.dm.crow.action_utils

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# File   : action_utils.py
# Author : Jiayuan Mao
# Email  : maojiayuan@gmail.com
# Date   : 03/17/2024
#
# This file is part of Project Concepts.
# Distributed under terms of the MIT license.

from typing import Any, Union, NamedTuple, List, Dict
from concepts.dsl.expression import ValueOutputExpression

from concepts.dsl.dsl_types import Variable, ObjectConstant
from concepts.dsl.expression_utils import surface_fol_downcast
from concepts.dm.crow.action import CrowAction
from concepts.dm.crow.domain import CrowDomain, CrowState
from concepts.dm.crow.executors.crow_executor import CrowExecutor


[docs]class ApplicableActionItem(NamedTuple): action: CrowAction bounded_variables: Dict[str, Union[Variable, ObjectConstant]]
[docs]def match_applicable_actions( domain: CrowDomain, state: CrowState, goal: ValueOutputExpression, goal_scope: Dict[str, Any], return_all_candidates: bool = True ) -> List[ApplicableActionItem]: candidate_actions = list() for action in domain.actions.values(): goal_expr = action.goal if (variable_binding := surface_fol_downcast(goal_expr, goal)) is None: continue for variable, value in variable_binding.items(): if isinstance(value, Variable): # assert value.name in goal_scope, f"Variable {value.name} not found in goal scope." if value.name in goal_scope: variable_binding[variable] = goal_scope[value.name] candidate_actions.append(ApplicableActionItem(action, variable_binding)) # TODO: implement return_all_candidates return candidate_actions