IriStrategy class
Defines the strategy for generating IRIs for RDF resources.
This class is a key component of the @RdfGlobalResource annotation that specifies
how to construct unique IRIs for instances of annotated classes. It provides a
template-based mechanism that combines static text with dynamic values from object properties.
IRI Template Variables
The IRI template supports two types of variables with different expansion syntaxes:
Variable Expansion Syntax
- Standard variables (
{variable}): Values are percent-encoded according to URI standards - Reserved expansion (
{+variable}): Values containing URI-reserved characters like slashes (/) are not percent-encoded, allowing them to remain as structural URI components
Variable Types
-
Property-based variables - Values from properties marked with
@RdfIriPart:@RdfGlobalResource(SchemaBook.classIri, IriStrategy('http://example.org/books/{isbn}')) class Book { @RdfIriPart('isbn') // Maps to {isbn} in the template final String isbn; // ... } -
Context variables - Values provided externally, often using reserved expansion:
@RdfGlobalResource( SchemaBook.classIri, IriStrategy('{+baseUrl}/books/{id}') // {+baseUrl} preserves slashes ) class Book { @RdfIriPart('id') final String id; // ... } // When registerGlobally is true (default): final rdfMapper = initRdfMapper( baseUrlProvider: () => 'https://myapp.example.org' // Auto-injected parameter );
Context Variable Resolution
When the generator encounters template variables that aren't bound to properties with
@RdfIriPart, it treats them as context variables and resolves them in the following ways:
-
Global Registration (when
registerGlobally = truein@RdfGlobalResource, which is the default):- The generator adds required provider parameters to
initRdfMapper() - These providers must be supplied when initializing the RDF mapper
- Example:
{baseUrl}becomesrequired String Function() baseUrlProvider
- The generator adds required provider parameters to
-
Local Resolution (when
registerGlobally = false):- The parent mapper that uses this type needs to provide the context values
- Context variables can be resolved from:
a. Properties in the parent class annotated with
@RdfProvides('variableName')b. The parent resource's IRI itself, when the parent'sIriStrategyspecifiesprovidedAsparameter c. Or required in the parent mapper's constructor (which may propagate up toinitRdfMapper)
This system enables flexible, context-aware IRI patterns that can adapt to different
deployment environments without hardcoding values. Unlike the RdfIri annotation which is used
for classes that represent IRI terms themselves, IriStrategy is used within RdfGlobalResource
to define how instance IRIs are constructed from their properties.
Example: Providing Parent IRI to Child Resources
The providedAs parameter enables a resource to provide its own IRI to dependent mappers. This is particularly useful for hierarchical data structures:
@RdfGlobalResource(
ParentClass.classIri,
IriStrategy('{+baseUri}/parents/{id}', 'parentIri')
)
class Parent {
@RdfIriPart()
final String id;
@RdfProperty(Vocab.child)
final Child child;
}
@RdfGlobalResource(
ChildClass.classIri,
IriStrategy('{+parentIri}/children/{childId}'),
registerGlobally: false
)
class Child {
@RdfIriPart()
final String childId;
}
In this example, the Parent mapper will provide a String Function() that returns
the parent's IRI, making it available to the Child mapper via {+parentIri}.
Internal Record-Based Mechanism
Unlike RdfIri and IriMapping which work with complete objects, IriStrategy operates
on a record composed of the values from properties marked with @RdfIriPart. This
record-based approach allows resource mappers to:
- Extract only the necessary IRI-related properties when serializing
- Populate these same properties when deserializing from an IRI term
Constructor Choice and RdfIriPart Usage
-
Default constructor: The generator creates an
IriTermMapperimplementation that handles mapping between a record of the@RdfIriPartvalues and IRI terms. With this approach, you can use the standard@RdfIriPart([name])constructor. -
Custom mappers (via
.namedMapper(),.mapper(), or.mapperInstance()): You must implement anIriTermMapperthat works with a record of the property values. For multiple@RdfIriPartproperties, use@RdfIriPart.position(index, [name])to specify the positional order of fields in the record for more robustness and to avoid bugs introduced by changing field order.
- Inheritance
-
- Object
- BaseMapping<
IriTermMapper> - IriStrategy
Constructors
- IriStrategy([String? template, String? providedAs])
-
Creates a strategy for generating IRIs from resource properties.
const
- IriStrategy.mapper(Type mapperType, {String? providedAs})
-
Creates a reference to a mapper that will be instantiated from the given type.
const
- IriStrategy.mapperInstance(IriTermMapper instance, {String? providedAs})
-
Creates a reference to a directly provided mapper instance for this IRI term.
const
- IriStrategy.namedFactory(String name, [Object? configInstance, String? providedAs])
-
Creates a reference to a named factory function for creating IRI mappers.
const
- IriStrategy.namedMapper(String name, {String? providedAs})
-
Creates a reference to a named mapper for this IRI strategy.
const
- IriStrategy.withFragment(String baseIriTemplate, String fragmentTemplate, {String? providedAs})
-
Creates a strategy for generating IRIs by appending a fragment to a base IRI.
const
Properties
- fragmentTemplate → String?
-
Optional template for the fragment identifier to append to the base IRI.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
-
mapper
→ MapperRef<
IriTermMapper> ? -
Provides a MapperRef if a custom mapper is specified.
no setterinherited
- providedAs → String?
-
Optional name under which this resource's IRI will be provided to dependent mappers.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- template → String?
-
An optional template string for constructing IRIs from resource properties.
final
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited