/** * */ package es.upm.dit.gsi.sojason; import jason.asSyntax.Literal; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; /** * * * Project: Web40SOJason * Package: es.upm.dit.gsi.sojason * Class: SOModel * * @author Miguel Coronado (miguelcb@dit.upm.es) * @version Mar 9, 2012 * */ abstract public class SOModel { /** * This contains the data that will be */ private Map> serviceDataInbox; /** * Constructor. Just initializes attributes. */ public SOModel(){ this.serviceDataInbox = Collections.synchronizedMap(new HashMap>()); } /** *

This puts data into the serviceDataInbox for a * particular agent. The data loaded will be appended into the already * existing data if any.

* *

The data from the serviceDataInbox is removed as * described in the documentation of {@link #getDataFromInbox(String)}

* * @param agName The name of the agent. * @param serviceData The service-data. */ public void setDataInbox (String agName, Collection serviceData){ synchronized (serviceDataInbox) { if (!this.serviceDataInbox.containsKey(agName)) { Set set = new HashSet(); set.addAll(serviceData); // create a set and add all the collection this.serviceDataInbox.put(agName, set); return; } // There is no data in the inbox for the agent given Set set = this.serviceDataInbox.get(agName); set.addAll(serviceData); this.serviceDataInbox.put(agName, set); } } /** *

This provides a different way to call the method * {@linkplain #setDataInbox(String, Collection)} with a single literal * instead of a collection of literals.

* * @param agName the name of the agent. * @param literal the literal. */ public void setDataInbox (String agName, Literal literal) { Set set = new HashSet(); set.add(literal); setDataInbox(agName, set); } /** *

This gets from the serviceDataInbox the data stored for * the agent given. It will remove the data from the inbox, so two consequent * invocations of this method will return different results, actually, if no * new data is put, the second invocation will return no data.

* *

So, it is important to point out this method empties the * serviceDataInbox.

* *

This method never returns null to avoid null pointer

* * @param agName the name of the agent who data will be retrieved from * the inbox * @return The data retrieved */ public Collection getDataFromInbox (String agName) { if (!this.serviceDataInbox.containsKey(agName)){ return new HashSet(); } // return this.serviceDataInbox.get(agName); return this.serviceDataInbox.remove(agName); } }