Debian Codegraph @

ruby/example.rb

raw symbols SCIP
1 #!/usr/bin/env ruby
2 #
3 # example.rb - Redland example Ruby program
4 #
5 # Copyright (C) 2002-2004 David Beckett - http://www.dajobe.org/
6 # Copyright (C) 2002-2004 University of Bristol - http://www.bristol.ac.uk/
7 #
8 # This package is Free Software and part of Redland http://librdf.org/
9 #
10 # It is licensed under the following three licenses as alternatives:
11 # 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
12 # 2. GNU General Public License (GPL) V2 or any newer version
13 # 3. Apache License, V2.0 or any newer version
14 #
15 # You may not use this file except in compliance with at least one of
16 # the above three licenses.
17 #
18 # See LICENSE.html or LICENSE.txt at the top of this package for the
19 # full license terms.
20 #
21 #
22 # USAGE: ruby example.rb file:../data/dc.rdf raptor
23 #
24 #
25
26 require 'rdf/redland'
27
28 uri_string=ARGV[0]
29 parser_name=ARGV[1]
30
31 storage=Redland::TripleStore.new("hashes", "test", "new='yes',hash-type='bdb',dir='.'")
32 raise "Failed to create RDF storage" if !storage
33
34
35 model=Redland::Model.new(storage)
36 if !model then
37 raise "Failed to create RDF model"
38 end
39
40 parser=Redland::Parser.new(parser_name, "", nil)
41 if !parser then
42 raise "Failed to create RDF parser"
43 end
44
45 uri=Redland::Uri.new(uri_string)
46 stream=parser.parse_as_stream(uri, uri)
47
48 count=0
49 while !stream.end?()
50 statement=stream.current()
51 model.add_statement(statement)
52 puts "found statement: #{statement}"
53 count=count+1
54 stream.next()
55 end
56
57 puts "Parsing added #{count} statements"
58
59
60 puts "Printing all statements"
61 stream=model.as_stream()
62 while !stream.end?()
63 statement=stream.current()
64 puts "Statement: #{statement}"
65 stream.next()
66 end
67
68 q = Redland::Query.new(" PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?a ?c WHERE { ?a dc:title ?c } ")
69 puts "Querying for dc:titles:"
70 results=q.execute(model)
71 while !results.finished?()
72 puts "{"
73 for k in 0..results.bindings_count()-1
74 puts " #{k}= #{results.binding_value(k)}"
75 end
76 puts "}"
77 results.next()
78 end
79
80 results=q.execute(model)
81 size=results.to_string(Redland::Uri.new("http://www.w3.org/2001/sw/DataAccess/json-sparql/")).length()
82 puts "Serialized query results to JSON as a string size #{size} bytes"
83
84 puts "Writing model to test-out.rdf as rdf/xml"
85 # Use any rdf/xml parser that is available
86 serializer=Redland::Serializer.new()
87 serializer.set_namespace("dc", Redland::Uri.new("http://purl.org/dc/elements/1.1/"))
88 serializer.set_namespace("rdf", Redland::Uri.new("http://www.w3.org/1999/02/22-rdf-syntax-ns#"))
89 serializer.to_file("test-out.rdf", model)
90
91 size=model.to_string(name="ntriples", base_uri=Redland::Uri.new("http://example.org/base#")).length()
92 puts "Serialized to ntriples as a string size #{size} bytes"
93
94 puts "Done"