1 | package onetomany.bargainingchipsgame.players;
|
---|
2 |
|
---|
3 | import java.util.concurrent.BlockingQueue;
|
---|
4 |
|
---|
5 | import onetomany.bargainingchipsgame.interactions.Offer;
|
---|
6 | import onetomany.bargainingchipsgame.players.utilityfunction.UtilityFunction;
|
---|
7 |
|
---|
8 | public abstract class Agent implements Runnable
|
---|
9 | {
|
---|
10 | protected String name;
|
---|
11 | protected UtilityFunction u;
|
---|
12 |
|
---|
13 | int k;
|
---|
14 |
|
---|
15 | public Agent(String name, UtilityFunction u, BlockingQueue<Offer> in, BlockingQueue<Offer> out,
|
---|
16 | BlockingQueue<CoordinationMessage> cin, BlockingQueue<NegotiationStatusMessage> cout)
|
---|
17 | {
|
---|
18 | this.name = name;
|
---|
19 | this.u = u;
|
---|
20 | this.in = in;
|
---|
21 | this.out = out;
|
---|
22 | this.cin = cin;
|
---|
23 | this.cout = cout;
|
---|
24 | k = 0;
|
---|
25 | }
|
---|
26 |
|
---|
27 | // Messaging from and to the opponent
|
---|
28 | protected BlockingQueue<Offer> in;
|
---|
29 | protected BlockingQueue<Offer> out;
|
---|
30 | // Messaging from and to the coordinator
|
---|
31 | protected BlockingQueue<CoordinationMessage> cin;
|
---|
32 | protected BlockingQueue<NegotiationStatusMessage> cout;
|
---|
33 |
|
---|
34 | protected abstract void receiveOffer(Offer bundle);
|
---|
35 |
|
---|
36 | protected abstract Offer sendOffer();
|
---|
37 |
|
---|
38 | protected abstract Offer sendOpeningOffer();
|
---|
39 |
|
---|
40 | protected abstract void receiveCoordinationMessage(CoordinationMessage cpoll);
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public void run()
|
---|
44 | {
|
---|
45 | // A buyer may send an opening offer
|
---|
46 | Offer opening = do_sendOpeningOffer();
|
---|
47 | if (opening != null)
|
---|
48 | try {
|
---|
49 | out.put(opening);
|
---|
50 | } catch (InterruptedException e1) {
|
---|
51 | e1.printStackTrace();
|
---|
52 | }
|
---|
53 |
|
---|
54 | while (true)
|
---|
55 | {
|
---|
56 | try
|
---|
57 | {
|
---|
58 | // Wait for either a message from coordinator or an incoming offer from seller
|
---|
59 | while (true)
|
---|
60 | {
|
---|
61 | CoordinationMessage cpoll = cin.poll();
|
---|
62 | if (cpoll != null)
|
---|
63 | {
|
---|
64 | do_receiveCoordinationMessage(cpoll);
|
---|
65 | break;
|
---|
66 | }
|
---|
67 |
|
---|
68 | Offer poll = in.poll();
|
---|
69 | if (poll != null)
|
---|
70 | {
|
---|
71 | do_receiveOffer(poll);
|
---|
72 | break;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | // A sync happened, so we can send out a new offer, given the new information
|
---|
77 | Offer sendOffer = do_sendOffer();
|
---|
78 | out.put(sendOffer);
|
---|
79 |
|
---|
80 | if (k >= 0)
|
---|
81 | try {
|
---|
82 | Thread.sleep(1000);
|
---|
83 | } catch (InterruptedException e) {
|
---|
84 | // TODO Auto-generated catch block
|
---|
85 | e.printStackTrace();
|
---|
86 | }
|
---|
87 | }
|
---|
88 | catch (InterruptedException e) {
|
---|
89 | e.printStackTrace();
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | private void do_receiveCoordinationMessage(CoordinationMessage cpoll)
|
---|
95 | {
|
---|
96 | System.out.println(this + " received coordination msg " + cpoll);
|
---|
97 | receiveCoordinationMessage(cpoll);
|
---|
98 | }
|
---|
99 |
|
---|
100 | protected void do_receiveOffer(Offer o)
|
---|
101 | {
|
---|
102 | System.out.println(this + " received " + o
|
---|
103 | + (o.isBid() ? (", with util = " + u.getUtility(o.getBundle())) : "")
|
---|
104 | );
|
---|
105 | receiveOffer(o);
|
---|
106 | }
|
---|
107 |
|
---|
108 | protected Offer do_sendOffer()
|
---|
109 | {
|
---|
110 | Offer o = sendOffer();
|
---|
111 | System.out.println(this + " sends " + o);
|
---|
112 | k++;
|
---|
113 | return o;
|
---|
114 | }
|
---|
115 |
|
---|
116 | protected Offer do_sendOpeningOffer()
|
---|
117 | {
|
---|
118 | Offer o = sendOpeningOffer();
|
---|
119 | if (o != null)
|
---|
120 | {
|
---|
121 | System.out.println(this + " sends opening offer " + o);
|
---|
122 | k++;
|
---|
123 | }
|
---|
124 | return o;
|
---|
125 | }
|
---|
126 |
|
---|
127 | @Override
|
---|
128 | public String toString()
|
---|
129 | {
|
---|
130 | return "#" + k + ": " + name;
|
---|
131 | }
|
---|
132 | }
|
---|