Mercurial > hg > Members > nobuyasu > Alice > DPPAlice
changeset 12:bfaaebb50a2e
modified Fork.java
author | one |
---|---|
date | Sun, 25 Nov 2012 18:49:29 +0900 |
parents | 37b981eb4533 |
children | 888388e47fe5 |
files | src/alice/test/dpp/codesegment/EatCodeSegment.java src/alice/test/dpp/codesegment/PutForkCodeSegment.java src/alice/test/dpp/codesegment/StartCodeSegment.java src/alice/test/dpp/codesegment/TakeForkCodeSegment.java src/alice/test/dpp/codesegment/ThinkCodeSegment.java src/alice/test/dpp/model/Fork.java |
diffstat | 6 files changed, 41 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/src/alice/test/dpp/codesegment/EatCodeSegment.java Sun Nov 25 17:53:40 2012 +0900 +++ b/src/alice/test/dpp/codesegment/EatCodeSegment.java Sun Nov 25 18:49:29 2012 +0900 @@ -1,19 +1,31 @@ package alice.test.dpp.codesegment; import alice.codesegment.CodeSegment; +import alice.datasegment.CommandType; +import alice.datasegment.Receiver; +import alice.test.dpp.model.Fork; import alice.test.dpp.model.Philosophy; public class EatCodeSegment extends CodeSegment { Philosophy phil; + Receiver arg1 = ids.create(CommandType.TAKE); + Receiver arg2 = ids.create(CommandType.TAKE); + public EatCodeSegment(Philosophy p) { phil = p; + arg1.setKey(phil.getLeftFork()); + arg2.setKey(phil.getRightFork()); } @Override public void run() { - System.out.println(phil.getName() + " is eating."); + System.out.println(phil.getName() + " is eating."); + Fork leftFork = arg1.asClass(Fork.class); + Fork rightFork = arg2.asClass(Fork.class); new PutForkCodeSegment(phil); + ods.update("local", leftFork.getForkName(), leftFork); + ods.update("local", rightFork.getForkName(), rightFork); } }
--- a/src/alice/test/dpp/codesegment/PutForkCodeSegment.java Sun Nov 25 17:53:40 2012 +0900 +++ b/src/alice/test/dpp/codesegment/PutForkCodeSegment.java Sun Nov 25 18:49:29 2012 +0900 @@ -20,14 +20,14 @@ @Override public void run() { - System.out.println(phil +" put fork."); + System.out.println(phil.getName() +" put fork."); Fork leftFork = arg1.asClass(Fork.class); Fork rightFork = arg2.asClass(Fork.class); leftFork.onTheTable(phil); rightFork.onTheTable(phil); + new ThinkCodeSegment(phil); ods.update("local", leftFork.getForkName(), leftFork); ods.update("local", rightFork.getForkName(), rightFork); - new ThinkCodeSegment(phil); } }
--- a/src/alice/test/dpp/codesegment/StartCodeSegment.java Sun Nov 25 17:53:40 2012 +0900 +++ b/src/alice/test/dpp/codesegment/StartCodeSegment.java Sun Nov 25 18:49:29 2012 +0900 @@ -10,8 +10,12 @@ public void run() { System.out.println("run StartCodeSegment"); new ThinkCodeSegment(new Philosophy("phil1","fork1","fork2")); + new ThinkCodeSegment(new Philosophy("phil2","fork2","fork3")); + new ThinkCodeSegment(new Philosophy("phil3","fork3","fork1")); + ods.update("local", "fork1", new Fork("fork1")); ods.update("local", "fork2", new Fork("fork2")); + ods.update("local", "fork3", new Fork("fork3")); }
--- a/src/alice/test/dpp/codesegment/TakeForkCodeSegment.java Sun Nov 25 17:53:40 2012 +0900 +++ b/src/alice/test/dpp/codesegment/TakeForkCodeSegment.java Sun Nov 25 18:49:29 2012 +0900 @@ -20,7 +20,7 @@ @Override public void run() { - System.out.println(phil + " is trying to take the fork."); + System.out.println(phil.getName() + " is trying to take the fork."); Fork leftFork = arg1.asClass(Fork.class); Fork rightFork = arg2.asClass(Fork.class); if (leftFork.getFork(phil) && rightFork.getFork(phil) ) {
--- a/src/alice/test/dpp/codesegment/ThinkCodeSegment.java Sun Nov 25 17:53:40 2012 +0900 +++ b/src/alice/test/dpp/codesegment/ThinkCodeSegment.java Sun Nov 25 18:49:29 2012 +0900 @@ -1,22 +1,31 @@ package alice.test.dpp.codesegment; import alice.codesegment.CodeSegment; +import alice.datasegment.CommandType; +import alice.datasegment.Receiver; +import alice.test.dpp.model.Fork; import alice.test.dpp.model.Philosophy; public class ThinkCodeSegment extends CodeSegment { Philosophy phil; + Receiver arg1 = ids.create(CommandType.TAKE); + Receiver arg2 = ids.create(CommandType.TAKE); public ThinkCodeSegment(Philosophy p) { phil = p; + arg1.setKey(phil.getLeftFork()); + arg2.setKey(phil.getRightFork()); } @Override public void run() { + System.out.println(phil.getName() + " thinking"); + Fork leftFork = arg1.asClass(Fork.class); + Fork rightFork = arg2.asClass(Fork.class); double rand = Math.random(); if (rand < 0.5) { try { - System.out.println(phil + " is thinking."); Thread.sleep(10); new ThinkCodeSegment(phil); } catch (InterruptedException e) { @@ -25,6 +34,9 @@ } else { new TakeForkCodeSegment(phil); } + + ods.update("local", leftFork.getForkName(), leftFork); + ods.update("local", rightFork.getForkName(), rightFork); } }
--- a/src/alice/test/dpp/model/Fork.java Sun Nov 25 17:53:40 2012 +0900 +++ b/src/alice/test/dpp/model/Fork.java Sun Nov 25 18:49:29 2012 +0900 @@ -1,22 +1,25 @@ package alice.test.dpp.model; import org.msgpack.annotation.Message; +import org.msgpack.annotation.MessagePackOrdinalEnum; @Message public class Fork { - private final String forkName; + + private String forkName; private String owner; private State state; private final String TABLE = "TABLE"; - - @Message + + @MessagePackOrdinalEnum public enum State { ON_THE_TABLE, IN_THE_HAND; } + public Fork() {} public Fork(String n) { forkName = n; @@ -27,11 +30,11 @@ public State getState() { return state; } - + public String getForkName() { return forkName; } - + public boolean getFork(Philosophy phil) { if (state == State.ON_THE_TABLE){ state = State.IN_THE_HAND;