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.
Inheritance Hierarchy
Subclassed By
semantics > frontend > base ├── FilterBy └── New
Used By
semantics ├── frontend > base │ ├── Chain │ │ └── annotate │ ├── Concept │ │ └── annotate │ ├── FieldRef │ ├── Fragment │ │ └── annotate │ └── Relationship │ └── annotate └── reasoners > prescriptive > problem └── Problem └── solve_for
Returned By
semantics ├── frontend > base │ ├── Chain │ │ └── __call__ │ ├── Concept │ │ └── __call__ │ └── Relationship │ └── __call__ └── std ├── common │ ├── cast │ ├── hash │ ├── parse_uuid │ ├── range │ ├── raw_source │ └── uuid_to_string ├── constraints │ ├── anyof │ ├── exclusive │ └── unique ├── datetime │ ├── date │ │ ├── add │ │ ├── day │ │ ├── dayofyear │ │ ├── diff │ │ ├── format │ │ ├── fromisoformat │ │ ├── fromordinal │ │ ├── isoweekday │ │ ├── month │ │ ├── period_days │ │ ├── quarter │ │ ├── subtract │ │ ├── to_datetime │ │ ├── week │ │ ├── weekday │ │ └── year │ ├── datetime │ │ ├── add │ │ ├── day │ │ ├── dayofyear │ │ ├── diff │ │ ├── format │ │ ├── fromordinal │ │ ├── hour │ │ ├── isoweekday │ │ ├── minute │ │ ├── month │ │ ├── period_milliseconds │ │ ├── quarter │ │ ├── second │ │ ├── strptime │ │ ├── subtract │ │ ├── to_date │ │ ├── week │ │ ├── weekday │ │ └── year │ ├── days │ ├── hours │ ├── microseconds │ ├── milliseconds │ ├── minutes │ ├── months │ ├── nanoseconds │ ├── seconds │ ├── weeks │ └── years ├── decimals │ ├── parse │ └── parse_decimal ├── floats │ ├── float │ └── parse_float ├── integers │ ├── parse │ ├── parse_int128 │ └── parse_int64 ├── math │ ├── abs │ ├── acos │ ├── acosh │ ├── acot │ ├── asin │ ├── asinh │ ├── atan │ ├── atanh │ ├── cbrt │ ├── ceil │ ├── clip │ ├── cos │ ├── cosh │ ├── cot │ ├── degrees │ ├── erf │ ├── erfinv │ ├── exp │ ├── factorial │ ├── floor │ ├── haversine │ ├── isclose │ ├── isinf │ ├── isnan │ ├── log │ ├── log10 │ ├── log2 │ ├── maximum │ ├── minimum │ ├── natural_log │ ├── pow │ ├── radians │ ├── round │ ├── sign │ ├── sin │ ├── sinh │ ├── sqrt │ ├── tan │ ├── tanh │ └── trunc_divide ├── numbers │ ├── parse │ └── parse_number └── strings ├── concat ├── contains ├── endswith ├── join ├── len ├── levenshtein ├── like ├── lower ├── regex_match ├── replace ├── split_part ├── startswith ├── string ├── strip ├── substring └── upper
Referenced By
RelationalAI Documentation └── Build With RelationalAI └── Understand how PyRel works ├── Build a semantic model │ ├── Define base facts │ └── Derive facts with logic └── Use advanced reasoning > Rules-based reasoning ├── Work with strings │ └── Understand string expressions └── Work with numbers └── Understand numeric expressions