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.



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/tracery.rb', line 419

def initialize(grammar, raw)
    @raw = raw
    @grammar = grammar
    @falloff = 1
    
    @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



472
473
474
475
# File 'lib/tracery.rb', line 472

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

#selectRuleObject



437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/tracery.rb', line 437

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 = ((Tracery.random ** @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