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

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

File size: 3.2 KB
Line 
1package geniusweb.exampleparties.comparebids;
2
3import java.io.IOException;
4import java.util.Arrays;
5import java.util.Collections;
6import java.util.HashSet;
7import java.util.LinkedList;
8import java.util.List;
9import java.util.logging.Level;
10
11import geniusweb.actions.Action;
12import geniusweb.actions.Comparison;
13import geniusweb.actions.ElicitComparison;
14import geniusweb.actions.PartyId;
15import geniusweb.inform.ActionDone;
16import geniusweb.inform.Finished;
17import geniusweb.inform.Inform;
18import geniusweb.inform.Settings;
19import geniusweb.issuevalue.Bid;
20import geniusweb.party.Capabilities;
21import geniusweb.party.DefaultParty;
22import geniusweb.profile.PartialOrdering;
23import geniusweb.profile.Profile;
24import geniusweb.profileconnection.ProfileConnectionFactory;
25import geniusweb.profileconnection.ProfileInterface;
26import 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 */
33public 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}
Note: See TracBrowser for help on using the repository browser.