-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrif2spin.xqy
98 lines (77 loc) · 3.2 KB
/
rif2spin.xqy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
declare default element namespace "http://www.w3.org/2007/rif#";
declare namespace saxon="http://saxon.sf.net/";
declare namespace xsd = "http://www.w3.org/2001/XMLSchema#";
declare option saxon:output "method=text";
declare variable $nl := "
"; (: Newline :)
declare variable $q3 := """""""" ; (: Triple quote (for quoting blocks) :)
declare function local:unframe ($f)
{ fn:string-join((
for $x in $f/object/(Var|Const) return local:parseatom($x) ,
for $x in $f/slot/(Var|Const) return local:parseatom($x),
' .
' ), " ") };
declare function local:parsecall ($f as node())
{
let $fun := data($f//content/Atom/op)
let $args := for $x in $f//content/Atom/args/(Var|Const) return local:parseatom($x)
(: Switch on supported functions here :)
return if ($fun = "NEQ") then fn:concat ("FILTER (", $args[1], " != ", $args[2], ")", " .
") (: NEQ is not equal :)
else if ($fun = "NEQ") then fn:concat ("FILTER (", $args[1], " = ", $args[2], ")", " .
") (: EQ is equal :)
else () (: That's all :)
};
declare function local:parseatom ($a as item())
{
if (fn:name($a) = "Var") then fn:concat ("?", xs:string ($a))
else if ($a/@type = "http://www.w3.org/2007/rif#iri") then fn:concat ("<", xs:string ($a), ">")
else if (fn:matches (xs:string($a/@type), "http...www.w3.org.2001.XMLSchema", "i"))
then fn:concat ("""", data($a), """^^xsd:", fn:substring-after(data($a/@type), "#"))
else ()
};
let $constraints := //sentence/Forall/formula/Implies/then/Atom/op[Const="http://www.w3.org/2007/rif#error"]/../../../../../..
let $rules := //sentence/Forall/formula/Implies/then/Frame/../../../../..
return (
"
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sp: <http://spinrdf.org/sp#> .
@prefix spin: <http://spinrdf.org/spin#> .
@prefix spl: <http://spinrdf.org/spl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
",
"# Constraints" ,
$nl, $nl,
if (empty ($constraints)) then () else ( "owl:Thing spin:constraint ",
fn:string-join (for $i in $constraints//Implies
let $where := string-join(( for $x in $i/if//Frame return local:unframe($x),
for $x in $i/if//formula/External return local:parsecall($x)
), " ")
return
fn:concat (
" [ rdf:type sp:Ask ; sp:text ", $q3,
"

ASK 
WHERE {", $where, "}
",
$q3, "^^xsd:string ] ", $nl ), ","),
".", $nl),
$nl,
"# Rules" ,
$nl, $nl,
if (empty ($rules)) then () else ("owl:Thing spin:rule ",
fn:string-join (for $i in $rules//Implies
let $where := string-join( (
for $x in $i/if//Frame return local:unframe($x),
for $x in $i/if//formula/External return local:parsecall($x) )
, " ")
let $construct := string-join(for $x in $i/then//Frame return local:unframe($x), " ")
return
fn:concat (
" [ rdf:type sp:Construct ; sp:text ", $q3,
"

CONSTRUCT {", $construct, "} 
WHERE {", $where, "}
",
$q3, "^^xsd:string ] ", $nl ), ","),
"."), $nl
,
"<http://example.org/BE.spin.ttl>
rdf:type owl:Ontology ;
owl:imports <http://spinrdf.org/spin>
.
"
)