pub struct Triple {
pub subject: NodeId,
pub predicate: Predicate,
pub object: Value,
pub meta: TripleMeta,
}Expand description
A semantic triple, representing a single fact as a (Subject, Predicate, Object) statement.
A triple is the fundamental unit of data in the graph database. It represents a relationship between a subject and an object through a predicate.
§Examples
Creating a triple with a literal value:
use aingle_graph::{Triple, NodeId, Predicate, Value};
let triple = Triple::new(
NodeId::named("user:alice"),
Predicate::named("has_age"),
Value::integer(30),
);Creating a triple that links two nodes:
use aingle_graph::{Triple, NodeId, Predicate};
let triple = Triple::link(
NodeId::named("user:alice"),
Predicate::named("knows"),
NodeId::named("user:bob"),
);Creating a triple with metadata:
use aingle_graph::{Triple, TripleMeta, NodeId, Predicate, Value};
let meta = TripleMeta::new()
.with_author(NodeId::named("system"))
.with_source("import");
let triple = Triple::with_meta(
NodeId::named("user:alice"),
Predicate::named("has_name"),
Value::literal("Alice"),
meta,
);Fields§
§subject: NodeIdThe subject of the triple (the “thing” being described).
predicate: PredicateThe predicate (the relationship type).
object: ValueThe object (the value or related node).
meta: TripleMetaAssociated metadata, providing context and provenance for the triple.
Implementations§
Source§impl Triple
impl Triple
Sourcepub fn with_meta(
subject: NodeId,
predicate: Predicate,
object: Value,
meta: TripleMeta,
) -> Self
pub fn with_meta( subject: NodeId, predicate: Predicate, object: Value, meta: TripleMeta, ) -> Self
Creates a new Triple with the specified metadata.
Sourcepub fn literal(
subject: impl Into<NodeId>,
predicate: impl Into<Predicate>,
value: impl Into<String>,
) -> Self
pub fn literal( subject: impl Into<NodeId>, predicate: impl Into<Predicate>, value: impl Into<String>, ) -> Self
A convenience method to create a triple with a literal string object.
§Examples
use aingle_graph::{Triple, NodeId};
let triple = Triple::literal("user:alice", "has_name", "Alice Smith");
assert_eq!(triple.object.as_string(), Some("Alice Smith"));Sourcepub fn link(
subject: impl Into<NodeId>,
predicate: impl Into<Predicate>,
object: impl Into<NodeId>,
) -> Self
pub fn link( subject: impl Into<NodeId>, predicate: impl Into<Predicate>, object: impl Into<NodeId>, ) -> Self
A convenience method to create a triple that links two nodes.
This creates a relationship between two nodes in the graph.
§Examples
use aingle_graph::{Triple, NodeId};
let triple = Triple::link(
NodeId::named("user:alice"),
"knows",
NodeId::named("user:bob"),
);
assert!(triple.is_link());
assert_eq!(triple.object_node(), Some(&NodeId::named("user:bob")));Sourcepub fn id(&self) -> TripleId
pub fn id(&self) -> TripleId
Generates the unique, content-based ID for this triple.
The ID is deterministic - identical triples will always produce the same ID.
§Examples
use aingle_graph::{Triple, NodeId, Predicate, Value};
let triple = Triple::new(
NodeId::named("alice"),
Predicate::named("age"),
Value::integer(30),
);
let id = triple.id();
println!("Triple ID: {}", id);Sourcepub fn is_link(&self) -> bool
pub fn is_link(&self) -> bool
Returns true if the triple’s object is a reference to another node.
Sourcepub fn is_literal(&self) -> bool
pub fn is_literal(&self) -> bool
Returns true if the triple’s object is a literal value.
Sourcepub fn object_node(&self) -> Option<&NodeId>
pub fn object_node(&self) -> Option<&NodeId>
Returns the object as a NodeId if it is a node reference.
Sourcepub fn object_string(&self) -> Option<&str>
pub fn object_string(&self) -> Option<&str>
Returns the object as a string slice if it is a string-like literal.
Sourcepub fn to_ntriples(&self) -> String
pub fn to_ntriples(&self) -> String
Formats the triple into the N-Triples standard string format.
Sourcepub fn from_bytes(bytes: &[u8]) -> Option<Self>
pub fn from_bytes(bytes: &[u8]) -> Option<Self>
Deserializes a Triple from a byte slice.
Sourcepub fn index_keys(&self) -> TripleIndexKeys
pub fn index_keys(&self) -> TripleIndexKeys
Creates a set of pre-computed, lexicographically sortable keys for database indexing.