Algae

From W3C Wiki
Revision as of 14:23, 10 March 2005 by MortenFrederiksen (Talk)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Algae

Algae is syntactically derivative of, and named after, an access-limited query language called algernon. At this point, the degree of derivation is minimal, especially with Algae2, but the name lingers. Algae2 actually derives much of its syntax from Notation3.

Algae2 syntax

First an example:


ns foaf=<http://xmlns.com/foaf/0.1/>
slurp <http://www.w3.org/People/Eric/ericP-foaf.rdf> ()
ask (?s foaf:knows ?whom. ?whom foaf:name ?name. ?whom foaf:mbox ?mbox)
collect (?name ?mbox)


This shows how to parse a simple document and look for anyone sorry enough to be known by me. The algae script requires this as a -a parameter:

./algae -a'ns foaf=<http://xmlns.com/foaf/0.1/> slurp <http://www.w3.org/People/Eric/ericP-foaf.rdf> () ask (?s foaf:knows ?whom. ?whom foaf:name ?name. ?whom foaf:mbox ?mbox) collect (?name ?mbox)'

and produces a table of output like:


+------------------+-----------------------------------+
|              name|                               mbox|
|------------------|-----------------------------------|
|    "Dan Brickley"|    <mailto:spam@sucks.example.org>|
|       "Hugo Haas"|    <mailto:spam@sucks.example.org>|
|"Gerald Oskoboiny"| <mailto:spammers@suck.example.org>|
| "Nicole Sullivan"|<mailto:get-a-real-job@example.org>|
+------------------+-----------------------------------+


eBNF

<ACTIONs>
input		:= action *
actionStr	:= askStr | assertStr | attachStr | flagsStr | collectStr | namespaceStr | slurpStr
askStr		:= 'ask' DbSpec ? '(' QTerm (('.' QTerm) | ('.'? '||' QTerm) | ('.'? '~' QTerm)) * '.' ? ')'
assertStr	:= 'assert' DbSpec ? '(' decl ('.' decl) * '.' ? ')'
attachStr	:= 'attach' urlvar urlvarlit '(' binding * ')'
collectStr	:= 'collect' '(' variable + ')'
flagsStr	:= 'flags' ('-' | '+') ? '(' binding * ')';
namespaceStr	:= 'ns' (name '=' url) | url
slurpStr	:= 'slurp' sourceStr DbSpec ? '(' binding * ')'
</ACTIONs>

<ASSERTterms>
decl		:= subject propValueList
propValueList	:= (property valueList) | (propValueList ';' property valueList)
valueList	:= (value constraint *) | (valueList ',' value constraint * )
</ASSERTterms>

<QUERYterms>
QTerm		:= decl | ( '(' QTermPlus dotOpt ')' )
</QUERYterms>

<CONSTRAINTS>
constraint	:= '[' expr ']'
expr		:= ( expr op expr ) | single
single		:= ( '-' single ) | ( '!' single ) | ( '~' single ) | ( '(' expr ')' ) | internalName | variable | literal | url
op		:= '*' | '/' | '%' | '+' | '-' | '<' | '>' | '<=' | '>=' | '==' | '!=' | '&' | '^' | '|' | '&&' | '||' | '='
</CONSTRAINTS>

<BASIC_TYPES>
binding		:= ( name '=' urlvarlit ) | ( internalName '=' urlvarlit )
sourceStr	:= url | filename
subject		:= urlvar | ( '[' nulPropValList ']' )
property	:= urlvar
value		:= urlvarlit | ( '[' ( propValList ';' ? '.' ? ) ? ']' )
internalName	:= '%' name
urlvar		:= url | variable;
urlvarlit	:= urlvar | literal | internalName
variable	:= '?' variableName
dbSpec		:= url | variable

name		:= VAR
variableName	:= VAR
literal		:= STR | NUM
filename	:= FILE
url		:= ( URL ) | qname
qname		:= ( VAR ':' VAR ) | VAR
</BASIC_TYPES>

<LEXER>
URL		:= <\s*([\w]+:[^\(\)\s\"\'\>\\]+)\s*\>
STR		:= ( '"' [^"]+ '"' ) | ( "'" [^']+ "'" )
NUM		:= [0-9]+(?:\.[0-9]+)?
VAR		:= [A-Za-z][A-Za-z0-9_]*
FILE		:= [\.\;\~\|\&\w\/\-]+
</LEXER>


Further Examples

You may collect from different databases. In this case, you must attach to a database and name it, and ask queries of the database you want in particular:


# declare a handy namespace
ns dc=<http://purl.org/dc/elements/1.1/>
# attach to a couple namespaces
attach <http://www.w3.org/1999/02/26-modules/algae#ephemeral> ?foo ()
attach <http://www.w3.org/1999/02/26-modules/algae#ephemeral> ?bar ()
# assert into one and slurp into the other
assert ?foo (dc:title dc:title "title")
slurp <http://www.w3.org/People/Eric/ericP-foaf.rdf> ?bar ()
# now ask one for all its info
ask ?foo (?s ?p ?o)
collect (?s ?p ?o)