HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 k; Z" V1 e+ A4 q5 w& Y
5 o5 U X2 B9 P$ H
public Object buildActions () {
8 d& j3 z1 J6 F) u+ V super.buildActions();3 ?- P/ T4 m" I( E" `$ P" @( a7 z
% a5 u% ?3 t" q) k0 e // Create the list of simulation actions. We put these in" @0 e# B4 ^$ L `
// an action group, because we want these actions to be1 `3 N2 S$ O4 t: E
// executed in a specific order, but these steps should
) |' ~3 h" ]) t/ c; I // take no (simulated) time. The M(foo) means "The message8 g* y/ F2 |0 l; f- x# Z9 J3 u$ ]
// called <foo>". You can send a message To a particular# P! A' O5 ]" j) ?1 Z
// object, or ForEach object in a collection.: G- w! c- \) p3 V
. C6 P" z' H0 }$ d! n% M // Note we update the heatspace in two phases: first run7 {) o+ H; [$ s. X' H9 f
// diffusion, then run "updateWorld" to actually enact the
/ I' O7 K' ?2 w: a // changes the heatbugs have made. The ordering here is; V/ Y% v8 {9 t
// significant!$ o3 L- ]2 M2 Y* {9 X6 J
w& o$ r& ^# Q- I // Note also, that with the additional! @; U+ }: N; M, }/ w2 Z
// `randomizeHeatbugUpdateOrder' Boolean flag we can' U6 d4 n: V) F# i# F
// randomize the order in which the bugs actually run
: }4 y4 B' _( o, W+ @ // their step rule. This has the effect of removing any
: W1 y4 p1 G; [$ H // systematic bias in the iteration throught the heatbug
0 R+ R- T' J( Y: c0 ` // list from timestep to timestep
- ^# ]9 W$ r: V+ p4 }* j 7 a- z$ |$ f6 H( M* B
// By default, all `createActionForEach' modelActions have
; s. i7 a, d6 J5 d$ S. L6 d // a default order of `Sequential', which means that the
1 D5 _- P1 c; B // order of iteration through the `heatbugList' will be3 P2 T+ X5 V' r9 T* w
// identical (assuming the list order is not changed
) ], R! O2 L# B% j! E2 x# A3 w // indirectly by some other process).
$ w4 e6 u' F) S# a5 B* U f. K. R, m! \% `$ B
modelActions = new ActionGroupImpl (getZone ());( u6 z5 G4 ] E: w
/ G/ W" F8 e7 u: ~9 k2 ]8 y
try {
# ?6 }3 ]3 y/ \, ~9 N5 Z# u modelActions.createActionTo$message
* k6 u) J8 `- v& N4 W( _ (heat, new Selector (heat.getClass (), "stepRule", false));
$ G) w/ Z, o8 S( Y5 K+ [ } catch (Exception e) {
) w! b* s) o* e) D) G- N0 e' R System.err.println ("Exception stepRule: " + e.getMessage ());& ^4 T; M, M# P! Z/ }3 J
}
% q9 k2 x' w& M* a# m! Z9 r; [% ?( }, k0 T1 Q! ~, i' h
try {6 N3 l% E/ _+ q' t) C
Heatbug proto = (Heatbug) heatbugList.get (0);# T5 G _; v; E- U4 [/ Z
Selector sel =
) d0 f) p: C( \ new Selector (proto.getClass (), "heatbugStep", false);
Y# x9 ~% O% v actionForEach =
* Q/ N& O* P ^6 s) p3 \0 Y2 P: E( G modelActions.createFActionForEachHomogeneous$call0 {$ v B& k& s
(heatbugList,
$ E: g$ ]2 m4 D0 l" t; C new FCallImpl (this, proto, sel,; b5 {' N: _. m0 Q" w) L; w
new FArgumentsImpl (this, sel)));
/ Q: l2 B! ^4 [$ ]8 R* c3 M' E6 K } catch (Exception e) {( i8 `+ W2 l- |8 y7 y5 M
e.printStackTrace (System.err);
- E R% Q" S$ E }
& ~- ^& F* [9 y 6 F) ~3 |6 ~5 J0 s, I* w: w
syncUpdateOrder ();5 _; }, w9 N1 r* [& v
0 i( ]* z- J% I1 O9 p
try {0 V, _" X8 b6 q7 o4 [& K, i5 Q
modelActions.createActionTo$message 0 r @, `' c" Q) E& A3 A
(heat, new Selector (heat.getClass (), "updateLattice", false));5 o8 H( `7 u6 y; K1 q
} catch (Exception e) {3 C, G7 Q% u4 L- Y1 G: m% m
System.err.println("Exception updateLattice: " + e.getMessage ());7 ? i$ p" V3 W2 Y9 \5 {9 h b
}- {& x+ j/ D4 A% [
# g6 l9 W/ J% o4 r: l! D // Then we create a schedule that executes the; Y1 |" L% `8 |! _3 }! Y6 }
// modelActions. modelActions is an ActionGroup, by itself it" a% c7 s) P8 L% i s
// has no notion of time. In order to have it executed in+ ]7 b/ h5 P; L* a# b$ G; j6 Z& f# F
// time, we create a Schedule that says to use the! u- H* q/ H: t: ^
// modelActions ActionGroup at particular times. This
; ^9 K! L+ p4 e( O; q8 ~ // schedule has a repeat interval of 1, it will loop every
8 T! D9 _: _4 y/ u! Z9 z // time step. The action is executed at time 0 relative to
9 r' W8 G7 g" {3 c // the beginning of the loop./ l, ?5 W; m. d0 c) T
- h1 l: B5 [4 T' V- C // This is a simple schedule, with only one action that is0 V0 _' P$ Z# F) U1 R
// just repeated every time. See jmousetrap for more- ?2 W; J! L/ t; V* i5 Q
// complicated schedules.
- N) H/ r& H$ O+ T5 y1 K
( x) C9 x& w" d. ~ modelSchedule = new ScheduleImpl (getZone (), 1);& y; T& R5 _' W) }$ v
modelSchedule.at$createAction (0, modelActions);
/ ~& I% [' l0 u' }2 o& e3 Q% S
' w; G$ v" E5 f- Q return this;) X Z- ^1 t; D$ b# o, W
} |