Class: RdfContext::Triple

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

Overview

An RDF Triple, or statement.

Statements are composed of subjects, predicates and objects.

Instance Attribute Summary collapse

Class Method 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 pattern. A pattern may not be added to a graph.

Examples:

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:

Raises:

  • (Error)

    Checks parameter types and raises if they are incorrect.

Author:

  • Tom Morris



26
27
28
29
30
31
# File 'lib/rdf_context/triple.rb', line 26

def initialize (subject, predicate, object)
  @subject   = self.class.coerce_node(subject)
  @predicate = self.class.coerce_predicate(predicate)
  @object    = self.class.coerce_node(object)
  @pattern = subject.nil? || predicate.nil? || object.nil?
end

Instance Attribute Details

#objectObject

Returns the value of attribute object.



8
9
10
# File 'lib/rdf_context/triple.rb', line 8

def object
  @object
end

#predicateObject

Returns the value of attribute predicate.



8
9
10
# File 'lib/rdf_context/triple.rb', line 8

def predicate
  @predicate
end

#subjectObject

Returns the value of attribute subject.



8
9
10
# File 'lib/rdf_context/triple.rb', line 8

def subject
  @subject
end

Class Method Details

.coerce_node(node) ⇒ Object (protected)

Coerce a node (subject or object) to the appropriate RdfContext type.

@param[URI, URIRef, String, Integer, Float, BNode, Literal] object

If a String looks like a URI, a URI is created, otherwise an untyped Literal.

@raise

If node can’t be predicate.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rdf_context/triple.rb', line 130

def self.coerce_node(node)
  case node
  when Addressable::URI
    URIRef.new(node.to_s)
  when String
    if node.to_s =~ /^\w+:\/\/\S+/ # does it smell like a URI?
      URIRef.new(node.to_s)
    else
      Literal.untyped(node)
    end
  when Numeric, Date, Duration, Time, DateTime
    Literal.build_from(node)
  when URIRef, BNode, Literal, Graph, nil
    node
  else
    raise InvalidNode, "#{node.class}: #{node.inspect} is not a valid node"
  end
end

.coerce_predicate(predicate) ⇒ Object (protected)

Coerce a predicate to the appropriate RdfContext type.

@param[URI, URIRef, String] predicate

If a String looks like a URI, a URI is created

@raise

If predicate can’t be predicate.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rdf_context/triple.rb', line 109

def self.coerce_predicate(predicate)
  case predicate
  when Addressable::URI
    URIRef.new(predicate.to_s)
  when URIRef, BNode
    predicate
  when String
    URIRef.new predicate
  when nil
    predicate
  else
    raise InvalidPredicate, "Predicate should be a URI"
  end
rescue ParserException => e
  raise InvalidPredicate, "#{predicate.class}: #{predicate.inspect} is not a valid predicate"
end

Instance Method Details

#cloneTriple

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

Returns:

Raises:



81
82
83
84
85
86
87
# File 'lib/rdf_context/triple.rb', line 81

def clone
  raise RdfException.new("Can't clone pattern triple") if is_pattern?
  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 pattern and subject, predicate, object matches

Parameters:

Returns:

  • (Boolean)


70
71
72
73
74
75
# File 'lib/rdf_context/triple.rb', line 70

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

#hashString

For indexes

Returns:



99
100
101
# File 'lib/rdf_context/triple.rb', line 99

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

#inspectObject



50
51
52
53
54
55
56
57
58
# File 'lib/rdf_context/triple.rb', line 50

def inspect
  contents = [self.subject, self.predicate, self.object].map do |r|
    case r
    when URIRef, BNode, Literal then r.to_n3
    else r.inspect
    end
  end.join(" ")
  "#{self.class}[#{contents}]"
end

#is_pattern?Boolean

Is this a pattern triple (subject, predicate or object nil)

Returns:

  • (Boolean)


35
36
37
# File 'lib/rdf_context/triple.rb', line 35

def is_pattern?
  @pattern
end

#is_type?Boolean

Is the predicate of this statment rdf:type?

Returns:

  • (Boolean)


62
63
64
# File 'lib/rdf_context/triple.rb', line 62

def is_type?
  @predicate.to_s == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
end

#to_n3String Also known as: to_ntriples

Serialize Triple to N3

Returns:

Raises:



41
42
43
44
# File 'lib/rdf_context/triple.rb', line 41

def to_n3
  raise RdfException.new("Can't serialize pattern triple '#{@subject.inspect}, #{@predicate.inspect}, #{@object.inspect}'") if is_pattern?
  @subject.to_ntriples + " " + @predicate.to_ntriples + " " + @object.to_ntriples + " ."
end

#to_sString

Returns:



48
# File 'lib/rdf_context/triple.rb', line 48

def to_s; self.to_ntriples; end

#validate_rdf

This method returns an undefined value.

Validate that this triple is legal RDF, not extended for Notation-3



92
93
94
95
# File 'lib/rdf_context/triple.rb', line 92

def validate_rdf
  raise InvalidNode, "Triple has illegal RDF subject #{subject.inspect}" unless subject.is_a?(URIRef) || subject.is_a?(BNode)
  raise InvalidPredicate, "Triple has illegal RDF predicate #{predicate.inspect}" unless predicate.is_a?(URIRef)
end