00001 /* * Comedian example code to demonstrate CAST functionality. Copyright 00002 * (C) 2006-2007 Nick Hawes This library is free software; you can 00003 * redistribute it and/or modify it under the terms of the GNU Lesser 00004 * General Public License as published by the Free Software Foundation; 00005 * either version 2.1 of the License, or (at your option) any later 00006 * version. This library is distributed in the hope that it will be 00007 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 00008 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00009 * Lesser General Public License for more details. You should have 00010 * received a copy of the GNU Lesser General Public License along with 00011 * this library; if not, write to the Free Software Foundation, Inc., 51 00012 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00013 */ 00014 00015 package comedyarch; 00016 00017 import cast.DoesNotExistOnWMException; 00018 import cast.UnknownSubarchitectureException; 00019 import cast.WMException; 00020 import cast.architecture.ChangeFilterFactory; 00021 import cast.architecture.ManagedComponent; 00022 import cast.architecture.WorkingMemoryChangeReceiver; 00023 import cast.cdl.WorkingMemoryChange; 00024 import cast.cdl.WorkingMemoryOperation; 00025 import cast.core.CASTData; 00026 00027 import comedyarch.autogen.TwoLiner; 00028 00032 public class FunnyMan extends ManagedComponent { 00033 00034 /* 00035 * (non-Javadoc) 00036 * 00037 * @see cast.architecture.abstr.WorkingMemoryReaderProcess#start() 00038 */ 00039 @Override 00040 public void start() { 00041 00042 // create a new receiver object that just calls a member 00043 // function from this class 00044 WorkingMemoryChangeReceiver receiver = new WorkingMemoryChangeReceiver() { 00045 public void workingMemoryChanged(WorkingMemoryChange _wmc) { 00046 newTwoLinerAdded(_wmc); 00047 } 00048 }; 00049 00050 // add this receiver to listen for changes 00051 addChangeFilter( 00052 // listen for joke types that are 00053 // added that are local to this 00054 // subarchitecture 00055 ChangeFilterFactory.createLocalTypeFilter(TwoLiner.class, 00056 WorkingMemoryOperation.ADD), receiver); 00057 00058 // examples to demonstrate matching on supertypes 00059 00060 // addChangeFilter( 00061 // // listen for joke types that are 00062 // // added that are local to this 00063 // // subarchitecture 00064 // ChangeFilterFactory.createLocalTypeFilter(Joke.class, 00065 // WorkingMemoryOperation.ADD), 00066 // new WorkingMemoryChangeReceiver() { 00067 // public void workingMemoryChanged(WorkingMemoryChange _wmc) { 00068 // try { 00069 // Joke jk = getMemoryEntry(_wmc.address.id, Joke.class); 00074 // 00075 // } catch (DoesNotExistOnWMException e) { 00076 // e.printStackTrace(); 00077 // } 00078 // } 00079 // }); 00080 00081 00082 00083 // addChangeFilter( 00084 // // listen for joke types that are 00085 // // added that are local to this 00086 // // subarchitecture 00087 // ChangeFilterFactory.createLocalTypeFilter(OneLiner.class, 00088 // WorkingMemoryOperation.ADD), 00089 // new WorkingMemoryChangeReceiver() { 00090 // public void workingMemoryChanged(WorkingMemoryChange _wmc) { 00091 // try { 00092 // OneLiner jk = getMemoryEntry(_wmc.address.id, OneLiner.class); 00093 // println("found a oneliner"); 00094 // } catch (DoesNotExistOnWMException e) { 00095 // e.printStackTrace(); 00096 // } 00097 // } 00098 // }); 00099 00100 } 00101 00102 private String generatePunchline(String _setup) { 00103 return "A stick!"; 00104 } 00105 00109 private void quip(CASTData<TwoLiner> _data) { 00110 00111 TwoLiner jk = (TwoLiner) _data.getData(); 00112 // work out a smarty-pants punchline 00113 String punchline = generatePunchline(jk.setup); 00114 // time it right 00115 log("*cough*"); 00116 jk.punchline = punchline; 00117 // now write this back into working memory 00118 try { 00119 overwriteWorkingMemory(_data.getID(), jk); 00120 } catch (WMException e) { 00121 e.printStackTrace(); 00122 } 00123 00124 } 00125 00130 private void newTwoLinerAdded(WorkingMemoryChange _wmc) { 00131 00132 // get the data from working memory and store it 00133 // with its id 00134 try { 00135 00136 // System.out.println("FunnyMan.newTwoLinerAdded()"); 00137 00138 CASTData<TwoLiner> jokeData = getMemoryEntryWithData(_wmc.address, 00139 TwoLiner.class); 00140 TwoLiner jk = jokeData.getData(); 00141 00142 // now see if we can provide a punchline 00143 if (jk.punchline.equals("")) { 00144 // in this case we need to propose a task to do 00145 // some processsing 00146 quip(jokeData); 00147 } 00148 } catch (DoesNotExistOnWMException e) { 00149 e.printStackTrace(); 00150 } catch (UnknownSubarchitectureException e) { 00151 e.printStackTrace(); 00152 } 00153 00154 } 00155 00156 }
1.5.8