type_bridge.crud.role_players¶
role_players
¶
Role player utilities for relations.
build_role_player_match
¶
Build a match clause for a role player entity.
Prefers IID-based matching when available (more precise and faster), falls back to key attribute matching, and raises a clear error if neither is available.
Used by TypeDBManager for relation CRUD operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
var_name
|
str
|
The variable name to use (without $) |
required |
entity
|
Any
|
The entity instance |
required |
entity_type_name
|
str
|
The TypeDB type name for the entity |
required |
Returns:
| Type | Description |
|---|---|
str
|
A TypeQL match clause string like "$var_name isa type, iid 0x..." |
str
|
or "$var_name isa type, has key_attr value" |
Raises:
| Type | Description |
|---|---|
ValueError
|
If entity has neither _iid nor key attributes |
Source code in type_bridge/crud/role_players.py
resolve_entity_class_from_label
¶
Resolve the correct Python entity class from a TypeDB type label.
This is a thin wrapper around ModelRegistry.resolve() that provides polymorphic class resolution with caching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_label
|
str | None
|
The TypeDB type label (from label() function), e.g., "person" |
required |
allowed_entity_classes
|
tuple[type[Entity], ...]
|
Tuple of allowed entity classes for this role |
required |
Returns:
| Type | Description |
|---|---|
type[Entity]
|
The matching Python entity class, or the first allowed class as fallback |
Source code in type_bridge/crud/role_players.py
build_role_player_fetch_items
¶
Build fetch items for role players with their IIDs and type labels.
TypeQL 3.x doesn't allow mixing iid() with .* in the same nested block, so we fetch the IID as a separate key alongside the nested attributes. We also fetch the type label to correctly identify polymorphic role players.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
role_info
|
dict[str, tuple[str, Any]]
|
Dict mapping role_name -> (role_var, allowed_entity_types) |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of fetch item strings like: '"employee_iid": iid($employee)' '"employee_type": label($employee_type)' '"employee": { $employee.* }' |
The caller must add type variable bindings to the match clause:
$employee isa $employee_type;
Source code in type_bridge/crud/role_players.py
group_results_by_iid
¶
Group query results by relation/entity IID.
TypeDB returns one row per role player combination for multi-cardinality roles. This utility groups those rows by IID so they can be merged into a single relation instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
list[dict[str, Any]]
|
List of result dictionaries from TypeDB fetch |
required |
Returns:
| Type | Description |
|---|---|
dict[str, list[dict[str, Any]]]
|
Dictionary mapping IID -> list of result rows with that IID |
Source code in type_bridge/crud/role_players.py
extract_relation_attributes
¶
Extract relation attributes from a query result.
Uses the unified wrap_attribute_value() helper for consistent handling of multi-value and single-value attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_class
|
type[Relation]
|
The relation class to extract attributes for |
required |
result
|
dict[str, Any]
|
The result dictionary from TypeDB fetch |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary mapping field_name -> attribute value (ready for model constructor) |
Source code in type_bridge/crud/role_players.py
extract_role_players_from_results
¶
Extract and deduplicate role players from grouped query results.
For multi-player roles, collects all unique players from all rows in the group. For single-player roles, takes the first player found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result_group
|
list[dict[str, Any]]
|
List of result rows with the same relation IID |
required |
role_info
|
dict[str, tuple[str, tuple[type[Entity], ...]]]
|
Dict mapping role_name -> (role_var, allowed_entity_classes) |
required |
multi_player_roles
|
set[str]
|
Set of role names that allow multiple players |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary mapping role_name -> player entity or list of player entities |