HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: G# g8 _( v+ {, Z8 h3 _! W1 z, w7 ^& q2 j3 t$ g
public Object buildActions () {6 u& Z8 e3 }" G6 _: o6 `# I
super.buildActions();. e0 K, j2 d9 _% Z9 v4 v
4 O, `6 {2 E' q, V, ~8 W // Create the list of simulation actions. We put these in5 Q6 l6 M$ {4 k% A
// an action group, because we want these actions to be
$ z: H0 O% o1 X5 w; k6 C3 W // executed in a specific order, but these steps should& J+ Z% Y% r/ Z, k: b. p
// take no (simulated) time. The M(foo) means "The message
! W% [& N T0 }5 p. q& t. ^: |' X // called <foo>". You can send a message To a particular- S5 F! T" J6 t& p% N- m9 T
// object, or ForEach object in a collection.. ?, {. G2 z( r7 p) ]8 |
4 `! B7 h/ Z& |+ p* E& e' m // Note we update the heatspace in two phases: first run
7 |( b: j, x0 G( _) `9 W; } // diffusion, then run "updateWorld" to actually enact the) i3 H0 [( ]7 J6 y" e, c
// changes the heatbugs have made. The ordering here is
- s5 ~8 z) i0 p7 O* n // significant!: d. p6 J" A$ y6 H# ^5 | j" c
7 r+ b' t) U# y/ X9 P# q$ V4 q
// Note also, that with the additional- y6 E- d$ _) c/ Z& `
// `randomizeHeatbugUpdateOrder' Boolean flag we can
2 T" b( b$ m0 y* s: B) \9 Z, W // randomize the order in which the bugs actually run
" m& r0 n$ ]6 X) v v F // their step rule. This has the effect of removing any" V0 O8 b4 ^* _; E8 n1 z
// systematic bias in the iteration throught the heatbug& m$ T1 g: y- a4 r" J8 F4 n
// list from timestep to timestep
l C D* j; T$ b7 E& d7 J$ s % Y9 @# H- g5 a& z7 P" ]" o
// By default, all `createActionForEach' modelActions have6 h, w8 p5 n; A
// a default order of `Sequential', which means that the
' z& G* r6 Q. z5 g // order of iteration through the `heatbugList' will be
; V. v1 @: Y+ I& q // identical (assuming the list order is not changed: E5 T3 A8 d% I- g
// indirectly by some other process).
7 V: z* V8 Q; @* V# j# }7 G ' \2 z, s5 L1 t2 z$ b8 L/ |- x" [; e
modelActions = new ActionGroupImpl (getZone ());) j1 K$ |! ~; W! T
0 g) y2 W4 w1 P7 ^2 j try {4 A* g" ]7 b" `) [% f3 ]
modelActions.createActionTo$message M! ~% J5 Z5 B7 W M/ o7 v
(heat, new Selector (heat.getClass (), "stepRule", false));
" O/ y6 [( X4 R5 }& L4 r9 c } catch (Exception e) {7 L. x+ k( W) }& M9 l' B# @! U
System.err.println ("Exception stepRule: " + e.getMessage ());+ I b: Q- U' C4 \: H& g8 J% [
}
( V% B( l' h/ D5 W5 c1 G. t1 P
; u) v' B4 `: H5 \ try {3 }6 a# G. t; K1 B3 W- ]
Heatbug proto = (Heatbug) heatbugList.get (0);
7 `) e. G8 @: ?( ~3 {- G- p4 A* w Selector sel = + U4 b3 |, \8 d6 q0 z) ^
new Selector (proto.getClass (), "heatbugStep", false);
5 h: T+ c6 d0 \ k$ p3 { actionForEach =
0 H0 j: v3 I" Y& r" R8 p/ ? modelActions.createFActionForEachHomogeneous$call3 ^% I1 W) F2 K% A( J1 S: f
(heatbugList,
. H+ N' {. w1 \- U7 _ new FCallImpl (this, proto, sel,0 d2 ^: S) Q, U( r. a4 r
new FArgumentsImpl (this, sel)));
! I2 Q: s9 X* j2 r3 M4 h" R+ y } catch (Exception e) {
5 x7 L# M# h/ q4 q e.printStackTrace (System.err);
# {4 @& @" F5 v$ H' T }5 r8 N3 p) @- k, K" R/ ?
; v8 o7 h/ \2 @5 j' n$ [ syncUpdateOrder ();6 a( J( v1 k% P, w% T
$ d+ ?: w! }7 x/ |& v7 b/ ^* M/ ? try {
* ]! R: }8 M) n* G `: g( b1 X modelActions.createActionTo$message
! F$ i3 p5 T+ L' N (heat, new Selector (heat.getClass (), "updateLattice", false));0 s# P' K% ^; W& i+ L3 [% k! G i7 I
} catch (Exception e) {3 r0 H4 m0 ? a1 a; K3 M
System.err.println("Exception updateLattice: " + e.getMessage ());
! D, n$ R' C* {6 k. p- T. w; w [ }
7 w/ X9 l0 q! {2 M- y& E8 S. j
& u5 |7 S- @: R. V; ^ // Then we create a schedule that executes the
& ^. X& T2 ~9 e- Y3 V5 P // modelActions. modelActions is an ActionGroup, by itself it
$ v Y% F4 J1 k; Q // has no notion of time. In order to have it executed in8 J1 V6 S& q2 V$ d
// time, we create a Schedule that says to use the- A3 t4 ^1 M# @/ o
// modelActions ActionGroup at particular times. This
/ N X+ O( g5 R3 u$ w // schedule has a repeat interval of 1, it will loop every, J, z! m) K9 |4 j# G; I
// time step. The action is executed at time 0 relative to& f3 ~& c `. e7 t- r( y/ O
// the beginning of the loop.
z" g' P: u6 P) P) z6 ]& |* t
0 q0 H! |7 W' a // This is a simple schedule, with only one action that is* B7 e' g6 X% q( V6 T* n
// just repeated every time. See jmousetrap for more
; x l2 E; p3 A+ B9 ]- Y* {8 Z/ n // complicated schedules.- g1 A" n* G9 M! Z
+ p5 u$ D6 x% g8 S& d8 P7 a
modelSchedule = new ScheduleImpl (getZone (), 1);9 b' R* ]1 X5 W; S9 C8 P
modelSchedule.at$createAction (0, modelActions);
/ d# |$ ~8 H1 \0 k* K$ {7 S
|" G/ K) m7 s! Q, { return this;' g; A/ w3 ?4 Q6 J6 }- o5 B; }: p5 o
} |