ceml 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.5.0
data/ceml.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ceml}
8
- s.version = "0.4.0"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Joe Edelman"]
12
- s.date = %q{2011-01-09}
12
+ s.date = %q{2011-01-17}
13
13
  s.description = %q{a language for coordinating real world events}
14
14
  s.email = %q{joe@citizenlogistics.com}
15
15
  s.extra_rdoc_files = [
@@ -31,11 +31,10 @@ Gem::Specification.new do |s|
31
31
  "examples/breakfast-exchange.ceml",
32
32
  "examples/citizen-investigation.ceml",
33
33
  "examples/high-fives.ceml",
34
- "examples/sample_delegate.rb",
35
34
  "lib/ceml.rb",
36
35
  "lib/ceml/casting_criterion.rb",
37
36
  "lib/ceml/casting_statement.rb",
38
- "lib/ceml/delegate.rb",
37
+ "lib/ceml/driver.rb",
39
38
  "lib/ceml/incident.rb",
40
39
  "lib/ceml/instruction_statements.rb",
41
40
  "lib/ceml/script.rb",
@@ -60,8 +59,7 @@ Gem::Specification.new do |s|
60
59
  "test/test_casting.rb",
61
60
  "test/test_incident.rb",
62
61
  "test/test_instructions.rb",
63
- "test/test_scripts.rb",
64
- "examples/sample_delegate.rb"
62
+ "test/test_scripts.rb"
65
63
  ]
66
64
 
67
65
  if s.respond_to? :specification_version then
@@ -0,0 +1,58 @@
1
+ require 'set'
2
+
3
+ module Kernel
4
+ def gen_code(size = 8)
5
+ rand(36**size).to_s(36)
6
+ end
7
+ end
8
+
9
+ module CEML
10
+ class Driver
11
+ LOCATIONS = {}
12
+ PLAYERS = {}
13
+ INCIDENTS = {}
14
+ JUST_SAID = {}
15
+
16
+ def start(script)
17
+ x = CEML::Incident.new script
18
+ iid = x.id
19
+ INCIDENTS[iid] = x
20
+ PLAYERS[iid] = []
21
+ iid
22
+ end
23
+
24
+ def run(incident_id)
25
+ INCIDENTS[incident_id].run(PLAYERS[incident_id]) do |player, meth, what|
26
+ case meth
27
+ when :said
28
+ JUST_SAID[player[:id]] = what
29
+ puts "Said #{what.inspect}"
30
+ end
31
+ end
32
+ end
33
+
34
+ def post incident_id, player
35
+ player_id = player[:id]
36
+ player[:roles] = Set.new([*player[:roles]]) if player[:roles]
37
+ if existing_player = PLAYERS[incident_id].find{ |p| p[:id] == player_id }
38
+ existing_player.update player
39
+ else
40
+ PLAYERS[incident_id] << player
41
+ end
42
+ run incident_id
43
+ end
44
+
45
+ # yields a thing with #each and #<<
46
+ def ping script, candidate
47
+ LOCATIONS[script] ||= []
48
+ script.post candidate, LOCATIONS[script]
49
+ LOCATIONS[script].delete_if do |loc|
50
+ next unless loc.cast
51
+ iid = start(script)
52
+ loc.cast.each do |k, v|
53
+ post iid, :id => k, :roles => v
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
data/lib/ceml/incident.rb CHANGED
@@ -2,40 +2,37 @@ require 'set'
2
2
 
3
3
  module CEML
4
4
  class Incident
5
- attr_reader :script, :id, :players
6
- def this; @players[@current_id]; end
5
+ attr_reader :script, :id, :this
7
6
  def roles; this[:roles] ||= Set.new; end
8
7
  def got; this[:received]; end
9
8
  def recognized; this[:recognized]; end
10
9
  def pc; this[:pc] ||= 0; end
11
10
  def qs_answers; this[:qs_answers] ||= Hash.new; end
12
11
 
13
- def initialize(script, cast = {}, id = rand(36**10).to_s(36))
12
+ def handled!
13
+ this.delete :received
14
+ this.delete :recognized
15
+ end
16
+
17
+ def initialize(script, id = rand(36**10).to_s(36))
14
18
  @id = id
15
19
  @script = Script === script ? script : CEML.parse(:script, script)
