HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:: t$ U3 k" W! G( q
. Q( l3 V+ O2 c* a5 B& S$ C
public Object buildActions () {: ?( F! ~- m5 |0 N3 i' W
super.buildActions();
& F' y+ J. O0 g; e6 g; f* y 2 V5 w- M( U) i0 U4 f& W3 S
// Create the list of simulation actions. We put these in
8 Y y- z* O& X% R0 A4 W y // an action group, because we want these actions to be) m8 S, f. G+ J
// executed in a specific order, but these steps should
2 i! Z) c7 e2 W$ Y$ q // take no (simulated) time. The M(foo) means "The message W- W5 c [! H. o! W
// called <foo>". You can send a message To a particular
6 L2 D7 }; A; w5 \ // object, or ForEach object in a collection.
* w5 g5 X9 C9 c6 H# H
! b: `2 H7 f6 A; K | // Note we update the heatspace in two phases: first run- k* X' I* _* W( s) T9 c" k7 Q
// diffusion, then run "updateWorld" to actually enact the
" ^% C8 `" S/ u' Y+ L // changes the heatbugs have made. The ordering here is. p2 x8 H$ F: B, O- }
// significant!6 F4 H- ?$ y4 O* \
: d6 E. I# `2 I2 ^0 V& W u8 j
// Note also, that with the additional
- @) w& y3 W# [* h // `randomizeHeatbugUpdateOrder' Boolean flag we can! W: T% P! S J% P8 q) p/ T
// randomize the order in which the bugs actually run
3 V( Q, y2 r$ A4 J // their step rule. This has the effect of removing any
& ^' F4 X( U0 h; g# R- I7 H! p // systematic bias in the iteration throught the heatbug
8 Y0 _# x; J3 q* m* k // list from timestep to timestep1 W' I; @$ D; X1 Q1 I
* I2 f. p* O q+ s! e1 |
// By default, all `createActionForEach' modelActions have7 d! x/ `4 y1 J/ n; J& O ~
// a default order of `Sequential', which means that the% i' q( O6 q% |
// order of iteration through the `heatbugList' will be1 t: M- | {$ D5 G* }
// identical (assuming the list order is not changed8 u( W* T2 M5 P7 x+ x6 w/ X
// indirectly by some other process).
+ I4 f, ]" q: n2 l; t/ r1 b9 m
- K4 r. a% x6 I. P) Y: o modelActions = new ActionGroupImpl (getZone ());
& u( G- {) g' _2 ]
$ v# l0 T) k7 b+ L" E! V. q try {
3 a; U1 B, T. b! l3 U3 a% R$ r5 O modelActions.createActionTo$message- A- `2 j, H6 ^, g2 I* \
(heat, new Selector (heat.getClass (), "stepRule", false));6 v/ F8 K: K* ~& B3 v# L7 H& i/ A
} catch (Exception e) {: _# J! M* g" F3 ^: o& \5 x
System.err.println ("Exception stepRule: " + e.getMessage ());6 D2 h; U, h& ]' c% x6 F- `
}
9 w) @1 i) a* @9 w9 v% J' |9 U1 O: x) r
try {
6 D! _! c' z$ L- S+ g! }2 i Heatbug proto = (Heatbug) heatbugList.get (0);8 M3 Y3 G( D( V) w
Selector sel = $ y5 `! j$ h7 a2 w7 u: ^: ~
new Selector (proto.getClass (), "heatbugStep", false);
. t! H, x6 ~1 h' Y. n1 K actionForEach =
# T$ U; a [% {9 T( W/ p/ K modelActions.createFActionForEachHomogeneous$call
+ o- R' A3 U6 V& W (heatbugList,& \+ D8 k5 w* E% \; A/ Y+ I5 k& T
new FCallImpl (this, proto, sel,
( C# E( S/ s3 b8 a; M, f( z new FArgumentsImpl (this, sel)));/ M7 A2 t* x( W6 l5 s" g. O5 c
} catch (Exception e) {8 u1 L9 b2 \! S: O" B$ s
e.printStackTrace (System.err);( m6 n% v0 [5 {& V X
}
7 x1 f/ V9 i7 o: T, s
' M7 b" W/ M$ A3 S" W' i, P2 C syncUpdateOrder ();- c# R/ m j- E2 P+ H1 {0 X
+ j) i0 @0 w H try {
- p$ s5 _) | K# _1 s modelActions.createActionTo$message
- B" h4 Y- v- `+ f8 Y$ `8 k (heat, new Selector (heat.getClass (), "updateLattice", false));1 V# P1 n0 p. T n/ q5 V
} catch (Exception e) {
* f$ f7 Y6 o6 \/ B1 d- O2 o8 C System.err.println("Exception updateLattice: " + e.getMessage ());+ k7 g/ s) ]6 {$ f7 Q9 G
}
, V. D8 I/ f6 R `- I
( m% m% y4 }3 Y; _ // Then we create a schedule that executes the
, }% y P7 P" r // modelActions. modelActions is an ActionGroup, by itself it# o% q/ k% I! Z# K! L, O! @
// has no notion of time. In order to have it executed in1 l& y! S! T0 _: R" T. {
// time, we create a Schedule that says to use the
0 G, c' J& h/ }1 B // modelActions ActionGroup at particular times. This
5 }' L: H; ~; R. ]6 }+ V, Z // schedule has a repeat interval of 1, it will loop every
3 e' W* I6 J& u' ~" ?, h! i // time step. The action is executed at time 0 relative to6 A" \8 e; H( w5 U6 H
// the beginning of the loop.. d! j4 J! t4 ~& K. e9 f0 v
) n4 m+ y! \3 Z" A( M& I# k; z
// This is a simple schedule, with only one action that is
8 B+ q- n2 C" Y // just repeated every time. See jmousetrap for more
3 l+ ?: G' O5 k // complicated schedules.
$ g( M, u, q7 b- Q% c + n4 s1 x: f+ A1 k3 |, P" h
modelSchedule = new ScheduleImpl (getZone (), 1);
4 F; V% v5 }- p modelSchedule.at$createAction (0, modelActions);3 U( ^% R4 a& w# k0 F" d
7 r8 X2 X4 h, m2 l1 Q8 m8 r return this;+ X* f4 c$ e4 s x
} |