HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 M3 \6 x1 V q5 U* L9 t( ~0 ~. b# `
) G- |7 n) ^8 a+ g7 W+ R! b& E8 q8 P public Object buildActions () {9 `2 S+ Z8 o4 B2 r" P, u5 z
super.buildActions();
" A) \! U7 p7 @: a) E9 M$ O2 }* U ' V: q. }; i, z% ]! v) n* m, m
// Create the list of simulation actions. We put these in9 }" J& l+ x- i$ ^* F6 R! ~5 H
// an action group, because we want these actions to be
0 r+ J# x5 f: g! P4 n ? // executed in a specific order, but these steps should0 j& g8 C5 U5 @, J. m% P
// take no (simulated) time. The M(foo) means "The message" Q, e9 F$ L- ?0 ]( _4 q
// called <foo>". You can send a message To a particular
( X, b# o3 Q5 j. h, h) L$ e7 [/ l // object, or ForEach object in a collection.
u) X; k9 A* Q( e. o 0 S% @/ K. P. q. q; ^8 M$ x
// Note we update the heatspace in two phases: first run
& C1 Y7 O( o& P // diffusion, then run "updateWorld" to actually enact the _4 Y1 J/ o6 ]+ q! D; A- n9 s
// changes the heatbugs have made. The ordering here is
$ h z! [7 s6 b+ ~ // significant!
$ S2 W$ s& v- r : f8 w" f0 p' h" z2 W
// Note also, that with the additional4 Y5 v8 V3 Z" h! |& {7 d
// `randomizeHeatbugUpdateOrder' Boolean flag we can
: o& B# Q0 } ^, P# n4 h! M4 A8 B t& ] // randomize the order in which the bugs actually run$ ]5 o) e$ |* Y6 @, l* t- Y; O
// their step rule. This has the effect of removing any
) c* j+ m9 ]7 v* } // systematic bias in the iteration throught the heatbug& q& z; P q. T/ I
// list from timestep to timestep6 P- ^ Y* B1 Z/ K2 G4 ]
* n/ J" p/ p/ b- I/ {% k& b( J( r
// By default, all `createActionForEach' modelActions have' l! y+ [! W- d; x+ u
// a default order of `Sequential', which means that the( O' Q3 r8 X0 E
// order of iteration through the `heatbugList' will be
: {7 c0 a/ c% h9 f0 C2 B$ V0 w // identical (assuming the list order is not changed
2 }# \8 W0 T; l! ^3 [ // indirectly by some other process).
& y5 `' |% e, O3 x + ?8 f) y/ k( R9 V3 b5 H6 b
modelActions = new ActionGroupImpl (getZone ());
/ C9 T2 L' n9 q3 y1 e. ]" ^8 A# V+ H! w: c# F
try {/ f5 H& [ C& ^- z
modelActions.createActionTo$message
' b7 Q" b# d- `/ `/ G, O (heat, new Selector (heat.getClass (), "stepRule", false));
% ~) _- @ p0 l% y1 | } catch (Exception e) {
) l; O- G+ e) z [ System.err.println ("Exception stepRule: " + e.getMessage ());
# e4 \3 ?5 {! @! p" t5 C8 u5 ^2 M }
4 n i N, _; [* v9 j8 L: Y; P) b( v; z
try {5 ^! a3 e0 j4 [
Heatbug proto = (Heatbug) heatbugList.get (0);
* M# _$ M; |/ y; K+ S+ @0 z9 r Selector sel =
M: d* q) @& N. |6 V2 g- s new Selector (proto.getClass (), "heatbugStep", false);
. T4 N* q% n4 o, w actionForEach =
7 j; |# v7 V- G# C8 R6 k modelActions.createFActionForEachHomogeneous$call
+ I9 ], ~& ?9 @ (heatbugList, K6 K2 k: ?: C# P* A% G& V
new FCallImpl (this, proto, sel,
) a; n( w; g6 D* g! m9 f# k+ [ new FArgumentsImpl (this, sel)));
( `& u* C4 o2 e. e8 m/ w } catch (Exception e) {8 F* N* \. ^) s- l2 i8 k& F
e.printStackTrace (System.err);
/ g8 j2 e- h# H, h% |1 a6 w }
, d8 F1 x$ u) p5 ?. r
$ \" I! s L/ T) N3 G5 R$ w5 K syncUpdateOrder ();0 t( p4 Z1 ?& e1 ?) ^2 P
2 L7 E5 h" `6 z' k) ]" P4 Q; }1 n try {
' f) {; P) G/ } {* Y j& c$ T modelActions.createActionTo$message
7 n* T2 T) b: } y- N (heat, new Selector (heat.getClass (), "updateLattice", false));
; R! O+ H7 U" v! R ? } catch (Exception e) {2 v7 Y6 j/ m6 d
System.err.println("Exception updateLattice: " + e.getMessage ());- E4 j: W& x0 d4 U
}
. V2 [- n) Z" M: y+ W5 @# |
( L, f5 k% b" C2 H // Then we create a schedule that executes the
" r& z$ g# [- C4 R! V' o7 D // modelActions. modelActions is an ActionGroup, by itself it( l9 V: [- Z- F7 z
// has no notion of time. In order to have it executed in
6 j$ D; d1 h- b( b* Z/ ^. u // time, we create a Schedule that says to use the% e6 h7 C v& E X( R# k" l9 \; Z
// modelActions ActionGroup at particular times. This5 D' g# k; G- I
// schedule has a repeat interval of 1, it will loop every* Z. x5 {( i, ]' M
// time step. The action is executed at time 0 relative to7 t0 R3 m3 n* q b- ~
// the beginning of the loop." N1 a/ F) P/ v
* _2 W5 a: ]& N* F3 U% I
// This is a simple schedule, with only one action that is8 \4 E4 ~& f4 I% m
// just repeated every time. See jmousetrap for more
0 j, t7 B( m# a1 C5 } // complicated schedules.9 u1 Z2 P& |0 S- e# k
d; B5 ]; J* z3 q+ b" j0 |
modelSchedule = new ScheduleImpl (getZone (), 1);
" Y# J% Y& F9 q% _4 b modelSchedule.at$createAction (0, modelActions);
7 w$ u2 M& d3 I
. W0 c' l7 S e2 Q" s& D+ C return this;# Z( X2 ]; Q; N3 w
} |