Sunday, September 12, 2010

Marble Diagrams and Rx

Today I am going to discuss how to draw Marble Diagrams. I just came across with it in Reactive Framework. In Channel 9 videos, Jeffrey Van Gogh shows how to get started with Rx. Ever since I look deep into it, I find it very interesting, and perhaps a new way of thinking or understanding for Asynchronous programming structure. Reactive Framework introduces a few operators, which you might apply to an Observable to get the resultant observable. The way to represent each of these attributes can be done using Marble Diagrams. 


Before you start dealing with Reactive Framework, it is good to start with IObservable and IObserver. I have already introduced with these two interfaces which are the building block for the Reactive Framework. You can read these articles before going through with Marble Diagrams.




How to create Marble Diagram. 


Marble diagram is basically a way to represent the Rx operations. You may think a marble diagram to be a pictorial representation of different operators that is defined with Reactive Framework. Lets look how to create a Marble Diagram : 








So in the sample image you can see we measure Time in X-Axis while operations on Y-axis. Say for instance, you have an Observable which is represented by the first horizontal line. The small circles represents OnNext calls of IObserver. If you know already read about Observable and Observer, you might already know that each Observer has OnNext method, which gets invoked whenever the observer changes its state.  Hence, in our case the observer invokes a function f() to get to the new State.  The | represents the OnComplete for the Observer. So after OnComplete, the Observer will stop observing states.





We represents OnError using X in a marble diagram. In case of an Observable, OnError and OnComplete produces the end  point for the observer. So Here after executing the two OnNext if the Observer encounters with an Exception, OnError gets invoked and the Observer will terminate. 


So on the first image, we execute the function f() to get the new state with the Buttom Line. Now on, I am going to create a few Marble Diagrams, to make your understanding clear. 

SelectMany



SelectMany is very easy to explain. Say you have two or more Observers. In this case, SelectMany will eventually invoke all the OnNext of each observer. 




 So if you have 3 observers, the SelectMany will produce Observer which aggregates all of them. 





int[] collection = { 2, 5, 5, 6, 2, 4 };
int[] collection2 = { 1, 4, 2, 1, 5, 6 };
var ob1 = Observable.ToObservable(collection);
var ob2 = Observable.ToObservable(collection2);


var ob3 = ob1.SelectMany(ob2);

//var ob3 = ob1.Select(r => r + 2);
var disp = ob3.Subscribe(r => Console.WriteLine("OnNext : {0}", r.ToString()), 
                            r => Console.WriteLine("Error : {0}", r.ToString()), 
                            () => Console.WriteLine("Completed"));
            
disp.Dispose();
So the Observer will select from each of the Observer and get you the output.

In case of an error, the final Observer will stop when first error of any Observer is encountered.

SkipUntil / SkipWhile

SkipWhile and SkipUntil works in opposite. Say for instance, you have two Observable. SkipUntil gives you OnNext for each of them until the OnNext from the other observer is received. On the contrary, SkipWhile will produce OnNext for the Observer while the OnNext from the second observer is Received. When the second observer is Complete or gives an error, the final observer will terminate in case of SkipWhile.






So in the above diagrams, you can see While will bypass all the values until the second observer receives an entry.


Further Reference

To learn the operators of Marble Diagrams, you can see the latest Videos on Channel 9. Here is a few more Operators :
I hope this will help you to understand the Marble Diagrams, and hope these will describe you some more operators of Rx Framework.

3 comments:

  1. Thank you for describing about Marble Diagrams. I have seen few videos on Channel 9 and this post cleared me more

    ReplyDelete
  2. This is such a great resource that you are providing and you give it away for free. I enjoy seeing websites that understand the value of providing a prime resource for free. I truly loved reading your post. Thanks!

    ReplyDelete

Please make sure that the question you ask is somehow related to the post you choose. Otherwise you post your general question in Forum section.

Author's new book

Abhishek authored one of the best selling book of .NET. It covers ASP.NET, WPF, Windows 8, Threading, Memory Management, Internals, Visual Studio, HTML5, JQuery and many more...
Grab it now !!!