Class: Reddy::Triple

Inherits:
Object
  • Object
show all
Defined in:
lib/reddy/triple.rb

Overview

An RDF Triple, or statement.

Statements are composed of subjects, predicates and objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • subject:: (URIRef, BNode)

    the subject of the triple

  • predicate:: (URIRef)

    the predicate of the triple

  • object:: (URIRef, BNode, Literal, TypedLiteral)

    the object of the triple

Raises:

  • (Error)

    Checks parameter types and raises if they are incorrect.

Author:

  • Tom Morris



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

#objectObject

Returns the value of attribute object.



6
7
8
# File 'lib/reddy/triple.rb', line 6

def object
  @object
end

#predicateObject

Returns the value of attribute predicate.



6
7
8
# File 'lib/reddy/triple.rb', line 6

def predicate
  @predicate
end

#subjectObject

Returns the value of attribute subject.



6
7
8
# File 'lib/reddy/triple.rb', line 6

def subject
  @subject
end

Instance Method Details

#cloneObject

Clone triple, keeping references to literals and URIRefs, but cloning BNodes

Raises:



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

Returns:

  • (Boolean)


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

#hashObject

For indexes



73
74
75
# File 'lib/reddy/triple.rb', line 73

def hash
  [subject, predicate, object].hash
end

#inspectObject



43
44
45
# File 'lib/reddy/triple.rb', line 43

def inspect
  [@subject, @predicate, @object, @patern].inspect
end

#is_patern?Boolean

Returns:

  • (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?

Returns:

  • (Boolean)


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_ntriplesObject

Serialize Triple to N-Triples

Raises:



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_sObject



41
# File 'lib/reddy/triple.rb', line 41

def to_s; self.to_ntriples; end