relationalai.std.as_bool()
as_bool(expr: Expression) -> ExpressionConverts a filter expression to a Boolean expression that produces True when the filter expression is satisfied and False otherwise.
Must be used in a rule or query context.
Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
expr | Expression | The filter expression to convert to a Boolean expression. |
Returns
Section titled “Returns”An Expression object.
Example
Section titled “Example”Use as_bool() to convert a filter expression to a Boolean expression:
import relationalai as raifrom relationalai.std import as_bool
# =====# SETUP# =====
model = rai.Model("MyModel")Person = model.Type("Person")
with model.rule(): Person.add(name="Alice", age=15) Person.add(name="Bob", age=20)
# =======# EXAMPLE# =======
with model.query() as select: person = Person() is_adult = as_bool(person.age >= 18) response = select(person.name, is_adult)
print(response.results)# name v# 0 Alice False# 1 Bob TrueFilter expressions passed to as_bool() don’t filter results.
Instead, they produce a Boolean value that is True when the filter expression is satisfied and False otherwise.
You can think of as_bool() as sugar for a Model.match() block that assigns Boolean values to a variable:
with model.query() as select: person = Person() with model.match() as is_adult: with person.age >= 18: is_adult.add(True) with model.case(): is_adult.add(False) response = select(person.name, is_adult)
print(response.results)# name v# 0 Alice False# 1 Bob True