~kubuntu-members/korundum/4.11

« back to all changes in this revision

Viewing changes to korundum/modules/soprano/soprano.rb

  • Committer: Ian Monroe
  • Date: 2010-11-21 15:55:01 UTC
  • Revision ID: git-v1:c37670e4e3c59f5eb2ba112f5341a5e706217f6f
Split up Smoke into Qt and KDE directories. 
Move libsmoke stuff into the generator directory
Split up Ruby into qtruby and korundum directories

svn path=/trunk/KDE/kdebindings/ruby/; revision=1199320

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
=begin
 
2
/***************************************************************************
 
3
                          soprano.rb  -  Soprano SPARQL queries over DBus
 
4
                             -------------------
 
5
    begin                : Fri March 14 2008
 
6
    copyright            : (C) 2008 by Richard Dale
 
7
    email                : richard.j.dale@gmail.com
 
8
 ***************************************************************************/
 
9
 
 
10
/***************************************************************************
 
11
 *                                                                         *
 
12
 *   This program is free software; you can redistribute it and/or modify  *
 
13
 *   it under the terms of the GNU General Public License as published by  *
 
14
 *   the Free Software Foundation; either version 2 of the License, or     *
 
15
 *   (at your option) any later version.                                   *
 
16
 *                                                                         *
 
17
 ***************************************************************************/
 
18
=end
 
19
 
 
20
module Soprano
 
21
  module Internal
 
22
    def self.init_all_classes
 
23
#      Qt::Internal::add_normalize_proc(Proc.new do |classname|
 
24
#        if classname =~ /^Soprano/
 
25
#          now = classname.sub(/^Soprano?(?=[A-Z])/,'Soprano::')
 
26
#        end
 
27
#        now
 
28
#      end)
 
29
      getClassList.each do |c|
 
30
        classname = Qt::Internal::normalize_classname(c)
 
31
        id = Qt::Internal::findClass(c);
 
32
        Qt::Internal::insert_pclassid(classname, id)
 
33
        Qt::Internal::cpp_names[classname] = c
 
34
        klass = Qt::Internal::isQObject(c) ? Qt::Internal::create_qobject_class(classname, Soprano) \
 
35
                                           : Qt::Internal::create_qt_class(classname, Soprano)
 
36
        Qt::Internal::classes[classname] = klass unless klass.nil?
 
37
      end
 
38
    end
 
39
  end
 
40
 
 
41
  class Node < Qt::Base
 
42
    def type(*args)
 
43
      method_missing(:type, *args)
 
44
    end
 
45
 
 
46
    def inspect
 
47
      str = super
 
48
      case type.to_i
 
49
      when Soprano::Node::EmptyNode:
 
50
        str.sub(/>$/, " %s>" % "(empty)")
 
51
      when Soprano::Node::ResourceNode:
 
52
        str.sub(/>$/, " <%s>>" % uri.toString)
 
53
      when Soprano::Node::LiteralNode:
 
54
        if literal.isString && language
 
55
          str.sub(/>$/, ' "%s"@%s>' % [literal.toString, language])
 
56
        else 
 
57
          str.sub(/>$/, ' "%s"^^<%s>>' % [literal.toString, literal.dataTypeUri.toString])
 
58
        end
 
59
      when Soprano::Node::BlankNode:
 
60
        str.sub(/>$/, " _:%s>" % identifier)
 
61
      end
 
62
    end
 
63
                
 
64
    def pretty_print(pp)
 
65
      str = to_s
 
66
      case type.to_i
 
67
      when Soprano::Node::EmptyNode:
 
68
        pp.text str.sub(/>$/, " %s>" % "(empty)")
 
69
      when Soprano::Node::ResourceNode:
 
70
        pp.text str.sub(/>$/, " <%s>>" % uri.toString)
 
71
      when Soprano::Node::LiteralNode:
 
72
        if literal.isString && language
 
73
          pp.text str.sub(/>$/, ' "%s"@%s>' % [literal.toString, language])
 
74
        else 
 
75
          pp.text str.sub(/>$/, ' "%s"^^<%s>>' % [literal.toString, literal.dataTypeUri.toString])
 
76
        end
 
77
      when Soprano::Node::BlankNode:
 
78
        pp.text str.sub(/>$/, " _:%s>" % identifier)
 
79
      end
 
80
    end
 
81
 
 
82
    def self.demarshall(arg)
 
83
      arg.beginStructure
 
84
      type = Qt::Integer.new
 
85
      value = ""
 
86
      language = ""
 
87
      dataTypeUri = ""
 
88
      arg >> type >> value >> language >> dataTypeUri
 
89
 
 
90
      case type.to_i
 
91
      when Soprano::Node::EmptyNode:
 
