Class: ActiveFacts::Metamodel::Reading

Inherits:
Object
  • Object
show all
Defined in:
lib/activefacts/vocabulary/metamodel.rb,
lib/activefacts/vocabulary/extensions.rb

Instance Method Summary collapse

Instance Method Details

#expand(frequency_constraints = [], define_role_names = false) ⇒ Object

The frequency_constraints array here, if supplied, may provide for each role either:

  • a PresenceConstraint to be verbalised against the relevant role, or

  • a String, used as a definite or indefinite article on the relevant role, or

  • an array containing two strings (an article and a super-type name)

The order in the array is the same as the reading’s role-sequence. REVISIT: This should probably be changed to be the fact role sequence.

define_role_names here is false (use defined names), true (define names) or nil (neither)



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/activefacts/vocabulary/extensions.rb', line 314

def expand(frequency_constraints = [], define_role_names = false)
  expanded = "#{reading_text}"
  role_refs = role_sequence.all_role_ref.sort_by{|role_ref| role_ref.ordinal}
  (0...role_refs.size).each{|i|
      role_ref = role_refs[i]
      role = role_ref.role
      la = "#{role_ref.leading_adjective}"
      la.sub!(/(.\b|.\Z)/, '\1-')
      la = nil if la == ""
      ta = "#{role_ref.trailing_adjective}"
      ta.sub!(/(\b.|\A.)/, '-\1')
      ta = nil if ta == ""

      expanded.gsub!(/\{#{i}\}/) {
          player = role_refs[i].role.concept
          role_name = role.role_name
          role_name = nil if role_name == ""
          if role_name && define_role_names == false
            la = ta = nil   # When using role names, don't add adjectives
          end
          fc = frequency_constraints[i]
          fc = fc.frequency if fc && PresenceConstraint === fc
          if Array === fc
            fc, player_name = *fc
          else
            player_name = player.name
          end
          [
            fc ? fc : nil,
            la,
            define_role_names == false && role_name ? role_name : player_name,
            ta,
            define_role_names && role_name && player.name != role_name ? "(as #{role_name})" : nil
          ].compact*" "
      }
  }
  expanded.gsub!(/ *- */, '-')      # Remove spaces around adjectives
  #debug "Expanded '#{expanded}' using #{frequency_constraints.inspect}"
  expanded
end

#words_and_role_refsObject



355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/activefacts/vocabulary/extensions.rb', line 355

def words_and_role_refs
  reading_text.
  scan(/(?: |\{[0-9]+\}|[^{} ]+)/).   # split up the text into words
  reject{|s| s==' '}.                 # Remove white space
  map do |frag|                       # and go through the bits
    if frag =~ /\{([0-9]+)\}/
      role_sequence.all_role_ref.detect{|rr| rr.ordinal == $1.to_i}
    else
      frag
    end
  end
end