nondeterminism 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. data/History.txt +28 -0
  2. data/License.txt +17 -0
  3. data/Manifest.txt +37 -0
  4. data/README.txt +1 -0
  5. data/Rakefile +4 -0
  6. data/config/hoe.rb +71 -0
  7. data/config/requirements.rb +17 -0
  8. data/lib/nondeterminism.rb +22 -14
  9. data/lib/nondeterminism/array.rb +10 -0
  10. data/lib/nondeterminism/probability/event.rb +29 -0
  11. data/lib/nondeterminism/probability/event_chain.rb +42 -0
  12. data/lib/nondeterminism/probability/helper_methods.rb +17 -0
  13. data/lib/nondeterminism/probability/kernel.rb +10 -0
  14. data/lib/nondeterminism/probability/numeric.rb +8 -0
  15. data/lib/nondeterminism/range.rb +10 -0
  16. data/lib/nondeterminism/version.rb +9 -0
  17. data/log/debug.log +0 -0
  18. data/script/destroy +14 -0
  19. data/script/generate +14 -0
  20. data/script/txt2html +74 -0
  21. data/setup.rb +1585 -0
  22. data/tasks/deployment.rake +34 -0
  23. data/tasks/environment.rake +7 -0
  24. data/tasks/website.rake +17 -0
  25. data/test/{array_test.rb → test_array.rb} +1 -2
  26. data/test/test_helper.rb +2 -1
  27. data/test/{integer_generator_test.rb → test_integer_generator.rb} +10 -3
  28. data/test/{percent_probability_test.rb → test_percent_probability.rb} +1 -2
  29. data/test/{probability_event_chain_test.rb → test_probability_event_chain.rb} +1 -3
  30. data/test/{probability_generator_test.rb → test_probability_generator.rb} +1 -2
  31. data/test/{range_test.rb → test_range.rb} +1 -2
  32. data/test/{ratio_probability_test.rb → test_ratio_probability.rb} +1 -2
  33. data/website/index.html +181 -0
  34. data/website/index.txt +111 -0
  35. data/website/javascripts/rounded_corners_lite.inc.js +285 -0
  36. data/website/stylesheets/screen.css +138 -0
  37. data/website/template.rhtml +48 -0
  38. metadata +60 -21
  39. data/README +0 -86
