HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; j* G! b' q0 C: ]
8 P5 Q. O9 [8 j/ h# x public Object buildActions () {& i! g; b' ?# ^8 E4 H: w6 T8 E) O- M
super.buildActions(); I. q4 x4 u0 C$ T: u! {+ ^: G* E
0 X1 D& c. I# }% T9 q // Create the list of simulation actions. We put these in
3 Q2 F; H% M, p6 i // an action group, because we want these actions to be
: V( X8 h5 y1 ?2 f5 A$ j% ^+ v // executed in a specific order, but these steps should
, h6 K" \. S$ I4 W6 Z // take no (simulated) time. The M(foo) means "The message
" N- P+ E5 i, n+ H0 R // called <foo>". You can send a message To a particular# T2 O3 {: f# T& ]) }' Q
// object, or ForEach object in a collection.
* H7 Z! Z& b) n9 Y( V: o& d ) x6 g U& \" v" S2 ]
// Note we update the heatspace in two phases: first run
1 B; n% w8 S5 s. X ?( J8 E/ k // diffusion, then run "updateWorld" to actually enact the% r: h6 I6 g" a4 Y/ L0 v- I9 V; [7 e3 g
// changes the heatbugs have made. The ordering here is1 T; w( }$ T( K# Z0 x5 ], H
// significant!
: N3 B; k/ Q" H4 m$ j4 _
, q& J8 _ N1 u X0 L3 ]! h // Note also, that with the additional
q, L( _: J! O/ w* l // `randomizeHeatbugUpdateOrder' Boolean flag we can
7 {% F! m( E/ m/ Y) t9 a4 q // randomize the order in which the bugs actually run
+ x0 `& H9 \8 A: h D+ `, E/ g // their step rule. This has the effect of removing any. U7 m/ l. c- f6 w* G
// systematic bias in the iteration throught the heatbug
9 j/ [1 h; @' ~ // list from timestep to timestep
; ~ R' r' q+ n1 Y$ p) y' ~2 m k% |$ o3 |0 H+ f1 X) B
// By default, all `createActionForEach' modelActions have
- N5 r' ^0 I/ R2 ~' C9 k3 l9 k2 i4 M // a default order of `Sequential', which means that the
* V8 c0 K1 S+ G2 E8 h) v k5 e. r // order of iteration through the `heatbugList' will be
$ f! s" k/ E3 u0 k$ C% F // identical (assuming the list order is not changed8 L$ e, B3 Q9 T/ I! G& Z
// indirectly by some other process).% X$ ^2 \1 [% l- w
3 N" H4 W6 T% p K5 b5 t$ F modelActions = new ActionGroupImpl (getZone ());
' n; B2 U5 C6 j" S( o, \8 k3 K" n. _+ D. t
try {
1 ~8 C4 _; B9 D" P+ I4 f modelActions.createActionTo$message. f2 r. B6 Z- F9 f/ }6 x! A
(heat, new Selector (heat.getClass (), "stepRule", false));( t8 k# V4 t6 K Q# }1 H
} catch (Exception e) {2 G9 T) l9 a$ z& @4 s# H
System.err.println ("Exception stepRule: " + e.getMessage ());7 p, G9 n; a! s+ F+ d
}
# Y' ]/ b% [! Y- M7 P' t5 l- |8 }! N# @& R6 V+ R' S _
try {! I2 P. D! n, V- L: y8 d
Heatbug proto = (Heatbug) heatbugList.get (0);4 l$ S! n# I F4 f" T
Selector sel = & I( s/ a4 U6 `) a6 k3 i9 w
new Selector (proto.getClass (), "heatbugStep", false);
/ C( |- _% d6 h B6 G7 G1 o {! O actionForEach =' Z/ t/ S! f |$ @. _8 y! t
modelActions.createFActionForEachHomogeneous$call
- V; X( ^6 p ~' s (heatbugList,3 k: i4 Q; s2 x
new FCallImpl (this, proto, sel,9 q5 E% z- w; D. J% |# A- C
new FArgumentsImpl (this, sel)));
% m/ v/ S- w. ]9 x1 a \$ V/ \ } catch (Exception e) {7 i9 s+ `+ w1 O& _' _0 s$ f! K
e.printStackTrace (System.err);4 Q' }5 g n; |& U
}' g9 N( d+ n: @6 z
- s6 }+ ]: X; N X) E s* C" D
syncUpdateOrder ();8 U. m. d4 I9 m
% b! {- ~& s4 w. y" ? try {& M6 Z7 X+ y# n
modelActions.createActionTo$message
* J; |. L' ^$ t (heat, new Selector (heat.getClass (), "updateLattice", false));! f% m, ~4 o3 p& [( }2 [' R
} catch (Exception e) {
) e! d r4 A" I8 Q8 e System.err.println("Exception updateLattice: " + e.getMessage ());
( X5 V* `1 R+ X C9 J) G" g: `( z }
# m8 u3 y# n, U; f: z ' e5 s9 A0 C: O9 e( H! b% U
// Then we create a schedule that executes the3 Y4 | F" ] _+ l4 E9 o
// modelActions. modelActions is an ActionGroup, by itself it
& m0 \$ W1 h. L/ j$ @# a$ I4 N // has no notion of time. In order to have it executed in- |1 k5 N, L" u7 x
// time, we create a Schedule that says to use the
; O) w0 j3 T. l$ @+ M, G# b+ ]! q3 B // modelActions ActionGroup at particular times. This
2 L5 I! g6 ]2 }* k; m5 Q+ H9 S0 S% @ // schedule has a repeat interval of 1, it will loop every
2 ~$ Q' W& |& n6 D) O // time step. The action is executed at time 0 relative to
7 O9 t. n6 _/ W // the beginning of the loop.
^% ]1 t7 X& l+ i" _7 ^# a4 e2 Z! \# K1 I0 `* m
// This is a simple schedule, with only one action that is# r9 Z0 R/ Q1 E i7 G5 S
// just repeated every time. See jmousetrap for more7 ]7 H ?* s8 `6 a* \* C: q& p: d
// complicated schedules.4 e" F5 f9 C6 {( s2 V' u
3 m Q4 x0 ^, Z2 f8 }: i% G, C. D modelSchedule = new ScheduleImpl (getZone (), 1);
1 [" w; J, d% _4 [6 D" J modelSchedule.at$createAction (0, modelActions);5 }9 N% W9 h( c) J
+ J" J [$ Q) G' k+ [ return this;' U( E; m/ s. ^6 t
} |