Class: Kotodama::Rule

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language) ⇒ Rule

Returns a new instance of Rule.



5
6
7
8
9
10
# File 'lib/rule.rb', line 5

def initialize language
	@language = language
	@expressions = []
	@weights = []
	@excludes = []
end

Instance Attribute Details

#excludesObject (readonly)

Returns the value of attribute excludes.



3
4
5
# File 'lib/rule.rb', line 3

def excludes
  @excludes
end

#expressionsObject (readonly)

Returns the value of attribute expressions.



3
4
5
# File 'lib/rule.rb', line 3

def expressions
  @expressions
end

#languageObject (readonly)

Returns the value of attribute language.



3
4
5
# File 'lib/rule.rb', line 3

def language
  @language
end

#weightsObject (readonly)

Returns the value of attribute weights.



3
4
5
# File 'lib/rule.rb', line 3

def weights
  @weights
end

Instance Method Details

#add(expression, weight) ⇒ Object



12
13
14
15
# File 'lib/rule.rb', line 12

def add expression, weight
	@expressions << expression
	@weights << (weight || @language.options['$rule_weight'] || 1)
end

#generateObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rule.rb', line 17

def generate
	loop do
		expression = @expressions.random @weights
		output = []
		expression.each do |atom|
			if @language.rules.key? atom
				output += @language.rules[atom].generate
			elsif @language.types.key? atom
				output << @language.types[atom].generate
			else
				raise "`#{atom}' is not associated with the given language"
			end
		end
		catch :fail do
			@excludes.each do |n|
				throw :fail if output.join.match /#{n}/
			end
			return output
		end
	end
end