[email protected]/doc/tutorial/ldscript.pdf · •shacl interpreter •json ld...

67
Programming the Semantic Web [email protected]

Upload: others

Post on 21-Jun-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Programming the Semantic Web

[email protected]

Page 2: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Programming the Semantic Web

• RDF: Semantic Web of Linked Data

• SPARQL: RDF Query Language

Focus: SPARQL extension functions

Programming the Semantic Web 2

Page 3: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

prefix fun: <http://ns.inria.fr/sparql-function/>

select * where {

?x us:income ?inc

filter (?inc >= fun:fac(10)) # = 3628800

}

function fun:fac(n) {if (n = 0, 1, n * fun:fac(n – 1))

}

Wish: No compiling, no linking: just write & run

Programming the Semantic Web 3

Page 4: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Requirement for Function

1. Call function in SPARQL query

– Call function whose name is result of expression

2. Execute SPARQL query in function

– Pass parameter to SPARL query

3. Function objects = RDF terms + triple & graph

4. SPARQL operators and functions available

– isBlank, isURI, isLiteral, regex, datatype, …

Programming the Semantic Web 4

Page 5: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Candidate: SPARQL Filter ?

• Constant: 2.718, true, "1930-01-29", ex:trust

• Datatype: xsd:string, xsd:integer, xsd:boolean

• Variable: ?x, ?y, ?z, ?t

• Expression: ?x + 1, ?x = (10 * ?y) / ?z, ?t >= 3.14

• Connector: ! (?x < 0 || ?y = "Keynes" ) && …

• Graph match: exists { ?x a us:ScriptLanguage }

• If then else: if (?deficit <= .03, true, true)

• Function call: regex(?uri, ".*cnrs"), us:fac(?n)

Programming the Semantic Web 5

Page 6: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Function Language: SPARQL Filter++

• Constant: 2.718, true, "1930-01-29"

• Datatype: xsd:string, xsd:integer, xsd:boolean

• Variable: ?x, ?y, ?z, ?t

• Expression: ?x + 1, ?x = (10 * ?y) / ?z, ?t >= 3.14

• Connector: ! (?x < 0 || ?y = "Keynes" ) && …

• Graph match: exists { ?x a us:ScriptLanguage }

• If then else: if (?deficit <= .03, true, true)

• Function call: regex(?uri, ".*cnrs"), us:fac(?n)

• Function: function us:fun(x) { 1 / (x * x) }

Programming the Semantic Web 6

Page 7: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Function Language: SPARQL Filter++

function {

}

Programming the Semantic Web 7

Page 8: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Function Language: SPARQL Filter++

function us:foo(x) {

}

Programming the Semantic Web 8

Page 9: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Function Language: SPARQL Filter++

function us:foo(x) {

us:bar (x * x)

}

Programming the Semantic Web 9

Page 10: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Function Language: SPARQL Filter++

function us:foo(x) {

us:bar (x * x)

}

function us:bar(y) {

1 / y

}Programming the Semantic Web 10

Page 11: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Function Language Statement

1. Function Definition2. SPARQL Filter expression3. Let, For4. SPARQL Query5. Pattern Matching6. Second Order Function7. Lambda Expression8. Linked Function9. Literal Extension Datatype10.SPARQL Extension

Programming the Semantic Web 11

Page 12: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Let, For

let (x = y + 1) {

us:foo(x)

}

for (elem in list) {

us:bar(elem)

}

Programming the Semantic Web 12

Page 13: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Query in Let

let (select ?x ?r where { ?x us:radius ?r } ) {

}

Programming the Semantic Web 13

Page 14: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Query in Let

let (select ?x ?r where { ?x us:radius ?r } ) {

return (3.1416 * ?r * ?r)

}

Programming the Semantic Web 14

Page 15: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Query in Let

let (select ?x ?r where { ?x us:radius ?r } ) {

return (3.1416 * ?r * ?r)

}

Programming the Semantic Web 15

Page 16: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

« Same » in Java

String q = "select ?x ?r where { ?x us:radius ?r }" ;

QueryProcess exec = QueryProcess.create(graph);

Mappings map = exec.query(q);

Datatype dt = map.get(0).getValue("r");

double val = 3.1416 * dt.doubleValue() * dt.doubleValue(); Datatype res = Datatype.create (val);

Programming the Semantic Web 16

