HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 D! B8 m1 ?7 Z/ z& `8 l$ h6 r) Z
/ o& }: t" v: t, N! n public Object buildActions () {9 H6 b4 x4 b6 W" o2 @& }2 ^/ U
super.buildActions();
2 F- w: } r& c6 ]: t1 V + A x2 E% }3 w
// Create the list of simulation actions. We put these in
# c3 H: ~8 B% l // an action group, because we want these actions to be# @1 Q' J* L: J' i5 W1 Y
// executed in a specific order, but these steps should
9 ~( z8 C9 W! g: G5 c [( O // take no (simulated) time. The M(foo) means "The message
3 ^8 ~2 s" k& \ w // called <foo>". You can send a message To a particular$ s1 D6 p) g" E5 ]9 c0 y
// object, or ForEach object in a collection." t* G2 o! R! Z ^" Y) j) z, _
/ O% ~ @: d2 F5 b$ |' ? // Note we update the heatspace in two phases: first run- b8 P9 o1 }* r, E: G
// diffusion, then run "updateWorld" to actually enact the
& \8 p4 K9 M3 x4 N* P: t+ @ // changes the heatbugs have made. The ordering here is
6 W5 ` i/ M: _" [0 @. } // significant!9 f( [! t6 g$ B. Q, h
4 ]( } W8 T6 [) f2 B' H/ T4 M // Note also, that with the additional
* k% x+ D5 |# ]; f8 c1 p1 v5 H @ // `randomizeHeatbugUpdateOrder' Boolean flag we can/ C9 E' v. i* X9 \9 p
// randomize the order in which the bugs actually run
" H( }: X t! I# p" V3 J // their step rule. This has the effect of removing any
% K3 e; u" R* L, O1 B // systematic bias in the iteration throught the heatbug8 ^( f& Z. U7 g2 t! K; l
// list from timestep to timestep3 W4 m( s7 r! V
: A7 n% R* j; x# c& c9 w, S% V // By default, all `createActionForEach' modelActions have7 t& k5 x1 t! K
// a default order of `Sequential', which means that the
4 r F! t, N4 ]9 ?) h // order of iteration through the `heatbugList' will be9 @8 y# b3 s5 P$ u- P: [9 {1 `' ?$ U6 p
// identical (assuming the list order is not changed
- E5 ^8 O2 P9 s! h0 x% y1 E+ E5 B // indirectly by some other process).
7 o, t; F% `) Y3 ~; J+ O 9 w: d* \6 F4 c. c. d; c+ G3 s
modelActions = new ActionGroupImpl (getZone ());
: K( `, h* @, O1 ^9 l5 E& z
& D0 s- t; W! j$ M try {$ Z0 B; p9 r! x% o" r0 h) R
modelActions.createActionTo$message, M0 z" l, m" _9 `* r9 b( a3 d
(heat, new Selector (heat.getClass (), "stepRule", false));
X) m" o" t9 R4 P7 u8 l; a1 R! W |% _) f } catch (Exception e) {
* Q0 B: h0 ]1 B$ Z, O" g H& V System.err.println ("Exception stepRule: " + e.getMessage ());
$ m0 K( F/ @& l0 p1 c" P }( o: |; \- `& B8 ?% {* [2 u0 e) R
9 E% ^* x& j3 A5 y8 ?5 d$ N, s
try {0 {, P1 k. c* f, l; }
Heatbug proto = (Heatbug) heatbugList.get (0);
% L Q7 {/ P+ s9 h# G Selector sel =
J1 s4 d6 t* |2 d new Selector (proto.getClass (), "heatbugStep", false);. Y6 p) H# O# V9 f/ N2 S
actionForEach =
+ T( | `' i0 W# b9 X# e modelActions.createFActionForEachHomogeneous$call
1 O0 [5 p: l/ e (heatbugList,
4 G) i. O$ Y |: T! k3 i new FCallImpl (this, proto, sel,
8 d7 T- W" a4 U" ~ new FArgumentsImpl (this, sel)));
# [& i% q/ {# y/ B4 ` } catch (Exception e) {
+ [4 q) Q: `/ i& n# e9 }! j) f( x6 M e.printStackTrace (System.err);2 }6 w5 ~ `/ R
}
% Y/ q* S. h. h A. @- [ ) M8 A0 A0 {. q, C; D% @
syncUpdateOrder ();
4 ~. V4 k& i/ g s; ~5 C; H5 w: E9 L6 A+ f
try {
) P+ N( l, u+ L modelActions.createActionTo$message
' S! J( t) H9 _$ G) E. j (heat, new Selector (heat.getClass (), "updateLattice", false));
) b; {7 ^/ a/ \ l6 P N! Z4 D6 J } catch (Exception e) {, s6 H9 D5 s$ F. T
System.err.println("Exception updateLattice: " + e.getMessage ());
! C* h8 J* _% C' X }
5 d. `( m" { i! O6 t9 @" G% i * ?4 I/ {1 w v
// Then we create a schedule that executes the+ }+ l% w r$ E& R" ^$ O
// modelActions. modelActions is an ActionGroup, by itself it
* R* Y h! b3 F: X) k+ | // has no notion of time. In order to have it executed in8 \5 B0 ?" j- o8 j7 J& l4 h" H% z
// time, we create a Schedule that says to use the
1 ]0 F) a" i3 O* Y( H, b5 Y // modelActions ActionGroup at particular times. This, \- H' }1 Z! e: y- M
// schedule has a repeat interval of 1, it will loop every; [$ [9 k# i2 K
// time step. The action is executed at time 0 relative to
5 w( J& {5 F% v) u // the beginning of the loop. P' h( U) ?* c+ h+ f3 o. `+ X z) l
# B& d. r) c( U# z3 m7 v/ e // This is a simple schedule, with only one action that is9 f& s, e# K1 s& k
// just repeated every time. See jmousetrap for more
: J- v* H* G0 P // complicated schedules.! y& h8 J ^. ^3 j- ]6 ]
5 |$ V' z. N! r! R
modelSchedule = new ScheduleImpl (getZone (), 1);
( B5 ~$ ?5 Q7 w2 ?. N modelSchedule.at$createAction (0, modelActions);
: n# B& D. M- p; T
5 y7 W! y, G9 n( T d3 @! Y: @ return this;
% m* c& l. u* l& H5 c } |