SWI-Prolog Semantic Web Library 3.0
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • SWI-Prolog Semantic Web Library 3.0
        • Introduction
        • Scalability
        • Two RDF APIs
        • Plugin modules for rdf_db
        • library(semweb/turtle): Turtle: Terse RDF Triple Language
          • rdf_read_turtle/3
          • rdf_load_turtle/3
          • rdf_process_turtle/3
          • turtle_pn_local/1
          • turtle_write_quoted_string/2
          • turtle_write_uri/2
          • rdf_db:rdf_load_stream/3
          • rdf_save_ntriples/2
          • rdf_save_canonical_trig/2
          • rdf_save_trig/2
          • rdf_save_canonical_turtle/2
          • rdf_save_turtle/2
        • library(semweb/rdf_ntriples): Process files in the RDF N-Triples format
        • library(semweb/rdfa): Extract RDF from an HTML or XML DOM
        • library(semweb/rdfs): RDFS related queries
        • Managing RDF input files
        • library(semweb/sparql_client): SPARQL client library
        • library(semweb/rdf_compare): Compare RDF graphs
        • library(semweb/rdf_portray): Portray RDF resources
        • Related packages
        • Version 3 release notes

5 library(semweb/turtle): Turtle: Terse RDF Triple Language

See also
http://www.w3.org/TR/turtle/ (used W3C Recommendation 25 February 2014)

This module implements the Turtle language for representing the RDF triple model as defined by Dave Beckett from the Institute for Learning and Research Technology University of Bristol and later standardized by the W3C RDF working group.

This module acts as a plugin to rdf_load/2, for processing files with one of the extensions .ttl or .n3.

rdf_read_turtle(+Input, -Triples, +Options)
Read a stream or file into a set of triples or quadruples (if faced with TriG input) of the format
rdf(Subject, Predicate, Object [, Graph])

The representation is consistent with the SWI-Prolog RDF/XML and ntriples parsers. Provided options are:

