5
|
1 package alice.test.dpp.codesegment;
|
|
2
|
|
3 import alice.codesegment.CodeSegment;
|
|
4 import alice.datasegment.CommandType;
|
|
5 import alice.datasegment.Receiver;
|
|
6 import alice.test.dpp.model.Fork;
|
|
7 import alice.test.dpp.model.Philosophy;
|
|
8
|
|
9 public class TakeForkCodeSegment extends CodeSegment {
|
|
10
|
|
11 Philosophy phil;
|
|
12 Receiver arg1 = ids.create(CommandType.TAKE);
|
|
13 Receiver arg2 = ids.create(CommandType.TAKE);
|
|
14
|
|
15 public TakeForkCodeSegment(Philosophy p) {
|
|
16 phil = p;
|
|
17 arg1.setKey(phil.getLeftFork());
|
|
18 arg2.setKey(phil.getRightFork());
|
|
19 }
|
|
20
|
|
21 @Override
|
|
22 public void run() {
|
|
23 System.out.println(phil + " is trying to take the fork.");
|
|
24 Fork leftFork = arg1.asClass(Fork.class);
|
|
25 Fork rightFork = arg2.asClass(Fork.class);
|
|
26 if (leftFork.getFork(phil) && rightFork.getFork(phil) ) {
|
|
27 new EatCodeSegment(phil);
|
|
28 } else {
|
|
29 leftFork.onTheTable(phil);
|
|
30 rightFork.onTheTable(phil);
|
|
31 }
|
|
32 ods.update("local", leftFork.getForkName(), leftFork);
|
|
33 ods.update("local", rightFork.getForkName(), rightFork);
|
|
34 }
|
|
35
|
|
36 }
|