001    import swarm.simtoolsgui.*;
002    import swarm.gui.*;
003    import swarm.space.*;
004    import swarm.activity.*;
005    import swarm.defobj.*;
006    import swarm.objectbase.*;
007    import swarm.*;
008    import swarm.analysis.*;
009    import java.util.*;
010    
011    
012    public class ObserverSwarm extends GUISwarmImpl{
013            public int graphInterval;
014    
015            ActionGroup displayActions,statActions;
016            Schedule displaySchedule,statSchedule;
017            ModelSwarm modelSwarm;
018            Colormap colorMap;
019            ZoomRaster patternRaster;
020            Value2dDisplay patternDisplay;
021            EZGraph resultGraph;
022            EZBin clusterSizeHist;
023            LinkedList clusterSizeData;
024            
025            public ObserverSwarm(Zone aZone){
026                    super(aZone);
027                    graphInterval=10;
028            }
029            
030            public Object buildObjects(){
031                    super.buildObjects();
032                    Globals.env.createArchivedProbeDisplay (this, "observerSwarm");
033                    
034                    resultGraph=new EZGraphImpl(
035                            this,"Percolation Probability","points","P(points)","resultGraph");
036                    try {
037                            resultGraph.enableDestroyNotification$notificationMethod
038                              (this, new Selector(getClass(),"_resultGraphDeath_",false));
039                            resultGraph.createSequence$withFeedFrom$andSelector(
040                                    "P",this,new Selector(this.getClass(),"getPercolationProb", false));
041                            resultGraph.createSequence$withFeedFrom$andSelector(
042                                    "cluster num / 0.15 N^2",this,new Selector(this.getClass(),"getRelativeClusterNum", false));
043                    } catch (Exception e) {
044                            System.err.println ("Exception: " + e.getMessage());
045                    }
046                                                    
047                    modelSwarm = (ModelSwarm)
048                            Globals.env.lispAppArchiver.getWithZone$key(Globals.env.globalZone,"modelSwarm");
049                    Globals.env.createArchivedProbeDisplay (modelSwarm,"modelSwarm");
050                    if(modelSwarm==null){
051                            System.out.println("Can't find the modelSwarm parameters.");
052                            System.exit(1);
053                    }
054                    getControlPanel().setStateStopped();
055    
056                    modelSwarm.buildObjects();
057    
058                    clusterSizeData = new LinkedList ();
059                    clusterSizeData.add (new Double(0));
060                    EZBinC ezbinCreating = new EZBinCImpl (new EZBinImpl ());
061                    ezbinCreating.createBegin (getZone());
062                    ezbinCreating.setCollection (clusterSizeData);
063                    Class memberClass= clusterSizeData.get (0).getClass ();
064                    try{
065                            ezbinCreating.setProbedSelector(new Selector (memberClass, "doubleValue", false));
066                    } catch (Exception e) {
067                            e.printStackTrace (System.err);
068                    }
069                    ezbinCreating.setTitle ("Histgram of Cluster Size");
070                    ezbinCreating.setBinCount (10);
071                    ezbinCreating.setLowerBound (0);
072                    ezbinCreating.setUpperBound (modelSwarm.getWidth()*modelSwarm.getHeight()/200);
073                    clusterSizeHist = (EZBin) ezbinCreating.createEnd ();
074    
075                    colorMap=new ColormapImpl(this);
076                    colorMap.setColor$ToName((byte)0,"white");
077                    colorMap.setColor$ToName((byte)1,"black");
078                    colorMap.setColor$ToName((byte)2,"red");
079                    
080                    patternRaster=new ZoomRasterImpl(this);
081                    patternRaster.setColormap(colorMap);
082                    patternRaster.setZoomFactor(1);
083                    patternRaster.setWidth$Height(
084                            modelSwarm.getWidth(),
085                            modelSwarm.getHeight());
086                    patternRaster.setWindowTitle("Generated Pattern");
087                    patternRaster.pack();
088                    
089                    patternDisplay=new Value2dDisplayImpl(
090                            this,patternRaster,colorMap,modelSwarm.getPattern());
091            
092                    return this;
093            }
094            
095            /**
096             * 浸透確率の問い合わせる。
097             */
098            public double getPercolationProb(){
099                    return modelSwarm.getPattern().getPercolationProb();
100            }
101            
102            /**
103             * クラスタ数の問い合わせ
104             */
105            public double getRelativeClusterNum(){
106                    return modelSwarm.getPattern().getClusterNum()
107                            /(0.15*modelSwarm.getHeight()*modelSwarm.getWidth());
108            }
109            
110            /** 
111             * 2つのスケジュールを用いる。ひとつは画面の更新、
112             * 統計データ(クラスタの判定も含む)の更新<BR>
113             * クラスタリングの計算は重いことが予想されるから、
114             * graphIntervalの間隔で行う(初期値は10だが、Probeから変更できる)。 
115             */
116            public Object buildActions(){
117                    super.buildActions();
118                    modelSwarm.buildActions();
119                    
120                    displayActions=new ActionGroupImpl(this);
121                    try {
122                            displayActions.createActionTo$message(patternDisplay,
123                                    new Selector(patternDisplay.getClass(),"display",false));
124                            displayActions.createActionTo$message(patternRaster,
125                                    new Selector(patternRaster.getClass(),"drawSelf",false));
126                            displayActions.createActionTo$message(getActionCache(),
127                                    new Selector(getActionCache().getClass(),"doTkEvents",true));
128                            displayActions.createActionTo$message(this,
129                                    new Selector(getClass(),"checkToStop",false));
130                    } catch (Exception e) {
131                            System.out.println ("Exception: " + e.getMessage ());
132                            System.exit(1);
133                    }
134                    displaySchedule = new ScheduleImpl(this,1);
135                    displaySchedule.at$createAction(0,displayActions);
136                    
137                    statActions=new ActionGroupImpl(this);
138                    try {
139                            statActions.createActionTo$message(this,new Selector(getClass(),"showStats",false));
140                            statActions.createActionTo$message(this,new Selector(getClass(),"showHist",false));
141                            statActions.createActionTo$message(getActionCache(),
142                                    new Selector(getActionCache().getClass(),"doTkEvents",true));
143                    } catch (Exception e) {
144                            System.out.println ("Exception: " + e.getMessage ());
145                            System.exit(1);
146                    }
147                    statSchedule = new ScheduleImpl(this,graphInterval);
148                    statSchedule.at$createAction(0,statActions);
149                    
150                    return this;
151        }
152            
153            public Activity activateIn(Swarm context){
154            super.activateIn(context);
155                    modelSwarm.activateIn(this);
156                    displaySchedule.activateIn(this);
157                    statSchedule.activateIn(this);
158                    return getActivity();
159            }
160            
161            public Object _resultGraphDeath_(Object caller){
162                    resultGraph.drop();
163                    resultGraph = null;
164                    return this;
165            }
166            
167            /**
168             * 統計データ(クラスタリングも含む)の更新<BR>
169             */
170            public Object showStats(){
171                    if(resultGraph!=null)
172                            resultGraph.step();
173                    return this;
174            }
175            
176            /**
177             * ヒストグラムの更新
178             */
179            public Object showHist(){
180                    //clusterSizeData=modelSwarm.getPattern().getClusterSizeData();
181                    //これではうまくいかない。仕方なく、
182                    updateClusterSizeData();
183                    if(clusterSizeHist!=null){
184                            clusterSizeHist.reset();
185                            clusterSizeHist.update();
186                            clusterSizeHist.output();
187                    }
188                    return this;
189            }
190            
191            /**
192             * showHistの補助メソッド
193             */
194            void updateClusterSizeData(){
195                    clusterSizeData.clear();
196                    Iterator it=modelSwarm.getPattern().getClusterSizeData().iterator();
197                    while(it.hasNext()){
198                            clusterSizeData.add(new
199                                    Double(((Integer)it.next()).intValue() //このままだとすごく遅い
200                                                                                             //同じ値のデータがたくさんあるとだめらしい
201                                                                                             //Quick Sortしているわけでもないだろうに
202                                                                                             //仕方ないから、少しノイズを加えておく
203                                    +Globals.env.uniformDblRand.getDoubleWithMin$withMax(0.0,0.01)));
204                    }
205            }
206            
207            /**
208             * 終了の判定<BR>
209             * ModelSwarmが停止していたら、最終処理をして停止。
210             */
211            public void checkToStop(){
212                    if(modelSwarm.stopQ()){
213                            showStats();
214                            getActionCache().doTkEvents();
215                            System.out.println("Simulation is over.");
216                            getControlPanel().setStateStopped();
217                    }
218            }
219    }