HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 ~, x) C; l4 O }
3 E {# Z, _+ Y public Object buildActions () {
' E4 a' I+ r0 | super.buildActions();
# m2 V% _" M: k$ \ M) ?
# a0 L) }; k! v8 l // Create the list of simulation actions. We put these in
& @- V) Z) g; u7 u- ]7 P // an action group, because we want these actions to be
( ]2 y5 I5 T7 Y$ G, N // executed in a specific order, but these steps should% G! a* l: I9 [. O
// take no (simulated) time. The M(foo) means "The message' B) A# n: x Q/ U0 ~& X; c' U M$ F
// called <foo>". You can send a message To a particular
! m7 Z5 }- e& a // object, or ForEach object in a collection.
2 e7 l4 I2 ?& d# N. W4 L T ) ~# a2 Q9 E8 f" M7 X
// Note we update the heatspace in two phases: first run
- ?5 }: ^8 A5 @3 n! e2 {5 c // diffusion, then run "updateWorld" to actually enact the
9 }1 z" ^) W' e5 @8 J // changes the heatbugs have made. The ordering here is3 c# B) i" F( ~. s! p6 Z. b) L
// significant!
|6 L2 G: C: j* I4 J 9 d# L% j) W% N/ M/ \2 r
// Note also, that with the additional! l* |' }/ n6 t2 d' }+ U% s- q( v! p
// `randomizeHeatbugUpdateOrder' Boolean flag we can
( r4 G4 ]5 M/ c$ ~' F' T // randomize the order in which the bugs actually run
/ l0 p. r: q. z y" a3 } // their step rule. This has the effect of removing any
0 t, ^. G, X8 o* A // systematic bias in the iteration throught the heatbug
% i/ Q0 |; a) f+ X* D // list from timestep to timestep
/ h$ I2 k/ b- k7 V / \: Q; H3 ^( q" a+ K3 L
// By default, all `createActionForEach' modelActions have0 m0 O" \ W3 y/ v3 _: m
// a default order of `Sequential', which means that the( E" k1 B6 k: n l6 Y5 q* M
// order of iteration through the `heatbugList' will be7 O0 q: C) y. b7 n3 U) o) T% g9 r
// identical (assuming the list order is not changed. K. d- c5 y1 G$ t
// indirectly by some other process).
$ n; _ z) p. Z3 o' K* S: o
8 [2 z- p; j; w( U: n+ a modelActions = new ActionGroupImpl (getZone ());* f: C u, k6 ]! Y+ f& D1 d8 H
5 A5 h$ F: G5 T# G9 E
try {1 a$ l5 m1 X! Z! N8 p1 X% m) |
modelActions.createActionTo$message
v9 w, Z! t% p3 z (heat, new Selector (heat.getClass (), "stepRule", false)); m$ @9 J( }3 E9 n- @3 E
} catch (Exception e) {( }" X8 C/ j& G( ^8 ?$ I' J
System.err.println ("Exception stepRule: " + e.getMessage ());
0 {! N3 [* r% @" J }
! O# R; f) d m. G8 v" O/ R7 S5 I) `" Q T/ Y' S
try {
% H/ I$ [- w" V& V& u2 q Heatbug proto = (Heatbug) heatbugList.get (0);
" d( q' i+ c: }! B2 f Selector sel = 3 H0 `4 E% K9 I7 s" H* u; L4 h6 H
new Selector (proto.getClass (), "heatbugStep", false);
0 s/ [1 u9 q3 b: N* W actionForEach =
b5 A3 P0 [0 R) P$ `1 I# S modelActions.createFActionForEachHomogeneous$call
* E% h9 n8 l9 x0 [$ _: t (heatbugList,
/ E/ c" A3 T! L* u6 ? new FCallImpl (this, proto, sel,7 G8 w/ K4 A1 |/ m0 A! x4 X- ]
new FArgumentsImpl (this, sel))); B8 P F0 }' d0 r
} catch (Exception e) {# Z# V: r4 {$ G( I, H' L P
e.printStackTrace (System.err);
3 r9 i' K: e5 C- Z+ y2 b- U8 Z }
' u6 N2 }/ _9 y: U! j: R / {5 w. o) ~$ c6 p& U( R% Q0 ?
syncUpdateOrder ();) ?% u" e: q8 K1 U/ n* M
& L' |9 q. d& K; t8 W& r
try {, q+ j ? }$ P0 u# y5 f& D
modelActions.createActionTo$message
. ^2 a) ~* }- [; R, s4 |" W (heat, new Selector (heat.getClass (), "updateLattice", false));
2 q9 B8 B# \9 ~, [, a o0 Y: I } catch (Exception e) {. f; t2 K/ T/ o5 s1 M
System.err.println("Exception updateLattice: " + e.getMessage ());
/ Q6 R% m% x# q2 v( X) b6 u }& k& ~& |: Y+ u
4 H9 q+ p9 v. q) N" P# E
// Then we create a schedule that executes the
$ c! f/ Q/ R f1 A3 B- \ // modelActions. modelActions is an ActionGroup, by itself it
& v7 g, f( ]' e9 _7 B& ~ // has no notion of time. In order to have it executed in& H1 }9 T) d% ?7 [. K5 w f, Q! h& T
// time, we create a Schedule that says to use the
: f1 b# w6 e1 |% r& j1 ?6 | // modelActions ActionGroup at particular times. This
" W' u! A/ D) a& i1 U3 Q7 L // schedule has a repeat interval of 1, it will loop every( m% W4 q$ O% ?2 U, Q
// time step. The action is executed at time 0 relative to
# O0 M4 l3 w( j3 `5 J( f* A" } // the beginning of the loop.0 F4 j, k# A# X
/ r X4 U: ~: t* Y. x: A- O7 G
// This is a simple schedule, with only one action that is" H& K3 o7 D! U7 q3 v' H
// just repeated every time. See jmousetrap for more3 u4 R, F7 K) q2 X$ `2 U% R( Y$ z
// complicated schedules.* ^/ y7 H8 H3 |/ ~0 g! J' y% G
$ j+ @, b: d1 X) \ modelSchedule = new ScheduleImpl (getZone (), 1);
# f6 t) t. } k) I9 J' ~/ @- p modelSchedule.at$createAction (0, modelActions);: x/ W, D; y) @3 ~' y b
& A( X/ F3 j8 D1 ?4 M* a& z& G return this;
, ?# J1 ~3 L, f8 i } |