/// <summary> /// The method that makes a connection between two components. /// Dimitar /// </summary> /// <param name="connectTo">The second component that is to be connected.</param> /// <param name="connectFrom">The first component that is to be connected.</param> public void Connect(Component connectTo, Component connectFrom, string inputPosition, string outputPosition) { Pipeline pipe = new Pipeline(connectTo, connectFrom); if ((connectTo.ComponentType == ComponentType.Pump) || (connectFrom.ComponentType == ComponentType.Sink)) { pipe = new Pipeline(connectFrom, connectTo); } var connectionsFound = this.Connections .Where(x => x.InputElement.GetId() == connectTo.GetId()) .Where(x => x.OutputElement.GetId() == connectFrom.GetId()) .ToList(); if (connectionsFound.Count != 0) { if (connectTo.ComponentType == ComponentType.Merger) { if (connectionsFound.Count() > 1) { return; } if ((inputPosition == "down") && (((Merger)connectTo).LowerConnectedComponent != 0)) { return; } if ((inputPosition == "up") && (((Merger)connectTo).UpperConnectedComponent != 0)) { return; } } if ((connectFrom.ComponentType == ComponentType.Splitter) || (connectFrom.ComponentType == ComponentType.AdjustableSplitter)) { if (connectionsFound.Count() > 1) { return; } if ((outputPosition == "down") && (((MiddleComponent)connectFrom).LowerConnectedComponent != 0)) { return; } if ((outputPosition == "up") && (((MiddleComponent)connectFrom).UpperConnectedComponent != 0)) { return; } } else { return; } } this.Connections.Add(pipe); if ((inputPosition != null) && (inputPosition != String.Empty)) { if (inputPosition == "up") { ((MiddleComponent)connectTo).ConnectUpper(connectFrom); } else { ((MiddleComponent)connectTo).ConnectLower(connectFrom); } } if ((outputPosition != null) && (outputPosition != String.Empty)) { if (outputPosition == "up") { ((MiddleComponent)connectFrom).ConnectUpper(connectTo); } else { ((MiddleComponent)connectFrom).ConnectLower(connectTo); } } if (((connectTo.ComponentType == ComponentType.AdjustableSplitter) && ((inputPosition == null) || (inputPosition == String.Empty))) || (connectTo.ComponentType == ComponentType.Splitter) && ((inputPosition == null) || (inputPosition == String.Empty))) { ((MiddleComponent)connectTo).ConnectOther(connectFrom); } }
/// <summary> /// Dimitar /// </summary> /// <param name="c"></param> private void MakeFlowFromComponent(Component c) { switch (c.ComponentType) { case ComponentType.AdjustableSplitter: double coming = 0; var comingFound = this.Connections .Where(x => x.InputElement.GetId() == c.GetId()) .Select(x => x.CurrentFlow); if (comingFound.Any() == true) { coming = comingFound.First(); } if (((AdjustableSplitter)c).UpperConnectedComponent != 0) { var connectionsAds = this.Connections .Where(x => x.InputElement.GetId() == ((AdjustableSplitter)c).UpperConnectedComponent) .Select(x => x); Pipeline connectionAds = null; if (connectionsAds.Any() == true) { connectionAds = connectionsAds.First(); } if (connectionAds != null) { double flowToSet = (((AdjustableSplitter)c).DivisionRate * coming) / 100; connectionAds.SetFlow(flowToSet); Component nextComponent = connectionAds.InputElement; this.MakeFlowFromComponent(nextComponent); } } if (((AdjustableSplitter)c).LowerConnectedComponent != 0) { var connectionsAdsLower = this.Connections .Where(x => x.InputElement.GetId() == ((AdjustableSplitter)c).LowerConnectedComponent) .Select(x => x); Pipeline connectionAdsLower = null; if (connectionsAdsLower.Any() == true) { connectionAdsLower = connectionsAdsLower.First(); } if (connectionAdsLower != null) { double flowToSet2 = ((100 - ((AdjustableSplitter)c).DivisionRate) * coming) / 100; connectionAdsLower.SetFlow(flowToSet2); Component nextComponent = connectionAdsLower.InputElement; this.MakeFlowFromComponent(nextComponent); } } break; case ComponentType.Merger: double comingUpper = 0; double comingLower = 0; var comingUpperfound = this.Connections .Where(x => x.OutputElement.GetId() == ((Merger)c).UpperConnectedComponent) .Select(x => x.CurrentFlow); if (comingUpperfound.Any() == true) { comingUpper = comingUpperfound.First(); } var comingLowerfound = this.Connections .Where(x => x.OutputElement.GetId() == ((Merger)c).LowerConnectedComponent) .Select(x => x.CurrentFlow); if (comingLowerfound.Any() == true) { comingLower = comingLowerfound.First(); } Pipeline connectionFoundMerger = null; var connectionsFoundMerger = this.Connections .Where(x => x.OutputElement.GetId() == c.GetId()) .Select(x => x); if (connectionsFoundMerger.Any() == true) { connectionFoundMerger = connectionsFoundMerger.First(); } if (connectionFoundMerger != null) { connectionFoundMerger.SetFlow(comingLower + comingUpper); Component nextComponent = connectionFoundMerger.InputElement; this.MakeFlowFromComponent(nextComponent); } break; case ComponentType.Pump: Pipeline connectionFound = null; var connectionsFound = this.Connections .Where(x => x.OutputElement.GetId() == c.GetId()) .Select(x => x); if (connectionsFound.Any() == true) { connectionFound = connectionsFound.First(); } if (connectionFound != null) { connectionFound.SetFlow(((Pump)c).Output); Component nextComponent = connectionFound.InputElement; this.MakeFlowFromComponent(nextComponent); } break; case ComponentType.Sink: Pipeline connectionFound2 = null; var connectionsFound2 = this.Connections .Where(x => x.InputElement.GetId() == c.GetId()) .Select(x => x); if (connectionsFound2.Any()) { connectionFound2 = connectionsFound2.First(); ((Sink)c).SetInput(connectionFound2.CurrentFlow); } else { ((Sink)c).SetInput(0); } break; case ComponentType.Splitter: double comingToSpliter = this.Connections .Where(x => x.InputElement.GetId() == c.GetId()) .Select(x => x.CurrentFlow).First(); if (((Splitter)c).UpperConnectedComponent != 0) { var connectionsAds = this.Connections .Where(x => x.InputElement.GetId() == ((Splitter)c).UpperConnectedComponent) .Select(x => x); Pipeline connectionAds = null; if (connectionsAds.Any() == true) { connectionAds = connectionsAds.First(); } if (connectionAds != null) { double flowToSet = comingToSpliter / 2; connectionAds.SetFlow(flowToSet); Component nextComponent = connectionAds.InputElement; this.MakeFlowFromComponent(nextComponent); } } if (((Splitter)c).LowerConnectedComponent != 0) { var connectionsAdsLower = this.Connections .Where(x => x.InputElement.GetId() == ((Splitter)c).LowerConnectedComponent) .Select(x => x); Pipeline connectionAdsLower = null; if (connectionsAdsLower.Any() == true) { connectionAdsLower = connectionsAdsLower.First(); } if (connectionAdsLower != null) { double flowToSet2 = comingToSpliter / 2; connectionAdsLower.SetFlow(flowToSet2); Component nextComponent = connectionAdsLower.InputElement; this.MakeFlowFromComponent(nextComponent); } } break; default: throw new Exception("not a valid component type"); } }