/*********************************************************************/ /* */ /* AgentService Framework */ /* www.agentservice.it */ /* */ /* Copyright (C) 2007 l.i.d.o. - DIST, University of Genoa, Italy */ /* GNU Lesser General Public License */ /* */ /* This library is free software; you can redistribute it and/or */ /* modify it under the terms of the GNU Lesser General Public */ /* License as published by the Free Software Foundation, */ /* version 2.1 of the License. */ /* */ /* This library is distributed in the hope that it will be useful, */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */ /* Lesser General Public License for more details. */ /* */ /* You should have received a copy of the GNU Lesser General Public */ /* License along with this library; if not, write to the */ /* Free Software Foundation, Inc., 59 Temple Place - Suite 330, */ /* Boston, MA 02111-1307, USA. */ /* */ /*********************************************************************/ using System; using System.Collections.Generic; // this namespace defines the elements for the creation an management // of agents. using AgentService.Agent; // this namespace contains the elements for the instantiation // of new knowledges. using AgentService.Agent.Knowledge; // this namespac contains the elements for the instantiation // of new behaviours. using AgentService.Agent.Behaviour; // this namespace defines the interface for the runtime services using AgentService.Agent.Runtime; // this namespace defines the elements for the messaging system using AgentService.Platform.MTS; namespace AuctionDemo { /// /// The template of the agent that manages the auction /// [AgentKnowledges(typeof(AuctionData))] [AgentBehaviours(typeof(ManageAuction))] public class Auctioneer : AgentService.Agent.AgentTemplate { /// /// Reads data about the auction from a file. /// The file has the following structure: /// item CRLF /// price CRLF /// minIncrement CRLF /// maxParticpants CRLF /// /// configuration file path /// true if the file is successfully read public override bool Configure(string configFile) { System.IO.TextReader reader = null; try { reader = new System.IO.StreamReader(configFile); string line = reader.ReadLine(); this.settings.Add("Item", line); line = reader.ReadLine(); this.settings.Add("Price", float.Parse(line)); line = reader.ReadLine(); this.settings.Add("MinIncrement", float.Parse(line)); line = reader.ReadLine(); this.settings.Add("MaxParticipants", int.Parse(line)); return true; } catch(System.IO.IOException ioex) { return false; } catch(System.FormatException fex) { return false; } finally { if (reader != null) { reader.Close(); } } } /// /// This method initializes the auctionner agent. /// /// factory object that allows the instantiation of knwoledge and behaviour public override void Initiate(AgentService.Agent.Runtime.IFactoryContext factoryContext) { string item = this.settings["Item"] as string; float price = (float) this.settings["Price"]; float minIncrement = (float) this.settings["MinIncrement"]; int maxParticipants = (int)this.settings["MaxParticipants"]; // it creates an instance of the knwoledge factoryContext.CreateKnowledge(typeof(AuctionData), "Auction", PersistenceMode.StrongPersistent, item, price, minIncrement, maxParticipants); // it creates an instance of the behaviour previously registered. factoryContext.CreateBehaviour(typeof(ManageAuction),"ManageAuction","Auction"); } /// /// Handles the incoming conversation /// /// event source /// params related to the event public override void HandleConversation(AgentService.Agent.Runtime.IAgentContext sender, ConversationArgs args) { int maxParticipants = (int) this.settings["MaxParticipants"]; if (maxParticipants > 0) { maxParticipants--; // it accepts the conversation args.Conversation.Accept(); // it adds the conversation to the conversations queue. sender.AddConversation("ManageAuction",args.Conversation); this.settings["MaxParticipants"] = maxParticipants; } else { // if the maximum number of participants is reached the incoming conversation is rejected. args.Conversation.Reject(); args.Conversation.Close(); } } /// /// Handles the termination of the agent. /// public override void Terminate() { // non fa niente in questo caso. } /// /// Must override. /// Resumes the agent on Platform restart. /// /// reference to the context for the registration of new knowledge and behaviour types /// reference to the context for the istantiation of new knowledge and behaviour objects public override void Resume(ITypeContext typeContext, IFactoryContext factoryContext) { } } }