92
        node = Soprano::Node.new
 
93
      when Soprano::Node::ResourceNode:
 
94
        node = Soprano::Node.new(Qt::Url.new(value))
 
95
      when Soprano::Node::LiteralNode:
 
96
        node = Soprano::Node.new(Soprano::LiteralValue.fromString(value, Qt::Url.new(dataTypeUri)), language)
 
97
      when Soprano::Node::BlankNode:
 
98
        node = Soprano::Node.new(value)
 
99
      else
 
100
        node = Soprano::Node.new
 
101
      end
 
102
      arg.endStructure
 
103
      return node
 
104
    end
 
105
  end
 
106
 
 
107
  class Statement < Qt::Base
 
108
    def inspect
 
109
      str = super
 
110
      str.sub(/>$/, " valid?=%s, subject=%s, predicate=%s, object=%s, context=%s>" % 
 
111
        [isValid, subject.inspect, predicate.inspect, object.inspect, context.inspect])
 
112
    end
 
113
    
 
114
    def pretty_print(pp)
 
115
      str = to_s
 
116
      pp.text str.sub(/>$/, " valid?=%s,\n subject=%s,\n predicate=%s,\n object=%s,\n context=%s>" % 
 
117
        [isValid, subject.inspect, predicate.inspect, object.inspect, context.inspect])
 
118
    end
 
119
 
 
120
    def self.demarshall(arg)
 
121
      arg.beginStructure
 
122
      subject = Soprano::Node.demarshall(arg)
 
123
      predicate = Soprano::Node.demarshall(arg)
 
124
      object = Soprano::Node.demarshall(arg)
 
125
      context = Soprano::Node.demarshall(arg)
 
126
      statement = Soprano::Statement.new(subject, predicate, object, context)
 
127
      arg.endStructure
 
128
      return statement
 
129
    end
 
130
  end
 
131
  
 
132
  class BindingSet < Qt::Base
 
133
    def self.demarshall(arg)
 
134
      set = {}
 
135
      arg.beginStructure
 
136
      arg.beginMap
 
137
      while !arg.atEnd
 
138
        arg.beginMapEntry
 
139
        name = ""
 
140
        arg >> name
 
141
        node = Soprano::Node.demarshall(arg)
 
142
        arg.endMapEntry
 
143
        set[name.to_sym] = node
 
144
      end
 
145
      arg.endMap
 
146
      arg.endStructure
 
147
      return set
 
148
    end
 
149
 
 
150
    def [](v)
 
151
      value(v.to_s)
 
152
    end
 
153
  end
 
154
 
 
155
  class NodeIterator < Qt::Base
 
156
    include Enumerable
 
157
 
 
158
    def each
 
159
      allNodes.each do |node|
 
160
        yield node
 
161
      end
 
162
    end
 
163
  end
 
164
 
 
165
  class StatementIterator < Qt::Base
 
166
    include Enumerable
 
167
 
 
168
    def each
 
169
      allStatements.each do |statement|
 
170
        yield statement
 
171
      end
 
172
    end
 
173
  end
 
174
 
 
175
  class QueryResultIterator < Qt::Base
 
176
    include Enumerable
 
177
 
 
178
    def each
 
179
      allBindings.each do |bindingSet|
 
180
        yield bindingSet
 
181
      end
 
182
    end
 
183
  end
 
184
 
 
185
  class StorageModel < Qt::Base
 
186
    # executeQuery() is a pure virtual method with an optional 
 
187
    # arg. This isn't supported by the smoke code generation
 
188
    # yet, so cater for the optional arg here for now
 
189
    def executeQuery(*args)
 
190
      if args.length == 2
 
191
        super(args[0], args[1], nil)
 
192
      else
 
193
        super
 
194
      end
 
195
    end
 
196
  end
 
197
 
 
198
  class Client < Qt::Base
 
199
    class DBusNodeIterator < Qt::Base
 
200
      include Enumerable
 
201
 
 
202
      def each
 
203
        allNodes.each do |node|
 
204
          yield node
 
205
        end
 
206
      end
 
207
    end
 
208
 
 
209
    class DBusStatementIterator < Qt::Base
 
210
      include Enumerable
 
211
 
 
212
      def each
 
213
        allStatements.each do |statement|
 
214
          yield statement
 
215
        end
 
216
      end
 
217
    end
 
218
 
 
219
    class DBusQueryResultIterator < Qt::Base
 
220
      include Enumerable
 
221
 
 
222
      def each
 
223
        allBindings.each do |bindingSet|
 
224
          yield bindingSet
 
225
        end
 
226
      end
 
227
    end
 
228
 
 
229
  end
 
230
end
 
231
 
 
232
# kate: space-indent on; indent-width 2; replace-tabs on; mixed-indent off;