1. Brian Beck
  2. Telescope

Wiki

Clone wiki

Telescope / SPARQLSandbox

SPARQL Sandbox

Some features on this page are prefixed with a [CODE] in order to reference them elsewhere.

Select shortcut

select(a, b, c) should be a shortcut for Select([a, b, c]) since it is the common case.


Building intermediate graph patterns

Callables like optional and union should return graph patterns that can be generatively modified:

select(v.title, v.price).where(
    (v.x, DC.title, v.title),
    optional((v.x, NS.price, v.price)).filter(v.price < 30)
)

[POL] Predicate-object lists (common subject)

There are several interesting possibilities here. The following expressions all generate the same SPARQL query:

SELECT ?name
WHERE { ?x foaf:name ?name ; ns:empId ?emp }

A: Subject callable

A.1: Return a callable that accepts any number of predicate-object pairs

select(v.name).where(
    Subject(v.x)((FOAF.name, v.name), (NS.empId, v.emp))
)

A.2: Subject first, followed by predicate-object pairs

select(v.name).where(
    Subject(v.x, (FOAF.name, v.name), (NS.empId, v.emp))
)

B: (Subject, predicate-object list) 2-tuples

select(v.name).where(
    (v.x, [(FOAF.name, v.name), (NS.empId, v.emp)])
)

C: Indexable variable expressions

C.1: Using a sequence of predicate-object 2-tuples

select(v.name).where(
    v.x[(FOAF.name, v.name), (NS.empId, v.emp)]
)

C.2: Using slice syntax for predicate-object pairs

select(v.name).where(
    v.x[FOAF.name: v.name, NS.empId: v.emp]
)

[OL] Object lists (common subject-predicate)

A: Objects or ObjectList callable

select(v.x).where(
    (v.x, FOAF.nick, Objects("Alice", "Alice_"))
)

B: Python list in the object position

select(v.x).where(
    (v.x, FOAF.nick, ["Alice", "Alice_"])
)

[BNS] Blank node subjects

The following expressions all generate the same SPARQL query:

SELECT ?name
WHERE { _:b18 foaf:name ?name .
        _:b18 foaf:mbox <mailto:alice@example.org> }

A: Python list of 2-tuples

select(v.name).where(
    [(FOAF.name, v.name), (FOAF.mbox, URIRef('mailto:alice@example.org'))]
)

B: Some singleton

(Show here using syntax POL:A.1.)

select(v.name).where(
    Subject(Some)((FOAF.name, v.name), (FOAF.mbox, URIRef('mailto:alice@example.org')))
)

or:

select(v.name).where(
    Some((FOAF.name, v.name), (FOAF.mbox, URIRef('mailto:alice@example.org')))
)

C: Empty Subject call

If POL:A.1 is chosen, an empty call to Subject could imply a blank node:

select(v.name).where(
    Subject()((FOAF.name, v.name), (FOAF.mbox, URIRef('mailto:alice@example.org')))
)

Conclusions

The following SPARQL query uses both predicate-object lists, object lists, and blank node subjects:

SELECT ?name
WHERE { ?x foaf:name ?name ;
           foaf:nick "Alice" , "Alice_" ;
           foaf:knows [ a foaf:Person ; foaf:mbox <mailto:bob@example.org> ] }

Using POL:B, OL:B, and BNS:B...

select(v.name).where(
    (v.x, [(FOAF.name, v.name),
           (FOAF.nick, ["Alice", "Alice_"])
           (FOAF.knows, Some((is_a, FOAF.Person),
                             (FOAF.mbox, URIRef('mailto:bob@example.org'))))
           ])
)

Using POL:A.1/C.2, OL:B, and BNS:B...

select(v.name).where(
    Subject(v.x)[FOAF.name: v.name,
                 FOAF.nick: ["Alice", "Alice_"],
                 FOAF.knows: Some[is_a: FOAF.Person,
                                  FOAF.mbox: URIRef('mailto:bob@example.org')]]
)

Add more here...

Updated