Mercurial > hg > Members > nobuyasu > Alice > DPPAlice
changeset 14:33727db069c2
add ScheduleCodeSegment. modified CodeSegment
author | one |
---|---|
date | Wed, 28 Nov 2012 17:41:42 +0900 |
parents | 888388e47fe5 |
children | 7e91e919a7de |
files | src/alice/test/dpp/codesegment/EatCodeSegment.java src/alice/test/dpp/codesegment/PutForkCodeSegment.java src/alice/test/dpp/codesegment/ScheduleCodeSegment.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 src/alice/test/dpp/model/Philosophy.java |
diffstat | 8 files changed, 121 insertions(+), 59 deletions(-) [+] |
line wrap: on
line diff
--- a/src/alice/test/dpp/codesegment/EatCodeSegment.java Wed Nov 28 16:21:34 2012 +0900 +++ b/src/alice/test/dpp/codesegment/EatCodeSegment.java Wed Nov 28 17:41:42 2012 +0900 @@ -3,29 +3,23 @@ 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()); + public EatCodeSegment(String pName) { + arg1.setKey(pName); } @Override public void run() { + Philosophy phil = arg1.asClass(Philosophy.class); 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); + phil.setState(Philosophy.State.PUTFORK); + new ScheduleCodeSegment(phil.getName()); + ods.update("local", phil.getName(), phil); } }
--- a/src/alice/test/dpp/codesegment/PutForkCodeSegment.java Wed Nov 28 16:21:34 2012 +0900 +++ b/src/alice/test/dpp/codesegment/PutForkCodeSegment.java Wed Nov 28 17:41:42 2012 +0900 @@ -8,24 +8,27 @@ public class PutForkCodeSegment extends CodeSegment { - Philosophy phil; Receiver arg1 = ids.create(CommandType.TAKE); Receiver arg2 = ids.create(CommandType.TAKE); + Receiver arg3 = ids.create(CommandType.TAKE); - public PutForkCodeSegment(Philosophy p) { - phil = p; - arg1.setKey(phil.getLeftFork()); - arg2.setKey(phil.getRightFork()); + public PutForkCodeSegment(String pName, String rFork, String lFork) { + arg1.setKey(pName); + arg2.setKey(lFork); + arg3.setKey(rFork); } @Override public void run() { + Philosophy phil = arg1.asClass(Philosophy.class); + Fork leftFork = arg2.asClass(Fork.class); + Fork rightFork = arg3.asClass(Fork.class); 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); + leftFork.onTheTable(phil.getName()); + rightFork.onTheTable(phil.getName()); + phil.setState(Philosophy.State.THINK); + new ScheduleCodeSegment(phil.getName()); + ods.update("local", phil.getName(), phil); ods.update("local", leftFork.getForkName(), leftFork); ods.update("local", rightFork.getForkName(), rightFork); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/alice/test/dpp/codesegment/ScheduleCodeSegment.java Wed Nov 28 17:41:42 2012 +0900 @@ -0,0 +1,38 @@ +package alice.test.dpp.codesegment; + +import alice.codesegment.CodeSegment; +import alice.datasegment.CommandType; +import alice.datasegment.Receiver; +import alice.test.dpp.model.Philosophy; + +public class ScheduleCodeSegment extends CodeSegment { + + Receiver arg1 = ids.create(CommandType.TAKE); + + public ScheduleCodeSegment(String pName) { + arg1.setKey(pName); + } + + @Override + public void run() { + Philosophy phil = arg1.asClass(Philosophy.class); + switch(phil.getState()) { + case THINK: + new ThinkCodeSegment(phil.getName()); + break; + case TAKEFORK: + new TakeForkCodeSegment(phil.getName(), phil.getRightFork(), phil.getLeftFork() ); + break; + case EAT: + new EatCodeSegment(phil.getName()); + break; + case PUTFORK: + new PutForkCodeSegment(phil.getName(), phil.getRightFork(), phil.getLeftFork()); + break; + default: + + } + ods.update("local", phil.getName(), phil); + } + +}
--- a/src/alice/test/dpp/codesegment/StartCodeSegment.java Wed Nov 28 16:21:34 2012 +0900 +++ b/src/alice/test/dpp/codesegment/StartCodeSegment.java Wed Nov 28 17:41:42 2012 +0900 @@ -9,10 +9,14 @@ @Override 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")); - + + new ScheduleCodeSegment("phil1"); + new ScheduleCodeSegment("phil2"); + new ScheduleCodeSegment("phil3"); + + ods.update("local", "phil1", new Philosophy("phil1","fork1","fork2")); + ods.update("local", "phil2", new Philosophy("phil2","fork2","fork3")); + ods.update("local", "phil3", 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 Wed Nov 28 16:21:34 2012 +0900 +++ b/src/alice/test/dpp/codesegment/TakeForkCodeSegment.java Wed Nov 28 17:41:42 2012 +0900 @@ -8,30 +8,31 @@ public class TakeForkCodeSegment extends CodeSegment { - Philosophy phil; Receiver arg1 = ids.create(CommandType.TAKE); Receiver arg2 = ids.create(CommandType.TAKE); + Receiver arg3 = ids.create(CommandType.TAKE); + + public TakeForkCodeSegment(String pName, String rFork, String lFork) { + arg1.setKey(pName); + arg2.setKey(rFork); + arg3.setKey(lFork); + } - public TakeForkCodeSegment(Philosophy p) { - phil = p; - arg1.setKey(phil.getLeftFork()); - arg2.setKey(phil.getRightFork()); - } - @Override public void run() { - System.out.println(phil.getName() + " is trying to take the fork."); - Fork leftFork = arg1.asClass(Fork.class); + Philosophy phil = arg1.asClass(Philosophy.class); Fork rightFork = arg2.asClass(Fork.class); - if (leftFork.getFork(phil) && rightFork.getFork(phil) ) { - new EatCodeSegment(phil); + Fork leftFork = arg3.asClass(Fork.class); + System.out.println(phil.getName() + " is trying to take the fork."); + if (leftFork.getFork(phil.getName()) && rightFork.getFork(phil.getName()) ) { + phil.setState(Philosophy.State.EAT); } else { - leftFork.onTheTable(phil); - rightFork.onTheTable(phil); - new TakeForkCodeSegment(phil); + leftFork.onTheTable(phil.getName()); + rightFork.onTheTable(phil.getName()); } + new ScheduleCodeSegment(phil.getName()); + ods.update("local", phil.getName(), phil); ods.update("local", leftFork.getForkName(), leftFork); ods.update("local", rightFork.getForkName(), rightFork); } - }
--- a/src/alice/test/dpp/codesegment/ThinkCodeSegment.java Wed Nov 28 16:21:34 2012 +0900 +++ b/src/alice/test/dpp/codesegment/ThinkCodeSegment.java Wed Nov 28 17:41:42 2012 +0900 @@ -3,29 +3,23 @@ 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()); + public ThinkCodeSegment(String pName) { + arg1.setKey(pName); } @Override public void run() { - System.out.println(phil.getName() + " thinking"); - Fork leftFork = arg1.asClass(Fork.class); - Fork rightFork = arg2.asClass(Fork.class); - new TakeForkCodeSegment(phil); - ods.update("local", leftFork.getForkName(), leftFork); - ods.update("local", rightFork.getForkName(), rightFork); + Philosophy phil = arg1.asClass(Philosophy.class); + System.out.println(phil.getName() + " is thinking"); + phil.setState(Philosophy.State.TAKEFORK); + new ScheduleCodeSegment(phil.getName()); + ods.update("local", phil.getName(), phil); } }
--- a/src/alice/test/dpp/model/Fork.java Wed Nov 28 16:21:34 2012 +0900 +++ b/src/alice/test/dpp/model/Fork.java Wed Nov 28 17:41:42 2012 +0900 @@ -35,18 +35,18 @@ return forkName; } - public boolean getFork(Philosophy phil) { + public boolean getFork(String pName) { if (state == State.ON_THE_TABLE){ state = State.IN_THE_HAND; - owner = phil.getName(); + owner = pName; return true; } else { return false; } } - public boolean onTheTable(Philosophy phil) { - if (owner.equals(phil.getName())) { + public boolean onTheTable(String pName) { + if (owner.equals(pName)) { state = State.ON_THE_TABLE; owner = TABLE; return true;
--- a/src/alice/test/dpp/model/Philosophy.java Wed Nov 28 16:21:34 2012 +0900 +++ b/src/alice/test/dpp/model/Philosophy.java Wed Nov 28 17:41:42 2012 +0900 @@ -1,13 +1,33 @@ package alice.test.dpp.model; +import org.msgpack.annotation.Message; +import org.msgpack.annotation.MessagePackOrdinalEnum; + + + + + +@Message public class Philosophy { - private final String name, leftFork, rightFork; + @MessagePackOrdinalEnum + public static enum State { + THINK, + TAKEFORK, + EAT, + PUTFORK; + } + + private String name, leftFork, rightFork; + private State state; + + public Philosophy() {} public Philosophy(String n,String lFork, String rFork) { name = n; leftFork = lFork; rightFork = rFork; + state = State.THINK; } public Philosophy(Philosophy p) { @@ -16,6 +36,14 @@ rightFork = p.getRightFork(); } + public State getState() { + return state; + } + + public void setState(State s) { + state = s; + } + public String getName() { return name; }