withTriples method

RdfGraph withTriples(
  1. Iterable<Triple> triples
)

Creates a new graph with all the specified triples added

Since RdfGraph is immutable, this returns a new instance with all existing and new triples. The original graph remains unchanged.

This method automatically removes duplicate triples, treating the graph as a mathematical set. The order of triples in the resulting graph is not guaranteed to be preserved from either the original graph or the added triples collection.

Parameters:

  • triples The collection of triples to add to the graph

Returns: A new graph instance with the added triples, duplicates removed, and no guaranteed ordering

Example:

// Add multiple statements about Jane
final newGraph = graph.withTriples([
  Triple(jane, email, LiteralTerm.string('jane@example.com')),
  Triple(jane, age, LiteralTerm.integer(28))
]);

// Duplicate triples are automatically removed
final graphWithDuplicates = graph.withTriples([existingTriple, newTriple]);
// Result contains each unique triple only once

Implementation

RdfGraph withTriples(Iterable<Triple> triples) {
  final newTriples =
      {..._triples, ...triples}.toList(); // Use a set to avoid duplicates

  return RdfGraph(triples: newTriples);
}