Type.__call__()#
relationalai
#Type.__call__(
self,
*args: Type | TypeUnion | TypeIntersection,
**kwargs: Any,
) -> Instance
Returns an Instance
used to reference objects of the given type in a rule or query context.
Parameters#
Name | Type | Description |
---|---|---|
*args | Type , TypeUnion , TypeIntersection | Filter the objects referenced by the returned Instance to those that are also instances of all the specified types. These types are used as additional constraints on the Instance . |
**kwargs | Any | Filter the objects referenced by the returned Instance to those that have properties matching the specified key-value pairs. |
Returns#
An Instance
object.
Example#
#import relationalai as rai
# =====
# SETUP
# =====
model = rai.Model("BookModel")
# Declare Book, Fiction, and NonFiction types.
Book = model.Type("Book")
Fiction = model.Type("Fiction")
NonFiction = model.Type("NonFiction")
# Define some Book objects.
with model.rule():
Book.add(Fiction, title="Foundation", author="Isaac Asimov")
Book.add(NonFiction, title="Humble Pi", author="Matt Parker")
# =======
# EXAMPLE
# =======
# Call a Type to get an Instance that references objects of that type.
# Properties of objects are accessed as attributes of the Instance.
with model.query() as select:
book = Book()
response = select(book.title)
print(response.results)
# title
# 0 Foundation
# 1 Humble Pi
# You can filter the objects referenced by the returned Instance by passing
# additional types as positional arguments and property values as keyword arguments.
with model.query() as select:
# Get a reference to all Fiction books with the title "Foundation".
book = Book(Fiction, title="Foundation")
response = select(book.author)
print(response.results)
# author
# 0 Isaac Asimov
# The above query is equivalent to the following:
with model.query() as select:
book = Book()
Fiction(book) # Filter for Fiction books.
book.title == "Foundation" # Filter for books with the title "Foundation".
response = select(book.author)
print(response.results)
# author
# 0 Isaac Asimov
# When you pass multiple types as positional arguments, the Instance references
# objects that are instances of all the specified types.
with model.query() as select:
book = Book(Fiction, NonFiction)
response = select(book.title, book.author)
# The response is empty since no books are both Fiction and NonFiction.
print(response.results)