HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 [# P& ` Z7 h, R% E, @- D
# g" ^# D; ?) Z public Object buildActions () {) K2 e1 c: e; F! ]/ s
super.buildActions();
" S3 {1 z/ r0 Y& a4 J' y; }
8 n8 X6 g/ z3 t. U% Q$ b' q$ M // Create the list of simulation actions. We put these in9 m2 z( h: }! u, `" V
// an action group, because we want these actions to be s t" o# m0 T- P7 [3 K/ H
// executed in a specific order, but these steps should
/ V6 M& Q' c: U; _2 F // take no (simulated) time. The M(foo) means "The message
4 S) F8 O7 h6 V8 S( t) b5 ]1 Z // called <foo>". You can send a message To a particular
5 t0 R8 p B! z, t/ R) @ // object, or ForEach object in a collection.
7 [7 D+ d- a( w w' m" K* D6 Y % U2 v! v% {& d% N; @
// Note we update the heatspace in two phases: first run; }: K. ]& V0 {1 r6 q! n
// diffusion, then run "updateWorld" to actually enact the6 ~. f- T# l, n2 ?( R7 A
// changes the heatbugs have made. The ordering here is
4 o" ?- P1 X5 p- t. q // significant!8 y' T9 u b! J2 l* {& h8 P
, S A/ d% v, E# n% Y9 `
// Note also, that with the additional/ w/ Z* z: q) @5 g2 h
// `randomizeHeatbugUpdateOrder' Boolean flag we can- k( v6 m8 V1 D5 x) Y
// randomize the order in which the bugs actually run
( b1 p% f* A4 L" r/ N // their step rule. This has the effect of removing any
- R" q" s" C0 K. `& ~ // systematic bias in the iteration throught the heatbug
& g% O& H; Z- B- _0 m( [ // list from timestep to timestep
2 ?% z' M8 T8 D; M. H
8 ]9 H& X- @0 [/ [* {2 e! E // By default, all `createActionForEach' modelActions have$ }; G3 u. U D3 P+ V6 Z
// a default order of `Sequential', which means that the' `* D0 x; w- u O' @+ o! o: t
// order of iteration through the `heatbugList' will be
# B+ k' {% Y7 Q& ^( I // identical (assuming the list order is not changed
& o8 Y3 D0 J: a% R // indirectly by some other process).. W! }, a' A: A9 `
! b$ p$ Q6 A* U9 ?1 l6 f
modelActions = new ActionGroupImpl (getZone ());" {$ h; e8 ^1 D6 O9 \
( L7 R# E) t$ ~0 _% T, | try {( } ]. H4 Z8 Q
modelActions.createActionTo$message
. l; K% t P& G( s8 @ (heat, new Selector (heat.getClass (), "stepRule", false));
- d* i' j( b/ s" [* { } catch (Exception e) {$ P9 n e( W' Y& }
System.err.println ("Exception stepRule: " + e.getMessage ());
/ l) ?; ]. t! A9 g' r: Q) e }
/ K% s2 W2 k; C( J* ~( ?, Q
4 q- L* z9 x4 @2 b$ ] try {' j4 a& l/ l4 z1 Q2 I6 N
Heatbug proto = (Heatbug) heatbugList.get (0);4 ], W0 |: L. a# O0 j
Selector sel = 4 n) d5 b7 H8 t& `
new Selector (proto.getClass (), "heatbugStep", false);4 M) m; L7 ]3 j+ V! Y, e# q
actionForEach =
, s7 u6 C, v. q/ o) u8 {0 _, n# P modelActions.createFActionForEachHomogeneous$call
' v; x' J& N6 e z (heatbugList,
0 m# K* R/ X$ ? new FCallImpl (this, proto, sel,
: Z: O9 g8 Y `' t3 G# I, |4 j8 t new FArgumentsImpl (this, sel)));
- `# J {5 ]% B4 D9 k6 e } catch (Exception e) {' J0 m7 B: }. p
e.printStackTrace (System.err);2 j0 Q* M" E' w, E
}' z2 O4 B/ i8 P" B6 o4 |
3 J- o7 e6 i2 `; f' _4 x4 i& C
syncUpdateOrder ();
/ A% o: O& y. r! H1 j, |; r6 ~9 J0 g, O0 j8 T/ Y o- H4 \
try {
3 ]; S5 d- y4 e' }2 f8 W modelActions.createActionTo$message % L9 i9 p* B* p4 n/ |+ f
(heat, new Selector (heat.getClass (), "updateLattice", false));+ ?7 V9 D/ }0 _, t0 P; g% I% q! T
} catch (Exception e) {+ @: R* N9 b3 Z) J# S9 e e
System.err.println("Exception updateLattice: " + e.getMessage ());* S, k- Q" ^- j8 w
}
4 H, @1 B+ f4 i1 z4 Y9 c $ r% ~0 W% b8 @
// Then we create a schedule that executes the
/ w6 K# D* T/ X/ ^: n // modelActions. modelActions is an ActionGroup, by itself it
- S- t3 @) f2 I8 C, C: f // has no notion of time. In order to have it executed in1 y) X, Z2 H' K3 i e8 H, R$ r0 H3 x1 N
// time, we create a Schedule that says to use the* d }6 N3 F+ t
// modelActions ActionGroup at particular times. This3 T& V+ k. I1 a, O! s2 N$ f( a4 I8 g6 E
// schedule has a repeat interval of 1, it will loop every
9 r% v2 h" G+ q, i" j // time step. The action is executed at time 0 relative to1 e# i0 {4 m: Q& V4 r b
// the beginning of the loop.
1 a) [$ x0 S/ @! B2 _0 P. N! o: I7 z- j2 _) B" E2 r
// This is a simple schedule, with only one action that is
+ z! a% Z1 u4 L5 _7 I+ f" }& p M // just repeated every time. See jmousetrap for more, {- U; I# X" h& E
// complicated schedules.: C/ |2 D; t0 w) P
/ ?1 ?" ]2 {4 G4 H: D
modelSchedule = new ScheduleImpl (getZone (), 1);$ \, r: p3 u W( V1 G' Z8 E
modelSchedule.at$createAction (0, modelActions); e* s0 n- Z/ D/ @7 b0 x
% y* }$ `: S8 m! b4 |* u+ d
return this;
: c; X' b/ |" X# t9 H } |