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)).
true.

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

<http://www.w3.org/TR/rdf-syntax-grammar>
  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,
    [
        example_01/0
    ]).

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

example_01 :-
    rdf_read_turtle('./example.ttl',Triples,[]),
    rdf_portray_as(prefix:id=label),
    print_term(Triples,[]).

Example run

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

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

?- [rdf_learning].
true.

?- example_01.
[ rdf('http://www.w3.org/TR/rdf-syntax-grammar',
      dc:title,
      literal('RDF/XML Syntax Specification (Revised)')),
  rdf(node(1),
      'http://example.org/stuff/1.0/fullname',
      literal('Dave Beckett')),
  rdf(node(1),
      'http://example.org/stuff/1.0/homePage',
      'http://purl.org/net/dajobe/'),
  rdf('http://www.w3.org/TR/rdf-syntax-grammar',
      'http://example.org/stuff/1.0/editor',
      node(1))
]
true.

Notice the node(1) that were generated.

(ref)

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

example_02 :-
    rdf_read_turtle('./example.ttl',Triples,[anon_prefix(node_)]),
    setup_call_cleanup(
        open('./example_02.xml',write,Stream,[encoding(utf8)]),
        rdf_write_xml(Stream,Triples),
        close(Stream)
    ).

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