source: src/main/java/agents/qoagent2/QMessages.java@ 127

Last change on this file since 127 was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 16.7 KB
Line 
1package agents.qoagent2;
2
3import java.util.StringTokenizer;
4
5/*
6 * Created on 30/05/2004
7 *
8 */
9
10/**
11 * @author raz
12 * @version 1.0
13 * @see QOAgent
14 * @see QCommunication
15 */
16public class QMessages
17{
18 // constants for probability receiveMessage
19 public final static int MESSAGE_RECEIVED = 0;
20 public final static int MESSAGE_REJECTED = 1;
21
22 public final static int REGISTER = 0;
23 public final static int THREAT = 1;
24 public final static int COMMENT = 2;
25 public final static int OFFER = 3;
26 public final static int PROMISE = 4;
27 public final static int QUERY = 5;
28 public final static int ACCEPT = 6;
29 public final static int REJECT = 7;
30 public final static int OPT_OUT = 8;
31 public final static int COUNTER_OFFER = 9;
32
33 private QOAgent m_agent;
34
35 /**
36 * @param agent - saves the QOAgent in the member variable
37 */
38 public QMessages(QOAgent agent)
39 {
40 m_agent = agent;
41 }
42
43 /**
44 * Formats the message in the predefined structure for sending it
45 * later to the server
46 * @param nMessageKind - the message kind. Can be either:
47 * REGISTER, THREAT, COMMENT, OFFER, PROMISE, QUERY, ACCEPT, REJECT, OPT_OUT, COUNTER_OFFER.
48 * @param sMsgBody - the message body: additional data for creating the message.
49 * sMsgBody differs for the different message types
50 * @return the formatted message
51 * @see QOAgent
52 * @see QCommunication
53 */
54 public String formatMessage(int nMessageKind, String sMsgBody)
55 {
56 String sFormattedMsg = "";
57
58 switch (nMessageKind)
59 {
60 case REGISTER:
61 {
62 sFormattedMsg = "type register tag " + m_agent.getMsgId() + " id " +
63 sMsgBody + " side " + m_agent.getAgentSide() + " name " + m_agent.getAgentName() +
64 " supportMediator " + m_agent.getSupportMediator() +
65 " preferenceDetails automatedAgent";
66 ;
67 }
68 break;
69 case THREAT:
70 {
71 sFormattedMsg = "type threat" +
72 " source " + m_agent.getAgentId() +
73 " target " + m_agent.getOpponentAgentId() +
74 " tag " + m_agent.getMsgId() +
75 " body "+ sMsgBody;
76 }
77 break;
78 case COMMENT:
79 {
80 sFormattedMsg = "type comment" +
81 " source " + m_agent.getAgentId() +
82 " target " + m_agent.getOpponentAgentId() +
83 " tag " + m_agent.getMsgId() +
84 " body "+ sMsgBody;
85 }
86 break;
87 case OFFER:
88 {
89 sFormattedMsg = "type offer" +
90 " source " + m_agent.getAgentId() +
91 " target " + m_agent.getOpponentAgentId() +
92 " tag " + m_agent.getMsgId() +
93 " issueSet ";
94
95 sFormattedMsg += sMsgBody;
96 }
97 break;
98 case COUNTER_OFFER:
99 {
100 sFormattedMsg = "type counter_offer" +
101 " source " + m_agent.getAgentId() +
102 " target " + m_agent.getOpponentAgentId() +
103 " tag " + m_agent.getMsgId() +
104 " issueSet ";
105
106 sFormattedMsg += sMsgBody;
107 }
108 break;
109 case PROMISE:
110 {
111 sFormattedMsg = "type promise" +
112 " source " + m_agent.getAgentId() +
113 " target " + m_agent.getOpponentAgentId() +
114 " tag " + m_agent.getMsgId();
115
116 // build the agent's issue set
117 String sAgentPromise = " myIssueSet ";
118
119 // NOTE: In our scenario there are no actions
120 // that only for one side.
121 // We do not use the option of myIssueSet and yourIssueSet
122 sAgentPromise += sMsgBody;
123
124 /*
125 QPromiseType agentPromise = m_agent.getPromiseList();
126
127 String sAttribute, sValue;
128 ArrayList agentPromiseList = (ArrayList)agentPromise.agentIssueSet;
129 for (int i = 0; i < agentPromiseList.size(); ++i)
130 {
131 QAttributeValue av = (QAttributeValue)agentPromiseList.get(i);
132 sAttribute = av.sAttribute;
133 sValue= av.sValue;
134
135 sAgentPromise += sValue + "*" + sAttribute + "*";
136 }
137 */
138 sFormattedMsg += sAgentPromise + " ";
139
140 // build the opponent's issue set
141 String sOpponentPromise = "yourIssueSet ";
142
143 /*
144 ArrayList opponentPromiseList = (ArrayList)agentPromise.opponentIssueSet;
145
146 for (int i = 0; i < opponentPromiseList.size(); ++i)
147 {
148 QAttributeValue av = (QAttributeValue)opponentPromiseList.get(i);
149 sAttribute = av.sAttribute;
150 sValue= av.sValue;
151
152 sOpponentPromise += sValue + "*" + sAttribute + "*";
153 }
154 */
155 sFormattedMsg += sOpponentPromise;
156 }
157 break;
158 case QUERY:
159 {
160 sFormattedMsg = "type query" +
161 " source " + m_agent.getAgentId() +
162 " target " + m_agent.getOpponentAgentId() +
163 " tag " + m_agent.getMsgId() +
164 " issueSet ";
165
166 sFormattedMsg += sMsgBody;
167 }
168 break;
169 case ACCEPT:
170 {
171 sFormattedMsg = "type response" +
172 " source " + m_agent.getAgentId() +
173 " target " + m_agent.getOpponentAgentId() +
174 " tag " + m_agent.getMsgId() +
175 " answer AGREE" +
176 " message " + sMsgBody +
177 " reason "; // NOTE: No reason is supplied;
178
179 // save accepted msg
180 String sResponse = sFormattedMsg.substring(sFormattedMsg.indexOf("answer ")+7, sFormattedMsg.indexOf("reason")-1);
181 String sMessage = sResponse.substring(sResponse.indexOf("message ") + 8);
182
183 //TODO: Do more than just save accepted offer?
184
185 // message accepted - save message
186 // parse message by its type (offer, promise, query)
187 String sSavedMsg = "";
188 if (sMessage.startsWith("type query"))
189 sSavedMsg = sMessage.substring(sMessage.indexOf("issueSet ") + 9);
190 else if (sMessage.startsWith("type counter_offer"))
191 sSavedMsg = sMessage.substring(sMessage.indexOf("issueSet ") + 9);
192 else if (sMessage.startsWith("type offer"))
193 sSavedMsg = sMessage.substring(sMessage.indexOf("issueSet ") + 9);
194 else if (sMessage.startsWith("type promise"))
195 {
196 String sPromise = sMessage.substring(sMessage.indexOf("myIssueSet ") + 11);
197 String sMyIssueSet = sPromise.substring(0, sPromise.indexOf("yourIssueSet "));
198 String sYourIssueSet = sPromise.substring(sPromise.indexOf("yourIssueSet ") + 13);
199
200 // parse to one agreement
201 sSavedMsg = sMyIssueSet + sYourIssueSet;
202 }
203
204 // only if accepted an offer - save it
205 if ( sMessage.startsWith("type counter_offer") || sMessage.startsWith("type offer"))
206 m_agent.saveAcceptedMsg(sSavedMsg);
207 }
208 break;
209 case REJECT:
210 {
211 sFormattedMsg = "type response" +
212 " source " + m_agent.getAgentId() +
213 " target " + m_agent.getOpponentAgentId() +
214 " tag " + m_agent.getMsgId() +
215 " answer DISAGREE" +
216 " message " + sMsgBody +
217 " reason "; // NOTE: No reason is supplied
218 }
219 break;
220 case OPT_OUT:
221 {
222 sFormattedMsg = "type opt-out tag " + m_agent.getMsgId();
223 }
224 break;
225 default:
226 {
227 System.out.println("[QO]ERROR: Invalid message kind: " + nMessageKind + " [QMessages::formatMessage(199)]");
228 System.err.println("[QO]ERROR: Invalid message kind: " + nMessageKind + " [QMessages::formatMessage(199)]");
229 }
230 break;
231 }
232
233 return sFormattedMsg;
234 }
235
236 /**
237 * Parses messages from the server.
238 * NOTE: There is no validation that this agent is the
239 * target for the message. Assuming correctness of server routing messages.
240 * @param sServerLine - the server's message
241 * @return the parsed string - relevant only if "nak"
242 * @see QOAgent
243 * @see QCommunication
244 */
245 public String parseMessage(String sServerLine)
246 {
247 String sParsedString = "";
248
249 if (sServerLine.startsWith("type comment"))
250 {
251 String sComment=sServerLine.substring(sServerLine.indexOf(" body ")+6);
252
253 // TODO: use comments?
254 }
255 else if (sServerLine.startsWith("type threat"))
256 {
257 String sThreat=sServerLine.substring(sServerLine.indexOf(" body ")+6);
258
259 // TODO: use threats?
260 }
261 else if (sServerLine.startsWith("type endTurn"))
262 {
263 // turn ended
264 m_agent.incrementCurrentTurn();
265 }
266 else if (sServerLine.startsWith("type endNegotiation"))
267 {
268 // negotiation ended
269 m_agent.m_gtStopTurn.setRun(false);
270 m_agent.m_gtStopNeg.setRun(false);
271
272 String sEndNegDetails = sServerLine.substring(sServerLine.indexOf("whyEnded"));
273
274 System.out.println("[QO]Negotiation Ended");
275 System.err.println("[QO]Negotiation Ended");
276
277 m_agent.endNegotiation();
278
279 // NOTE: no need to parse the end reason, agreement
280 // and score. They are saved in the file.
281 }
282 else if (sServerLine.startsWith("type response"))
283 {
284 String sResponse = sServerLine.substring(sServerLine.indexOf("answer ")+7, sServerLine.indexOf("reason")-1);
285 String sAnswerType = sResponse.substring(0,sResponse.indexOf(" "));
286 String sMessage = sResponse.substring(sResponse.indexOf("message ") + 8);
287
288 String sReason = sServerLine.substring(sServerLine.indexOf("reason ") + 7);
289
290 if(sAnswerType.equals("AGREE"))
291 {
292 //TODO: Do more than just save accepted offer?
293
294 // message accepted - save message
295 // parse message by its type (offer, promise, query)
296 String sSavedMsg = "";
297 if (sMessage.startsWith("type query"))
298 sSavedMsg = sMessage.substring(sMessage.indexOf("issueSet ") + 9);
299 else if (sMessage.startsWith("type counter_offer"))
300 sSavedMsg = sMessage.substring(sMessage.indexOf("issueSet ") + 9);
301 else if (sMessage.startsWith("type offer"))
302 sSavedMsg = sMessage.substring(sMessage.indexOf("issueSet ") + 9);
303 else if (sMessage.startsWith("type promise"))
304 {
305 String sPromise = sMessage.substring(sMessage.indexOf("myIssueSet ") + 11);
306 String sMyIssueSet = sPromise.substring(0, sPromise.indexOf("yourIssueSet "));
307 String sYourIssueSet = sPromise.substring(sPromise.indexOf("yourIssueSet ") + 13);
308
309 // parse to one agreement
310 sSavedMsg = sMyIssueSet + sYourIssueSet;
311 }
312
313 // only if accepted an offer - save it
314 // TODO: if our agent send queries/promises - need to offer it now [if it's still worthwhile for us]
315 if ( sMessage.startsWith("type counter_offer") || sMessage.startsWith("type offer"))
316 m_agent.saveAcceptedMsg(sSavedMsg);
317 }
318 else if(sAnswerType.equals("DISAGREE"))
319 {
320 // message rejected
321 // parse message by its type (offer, promise, query)
322 String sSavedMsg = "";
323 int nMessageType = -1;
324 if (sMessage.startsWith("type query"))
325 {
326 sSavedMsg = sMessage.substring(sMessage.indexOf("issueSet ") + 9);
327 nMessageType = QUERY;
328 }
329 else if (sMessage.startsWith("type counter_offer"))
330 {
331 nMessageType = OFFER;
332 sSavedMsg = sMessage.substring(sMessage.indexOf("issueSet ") + 9);
333 }
334 else if (sMessage.startsWith("type offer"))
335 {
336 nMessageType = OFFER;
337 sSavedMsg = sMessage.substring(sMessage.indexOf("issueSet ") + 9);
338 }
339 else if (sMessage.startsWith("type promise"))
340 {
341 nMessageType = PROMISE;
342 String sPromise = sMessage.substring(sMessage.indexOf("myIssueSet ") + 11);
343 String sMyIssueSet = sPromise.substring(0, sPromise.indexOf("yourIssueSet "));
344 String sYourIssueSet = sPromise.substring(sPromise.indexOf("yourIssueSet ") + 13);
345
346 // parse to one agreement
347 sSavedMsg = sMyIssueSet + sYourIssueSet;
348 }
349
350 // receiveMessage opponent's probability
351 int CurrentAgreementIdx[] = new int[QAgentType.MAX_ISSUES];
352 CurrentAgreementIdx = m_agent.getAgreementIndices(sSavedMsg);
353
354 // Update probability of opponent based on message
355 m_agent.updateOpponentProbability(CurrentAgreementIdx, nMessageType, MESSAGE_REJECTED);
356 }
357 }
358 else if (sServerLine.startsWith("type registered"))
359 {
360 String sSecsForTurn = sServerLine.substring(sServerLine.indexOf("secForTurn ")+11);
361 StringTokenizer st = new StringTokenizer(sSecsForTurn);
362
363 long lSecondsForTurn = Long.parseLong(st.nextToken());
364 m_agent.setSecondsForTurn(lSecondsForTurn);
365
366 String sMaxTurns=sServerLine.substring(sServerLine.indexOf("maxTurn ")+8);
367 st = new StringTokenizer(sMaxTurns);
368 m_agent.setMaxTurns(Integer.parseInt(st.nextToken()));
369
370 long lTotalSec=lSecondsForTurn;
371 int nHours=(int)lTotalSec/3600;
372
373 lTotalSec -= nHours*3600;
374 int nMinutes=(int)lTotalSec/60;
375
376 lTotalSec -= nMinutes*60;
377
378 m_agent.m_gtStopTurn = new QGameTime(false,nHours,nMinutes,(int)lTotalSec,m_agent,true);
379 m_agent.m_gtStopTurn.newGame(); // initializing the stop-watch
380
381 new Thread(m_agent.m_gtStopTurn).start();
382
383 lTotalSec=lSecondsForTurn * m_agent.getMaxTurns();
384 nHours=(int)lTotalSec/3600;
385
386 lTotalSec -= nHours*3600;
387 nMinutes=(int)lTotalSec/60;
388
389 lTotalSec -= nMinutes*60;
390
391 m_agent.m_gtStopNeg = new QGameTime(false,nHours,nMinutes,(int)lTotalSec,m_agent,false);
392 m_agent.m_gtStopNeg.newGame(); // initializing the stop-watch
393
394 new Thread(m_agent.m_gtStopNeg).start();
395
396 String sAgentID = sServerLine.substring(sServerLine.indexOf("agentID ")+8);
397
398 m_agent.setHasOpponent(true, sAgentID);
399
400 m_agent.calculateFirstOffer();
401 }
402 else if (sServerLine.startsWith("type agentOptOut"))
403 {
404 m_agent.setHasOpponent(false, null);
405
406 m_agent.m_gtStopTurn.setRun(false);
407 m_agent.m_gtStopNeg.setRun(false);
408 }
409 else if (sServerLine.equals("type log request error"))
410 {
411 // not relevant for the QOAgent
412 }
413 else if (sServerLine.startsWith("type log response"))
414 {
415 // not relevant for the QOAgent
416 }
417 else if (sServerLine.startsWith("type query"))
418 {
419 // query received
420
421 // get message id
422 String sMsgId;
423 StringTokenizer st = new StringTokenizer(sServerLine);
424
425 boolean bFound = false;
426 while (st.hasMoreTokens() && !bFound)
427 {
428 if (st.nextToken().equals("tag"))
429 {
430 bFound = true;
431 sMsgId = st.nextToken();
432 }
433 }
434
435 String sQuery=sServerLine.substring(sServerLine.indexOf("issueSet ")+9);
436
437 int CurrentAgreementIdx[] = new int[QAgentType.MAX_ISSUES];
438 CurrentAgreementIdx = m_agent.getAgreementIndices(sQuery);
439
440 m_agent.calculateResponse(QUERY, CurrentAgreementIdx, sServerLine);
441 }
442 else if (sServerLine.startsWith("type counter_offer"))
443 {
444 // counter_offer received
445
446 // get message id
447 String sMsgId;
448 StringTokenizer st = new StringTokenizer(sServerLine);
449
450 boolean bFound = false;
451 while (st.hasMoreTokens() && !bFound)
452 {
453 if (st.nextToken().equals("tag"))
454 {
455 bFound = true;
456 sMsgId = st.nextToken();
457 }
458 }
459
460 String sOffer=sServerLine.substring(sServerLine.indexOf("issueSet ")+9);
461
462 int CurrentAgreementIdx[] = new int[QAgentType.MAX_ISSUES];
463 CurrentAgreementIdx = m_agent.getAgreementIndices(sOffer);
464 m_agent.calculateResponse(COUNTER_OFFER, CurrentAgreementIdx, sServerLine);
465 }
466 else if (sServerLine.startsWith("type offer"))
467 {
468 // offer received
469
470 // get message id
471 String sMsgId;
472 StringTokenizer st = new StringTokenizer(sServerLine);
473
474 boolean bFound = false;
475 while (st.hasMoreTokens() && !bFound)
476 {
477 if (st.nextToken().equals("tag"))
478 {
479 bFound = true;
480 sMsgId = st.nextToken();
481 }
482 }
483
484 String sOffer=sServerLine.substring(sServerLine.indexOf("issueSet ")+9);
485
486 int CurrentAgreementIdx[] = new int[QAgentType.MAX_ISSUES];
487 CurrentAgreementIdx = m_agent.getAgreementIndices(sOffer);
488
489 m_agent.calculateResponse(OFFER, CurrentAgreementIdx, sServerLine);
490 }
491 else if (sServerLine.startsWith("type promise"))
492 {
493 // promise received
494
495 // get message id
496 String sMsgId;
497 StringTokenizer st = new StringTokenizer(sServerLine);
498
499 boolean bFound = false;
500 while (st.hasMoreTokens() && !bFound)
501 {
502 if (st.nextToken().equals("tag"))
503 {
504 bFound = true;
505 sMsgId = st.nextToken();
506 }
507 }
508
509 String sPromise=sServerLine.substring(sServerLine.indexOf("myIssueSet ")+11);
510 String sMyIssueSet=sPromise.substring(0,sPromise.indexOf("yourIssueSet "));
511 String sYourIssueSet=sPromise.substring(sPromise.indexOf("yourIssueSet ")+13);
512
513 // parse to one agreement
514 int CurrentAgreementIdxMine[] = new int[QAgentType.MAX_ISSUES];
515 int CurrentAgreementIdxYours[] = new int[QAgentType.MAX_ISSUES];
516 CurrentAgreementIdxMine = m_agent.getAgreementIndices(sMyIssueSet);
517 CurrentAgreementIdxYours = m_agent.getAgreementIndices(sYourIssueSet);
518
519 // combine indices
520 for (int i = 0; i < QAgentType.MAX_ISSUES; ++i)
521 {
522 if (CurrentAgreementIdxYours[i] != QAgentType.NO_VALUE)
523 CurrentAgreementIdxMine[i] = CurrentAgreementIdxYours[i];
524 }
525
526 m_agent.calculateResponse(PROMISE, CurrentAgreementIdxMine, sServerLine);
527 }
528 else if (sServerLine.equals("nak") || sServerLine.equals("ack"))
529 {
530 sParsedString = sServerLine;
531 }
532 else // other unknown message
533 {
534 System.out.println("[QO]Unknown Message Error: " + sServerLine + " [QMessages::parseMessage(470)]");
535 System.err.println("[QO]Unknown Message Error: " + sServerLine + " [QMessages::parseMessage(470)]");
536
537 sParsedString = sServerLine;
538 }
539
540 return sParsedString;
541 }
542}
Note: See TracBrowser for help on using the repository browser.