Class: Reddy::Triple
- Inherits:
-
Object
- Object
- Reddy::Triple
- Defined in:
- lib/reddy/triple.rb
Overview
An RDF Triple, or statement.
Statements are composed of subjects, predicates and objects.
Instance Attribute Summary collapse
-
#object ⇒ Object
Returns the value of attribute object.
-
#predicate ⇒ Object
Returns the value of attribute predicate.
-
#subject ⇒ Object
Returns the value of attribute subject.
Instance Method Summary collapse
-
#clone ⇒ Object
Clone triple, keeping references to literals and URIRefs, but cloning BNodes.
-
#eql?(other) ⇒ Boolean
(also: #==)
Two triples are equal if their of their subjects, predicates and objects are equal.
-
#hash ⇒ Object
For indexes.
-
#initialize(subject, predicate, object) ⇒ Triple
constructor
Creates a new triple directly from the intended subject, predicate, and object.
- #inspect ⇒ Object
- #is_patern? ⇒ Boolean
-
#is_type? ⇒ Boolean
Is the predicate of this statment rdf:type?.
-
#to_ntriples ⇒ Object
Serialize Triple to N-Triples.
- #to_s ⇒ Object
Constructor Details
#initialize(subject, predicate, object) ⇒ Triple
Creates a new triple directly from the intended subject, predicate, and object.
Any or all of subject, predicate or object may be nil, to create a triple patern. A patern may not be added to a graph.
Example
Triple.new(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new) # => results in the creation of a new triple and returns it
24 25 26 27 28 29 |
# File 'lib/reddy/triple.rb', line 24 def initialize (subject, predicate, object) @subject = self.class.coerce_subject(subject) @predicate = self.class.coerce_predicate(predicate) @object = self.class.coerce_object(object) @patern = subject.nil? || predicate.nil? || object.nil? end |
Instance Attribute Details
#object ⇒ Object
Returns the value of attribute object.
6 7 8 |
# File 'lib/reddy/triple.rb', line 6 def object @object end |
#predicate ⇒ Object
Returns the value of attribute predicate.
6 7 8 |
# File 'lib/reddy/triple.rb', line 6 def predicate @predicate end |
#subject ⇒ Object
Returns the value of attribute subject.
6 7 8 |
# File 'lib/reddy/triple.rb', line 6 def subject @subject end |
Instance Method Details
#clone ⇒ Object
Clone triple, keeping references to literals and URIRefs, but cloning BNodes
64 65 66 67 68 69 70 |
# File 'lib/reddy/triple.rb', line 64 def clone raise RdfException.new("Can't clone patern triple") if is_patern? s = subject.is_a?(BNode) ? subject.clone : subject p = predicate.is_a?(BNode) ? predicate.clone : predicate o = object.is_a?(BNode) ? object.clone : object Triple.new(subject, predicate, object) end |
#eql?(other) ⇒ Boolean Also known as: ==
Two triples are equal if their of their subjects, predicates and objects are equal. Or self or other is a patern and subject, predicate, object matches
54 55 56 57 58 59 |
# File 'lib/reddy/triple.rb', line 54 def eql? (other) other.is_a?(Triple) && (other.subject == self.subject || other.subject.nil? || self.subject.nil?) && (other.predicate == self.predicate || other.predicate.nil? || self.predicate.nil?) && (other.object == self.object || other.object.nil? || self.object.nil?) end |
#hash ⇒ Object
For indexes
73 74 75 |
# File 'lib/reddy/triple.rb', line 73 def hash [subject, predicate, object].hash end |
#inspect ⇒ Object
43 44 45 |
# File 'lib/reddy/triple.rb', line 43 def inspect [@subject, @predicate, @object, @patern].inspect end |
#is_patern? ⇒ Boolean
31 32 33 |
# File 'lib/reddy/triple.rb', line 31 def is_patern? @patern end |
#is_type? ⇒ Boolean
Is the predicate of this statment rdf:type?
48 49 50 |
# File 'lib/reddy/triple.rb', line 48 def is_type? @predicate.to_s == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" end |
#to_ntriples ⇒ Object
Serialize Triple to N-Triples
36 37 38 39 |
# File 'lib/reddy/triple.rb', line 36 def to_ntriples raise RdfException.new("Can't serialize patern triple") if is_patern? @subject.to_ntriples + " " + @predicate.to_ntriples + " " + @object.to_ntriples + " ." end |
#to_s ⇒ Object
41 |
# File 'lib/reddy/triple.rb', line 41 def to_s; self.to_ntriples; end |