gale.ai.scripting module
This file contains a small data-driven “scripting” layer for AI: a Registry of named actions/conditions/tests/weights, plus build_behavior_tree and build_decision_tree, which recursively turn a plain, JSON-friendly dict into a gale.ai.behavior_tree.BehaviorTree or gale.ai.decision_tree.DecisionTree. This lets a designer describe an agent’s decision-making as data (hand-written, generated, or loaded from a JSON file) instead of writing Python subclasses for every leaf, without needing a custom text-based language and its own parser.
Author: Alejandro Mujica (aledrums@gmail.com)
- class gale.ai.scripting.Registry[source]
Bases:
objectHolds the named building blocks a behavior tree/decision tree spec can refer to by name instead of by direct Python reference: actions (agent, dt) -> Status/Any, conditions/tests (agent) -> bool, and named weights for RandomDecisionNode branches.
Usage example:
registry = Registry() registry.register_action(“patrol”, lambda agent, dt: agent.patrol(dt)) registry.register_condition(“enemy_visible”, lambda agent: agent.can_see_enemy())
- register_action(name, function)[source]
- Parameters:
name (str) – Name a spec can refer to this action by.
function (Callable[[Any, float], Any]) – Callable(agent, dt) implementing it.
- Return type:
None
- register_condition(name, predicate)[source]
- Parameters:
name (str) – Name a spec can refer to this condition by.
predicate (Callable[[Any], bool]) – Callable(agent) -> bool implementing it.
- Return type:
None
- gale.ai.scripting.build_behavior_tree(spec, registry)[source]
Recursively build a behavior tree node from a spec such as:
- {
“type”: “selector”, “children”: [
- {“type”: “sequence”, “children”: [
{“type”: “condition”, “name”: “enemy_visible”}, {“type”: “action”, “name”: “attack”},
]}, {“type”: “action”, “name”: “patrol”},
],
}
Recognized “type” values: “sequence”/”selector” (need “children”: a list of specs), “parallel” (also accepts “success_threshold”/ “failure_threshold”), “inverter”/”succeeder”/”failer”/ “until_success”/”until_failure” (need “child”: a single spec), “repeater” (needs “child”, accepts “times”), “cooldown” (needs “child” and “duration”), “action” (needs “name”, looked up in registry), “condition” (needs “name”, looked up in registry).
- Parameters:
spec (Dict[str, Any]) – The spec to build a node from.
registry (Registry) – Where “action”/”condition” names are looked up.
- Returns:
The built Node. Wrap the result in gale.ai.behavior_tree.BehaviorTree to tick it.
- Raises:
KeyError – If spec’s “type” is not one of the recognized values above, or an “action”/”condition” name is not registered.
- Return type:
- gale.ai.scripting.build_decision_tree(spec, registry)[source]
Recursively build a decision tree node from a spec such as:
- {
“type”: “decision”, “test”: “low_health”, “true”: {“type”: “action”, “name”: “flee”}, “false”: {“type”: “action”, “name”: “attack”},
}
Recognized “type” values: “decision” (needs “test”, “true”, “false”), “random” (needs “branches”: a list of [spec, weight] pairs), “action” (needs “name”, looked up in registry).
- Parameters:
spec (Dict[str, Any]) – The spec to build a node from.
registry (Registry) – Where “test”/”action” names are looked up.
- Returns:
The built DecisionTreeNode. Wrap the result in gale.ai.decision_tree.DecisionTree to evaluate it.
- Raises:
KeyError – If spec’s “type” is not one of the recognized values above, or a “test”/”action” name is not registered.
- Return type: