1:- module(
    2  rdf_export,
    3  [
    4    rdf_write_quad/2,    % +Out, +Quad
    5    rdf_write_quad/3,    % +Out, +Tuple, +GraphName
    6    rdf_write_triple/2,  % +Out, +Tuple
    7    rdf_write_tuple/2    % +Out, +Tuple
    8  ]
    9).

RDF export

RDF export predicates that are backend-independent.

*/

   17:- use_module(library(lists)).   18:- use_module(library(semweb/turtle), []).   19
   20:- use_module(library(hash_ext)).   21:- use_module(library(rdf_prefix)).   22:- use_module(library(rdf_term)).   23
   24:- rdf_meta
   25   rdf_write_iri(+, r),   rdf_write_literal(+, o),   rdf_write_name(+, o),   rdf_write_quad(+, t),   rdf_write_quad(+, t, r),   rdf_write_quad(+, r, r, o, r),   rdf_write_triple(+, t),   rdf_write_tuple(+, t).
 rdf_write_graph(+Out:ostream, +GraphName:rdf_graph_name) is det
   40rdf_write_graph(Out, G) :-
   41  rdf_write_iri(Out, G).
 rdf_write_iri(+Out:ostream, +Iri:rdf_iri) is det
   47rdf_write_iri(Out, Iri) :-
   48  turtle:turtle_write_uri(Out, Iri).
 rdf_write_literal(+Out:ostream, +Literal:rdf_literal) is det
   54rdf_write_literal(Out, Input) :-
   55  rdf_literal_dwim(Input, Literal),
   56  rdf_write_literal_(Out, Literal).
   57
   58rdf_write_literal_(Out, literal(lang(LTag,Lex))) :- !,
   59  turtle:turtle_write_quoted_string(Out, Lex),
   60  format(Out, "@~a", [LTag]).
   61rdf_write_literal_(Out, literal(type(D,Lex))) :-
   62  turtle:turtle_write_quoted_string(Out, Lex),
   63  format(Out, "^^", []),
   64  rdf_write_iri(Out, D).
 rdf_write_name(+Out:ostream, +Name:rdf_name) is det
   70% IRI.
   71rdf_write_name(Out, Iri) :-
   72  rdf_is_iri(Iri), !,
   73  rdf_write_iri(Out, Iri).
   74% Literal.
   75rdf_write_name(Out, Literal) :-
   76  rdf_is_literal(Literal),
   77  rdf_write_literal(Out, Literal).
 rdf_write_quad(+Out:ostream, +Quad:rdf_quad) is det
 rdf_write_quad(+Out:ostream, +Tuple:rdf_tuple, +GraphName:rdf_graph_name) is det
Quad must be a quadruple (denoted by compound term rdf/4). Triples (denoted by compound term rdf/3) are not supported.
   87rdf_write_quad(Out, tp(S,P,O,G)) :-
   88  rdf_write_triple_open_(Out, tp(S,P,O)),
   89  put_char(Out, ' '),
   90  rdf_write_graph(Out, G),
   91  format(Out, ".\n", []).
   92
   93
   94rdf_write_quad(Out, tp(S,P,O), G) :- !,
   95  rdf_write_quad(Out, tp(S,P,O,G)).
   96rdf_write_quad(Out, tp(S,P,O,_), G) :-
   97  rdf_write_quad(Out, tp(S,P,O,G)).
 rdf_write_term(+Out:ostream, +Name:rdf_name) is det
  103rdf_write_term(Out, Iri) :-
  104  rdf_is_iri(Iri), !,
  105  rdf_write_iri(Out, Iri).
  106rdf_write_term(Out, Literal) :-
  107  rdf_write_literal(Out, Literal).
 rdf_write_triple(+Out:ostream, +Tuple:rdf_tuple) is det
Accepts triples (tp/3) and quadrupleds (tp/4), but always writes a triple.
  116rdf_write_triple(Out, tp(S,P,O)) :- !,
  117  rdf_write_triple_open_(Out, tp(S,P,O)),
  118  format(Out, ".\n", []).
  119rdf_write_triple(Out, tp(S,P,O,_)) :-
  120  rdf_write_triple(Out, tp(S,P,O)).
  121
  122rdf_write_triple_open_(Out, tp(S,P,O)) :-
  123  rdf_write_term(Out, S),
  124  put_char(Out, ' '),
  125  rdf_write_iri(Out, P),
  126  put_char(Out, ' '),
  127  rdf_write_term(Out, O).
 rdf_write_tuple(+Out:ostream, +Tuple:rdf_tuple) is det
Writes a triple if Tuple is a triple pattern term (tp/3). Writes a quadruple if Tuple is a quadruple pattern term (tp/4).
  136rdf_write_tuple(Out, tp(S,P,O)) :- !,
  137  rdf_write_triple(Out, tp(S,P,O)).
  138rdf_write_tuple(Out, tp(S,P,O,G)) :-
  139  rdf_write_quad(Out, tp(S,P,O,G))