findTriples method

List<Triple> findTriples({
  1. RdfSubject? subject,
  2. RdfPredicate? predicate,
  3. RdfObject? object,
})

Find all triples matching the given pattern

This method returns triples that match all the specified pattern components. Unlike withoutMatching, this method uses AND logic - all specified components must match. If a pattern component is null, it acts as a wildcard.

Parameters:

  • subject Optional subject to match
  • predicate Optional predicate to match
  • object Optional object to match

Returns: List of matching triples as an unmodifiable collection. The list may be empty if no matching triples exist.

Example:

// Find all statements about John
final johnsTriples = graph.findTriples(subject: john);

// Find all name statements
final nameTriples = graph.findTriples(predicate: name);

// Find John's name specifically
final johnsName = graph.findTriples(subject: john, predicate: name);

Implementation

List<Triple> findTriples({
  RdfSubject? subject,
  RdfPredicate? predicate,
  RdfObject? object,
}) {
  if (subject != null) {
    switch (_effectiveIndex) {
      case null:
        break;
      case final index:
        final subjectMap = index[subject];
        if (subjectMap == null) {
          return const [];
        }
        if (predicate != null) {
          final predicateList = subjectMap[predicate];
          if (predicateList == null) {
            return const [];
          }
          if (object != null) {
            return List.unmodifiable(
                predicateList.where((triple) => triple.object == object));
          } else {
            return List.unmodifiable(predicateList);
          }
        } else {
          final allTriples = subjectMap.values.expand((list) => list);
          if (object != null) {
            return List.unmodifiable(
                allTriples.where((triple) => triple.object == object));
          } else {
            return List.unmodifiable(allTriples);
          }
        }
    }
  }
  return List.unmodifiable(
    _triples.where((triple) => _matches(triple, subject, predicate, object)),
  );
}