Sha256: 0305d45c1ded7495dfc5e17ace68bcc57442d008b948f04625e31a08ff74382a

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

# license is MIT

# this example is strongly inspired by the diagnosis example
# for Ruleby ( http://github.com/codeaspects/ruleby )
# but please remember that Ruleby is a Rete implementation and is thus
# vastly superior to what is implemented here

$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

require 'volute'


#
# some classes

class Patient

  attr_reader :name

  attr_accessor :fever
  attr_accessor :rash
  attr_accessor :spots
  attr_accessor :sore_throat
  attr_accessor :innoculated

  attr_accessor :diagnosis

  def initialize(name)

    @name = name
    @symptoms = {}
    @innoculated = false
  end

  def diagnose!

    Volute.apply(self)

    @diagnosis
  end
end

#
# the volutes

volute Patient do

  # These volutes are 'state' volutes, they trigger if all the attributes
  # mentioned as keys yield the given value.
  # unlike classical volutes that act as OR, these states volutes have an
  # AND behaviour.

  volute :fever => :high, :spots => :true, :innoculated => true do
    object.diagnosis = 'measles'
    over # prevent further evals
  end
  volute :spots => true do
    object.diagnosis = 'allergy (spots but no measles)'
  end
  volute :rash => true do
    object.diagnosis = 'allergy (rash)'
  end
  volute :sore_throat => true, :fever => :mild do
    object.diagnosis = 'flu'
  end
  volute :sore_throat => true, :fever => :high do
    object.diagnosis = 'flu'
  end
end

pat = Patient.new('alice')
pat.rash = true

puts "#{pat.name} : diagnosed with #{pat.diagnose!}"

pat = Patient.new('bob')
pat.sore_throat = true
pat.fever = :high

puts "#{pat.name} : diagnosed with #{pat.diagnose!}"

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
volute-0.1.1 examples/diagnosis.rb