[127] | 1 | package agents;
|
---|
| 2 |
|
---|
| 3 | import javax.swing.JOptionPane;
|
---|
| 4 |
|
---|
| 5 | import genius.core.Agent;
|
---|
| 6 | import genius.core.Bid;
|
---|
| 7 | import genius.core.SupportedNegotiationSetting;
|
---|
| 8 | import genius.core.actions.Accept;
|
---|
| 9 | import genius.core.actions.Action;
|
---|
| 10 | import genius.core.actions.EndNegotiation;
|
---|
| 11 | import genius.core.actions.Offer;
|
---|
| 12 | import genius.core.utility.AdditiveUtilitySpace;
|
---|
| 13 | import genius.core.utility.Evaluator;
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
| 16 | * Uses {@link Evaluator}s and therefore requires {@link AdditiveUtilitySpace}
|
---|
| 17 | *
|
---|
| 18 | * @author W.Pasman, modified version of Dmytro's UIAgent
|
---|
| 19 | *
|
---|
| 20 | */
|
---|
| 21 | public class UIAgent extends Agent {
|
---|
| 22 | private Action opponentAction = null;
|
---|
| 23 | private EnterBidDialog ui = null;
|
---|
| 24 | private Bid myPreviousBid = null;
|
---|
| 25 |
|
---|
| 26 | /** Creates a new instance of UIAgent */
|
---|
| 27 | @Override
|
---|
| 28 | public String getVersion() {
|
---|
| 29 | return "1.0";
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * One agent will be kept alive over multiple sessions. Init will be called
|
---|
| 34 | * at the start of each nego session.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | @Override
|
---|
| 38 | public void init() {
|
---|
| 39 | System.out.println("init UIAgent");
|
---|
| 40 |
|
---|
| 41 | System.out.println("closing old dialog of ");
|
---|
| 42 | if (ui != null) {
|
---|
| 43 | ui.dispose();
|
---|
| 44 | ui = null;
|
---|
| 45 | }
|
---|
| 46 | System.out.println("old dialog closed. Trying to open new dialog. ");
|
---|
| 47 | try {
|
---|
| 48 | ui = new EnterBidDialog(this, null, true,
|
---|
| 49 | (AdditiveUtilitySpace) utilitySpace, null);
|
---|
| 50 | } catch (Exception e) {
|
---|
| 51 | System.out.println("Problem in UIAgent2.init:" + e.getMessage());
|
---|
| 52 | e.printStackTrace();
|
---|
| 53 | }
|
---|
| 54 | System.out.println("finished init of UIAgent2");
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | @Override
|
---|
| 58 | public void ReceiveMessage(Action opponentAction) {
|
---|
| 59 | this.opponentAction = opponentAction;
|
---|
| 60 | if (opponentAction instanceof Accept)
|
---|
| 61 | JOptionPane.showMessageDialog(null,
|
---|
| 62 | "Opponent accepted your last offer.");
|
---|
| 63 |
|
---|
| 64 | else if (opponentAction instanceof EndNegotiation)
|
---|
| 65 | JOptionPane.showMessageDialog(null,
|
---|
| 66 | "Opponent canceled the negotiation session");
|
---|
| 67 | else if (opponentAction instanceof Offer) {
|
---|
| 68 | try {
|
---|
| 69 | ui = new EnterBidDialog(this, null, true,
|
---|
| 70 | (AdditiveUtilitySpace) utilitySpace,
|
---|
| 71 | ((Offer) opponentAction).getBid());
|
---|
| 72 | } catch (Exception e) {
|
---|
| 73 | System.out
|
---|
| 74 | .println("failed to initialize the UI with new offer!");
|
---|
| 75 | e.printStackTrace();
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | return;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | @Override
|
---|
| 82 | public Action chooseAction() {
|
---|
| 83 | Action action = ui.askUserForAction(opponentAction, myPreviousBid);
|
---|
| 84 | if ((action != null) && (action instanceof Offer))
|
---|
| 85 | myPreviousBid = ((Offer) action).getBid();
|
---|
| 86 | return action;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | public boolean isUIAgent() {
|
---|
| 90 | return true;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | @Override
|
---|
| 94 | public SupportedNegotiationSetting getSupportedNegotiationSetting() {
|
---|
| 95 | return SupportedNegotiationSetting.getLinearUtilitySpaceInstance();
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | @Override
|
---|
| 99 | public String getDescription() {
|
---|
| 100 | return "Simple human user interface to place bids";
|
---|
| 101 | }
|
---|
| 102 | }
|
---|