Mercurial > hg > Members > nobuyasu > Alice > DPPAlice
changeset 30:5eb22e2f7169 draft
merge 28, 29
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 29 Jan 2013 17:17:44 +0900 |
parents | f77f0bb99f35 (current diff) d45de183af46 (diff) |
children | b815328a8d42 |
files | src/alice/test/dpp/codesegment/philosophy2/TakeLeftForkCodeSegment.java src/alice/test/dpp/codesegment/philosophy2/TakeRightForkCodeSegment.java |
diffstat | 7 files changed, 119 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/.hgtags Tue Jan 15 16:41:57 2013 +0900 +++ b/.hgtags Tue Jan 29 17:17:44 2013 +0900 @@ -1,1 +1,2 @@ 6018e62cb198b9510280a2ab2ceab7501a6ed7f5 dead lock dpp +52e9b33f8a995c24e00cf53713abef8cbc2c6717 unuse ForkStateTakeCodeSegment version
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/alice/test/dpp/codesegment/fork/ForkStateTakeCodeSegment.java Tue Jan 29 17:17:44 2013 +0900 @@ -0,0 +1,30 @@ +package alice.test.dpp.codesegment.fork; + +import alice.codesegment.CodeSegment; +import alice.datasegment.CommandType; +import alice.datasegment.Receiver; +import alice.test.dpp.codesegment.philosophy.CheckForkOwnerCodeSegment; +import alice.test.dpp.datasegment.Fork; +import alice.test.dpp.datasegment.Philosophy; + +public class ForkStateTakeCodeSegment extends CodeSegment { + + Receiver arg1 = ids.create(CommandType.TAKE); + Receiver arg2 = ids.create(CommandType.TAKE); + + public ForkStateTakeCodeSegment(String pName, String forkName) { + arg1.setKey(pName); + arg2.setKey(forkName); + } + + @Override + public void run() { + Philosophy phil = arg1.asClass(Philosophy.class); + Fork fork = arg2.asClass(Fork.class); + System.out.println(phil.getName() + " try to take " + fork.getForkName()); + fork.takeFork(phil.getName()); + new CheckForkOwnerCodeSegment(phil.getName(), fork.getForkName()); + ods.update("local", phil.getName(), phil); + ods.update("local", fork.getForkName(), fork); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/alice/test/dpp/codesegment/philosophy/CheckForkOwnerCodeSegment.java Tue Jan 29 17:17:44 2013 +0900 @@ -0,0 +1,32 @@ +package alice.test.dpp.codesegment.philosophy; + +import alice.codesegment.CodeSegment; +import alice.datasegment.CommandType; +import alice.datasegment.Receiver; +import alice.test.dpp.datasegment.Fork; +import alice.test.dpp.datasegment.Philosophy; + +public class CheckForkOwnerCodeSegment extends CodeSegment { + + Receiver arg1 = ids.create(CommandType.TAKE); + Receiver arg2 = ids.create(CommandType.TAKE); + + public CheckForkOwnerCodeSegment(String pName, String forkName) { + arg1.setKey(pName); + arg2.setKey(forkName); + } + + @Override + public void run() { + Philosophy phil = arg1.asClass(Philosophy.class); + Fork fork = arg2.asClass(Fork.class); + if (fork.ownerEquals(phil.getName())) { + + phil.checkForkFlag(fork.getForkName()); + } + new TakeForkManagerCodeSegment(phil.getName()); + ods.update("local", phil.getName(), phil); + ods.update("local", fork.getForkName(), fork); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/alice/test/dpp/codesegment/philosophy/TakeForkManagerCodeSegment.java Tue Jan 29 17:17:44 2013 +0900 @@ -0,0 +1,31 @@ +package alice.test.dpp.codesegment.philosophy; + +import alice.codesegment.CodeSegment; +import alice.datasegment.CommandType; +import alice.datasegment.Receiver; +import alice.test.dpp.datasegment.Philosophy; + +public class TakeForkManagerCodeSegment extends CodeSegment { + + Receiver arg1 = ids.create(CommandType.TAKE); + + public TakeForkManagerCodeSegment(String pName) { + arg1.setKey(pName); + } + + @Override + public void run() { + Philosophy phil = arg1.asClass(Philosophy.class); + if (phil.hasLeftFork && phil.hasRightFork) { + phil.setState(Philosophy.State.EAT); + new EatCodeSegment(phil.getName()); + } else if (phil.hasRightFork) { + phil.setState(Philosophy.State.TAKELEFTFORK); + new TakeLeftForkCodeSegment(phil.getName(), phil.getLeftFork()); + } else { + phil.setState(Philosophy.State.TAKERIGHTFORK); + new TakeRightForkCodeSegment(phil.getName(), phil.getRightFork()); + } + ods.update("local", phil.getName(), phil); + } +}
--- a/src/alice/test/dpp/codesegment/philosophy2/TakeRightForkCodeSegment.java Tue Jan 15 16:41:57 2013 +0900 +++ b/src/alice/test/dpp/codesegment/philosophy2/TakeRightForkCodeSegment.java Tue Jan 29 17:17:44 2013 +0900 @@ -3,6 +3,7 @@ import alice.codesegment.CodeSegment; import alice.datasegment.CommandType; import alice.datasegment.Receiver; +import alice.test.dpp.codesegment.fork.ForkStateTakeCodeSegment; import alice.test.dpp.datasegment.Fork; import alice.test.dpp.datasegment.Philosophy; @@ -20,12 +21,8 @@ public void run() { Philosophy phil = arg1.asClass(Philosophy.class); Fork fork = arg2.asClass(Fork.class); - if (fork.takeFork(phil.getName())) { - phil.setState(Philosophy.State.TAKELEFTFORK); - new TakeLeftForkCodeSegment(phil.getName(), phil.getLeftFork()); - } else { - new TakeRightForkCodeSegment(phil.getName(), phil.getRightFork()); - } + System.out.println(phil.getName() + " take right fork."); + new ForkStateTakeCodeSegment(phil.getName(), fork.getForkName()); ods.update("local", phil.getName(), phil); ods.update("local", fork.getForkName(), fork); }
--- a/src/alice/test/dpp/datasegment/Fork.java Tue Jan 15 16:41:57 2013 +0900 +++ b/src/alice/test/dpp/datasegment/Fork.java Tue Jan 29 17:17:44 2013 +0900 @@ -55,6 +55,10 @@ } } + public boolean ownerEquals(String s) { + return owner.equals(s); + } + public String getOwner() { return owner; }
--- a/src/alice/test/dpp/datasegment/Philosophy.java Tue Jan 15 16:41:57 2013 +0900 +++ b/src/alice/test/dpp/datasegment/Philosophy.java Tue Jan 29 17:17:44 2013 +0900 @@ -24,7 +24,8 @@ private String name, leftFork, rightFork; private State state; - + public boolean hasRightFork = false; + public boolean hasLeftFork = false; public Philosophy() {} @@ -60,5 +61,20 @@ public String getRightFork() { return rightFork; } - + + public void resetHasForkFlag() { + hasRightFork = false; + hasLeftFork = false; + } + + public void checkForkFlag(String fork) { + if (rightFork.equals(fork)) { + hasRightFork = true; + } else if (leftFork.equals(fork)) { + hasLeftFork = true; + } else {} + } + + + }