Type#
relationalai
#class Type
Types are used to categorize objects in a model.
You create types using the Model.Type()
method,
which returns an instance of the Type
class.
Attributes#
Methods#
Name | Description | Returns |
---|---|---|
.__call__() | Query objects of this type. | Instance |
.__or__(value) | Create a union of two types. | TypeUnion |
.add(*args, **kwargs) | Add a new object to the model. | Instance |
.define(**kwargs) | Define properties for a type based on relationships with other types. | None |
.extend(others) | Extend the type with all objects from another type. | None |
.known_properties() | Get all known properties of objects of type Type . | List[str] |
Example#
Use Model.Type()
to create a Type
object rather than constructing one directly:
#import relationalai as rai
# Create a new model named "people" with Person and Adult types.
model = rai.Model("people")
Person = model.Type("Person")
Adult = model.Type("Adult")
# Add some people to the model in a rule context using Person.add().
with model.rule():
Person.add(name="Alice", age=30)
Person.add(name="Bob", age=20)
Person.add(name="Carol", age=10)
# All people who are at least 18 years old are adults.
with model.rule():
person = Person() # Get all Person objects.
person.age >= 18 # Filter for people who are at least 18 years old.
person.set(Adult) # Set each person as a member of the Adult type.
# Query the model for names of adults.
with model.query() as select:
adult = Adult() # Get all Adult objects.
response = select(adult.name) # Select the name of each adult.
print(response.results)
# Output:
# name
# 0 Alice
# 1 Bob