/
diagnosis.rb
82 lines (58 loc) · 1.6 KB
/
diagnosis.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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!}"