musician.erl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. % Autogenerated with DRAKON Editor 1.26
  2. -module(musician).
  3. -export([code_change/3, handle_call/3, handle_cast/2, handle_info/2, init/1, run/0, start_link/2, stop/1, terminate/2]).
  4. -behaviour(gen_server).
  5. -record(state, {name="", role, skill=good}).
  6. -define(DELAY, 750).
  7. code_change(_OldVsn, State, _Extra) ->
  8. % item 75
  9. {ok, State}
  10. .
  11. first_names() ->
  12. % item 11
  13. ["Valerie", "Arnold", "Carlos", "Dorothy", "Keesha",
  14. "Phoebe", "Ralphie", "Tim", "Wanda", "Janet"]
  15. .
  16. handle_call(Message, _From, State) ->
  17. % item 38
  18. case Message =:= stop of true ->
  19. % item 41
  20. {stop, normal, ok, State}
  21. ; false ->
  22. % item 43
  23. {noreply, State, ?DELAY}
  24. end
  25. .
  26. handle_cast(_Message, State) ->
  27. % item 49
  28. {noreply, State, ?DELAY}
  29. .
  30. handle_info(timeout, State) ->
  31. % item 55
  32. #state {
  33. name = Name,
  34. skill = Skill
  35. } = State,
  36. case Skill of
  37. good ->
  38. % item 62
  39. io:format("~s produced sound!~n",[Name]),
  40. % item 68
  41. {noreply, State, ?DELAY}
  42. ;
  43. bad ->
  44. % item 63
  45. case random:uniform(5) =:= 1 of true ->
  46. % item 66
  47. io:format(
  48. "~s played a false note. Uh oh~n",
  49. [Name]
  50. ),
  51. % item 69
  52. {stop, bad_note, State}
  53. ; false ->
  54. % item 62
  55. io:format("~s produced sound!~n",[Name]),
  56. % item 68
  57. {noreply, State, ?DELAY}
  58. end
  59. ;
  60. _ ->
  61. throw("Unexpected switch value")
  62. end
  63. .
  64. init([Role, Skill]) ->
  65. % item 96
  66. process_flag(trap_exit, true),
  67. % item 6
  68. random:seed(now()),
  69. % item 29
  70. TimeToPlay = random:uniform(3000),
  71. Name = pick_name(),
  72. % item 30
  73. io:format(
  74. "Musician ~s, playing the ~s entered the room~n",
  75. [Name, Role]
  76. ),
  77. % item 31
  78. State = #state {
  79. name = Name,
  80. role = Role,
  81. skill = Skill
  82. },
  83. % item 32
  84. {ok, State, TimeToPlay}
  85. .
  86. last_names() ->
  87. % item 16
  88. ["Frizzle", "Perlstein", "Ramon", "Ann", "Franklin",
  89. "Terese", "Tennelli", "Jamal", "Li", "Perlstein"]
  90. .
  91. pick_name() ->
  92. % item 21
  93. First = random_element(first_names()),
  94. Last = random_element(last_names()),
  95. % item 42
  96. First ++ " " ++ Last
  97. .
  98. random_element(List) ->
  99. % item 27
  100. Length = length(List),
  101. Index = random:uniform(Length),
  102. % item 28
  103. lists:nth(Index, List)
  104. .
  105. run() ->
  106. % item 114
  107. start_link(bass, bad),
  108. % item 116
  109. start_link(cello, good),
  110. % item 115
  111. timer:sleep(10000),
  112. % item 117
  113. stop(cello)
  114. .
  115. start_link(Role, Skill) ->
  116. % item 102
  117. gen_server:start_link(
  118. {local, Role},
  119. ?MODULE,
  120. [Role, Skill],
  121. []
  122. )
  123. .
  124. stop(Role) ->
  125. % item 108
  126. gen_server:call(Role, stop)
  127. .
  128. terminate(Reason, State) ->
  129. % item 91
  130. #state {
  131. name = Name,
  132. role = Role
  133. } = State,
  134. case Reason of
  135. normal ->
  136. % item 92
  137. io:format(
  138. "~s left the room (~s)~n",
  139. [Name, Role]
  140. )
  141. ;
  142. bad_note ->
  143. % item 93
  144. io:format(
  145. "~s sucks! kicked that member out of the band! (~s)~n",
  146. [Name, Role]
  147. )
  148. ;
  149. shutdown ->
  150. % item 94
  151. io:format(
  152. "The manager is mad and fired the whole band! "
  153. ++ "~s just got back to playing in the subway~n",
  154. [Name]
  155. )
  156. ;
  157. _ ->
  158. % item 95
  159. io:format(
  160. "~s has been kicked out (~s)",
  161. [Name, Role]
  162. )
  163. end
  164. .