/*********************************************************************/ /* */ /* 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.Threading; namespace AgentService.Agent.Behaviour { /// /// Class BehaviourScheduleUnit. Unit of schedule for a behaviour object. /// [Serializable] public class BehaviourScheduleUnit { /// /// Thread that schedules the Behaviour /// private Thread thread; /// /// Gets, sets the scheduling priority for the Behavoiur /// public ThreadPriority Priority { get { return this.thread.Priority; } set { this.thread.Priority = value; } } /// /// Behaviour instance /// public Behaviour behaviour; /// /// Constructs a BehaviourScheduleUnit for the given Behaviour /// /// The Behaviour to be scheduled public BehaviourScheduleUnit(Behaviour behaviour) { this.behaviour = behaviour; this.thread = new Thread(new ThreadStart(this.behaviour.Execute)); this.thread.Name = String.Format("{0}#{1}", behaviour.Owner.Name, behaviour.Name); } /// /// Constructs a BehaviourScheduleUnit for the given Behaviour with a specified priority /// /// The Behaviour to be scheduled /// The priority for the Behaviour public BehaviourScheduleUnit(Behaviour behaviour, ThreadPriority priority) { this.behaviour = behaviour; this.thread = new Thread(new ThreadStart(this.behaviour.Execute)); this.Priority = priority; } /// /// Starting the Body of the associeted Behaviour /// public void Start() { this.thread.Start(); } /// /// Abort the execution of the wrapped thread /// public void Abort() { this.thread.Abort(); } /// /// Resume the execution of the wrapped thread /// public void Resume() { this.thread.Resume(); } /// /// Suspend the execution of the wrapped thread /// public void Suspend() { this.thread.Suspend(); } /// /// At Behaviour's end /// Release the Knowledge locked by the Behaviour /// public void Release() { try { this.behaviour.ReleaseKnowledge(true); } catch(AgentService.Agent.Knowledge.KnowledgeUnlockedException e) { // Put log Console.WriteLine(e.Message); } } /// /// Overrides Equals method /// /// The object to compare to /// true if Behaviour fields are the same public override bool Equals(object obj) { BehaviourScheduleUnit bsu = obj as BehaviourScheduleUnit; if (bsu == null) { return false; } return this.behaviour.Equals(bsu.behaviour); } /// /// Gets the hash code for this BehaviourScheduleUnit instance. /// /// wrapped behaviour has code public override int GetHashCode() { return this.behaviour.GetHashCode(); } } }