ContextSelect.__getattr__()#
#ContextSelect.__getattr__(name: str) -> Instance | InstanceProperty
Gets properties of values assigned to a ContextSelect
object.
May only be called from ContextSelect
objects created in a Model.match()
block.
Parameters#
Name | Type | Description |
---|---|---|
name | str | The name of the property to get. |
Returns#
An Instance
or an InstanceProperty
.
Example#
.__getattr__()
is called when you access an attribute of a ContextSelect
object using the dot (.
) operator.
It returns properties of values assigned to the ContextSelect
object:
#import relationalai as rai
from relationalai.std import alias
# =====
# SETUP
# =====
model = rai.Model("MyModel")
Item = model.Type("Item")
with model.rule():
Item.add(id=1).set(name="A", price=10.00)
Item.add(id=2).set(name="B", price=10.00)
Item.add(id=3).set(name="C", price=20.00)
# =======
# EXAMPLE
# =======
# Compute an item's discount based on its price.
with model.query() as select:
item = Item()
with model.match() as discount: # discount is a ContextSelect object
# Items with a price greater than 15 get a 25% discount.
with item.price > 15:
discount.add(0.25, reason="Over $15")
# All other items get a 10% discount.
with model.case():
discount.add(0.1, reason="$15 or less")
# Compute the discounted price.
discounted_price = item.price * (1 - discount)
response = select(
item.id,
item.name,
item.price,
alias(discount, "discount"),
alias(discounted_price, "discounted_price"),
# discount.reason accesses the reason property set in discount.add().
alias(discount.reason, "reason"),
)
print(response.results)
# id name price discount discounted_price reason
# 0 1 A 10.0 0.10 9.0 $15 or less
# 1 2 B 10.0 0.10 9.0 $15 or less
# 2 3 C 20.0 0.25 15.0 Over $15
If the value assigned to the ContextSelect
is a Producer
, such as an Instance
or an InstanceProperty
, then .__getattr__()
may also return properties of the Producer
set elsewhere in the model:
## Declare a Discount type and define some Discount objects.
Discount = model.Type("Discount")
with model.rule():
Discount.add(rate=0.25).set(code="25OFF")
Discount.add(rate=0.10).set(code="10OFF")
# Compute an item's discount based on its price.
with model.query() as select:
item = Item()
with model.match() as discount:
# Items with a price greater than 15 get a 25% discount.
with item.price > 15:
discount.add(Discount(rate=0.25))
# All other items get a 10% discount.
with model.case():
discount.add(Discount(rate=0.10))
# Compute the discounted price. Properties of the Discount object can be
# accessed directly from the discount variable.
discounted_price = item.price * (1 - discount.rate)
response = select(
item.name,
item.price,
alias(discount.code, "discount_code"),
alias(discounted_price, "discounted_price"),
)
print(response.results)
# name price discount_code discounted_price
# 0 A 10.0 10OFF 9.0
# 1 B 10.0 10OFF 9.0
# 2 C 20.0 25OFF 15.0
Properties set in ContextSelect.add()
take precedence over properties set elsewhere with the same name:
#with model.query() as select:
item = Item()
with model.match() as discount:
with item.price > 15:
# The code property set here takes precedence over the code property
# set on the Discount object.
discount.add(Discount(rate=0.25), code="SUMMER25")
with model.case():
discount.add(Discount(rate=0.10), code="SUMMER10")
discounted_price = item.price * (1 - discount.rate)
response = select(
item.name,
item.price,
alias(discount.code, "discount_code"),
alias(discounted_price, "discounted_price"),
)
print(response.results)
# name price discount_code discounted_price
# 0 A 10.0 SUMMER10 9.0
# 1 B 10.0 SUMMER10 9.0
# 2 C 20.0 SUMMER25 15.0
See Model.match()
for more information on .match()
contexts.