16
- run do
17
- cast.each{ |guy,role| add guy, role }
18
- end
19
20
  end
20
21
 
21
- def add(id, *roles)
22
- obj = Hash === roles[-1] ? roles.pop : {}
23
- @players[id] = obj.merge :roles => Set.new(roles)
22
+ def cb *stuff
23
+ @callback.call this, *stuff if @callback
24
24
  end
25
25
 
26
- def run
27
- CEML.delegate.with_players(@id) do |players|
28
- @players = players
29
- yield self if block_given?
30
- :loop while players.keys.any? do |@current_id|
31
- # puts "seq for roles: #{roles.inspect} #{seq.inspect}"
32
- # puts "trying: #{@current_id}: #{seq[pc]}"
33
- next unless seq[pc] and send(*seq[pc])
34
- CEML.delegate.send(*seq[pc] + [@iid, @current_id])
35
- this[:pc]+=1
36
- end
37
- @players = nil
26
+ def run(players, &blk)
27
+ @players = players
28
+ @callback = blk
29
+ :loop while players.any? do |@this|
30
+ # puts "seq for roles: #{roles.inspect} #{seq.inspect}"
31
+ next unless seq[pc] and send(*seq[pc])
32
+ cb(*seq[pc])
33
+ this[:pc]+=1
38
34
  end
35
+ @callback = @players = nil
39
36
  end
40
37
 
41
38
  def seq
@@ -65,18 +62,13 @@ module CEML
65
62
  end
66
63
  end
67
64
 
68
- def say x, params = {}
69
- this[:said] = x
70
- this.merge! params
71
- end
72
-
73
65
  def expand(role, var)
74
66
  role = nil if role == 'otherguy'
75
67
  role = role.to_sym if role
76
- @players.each do |key, thing|
77
- next if key == @current_id
78
- next if role and not thing[:roles].include? role
79
- value = (thing[:qs_answers]||{})[var] and return value
68
+ @players.each do |p|
69
+ next if p == this
70
+ next if role and not p[:roles].include? role
71
+ value = (p[:qs_answers]||{})[var] and return value
80
72
  end
81
73
  nil
82
74
  end
@@ -85,6 +77,10 @@ module CEML
85
77
  # = basic flow =
86
78
  # ==============
87
79
 
80
+ def say x, params = {}
81
+ cb :said, params.merge(:said => x)
82
+ end
83
+
88
84
  def start
89
85
  # roles.include? :agent or return false
90
86
  true
@@ -99,6 +95,7 @@ module CEML
99
95
  def answered_q q
100
96
  got or return false
101
97
  qs_answers[q.key] = got
98
+ handled!
102
99
  true
103
100
  end
104
101
 
@@ -118,9 +115,11 @@ module CEML
118
115
  got or return false
119
116
  if recognized == :done
120
117
  say :ok
118
+ handled!
121
119
  true
122
120
  else
123
- CEML.delegate.send :did_report
121
+ cb :did_report
122
+ handled!
124
123
  false
125
124
  end
126
125
  end
data/lib/ceml/script.rb CHANGED
@@ -9,12 +9,10 @@ module CEML
9
9
  # ===========
10
10
 
11
11
  # public
12
- def post candidate
12
+ def post candidate, locs
13
13
  return unless candidate = candidate.dup.load(awaited_criteria)
14
- delegate.with_locations(self) do |locs|
15
- locs.each{ |l| l.push candidate }
16
- locs << CastingLocation.create(self, candidate) if locs.none?(&:added)
17
- end
14
+ locs.each{ |l| l.push candidate }
15
+ locs << CastingLocation.create(self, candidate) if locs.none?(&:added)
18
16
  end
19
17
 
20
18
  def awaited_criteria
data/lib/ceml.rb CHANGED
@@ -9,14 +9,14 @@ require 'ceml/tt/casting'
9
9
  require 'ceml/tt/instructions'
10
10
  require 'ceml/tt/scripts'
11
11
 
12
- require 'ceml/delegate'
13
12
  require 'ceml/casting_criterion'
14
13
  require 'ceml/incident'
14
+ require 'ceml/driver'
15
15
 
16
16
  module CEML
17
17
  extend self
18
18
  attr_accessor :delegate
19
- @delegate = Delegate.new
19
+ # @delegate = Delegate.new
20
20
  end
