RDF Vocabulary to Dart

Transform RDF vocabularies into type-safe Dart code for seamless semantic web development

Get Started View on GitHub
pub package build codecov license

What is RDF Vocabulary to Dart?

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.

Vocabulary Classes

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

RDF Class-Specific Classes

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

Key Features

Type-Safe RDF Terms

Access vocabulary terms as constants with proper typing, eliminating string-based errors and enabling IDE autocompletion.

Intelligent Code Generation

Automatic handling of namespaces, prefixes, and term resolution, making RDF concepts accessible in a Dart-native way.

Cross-Vocabulary Integration

Properties from related vocabularies are properly prefixed and included, preserving the semantic relationships between vocabularies.

IDE Completion

Discover available terms through IDE autocompletion, making it easier to work with even unfamiliar vocabularies.

Inheritance Support

Class-specific objects include properties from parent classes, maintaining the hierarchical structure of RDF vocabularies.

Comprehensive Coverage

Works with any RDF vocabulary accessible via URL or local file, giving you flexibility in your semantic web projects.

Getting Started

1. Installation

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

2. Configuration

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

Configuration Options

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"

3. Generate Code

Run the build_runner to generate Dart files from your RDF vocabularies:

dart run build_runner build
Shell

Usage Examples

Using Vocabulary Classes

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

Using Class-Specific Classes

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

How It Works

1

Configuration

Define vocabulary sources in JSON

2

Generation

Run build_runner to generate Dart code

3

Integration

Import and use generated classes

4

Development

Work with type-safe RDF in Dart

Generated Code Samples

For a vocabulary like Schema.org, the generator produces these types of files:

1. Schema.dart - All vocabulary terms:

/// 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

2. SchemaPerson.dart - Properties for the Person class:

/// 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

Start Building Type-Safe RDF Applications Today

Bridge the gap between semantic web technologies and modern Dart development with RDF Vocabulary to Dart.

Get Started View on GitHub