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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
use uri::Uri;
use triple::*;
use namespace::*;
use node::*;
use std::slice::Iter;
use std::collections::HashMap;
use Result;


/// Representation of an RDF graph.
#[derive(Debug)]
pub struct Graph {
  /// Base URI of the RDF graph.
  base_uri: Option<Uri>,

  /// All triples of the RDF graph.
  triples: TripleStore,

  /// All namespaces associated to the RDF graph.
  namespaces: NamespaceStore,

  /// Next unique ID that can be used for a new blank node.
  next_id: u64
}


impl Graph {
  /// Constructor for the RDF graph.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::graph::Graph;
  ///
  /// let graph = Graph::new(None);
  /// ```
  pub fn new(base_uri: Option<&Uri>) -> Graph {
    let cloned_uri = match base_uri {
      None => None,
      Some(u) => Some(u.clone())
    };

    Graph {
      base_uri: cloned_uri,
      triples: TripleStore::new(),
      namespaces: NamespaceStore::new(),
      next_id: 0
    }
  }

  /// Returns `true` if the graph does not contain any triples.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::graph::Graph;
  ///
  /// let graph = Graph::new(None);
  ///
  /// assert_eq!(graph.is_empty(), true);
  /// ```
  pub fn is_empty(&self) -> bool {
    self.triples.is_empty()
  }

  /// Returns the number of triples that are stored in the graph.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::graph::Graph;
  ///
  /// let graph = Graph::new(None);
  ///
  /// assert_eq!(graph.count(), 0);
  /// ```
  pub fn count(&self) -> usize {
    self.triples.count()
  }

  /// Returns the base URI of the graph.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::uri::Uri;
  /// use rdf::graph::Graph;
  ///
  /// let base_uri = Uri::new("http://example.org/".to_string());
  /// let graph = Graph::new(Some(&base_uri));
  ///
  /// assert_eq!(graph.base_uri(), &Some(base_uri));
  /// ```
  pub fn base_uri(&self) -> &Option<Uri> {
    &self.base_uri
  }

  /// Sets the base URI of the graph.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::uri::Uri;
  /// use rdf::graph::Graph;
  ///
  /// let base_uri = Uri::new("http://base.example.org/".to_string());
  /// let mut graph = Graph::new(None);
  ///
  /// graph.set_base_uri(&base_uri);
  ///
  /// assert_eq!(graph.base_uri(), &Some(base_uri));
  /// ```
  pub fn set_base_uri(&mut self, uri: &Uri) {
    self.base_uri = Some(uri.clone());
  }

  /// Returns a hash map of namespaces and prefixes.
  pub fn namespaces(&self) -> &HashMap<String, Uri> {
    self.namespaces.namespaces()
  }

  /// Adds a new namespace with a specific prefix to the graph.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::uri::Uri;
  /// use rdf::graph::Graph;
  /// use rdf::namespace::Namespace;
  ///
  /// let mut graph = Graph::new(None);
  /// graph.add_namespace(&Namespace::new("example".to_string(),
  ///                                     Uri::new("http://example.org/".to_string())));
  ///
  /// assert_eq!(graph.namespaces().len(), 1);
  /// ```
  pub fn add_namespace(&mut self, ns: &Namespace) {
    self.namespaces.add(ns);
  }

  /// Returns the URI of a namespace with the provided prefix.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::uri::Uri;
  /// use rdf::graph::Graph;
  /// use rdf::namespace::Namespace;
  ///
  /// let mut graph = Graph::new(None);
  /// let uri = Uri::new("http://example.org/".to_string());
  /// graph.add_namespace(&Namespace::new("example".to_string(), uri.to_owned()));
  ///
  /// assert_eq!(graph.get_namespace_uri_by_prefix("example".to_string()).unwrap(), &uri);
  /// ```
  ///
  /// # Failures
  ///
  /// - No namespace with the provided prefix exists
  ///
  pub fn get_namespace_uri_by_prefix(&self, prefix: String) -> Result<&Uri> {
    self.namespaces.get_uri_by_prefix(prefix)
  }

  /// Returns a literal node of the specified namespace.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::graph::Graph;
  /// use rdf::node::Node;
  ///
  /// let graph = Graph::new(None);
  /// let literal_node = graph.create_literal_node("literal".to_string());
  ///
  /// assert_eq!(literal_node, Node::LiteralNode {
  ///   literal: "literal".to_string(),
  ///   data_type: None,
  ///   language: None
  /// });
  /// ```
  pub fn create_literal_node(&self, literal: String) -> Node {
    Node::LiteralNode {
      literal: literal,
      data_type: None,
      language: None
    }
  }

