Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge commit 'b4dbc6f7f054f398bfd3ec2310d2efb21bddcb7f' into talia_re…

…merge

Conflicts:
	lib/active_rdf/queryengine/query.rb
	lib/active_rdf/queryengine/query2sparql.rb
  • Loading branch information...
commit 13996c54e67d3d113d1c80f3c8e39299f4e63290 2 parents ef86d5d + b4dbc6f
@averell23 averell23 authored
View
42 lib/active_rdf/queryengine/query.rb
@@ -15,10 +15,10 @@ class Query
# Creates a new query. You may pass a different class that is used for "resource"
# type objects instead of RDFS::Resource
- def initialize(resource_type = RDFS::Resource)
- @distinct = false
- @limits = nil
- @offsets = nil
+ def initialize(resource_type = RDFS::Resource)
+ @distinct = false
+ @limit = nil
+ @offset = nil
@select_clauses = []
@where_clauses = []
@sort_clauses = []
@@ -56,11 +56,8 @@ def clear_select
# Adds variables to select clause
def select *s
@select = true
- s.each do |e|
- @select_clauses << parametrise(e)
- end
# removing duplicate select clauses
- @select_clauses.uniq!
+ @select_clauses.concat(s).uniq!
self
end
@@ -86,9 +83,7 @@ def count *s
# Adds sort predicates
def sort *s
# add sort clauses without duplicates
- s.each { |clause| @sort_clauses << parametrise(clause) }
- @sort_clauses.uniq!
-
+ @sort_clauses.concat(s).uniq!
self
end
@@ -96,9 +91,7 @@ def sort *s
# NOTE: you have to use SPARQL syntax for variables, eg. regex(?s, 'abc')
def filter *s
# add filter clauses
- @filter_clauses << s
- @filter_clauses.uniq!
-
+ @filter_clauses.concat(s).uniq!
self
end
@@ -136,9 +129,7 @@ def lang variable, tag, exact=false
# adds reverse sorting predicates
def reverse_sort *s
# add sort clauses without duplicates
- s.each { |clause| @reverse_sort_clauses << parametrise(clause) }
- @reverse_sort_clauses.uniq!
-
+ @reverse_sort_clauses.concat(s).uniq!
self
end
@@ -178,7 +169,7 @@ def where s,p,o,c=nil
end
raise(ActiveRdfErrror, "Cannot add a where clause where o is a blank node") if(o.is_a?(RDFS::BNode))
- @where_clauses << [s,p,o,c].collect{|arg| parametrise(arg)}
+ @where_clauses << [s,p,o,c]
end
self
end
@@ -187,7 +178,6 @@ def where s,p,o,c=nil
# the constraint (e.g. keyword_where(:s,'eyal|benjamin')
def keyword_where s,o
@keyword = true
- s = parametrise(s)
if @keywords.include?(s)
@keywords[s] = @keywords[s] + ' ' + o
else
@@ -229,17 +219,5 @@ def to_sp
Query2SPARQL.translate(self)
end
- private
- def parametrise s
- case s
- when Symbol, RDFS::Resource, RDFS::Literal, Class, nil
- s
- else
- if(s.respond_to?(:uri))
- s
- else
- '"' + s.to_s + '"'
- end
- end
-end
+ # Parameterization removed. This should be handled by the adapter.
end
View
42 lib/active_rdf/queryengine/query2sparql.rb
@@ -10,7 +10,7 @@ def self.translate(query, engine=nil)
str = ""
if query.select?
distinct = query.distinct? ? "DISTINCT " : ""
- select_clauses = query.select_clauses.collect{|s| construct_clause(s)}
+ select_clauses = query.select_clauses.collect{|s| construct_clause(s)}
str << "SELECT #{distinct}#{select_clauses.join(' ')} "
str << "WHERE { #{where_clauses(query)} #{filter_clauses(query)}} "
@@ -24,7 +24,7 @@ def self.translate(query, engine=nil)
elsif query.ask?
str << "ASK { #{where_clauses(query)} } "
end
-
+
return str
end
@@ -81,25 +81,35 @@ def self.where_clauses(query)
end
end
- where_clauses = query.where_clauses.collect do |s,p,o,c|
+ where_clauses = query.where_clauses.collect do |s,p,o,c|
# does there where clause use a context ?
- if c.nil?
- [s,p,o].collect {|term| construct_clause(term) }.join(' ')
- else
- "GRAPH #{construct_clause(c)} { #{construct_clause(s)} #{construct_clause(p)} #{construct_clause(o)} }"
- end
- end
+ if c.nil?
+ [s,p,o].collect {|term| construct_clause(term) }.join(' ')
+ else
+ "GRAPH #{construct_clause(c)} { #{construct_clause(s)} #{construct_clause(p)} #{construct_clause(o)} }"
+ end
+ end
"#{where_clauses.join(' . ')} ."
end
- def self.construct_clause(term)
- if term.is_a?(Symbol)
- "?#{term}"
- else
- term.to_literal_s
+ def self.construct_clause(term)
+ case term
+ when Symbol
+ "?#{term}"
+ when RDFS::Resource
+ term.to_s # Resource.to_s adds necessary brackets to uri: <uri>
+ when RDFS::Literal
+ term.to_literal_s
+ when Class
+ raise ActiveRdfError, "class must inherit from RDFS::Resource" unless term.ancestors.include?(RDFS::Resource)
+ term.class_uri.to_s
+ when nil
+ nil
+ else
+ "\"#{term.to_s}\""
end
- end
+ end
def self.sparql_engine
sparql_adapters = ConnectionPool.read_adapters.select{|adp| adp.is_a? SparqlAdapter}
@@ -126,7 +136,7 @@ def self.keyword_predicate
raise ActiveRdfError, "default SPARQL does not support keyword queries, remove the keyword clause or specify the type of SPARQL engine used"
end
end
-
+
private_class_method :where_clauses, :construct_clause, :keyword_predicate, :sparql_engine
end
Please sign in to comment.
Something went wrong with that request. Please try again.