Class: FuzzySystem
- Inherits:
-
Object
- Object
- FuzzySystem
- Defined in:
- lib/rfuzzy/fuzzy_system.rb
Overview
Used for representing expert system of any kind
Instance Method Summary collapse
-
#eject ⇒ Object
Returns a dict of all calculated outputs.
-
#initialize ⇒ FuzzySystem
constructor
Constructor - yields self.
-
#inject(dict) ⇒ Object
Used for pushing input into the system.
-
#input(name, domain) ⇒ Object
Declare an input in the system.
-
#output(name, default = nil) ⇒ Object
Declare an an output in the system.
-
#process ⇒ Object
Calls .apply on every rule in the system in order they were declared.
-
#reset ⇒ Object
Resets all outputs.
-
#rule(ant, cons) ⇒ Object
Used for adding rules to the system.
Constructor Details
#initialize ⇒ FuzzySystem
Constructor - yields self. Usage: .new()
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/rfuzzy/fuzzy_system.rb', line 11 def initialize @domains = {} @inputs = {} @outputs = {} @defaults = {} @rules = [] if block_given? yield self end end |
Instance Method Details
#eject ⇒ Object
Returns a dict of all calculated outputs. Usage: .eject()
90 91 92 |
# File 'lib/rfuzzy/fuzzy_system.rb', line 90 def eject return @outputs end |
#inject(dict) ⇒ Object
Used for pushing input into the system. Takes a dict in which keys are declared input names, and values are current values (FuzzyVariables). This method performs a check if given data matches the one declared for the system. Usage: .inject(Hash inputs)
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/rfuzzy/fuzzy_system.rb', line 75 def inject(dict) unless dict.keys == @domains.keys raise ArgumentError, 'Input dictionary does not match the system inputs' end dict.each do |key, value| if value.respond_to? :domain= value.domain = @domains[key] end end @inputs = dict end |
#input(name, domain) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rfuzzy/fuzzy_system.rb', line 26 def input(name, domain) @domains[name] = domain body = Proc.new do @inputs[name] end self.class.send(:define_method, "i_#{name}", body) body = Proc.new do |val| @inputs[name] = val end self.class.send(:define_method, "i_#{name}=", body) end |
#output(name, default = nil) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rfuzzy/fuzzy_system.rb', line 44 def output(name, default = nil) @defaults[name] = default body = Proc.new do unless @outputs[name].nil? @outputs[name] else @defaults[name] end end self.class.send(:define_method, "o_#{name}", body) body = Proc.new do |val| @outputs[name] = val end self.class.send(:define_method, "o_#{name}=", body) end |
#process ⇒ Object
Calls .apply on every rule in the system in order they were declared. Usage: .process()
102 103 104 105 106 |
# File 'lib/rfuzzy/fuzzy_system.rb', line 102 def process @rules.each do |r| r.apply end end |
#reset ⇒ Object
Resets all outputs. Usage: .reset()
96 97 98 |
# File 'lib/rfuzzy/fuzzy_system.rb', line 96 def reset @outputs = {} end |
#rule(ant, cons) ⇒ Object
Used for adding rules to the system. Usage: .rule(Proc antecendent, Proc consequent)
64 65 66 67 68 |
# File 'lib/rfuzzy/fuzzy_system.rb', line 64 def rule(ant, cons) r = Rule.new(ant, cons) @rules.push r return @rules[@rules.length-1] end |