hasTriples method
- RdfSubject? subject,
- Iterable<
RdfSubject> ? subjectIn, - RdfPredicate? predicate,
- Iterable<
RdfPredicate> ? predicateIn, - RdfObject? object,
- Iterable<
RdfObject> ? objectIn,
Checks if the graph contains any triples matching the given pattern
This method determines whether at least one triple in the graph matches all the specified pattern components. Unlike findTriples, this method returns a boolean result and is more efficient when you only need to know if matching triples exist rather than retrieving them.
Pattern matching uses AND logic - all non-null parameters must match for a triple to be considered a match. Null parameters act as wildcards and match any value in that position.
Set-based Queries: The *In parameters allow efficient checking for
triples that match any value in a set. When multiple values are provided
via these parameters, the method checks if ANY of those values match
(OR logic within the set), while different parameter types still use AND logic.
Performance: When subjectIn is used with indexing enabled, this method
leverages the internal index structure for optimal performance. The method
short-circuits as soon as a match is found, making it very efficient for
existence checks even with large sets.
Parameters:
subjectOptional subject to match (null acts as wildcard)subjectInOptional set of subjects - returns true if ANY of these subjects have matching triplespredicateOptional predicate to match (null acts as wildcard)predicateInOptional set of predicates - returns true if ANY of these predicates matchobjectOptional object to match (null acts as wildcard)objectInOptional set of objects - returns true if ANY of these objects match
Note: If both subject and subjectIn are provided, they are combined.
The same applies to predicate/predicateIn and object/objectIn pairs.
Empty sets in *In parameters will always return false.
Returns:
true if at least one triple matches the pattern, false otherwise.
Returns true for empty pattern (all parameters null) if graph is not empty.
Returns false if any *In parameter is an empty set.
Example:
// Check if John has any statements about him
if (graph.hasTriples(subject: john)) {
print('Found information about John');
}
// Check if anyone has a name property
if (graph.hasTriples(predicate: foaf.name)) {
print('Graph contains name information');
}
// Check if John specifically has a name
if (graph.hasTriples(subject: john, predicate: foaf.name)) {
print('John has a name in the graph');
}
// Check if graph has any triples at all
if (graph.hasTriples()) {
print('Graph is not empty');
}
// Check if John OR Jane have any triples (set-based query)
if (graph.hasTriples(subjectIn: [john, jane])) {
print('Found information about John or Jane');
}
// Check if any name OR email properties exist
if (graph.hasTriples(predicateIn: [name, email])) {
print('Graph contains contact information');
}
// Complex existence check with multiple sets
if (graph.hasTriples(
subjectIn: [john, jane, bob],
predicateIn: [name, email],
)) {
print('At least one person has contact info');
}
// Empty set always returns false
assert(graph.hasTriples(subjectIn: []) == false);
Implementation
bool hasTriples({
RdfSubject? subject,
Iterable<RdfSubject>? subjectIn,
RdfPredicate? predicate,
Iterable<RdfPredicate>? predicateIn,
RdfObject? object,
Iterable<RdfObject>? objectIn,
}) {
final subjectSet = (subject != null || subjectIn != null)
? {if (subject != null) subject, ...?subjectIn}
: null;
final predicateSet = (predicate != null || predicateIn != null)
? {if (predicate != null) predicate, ...?predicateIn}
: null;
final objectSet = (object != null || objectIn != null)
? {if (object != null) object, ...?objectIn}
: null;
// 2. Optimized path using the index
if (subjectSet != null && _effectiveIndex != null) {
final index = _effectiveIndex!;
// If any subject in the set can be found with a matching triple, return true.
return subjectSet.any((s) {
final subjectMap = index[s];
if (subjectMap == null) return false; // No triples for this subject
final Iterable<Triple> candidates;
if (predicateSet != null) {
// If predicateSet is empty, this will result in an empty sequence,
// correctly finding no candidates.
candidates = predicateSet
.map((p) => subjectMap[p])
.where((list) => list != null)
.expand((list) => list!);
} else {
candidates = subjectMap.values.expand((list) => list);
}
// Check if any candidate matches the object filter.
if (objectSet != null) {
return candidates.any((triple) => objectSet.contains(triple.object));
} else {
// If there's no object filter, having any candidates is a match.
return candidates.isNotEmpty;
}
});
}
return _triples
.any((triple) => _matches(triple, subjectSet, predicateSet, objectSet));
}