motel 0.2 → 0.3

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/README.rdoc +35 -0
  2. data/Rakefile +48 -0
  3. data/bin/clients/simple/main.rb +131 -0
  4. data/bin/server/main.rb +61 -0
  5. data/conf/motel-schema.xml +72 -0
  6. data/lib/motel/common.rb +35 -11
  7. data/lib/motel/dsl.rb +12 -0
  8. data/lib/motel/exceptions.rb +14 -0
  9. data/lib/motel/location.rb +100 -0
  10. data/lib/motel/{models → movement_strategies}/elliptical.rb +51 -89
  11. data/lib/motel/movement_strategies/linear.rb +54 -0
  12. data/lib/motel/movement_strategies/stopped.rb +18 -0
  13. data/lib/motel/movement_strategy.rb +29 -0
  14. data/lib/motel/runner.rb +94 -108
  15. data/lib/motel/simrpc.rb +131 -0
  16. data/lib/motel/thread_pool.rb +51 -0
  17. data/lib/motel.rb +9 -5
  18. data/spec/common_spec.rb +77 -0
  19. data/spec/dsl_spec.rb +27 -0
  20. data/spec/location_spec.rb +109 -0
  21. data/spec/movement_strategies/elliptical_spec.rb +90 -0
  22. data/spec/movement_strategies/linear_spec.rb +51 -0
  23. data/spec/movement_strategies/stopped_spec.rb +39 -0
  24. data/spec/movement_strategy_spec.rb +24 -0
  25. data/spec/runner_spec.rb +45 -0
  26. data/spec/simrpc_spec.rb +85 -0
  27. data/spec/spec_helper.rb +26 -0
  28. metadata +36 -34
  29. data/README +0 -0
  30. data/conf/amqp.yml +0 -13
  31. data/conf/database.yml +0 -41
  32. data/db/migrate/001_create_locations_and_movement_strategies.rb +0 -45
  33. data/db/migrate/002_create_linear_movement_strategy.rb +0 -23
  34. data/db/migrate/003_create_elliptical_movement_strategy.rb +0 -52
  35. data/lib/motel/loader.rb +0 -47
  36. data/lib/motel/models/linear.rb +0 -76
  37. data/lib/motel/models/location.rb +0 -109
  38. data/lib/motel/models/movement_strategy.rb +0 -82
  39. data/lib/motel/models/stopped.rb +0 -16
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motel
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.2"
4
+ version: "0.3"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mohammed Morsi
@@ -9,67 +9,69 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-04 00:00:00 -04:00
12
+ date: 2010-03-14 00:00:00 -05:00
13
13
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: activerecord
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 2.1.1
24
- version:
14
+ dependencies: []
15
+
25
16
  description: Motel is a library to track and move the locations of objects in a 3D environment.
26
17
  email: movitto@yahoo.com
27
18
  executables: []
28
19
 
29
20
  extensions: []
30
21
 
31
- extra_rdoc_files:
32
- - README
22
+ extra_rdoc_files: []
23
+
33
24
  files:
34
- - conf/amqp.yml
35
- - conf/database.yml
36
- - db/migrate/001_create_locations_and_movement_strategies.rb
37
- - db/migrate/002_create_linear_movement_strategy.rb
38
- - db/migrate/003_create_elliptical_movement_strategy.rb
39
- - lib/motel/models/location.rb
40
- - lib/motel/models/movement_strategy.rb
41
- - lib/motel/models/stopped.rb
42
- - lib/motel/models/linear.rb
43
- - lib/motel/models/elliptical.rb
25
+ - bin/server/main.rb
26
+ - bin/clients/simple/main.rb
27
+ - conf/motel-schema.xml
28
+ - lib/motel/movement_strategies/elliptical.rb
29
+ - lib/motel/movement_strategies/stopped.rb
30
+ - lib/motel/movement_strategies/linear.rb
31
+ - lib/motel/dsl.rb
32
+ - lib/motel/movement_strategy.rb
33
+ - lib/motel/location.rb
44
34
  - lib/motel/common.rb
