public
Description: CSV based Ruby decision tables
Homepage: http://rufus.rubyforge.org/rufus-decision
Clone URL: git://github.com/jmettraux/rufus-decision.git
rufus-decision / README.txt
100644 152 lines (86 sloc) 3.266 kb
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
= rufus-decision
 
 
== getting it
 
  sudo gem install -y rufus-decision
 
or at
 
http://rubyforge.org/frs/?group_id=4812
 
== intro blog post
 
http://jmettraux.wordpress.com/2009/04/25/rufus-decision-11-ruby-decision-tables/
 
== usage
 
More info at http://rufus.rubyforge.org/rufus-decision/classes/Rufus/Decision/Table.html but here is a recap.
 
An example where a few rules determine which salesperson should interact with a customer with given characteristics.
 
 
  require 'rubygems'
  require 'rufus/decision'
  
  TABLE = Rufus::Decision::Table.new(%{
    in:age,in:trait,out:salesperson
    
    18..35,,adeslky
    25..35,,bronco
    36..50,,espadas
    51..78,,thorsten
    44..120,,ojiisan
    
    25..35,rich,kerfelden
    ,cheerful,swanson
    ,maniac,korolev
  })
  
  # Given a customer (a Hash instance directly, for
  # convenience), returns the name of the first
  # corresponding salesman.
  #
  def determine_salesperson (customer)
  
    TABLE.transform(customer)["salesperson"]
  end
  
  puts determine_salesperson(
    "age" => 72) # => thorsten
  
  puts determine_salesperson(
    "age" => 25, "trait" => "rich") # => adeslky
  
  puts determine_salesperson(
    "age" => 23, "trait" => "cheerful") # => adeslky
  
  puts determine_salesperson(
    "age" => 25, "trait" => "maniac") # => adeslky
  
  puts determine_salesperson(
    "age" => 44, "trait" => "maniac") # => espadas
 
 
More at Rufus::Decision::Table
 
Note that you can use a CSV table served over HTTP like in :
 
  
  require 'rubygems'
  require 'rufus/decision'
  
  TABLE = Rufus::DecisionTable.new(
    'http://spreadsheets.google.com/pub?key=pCkopoeZwCNsMWOVeDjR1TQ&output=csv')
  
  # the CSV is :
  #
  # in:weather,in:month,out:take_umbrella?
  #
  # raining,,yes
  # sunny,,no
  # cloudy,june,yes
  # cloudy,may,yes
  # cloudy,,no
  
  def take_umbrella? (weather, month=nil)
    h = TABLE.transform('weather' => weather, 'month' => month)
    h['take_umbrella?'] == 'yes'
  end
  
  puts take_umbrella?('cloudy', 'june')
    # => true
  
  puts take_umbrella?('sunny', 'june')
    # => false
 
In this example, the CSV table is the direction CSV representation of the Google spreadsheet at : http://spreadsheets.google.com/pub?key=pCkopoeZwCNsMWOVeDjR1TQ
 
WARNING though : use at your own risk. CSV loaded from untrusted locations may contain harmful code. The rufus-decision gem has an abstract tree checker integrated, it will check all the CSVs that contain calls in Ruby and raise a security error when possibly harmful code is spotted. Bullet vs Armor. Be warned.
 
 
== uninstalling it
 
    sudo gem uninstall -y rufus-decision
 
 
== dependencies
 
The gem 'rufus-dollar' (http://rufus.rubyforge.org/rufus-dollar) and the 'rufus-treechecker' gem (http://rufus.rubyforge.org/rufus-treechecker).
 
 
== mailing list
 
On the rufus-ruby list :
 
http://groups.google.com/group/rufus-ruby
 
 
== irc
 
irc.freenode.net #ruote
 
 
== issue tracker
 
http://rubyforge.org/tracker/?atid=18584&group_id=4812&func=browse
 
 
== source
 
http://github.com/jmettraux/rufus-decision
 
  git clone git://github.com/jmettraux/rufus-decision.git
 
 
== author
 
John Mettraux, jmettraux@gmail.com
http://jmettraux.wordpress.com
 
 
== the rest of Rufus
 
http://rufus.rubyforge.org
 
 
== license
 
MIT