  /// Returns a literal node with a specified data type.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::graph::Graph;
  /// use rdf::node::Node;
  /// use rdf::uri::Uri;
  ///
  /// let graph = Graph::new(None);
  /// let literal_node = graph.create_literal_node_with_data_type("literal".to_string(), &Uri::new("http://example.org/show/localName".to_string()));
  ///
  /// assert_eq!(literal_node, Node::LiteralNode {
  ///   literal: "literal".to_string(),
  ///   data_type: Some(Uri::new("http://example.org/show/localName".to_string())),
  ///   language: None
  /// });
  /// ```
  pub fn create_literal_node_with_data_type(&self, literal: String, data_type: &Uri) -> Node {
    Node::LiteralNode {
      literal: literal,
      data_type: Some(data_type.clone()),
      language: None
    }
  }

  /// Returns a literal node with a specified language.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::graph::Graph;
  /// use rdf::node::Node;
  ///
  /// let graph = Graph::new(None);
  /// let literal_node = graph.create_literal_node_with_language("literal".to_string(), "en".to_string());
  ///
  /// assert_eq!(literal_node, Node::LiteralNode {
  ///   literal: "literal".to_string(),
  ///   data_type: None,
  ///   language: Some("en".to_string())
  /// });
  /// ```
  pub fn create_literal_node_with_language(&self, literal: String, language: String) -> Node {
    Node::LiteralNode {
      literal: literal,
      data_type: None,
      language: Some(language)
    }
  }

  /// Returns the next unique ID that can be used for a blank node.
  fn get_next_id(&self) -> u64 {
    self.next_id
  }

  /// Creates a blank node with a unique ID.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::graph::Graph;
  /// use rdf::node::Node;
  ///
  /// let mut graph = Graph::new(None);
  /// let blank_node = graph.create_blank_node();
  ///
  /// assert_eq!(blank_node, Node::BlankNode {
  ///   id: "auto0".to_string()
  /// });
  /// ```
  pub fn create_blank_node(&mut self) -> Node {
    let id = self.get_next_id();

    self.next_id = id + 1;

    Node::BlankNode {
      id: "auto".to_string() + &id.to_string()
    }
  }

  /// Creates a blank node with the specified ID.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::graph::Graph;
  /// use rdf::node::Node;
  ///
  /// let graph = Graph::new(None);
  /// let blank_node = graph.create_blank_node_with_id("foobar".to_string());
  ///
  /// assert_eq!(blank_node, Node::BlankNode {
  ///   id: "foobar".to_string()
  /// });
  /// ```
  pub fn create_blank_node_with_id(&self, id: String) -> Node {
    Node::BlankNode {
      id: id
    }
  }

  /// Creates a new URI node.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::graph::Graph;
  /// use rdf::node::Node;
  /// use rdf::uri::Uri;
  ///
  /// let graph = Graph::new(None);
  /// let uri_node = graph.create_uri_node(&Uri::new("http://example.org/show/localName".to_string()));
  ///
  /// assert_eq!(uri_node, Node::UriNode {
  ///   uri: Uri::new("http://example.org/show/localName".to_string())
  /// });
  /// ```
  pub fn create_uri_node(&self, uri: &Uri) -> Node {
    Node::UriNode {
      uri: uri.clone()
    }
  }

  /// Adds a triple to the graph.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::graph::Graph;
  /// use rdf::uri::Uri;
  /// use rdf::triple::Triple;
  ///
  /// let mut graph = Graph::new(None);
  ///
  /// let subject = graph.create_blank_node();
  /// let predicate = graph.create_uri_node(&Uri::new("http://example.org/show/localName".to_string()));
  /// let object = graph.create_blank_node();
  /// let triple = Triple::new(&subject, &predicate, &object);
  ///
  /// graph.add_triple(&triple);
  ///
  /// assert_eq!(graph.count(), 1);
  /// ```
  pub fn add_triple(&mut self, triple: &Triple) {
    self.triples.add_triple(triple);
  }

