-
Notifications
You must be signed in to change notification settings - Fork 1
/
transform.rb
62 lines (47 loc) · 1.55 KB
/
transform.rb
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
require 'sparql'
class SPARQLTransform
attr_accessor sparql
def initialize(args = {sparql: ""})
@sparql = args.fetch(:sparql)
end
def transform
parsed = SPARQL.parse(@query) # this is a nightmare method, that returns a wide variety of things! LOL!
select = false
distinct = false
vars = ""
prefixes = Array.new
rdf_query=''
if parsed.is_a?(RDF::Query) # we need to get the RDF:Query object out of the list of things returned from the parse
rdf_query = parsed
else
parsed.each do |c|
rdf_query = c if c.is_a?(RDF::Query)
select = true if c.is_a? SPARQL::Algebra::Operator::Project
distinct = true if c.is_a? SPARQL::Algebra::Operator::Project
vars += " #{c.to_s}" if c.is_a? RDF::Query::Variable
next if c.is_a? Array and c.first.is_a? RDF::Query::Variable
prefixes << c if (c.is_a? Array and !(c.first.is_a? Array))
end
end
qs = ""
prefixes.each {|e| qs += "PREFIX #{e[0].to_s} <#{e[1].to_s}>\n"}
if select
qs += "SELECT "
else
qs += "SELECT *"
end
qs += "DISTINCT " if distinct
qs += vars
qs += " WHERE { \n"
patterns = rdf_query.patterns # returns the triple patterns in the query
patterns.each do |pattern|
pat = RDF::Query::Pattern.new(pattern.subject, pattern.predicate, pattern.object, {optional: true}).to_s
if pat =~ /^OPTIONAL(.*)/
pat = "OPTIONAL {#{$1}}"
end
qs += "#{pat}\n"
end
qs += "}"
puts qs
end
end