- Documentation
- Reference manual
- Packages
- SWI-Prolog Semantic Web Library 3.0
- Two RDF APIs
- library(semweb/rdf_db): The RDF database
- Query the RDF database
- Enumerating objects
- Modifying the RDF database
- Update view, transactions and snapshots
- Type checking predicates
- Loading and saving to file
- Graph manipulation
- Literal matching and indexing
- Predicate properties
- Prefix Handling
- Miscellaneous predicates
- Memory management considerations
- library(semweb/rdf_db): The RDF database
- Two RDF APIs
- SWI-Prolog Semantic Web Library 3.0
3.3.6 Loading and saving to file
The RDF library can read and write triples in RDF/XML and a
proprietary binary format. There is a plugin interface defined to
support additional formats. The library(semweb/turtle)
uses
this plugin API to support loading Turtle files using rdf_load/2.
- [det]rdf_load(+FileOrList)
- Same as
rdf_load(FileOrList, [])
. See rdf_load/2. - [det]rdf_load(+FileOrList, :Options)
- Load RDF data. Options provides additional processing
options. Defined options are:
- blank_nodes(+ShareMode)
- How to handle equivalent blank nodes. If
share
(default), equivalent blank nodes are shared in the same resource. - base_uri(+URI)
- URI that is used for rdf:about="" and other RDF constructs that are relative to the base uri. Default is the source URL.
- concurrent(+Jobs)
- If FileOrList is a list of files, process the input files using Jobs threads concurrently. Default is the mininum of the number of cores and the number of inputs. Higher values can be useful when loading inputs from (slow) network connections. Using 1 (one) does not use separate worker threads.
- format(+Format)
- Specify the source format explicitly. Normally this is deduced from the
filename extension or the mime-type. The core library understands the
formats xml (RDF/XML) and triples (internal quick load and cache
format). Plugins, such as
library(semweb/turtle)
extend the set of recognised extensions. - graph(?Graph)
- Named graph in which to load the data. It is not allowed to load
two sources into the same named graph. If Graph is unbound,
it is unified to the graph into which the data is loaded. The default
graph is a
file://
URL when loading a file or, if the specification is a URL, its normalized version without the optional #fragment. - if(Condition)
- When to load the file. One of
true
,changed
(default) ornot_loaded
. - modified(-Modified)
- Unify Modified with one of
not_modified
,cached(File)
,last_modified(Stamp)
orunknown
. - cache(Bool)
- If
false
, do not use or create a cache file. - register_namespaces(Bool)
- If
true
(defaultfalse
), registerxmlns
namespace declarations or Turtle@prefix
prefixes using rdf_register_prefix/3 if there is no conflict. - silent(+Bool)
- If
true
, the message reporting completion is printed using levelsilent
. Otherwise the level isinformational
. See also print_message/2. - prefixes(-Prefixes)
- Returns the prefixes defined in the source data file as a list of pairs.
- multifile
Boolean+
- Indicate that the addressed graph may be populated with triples from multiple sources. This disables caching and avoids that an rdf_load/2 call affecting the specified graph cleans the graph.
Other options are forwarded to process_rdf/3. By default, rdf_load/2 only loads RDF/XML from files. It can be extended to load data from other formats and locations using plugins. The full set of plugins relevant to support different formats and locations is below:
:- use_module(library(semweb/turtle)). % Turtle and TriG :- use_module(library(semweb/rdf_ntriples)). :- use_module(library(semweb/rdf_zlib_plugin)). :- use_module(library(semweb/rdf_http_plugin)). :- use_module(library(http/http_ssl_plugin)).
- See also
- rdf_db:rdf_open_hook/3,
library(semweb/rdf_persistency)
andlibrary(semweb/rdf_cache)
- [det]rdf_unload(+Source)
- Identify the graph loaded from Source and use rdf_unload_graph/1
to erase this graph.
- deprecated
- For compatibility, this predicate also accepts a graph name instead of a source specification. Please update your code to use rdf_unload_graph/1.
- [det]rdf_save(+Out)
- Same as
rdf_save(Out, [])
. See rdf_save/2 for details. - [det]rdf_save(+Out, :Options)
- Write RDF data as RDF/XML. Options is a list of one or more
of the following options:
- graph(+Graph)
- Save only triples associated to the given named Graph.
- anon(Bool)
- If
false
(defaulttrue
) do not save blank nodes that do not appear (indirectly) as object of a named resource. - base_uri(URI)
- BaseURI used. If present, all URIs that can be represented relative to
this base are written using their shorthand. See also
write_xml_base
option. - convert_typed_literal(:Convertor)
- Call Convertor(-Type, -Content, +RDFObject), providing the opposite for the convert_typed_literal option of the RDF parser.
- document_language(+Lang)
- Initial
xml:lang
saved with rdf:RDF element. - encoding(Encoding)
- Encoding for the output. Either utf8 or iso_latin_1.
- inline(+Bool)
- If
true
(defaultfalse
), inline resources when encountered for the first time. Normally, only bnodes are handled this way. - namespaces(+List)
- Explicitly specify saved namespace declarations. See rdf_save_header/2 option namespaces for details.
- sorted(+Boolean)
- If
true
(defaultfalse
), emit subjects sorted on the full URI. Useful to make file comparison easier. - write_xml_base(Bool)
- If
false
, do not include thexml:base
declaration that is written normally when using thebase_uri
option. - xml_attributes(+Bool)
- If
false
(defaulttrue
), never use xml attributes to save plain literal attributes, i.e., always used an XML element as in<name>Joe</name>
.
Out Location to save the data. This can also be a file-url ( file://path
) or a stream wrapped in a termstream(Out)
.- See also
- rdf_save_db/1
- rdf_make
- Reload all loaded files that have been modified since the last time they were loaded.
Partial save
Sometimes it is necessary to make more arbitrary selections of
material to be saved or exchange RDF descriptions over an open network
link. The predicates in this section provide for this. Character
encoding issues are derived from the encoding of the Stream,
providing support for
utf8
, iso_latin_1
and ascii
.
- rdf_save_header(+Fd, +Options)
- Save XML document header, doctype and open the RDF environment. This
predicate also sets up the namespace notation.
Save an RDF header, with the XML header, DOCTYPE, ENTITY and opening the rdf:RDF element with appropriate namespace declarations. It uses the primitives from section 3.5 to generate the required namespaces and desired short-name. Options is one of:
- graph(+URI)
- Only search for namespaces used in triples that belong to the given named graph.
- namespaces(+List)
- Where List is a list of namespace abbreviations. With this
option, the expensive search for all namespaces that may be used by your
data is omitted. The namespaces
rdf
andrdfs
are added to the provided List. If a namespace is not declared, the resource is emitted in non-abreviated form.
- [det]rdf_save_footer(Out:stream)
- Finish XML generation and write the document footer.
- See also
- rdf_save_header/2, rdf_save_subject/3.
- [det]rdf_save_subject(+Out, +Subject:resource, +Options)
- Save the triples associated to Subject to Out. Options:
- graph(+Graph)
- Only save properties from Graph.
- base_uri(+URI)
- convert_typed_literal(:Goal)
- document_language(+XMLLang)
- See also
- rdf_save/2 for a description of these options.
Fast loading and saving
Loading and saving RDF format is relatively slow. For this reason we
designed a binary format that is more compact, avoids the complications
of the RDF parser and avoids repetitive lookup of (URL) identifiers.
Especially the speed improvement of about 25 times is worth-while when
loading large databases. These predicates are used for caching by
rdf_load/2 under certain
conditions as well as for maintaining persistent snapshots of the
database using
library(semweb/rdf_persistency)
.
- [det]rdf_save_db(+File)
- [det]rdf_save_db(+File, +Graph)
- Save triples into File in a quick-to-load binary format. If Graph is supplied only triples flagged to originate from that database are added. Files created this way can be loaded using rdf_load_db/1.
- [det]rdf_load_db(+File)
- Load triples from a file created using rdf_save_db/2.