I am trying to create a generic simulation runner. Each simulation implements a variety of interfaces. Eventually, it will pick up simulation types via DLLs at run time, so I will have no way of knowing the types beforehand.
My Currrent Code:
public class SimulationRunner<TSpace, TCell>
where TSpace : I2DState<TCell>
where TCell : Cell
{
public TSpace InitialState { get; set; }
public IStepAlgorithm<TSpace,TCell> StepAlgorithm { get; set; }
public IDisplayStateAlgorithm<TSpace,TCell> DisplayStateAlgorithm { get; set; }
public int MaxStepCount { get; set; }
...
public void Run() {...}
public void Step() {...}
public void Stop() {...}
}
I want my UI class to store a generic instance of the simulation runner (Eg
public partial class UI : Window
{
SimulationRunner<TSpace,TCell> simulation;
...
}
so that i can assign different types of simulations to it. Eg
simulation = new SimulationRunner<2DSpace, SimpleCell>();
// do stuff
// start new simulation of different type
simulation = new SimulationRunner<3DSpace, ComplexCell>();
I want to have my UI controls wired to the simulation variable so i can do things like
private void mnuiSimulate_Click(object sender, RoutedEventArgs e)
{
if (simulation != null) simulation.RunSimulation();
}
and have it work regardless of what types are currently bound to TSpace and TCell.
Currently I am getting errors saying "Error 10 The type or namespace name 'U' could not be found (are you missing a using directive or an assembly reference?)", and the same again for T.
I have tried creating a controller class that wraps the SimulationRunner, but I still have the same problem in that I have to pass in the types of TSpace and TCell when I create it, so the problem just gets moved to a different class.
How can I store any type of simulation in a variable? How can I bind the controls to work on any type of simulation?
The solution is to take the non-generic methods and properties into a non-generic interface, so that the caller of the interface doesn't have to know which type parameters the class accepts:
public interface ISimulationRunner {
public int MaxStepCount { get; set; }
...
public void Run() {...}
public void Step() {...}
public void Stop() {...}
}
public class SimulationRunner<TSpace, TCell> : ISimulationRunner
where TSpace : I2DState<TCell>
where TCell : Cell
{
public TSpace InitialState { get; set; }
public IStepAlgorithm<TSpace,TCell> StepAlgorithm { get; set; }
public IDisplayStateAlgorithm<TSpace,TCell> DisplayStateAlgorithm { get; set; }
}
public partial class UI : Window
{
ISimulationRunner simulation = new SimulationRunner<2DSpace, SimpleCell>();
private void mnuiSimulate_Click(object sender, RoutedEventArgs e)
{
if (simulation != null) simulation.RunSimulation();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With