1 | package geniusweb.exampleparties.comparebids;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.Collections;
|
---|
6 | import java.util.HashSet;
|
---|
7 | import java.util.LinkedList;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.logging.Level;
|
---|
10 |
|
---|
11 | import geniusweb.actions.Action;
|
---|
12 | import geniusweb.actions.Comparison;
|
---|
13 | import geniusweb.actions.ElicitComparison;
|
---|
14 | import geniusweb.actions.PartyId;
|
---|
15 | import geniusweb.inform.ActionDone;
|
---|
16 | import geniusweb.inform.Finished;
|
---|
17 | import geniusweb.inform.Inform;
|
---|
18 | import geniusweb.inform.Settings;
|
---|
19 | import geniusweb.issuevalue.Bid;
|
---|
20 | import geniusweb.party.Capabilities;
|
---|
21 | import geniusweb.party.DefaultParty;
|
---|
22 | import geniusweb.profile.PartialOrdering;
|
---|
23 | import geniusweb.profile.Profile;
|
---|
24 | import geniusweb.profileconnection.ProfileConnectionFactory;
|
---|
25 | import geniusweb.profileconnection.ProfileInterface;
|
---|
26 | import tudelft.utilities.logging.Reporter;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * A party following the COB protocol and thus answers {@link ElicitComparison}
|
---|
30 | * questions. This party does not use a GUI but uses the given profile to
|
---|
31 | * determine the answer.
|
---|
32 | */
|
---|
33 | public class CompareBids extends DefaultParty {
|
---|
34 |
|
---|
35 | private PartyId me;
|
---|
36 | protected ProfileInterface profileint;
|
---|
37 |
|
---|
38 | public CompareBids() {
|
---|
39 | }
|
---|
40 |
|
---|
41 | public CompareBids(Reporter reporter) {
|
---|
42 | super(reporter); // for debugging
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public void notifyChange(Inform info) {
|
---|
47 | try {
|
---|
48 | if (info instanceof Settings) {
|
---|
49 | Settings settings = (Settings) info;
|
---|
50 | this.profileint = ProfileConnectionFactory
|
---|
51 | .create(settings.getProfile().getURI(), getReporter());
|
---|
52 | this.me = settings.getID();
|
---|
53 | } else if (info instanceof ActionDone) {
|
---|
54 | Action action = ((ActionDone) info).getAction();
|
---|
55 | if (action instanceof ElicitComparison) {
|
---|
56 | reply((ElicitComparison) action);
|
---|
57 | }
|
---|
58 | } else if (info instanceof Finished) {
|
---|
59 | terminate(); // stop this party and free resources.
|
---|
60 | } else {
|
---|
61 | reporter.log(Level.WARNING, "COB party ignores " + info);
|
---|
62 | }
|
---|
63 | } catch (Exception e) {
|
---|
64 | throw new RuntimeException("Failed to handle info", e);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public Capabilities getCapabilities() {
|
---|
70 | return new Capabilities(new HashSet<>(Arrays.asList("COB")),
|
---|
71 | Collections.singleton(PartialOrdering.class));
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public String getDescription() {
|
---|
76 | return "compares a bid with a given list of other bids.";
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Handle the {@link ElicitComparison} and reply with {@link Comparison}.
|
---|
81 | * This shoul dbe fast (assuming the request is reasonably sized) so we
|
---|
82 | * handle this in-line instead of in separate thread.
|
---|
83 | */
|
---|
84 | private void reply(ElicitComparison compareRequest) throws IOException {
|
---|
85 | List<Bid> better = new LinkedList<>();
|
---|
86 | List<Bid> worse = new LinkedList<>();
|
---|
87 | Profile profile1 = profileint.getProfile();
|
---|
88 | if (!(profile1 instanceof PartialOrdering)) {
|
---|
89 | throw new IllegalStateException(
|
---|
90 | "Profile must be a partial ordering");
|
---|
91 | }
|
---|
92 | PartialOrdering profile = (PartialOrdering) profile1;
|
---|
93 |
|
---|
94 | Bid bid = compareRequest.getBid();
|
---|
95 | for (Bid otherbid : compareRequest.getOptions()) {
|
---|
96 | boolean betteroreq = profile.isPreferredOrEqual(otherbid, bid);
|
---|
97 | boolean worseoreq = profile.isPreferredOrEqual(bid, otherbid);
|
---|
98 | if (betteroreq && !worseoreq)
|
---|
99 | better.add(otherbid);
|
---|
100 | else if (worseoreq && !betteroreq)
|
---|
101 | worse.add(otherbid);
|
---|
102 | }
|
---|
103 | getConnection().send(new Comparison(me, bid, better, worse));
|
---|
104 | }
|
---|
105 |
|
---|
106 | }
|
---|