Class: RuleSet

Inherits:
Object
  • Object
show all
Defined in:
lib/tracery.rb

Overview

Sets of rules (Can also contain conditional or fallback sets of rulesets)

Instance Method Summary collapse

Constructor Details

#initialize(grammar, raw) ⇒ RuleSet

Returns a new instance of RuleSet.



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/tracery.rb', line 404

def initialize(grammar, raw)
    @raw = raw
    @grammar = grammar
    @falloff = 1
    @random = Random.new
    
    @defaultUses = {}
    
    if(raw.is_a? Array) then
        @defaultRules = raw
    else
        if(raw.is_a? String) then
            @defaultRules = [raw];
        else
            # TODO: support for conditional and hierarchical rule sets
        end
    end
end

Instance Method Details

#clearStateObject



458
459
460
461
# File 'lib/tracery.rb', line 458

def clearState
    @defaultUses = {}
    #TODO_ should clear shuffled deck too?
end

#selectRuleObject



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/tracery.rb', line 423

def selectRule
    # puts "Get rule #{@raw}"
    
    #TODO_ : RuleSet.getRule @ conditionalRule
    #TODO_ : RuleSet.getRule @ ranking
    
    if(!@defaultRules.nil?) then
        index = 0
        # Select from this basic array of rules
        # Get the distribution from the grammar if there is no other
        distribution = @distribution || @grammar.distribution
        case(distribution)
            when "shuffle" then
                #create a shuffled deck
                if(@shuffledDeck.nil? || @shuffledDeck.empty?)
                    #TODO_ - use fyshuffle and falloff
                    @shuffledDeck = (0...@defaultRules.size).to_a.shuffle
                end
                index = @shuffledDeck.pop
            when "weighted" then
                @errors << "Weighted distribution not yet implemented"
            when "falloff" then
                @errors << "Falloff distribution not yet implemented"
            else
                index = ((@random.rand ** @falloff) * @defaultRules.size).floor
        end
    
        @defaultUses[index] = (@defaultUses[index] || 0) + 1
        return @defaultRules[index]
    end

    @errors << "No default rules defined for #{self}"
    return nil
end