-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrif2mlrules.xqy
69 lines (56 loc) · 2.36 KB
/
rif2mlrules.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
declare default element namespace "http://www.w3.org/2007/rif#";
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), """^^xs:", fn:substring-after(data($a/@type), "#"))
else ()
};
let $constants := //sentence/Frame
(: No constraint support yet :)
(: 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 xs: <http://www.w3.org/2001/XMLSchema#> . ",
if($constants) then (
"# Constants ",
fn:string-join (
let $construct := string-join(for $x in $constants return local:unframe($x), " ")
return fn:concat (
"rule ""constants"" construct { ",
$construct, "} {} "
)
, " ")
) else (),
if($rules) then (
"# Rules ",
fn:string-join (
for $i at $p 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 (
"rule ""rule", fn:string($p), """ construct { ",
$construct, "} { ", $where, "} "
)
, " ")
) else ()
)