MonkeyEngine 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.idea/.name +1 -0
  4. data/.idea/.rakeTasks +7 -0
  5. data/.idea/MonkeyEngine.iml +133 -0
  6. data/.idea/codeStyleSettings.xml +13 -0
  7. data/.idea/encodings.xml +5 -0
  8. data/.idea/misc.xml +5 -0
  9. data/.idea/modules.xml +9 -0
  10. data/.idea/runConfigurations/All_specs_in_test__MonkeyEngine.xml +38 -0
  11. data/.idea/runConfigurations/IRB_console.xml +25 -0
  12. data/.idea/runConfigurations/Start_Yard_Server.xml +26 -0
  13. data/.idea/runConfigurations/monkey_run.xml +26 -0
  14. data/.idea/scopes/scope_settings.xml +5 -0
  15. data/.idea/vcs.xml +7 -0
  16. data/.idea/workspace.xml +886 -0
  17. data/.ruby-version +1 -0
  18. data/Gemfile +4 -0
  19. data/LICENSE.txt +22 -0
  20. data/README.md +29 -0
  21. data/Rakefile +3 -0
  22. data/lib/Action/action.rb +29 -0
  23. data/lib/Monkey.rb +1 -0
  24. data/lib/Monkey/monkey.rb +108 -0
  25. data/lib/MonkeyAction/monkey_action.rb +14 -0
  26. data/lib/MonkeyAction/monkey_action_dead.rb +26 -0
  27. data/lib/MonkeyAction/monkey_action_eat.rb +31 -0
  28. data/lib/MonkeyAction/monkey_action_pause.rb +32 -0
  29. data/lib/MonkeyAction/monkey_action_sleep.rb +31 -0
  30. data/lib/MonkeyAction/monkey_action_type.rb +40 -0
  31. data/lib/MonkeyAction/monkey_action_wake.rb +26 -0
  32. data/lib/MonkeyAction/monkey_timed_action.rb +27 -0
  33. data/lib/MonkeyActions.rb +6 -0
  34. data/lib/MonkeyEngine.rb +1 -0
  35. data/lib/MonkeyEngine/action_rules.rb +111 -0
  36. data/lib/MonkeyEngine/exceptions.rb +21 -0
  37. data/lib/MonkeyEngine/monkey_engine.rb +53 -0
  38. data/lib/MonkeyEngine/version.rb +3 -0
  39. data/lib/MonkeyFactory.rb +1 -0
  40. data/lib/MonkeyFactory/monkey_factory.rb +14 -0
  41. data/lib/MonkeyKeyboard/keyboard_char.rb +10 -0
  42. data/lib/MonkeyKeyboard/keyboard_input.rb +14 -0
  43. data/lib/MonkeyKeyboard/keyboard_key.rb +26 -0
  44. data/lib/MonkeyKeyboard/keyboard_key_evaluator.rb +25 -0
  45. data/lib/MonkeyKeyboard/monkey_keyboard_en_us.rb +137 -0
  46. data/lib/MonkeyKeyboardEnUs.rb +1 -0
  47. data/lib/MonkeyManager.rb +1 -0
  48. data/lib/MonkeyManager/monkey_manager.rb +162 -0
  49. data/lib/MonkeyService.rb +1 -0
  50. data/lib/MonkeyService/monkey_service.rb +137 -0
  51. data/lib/tasks/engine.rb +91 -0
  52. data/monkeyengine.gemspec +32 -0
  53. data/spec/action_rules_spec.rb +59 -0
  54. data/spec/engine_spec.rb +56 -0
  55. data/spec/keyboard_char_spec.rb +12 -0
  56. data/spec/keyboard_key_spec.rb +15 -0
  57. data/spec/monkey_action_eat_spec.rb +86 -0
  58. data/spec/monkey_action_pause_spec.rb +91 -0
  59. data/spec/monkey_action_sleep_spec.rb +90 -0
  60. data/spec/monkey_action_type_spec.rb +94 -0
  61. data/spec/monkey_action_wake_spec.rb +58 -0
  62. data/spec/monkey_factory_spec.rb +23 -0
  63. data/spec/monkey_keyboard_en_us_spec.rb +42 -0
  64. data/spec/monkey_manager_spec.rb +8 -0
  65. data/spec/monkey_service_spec.rb +96 -0
  66. data/spec/monkey_spec.rb +8 -0
  67. data/spec/spec_helpers.rb +20 -0
  68. data/spec/support/shared_examples.rb +41 -0
  69. metadata +258 -0
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in monkeyengine.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Gene M. Angelo, Jr.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # MonkeyEngine
2
+
3
+ Have some fun! MonkeyEngine is a gem that allows virtual monkeys to tap away on a virtual keyboard.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'MonkeyEngine'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install MonkeyEngine
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+ require File.expand_path('../lib/tasks/engine.rb', __FILE__)
3
+
@@ -0,0 +1,29 @@
1
+ require 'time'
2
+
3
+ # Defines a base action.
4
+ class Action
5
+ attr_reader :value, :weight
6
+ attr_accessor :action_time
7
+
8
+ def initialize(value, weight)
9
+ @value = value
10
+ @weight = weight
11
+ @action_time = Time.now
12
+ @action_completed = false
13
+
14
+ self
15
+ end
16
+
17
+ def action_completed?
18
+ @action_completed
19
+ end
20
+
21
+ def action_completed=(value)
22
+ @action_completed = value
23
+ end
24
+
25
+ protected
26
+ def validate
27
+ # throw if invalid state
28
+ end
29
+ end
data/lib/Monkey.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative 'Monkey/monkey'
@@ -0,0 +1,108 @@
1
+ require 'thread'
2
+
3
+ require 'MonkeyActions'
4
+ require 'MonkeyService'
5
+ require 'ProtectedConstructor'
6
+
7
+ # Monkey class.
8
+ #
9
+ # @note The Monkey class represents a virtual monkey that performs particular _actions_
10
+ # that may or may not be typical of a real monkey. Monkey _actions_ are derived from class _MonkeyAction_.
11
+ #
12
+ # @note The constructor for this class is protected; to instantiate a managed _Monkey_, use
13
+ # MonkeyEngine::MonkeyService#add; to instantiate an unmanaged _Monkey_, use MonkeyFactory::create.
14
+ #
15
+ # @see MonkeyAction
16
+ # @see MonkeyEngine::MonkeyService#add
17
+ # @see MonkeyFactory::create
18
+ #
19
+ class Monkey
20
+ include ProtectedConstructor
21
+
22
+ # @!attribute [r] monkey_symbol
23
+ # @return [Symbol] the _Symbol_ that identifies this Monkey.
24
+ # @!attribute [r] action
25
+ # @return [MonkeyAction] the current _MonkeyAction_ that this _Monkey_ is engaged in.
26
+ #
27
+ attr_reader :monkey_symbol, :action
28
+
29
+ protected
30
+ def initialize(monkey_symbol)
31
+ @monkey_symbol = monkey_symbol
32
+ @monkey_service = MonkeyEngine::MonkeyService.instance
33
+ self
34
+ end
35
+
36
+ public
37
+
38
+ # Determines if the Monkey is alive.
39
+ #
40
+ # @note The Monkey is considered alive if the Monkey#thread.alive? is true.
41
+ #
42
+ # @return [Boolean] true if the Monkey is alive, false otherwise.
43
+ #
44
+ def alive?
45
+ return false if @thread.nil?
46
+ @thread.alive?
47
+ end
48
+
49
+ # Retrieves the current _Action_ that this Monkey is engaged in.
50
+ #
51
+ # @return [MonkeyAction] The _MonkeyAction_.
52
+ #
53
+ def current_action
54
+ return MonkeyActionDead.new(self) unless alive?
55
+ @action
56
+ end
57
+
58
+ # Kills this Monkey.
59
+ #
60
+ # @note The Monkey#monkey_do method executed continually by Monkey#thread is
61
+ # terminated.
62
+ #
63
+ def kill
64
+ @kill_thread = true
65
+ end
66
+
67
+ # Starts this Monkey.
68
+ #
69
+ # @raise [MonkeyEngine::Exceptions::InvalidOperationException] if Monkey#thread is already alive? (started).
70
+ #
71
+ # @return [Monkey, self]
72
+ #
73
+ def start
74
+ raise MonkeyEngine::Exceptions::InvalidOperationException.new "The monkey [#{@monkey_symbol}] thread is already started" \
75
+ if alive?
76
+ initialize_thread
77
+ self
78
+ end
79
+
80
+ # The thread that this Monkey uses to perform its actions.
81
+ #
82
+ # @return [Thread]
83
+ #
84
+ def thread
85
+ @thread
86
+ end
87
+
88
+ protected
89
+ def initialize_thread
90
+ @sleep_time = 1.0/250.0
91
+ @kill_thread = false
92
+
93
+ @thread = Thread.new {
94
+ monkey_do
95
+ }
96
+
97
+ #@thread.freeze
98
+ @thread.abort_on_exception = true
99
+ end
100
+
101
+ def monkey_do
102
+ until @kill_thread do
103
+ @action = @monkey_service.new_action(self) if @action.nil? || @action.action_completed?
104
+ @monkey_service.monkey_do @action unless @monkey_service.action_eval! @action
105
+ sleep(@sleep_time)
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,14 @@
1
+ require 'Action/action'
2
+
3
+ # MonkeyAction.
4
+ class MonkeyAction < Action
5
+ attr_reader :monkey
6
+
7
+ def initialize(monkey, value, weight)
8
+ super value, weight
9
+
10
+ @monkey = monkey
11
+
12
+ self
13
+ end
14
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'monkey_action'
2
+ require 'MonkeyEngine/exceptions'
3
+
4
+ # Monkey action: dead (as in not alive)
5
+ # The monkey has been killed - the thread is not running.
6
+ class MonkeyActionDead < MonkeyAction
7
+
8
+ WEIGHT = 100.0
9
+ VALID_VALUES = [ true ]
10
+
11
+ def initialize(monkey)
12
+ super monkey, true , WEIGHT
13
+
14
+ validate
15
+
16
+ self
17
+ end
18
+
19
+ protected
20
+ def validate
21
+ super
22
+
23
+ raise MonkeyEngine::Exceptions::InvalidArgumentValueException.new "Value '#{value}' is not a valid value" \
24
+ if !VALID_VALUES.include?(@value)
25
+ end
26
+ end
@@ -0,0 +1,31 @@
1
+ require 'time'
2
+
3
+ require_relative 'monkey_action'
4
+ require_relative 'monkey_timed_action'
5
+ require 'MonkeyEngine/exceptions'
6
+
7
+ # Monkey action: eat (as in banana)
8
+ # The monkey is eating.
9
+ class MonkeyActionEat < MonkeyTimedAction
10
+
11
+ WEIGHT = 2.0
12
+ VALID_VALUES = (30..60) # 30 through 60 minutes
13
+
14
+ def initialize(monkey, value)
15
+ super monkey, value, WEIGHT
16
+
17
+ @action_time_of_completion = @action_time + (value * 60)
18
+
19
+ validate
20
+
21
+ self
22
+ end
23
+
24
+ protected
25
+ def validate
26
+ super
27
+
28
+ raise MonkeyEngine::Exceptions::InvalidArgumentValueException.new "Value '#{value}' is not a valid value" \
29
+ if !VALID_VALUES.include?(@value)
30
+ end
31
+ end
@@ -0,0 +1,32 @@
1
+ require 'time'
2
+
3
+ require_relative 'monkey_action'
4
+ require_relative 'monkey_timed_action'
5
+ require 'MonkeyEngine/exceptions'
6
+
7
+ # Monkey action: pause (as in take a break)
8
+ # The monkey paused, usually from typing.
9
+ class MonkeyActionPause < MonkeyTimedAction
10
+
11
+ WEIGHT = 2.0
12
+ VALID_VALUES = (0..60) # Seconds (0 - 60 seconds)
13
+
14
+ def initialize(monkey, value)
15
+ super monkey, value, WEIGHT
16
+
17
+ @action_time_of_completion = @action_time + value
18
+
19
+ validate
20
+
21
+ self
22
+ end
23
+
24
+ protected
25
+ def validate
26
+ super
27
+
28
+ raise MonkeyEngine::Exceptions::InvalidArgumentValueException.new "Value '#{value}' is not a valid value" \
29
+ if !VALID_VALUES.include?(@value)
30
+ end
31
+
32
+ end
@@ -0,0 +1,31 @@
1
+ require 'time'
2
+
3
+ require_relative 'monkey_action'
4
+ require_relative 'monkey_timed_action'
5
+ require 'MonkeyEngine/exceptions'
6
+
7
+ # Monkey action: sleep (as in take a snooze)
8
+ # The monkey is a sleep.
9
+ class MonkeyActionSleep < MonkeyTimedAction
10
+
11
+ WEIGHT = 10.0
12
+ VALID_VALUES = ((60 * 6)..(60 * 8)) # 6 through 8 hours
13
+
14
+ def initialize(monkey, value)
15
+ super monkey, value, WEIGHT
16
+
17
+ @action_time_of_completion = @action_time + (value * (60 * 60))
18
+
19
+ validate
20
+
21
+ self
22
+ end
23
+
24
+ protected
25
+ def validate
26
+ super
27
+
28
+ raise MonkeyEngine::Exceptions::InvalidArgumentValueException.new "Value '#{value}' is not a valid value" \
29
+ if !VALID_VALUES.include?(@value)
30
+ end
31
+ end
@@ -0,0 +1,40 @@
1
+ require_relative 'monkey_action'
2
+ require 'MonkeyEngine/exceptions'
3
+
4
+ # Monkey action: type (as in keyboard)
5
+ # The monkey typed something on the keyboard.
6
+ class MonkeyActionType < MonkeyAction
7
+
8
+ attr_reader :keyboard_input
9
+
10
+ WEIGHT = 5.0
11
+
12
+ def initialize(monkey, keyboard_input)
13
+ raise MonkeyEngine::Exceptions::NilArgumentException.new "keyboard_input '#{keyboard_input}' cannot be nil" \
14
+ if keyboard_input.nil?
15
+
16
+ raise MonkeyEngine::Exceptions::InvalidArgumentTypeException.new "keyboard_input '#{keyboard_input}' is not a valid argument type" \
17
+ unless keyboard_input.is_a?(KeyboardInput)
18
+
19
+ @keyboard_input = keyboard_input
20
+
21
+ super monkey, @keyboard_input.input, WEIGHT
22
+
23
+ validate
24
+
25
+ self
26
+ end
27
+
28
+ protected
29
+ def validate
30
+
31
+ raise MonkeyEngine::Exceptions::NilArgumentException.new "Value '#{value}' cannot be nil" \
32
+ if @value.nil?
33
+
34
+ raise MonkeyEngine::Exceptions::InvalidArgumentTypeException.new "Value '#{value}' is not a valid argument type (#{value.class.name})" \
35
+ unless @value.is_a?(Array)
36
+
37
+ raise MonkeyEngine::Exceptions::InvalidArgumentValueException.new "Value '#{value}' cannot be empty" \
38
+ if @value.empty?
39
+ end
40
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'monkey_action'
2
+ require 'MonkeyEngine/exceptions'
3
+
4
+ # Monkey action: wake (as in from sleep)
5
+ # The monkey has awakened from sleep.
6
+ class MonkeyActionWake < MonkeyAction
7
+
8
+ WEIGHT = 2.0
9
+ VALID_VALUES = [ true ]
10
+
11
+ def initialize(monkey)
12
+ super monkey, true , WEIGHT
13
+
14
+ validate
15
+
16
+ self
17
+ end
18
+
19
+ protected
20
+ def validate
21
+ super
22
+
23
+ raise MonkeyEngine::Exceptions::InvalidArgumentValueException.new "Value '#{value}' is not a valid value" \
24
+ if !VALID_VALUES.include?(@value)
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'monkey_action'
2
+ require 'MonkeyEngine/exceptions'
3
+
4
+ # Monkey action: one that is timed based on an Integer.
5
+ class MonkeyTimedAction < MonkeyAction
6
+
7
+ attr_accessor :action_time_of_completion
8
+
9
+ def initialize(monkey, value, weight)
10
+ super monkey, value, weight
11
+
12
+ @action_time_of_completion = nil
13
+
14
+ validate
15
+
16
+ self
17
+ end
18
+
19
+ protected
20
+ def validate
21
+ raise MonkeyEngine::Exceptions::NilArgumentException.new "Value '#{value}' cannot be nil" \
22
+ if @value.nil?
23
+
24
+ raise MonkeyEngine::Exceptions::InvalidArgumentTypeException.new "Value '#{value}' is not a valid argument type" \
25
+ unless @value.is_a?(Integer)
26
+ end
27
+ end