HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 ~) \; I7 B5 A4 z( t3 Z
~/ b+ p9 X5 n- _* l: t6 Q U0 q" X
public Object buildActions () {' E+ P# h, ?) N) g8 G7 D C
super.buildActions();" t+ `3 m+ i5 K# C( D; Q+ n
+ p" |9 O8 t. W. p/ x) k // Create the list of simulation actions. We put these in
, s) f% }( ?) z. b9 z2 A1 z+ \ // an action group, because we want these actions to be
' s) q1 S+ r1 v, {6 L( \) N6 j; O // executed in a specific order, but these steps should
2 v+ U" H" H, g2 z$ _ // take no (simulated) time. The M(foo) means "The message
F% M% h0 ^3 U3 M7 X) ~6 I // called <foo>". You can send a message To a particular+ D* Y! x8 r& w' u0 s3 T) B
// object, or ForEach object in a collection.
" @4 A1 t( E2 u! y, ?
/ d7 G) U/ x, S, r& ]% Y3 l // Note we update the heatspace in two phases: first run
4 v3 }0 i1 Q: V0 C' o9 A // diffusion, then run "updateWorld" to actually enact the4 _ l: m4 P& [7 V
// changes the heatbugs have made. The ordering here is
- v4 H2 I( x% e! A // significant!; c. f2 P" q/ S0 ]
9 |) B& l0 T7 g2 S // Note also, that with the additional
/ n5 O8 g" o6 h4 m; ]' U" B // `randomizeHeatbugUpdateOrder' Boolean flag we can
5 G( ]1 w2 ~5 J! s9 t, z, D+ s // randomize the order in which the bugs actually run
" z* I( {6 g5 J8 |# g3 U0 }. Q! r // their step rule. This has the effect of removing any: Q$ H p8 C2 [- Z6 T/ N* c& { U$ X
// systematic bias in the iteration throught the heatbug
0 q+ c+ }8 K" h // list from timestep to timestep
* P. Z" B: }9 m. c. V3 P! D: i$ I
/ ?1 s8 I# [. ^0 y // By default, all `createActionForEach' modelActions have
, m% t7 r j* e // a default order of `Sequential', which means that the
; i% v0 F7 U% H0 b8 i2 K // order of iteration through the `heatbugList' will be
4 F6 F. S5 o, }- G) l8 j: E // identical (assuming the list order is not changed- m0 J0 F" f6 X6 I) S. O
// indirectly by some other process).
& `) n6 b: Y W4 {; ]3 B0 ] , Y& T( c5 D+ \* a; y
modelActions = new ActionGroupImpl (getZone ());2 w N# j3 t8 @. y4 O# _
, Z4 P9 u/ j8 f! ^+ ~! X try {: k/ e* K3 s6 F+ W8 L3 D; b$ u
modelActions.createActionTo$message
) F+ n& p Z2 d6 O; _$ X/ w (heat, new Selector (heat.getClass (), "stepRule", false));
0 a. D) ?) v; t% r& M+ E } catch (Exception e) {# @! I6 Z; k& t4 g) q- n8 S4 c
System.err.println ("Exception stepRule: " + e.getMessage ());
; p/ k, s M2 ~. W; P6 @& d5 }; } }7 Y' D2 B' y, X' {' C' J" i
/ j- ^3 M& F- \0 d8 w7 o( T0 e try {
1 N. {1 T8 q! K7 I' t- A Heatbug proto = (Heatbug) heatbugList.get (0);: L2 b$ X/ n& @, @( `
Selector sel = : |/ d. G3 f) J& W9 m/ H3 z3 [
new Selector (proto.getClass (), "heatbugStep", false);+ p( n# Y" C; N* t2 u$ l! [
actionForEach =
0 }# l6 Z9 d7 p( \: ?4 P% ? modelActions.createFActionForEachHomogeneous$call
5 l d8 O; V& w8 r) p' j" c% N1 b (heatbugList,( h* H1 I6 C. d0 N% c" _
new FCallImpl (this, proto, sel,( Y y/ k0 m5 P! q* `
new FArgumentsImpl (this, sel)));* o9 u: n9 L a9 k
} catch (Exception e) {
% k, v8 H! {& r* | e.printStackTrace (System.err);" l8 o$ i t. n9 ?& l! V
}
( B# T3 V9 q1 G @& v( Z7 a! Y
) u" K' W* u8 w' f syncUpdateOrder ();
" G1 Q9 |5 A) N6 e, A
' F8 o% \; f: m+ [ try { J$ M; d( V: c
modelActions.createActionTo$message * O1 J$ [ h" h, _6 G3 _
(heat, new Selector (heat.getClass (), "updateLattice", false));
5 o7 i1 h y/ R5 u% A } catch (Exception e) {
1 L' x# n. v# l9 G6 n System.err.println("Exception updateLattice: " + e.getMessage ());$ w1 }2 ^$ P$ {& i4 R
}: e2 j% H3 h! g+ H* a
0 [# w9 ?# v) K( f( `/ h. o // Then we create a schedule that executes the
2 q9 W. t& J7 s4 H // modelActions. modelActions is an ActionGroup, by itself it
( T" ~+ P! x) i1 C // has no notion of time. In order to have it executed in8 Z, k8 ?. O+ S2 w: a1 E' G! K' c
// time, we create a Schedule that says to use the V5 |0 ]# V) J0 ~! F! R
// modelActions ActionGroup at particular times. This7 g1 W* d2 \) {1 C
// schedule has a repeat interval of 1, it will loop every
/ ~ g2 @# N2 L" [% q // time step. The action is executed at time 0 relative to% d% J/ h$ K6 V: `! S
// the beginning of the loop.
4 J: u+ b, X, q% X% |6 [+ y9 Z7 O: I# c8 V: ^5 t. r# Z0 g- Z* y
// This is a simple schedule, with only one action that is+ o: x# e1 H' b9 `: P2 k
// just repeated every time. See jmousetrap for more
* h. l% W( j, P; o4 r0 M // complicated schedules.) }, M7 `1 K) Z" |% u4 R
' x# y: N9 O/ E) _4 o3 y3 X modelSchedule = new ScheduleImpl (getZone (), 1);: A: A# y- W" N
modelSchedule.at$createAction (0, modelActions);4 U. F: I/ H) f4 W
# f D1 ^* ], q: {/ F0 y& y
return this;$ z) f9 S+ _; h+ l* O4 I6 @/ M
} |