Sha256: dee95e1245d019f1555c98202ddcea00245c009d701ca9a18f50c9c1040c9a4d

Contents?: true

Size: 1.49 KB

Versions: 5

Compression:

Stored size: 1.49 KB

Contents

module SPARQL; module Algebra
  class Operator
    ##
    # The SPARQL `datatype` operator.
    #
    # @example
    #   (prefix ((xsd: <http://www.w3.org/2001/XMLSchema#>)
    #            (rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
    #            (: <http://example.org/>))
    #     (project (?s)
    #       (filter (= (datatype (xsd:double ?v)) xsd:double)
    #         (bgp (triple ?s :p ?v)))))
    #
    # @see http://www.w3.org/TR/rdf-sparql-query/#func-datatype
    class Datatype < Operator::Unary
      include Evaluatable

      NAME = :datatype

      ##
      # Returns the datatype IRI of the operand.
      #
      # If the operand is a simple literal, returns a datatype of
      # `xsd:string`.
      #
      # @param  [RDF::Literal] literal
      #   a typed or simple literal
      # @return [RDF::URI] the datatype IRI, or `xsd:string` for simple literals
      # @raise  [TypeError] if the operand is not a typed or simple literal
      def apply(literal)
        case literal
          when RDF::Literal then case
            when RDF::VERSION.to_s >= "1.1" then literal.datatype
            when literal.simple? then RDF::XSD.string
            when literal.datatype == RDF::XSD.string then RDF::XSD.string
            when literal.plain? then RDF.langString
            else RDF::URI(literal.datatype)
          end
          else raise TypeError, "expected an RDF::Literal, but got #{literal.inspect}"
        end
      end
    end # Datatype
  end # Operator
end; end # SPARQL::Algebra

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sparql-1.1.1 lib/sparql/algebra/operator/datatype.rb
sparql-1.1.0 lib/sparql/algebra/operator/datatype.rb
sparql-1.1.0p0 lib/sparql/algebra/operator/datatype.rb
sparql-1.0.8 lib/sparql/algebra/operator/datatype.rb
sparql-1.0.7 lib/sparql/algebra/operator/datatype.rb