/* * Copyright (c) 2005-2006 Erik Tigerholm * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ namespace OpenTraffic.Model.SignalPlans { using System; using System.Collections.Generic; using System.Xml.Serialization; [XmlInclude(typeof(Fixed))] [Serializable] public abstract class SignalPlan { [XmlElement(ElementName = "Id")] public string Id { get; set; } [XmlElement(ElementName = "Name")] public string Name { get; set; } [XmlAttribute(AttributeName = "CycleTime")] public float CycleTime { get; set; } [XmlAttribute(AttributeName = "CycleOffset")] public float CycleOffset { get; set; } [XmlIgnore] public int NumberOfStages { get { return Stages.Count; } } public TimeSlice Active { get; set; } public List Stages { get; set; } protected SignalPlan() { Stages = new List(); } protected SignalPlan(string id, string name, float cycleTime, float cycleOffset, TimeSlice active) { CycleTime = cycleTime; CycleOffset = cycleOffset; Active = active; this.Name = name; this.Id = id; Stages = new List(); } public abstract void BlockTurns(float t); public override string ToString() { return Id + " - " + Name; } public bool IsActive(float time) { if (Active.StartTime < Active.StopTime) { if (time > Active.StartTime && time < Active.StopTime) { return true; } } else { if (time < Active.StartTime || time > Active.StopTime) { return true; } } return false; } public bool InvolvesNode(IntersectionNode n) { foreach (SignalStage st in Stages) { if (st.InvolvesNode(n)) return true; } return false; } protected int GetStage(float time) { float t = (time - CycleOffset) % CycleTime; float tTot = 0; for (int i = 0; i < Stages.Count; i++) { tTot += Stages[i].Duration; if (tTot >= t) return i; } return 0; } } }