  /// Adds a vector of triples.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::graph::Graph;
  /// use rdf::uri::Uri;
  /// use rdf::triple::Triple;
  ///
  /// let mut graph = Graph::new(None);
  ///
  /// let subject = graph.create_blank_node();
  /// let predicate = graph.create_uri_node(&Uri::new("http://example.org/show/localName".to_string()));
  /// let object = graph.create_blank_node();
  ///
  /// let triple1 = Triple::new(&subject, &predicate, &object);
  /// let triple2 = Triple::new(&subject, &predicate, &object);
  ///
  /// graph.add_triples(&vec![triple1, triple2]);
  ///
  /// assert_eq!(graph.count(), 2);
  /// ```
  pub fn add_triples(&mut self, triples: &Vec<Triple>) {
    for triple in triples {
      self.add_triple(triple);
    }
  }

  /// Deletes the triple from the graph.
  ///
  /// # Examples
  /// ```
  /// use rdf::graph::Graph;
  /// use rdf::uri::Uri;
  /// use rdf::triple::Triple;
  ///
  /// let mut graph = Graph::new(None);
  ///
  /// let subject = graph.create_blank_node();
  /// let predicate = graph.create_uri_node(&Uri::new("http://example.org/show/localName".to_string()));
  /// let object = graph.create_blank_node();
  /// let triple = Triple::new(&subject, &predicate, &object);
  ///
  /// graph.add_triple(&triple);
  /// graph.remove_triple(&triple);
  ///
  /// assert_eq!(graph.count(), 0);
  /// ```
  pub fn remove_triple(&mut self, triple: &Triple) {
    self.triples.remove_triple(triple);
  }

  /// Returns all triples from the store that have the specified subject node.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::graph::Graph;
  /// use rdf::uri::Uri;
  /// use rdf::triple::Triple;
  ///
  /// let mut graph = Graph::new(None);
  ///
  /// let subject1 = graph.create_blank_node();
  /// let subject2 = graph.create_blank_node();
  /// let predicate = graph.create_uri_node(&Uri::new("http://example.org/show/localName".to_string()));
  /// let object = graph.create_blank_node();
  ///
  /// let triple1 = Triple::new(&subject1, &predicate, &object);
  /// let triple2 = Triple::new(&subject2, &predicate, &object);
  ///
  /// graph.add_triples(&vec![triple1.to_owned(), triple2]);
  ///
  /// assert_eq!(graph.get_triples_with_subject(&subject1), vec![&triple1]);
  /// ```
  pub fn get_triples_with_subject(&self, node: &Node) -> Vec<&Triple> {
    self.triples.get_triples_with_subject(node)
  }

  /// Returns all triples from the store that have the specified predicate node.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::graph::Graph;
  /// use rdf::uri::Uri;
  /// use rdf::triple::Triple;
  ///
  /// let mut graph = Graph::new(None);
  ///
  /// let subject1 = graph.create_blank_node();
  /// let subject2 = graph.create_blank_node();
  /// let predicate = graph.create_uri_node(&Uri::new("http://example.org/show/localName".to_string()));
  /// let object = graph.create_blank_node();
  ///
  /// let triple1 = Triple::new(&subject1, &predicate, &object);
  /// let triple2 = Triple::new(&subject2, &predicate, &object);
  ///
  /// graph.add_triples(&vec![triple1.to_owned(), triple2.to_owned()]);
  ///
  /// assert_eq!(graph.get_triples_with_predicate(&predicate), vec![&triple1, &triple2]);
  /// ```
  pub fn get_triples_with_predicate(&self, node: &Node) -> Vec<&Triple> {
    self.triples.get_triples_with_predicate(node)
  }

  /// Returns all triples from the store that have the specified object node.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::graph::Graph;
  /// use rdf::uri::Uri;
  /// use rdf::triple::Triple;
  ///
  /// let mut graph = Graph::new(None);
  ///
  /// let subject1 = graph.create_blank_node();
  /// let subject2 = graph.create_blank_node();
  /// let predicate = graph.create_uri_node(&Uri::new("http://example.org/show/localName".to_string()));
  /// let object = graph.create_blank_node();
  ///
  /// let triple1 = Triple::new(&subject1, &predicate, &object);
  /// let triple2 = Triple::new(&subject2, &predicate, &object);
  ///
  /// graph.add_triples(&vec![triple1.to_owned(), triple2.to_owned()]);
  ///
  /// assert_eq!(graph.get_triples_with_object(&object), vec![&triple1, &triple2]);
  /// ```
  pub fn get_triples_with_object(&self, node: &Node) -> Vec<&Triple> {
    self.triples.get_triples_with_object(node)
  }

