Class: RdfContext::Triple
- Inherits:
-
Object
- Object
- RdfContext::Triple
- 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)
-
- (Object) object
Returns the value of attribute object.
-
- (Object) predicate
Returns the value of attribute predicate.
-
- (Object) subject
Returns the value of attribute subject.
Class Method Summary (collapse)
-
+ (Object) coerce_node(node)
protected
Coerce a node (subject or object) to the appropriate RdfContext type.
-
+ (Object) coerce_predicate(predicate)
protected
Coerce a predicate to the appropriate RdfContext type.
Instance Method Summary (collapse)
-
- (Triple) clone
Clone triple, keeping references to literals and URIRefs, but cloning BNodes.
-
- (Boolean) eql?(other)
(also: #==)
Two triples are equal if their of their subjects, predicates and objects are equal.
-
- (String) hash
For indexes.
-
- (Triple) initialize(subject, predicate, object)
constructor
Creates a new triple directly from the intended subject, predicate, and object.
- - (Object) inspect
-
- (Boolean) is_pattern?
Is this a pattern triple (subject, predicate or object nil).
-
- (Boolean) is_type?
Is the predicate of this statment rdf:type?.
-
- (String) to_n3
(also: #to_ntriples)
Serialize Triple to N3.
- - (String) to_s
-
- validate_rdf
Validate that this triple is legal RDF, not extended for Notation-3.
Constructor Details
- (Triple) initialize(subject, predicate, object)
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.
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
- (Object) object
Returns the value of attribute object
8 9 10 |
# File 'lib/rdf_context/triple.rb', line 8 def object @object end |
- (Object) predicate
Returns the value of attribute predicate
8 9 10 |
# File 'lib/rdf_context/triple.rb', line 8 def predicate @predicate end |
- (Object) subject
Returns the value of attribute subject
8 9 10 |
# File 'lib/rdf_context/triple.rb', line 8 def subject @subject end |
Class Method Details
+ (Object) coerce_node(node) (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. |
|
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 |
+ (Object) coerce_predicate(predicate) (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 |
|
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
- (Triple) clone
Clone triple, keeping references to literals and URIRefs, but cloning BNodes
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 |
- (Boolean) eql?(other) 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
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 |
- (String) hash
For indexes
99 100 101 |
# File 'lib/rdf_context/triple.rb', line 99 def hash [subject, predicate, object].hash end |
- (Object) inspect
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 |
- (Boolean) is_pattern?
Is this a pattern triple (subject, predicate or object nil)
35 36 37 |
# File 'lib/rdf_context/triple.rb', line 35 def is_pattern? @pattern end |
- (Boolean) is_type?
Is the predicate of this statment rdf:type?
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 |
- (String) to_n3 Also known as: to_ntriples
Serialize Triple to N3
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 |
- 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 |