source: boa/src/main/java/geniusweb/boa/NegoState.java@ 14

Last change on this file since 14 was 14, checked in by bart, 5 years ago

Release 1.4.0

File size: 6.2 KB
Line 
1package geniusweb.boa;
2
3import java.lang.reflect.Parameter;
4import java.util.HashMap;
5import java.util.Map;
6import java.util.logging.Level;
7
8import geniusweb.actions.Action;
9import geniusweb.actions.ActionWithBid;
10import geniusweb.actions.PartyId;
11import geniusweb.boa.acceptancestrategy.AcceptanceStrategy;
12import geniusweb.boa.biddingstrategy.BiddingStrategy;
13import geniusweb.issuevalue.Bid;
14import geniusweb.party.inform.Settings;
15import geniusweb.profile.Profile;
16import geniusweb.progress.Progress;
17import tudelft.utilities.logging.Reporter;
18
19/**
20 * Stores the current state of the negotiation for a {@link BoaParty}, including
21 * the state of the opponent models
22 *
23 */
24public class NegoState {
25 private final Settings settings;
26 private final Profile profile;
27 private final Progress progress;
28 private final Class<OpponentModel> opponentModelClass;
29 private final AcceptanceStrategy acceptanceStrat;
30 private final Bid lastReceivedBid;
31 private final Map<PartyId, OpponentModel> opponentModels;
32 private final Reporter reporter;
33
34 public NegoState() {
35 this(null, null, null, null, null, null, new HashMap<>(), null);
36 }
37
38 public NegoState(Settings settings, Profile profile, Progress progr,
39 Class<OpponentModel> omClass, AcceptanceStrategy as, Bid lastRcBid,
40 Map<PartyId, OpponentModel> oppMods, Reporter reporter) {
41 this.settings = settings;
42 this.profile = profile;
43 this.progress = progr;
44 this.opponentModelClass = omClass;
45 this.acceptanceStrat = as;
46 this.lastReceivedBid = lastRcBid;
47 this.opponentModels = oppMods;
48 this.reporter = reporter;
49 }
50
51 /**
52 * Initialize the component. Must be called first.
53 *
54 * @param progress the progress to use
55 * @param myId the Id of this party
56 * @param as the acceptance strategy class
57 * @param om the Class of the OpponentModels
58 * @param reporter a {@link Reporter} to log messages and errors to
59 * @return initialized {@link BiddingStrategy}
60 * @throws InstantiationFailedException if one of provided classes can not
61 * be instantiated.
62 */
63
64 public NegoState with(Progress progr, PartyId myId,
65 Class<AcceptanceStrategy> asClass, Class<OpponentModel> omClass,
66 Reporter reporter) throws InstantiationFailedException {
67 AcceptanceStrategy as;
68 try {
69 as = asClass.newInstance();
70 } catch (InstantiationException | IllegalAccessException e) {
71 throw new InstantiationFailedException(
72 "Failed to create " + asClass, e);
73 }
74 return new NegoState(settings, profile, progr, omClass, as,
75 lastReceivedBid, opponentModels, reporter);
76 }
77
78 /**
79 *
80 * @param profile the profile that we use. Must be called second (after the
81 * init call above). Can be called multiple times, if a
82 * profile changes
83 * @throws InstantiationFailedException if one of provided classes can not
84 * be instantiated.
85 */
86 public NegoState with(Profile prof) {
87 if (profile != null)
88 reporter.log(Level.WARNING,
89 "DefaultBiddingStrategy does not support profile changes. Ignored the change.");
90 return new NegoState(settings, prof, progress, opponentModelClass,
91 acceptanceStrat, lastReceivedBid, opponentModels, reporter);
92 }
93
94 /**
95 *
96 * @return the settings for this negotiation, includes my {@link PartyId},
97 * {@link Parameter}, {@link Profile} etc.
98 */
99 public Settings getSettings() {
100 return settings;
101 }
102
103 /**
104 *
105 * @return the {@link Profile} as fetched from the profiles server, or null
106 * if no profile was received yet.
107 */
108 public Profile getProfile() {
109 return profile;
110 }
111
112 /**
113 *
114 * @param action the action that was done by some participant.
115 * @return new BoaState that includes action.getBid as last bid (if action
116 * contains a bid), and with updated opponentmodel of that
117 * participant.
118 * @throws InstantiationFailedException if one of provided classes can not
119 * be instantiated.
120 */
121 public NegoState with(Action action) throws InstantiationFailedException {
122 try {
123 Map<PartyId, OpponentModel> newmodels = updateModels(action);
124 } catch (InstantiationException | IllegalAccessException e) {
125 throw new InstantiationFailedException("Updating the models failed:"
126 + e.getMessage() + ". Ignoring the action", e);
127 }
128 Bid lastbid = action instanceof ActionWithBid
129 ? ((ActionWithBid) action).getBid()
130 : lastReceivedBid;
131 return new NegoState(settings, profile, progress, opponentModelClass,
132 acceptanceStrat, lastbid, opponentModels, reporter);
133 }
134
135 /**
136 * Must be called every time a 'round' as been made and the progress needs
137 * to be incremented.
138 *
139 * @param progress the new value of the progress
140 * @return new state
141 * @throws InstantiationFailedException if one of provided classes can not
142 * be instantiated.
143 */
144 public NegoState with(Progress prog) {
145 return new NegoState(settings, profile, prog, opponentModelClass,
146 acceptanceStrat, lastReceivedBid, opponentModels, reporter);
147 }
148
149 /**
150 * @param action
151 * @return the map with the opponentmodels, with the party that did the
152 * action updated.
153 * @throws InstantiationException
154 * @throws IllegalAccessException
155 */
156 private Map<PartyId, OpponentModel> updateModels(Action action)
157 throws InstantiationException, IllegalAccessException {
158 PartyId actor = action.getActor();
159 Map<PartyId, OpponentModel> updated = extendedOpponentModels(actor);
160 updated.put(actor, updated.get(actor).with(action, progress));
161 return updated;
162 }
163
164 /**
165 * Get the current opponent model, extended with newparty if there is no
166 * model yet for newparty.
167 *
168 * @param newparty a possibly new party
169 * @return a (possibly new) opponentModels map
170 * @throws InstantiationException
171 * @throws IllegalAccessException
172 */
173 private Map<PartyId, OpponentModel> extendedOpponentModels(PartyId newparty)
174 throws InstantiationException, IllegalAccessException {
175 // check if this is new party
176 if (opponentModels.containsKey(newparty))
177 return opponentModels;
178
179 Map<PartyId, OpponentModel> newmodels = new HashMap<>(opponentModels);
180 OpponentModel newmodel = opponentModelClass.newInstance();
181 newmodel = newmodel.with(profile.getDomain());
182 newmodels.put(newparty, newmodel);
183 return newmodels;
184 }
185}
Note: See TracBrowser for help on using the repository browser.