1

I have two classes. One is called Component and the second one is called PipeLine.

*Component Class*
    string id;
    int flow;
    List<PipeLine> ConnectedPipeLines;
    void ChangeFlow(int flow);
    void CalculateFlow();


*PipeLine Class*
    int flow;
    Component startComponent;
    Component endComponent;
    void RecalculateFlow();

A component can have multiple pipelines but a pipeline can only connect to two components. One at the start and one at the end.

The pipeline gets its flow from the startComponent, and the endComponent gets its flow from the pipeline.Flow.

What the lists look like

public Pipeline(Component startComponent, Component endComponent);

When a pipeline is created the pipeline gets added to the list of ConnectedPipelines for the start and end component.

So ConnectedPipeLines.Add(p); gets executed.

I have a function that can change the flow of any component, and I would like to go through every component and execute component.CalculateFlow() and with that then I can execute pipeline.CalculateFlow() to recalculate the flow of the pipeline, because the endComponent gets its flow from the pipelines flow.

This is the way I am doing it, but if I debug it to find out which two components are being connected in the current pipe that I am looping through it it only recalculates the first level (Component A1 and AB) but not Component A10 and Component A11. Where am I going wrong?

foreach(var pipe in pump.Output.StartComponent.ConnectedPipeLines) {
 pipe.EndComponent.CalculateFlow();

 foreach(var p in pipe.EndComponent.ConnectedPipeLines) {
  p.EndComponent.CalculateFlow();

  foreach(var item in p.EndComponent.ConnectedPipeLines) {
   item.EndComponent.CalculateFlow()
  }
 }
}
2
  • Hmmm it's hard to recreate your whole flow in short time but first thought: you shoud have used recurrence here Commented Jan 27, 2017 at 17:12
  • It's not clear what you are trying to achieve here. Are trying to figure out how much flow is coming to component A based on the flows between A10 and A1 etc? Or are you trying to figure out how much flow is coming to A10, A11 etc based on how much A is outputting? Are the components each adding flow in addition to what's incoming from other? Commented Jan 27, 2017 at 18:09

1 Answer 1

1

You could descend down the flow recursively. Something like this:

calculateDownstreamFlow(pump.Output.StartComponent.ConnectedPipeLines);

function void calculateDownstreamFlow(List<Pipeline> pipes) {
  foreach(var pipe in pipes) {
    pipe.EndComponent.CalculateFlow();
    calculateDownstreamFlow(pipe.EndComponent.ConnectedPipeLines);
   }
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Component doesn't have the StartComponent variable, the PipeLine does. It's pipe.StartComponent.ConnectedPipeLines. Appreciate your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.