XML.com: XML From the Inside Out
oreilly.comSafari Bookshelf.Conferences.

advertisement

RDF Applications with Prolog
by Bijan Parsia | Pages: 1, 2, 3, 4, 5, 6

The rdf_db Module

In the previous section, I gave some examples of reading into the Prolog knowledge base a set of RDF statements. Jan Wielemaker (the author of SWI-Prolog) put together a little module of convenience predicates for doing this sort of thing, called rdf_db. One very nice feature of rdf_db is that it allows you to maintain multiple, disjoint RDF "databases". You can have the advantages of dealing with RDF documents as separate lists of statements while having a Prologesque interface. rdf_db is also a good example of a simple Prolog-based RDF application.

To get going with rdf_db, first download and pop into a convenient spot (for me, that's the "library" directory) rdf_db.pl and xml_write.pl. Now a simple [library(rdf_db)]. at the query prompt will load all the relevant stuff. The slightly confusingly named rdf_load/1 will open the file at the supplied path, parse the RDF/XML into rdf/3 based terms, and then asserts them into a special RDF database named, by default, "user".

?- rdf_load('bijan/xmlhack.rss').

Yes

?- rdf(X,Y,Z).

X = 'http://xmlhack.com/rss10.php'
Y = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
Z = 'http://purl.org/rss/1.0/channel' ;

X = 'http://xmlhack.com/rss10.php'
Y = 'http://purl.org/rss/1.0/title'
Z = literal('XMLhack')

Yes

(Prolog said "Yes" because I didn't ask it for any more answers. It says "No" when it can't find an answer, and in this case it had already found one.)

Most of the predicates in rdf_db have two variants, the basic one and one with an extra argument. The extra argument specifies the particular RDF database which is to be the context of the predicate. Without that argument, the predicate is evaluated in the context of the current RDF database, which may be specified by the rdf_db/2 predicate. For example,

?-rdf_load('bijan/rdfiglog-2001-03-22.rdf', rdfiglog).

Yes

?-rdf_db(CurrentDB,rdfiglog).

CurrentDB = user

Yes

?-rdf_db(CurrentDB).

CurrentDB = rdfiglog

Yes

?-rdf(X, Y, Z).

X = 'irc://irc.openprojects.net/rdfig'
Y = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
Z = 'http://xmlns.com/foaf/0.1/ChatChannel'

Yes

?-rdf(X,Y,Z, user).

X = 'http://xmlhack.com/rss10.php'
Y = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
Z = 'http://purl.org/rss/1.0/channel'

Yes

In the previous section, when I asserted the terms returned by load_rdf/2, those terms were of the form rdf(Subject,Predicate,Object), and that was how they were entered into the Prolog database. With rdf_db, the rdf/3 predicate is, in fact, a rule! Each ground level rdf statement is asserted as rdf/4 so that each statement's "provenance" can be determined (by the fourth argument). This is, of course, merely an implementation detail. One could replace rdf_db's use of the builtin Prolog knowledge base with something else.

The rdf_db library also has convenience predicates for asserting and retracting RDF statments, either from the current RDF database or from a named one. Perhaps more importantly, rdf_register_ns/2assigns prefixes to URIs in the standard XML namespace way but also permits the use of prefixed names ("QNames") in Prolog statements:

?-rdf_register_ns(dc, 'http://purl.org/dc/elements/1.1/').

Yes

?-rdf(X,dc:date,Y).

X = 'Description__5'
Y = literal('2001-03-22T00:00:01Z') ;
X = 'Description__7'
Y = literal('2001-03-22T00:00:18Z')

Yes

?-rdf_assert('http://www.xmlhack.com/a/spurious/article', dc:creator, literal('A spurious author!')).

Yes

?- rdf('http://www.xmlhack.com/a/spurious/article', dc:creator, Who).

Who = literal('A spurious author!')

Yes

?-rdf_save('bijan/current-rdf-db-dumped-to-XML-format.rdf').

Yes

The rdf_save/1 and /2 predicates will use the namespace declarations established by rdf_register_ns/2.

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow