public void MyTestInitialize() { CTCController ctc = CTCController.GetCTCController(); PrivateObject privateAccessor = new PrivateObject(ctc); m_ctcAccessor = new CTCController_Accessor(privateAccessor); List <TrackBlock> trackRegion = new List <TrackBlock>(); TrackBlock redBlock1 = new TrackBlock("red1", TrackOrientation.EastWest, new Point(0, 0), 50.0, 0.25, 0.5, false, false, 40, TrackAllowedDirection.Both, false, "redController1", null, null, "red2"); trackRegion.Add(redBlock1); TrackBlock redBlock2 = new TrackBlock("red2", TrackOrientation.SouthWestNorthEast, redBlock1.EndPoint, 50.0, 0.75, 1, false, false, 40, TrackAllowedDirection.Both, false, "redController1", "redController2", "red1", "red3"); trackRegion.Add(redBlock2); TrackBlock redBlock3 = new TrackBlock("red3", TrackOrientation.NorthSouth, redBlock2.EndPoint, 50.0, 1.50, 1.5, false, false, 40, TrackAllowedDirection.Both, false, "redController2", "redController1", "red2", "red4"); trackRegion.Add(redBlock3); TrackBlock redBlock4 = new TrackBlock("red4", TrackOrientation.SouthWestNorthEast, redBlock3.EndPoint, 50.0, 2.5, 2, false, false, 40, TrackAllowedDirection.Both, false, "redController2", null, "red3", "red5"); trackRegion.Add(redBlock4); m_ctcAccessor.BuildLayout(trackRegion, new List <TrackSwitch>()); // update this for switches }
public void TrackBlockConstructorTest() { TrackOrientation orientation = TrackOrientation.EastWest; string name = "TestBlock"; double length = 3.5; double endElevation = 1.5; double grade = 1; int staticSpeed = 75; bool tunnel = false; bool railroadCrossing = false; Point startPoint = new Point(0, 0); TrackBlock target = new TrackBlock(name, orientation, startPoint, length, endElevation, grade, false, true, staticSpeed, TrackAllowedDirection.Both, false, "controller", "controller2", "prevBlock", "nextBlock"); target.Transponder = new Transponder("Station1", 2); Assert.AreEqual(orientation, target.Orientation); Assert.AreEqual(length, target.LengthMeters); Assert.IsFalse(target.HasTunnel); Assert.IsNotNull(target.Transponder); Assert.AreEqual("Station1", target.Transponder.StationName); Assert.AreEqual(name, target.Name); Assert.AreEqual(2, target.Transponder.DistanceToStation); Assert.AreEqual(startPoint.X, target.StartPoint.X); Assert.AreEqual(startPoint.Y, startPoint.Y); Assert.AreEqual(grade, target.Grade); Assert.AreEqual(endElevation, target.EndElevationMeters); }
static void Main() { TrackBlock redBlock1 = new TrackBlock("red1", TrackOrientation.EastWest, new Point(35, 0), 50.0, 0, 0, false, false, 40, TrackAllowedDirection.Both, null, "redController1", null, "red8", "red2"); TrackBlock redBlock2 = new TrackBlock("red2", TrackOrientation.NorthWestSouthEast, new Point(85, 0), 50.0, 0, 0, false, false, 40, TrackAllowedDirection.Both, null, "redController1", null, "red1", "red3"); TrackBlock redBlock3 = new TrackBlock("red3", TrackOrientation.NorthSouth, new Point(121, 85), 50.0, 0, 0, false, false, 40, TrackAllowedDirection.Both, null, "redController1", null, "red2", "red4"); TrackBlock redBlock4 = new TrackBlock("red4", TrackOrientation.SouthWestNorthEast, new Point(85, 121), 50.0, 0, 0, false, false, 40, TrackAllowedDirection.Both, null, "redController1", null, "red3", "red5"); TrackBlock redBlock5 = new TrackBlock("red5", TrackOrientation.EastWest, new Point(35, 121), 50.0, 0, 0, false, false, 40, TrackAllowedDirection.Both, null, "redController1", null, "red4", "red6"); TrackBlock redBlock6 = new TrackBlock("red6", TrackOrientation.NorthWestSouthEast, new Point(0, 85), 50.0, 0, 0, false, false, 40, TrackAllowedDirection.Both, null, "redController1", null, "red5", "red7"); TrackBlock redBlock7 = new TrackBlock("red7", TrackOrientation.NorthSouth, new Point(0, 85), 50.0, 0, 0, false, false, 40, TrackAllowedDirection.Both, null, "redController1", null, "red6", "red8"); TrackBlock redBlock8 = new TrackBlock("red8", TrackOrientation.SouthWestNorthEast, new Point(0, 35), 50.0, 0, 0, false, false, 40, TrackAllowedDirection.Both, null, "redController1", null, "red7", "red1"); redBlock1.NextBlock = redBlock2; redBlock2.NextBlock = redBlock3; redBlock3.NextBlock = redBlock4; redBlock4.NextBlock = redBlock5; redBlock5.NextBlock = redBlock6; redBlock6.NextBlock = redBlock7; redBlock7.NextBlock = redBlock8; redBlock8.NextBlock = redBlock1; ITrain t = new Train("Train1", redBlock1, Direction.East); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new TrainForm(t)); System.AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; }
/// <summary> /// Displays info about the train yard /// </summary> /// <param name="b">Track block</param> public void SetTrainYardInfo(TrackBlock b) { //Just show the block name m_displayedTrain = null; m_displayedBlock = b; SetInfo(b.Name, null); }
/// <summary> /// Sets the selected track block, or clears it if null /// </summary> /// <param name="block">Selected track block</param> public void SetSelectedTrackBlock(TrackBlock block) { m_selectedBlock = block; if (block != null) { chkBrokenRail.Visible = chkCircuitFail.Visible = chkPowerFail.Visible = true; if (block.Status != null) { //Set the failure status chkBrokenRail.Checked = block.Status.BrokenRail; chkCircuitFail.Checked = block.Status.CircuitFail; chkPowerFail.Checked = block.Status.PowerFail; } else { //This shouldn't happen. Uncheck the boxes chkBrokenRail.Checked = chkCircuitFail.Checked = chkPowerFail.Checked = false; } } else { //Hide the check boxes chkBrokenRail.Visible = chkCircuitFail.Visible = chkPowerFail.Visible = false; chkBrokenRail.Checked = chkCircuitFail.Checked = chkPowerFail.Checked = false; } }
// METHOD: TrackBlockGraphic //-------------------------------------------------------------------------------------- /// <summary> /// Primary constructor for the track block graphic class /// </summary> /// <param name="block">Track block object this graphic represents</param> /// <param name="scale">Scaling constant for the screen</param> //-------------------------------------------------------------------------------------- public TrackBlockGraphic(TrackBlock block, double scale) { this.SuspendLayout(); InitializeComponent(); //Need this crap to make it transparent this.DoubleBuffered = true; SetStyle(ControlStyles.OptimizedDoubleBuffer, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.SupportsTransparentBackColor, true); SetStyle(ControlStyles.UserPaint, true); RedColor = Color.Red; YellowColor = Color.Yellow; GreenColor = Color.Green; SuperGreenColor = Color.Green; LineThickness = 5; ArrowLength = 5; Block = block; m_currentColor = GetDrawColor(); SetScale(scale); CalculateArrowPoints(); this.BackColor = Color.Transparent; this.ResumeLayout(); }
/// <summary> /// Updates the owners of the track blocks notifiying them that a train moved to a new position /// </summary> /// <param name="previous">Previous block the train was on</param> /// <param name="current">Current block the train is now on</param> /// <returns></returns> public bool UpdateTrackControllers(TrackBlock previous, TrackBlock current) { //Initialize the track controllers if (m_controllerList != null) { foreach (ITrackController controller in m_controllerList) { controller.Update(); } } return(true); /* * bool result = false; * if (previous != null && current != null) * { * ITrackController previousController = GetTrackController(previous); * ITrackController newController = GetTrackController(current); * * previousController.Update(); * * if (newController != null) * { * if (newController != previousController) * { * newController.Update(); * } * * result = true; * } * } * * return result;*/ }
private void UpdateAuthoritySignal(TrackBlock block, int authority) { if (block == null) { throw new ArgumentNullException(); } if (authority < -1) { throw new ArgumentOutOfRangeException(); } AddUpdatedStatus(block); block.Authority.Authority = authority; if (authority > AUTH_THRESH_SUPERGREEN) { block.Status.SignalState = TrackSignalState.SuperGreen; } else if (authority > AUTH_THRESH_GREEN) { block.Status.SignalState = TrackSignalState.Green; } else if (authority > AUTH_THRESH_YELLOW) { block.Status.SignalState = TrackSignalState.Yellow; } else { block.Status.SignalState = TrackSignalState.Red; } }
private bool IsTrainApproaching(TrackBlock curr, TrackBlock dest) { if (dest == null) { throw new ArgumentNullException(); } List <TrackBlock> blocks = new List <TrackBlock>(m_trackBlocks.Values); while (blocks.Count > 0) { TrackBlock b = blocks.ElementAt <TrackBlock>(0); blocks.Remove(b); if (b == curr) { continue; } if (b.Status.TrainPresent) { for (TrackBlock t = b.GetNextBlock(b.Status.TrainDirection); t != null && blocks.Contains <TrackBlock>(t) && t.Status.IsOpen && !t.Status.TrainPresent; t = (t.NextBlock == t) ? t.PreviousBlock : t.NextBlock) { if (t == dest) { return(true); } blocks.Remove(t); } } } return(false); }
public void CloseTrackTest() { TrackController target = new TrackController(); // TODO: Initialize to an appropriate value string trackId = string.Empty; // TODO: Initialize to an appropriate value bool actual; actual = target.CloseTrack(trackId); Assert.IsFalse(actual); // can't close a track that isn't owned trackId = "akljkjcbao"; actual = target.CloseTrack(trackId); Assert.IsFalse(actual); // assert true if controller owns track and is able to close it TrackBlock block = new TrackBlock(); block.Name = trackId = "TestName"; target.AddTrackBlock(block); actual = target.CloseTrack(trackId); Assert.IsTrue(actual); // assert false if track is already closed actual = target.CloseTrack(trackId); Assert.IsFalse(actual); // shouldn't be able to close a track with a train on it block.Status.IsOpen = true; block.Status.TrainPresent = true; actual = target.CloseTrack(trackId); Assert.IsFalse(actual); }
private void AddUpdatedStatus(TrackBlock b) { if (!m_updatedBlocks.ContainsKey(b.Name)) { m_updatedBlocks.Add(b.Name, b); } }
/// <summary> /// Calculates the position of the graphic on the display panel /// </summary> /// <param name="block">Track block to display</param> /// <param name="arrowLength">Length of arrow graphics for position adjustment</param> /// <returns>Point of the graphic on the display panel</returns> private Point CalculateBlockPosition(TrackBlock block, Point layoutPosition, int arrowLength) { if (block != null) { switch (block.Orientation) { case TrackOrientation.EastWest: return(new Point(System.Convert.ToInt32((block.StartPoint.X - layoutPosition.X) * m_scale), System.Convert.ToInt32((block.StartPoint.Y - layoutPosition.Y) * m_scale - arrowLength))); case TrackOrientation.NorthWestSouthEast: return(new Point(System.Convert.ToInt32((block.StartPoint.X - layoutPosition.X) * m_scale), System.Convert.ToInt32((block.StartPoint.Y - layoutPosition.Y) * m_scale))); case TrackOrientation.SouthWestNorthEast: return(new Point(System.Convert.ToInt32((block.StartPoint.X - layoutPosition.X) * m_scale), System.Convert.ToInt32((block.EndPoint.Y - layoutPosition.Y) * m_scale))); case TrackOrientation.NorthSouth: return(new Point(System.Convert.ToInt32((block.StartPoint.X - layoutPosition.X) * m_scale - arrowLength), System.Convert.ToInt32((block.EndPoint.Y - layoutPosition.Y) * m_scale))); default: return(new Point()); } } return(new Point()); }
// METHOD: SystemController //-------------------------------------------------------------------------------------- /// <summary> /// Manage the starting, stopping, coordination and scheduling of all system processes /// </summary> /// <param name="samplePeriod">Sample period</param> //-------------------------------------------------------------------------------------- private void SystemController(double samplePeriod) { // If this is the first time SystemController has been called, // the train is at the yard and needs to leave if (m_samplePeriod == 0.0) { LeaveStation(); } // Keep track of the time that has passed since leaving the last station m_timePassed += samplePeriod; // Set the sample period m_samplePeriod = samplePeriod; // Set the current track block m_currentBlock = m_currentState.CurrentBlock; // Determine the m_setPoint DetermineSetPoint(); // Check for faults FaultMonitor(); // Generate and issue a power command m_lastCommand = VelocityController(); // Control lights LightController(); // Control announcements and doors StationController(); }
// METHOD: TrackBlockGraphic //-------------------------------------------------------------------------------------- /// <summary> /// Primary constructor for the track block graphic class /// </summary> /// <param name="block">Track block object this graphic represents</param> /// <param name="scale">Scaling constant for the screen</param> //-------------------------------------------------------------------------------------- public TrackBlockGraphic(TrackBlock block, double scale) { this.SuspendLayout(); InitializeComponent(); //Need this crap to make it transparent this.DoubleBuffered = true; SetStyle(ControlStyles.OptimizedDoubleBuffer, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.SupportsTransparentBackColor, true); SetStyle(ControlStyles.UserPaint, true); RedColor = Color.Red; YellowColor = Color.Yellow; GreenColor = Color.Green; SuperGreenColor = Color.Green; DotColor = Color.Orange; LineThickness = 5; ArrowLength = 10; Block = block; m_currentColor = GetDrawColor(); SetScale(scale); CalculateArrowPoints(); this.BackColor = Color.Transparent; this.ResumeLayout(); }
public void SetFrictionTest() { string trainID = "FrictionTestTrain"; TrackBlock block = new TrackBlock("block", TrackOrientation.EastWest, new Point(35, 0), 50.0, 0, 0, false, false, 40, TrackAllowedDirection.Both, false, null, null, "block", "block"); block.NextBlock = block; Direction direction = Direction.East; Train target = new Train(trainID, block, direction); target.SetPower(10000, 5); target.SetEngineFailure(true); double previousSpeed = target.GetSpeed(); target.Update(1); double newSpeed = target.GetSpeed(); double previousChange = previousSpeed - newSpeed; previousSpeed = newSpeed; target.SetFriction(0.5); target.Update(1); newSpeed = target.GetSpeed(); double newChange = previousSpeed - newSpeed; Assert.IsTrue(newChange > previousChange); }
/// <summary> /// Sets the selected track block, or clears it if null /// </summary> /// <param name="block">Selected track block</param> public void SetSelectedTrackBlock(TrackBlock block) { m_selectedBlock = block; if (block != null) { chkBrokenRail.Visible = chkCircuitFail.Visible = chkPowerFail.Visible = true; if (block.Status != null) { //Set the failure status chkBrokenRail.Checked = block.Status.BrokenRail; chkCircuitFail.Checked = block.Status.CircuitFail; chkPowerFail.Checked = block.Status.PowerFail; } else { //This shouldn't happen. Uncheck the boxes chkBrokenRail.Checked = chkCircuitFail.Checked = chkPowerFail.Checked = false; } } else { //Hide the check boxes chkBrokenRail.Visible = chkCircuitFail.Visible = chkPowerFail.Visible = false; chkBrokenRail.Checked = chkCircuitFail.Checked = chkPowerFail.Checked = false; } }
public void BuildLayoutTest() { List <TrackBlock> trackRegion = new List <TrackBlock>(); TrackBlock redBlock1 = new TrackBlock("red1", TrackOrientation.EastWest, new Point(0, 0), 50.0, 0.25, 0.5, false, false, 40, TrackAllowedDirection.Both, false, "redController1", null, null, "red2"); trackRegion.Add(redBlock1); TrackBlock redBlock2 = new TrackBlock("red2", TrackOrientation.SouthWestNorthEast, redBlock1.EndPoint, 50.0, 0.75, 1, false, false, 40, TrackAllowedDirection.Both, false, "redController1", "redcontroller2", "red1", "red3"); trackRegion.Add(redBlock2); TrackBlock redBlock3 = new TrackBlock("red3", TrackOrientation.NorthSouth, redBlock2.EndPoint, 50.0, 1.50, 1.5, false, false, 40, TrackAllowedDirection.Both, false, "redController2", "redcontroller1", "red2", "red4"); trackRegion.Add(redBlock3); TrackBlock redBlock4 = new TrackBlock("red4", TrackOrientation.SouthWestNorthEast, redBlock3.EndPoint, 50.0, 2.5, 2, false, false, 40, TrackAllowedDirection.Both, false, "redController2", null, "red3", "red5"); trackRegion.Add(redBlock4); m_ctcAccessor.BuildLayout(trackRegion, new List <TrackSwitch>()); Assert.IsNotNull(m_ctcAccessor.m_blockTable); Assert.IsTrue(m_ctcAccessor.m_trackTable.ContainsKey(redBlock1)); Assert.IsTrue(m_ctcAccessor.m_trackTable.ContainsKey(redBlock2)); Assert.IsTrue(m_ctcAccessor.m_trackTable.ContainsKey(redBlock3)); Assert.IsTrue(m_ctcAccessor.m_trackTable.ContainsKey(redBlock4)); Assert.IsNotNull(m_ctcAccessor.m_trackTable[redBlock1]); Assert.IsNotNull(m_ctcAccessor.m_trackTable[redBlock2]); Assert.IsNotNull(m_ctcAccessor.m_trackTable[redBlock3]); Assert.IsNotNull(m_ctcAccessor.m_trackTable[redBlock4]); }
/// <summary> /// A train entered a new track block. Need to push an update to the track controllers /// </summary> /// <param name="previous">Previous track block</param> /// <param name="next">Next track block</param> private void OnTrainEnteredNewBlock(TrackBlock previous, TrackBlock next) { if (!m_ctcController.UpdateTrackControllers(previous, next)) { m_log.LogError("Failed to update track controllers"); } }
/// <summary> /// Attempts to set the speed limit for the specified block /// </summary> /// <param name="block">Track block</param> /// <param name="value">Speed limit value</param> /// <returns>Bool success</returns> public bool SetSpeedLimit(TrackBlock block, string value) { bool result = false; int limit; if (Int32.TryParse(value, out limit)) //Parse the string into an integer { //Let the Wayside controller determine if the speed limit is valid //Send speed limit to wayside controller ITrackController controller = GetTrackController(block); if (controller != null) { try { result = controller.SuggestAuthority(block.Name, new BlockAuthority(limit, block.Authority.Authority)); } catch (Exception e) { m_log.LogError("Error in setting speed limit", e); } } } return(result); }
public void GetStateTest() { string trainID = "StateTestTrain"; TrackBlock block = new TrackBlock("block", TrackOrientation.EastWest, new Point(35, 0), 50.0, 0, 0, false, false, 40, TrackAllowedDirection.Both, false, null, null, "block", "block"); block.NextBlock = block; Direction direction = Direction.East; Train target = new Train(trainID, block, direction); TrainState state = target.GetState(); Assert.AreEqual(null, state.Announcement); Assert.AreEqual(0, state.BlockProgress); Assert.AreEqual(false, state.BrakeFailure); Assert.AreEqual(1, state.Cars); Assert.AreEqual(0, state.Crew); Assert.AreEqual(block, state.CurrentBlock); Assert.AreEqual(direction, state.Direction); Assert.AreEqual(TrainState.Door.Open, state.Doors); Assert.AreEqual(false, state.EngineFailure); Assert.AreEqual(TrainState.Light.Off, state.Lights); Assert.AreEqual(40900, state.Mass); Assert.AreEqual(0, state.Passengers); Assert.AreEqual(false, state.SignalPickupFailure); Assert.AreEqual(0, state.Speed); Assert.AreEqual(0, state.Temperature); Assert.AreEqual(trainID, state.TrainID); Assert.AreEqual(35, state.X); Assert.AreEqual(0, state.Y); }
public void OpenTrackBlockTest() { TrackBlock block = m_ctcAccessor.GetBlockList()[0]; bool expected = false; //Block is already open so it returns false bool actual = m_ctcAccessor.OpenTrackBlock(block); Assert.AreEqual(expected, actual); }
public void ShowTrackBlockCommandsTest1() { CommandPanel target = new CommandPanel(); // TODO: Initialize to an appropriate value TrackBlock block = null; // TODO: Initialize to an appropriate value target.ShowTrackBlockCommands(block); Assert.Inconclusive("A method that does not return a value cannot be verified."); }
public void TrackBlockGraphicConstructorTest() { TrackBlock block = null; // TODO: Initialize to an appropriate value double scale = 0F; // TODO: Initialize to an appropriate value TrackBlockGraphic target = new TrackBlockGraphic(block, scale); Assert.Inconclusive("TODO: Implement code to verify target"); }
public void CloseTrackBlockTest_badBlock() { TrackBlock block = new TrackBlock(); // TODO: Initialize to an appropriate value bool expected = false; // TODO: Initialize to an appropriate value bool actual = m_ctcAccessor.CloseTrackBlock(block); Assert.AreEqual(expected, actual); }
public void OpenTrackBlockTest_badBlock() { TrackBlock block = new TrackBlock(); bool expected = false; bool actual = m_ctcAccessor.OpenTrackBlock(block); Assert.AreEqual(expected, actual); }
public void CloseTrackBlockTest_null() { TrackBlock block = null; bool expected = false; bool actual = m_ctcAccessor.CloseTrackBlock(block); Assert.AreEqual(expected, actual); }
public void CloseTrackBlockTest() { TrackBlock block = m_ctcAccessor.GetBlockList()[2]; bool expected = true; bool actual = m_ctcAccessor.CloseTrackBlock(block); Assert.AreEqual(expected, actual); }
public void SetTrainYardInfoTest1() { InfoPanel target = new InfoPanel(); // TODO: Initialize to an appropriate value TrackBlock b = null; // TODO: Initialize to an appropriate value target.SetTrainYardInfo(b); Assert.Inconclusive("A method that does not return a value cannot be verified."); }
public void SetSelectedTrackBlockTest1() { SimulatorWindow target = new SimulatorWindow(); // TODO: Initialize to an appropriate value TrackBlock block = null; // TODO: Initialize to an appropriate value target.SetSelectedTrackBlock(block); Assert.Inconclusive("A method that does not return a value cannot be verified."); }
public void UpdateAuthoritySignalTest1() { TrackController_Accessor target = new TrackController_Accessor(); // TODO: Initialize to an appropriate value TrackBlock block = null; // TODO: Initialize to an appropriate value int authority = 0; // TODO: Initialize to an appropriate value target.UpdateAuthoritySignal(block, authority); Assert.Inconclusive("A method that does not return a value cannot be verified."); }
// METHOD: TrainController //-------------------------------------------------------------------------------------- /// <summary> /// Primary constructor /// </summary> /// /// <param name="myTrain">The train associated with this controller</param> //-------------------------------------------------------------------------------------- public TrainController(ITrain myTrain) { this.m_myTrain = myTrain; this.m_currentState = m_myTrain.GetState(); this.m_trainID = m_currentState.TrainID; this.m_currentBlock = m_currentState.CurrentBlock; this.m_passengerGenerator = new Random((int)DateTime.Now.Ticks); this.ManualSpeed = -1; }
// METHOD: TrainController //-------------------------------------------------------------------------------------- /// <summary> /// Primary constructor /// </summary> /// /// <param name="myTrain">The train associated with this controller</param> //-------------------------------------------------------------------------------------- public TrainController(ITrain myTrain) { this.m_myTrain = myTrain; this.m_currentState = m_myTrain.GetState(); this.m_trainID = m_currentState.TrainID; this.m_currentBlock = m_currentState.CurrentBlock; this.m_passengerGenerator = new Random((int)DateTime.Now.Ticks); this.ManualSpeed = -1; }
public void BlinkTest() { TrackBlock block = null; // TODO: Initialize to an appropriate value double scale = 0F; // TODO: Initialize to an appropriate value TrackBlockGraphic target = new TrackBlockGraphic(block, scale); // TODO: Initialize to an appropriate value target.Blink(); Assert.Inconclusive("A method that does not return a value cannot be verified."); }
public void AddTrackBlockTest() { TrackController target = new TrackController(); // TODO: Initialize to an appropriate value TrackBlock block = null; // TODO: Initialize to an appropriate value bool actual; actual = target.AddTrackBlock(block); Assert.IsFalse(actual); block = new TrackBlock(); actual = target.AddTrackBlock(block); Assert.IsTrue(actual); }
// METHOD: TrackSwitch //-------------------------------------------------------------------------------------- /// <summary> /// Primary constructor with initial state /// </summary> /// /// <param name="name">Track switch name</param> /// <param name="controllerID">ID of the TrackController assigned to the switch</param> /// <param name="trunk">the trunk of the switch</param> /// <param name="branch1">the first branch of the switch</param> /// <param name="branch2">the second branch of the switch</param> /// <remarks> /// The TrackBlock that you pass as trunk should be connected to Branch /// The TrackBlock that you pass as branch1 should be connected to Trunk1 /// The TrackBlock that you pass as branch2 should be connected to Trunk2 /// </remarks> //-------------------------------------------------------------------------------------- public TrackSwitch(string name, string controllerID, TrackBlock trunk, TrackBlock branchClosed, TrackBlock branchOpen) { Name = name; ControllerId = controllerID; TrunkId = trunk.Name; BranchClosedId = branchClosed.Name; BranchOpenId = branchOpen.Name; // initial state BranchClosed = branchClosed; BranchOpen = branchOpen; Trunk = trunk; m_state = TrackSwitchState.Closed; }
public void TrackBlockConstructorTest() { TrackOrientation orientation = TrackOrientation.EastWest; double length = 3.5; bool tunnel = false; bool railroadCrossing = false; Transponder transponder = new Transponder("Station1", 2); Point startPoint = new Point(0,0); TrackBlock target = new TrackBlock(orientation, length, tunnel, railroadCrossing, transponder, startPoint); Assert.AreEqual(orientation, target.Orientation); Assert.AreEqual(length, target.LengthMeters); Assert.IsFalse(target.HasTunnel); Assert.IsNotNull(target.Transponder); Assert.AreEqual("Station1", target.Transponder.StationName); Assert.AreEqual(2, target.Transponder.DistanceToStation); Assert.AreEqual(startPoint.X, target.StartPoint.X); Assert.AreEqual(startPoint.Y, startPoint.Y); }
// METHOD: TrackController // - TrackController Constructor public bool AddTrackBlock(TrackBlock block, List<TrackBlock> adjBlocks) { // Null Arguments if (block == null) return false; if (adjBlocks == null) return false; // TrackBlock Duplicated if (m_trackBlocks.ContainsKey(block.Name)) return false; m_trackBlocks.Add(block.Name, block); // TODO: Add code for adjecent blocks return true; }
/// <summary> /// Sets the text information for a track block /// </summary> /// <param name="block">Track block</param> public void SetTrackBlockInfo(TrackBlock block) { if (block == null) return; Dictionary<string, string> info = new Dictionary<string, string>(); if (block.Authority != null) { info.Add("Authority:", block.Authority.Authority.ToString()); } if (block.Authority != null) { info.Add("Speed Limit:", block.Authority.SpeedLimitKPH.ToString() + " " + KPH); } if (block.Status.TrainPresent) { info.Add("Train present:", "yes"); } else { info.Add("Train present:", "no"); } info.Add("Signal:", block.Status.SignalState.ToString()); info.Add("Start Elevation:", block.StartElevationMeters.ToString() + " " + METERS); info.Add("End Elevation", block.EndElevationMeters.ToString() + " " + METERS); info.Add("Grade", block.Grade.ToString() + "%"); info.Add("Length:", block.LengthMeters.ToString() + " " + METERS); if (block.HasTunnel) { info.Add("Has Tunnel:", "yes"); } else { info.Add("Has Tunnel:", "no"); } if (block.RailroadCrossing) { info.Add("Has RR Crossing:", "yes"); } else { info.Add("Has RR Crossing:", "no"); } if (block.Transponder != null) //Could use block.HasTransponder property, but check for null to be on the safe side... { if (block.Transponder.DistanceToStation != 0) { info.Add("Transponder:", block.Transponder.StationName + "in " + block.Transponder.DistanceToStation.ToString() + " block(s)"); } else { info.Add("Transponder:", "At " + block.Transponder.StationName); } } else { info.Add("Transponder:", "none"); } KeyValuePair<string, string> failure = GetBlockFailureStateString(block); info.Add(failure.Key, failure.Value); #if DEBUG info.Add("Start Point", block.StartPoint.X + ", " + block.StartPoint.Y); info.Add("End Point", block.EndPoint.X + ", " + block.EndPoint.Y); #endif SetInfo(block.Name, info); }
/// <summary> /// Creates a new train on the track /// /// </summary> /// <param name="initialBlock">Starting block of the train</param> /// <param name="name">Name of the train</param> public void SpawnNewTrain(TrackBlock initialBlock, string name) { if (initialBlock != null) { if (initialBlock.HasTransponder) { string start = initialBlock.Transponder.StationName; if (start.Contains(Constants.TRAINYARD)) //Can only spawn trains from stations { if (m_startingDirections.ContainsKey(start)) { m_log.LogInfoFormat("Spawning new train \"{0}\" at start {1}", name, start); //Create the new train and train controller ITrain train = new TrainLib.Train(name, initialBlock, m_startingDirections[start]); train.TrainEnteredNewBlock += OnTrainEnteredNewBlock; TrainController trainController = new TrainController(train); m_trainControllerList.Add(trainController); m_trainControllerTable[train] = trainController; CTCController.GetCTCController().AddTrainToList(train); //Set the train schedule if (start == Constants.REDYARD) { m_log.LogInfoFormat("Setting schedule of {0} to red line", name); trainController.Schedule = CTCController.GetCTCController().GetRedlineSchedule(); } else if (start == Constants.GREENYARDOUT) { m_log.LogInfoFormat("Setting schedule of {0} to green line", name); trainController.Schedule = CTCController.GetCTCController().GetGreenlineSchedule(); } } } } } }
/// <summary> /// Sets the text information for a train /// </summary> /// <param name="train">Train</param> public void SetTrainInfo(ITrain train) { if (train == null) return; TrainState state = train.GetState(); if (state == null) return; m_displayedBlock = null; m_displayedTrain = train; Dictionary<string, string> info = new Dictionary<string, string>(); info.Add("Number of cars:", state.Cars.ToString()); info.Add("Crew members:", state.Crew.ToString()); info.Add("Direction:", state.Direction.ToString()); info.Add("Door Status:", state.Doors.ToString()); info.Add("Light Status:", state.Lights.ToString()); info.Add("Mass:", state.Mass.ToString()); info.Add("Passengers:", state.Passengers.ToString()); info.Add("Speed:", string.Format("{0:0.00} {1}", state.Speed.ToString(), KPH)); info.Add("Temperature:", state.Temperature.ToString()); info.Add("Position:", string.Format("{0:0.00} , {1:0.00}", state.X.ToString(), state.Y.ToString())); if (state.TrainID != null) { SetInfo(state.TrainID.ToString(), info); } else { SetInfo(UNKNOWN_TEXT, info); } }
public void OnTrackBlockClickedTest() { MainScreen_Accessor target = new MainScreen_Accessor(); TrackBlock b = new TrackBlock(); target.OnTrackBlockClicked(b); Assert.AreEqual(b, target.m_selectedTrackBlock); Assert.IsNull(target.m_selectedTrain); Assert.IsNotNull(target.m_simulatorWindow); Assert.AreEqual(0, target.m_openPopups.Count); }
/// <summary> /// Simulates a track block power failure /// </summary> /// <param name="block">Track block to simulate on</param> /// <param name="failure">True to invoke failure or false to clear it</param> public void SimulatePowerFailure(TrackBlock block, bool failure) { if (block != null) { if (block.Status != null) { m_log.LogInfoFormat("Setting power fail of block {0} to {1}", block.Name, failure); block.Status.PowerFail = failure; m_ctcController.UpdateTrackController(block); } } }
/// <summary> /// Event handler for the track block clicked event /// </summary> /// <param name="b">Track block that was clicked</param> private void OnTrackBlockClicked(TrackBlock b) { m_log.LogInfo("Track block was clicked"); m_selectedTrain = null; m_selectedTrackBlock = b; if (m_trainGUI != null) { m_trainGUI.Close(); m_trainGUI = null; } if (m_simulatorWindow == null || m_simulatorWindow.IsDisposed || m_simulatorWindow.Disposing) { m_simulatorWindow = new SimulatorWindow(); //User may have closed the simulator m_simulatorWindow.Show(); } m_simulatorWindow.SetSelectedTrackBlock(m_selectedTrackBlock); if (b != null) { bool trainYard = false; if (b.HasTransponder) { if (!string.IsNullOrEmpty(b.Transponder.StationName)) { if (b.Transponder.StationName.Contains(Constants.TRAINYARD) && b.Transponder.DistanceToStation == 0) { //This is a train yard, handle it specially infoPanel.SetTrainYardInfo(b); commandPanel.ShowTrainYardCommands(); trainYard = true; } } } if (!trainYard) { //Normal track block infoPanel.SetTrackBlockInfo(b); commandPanel.ShowTrackBlockCommands(b); } } CloseOpenPopups(); }
public void IsTrainApproachingTest() { TrackController_Accessor target = new TrackController_Accessor(); // TODO: Initialize to an appropriate value TrackBlock dest = new TrackBlock("dest", TrackOrientation.EastWest, new Point(), 0.0, 0.0, 0.0, false, false, 0, TrackAllowedDirection.Both, true, "controller", "", "", ""); TrackBlock b1 = new TrackBlock("track1", TrackOrientation.EastWest, new Point(), 0.0, 0.0, 0.0, false, false, 0, TrackAllowedDirection.Both, false, "controller", "", "", ""); TrackBlock b2 = new TrackBlock("track2", TrackOrientation.EastWest, new Point(), 0.0, 0.0, 0.0, false, false, 0, TrackAllowedDirection.Both, false, "controller", "", "", ""); TrackBlock b3 = new TrackBlock("track3", TrackOrientation.EastWest, new Point(), 0.0, 0.0, 0.0, false, false, 0, TrackAllowedDirection.Both, false, "controller", "", "", ""); b1.Orientation = TrackOrientation.EastWest; b1.Status.TrainDirection = Direction.East; b1.Status.TrainPresent = true; b1.NextBlock = b2; b2.Orientation = TrackOrientation.EastWest; b2.Status.TrainDirection = Direction.East; b2.NextBlock = b3; b3.Orientation = TrackOrientation.EastWest; b3.Status.TrainDirection = Direction.East; b3.NextBlock = dest; target.AddTrackBlock(b1); target.AddTrackBlock(b2); target.AddTrackBlock(b3); target.AddTrackBlock(dest); bool actual; actual = target.IsTrainApproaching(dest); Assert.IsTrue(actual); b1.Orientation = TrackOrientation.EastWest; b1.Status.TrainDirection = Direction.West; b1.Status.TrainPresent = true; b1.PreviousBlock = b2; b2.Orientation = TrackOrientation.EastWest; b2.Status.TrainDirection = Direction.West; b2.PreviousBlock = b3; b3.Orientation = TrackOrientation.EastWest; b3.Status.TrainDirection = Direction.West; b3.PreviousBlock = dest; target.AddTrackBlock(b1); target.AddTrackBlock(b2); target.AddTrackBlock(b3); target.AddTrackBlock(dest); actual = target.IsTrainApproaching(dest); Assert.IsTrue(actual); b1.Orientation = TrackOrientation.EastWest; b1.Status.TrainDirection = Direction.West; b1.PreviousBlock = b2; b2.Orientation = TrackOrientation.EastWest; b2.Status.TrainDirection = Direction.West; b2.PreviousBlock = b3; b3.Orientation = TrackOrientation.EastWest; b3.Status.TrainDirection = Direction.West; b3.PreviousBlock = dest; target.AddTrackBlock(b1); target.AddTrackBlock(b2); target.AddTrackBlock(b3); target.AddTrackBlock(dest); actual = target.IsTrainApproaching(dest); Assert.IsFalse(actual); b1.Orientation = TrackOrientation.NorthSouth; b1.Status.TrainDirection = Direction.South; b1.PreviousBlock = b2; b2.Orientation = TrackOrientation.NorthSouth; b2.Status.TrainDirection = Direction.South; b2.PreviousBlock = b3; b3.Orientation = TrackOrientation.NorthSouth; b3.Status.TrainDirection = Direction.South; b3.PreviousBlock = dest; target.AddTrackBlock(b1); target.AddTrackBlock(b2); target.AddTrackBlock(b3); target.AddTrackBlock(dest); actual = target.IsTrainApproaching(dest); Assert.IsFalse(actual); }
// METHOD: CreateTrain_Click //-------------------------------------------------------------------------------------- /// <summary> /// Start the GUI, and create a train and train controller if in a demo /// </summary> /// /// <param name="sender">Sender</param> /// <param name="e">Event Arguments</param> //-------------------------------------------------------------------------------------- private void CreateTrain_Click(object sender, EventArgs e) { if (m_isDemo) { m_startingBlock = new TrackBlock("Block1", TrackOrientation.EastWest, new Point(0, 0), 1650, 0, 0, false, false, 70, TrackAllowedDirection.Both, false, "controller1", "controller2", "Block0", "Block2"); m_startingBlock.NextBlock = new TrackBlock("Block2", TrackOrientation.EastWest, new Point(1650, 0), 50, 0, 0, true, false, 70, TrackAllowedDirection.Both, false, "controller1", "controller2", "Block1", "Block3"); m_startingBlock.Authority = new BlockAuthority(70, 3); m_startingBlock.NextBlock.Authority = new BlockAuthority(70, 2); m_startingBlock.NextBlock.NextBlock = new TrackBlock("Block3", TrackOrientation.EastWest, new Point(1700, 0), 1000, 0, 0, true, false, 70, TrackAllowedDirection.Both, false, "controller1", "controller2", "Block2", "Block4"); m_startingBlock.NextBlock.NextBlock.NextBlock = new TrackBlock("Block4", TrackOrientation.EastWest, new Point(2700, 0), 100, 0, 0, false, false, 70, TrackAllowedDirection.Both, false, "controller1", "controller2", "Block3", "Block5"); m_startingBlock.NextBlock.NextBlock.NextBlock.NextBlock = new TrackBlock("Block5", TrackOrientation.EastWest, new Point(2800, 0), 100, 0, 0, false, false, 70, TrackAllowedDirection.Both, false, "controller1", "controller2", "Block4", "Block6"); m_startingBlock.NextBlock.NextBlock.NextBlock.NextBlock.NextBlock = new TrackBlock("Block6", TrackOrientation.EastWest, new Point(2800, 0), 100, 0, 0, false, false, 70, TrackAllowedDirection.Both, false, "controller1", "controller2", "Block5", "Block7"); m_startingBlock.NextBlock.NextBlock.Authority = new BlockAuthority(70, 1); m_startingBlock.NextBlock.NextBlock.NextBlock.Authority = new BlockAuthority(40, 0); m_startingBlock.NextBlock.NextBlock.NextBlock.NextBlock.Authority = new BlockAuthority(40, 0); m_startingBlock.NextBlock.NextBlock.NextBlock.NextBlock.NextBlock.Authority = new BlockAuthority(40, 0); m_startingBlock.Transponder = new Transponder("SHADYSIDE", 1); m_startingBlock.NextBlock.Transponder = new Transponder("SHADYSIDE", 0); m_myTrain = new TrainLib.Train("train1", m_startingBlock, Direction.East); m_myTrainState = m_myTrain.GetState(); m_myTrainController = new TrainController(m_myTrain); m_myTrainController.Schedule = GetRedlineSchedule(); Timer updateTimer = new Timer(); updateTimer.Tick += new EventHandler(Update); updateTimer.Interval = 40; updateTimer.Enabled = true; updateTimer.Start(); } m_myTimer = new Timer(); m_myTimer.Tick += new EventHandler(UpdateDisplay); m_myTimer.Interval = 500; m_myTimer.Enabled = true; m_myTimer.Start(); }
public void SpawnNewTrainTest_nullTransponder() { Simulator_Accessor target = new Simulator_Accessor(); TrackBlock block = new TrackBlock(); block.Transponder = null; string name = "train"; target.SpawnNewTrain(block, name); Assert.AreEqual(0, target.m_trainControllerList.Count); }
/// <summary> /// Event handler for the train clicked event /// </summary> /// <param name="train">Train that was clicked</param> private void OnTrainClicked(ITrain train) { m_log.LogInfo("Train was clicked"); m_selectedTrackBlock = null; m_selectedTrain = train; infoPanel.SetTrainInfo(train); commandPanel.ShowTrainCommands(); if (m_trainGUI != null) { m_trainGUI.Close(); } TrainController trainController = Simulator.GetSimulator().GetTrainController(train); if (trainController != null) { m_trainGUI = new TrainOperator(train, trainController); m_trainGUI.Show(); } CloseOpenPopups(); }
/// <summary> /// A train entered a new track block. Need to push an update to the track controllers /// </summary> /// <param name="previous">Previous track block</param> /// <param name="next">Next track block</param> private void OnTrainEnteredNewBlock(TrackBlock previous, TrackBlock next) { if (!m_ctcController.UpdateTrackControllers(previous, next)) { m_log.LogError("Failed to update track controllers"); } }
public void CloseTrackTest() { TrackController target = new TrackController(); // TODO: Initialize to an appropriate value string trackId = string.Empty; // TODO: Initialize to an appropriate value bool actual; actual = target.CloseTrack(trackId); Assert.IsFalse(actual); // can't close a track that isn't owned trackId = "akljkjcbao"; actual = target.CloseTrack(trackId); Assert.IsFalse(actual); // assert true if controller owns track and is able to close it TrackBlock block = new TrackBlock(); block.Name = trackId = "TestName"; target.AddTrackBlock(block); actual = target.CloseTrack(trackId); Assert.IsTrue(actual); // assert false if track is already closed actual = target.CloseTrack(trackId); Assert.IsFalse(actual); // shouldn't be able to close a track with a train on it block.Status.IsOpen = true; block.Status.TrainPresent = true; actual = target.CloseTrack(trackId); Assert.IsFalse(actual); }
/// <summary> /// Creates a new train on the track /// /// </summary> /// <param name="initialBlock">Starting block of the train</param> /// <param name="name">Name of the train</param> public void SpawnNewTrain(TrackBlock initialBlock, string name) { if (initialBlock != null) { if (initialBlock.HasTransponder) { string start = initialBlock.Transponder.StationName; if (m_startingDirections.ContainsKey(start)) { m_log.LogInfoFormat("Spawning new train \"{0}\" at start {1}", name, start); //Create the new train and train controller ITrain train = new Train.Train(name, initialBlock, m_startingDirections[start]); ITrainController trainController = new TrainController(train); m_trainList.Add(trainController); //Set the train schedule if (start == Constants.REDYARD) { m_log.LogInfoFormat("Setting schedule of {0} to red line", name); trainController.SetSchedule(CTCController.GetCTCController().GetRedlineSchedule()); } else if (start == Constants.GREENYARDOUT) { m_log.LogInfoFormat("Setting schedule of {0} to green line", name); trainController.SetSchedule(CTCController.GetCTCController().GetGreenlineSchedule()); } } } } }
/// <summary> /// Displays info about the train yard /// </summary> /// <param name="b">Track block</param> public void SetTrainYardInfo(TrackBlock b) { //Just show the block name SetInfo(b.Name, null); }
/// <summary> /// Gets the failure state string for the given track block /// </summary> /// /// <remarks>There are 8 different possible strings, so this chunk of code gets ugly</remarks> /// <param name="block">Track block</param> /// <returns>Failure state string</returns> private KeyValuePair<string, string> GetBlockFailureStateString(TrackBlock block) { KeyValuePair<string, string> pair; //Check all 8 possible states if (!block.Status.BrokenRail && !block.Status.CircuitFail && !block.Status.PowerFail) { pair = new KeyValuePair<string, string>("Failure State:", "none"); } else if (!block.Status.BrokenRail && !block.Status.CircuitFail && block.Status.PowerFail) { pair = new KeyValuePair<string, string>("Failure State:", "Power Failure"); } else if (!block.Status.BrokenRail && block.Status.CircuitFail && !block.Status.PowerFail) { pair = new KeyValuePair<string, string>("Failure State:", "Circuit Failure"); } else if (!block.Status.BrokenRail && block.Status.CircuitFail && block.Status.PowerFail) { pair = new KeyValuePair<string, string>("Failure State:", "Circuit & Power Failure"); } else if (block.Status.BrokenRail && !block.Status.CircuitFail && !block.Status.PowerFail) { pair = new KeyValuePair<string, string>("Failure State:", "Broken Rail"); } else if (block.Status.BrokenRail && !block.Status.CircuitFail && block.Status.PowerFail) { pair = new KeyValuePair<string, string>("Failure State:", "Broken Rail & Power Failure"); } else if (block.Status.BrokenRail && block.Status.CircuitFail && !block.Status.PowerFail) { pair = new KeyValuePair<string, string>("Failure State:", "Broken Rail & Circuit Failure"); } else if (block.Status.BrokenRail && block.Status.CircuitFail && block.Status.PowerFail) { pair = new KeyValuePair<string, string>("Failure State:", "Broken Rail, Circuit, & Power Failure"); } else { //Unreachable, but needed since pair is not nullable pair = new KeyValuePair<string, string>(); } return pair; }
/// <summary> /// Displays info about the train yard /// </summary> /// <param name="b">Track block</param> public void SetTrainYardInfo(TrackBlock b) { //Just show the block name m_displayedTrain = null; m_displayedBlock = b; SetInfo(b.Name, null); }
/// <summary> /// Simulates a track block circuit failure /// </summary> /// <param name="block">Track block to simulate on</param> /// <param name="failure">True to invoke failure or false to clear it</param> public void SimulateCircuitFailure(TrackBlock block, bool failure) { if (block != null) { if (block.Status != null) { m_log.LogInfoFormat("Setting circuit fail of block {0} to {1}", block.Name, failure); block.Status.CircuitFail = failure; } } }
// METHOD: SystemController //-------------------------------------------------------------------------------------- /// <summary> /// Manage the starting, stopping, coordination and scheduling of all system processes /// </summary> /// <param name="samplePeriod">Sample period</param> //-------------------------------------------------------------------------------------- private void SystemController(double samplePeriod) { // If this is the first time SystemController has been called, // the train is at the yard and needs to leave if (m_samplePeriod == 0.0) { LeaveStation(); } // Keep track of the time that has passed since leaving the last station m_timePassed += samplePeriod; // Set the sample period m_samplePeriod = samplePeriod; // Set the current track block m_currentBlock = m_currentState.CurrentBlock; // Determine the m_setPoint DetermineSetPoint(); // Check for faults FaultMonitor(); // Generate and issue a power command VelocityController(); // Control lights LightController(); // Control announcements and doors StationController(); }
/// <summary> /// Gets the failure state string for the given track block /// </summary> /// /// <remarks>There are 8 different possible strings, so this chunk of code gets ugly</remarks> /// <param name="block">Track block</param> /// <returns>Failure state string</returns> private string GetBlockFailureStateString(TrackBlock block) { string failString = string.Empty; //Check all 8 possible states if (!block.Status.BrokenRail && !block.Status.CircuitFail && !block.Status.PowerFail) { failString = "none"; } else if (!block.Status.BrokenRail && !block.Status.CircuitFail && block.Status.PowerFail) { failString = "Power Failure"; } else if (!block.Status.BrokenRail && block.Status.CircuitFail && !block.Status.PowerFail) { failString = "Circuit Failure"; } else if (!block.Status.BrokenRail && block.Status.CircuitFail && block.Status.PowerFail) { failString = "Circuit & Power Failure"; } else if (block.Status.BrokenRail && !block.Status.CircuitFail && !block.Status.PowerFail) { failString = "Broken Rail"; } else if (block.Status.BrokenRail && !block.Status.CircuitFail && block.Status.PowerFail) { failString = "Broken Rail & Power Failure"; } else if (block.Status.BrokenRail && block.Status.CircuitFail && !block.Status.PowerFail) { failString = "Broken Rail & Circuit Failure"; } else if (block.Status.BrokenRail && block.Status.CircuitFail && block.Status.PowerFail) { failString = "Broken Rail, Circuit, & Power Failure"; } return failString; }
/// <summary> /// Simulates a track broken rail /// </summary> /// <param name="block">Block to simulate on</param> /// <param name="failure">True to invoke failure or false to clear it</param> public void SimulateBrokenRail(TrackBlock block, bool failure) { if (block != null) { if (block.Status != null) { m_log.LogInfoFormat("Setting broken rail of block {0} to {1}", block.Name, failure); block.Status.BrokenRail = failure; } } }