annotate src/alice/test/dpp/codesegment/TakeForkCodeSegment.java @ 5:2db314b1a970

add EatCodeSegment TakeForkCodeSegment, ForkState, Table
author one
date Sun, 25 Nov 2012 17:08:21 +0900
parents
children b59c04a332a1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
1 package alice.test.dpp.codesegment;
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
2
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
3 import alice.codesegment.CodeSegment;
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
4 import alice.datasegment.CommandType;
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
5 import alice.datasegment.Receiver;
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
6 import alice.test.dpp.model.Fork;
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
7 import alice.test.dpp.model.Philosophy;
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
8
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
9 public class TakeForkCodeSegment extends CodeSegment {
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
10
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
11 Philosophy phil;
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
12 Receiver arg1 = ids.create(CommandType.TAKE);
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
13 Receiver arg2 = ids.create(CommandType.TAKE);
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
14
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
15 public TakeForkCodeSegment(Philosophy p) {
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
16 phil = p;
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
17 arg1.setKey(phil.getLeftFork());
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
18 arg2.setKey(phil.getRightFork());
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
19 }
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
20
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
21 @Override
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
22 public void run() {
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
23 System.out.println(phil + " is trying to take the fork.");
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
24 Fork leftFork = arg1.asClass(Fork.class);
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
25 Fork rightFork = arg2.asClass(Fork.class);
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
26 if (leftFork.getFork(phil) && rightFork.getFork(phil) ) {
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
27 new EatCodeSegment(phil);
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
28 } else {
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
29 leftFork.onTheTable(phil);
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
30 rightFork.onTheTable(phil);
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
31 }
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
32 ods.update("local", leftFork.getForkName(), leftFork);
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
33 ods.update("local", rightFork.getForkName(), rightFork);
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
34 }
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
35
2db314b1a970 add EatCodeSegment TakeForkCodeSegment, ForkState, Table
one
parents:
diff changeset
36 }