source: boa/src/main/java/geniusweb/boa/BoaParty.java@ 16

Last change on this file since 16 was 16, checked in by bart, 4 years ago

Enhanced tournament runner

File size: 4.3 KB
Line 
1package geniusweb.boa;
2
3import java.io.IOException;
4import java.util.Arrays;
5import java.util.Date;
6import java.util.HashSet;
7import java.util.Timer;
8import java.util.TimerTask;
9import java.util.logging.Level;
10
11import javax.websocket.DeploymentException;
12
13import geniusweb.actions.Action;
14import geniusweb.boa.acceptancestrategy.AcceptanceStrategy;
15import geniusweb.boa.biddingstrategy.BiddingStrategy;
16import geniusweb.party.Capabilities;
17import geniusweb.party.DefaultParty;
18import geniusweb.party.inform.ActionDone;
19import geniusweb.party.inform.Finished;
20import geniusweb.party.inform.Inform;
21import geniusweb.party.inform.Settings;
22import geniusweb.party.inform.YourTurn;
23import geniusweb.profileconnection.ProfileConnectionFactory;
24import 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 */
40public 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}
Note: See TracBrowser for help on using the repository browser.