Implementation:AIScientists Dev WorldSeed aggregation
| Knowledge Sources | |
|---|---|
| Domains | DSL, Query |
| Last Updated | 2026-10-26 18:00 GMT |
Overview
Concrete DSL functions for count, sum, and max_by aggregation queries with compound WHERE support, provided by the WorldSeed library.
Description
The aggregation module registers four DSL functions: `count(type=X, where=...)` counts matching entities; `sum(type=X, property=Y, where=...)` sums a numeric property across matches; `max_by(type=X, property=Y, where=...)` returns the entity ID with the highest property value (returns `""` on tie or empty — a director-signal contract for tiebreak requests); `max_by_key(path)` returns the key with the highest value from a dict at a resolved path. All support compound `AND`-joined WHERE clauses with `==`/`!=` operators, and RHS values are resolved once via the path resolver before iterating entities. System entities (e.g. narrator) are excluded from all queries.
Usage
Use these functions in scene YAML DSL expressions when you need to query aggregate state — e.g., counting alive agents, summing votes, or finding the leading candidate. The `""` tie sentinel from `max_by` enables branching to a tiebreak action.
Code Reference
Source Location
- Repository: WorldSeed
- File: src/worldseed/dsl/functions/aggregation.py
Signature
def count(store: StateStore, entity_type: str, where: str | None = None, ctx: dict[str, Any] | None = None) -> int: ...
def sum_property(store: StateStore, entity_type: str, prop: str, where: str | None = None, ctx: dict[str, Any] | None = None) -> float: ...
def _call_max_by(args_str: str, store: StateStore, ctx: dict[str, Any]) -> str:
"""max_by(type=X, property=Y, where=...) → entity ID with highest Y. "" on tie/empty."""
def _call_max_by_key(args_str: str, store: StateStore, ctx: dict[str, Any]) -> str:
"""max_by_key(path) → key with highest value from dict at path."""
Import
# Auto-registered on import of the functions package:
# import worldseed.dsl.functions # registers "count", "sum", "max_by", "max_by_key"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| type | str | Yes | Entity type to query |
| property | str | Yes (sum/max_by) | Dot-path to the numeric property |
| where | str | No | Compound WHERE clause (e.g. `location == town AND infected == true`) |
| path | str | Yes (max_by_key) | DSL path resolving to a dict |
Outputs
| Name | Type | Description |
|---|---|---|
| count returns | int | Number of matching non-system entities |
| sum returns | float | Sum of property across matches |
| max_by returns | str | Entity ID with highest value, or "" on tie/empty |
| max_by_key returns | str | Dict key with highest value, or "" on tie/empty |
Usage Examples
# Count alive agents
preconditions:
- operator: check
left: "count(type='agent', where='alive == true')"
op: ">="
right: 2
# Find the winner (tie triggers director signal)
effects:
- operator: set
target: "election.winner"
value: "max_by(type='candidate', property='votes')"
# Sum total gold across all players
- operator: set
target: "bank.total_gold"
value: "sum(type='player', property='gold')"