base_uri(+BaseURI)
Initial base URI. Defaults to file://<file> for loading files.
anon_prefix(+Prefix)
Blank nodes are generated as <Prefix>1, <Prefix>2, etc. If Prefix is not an atom blank nodes are generated as node(1), node(2), ...
format(+Format)
One of auto (default), turtle or trig. The auto mode switches to TriG format of there is a { before the first triple. Finally, of the format is explicitly stated as turtle and the file appears to be a TriG file, a warning is printed and the data is loaded while ignoring the graphs.
resources(URIorIRI)
Officially, Turtle resources are IRIs. Quite a few applications however send URIs. By default we do URI->IRI mapping because this rarely causes errors. To force strictly conforming mode, pass iri.
prefixes(-Pairs)
Return encountered prefix declarations as a list of Alias-URI
namespaces(-Pairs)
Same as prefixes(Pairs). Compatibility to rdf_load/2.
base_used(-Base)
Base URI used for processing the data. Unified to [] if there is no base-uri.
on_error(+ErrorMode)
In warning (default), print the error and continue parsing the remainder of the file. If error, abort with an exception on the first error encountered.
error_count(-Count)
If on_error(warning) is active, this option cane be used to retrieve the number of generated errors.
Input is one of stream(Stream), atom(Atom), a http, https or file url or a filename specification as accepted by absolute_file_name/3.
rdf_load_turtle(+Input, -Triples, +Options)
deprecated
Use rdf_read_turtle/3
[det]rdf_process_turtle(+Input, :OnObject, +Options)
Streaming Turtle parser. The predicate rdf_process_turtle/3 processes Turtle data from Input, calling OnObject with a list of triples for every Turtle statement found in Input. OnObject is called as below, where ListOfTriples is a list of rdf(S,P,O) terms for a normal Turtle file or rdf(S,P,O,G) terms if the GRAPH keyword is used to associate a set of triples in the document with a particular graph. The Graph argument provides the default graph for storing the triples and Line is the line number where the statement started.
call(OnObject, ListOfTriples, Graph:Line)

This predicate supports the same Options as rdf_load_turtle/3.

Errors encountered are sent to print_message/2, after which the parser tries to recover and parse the remainder of the data.

See also
This predicate is normally used by load_rdf/2 for processing RDF data.
[semidet]turtle_pn_local(+Atom:atom)
True if Atom is a valid Turtle PN_LOCAL name. The PN_LOCAL name is what can follow the : in a resource. In the new Turtle, this can be anything and this function becomes meaningless. In the old turtle, PN_LOCAL is defined similar (but not equal) to an XML name. This predicate is used by rdf_save_turtle/2 to write files such that can be read by old parsers.
See also
xml_name/2.
[det]turtle_write_quoted_string(+Out, +Value)
Same as turtle_write_quoted_string(Out, Value, false), writing a string with only a single ". Embedded newlines are escapes as \n.
[det]turtle_write_uri(+Out, +Value)
Write a URI as <...>
[multifile]rdf_db:rdf_load_stream(+Format, +Stream, :Options)
(Turtle clauses)
[det]rdf_save_ntriples(+Spec, :Options)
Save RDF using ntriples format. The ntriples format is a subset of Turtle, writing each triple fully qualified on its own line.
[det]rdf_save_canonical_trig(+Spec, :Options)
Save triples in a canonical format. See rdf_save_canonical_turtle/2 foir details.
[det]rdf_save_trig(+Spec, :Options)
Save multiple RDF graphs into a TriG file. Options are the same as for rdf_save_turtle/2. rdf_save_trig/2 ignores the graph(+Graph) option and instead processes one additional option:
graphs(+ListOfGraphs)
List of graphs to save. When omitted, all graphs in the RDF store are stored in the TriG file.
[det]rdf_save_canonical_turtle(+Spec, :Options)
Save triples in a canonical format. This is the same as rdf_save_turtle/2, but using different defaults. In particular:

  • encoding(utf8),
  • indent(0),
  • tab_distance(0),
  • subject_white_lines(1),
  • align_prefixes(false),
  • user_prefixes(false)
  • comment(false),
  • group(false),
  • single_line_bnodes(true)
To be done
Work in progress. Notably blank-node handling is incomplete.
[det]rdf_save_turtle(+Out, :Options)
Save an RDF graph as Turtle. Options processed are:
a(+Boolean)
If true (default), use a for the predicate rdf:type. Otherwise use the full resource.
align_prefixes(+Boolean)
Nicely align the @prefix declarations
base(+Base)
Save relative to the given Base
canonize_numbers(+Boolean)
If true (default false), emit numeric datatypes using Prolog's write to achieve canonical output.
comment(+Boolean)
It true (default), write some informative comments between the output segments
encoding(+Encoding)
Encoding used for the output stream. Default is UTF-8.
expand(:Goal)
Query an alternative graph-representation. See below.
indent(+Column)
Indentation for ; -lists.‘0' does not indent, but writes on the same line. Default is 8.
graph(+Graph)
Save only the named graph
group(+Boolean)
If true (default), using P-O and O-grouping.
inline_bnodes(+Boolean)
if true (default), inline bnodes that are used once.
abbreviate_literals(+Boolean)
if true (default), omit the type if allowed by turtle.
only_known_prefixes(+Boolean)
Only use prefix notation for known prefixes. Without, some documents produce huge amounts of prefixes.
prefixes(+List)
If provided, uses exactly these prefixes. List is a list of prefix specifications, where each specification is either a term Prefix_-_URI or a prefix that is known to rdf_current_prefix/2.
silent(+Boolean)
If true (default false), do not print the final informational message.
single_line_bnodes(+Bool)
If true (default false), write [...] and (...) on a single line.
subject_white_lines(+Count)
Extra white lines to insert between statements about a different subject. Default is 1.
tab_distance(+Tab)
Distance between tab-stops.‘0' forces the library to use only spaces for layout. Default is 8.
user_prefixes(+Boolean)
If true (default), use prefixes from rdf_current_prefix/2.

The option expand allows for serializing alternative graph representations. It is called through call/5, where the first argument is the expand-option, followed by S,P,O,G. G is the graph-option (which is by default a variable). This notably allows for writing RDF graphs represented as rdf(S,P,O) using the following code fragment:

triple_in(RDF, S,P,O,_G) :-
    member(rdf(S,P,O), RDF).

    ...,
    rdf_save_turtle(Out, [ expand(triple_in(RDF)) ]),
Out is one of stream(Stream), a stream handle, a file-URL or an atom that denotes a filename.