1 | package geniusweb.boa;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.Date;
|
---|
6 | import java.util.HashSet;
|
---|
7 | import java.util.Timer;
|
---|
8 | import java.util.TimerTask;
|
---|
9 | import java.util.logging.Level;
|
---|
10 |
|
---|
11 | import javax.websocket.DeploymentException;
|
---|
12 |
|
---|
13 | import geniusweb.actions.Action;
|
---|
14 | import geniusweb.boa.acceptancestrategy.AcceptanceStrategy;
|
---|
15 | import geniusweb.boa.biddingstrategy.BiddingStrategy;
|
---|
16 | import geniusweb.party.Capabilities;
|
---|
17 | import geniusweb.party.DefaultParty;
|
---|
18 | import geniusweb.party.inform.ActionDone;
|
---|
19 | import geniusweb.party.inform.Finished;
|
---|
20 | import geniusweb.party.inform.Inform;
|
---|
21 | import geniusweb.party.inform.Settings;
|
---|
22 | import geniusweb.party.inform.YourTurn;
|
---|
23 | import geniusweb.profileconnection.ProfileConnectionFactory;
|
---|
24 | import geniusweb.profileconnection.ProfileInterface;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Determines the next {@link Action} given our profile, the opponent model and
|
---|
28 | * the acceptance strategy Could also be called OfferingStrategy, but we use
|
---|
29 | * BiddingStrategy because of the name "BOA".
|
---|
30 | * <p>
|
---|
31 | * The BiddingStrategy must handle the connection, so it has to connect with the
|
---|
32 | * profile when the {@link Settings} come in , and create the subcomponents.
|
---|
33 | *
|
---|
34 | * The settings are assumed to contain also parameters "as" containing the
|
---|
35 | * full.class.path to the {@link AcceptanceStrategy} clas, and "os" containing
|
---|
36 | * the full.class.path to the {@link OpponentModel} class and "bs" containing
|
---|
37 | * the full.class.path to the {@link BiddingStrategy} class.
|
---|
38 | *
|
---|
39 | */
|
---|
40 | public class BoaParty extends DefaultParty {
|
---|
41 | /**
|
---|
42 | * This only supports SAOP protocol because it is not clear how the
|
---|
43 | * BiddingStrategy could be made more general without basically becoming a
|
---|
44 | * full Party.
|
---|
45 | *
|
---|
46 | */
|
---|
47 | private ProfileInterface profileint;
|
---|
48 | private NegoState negoState = new NegoState();
|
---|
49 | private BiddingStrategy biddingStrategy;
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public Capabilities getCapabilities() {
|
---|
53 | return new Capabilities(new HashSet<>(Arrays.asList("SAOP")));
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | public String getDescription() {
|
---|
58 | return "Takes parameters om=OpponentModel full.class.path, as=AcceptanceStrategy full.class.path "
|
---|
59 | + "and bs=BiddingStrategy full.class.path. "
|
---|
60 | + "Builds a BOA party running on SAOP.";
|
---|
61 | }
|
---|
62 |
|
---|
63 | @SuppressWarnings("unchecked")
|
---|
64 | @Override
|
---|
65 | public void notifyChange(Inform info) {
|
---|
66 | try {
|
---|
67 | if (info instanceof Settings) {
|
---|
68 | Settings settings = (Settings) info;
|
---|
69 | this.profileint = ProfileConnectionFactory
|
---|
70 | .create(settings.getProfile().getURI(), getReporter());
|
---|
71 | Class<BiddingStrategy> bsClass = (Class<BiddingStrategy>) Class
|
---|
72 | .forName((String) settings.getParemeters().get("bs"));
|
---|
73 | Class<AcceptanceStrategy> asClass = (Class<AcceptanceStrategy>) Class
|
---|
74 | .forName((String) settings.getParemeters().get("as"));
|
---|
75 | Class<OpponentModel> omClass = (Class<OpponentModel>) Class
|
---|
76 | .forName((String) settings.getParemeters().get("om"));
|
---|
77 | ;
|
---|
78 | biddingStrategy = bsClass.newInstance();
|
---|
79 | negoState = negoState.with(settings.getProgress(),
|
---|
80 | settings.getID(), asClass, omClass, reporter);
|
---|
81 | return;
|
---|
82 | }
|
---|
83 |
|
---|
84 | // Beyond this point the biddingStrategy should contain profile
|
---|
85 | if (negoState.getProfile() == null) {
|
---|
86 | negoState = negoState.with(profileint.getProfile());
|
---|
87 | }
|
---|
88 |
|
---|
89 | if (info instanceof ActionDone) {
|
---|
90 | negoState = negoState.with(((ActionDone) info).getAction());
|
---|
91 | } else if (info instanceof YourTurn) {
|
---|
92 | waitAndAct();
|
---|
93 | } else if (info instanceof Finished) {
|
---|
94 | getReporter().log(Level.INFO, "Final ourcome:" + info);
|
---|
95 | }
|
---|
96 | } catch (DeploymentException | IOException | ClassNotFoundException
|
---|
97 | | InstantiationFailedException | InstantiationException
|
---|
98 | | IllegalAccessException e) {
|
---|
99 | throw new RuntimeException("Failed to handle info", e);
|
---|
100 | }
|
---|
101 |
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * If current time > {@link BiddingStrategy#getActionTime()} then execute
|
---|
106 | * the action of the biddingStrategy. Otherwise, wait the specified time and
|
---|
107 | * retry.
|
---|
108 | */
|
---|
109 | private void waitAndAct() {
|
---|
110 | Long actionTime = biddingStrategy.getActionTime(negoState);
|
---|
111 | if (System.currentTimeMillis() > actionTime) {
|
---|
112 | try {
|
---|
113 | getConnection().send(biddingStrategy.getAction(negoState));
|
---|
114 | } catch (IOException e) {
|
---|
115 | getReporter().log(Level.SEVERE,
|
---|
116 | "Failed to execute action, connection may be broken?");
|
---|
117 | e.printStackTrace();
|
---|
118 | }
|
---|
119 | } else {
|
---|
120 | // wait specified time and retry
|
---|
121 | new Timer().schedule(new TimerTask() {
|
---|
122 | @Override
|
---|
123 | public void run() {
|
---|
124 | waitAndAct();
|
---|
125 | }
|
---|
126 | }, new Date(actionTime));
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | }
|
---|