Lesson 11: Manipulating stateful objects
|
This documentation is not current - it is for TypeDB 2.x, which is no longer supported. View the documentation for TypeDB 3.x: |
Retrieval by query
-
When issued using a TypeDB driver, Get queries are used to retrieve stateful objects. They comprise a
matchclause and agetclause.match # match clause get; -
The
TypeDBTransaction.query.getmethod is used to execute Get queries. It returns an iterator of results in the form of concept maps. A concept map is a collection of stateful objects that were matched by the variables in the query.results: Iterator[ConceptMap] = transaction.query.fetch(QUERY) -
The
ConceptMap.getmethod is used to retrieve a stateful object from the concept map corresponding to a particular variable label. The object must then be cast to the appropriate type.entity: Entity = result.get(VARIABLE_1).as_entity() relation: Relation = result.get(VARIABLE_2).as_relation() attribute: Attribute = result.get(VARIABLE_3).as_attribute() entity_type: EntityType = result.get(VARIABLE_4).as_entity_type() relation_type: RelationType = result.get(VARIABLE_5).as_relation_type() attribute_type: AttributeType = result.get(VARIABLE_6).as_attribute_type() -
One result is returned per variable for each match returned by the
matchclause. The set of results can be filtered before being returned using thegetclause. The results are filtered for each unique combination of instances of the filter variables.get $a, $b, $c;
Programmatic retrieval
-
The
TypeDBTransaction.conceptsproperty is used to access programmatic schema type retrieval methods. Itsget_entity_type,get_relation_type, andget_attribute_typemethods are used to retrieve entity, relation, and attribute types respectively. These methods return promises, and must be resolved using theirresolvemethods in order to obtain the responses.entity_type: EntityType = transaction.concepts.get_entity_type(LABEL_1).resolve() relation_type: RelationType = transaction.concepts.get_relation_type(LABEL_2).resolve() attribute_type: AttributeType = transaction.concepts.get_attribute_type(LABEL_3).resolve() -
The
RelationType.get_relatesmethod is used to retrieve the roles in a relation type.role_types: Iterator[RoleType] = relation_type.get_relates(transaction) -
The
Type.get_instancesmethod is used to retrieve all instances of a type.entities: Iterator[Entity] = entity_type.get_instances(transaction) relations: Iterator[Relation] = relation_type.get_instances(transaction) attributes: Iterator[Attribute] = attribute_type.get_instances(transaction) -
The
Entity.get_hasandRelation.get_hasmethods are used to retrieve the attributes owned by an entity or relation.attributes: Iterator[Attribute] = entity.get_has(transaction) -
The
Entity.get_relationsandRelation.get_relationsmethods are used to retrieve the relations that an entity or relation plays a role in.relations: Iterator[Relation] = entity.get_relations(transaction) -
The
Attribute.get_ownersmethod is used to retrieve the owners of an attribute.owners: Iterator[Thing] = attribute.get_owners(transaction) -
The
Relation.get_players_by_role_typemethod is used to retrieve the roleplayers in a relation.players: Iterator[Thing] = relation.get_players(transaction) -
Because the owners of an attribute or the roleplayers in a relation can be both entities and relations, the objects returned by
get_ownersandget_players_by_role_typeare of the genericThingclass and must be cast to the appropriate type.def cast_thing(thing: Thing) -> Entity | Relation: if thing.is_entity(): return thing.as_entity() if thing.is_relation(): return thing.as_relation()
Operating on objects
-
The
EntityType.create,RelationType.create, andAttributeType.putmethods are used to instantiate entity, relation, and attribute types respectively. When instantiating an attribute, its value must be provided as an argument.entity: Entity = entity_type.create(transaction).resolve() relation: Relation = relation_type.create(transaction).resolve() attribute: Attribute = attribute_type.put(transaction, VALUE).resolve() -
The
Entity.set_hasandRelation.set_hasmethods are used to give an entity or relation ownership of an attribute.entity.set_has(attribute, transaction).resolve() -
The
Entity.unset_hasandRelation.unset_hasmethods are used to remove ownership of an attribute from an entity or relation.entity.unset_has(attribute, transaction).resolve() -
The
Relation.add_playersmethod is used to add roleplayers to a relation.relation.add_player(transaction, entity, role_type).resolve() -
The
Entity.deleteandRelation.deletemethods are used to delete entities and relations.relation.delete(transaction).resolve()