21
21
 
22
22
  module CEML
data/test/helper.rb CHANGED
@@ -6,47 +6,40 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
6
  require 'ceml'
7
7
 
8
8
  class Test::Unit::TestCase
9
+ DRIVER = CEML::Driver.new
10
+
9
11
  def play script = nil
10
- @e = CEML::Incident.new(script) if script
12
+ @iid = script && DRIVER.start(script)
11
13
  yield
12
- CEML::Delegate::PLAYERS.clear
14
+ CEML::Driver::JUST_SAID.clear
15
+ end
16
+
17
+ def ping s, candidate
18
+ DRIVER.ping s, candidate
19
+ end
20
+
21
+ def says id, str
22
+ DRIVER.post @iid, :id => id, :received => str
13
23
  end
14
24
 
15
25
  def player id, *roles
16
- @e.run do |script|
17
- script.add id, *roles
18
- end
26
+ DRIVER.post @iid, :id => id, :roles => roles
19
27
  end
20
28
 
21
29
  def asked id, rx
22
- assert g = CEML::Delegate::PLAYERS.values.find{ |game| game[id] }
23
- p = g[id]
30
+ assert p = CEML::Driver::JUST_SAID[id]
24
31
  assert_equal :ask, p[:said]
25
32
  assert_match rx, p[:q]
26
- p.delete :said
33
+ CEML::Driver::JUST_SAID.delete id
27
34
  end
28
35
 
29
36
  def silent id
30
- if g = CEML::Delegate::PLAYERS.values.find{ |game| game[id] }
31
- p = g[id]
32
- assert !p[:msg]
33
- end
37
+ assert !CEML::Driver::JUST_SAID[id]
34
38
  end
35
39
 
36
40
  def told id, rx
37
- assert g = CEML::Delegate::PLAYERS.values.find{ |game| game[id] }
38
- p = g[id]
41
+ assert p = CEML::Driver::JUST_SAID[id]
39
42
  assert_match rx, p[:msg]
40
- p.delete :said
41
- end
42
-
43
- def says id, str
44
- @e.run do |incident|
45
- incident.players[id][:received] = str
46
- end
47
-
48
- CEML.delegate.with_players(@e.id) do |players|
49
- players[id][:received] = nil
50
- end
43
+ CEML::Driver::JUST_SAID.delete id
51
44
  end
52
45
  end
data/test/test_casting.rb CHANGED
@@ -71,7 +71,7 @@ class TestCasting < Test::Unit::TestCase
71
71
 
72
72
  def test_radius
73
73
  assert_equal 600, pcs("gather 3 runners and 5 hot_babes within 3 blocks").radius
74
- assert pcs("gather 3 runners and 5 hot_babes").radius > 50000
74
+ assert !pcs("gather 3 runners and 5 hot_babes").radius
75
75
  end
76
76
 
77
77
  end
@@ -8,22 +8,12 @@ ask organizer re target: Describe their appearance and location
8
8
  tell agents: Look for |otherguy.target| and compliment them briefly, then move on.
9
9
  XXX
10
10
 
11
- ASKCHAIN_SCRIPT = <<XXX
12
- "Meet your neighbor"
13
- gather 2 players within 1 block
14
- ask players re color: what's your favorite color?
15
- ask players re observation: find someone near you with the color |otherguy.color|. what are they wearing?
16
- ask players re rightmatch: are you wearing |otherguy.observation|?
17
- ask players re task: take your new partner and see if you can find something beautiful in the park.
18
- XXX
19
-
20
-
21
11
  class TestIncident < Test::Unit::TestCase
22
12
 
23
13
  def test_signup_1
24
14
  s = CEML.parse(:script, "await 1 new signup\ntell signup: thanks")
25
15
  play do
26
- s.post CEML::Candidate.new('fred', ['new'], {})
16
+ ping s, CEML::Candidate.new('fred', ['new'], {})
27
17
  told 'fred', /thanks/
28
18
  end
29
19
  end
@@ -31,11 +21,11 @@ class TestIncident < Test::Unit::TestCase
31
21
  def test_signup_2
32
22
  s = CEML.parse(:script, "await 2 new signups\ntell signups: thanks")
33
23
  play do
