Class: Reddy::Graph
- Inherits:
-
Object
- Object
- Reddy::Graph
- Defined in:
- lib/reddy/graph.rb
Instance Attribute Summary collapse
-
#nsbinding ⇒ Object
Returns the value of attribute nsbinding.
-
#triples ⇒ Object
Returns the value of attribute triples.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(triple) ⇒ Object
- #[](item) ⇒ 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
- #get_by_type(object) ⇒ Object
- #get_resource(subject) ⇒ Object
- #has_bnode_identifier?(bnodeid) ⇒ Boolean
-
#initialize ⇒ Graph
constructor
A new instance of Graph.
- #join(graph) ⇒ Object
-
#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.
7 8 9 10 |
# File 'lib/reddy/graph.rb', line 7 def initialize @triples = [] @nsbinding = {} end |
Instance Attribute Details
#nsbinding ⇒ Object
Returns the value of attribute nsbinding.
5 6 7 |
# File 'lib/reddy/graph.rb', line 5 def nsbinding @nsbinding end |
#triples ⇒ Object
Returns the value of attribute triples.
5 6 7 |
# File 'lib/reddy/graph.rb', line 5 def triples @triples end |
Class Method Details
.load(uri) ⇒ Object
12 13 14 |
# File 'lib/reddy/graph.rb', line 12 def self.load (uri) RdfXmlParser.new(open(uri)).graph end |
Instance Method Details
#<<(triple) ⇒ Object
72 73 74 75 |
# File 'lib/reddy/graph.rb', line 72 def << (triple) # self.add_triple(s, p, o) @triples += [ triple ] end |
#[](item) ⇒ Object
24 25 26 |
# File 'lib/reddy/graph.rb', line 24 def [] (item) @triples[item] end |
#add_triple(s, p, o) ⇒ Array
54 55 56 |
# File 'lib/reddy/graph.rb', line 54 def add_triple(s, p, o) @triples += [ Triple.new(s, p, o) ] end |
#bind(namespace) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/reddy/graph.rb', line 114 def bind(namespace) if namespace.class == Namespace @nsbinding["#{namespace.short}"] = namespace else raise end end |
#each ⇒ Object
20 21 22 |
# File 'lib/reddy/graph.rb', line 20 def each @triples.each { |value| yield value } end |
#each_with_subject(subject) ⇒ Object
28 29 30 31 32 |
# File 'lib/reddy/graph.rb', line 28 def each_with_subject(subject) @triples.each do |value| yield value if value.subject == subject end end |
#get_bnode_by_identifier(bnodeid) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/reddy/graph.rb', line 138 def get_bnode_by_identifier(bnodeid) temp_bnode = BNode.new(bnodeid) each do |triple| if triple.subject == temp_bnode return triple.subject end if triple.object == temp_bnode return triple.object end end return false end |
#get_by_type(object) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/reddy/graph.rb', line 151 def get_by_type(object) out = [] each do |t| next unless t.is_type? next unless case object when String object == t.object.to_s when Regexp object.match(t.object.to_s) else object == t.object end out << t.subject end return out end |
#get_resource(subject) ⇒ Object
34 35 36 |
# File 'lib/reddy/graph.rb', line 34 def get_resource(subject) @triples.find_all { |i| true if i.subject == subject} end |
#has_bnode_identifier?(bnodeid) ⇒ Boolean
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/reddy/graph.rb', line 122 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 |
#join(graph) ⇒ Object
168 169 170 171 172 173 174 175 176 |
# File 'lib/reddy/graph.rb', line 168 def join(graph) if graph.class == Graph graph.each { |t| self << t } else raise "join requires you provide a graph object" end end |
#namespace(uri, short) ⇒ Namespace
110 111 112 |
# File 'lib/reddy/graph.rb', line 110 def namespace(uri, short) self.bind Namespace.new(uri, short) end |
#size ⇒ Object
16 17 18 |
# File 'lib/reddy/graph.rb', line 16 def size @triples.size end |
#to_ntriples ⇒ String
88 89 90 91 92 |
# File 'lib/reddy/graph.rb', line 88 def to_ntriples @triples.collect do |t| t.to_ntriples end * "\n" + "\n" end |