class RuleSet

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

Public Class Methods

new(grammar, raw) click to toggle source
# 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

Public Instance Methods

clearState() click to toggle source
# File lib/tracery.rb, line 472
def clearState
    @defaultUses = {}
    #TODO_ should clear shuffled deck too?
end
selectRule() click to toggle source
# 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