34
- s.post CEML::Candidate.new('fred', ['new'], {})
24
+ ping s, CEML::Candidate.new('fred', ['new'], {})
35
25
  silent 'fred'
36
- s.post CEML::Candidate.new('wilma', ['old'], {})
26
+ ping s, CEML::Candidate.new('wilma', ['old'], {})
37
27
  silent 'fred'
38
- s.post CEML::Candidate.new('betty', ['new'], {})
28
+ ping s, CEML::Candidate.new('betty', ['new'], {})
39
29
  told 'fred', /thanks/
40
30
  end
41
31
  end
@@ -43,12 +33,12 @@ class TestIncident < Test::Unit::TestCase
43
33
  def test_await
44
34
  s = CEML.parse(:script, "await a,b,c\ntell a: foo\ntell b: bar\ntell c: baz")
45
35
  play do
46
- s.post CEML::Candidate.new('fred', [], {})
36
+ ping s, CEML::Candidate.new('fred', [], {})
47
37
  silent 'fred'
48
- s.post CEML::Candidate.new('wilma', [], {})
38
+ ping s, CEML::Candidate.new('wilma', [], {})
49
39
  silent 'fred'
50
40
  silent 'wilma'
51
- s.post CEML::Candidate.new('betty', [], {})
41
+ ping s, CEML::Candidate.new('betty', [], {})
52
42
  told 'fred', /foo/
53
43
  told 'betty', /baz/
54
44
  end
@@ -66,6 +56,16 @@ class TestIncident < Test::Unit::TestCase
66
56
  end
67
57
  end
68
58
 
59
+ ASKCHAIN_SCRIPT = <<XXX
60
+ "Meet your neighbor"
61
+ gather 2 players within 1 block
62
+ ask players re color: what's your favorite color?
63
+ ask players re observation: find someone near you with the color |otherguy.color|. what are they wearing?
64
+ ask players re rightmatch: are you wearing |otherguy.observation|?
65
+ ask players re task: take your new partner and see if you can find something beautiful in the park.
66
+ XXX
67
+
68
+
69
69
  def test_askchain
70
70
  play ASKCHAIN_SCRIPT do
71
71
  player :joe, :players, :agent
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 4
7
+ - 5
8
8
  - 0
9
- version: 0.4.0
9
+ version: 0.5.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Joe Edelman
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-01-09 00:00:00 -08:00
17
+ date: 2011-01-17 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -53,11 +53,10 @@ files:
53
53
  - examples/breakfast-exchange.ceml
54
54
  - examples/citizen-investigation.ceml
55
55
  - examples/high-fives.ceml
56
- - examples/sample_delegate.rb
57
56
  - lib/ceml.rb
58
57
  - lib/ceml/casting_criterion.rb
59
58
  - lib/ceml/casting_statement.rb
60
- - lib/ceml/delegate.rb
59
+ - lib/ceml/driver.rb
61
60
  - lib/ceml/incident.rb
62
61
  - lib/ceml/instruction_statements.rb
63
62
  - lib/ceml/script.rb
@@ -107,4 +106,3 @@ test_files:
107
106
  - test/test_incident.rb
108
107
  - test/test_instructions.rb
109
108
  - test/test_scripts.rb
110
- - examples/sample_delegate.rb
@@ -1,4 +0,0 @@
1
- class CEMLDelegate
2
- def locations(script)
3
- end
4
- end
data/lib/ceml/delegate.rb DELETED
@@ -1,28 +0,0 @@
1
- module Kernel
2
- def gen_code(size = 8)
3
- rand(36**size).to_s(36)
4
- end
5
- end
6
-
7
- module CEML
8
- class Delegate
9
- LOCATIONS = {}
10
- PLAYERS = {}
11
-
12
- # yields thing w. #keys, #each, #[], #[]=
13
- def with_players id
14
- yield PLAYERS[id] ||= {}
15
- end
16
-
17
- # yields a thing with #each and #<<
18
- def with_locations script
19
- yield LOCATIONS[script] ||= []
20
- LOCATIONS[script].delete_if{ |loc| loc.cast and Incident.new(script, loc.cast).run }
21
- end
22
-
23
- def method_missing(meth, *args, &blk)
24
- puts "#{meth}: #{args.map(&:to_s).join(', ')}"
25
- end
26
-
27
- end
28
- end