Class: Rena::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/rena/graph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraph

Returns a new instance of Graph.



11
12
13
14
# File 'lib/rena/graph.rb', line 11

def initialize
  @triples = []
  @nsbinding = {}
end

Instance Attribute Details

#nsbindingObject

Returns the value of attribute nsbinding.



9
10
11
# File 'lib/rena/graph.rb', line 9

def nsbinding
  @nsbinding
end

#triplesObject

Returns the value of attribute triples.



9
10
11
# File 'lib/rena/graph.rb', line 9

def triples
  @triples
end

Instance Method Details

#<<(triple) ⇒ Object



78
79
80
81
# File 'lib/rena/graph.rb', line 78

def << (triple)
  #    self.add_triple(s, p, o)
  @triples += [ triple ]
end

#[](item) ⇒ Object



24
25
26
# File 'lib/rena/graph.rb', line 24

def [] (item)
  @triples[item]
end

#add_triple(s, p, o) ⇒ Array

Adds a triple to a graph directly from the intended subject, predicate, and object.

Example

g = Graph.new; g.add_triple(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new) # => results in the triple being added to g; returns an array of g's triples

Returns

Parameters:

Returns:

  • (Array)

    An array of the triples (leaky abstraction? consider returning the graph instead)

Raises:

  • (Error)

    Checks parameter types and raises if they are incorrect.

Author:

  • Tom Morris



60
61
62
# File 'lib/rena/graph.rb', line 60

def add_triple(s, p, o)
  @triples += [ Triple.new(s, p, o) ]
end

#bind(namespace) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/rena/graph.rb', line 120

def bind(namespace)
  if namespace.class == Namespace
    @nsbinding["#{namespace.short}"] = namespace
  else
    raise
  end
end

#eachObject



20
21
22
# File 'lib/rena/graph.rb', line 20

def each
  @triples.each { |value| yield value }
end

#each_with_subject(subject) ⇒ Object



28
29
30
31
32
# File 'lib/rena/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



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/rena/graph.rb', line 144

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



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rena/graph.rb', line 157

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
37
38
39
40
41
42
# File 'lib/rena/graph.rb', line 34

def get_resource(subject)
  temp = []
  each_with_subject(subject) do |value|
    temp << subject
  end
  if temp.any?
    Resource.new(temp)
  end
end

#has_bnode_identifier?(bnodeid) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/rena/graph.rb', line 128

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



174
175
176
177
178
179
180
181
182
# File 'lib/rena/graph.rb', line 174

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

Creates a new namespace given a URI and the short name and binds it to the graph.

Example

g = Graph.new; g.namespace("http://xmlns.com/foaf/0.1/", "foaf") # => binds the Foaf namespace to g

Returns

Parameters:

  • uri (String)

    the URI of the namespace

  • short (String)

    the short name of the namespace

Returns:

  • (Namespace)

    The newly created namespace.

Raises:

  • (Error)

    Checks validity of the desired shortname and raises if it is incorrect.

  • (Error)

    Checks that the newly created Namespace is of type Namespace and raises if it is incorrect.

Author:

  • Tom Morris



116
117
118
# File 'lib/rena/graph.rb', line 116

def namespace(uri, short)
  self.bind Namespace.new(uri, short)
end

#sizeObject



16
17
18
# File 'lib/rena/graph.rb', line 16

def size
  @triples.size
end

#to_ntriplesString

Exports the graph to RDF in N-Triples form.

Example

g = Graph.new; g.add_triple(BNode.new, URIRef.new("http://xmlns.com/foaf/0.1/knows"), BNode.new); g.to_ntriples  # => returns a string of the graph in N-Triples form

Returns

Returns:

  • (String)

    The graph in N-Triples.

Author:

  • Tom Morris



94
95
96
97
98
# File 'lib/rena/graph.rb', line 94

def to_ntriples
  @triples.collect do |t|
    t.to_ntriples
  end * "\n"
end