Parent

Namespace

Class/Module Index [+]

Quicksearch

Ruote::Reader

A process definition reader.

Can reader XML, JSON, Ruby (and more) process definition representations.

Public Class Methods

new(context) click to toggle source
# File lib/ruote/reader.rb, line 71
def initialize(context)

  @context = context
end
read(d) click to toggle source

Class method for parsing process definition (XML, Ruby, from file or from a string, …) to syntax trees. Used by ruote-fluo for example.

# File lib/ruote/reader.rb, line 123
def self.read(d)

  unless @reader

    require 'ostruct'
    require 'ruote/svc/treechecker'

    @reader = Ruote::Reader.new(
      OpenStruct.new('treechecker' => Ruote::TreeChecker.new({})))
  end

  @reader.read(d)
end
remote?(definition) click to toggle source

Returns true if the defintion is a remote URI

# File lib/ruote/reader.rb, line 281
def self.remote?(definition)

  u = URI.parse(definition)

  (u.scheme != nil) && ( ! ('A'..'Z').include?(u.scheme))
end
to_expid_radial(tree) click to toggle source

Produces an expid annotated radial version of the process definition, like:

0  define name: "nada"
  0_0  sequence
    0_0_0  alpha
    0_0_1  participant "bravo", timeout: "2d", on_board: true

Can be useful when debugging noisy engines.

# File lib/ruote/reader.rb, line 238
def self.to_expid_radial(tree)

  lines = to_raw_expid_radial(tree, '0')
  max = lines.collect { |l| l[1].length }.max

  lines.collect { |l|
    "%#{max}s  " % l[1] + "  " * l[0] + l[2] + l[3]
  }.join("\n")
end
to_json(tree) click to toggle source

Turns the process definition tree (ruote syntax tree) to a JSON String.

# File lib/ruote/reader.rb, line 274
def self.to_json(tree)

  tree.to_json
end
to_radial(tree, level=0) click to toggle source

Turns the given tree into a radial process definition.

# File lib/ruote/reader.rb, line 219
def self.to_radial(tree, level=0)

  s = '  ' * level + tree[0] + atts_to_radial(tree[1]) + "\n"

  return s if tree[2].empty?

  tree[2].inject(s) { |ss, child| ss << to_radial(child, level + 1); ss }
end
to_raw_expid_radial(tree, expid='0') click to toggle source

Used by .to_expid_radial. Outputs an array of ‘lines’. Each line is a process definition line, represented as an array:

[ level, expid, name, atts ]

Like in:

[[0, "0", "define", " name: \"nada\""],
 [1, "0_0", "sequence", ""],
 [2, "0_0_0", "alpha", ""],
 [2, "0_0_1", "participant", " \"bravo\", timeout: \"2d\"]]
# File lib/ruote/reader.rb, line 260
def self.to_raw_expid_radial(tree, expid='0')

  i = -1

  [
    [ expid.split('_').size - 1, expid, tree[0], atts_to_radial(tree[1]) ]
  ] +
  tree[2].collect { |t|
    i = i + 1; to_raw_expid_radial(t, "#{expid}_#{i}")
  }.flatten(1)
end
to_ruby(tree, level=0) click to toggle source

Turns the given process definition tree (ruote syntax tree) to a Ruby process definition (a String containing that ruby process definition).

Mainly used by ruote-fluo.

# File lib/ruote/reader.rb, line 201
def self.to_ruby(tree, level=0)

  expname = tree[0]
  expname = 'Ruote.process_definition' if level == 0 && expname == 'define'

  s = '  ' * level + expname + atts_to_ruby(tree[1])

  return "#{s}\n" if tree[2].empty?

  s << " do\n"
  tree[2].each { |child| s << to_ruby(child, level + 1) }
  s << "#{'  ' * level}end\n"

  s
end
to_xml(tree, options={}) click to toggle source

Turns the given process definition tree (ruote syntax tree) to an XML String.

Mainly used by ruote-fluo.

# File lib/ruote/reader.rb, line 142
def self.to_xml(tree, options={})

  s = StringIO.new
  s.puts('<?xml version="1.0" encoding="UTF-8"?>')

  _to_xml(tree, options[:indent], 0, s)

  s.string
end

Protected Class Methods

atts_to_radial(atts, &block) click to toggle source

As used by to_radial

# File lib/ruote/reader.rb, line 329
def self.atts_to_radial(atts, &block)

  s = []
  txt, atts = split_atts(atts)

  s << to_ra_string(txt) if txt
  s += atts.collect { |k, v| "#{to_ra_string(k)}: #{to_ra_string(v)}" }

  s = s.join(', ')

  s.length > 0 ? " #{s}" : s
end
atts_to_ruby(atts, &block) click to toggle source

As used by to_ruby

# File lib/ruote/reader.rb, line 344
def self.atts_to_ruby(atts, &block)

  s = []
  txt, atts = split_atts(atts)

  s << txt.inspect if txt
  s += atts.collect { |k, v| ":#{k} => #{v.inspect}" }

  s = s.join(', ')

  s.length > 0 ? " #{s}" : s
end
split_atts(atts) click to toggle source

split the txt => nil entry and sorts the rest of the attributes.

# File lib/ruote/reader.rb, line 318
def self.split_atts(atts)

  atts = atts.to_a.sort_by { |k, v| k }
  txt = atts.find { |k, v| v == nil }
  atts.delete(txt) if txt

  [ txt ? txt.first : nil, atts ]
end
to_ra_string(o) click to toggle source
# File lib/ruote/reader.rb, line 299
def self.to_ra_string(o)

  return 'nil' if o == nil

  s = o.to_s

  return s if [ true, false ].include?(o)

  i = o.inspect

  return i if ] true false nil ].include?(s)
  return i if s.match(/[\s:]/)
  return s if i == "\"#{o.to_s}\""

  i
end

Public Instance Methods

read(definition) click to toggle source

Turns the input into a ruote syntax tree (raw process definition). This method is used by engine.launch(x) for example.

# File lib/ruote/reader.rb, line 79
def read(definition)

  return definition if Ruote.is_tree?(definition)

  raise ArgumentError.new(
    "cannot read process definitions of class #{definition.class}"
  ) unless definition.is_a?(String)

  if is_uri?(definition)

    if
      Ruote::Reader.remote?(definition) &&
      @context['remote_definition_allowed'] != true
    then
      raise ArgumentError.new('remote process definitions are not allowed')
    end

    return read(open(definition).read)
  end

  tree = nil
  error = Error.new(definition)

  [
    Ruote::RubyReader, Ruote::RadialReader,
    Ruote::XmlReader, Ruote::JsonReader
  ].each do |reader|

    next if tree
    next unless reader.understands?(definition)

    begin
      tree = reader.read(definition, @context.treechecker)
    rescue => e
      error << [ reader, e ]
    end
  end

  tree || raise(error)
end

Protected Instance Methods

is_uri?(s) click to toggle source

Minimal test. Used by read.

# File lib/ruote/reader.rb, line 292
def is_uri?(s)

  return false if s.index("\n")

  ((URI.parse(s); true) rescue false)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.