Class: LSystem::RulesEngine
- Inherits:
-
Object
- Object
- LSystem::RulesEngine
- Extended by:
- Loggability
- Defined in:
- lib/l_system/rules_engine.rb
Overview
An engine for iterating over successive applications of the L-System’s ruleset to its axiom.
Instance Method Summary collapse
-
#alphabet ⇒ Object
Return the system’s variables and constants.
-
#apply_rules(state) ⇒ Object
Apply the system’s rules to the given
state
and return the result. -
#axiom(new_value = nil) ⇒ Object
Get/set the axiom of the system.
-
#axiom=(new_value) ⇒ Object
Set the axiom of the system.
-
#constants(*new_values) ⇒ Object
Get/set the system’s constants (the static parts of its alphabet).
-
#constants=(new_values) ⇒ Object
Set the systems constants to
new_values
. -
#each(&block) ⇒ Object
Yield each successive generation to the given
block
, or return an Enumerator that will do so if no block is given. -
#initialize(&block) ⇒ RulesEngine
constructor
Create a new rules engine for an L-System.
-
#rules(*new_values) ⇒ Object
Get/set the system’s rules.
-
#rules=(new_values) ⇒ Object
Set the system’s rules.
-
#variables(*new_values) ⇒ Object
Get/set the system’s variables (the replaceable parts of its alphabet).
-
#variables=(new_values) ⇒ Object
Set the systems variables to
new_values
.
Constructor Details
#initialize(&block) ⇒ RulesEngine
Create a new rules engine for an L-System. If the block
is present, it is called with the new instance as self
.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/l_system/rules_engine.rb', line 22 def initialize( &block ) @variables = Set.new @constants = Set.new @axiom = nil @rules = [] @rules_as_hash = nil self.instance_eval( &block ) if block end |
Instance Method Details
#alphabet ⇒ Object
Return the system’s variables and constants.
85 86 87 |
# File 'lib/l_system/rules_engine.rb', line 85 def alphabet return @variables | @constants end |
#apply_rules(state) ⇒ Object
Apply the system’s rules to the given state
and return the result.
122 123 124 125 126 127 |
# File 'lib/l_system/rules_engine.rb', line 122 def apply_rules( state ) rules_hash = self.rules_as_hash return state.each_char.with_object( String.new(encoding: 'utf-8') ) do |char, new_state| new_state << rules_hash[ char ] end end |
#axiom(new_value = nil) ⇒ Object
Get/set the axiom of the system.
91 92 93 94 |
# File 'lib/l_system/rules_engine.rb', line 91 def axiom( new_value=nil ) self.axiom = new_value if new_value return @axiom end |
#axiom=(new_value) ⇒ Object
Set the axiom of the system.
98 99 100 |
# File 'lib/l_system/rules_engine.rb', line 98 def axiom=( new_value ) @axiom = new_value end |
#constants(*new_values) ⇒ Object
Get/set the system’s constants (the static parts of its alphabet).
64 65 66 67 |
# File 'lib/l_system/rules_engine.rb', line 64 def constants( *new_values ) self.constants = new_values unless new_values.empty? return @constants.dup end |
#constants=(new_values) ⇒ Object
Set the systems constants to new_values
.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/l_system/rules_engine.rb', line 71 def constants=( new_values ) @rules_as_hash = nil new_values = Set.new( new_values, &:to_s ) unless new_values.disjoint?( self.variables ) common_char = (new_values & self.variables).to_a.first raise ArgumentError, "%p is already included in the variable set" % [ common_char ] end @constants = new_values end |
#each(&block) ⇒ Object
Yield each successive generation to the given block
, or return an Enumerator that will do so if no block is given.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/l_system/rules_engine.rb', line 132 def each( &block ) iter = Enumerator.new do |yielder| state = new_state = self.axiom.dup begin yielder.yield( new_state ) state = new_state new_state = self.apply_rules( state ) end until state == new_state end return iter unless block return iter.each( &block ) end |
#rules(*new_values) ⇒ Object
Get/set the system’s rules.
104 105 106 107 |
# File 'lib/l_system/rules_engine.rb', line 104 def rules( *new_values ) self.rules = new_values unless new_values.empty? return @rules end |
#rules=(new_values) ⇒ Object
Set the system’s rules.
111 112 113 114 |
# File 'lib/l_system/rules_engine.rb', line 111 def rules=( new_values ) @rules_as_hash = nil @rules = Array( new_values ).map( &:to_s ) end |
#variables(*new_values) ⇒ Object
Get/set the system’s variables (the replaceable parts of its alphabet).
43 44 45 46 |
# File 'lib/l_system/rules_engine.rb', line 43 def variables( *new_values ) self.variables = new_values unless new_values.empty? return @variables.dup end |
#variables=(new_values) ⇒ Object
Set the systems variables to new_values
.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/l_system/rules_engine.rb', line 50 def variables=( new_values ) @rules_as_hash = nil new_values = Set.new( new_values, &:to_s ) unless new_values.disjoint?( self.constants ) common_char = (new_values & self.constants).to_a.first raise ArgumentError, "%p is already included in the constant set" % [ common_char ] end @variables = new_values end |