Skip to main content

typedb_driver/concept/
thing.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#[derive(Clone, Debug, PartialEq)]
24pub enum Thing {
25    Entity(Entity),
26    Relation(Relation),
27    Attribute(Attribute),
28}
29
30impl Thing {
31    pub fn iid(&self) -> &IID {
32        match self {
33            Self::Entity(entity) => &entity.iid,
34            Self::Relation(relation) => &relation.iid,
35            Self::Attribute(attribute) => &attribute.iid,
36        }
37    }
38}
39
40// TODO: Storing the Type here is *extremely* inefficient; we could be effectively creating
41//       1 million copies of the same data when matching concepts of homogeneous types
42/// Instance of data of an entity type, representing a standalone object
43/// that exists in the data model independently.
44/// Entity does not have a value. It is usually addressed by its ownership over attribute instances
45/// and/or roles played in relation instances.
46#[derive(Clone, Debug, PartialEq, Eq)]
47pub struct Entity {
48    /// The unique id of this Entity
49    pub iid: IID,
50    /// The type which this Entity belongs to
51    pub type_: EntityType,
52    /// If this Thing is inferred by a [Reasoning Rule] or not
53    pub is_inferred: bool,
54}
55
56/// Relation is an instance of a relation type and can be uniquely addressed by
57/// a combination of its type, owned attributes and role players.
58#[derive(Clone, Debug, PartialEq, Eq)]
59pub struct Relation {
60    /// The unique id of this Relation
61    pub iid: IID,
62    /// The type which this Relation belongs to
63    pub type_: RelationType,
64    /// If this Relation is inferred by a [Reasoning Rule] or not
65    pub is_inferred: bool,
66}
67
68/// Attribute is an instance of the attribute type and has a value.
69/// This value is fixed and unique for every given instance of the attribute type.
70/// Attributes can be uniquely addressed by their type and value.
71#[derive(Clone, Debug, PartialEq)]
72pub struct Attribute {
73    /// The unique id of this Attribute
74    pub iid: IID,
75    /// The type which this Attribute belongs to
76    pub type_: AttributeType,
77    /// The value which this Attribute instance holds.
78    pub value: Value,
79    /// If this Attribute is inferred by a [Reasoning Rule] or not
80    pub is_inferred: bool,
81}