45
- - lib/motel/loader.rb
46
- - lib/motel/runner.rb
47
35
  - lib/motel/semaphore.rb
36
+ - lib/motel/thread_pool.rb
37
+ - lib/motel/runner.rb
38
+ - lib/motel/exceptions.rb
39
+ - lib/motel/simrpc.rb
48
40
  - lib/motel.rb
49
- - LICENSE
50
41
  - COPYING
51
- - README
42
+ - LICENSE
43
+ - Rakefile
44
+ - README.rdoc
45
+ - spec/movement_strategies/stopped_spec.rb
46
+ - spec/movement_strategies/elliptical_spec.rb
47
+ - spec/movement_strategies/linear_spec.rb
48
+ - spec/common_spec.rb
49
+ - spec/spec_helper.rb
50
+ - spec/movement_strategy_spec.rb
51
+ - spec/location_spec.rb
52
+ - spec/simrpc_spec.rb
53
+ - spec/runner_spec.rb
54
+ - spec/dsl_spec.rb
52
55
  has_rdoc: true
53
56
  homepage: http://morsi.org/projects/motel
54
57
  licenses: []
55
58
 
56
59
  post_install_message:
57
- rdoc_options:
58
- - --inline-source
59
- - --charset=UTF-8
60
+ rdoc_options: []
61
+
60
62
  require_paths:
61
63
  - lib
62
64
  required_ruby_version: !ruby/object:Gem::Requirement
63
65
  requirements:
64
66
  - - ">="
65
67
  - !ruby/object:Gem::Version
66
- version: "0"
68
+ version: 1.8.1
67
69
  version:
68
70
  required_rubygems_version: !ruby/object:Gem::Requirement
69
71
  requirements:
70
72
  - - ">="
71
73
  - !ruby/object:Gem::Version
72
- version: 1.3.1
74
+ version: 1.3.3
73
75
  version:
74
76
  requirements: []
75
77
 
