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