# RDF basics

**URL:** <https://swi-prolog.discourse.group/t/rdf-basics/4105>\
**Category:** Wiki\
**Created:** [June 23, 2021, 4:00am UTC](https://swi-prolog.discourse.group/t/rdf-basics/4105 "2021-06-23T04:00:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wiki](https://avatars.discourse-cdn.com/v4/letter/w/e8c25b/32.png) [@Wiki](https://swi-prolog.discourse.group/u/Wiki)\
**Post date:** [June 23, 2021, 4:00am UTC](https://swi-prolog.discourse.group/t/rdf-basics/4105/1 "2021-06-23T04:00:00Z")

</div>

# RDF Basics

> Note: This is a topic for understanding the basics of using RDF with SWI-Prolog.  
> If you have [Trust level](https://blog.discourse.org/2018/06/understanding-discourse-trust-levels/): [Basic](https://swi-prolog.discourse.group/badges/1/basic) you can edit this Wiki topic by clicking on the edit icon in the lower right. ![Capture](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/1X/bfd4c3b77dc7f3f98ece3d5ae3202fcccb0fb8b3.png)

> Note: Do not reply to this topic; questions, concerns, comments, etc. are to be handled in  
> [Wiki Discussion: RDF basics](https://swi-prolog.discourse.group/t/wiki-discussion-rdf-basics/4110)

* * *

* * *

1. The primary documentaion for using RDF with SWI-Prolog is in [semweb libraries](https://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/semweb.html%27)).

2. Unlike [ugraph](https://www.swi-prolog.org/pldoc/man?section=ugraphs), a default RDF graph is created.

```prolog
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](https://www.swi-prolog.org/pldoc/man?section=semweb-rdfapi)

> The current‘semweb’ package provides two sets of interface predicates. The original set is described in [section 3.3](https://www.swi-prolog.org/pldoc/man?section=semweb-rdf-db). The new API is described in [section 3.1](https://www.swi-prolog.org/pldoc/man?section=semweb-rdf11). 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](https://en.wikipedia.org/wiki/Semantic_triple#Subject,_predicate_and_object))

`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](https://github.com/SWI-Prolog/packages-semweb/tree/master/Tests/Turtle) instead.  
Here is an example of creating a Turtle file ([Turtle (syntax) example](https://en.wikipedia.org/wiki/Turtle_(syntax)#Example)), loading it with rdf\_read\_turtle/3 and displayed it with rdf\_portray\_as/1 and [print\_term/2](https://www.swi-prolog.org/pldoc/doc_for?object=prolog_pretty_print%3Aprint_term/2).

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

```plaintext
# 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`

```prolog
:- 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

```prolog
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](https://en.wikipedia.org/wiki/Turtle_(syntax)#Example))

> 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

```prolog
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](https://www.swi-prolog.org/Tutorials/Semweb.txt)  
[SWI-Prolog for the (semantic) web](https://www.swi-prolog.org/web/)

Related RDF references:  
[RDF Schema 1.1](https://www.w3.org/TR/rdf-schema/)  
[RDF Tutorial](http://w3schools.sinsixx.com/rdf/default.asp.htm)  
[W3 CRDF Validation Service](https://www.w3.org/RDF/Validator/rdfval)
