Class: Graph
- Inherits:
-
Object
- Object
- Graph
- Defined in:
- lib/rena/graph.rb
Instance Attribute Summary collapse
-
#nsbinding ⇒ Object
Returns the value of attribute nsbinding.
-
#triples ⇒ Object
Returns the value of attribute triples.
Instance Method Summary collapse
- #<<(triple) ⇒ Object
-
#add_triple(s, p, o) ⇒ Array
Adds a triple to a graph directly from the intended subject, predicate, and object.
- #bind(namespace) ⇒ Object
- #each ⇒ Object
- #each_with_subject(subject) ⇒ Object
- #get_bnode_by_identifier(bnodeid) ⇒ Object
- #has_bnode_identifier?(bnodeid) ⇒ Boolean
-
#initialize ⇒ Graph
constructor
A new instance of Graph.
-
#namespace(uri, short) ⇒ Namespace
Creates a new namespace given a URI and the short name and binds it to the graph.
- #size ⇒ Object
-
#to_ntriples ⇒ String
Exports the graph to RDF in N-Triples form.
Constructor Details
#initialize ⇒ Graph
Returns a new instance of Graph.
10 11 12 13 |
# File 'lib/rena/graph.rb', line 10 def initialize @triples = [] @nsbinding = {} end |
Instance Attribute Details
#nsbinding ⇒ Object
Returns the value of attribute nsbinding.
8 9 10 |
# File 'lib/rena/graph.rb', line 8 def nsbinding @nsbinding end |
#triples ⇒ Object
Returns the value of attribute triples.
8 9 10 |
# File 'lib/rena/graph.rb', line 8 def triples @triples end |
Instance Method Details
#<<(triple) ⇒ Object
65 66 67 68 |
# File 'lib/rena/graph.rb', line 65 def << (triple) # self.add_triple(s, p, o) @triples += [ triple ] end |
#add_triple(s, p, o) ⇒ Array
47 48 49 |
# File 'lib/rena/graph.rb', line 47 def add_triple(s, p, o) @triples += [ Triple.new(s, p, o) ] end |
#bind(namespace) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/rena/graph.rb', line 109 def bind(namespace) if namespace.class == Namespace @nsbinding["#{namespace.short}"] = namespace else raise end end |
#each ⇒ Object
19 20 21 |
# File 'lib/rena/graph.rb', line 19 def each @triples.each { |value| yield value } end |
#each_with_subject(subject) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/rena/graph.rb', line 23 def each_with_subject(subject) @triples.each {|value| if value.subject == subject yield value end } end |
#get_bnode_by_identifier(bnodeid) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/rena/graph.rb', line 133 def get_bnode_by_identifier(bnodeid) temp_bnode = BNode.new(bnodeid) returnval = false @triples.each { |triple| if triple.subject.eql?(temp_bnode) returnval = triple.subject break end if triple.object.eql?(temp_bnode) returnval = triple.object break end } return returnval end |
#has_bnode_identifier?(bnodeid) ⇒ Boolean
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/rena/graph.rb', line 117 def has_bnode_identifier?(bnodeid) temp_bnode = BNode.new(bnodeid) returnval = false @triples.each { |triple| if triple.subject.eql?(temp_bnode) returnval = true break end if triple.object.eql?(temp_bnode) returnval = true break end } return returnval end |
#namespace(uri, short) ⇒ Namespace
105 106 107 |
# File 'lib/rena/graph.rb', line 105 def namespace(uri, short) self.bind Namespace.new(uri, short) end |
#size ⇒ Object
15 16 17 |
# File 'lib/rena/graph.rb', line 15 def size @triples.size end |
#to_ntriples ⇒ String
81 82 83 84 85 86 87 |
# File 'lib/rena/graph.rb', line 81 def to_ntriples str = "" @triples.each do |t| str << t.to_ntriples + "\n" end return str end |