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 = nil, literals = [], &subscript_block) ⇒ 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)



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/activefacts/vocabulary/extensions.rb', line 346

def expand(frequency_constraints = [], define_role_names = nil, literals = [], &subscript_block)
  expanded = "#{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}".sub(/(.\b|.\Z)/, '\1-').sub(/- /,'-  ')
      la = nil if la == ""
      # Double the space to compensate for space removed below
      ta = "#{role_ref.trailing_adjective}".sub(/(\b.|\A.)/, '-\1').sub(/ -/,'  -')
      ta = nil if ta == ""

      expanded.gsub!(/\{#{i}\}/) {
          role_ref = role_refs[i]
          player = role_ref.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 && fc.is_a?(ActiveFacts::Metamodel::PresenceConstraint)
          if fc.is_a?(Array)
            fc, player_name = *fc
          else
            player_name = player.name
          end
          literal = literals[i]
          [
            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,
            # Can't have both a literal and a value constraint, but we don't enforce that here:
            literal ? literal : nil
          ].compact*" " +
            (subscript_block ? subscript_block.call(role_ref) : "")
      }
  }
  expanded.gsub!(/ ?- ?/, '-')        # Remove single spaces around adjectives
  #debug "Expanded '#{expanded}' using #{frequency_constraints.inspect}"
  expanded
end

#words_and_role_refsObject



391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/activefacts/vocabulary/extensions.rb', line 391

def words_and_role_refs
  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