Page 17: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Query in Let

function us:area(?x) {

let (select ?x ?r where { ?x us:radius ?r } ) {

return(3.1416 * ?r * ?r)

}

}

Programming the Semantic Web 17

Page 18: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Select Query in For

for (select * where { ?s ?p ?o } ) {

us:foo(s, p, o)

}

Programming the Semantic Web 18

Page 19: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Construct Query in For

for (atriple in construct where { ?x rdfs:label ?l } ) {

us:foo(atriple)

}

Programming the Semantic Web 19

Page 20: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Construct Query in For

for ((s p o) in construct where { ?x rdfs:label ?l } ) {

us:bar(s, p, o)

}

Programming the Semantic Web 20

Page 21: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Update in Function

function us:myfun() {query (insert data { });query (delete data { });query (delete { } insert { } where { })

}

Function update available in update query, not available in select query, because there is a read/write lock

Programming the Semantic Web 21

Page 22: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Pattern Matching

let ((first | rest . last) = list) {

}

Programming the Semantic Web 22

Page 23: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Pattern Matching

let ((first | rest . last) = @(1 2 3 4)) {

first = 1 ;

rest = (2 3) ;

last = 4

}

Programming the Semantic Web 23

Page 24: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Second Order Function

• funcall(exp, arg)

eval(exp) = function name or lambda expression

eval(exp)= ex:foo

funcall(exp, arg) = funcall(ex:foo, arg) = ex:foo(arg)

Programming the Semantic Web 24

Page 25: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Second Order Function

• funcall(exp, arg)

let (select * where { ?x us:function ?name } ) {

funcall(?name, ?x)

}

Programming the Semantic Web 25

Page 26: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Second Order Function

• funcall(exp, arg)

• apply(exp, argList) apply(rq:mult, xt:list(2, 3))

• map(exp, list) map(xt:print, list)

– map, maplist, mapany, mapevery

• reduce(exp, list) reduce(rq:plus, list)

• reduce(rq:plus, maplist(ex:square, xt:iota(10)))

Programming the Semantic Web 26

Page 27: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Lambda Expression

• Anonymous function

lambda(x, y) { x + y }

Programming the Semantic Web 27

Page 28: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Lambda Expression

• Anonymous function

maplist (lambda(x) { x * x } , list)

let (fun = lambda(y) { 1 / (y * y) } ) {

funcall(fun, 10)

}

Programming the Semantic Web 28

Page 29: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Extension Datatype

• dt:graph RDF Graph

• dt:triple RDF Triple

• dt:mappings Query Solution Sequence

• dt:mapping Query Solution

• dt:list, dt:map

• dt:xml, dt:json

Programming the Semantic Web 29

Page 30: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Graph Datatype

let (g = xt:graph()) {

datatype(g) = dt:graph ;

for (t in g) {

datatype(t) = dt:triple

}

}

Programming the Semantic Web 30

Page 31: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Mapping Datatype

let (sol = select * where { } ) {

datatype(sol) = dt:mappings ;

for (res in sol) {

datatype(res) = dt:mapping

}

}

Programming the Semantic Web 31

Page 32: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

XML Datatype

let (obj = "<book><title>1984</title></book>" ,

xml = xt:xml(obj),

list = xpath(xml, "/book/title")) {

dom:getTextContent(xt:first(list))

}

Programming the Semantic Web 32

Page 33: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

XML DOM

• dom:getChildNodes()

• dom:getTextContent()

• dom:getElementsbyTagName()

• …

Programming the Semantic Web 33

Page 34: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

JSON

