type_bridge.expressions.role_player¶
role_player
¶
Role-player expression for type-safe relation filtering.
This module provides a type-safe wrapper for filtering role players by their attributes in relation queries.
TypeDB 3.x Variable Scoping: TypeDB uses variable bindings to create implicit equality constraints. If the same variable is used twice in a match clause, both bindings must have the same value.
Wrong approach:
$actor has name $name; -- $name binds to actor's name
$target has name $name; -- CONSTRAINT: target's name must EQUAL actor's name!
Correct approach (unique variables):
$actor has name $actor_name; -- $actor_name binds to actor's name
$target has name $target_name; -- $target_name binds to target's name (independent)
This is why expressions generate ${var_prefix}_${attr_name} patterns.
RolePlayerExpr
¶
Bases: Expression
Type-safe expression for filtering role players by their attributes.
Wraps an attribute expression and associates it with a specific role, ensuring type safety and proper TypeQL variable scoping.
TypeDB 3.x Note: Variable names are prefixed with the entity variable to avoid collisions. Using the same variable twice creates an implicit equality constraint.
Example
If both actor and target have 'name' attribute:¶
$actor has name $actor_name; $actor_name contains "Bot"; $target has name $target_name; $target_name == "Resource1";
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
role_name
|
str
|
The role name (e.g., "employee", "employer", "actor") |
required |
inner_expr
|
Expression
|
The attribute expression to apply (e.g., Age.gt(Age(30))) |
required |
player_types
|
tuple[type[T], ...]
|
Tuple of allowed entity types that can play this role |
required |
Example
expr = RolePlayerExpr("employee", Age.gt(Age(30)), (Person,)) expr.to_typeql("$employee") '$employee has age $employee_age; $employee_age > 30'
Initialize a role player expression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
role_name
|
str
|
Name of the role being filtered |
required |
inner_expr
|
Expression
|
Attribute expression to apply to the role player |
required |
player_types
|
tuple[type[T], ...]
|
Tuple of entity types allowed to play this role |
required |
Source code in type_bridge/expressions/role_player.py
to_ast
¶
Generate AST patterns using role-prefixed variable names.
Delegates to the inner expression with role variable.
get_attribute_types
¶
Get all attribute types referenced by this expression.
Delegates to the inner expression.
Returns:
| Type | Description |
|---|---|
set[type[Attribute]]
|
Set of attribute types used in this expression |
Source code in type_bridge/expressions/role_player.py
__repr__
¶
Return string representation for debugging.