as_bool()#

relationalai.std
#as_bool(expr: Expression) -> Expression

Converts 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#

NameTypeDescription
exprExpressionThe filter expression to convert to a Boolean expression.

Returns#

An Expression object.

Example#

Use as_bool() to convert a filter expression to a Boolean expression:

#import relationalai as rai
from 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   True

Filter 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

See Also#