HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 D/ A3 n" `/ V& G
1 X2 i6 Q$ b- D: x
public Object buildActions () {
. ~ i& H8 {' B+ j) S( h super.buildActions();* h) f3 U A6 i2 F/ D0 \- Y
! f8 d; p/ k1 a% S // Create the list of simulation actions. We put these in% D9 [- s6 F9 U0 E3 O4 L. Y
// an action group, because we want these actions to be8 S" Z1 h" S2 v3 ?+ c* e' T
// executed in a specific order, but these steps should
* P* p1 S. ~3 ?0 \6 l x1 ^& D F // take no (simulated) time. The M(foo) means "The message. g8 Z4 A9 Y1 @. k, L9 t
// called <foo>". You can send a message To a particular1 z9 B' M2 }% Y' v! }
// object, or ForEach object in a collection.; f7 s$ T- k+ @ r
8 D/ a% j' V& W2 y // Note we update the heatspace in two phases: first run
* {* K! y, x* d9 r D // diffusion, then run "updateWorld" to actually enact the" ]. i( Z+ `. b; h& p& w+ A5 m
// changes the heatbugs have made. The ordering here is/ M! E8 V5 B1 t
// significant!
3 X& F, z4 `. d3 W: n& W% x7 P
: Q) e* t1 ^' q ]4 Z' s8 e$ T$ r // Note also, that with the additional
) n. e2 v& f' A L7 k1 A7 t // `randomizeHeatbugUpdateOrder' Boolean flag we can
& C* \, V8 N" u! ?' k% w // randomize the order in which the bugs actually run$ g0 P8 u' z. c- v
// their step rule. This has the effect of removing any
6 `! a3 T! U# n9 m0 g // systematic bias in the iteration throught the heatbug
: e* M+ W4 e! S. {7 W0 c // list from timestep to timestep$ s$ v0 b9 b0 h- ?
# T+ u6 l( b+ N7 q; v3 T& Y9 X
// By default, all `createActionForEach' modelActions have# V4 A) ^. t4 H9 v% N
// a default order of `Sequential', which means that the$ w4 k" e. j- J1 N9 |) w5 D0 F
// order of iteration through the `heatbugList' will be$ X" h: M! k) k; Q& r/ F/ p7 l
// identical (assuming the list order is not changed$ k$ A6 n# g" w/ y5 x3 G
// indirectly by some other process).
8 }2 v0 j9 Q; O4 J+ k. q8 F! a+ n - _6 r `" F6 p: X' C
modelActions = new ActionGroupImpl (getZone ());# m! t( \/ |- C* `8 @% t
s; y* ?" z( C e a% b. s try {6 l0 q9 N6 a5 K- G" q- H
modelActions.createActionTo$message
4 q3 p! P! t8 o8 w- _ O (heat, new Selector (heat.getClass (), "stepRule", false));# ?& w/ n. Z3 m T1 a, f
} catch (Exception e) {- m+ d0 B' r1 X% ^; M0 }
System.err.println ("Exception stepRule: " + e.getMessage ());
2 a6 w+ ?6 e9 c: u( }- b. ^ }
3 D( C; y* u6 Q5 d L( E5 r/ d/ }/ C6 n) L+ C( Z, Y- h. y
try {
) I z4 f; }9 m$ {$ O$ k I1 ~ Heatbug proto = (Heatbug) heatbugList.get (0);8 J1 L6 Y4 b$ |: B* ]
Selector sel = " x* ?' j' P0 s4 f
new Selector (proto.getClass (), "heatbugStep", false);( r/ b8 b8 D7 H$ S; ?
actionForEach =
+ _$ `" X3 m5 ^% `& x! v0 w8 A modelActions.createFActionForEachHomogeneous$call' t2 X' N F* R2 V5 w9 n: i, l
(heatbugList,( z9 R8 W0 R) Q/ K! `/ q6 x# }% ?
new FCallImpl (this, proto, sel,8 O& x1 P# J* l, E
new FArgumentsImpl (this, sel)));
" q" y* i; v: k. f& q( U V9 W9 @ } catch (Exception e) {
! l* Z6 _* z5 w6 P7 ?0 S e.printStackTrace (System.err);
1 l, Q! o' M3 v }* Y' L% y, l6 C# {: q X
. o8 ]) S; l# |
syncUpdateOrder ();
$ m8 j0 O+ c N/ X# X8 R) J6 v( y
, [9 g' z4 t% b" _. `6 S( w try {* z7 ? w6 B" l5 E
modelActions.createActionTo$message ' m) V) y2 g( a$ Z4 b2 p
(heat, new Selector (heat.getClass (), "updateLattice", false));
" @2 Z0 b$ g. l% I' Y } catch (Exception e) {6 H& f4 h+ F4 e* j! C. r
System.err.println("Exception updateLattice: " + e.getMessage ());" N4 V+ n8 N8 j3 E5 c
}
' X- s/ S( M* A9 A/ x " |% |2 `5 Q' V# n
// Then we create a schedule that executes the
5 D9 O4 p, c2 b9 {6 D8 H0 e- n { // modelActions. modelActions is an ActionGroup, by itself it( N8 d/ D; k. ]! W5 e d2 N ~# q$ w7 O
// has no notion of time. In order to have it executed in
* c/ Z* D v9 D+ U // time, we create a Schedule that says to use the
$ H$ {, l- ]' o1 R // modelActions ActionGroup at particular times. This I c5 H2 }9 O" e" c
// schedule has a repeat interval of 1, it will loop every
7 F5 B& f5 b/ C4 K+ u // time step. The action is executed at time 0 relative to0 x8 ] W) X7 Y: p
// the beginning of the loop.
2 z5 {3 o3 c; i( B( K; d0 x% u4 H$ O, x8 R
// This is a simple schedule, with only one action that is
- B0 L3 f& z) h // just repeated every time. See jmousetrap for more
- r, \1 w5 v4 r2 @* A( R2 G- d // complicated schedules.2 B! R6 Y9 Y w3 S }$ t$ H
6 l) e* F. {) H, H modelSchedule = new ScheduleImpl (getZone (), 1);
: J5 m6 l1 V7 K# x modelSchedule.at$createAction (0, modelActions);
v' p3 U s) f5 r" `4 z8 j ! x6 d2 P2 T3 a! d, i( [2 x
return this;
- x9 w# {2 X, ?, g* y! Y8 d; w2 w } |