-
Notifications
You must be signed in to change notification settings - Fork 3
/
bitmask.sparql
33 lines (26 loc) · 917 Bytes
/
bitmask.sparql
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
# Exploit named graphs to model associative arrays (i.e. Maps) in RDF
INSERT DATA {
GRAPH eg:modeOfTransportation {
eg:Bike eg:hasBit 1 .
eg:Car eg:hasBit 2 .
eg:Train eg:hasBit 4 .
eg:Airplane eg:hasBit 8 .
}
}
CONSTRUCT {
eg:s eg:usesMode ?o
} WHERE {
BIND(10 AS ?bitmask)
# Convert the integer to
# (1) a binary string representation, (2) split the string by each character into a JSON array
# (3) reverse the order
BIND(json:reverse(json:split(json:binaryString(?bitmask), "(?!$)")) AS ?bitarr)
# Unnest each charactor ("0" or "1") with its array index
?bitarr json:unnest (?bitstr ?bitindex)
# Filter out zero bits
FILTER(?bitstr != "0")
# Obtain the bit's corresponding decimal value (using Jena's power function)
BIND(math:pow(2, ?bitindex) AS ?val)
# Perform the lookup
OPTIONAL { GRAPH eg:modeOfTransportation { ?o eg:hasBit ?val } }
}