Last week I have finished Rx Road show. I have been giving presentations about Reactive Framework on four Polish .Net Users Groups: in Wroclaw, Lodz, Krakow and Chorzow. Thank you all for coming to my session and filling the evaluations, I hope you have learned something useful about Rx during the session. I have also good news for those of you who were interested in my presentation but for some reason couldn’t come, one of the presentation has been recorded and it is now available for download or watching live online here. Slides and all code samples can be download from here. Enjoy!
Tuesday, March 30, 2010
Monday, March 8, 2010
RxSandbox V1
[New version (2011.05.06)]
[RxSandbox downlaod has been upgraded to the newest version of Rx (Build 1.0.2677.0 08/27/2010).] download
[RxSandbox downlaod has been upgraded to the newest version of Rx (Build 1.0.2617.0 07/15/2010).] download
(Changes: solution converted to VS2010, Net 4.0; 56 operators, grouping operators on the tree control; zoom)
[RxSandbox downlaod has been upgraded to the newest version of Rx (Build 1.0.2441.0 04/14/2010).] download
[RxSandbox downlaod has been upgraded to the newest version of Rx (Build 1.0.2350.0 03/15/2010).] download
I am really happy to announce that the new version of RxSandbox has been just released. Last time I have been writing about the concept and the main ideas behind the project. That was just a small prototype, the proof of concept. This release is much more mature, the user can test more standard Rx operators and the API has been changed a little bit, however the concept stated the same.
These are the main new features:
- Marble diagrams
- New powerful API
- Extensibility mechanism
- Description of Rx the operators taken from Rx documentation
Lets start from the the end-user which is not necessary interested in writing any code. He just wants to experiment with Rx operators, check how they behave in specific scenarios. When we start RxSandbox application we will see the list of standard operators on the left hand side. Lets assume that we don’t know how Merge operator works. After double click on the Merge node the new tab will be displayed. We can find the short description of Merge operator taken from documentation provided by Rx installer, we can also find the code sample that can be tested interactively trough UI. Each input argument is presented as a simple automatically generated UI with one textbox where we can write input value, tree buttons and the history of the source. The output of the expression can be presented in two ways: by a simple list displaying the results (“Output” tab) or the marble diagrams drawn live during testing (‘Marble diagram – Live” tab). There is also one tab called “Marble diagram - Definition” showing the operator definition.
Now lets see what the developer can do with RxSandbox. The most important class in Rx API is the ExpressionDefinition. It holds all necessary information describing tested expression such as name, description and sample code.
When we look inside RxSandbox project we will see that the definitions of all standard operators are very similar, for example the Merge operator is defined like this:
public static ExpressionDefinition Merge() { Expression<Func<IObservable<string>, IObservable<string>, IObservable<string>, IObservable<string>>> expression = (a, b, c) => Observable.Merge(a, b, c); return ExpressionDefinition.Create(expression); }
This is all we need to write. Other things like operator’s name, description and the text of expression can be inferred from the Linq expression. Of course all these information can be set manually using appropriate Create method overload and ExpressionSettings class. All .Net types are supported as observable type, not only the System.String type like in this example. The only requirement is there must exist a TypeConverter for that type. Later in this post I’ll how implement TypeConverter for custom type and how to create ExpressionDefinition without using Linq expression but using the whole method with many statements. The second very important class in RxSandbox API is an ExpressionInstance class which is very useful in scenarios where we want to use some RxSandbox functionalities directly from code without any UI experience (for example during writing Unit Tests or recording marble diagram).
Expression<Func<IObservable<string>, IObservable<string>, IObservable<string>, IObservable<string>>> expression = (a, b, c) => Observable.Merge(a, b, c); ExpressionDefinition definition = ExpressionDefinition.Create(expression); using (var instance = ExpressionInstance.Create(definition)) { ExpressionInstance instance = ExpressionInstance.Create(definition); // using non-generic type 'ObservableSource' ObservableSource output1 = instance.Output; output1.ObservableStr.Subscribe(Console.WriteLine); // using generic type 'ObservableSource<T>' ObservableOutput<string> output2 = instance.Output as ObservableOutput<string>; output2.Observable.Subscribe(Console.WriteLine); instance["a"].OnNext("one"); // using non-generic type 'ObservableInput' (instance["a"] as ObservableInput<string>).OnNext("two"); // using generic type instance["b"].OnNext("tree"); instance["a"].OnCompleted(); instance["b"].OnCompleted(); instance["c"].OnCompleted(); }
When we add delay before sending each input signal we can very easily record sample marble diagram.
internal static class Extensions { internal static void OnNext2(this ObservableInput input, string value) { input.OnNext(value); Thread.Sleep(100); } internal static void OnError2(this ObservableInput input) { input.OnError(new Exception()); Thread.Sleep(100); } internal static void OnCompleted2(this ObservableInput input) { input.OnCompleted(); Thread.Sleep(100); } }
Marble diagrams are described in a very simple object model.
This object model can be serialized to Xml format, for instance the code above creates fallowing marble diagram:
<Diagram> <Input Name="a"> <Marble Value="one" Order="0" /> <Marble Value="two" Order="1" /> <Marble Kind="OnCompleted" Order="3" /> </Input> <Input Name="b"> <Marble Value="tree" Order="2" /> <Marble Kind="OnCompleted" Order="4" /> </Input> <Input Name="c"> <Marble Kind="OnCompleted" Order="5" /> </Input> <Output> <Marble Value="one" Order="0" /> <Marble Value="two" Order="1" /> <Marble Value="tree" Order="2" /> <Marble Kind="OnCompleted" Order="5" /> </Output> </Diagram>
As we can see single marble diagram have a very simple Xml representation and diagrams for all standard operators from RxSandbox are stored in Diagrams.xml file (this file path can be changed in the configuration files).
Short description added to all standard operators extracted from Rx documentation Xml file is a next new feature of current release. Of course not all tested reactive expression are related to one particular operator so the description can be set manually (ExpressionSettings.Description property). When we want write a very complicated Linq expression or the expression is passed as delegate type to the ExpressionDefinition.Create method and we also want to provide description from particular Rx operator at the same time, we can do it indicating operator thought MethodInfo type (ExpressionSettings.Operator property).
The last but not least new feature is the extensibility mechanism. When we want to write our custom reactive expression without changing anything inside RxSadbox project we can do it by implementing IExpressionProvider interface directly or by inheriting from an abstract class ExpressionAttributeBasedProvider and setting our assembly name in the configuration file (ExtensionsAssembly element). During startup process RxSandbox loads that assembly, analyzes it and finds all expression providers. Few weeks ago I have been writing about Incremental Find implemented using Rx, lets see how such a query can tested via RxSandbox.
[AttributeUsage(AttributeTargets.Method,AllowMultiple = false, Inherited = true)] public class ExpressionAttribute : Attribute { } public interface IExpressionProvider { IEnumerable<ExpressionDefinition> GetExpressions(); } public abstract class ExpressionAttributeBasedProvider : IExpressionProvider { public IEnumerable<ExpressionDefinition> GetExpressions() { var q = from m in this.GetType().GetMethods() let attr = Attribute.GetCustomAttribute(m, typeof(ExpressionAttribute)) as ExpressionAttribute where attr != null select m.Invoke(null, null) as ExpressionDefinition; return q.ToList(); } } public class CustomExpressions : ExpressionAttributeBasedProvider { [Expression] public static ExpressionDefinition IncrementalSearch() { Func<IObservable<string>, IObservable<Person>, IObservable<Person>> expression = (codeChanged, webServiceCall) => { var q = from code in codeChanged from x in Observable.Return(new Unit()) .Delay(TimeSpan.FromSeconds(4)).TakeUntil(codeChanged) from result in webServiceCall.TakeUntil(codeChanged) select result; return q; }; return ExpressionDefinition.Create(expression, new ExpressionSettings { Name = "Incremental find", Description = @"Send the code of the person you are looking for, " + "after four seconds (if you don't send new code again) web service " + "will be called. The result won't be returned if new code is provided " + "in the meantime.", }); } } [TypeConverter(typeof(PersonConverter))] public class Person { public string Code { get; set; } public string Name { get; set; } } public class PersonConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof (string)) return true; return base.CanConvertFrom(context, sourceType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string[] v = ((string)value).Split(new[] { ',' }); return new Person {Code = v[0], Name = v[1]}; } return base.ConvertFrom(context, culture, value); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof (string)) { var person = value as Person; return person.Code + "," + person.Name; } return base.ConvertTo(context, culture, value, destinationType); } }
So that’s it for this release of RxSandbox. I encourage you to play with it a little bit and let me know what you think.
And that’s not the end of the project. In upcoming releases I plan to:
- create Silverlight version with ability to write reactive expression directly in the web browser
- add integration with MEF
- add better look and feel experience
Here you can download sources and binaries