2 Parsing RDF in Prolog
We realised an RDF compiler in Prolog on top of the sgml2pl package (providing a name-space sensitive XML parser). The transformation is realised in two passes.
The first pass rewrites the XML term into a Prolog term conveying the same information in a more friendly manner. This transformation is defined in a high-level pattern matching language defined on top of Prolog with properties similar to DCG (Definite Clause Grammar).
The source of this translation is very close to the BNF notation used by the specification, so correctness is `obvious'. Below is a part of the definition for RDF containers. Note that XML elements are represented using a term of the format:
element(Name, [AttrName = Value...], [Content ...])
memberElt(LI) ::= \referencedItem(LI). memberElt(LI) ::= \inlineItem(LI). referencedItem(LI) ::= element(\rdf(li), [ \resourceAttr(LI) ], []). inlineItem(literal(LI)) ::= element(\rdf(li), [ \parseLiteral ], LI). inlineItem(description(description, _, _, Properties)) ::= element(\rdf(li), [ \parseResource ], \propertyElts(Properties)). inlineItem(LI) ::= element(\rdf(li), [], [\rdf_object(LI)]), !. % inlined object inlineItem(literal(LI)) ::= element(\rdf(li), [], [LI]). % string value
Expression in the rule that are prefixed by the \
operator acts as invocation of another rule-set. The body-term is
converted into a term where all rule-references are replaced by
variables. The resulting term is matched and translation of the
arguments is achieved by calling the appropriate rule. Below is the
Prolog code for the
referencedItem rule:
referencedItem(A, element(B, [C], [])) :- rdf(li, B), resourceAttr(A, C).
Additional code can be added using a notation close to the Prolog DCG notation. Here is the rule for a description, producing properties both using propAttrs and propertyElts.
description(description, About, BagID, Properties) ::= element(\rdf('Description'), \attrs([ \?idAboutAttr(About), \?bagIdAttr(BagID) | \propAttrs(PropAttrs) ]), \propertyElts(PropElts)), { !, append(PropAttrs, PropElts, Properties) }.