001: /**
002: * <copyright>
003: *
004: * Copyright 2002-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */package org.cougaar.pizza.plugin;
026:
027: import org.cougaar.core.service.LoggingService;
028: import org.cougaar.core.util.UID;
029: import org.cougaar.core.util.UniqueObject;
030:
031: import java.util.HashMap;
032: import java.util.Iterator;
033: import java.util.Map;
034: import java.util.Set;
035: import java.util.TreeSet;
036:
037: import org.cougaar.util.log.Logger;
038: import org.cougaar.util.log.Logging;
039:
040: import org.cougaar.pizza.Constants;
041: import org.cougaar.pizza.servlet.HistoryServletFriendly;
042:
043: /**
044: * Local accumulation of replies to invite, marking who has replied and the kinds
045: * of pizza they want. Automatically updated by the {@link org.cougaar.pizza.relay.RSVPRelaySource}. Published
046: * by the InvitePlugin so that the PlaceOrderPlugin knows to start.
047: *<p>
048: * This class works as an example of publishing a custom object on
049: * the Blackboard. Note that it implements UniqueObject
050: * (which extends Serializable) for easy identification and retrieval
051: * on the Blackboard, and so it can be sent in messages.
052: *
053: * @see org.cougaar.pizza.plugin.InvitePlugin
054: * @see org.cougaar.pizza.plugin.PlaceOrderPlugin
055: */
056: public class PizzaPreferences implements UniqueObject,
057: HistoryServletFriendly {
058: private UID uid;
059: // Map of accumulated results
060: private Map friendToPizza = new HashMap();
061:
062: // Note this static method of getting a logger within an object.
063: private static Logger log = Logging
064: .getLogger(PizzaPreferences.class);
065:
066: // Total meat and veg servings to order
067: private int numMeat = 0;
068: private int numVeg = 0;
069:
070: public PizzaPreferences(UID uid) {
071: this .uid = uid;
072: }
073:
074: // for UniqueObject interface
075: public UID getUID() {
076: return uid;
077: }
078:
079: public void setUID(UID uid) {
080: this .uid = uid;
081: }
082:
083: /**
084: * The given friend has replied, requesting the given pizza, so store
085: * their response.
086: *
087: * @param friend The agent name responding
088: * @param preference The kind of meat they like (using a pizza Constant)
089: */
090: public void addFriendToPizza(String friend, String preference) {
091: if (friend == null || friend.equals(""))
092: return;
093:
094: friendToPizza.put(friend, preference);
095:
096: if (preference.equals(Constants.MEAT_PIZZA))
097: numMeat++;
098: else if (preference.equals(Constants.VEGGIE_PIZZA))
099: numVeg++;
100: else
101: log.warn("Unknown preference " + preference + " for "
102: + friend);
103: }
104:
105: /**
106: * What kind of pizza does the given friend want?
107: * @param friend to look up
108: * @return kind of pizza they want
109: */
110: public String getPreferenceForFriend(String friend) {
111: return (String) friendToPizza.get(friend);
112: }
113:
114: public Set getFriends() {
115: return friendToPizza.keySet();
116: }
117:
118: /** @return printable list of the friends we have preferences for */
119: public String getFriendNames() {
120: return friendToPizza.keySet().toString();
121: }
122:
123: /** @return printable list of just the preferences collected */
124: public String getPreferenceValues() {
125: return friendToPizza.values().toString();
126: }
127:
128: /** @return printable list of the friend-to-preference mapping */
129: public String getFriendToPreference() {
130: return friendToPizza.toString();
131: }
132:
133: /** @return Number of people wanting meat pizza */
134: public int getNumMeat() {
135: return numMeat;
136: }
137:
138: /** @return Number of orders for veggie pizza */
139: public int getNumVeg() {
140: return numVeg;
141: }
142:
143: /**
144: * Print an HTML-friendly description of the object, for use in the HistoryServlet.
145: * @param type Not-used here, is the transaction type
146: * @return HTML text to display this object
147: */
148: public String toHTML(int type) {
149: StringBuffer buf = new StringBuffer();
150: buf.append("<table border=1 align=center>");
151: buf.append("<tr>");
152: buf.append("<th>");
153: buf.append("Friend");
154: buf.append("</th>");
155: buf.append("<th>");
156: buf.append("Preference");
157: buf.append("</th>");
158: buf.append("</tr>");
159: for (Iterator iter = new TreeSet(getFriends()).iterator(); iter
160: .hasNext();) {
161: buf.append("<tr>");
162:
163: buf.append("<td>");
164: String friend = (String) iter.next();
165: buf.append(friend);
166: buf.append("</td>");
167:
168: buf.append("<td>");
169: String preference = getPreferenceForFriend(friend);
170: buf.append(preference);
171: buf.append("</td>");
172:
173: buf.append("</tr>");
174: }
175: buf.append("</table>");
176: return buf.toString();
177: }
178:
179: public String toString() {
180: return "Party guests pizza preferences: " + friendToPizza;
181: }
182: }
|