Mercurial > hg > Members > nobuyasu > Alice > DPPAlice
changeset 4:2794091a7058
modified CodeSegment
author | one |
---|---|
date | Sun, 25 Nov 2012 17:07:45 +0900 |
parents | 6b3f0f87a36b |
children | 2db314b1a970 |
files | src/alice/test/dpp/codesegment/StartCodeSegment.java src/alice/test/dpp/codesegment/ThinkCodeSegment.java src/alice/test/dpp/model/Fork.java |
diffstat | 3 files changed, 57 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/src/alice/test/dpp/codesegment/StartCodeSegment.java Sun Nov 25 02:38:09 2012 +0900 +++ b/src/alice/test/dpp/codesegment/StartCodeSegment.java Sun Nov 25 17:07:45 2012 +0900 @@ -1,6 +1,7 @@ package alice.test.dpp.codesegment; import alice.codesegment.CodeSegment; +import alice.test.dpp.model.Fork; import alice.test.dpp.model.Philosophy; public class StartCodeSegment extends CodeSegment { @@ -9,11 +10,9 @@ public void run() { System.out.println("run StartCodeSegment"); new ThinkCodeSegment(new Philosophy("phil1","fork1","fork2")); - ods.update("local", "fork1", 1); - ods.update("local", "fork2", 2); -// ods.update("local", "fork3", 3); -// ods.update("local", "fork4", 4); -// ods.update("local", "fork4", 4); + ods.update("local", "fork1", new Fork("fork1")); + ods.update("local", "fork2", new Fork("fork2")); + } }
--- a/src/alice/test/dpp/codesegment/ThinkCodeSegment.java Sun Nov 25 02:38:09 2012 +0900 +++ b/src/alice/test/dpp/codesegment/ThinkCodeSegment.java Sun Nov 25 17:07:45 2012 +0900 @@ -1,28 +1,30 @@ 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 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(p.getLeftFork()); - arg2.setKey(p.getRightFork()); } - - + @Override public void run() { - - - + double rand = Math.random(); + if (rand < 0.5) { + try { + System.out.println(phil + " is thinking."); + Thread.sleep(10); + new ThinkCodeSegment(phil); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } else { + new TakeForkCodeSegment(phil); + } } }
--- a/src/alice/test/dpp/model/Fork.java Sun Nov 25 02:38:09 2012 +0900 +++ b/src/alice/test/dpp/model/Fork.java Sun Nov 25 17:07:45 2012 +0900 @@ -2,7 +2,47 @@ public class Fork { + private String owner; + private String forkName; + private ForkState state; + public Fork(String n) { + forkName = n; + owner = Table.TABLE; + state = ForkState.ON_THE_TABLE; + } + + public ForkState getState() { + return state; + } + public String getForkName() { + return forkName; + } + + public boolean getFork(Philosophy phil) { + if (state == ForkState.ON_THE_TABLE){ + state = ForkState.IN_THE_HAND; + owner = phil.getName(); + return true; + } else { + return false; + } + } + + public boolean onTheTable(Philosophy phil) { + if (owner.equals(phil.getName())) { + state = ForkState.ON_THE_TABLE; + owner = Table.TABLE; + return true; + } else { + return false; + } + } + + public String getOwner() { + return owner; + } + }