let (obj = xt:read(<http://example.org/myservice>),

json = xt:json(obj)) {

xt:get(json, "slotname");

for ((key val) in json) {

xt:print(key, val)

}

}

Programming the Semantic Web 34

Page 35: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

List

• xt:list()

• xt:get(list, n)

• xt:first(list)

• xt:rest(list)

• xt:sort(list)

• xt:reverse(list)

• xt:append(l1, l2)

• …

Programming the Semantic Web 35

Page 36: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

SPARQL Extension

Programming the Semantic Web 36

Page 37: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

SPARQL Function Statement

select * where {

?x ex:function ?name

filter funcall(?name, ?x)

}

Programming the Semantic Web 37

Page 38: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

SPARQL List aggregate

select (aggregate(?y) as ?list)where { ?x foaf:knows ?y }

Programming the Semantic Web 38

Page 39: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

SPARQL Custom aggregate

select (aggregate(?y) as ?list)

(xt:sort(?list) as ?sort)where { ?x foaf:knows ?y }

Programming the Semantic Web 39

Page 40: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

SPARQL Custom aggregate

select (aggregate(?z) as ?list)

(ag:median(?list) as ?med) where { … }

function ag:median(?list) {

xt:get(xt:sort(?list), xt:size(?list) / 2)}

Programming the Semantic Web 40

Page 41: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

SPARQL Values

values ?x { 1 2 3 }

values var { unnest(exp) }

values (?s ?p ?o) { unnest(xt:load(URI)) }

Programming the Semantic Web 41

Page 42: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Named Graph Overloading

let (g = construct where { ?x rdfs:label ?l },

select * where { graph ?g { ?s ?p ?o } } ) {

}

Programming the Semantic Web 42

Page 43: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Event Driven Programming

• Associate events to SPARQL query processing

• Associate functions to events

• When event occurs, call the function

Programming the Semantic Web 43

Page 44: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Event Driven Programming

• Event : before, after, …

@before

function us:before(query) {

xt:print("before:", query)

}

Programming the Semantic Web 44

Page 45: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Event Driven Overloading

• Overload operator for extension datatype

@type us:distance

function us:eq(x, y) {

us:convert(x) = us:convert(y)

}

x = "100 km"^^us:distance

Programming the Semantic Web 45

Page 46: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Event Update

@update

function us:myupdate(deleteList, insertList) {

for ((s p o) in deleteList) {

xt:print("delete:", s, p, o)

}

}

Programming the Semantic Web 47

Page 47: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Event Update

@update

function us:myupdate(deleteList, insertList) {

query(

insert { ?s ?p ?o}

where {

values ?deleteList { undef }

values (?s ?p ?o) { unnest(deleteList) }

})

}Programming the Semantic Web 48

Page 48: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Event Processing

@event

select where {}

Programming the Semantic Web 49

Page 49: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

@init function us:init(dt:query ?q)

@before function us:before(dt:query ?q)

@after function us:after(dt:mappings ?map)

@start function us:start(dt:query ?q)

@finish function us:finish(dt:mappings ?map)

@statement function us:statement(URI ?g, dt:statement ?e)

@function function us:function(dt:expression ?call, dt:expression ?fun)

@produce function us:produce(URI ?g, dt:triple ?q)

@candidate function us:candidate(URI ?g, dt:triple ?q, dt:triple ?t)

@result function us:result(dt:mappings ?map, dt:mapping ?m)

@distinct function us:key(dt:mapping ?m)

@orderby function us:compare(dt:mapping ?m1, dt:mapping ?m2)

@limit function us:limit(dt:mappings ?map)

@timeout function us:timeout(URI ?serv)

@slice function us:slice(URI ?serv, dt:mappings ?map)

@join function us:join (URI ?g, dt:statement ?e, dt:mappings ?m1, dt:mappings ?m2)

@minus function us:minus (URI ?g, dt:statement ?e, dt:mappings ?m1, dt:mappings ?m2)

@union function us:union (URI ?g, dt:statement ?e, dt:mappings ?m1, dt:mappings ?m2)

@optional function us:optional(URI ?g, dt:statement ?e, dt:mappings ?m1, dt:mappings ?m2)

@bgp function us:bgp (URI ?g, dt:statement ?e, dt:mappings ?m)

@graph function us:graph (URI ?g, dt:statement ?e, dt:mappings ?m)

@service function us:service(URI ?s, dt:statement ?e, dt:mappings ?m)

@query function us:query (URI ?g, dt:statement ?e, dt:mappings ?m)

@values function us:values (URI ?g, dt:statement ?e, dt:mappings ?m)

@path function us:path(URI ?g, dt:triple ?q, dt:path ?p, term ?s, term ?o)

@step function us:step(URI ?g, dt:triple ?q, dt:path ?p, term ?s, term ?o)

@select function us:select(dt:expression ?e, term ?v)

@aggregate function us:agg(dt:expression ?e, term ?v)

@bind function us:bind(URI ?g, dt:expression ?e, term ?v)

@filter function us:filter(URI ?g, dt:expression ?e, xsd:boolean ?b)

@having function us:having(dt:expression ?e, xsd:boolean ?b)

Programming the Semantic Web 50

Page 50: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Usage

select where {

}

function us:fun(x) {

}

Programming the Semantic Web 51

Page 51: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Usage

select where {

}

@public

function us:fun(x) {

}

Programming the Semantic Web 52

Page 52: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Usage

@import </home/user/me/myfun.rq>

select where {

}

Programming the Semantic Web 53

Page 53: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Usage

@public @import </home/user/me/myfun.rq>

select where {

}

Programming the Semantic Web 54

Page 54: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Usage: Java Function Call

QueryProcess exec = QueryProcess.create(graph);

exec.compile(fundef);

IDatatype res = exec.funcall(name, arg);

Programming the Semantic Web 55

Page 55: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Validation: Application

• SHACL Interpreter

• JSON LD Parser

• Turtle Pretty Printer

• Day of date

• Roman to decimal and converse

• Fibonacci, factorial, sort, standard deviation, etc.

• Extensively used with STTL on corese.inria.fr

Programming the Semantic Web 56

Page 56: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Example: RDF List to LDScript List

# bnode is start of RDF List

function sh:list(bnode) {

let (select ?bnode (aggregate(?e) as ?list)

where { ?bnode rdf:rest*/rdf:first ?e } ) {

return (list)

}

}

Programming the Semantic Web 57

Page 57: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Example: RDF List to LDScript List

function sh:reclist(bn) {let (select ?bn

(aggregate (if (?b, sh:reclist(?e), if (?e = rdf:nil, xt:list(), ?e))) as ?list)

where {?bn rdf:rest*/rdf:first ?ebind (exists { ?e rdf:rest ?a } as ?b) } ) {

return (list)}

}

Programming the Semantic Web 58

Page 58: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Example: SHACL

• Validate graph with SHACL

select (sh:conform(?g) as ?b) (xt:turtle(?g) as ?t)

where {

bind (sh:shacl() as ?g)

}

Programming the Semantic Web 59

Page 59: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

SHACL Example

us:test a sh:NodeShape ;

sh:targetClass foaf:Person ;

sh:property [

sh:path foaf:knows ;

sh:minCount 1;

sh:class foaf:Person

]

Programming the Semantic Web 60

Page 60: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

SHACL Extension: Function

sh:property [

sh:path foaf:knows ;

xsh:function [ us:test (foaf:age 62) ]

]

function us:test(source, node, param) {

let ((pred val) = param) {

xt:value(node, pred) >= val

}

} Programming the Semantic Web 61

Page 61: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

SHACL Extension: Function

xsh:function [ us:test ( [ sh:class foaf:Person ] ) ]

function us:test(source, node, param) {

let ((shape) = param) {

sh:eval(shape, node)

}

}

Programming the Semantic Web 62

Page 62: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

SHACL Extension: Function

sh:path [xsh:predicatePath xsh:subject] ;

function xsh:predicatePath(focus, source, node, exp) {

let (list = xsh:triplePath(focus, source, node, exp)) {

xt:merge(maplist(xt:predicate, list))

}

}

Programming the Semantic Web 63

Page 63: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

SHACL Extension: Expression

xsh:evaluate (rq:gt us:length (rq:mult 2 us:width))

Programming the Semantic Web 64

Page 64: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

SHACL: Validation Report

Programming the Semantic Web 65

Page 65: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

SHACL: RDF Graph Pretty Print

Programming the Semantic Web 66

Page 66: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Conclusion

• SPARQL Function Language• Extension of Filter Language• SPARQL -> Function -> SPARQL• SPARQL Extension• « Java Ultra Lite » where objects are RDF terms

• SHACL interpreter written with SPARQL Function• Corese Semantic Web Factory

– http://project.inria.fr/corese– http://ns.inria.fr/sparql-extension

• Implementation of Core with Jena, Mines St Etienne

Programming the Semantic Web 68

Page 67: olivier.corby@inriawimmics.inria.fr/doc/tutorial/ldscript.pdf · •SHACL Interpreter •JSON LD Parser •Turtle Pretty Printer •Day of date •Roman to decimal and converse •Fibonacci,

Perspective

• Type system, Type checking

• Structured Editor

• Second implementation with Jena

• W3C Member submission

Programming the Semantic Web 69