00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00026 package comedyarch;
00027
00028 import java.util.Map;
00029
00030 import cast.AlreadyExistsOnWMException;
00031 import cast.DoesNotExistOnWMException;
00032 import cast.PermissionException;
00033 import cast.SubarchitectureComponentException;
00034 import cast.UnknownSubarchitectureException;
00035 import cast.architecture.ChangeFilterFactory;
00036 import cast.architecture.ManagedComponent;
00037 import cast.architecture.WorkingMemoryChangeReceiver;
00038 import cast.cdl.WorkingMemoryAddress;
00039 import cast.cdl.WorkingMemoryChange;
00040 import cast.cdl.WorkingMemoryOperation;
00041
00042 import comedyarch.autogen.DirectorAction;
00043 import comedyarch.autogen.DirectorActionType;
00044 import comedyarch.autogen.TwoLiner;
00045 import comedyarch.autogen.Reaction;
00046
00050 public class Director extends ManagedComponent {
00051
00052
00053 private String m_audienceSA;
00054
00055
00056
00057
00058
00059
00060 @Override
00061 public void start() {
00062 addChangeFilter(ChangeFilterFactory.createLocalTypeFilter(
00063 DirectorAction.class, WorkingMemoryOperation.ADD),
00064 new WorkingMemoryChangeReceiver() {
00065
00066 public void workingMemoryChanged(WorkingMemoryChange _wmc) {
00067
00068 takeAction(_wmc);
00069 }
00070 });
00071 }
00072
00076 private void takeAction(WorkingMemoryChange _actionChange) {
00077
00078 try {
00079 DirectorAction action = getMemoryEntry(_actionChange.address,
00080 DirectorAction.class);
00081 switch (action.action) {
00082 case AskTheAudience:
00083 askTheAudience(action.address);
00084 break;
00085 case CheckTheReaction:
00086 checkTheReaction(action.address);
00087 break;
00088 default:
00089 log("Unknown action type");
00090 break;
00091 }
00092
00093
00094
00095 deleteFromWorkingMemory(_actionChange.address);
00096 } catch (SubarchitectureComponentException e) {
00097 e.printStackTrace();
00098 }
00099 }
00100
00108 private void checkTheReaction(WorkingMemoryAddress _reactionAddress)
00109 throws DoesNotExistOnWMException, PermissionException, UnknownSubarchitectureException {
00110
00111
00112 String reaction = getMemoryEntry(_reactionAddress, Reaction.class).react;
00113
00114 println("and the audience says...");
00115 println(reaction);
00116
00117
00118 deleteFromWorkingMemory(_reactionAddress);
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 }
00129
00137 private void askTheAudience(WorkingMemoryAddress _jokeAddress)
00138 throws DoesNotExistOnWMException, PermissionException,
00139 AlreadyExistsOnWMException, UnknownSubarchitectureException {
00140
00141
00142
00143 println("someone told a joke! ");
00144
00145
00146 TwoLiner joke = getMemoryEntry(_jokeAddress, TwoLiner.class);
00147
00148 println("let's see what the audience thinks of...");
00149 println("Q: " + joke.setup);
00150 println("A: " + joke.punchline);
00151
00152
00153 addToWorkingMemory(newDataID(), m_audienceSA, joke);
00154
00155
00156 deleteFromWorkingMemory(_jokeAddress);
00157 }
00158
00159 @Override
00160 public void configure(Map<String, String> _config) {
00161 String asa = _config.get("--audience");
00162 if (asa != null) {
00163 m_audienceSA = asa;
00164 } else {
00165 throw new RuntimeException(
00166 "Missing --audience parameter for audience subarch address");
00167 }
00168 }
00169
00170 }