source: exampleparties/comparebids/src/main/java/geniusweb/exampleparties/comparebids/CompareBids.java@ 26

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

Voting requests now contain Offers. Fixed windows whitespace issue. Partiesserver now supports up to 8 parties simultaneously.

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