data/README DELETED
File without changes
data/conf/amqp.yml DELETED
@@ -1,13 +0,0 @@
1
- # motel amqp config
2
-
3
- production:
4
- broker: 127.0.0.1
5
- port: 5672
6
-
7
- development:
8
- broker: 127.0.0.1
9
- port: 5672
10
-
11
- test:
12
- broker: 127.0.0.1
13
- port: 5672
data/conf/database.yml DELETED
@@ -1,41 +0,0 @@
1
- # motel database config
2
- #
3
- # Fedora Install Instructions:
4
- # sudo yum install ruby-postgres postgresql-server
5
- # sudo /sbin/service postgresql initdb
6
- # sudo /sbin/service postgresql start
7
- # sudo su - postgres
8
- # createdb motel
9
- # $ psql motel
10
- # $-# CREATE USER motel WITH PASSWORD 'motel';
11
- # $-# GRANT ALL PRIVILEGES ON DATABASE motel to motel;
12
- # $-# CREATE DATABASE motel_test;
13
- # $-# GRANT ALL PRIVILEGES ON DATABASE motel_test to motel;
14
- # $-# CREATE DATABASE motel_development;
15
- # $-# GRANT ALL PRIVILEGES ON DATABASE motel_development to motel;
16
- # $-# \q
17
- # $ exit
18
-
19
- production:
20
- adapter: postgresql
21
- database: motel
22
- username: motel
23
- password: motel
24
- host: localhost
25
- pool: 20
26
-
27
- development:
28
- adapter: postgresql
29
- database: motel_development
30
- username: motel
31
- password: motel
32
- host: localhost
33
- pool: 20
34
-
35
- test:
36
- adapter: postgresql
37
- database: motel_test
38
- username: motel
39
- password: motel
40
- host: localhost
41
- pool: 20
@@ -1,45 +0,0 @@
1
- # creates the locations and movement_strategies tables
2
- #
3
- # Copyright (C) 2009 Mohammed Morsi <movitto@yahoo.com>
4
- # See COPYING for the License of this software
5
-
6
- require 'motel'
7
-
8
- # ActiveRecord::Migration 001
9
- class CreateLocationsAndMovementStrategies < ActiveRecord::Migration
10
- def self.up
11
-
12
- create_table :movement_strategies do |t|
13
- t.string :type, :null => false, :size => 50
14
- t.float :step_delay, :null => false
15
- end
16
-
17
- create_table :locations do |t|
18
- t.float :x, :default => nil
19
- t.float :y, :default => nil
20
- t.float :z, :default => nil
21
- t.integer :movement_strategy_id, :null => false
22
-
23
- t.integer :parent_id, :default => nil
24
- end
25
-
26
- execute "alter table locations add constraint fk_location_parent
27
- foreign key(parent_id) references locations(id)"
28
-
29
- execute "alter table locations add constraint fk_location_movement_strategy
30
- foreign key(movement_strategy_id) references movement_strategies(id)"
31
-
32
- execute "alter table locations add constraint root_or_child
33
- check (parent_id IS NULL AND x IS NULL AND y IS NULL AND z IS NULL OR
34
- parent_id IS NOT NULL AND x IS NOT NULL AND y IS NOT NULL AND z IS NOT NULL)"
35
-
36
-
37
- # create the first / default movement strategy 'stopped'
38
- Motel::Models::Stopped.new(:step_delay => 5).save!
39
- end
40
-
41
- def self.down
42
- drop_table :locations
43
- drop_table :movement_strategies
44
- end
45
- end
@@ -1,23 +0,0 @@
1
- # creates the neccessary fields for the Linear MovementStrategy
2
- #
3
- # Copyright (C) 2009 Mohammed Morsi <movitto@yahoo.com>
4
- # See COPYING for the License of this software
5
-
6
- require 'motel'
7
-
8
- # ActiveRecord::Migration 002
9
- class CreateLinearMovementStrategy < ActiveRecord::Migration
10
- def self.up
11
- add_column :movement_strategies, :speed, :float
12
- add_column :movement_strategies, :direction_vector_x, :float
13
- add_column :movement_strategies, :direction_vector_y, :float
14
- add_column :movement_strategies, :direction_vector_z, :float
15
- end
16
-
17
- def self.down
18
- remove_column :movement_strategies, :speed
19
- remove_column :movement_strategies, :direction_vector_x
20
- remove_column :movement_strategies, :direction_vector_y
21
- remove_column :movement_strategies, :direction_vector_z
22
- end
23
- end
@@ -1,52 +0,0 @@
1
- # creates the neccessary fields for the Elliptical MovementStrategy
2
- #
3
- # Copyright (C) 2009 Mohammed Morsi <movitto@yahoo.com>
4
- # See COPYING for the License of this software
5
-
6
- require 'motel'
7
-
8
- # ActiveRecord::Migration 003
9
- class CreateEllipticalMovementStrategy < ActiveRecord::Migration
10
- def self.up
11
- # we already have speed from linear
12
- #add_column :movement_strategies, :speed, :float
13
-
14
- # relative_to field indicates what type of parent
15
- # this movement stategy is relative to, for Ellilptical
16
- # strategies, this can be 'center' indicating the parent
17
- # is the center of the ellipse or 'foci' indicating the
18
- # parent is on of the ellipse foci's
19
- add_column :movement_strategies, :relative_to, :string, :size => 50
20
-
21
- # eccentricity of the ellipse
22
- add_column :movement_strategies, :eccentricity, :float
23
-
24
- # semi_latus_rectum of the ellipse
25
- add_column :movement_strategies, :semi_latus_rectum, :float
26
-
27
- # unit direction vector of the major axis
28
- add_column :movement_strategies, :direction_major_x, :float
29
- add_column :movement_strategies, :direction_major_y, :float
30
- add_column :movement_strategies, :direction_major_z, :float
31
-
32
- # unit direction vector of the minor axis
33
- add_column :movement_strategies, :direction_minor_x, :float
34
- add_column :movement_strategies, :direction_minor_y, :float
35
- add_column :movement_strategies, :direction_minor_z, :float
36
- end
37
-
38
- def self.down
39
- # remove in linear strategy
40
- #remove_column :movement_strategies, :speed
41
-
42
- remove_column :movement_strategies, :relative_to
43
- remove_column :movement_strategies, :eccentricity
44
- remove_column :movement_strategies, :semi_latus_rectum
45
- remove_column :movement_strategies, :direction_major_x
46
- remove_column :movement_strategies, :direction_major_y
47
- remove_column :movement_strategies, :direction_major_z
48
- remove_column :movement_strategies, :direction_minor_x
49
- remove_column :movement_strategies, :direction_minor_y
50
- remove_column :movement_strategies, :direction_minor_z
51
- end
52
- end
data/lib/motel/loader.rb DELETED
@@ -1,47 +0,0 @@
1
- # The Loader class definition
2
- #
3
- # Copyright (C) 2009 Mohammed Morsi <movitto@yahoo.com>
4
- # See COPYING for the License of this software
5
-
6
- require 'motel/runner'
7
-
8
- module Motel
9
-
10
- # A Loader loads instances of Location from the db
11
- # according to any specified conditions, instantiates
12
- # a new Runner for each of those locations and returns them
13
- class Loader
14
-
15
- public
16
-
17
- # Default class constructor
18
- def initialize
19
- end
20
-
21
- # Static member to load all locations that match a specified
22
- # condition (ala activerecord) and add it to the singleton Runner
23
- # instance using it to run the locations.
24
- def self.Load(conditions = 'parent_id IS NULL')
25
- locations = Location.find(:all, :conditions => conditions)
26
- return nil if locations.size == 0
27
-
28
- locations.each { |location|
29
- run_location(location)
30
- }
31
- return locations.size
32
- end
33
-
34
- private
35
-
36
- # Static internal helper method that adds a location and
37
- # all its children to the Runner
38
- def self.run_location(location)
39
- Runner.get.run location
40
- location.children.each { |child|
41
- run_location child
42
- }
43
- end
44
-
45
- end
46
-
47
- end
@@ -1,76 +0,0 @@
1
- # The Linear MovementStrategy model definition
2
- #
3
- # Copyright (C) 2009 Mohammed Morsi <movitto@yahoo.com>
4
- # See COPYING for the License of this software
5
-
6
- require 'motel/common'
7
- require 'motel/models/movement_strategy'
8
-
9
- module Motel
10
- module Models
11
-
12
- # The Linear MovementStrategy moves a location
13
- # in a linear manner as defined by a
14
- # unit direction vector and a floating point
15
- # speed
16
- class Linear < MovementStrategy
17
-
18
- # Linear MovementStrategy must specify x,y,z components of
19
- # a unit direction vector
20
- validates_presence_of [:direction_vector_x,
21
- :direction_vector_y,
22
- :direction_vector_z]
23
-
24
- # make sure the unit direction vector is normal
25
- before_validation :normalize_direction_vector
26
- def normalize_direction_vector
27
- dx, dy, dz =
28
- normalize(direction_vector_x, direction_vector_y, direction_vector_z)
29
- self.direction_vector_x, self.direction_vector_y, self.direction_vector_z = dx, dy, dz
30
- end
31
-
32
-
33
- # Linear MovementStrategy must specify the speed
34
- # at which the location is moving
35
- validates_presence_of :speed
36
- validates_numericality_of :speed,
37
- :greater_than_or_equal_to => 0
38
-
39
- # Motel::Models::MovementStrategy::move
40
- def move(location, elapsed_seconds)
41
- unless valid?
42
- Logger.warn "linear movement strategy not valid, not proceeding with move"
43
- return
44
- end
45
-
46
- Logger.debug "moving location #{location.to_s} via linear movement strategy"
47
-
48
- # calculate distance and update x,y,z accordingly
49
- distance = speed * elapsed_seconds
50
-
51
- location.x += distance * direction_vector_x
52
- location.y += distance * direction_vector_y
53
- location.z += distance * direction_vector_z
54
-
55
- Logger.debug "moved location #{location} via linear movement strategy"
56
- end
57
-
58
- # convert non-nil linear movement strategy attributes to a hash
59
- def to_h
60
- result = {}
61
- result[:speed] = speed unless speed.nil?
62
- result[:direction_vector_x] = direction_vector_x unless direction_vector_x.nil?
63
- result[:direction_vector_y] = direction_vector_y unless direction_vector_y.nil?
64
- result[:direction_vector_z] = direction_vector_z unless direction_vector_z.nil?
65
- return result
66
- end
67
-
68
- # convert linear movement strategy to a string
69
- def to_s
70
- super + "; speed: #{speed}; direction_vector_x:#{direction_vector_x}; " +
71
- "direction_vector_y:#{direction_vector_y}; direction_vector_z:#{direction_vector_z}"
72
- end
73
- end
74
-
75
- end # module Models
76
- end # module Motel
@@ -1,109 +0,0 @@
1
- # The Location model definition
2
- #
3
- # Copyright (C) 2009 Mohammed Morsi <movitto@yahoo.com>
4
- # See COPYING for the License of this software
5
-
6
- require 'motel/models/movement_strategy'
7
-
8
- module Motel
9
- module Models
10
-
11
- # A Location defines an optional parent location and the x,y,z
12
- # cartesian coordinates of the location relative to that parent.
13
- # If parent is not specified x,y,z are ignored and this location
14
- # is assumed to be the 'center' of the system to which it belongs
15
- # Also is related to a movable object defining the parameters
16
- # of the locations movement
17
- class Location < ActiveRecord::Base
18
- # a location may have a parent and/or act as a parent to others
19
- belongs_to :location, :foreign_key => :parent_id
20
- has_many :locations, :foreign_key => :parent_id, :dependent => :destroy
21
-
22
- belongs_to :movement_strategy
23
-
24
- # a generic association which this location can belong to
25
- belongs_to :entity, :polymorphic => true
26
- #validates_presense_of [ :entity_type, :entity_id ]
27
-
28
- # FIXME add optional remote_location_server field
29
-
30
- alias :parent :location
31
- alias :parent= :location=
32
- alias :children :locations
33
- alias :children= :locations=
34
-
35
- # return this location's root location
36
- def root
37
- return self if parent.nil?
38
- return parent.root
39
- end
40
-
41
- # traverse all chilren recursively, calling block for each
42
- def traverse_descendants(&bl)
43
- children.each { |child|
44
- bl.call child
45
- child.traverse_descendants &bl
46
- }
47
- end
48
-
49
- # default to the stopped movement strategy if not set on validation
50
- before_validation :default_movement_strategy
51
- def default_movement_strategy
52
- self.movement_strategy = MovementStrategy.stopped if self.movement_strategy.nil?
53
- end
54
-
55
- public
56
- validates_presence_of :movement_strategy
57
-
58
- validates_presence_of [:x, :y, :z],
59
- :unless => Proc.new { |location| location.location.nil? }
60
-
61
- validates_presence_of :location,
62
- :unless => Proc.new { |location| location.x.nil? &&
63
- location.y.nil? &&
64
- location.z.nil? }
65
-
66
- public
67
-
68
- # return non-nil attributes in hash
69
- def to_h
70
- result = {}
71
- #result[:id] = id unless id.nil?
72
- result[:parent_id] = parent_id unless parent_id.nil?
73
- result[:x] = x unless x.nil?
74
- result[:y] = y unless y.nil?
75
- result[:z] = z unless z.nil?
76
- return result
77
- end
78
-
79
- # convert location to a string
80
- def to_s
81
- "id:#{id}; parent_id:#{parent_id}; parent: #{parent.nil? ? "nil" : "notnil"}; entity type: #{entity_type}; x:#{x}; y:#{y}; z:#{z}; " +
82
- "movement_strategy:#{movement_strategy.to_s}; children:#{locations.size}"
83
- end
84
-
85
- # return sum of the x values of this location and all its parents,
86
- # eg the absolute 'x' of the location
87
- def total_x
88
- return 0 if location.nil?
89
- return location.total_x + x
90
- end
91
-
92
- # return sum of the y values of this location and all its parents
93
- # eg the absolute 'y' of the location
94
- def total_y
95
- return 0 if location.nil?
96
- return location.total_y + y
97
- end
98
-
99
- # return sum of the z values of this location and all its parents
100
- # eg the absolute 'z' of the location
101
- def total_z
102
- return 0 if location.nil?
103
- return location.total_z + z
104
- end
105
-
106
- end
107
-
108
- end # module Models
109
- end # module Motel
@@ -1,82 +0,0 @@
1
- # The MovementStrategy model definition
2
- #
3
- # Copyright (C) 2009 Mohammed Morsi <movitto@yahoo.com>
4
- # See COPYING for the License of this software
5
-
6
- require 'motel/common'
7
- require 'motel/models/location'
8
-
9
- module Motel
10
- module Models
11
-
12
- # MovementStrategy subclasses define the rules and params which
13
- # a location changes its position.
14
- class MovementStrategy < ActiveRecord::Base
15
- # strategy is associated with location to move it
16
- has_many :locations, :dependent => :destroy
17
-
18
- # callbacks invoked when this object is moved
19
- attr_accessor :movement_callbacks
20
-
21
- # every movement strategy needs a type
22
- validates_presence_of :type
23
-
24
- # as more types are supported, add them here
25
- validates_inclusion_of :type,
26
- :in => %w( Stopped Linear Elliptical )
27
-
28
- # step delay is recommended number of seconds
29
- # a runner should sleep for between move invocations
30
- validates_presence_of :step_delay
31
-
32
- # default step_delay if not set
33
- before_validation :default_step_delay
34
- def default_step_delay
35
- self.step_delay = 5 if step_delay.nil?
36
- end
37
-
38
- # use after_initialize instead of initialize
39
- # http://blog.dalethatcher.com/2008/03/rails-dont-override-initialize-on.html
40
- def after_initialize
41
- @movement_callbacks = []
42
- end
43
-
44
- # default movement strategy is to do nothing
45
- def move(location, elapsed_seconds)
46
- end
47
-
48
- # retreive the 'stopped' movement strategy
49
- def self.stopped
50
- ms = MovementStrategy.find(:first, :conditions => "type = 'Stopped'")
51
- ms.nil? ? Stopped.new(:step_delay => 5) : ms
52
- end
53
-
54
- # return subclass corresponding to specified strategy type
55
- def self.factory(strategy_type)
56
- return MovementStrategy if strategy_type.nil?
57
-
58
- if strategy_type.downcase == "stopped" || strategy_type == "Motel::Models::Stopped"
59
- return Stopped
60
- elsif strategy_type.downcase == "linear" || strategy_type == "Motel::Models::Linear"
61
- return Linear
62
- elsif strategy_type.downcase == "elliptical" || strategy_type == "Motel::Models::Elliptical"
63
- return Elliptical
64
- end
65
-
66
- return MovementStrategy
67
- end
68
-
69
- # convert movement strategy to a hash
70
- def to_h
71
- {}
72
- end
73
-
74
- # convert movement strategy to a string
75
- def to_s
76
- "id:#{id}; type:#{self.class}" # XXX should be self.type but ruby will complain, this works for now
77
- end
78
-
79
- end
80
-
81
- end # module Models
82
- end # module Motel
@@ -1,16 +0,0 @@
1
- # The Stopped MovementStrategy model definition
2
- #
3
- # Copyright (C) 2009 Mohammed Morsi <movitto@yahoo.com>
4
- # See COPYING for the License of this software
5
-
6
- require 'motel/models/movement_strategy'
7
-
8
- module Motel
9
- module Models
10
-
11
- # Stopped is the default MovementStrategy which does nothing
12
- class Stopped < MovementStrategy
13
- end
14
-
15
- end # module Models
16
- end # module Motel