Skip to content

Commit

Permalink
Add to_sxp extensions from SPARQL.
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Dec 16, 2021
1 parent f9b73b6 commit 57861f5
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 98 deletions.
111 changes: 71 additions & 40 deletions lib/sxp/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class Object
##
# Returns the SXP representation of this object.
#
# @param [Hash] prefixes(nil)
# @param [String] base_uri(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
to_s.to_json
Expand All @@ -22,8 +22,8 @@ class NilClass
##
# Returns the SXP representation of this object.
#
# @param [Hash] prefixes(nil)
# @param [String] base_uri(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
'#n'
Expand All @@ -36,8 +36,8 @@ class FalseClass
##
# Returns the SXP representation of this object.
#
# @param [Hash] prefixes(nil)
# @param [String] base_uri(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
'#f'
Expand All @@ -50,8 +50,8 @@ class TrueClass
##
# Returns the SXP representation of this object.
#
# @param [Hash] prefixes(nil)
# @param [String] base_uri(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
'#t'
Expand All @@ -64,8 +64,8 @@ class String
##
# Returns the SXP representation of this object.
#
# @param [Hash] prefixes(nil)
# @param [String] base_uri(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
inspect
Expand All @@ -78,8 +78,8 @@ class Symbol
##
# Returns the SXP representation of this object.
#
# @param [Hash] prefixes(nil)
# @param [String] base_uri(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
to_s
Expand All @@ -100,8 +100,8 @@ class Integer
##
# Returns the SXP representation of this object.
#
# @param [Hash] prefixes(nil)
# @param [String] base_uri(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
to_s
Expand All @@ -114,8 +114,8 @@ class BigDecimal
##
# Returns the SXP representation of this object.
#
# @param [Hash] prefixes(nil)
# @param [String] base_uri(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
to_f.to_s
Expand All @@ -128,8 +128,8 @@ class Float
##
# Returns the SXP representation of this object.
#
# @param [Hash] prefixes(nil)
# @param [String] base_uri(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
case
Expand All @@ -152,22 +152,36 @@ class Array
#
# Prefixes always are terminated by a ':'
#
# @param [Hash] prefixes(nil)
# @param [String] base_uri(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
'(' << map { |x| x.to_sxp(prefixes: prefixes, base_uri: base_uri) }.join(' ') << ')'
end
end

##
# Extensions for Ruby's `Hash` class.
class Hash
##
# Returns the SXP representation of this object.
#
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
to_a.to_sxp(prefixes: prefixes, base_uri: base_uri)
end
end

##
# Extensions for Ruby's `Time` class.
class Time
##
# Returns the SXP representation of this object.
#
# @param [Hash] prefixes(nil)
# @param [String] base_uri(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
'#@' << (respond_to?(:xmlschema) ? xmlschema : to_i).to_s
Expand All @@ -180,8 +194,8 @@ class Regexp
##
# Returns the SXP representation of this object.
#
# @param [Hash] prefixes(nil)
# @param [String] base_uri(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
'#' << inspect
Expand All @@ -204,8 +218,8 @@ class Array
#
# Prefixes always are terminated by a ':'
#
# @param [Hash] prefixes(nil)
# @param [String] base_uri(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
if self.first == :base && self.length == 3 && self[1].is_a?(RDF::URI)
Expand All @@ -228,8 +242,8 @@ class RDF::URI
##
# Returns the SXP representation of this a URI. Uses Lexical representation, if set, otherwise, any PName match, otherwise, the relativized version of the URI if a base_uri is given, otherwise just the URI.
#
# @param [Hash] prefixes(nil)
# @param [String] base_uri(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
return lexical if lexical
Expand All @@ -248,8 +262,8 @@ class RDF::Node
##
# Returns the SXP representation of this object.
#
# @param [Hash] prefixes(nil)
# @param [String] base_uri(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
to_s
Expand All @@ -260,30 +274,46 @@ class RDF::Literal
##
# Returns the SXP representation of a Literal.
#
# @param [Hash] prefixes(nil)
# @param [String] base_uri(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
case datatype
when RDF::XSD.boolean, RDF::XSD.integer, RDF::XSD.double, RDF::XSD.decimal, RDF::XSD.time
# Retain stated lexical form if possible
valid? ? to_s : object.to_sxp
valid? ? to_s : object.to_sxp(prefixes: nil, base_uri: nil)
else
text = value.dump
text << "@#{language}" if self.has_language?
text << "^^#{datatype.to_sxp(prefixes: prefixes, base_uri: base_uri)}" if self.has_datatype?
text
end
end

class Double
##
# Returns the SXP representation of this object.
#
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
case
when nan? then 'nan.0'
when infinite? then (infinite? > 0 ? '+inf.0' : '-inf.0')
else canonicalize.to_s.downcase
end
end
end
end

