Module: RDF::Parser

Included in:
Graph, Node, Repository
Defined in:
lib/lightrdf/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(format, text, uri = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lightrdf/parser.rb', line 54

def self.parse format, text, uri=nil
  case format
  when :ntriples
    graph = RDF::Graph.new
    graph.triples = text.split("\n").select{|l| l.strip!=''}.map do |l|
      s, lang1, p, lang2, o, lang3 = l.strip.match(/\A(<\S+>|".*"(@\w+)?|_:\w+)\s+(<\S+>|".*"(@\w+)?|_:\w+)\s+(<\S+>|".*"(@\w+)?|_:\w+)\s+\.\Z/).captures
      [parse_chunk_ntriples(s,uri), parse_chunk_ntriples(p,uri), parse_chunk_ntriples(o,uri)]
    end
    graph
  when :yarf
    graph = RDF::Graph.new
    ns = {}
    # Preprocessing - Extract namespaces, remove comments, get indent levels
    lines = []
    text.split("\n").each_with_index do |line, n|
      if line =~ /(\A\s*#|\A\w*\Z)/ # Comment or blank line - do nothing
      elsif line =~ /\A(\w+):\s+(.+)/ # Namespace
        ns[$1.to_sym] = $2
      else # Normal line - store line number, get indent level and strip line
        lines << [n+1, (line.size - line.lstrip.size)/2,  line.strip]
      end
    end
    parse_yarf_nodes lines, graph, ns
    graph.ns = ns
    graph
  when :rdf
    parse :rdfxml, text
  when :ejson
    raise Exception, "eJSON format cannot be parsed (yet)"
  else
    tempfile = new_tempfile
    File.open(tempfile, 'w') { |f| f.write text }
    parse :ntriples, %x[rapper -q -i #{format} -o ntriples #{tempfile} 2> /dev/null], uri
  end
end

Instance Method Details

#serialize(format, header = true) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lightrdf/parser.rb', line 5

def serialize format, header=true
  nodes = if self.is_a?(Graph)
    nodes_as_objects = find([], [], nil).select{|n| n.is_a?(Node)}
    values - nodes_as_objects + nodes_as_objects # Put them at the end
  else [self]
  end

  case format.to_sym
  when :ntriples
    triples.map { |s,p,o| "#{serialize_chunk_ntriples(s)} #{serialize_chunk_ntriples(p)} #{serialize_chunk_ntriples(o)} .\n" } * ''
  when :yarf
    ns = respond_to?(:ns) ? ID.ns.merge(self.ns) : ID.ns
    if header
      used_ns = {}
      triples.flatten.select { |node| node.is_a?(Symbol) and ID.uri?(node) }.each do |uri|
        prefix = ID.compress(uri).split(':').first.to_sym
        used_ns[prefix] = ns[prefix] if ns[prefix]
      end
      (used_ns.map{|k,v| "#{k}: #{v}\n"} * '') + serialize_yarf(nodes, ns)
    else
      serialize_yarf(nodes, ns)
    end
  when :ejson
    RDF::Parser.run "python -mjson.tool", ActiveSupport::JSON.encode(serialize_ejson(nodes))
  when :png
    dot = serialize(:dot)
    ns = respond_to?(:ns) ? ID.ns.merge(self.ns) : ID.ns
    ns.each { |k,v| dot.gsub!(v, "#{k}:") }
    dot.gsub!(/label=\"\\n\\nModel:.*\)\";/, '')

    RDF::Parser.run "dot -o/dev/stdout -Tpng", dot
  when :rdf
    serialize(:'rdfxml-abbrev')
  else
    namespaces = if [:rdfxml, :'rdfxml-abbrev'].include?(format)
      ns = respond_to?(:ns) ? ID.ns.merge(self.ns) : ID.ns
      used_ns = {}
      triples.flatten.select { |node| node.is_a?(Symbol) and ID.uri?(node) }.each do |uri|
        prefix = ID.compress(uri).split(':').first.to_sym
        used_ns[prefix] = ns[prefix] if ns[prefix]
      end
      used_ns.map {|k,v| "-f 'xmlns:#{k}=#{v.inspect}'" } * " "
    end
    tempfile = RDF::Parser.new_tempfile
    File.open(tempfile, 'w') { |f| f.write(serialize(:ntriples)) }
    %x[rapper -q -i ntriples #{namespaces} -o #{format} #{tempfile} 2> /dev/null]
  end
end

#to_ntriplesObject



3
# File 'lib/lightrdf/parser.rb', line 3

def to_ntriples; serialize :ntriples; end