From 89a8f665f70644989adf70cd132bf0f38ef3414d Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Tue, 28 Dec 2021 16:54:30 +0100 Subject: [PATCH] fix #4 ; add regex filter --- lib/goo/base/filter.rb | 22 +++++++++++++--------- lib/goo/sparql/queries.rb | 25 ++++++++++++++++--------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/lib/goo/base/filter.rb b/lib/goo/base/filter.rb index 66f2095d..50fa58ec 100644 --- a/lib/goo/base/filter.rb +++ b/lib/goo/base/filter.rb @@ -11,50 +11,54 @@ def initialize(pattern) end def >(value) - @filter_tree << FILTER_TUPLE.new(:>,value) + @filter_tree << FILTER_TUPLE.new(:>, value) self end def <(value) - @filter_tree << FILTER_TUPLE.new(:<,value) + @filter_tree << FILTER_TUPLE.new(:<, value) self end def <=(value) - @filter_tree << FILTER_TUPLE.new(:<=,value) + @filter_tree << FILTER_TUPLE.new(:<=, value) self end def >=(value) - @filter_tree << FILTER_TUPLE.new(:>=,value) + @filter_tree << FILTER_TUPLE.new(:>=, value) self end def or(value) - @filter_tree << FILTER_TUPLE.new(:or,value) + @filter_tree << FILTER_TUPLE.new(:or, value) self end def ==(value) - @filter_tree << FILTER_TUPLE.new(:==,value) + @filter_tree << FILTER_TUPLE.new(:==, value) self end def and(value) - @filter_tree << FILTER_TUPLE.new(:and,value) + @filter_tree << FILTER_TUPLE.new(:and, value) self end def unbound - @filter_tree << FILTER_TUPLE.new(:unbound,nil) + @filter_tree << FILTER_TUPLE.new(:unbound, nil) self end def bound - @filter_tree << FILTER_TUPLE.new(:bound,nil) + @filter_tree << FILTER_TUPLE.new(:bound, nil) self end + def regex(value) + @filter_tree << FILTER_TUPLE.new(:regex, value) + self + end end end end diff --git a/lib/goo/sparql/queries.rb b/lib/goo/sparql/queries.rb index 8ed102dd..7cc04642 100644 --- a/lib/goo/sparql/queries.rb +++ b/lib/goo/sparql/queries.rb @@ -131,22 +131,29 @@ def self.query_filter_sparql(klass,filter,filter_patterns,filter_graphs, end filter_var = inspected_patterns[filter_pattern_match] if !filter_operation.value.instance_of?(Goo::Filter) - unless filter_operation.operator == :unbound || filter_operation.operator == :bound + case filter_operation.operator + when :unbound + filter_operations << "!BOUND(?#{filter_var.to_s})" + return :optional + + when :bound + filter_operations << "BOUND(?#{filter_var.to_s})" + return :optional + when :regex + if filter_operation.value.is_a?(String) + filter_operations << "REGEX(?#{filter_var.to_s} , \"#{filter_operation.value.to_s}\")" + end + + else value = RDF::Literal.new(filter_operation.value) if filter_operation.value.is_a? String value = RDF::Literal.new(filter_operation.value, :datatype => RDF::XSD.string) end filter_operations << ( "?#{filter_var.to_s} #{sparql_op_string(filter_operation.operator)} " + - " #{value.to_ntriples}") - else - if filter_operation.operator == :unbound - filter_operations << "!BOUND(?#{filter_var.to_s})" - else - filter_operations << "BOUND(?#{filter_var.to_s})" - end - return :optional + " #{value.to_ntriples}") end + else filter_operations << "#{sparql_op_string(filter_operation.operator)}" query_filter_sparql(klass,filter_operation.value,filter_patterns,