Class: 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.



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

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

Instance Attribute Details

#nsbindingObject

Returns the value of attribute nsbinding.



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

def nsbinding
  @nsbinding
end

#triplesObject

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

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



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

#eachObject



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

Returns:

  • (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

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



105
106
107
# File 'lib/rena/graph.rb', line 105

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

#sizeObject



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

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



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