In a previous post, I introduced SPQR and presented a couple of examples of how one could use SPQR to publish Ruby objects over QMF. Sometimes, though, you aren’t starting from an application – instead, you’re starting from an XML QMF schema document. SPQR includes spqr-gen, a tool designed to automatically generate a skeleton SPQR application from a QMF schema. In this post, we’ll see an example of spqr-gen in action.

First, let’s look at a simple QMF schema for a class that exposes one method, echo, which returns its argument (note that the code examples may not show up if you’re viewing this in a feed reader):

codegen-example.xml
1
2
3
4
5
6
7
<schema package="examples.codegen">
  <class name="EchoAgent">
    <method name="echo" desc="returns its argument">
      <arg name="arg" dir="IO" type="lstr"/>
    </method>
  </class>
</schema>

Running spqr-gen on this example produces two files: agent-app.rb and examples/codegen/EchoAgent.rb. As you can see, these two files contain all of the boilerplate we need to start implementing these QMF methods — and, by chance, the boilerplate methods actually have the behavior that our agent is meant to!

agent-app.rb
1
2
3
4
5
6
7
8
9
require 'spqr/spqr'
require 'spqr/app'

require 'examples/codegen/EchoAgent'

app = SPQR::App.new(:loglevel => :debug)
app.register Examples::Codegen::EchoAgent

app.main
EchoAgent.rb
1
2
3
4
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
module Examples
  module Codegen
    class EchoAgent
      include SPQR::Manageable
      # Find method (NB:  you must implement this)
      def EchoAgent.find_by_id(objid)
        EchoAgent.new
      end

# Find-all method (NB:  you must implement this)
      def EchoAgent.find_all
        [EchoAgent.new]
      end
      ### Schema method declarations

      # echo returns its argument
      # * arg (lstr/IO)
      # 
      def echo(args)
        # Print values of in/out parameters
        log.debug "arg => #{args["arg"]}" # 
        # Assign values to in/out parameters
        args["arg"] = args["arg"]
      end

      spqr_expose :echo do |args|
        args.declare :arg, :lstr, :inout, {}
      end
    end
  end
end

spqr-gen is included in the SPQR repository; the system as documented in the last two posts is tagged ”introducing-spqr”.

  qmf, qpid, ruby, spqr • You may reply to this post on Twitter or