Class: FuzzySystem

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

Overview

Used for representing expert system of any kind

Instance Method Summary collapse

Constructor Details

#initializeFuzzySystem

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

#ejectObject

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

Declare an input in the system. This will result in creating i_ and i_= used for getting and setting the values for this input. Usage: .input(String name, FuzzyDomain domain)



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

Declare an an output in the system. This will result in creating o_ and o_= used for getting and setting the values for this output. Usage: .output(String name, FuzzyDomain domain)



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

#processObject

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

#resetObject

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