Transform RDF vocabularies into type-safe Dart code for seamless semantic web development
RDF Vocabulary to Dart is a powerful Dart build tool that transforms RDF vocabularies (like Schema.org, FOAF, etc.) into type-safe Dart code. Built on top of rdf_core, it bridges the gap between semantic web technologies and modern Dart development.
Each vocabulary (like Schema.org, FOAF) gets a dedicated class containing constants for all terms in that vocabulary.
Perfect for developers already familiar with RDF concepts, providing direct access to vocabulary terms with proper typing.
// Access Schema.org terms directly import 'package:my_package/vocab/generated/schema.dart'; final triple = Triple( IriTerm('http://example.org/john'), Schema.name, // https://schema.org/name LiteralTerm.string('John Doe') );
Dart
For each RDF class within a vocabulary (like schema:Person), a dedicated Dart class containing all properties from that class and its superclasses.
Ideal for Dart developers new to RDF, providing a familiar object-oriented approach.
// Using the class-specific approach import 'package:my_package/vocab/generated/schema_person.dart'; // SchemaPerson contains all properties related to Person Triple(person, SchemaPerson.name, LiteralTerm.string('Jane Doe')), Triple(person, SchemaPerson.email, LiteralTerm.string('jane@example.com')), // Properties from other vocabularies are properly prefixed Triple(person, SchemaPerson.rdfType, SchemaPerson.classIri),
Dart
Access vocabulary terms as constants with proper typing, eliminating string-based errors and enabling IDE autocompletion.
Automatic handling of namespaces, prefixes, and term resolution, making RDF concepts accessible in a Dart-native way.
Properties from related vocabularies are properly prefixed and included, preserving the semantic relationships between vocabularies.
Discover available terms through IDE autocompletion, making it easier to work with even unfamiliar vocabularies.
Class-specific objects include properties from parent classes, maintaining the hierarchical structure of RDF vocabularies.
Works with any RDF vocabulary accessible via URL or local file, giving you flexibility in your semantic web projects.
Add the dependencies to your pubspec.yaml
:
dependencies: rdf_core: ^0.9.0 # Core library for working with RDF data dev_dependencies: build_runner: ^2.4.0 # Runs the code generator rdf_vocabulary_to_dart: ^0.9.0 # The code generator
YAML
Create a vocabulary configuration file in your project:
// lib/src/vocab/vocabulary_sources.vocab.json { "vocabularies": { "schema": { "type": "url", "namespace": "https://schema.org/" }, "foaf": { "type": "url", "namespace": "http://xmlns.com/foaf/0.1/" }, "custom": { "type": "file", "namespace": "http://example.org/myvocab#", "filePath": "lib/src/vocab/custom_vocab.ttl" } } }
JSON
Configure build.yaml
in your project root:
targets: $default: builders: rdf_vocabulary_to_dart|rdf_to_dart_generator: enabled: true options: vocabulary_config_path: "lib/src/vocab/vocabulary_sources.vocab.json" output_dir: "lib/src/vocab/generated"
YAML
Each vocabulary in your configuration file can have these properties:
Property | Description | Required |
---|---|---|
type |
Either "url" for remote vocabularies or "file" for local files | Yes |
namespace |
The base IRI namespace of the vocabulary | Yes |
source |
For "url" type: URL to fetch the vocabulary from; for "file" type: path to local file | Yes (for "file" type), No (for "url" type, defaults to namespace) |
parsingFlags |
Array of string flags passed to the TurtleFormat when parsing Turtle files | No |
generate |
Boolean indicating if this vocabulary should be processed (defaults to true) | No |
contentType |
Explicit content type to use for the vocabulary source, overriding auto-detection | No |
skipDownload |
Boolean flag to deliberately skip a vocabulary (defaults to false) | No |
skipDownloadReason |
Text explanation for why a vocabulary is skipped | No |
The build.yaml
file supports these options:
Option | Description | Default |
---|---|---|
vocabulary_config_path |
Path to vocabulary configuration JSON | "lib/src/vocab/vocabulary_sources.vocab.json" |
output_dir |
Directory where generated files are placed | "lib/src/vocab/generated" |
Run the build_runner to generate Dart files from your RDF vocabularies:
dart run build_runner build
Shell
import 'package:rdf_core/rdf_core.dart'; import 'package:your_package/src/vocab/generated/schema.dart'; import 'package:your_package/src/vocab/generated/foaf.dart'; // Create a triple using vocabulary constants final triple = Triple( IriTerm('http://example.org/john'), Schema.name, // https://schema.org/name LiteralTerm.string('John Doe') ); // Use vocabulary terms in queries or graph operations final graph = RdfGraph(triples: [triple]); final nameQuery = graph.find( subject: null, predicate: Schema.name, object: null );
Dart
import 'package:rdf_core/rdf_core.dart'; import 'package:your_package/src/vocab/generated/schema_person.dart'; void createPersonTriples(IriTerm person) { // SchemaPerson contains all properties related to schema:Person // including inherited properties from parent classes final graph = RdfGraph(triples: [ // Properties from other vocabularies are properly prefixed Triple(person, SchemaPerson.rdfType, SchemaPerson.classIri), Triple(person, SchemaPerson.name, LiteralTerm.string('Jane Doe')), Triple(person, SchemaPerson.email, LiteralTerm.string('jane@example.com')), ]); }
Dart
Define vocabulary sources in JSON
Run build_runner to generate Dart code
Import and use generated classes
Work with type-safe RDF in Dart
For a vocabulary like Schema.org, the generator produces these types of files:
/// Schema.org Vocabulary class Schema { Schema._(); /// Base namespace for Schema.org static const namespace = 'https://schema.org/'; /// A person (alive, dead, undead, or fictional). static const Person = IriTerm('https://schema.org/Person'); /// The name of the item. static const name = IriTerm('https://schema.org/name'); // ... many more terms }
Dart
/// Properties for the Schema.org Person class class SchemaPerson { SchemaPerson._(); /// The RDF class IRI static const classIri = Schema.Person; /// The name of the person. static const name = Schema.name; /// Email address. static const email = Schema.email; /// A person known by this person (from FOAF vocabulary). static const foafKnows = FOAF.knows; // ... including inherited properties from parent classes }
Dart
Bridge the gap between semantic web technologies and modern Dart development with RDF Vocabulary to Dart.
Get Started View on GitHub