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

Last change on this file since 31 was 31, checked in by bart, 3 years ago

New protocols Learn and APPLearn. Fixed memory leak.

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 } else {
60 reporter.log(Level.WARNING, "COB party ignores " + info);
61 }
62 } catch (Exception e) {
63 throw new RuntimeException("Failed to handle info", e);
64 }
65 }
66
67 @Override
68 public Capabilities getCapabilities() {
69 return new Capabilities(new HashSet<>(Arrays.asList("COB")),
70 Collections.singleton(PartialOrdering.class));
71 }
72
73 @Override
74 public String getDescription() {
75 return "compares a bid with a given list of other bids.";
76 }
77
78 /**
79 * Handle the {@link ElicitComparison} and reply with {@link Comparison}.
80 * This shoul dbe fast (assuming the request is reasonably sized) so we
81 * handle this in-line instead of in separate thread.
82 */
83 private void reply(ElicitComparison compareRequest) throws IOException {
84 List<Bid> better = new LinkedList<>();
85 List<Bid> worse = new LinkedList<>();
86 Profile profile1 = profileint.getProfile();
87 if (!(profile1 instanceof PartialOrdering)) {
88 throw new IllegalStateException(
89 "Profile must be a partial ordering");
90 }
91 PartialOrdering profile = (PartialOrdering) profile1;
92
93 Bid bid = compareRequest.getBid();
94 for (Bid otherbid : compareRequest.getOptions()) {
95 boolean betteroreq = profile.isPreferredOrEqual(otherbid, bid);
96 boolean worseoreq = profile.isPreferredOrEqual(bid, otherbid);
97 if (betteroreq && !worseoreq)
98 better.add(otherbid);
99 else if (worseoreq && !betteroreq)
100 worse.add(otherbid);
101 }
102 getConnection().send(new Comparison(me, bid, better, worse));
103 }
104
105}
Note: See TracBrowser for help on using the repository browser.