  /// Returns all triples from the triple store where the subject and object nodes match the provided nodes.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::graph::Graph;
  /// use rdf::uri::Uri;
  /// use rdf::triple::Triple;
  ///
  /// let mut graph = Graph::new(None);
  ///
  /// let subject1 = graph.create_blank_node();
  /// let subject2 = graph.create_blank_node();
  /// let predicate = graph.create_uri_node(&Uri::new("http://example.org/show/localName".to_string()));
  /// let object = graph.create_blank_node();
  ///
  /// let triple1 = Triple::new(&subject1, &predicate, &object);
  /// let triple2 = Triple::new(&subject2, &predicate, &object);
  ///
  /// graph.add_triples(&vec![triple1.to_owned(), triple2]);
  ///
  /// assert_eq!(graph.get_triples_with_subject_and_object(&subject1, &object), vec![&triple1]);
  /// ```
  pub fn get_triples_with_subject_and_object(&self, subject_node: &Node, object_node: &Node) -> Vec<&Triple> {
    self.triples.get_triples_with_subject_and_object(subject_node, object_node)
  }

  /// Returns all triples from the triple store where the subject and predicate nodes match the provided nodes.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::graph::Graph;
  /// use rdf::uri::Uri;
  /// use rdf::triple::Triple;
  ///
  /// let mut graph = Graph::new(None);
  ///
  /// let subject1 = graph.create_blank_node();
  /// let subject2 = graph.create_blank_node();
  /// let predicate = graph.create_uri_node(&Uri::new("http://example.org/show/localName".to_string()));
  /// let object = graph.create_blank_node();
  ///
  /// let triple1 = Triple::new(&subject1, &predicate, &object);
  /// let triple2 = Triple::new(&subject2, &predicate, &object);
  ///
  /// graph.add_triples(&vec![triple1.to_owned(), triple2]);
  ///
  /// assert_eq!(graph.get_triples_with_subject_and_predicate(&subject1, &predicate), vec![&triple1]);
  /// ```
  pub fn get_triples_with_subject_and_predicate(&self, subject_node: &Node, predicate_node: &Node) -> Vec<&Triple> {
    self.triples.get_triples_with_subject_and_predicate(subject_node, predicate_node)
  }

  /// Returns all triples from the triple store where the predicate and object nodes match the provided nodes.
  ///
  /// # Examples
  ///
  /// ```
  /// use rdf::graph::Graph;
  /// use rdf::uri::Uri;
  /// use rdf::triple::Triple;
  ///
  /// let mut graph = Graph::new(None);
  ///
  /// let subject1 = graph.create_blank_node();
  /// let subject2 = graph.create_blank_node();
  /// let predicate = graph.create_uri_node(&Uri::new("http://example.org/show/localName".to_string()));
  /// let object = graph.create_blank_node();
  ///
  /// let triple1 = Triple::new(&subject1, &predicate, &object);
  /// let triple2 = Triple::new(&subject2, &predicate, &object);
  ///
  /// graph.add_triples(&vec![triple1.to_owned(), triple2.to_owned()]);
  ///
  /// assert_eq!(graph.get_triples_with_predicate_and_object(&predicate, &object), vec![&triple1, &triple2]);
  /// ```
  pub fn get_triples_with_predicate_and_object(&self, predicate_node: &Node, object_node: &Node) -> Vec<&Triple> {
    self.triples.get_triples_with_predicate_and_object(predicate_node, object_node)
  }

  /// Returns an iterator over the triples of the graph.
  pub fn triples_iter(&self) -> Iter<Triple> {
    self.triples.iter()
  }
}


#[cfg(test)]
mod tests {
  use graph::Graph;
  use node::*;

  #[test]
  fn empty_graph() {
    let graph = Graph::new(None);
    assert_eq!(graph.is_empty(), true);
  }

  #[test]
  fn create_literal_node() {
    let graph = Graph::new(None);
    let literal_node = graph.create_literal_node("literal".to_string());

    assert_eq!(literal_node, Node::LiteralNode {
      literal: "literal".to_string(),
      data_type: None,
      language: None
    });
  }

  #[test]
  fn create_multiple_blank_nodes() {
    let mut graph = Graph::new(None);

    let blank_node_0 = graph.create_blank_node();
    let blank_node_1 = graph.create_blank_node();

    assert_eq!(blank_node_0, Node::BlankNode {
      id: "auto0".to_string()
    });

    assert_eq!(blank_node_1, Node::BlankNode {
      id: "auto1".to_string()
    });
  }
}