Debian Codegraph @

ruby/lib/rdf/redland/queryresults.rb

raw symbols SCIP
1 require 'rdf/redland'
2
3 module Redland
4
5 # This class provides query language results support
6 class QueryResults
7
8 attr_reader :results
9
10 # this is not a public constructor
11 def initialize(object)
12 @results = object
13 ObjectSpace.define_finalizer(self,QueryResults.create_finalizer(@results))
14 end
15
16 # You shouldn't use this. Used internally for cleanup.
17 def QueryResults.create_finalizer(results)
18 proc{|id| # "Finalizer on #{id}"
19 #$log_final.info "closing results"
20 Redland::librdf_free_query_results(results)
21 }
22 end
23
24 # Get the number of results so far
25 def size
26 return Redland.librdf_query_results_get_count(@results)
27 end
28
29 # Get the number of results so far
30 def count
31 return Redland.librdf_query_results_get_count(@results)
32 end
33
34 # Test if the results are finished
35 def finished?
36 return (Redland.librdf_query_results_finished(@results) != 0)
37 end
38
39 # Get number of binding variables
40 def bindings_count
41 return Redland.librdf_query_results_get_bindings_count(@results)
42 end
43
44 # Get binding name for the current result
45 def binding_name(index)
46 return Redland.librdf_query_results_get_binding_name(@results,index)
47 end
48
49 # Get the names all of the variable bindings as an array
50 def binding_names
51 count=Redland.librdf_query_results_get_bindings_count(@results)
52 names=[]
53 for i in 0..count-1
54 names << binding_name(i)
55 end
56 return names
57 end
58
59 # Get one binding value for the current result
60 def binding_value(index)
61 node = Redland.librdf_query_results_get_binding_value(@results,index)
62 return node.nil? ? nil : Node.new(node)
63 end
64
65 # Get an array of all the values of all of the variable bindings in the current query result.
66 def binding_values
67 count=Redland.librdf_query_results_get_bindings_count(@results)
68 values=[]
69 for i in 0..count-1
70 values << binding_value(i)
71 end
72 return values
73 end
74
75 # Get the value of the variable binding name in the current query result.
76 def binding_value_by_name(name)
77 node=Redland.librdf_query_results_get_binding_value_by_name(@results,name)
78 return node.nil? ? nil : Node.new(node)
79 end
80
81 # Get an array of binding values
82 def bindings
83 (0...self.size).each{|i| binding_value( i ) }
84 end
85
86 # Move to the next query result
87 def next
88 Redland.librdf_query_results_next(@results)
89 end
90
91 # Get a new Stream object representing the query results as an RDF Graph.
92 def as_stream
93 my_stream=Redland.librdf_query_results_as_stream(@results)
94 return Stream.new(my_stream, self)
95 end
96
97 # Serialize to a string syntax in format_uri using the optional
98 # base_uri. The default format when none is given is determined by
99 # librdf_query_results_to_string
100 def to_string(format_uri = nil, base_uri = nil)
101 if self.is_graph?()
102 tmpmodel=Model.new()
103 tmpmodel.add_statements(self.as_stream())
104 serializer=Serializer.new()
105 return serializer.model_to_string(base_uri, tmpmodel)
106 end
107
108 if not self.is_boolean?() and not self.is_bindings?()
109 raise RedlandError.new("Unknown query result format cannot be written as a string")
110 end
111
112 if format_uri
113 format_uri = format_uri.uri
114 end
115 if base_uri
116 base_uri = base_uri.uri
117 end
118
119 return Redland.librdf_query_results_to_string(@results, format_uri, base_uri)
120 end
121
122
123 # Test if is variable bindings result
124 def is_bindings?()
125 return (Redland.librdf_query_results_is_bindings(@results) != 0)
126 end
127
128 # Test if is a boolean result
129 def is_boolean?()
130 return (Redland.librdf_query_results_is_boolean(@results) != 0)
131 end
132
133 # Test if is an RDF graph result
134 def is_graph?()
135 return (Redland.librdf_query_results_is_graph(@results) != 0)
136 end
137
138 # Get the boolean query result
139 def get_boolean?()
140 return (Redland.librdf_query_results_get_boolean(@results) != 0)
141 end
142
143
144 end
145
146 end #Redland
147