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/rdf_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 file. Options provides additional processing
options. Currently 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.
- 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), register xmlns= namespace declarations as ns/2 namespaces 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.
Other options are forwarded to process_rdf/3.
- [det]rdf_unload(+Source)
- Identify the graph loaded from Source and use rdf_unload_graph/1 to erase this graph.
- [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 (default true) 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_baseoption - 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)
- Explicitely 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:basedeclaration that is written normally when using thebase_urioption. - 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). - rdf_make
- Reload all loaded files that have been modified since the last time they were loaded.
3.6.1 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
rdfandrdfsare 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.
- [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)
- convert_typed_literal(:Goal)
3.6.2 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_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.