How would you structure a simple simulation engine using Reactive Extensions? For example, suppose you had a class Motor with properties IsPowered and Speed. You want to react to IsPowered changing from false to true by having Speed ramp up from 0 to 1000 RPM by 10 RPM each second for 100 seconds.
Abstracting this a bit, suppose you want to provide a pluggable API where the class and its properties aren’t known in advance. Instead, the simulation engine gets an observable stream of property changes which is reacts to by creating additional property changes, many of which take place in increments over time leveraging Rx. What might such an API look like?
A simulation API would need a type to represent property changes and a simulation method that had an incoming and outgoing
IObservable<T>stream of changes. The implementation of the simulation engine would consist of rules that reacted to incoming property changes to update the output stream.The type would be like this (although a struct would probably be better than a class):
The simulation method would look like the following. To demonstrate multiple rules, this code presupposes a property
IsLightOnthat simply tracksIsPowered.