inexor_rgf_core_model/
relation_instance.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use std::collections::HashMap;
use std::fmt::Display;
use std::fmt::Formatter;

use indradb::EdgeKey;
use indradb::EdgeProperties;
use serde::Deserialize;
use serde::Serialize;
use serde_json::Map;
use serde_json::Value;
use uuid::Uuid;

use crate::Extension;
use crate::ExtensionContainer;
use crate::ExtensionTypeId;
use crate::MutablePropertyInstanceSetter;
use crate::NamespacedTypeGetter;
use crate::PropertyInstanceGetter;
use crate::RelationInstanceTypeId;
use crate::RelationTypeId;
use crate::TypeDefinition;
use crate::TypeDefinitionGetter;

/// Relation instances are edges from an outbound entity instance to an
/// inbound entity instance.
///
/// The relation instance is of a relation type. The relation type defines
/// the entity types of the outbound entity instance and the inbound entity
/// instance. Furthermore the relation type defines which properties
/// (name, data type, socket type) a relation instance have to have.
///
/// In contrast to the relation type, the relation instance stores values/
/// documents in it's properties.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct RelationInstance {
    /// The id of the outbound vertex.
    pub outbound_id: Uuid,

    /// The type definition of the relation instance type.
    #[serde(flatten)]
    pub ty: RelationInstanceTypeId,

    /// The id of the inbound vertex.
    pub inbound_id: Uuid,

    /// Textual description of the relation instance.
    #[serde(default = "String::new")]
    pub description: String,

    /// The properties of then relation instance.
    ///
    /// Each property is represented by it's name (String) and it's value. The value is
    /// a representation of a JSON. Therefore the value can be boolean, number, string,
    /// array or an object. For more information about the data types please look at
    /// <https://docs.serde.rs/serde_json/value/enum.Value.html>
    #[serde(default = "HashMap::new")]
    pub properties: HashMap<String, Value>,

    /// Relation instance specific extensions.
    #[serde(default = "Vec::new")]
    pub extensions: Vec<Extension>,
}

impl RelationInstance {
    /// Constructs a new relation instance with the given outbound_id, type, inbound_id and properties
    pub fn new<T: Into<RelationInstanceTypeId>>(outbound_id: Uuid, ty: T, inbound_id: Uuid, properties: HashMap<String, Value>) -> RelationInstance {
        RelationInstance {
            outbound_id,
            ty: ty.into(),
            inbound_id,
            description: String::new(),
            properties,
            extensions: Vec::new(),
        }
    }

    /// Constructs a new relation instance with the given outbound_id, type, inbound_id and properties
    pub fn new_from_type_unique_id<S: Into<String>>(
        namespace: S,
        outbound_id: Uuid,
        type_name: S,
        inbound_id: Uuid,
        properties: HashMap<String, Value>,
    ) -> RelationInstance {
        RelationInstance {
            outbound_id,
            ty: RelationInstanceTypeId::new_from_type_unique_id(namespace, type_name),
            inbound_id,
            description: String::new(),
            properties,
            extensions: Vec::new(),
        }
    }

    /// Constructs a new relation instance with the given outbound_id, type, inbound_id and properties
    pub fn new_from_type_unique_for_instance_id<S: Into<String>>(
        namespace: S,
        outbound_id: Uuid,
        type_name: S,
        instance_id: S,
        inbound_id: Uuid,
        properties: HashMap<String, Value>,
    ) -> RelationInstance {
        RelationInstance {
            outbound_id,
            ty: RelationInstanceTypeId::new_from_type_unique_for_instance_id(namespace, type_name, instance_id),
            inbound_id,
            description: String::new(),
            properties,
            extensions: Vec::new(),
        }
    }

    /// Constructs a new relation instance with the given outbound_id, type, inbound_id and properties
    pub fn new_from_type_with_random_instance_id<S: Into<String>>(
        namespace: S,
        outbound_id: Uuid,
        type_name: S,
        inbound_id: Uuid,
        properties: HashMap<String, Value>,
    ) -> RelationInstance {
        RelationInstance {
            outbound_id,
            ty: RelationInstanceTypeId::new_from_type_with_random_instance_id(namespace, type_name),
            inbound_id,
            description: String::new(),
            properties,
            extensions: Vec::new(),
        }
    }

