Skip to content

Expression

relationalai.semantics.frontend.base
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.

  • op

    (Relationship | Concept) - The operation being applied.
  • args

    (Sequence[Value]) - Positional arguments. Python primitives are wrapped as Literal.
  • kwargs

    (dict, default: None) - Keyword field values for the call.
  • root

    (Chain | Relationship | Concept, default: None) - Root used for chained expressions.

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 Boots
>>> m.where(alice.pets == "Boots").select(alice).to_df()

Most users should not instantiate this class directly.

Expression.__getattr__(item: str) -> Chain

Follow 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:

  • item

    (str) - 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 the model.implicit_properties config 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 name
>>> m.select(works_at(alice).name).to_df()
Expression.__getitem__(field: str | int | Concept) -> FieldRef

Return a reference to one of this expression’s relationship fields.

Indexing an Expression returns a FieldRef for the underlying relationship.

Parameters:

  • field

    (str | 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 - If field is an integer index that is out of range.
  • KeyError - If no field matches the provided selector.
ExpressionVariableDSLBase
 semantics
├──  frontend > base
│   ├──  FilterBy
│   └──  New
└──  std > re
    └──  RegexMatch
 semantics > frontend > base
├──  Chain
│   └──  annotate
├──  Concept
│   └──  annotate
├──  FieldRef
├──  Fragment
│   └──  annotate
└──  Relationship
    └──  annotate
 semantics
├──  frontend > base
│   ├──  Chain
│   │   └──  __call__
│   ├──  Concept
│   │   └──  __call__
│   └──  Relationship
│       └──  __call__
└──  std
    ├──  common
    │   ├──  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
    │   │   ├──  now
    │   │   ├──  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
    │   ├──  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
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