@@ -0,0 +1,34 @@
1
+ desc 'Release the website and new gem version'
2
+ task :deploy => [:check_version, :website, :release] do
3
+ puts "Remember to create SVN tag:"
4
+ puts "svn copy svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/trunk " +
5
+ "svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/tags/REL-#{VERS} "
6
+ puts "Suggested comment:"
7
+ puts "Tagging release #{CHANGES}"
8
+ end
9
+
10
+ desc 'Runs tasks website_generate and install_gem as a local deployment of the gem'
11
+ task :local_deploy => [:website_generate, :install_gem]
12
+
13
+ task :check_version do
14
+ unless ENV['VERSION']
15
+ puts 'Must pass a VERSION=x.y.z release version'
16
+ exit
17
+ end
18
+ unless ENV['VERSION'] == VERS
19
+ puts "Please update your version.rb to match the release version, currently #{VERS}"
20
+ exit
21
+ end
22
+ end
23
+
24
+ desc 'Install the package as a gem, without generating documentation(ri/rdoc)'
25
+ task :install_gem_no_doc => [:clean, :package] do
26
+ sh "#{'sudo ' unless Hoe::WINDOZE }gem install pkg/*.gem --no-rdoc --no-ri"
27
+ end
28
+
29
+ namespace :manifest do
30
+ desc 'Recreate Manifest.txt to include ALL files'
31
+ task :refresh do
32
+ `rake check_manifest | patch -p0 > Manifest.txt`
33
+ end
34
+ end
@@ -0,0 +1,7 @@
1
+ task :ruby_env do
2
+ RUBY_APP = if RUBY_PLATFORM =~ /java/
3
+ "jruby"
4
+ else
5
+ "ruby"
6
+ end unless defined? RUBY_APP
7
+ end
@@ -0,0 +1,17 @@
1
+ desc 'Generate website files'
2
+ task :website_generate => :ruby_env do
3
+ (Dir['website/**/*.txt'] - Dir['website/version*.txt']).each do |txt|
4
+ sh %{ #{RUBY_APP} script/txt2html #{txt} > #{txt.gsub(/txt$/,'html')} }
5
+ end
6
+ end
7
+
8
+ desc 'Upload website files to rubyforge'
9
+ task :website_upload do
10
+ host = "#{rubyforge_username}@rubyforge.org"
11
+ remote_dir = "/var/www/gforge-projects/#{PATH}/"
12
+ local_dir = 'website'
13
+ sh %{rsync -aCv #{local_dir}/ #{host}:#{remote_dir}}
14
+ end
15
+
16
+ desc 'Generate and upload website files'
17
+ task :website => [:website_generate, :website_upload, :publish_docs]
@@ -1,7 +1,6 @@
1
- require 'test/unit'
2
1
  require File.dirname(__FILE__) + '/test_helper'
3
2
 
4
- class ArrayTest < Test::Unit::TestCase
3
+ class TestArray < Test::Unit::TestCase
5
4
 
6
5
  def setup
7
6
  Nondeterminism::integer_generator = Proc.new { |x, y| x }
data/test/test_helper.rb CHANGED
@@ -1 +1,2 @@
1
- require 'nondeterminism'
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/nondeterminism'
@@ -1,7 +1,5 @@
1
- require 'test/unit'
2
1
  require File.dirname(__FILE__) + '/test_helper'
3
2
 
4
-
5
3
  class NoToProc
6
4
  end
7
5
 
@@ -23,7 +21,7 @@ class ToProcArityTwoReturnsAverage
23
21
  end
24
22
  end
25
23
 
26
- class IntegerGeneratorTest < Test::Unit::TestCase
24
+ class TestIntegerGenerator < Test::Unit::TestCase
27
25
 
28
26
  def test_generator_must_define_to_proc
29
27
  assert_raises(ArgumentError) { Nondeterminism::integer_generator = NoToProc.new }
@@ -42,4 +40,13 @@ class IntegerGeneratorTest < Test::Unit::TestCase
42
40
  end
43
41
  end
44
42
 
43
+ def test_default_generator
44
+ Nondeterminism::integer_generator = Nondeterminism::DEFAULT_INTEGER_GENERATOR
45
+ i = 0
46
+ 10000.times do
47
+ i += Nondeterminism::random_integer(1, 19)
48
+ end
49
+ assert_in_delta 100000, i, 800, 'it may be a fluke; try again'
50
+ end
51
+
45
52
  end
@@ -1,7 +1,6 @@
1
- require 'test/unit'
2
1
  require File.dirname(__FILE__) + '/test_helper'
3
2
 
4
- class PercentProbabilisticTest < Test::Unit::TestCase
3
+ class TestPercentProbabilistic < Test::Unit::TestCase
5
4
 
6
5
  def setup
7
6
  Nondeterminism::probability_generator = Proc.new { 0.5 }
@@ -1,8 +1,6 @@
1
- require 'test/unit'
2
- require 'nondeterminism/probability/event_chain'
3
1
  require File.dirname(__FILE__) + '/test_helper'
4
2
 
5
- class ProbabilityEventChainTest < Test::Unit::TestCase
3
+ class TestProbabilityEventChain < Test::Unit::TestCase
6
4
 
7
5
  def new_chain(*args)
8
6
  Nondeterminism::Probability::EventChain.new(*args)
@@ -1,4 +1,3 @@
1
- require 'test/unit'
2
1
  require File.dirname(__FILE__) + '/test_helper'
3
2
 
4
3
 
@@ -18,7 +17,7 @@ class ToProcReturnsFour
18
17
  end
19
18
  end
20
19
 
21
- class ProbabilityGeneratorTest < Test::Unit::TestCase
20
+ class TestProbabilityGenerator < Test::Unit::TestCase
22
21
 
23
22
  def test_generator_must_define_to_proc
24
23
  assert_raises(ArgumentError) { Nondeterminism::probability_generator = NoToProc.new }
@@ -1,7 +1,6 @@
1
- require 'test/unit'
2
1
  require File.dirname(__FILE__) + '/test_helper'
3
2
 
4
- class RangeTest < Test::Unit::TestCase
3
+ class TestRange < Test::Unit::TestCase
5
4
 
6
5
  def setup
7
6
  Nondeterminism::integer_generator = Proc.new { |x, y| y }
@@ -1,7 +1,6 @@
1
- require 'test/unit'
2
1
  require File.dirname(__FILE__) + '/test_helper'
3
2
 
4
- class RatioProbabilisticTest < Test::Unit::TestCase
3
+ class TestRatioProbabilistic < Test::Unit::TestCase
5
4
 
6
5
  def setup
7
6
  Nondeterminism::probability_generator = Proc.new { 0.5 }
@@ -0,0 +1,181 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
+ <head>
5
+ <link rel="stylesheet" href="stylesheets/screen.css" type="text/css" media="screen" />
6
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
7
+ <title>
8
+ nondeterminism
9
+ </title>
10
+ <script src="javascripts/rounded_corners_lite.inc.js" type="text/javascript"></script>
11
+ <style>
12
+
13
+ </style>
14
+ <script type="text/javascript">
15
+ window.onload = function() {
16
+ settings = {
17
+ tl: { radius: 10 },
18
+ tr: { radius: 10 },
19
+ bl: { radius: 10 },
20
+ br: { radius: 10 },
21
+ antiAlias: true,
22
+ autoPad: true,
23
+ validTags: ["div"]
24
+ }
25
+ var versionBox = new curvyCorners(settings, document.getElementById("version"));
26
+ versionBox.applyCornersToAll();
27
+ }
28
+ </script>
29
+ </head>
30
+ <body>
31
+ <div id="main">
32
+
33
+ <h1>nondeterminism</h1>
34
+ <div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/nondeterminism"; return false'>
35
+ <p>Get Version</p>
36
+ <a href="http://rubyforge.org/projects/nondeterminism" class="numbers">0.2.1</a>
37
+ </div>
38
+ <h1>&#x2192; &#8216;nondeterminism&#8217;</h1>
39
+
40
+
41
+ <h2>What
42
+ probabilistic code utilities</h2>
43
+
44
+
45
+ <h2>Installing</h2>
46
+
47
+
48
+ <p><pre class='syntax'><span class="ident">sudo</span> <span class="ident">gem</span> <span class="ident">install</span> <span class="ident">nondeterminism</span></pre></p>
49
+
50
+
51
+ <h2>The basics</h2>
52
+
53
+
54
+ <h2>Demonstration of usage</h2>
55
+
56
+
57
+ <pre class='syntax'>
58
+ <span class="comment"># accept only half of emails</span>
59
+ <span class="number">50</span><span class="punct">.</span><span class="ident">percent_of_the_time</span> <span class="keyword">do</span>
60
+ <span class="ident">email</span><span class="punct">.</span><span class="ident">accept!</span>
61
+ <span class="keyword">end</span></pre>
62
+
63
+
64
+ <pre class='syntax'>
65
+ <span class="comment"># accept only half of emails and send nasty</span>
66
+ <span class="comment"># responses to the rest</span>
67
+ <span class="number">50</span><span class="punct">.</span><span class="ident">percent_of_the_time</span> <span class="keyword">do</span>
68
+ <span class="ident">email</span><span class="punct">.</span><span class="ident">accept!</span>
69
+ <span class="keyword">end</span><span class="punct">.</span><span class="ident">else</span> <span class="keyword">do</span>
70
+ <span class="ident">email</span><span class="punct">.</span><span class="ident">send_nasty_response!</span>
71
+ <span class="keyword">end</span></pre>
72
+
73
+
74
+ <pre class='syntax'>
75
+ <span class="comment"># name should be</span>
76
+ <span class="comment"># 'jerome' 30% of the time</span>
77
+ <span class="comment"># 'kevin' 40% of the time</span>
78
+ <span class="comment"># 'leslie' the rest of the time</span>
79
+ <span class="ident">with_probability</span><span class="punct">(</span><span class="number">0.30</span><span class="punct">)</span> <span class="keyword">do</span>
80
+ <span class="ident">name</span> <span class="punct">=</span> <span class="punct">'</span><span class="string">jerome</span><span class="punct">'</span>
81
+ <span class="keyword">end</span><span class="punct">.</span><span class="ident">else_with_probability</span><span class="punct">(</span><span class="number">0.40</span><span class="punct">)</span> <span class="keyword">do</span>
82
+ <span class="ident">name</span> <span class="punct">=</span> <span class="punct">'</span><span class="string">kevin</span><span class="punct">'</span>
83
+ <span class="keyword">end</span><span class="punct">.</span><span class="ident">else</span>
84
+ <span class="ident">name</span> <span class="punct">=</span> <span class="punct">'</span><span class="string">leslie</span><span class="punct">'</span>
85
+ <span class="keyword">end</span></pre>
86
+
87
+
88
+ <pre class='syntax'>
89
+ <span class="comment"># select a random friend from an Array:</span>
90
+ <span class="ident">friend</span> <span class="punct">=</span> <span class="ident">person</span><span class="punct">.</span><span class="ident">friends</span><span class="punct">.</span><span class="ident">random_element</span></pre>
91
+
92
+
93
+ <pre class='syntax'>
94
+ <span class="comment"># call foo between 5 and 8 times (inclusive):</span>
95
+ <span class="punct">(</span><span class="number">5</span><span class="punct">..</span><span class="number">8</span><span class="punct">).</span><span class="ident">times</span> <span class="keyword">do</span>
96
+ <span class="ident">foo</span>
97
+ <span class="keyword">end</span></pre>
98
+
99
+
100
+ <h2>Customization</h2>
101
+
102
+
103
+ <h3>Using a custom random probability generator:</h3>
104
+
105
+
106
+ <p>if you want to change the random-probability-generation mechanism, do
107
+ something like:</p>
108
+
109
+
110
+ <pre class='syntax'>
111
+ <span class="constant">Nondeterminism</span><span class="punct">::</span><span class="ident">probability_generator</span> <span class="punct">=</span> <span class="constant">Proc</span><span class="punct">.</span><span class="ident">new</span> <span class="punct">{</span> <span class="punct">...</span> <span class="punct">}</span></pre>
112
+
113
+
114
+ <p>or</p>
115
+
116
+
117
+ <pre class='syntax'>
118
+ <span class="constant">Nondeterminism</span><span class="punct">::</span><span class="ident">probability_generator</span> <span class="punct">=</span> <span class="ident">my_obj</span></pre>
119
+
120
+
121
+ <p>just make sure my_obj responds to #to_proc and the Proc only returns
122
+ numbers in [0.0, 1.0]</p>
123
+
124
+
125
+ <h3>Using a custom random integer generator:</h3>
126
+
127
+
128
+ <p>if you want to change the random-integer-generation mechanism, do
129
+ something like:</p>
130
+
131
+
132
+ <pre class='syntax'>
133
+ <span class="constant">Nondeterminism</span><span class="punct">::</span><span class="ident">integer_generator</span> <span class="punct">=</span> <span class="constant">Proc</span><span class="punct">.</span><span class="ident">new</span> <span class="punct">{</span> <span class="punct">|</span><span class="ident">low</span><span class="punct">,</span> <span class="ident">high</span><span class="punct">|</span> <span class="punct">...</span> <span class="punct">}</span></pre>
134
+
135
+
136
+ <p>or</p>
137
+
138
+
139
+ <pre class='syntax'>
140
+ <span class="constant">Nondeterminism</span><span class="punct">::</span><span class="ident">integer_generator</span> <span class="punct">=</span> <span class="constant">Proc</span><span class="punct">.</span><span class="ident">new</span> <span class="punct">{</span> <span class="punct">|</span><span class="ident">low</span><span class="punct">,</span> <span class="ident">high</span><span class="punct">|</span> <span class="punct">...</span> <span class="punct">}</span></pre>
141
+
142
+
143
+ <p>just make sure my_obj responds to #to_proc, the Proc has #arity = 2
144
+ (or &lt; -2), and only returns numbers in [low, high]</p>
145
+
146
+
147
+ <h2>Forum</h2>
148
+
149
+
150
+ <p><a href="http://groups.google.com/group/ruby-nondeterminism">http://groups.google.com/group/ruby-nondeterminism</a></p>
151
+
152
+
153
+ <h2>How to submit patches</h2>
154
+
155
+
156
+ <p>Read the <a href="http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/">8 steps for fixing other people&#8217;s code</a> and for section <a href="http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/#8b-google-groups">8b: Submit patch to Google Groups</a>, use the Google Group above.</p>
157
+
158
+
159
+ <p>The trunk repository is svn://rubyforge.org/var/svn/nondeterminism/trunk for anonymous access.</p>
160
+
161
+
162
+ <h2>License</h2>
163
+
164
+
165
+ <p>This code is free to use under the terms of the Apache License.</p>
166
+
167
+
168
+ <h2>Contact</h2>
169
+
170
+
171
+ <p>Comments are welcome. Send an email to <a href="mailto:ruby-nondeterminism@googlegroups.com">Ruby Nondeterminism Google Group</a> email via the <a href="http://groups.google.com/group/ruby-nondeterminism">forum</a></p>
172
+ <p class="coda">
173
+ <a href="mailto:james@u-presence.com">James Rosen</a>, 30th January 2008<br>
174
+ Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
175
+ </p>
176
+ </div>
177
+
178
+ <!-- insert site tracking codes here, like Google Urchin -->
179
+
180
+ </body>
181
+ </html>
data/website/index.txt ADDED
@@ -0,0 +1,111 @@
1
+ h1. nondeterminism
2
+
3
+ h1. &#x2192; 'nondeterminism'
4
+
5
+
6
+ h2. What
7
+ probabilistic code utilities
8
+
9
+ h2. Installing
10
+
11
+ <pre syntax="ruby">sudo gem install nondeterminism</pre>
12
+
13
+ h2. The basics
14
+
15
+
16
+ h2. Demonstration of usage
17
+
18
+ <pre syntax='ruby'>
19
+ # accept only half of emails
20
+ 50.percent_of_the_time do
21
+ email.accept!
22
+ end</pre>
23
+
24
+ <pre syntax='ruby'>
25
+ # accept only half of emails and send nasty
26
+ # responses to the rest
27
+ 50.percent_of_the_time do
28
+ email.accept!
29
+ end.else do
30
+ email.send_nasty_response!
31
+ end</pre>
32
+
33
+ <pre syntax='ruby'>
34
+ # name should be
35
+ # 'jerome' 30% of the time
36
+ # 'kevin' 40% of the time
37
+ # 'leslie' the rest of the time
38
+ with_probability(0.30) do
39
+ name = 'jerome'
40
+ end.else_with_probability(0.40) do
41
+ name = 'kevin'
42
+ end.else
43
+ name = 'leslie'
44
+ end</pre>
45
+
46
+ <pre syntax='ruby'>
47
+ # select a random friend from an Array:
48
+ friend = person.friends.random_element</pre>
49
+
50
+ <pre syntax='ruby'>
51
+ # call foo between 5 and 8 times (inclusive):
52
+ (5..8).times do
53
+ foo
54
+ end</pre>
55
+
56
+
57
+ h2. Customization
58
+
59
+ h3. Using a custom random probability generator:
60
+
61
+ if you want to change the random-probability-generation mechanism, do
62
+ something like:
63
+
64
+ <pre syntax='ruby'>
65
+ Nondeterminism::probability_generator = Proc.new { ... }</pre>
66
+
67
+ or
68
+
69
+ <pre syntax='ruby'>
70
+ Nondeterminism::probability_generator = my_obj</pre>
71
+
72
+ just make sure my_obj responds to #to_proc and the Proc only returns
73
+ numbers in [0.0, 1.0]
74
+
75
+
76
+ h3. Using a custom random integer generator:
77
+
78
+ if you want to change the random-integer-generation mechanism, do
79
+ something like:
80
+
81
+ <pre syntax='ruby'>
82
+ Nondeterminism::integer_generator = Proc.new { |low, high| ... }</pre>
83
+
84
+ or
85
+
86
+ <pre syntax='ruby'>
87
+ Nondeterminism::integer_generator = Proc.new { |low, high| ... }</pre>
88
+
89
+ just make sure my_obj responds to #to_proc, the Proc has #arity = 2
90
+ (or < -2), and only returns numbers in [low, high]
91
+
92
+
93
+
94
+ h2. Forum
95
+
96
+ "http://groups.google.com/group/ruby-nondeterminism":http://groups.google.com/group/ruby-nondeterminism
97
+
98
+ h2. How to submit patches
99
+
100
+ Read the "8 steps for fixing other people's code":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/ and for section "8b: Submit patch to Google Groups":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/#8b-google-groups, use the Google Group above.
101
+
102
+ The trunk repository is svn://rubyforge.org/var/svn/nondeterminism/trunk for anonymous access.
103
+
104
+ h2. License
105
+
106
+ This code is free to use under the terms of the Apache License.
107
+
108
+ h2. Contact
109
+
110
+ Comments are welcome. Send an email to "Ruby Nondeterminism Google Group":mailto:ruby-nondeterminism@googlegroups.com email via the "forum":http://groups.google.com/group/ruby-nondeterminism
111
+