    /// Constructs a new relation instance with the given outbound_id, type, inbound_id but without properties
    pub fn new_without_properties<T: Into<RelationInstanceTypeId>>(outbound_id: Uuid, ty: T, inbound_id: Uuid) -> RelationInstance {
        RelationInstance {
            outbound_id,
            ty: ty.into(),
            inbound_id,
            description: String::new(),
            properties: HashMap::new(),
            extensions: Vec::new(),
        }
    }

    /// Returns the inner relation type id.
    pub fn relation_type_id(&self) -> RelationTypeId {
        self.ty.relation_type_id()
    }

    /// Returns the relation instance type id.
    pub fn instance_id(&self) -> String {
        self.ty.instance_id()
    }

    /// Returns the edge key of the relation instance.
    pub fn get_key(&self) -> EdgeKey {
        EdgeKey::new(self.outbound_id, self.type_id(), self.inbound_id)
    }
}

impl TryFrom<EdgeProperties> for RelationInstance {
    type Error = ();

    fn try_from(properties: EdgeProperties) -> Result<Self, Self::Error> {
        let ty = RelationInstanceTypeId::try_from(&properties.edge.key.t)?;
        Ok(RelationInstance {
            outbound_id: properties.edge.key.outbound_id,
            ty,
            inbound_id: properties.edge.key.inbound_id,
            description: String::new(),
            properties: properties.props.iter().map(|p| (p.name.to_string(), p.value.clone())).collect(),
            extensions: Vec::new(),
        })
    }
}

impl PropertyInstanceGetter for RelationInstance {
    fn get<S: Into<String>>(&self, property_name: S) -> Option<Value> {
        self.properties.get(&property_name.into()).cloned()
    }

    fn as_bool<S: Into<String>>(&self, property_name: S) -> Option<bool> {
        self.properties.get(&property_name.into()).and_then(|p| p.as_bool())
    }

    fn as_u64<S: Into<String>>(&self, property_name: S) -> Option<u64> {
        self.properties.get(&property_name.into()).and_then(|p| p.as_u64())
    }

    fn as_i64<S: Into<String>>(&self, property_name: S) -> Option<i64> {
        self.properties.get(&property_name.into()).and_then(|p| p.as_i64())
    }

    fn as_f64<S: Into<String>>(&self, property_name: S) -> Option<f64> {
        self.properties.get(&property_name.into()).and_then(|p| p.as_f64())
    }

    fn as_string<S: Into<String>>(&self, property_name: S) -> Option<String> {
        self.properties.get(&property_name.into()).and_then(|p| p.as_str().map(|s| s.to_string()))
    }

    fn as_array<S: Into<String>>(&self, property_name: S) -> Option<Vec<Value>> {
        self.properties.get(&property_name.into()).and_then(|p| p.as_array().map(Vec::clone))
    }

    fn as_object<S: Into<String>>(&self, property_name: S) -> Option<Map<String, Value>> {
        self.properties.get(&property_name.into()).and_then(|p| p.as_object().map(Map::clone))
    }
}

impl MutablePropertyInstanceSetter for RelationInstance {
    fn set<S: Into<String>>(&mut self, property_name: S, value: Value) {
        if let Some(property_value) = self.properties.get_mut(&property_name.into()) {
            *property_value = value
        }
    }
}

impl ExtensionContainer for RelationInstance {
    fn has_own_extension(&self, extension_ty: &ExtensionTypeId) -> bool {
        self.extensions.iter().any(|extension| &extension.ty == extension_ty)
    }

    fn get_own_extension(&self, extension_ty: &ExtensionTypeId) -> Option<Extension> {
        self.extensions.iter().find(|extension| &extension.ty == extension_ty).cloned()
    }

    fn merge_extensions(&mut self, extensions_to_merge: Vec<Extension>) {
        for extension_to_merge in extensions_to_merge {
            if !self.has_own_extension(&extension_to_merge.ty) {
                self.extensions.push(extension_to_merge);
            } else if let Some(existing_extension) = self.extensions.iter_mut().find(|e| e.ty == extension_to_merge.ty) {
                existing_extension.description = extension_to_merge.description.clone();
                existing_extension.extension = extension_to_merge.extension.clone();
            }
        }
    }
}

impl NamespacedTypeGetter for RelationInstance {
    fn namespace(&self) -> String {
        self.ty.namespace()
    }

    fn type_name(&self) -> String {
        self.ty.type_name()
    }
}

impl TypeDefinitionGetter for RelationInstance {
    fn type_definition(&self) -> TypeDefinition {
        self.ty.type_definition()
    }
}

impl Display for RelationInstance {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}--[{}]-->{}", self.outbound_id, &self.ty, self.inbound_id)
    }
}