typedb_driver/concept/instance.rs
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20use super::{AttributeType, EntityType, RelationType, Value};
21use crate::common::IID;
22
23// TODO: Storing the Type here is *extremely* inefficient; we could be effectively creating
24// 1 million copies of the same data when matching concepts of homogeneous types
25/// Instance of data of an entity type, representing a standalone object
26/// that exists in the data model independently.
27/// Entity does not have a value. It is usually addressed by its ownership over attribute instances
28/// and/or roles played in relation instances.
29#[derive(Clone, Debug, PartialEq, Eq)]
30pub struct Entity {
31 /// The unique id of this Entity
32 pub iid: IID,
33 /// The type which this Entity belongs to
34 pub type_: Option<EntityType>,
35}
36
37impl Entity {
38 /// Retrieves the unique id of the `Entity`.
39 ///
40 /// # Examples
41 ///
42 /// ```rust
43 /// entity.iid();
44 /// ```
45 pub fn iid(&self) -> &IID {
46 &self.iid
47 }
48
49 pub fn type_(&self) -> Option<&EntityType> {
50 self.type_.as_ref()
51 }
52}
53
54/// Relation is an instance of a relation type and can be uniquely addressed by
55/// a combination of its type, owned attributes and role players.
56#[derive(Clone, Debug, PartialEq, Eq)]
57pub struct Relation {
58 /// The unique id of this Relation
59 pub iid: IID,
60 /// The type which this Relation belongs to
61 pub type_: Option<RelationType>,
62}
63
64impl Relation {
65 /// Retrieves the unique id of the `Relation`.
66 ///
67 /// # Examples
68 ///
69 /// ```rust
70 /// relation.iid();
71 /// ```
72 pub fn iid(&self) -> &IID {
73 &self.iid
74 }
75
76 pub fn type_(&self) -> Option<&RelationType> {
77 self.type_.as_ref()
78 }
79}
80
81/// Attribute is an instance of the attribute type and has a value.
82/// This value is fixed and unique for every given instance of the attribute type.
83/// Attributes can be uniquely addressed by their type and value.
84#[derive(Clone, Debug, PartialEq)]
85pub struct Attribute {
86 /// The unique id of this Attribute (internal use only)
87 pub iid: IID,
88 /// The (dataful) value of this attribute
89 pub value: Value,
90 /// The type which this Attribute belongs to
91 pub type_: Option<AttributeType>,
92}
93
94impl Attribute {
95 pub fn type_(&self) -> Option<&AttributeType> {
96 self.type_.as_ref()
97 }
98}