Class: RDF::Literal
- Inherits:
-
Object
- Object
- RDF::Literal
- Includes:
- Term
- Defined in:
- lib/rdf/model/literal.rb,
lib/rdf/model/literal/xml.rb,
lib/rdf/model/literal/date.rb,
lib/rdf/model/literal/time.rb,
lib/rdf/model/literal/token.rb,
lib/rdf/model/literal/double.rb,
lib/rdf/model/literal/boolean.rb,
lib/rdf/model/literal/decimal.rb,
lib/rdf/model/literal/integer.rb,
lib/rdf/model/literal/numeric.rb,
lib/rdf/model/literal/datetime.rb
Overview
An RDF literal.
Subclasses of Literal should define DATATYPE and GRAMMAR constants, which are used for identifying the appropriate class to use for a datatype URI and to perform lexical matching on the value.
Literal comparison with other Value instances call Value#type_error, which, returns false. Implementations wishing to have TypeError raised should mix-in TypeCheck. This is required for strict SPARQL conformance.
Specific typed literals may have behavior different from the default implementation. See the following defined sub-classes for specific documentation. Additional sub-classes may be defined, and will interoperate by defining ‘DATATYPE` and `GRAMMAR` constants, in addition other required overrides of RDF::Literal behavior.
Defined Under Namespace
Classes: Boolean, Date, DateTime, Decimal, Double, Integer, Numeric, Time, Token, XML
Constant Summary collapse
- TRUE =
RDF::Literal.new(true).freeze
- FALSE =
RDF::Literal.new(false).freeze
- ZERO =
RDF::Literal.new(0).freeze
Instance Attribute Summary collapse
-
#datatype ⇒ URI
The XML Schema datatype URI (optional).
-
#language ⇒ Symbol
The language tag (optional).
Class Method Summary collapse
-
.datatyped_class(uri) ⇒ Object
Return datatype class for uri, or nil if none is found.
- .new(value, options = {}) ⇒ Object
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #===)
Returns ‘true` if this literal is equivalent to `other` (with type check).
-
#anonymous? ⇒ Boolean
Returns ‘false`.
-
#canonicalize ⇒ RDF::Literal
Returns a copy of this literal converted into its canonical lexical representation.
-
#canonicalize! ⇒ RDF::Literal
Converts this literal into its canonical lexical representation.
-
#comperable_datatype?(other) ⇒ Boolean
Returns ‘true` if the literal has a datatype and the comparison should return false instead of raise a type error.
-
#eql?(other) ⇒ Boolean
Determins if ‘self` is the same term as `other`.
-
#has_datatype? ⇒ Boolean
(also: #datatype?, #typed?, #datatyped?)
Returns ‘true` if this is a datatyped literal.
-
#has_language? ⇒ Boolean
(also: #language?)
Returns ‘true` if this is a language-tagged literal.
-
#hash ⇒ Fixnum
Returns a hash code for this literal.
-
#initialize(value, options = {}) ⇒ Literal
constructor
A new instance of Literal.
-
#inspect ⇒ String
Returns a developer-friendly representation of ‘self`.
-
#invalid? ⇒ Boolean
Returns ‘true` if the value does not adhere to the defined grammar of the datatype.
-
#literal? ⇒ Boolean
Returns ‘true`.
- #object ⇒ Object
-
#plain? ⇒ Boolean
(also: #simple?)
Returns ‘true` if this is a plain literal.
-
#to_s ⇒ String
Returns the value as a string.
-
#valid? ⇒ Boolean
Returns ‘true` if the value adheres to the defined grammar of the datatype.
-
#validate! ⇒ RDF::Literal
(also: #validate)
Validates the value using #valid?, raising an error if the value is invalid.
-
#value ⇒ String
Returns the value as a string.
Methods included from Term
Methods included from Value
#graph?, #inspect!, #iri?, #node?, #resource?, #statement?, #to_ntriples, #to_quad, #to_rdf, #type_error, #uri?, #variable?
Constructor Details
#initialize(value, options = {}) ⇒ Literal
Returns a new instance of Literal.
139 140 141 142 143 144 145 146 |
# File 'lib/rdf/model/literal.rb', line 139 def initialize(value, = {}) @object = value @string = [:lexical] if [:lexical] @string = value if !defined?(@string) && value.is_a?(String) @language = [:language].to_s.to_sym if [:language] @datatype = RDF::URI([:datatype]) if [:datatype] @datatype ||= self.class.const_get(:DATATYPE) if self.class.const_defined?(:DATATYPE) end |
Instance Attribute Details
#datatype ⇒ URI
Returns The XML Schema datatype URI (optional).
133 134 135 |
# File 'lib/rdf/model/literal.rb', line 133 def datatype @datatype end |
#language ⇒ Symbol
Returns The language tag (optional).
130 131 132 |
# File 'lib/rdf/model/literal.rb', line 130 def language @language end |
Class Method Details
.datatyped_class(uri) ⇒ Object
Return datatype class for uri, or nil if none is found
93 94 95 |
# File 'lib/rdf/model/literal.rb', line 93 def self.datatyped_class(uri) @@subclasses.detect {|klass| klass.const_defined?(:DATATYPE) && klass.const_get(:DATATYPE) == uri} end |
.new(value, options = {}) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/rdf/model/literal.rb', line 99 def self.new(value, = {}) klass = case when !self.equal?(RDF::Literal) self # subclasses can be directly constructed without type dispatch when typed_literal = datatyped_class(RDF::URI([:datatype])) typed_literal else case value when ::TrueClass then RDF::Literal::Boolean when ::FalseClass then RDF::Literal::Boolean when ::Integer then RDF::Literal::Integer when ::Float then RDF::Literal::Double when ::BigDecimal then RDF::Literal::Decimal when ::DateTime then RDF::Literal::DateTime when ::Date then RDF::Literal::Date when ::Time then RDF::Literal::Time # FIXME: Ruby's Time class can represent datetimes as well when ::Symbol then RDF::Literal::Token else self end end literal = klass.allocate literal.send(:initialize, value, ) literal.validate! if [:validate] literal.canonicalize! if [:canonicalize] literal end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: ===
Returns ‘true` if this literal is equivalent to `other` (with type check).
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/rdf/model/literal.rb', line 213 def ==(other) case other when Literal case when self.eql?(other) true when self.has_language? && self.language.to_s.downcase == other.language.to_s.downcase # Literals with languages can compare if languages are identical self.value == other.value when (self.simple? || self.datatype == XSD.string) && (other.simple? || other.datatype == XSD.string) self.value == other.value when other.comperable_datatype?(self) || self.comperable_datatype?(other) # Comoparing plain with undefined datatypes does not generate an error, but returns false # From data-r2/expr-equal/eq-2-2. false else type_error("unable to determine whether #{self.inspect} and #{other.inspect} are equivalent") end when String self.plain? && self.value.eql?(other) else false end end |
#anonymous? ⇒ Boolean
Returns ‘false`.
174 175 176 |
# File 'lib/rdf/model/literal.rb', line 174 def anonymous? false end |
#canonicalize ⇒ RDF::Literal
Returns a copy of this literal converted into its canonical lexical representation.
Subclasses should override ‘#canonicalize!` as needed and appropriate, not this method.
341 342 343 |
# File 'lib/rdf/model/literal.rb', line 341 def canonicalize self.dup.canonicalize! end |
#canonicalize! ⇒ RDF::Literal
Converts this literal into its canonical lexical representation.
Subclasses should override this as needed and appropriate.
352 353 354 355 |
# File 'lib/rdf/model/literal.rb', line 352 def canonicalize! @language = @language.to_s.downcase.to_sym if @language self end |
#comperable_datatype?(other) ⇒ Boolean
Returns ‘true` if the literal has a datatype and the comparison should return false instead of raise a type error.
This behavior is intuited from SPARQL data-r2/expr-equal/eq-2-2
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/rdf/model/literal.rb', line 297 def comperable_datatype?(other) return false unless self.plain? || self.has_language? case other when RDF::Literal::Numeric, RDF::Literal::Boolean, RDF::Literal::Date, RDF::Literal::Time, RDF::Literal::DateTime # Invald types can be compared without raising a TypeError if literal has a language (open-eq-08) !other.valid? && self.has_language? else case other.datatype when XSD.string true when nil # A different language will not generate a type error other.has_language? else # An unknown datatype may not be used for comparison, unless it has a language? (open-eq-8) self.has_language? end end end |
#eql?(other) ⇒ Boolean
Determins if ‘self` is the same term as `other`.
194 195 196 197 198 199 200 |
# File 'lib/rdf/model/literal.rb', line 194 def eql?(other) self.equal?(other) || (self.class.eql?(other.class) && self.value.eql?(other.value) && self.language.to_s.downcase.eql?(other.language.to_s.downcase) && self.datatype.eql?(other.datatype)) end |
#has_datatype? ⇒ Boolean Also known as: datatype?, typed?, datatyped?
Returns ‘true` if this is a datatyped literal.
263 264 265 |
# File 'lib/rdf/model/literal.rb', line 263 def has_datatype? !datatype.nil? end |
#has_language? ⇒ Boolean Also known as: language?
Returns ‘true` if this is a language-tagged literal.
253 254 255 |
# File 'lib/rdf/model/literal.rb', line 253 def has_language? !language.nil? end |
#hash ⇒ Fixnum
Returns a hash code for this literal.
182 183 184 |
# File 'lib/rdf/model/literal.rb', line 182 def hash to_s.hash end |
#inspect ⇒ String
Returns a developer-friendly representation of ‘self`.
369 370 371 |
# File 'lib/rdf/model/literal.rb', line 369 def inspect sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, RDF::NTriples.serialize(self)) end |
#invalid? ⇒ Boolean
Returns ‘true` if the value does not adhere to the defined grammar of the datatype.
287 288 289 |
# File 'lib/rdf/model/literal.rb', line 287 def invalid? !valid? end |
#literal? ⇒ Boolean
Returns ‘true`.
166 167 168 |
# File 'lib/rdf/model/literal.rb', line 166 def literal? true end |
#object ⇒ Object
158 159 160 |
# File 'lib/rdf/model/literal.rb', line 158 def object defined?(@object) ? @object : value end |
#plain? ⇒ Boolean Also known as: simple?
Returns ‘true` if this is a plain literal.
243 244 245 |
# File 'lib/rdf/model/literal.rb', line 243 def plain? language.nil? && datatype.nil? end |
#to_s ⇒ String
Returns the value as a string.
361 362 363 |
# File 'lib/rdf/model/literal.rb', line 361 def to_s @object.to_s end |
#valid? ⇒ Boolean
Returns ‘true` if the value adheres to the defined grammar of the datatype.
276 277 278 279 |
# File 'lib/rdf/model/literal.rb', line 276 def valid? grammar = self.class.const_get(:GRAMMAR) rescue nil grammar.nil? || !!(value =~ grammar) end |
#validate! ⇒ RDF::Literal Also known as: validate
Validates the value using #valid?, raising an error if the value is invalid.
326 327 328 329 |
# File 'lib/rdf/model/literal.rb', line 326 def validate! raise ArgumentError, "#{to_s.inspect} is not a valid <#{datatype.to_s}> literal" if invalid? self end |
#value ⇒ String
Returns the value as a string.
152 153 154 |
# File 'lib/rdf/model/literal.rb', line 152 def value @string || to_s end |