HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! b1 S! y8 X: @# E; m# i9 L, X
public Object buildActions () {, S0 _2 Z$ ^! d: Q
super.buildActions();# W5 a4 c: r! a* b, D# {
$ U: R$ v' y* ~% ^" A // Create the list of simulation actions. We put these in
$ [+ f# }) f/ b' ?5 V. n' Y, \3 _9 n: g // an action group, because we want these actions to be2 _% E3 c& W$ w; }* d% g! ~
// executed in a specific order, but these steps should
: j5 A e, z+ B // take no (simulated) time. The M(foo) means "The message9 d# r( \1 x9 w# ?7 h$ T* j
// called <foo>". You can send a message To a particular5 I% v! \% z9 Z4 v' `
// object, or ForEach object in a collection.
1 W9 y8 f* |6 n" K- f. D $ x' L$ C$ E4 ~. }5 A2 D2 u
// Note we update the heatspace in two phases: first run
9 M9 y4 ], @9 j0 n' V7 L. V, q // diffusion, then run "updateWorld" to actually enact the' v; ?3 i; d! L- e
// changes the heatbugs have made. The ordering here is4 X( Z3 o, y2 Q
// significant!( g8 W! d4 D* j$ Z, @- a" w
: P ^1 Z1 X4 p$ H4 A/ \+ q // Note also, that with the additional2 d/ A" |2 g% E3 L" h! m7 u2 a
// `randomizeHeatbugUpdateOrder' Boolean flag we can
) A; e, e5 M) b // randomize the order in which the bugs actually run
/ U( f8 A3 f# { // their step rule. This has the effect of removing any
/ ^& c+ Y1 i. a4 C // systematic bias in the iteration throught the heatbug
0 \4 T9 H) H6 n4 A* i9 U8 m- m // list from timestep to timestep# q, Y4 r) S' Z
3 A: _& W; f( K! Y" y, Y0 e+ ~0 l // By default, all `createActionForEach' modelActions have
! t+ `2 V) n: @' O" r6 K // a default order of `Sequential', which means that the B1 T. D" v5 {3 O5 Y
// order of iteration through the `heatbugList' will be& I: h& J, V; r R2 a; Q: q
// identical (assuming the list order is not changed
- C" U3 N) k' J // indirectly by some other process).4 R* Q3 }- J9 x9 @' u
R, i5 k- F$ z1 ~& A6 e; ] modelActions = new ActionGroupImpl (getZone ());
* G1 m3 h p8 r. }2 j( f7 p1 j
~5 t1 n$ ~( V. t try {2 O2 R* c: ?/ `: o
modelActions.createActionTo$message
I" M, F: j0 n# c5 P# i' ?# X (heat, new Selector (heat.getClass (), "stepRule", false));8 D% h! L$ C0 G' e' W1 R' m* h' o
} catch (Exception e) {) a5 {+ G3 U5 Z" n+ o2 {
System.err.println ("Exception stepRule: " + e.getMessage ());3 p. m9 F! N1 e, \! A3 J l
}& b( J- a0 y3 }5 U' S" Z* j! E* P
5 W- C6 ~& S' h, p
try {! `( V7 b* H7 ?; E/ V
Heatbug proto = (Heatbug) heatbugList.get (0);: {$ p8 S! ~; Z% s7 |
Selector sel =
4 @' K& J) w6 M& [5 w# d' i0 [! { new Selector (proto.getClass (), "heatbugStep", false);
( |; `" W# v: W5 f! S5 U actionForEach =
5 s# z/ w" {# P' G modelActions.createFActionForEachHomogeneous$call8 |! r$ R# s2 `! r3 A% ^* [1 `
(heatbugList,. [$ K2 \$ t: l, F
new FCallImpl (this, proto, sel,! Q! V8 E Y& O; e& B
new FArgumentsImpl (this, sel)));
7 X$ r# U# M t1 A5 Q, F" Q } catch (Exception e) {
, m ]1 V' A- M5 {) L4 E e.printStackTrace (System.err);- V5 r( p/ n# g ?* p: t1 Y! B/ K
}
1 T/ ~& \" H) | _+ \2 B- S# P D: g$ _# L* ]1 M) ]5 S5 Y3 t" _
syncUpdateOrder ();6 E- E: x) \6 j9 q6 p; @
! w/ N w" X" |/ L! S8 W" v) w+ r% ]
try { ?" C4 n& U7 z5 k$ n/ ~
modelActions.createActionTo$message 6 Z$ G# f, ?% p; T4 P; E t* F, Z2 h
(heat, new Selector (heat.getClass (), "updateLattice", false));" V% A: w) \5 d6 B: W' m. k
} catch (Exception e) {6 X. l3 H* a' z5 p4 Q; D
System.err.println("Exception updateLattice: " + e.getMessage ());. |# \: C" a) P9 b/ j. t2 M
}2 t5 Q5 @, d n5 N, q0 l
" |8 q' |, Q- f: R% [6 m9 ^4 S5 @
// Then we create a schedule that executes the
+ L' [( I' l, x7 `4 ?+ d8 n3 N // modelActions. modelActions is an ActionGroup, by itself it' F% m, H- ]& h6 A* m9 I; ]* i' z& @
// has no notion of time. In order to have it executed in+ \8 H, {$ u$ R7 T! C. t$ Y6 b
// time, we create a Schedule that says to use the# u. C; w0 e, H* H5 L2 { l0 i
// modelActions ActionGroup at particular times. This$ o, n% x) A$ R, {2 _
// schedule has a repeat interval of 1, it will loop every
; f4 Z0 U% Q6 ]) d // time step. The action is executed at time 0 relative to8 T) X7 J' f, p7 t7 t# ^
// the beginning of the loop.: J; {: k$ v0 I* O1 A' P
) e! y+ I0 r. h, k+ @) Y v
// This is a simple schedule, with only one action that is
: @7 h+ Y3 o/ @1 x S1 q: w // just repeated every time. See jmousetrap for more/ l. k% W$ s+ l/ c, _
// complicated schedules.
' R: r2 m3 `+ K! ^ 2 o$ W$ ^: d& U4 {. X$ t
modelSchedule = new ScheduleImpl (getZone (), 1);: D( F X% o4 w }* ^3 J
modelSchedule.at$createAction (0, modelActions);
0 s4 H0 H6 m; x- W+ I3 W4 D+ d: T 3 z$ q& k# H3 ~/ S
return this;1 [- Y; _' i5 A% g. I/ ]/ I
} |