HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:' x3 `! q' D& w1 ?+ f/ ^2 B
/ |" E. m( _. }) w+ ?/ \
public Object buildActions () {
0 W- u8 o: r+ A0 x% n# S8 z super.buildActions();5 D% A" z# J9 i( v* |
- }5 z9 Y6 E \. b, f# H
// Create the list of simulation actions. We put these in
7 N' v- E; V# Y$ X6 f // an action group, because we want these actions to be# J$ l$ O3 L+ K
// executed in a specific order, but these steps should# H1 C9 h6 _% Y/ m: z3 ]# S2 s' ~) g+ K
// take no (simulated) time. The M(foo) means "The message/ |. M$ k( u' t' G( K9 Y
// called <foo>". You can send a message To a particular) |' p1 D( e' l: C( q
// object, or ForEach object in a collection.6 P! t6 u/ ]7 W0 Q& M
% [' y& j, T# K' C l // Note we update the heatspace in two phases: first run2 p) i3 z8 c+ L5 o7 u. a# X
// diffusion, then run "updateWorld" to actually enact the
8 I6 P9 x6 \9 h i/ Q6 s9 N // changes the heatbugs have made. The ordering here is
2 u; l/ C* v/ U' g // significant!
$ m: b5 n, {, M8 o; [9 n
0 V& ?, O* a5 \+ {$ |2 ~ // Note also, that with the additional
! s | J% S/ u8 y) N // `randomizeHeatbugUpdateOrder' Boolean flag we can. ?$ Q4 G$ d& z5 m4 M+ T
// randomize the order in which the bugs actually run+ x8 c, u: p' }# u
// their step rule. This has the effect of removing any
' j1 M; k$ c: x6 T // systematic bias in the iteration throught the heatbug8 r0 N( ~4 O. S& \5 M- ~0 Y& l
// list from timestep to timestep
9 I& b% h# t' I" j A+ ] / J! H; H2 v7 g% U5 i/ i
// By default, all `createActionForEach' modelActions have6 P' [/ {7 a2 R D L$ q4 p
// a default order of `Sequential', which means that the; X0 p, _8 V& \: u9 q
// order of iteration through the `heatbugList' will be7 @1 ?7 f; W4 S0 d
// identical (assuming the list order is not changed1 F% U. D2 r- {& _8 X; J8 }/ V8 q; g
// indirectly by some other process).& r" ?! @& N, i' s5 t& l
% [& ~$ D, r& O& E' Y
modelActions = new ActionGroupImpl (getZone ());9 u* ]5 M' G5 _) q
& e/ w' Z! |- C! Q( L5 ` try {2 h4 P' Z5 J2 H6 A
modelActions.createActionTo$message
) r: w0 \6 N ], z (heat, new Selector (heat.getClass (), "stepRule", false));0 |7 Y! A( O$ @* a' s" b
} catch (Exception e) {
/ L# G5 D' [6 F+ m System.err.println ("Exception stepRule: " + e.getMessage ());
& J4 N" `5 U9 q* s x' F! W }
0 ~4 E3 l# Z: f/ O$ m, Q& f4 ?
$ l) r# H3 o, v7 k' R& g' O9 N try {! z2 J$ \5 Y# [+ o
Heatbug proto = (Heatbug) heatbugList.get (0);1 g9 O4 c# [2 }& i' L( n* M
Selector sel = - \( t% M4 Y1 K- |0 u; i- d+ s
new Selector (proto.getClass (), "heatbugStep", false);4 X+ m8 e' k4 v+ ~
actionForEach =
' x T3 V1 R6 ^ modelActions.createFActionForEachHomogeneous$call' n1 A0 J. l3 u+ v7 m/ @
(heatbugList,
0 P9 ^- ~0 N( Z% O9 D$ x% C new FCallImpl (this, proto, sel,2 H2 `9 u/ B1 N3 ]
new FArgumentsImpl (this, sel)));9 W) a# r& Y1 Y0 Q+ U( _
} catch (Exception e) {
9 U# E1 x: l& T9 m& K e.printStackTrace (System.err);' D- Q8 V$ m1 L( b" w0 r& J5 ?
}
0 o/ ~0 Y4 J. L2 {
* N9 H. J3 @2 u1 \ syncUpdateOrder ();
* r: \) Y7 V+ V3 |. [# a* H: C
+ d3 R" n3 h6 `( o4 x2 n try {
( n4 y: r0 T# }# d' X! n) I modelActions.createActionTo$message # x! ?* A0 D& V: O5 A
(heat, new Selector (heat.getClass (), "updateLattice", false));$ ?% v9 H# D) f% ^$ K
} catch (Exception e) {
2 j" I! S0 W e; B System.err.println("Exception updateLattice: " + e.getMessage ());; U$ x( R, ^" }! p) s
}. x' K! G$ C1 J* Z: ]. ^
; ?6 u7 o2 E, l. Q+ a h // Then we create a schedule that executes the
& X. J7 _0 q( |: S: _$ e // modelActions. modelActions is an ActionGroup, by itself it
( k1 V" _7 n2 ~2 F/ M1 m" X // has no notion of time. In order to have it executed in
: G* e: L% H( H3 W0 @" B5 G& S$ ] // time, we create a Schedule that says to use the
7 d+ b% y/ G- C6 p. m/ f // modelActions ActionGroup at particular times. This* r, b5 T( E, c( t5 n
// schedule has a repeat interval of 1, it will loop every- U- p2 @3 F. I/ C- ]6 h/ r
// time step. The action is executed at time 0 relative to0 b+ P% n* v, t1 s# T
// the beginning of the loop.7 N' ?# P7 N% g! i& h3 e
8 d( b6 j/ B' q // This is a simple schedule, with only one action that is
4 D2 x; @9 E; P& g1 U p6 ^& I // just repeated every time. See jmousetrap for more
& g* n) \5 E" n" k: l% T1 P // complicated schedules.
- X& G) S7 U% r' s% I2 j
) X1 _5 E, M! K! f8 U( o7 a- c modelSchedule = new ScheduleImpl (getZone (), 1);" F$ b& K) j7 y( W
modelSchedule.at$createAction (0, modelActions);, b. h/ f1 \9 q# f, j
1 u- ]2 a9 c' P2 q6 P8 q
return this;
9 L% E' h" D" ~5 S, V1 e } |