RDF basics

RDF Basics

Note: This is a topic for understanding the basics of using RDF with SWI-Prolog.
If you have Trust level: Basic you can edit this Wiki topic by clicking on the edit icon in the lower right. Capture

Note: Do not reply to this topic; questions, concerns, comments, etc. are to be handled in
Wiki Discussion: RDF basics

  1. The primary documentaion for using RDF with SWI-Prolog is in semweb libraries.

  2. Unlike ugraph, a default RDF graph is created.

Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.25)

?- rdf_default_graph(G).
ERROR: Unknown procedure: rdf_default_graph/1 (DWIM could not correct goal)

?- use_module(library(semweb/rdf11)).

?- rdf_default_graph(G).
G = default.
  1. There are Two RDF APIs

The current‘semweb’ package provides two sets of interface predicates. The original set is described in section 3.3. The new API is described in section 3.1. The original API was designed when RDF was not yet standardised and did not yet support data types and language indicators. The new API is designed from the RDF 1.1 specification, introducing consistent naming and access to literals using the value space . The new API is currently defined on top of the old API, so both APIs can be mixed in a single application.

So be sure to check the documentation of both APIs before you find yourself writing something that already exist.

  1. An RDF subject and/or object can be another RDF triple. (ref)

Mike → said → (triples → can be → objects)

  1. If one searches the SWI-Prolog GitHub repository for example code using rdf_assert/3 they will find Turtle files instead.
    Here is an example of creating a Turtle file (Turtle (syntax) example), loading it with rdf_read_turtle/3 and displayed it with rdf_portray_as/1 and print_term/2.

File: example.ttl
Directory: C:/Users/Groot/Documents

# from https://en.wikipedia.org/wiki/Turtle_(syntax)#Example

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix ex: <http://example.org/stuff/1.0/> .

  dc:title "RDF/XML Syntax Specification (Revised)" ;
  ex:editor [
    ex:fullname "Dave Beckett";
    ex:homePage <http://purl.org/net/dajobe/>
  ] .

File: rdf_learning.pl
Directory: C:/Users/Groot/Documents

:- module(rdf_learning,

:- use_module(library(semweb/rdf11)).
:- use_module(library(semweb/turtle)).
:- use_module(library(semweb/rdf_portray)).

example_01 :-

Example run

Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.25)

?- working_directory(_,'C:/Users/Groot/Documents').

?- [rdf_learning].

?- example_01.
[ rdf('http://www.w3.org/TR/rdf-syntax-grammar',
      literal('RDF/XML Syntax Specification (Revised)')),
      literal('Dave Beckett')),

Notice the node(1) that were generated.


The example encodes an RDF graph made of four triples, which express these facts:

  • The W3C technical report on RDF syntax and grammar has the title RDF/XML Syntax Specification (Revised) .
  • That report’s editor is a certain individual, who in turn
  • Has full name Dave Beckett .
  • Has a home page at a certain place.
  1. When converting a Turtle file to an XML file, rdf_write_xml/2 is used. However rdf_write_xml/2 will not accept subject or object resouces as Prolog compounds, e.g. node(1).

ERROR: Type error: atomic' expected, found node(1)’ (a compound)

To resolve this use rdf_read_turtle/3 with the option anon_prefix(<Prefix>)


example_02 :-

Related SWI-Prolog references:
RDF Applications with Prolog
SWI-Prolog for the (semantic) web

Related RDF references:
RDF Schema 1.1
RDF Tutorial
W3 CRDF Validation Service