sophia_api/graph.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 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 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
//! An RDF graph, the central notion of the RDF data model,
//! is a collection of [triples](super::triple).
//!
//! This module provides [reusable abstractions](#traits)
//! for different kinds of graph,
//! as well as a few implementations for them.
use crate::dataset::adapter::GraphAsDataset;
use crate::source::{IntoTripleSource, StreamResult, TripleSource};
use crate::term::{matcher::TermMatcher, SimpleTerm, Term};
use crate::triple::Triple;
use resiter::{filter::*, flat_map::*, map::*};
use std::error::Error;
mod _foreign_impl;
pub mod adapter;
#[cfg(any(test, feature = "test_macro"))]
#[macro_use]
pub mod test;
/// Type alias for results produced by a graph.
pub type GResult<G, T> = Result<T, <G as Graph>::Error>;
/// Type alias for fallible triple iterators produced by a graph.
///
/// See [`Graph::triples`] for more information about how to use it.
pub type GTripleSource<'a, G> = Box<dyn Iterator<Item = GResult<G, <G as Graph>::Triple<'a>>> + 'a>;
/// Type alias for terms produced by a graph.
pub type GTerm<'a, G> = <<G as Graph>::Triple<'a> as Triple>::Term;
/// Type alias for fallible term iterators produced by a graph.
///
/// See [`Graph::subjects`] for more information about how to use it.
pub type GTermSource<'a, G> = Box<dyn Iterator<Item = GResult<G, GTerm<'a, G>>> + 'a>;
/// Generic trait for RDF graphs.
///
/// For convenience, this trait is implemented
/// by [standard collections of triples](#foreign-impls).
///
/// NB: the semantics of this trait allows a graph to contain duplicate triples;
/// see also [`SetGraph`].
pub trait Graph {
/// Determine the type of [`Triple`]s
/// that the methods of this graph will yield.
type Triple<'x>: Triple
where
Self: 'x;
/// The error type that this graph may raise.
type Error: Error + 'static;
/// An iterator visiting all triples of this graph in arbitrary order.
///
/// This iterator is fallible:
/// its items are `Result`s,
/// an error may occur at any time during the iteration.
///
/// # Examples
///
/// The result of this method is an iterator,
/// so it can be used in a `for` loop:
/// ```
/// # fn test() -> Result<(), Box<dyn std::error::Error>> {
/// # use sophia_api::graph::Graph;
/// # use sophia_api::term::SimpleTerm;
/// # let graph = Vec::<[SimpleTerm;3]>::new();
/// #
/// for t in graph.triples() {
/// let t = t?; // rethrow error if any
/// // do something with t
/// }
/// #
/// # Ok(())
/// # }
/// ```
/// Another way is to use the specific methods provided by [`TripleSource`],
/// for example:
/// ```
/// # use sophia_api::graph::Graph;
/// # use sophia_api::term::SimpleTerm;
/// # use sophia_api::source::TripleSource;
/// # fn test() -> Result<(), Box<dyn std::error::Error>> {
/// # let graph = Vec::<[SimpleTerm;3]>::new();
/// #
/// graph.triples().for_each_triple(|t| {
/// // do something with t
/// })?; // rethrow error if any
/// #
/// # Ok(())
/// # }
/// ```
fn triples(&self) -> GTripleSource<Self>;
/// An iterator visiting all triples matching the given subject, predicate and object.
/// See [`crate::term::matcher`].
///
/// See also [`triples`](Graph::triples).
///
/// # Usage
///
/// Typical implementations of [`TermMatcher`] include arrays/slices of [`Term`]s,
/// closure accepting a [`SimpleTerm`], or the special matcher [`Any`].
///
/// [`Term`]: crate::term::Term
/// [`SimpleTerm`]: crate::term::SimpleTerm
/// [`Any`]: crate::term::matcher::Any
/// ```
/// # use sophia_api::prelude::*;
/// # use sophia_api::ns::{Namespace, rdf};
/// #
/// # fn test<G: Graph>(graph: &G) -> Result<(), Box<dyn std::error::Error>>
/// # where
/// # G: Graph,
/// # {
/// #
/// let s = Namespace::new("http://schema.org/")?;
/// let city = s.get("City")?;
/// let country = s.get("Country")?;
///
/// for t in graph.triples_matching(Any, [&rdf::type_], [city, country]) {
/// println!("{:?} was found", t?.s());
/// }
/// #
/// # Ok(()) }
/// ```
///
/// Here is another example using a closure as a [`TermMatcher`].
///
/// ```
/// # use sophia_api::prelude::*;
/// # use sophia_api::ns::rdfs;
/// # use sophia_api::term::SimpleTerm;
/// #
/// # fn test<G>(graph: &G) -> Result<(), Box<dyn std::error::Error>>
/// # where
/// # G: Graph,
/// # {
/// #
/// for t in graph.triples_matching(
/// Any,
/// [&rdfs::label],
/// |t: SimpleTerm| t.lexical_form().map(|v| v.contains("needle")).unwrap_or(false),
/// ) {
/// println!("{:?} was found", t?.s());
/// }
/// #
/// # Ok(()) }
/// ```
fn triples_matching<'s, S, P, O>(&'s self, sm: S, pm: P, om: O) -> GTripleSource<'s, Self>
where
S: TermMatcher + 's,
P: TermMatcher + 's,
O: TermMatcher + 's,
{
Box::new(
self.triples().filter_ok(move |t| {
t.matched_by(sm.matcher_ref(), pm.matcher_ref(), om.matcher_ref())
}),
)
}
/// Return `true` if this graph contains the given triple.
fn contains<TS, TP, TO>(&self, s: TS, p: TP, o: TO) -> GResult<Self, bool>
where
TS: Term,
TP: Term,
TO: Term,
{
self.triples_matching([s], [p], [o])
.next()
.transpose()
.map(|o| o.is_some())
}
/// Build a fallible iterator of all the terms used as subject in this Graph.
///
/// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so.
/// Users MUST therefore be prepared to deal with duplicates.
fn subjects(&self) -> GTermSource<Self> {
Box::new(self.triples().map_ok(Triple::to_s))
}
/// Build a fallible iterator of all the terms used as predicate in this Graph.
///
/// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so.
/// Users MUST therefore be prepared to deal with duplicates.
fn predicates(&self) -> GTermSource<Self> {
Box::new(self.triples().map_ok(Triple::to_p))
}
/// Build a fallible iterator of all the terms used as object in this Graph.
///
/// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so.
/// Users MUST therefore be prepared to deal with duplicates.
fn objects(&self) -> GTermSource<Self> {
Box::new(self.triples().map_ok(Triple::to_o))
}
/// Build a fallible iterator of all the IRIs used in this Graph
/// (including those used inside quoted triples, if any).
///
/// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so.
/// Users MUST therefore be prepared to deal with duplicates.
fn iris(&self) -> GTermSource<Self> {
Box::new(
self.triples()
.flat_map_ok(Triple::to_spo)
.flat_map_ok(Term::to_atoms)
.filter_ok(Term::is_iri),
)
}
/// Build a fallible iterator of all the blank nodes used in this Graph
/// (including those used inside quoted triples, if any).
///
/// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so.
/// Users MUST therefore be prepared to deal with duplicates.
fn blank_nodes(&self) -> GTermSource<Self> {
Box::new(
self.triples()
.flat_map_ok(Triple::to_spo)
.flat_map_ok(Term::to_atoms)
.filter_ok(Term::is_blank_node),
)
}
/// Build a fallible iterator of all the literals used in this Graph
/// (including those used inside quoted triples, if any).
///
/// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so.
/// Users MUST therefore be prepared to deal with duplicates.
fn literals(&self) -> GTermSource<Self> {
Box::new(
self.triples()
.flat_map_ok(Triple::to_spo)
.flat_map_ok(Term::to_atoms)
.filter_ok(Term::is_literal),
)
}
/// Build a fallible iterator of all the quoted triples used in this Graph
/// (including those used inside quoted triples, if any).
///
/// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so.
/// Users MUST therefore be prepared to deal with duplicates.
fn quoted_triples<'s>(&'s self) -> GTermSource<'s, Self>
where
GTerm<'s, Self>: Clone,
{
Box::new(
self.triples()
.flat_map_ok(Triple::to_spo)
.flat_map_ok(Term::to_constituents)
.filter_ok(Term::is_triple),
)
}
/// Build a fallible iterator of all the variables used in this Graph
/// (including those used inside quoted triples, if any).
///
/// NB: implementations SHOULD avoid yielding the same term multiple times, but MAY do so.
/// Users MUST therefore be prepared to deal with duplicates.
fn variables(&self) -> GTermSource<Self> {
Box::new(
self.triples()
.flat_map_ok(Triple::to_spo)
.flat_map_ok(Term::to_atoms)
.filter_ok(Term::is_variable),
)
}
/// [`Dataset`](crate::dataset::Dataset) adapter borrowing this graph
fn as_dataset(&self) -> GraphAsDataset<&Self> {
GraphAsDataset::new(self)
}
/// [`Dataset`](crate::dataset::Dataset) adapter borrowing this graph mutably
fn as_dataset_mut(&mut self) -> GraphAsDataset<&mut Self> {
GraphAsDataset::new(self)
}
/// [`Dataset`](crate::dataset::Dataset) adapter taking ownership of this graph
fn into_dataset(self) -> GraphAsDataset<Self>
where
Self: Sized,
{
GraphAsDataset::new(self)
}
}
/// A [`Graph`] that can be constructed from a [`TripleSource`]
pub trait CollectibleGraph: Graph + Sized {
/// Construct a graph from the given source
fn from_triple_source<TS: TripleSource>(
triples: TS,
) -> StreamResult<Self, TS::Error, Self::Error>;
}
/// Type alias for results produced by a mutable graph.
pub type MgResult<G, T> = std::result::Result<T, <G as MutableGraph>::MutationError>;
/// Generic trait for mutable RDF graphs.
///
/// NB: the semantics of this trait allows a graph to contain duplicate triples;
/// see also [`SetGraph`].
pub trait MutableGraph: Graph {
/// The error type that this graph may raise during mutations.
type MutationError: Error + 'static;
/// Insert in this graph a triple made of the the given terms.
///
/// # Return value
/// The `bool` value returned in case of success is
/// **not significant unless** this graph also implements [`SetGraph`].
///
/// If it does,
/// `true` is returned iff the insertion actually changed the graph.
/// In other words,
/// a return value of `false` means that the graph was not changed,
/// because the triple was already present in this [`SetGraph`].
///
/// See also [`insert_triple`](MutableGraph::insert_triple)
///
/// # Usage
/// ```
/// # use sophia_api::graph::{MutableGraph, MgResult};
/// # use sophia_api::ns::{Namespace, rdf, rdfs, xsd};
/// # fn populate<G: MutableGraph>(graph: &mut G) -> MgResult<G, ()> {
/// #
/// let schema = Namespace::new("http://schema.org/").unwrap();
/// let s_name = schema.get("name").unwrap();
///
/// graph.insert(&s_name, &rdf::type_, &rdf::Property)?;
/// graph.insert(&s_name, &rdfs::range, &xsd::string)?;
/// graph.insert(&s_name, &rdfs::comment, "The name of the item.")?;
/// #
/// # Ok(())
/// # }
/// ```
fn insert<TS, TP, TO>(&mut self, s: TS, p: TP, p: TO) -> MgResult<Self, bool>
where
TS: Term,
TP: Term,
TO: Term;
/// Insert in this graph the given triple.
///
/// NB: if you want to insert a triple `t` while keeping its ownership,
/// you can still pass [`t.spo()`](Triple::spo).
///
/// See also [`MutableGraph::insert`]
fn insert_triple<T>(&mut self, triple: T) -> MgResult<Self, bool>
where
T: Triple,
{
let [s, p, o] = triple.to_spo();
self.insert(s, p, o)
}
/// Remove from this graph the triple made of the the given terms.
///
/// # Return value
/// The `bool` value returned in case of success is
/// **not significant unless** this graph also implements [`SetGraph`].
///
/// If it does,
/// `true` is returned iff the removal actually changed the graph.
/// In other words,
/// a return value of `false` means that the graph was not changed,
/// because the triple was already absent from this [`SetGraph`].
fn remove<TS, TP, TO>(&mut self, s: TS, p: TP, o: TO) -> MgResult<Self, bool>
where
TS: Term,
TP: Term,
TO: Term;
/// Remoe from this graph the given triple.
///
/// NB: if you want to remove a triple `t` while keeping its ownership,
/// you can still pass [`t.spo()`](Triple::spo).
///
/// See also [MutableGraph::remove]
fn remove_triple<T>(&mut self, triple: T) -> MgResult<Self, bool>
where
T: Triple,
{
let [s, p, o] = triple.to_spo();
self.remove(s, p, o)
}
/// Insert into this graph all triples from the given source.
///
/// # Blank node scope
/// The blank nodes contained in the triple source will be inserted as is.
/// If they happen to have the same identifier as blank nodes already present,
/// they will be considered equal.
/// This might *not* be what you want,
/// especially if the graph contains data from a file,
/// and you are inserting data from a different file.
/// In that case, you should first transform the triple source,
/// in order to get fresh blank node identifiers.
///
/// # Return value
/// The `usize` value returned in case of success is
/// **not significant unless** this graph also implements [`SetGraph`].
///
/// If it does,
/// the number of triples that were *actually* inserted
/// (i.e. that were not already present in this [`SetGraph`])
/// is returned.
#[inline]
fn insert_all<TS: TripleSource>(
&mut self,
src: TS,
) -> StreamResult<usize, TS::Error, <Self as MutableGraph>::MutationError> {
let mut src = src;
let mut c = 0;
src.try_for_each_triple(|t| -> MgResult<Self, ()> {
if self.insert_triple(t.spo())? {
c += 1;
}
Ok(())
})
.and(Ok(c))
}
/// Remove from this graph all triples from the given source.
///
/// # Return value
/// The `usize` value returned in case of success is
/// **not significant unless** this graph also implements [`SetGraph`].
///
/// If it does,
/// the number of triples that were *actually* removed
/// (i.e. that were not already absent from this [`SetGraph`])
/// is returned.
#[inline]
fn remove_all<TS: TripleSource>(
&mut self,
src: TS,
) -> StreamResult<usize, TS::Error, <Self as MutableGraph>::MutationError> {
let mut src = src;
let mut c = 0;
src.try_for_each_triple(|t| -> MgResult<Self, ()> {
if self.remove_triple(t.spo())? {
c += 1;
}
Ok(())
})
.and(Ok(c))
}
/// Remove all triples matching the given matchers.
///
/// # Return value
/// The `usize` value returned in case of success is
/// **not significant unless** this graph also implements [`SetGraph`].
///
/// If it does,
/// the number of triples that were *actually* removed
/// (i.e. that were not already absent from this [`SetGraph`])
/// is returned.
///
/// # Note to implementors
/// The default implementation is rather naive,
/// and could be improved in specific implementations of the trait.
fn remove_matching<S, P, O>(
&mut self,
ms: S,
mp: P,
mo: O,
) -> Result<usize, Self::MutationError>
where
S: TermMatcher,
P: TermMatcher,
O: TermMatcher,
Self::MutationError: From<Self::Error>,
{
let to_remove: Result<Vec<[SimpleTerm; 3]>, _> = self
.triples_matching(ms, mp, mo)
.map_ok(|t| t.spo().map(Term::into_term))
.collect();
self.remove_all(to_remove?.into_iter().into_triple_source())
.map_err(|err| err.unwrap_sink_error())
}
/// Keep only the triples matching the given matchers.
///
/// # Note to implementors
/// The default implementation is rather naive,
/// and could be improved in specific implementations of the trait.
fn retain_matching<S, P, O>(&mut self, ms: S, mp: P, mo: O) -> Result<(), Self::MutationError>
where
S: TermMatcher,
P: TermMatcher,
O: TermMatcher,
Self::MutationError: From<Self::Error>,
{
let to_remove: Result<Vec<[SimpleTerm; 3]>, _> = self
.triples()
.filter_ok(|t| !t.matched_by(ms.matcher_ref(), mp.matcher_ref(), mo.matcher_ref()))
.map_ok(|t| t.spo().map(Term::into_term))
.collect();
self.remove_all(to_remove?.into_iter().into_triple_source())
.map_err(|err| err.unwrap_sink_error())?;
Ok(())
}
}
/// Marker trait constraining the semantics of
/// [`Graph`] and [`MutableGraph`].
///
/// It guarantees that
/// (1) triples will never be returned / stored multiple times.
///
/// If the type also implements [`MutableGraph`],
/// it must also ensure that
/// (2) the `bool` or `usize` values returned by [`MutableGraph`]
/// methods accurately describe how many triples were actually added/removed.
///
/// # Note to implementors
/// A type implementing both [`Graph`] and [`MutableGraph`],
/// enforcing (1) but failing to enforce (2)
/// *must not* implement this trait.
pub trait SetGraph: Graph {}
#[cfg(test)]
mod check_implementability {
/// This is a naive implementation of an RDF-star graph,
/// where the graph maintains
/// - a list of terms (either atoms or index of triple)
/// - a list of triples (SPO indexes, plus an 'asserted' flag)
/// This avoids the need to store arbitrarily nested triples.
use super::*;
use crate::term::SimpleTerm;
#[derive(Clone, Debug, Eq, PartialEq)]
#[allow(dead_code)] // testing implementability
enum MyInternalTerm {
Atom(SimpleTerm<'static>),
QuotedTriple(usize),
}
use MyInternalTerm::*;
#[derive(Clone, Debug, Eq, PartialEq)]
struct MyInternalTriple {
asserted: bool,
spo: [usize; 3],
}
#[derive(Clone, Debug)]
struct MyGraph {
terms: Vec<MyInternalTerm>,
triples: Vec<MyInternalTriple>,
}
impl MyGraph {
fn make_term(&self, i: usize) -> SimpleTerm<'_> {
match &self.terms[i] {
Atom(t) => t.as_simple(),
QuotedTriple(j) => {
SimpleTerm::Triple(Box::new(self.make_triple(self.triples[*j].spo)))
}
}
}
fn make_triple(&self, spo: [usize; 3]) -> [SimpleTerm<'_>; 3] {
spo.map(|j| self.make_term(j))
}
}
impl Graph for MyGraph {
type Triple<'x> = [SimpleTerm<'x>; 3] where Self: 'x;
type Error = std::convert::Infallible;
fn triples(&self) -> GTripleSource<Self> {
Box::new(
self.triples
.iter()
.filter(|t| t.asserted)
.map(|t| Ok(self.make_triple(t.spo))),
)
}
}
}
#[cfg(test)]
mod check_implementability_lazy_term {
/// This implementation is internally similar to the one above,
/// but using dedicated lazy implementations of Term
/// (lazy because it avoids allocating nested triples until forced)
use super::*;
use crate::term::{SimpleTerm, TermKind};
#[derive(Clone, Debug, Eq, PartialEq)]
#[allow(dead_code)] // testing implementability
enum MyInternalTerm {
Atom(SimpleTerm<'static>),
QuotedTriple(usize),
}
use MyInternalTerm::*;
#[derive(Clone, Debug, Eq, PartialEq)]
struct MyInternalTriple {
asserted: bool,
spo: [usize; 3],
}
#[derive(Clone, Debug)]
struct MyGraph {
terms: Vec<MyInternalTerm>,
triples: Vec<MyInternalTriple>,
}
#[derive(Clone, Copy, Debug)]
struct MyTerm<'a> {
graph: &'a MyGraph,
index: usize,
}
impl<'a> Term for MyTerm<'a> {
type BorrowTerm<'x> = MyTerm<'x> where Self: 'x;
fn kind(&self) -> crate::term::TermKind {
if let Atom(t) = &self.graph.terms[self.index] {
t.kind()
} else {
TermKind::Triple
}
}
fn iri(&self) -> Option<crate::term::IriRef<mownstr::MownStr>> {
if let Atom(t) = &self.graph.terms[self.index] {
t.iri()
} else {
None
}
}
fn bnode_id(&self) -> Option<crate::term::BnodeId<mownstr::MownStr>> {
if let Atom(t) = &self.graph.terms[self.index] {
t.bnode_id()
} else {
None
}
}
fn lexical_form(&self) -> Option<mownstr::MownStr> {
if let Atom(t) = &self.graph.terms[self.index] {
t.lexical_form()
} else {
None
}
}
fn datatype(&self) -> Option<crate::term::IriRef<mownstr::MownStr>> {
if let Atom(t) = &self.graph.terms[self.index] {
t.datatype()
} else {
None
}
}
fn language_tag(&self) -> Option<crate::term::LanguageTag<mownstr::MownStr>> {
if let Atom(t) = &self.graph.terms[self.index] {
t.language_tag()
} else {
None
}
}
fn variable(&self) -> Option<crate::term::VarName<mownstr::MownStr>> {
if let Atom(t) = &self.graph.terms[self.index] {
t.variable()
} else {
None
}
}
fn triple(&self) -> Option<[Self::BorrowTerm<'_>; 3]> {
self.to_triple()
}
fn to_triple(self) -> Option<[Self; 3]> {
if let QuotedTriple(i) = &self.graph.terms[self.index] {
Some(self.graph.triples[*i].spo.map(|t| MyTerm {
graph: self.graph,
index: t,
}))
} else {
None
}
}
fn borrow_term(&self) -> Self::BorrowTerm<'_> {
*self
}
}
impl Graph for MyGraph {
type Triple<'x> = [MyTerm<'x>; 3] where Self: 'x;
type Error = std::convert::Infallible;
fn triples(&self) -> GTripleSource<Self> {
Box::new(self.triples.iter().filter(|t| t.asserted).map(|t| {
Ok(t.spo.map(|i| MyTerm {
graph: self,
index: i,
}))
}))
}
}
}