Heuristic:AKSW RDFUnit Deterministic Test URI Generation
| Knowledge Sources | |
|---|---|
| Domains | Optimization, Test_Generation, URI_Design |
| Last Updated | 2026-10-16 00:00 GMT |
Overview
Auto-generated test case URIs use SHA-256 hashes of the generator URI and binding values to produce deterministic, collision-resistant identifiers.
Description
When RDFUnit auto-generates test cases from patterns and TAG-based generators, each test case needs a unique URI for identification, caching, and result correlation. The `TestUtils.generateTestURI()` method constructs a URI by combining the RDFUnit namespace prefix, the source prefix, the pattern ID, and a hash of the generator URI concatenated with all binding values.
The hash function used is SHA-256 (not MD5, despite the method comment referencing "MD5"). The input to the hash is the generator URI followed by each binding's string value. If the hash is empty (which should never happen in practice), a unique random URI is generated as a fallback via `JenaUtils.getUniqueIri()`.
This approach ensures that the same generator + bindings always produce the same test URI, enabling test caching: if the same test is regenerated, it produces the same URI and can be loaded from cache instead of being re-executed.
Usage
Use this heuristic when designing custom test generators or when debugging test URI collisions. Ensure that the generator URI and binding values together form a unique combination. Two different generators producing tests for the same pattern with the same bindings will produce different URIs because the generator URI is part of the hash input.
The Insight (Rule of Thumb)
- Action: When creating custom test generators, ensure the generator URI and binding values uniquely identify each test case.
- Value: SHA-256 hash of `generatorURI + bindingValues`; URI format: `{rutt-prefix}{sourcePrefix}-{patternId}-{sha256hash}`.
- Trade-off: Deterministic URIs enable caching but mean the same input always maps to the same test case (cannot regenerate with different parameters without changing the generator URI or bindings).
- Compatibility: Works with all pattern-based test cases. Manual test cases use their own explicit URIs.
Reasoning
Deterministic URI generation is essential for test caching: the `GenerateAndCacheRdfUnitTestGenerator` stores generated tests to disk, and on subsequent runs, the `CacheTestGenerator` loads them by URI. If URIs were random, cache lookups would always miss. SHA-256 provides a negligible collision probability (2^128 birthday bound) while being deterministic and fast. Including the generator URI in the hash ensures tests from different generators targeting the same pattern do not collide.
Code Evidence
URI generation from `TestUtils.java:64-79`:
public static String generateTestURI(String sourcePrefix, Pattern pattern,
Collection<Binding> bindings, String generatorURI) {
String testURI =
org.aksw.rdfunit.services.PrefixNSService.getNSFromPrefix("rutt") + sourcePrefix + "-"
+ pattern.getId() + "-";
StringBuilder string2hash = new StringBuilder(generatorURI);
for (Binding binding : bindings) {
string2hash.append(binding.getValueAsString());
}
String md5Hash = StringUtils.getHashFromString(string2hash.toString());
if (md5Hash.isEmpty()) {
return JenaUtils.getUniqueIri(testURI);
} else {
return testURI + md5Hash;
}
}
SHA-256 implementation from `StringUtils.java:23-35`:
public static String getHashFromString(String str) {
try {
java.security.MessageDigest md = java.security.MessageDigest.getInstance("SHA-256");
byte[] array = md.digest(str.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
for (byte anArray : array) {
sb.append(Integer.toHexString((anArray & 0xFF) | 0x100).substring(1, 3));
}
return sb.toString();
} catch (java.security.NoSuchAlgorithmException e) {
throw new IllegalArgumentException("Cannot calculate SHA-256 hash for :" + str, e);
}
}