Chapter 10. RDF, RDF Tools, and the Content Model-P4
When you create RDF statements with assertions or work with in-memory
datasources, it is often difficult to remember the shape of the graph, which
statements exist about which resources, or which objects are attached to
which subjects. These "getter" methods can help you verify the shape of
your graph.
10.3.6. nsIRDFRemoteDataSource
The Section 10.3.3 section (earlier in this chapter) showed how to load a
datasource from a remote server simply. If you want control over that
datasource, you can manage it by using the nsIRDFRemoteDatasource to set
up a remote datasource:
xml = '@mozilla.org/rdf/datasource;1?name=xml-
datasource';
datasource = Components.classes[xml].
createInstance(Components.interfaces.nsIRDFRemoteDa
taSource);
datasource.Init('http://books.mozdev.org/file.rdf')
;
datasource.Refresh(false);
In this example, the Init and Refresh methods control the datasource on
the server. In addition to these methods, you can call the Flush method to
flush the data that's been changed and reload, or you can check whether the
datasource is loaded by using the loaded property:
if (datasource.loaded) {
// Do something
}
Built-in datasources that implement nsIRDFRemoteDataSource (and other
necessary interfaces) and do their own data handling include:
@mozilla.org/rdf/datasource;1?name=history
@mozilla.org/browser/bookmarks-service;1
@mozilla.org/autocompleteSession;1?type=history
@mozilla.org/browser/global-history;1
@mozilla.org/rdf/datasource;1?name=bookmarks
10.3.7. nsIRDFPurgeableDataSource
Using the nsIRDFPurgeableDatasource interface allows you to delete a
whole section of an existing in-memory datasource in one fell swoop. This
means that all relatives -- all statements derived from that node -- are
removed. When you work with large in-memory datasources (such as email
systems), the using interface can manipulate the data efficiently. The
Sweep( ) method can delete a section that is marked in the datasource.
datasource.
QueryInterface(Components.interfaces.nsIRDFPurgeabl
eDataSource);
rootSubject = RDF.GetResource('urn:root');
predicate =
RDF.GetResource('http://books.mozdev.org/rdf#chapte
rs');
object = RDF.GetResource('Chapter1');
datasource.Mark(rootSubject,predicate,object,true);
datasource.Sweep( );
In this instance, a statement about a chapter in a book is marked and then
removed from the datasource. You can also mark more than one node before
sweeping.
10.3.8. nsIRDFNode, nsIRDFResource, and nsIRDFLiteral
These types of objects come from only a few different places. Here are all
the functions that can return the resource of a literal:
nsIRDFService.GetResource
nsIRDFService.GetAnonymousResource
nsIRDFService.GetLiteral
nsIRDFDataSource.GetSource
nsIRDFDataSource.GetTarget
nsIRDFNode is the parent of nsIRDFResource and nsIRDFLiteral. It is not
used often because it's sole function is to test equality:
isEqual = resource1.EqualsNode(resource2);
The other two interfaces inherit this function automatically. EqualsNode
tests the equivalency of two resources, which can be useful when you try to
put together different statements (e.g., "Eric wrote a book" and "[This] book
is about XML") and want to verify that a resource like "book" is the same in
both cases.
10.3.8.1. nsIRDFResource
Like nsIRDFNode, nsIRDFResource is a minimalist interface. Here are the
functions and the property available in a resource from the nsIRDFResource
interface:
resource = RDF.GetAnonymousResource( );
// get the resource value, something like
'rdf:#$44RG7'
resourceIdentifierString = resource.Value;
// compare the resource to an identifier
isTrue =
resourceEqualsString(resourceIdentifierString);
// Give the resource a real name.
resource.Init('Eric');
10.3.8.2. nsIRDFLiteral
A literal's value can be read but not written. To change the value of a literal,
make a new literal and set it properly:
aValue = literal.Value;
Note that aValue could be a string or an integer in this case. The base type
conversion, based on the data's format, is done automatically.
10.3.9. nsIRDFContainerUtils
This interface facilitates the creation of containers and provides other
container-related functions. It provides functions that make and work with a
sequence, bag, and alternative. (The functions work the same way
for all types of containers, so only sequence is covered here.) To create an
instance of nsIRDFContainerUtils, use the following:
containerUtils =
Components.classes['@mozilla.org/rdf/container-
utils;1'].
getService(Components.interfaces.nsIRDFContainerUti
ls);
Once you create an anonymous resource, you can create a sequence from it.
Then you can test the type of the container and see whether it's empty:
// create an anonymous resource
anonResource = RDF.GetAnonymousResource( );
// create a sequence from that resource
aSequence =
containerUtils.MakeSeq(datasource,anonResource);
// test the resource
// (all of these are true)
isContainer =
containerUtils.isContainer(datasource,anonResource)
;