class RDF::Query
# Transform Query into an Array form of an SXP
#
# If Query is named, it's treated as a GroupGraphPattern, otherwise, a BGP
#
# @param [Hash] prefixes(nil)
# @param [String] base_uri(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [Array]
def to_sxp(prefixes: nil, base_uri: nil)
res = [:bgp] + patterns
Expand All @@ -294,8 +324,8 @@ def to_sxp(prefixes: nil, base_uri: nil)
class RDF::Query::Pattern
# Transform Query Pattern into an SXP
#
# @param [Hash] prefixes(nil)
# @param [String] base_uri(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
[:triple, subject, predicate, object].to_sxp(prefixes: prefixes, base_uri: base_uri)
Expand All @@ -306,11 +336,12 @@ class RDF::Query::Variable
##
# Transform Query variable into an SXP.
#
# @param [Hash] prefixes(nil)
# @param [String] base_uri(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)
# @return [String]
def to_sxp(prefixes: nil, base_uri: nil)
to_s
prefix = distinguished? ? (existential? ? '$' : '?') : (existential? ? '$$' : '??')
unbound? ? "#{prefix}#{name}".to_sym.to_sxp : ["#{prefix}#{name}".to_sym, value].to_sxp
end
end
rescue LoadError
Expand Down
4 changes: 2 additions & 2 deletions lib/sxp/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class Block
##
# @param [Object] obj
# @param [Integer] indent
# @param [Hash] prefixes(nil)
# @param [String] base_uri(nil)
# @param [Hash{Symbol => RDF::URI}] prefixes(nil)
# @param [RDF::URI] base_uri(nil)l)
def initialize(obj, indent, prefixes: nil, base_uri: nil)
@indent = indent
@elements = []
Expand Down
24 changes: 14 additions & 10 deletions spec/common_lisp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,20 @@
# TODO
end

context "when reading integers in decimal form" do
it "reads `123` as an integer" do
expect(read(%q(123))).to eq 123
end
end

context "when reading integers in hexadecimal form" do
%w(#xFF #XFF #xff #XFF).each do |input|
it "reads `#{input}` as an integer" do
expect(read(input)).to eq 0xFF
context "when reading integers" do
{
'#b1010' => 0b1010,
'#B1010' => 0b1010,
'#o755' => 0755,
'#O755' => 0755,
'123' => 123,
'#xFF' => 0xFF,
'#XFF' => 0xFF,
'#xff' => 0xFF,
'#Xff' => 0xFF,
}.each_pair do |input, output|
it "reads #{input} as an integer" do
expect(read(input)).to eq output
end
end
end
Expand Down
18 changes: 14 additions & 4 deletions spec/extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
[['a', 2], '("a" 2)'],
[Time.parse("2011-03-13T11:22:33Z"), '#@2011-03-13T11:22:33Z'],
[/foo/, '#/foo/'],
[{a: 'b'}, %q(((a "b")))],
].each do |(value, result)|
it "returns #{result.inspect} for #{value.inspect}" do
expect(value.to_sxp).to eq result
Expand All @@ -30,10 +31,19 @@
end

describe "RDF::Literal#to_sxp" do
specify { expect(RDF::Literal.new("a").to_sxp).to eq %q("a")}
specify { expect(RDF::Literal.new("a", language: "en-us").to_sxp).to eq %q("a"@en-us)}
specify { expect(RDF::Literal.new("a", datatype: RDF::XSD.string).to_sxp).to eq %q("a")}
specify { expect(RDF::Literal.new("2013-11-21", datatype: RDF::XSD.date).to_sxp).to eq %q("2013-11-21"^^<http://www.w3.org/2001/XMLSchema#date>)}
{
RDF::Literal.new("a") => %q("a"),
RDF::Literal.new("a", language: "en-us") => %q("a"@en-us),
RDF::Literal.new("2013-11-21", datatype: RDF::XSD.date) => %q("2013-11-21"^^<http://www.w3.org/2001/XMLSchema#date>),
RDF::Literal(1) => %q(1),
RDF::Literal::Decimal.new(1.0) => '1.0',
RDF::Literal(1.0e1) => '1.0e1',
RDF::Literal(Float::INFINITY) => "+inf.0",
RDF::Literal(-Float::INFINITY) => "-inf.0",
RDF::Literal(Float::NAN) => "nan.0",
}.each_pair do |l, sxp|
specify {expect(l.to_sxp).to eq sxp}
end
end

describe "RDF::Literal#to_sxp with prefix" do
Expand Down
7 changes: 6 additions & 1 deletion spec/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,16 @@
(triple <a> <b> 123)
(triple <a> <b> -18)
(triple <a> <b> 123.0)
(triple <a> <b> 1.0)
(triple <a> <b> 1.0e0)
(triple <a> <b> "lex"^^<http://example.org/thing>)
(triple <a> <b> ?x))
}.gsub(/^ /, '')
],
"SPARQL base": [
[:base, RDF::URI("http://example.com/"), RDF::URI("http://example.com/a")],
%q{(base <http://example.com/> <a>)
}.gsub(/^ /, '')
],
"issue 20": [
[:prefix,
[[:"wdt:", RDF::URI("http://www.wikidata.org/prop/direct/")],
Expand Down
Loading

0 comments on commit 57861f5

Please sign in to comment.