ASRDF Tutorial
ASRDF (Abreviated Syntax for RDF) is an RDF XML syntax converter allowing you to quickly and accurately write RDF files from scratch. It is a terse RDF triples notation using the outliner paradigm to structure RDF graph constructs.
For example:
subject-resource-1
predicate-resource-1
literal-object-1
is a simple triple with subject-resource-1 as subject, predicate-resource-1 as predicate and literal-object-1 as object.
An RDF XML syntax equivalent would be (transcribed by Mozquery ASRDF tool):
<rdf:Description rdf:about="subject-resource-1">
<predicate-resource-1>literal-object-1</predicate-resource-1>
</rdf:Description>
This simple outline construct uses line breaking and tabulations characters to represent the triple (statement) components:
the triple subject has no tab before its resource name, the triple predicate has one tab before its resource name and the literal/resource object has two tabs before its content/resource name.
ASRDF outline constructs are recursive, so you can have as many outlining levels as you want, for instance:
subject-resource-2
predicate-resource-2
() object-resource-2
nested-predicate-resource-2
() nested-object-resource-2
yields:
<rdf:Description rdf:about="subject-resource-2">
<predicate-resource-2 rdf:resource="object-resource-2" />
</rdf:Description>
<rdf:Description rdf:about="object-resource-2">
<nested-predicate-resource-2 rdf:resource="nested-object-resource-2" />
</rdf:Description>
When a triple object is a resource (not a literal) then you can use it as the subject of a nested triple, and so on.
As a shortcut you can outline at the same level either multiple predicates of the same subject or multiple objects of the same predicate. Which gives you:
subject-resource-3
predicate-resource-3.a
literal-object-3.a
literal-object-3.b
predicate-resource-3.b
literal-object-3.c
literal-object-3.d
as:
<rdf:Description rdf:about="subject-resource-3">
<predicate-resource-3.a>literal-object-3.a</predicate-resource-3.a>
<predicate-resource-3.a>literal-object-3.b</predicate-resource-3.a>
<predicate-resource-3.b>literal-object-3.c</predicate-resource-3.b>
<predicate-resource-3.b>literal-object-3.d</predicate-resource-3.b>
</rdf:Description>
Object literals are not restricted to a single line: long literals can use as many lines as necessary, using the backward slash character to indicate each line continuation, as in:
subject-resource-4
predicate-resource-4
literal-object-4\
needing several lines\
to be complete
You can also use blank lines and comment lines or line ending to format and document our code:
# A line begining with a # is a comment line
resource # A space character (tab or space) followed by a # character begins a comment
What makes ASRDF notation very powerful is the availability of typical RDF XML attribute (data typing, data parsing) and construct (container, collection) representations.
To define a bag container just use:
(Bag) container-subject-resource-6
_
1st literal-object member
2nd literal-object member
3rd literal-object member
yields:
<rdf:Bag rdf:about="container-subject-resource-6">
<rdf:li>1st literal-object member</rdf:li>
<rdf:li>2nd literal-object member</rdf:li>
<rdf:li>3rd literal-object member</rdf:li>
</rdf:Bag>
For all containers, the subject is prefixed by the parenthesized container type and the predicate is represented by the underscore character (_).
You can also use blank node resources for object containers:
subject-resource-7
predicate-resource-7
(Seq)
_
1st literal-object member
2nd literal-object member
(Seq)
_
3rd literal-object member
4th literal-object member
is as:
<rdf:Description rdf:about="subject-resource-7">
<predicate-resource-7 rdf:resource="subject-resource-7:bnode:2003-11-05T17:36:48.046:0" />
<predicate-resource-7 rdf:resource="subject-resource-7:bnode:2003-11-05T17:36:48.046:1" />
</rdf:Description>
<rdf:Seq rdf:about="subject-resource-7:bnode:2003-11-05T17:36:48.046:0">
<rdf:li>1st literal-object member</rdf:li>
<rdf:li>2nd literal-object member</rdf:li>
</rdf:Seq>
<rdf:Seq rdf:about="subject-resource-7:bnode:2003-11-05T17:36:48.046:1">
<rdf:li>3rd literal-object member</rdf:li>
<rdf:li>4th literal-object member</rdf:li>
</rdf:Seq>
Here, unique digest identifiers have been automaticaly given to blank nodes.
Literal can be given data types according to the revised RDF XML syntax. Any literal begining with an arobace character followed by a valid XML Schema data type will be typed:
subject-resource-8
predicate-resource-8
@string literal-object-8
@string:en-US literal-object-8.b
@positiveInteger 1234
is transcribed as:
<rdf:Description rdf:about="subject-resource-8">
<predicate-resource-8 rdf:datatype="http://www.w3.org/2001/XMLSchema#string">literal-object-8.a</predicate-resource-8>
<predicate-resource-8 rdf:datatype="http://www.w3.org/2001/XMLSchema#string" xml:lang="en-US">literal-object-8.b</predicate-resource-8>
<predicate-resource-8 rdf:datatype="http://www.w3.org/2001/XMLSchema#positiveInteger">1234</predicate-resource-8>
</rdf:Description>
String attribute can even be associated with an xml:lang code as shown in this second literal object.
Data typing can be turn on and off with a preference setting.
Literal or resource parsing are also taken into account:
subject-resource-9
predicate-resource-9
@Literal <xmltag>an XML code fragment</xmltag>
A literal object prefixed with "@Literal" will give:
<rdf:Description rdf:about="subject-resource-9">
<predicate-resource-9 rdf:parseType="Literal"><xmltag>an XML code fragment</xmltag></predicate-resource-9>
</rdf:Description>
A collection of object resources is represented by adding to the list of resources an initial (Collection) resource object:
subject-resource-10
predicate-resource-10
(Collection) # this Collection pseudo-resource marks the begining of the list
() collection-object-resource-1
() collection-object-resource-2
() collection-object-resource-3
will be converted as:
<rdf:Description rdf:about="subject-resource-10">
<predicate-resource-10 rdf:parseType="Collection">
<rdf:Description rdf:about="collection-object-resource-1" />
<rdf:Description rdf:about="collection-object-resource-2" />
<rdf:Description rdf:about="collection-object-resource-3" /></predicate-resource-10>
</rdf:Description>
As a terse notation language, ASRDF allows you to define shortcuts for resources frequently used. This particularly useful for predicate resources. For instance:
.keys
PR = predicate-resource-11
SPR = PR -a
.triples
subject-resource-11
PR
() object-resource-11
SPR
() object-resource-11-a
is:
<rdf:Description rdf:about="subject-resource-11">
<predicate-resource-11 rdf:resource="object-resource-11" />
<predicate-resource-11-a rdf:resource="object-resource-11-a" />
</rdf:Description>
Here, the PR key is replaced by its property as is the SPR key, itself defined using the PR key. This is very handy for resources having a common prefix.
Keys can be used directly as resource shortcuts or as prefixes to resources. Then the key part must be separated from the resource extension with a space: "KEY resource".
You can also define key properties for namespaces and for XML entites, which are grouped in a DOCTYPE declaration at the begining of the resulting RDF file. All keys are defined as Java properties are:
As you may have noted, an ASRDF file is divided into several sections:
doctype, namespaces, (shortcut) keys and triples.
To help you modularized your ASRDF files there are also section devoted to file imports:
import_doctype, import_ns, import_keys and import_triples. Any valid file path (with read access) included in an import section will be parsed and its content used in the parsing of the main ASRDF file.
Any section is entered using its name prefixed with a period:
#======== DOCTYPE SECTION
.import_doctype
/path/to/doctype/file.properties
.doctype
AN_XML_ENTITY = Entity value
#======== NAMESPACES SECTION
.import_ns
/path/to/namespaces/file.properties
.ns
NS.RDF = http://www.w3.org/1999/02/22-rdf-syntax-ns#
rdf = &NS.RDF; # NS.RDF entity previously defined in /path/to/doctype/file.properties
#======== KEYS SECTION
.import_keys
/path/to/keys/file.properties
.keys
A_KEY = key-resource
#======== TRIPLES SECTION
.import_triples
/path/to/triples/file.properties
.triples
subject
predicate
object
This is the ordinary ordering of an ASRDF file. But you can mix these section as you want, even including other imports in any triples section (other properties files must contain only key/property definitions). As a convention, properties files are given a "properties" extension and triples files an "asrdf" extension. But you can use your own extension schemes.
Mozquery ASRDF user interface
ASRDF Mozquery user interface is based on the interface designed for
JS Console extension I developed (hosted at mozdev.org).
It has the same look and functionalities, but due to tabulation constraints (poorly managed by Mozilla rich text editing features), the Code panel is not taken as the input of the parsing process (as it is in JS Console).
In
Mozquery the input is always the file displayed in the Code panel, not the content, but you can however edit this content and save it in a file.
So to run
Mozquery on an
ASRDF file, just edit the file with your favorite editor, load it in the Code panel, using the
Load entry of the contextual menu (or with the Ctrl + O shortcut) and use the
Run menu entry (or the Ctrl + R shortcut). The Output panel will then display the RDF translation of your
ASRDF file, or the Error panel will display a message to indicate where an error occured.
If you have to change something in your source file, do it in your editor and reload the file, using the
Reload menu entry (or the Ctrl + Shift + O shortcut) and run it again.
Output panel content can, of course be saved to a file, using the
Save menu entry (or with the Ctrl + S shortcut).
ASRDF example
To give you a concrete example of an ASRDF file, have a look at the mozquerystore.asrdf file (located in the chrome/mozquery/content/profile directory). It defines the Store items you get in the Store > List code... menu entry. Its length is 1900 characters. Its RDF equivalent, mozquerystore.rdf, is 7823 characters length. That's a 1:4 eloquant ratio.