Expression
Expression( op: Relationship | Concept, args: Sequence[Value], kwargs: dict | None = None, root: Chain | Relationship | Concept | None = None,)Represents an expression in the DSL.
Expression objects are produced when you call a Relationship,
Property, Chain, or Concept (and by operators
like == and +). You can pass them to Model.define,
Model.where, and Model.select.
Because expressions are composable, you can build complex nested expressions that combine multiple relationships, properties, and operators.
Parameters
(opRelationship|Concept) - The operation being applied.
(argsSequence[Value]) - Positional arguments. Python primitives are wrapped asLiteral.
(kwargsdict, default:None) - Keyword field values for the call.
(rootChain|Relationship|Concept, default:None) - Root used for chained expressions.
Examples
Build a query by comparing a relationship’s output to a literal value:
from relationalai.semantics import Model, String
m = Model()Person = m.Concept("Person")Person.pets = m.Relationship(f"{Person} has pet {String:pet_name}")m.define(alice := Person.new(name="Alice"), alice.pets("Boots"))# The expression alice.pets == "Boots" produces an Expression# that we can use in a where clause to filter to people whose pet is Bootsm.where(alice.pets == "Boots").select(alice).to_df()Notes
Most users should not instantiate this class directly.
Methods
.__getattr__()
Expression.__getattr__(item: str) -> ChainFollow a relationship/property from this expression.
Attribute access returns a Chain
rooted at this expression. This is mainly useful when a relationship call
leaves its output “open” (by providing fewer than the full number of
arguments), and you then want to access another relationship/property from
that output.
Names that start with an underscore are treated as normal Python attribute access.
Parameters:
(itemstr) - Relationship/property name to access next.
Returns:
Chain- A chained value representing the extended relationship path.
Raises:
relationalai.util.error.RAIException- If the relationship/property cannot be resolved (for example when themodel.implicit_propertiesconfig flag is disabled).
Examples:
Access a property on a relationship’s output field via chaining from an expression:
from relationalai.semantics import Model, String
m = Model()Person = m.Concept("Person")Company = m.Concept("Company")works_at = m.Relationship(f"{Person} works at {Company}")Company.name = m.Property(f"{Company} is named {String:name}")alice = Person.new(name="Alice")# Follow the relationship output (Company) and then read its namem.select(works_at(alice).name).to_df().__getitem__()
Expression.__getitem__(field: str | int | Concept) -> FieldRefReturn a reference to one of this expression’s relationship fields.
Indexing an Expression returns a FieldRef for
the underlying relationship.
Parameters:
(fieldstr|int|Concept) - Field selector: a 0-based index, an exact field name, or a field type (concept).
Returns:
FieldRef- A reference to the selected field.
Raises:
TypeError- If this expression is not a relationship call.IndexError- Iffieldis an integer index that is out of range.KeyError- If no field matches the provided selector.