HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 I8 ^* N5 F' f7 Y& E J
0 X' b( `$ Z8 j; c8 e4 U public Object buildActions () {3 B" d0 X9 g3 z$ \5 N
super.buildActions();
3 H: q- L: M. S. a 8 F; m n/ \& ?/ |2 Y! D
// Create the list of simulation actions. We put these in
8 V6 e# ~: i& y& j3 w' t // an action group, because we want these actions to be
6 C& O0 _8 O* [8 s: E# V // executed in a specific order, but these steps should+ _5 z6 [& x6 i4 G1 M: ]% t' v
// take no (simulated) time. The M(foo) means "The message% @+ s L2 v w3 m9 c
// called <foo>". You can send a message To a particular; ~4 K/ F2 S! X4 D7 @1 V
// object, or ForEach object in a collection.* {, v9 T2 P2 o0 K( F
6 S6 R |, V7 U6 `4 _( r; a+ E& j6 i // Note we update the heatspace in two phases: first run; Z6 Z& A$ G9 ]! V
// diffusion, then run "updateWorld" to actually enact the
" V" ] T* U* B9 ~ // changes the heatbugs have made. The ordering here is
' ]* q- ]) u9 Y6 f# U5 _- K // significant!
/ [( X8 f5 y9 r
8 V5 C; H4 Z5 j5 i% i# A: U9 K6 @8 P& X // Note also, that with the additional
7 ~- U7 l/ }- [/ A // `randomizeHeatbugUpdateOrder' Boolean flag we can
( M/ z- D* x. S! b, T# }3 `1 i2 x- @ // randomize the order in which the bugs actually run: h6 P% w5 ]) k) {, q7 n( C
// their step rule. This has the effect of removing any
Q; K7 k' B1 F4 a // systematic bias in the iteration throught the heatbug* v2 h% x, q: M! A
// list from timestep to timestep
( h' }# w: l: z% M: Y4 O
* l( X+ s6 R7 i w // By default, all `createActionForEach' modelActions have
s0 L# \6 ]* W+ s // a default order of `Sequential', which means that the* h9 K5 u8 [) Z- _5 ^8 F
// order of iteration through the `heatbugList' will be5 C6 ?: R+ F: ]* H" Y' k+ s
// identical (assuming the list order is not changed
2 O" u, D6 b/ [1 t // indirectly by some other process).
" W4 E0 W+ K3 \" M; u ( i |8 T8 g" T5 I) F
modelActions = new ActionGroupImpl (getZone ());0 H6 q% [9 O1 w" U9 A9 N2 J
" o$ s9 Q( J& ~ try {3 z% F t/ d7 I6 N0 M$ C/ H0 I: \
modelActions.createActionTo$message
5 A# }( E& {) r* m (heat, new Selector (heat.getClass (), "stepRule", false));
+ { [3 e7 ]4 R/ t/ R) E& e! o } catch (Exception e) {
l N7 J9 k8 H4 P; L# p$ R System.err.println ("Exception stepRule: " + e.getMessage ());5 `6 g3 M* K0 [& { h1 Q
}
2 z3 n2 {& u/ B! R% x' h( h" U& K9 |+ r* o1 N5 U/ p* s% U! N
try {% ?- y' ?/ f$ J! w
Heatbug proto = (Heatbug) heatbugList.get (0);) A+ Y+ @3 j( ?. u/ H5 T
Selector sel =
0 s* V, t6 s9 i5 k: E, Q new Selector (proto.getClass (), "heatbugStep", false);3 ]" V9 ]" h* w7 s& _: R
actionForEach =
! Z6 y( q. X# }' u modelActions.createFActionForEachHomogeneous$call! L3 C h) I1 x. S1 m0 ^: f
(heatbugList,$ V& q2 P/ l/ t3 {, S% z! c, E% ~
new FCallImpl (this, proto, sel,- M5 b4 B2 z0 _, L. `( y. T
new FArgumentsImpl (this, sel)));
( |% }% t. l( i! Y4 g6 P- s# g" R } catch (Exception e) {4 [. }* t0 O+ ]% x+ C- f7 f
e.printStackTrace (System.err);
4 l0 ^2 X( l1 X; n }
" n6 [3 H% A5 {) l8 t' `& | G
0 d4 H: o" k; a syncUpdateOrder ();
- l9 y' e1 q/ v" K9 c3 P9 \: ]- C4 D R6 D; I- o0 ]) f. C v
try {
: `" f0 \3 Z5 [% ^/ W- ? modelActions.createActionTo$message
4 h0 d, J6 o8 v+ R7 v (heat, new Selector (heat.getClass (), "updateLattice", false));
) h$ P) ^5 s8 @$ ^- @9 X3 \ } catch (Exception e) {
9 l3 w# z* B: K! A! J/ V System.err.println("Exception updateLattice: " + e.getMessage ());$ l9 O( W9 x& z7 f. v* l$ G
}- k+ d2 }* ~5 o; H E3 O" x
- A2 ?: L B% h7 ~& f: D; w
// Then we create a schedule that executes the# x' n- ~. Z6 d* ?6 ]# K# I
// modelActions. modelActions is an ActionGroup, by itself it) P- q# @8 f8 z8 A
// has no notion of time. In order to have it executed in5 I; b J9 X' Z/ H5 m# U) Y7 S
// time, we create a Schedule that says to use the( q. h; Z& D- L' k
// modelActions ActionGroup at particular times. This
7 x; Z% s! C& \! U* W, U# S2 } // schedule has a repeat interval of 1, it will loop every
( j& Q( x& D: ] // time step. The action is executed at time 0 relative to3 g1 H4 n/ w, z; I5 b6 m0 R
// the beginning of the loop.9 h1 W1 M9 r4 l; V1 F5 O! ^% G
4 H+ a8 V/ U$ q% V2 B
// This is a simple schedule, with only one action that is
/ S7 `; R3 m r M. Z // just repeated every time. See jmousetrap for more0 K6 ^( L% U& R' W. n4 P8 v
// complicated schedules.
7 i6 m F; |; }) Y8 V. Q ' U! L/ X6 W7 l- v2 g! @$ z, w o- |
modelSchedule = new ScheduleImpl (getZone (), 1);
6 E6 e8 w# r- b modelSchedule.at$createAction (0, modelActions);$ i" a, Y, a* u9 V7 y
! g- ~7 o7 X) y! a* c* j0 c: h
return this;+ F: t/ g9 M- x* U' ^
} |