Class: RdfaParser::Graph

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

Overview

A simple graph to hold triples (from Reddy)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraph

Returns a new instance of Graph.



6
7
8
9
# File 'lib/rdfa_parser/graph.rb', line 6

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

Instance Attribute Details

#nsbindingObject

Returns the value of attribute nsbinding.



4
5
6
# File 'lib/rdfa_parser/graph.rb', line 4

def nsbinding
  @nsbinding
end

#triplesObject

Returns the value of attribute triples.



4
5
6
# File 'lib/rdfa_parser/graph.rb', line 4

def triples
  @triples
end

Class Method Details

.load(uri) ⇒ Object



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

def self.load (uri)
  RdfXmlParser.new(open(uri)).graph
end

Instance Method Details

#<<(triple) ⇒ Array

Adds an extant triple to a graph

Example

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

Returns

Parameters:

  • t (Triple)

    the triple to be added to the graph

Returns:

  • (Array)

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

Author:

  • Tom Morris



73
74
75
76
# File 'lib/rdfa_parser/graph.rb', line 73

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

#[](item) ⇒ Object



27
28
29
# File 'lib/rdfa_parser/graph.rb', line 27

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



56
57
58
# File 'lib/rdfa_parser/graph.rb', line 56

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

#bind(namespace) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/rdfa_parser/graph.rb', line 157

def bind(namespace)
  if namespace.class == Namespace
    @nsbinding["#{namespace.short}"] = namespace
  else
    raise GraphException, "Can't bind #{namespace.inspect} as namespace"
  end
end

#eachObject



19
20
21
# File 'lib/rdfa_parser/graph.rb', line 19

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

#each_with_subject(subject) ⇒ Object



31
32
33
34
35
# File 'lib/rdfa_parser/graph.rb', line 31

def each_with_subject(subject)
  @triples.each do |value|
    yield value if value.subject == subject
  end
end

#get_bnode_by_identifier(bnodeid) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/rdfa_parser/graph.rb', line 181

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



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/rdfa_parser/graph.rb', line 194

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



37
38
39
# File 'lib/rdfa_parser/graph.rb', line 37

def get_resource(subject)
  @triples.find_all { |i| true if i.subject == subject}
end

#has_bnode_identifier?(bnodeid) ⇒ Boolean

Returns:

  • (Boolean)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/rdfa_parser/graph.rb', line 165

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



211
212
213
214
215
216
217
218
219
# File 'lib/rdfa_parser/graph.rb', line 211

def join(graph)
  if graph.class == Graph
    graph.each { |t| 
      self << t
    }
  else
    raise GraphException, "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



153
154
155
# File 'lib/rdfa_parser/graph.rb', line 153

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

#sizeObject



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

def size
  @triples.size
end

#subjectsObject



23
24
25
# File 'lib/rdfa_parser/graph.rb', line 23

def subjects
  @triples.map {|t| t.subject}.uniq
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



89
90
91
92
93
# File 'lib/rdfa_parser/graph.rb', line 89

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

#to_rdfxmlObject

Dump model to RDF/XML



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rdfa_parser/graph.rb', line 99

def to_rdfxml
  replace_text = {}
  rdfxml = ""
  xml = builder = Builder::XmlMarkup.new(:target => rdfxml, :indent => 2)

  extended_bindings = nsbinding.merge(
    "rdf"   => RDF_NS,
    "rdfs"  => RDFS_NS,
    "xhv"   => XHV_NS,
    "xml"   => XML_NS
  )
  rdf_attrs = extended_bindings.values.inject({}) { |hash, ns| hash.merge(ns.xmlns_attr => ns.uri.to_s)}
  uri_bindings = extended_bindings.values.inject({}) { |hash, ns| hash.merge(ns.uri.to_s => ns.short)}
  xml.instruct!
  xml.rdf(:RDF, rdf_attrs) do
    # Add statements for each subject
    subjects.each do |s|
      xml.rdf(:Description, (s.is_a?(BNode) ? "rdf:nodeID" : "rdf:about") => s) do
        each_with_subject(s) do |triple|
          xml_args = triple.object.xml_args
          if triple.object.is_a?(Literal) && triple.object.xmlliteral?
            replace_text["__replace_with_#{triple.object.object_id}__"] = xml_args[0]
            xml_args[0] = "__replace_with_#{triple.object.object_id}__"
          end
          xml.tag!(triple.predicate.to_qname(uri_bindings), *xml_args)
        end
      end
    end
  end

  # Perform literal substitutions
  replace_text.each_pair do |match, value|
    rdfxml.sub!(match, value)
  end
  
  rdfxml
end

#to_sObject

Output graph using to_ntriples



96
# File 'lib/rdfa_parser/graph.rb', line 96

def to_s; self.to_ntriples; end