source: src/main/java/agents/qoagent/AutomatedAgentMessages.java

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