<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Orbifold</title>
	<atom:link href="http://visualizationtools.net/default/feed/" rel="self" type="application/rss+xml" />
	<link>http://visualizationtools.net/default</link>
	<description>Think. Visualize. Understand.</description>
	<lastBuildDate>Sun, 13 May 2012 16:47:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Data Visualization using d3.js and Telerik&#8217;s KendoUI</title>
		<link>http://visualizationtools.net/default/data-visualization-html5/</link>
		<comments>http://visualizationtools.net/default/data-visualization-html5/#comments</comments>
		<pubDate>Sun, 13 May 2012 16:45:55 +0000</pubDate>
		<dc:creator>Francois Vanderseypen</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Data Visualization]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[data-visualization]]></category>

		<guid isPermaLink="false">http://visualizationtools.net/default/?p=5739</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p><a href="http://orbifold.net/html5"><img class="size-medium wp-image-5740 alignleft" title="DataViz HTML5" src="http://visualizationtools.net/default/wp-content/uploads/2012/05/DataVizHTML5-300x184.png" alt="" width="300" height="184" /></a></p>
<p>Head over to <a title="Data visualization " href="http://orbifold.net/html5" target="_blank">this page to see my latest mashup</a> (aka data visualization experiment) with <a title="d3.js" href="http://d3js.org/" target="_blank">d3.js</a> and <a title="Telerik KendoUI" href="http://www.kendoui.com" target="_blank">KendoUI</a>. A lot of fun really to mix &#8216;nd match animations, effects and visual elements into something which should represent an HMI dashboard (human-machine interface) or a dashboard of real-time data sources. No efforts was made to represent anything in particular since my focus was on trying out something on an artistic level rather than on a business level. Still, now that I have a good understanding of the technicalities involved I estimate it no more (or less) difficult than Silverlight or WPF to develop a true business dashboard (Sharepoint, WebSphere or whatever your local flavor is).</p>
<p>Much appreciation goes to the guys behind KendoUI and Mike Bostock&#8217;s amazing d3 library. Sheer pleasure to develop with these libs.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://visualizationtools.net/default/data-visualization-html5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RadDiagram Q2.2012 Roadmap</title>
		<link>http://visualizationtools.net/default/raddiagramroadmapq2/</link>
		<comments>http://visualizationtools.net/default/raddiagramroadmapq2/#comments</comments>
		<pubDate>Tue, 20 Mar 2012 19:27:11 +0000</pubDate>
		<dc:creator>Francois Vanderseypen</dc:creator>
				<category><![CDATA[Diagramming]]></category>
		<category><![CDATA[RadDiagram]]></category>

		<guid isPermaLink="false">http://visualizationtools.net/default/?p=5729</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>It was just a month ago when we released the first version of the <a href="http://blogs.telerik.com/miromiroslavov/posts/12-02-21/raddiagram-architecture.aspx">RadDiagram control/framework</a> and now is time to share with you some plans about the future. We are very committed to the RadDiagram success and becoming one of the top diagramming frameworks for WPF and Silverlight out there. We will be pushing new features and possibilities as much as possible as well as trying to reach different platforms.</p>
<h2>Official for Q2</h2>
<p>As you already know it is currently in<strong> beta state</strong> (even we think it is more than a beta) but with the upcoming <strong>Q2.2012 release the RadDiagram will become official control</strong>. That is not the only major thing that is coming; we are already working on lots of new cool features and examples. Let&#8217;s just name a few&#8230;</p>
<h2>New Features</h2>
<ul>
<li>Shapes
<ul>
<li>Hierarchical <strong>groups </strong>- you&#8217;ll be able to group shapes together and manipulate them accordingly.</li>
<li><strong>Custom connectors</strong> - specify exact point on the shape where a connection could be pinned.</li>
</ul>
</li>
<li>Connections
<ul>
<li><strong>Bezier </strong>curves &#8211; read more about Bezier <a href="http://en.wikipedia.org/wiki/B%C3%A9zier_curve">here</a>.</li>
<li><strong>Polyline </strong>improvements</li>
<li><strong>Routing </strong>improvements</li>
</ul>
</li>
<li>Diagram export
<ul>
<li><strong>Printing</strong></li>
<li><strong>Export to Image</strong></li>
</ul>
</li>
<li>Diagram Canvas improvements
<ul>
<li><strong>Rulers</strong> - we&#8217;ll add vertical and horizontal rulers for easier positioning of the shapes.</li>
<li><strong>Thumbnail </strong>(mini-map) view &#8211; this is a very cool feature. The control will visualize the whole diagram &#8220;world&#8221; and what part of it you&#8217;re seeing.</li>
<li><strong>Auto-layout</strong> and auto-aligning</li>
<li>Improved handling of <strong>dropped content</strong></li>
</ul>
</li>
<li><strong>Tree layout </strong>and variations &#8211; this is something huge. You&#8217;ll be able to easily choose between different types of tree layouts like mindmap, tip-over-tree or radial trees and forests.</li>
<li>New <strong>Extension assembly</strong> &#8211; It will contain the Toolbox control and some other cool stuff to help you create easily diagramming applications. It will be prety much like the current Features assembly that is part of the demos distribution.</li>
</ul>
<h2>New Samples</h2>
<ul>
<li><strong>Mindmap </strong>example &#8211; we are creating a <a href="http://en.wikipedia.org/wiki/Mind_map">mindmap </a>example that will demonstrate the power of the RadDiagram framework.</li>
</ul>
<h2>Connection Routing</h2>
<p>Connection routing is an algorithm or a set of algorithms that should &#8220;trace&#8221; the route of link/connection, that takes into account the other nodes/shapes and some other heuristics.</p>
<p><a href="http://visualizationtools.net/default/wp-content/uploads/2012/03/routing.png"><img class="alignnone size-full wp-image-5732" title="routing" src="http://visualizationtools.net/default/wp-content/uploads/2012/03/routing.png" alt="" width="600" height="332" /></a></p>
<h2>Tree Layouts</h2>
<p>Have a look at few screenshots of the RadDiagram showing the new tree layouts.</p>
<p><a href="http://visualizationtools.net/default/wp-content/uploads/2012/03/circle.png"><img class="alignnone size-full wp-image-5730" title="circle" src="http://visualizationtools.net/default/wp-content/uploads/2012/03/circle.png" alt="" width="600" height="458" /></a></p>
<p><a href="http://visualizationtools.net/default/wp-content/uploads/2012/03/treelayout.png"><img class="alignnone size-full wp-image-5734" title="treelayout" src="http://visualizationtools.net/default/wp-content/uploads/2012/03/treelayout.png" alt="" width="600" height="306" /></a></p>
<p><a href="http://visualizationtools.net/default/wp-content/uploads/2012/03/mindmap.png"><img class="alignnone size-full wp-image-5731" title="mindmap" src="http://visualizationtools.net/default/wp-content/uploads/2012/03/mindmap.png" alt="" width="600" height="431" /></a></p>
<p><a href="http://visualizationtools.net/default/wp-content/uploads/2012/03/tipovertree.png"><img class="alignnone size-full wp-image-5733" title="tipovertree" src="http://visualizationtools.net/default/wp-content/uploads/2012/03/tipovertree.png" alt="" width="240" height="600" /></a></p>
<p>That is our current plan, but stay tuned for more information on the RadDiagram. Your feedback and ideas are more than welcomed.</p>
]]></content:encoded>
			<wfw:commentRss>http://visualizationtools.net/default/raddiagramroadmapq2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A financial dashboard designer based on RadDiagram</title>
		<link>http://visualizationtools.net/default/financial-dashboard/</link>
		<comments>http://visualizationtools.net/default/financial-dashboard/#comments</comments>
		<pubDate>Wed, 07 Mar 2012 13:22:57 +0000</pubDate>
		<dc:creator>Francois Vanderseypen</dc:creator>
				<category><![CDATA[Diagramming]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[RadDiagram]]></category>

		<guid isPermaLink="false">http://visualizationtools.net/default/?p=5699</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[
<p>co-authored with <a href="https://twitter.com/#!/mmiroslavov" target="_blank">Miroslav Miroslavov</a></p>
<p><strong>&raquo;</strong> <a href="/Downloads/Stuff/DashboardDesigner.zip">Full source code of the demo application here</a>. <a href="http://www.telerik.com/products/wpf/overview.aspx" target="_blank">You can download a trial or purchase RadDiagram here.</a></p>
<p>The <strong><a href="http://www.telerik.com/help/wpf/raddiagram-overview.html" title="RadDiagram" target="_blank">new RadDiagram framework</a></strong> brings, together with a host of new exciting things in Telerik&#8217;s 2012 Q1 release, a new world of possibilities to the WPF and Silverlight community. This article attempts to show you that Telerik&#8217;s RadDiagram is <strong>easy-to-use</strong> and that it differs from other Telerik controls in that it more closely <strong>represents a framework than a control</strong>. It’s an invitation to augment and customize the RadDiagram toolset in function of your application and business context. That said, we have to tell you straight away that RadDiagram, as released with Q1, is just the beginning. We have at this point put down the foundations for lots of exciting things to come and you can expect to see this RadDiagram branch off in various directions in coming releases. Great care and much energy were put in making it a flexible, robust, and broad framework for future developments. We have tried to make your (and our) task of <strong>customizing things easy while retaining the possibility to make the complex possible</strong>.&nbsp; </p>
<p>The dashboard designer described in this article aims to highlight various development tasks related to RadDiagram and how to integrate it into a real-world LOB application. It also tries to underline the fact that the diagramming world is a very broad subject on many levels. In its most basic form a diagram (graph, network) is just a set of shapes and lines connecting these shapes, but diagramming frameworks are also used for a wild spectrum of other tasks:</p>
<ul>
<li>For so-called&nbsp; HMI (<a title="Wikipedia on HMI" href="http://en.wikipedia.org/wiki/Human-machine_interface" target="_blank">human-machine interface</a>) diagrams where the shapes contain data from sensors and other real-time data providers</li>
<li>In geographic maps <a href="http://en.wikipedia.org/wiki/Automatic_label_placement" title="Wikipedia on automatic label placement">to position labels</a> (using appropriate layout algorithms) and to calculate shortest paths or other extremum, graph-based things</li>
<li>To design <a href="http://en.wikipedia.org/wiki/Workflow_application" target="_blank" title="Wikipedia on workflows">workflows</a> in which case shapes and connections are tightly bound to an underlying API</li>
<li>To simulate processes and represent data logic (so-called <a href="http://en.wikipedia.org/wiki/System_dynamics" target="_blank" title="Wikipedia on system dynamics">system dynamics</a>) in which case the graph effectively represents a flow of data and logical switches. The typical example being the diagrams used in rendering applications where texture and material properties are modeled through graphs.</li>
<li>For <a href="http://en.wikipedia.org/wiki/Data_visualization" title="Wikipedia on data visualization">data visualizations</a> and data analysis; social networks and IP networks, for example</li>
</ul>
<table border="0" cellspacing="0" cellpadding="0" style="border: none;  margin-left: 11.35pt; border-collapse: collapse; border-image: initial;">
<tbody>
<tr>
<td valign="top">
			<img alt="Typical dashboad" title="Typical dashboad" src="/default/wp-content/uploads/2012/03/Picture1.png" /> </td>
</tr>
<tr>
<td valign="top">
			<em>[Picture above] Typical dashboard in our sample application representing an overview of stock-related data coming from Yahoo Finance.</em></td>
</tr>
</tbody>
</table>
<p>Yet another context wherein diagrams come in handy is when you need a flexible surface on which you need to drop elements to design a document or data dashboard. While graphs are intrinsically related to links and connections, nothing prevents you from using a diagram framework without them and thus reduce it to a layout or design engine (a form designer anyone?). Our financial dashboard designer uses the RadDiagram surface in both ways;<strong> to design data dashboards</strong> (no connections here) and <strong>to represent the hierarchical structure</strong> of these dashboards (relationship graph).</p>
<table border="0" cellspacing="0" cellpadding="0" style="border: none;  margin-left: 12.5pt; border-collapse: collapse; border-image: initial;">
<tbody>
<tr>
<td valign="top">
<p>
			<img alt="Screen flow" title="Screen flow" src="/default/wp-content/uploads/2012/03/Picture2.png" /></p>
</td>
</tr>
<tr>
<td valign="top">
           <em>[Picture above] The very same RadDiagram surface which presents a digital dashboard is re-used to display the relationships contained within different dashboards by inspecting the hyperlinks between the screens.</em></td>
</tr>
</tbody>
</table>
<p>How does the application gets its relational structure? Put simply, the hyperlinks you add on a dashboard can be internal; you can create a link which jumps to another dashboard page much like hyperlinks in a web site.</p>
<table border="0" cellspacing="0" cellpadding="0" style="border: none;  margin-left: 11.35pt; border-collapse: collapse; border-image: initial;">
<tbody>
<tr>
<td valign="top">
<p>
			<img alt="Sample internal links" title="Sample internal link" src="/default/wp-content/uploads/2012/03/Picture3.png"  /></p>
</td>
</tr>
<tr>
<td valign="top">
            <em>[Picture above] Example of an internal link in a dashboard page.</em></td>
</tr>
</table>
<table border="0" cellspacing="0" cellpadding="0" style="border: none;  margin-left: 11.35pt; border-collapse: collapse; border-image: initial;">
<tbody>
<tr>
<td valign="top">
<p>
			<img alt="Text editor" title="Text editor" src="/default/wp-content/uploads/2012/03/Picture4.png"  /></p>
</td>
<td valign="top">
            <em>[Picture left]The link editor (which pops up on double-clicking a link) allows you to add an internal link to another dashboard page or to an arbitrary (external) address.</em></td>
</tr>
</tbody>
</table>
<p>The important bit here is the fact that across the application <strong>there is only one RadDiagram instance</strong> used in multiple ways and that all the dynamics occur through the (de)serialization. In fact, the generic flow in the application is as follows:</p>
<ul>
<li>At any moment there is only one active page which corresponds to a diagram. You can remove or add pages as you please, but only one is active at a time, much like a TabControl tab-page in fact. </li>
<li>From the toolbox data elements can be drag-dropped onto the diagram (dashboard) and real-time data is fetched from the Yahoo Finance service (you need to specify a stock symbol, of course).</li>
<li>The elements can be customized, if needed, by double-clicking them.</li>
<li>When a page is saved it&#8217;s being serialized to the isolated storage space and the hyperlinks are analyzed thus creating a graph (an hypergraph in fact) of relations between pages/dashboards.</li>
<li>Whenever a page is loaded from the storage the corresponding diagram XML is deserialized.</li>
</ul>
<table border="0" cellspacing="0" cellpadding="0" style="border: none;  margin-left: 11.35pt; border-collapse: collapse; border-image: initial;">
<tbody>
<tr>
<td valign="top">
<p><a href="http://visualizationtools.net/default/wp-content/uploads/2012/03/Picture5.png"><img src="http://visualizationtools.net/default/wp-content/uploads/2012/03/Picture5-590x300.png" alt="A dashboard page in edit mode" title="A dashboard page in edit mode" width="590" height="300" class="alignnone size-slider_small wp-image-5707" /></a></p>
</td>
</tr>
<tr>
<td valign="top">
<p><em>[Picture above] A dashboard page in design mode shows some additional elements in the UI: the toolbox and the toolbar. Through these you can achieve a very diverse set of data pages. In addition, it’s really easy to add your own toolbox items.</em></p>
</td>
</tr>
</tbody>
</table>
<p>This flow demonstrates the following aspects of RadDiagram:</p>
<ul>
<li>The flexible serialization pipeline you can plug into to include your application-specific data.</li>
<li>The ease with which you can customize the RadDiagram toolbox to incorporate your own bits.</li>
<li>The way you can use the many switches and properties of the diagramming surface to model a particular behavior, enabling/disabling e.g. connections, resizing, rotation, and so on.</li>
<li>The speedy loading and unloading of diagrams, which effectively enables a multi-view paradigm, bypassing the need for a TabControl </li>
<li>The way you can use diagramming shapes as containers for other Telerik controls, for your own data or to act as recipients for real-time data sources</li>
</ul>
<p>Now that you have an overview of what the demo application does and its various features let’s dig a bit more into the technical details.</p>
<h1>A note on the Yahoo Finance API</h1>
<p>The dashboard designer uses realtime financial information from the <a href="http://finance.yahoo.com/">Yahoo Finance</a> API:</p>
<ul>
<li>You can drag-drop the &#8216;company profile&#8217; shape onto the diagram surface and it will fetch the company profile for the stock symbol of your choice. </li>
<li>General financial information, timeline graph and other data can be fetched in the same way. Each type of information is presented in a different shape (format) and demonstrates how you can combine Telerik&#8217;s controls into the RadDiagram framework</li>
<li>RSS news feeds can be appended</li>
</ul>
<p>The code to access the Yahoo Finance API is quite straightforward and one can, of course, reuse these controls and the underlying code for other data sources. </p>
<table border="0" cellspacing="0" cellpadding="0" style="border: none;  margin-left: 11.35pt; border-collapse: collapse; border-image: initial;">
<tbody>
<tr>
<td valign="center">
<p>			<img alt="Sample Yahoo Finance output in a custom shape" title="Sample Yahoo output in a custom shape" src="/default/wp-content/uploads/2012/03/Picture6.png" sfref="[Libraries]c28c8818-a235-412a-bf52-f2042bd33a9d" />
            </td>
<td valign="center">
            <em>[Picture left] Sample output of the Yahoo API which lets you query the stock details of an arbitrary symbol (here show is ‘MSFT’).</em>
            </td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<h1>Creating custom shapes with RadDiagram</h1>
<p>The RadDiagram framework comes with a standard set of flowcharting shapes (geometry shapes) but <strong>this shouldn&#8217;t prevent you from enlarging it with your own set</strong>. In fact, diagramming is such a vast domain and its integration within the business context is so tight that any diagramming framework would fail to deliver a complete set of shapes. Rather than trying to offer lots of <strong>shapes RadDiagram offers you a flexible and straightforward route to designing custom shapes</strong> and embedding (Telerik or third-party) controls.</p>
<table border="0" cellspacing="0" cellpadding="0" style="border: none;  margin-left: 11.35pt; border-collapse: collapse; border-image: initial;">
<tbody>
<tr>
<td valign="top">
<p>
			<img alt="RadGauge inside a RadDiagram shape" title="RadGauge inside a RadDiagram shape" src="/default/wp-content/uploads/2012/03/Picture7.png" /></p>
</td>
<td valign="top">
<p><em>[Picture left] Sample custom RadDiagram shape hosting a RadChart control,which displays the latest trade price as well as the procentual change.</em></p>
</td>
</tr>
</tbody>
</table>
<p>The generic recipe for creating a custom shape goes as follows:</p>
<ul>
<li>Inherit from a base class, either RadDiagramShapeBase or RadDiagramShape. The latter one contains a Geometry property, if you need to have some geometry inside your shape. </li>
<li>Design a control or content template in XAML. This is the point where you can go wild in combining the Telerik controls and all the goodness of the XAML framework.</li>
<li>Optional: Add some (dependency) properties reflecting the business context. For example, in the custom shapes provided with the sample you’ll often see the stock Symbol property defined.</li>
<li>Optional: Override a couple of serialization methods, if the custom properties need to be saved/loaded/copied. </li>
</ul>
<p>Let’s take, as an example, the <strong>GaugeShape</strong> from the sample, which (see adjacent picture) displays the latest price and price change of a stock. The shape inherits from RadDiagramShape</p>
<div class="wp_codebox">
<table>
<tr id="p56991">
<td class="line_numbers">
<pre>1
</pre>
</td>
<td class="code" id="p5699code1">
<pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">sealed</span> <span style="color: #6666cc; font-weight: bold;">class</span> GaugeShape <span style="color: #008000;">:</span> RadDiagramShape</pre>
</td>
</tr>
</table>
</div>
<p>and defines a custom Symbol dependency property</p>
<div class="wp_codebox">
<table>
<tr id="p56992">
<td class="line_numbers">
<pre>1
</pre>
</td>
<td class="code" id="p5699code2">
<pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DependencyProperty SymbolProperty <span style="color: #008000;">=</span> DependencyProperty<span style="color: #008000;">.</span><span style="color: #0000FF;">Register</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Symbol&quot;</span>, <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>, <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #008000;">&#40;</span>GaugeShape<span style="color: #008000;">&#41;</span>, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> FrameworkPropertyMetadata<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">null</span>, OnSymbolChanged<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre>
</td>
</tr>
</table>
</div>
<p> Whether it’s a standard or a dependency property depends really on what you want to do with it, see<br />
<a href="http://msdn.microsoft.com/en-us/library/ms752914.aspx" title="Dependency properties" target="_blank">the MSDN documentation</a> on this. </p>
<p>Next, you need to define the visual appearance of the control. Here is an important point you need to consider; do you need the adorning elements (resizing adorner, connectors, rotation handle) of the RadDiagram framework to appear? If so, the easiest way is to base your XAML style on the base style (using the BasedOn property) from the framework;</p>
<div class="wp_codebox">
<table>
<tr id="p56993">
<td class="line_numbers">
<pre>1
</pre>
</td>
<td class="code" id="p5699code3">
<pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Style</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;{x:Type local:GaugeShape}&quot;</span> <span style="color: #000066;">BasedOn</span>=<span style="color: #ff0000;">&quot;{StaticResource {x:Type telerik:RadDiagramShape}}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span></pre>
</td>
</tr>
</table>
</div>
<p>As a result, all the adorning diagramming elements are already present and your custom shape will take part in the internal mechanics of RadDiagram. If you omit the directive, then the shape will still be part of the diagram, but you will not see or be able to use the resizer, rotation handle, and so on.</p>
<p>From this point on you can define inside the style all you wish, but there are some gotchas:</p>
<ul>
<li>The ContentTemplate does not immediately give access to the (dependency) properties of the class, but instead exposeses the Content property. So, if you want to access the DataContext inside the Content template you have to either assign this data context to the Content, or you need to walk up the visual tree a bit (see below).</li>
<li>The EditTemplate can be (and usually is) a totally different template than the ContentTemplate; it allows you to define how the control looks when the user double-clicks the shape and shifts to ‘edit’ mode. A good pattern here would be to set some data in the DataContext, assign the DataContext to the Content, and to access this data bucket both from the ContentTemplate and the EditTemplate. In situations where the EditTemplate is not sufficient you can hook up the routed DoubleClick event and redirect the default editing behavior (showing the EditTemplate) to some custom behavior. For example, in the sample you will notice that the TextShape shows a custom window on double-clicking. One essential elements here is that you need to halt the event bubbling by setting ‘Handled = true’ otherwise you will have both a custom handler active and the EditTemplate visible.</li>
</ul>
<p>In the content template of the GaugeShape you will see the following bits:</p>
<div class="wp_codebox">
<table>
<tr id="p56994">
<td class="line_numbers">
<pre>1
2
</pre>
</td>
<td class="code" id="p5699code4">
<pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>TextBlock Text<span style="color: #008000;">=</span><span style="color: #666666;">&quot;{Binding Path=Name}&quot;</span> Foreground<span style="color: #008000;">=</span><span style="color: #666666;">&quot;{StaticResource MetroAccentBrush}&quot;</span> FontWeight<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Bold&quot;</span> HorizontalAlignment<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Center&quot;</span> <span style="color: #008000;">/&gt;</span>
<span style="color: #008000;">&lt;</span>telerik<span style="color: #008000;">:</span>RadRadialGauge OuterBorderBrush<span style="color: #008000;">=</span><span style="color: #666666;">&quot;#FF08FF00&quot;</span> Grid<span style="color: #008000;">.</span><span style="color: #0000FF;">Row</span><span style="color: #008000;">=</span><span style="color: #666666;">&quot;1&quot;</span> Background<span style="color: #008000;">=</span><span style="color: #666666;">&quot;WhiteSmoke&quot;</span><span style="color: #008000;">&gt;</span></pre>
</td>
</tr>
</table>
</div>
<p>The TextBlock accesses the Name property because the Content contains the Quote information:</p>
<div class="wp_codebox">
<table>
<tr id="p56995">
<td class="line_numbers">
<pre>1
2
3
</pre>
</td>
<td class="code" id="p5699code5">
<pre class="csharp" style="font-family:monospace;">var quote <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Quote<span style="color: #008000;">&#40;</span>Symbol<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
YahooEngine<span style="color: #008000;">.</span><span style="color: #0000FF;">Fetch</span><span style="color: #008000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span>Quote<span style="color: #008000;">&gt;</span> <span style="color: #008000;">&#123;</span> quote <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Content</span> <span style="color: #008000;">=</span> quote<span style="color: #008000;">;</span></pre>
</td>
</tr>
</table>
</div>
<p>On the other hand, if you want your shape to be stretched and resized according to the outer resizing adorners, you should set the following properties:</p>
<div class="wp_codebox">
<table>
<tr id="p56996">
<td class="line_numbers">
<pre>1
2
</pre>
</td>
<td class="code" id="p5699code6">
<pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;HorizontalContentAlignment&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;Stretch&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;VerticalContentAlignment&quot;</span> <span style="color: #000066;">Value</span>=<span style="color: #ff0000;">&quot;Stretch&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre>
</td>
</tr>
</table>
</div>
<p>Finally, in order to have the Symbol property persistent, you need to tell the framework how to serialize it. While this might sound complicated we really made it simple for you to perform this step. &nbsp;The serialization of a property (say, the Symbol property) amounts to adding an entry in a dictionary:</p>
<div class="wp_codebox">
<table>
<tr id="p56997">
<td class="line_numbers">
<pre>1
2
3
4
5
6
</pre>
</td>
<td class="code" id="p5699code7">
<pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> SerializationInfo Serialize<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var info <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Serialize</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    info<span style="color: #008000;">&#91;</span>“Symbol”<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Symbol</span> <span style="color: #008000;">??</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> info<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre>
</td>
</tr>
</table>
</div>
<p>Conversely, if you want to pick up the saved value of the Symbol property you just ask the dictionary for the value:</p>
<div class="wp_codebox">
<table>
<tr id="p56998">
<td class="line_numbers">
<pre>1
2
3
4
5
</pre>
</td>
<td class="code" id="p5699code8">
<pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> Deserialize<span style="color: #008000;">&#40;</span>SerializationInfo info<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Deserialize</span><span style="color: #008000;">&#40;</span>info<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>info<span style="color: #008000;">&#91;</span>“Symbol”<span style="color: #008000;">&#93;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Symbol</span> <span style="color: #008000;">=</span> info<span style="color: #008000;">&#91;</span>SymbolTag<span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre>
</td>
</tr>
</table>
</div>
<p>There is more under the hood than this straightforward serialization alone, please consult<br />
<a href="http://www.telerik.com/help/wpf/raddiagram-overview.html" title="RadDiagram documentation" target="_blank">the RadDiagram documentation</a> for more serialization goodies.</p>
<p>Once all this is in place you can add the custom shape to the diagram and bind it to the application data like so:</p>
<div class="wp_codebox">
<table>
<tr id="p56999">
<td class="line_numbers">
<pre>1
2
3
4
5
</pre>
</td>
<td class="code" id="p5699code9">
<pre class="csharp" style="font-family:monospace;">var shape <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> GaugeShape
    <span style="color: #008000;">&#123;</span>
        Position <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Point<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">10</span>, <span style="color: #FF0000;">120</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
Diagram<span style="color: #008000;">.</span><span style="color: #0000FF;">AddShape</span><span style="color: #008000;">&#40;</span>shape<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre>
</td>
</tr>
</table>
</div>
<p> <strong>Many more possibilities and extension points are at your disposal</strong>, several of which are demonstrated in the custom shapes inside the sample application. This elementary overview, however,should get you going and we have made the documentation as complete as possible to guide you through the ins and outs of the RadDiagram framework.</p>
<h1>The toolbox: how to add your custom items</h1>
<p>The RadDiagram framework does not provide a Toolbox control out-of-the-box. It is too specific a part of the story and could just be used for some diagramming applications. That said, it is not part of the core framework, but we wrote several lines of code that can be used and easily customized for your needs. There is an Assembly that we called Features (Telerik.Windows.Diagrams.Features.dll), which is an extension of the core RadDiagram framework, and contains some nice features. For example, there is a ToolBox control there. It is a very simple Hierarchical ItemsControl that you can populate with a hierarchical data source or manually with ToolBoxGroups and items. On the other hand, the items are not much more than ContentControl, so you can<br />
effectively add any UI Elements you would like to the toolbox(i.e. you can even add regular shapes from the RadDiagram framework)</p>
<div class="wp_codebox">
<table>
<tr id="p569910">
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
10
</pre>
</td>
<td class="code" id="p5699code10">
<pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;expandertoolbox:DiagramToolbox<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;expandertoolbox:ToolboxGroup</span> <span style="color: #000066;">Header</span>=<span style="color: #ff0000;">&quot;Shapes&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;expandertoolbox:ToolboxItem</span> <span style="color: #000066;">Header</span>=<span style="color: #ff0000;">&quot;Rectangle&quot;</span> <span style="color: #000066;">Description</span>=<span style="color: #ff0000;">&quot;A simple rectangle&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
             <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:ShapeToolboxItem</span> <span style="color: #000066;">Geometry</span>=<span style="color: #ff0000;">&quot;{telerik:CommonShape ShapeType=RoundedRectangleShape}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/expandertoolbox:ToolboxItem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;expandertoolbox:ToolboxItem</span> <span style="color: #000066;">Header</span>=<span style="color: #ff0000;">&quot;Ellipse&quot;</span> <span style="color: #000066;">Description</span>=<span style="color: #ff0000;">&quot;A simple rectangle&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
             <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;local:ShapeToolboxItem</span> <span style="color: #000066;">Geometry</span>=<span style="color: #ff0000;">&quot;{telerik:CommonShape ShapeType=EllipseShape}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/expandertoolbox:ToolboxItem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/expandertoolbox:ToolboxGroup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/expandertoolbox:DiagramToolbox<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>
</td>
</tr>
</table>
</div>
<p>But the interesting part here is how to handle the drop from the Toolbox to the diagram surface. To make our lives easier, we’re using the Telerik DragDropManager, which automatically handles the start-drag operation of the toolbox item. Thus we need to handle the drop operation at the other side, on the diagram surface.</p>
<div class="wp_codebox">
<table>
<tr id="p569911">
<td class="line_numbers">
<pre>1
</pre>
</td>
<td class="code" id="p5699code11">
<pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">diagram</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Drop</span> <span style="color: #008000;">+=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnDropToolboxItem</span><span style="color: #008000;">;</span></pre>
</td>
</tr>
</table>
</div>
<p>Let’s take a look at the handler as well.</p>
<div class="wp_codebox">
<table>
<tr id="p569912">
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre>
</td>
<td class="code" id="p5699code12">
<pre class="csharp" style="font-family:monospace;">var item <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">Data</span> <span style="color: #0600FF; font-weight: bold;">as</span> DataObject<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetData</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Telerik.Windows.Diagrams.Features.ExpanderToolbox.ToolboxItem&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">as</span> ToolboxItem<span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>item <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        IToolboxItem toolboxItem <span style="color: #008000;">=</span> item<span style="color: #008000;">.</span><span style="color: #0000FF;">Content</span> <span style="color: #0600FF; font-weight: bold;">as</span> IToolboxItem<span style="color: #008000;">;</span>
        RadDiagramShape shape <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
        var position <span style="color: #008000;">=</span> e<span style="color: #008000;">.</span><span style="color: #0000FF;">GetPosition</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">diagram</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>toolboxItem <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            shape <span style="color: #008000;">=</span> toolboxItem<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateShape</span><span style="color: #008000;">&#40;</span>position<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>shape <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">diagram</span><span style="color: #008000;">.</span><span style="color: #0000FF;">AddShape</span><span style="color: #008000;">&#40;</span>shape<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            e<span style="color: #008000;">.</span><span style="color: #0000FF;">Handled</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre>
</td>
</tr>
</table>
</div>
<p>The trickier part is that each type of ToolboxItem implements the CreateShape factory method (from the IToolboxItem interface), which creates a new, configured shape for this type of toolbox item.</p>
<div class="wp_codebox">
<table>
<tr id="p569913">
<td class="line_numbers">
<pre>1
2
3
4
5
</pre>
</td>
<td class="code" id="p5699code13">
<pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> RadDiagramShape CreateShape<span style="color: #008000;">&#40;</span>Point position<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
var shape <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> ImageShape <span style="color: #008000;">&#123;</span> Location <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ImageLocation</span>, Address <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Address</span>, Position <span style="color: #008000;">=</span> position, NavigateUri <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">NavigateUri</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">return</span> shape<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre>
</td>
</tr>
</table>
</div>
<h1>The RadDiagram surface properties</h1>
<p>There is one more interesting story in this application: How to switch between Edit and Design surface modes. The <strong>RadDiagram framework provides you with plenty of properties </strong>that can control the read-only state in different aspects. Mentioning these properties here affects the run-time through UI behavior and does not enforce changes through the APIs.</p>
<p>They are:</p>
<ul>
<li><strong>IsEditable</strong> &#8211; Indicates whether editing is enabled.</li>
<li><strong>IsPanningEnabled</strong> – Indicates whether panning is enabled.</li>
<li><strong>IsZoomEnabled</strong> &#8211; Indicates whether zooming is enabled.</li>
<li><strong>IsResizingEnabled</strong> &#8211; Indicates whether shape resizing is enabled.</li>
<li><strong>IsRotationEnabled</strong> &#8211; Indicates whether shape rotation is enabled.</li>
<li><strong>IsDraggingEnabled</strong> &#8211; Indicates whether shape dragging is enabled.</li>
<li><strong>IsConnectorsManipulationEnabled</strong> &#8211; Indicates whether connection creating is enabled through the connectors.</li>
<li><strong>AllowDelete</strong> – Indicates whether item deletion is enabled.</li>
<li><strong>AllowDrop</strong> – Indicates whether the diagram can accept drop.</li>
<li><strong>AllowCopy</strong> – Indicates whether item copying is enabled.</li>
<li><strong>AllowPaste</strong> – Indicates whether item pasting is enabled.</li>
<li><strong>SelectionMode</strong> – Can choose the selection mode through enumeration:
<ul>
<li>None – selection is disabled.</li>
<li>Single – only one can be selection either with Rectangle selection tool or pressing CTRL key.</li>
<li>Multiple – selecting items on each click and with Rectangle selection tool.</li>
<li>Extended – selecting items with CTRL or using Rectangle selection tool.</li>
</ul>
</li>
</ul>
<h1>To conclude</p>
</h1>
<table border="0" cellspacing="0" cellpadding="0" style="border: none;  margin-left: 11.35pt; border-collapse: collapse; border-image: initial;">
<tbody>
<tr>
<td valign="top">
			 <img src="http://visualizationtools.net/default/wp-content/uploads/2012/03/Picture8-570x200.png" alt="Much more to discover" title="Much more to discover" width="570" height="200" class="alignnone size-post wp-image-5710" />
            </td>
</tr>
<tr>
<td valign="top">
<p ><em>[Picture above] Please visit<a title="RadDiagram documentation" target="_blank" href="http://www.telerik.com/help/wpf/raddiagram-overview.html"> the documentation on RadDiagram</a> for much more info on other aspects not touched by this overview article.</em></p>
</td>
</tr>
</tbody>
</table>
<p>While this demo application shows off some of the more juicy features of RadDiagram it is by no means the end of the story. You are invited to continue reading on: </p>
<ul>
<li>
	<a href="http://www.telerik.com/help/silverlight/raddiagram-overview.html" title="RadDiagram for Silverlight" target="_blank">RadDiagram for Silverlight</a> and the many additional possibilities Silverlight offers to display rich diagrams in the browser</li>
<li>The <a href="http://www.telerik.com/help/wpf/raddiagram-data-databinding.html" title="RadDiagram and databinding features" target="_blank">MVVM mechanism for RadDiagram<br />
	</a> </li>
<li>The <a href="http://www.telerik.com/help/wpf/raddiagrams-features-clipboard.html" title="RadDiagram and te clipboard" target="_blank">inter-operability of the clipboard between RadDiagram for Silverlight and WPF</a></li>
<li>The <a href="http://www.telerik.com/help/silverlight/raddiagrams-features-serialization.html" title="RadDiagram and serialization" target="_blank">shared WPF and Silverlight document format</a>, which allows you to exchange diagrams between the two technologies.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://visualizationtools.net/default/financial-dashboard/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A Nuget package for Processing.js</title>
		<link>http://visualizationtools.net/default/nuget-processing/</link>
		<comments>http://visualizationtools.net/default/nuget-processing/#comments</comments>
		<pubDate>Wed, 07 Dec 2011 08:12:23 +0000</pubDate>
		<dc:creator>Francois Vanderseypen</dc:creator>
				<category><![CDATA[Data Visualization]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Web client]]></category>
		<category><![CDATA[data-visualization]]></category>
		<category><![CDATA[Processing]]></category>

		<guid isPermaLink="false">http://visualizationtools.net/default/?p=5679</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>Nuget packages make it easy to add functionality to your applications. Much like adding a WCF service reference, you can add (or update) with a few clicks all sorts of libraries and tools to a Visual Studio project. If you have a website (MVC or standard webforms) you can add various JavaScript libraries; jQuery, RazorJS, fastJSON&#8230;and it&#8217;s so much easier than figuring out the download page, getting it into the correct folder and so on. Since I&#8217;m a big fan of data visualization libraries it&#8217;s a pleasure to have easy access to D3.js in this way, but I was missing for a long time access to Processing.js through the Nuget library. Not anymore. After a bit looking around and agreeing with the good people at <a title="Processing.js" href="http://processingjs.org/" target="_blank">Processing </a>I created/uploaded <a title="Processing.js" href="http://nuget.org/packages/Processing.js" target="_blank">a Nuget package for processing.js</a>.</p>
<p><a href="http://nuget.org/packages/Processing.js"><img class="alignnone size-medium wp-image-5681" title="Nuget Processing.js " src="http://visualizationtools.net/default/wp-content/uploads/2011/12/NugetProcessingScreenshot-300x274.png" alt="" width="300" height="274" /></a></p>
<p>&nbsp;</p>
<h1>1-2-3 example</h1>
<p>Create a website in Visual Studio (I&#8217;ve taken a MVC one here) and right-click on your project, go to the Nuget package manager and search for &#8216;Processing.js&#8217; in the online section</p>
<p><a href="http://nuget.org/packages/Processing.js"><img class="alignnone size-medium wp-image-5682" title="Nuget Package Manager" src="http://visualizationtools.net/default/wp-content/uploads/2011/12/NugetPackageManager-259x300.png" alt="" width="259" height="300" /></a></p>
<p><a href="http://nuget.org/packages/Processing.js"><img class="alignnone size-medium wp-image-5683" title="Nuget Package Manager" src="http://visualizationtools.net/default/wp-content/uploads/2011/12/NugetPackageManager2-300x129.png" alt="" width="300" height="129" /></a></p>
<p>Click on &#8216;Install&#8217; and a couple of JS libraries will be added to your Scripts folder.</p>
<p>Either in your aspx/html or in the &#8216;_layout.cshtml&#8217; file add a reference to the &#8216;Processing.js&#8217; library. You can simply drag-drop the file from the Solution Browser into the text editor and Visual Studio will automatically create the correct reference. From this point on the possibilities are endless, have a look at the <a title="Processing exhibition" href="http://processingjs.org/exhibition" target="_blank">Processing ehibition</a> for instance.</p>
<p>For example, if you wish to add a clock to your page change the &#8216;Index.cshtml&#8217; to the following:</p>
<div class="wp_codebox">
<table>
<tr id="p567914">
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
</pre>
</td>
<td class="code" id="p5679code14">
<pre class="xml" style="font-family:monospace;">@{
    ViewBag.Title = &quot;Home Page&quot;;
}
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    A simple processing.js clock
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;canvas</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;canvas1&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;200&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;200&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/canvas<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;script</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;script1&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    // Simple way to attach js code to the canvas is by using a function
    function sketchProc(processing) {
        // Override draw function, by default it will be called 60 times per second
        processing.draw = function () {
            // determine center and max clock arm length
            var centerX = processing.width / 2, centerY = processing.height / 2;
            var maxArmLength = Math.min(centerX, centerY);
&nbsp;
            function drawArm(position, lengthScale, weight) {
                processing.strokeWeight(weight);
                processing.line(centerX, centerY,
        centerX + Math.sin(position * 2 * Math.PI) * lengthScale * maxArmLength,
        centerY - Math.cos(position * 2 * Math.PI) * lengthScale * maxArmLength);
            }
&nbsp;
            // erase background
            processing.background(224);
&nbsp;
            var now = new Date();
&nbsp;
            // Moving hours arm by small increments
            var hoursPosition = (now.getHours() % 12 + now.getMinutes() / 60) / 12;
            drawArm(hoursPosition, 0.5, 5);
&nbsp;
            // Moving minutes arm by small increments
            var minutesPosition = (now.getMinutes() + now.getSeconds() / 60) / 60;
            drawArm(minutesPosition, 0.80, 3);
&nbsp;
            // Moving hour arm by second increments
            var secondsPosition = now.getSeconds() / 60;
            drawArm(secondsPosition, 0.90, 1);
        };
&nbsp;
    }
&nbsp;
    var canvas = document.getElementById(&quot;canvas1&quot;);
    // attaching the sketchProc function to the canvas
    var p = new Processing(canvas, sketchProc);
    // p.exit(); to detach it
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;height: 0px; width: 0px; overflow: hidden;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>
</td>
</tr>
</table>
</div>
<p>Run/display this page and you&#8217;ll get something like the following:</p>
<p><a href="http://nuget.org/packages/Processing.js"><img src="http://visualizationtools.net/default/wp-content/uploads/2011/12/NugetSimpleProcessingApp-300x143.png" alt="" title="Nuget Simple Processing App" width="300" height="143" class="alignnone size-medium wp-image-5687" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://visualizationtools.net/default/nuget-processing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The job of the future</title>
		<link>http://visualizationtools.net/default/job-of-the-future/</link>
		<comments>http://visualizationtools.net/default/job-of-the-future/#comments</comments>
		<pubDate>Mon, 17 Oct 2011 16:54:09 +0000</pubDate>
		<dc:creator>Francois Vanderseypen</dc:creator>
				<category><![CDATA[Data Visualization]]></category>
		<category><![CDATA[data-visualization]]></category>

		<guid isPermaLink="false">http://visualizationtools.net/default/?p=5672</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p><iframe src="http://player.vimeo.com/video/29684853?title=0&amp;byline=0&amp;portrait=0" width="400" height="225" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>
<p><a href="http://vimeo.com/29684853">The Value of Data Visualization</a> from <a href="http://vimeo.com/columnfive">Column Five</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p>Finding the needle in the haystack is now more pressing than before. The more data we produce and have access to, the more the need for something intelligent to get the information out of the data becomes important. Obviously the ever-increasing amount of data and the shift to global data stores like the cloud have increased the need for better visualizations techniques and innovative thinking.But the amount of noise has also increased and the need to choose through it. How to filter out of the billions of tweets the stuff that matters to you? The only way to do that is by choosing; what matters to you is related to the things that you choose for and which have a direct relevance in your life (personal or professional). Before the filtering comes the criteria and the criteria are dictated by the business or personal context combined with a more-or-less precise definition of what the solution or answer should be like. Usually a question has many answers or complementary answers, much like a mathematical problem has a one or more solutions depending on the target space within which the solution should be found. </p>
<p>Although I find it refreshing to see that data visualization and presentation techniques are now more important than ever before, it&#8217;s not for myself the reason why this field is interesting or fertile. Data visualization is useful as a product for the end-user or consumer but is equally well interesting for the producer or creative inventor because it combines various fields. Like nothing else you need to be partly an artist, partly a scientist to come up with something beautiful yet meaningful and interesting. You need to understand sometimes deep technical details and exotic programming concepts while having a good understanding of the business context and what a customer needs. You need to fathom the (business) question and at the same time have a vision, which usually means a mixture of intuition and expertise. You need to understand the language of different people and sub-cultures, read through minor details, yet see through the details and go beyond to deliver a broad concept. Having a bird&#8217;s-eye view while knowing the murky details on a low-level demands a mind which can stretch across all levels of thinking. Can you make the simple complex and the complex simple? Can you see color where others see mathematical formula&#8217;s? Can you see sound where others see data and shapes? Can you see code where someone sees motion? Can you see patterns where others see noise? Can you see information where others see data?</p>
<ul>
<li><a href="http://www.visualizing.org/" title="Visualizing" target="_blank">Visualizing.org</a></li>
<li><a href="http://www.informationisbeautiful.net/" title="Information is beautiful" target="_blank">Information is beautiful</a></li>
<li><a href="http://datavisualization.ch/showcases/" title="Data Viz Showcase" target="_blank">Data Visualization Showcase</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://visualizationtools.net/default/job-of-the-future/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A functional graph library in F#</title>
		<link>http://visualizationtools.net/default/functional-graph-library/</link>
		<comments>http://visualizationtools.net/default/functional-graph-library/#comments</comments>
		<pubDate>Wed, 13 Jul 2011 10:46:56 +0000</pubDate>
		<dc:creator>Francois Vanderseypen</dc:creator>
				<category><![CDATA[FSharp]]></category>
		<category><![CDATA[Diagramming]]></category>
		<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">http://visualizationtools.net/default/?p=5636</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s well-know that adjacency lists are the way to go if you wish to model a graph data structure, so we&#8217;ll represent a graph as (node*adjacency list) and include some generic data buckets in the tuple. More precisely</p>
<div class="wp_codebox">
<table>
<tr id="p563615">
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre>
</td>
<td class="code" id="p5636code15">
<pre class="fsharp" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">type</span> Node <span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">int</span> <span style="color: #5d478b; font-style: italic;">(* node identifier *)</span> <span style="color: #a52a2a;">*</span>
    <span style="color: #3cb371;">'TDaton (* node data *)
&nbsp;
type Connection&lt;'</span>TDaton<span style="color: #a52a2a;">&gt;</span> <span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">int</span> <span style="color: #5d478b; font-style: italic;">(* source Id *)</span> <span style="color: #a52a2a;">*</span>
    <span style="color: #06c; font-weight: bold;">int</span> <span style="color: #5d478b; font-style: italic;">(* sink Id *)</span> <span style="color: #a52a2a;">*</span>
    <span style="color: #06c; font-weight: bold;">int</span> <span style="color: #5d478b; font-style: italic;">(* weight *)</span> <span style="color: #a52a2a;">*</span>
    <span style="color: #3cb371;">'TDaton (* connection data *)
&nbsp;
type Adjacency&lt;'</span>TDaton<span style="color: #a52a2a;">&gt;</span> <span style="color: #a52a2a;">=</span>
    Connection<span style="color: #a52a2a;">&lt;</span><span style="color: #3cb371;">'TDaton&gt; list (* a list of connections to other nodes*)
&nbsp;
type Atom&lt;'</span>TNodeDaton, <span style="color: #3cb371;">'TConnectionDaton&gt; =
    Node&lt;'</span>TNodeDaton<span style="color: #a52a2a;">&gt;</span> <span style="color: #a52a2a;">*</span> Adjacency<span style="color: #a52a2a;">&lt;</span><span style="color: #3cb371;">'TConnectionDaton&gt;
&nbsp;
type Graph&lt;'</span>TNodeDaton, <span style="color: #3cb371;">'TConnectionDaton&gt; =
    Atom&lt;'</span>TNodeDaton, <span style="color: #3cb371;">'TConnectionDaton&gt; list</span></pre>
</td>
</tr>
</table>
</div>
<p>The connections are directed links between nodes and the weight tuple factor is taken out of the generic data bucket in order to make weighted graph analysis possible. One could replace the generic data attached to the connection by an interface which would require a weight property, but here again this would complicate things in code. In any case, in the documentation of the library I have added an example of how to attach polymorphic data buckets to nodes and connections.</p>
<p>The adjacency type is just an alias for a connection list and, finally, the graph is as advertised above just a list of nodes with their adjacency list. The Atom type is a type alias for one single graph (list) element and comes handy in the algorithms.</p>
<p>From these few type definitions you can spend the rest of your life doing graph analysis and implementing all the known (or unknown) graph algorithms; shortest path, Hamiltionian cycles, connected components and so on. At this point the current implementation contains the most used ones and the emphasis was also on having a quick (and easy) way to display the graph (and their properties).</p>
<div class="image-wrap portfolio"><a href="/default/wp-content/uploads/2011/07/FGraphInteractive.png" rel="prettyPhoto" title="FGraph in F# Interactive"><span class="zoom"></span>
<img src="http://visualizationtools.net/default/wp-content/uploads/2011/07/FGraphInteractive-300x173.png" alt="FGraph Interactive" title="FGraph Interactive" width="300" height="173" class="alignnone size-medium wp-image-5643" />
</a></div><p style="clear:both;">&nbsp;</p><p style="clear:both">&nbsp;</p>
<p>Obviously, if you want to implement some graph algorithms you need all the basics like; adding nodes, removing connections, setting node data, fetching upstream and downstream nodes, testing whether a link exists and so on. In addition, you also need a way to easily create graphs or generate random graphs (with a fixed node degree for example). Since most of the graph analysis focuses on paths and structure it&#8217;s often not necessary to adorn the graph with data or weights, hence the possibility to define graphs through a more simple structure:</p>
<div class="wp_codebox">
<table>
<tr id="p563616">
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
10
</pre>
</td>
<td class="code" id="p5636code16">
<pre class="fsharp" style="font-family:monospace;">&nbsp;
<span style="color: #06c; font-weight: bold;">let</span> pregraph <span style="color: #a52a2a;">=</span>
        <span style="color: #6c6;">&#91;</span>
        <span style="color: #6c6;">&#40;</span><span style="color: #c6c;">1</span>, <span style="color: #6c6;">&#91;</span><span style="color: #c6c;">2</span><span style="color: #6c6;">&#93;</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">;</span>
        <span style="color: #6c6;">&#40;</span><span style="color: #c6c;">2</span>, <span style="color: #6c6;">&#91;</span><span style="color: #c6c;">3</span><span style="color: #a52a2a;">;</span><span style="color: #c6c;">5</span><span style="color: #6c6;">&#93;</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">;</span>
        <span style="color: #6c6;">&#40;</span><span style="color: #c6c;">3</span>, <span style="color: #6c6;">&#91;</span><span style="color: #c6c;">4</span><span style="color: #6c6;">&#93;</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">;</span>
        <span style="color: #6c6;">&#40;</span><span style="color: #c6c;">5</span>, <span style="color: #6c6;">&#91;</span><span style="color: #c6c;">4</span><span style="color: #6c6;">&#93;</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">;</span>
        <span style="color: #6c6;">&#93;</span>
<span style="color: #06c; font-weight: bold;">let</span> graph <span style="color: #a52a2a;">=</span> Orbifold.<span style="color: #060;">FGraph</span>.<span style="color: #060;">ParseList</span> pregraph
graph <span style="color: #a52a2a;">|&gt;</span> GraphVisualizer.<span style="color: #060;">Show</span></pre>
</td>
</tr>
</table>
</div>
<p>This will launch the visualizer and produce the following simple graph:</p>
<p><img class="alignnone size-full wp-image-5638" title="a simple graph" src="http://visualizationtools.net/default/wp-content/uploads/2011/07/simplegraph.png" alt="a simple graph" width="547" height="509" /></p>
<p>The power of F# resides in the handling of lists and avoiding loops as one would in C# tremendously helps in situations like this. One should compare the ParseList method implementation with what it would take in C#:</p>
<div class="wp_codebox">
<table>
<tr id="p563617">
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre>
</td>
<td class="code" id="p5636code17">
<pre class="fsharp" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">///&lt;summary&gt;Parses a simple list made out of tuples of (int, int list). The first integer is the index and the list is the list of nodes indices to which the current node is connected.&lt;/summary&gt;</span>
<span style="color: #06c; font-weight: bold;">let</span> ParseList <span style="color: #6c6;">&#40;</span>list : <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">int</span><span style="color: #a52a2a;">*</span> <span style="color: #06c; font-weight: bold;">int</span> list<span style="color: #6c6;">&#41;</span> list<span style="color: #6c6;">&#41;</span> : Graph<span style="color: #a52a2a;">&lt;</span>Object,Object<span style="color: #a52a2a;">&gt;=</span>
        <span style="color: #06c; font-weight: bold;">let</span> g <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">ref</span> Graph.<span style="color: #060;">Empty</span>
        <span style="color: #06c; font-weight: bold;">let</span> linkedIds: <span style="color: #06c; font-weight: bold;">int</span> list <span style="color: #06c; font-weight: bold;">ref</span> <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">ref</span> <span style="color: #6c6;">&#91;</span><span style="color: #6c6;">&#93;</span>
        <span style="color: #06c; font-weight: bold;">let</span> containsNumber number list <span style="color: #a52a2a;">=</span> <a href="http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/namespaces.html"><span style="color: #06c; font-weight: bold;">List</span></a>.<span style="color: #060;">exists</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> elem <span style="color: #a52a2a;">-&gt;</span> elem <span style="color: #a52a2a;">=</span> number<span style="color: #6c6;">&#41;</span> list
        g.<span style="color: #060;">Value</span> <span style="color: #a52a2a;">&lt;-</span> <span style="color: #6c6;">&#40;</span>list <span style="color: #a52a2a;">|&gt;</span> <a href="http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/namespaces.html"><span style="color: #06c; font-weight: bold;">List</span></a>.<span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> <span style="color: #6c6;">&#40;</span>idx,adjs<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">-&gt;</span>
                                <span style="color: #06c; font-weight: bold;">let</span> newNode : Node<span style="color: #a52a2a;">&lt;</span>Object<span style="color: #a52a2a;">&gt;</span> <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#40;</span>idx, <span style="color: #06c; font-weight: bold;">null</span><span style="color: #6c6;">&#41;</span>
                                <span style="color: #06c; font-weight: bold;">let</span> ad <span style="color: #a52a2a;">=</span> <a href="http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/namespaces.html"><span style="color: #06c; font-weight: bold;">List</span></a>.<span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> t<span style="color: #a52a2a;">-&gt;</span>
                                                        <span style="color: #06c; font-weight: bold;">if</span> <span style="color: #06c; font-weight: bold;">not</span> <span style="color: #6c6;">&#40;</span>containsNumber t <span style="color: #a52a2a;">!</span>linkedIds<span style="color: #6c6;">&#41;</span> <span style="color: #06c; font-weight: bold;">then</span> linkedIds.<span style="color: #060;">Value</span><span style="color: #a52a2a;">&lt;-</span> t::<span style="color: #a52a2a;">!</span>linkedIds
                                                        <span style="color: #06c; font-weight: bold;">let</span> con : Connection<span style="color: #a52a2a;">&lt;</span>Object<span style="color: #a52a2a;">&gt;</span> <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#40;</span>idx,t,<span style="color: #c6c;">0</span>,<span style="color: #06c; font-weight: bold;">null</span><span style="color: #6c6;">&#41;</span>
                                                        con
                                                   <span style="color: #6c6;">&#41;</span> adjs
                                <span style="color: #06c; font-weight: bold;">let</span> atom : Atom<span style="color: #a52a2a;">&lt;</span>Object,Object<span style="color: #a52a2a;">&gt;</span> <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#40;</span>newNode,ad<span style="color: #6c6;">&#41;</span>
                                atom
                          <span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span>
        <span style="color: #5d478b; font-style: italic;">(*we auto-add the node if it appears in the connections*)</span>
        linkedIds.<span style="color: #060;">Value</span> <span style="color: #a52a2a;">|&gt;</span> <a href="http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/namespaces.html"><span style="color: #06c; font-weight: bold;">List</span></a>.<span style="color: #060;">iter</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> t <span style="color: #a52a2a;">-&gt;</span>
                                             <span style="color: #06c; font-weight: bold;">if</span> <span style="color: #06c; font-weight: bold;">not</span> <span style="color: #6c6;">&#40;</span>NodeExists t <span style="color: #a52a2a;">!</span>g<span style="color: #6c6;">&#41;</span> <span style="color: #06c; font-weight: bold;">then</span>
                                                            <span style="color: #06c; font-weight: bold;">let</span> n : Node<span style="color: #a52a2a;">&lt;</span>Object<span style="color: #a52a2a;">&gt;</span> <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#40;</span>t,<span style="color: #06c; font-weight: bold;">null</span><span style="color: #6c6;">&#41;</span>
                                                            <span style="color: #06c; font-weight: bold;">let</span> c : Adjacency<span style="color: #a52a2a;">&lt;</span>Object<span style="color: #a52a2a;">&gt;</span> <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#91;</span><span style="color: #6c6;">&#93;</span>
                                                            g.<span style="color: #060;">Value</span><span style="color: #a52a2a;">&lt;-</span><span style="color: #6c6;">&#40;</span>n,c<span style="color: #6c6;">&#41;</span>::<span style="color: #a52a2a;">!</span>g
                                      <span style="color: #6c6;">&#41;</span>
        g.<span style="color: #060;">Value</span></pre>
</td>
</tr>
</table>
</div>
<p>The interesting part of the library sits in the analysis module where one encounters the unavoidable traversals and where I tried to also implement useful variants like the depth-first traversal of edges:</p>
<div class="wp_codebox">
<table>
<tr id="p563618">
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre>
</td>
<td class="code" id="p5636code18">
<pre class="fsharp" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">let</span> DFTEdgeVisit <span style="color: #6c6;">&#40;</span>g:Graph<span style="color: #a52a2a;">&lt;</span>_,_<span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>start:<span style="color: #06c; font-weight: bold;">int</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>processEdge: <span style="color: #06c; font-weight: bold;">int</span><span style="color: #a52a2a;">*</span>int<span style="color: #a52a2a;">-&gt;</span><span style="color: #06c; font-weight: bold;">unit</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>
        <span style="color: #06c; font-weight: bold;">let</span> nextToVisit <span style="color: #a52a2a;">=</span> Stack<span style="color: #a52a2a;">&lt;</span><span style="color: #06c; font-weight: bold;">int</span><span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span>
        <span style="color: #06c; font-weight: bold;">let</span> discovered <span style="color: #a52a2a;">=</span> HashSet<span style="color: #a52a2a;">&lt;</span><span style="color: #06c; font-weight: bold;">int</span><span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span>
        <span style="color: #06c; font-weight: bold;">let</span> v <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">ref</span> <span style="color: #c6c;">0</span> <span style="color: #5d478b; font-style: italic;">(* current vertex *)</span>
        <span style="color: #06c; font-weight: bold;">ignore</span><span style="color: #6c6;">&#40;</span>nextToVisit.<span style="color: #060;">Push</span><span style="color: #6c6;">&#40;</span>start<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span>
        <span style="color: #06c; font-weight: bold;">ignore</span><span style="color: #6c6;">&#40;</span>discovered.<span style="color: #060;">Add</span><span style="color: #6c6;">&#40;</span>start<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span>
        <span style="color: #06c; font-weight: bold;">let</span> parents <span style="color: #a52a2a;">=</span> Dictionary<span style="color: #a52a2a;">&lt;</span><span style="color: #06c; font-weight: bold;">int</span>,<span style="color: #06c; font-weight: bold;">int</span><span style="color: #a52a2a;">&gt;</span><span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span>
        <span style="color: #06c; font-weight: bold;">while</span> nextToVisit.<span style="color: #060;">Count</span> <span style="color: #a52a2a;">&gt;</span> <span style="color: #c6c;">0</span> <span style="color: #06c; font-weight: bold;">do</span>
            v.<span style="color: #060;">Value</span> <span style="color: #a52a2a;">&lt;-</span> nextToVisit.<span style="color: #060;">Pop</span><span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span>
            <span style="color: #06c; font-weight: bold;">if</span> parents.<span style="color: #060;">ContainsKey</span><span style="color: #6c6;">&#40;</span><span style="color: #a52a2a;">!</span>v<span style="color: #6c6;">&#41;</span> <span style="color: #06c; font-weight: bold;">then</span> processEdge <span style="color: #6c6;">&#40;</span>parents.<span style="color: #6c6;">&#91;</span><span style="color: #a52a2a;">!</span>v<span style="color: #6c6;">&#93;</span>,<span style="color: #a52a2a;">!</span>v<span style="color: #6c6;">&#41;</span>
            <span style="color: #06c; font-weight: bold;">let</span> someDescendants <span style="color: #a52a2a;">=</span> GetDescendants <span style="color: #a52a2a;">!</span>v g
            <span style="color: #06c; font-weight: bold;">match</span> someDescendants <span style="color: #06c; font-weight: bold;">with</span>
            |None <span style="color: #a52a2a;">-&gt;</span> <span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span>
            |Some<span style="color: #6c6;">&#40;</span>desc<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">-&gt;</span> <a href="http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/namespaces.html"><span style="color: #06c; font-weight: bold;">List</span></a>.<span style="color: #060;">iter</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> d <span style="color: #a52a2a;">-&gt;</span> <span style="color: #06c; font-weight: bold;">if</span> <span style="color: #06c; font-weight: bold;">not</span> <span style="color: #6c6;">&#40;</span>discovered.<span style="color: #060;">Contains</span><span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fst</span> d<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span> <span style="color: #06c; font-weight: bold;">then</span>
                                                                                    <span style="color: #06c; font-weight: bold;">ignore</span><span style="color: #6c6;">&#40;</span>parents.<span style="color: #060;">Add</span><span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fst</span> d,<span style="color: #a52a2a;">!</span>v<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span>
                                                                                    <span style="color: #06c; font-weight: bold;">ignore</span><span style="color: #6c6;">&#40;</span>discovered.<span style="color: #060;">Add</span><span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fst</span> d<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span>
                                                                                    <span style="color: #06c; font-weight: bold;">ignore</span><span style="color: #6c6;">&#40;</span>nextToVisit.<span style="color: #060;">Push</span><span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fst</span> d<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span><a href="http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/namespaces.html"><span style="color: #06c; font-weight: bold;">List</span></a>.<span style="color: #060;">rev</span> desc<span style="color: #6c6;">&#41;</span></pre>
</td>
</tr>
</table>
</div>
<p>Here again, one would use in C# Action<> parameters for the visitors but in F# things are made easy since functions and variables are on the same footing.</p>
<p>Some of the algorithms, like the shortest path between two nodes, are quite straightforward in C# but are really an headache when trying to convert to purely functional analogs. At this points the library does its best to honour the functional paradigm (also because often it represents immense performance gains w.r.t. the more linear non-functional versions) but sometimes the traditional algorithms are more readable and hence more accessible for developers who wish to alter the code. </p>
<p>Finally, I&#8217;d like to highlight the graph visualization part which resides in a separate library. When experimenting with F# and graphs in the F# interactive mode you can easily display the graph by feeding it to the <strong>GraphVisualizer.Show</strong> method:</p>
<div class="wp_codebox">
<table>
<tr id="p563619">
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
</pre>
</td>
<td class="code" id="p5636code19">
<pre class="fsharp" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">let</span> g <span style="color: #a52a2a;">=</span>
        <span style="color: #6c6;">&#91;</span>
        <span style="color: #6c6;">&#40;</span><span style="color: #c6c;">1</span>, <span style="color: #6c6;">&#91;</span><span style="color: #c6c;">2</span><span style="color: #6c6;">&#93;</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">;</span>
        <span style="color: #6c6;">&#40;</span><span style="color: #c6c;">2</span>, <span style="color: #6c6;">&#91;</span><span style="color: #c6c;">3</span><span style="color: #a52a2a;">;</span><span style="color: #c6c;">5</span><span style="color: #6c6;">&#93;</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">;</span>
        <span style="color: #6c6;">&#40;</span><span style="color: #c6c;">3</span>, <span style="color: #6c6;">&#91;</span><span style="color: #c6c;">4</span><span style="color: #6c6;">&#93;</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">;</span>
        <span style="color: #6c6;">&#40;</span><span style="color: #c6c;">5</span>, <span style="color: #6c6;">&#91;</span><span style="color: #c6c;">4</span><span style="color: #6c6;">&#93;</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">;</span>
        <span style="color: #6c6;">&#93;</span>
g <span style="color: #a52a2a;">|&gt;</span> GraphVisualizer.<span style="color: #060;">Show</span><span style="color: #a52a2a;">;;</span>
GraphVisualizer.<span style="color: #060;">ShowShortestPath</span><span style="color: #6c6;">&#40;</span><span style="color: #c6c;">1</span>,<span style="color: #c6c;">4</span>,g<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">;;</span></pre>
</td>
</tr>
</table>
</div>
<p>You can also use the graph visualizer as a WPF component in your application. The control is a classic MVVM control with an ItemsSource accepting a Graph structure. Typically you&#8217;d use it as follows:</p>
<div class="wp_codebox">
<table>
<tr id="p563620">
<td class="line_numbers">
<pre>1
</pre>
</td>
<td class="code" id="p5636code20">
<pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Visualizer:GraphControl</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;Diagram&quot;</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Tension</span>=<span style="color: #ff0000;">&quot;{Binding Value, ElementName=TensionSlider}&quot;</span> <span style="color: #000066;">ItemsSource</span>=<span style="color: #ff0000;">&quot;{Binding Graph}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre>
</td>
</tr>
</table>
</div>
<p>where the tension determines the tension between the node in the spring-embedder layout algorithm. I&#8217;ll add some more layout algorithms in the next pass (tree layout variations in particular).</p>
<p>The WPF component also has easy access to the analysis like for example the shortes path:</p>
<div class="wp_codebox">
<table>
<tr id="p563621">
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre>
</td>
<td class="code" id="p5636code21">
<pre class="fshapr" style="font-family:monospace;">let g2 = Graph.ParseList [
                            (1, [3;6;9]);
                            (2, [4]);
                            (3, [2;5]);
                            (5, [20]);
                            (6, [7;8]);
                            (7, [12]);
                            (9, [10;11]);
                            (12, [13]);
                            (20, [12]);
&nbsp;
                        ]
this.Graph &lt;- Graph.Analysis.ColorizeShortestPath( 1,13, g2)</pre>
</td>
</tr>
</table>
</div>
<p>where &#8220;this.Graph&#8221; is here the MVVM property of the model bound to the GraphControl. The result would be the picture below.<br />
<img src="http://visualizationtools.net/default/wp-content/uploads/2011/07/shortestpath.png" alt="shortest path visualization" title="shortest path visualization" width="572" height="571" class="alignnone size-full wp-image-5639" /></p>
<p>Some final remarks and questions regarding this project:</p>
<ul>
<li>all&#8217;s F#; the custom diagramming control, the library and the MVVM stuff. It would make sense to use this library in a C# context (to benefit from the F# performance) but I haven&#8217;t put any energy in this. Not sure it&#8217;s a priority since our other libraries (<a href="/G2" title="G2 library" target="_blank">G2</a>, <a href="/Lynx" title="Lynx library" target="_blank">Lynx</a>) contain all this as well. </li>
<li>this is work in progress and I spend a lot of time documenting things as well as creating sample code, I reckon things will settle by September this year.</li>
<li>making this open source would be a good thing for the community but I&#8217;ll have to sleep over it. You can try to convince me.</li>
<li>the WPF control is a templated control and can be customized. On the other hand, the arrow and node visual are hard coded. It could be useful to template them as well and in this fashion enable databinding of the databuckets defined above to the visuals. However, this library is not a full-fledged Visio replacement, rather it focuses on graph analysis. Does is make sense to invent a mechanism to enable more line-of-business scenario&#8217;s? I think our existing graph libraries are filling the need there. The only reason I would put energy in this is that at this moment there is no graph visualization library for F# on the market. </li>
<li>What about Silverlight? Well, I don&#8217;t see a technical issue there but I am less sure about the audience doing Silverlight development in F#. Is there an audience for this?</li>
</ul>
<p>Thanks to <a href="http://tomasp.net/" title="Tomas Petricek" target="_blank">Tomas Petricek</a> for some useful suggestions and the <a href="http://www.ffconsultancy.com/" title="Flying Frog Consulting" target="_blank">Flying Frog</a> for insightful directions.</p>
]]></content:encoded>
			<wfw:commentRss>http://visualizationtools.net/default/functional-graph-library/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Categories, monads and F# for dummies</title>
		<link>http://visualizationtools.net/default/categories-for-dummies/</link>
		<comments>http://visualizationtools.net/default/categories-for-dummies/#comments</comments>
		<pubDate>Fri, 01 Jul 2011 12:19:58 +0000</pubDate>
		<dc:creator>Francois Vanderseypen</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[FSharp]]></category>
		<category><![CDATA[Science]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[Mathematics]]></category>

		<guid isPermaLink="false">http://visualizationtools.net/default/?p=5554</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>This is an article to stir your curiosity about monads, categories and functional programming. I didn&#8217;t try to make it mathematically correct and it&#8217;s not a precise treatment of monads from the functional programming point of view. It&#8217;s only to help you understand and appreciate the unifying power of monads as they appear in the context of F# and Haskell. I also think there is a widely spread confusion around monads and the fact that diverse concepts are mixed up:</p>
<ul>
<li>is a computational expression the same as a workflow, is it a monad?</li>
<li>how do math articles about monads map to articles about functional programming?</li>
<li>are the methods defined by F# as part of a computation expression dictated by the idea of a monad? How to translate the concepts from categories to functional programming and back?</li>
<li>why should I care about monads, they never appear in C# or JavaScript&#8230;!?</li>
<li>how can the Haskell or OCaml stuff be translated to F#? Though Haskell has a whole lot of info regarding monads, it&#8217;s not always easy to see how it maps to the F# syntax or way of thinking.</li>
</ul>
<p>Let me say straight away that understanding monads will not directly solve your marketing challenges, nor will it make writing software easier, not is it a magical recipe to simplify the data access layer of your website. However, it does</p>
<ul>
<li><strong>make you a better programmer</strong> in the same way that learning design patterns (anti-pattern etc.) teaches you to recognize structures and solutions on a higher level of abstraction</li>
<li><strong>shed light on the relationship between domains which seem unrelated</strong> (exception handling, state management, graphs and networks, asynchronous calls, domain specific languages and so on)</li>
<li>suggest you to <strong>rethink software structures</strong> and how typical programming challenges are solved</li>
<li>open up a large field of ideas in the overlap of software development and maths which are fun to explore.</li>
</ul>
<p>I don&#8217;t expect to see monadic stuff appear in Silverlight animations or in CSS style sheets but for sure you&#8217;ll see it appear in</p>
<ul>
<li>the NoSQL movement and graph analysis algorithms</li>
<li>the financial and insurance business where F# is at this moment a big hit</li>
<li>DSL developments and meta-programming</li>
</ul>
<p>and who knows what the future brings?</p>
<h2>A simple idea</h2>
<p>We are surrounded by data collections and structures, sets of interrelated data and hierarchies:</p>
<ul>
<li>a family and its members where relationships are defined by concepts like father, mother, sister, uncle and so on</li>
<li>company hierarchies with employees and relationships defined by team structures, divisions and so on</li>
<li>modules of code and calling/instantiation relations where exceptions are bubbling from down the stack to the upper (UI level if not properly handled) parts</li>
<li>algorithmic financial transactions (flows) based on some intricate logic</li>
</ul>
<p>and on a more scientific level even more where one notices certain similarities and intriguing analogies:</p>
<ul>
<li>genes and memes: the formation of genetic structures vs. the formation of thoughts and ideas</li>
<li>the spreading of viral diseases vs. the spreading of computer viruses</li>
<li>the six-degrees of freedom appearing on the internet vs. the plex of actors in the IMDB database</li>
</ul>
<p>In a lot of cases similarities are just accidental (it&#8217;s because a banana and a lemon are yellow that there is deep connection, except the shared pigment) but there are equally well amazing symmetries which can be made precise. Category theory is a theory which makes this idea of &#8216;being similar&#8217; exact and at the heart of it is really a very simple notion:</p>
<ul>
<li>you have <strong>collection </strong>of objects</li>
<li>a collection has a certain <strong>structure</strong></li>
<li>you can <strong>map </strong>objects from one collection to another and the relationship can also be mapped.</li>
</ul>
<p><a href="http://visualizationtools.net/default/wp-content/uploads/2011/06/CategoryIdea.png"><img class="alignnone size-full wp-image-5557" title="Category Idea" src="http://visualizationtools.net/default/wp-content/uploads/2011/06/CategoryIdea.png" alt="Category Idea" width="469" height="287" /></a></p>
<table style="border: 1px solid #6B8B99; float: none; width: 90%;">
<tbody>
<tr style="background-color: #6b8b99;">
<th style="color: #fff;"> <strong>Example </strong></th>
</tr>
<tr>
<td>Take the members of a family (with their relationships; mother, father etc.) and take the employees in a company with its hierarchy. Can you create a mapping from one collection to the other? If your thinking is more in terms of pictures and visuals you&#8217;ll probably starts drawing a graph and map the nodes and edges from one graph to the other. This is no accident, graphs and mappings from one graph onto another is a good playground to study categories.</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<table style="border: 1px solid #6B8B99; float: none; width: 90%;">
<tbody>
<tr style="background-color: #6b8b99;">
<th style="color: #fff;"> <strong>Example </strong></th>
</tr>
<tr>
<td>Take some business logic and imagine that at some point an exception is thrown; it propagates through the logic to end up being caught (or not). Take, on the other hand, the stateful (call it state-machine if you prefer) flow of an online ordering system where you are guided through a wizard to check out and buy some goods (like you&#8217;d do on Amazon). Is there some underlying structure governing these two systems? Probably this would take you somewhat longer than the previous example and it also requires a bit more abstraction, but the answer is &#8216;yes&#8217; there is a mapping (i.e. a mathematically exact symmetry) between the two.State machines (<a title="Automata" href="http://en.wikipedia.org/wiki/Automata_theory" target="_blank">automata</a>) and the way exceptions or infinities can be handled in code have a common mathematical underpinning.</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>From this we take a bold step a define a (pedestrian) category as follows (more precise definition below):</p>
<table style="border: 1px solid #D94D23; float: none; width: 90%;">
<tbody>
<tr style="background-color: #d94d23;">
<th style="color: #fff;"> <strong>Definition</strong></th>
</tr>
<tr>
<td><em>A <strong>category </strong>is a collection of objects with some relationships. The objects in the collection are called (duh!) <strong>objects </strong>and the relationships are called <strong>morphisms</strong>. A mapping between two categories is called a <strong>functor</strong>.</em></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>Maybe it should be called at this point a pedestrian category or something because this is of course not very precise; this definition would turn pretty much everything into categories and functors! At this point I only want to help you understand where the real maths and the hard F# stuff is coming from and make it more acceptable.</p>
<p>A <strong>morphism </strong>is something which morphs one thing into another, something which transfers one thing into another. You will also encounter in the literature similar concepts like</p>
<ul>
<li><strong>homomorphism</strong>: it emphasizes even more the idea of morphing. The prefix &#8216;homo&#8217; meaning &#8216;the same&#8217; and &#8216;morph&#8217; means &#8216;changing&#8217;. Technically speaking the term homomorphism comes from group theory and morphism refers to the more general idea which can be applied outside group theory and Lie groups.</li>
<li><strong>endomorphism</strong>: &#8216;endo&#8217; means &#8216;in itself&#8217; and this emphasizes that a morphism is not going somewhere else but stays in the same package. So, an endomorphism of a family would map one person onto another inside the same family. A morphism would in general morph one family onto another.</li>
<li><strong>epimorphism</strong>: here the emphasis is on the fact that the morphism reaches the whole target collection.</li>
<li><strong>isomorphism</strong>: in this case the morphism is mapping elements one-to-one and onto the target. The notion is a very important one and appears pretty much everywhere in maths and science. In essence it says that if two things are isomorphic they are indistinguishable from one another and anything.everything you say about one is valid for the other.</li>
</ul>
<h2>Some more simple examples</h2>
<p>In order to move away from the heuristics above towards some more serious definitions I need to highlight more (simple) examples.</p>
<table style="border: 1px solid #6B8B99; float: none; width: 90%;">
<tbody>
<tr style="background-color: #6b8b99;">
<th style="color: #fff;"> <strong>Example </strong></th>
</tr>
<tr>
<td>Take the collection of sets and the mappings between sets. This is a category where the mappings are the morphisms (morphings) and it&#8217;s often denoted with <strong>Set</strong> (in bold). An endofunctor F here is, for example, a mapping into itself which maps a set, say S, to the set of subsets:</p>
<p><img src='http://s0.wp.com/latex.php?latex=F%3A+S+%5Crightarrow+2%5ES+&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='F: S &#92;rightarrow 2^S ' title='F: S &#92;rightarrow 2^S ' class='latex' /></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<table style="border: 1px solid #6B8B99; float: none; width: 90%;">
<tbody>
<tr style="background-color: #6b8b99;">
<th style="color: #fff;"> <strong>Example </strong></th>
</tr>
<tr>
<td>Take the real number (floats, if you prefer) with an operation called &#8216;addition&#8217;. Take the same real number with an operation called &#8216;multiplication&#8217;. We call them objects in the category of &#8216;real numbers with an operation&#8217;. A morphism f between the two objects is<br />
<img src='http://s0.wp.com/latex.php?latex=f%3A+%5Cmathbb%7BR%7D%2C%2B+%5Crightarrow+%5Cmathbb%7BR%7D%2C+x+%5Cmapsto+e%5Ex.&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='f: &#92;mathbb{R},+ &#92;rightarrow &#92;mathbb{R}, x &#92;mapsto e^x.' title='f: &#92;mathbb{R},+ &#92;rightarrow &#92;mathbb{R}, x &#92;mapsto e^x.' class='latex' /><br />
This morphism has the amazing property that:<br />
<img src='http://s0.wp.com/latex.php?latex=f%28x%2By%29+%3D+f%28x%29.f%28y%29.&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='f(x+y) = f(x).f(y).' title='f(x+y) = f(x).f(y).' class='latex' /><br />
and this property is shared with many other types of collections. In addition, you should also notice that zero is mapped to one and that zero is the identity (null element) for the addition while the number one is the identity for the multiplication.</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<table style="border: 1px solid #6B8B99; float: none; width: 90%;">
<tbody>
<tr style="background-color: #6b8b99;">
<th style="color: #fff;"> <strong>Example </strong></th>
</tr>
<tr>
<td>Take the collection of lists of strings (List in C# and &#8216;string List&#8217; in F#) and consider the morphism called &#8216;concatenation&#8217; between lists. Take the collection of lists of integers on the other hand and with the same concatenation of of lists as with the string collection. A functor here is the <strong>List.map</strong> operation of F# and you should note that it doesn&#8217;t matter whether you concatenate things first and then apply the map or first apply the map and then concatenate things. In a mathematical fashion you can write that if F is the List.map action and f, g are morphisms then<br />
<img src='http://s0.wp.com/latex.php?latex=F%28f.g%29+%3D+F%28f%29.F%28g%29+&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='F(f.g) = F(f).F(g) ' title='F(f.g) = F(f).F(g) ' class='latex' /></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<table style="border: 1px solid #6B8B99; float: none; width: 90%;">
<tbody>
<tr style="background-color: #6b8b99;">
<th style="color: #fff;"> <strong>Example </strong></th>
</tr>
<tr>
<td>The collection of data types in .Net is a category so is the collection of data types in Haskell (usually denoted by <strong>Hask</strong>). Let&#8217;s call this category <strong>Net</strong>. There is a useful lifting procedure or amplifier inside the collection; take any type and map it to a collection of this same type:</p>
<p><img src='http://s0.wp.com/latex.php?latex=Amp%3A+T+%5Cmapsto+List%3C+T%3E&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='Amp: T &#92;mapsto List&lt; T&gt;' title='Amp: T &#92;mapsto List&lt; T&gt;' class='latex' /></p>
<p>Note that you can iterate this, even though in practice you won&#8217;t often see a List&lt;List&lt;List&lt;T&gt;&gt;&gt; type defined in an application. The things is, this is an endofunctor of <strong>the category Net </strong>into <strong>Net</strong>.</td>
</tr>
</tbody>
</table>
<h2>Some simple list manipulations</h2>
<p>There are some well-know examples of categories and monads like the so-called maybe monad, the identity monad and the state monad but I think the easiest way to explore or check the ideas is by means of the <strong>concat monad</strong> over the category of lists of integers, called from here on <strong>List</strong> (in bold). It consists of the following:</p>
<ul>
<li><strong>sets and subsets of integers</strong>: [], [1], [1;2;3], [[1], [5;6]]&#8230;are examples of objects in this category</li>
<li>the <strong>return operation</strong> which returns a singleton set from any given element<br />
<img src='http://s0.wp.com/latex.php?latex=return%3A+x+%5Cmapsto+%5Bx%5D&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='return: x &#92;mapsto [x]' title='return: x &#92;mapsto [x]' class='latex' /></li>
<li>the <strong>identity operator</strong> which returns whatever is given:<br />
<img src='http://s0.wp.com/latex.php?latex=id%3A+x+%5Cmapsto+x+%5Cnewline++id%3A+%5Bx%5D%5Cmapsto+%5Bx%5D++&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='id: x &#92;mapsto x &#92;newline  id: [x]&#92;mapsto [x]  ' title='id: x &#92;mapsto x &#92;newline  id: [x]&#92;mapsto [x]  ' class='latex' /></li>
<li>the<strong> concatenation operator</strong> which concatenates list:<br />
<img src='http://s0.wp.com/latex.php?latex=concat%3A+%5B%5Ba%5D%2C%5Bb%5D%5D+%5Cmapsto+%5Ba%2Cb%5D&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='concat: [[a],[b]] &#92;mapsto [a,b]' title='concat: [[a],[b]] &#92;mapsto [a,b]' class='latex' /></li>
</ul>
<p>Now, you can play with these operations and discover some interesting properties like the fact that:</p>
<table style="border: 1px solid #A7BD5B; float: none; width: 90%;">
<tbody>
<tr style="background-color: #a7bd5b; color: #fff;">
<th> Property</th>
</tr>
<tr>
<td><img src='http://s0.wp.com/latex.php?latex=concat+.+concat+%3D+concat+.+%28List.map%29%28concat%29&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='concat . concat = concat . (List.map)(concat)' title='concat . concat = concat . (List.map)(concat)' class='latex' /></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>Indeed if you take an arbitrary set, say [[[a];[b]];[[c;d];[[f]]], an apply the left hand side you get:</p>
<p><img src='http://s0.wp.com/latex.php?latex=concat%28concat%28%5B%5B%5Ba%5D%3B%5Bb%5D%5D%3B%5B%5Bc%3Bd%5D%3B%5B%5Bf%5D%5D%5D%29%29%5Cnewline++%3D+concat%28+%5B%5Ba%5D%3B%5Bb%5D%3B%5Bc%3Bd%5D%3B%5Bf%5D%5D%29%5Cnewline++%3D+%5Ba%3Bb%3Bc%3Bd%3Bf%5D++&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='concat(concat([[[a];[b]];[[c;d];[[f]]]))&#92;newline  = concat( [[a];[b];[c;d];[f]])&#92;newline  = [a;b;c;d;f]  ' title='concat(concat([[[a];[b]];[[c;d];[[f]]]))&#92;newline  = concat( [[a];[b];[c;d];[f]])&#92;newline  = [a;b;c;d;f]  ' class='latex' /><br />
The other side of the equation tells you that:</p>
<p><img src='http://s0.wp.com/latex.php?latex=concat+.+%28List.map%29%28concat%29%28%5B%5B%5Ba%5D%3B%5Bb%5D%5D%3B%5B%5Bc%3Bd%5D%3B%5B%5Bf%5D%5D%5D%29%5Cnewline++%3D+concat%28+%5B%5Ba%3Bb%5D%3B%5Bc%3Bd%3Bf%5D%5D%29%5Cnewline++%3D+%5Ba%3Bb%3Bc%3Bd%3Bf%5D++&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='concat . (List.map)(concat)([[[a];[b]];[[c;d];[[f]]])&#92;newline  = concat( [[a;b];[c;d;f]])&#92;newline  = [a;b;c;d;f]  ' title='concat . (List.map)(concat)([[[a];[b]];[[c;d];[[f]]])&#92;newline  = concat( [[a;b];[c;d;f]])&#92;newline  = [a;b;c;d;f]  ' class='latex' /></p>
<p>In a similar fashion you can show that</p>
<table style="border: 1px solid #A7BD5B; float: none; width: 90%;">
<tbody>
<tr style="background-color: #a7bd5b; color: #fff;">
<th> Property</th>
</tr>
<tr>
<td><img src='http://s0.wp.com/latex.php?latex=concat.+return+%3D+id%3D+concat.%28List.map%29%28return%29++&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='concat. return = id= concat.(List.map)(return)  ' title='concat. return = id= concat.(List.map)(return)  ' class='latex' /></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>Now, let&#8217;s introduce a *-operator which acts on morphisms f in the List category:<br />
<img src='http://s0.wp.com/latex.php?latex=f%5E%2A+%3D+concat.%28List.map%29%28f%29+&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='f^* = concat.(List.map)(f) ' title='f^* = concat.(List.map)(f) ' class='latex' /></p>
<p>This star-operator has also some interesting features which immediately generalize to arbitrary monads later on, namely:</p>
<table style="border: 1px solid #A7BD5B; float: none; width: 90%;">
<tbody>
<tr style="background-color: #a7bd5b; color: #fff;">
<th> Property</th>
</tr>
<tr>
<td><img src='http://s0.wp.com/latex.php?latex=return%5E%2A%3D+id%5Cnewline++f%5E%2A+.+return+%3D+f%5Cnewline++g%5E%2A.f%5E%2A%3D%28g%5E%2A.f%29%5E%2A+&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='return^*= id&#92;newline  f^* . return = f&#92;newline  g^*.f^*=(g^*.f)^* ' title='return^*= id&#92;newline  f^* . return = f&#92;newline  g^*.f^*=(g^*.f)^* ' class='latex' /></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>All these things are really easy to show inside this List category but have a deeper meaning on a general level.</p>
<h2>Some simple maths</h2>
<p>At this point you are ready to receive the general definition of a category and a monad.</p>
<table style="border: 1px solid #D94D23; float: none; width: 90%;">
<tbody>
<tr style="background-color: #d94d23;">
<th style="color: #fff;"> <strong>Definition</strong></th>
</tr>
<tr>
<td>A <strong>category </strong>consists of three things (aka triplet):</p>
<ul>
<li>a collection of <strong>objects</strong></li>
<li>a collection of <strong>morphisms </strong>f between the objects</li>
<li>a <strong>binary operation</strong> (which tunrs the structure in a so-called monoid) inside the collection such that<br />
<img src='http://s0.wp.com/latex.php?latex=f.%28g.h%29+%3D+%28f.g%29.h+&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='f.(g.h) = (f.g).h ' title='f.(g.h) = (f.g).h ' class='latex' /><br />
and an identity which keeps things identitical<br />
<img src='http://s0.wp.com/latex.php?latex=id.f+%3D+f.id+%3D+f+&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='id.f = f.id = f ' title='id.f = f.id = f ' class='latex' /></li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>The only difference here with the pedestrian definition above is that morphism need to satisfy the associative constraint and the existence of an identity.</p>
<table style="border: 1px solid #D94D23; float: none; width: 90%;">
<tbody>
<tr style="background-color: #d94d23;">
<th style="color: #fff;"> <strong>Definition</strong></th>
</tr>
<tr>
<td>A <strong>functor </strong>is a map F from one category to another which maps both objects and morphisms in such a way that<br />
<img src='http://s0.wp.com/latex.php?latex=F+.+id+%3D+id.F%5Cnewline++F%28f%29.F%28g%29+%3D+F%28f.g%29++++&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='F . id = id.F&#92;newline  F(f).F(g) = F(f.g)    ' title='F . id = id.F&#92;newline  F(f).F(g) = F(f.g)    ' class='latex' /></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>At this point you should see how this abstract definition corresponds to the various examples above, in particular the <strong>List </strong>category in the previous section. In the literature you will also see that the collection of morphisms between two objects A and B is denoted by <strong>Hom(A,B)</strong>, which is an abbreviation for &#8216;Hom-omorphisms&#8217;.</p>
<p>In the previous section you was that the List category of integers has some interesting features when you combine the (List.map), the return, the id and the concat operations. The general definition of a monad is simply expressing the fact that many structures have the same set of properties. Let me first rephrase it in this specific category;</p>
<p>The <strong>List </strong>category has</p>
<ul>
<li>an operation (actually an endofunctor) (List.map) which maps lists onto lists</li>
<li>the return operation creates a singleton from an element</li>
<li>the concat operation together with the return operation has the following properties:<br />
<img src='http://s0.wp.com/latex.php?latex=concat.+return+%3D+id%3D+concat.%28List.map%29%28return%29+%5Cnewline++concat+.+concat+%3D+concat+.+%28List.map%29%28concat%29++&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='concat. return = id= concat.(List.map)(return) &#92;newline  concat . concat = concat . (List.map)(concat)  ' title='concat. return = id= concat.(List.map)(return) &#92;newline  concat . concat = concat . (List.map)(concat)  ' class='latex' /></li>
</ul>
<p>and this turns the triple (List.map, return, concat) into a monad.</p>
<p>In general, <strong>a monad is a triple which satisfies the same set of properties and you&#8217;ll see instead of concat and return the following</strong>:</p>
<table style="border: 1px solid #D94D23; float: none; width: 90%;">
<tbody>
<tr style="background-color: #d94d23;">
<th style="color: #fff;"> <strong>Definition</strong></th>
</tr>
<tr>
<td>A monad is a category C together with&nbsp;</p>
<ul>
<li> an <strong>endofunctor </strong>T from C to C</li>
<li>a transformation <img src='http://s0.wp.com/latex.php?latex=%5Ceta%3A+C%5Crightarrow+T&#038;bg=ffffff&#038;fg=000&#038;s=0' alt='&#92;eta: C&#92;rightarrow T' title='&#92;eta: C&#92;rightarrow T' class='latex' /></li>
<li>a transformation <img src='http://s0.wp.com/latex.php?latex=%5Cmu%3A+T%5E2%5Cmapsto+T&#038;bg=ffffff&#038;fg=000&#038;s=0' alt='&#92;mu: T^2&#92;mapsto T' title='&#92;mu: T^2&#92;mapsto T' class='latex' /></li>
</ul>
<p>satisfying</p>
<p><img src='http://s0.wp.com/latex.php?latex=%5Cmu+.+%5Cmu+%3D+%5Cmu+.+T+%28%5Cmu%29%5Cnewline++%5Cmu+.+%5Ceta+%3D+id+%3D+%5Cmu+.+T%28%5Ceta%29++&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='&#92;mu . &#92;mu = &#92;mu . T (&#92;mu)&#92;newline  &#92;mu . &#92;eta = id = &#92;mu . T(&#92;eta)  ' title='&#92;mu . &#92;mu = &#92;mu . T (&#92;mu)&#92;newline  &#92;mu . &#92;eta = id = &#92;mu . T(&#92;eta)  ' class='latex' /></td>
</tr>
</tbody>
</table>
<p>Don&#8217;t be scared away by all this, it&#8217;s just an abstract reformulation of the simple concatenation and mappings of lists above! I do need however to push things a bit further because the way a monad is defined in maths is in general not the way you&#8217;ll recognize it in F# or Haskell. Usually you&#8217;ll see the equivalent formulation by Heinrich Kleisli who invented the Kleisli triple and which turned out to be an equivalent definition for monads. That is, it can be shown that there is a one-to-one correspondence between a Kleisli structure and a monad.</p>
<table style="border: 1px solid #D94D23; float: none; width: 90%;">
<tbody>
<tr style="background-color: #d94d23;">
<th style="color: #fff;"> <strong>Definition</strong></th>
</tr>
<tr>
<td>In this equivalent formulation a monad (or Kleisli triple if you prefer) is defined as a triple over a category consisting of&nbsp;</p>
<ul>
<li>an endofunctor T</li>
<li>a return operation <img src='http://s0.wp.com/latex.php?latex=%5Ceta%3A+C%5Crightarrow+T+&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='&#92;eta: C&#92;rightarrow T ' title='&#92;eta: C&#92;rightarrow T ' class='latex' /></li>
<li>a *-operator on morphisms which lifts a morphism</li>
</ul>
<p>satisfying:<br />
<img src='http://s0.wp.com/latex.php?latex=%5Ceta%5E%2A+%3D+id%5Cnewline++f%5E%2A+.%5Ceta+%3D+f%5Cnewline++g%5E%2A+.f%5E%2A+%3D+%28g%5E%2A+.+f%29%5E%2A++&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='&#92;eta^* = id&#92;newline  f^* .&#92;eta = f&#92;newline  g^* .f^* = (g^* . f)^*  ' title='&#92;eta^* = id&#92;newline  f^* .&#92;eta = f&#92;newline  g^* .f^* = (g^* . f)^*  ' class='latex' /></td>
</tr>
</tbody>
</table>
<p>and you should immediately recognize the properties we mentioned above in the context of the List category. The proof that the two definitions are leading to the same structure is not difficult but it wouldn&#8217;t be useful at this point (unless you are math oriented and you need to see it to understand it).</p>
<h2>Some simple F#</h2>
<p>The prototypical example in the context of F# workflows is the &#8216;dividing by zero&#8217; problem; one cannot divide by zero. If you have a computation where at some point an exception is raised (either explicitly or by the runtime) it doesn&#8217;t make sense to continue a computation but maybe you want to handle the exception.<br />
The way to handle this in F# is by using a label attached to a number; if the label says &#8216;failure&#8217; it doesn&#8217;t matter what the value is, if the label says &#8216;success&#8217; it makes sense to read the value. It&#8217;s a way to include infinity in the constraint range of values the runtime can handle:</p>
<div class="wp_codebox">
<table>
<tr id="p555422">
<td class="line_numbers">
<pre>1
</pre>
</td>
<td class="code" id="p5554code22">
<pre class="fsharp" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">type</span> Result <span style="color: #a52a2a;">=</span> Success <span style="color: #06c; font-weight: bold;">of</span> <span style="color: #06c; font-weight: bold;">float</span> | Infinite</pre>
</td>
</tr>
</table>
</div>
<p>Next, instead of the usual division you define a divide function like so</p>
<div class="wp_codebox">
<table>
<tr id="p555423">
<td class="line_numbers">
<pre>1
2
3
4
</pre>
</td>
<td class="code" id="p5554code23">
<pre class="fsharp" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">let</span> divide x y <span style="color: #a52a2a;">=</span>
  <span style="color: #06c; font-weight: bold;">match</span> y <span style="color: #06c; font-weight: bold;">with</span>
  | <span style="color: #c6c;">0.0</span> <span style="color: #a52a2a;">-&gt;</span> Infinite
  | _ <span style="color: #a52a2a;">-&gt;</span> Success<span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">/</span>y<span style="color: #6c6;">&#41;</span></pre>
</td>
</tr>
</table>
</div>
<p>and plug it into a class with two members:</p>
<div class="wp_codebox">
<table>
<tr id="p555424">
<td class="line_numbers">
<pre>1
2
3
4
5
6
</pre>
</td>
<td class="code" id="p5554code24">
<pre class="fsharp" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">type</span> SaveDivision<span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>
 <span style="color: #06c; font-weight: bold;">member</span> this Bind <span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#40;</span>x:Results<span style="color: #6c6;">&#41;</span>, <span style="color: #6c6;">&#40;</span>res: float<span style="color: #a52a2a;">-</span>&amp;gt<span style="color: #a52a2a;">;</span>Result<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span>
  <span style="color: #06c; font-weight: bold;">match</span> x <span style="color: #06c; font-weight: bold;">with</span>
  |Success<span style="color: #6c6;">&#40;</span>x<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">-&gt;</span> rest x
  | Infinite <span style="color: #a52a2a;">-&gt;</span> Infinite
 <span style="color: #06c; font-weight: bold;">member</span> this.<span style="color: #060;">Return</span> x <span style="color: #a52a2a;">=</span> x</pre>
</td>
</tr>
</table>
</div>
<p>At this point you can look at this as a class with two methods but what you really should see is that</p>
<ul>
<li>the map from a float number to the Result type is an amplifier or lift map as we described in the example above; it&#8217;s the endofunctor on the Net category of data types</li>
<li>the Bind operation corresponds to the *-operation of a Kleisli triple</li>
<li>the return corresponds to the eta-mapping of the Kleisli triple</li>
</ul>
<p>and it turns this triple into a monad! You should also reflect on the fact that the Bind member correspond to the concat operation on the List category (and similarly the return corresponds to the return member).</p>
<p>The way you use the SaveDivision monad in a concrete computation is by instantiating it and do something inside as follows:</p>
<div class="wp_codebox">
<table>
<tr id="p555425">
<td class="line_numbers">
<pre>1
2
3
4
5
6
7
</pre>
</td>
<td class="code" id="p5554code25">
<pre class="fsharp" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">let</span> safe <span style="color: #a52a2a;">=</span> SaveDivision<span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span>
<span style="color: #06c; font-weight: bold;">let</span> SomeCalculation x1 x2 <span style="color: #a52a2a;">=</span>
 safe <span style="color: #6c6;">&#123;</span>
    <span style="color: #06c; font-weight: bold;">let!</span> x <span style="color: #a52a2a;">=</span> divide <span style="color: #c6c;">1</span> x1
    <span style="color: #06c; font-weight: bold;">let!</span> y <span style="color: #a52a2a;">=</span> divide <span style="color: #c6c;">1</span> x2
    <span style="color: #06c; font-weight: bold;">return</span> divide x y
 <span style="color: #6c6;">&#125;</span></pre>
</td>
</tr>
</table>
</div>
<p>and as soon as you try this on zero you get the Infinite as an answer. Without this construction you would get an exception, obviously.The &#8216;bang&#8217; or exclamation mark after the let keywords is actually a way to tell F# that the Bind operation should be used instead of the normal assignment (or type inference).</p>
<p>So, this very simple example highlights the following:</p>
<ul>
<li><strong>monads in F# allows you to do &#8216;stuff&#8217; </strong>under the hood and redefine how things are being assigned or returned</li>
<li><strong>a monad in F# is really a Kleisli triple</strong> but because it&#8217;s mathematically equivalent to a monad there is no need to differentiate. I does confuse however when you try to compare the mathematical textbooks with the functional programming textbooks</li>
<li><strong>monads are often called workflows</strong> because it allows you do embed a whole logic or workflow inside the instantiated class (monad) and redefine how things like the assignment (let!) should function. You will also see that F# in fact has extended the notion of monad to include other monad members and in this fashion allowing you to redefine how for-next, try, yield and other statements are acting inside the monad.</li>
<li>monads are <strong>sometimes called computation expression</strong> since it redefines how assignments and expressions should act on (category) objects</li>
</ul>
<p>In Haskell you will not encounter the alternative names and the term monad is used uniformly.</p>
<p>The suspicious mind should by now worry about a lack of consistency and feel the need for more formal proofs that for example the constraints (or defining rules) of a Kleisli triple (aka monad) are satisfied. Indeed, is the SafeDivision monad with the specified operations satisfying the constraints? Well, yes, it does. But I will refrain to reproduce the proofs and am happy to forward you to someone you tediously wrote it all down; see <a title="Paul Abraham" href="http://paul-abraham.blogspot.com/">Paul Abraham&#8217;s blog </a>and <a title="Monads in F#" href="http://www.paul-abraham.com/MonadsInFSharp.doc">this document in particular wherein you&#8217;ll find the F# proofs</a>.</p>
<h2>A simple dictionary</h2>
<p>In Haskell the following type class is used to implement monads:</p>
<div class="wp_codebox">
<table>
<tr id="p555426">
<td class="line_numbers">
<pre>1
2
3
</pre>
</td>
<td class="code" id="p5554code26">
<pre class="fsharp" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">class</span> Monad m where
   <span style="color: #06c; font-weight: bold;">return</span> :: a →m a
   <span style="color: #6c6;">&#40;</span><span style="color: #a52a2a;">&gt;&gt;=</span><span style="color: #6c6;">&#41;</span> :: m a →<span style="color: #6c6;">&#40;</span>a →m b<span style="color: #6c6;">&#41;</span> →m b</pre>
</td>
</tr>
</table>
</div>
<p>You can immediately recognize the return and the bind methods but the symbol &#8216;&gt;&gt;=&#8217; is usually used instead of &#8216;bind&#8217;. Purists will tell you that the discussion around monads in Haskell is not complete without the topic of &#8216;comonads&#8217; and &#8216;cofunctors&#8217; but it will take a while before these discussions appear in the F# community. See <a href="http://blog.ezyang.com/2010/07/flipping-arrows-in-coburger-king/" target="_blank">this article</a> in case you want to know more about this.</p>
<p>In order to use monads in F# you only need to implement an (implicit) class which implements the Bind and Return member. The language extends the purely mathematical definition with eight extra methods:</p>
<table style="border: 1px solid #D94D23; float: none; width: 90%;">
<tbody>
<tr style="background-color: #d94d23;">
<th style="color: #fff;">Member</th>
<th width="50%">Description</th>
</tr>
<tr>
<td valign="top"><code>member <strong>Bind </strong>: M&lt;'a&gt; * ('a -&gt; M&lt;'b&gt;) -&gt; M&lt;'b&gt;</code></td>
<td valign="top"><span style="color: #ff6600;">Required member</span>. Converts <code>let!</code> and <code>do!</code> within the monad.</td>
</tr>
<tr>
<td valign="top"><code>member <strong>Return </strong>: 'a -&gt; M&lt;'a&gt;</code></td>
<td valign="top"><span style="color: #ff6600;">Required member.</span> Converts the <code>return</code> within the monad.</td>
</tr>
<tr>
<td valign="top"><code>member <strong>Delay </strong>: (unit -&gt; M&lt;'a&gt;) -&gt; M&lt;'a&gt;</code></td>
<td valign="top"><span style="color: #99cc00;">Optional</span>. Ensures that side effects within the monad are performed when expected.</td>
</tr>
<tr>
<td valign="top"><code>member <strong>Yield </strong>: 'a -&gt; M&lt;'a&gt;</code></td>
<td valign="top"><span style="color: #99cc00;">Optional</span>. Converts the <code>yield</code> within the monad.</td>
</tr>
<tr>
<td valign="top"><code>member <strong>For </strong>: seq&lt;'a&gt; * ('a -&gt; M&lt;'b&gt;) -&gt; M&lt;'b&gt;</code></td>
<td valign="top"><span style="color: #99cc00;">Optional</span>. Converts the <code>for ... do ...</code> within the monad.&nbsp; <code>M&lt;'b&gt;</code> can optionally be <code>M&lt;unit&gt;</code></td>
</tr>
<tr>
<td valign="top"><code>member <strong>While </strong>: (unit -&gt; bool) * M&lt;'a&gt; -&gt; M&lt;'a&gt;</code></td>
<td valign="top"><span style="color: #99cc00;">Optional</span>. Converts the <code>while ... do ...</code> within the monad. <code>M&lt;'b&gt;</code> can optionally be <code>M&lt;unit&gt;</code></td>
</tr>
<tr>
<td valign="top"><code>member <strong>Using </strong>: 'a * ('a -&gt; M&lt;'b&gt;) -&gt; M&lt;'b&gt; when 'a&nbsp;:&gt; IDisposable</code></td>
<td valign="top"><span style="color: #99cc00;">Optional</span>. Converts the <code>use</code> bindings within the monad.</td>
</tr>
<tr>
<td valign="top"><code>member <strong>Combine </strong>: M&lt;'a&gt; -&gt; M&lt;'a&gt; -&gt; M&lt;'a&gt;</code></td>
<td valign="top"><span style="color: #99cc00;">Optional</span>. Used to convert sequencing within the monad. The first <code>M&lt;'a&gt;</code> can optionally be <code>M&lt;unit&gt;</code></td>
</tr>
<tr>
<td valign="top"><code>member <strong>Zero </strong>: unit -&gt; M&lt;'a&gt;</code></td>
<td valign="top"><span style="color: #99cc00;">Optional</span>. Converts the empty <code>else</code> branches of <code>if/then</code> in the monad.</td>
</tr>
<tr>
<td valign="top"><code>member <strong>TryWith </strong>: M&lt;'a&gt; -&gt; M&lt;'a&gt; -&gt; M&lt;'a&gt;</code></td>
<td valign="top"><span style="color: #99cc00;">Optional</span>. Converts the <code>try/with</code> bindings of the monad.</td>
</tr>
<tr>
<td valign="top"><code>member <strong>TryFinally </strong>: M&lt;'a&gt; -&gt; M&lt;'a&gt; -&gt; M&lt;'a&gt;</code></td>
<td valign="top"><span style="color: #99cc00;">Optional</span>. Converts the <code>try/finally</code> bindings of the monad.</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<p>Finally, the following table should be helpful in case you wish to move back and forth between F#, Haskell and the math literature.</p>
<table style="border: 1px solid #A7BD5B; float: none; width: 90%;">
<tbody>
<tr style="background-color: #a7bd5b; color: #fff;">
<th> Haskell</th>
<th> F#</th>
<th> Math</th>
</tr>
<tr>
<td>return</td>
<td>return</td>
<td><img src='http://s0.wp.com/latex.php?latex=%5Ceta+&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='&#92;eta ' title='&#92;eta ' class='latex' /></td>
</tr>
<tr>
<td>&gt;&gt;==</td>
<td>bind</td>
<td>* operator</td>
</tr>
<tr>
<td>instantiate the Monad type class</td>
<td>implement at least the Return and Bind members</td>
<td>NA</td>
</tr>
<tr>
<td>Monad</td>
<td>Monad, Monad Expression, Workflow, Computation Expression</td>
<td>Monad with an equivalent definition through Kleisli triple.<br />
The defining structure in F# and Haskell is really a Kleisli triple but no distinction is made there between the two types.</td>
</tr>
<tr>
<td>NA</td>
<td>NA</td>
<td><img src='http://s0.wp.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=000&#038;s=1' alt='&#92;mu' title='&#92;mu' class='latex' /></td>
</tr>
<tr>
<td>functor through a type class definition</td>
<td>usually not mentioned</td>
<td>functor</td>
</tr>
<tr>
<td>function</td>
<td>function (&#8216;fun&#8217;)</td>
<td>morphism</td>
</tr>
<tr>
<td><strong>Hask </strong>category of Haskell data types</td>
<td><strong>Net </strong>category of data types</td>
<td>a wild zoo of categories; groups, topologies, graphs, differential geometries, quantum field theories and so on</td>
</tr>
<tr>
<td>composable functions</td>
<td>composable functions</td>
<td>binary operation and monoids</td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<h2>References</h2>
<ul>
<li><a title="Monad library for F# on GitHub" href="https://github.com/panesofglass/FSharp.Monad" target="_blank">F# monad library</a></li>
<li><a title="Haskell wiki" href="http://www.haskell.org/haskellwiki/Haskell" target="_blank">Haskell wiki</a></li>
<li><a title="Haskell categories" href="http://en.wikibooks.org/wiki/Haskell/Category_theory#Monads" target="_blank">Haskell category theory on Wikibooks</a></li>
<li><a title="Awesome Princess wiki book on F#" href="http://en.wikibooks.org/wiki/F_Sharp_Programming" target="_blank">F# programming on Wikibooks</a></li>
<li><a title="Try F#" href="http://www.tryfsharp.org/" target="_blank">Try F#; awesome online F# learning site</a></li>
<li><a title="nLab" href="http://ncatlab.org/nlab/show/HomePage" target="_blank">nLab on everything categories in mathematical physics</a></li>
</ul>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://visualizationtools.net/default/categories-for-dummies/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>SharePoint Workspace (sync) unable to connect</title>
		<link>http://visualizationtools.net/default/sharepoint-workspace-sync-unable-to-connect/</link>
		<comments>http://visualizationtools.net/default/sharepoint-workspace-sync-unable-to-connect/#comments</comments>
		<pubDate>Wed, 29 Jun 2011 14:32:47 +0000</pubDate>
		<dc:creator>Francois Vanderseypen</dc:creator>
				<category><![CDATA[SharePoint]]></category>

		<guid isPermaLink="false">http://visualizationtools.net/default/sharepoint-workspace-sync-unable-to-connect/</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>Typically when you install a fresh SharePoint 2010 Enterprise and a user starts to connect to it through SharePoint WorkSpace/Groove he will receive the error:</p>
<p><em><font color="#ff0000">SharePoint Workspace was unable to interpret the SharePoint location. Please check and ensure the location contains no typing errors. </font></em></p>
<p>The solution to this consists in adding <strong>a public facing URL</strong> and making sure the server has <strong>remote differential compression installed</strong>/enabled.</p>
<p>Go to <strong>Server Manager &gt; Features </strong>and take a look at whether the remote differential compression is enabled. If not, check and install.</p>
<p><a href="http://visualizationtools.net/default/wp-content/uploads/2011/06/image6.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://visualizationtools.net/default/wp-content/uploads/2011/06/image_thumb6.png" width="451" height="337" /></a></p>
<p>Next go to <strong>SharePoint Central Administration &gt; Alternate Access Mappings</strong> and click the ‘<strong>Edit public URL&#8217;</strong> . Once there, make sure that besides the default internal URL there is also a public facing URL through which users can sync via the Workspace application.</p>
<p><a href="http://visualizationtools.net/default/wp-content/uploads/2011/06/image7.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://visualizationtools.net/default/wp-content/uploads/2011/06/image_thumb7.png" width="462" height="178" /></a></p>
<p>&#160;</p>
<p>Like most issues in SharePoint, fairly simple to fix but often a whole research before figuring out what is going on.</p>
]]></content:encoded>
			<wfw:commentRss>http://visualizationtools.net/default/sharepoint-workspace-sync-unable-to-connect/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Particle systems and Silverlight</title>
		<link>http://visualizationtools.net/default/silverlight-particle-systems/</link>
		<comments>http://visualizationtools.net/default/silverlight-particle-systems/#comments</comments>
		<pubDate>Sun, 26 Jun 2011 17:17:55 +0000</pubDate>
		<dc:creator>Francois Vanderseypen</dc:creator>
				<category><![CDATA[Data Visualization]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[data-visualization]]></category>
		<category><![CDATA[Layout algorithms]]></category>

		<guid isPermaLink="false">http://visualizationtools.net/default/?p=5515</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<div class="image-wrap portfolio"><a href="/default/wp-content/uploads/2011/06/image5.png" rel="prettyPhoto" title="Sample particle system"><span class="zoom"></span>
<img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="image" src="/default/wp-content/uploads/2011/06/image_thumb5.png" border="0" alt="image" width="530" height="368" />
</a></div><p style="clear:both;">&nbsp;</p><p style="clear:left;">&nbsp;</p>
<p>Recently a customer requested a particle simulation with the intention to investigate correlations and the effect of multiple ‘attractors’. The real, underlying issue in this demand is the fact that certain situations and systems are governed by laws (differential equations) which cannot be solved exactly and require a dynamic system to search for critical coefficients. These coefficients result in (semi) stable configurations or highlight boundary conditions inside which things make sense.</p>
<p>The problem with these kind of systems is the scalability which has two aspects; <strong>the visualization</strong> which doesn’t scale well with the retained graphics pipeline of WPF/SL/WP7 on the one hand&nbsp; and the O(N*N) <strong>calculation of interactions between particles</strong>.</p>
<p>Regarding the visuals there is really a lot to consider:</p>
<ul>
<li>if you inherit from a baseclass like FrameworkElement or Control or even UserControl you carry around with each and every particle a whole lot of stuff; data binding, styling, event and other infrastructure. While all these things are really great in an LOB, one doesn’t usually need all this when dealing with (lots of) particles</li>
<li>typically the underlying surface needs to be a Canvas but this element also brings along many gears; the retained rendering system, the measure/layout mechanics, styling and more. Maybe the bonus you miss when bypassing the Canvas is the ability to easily integrate zoom and pan in an application.</li>
<li>how much user-interaction is required, are mouse-events necessary or is it a ‘let it run’ system? Here as well, you need to consider carefully the implications (especially related to the scalability of the app).</li>
</ul>
<p>With respect to the simulation or calculation part things are more straightforward but you also need to think about</p>
<ul>
<li>the possibility to use F# for the more mathematically oriented parts. The disadvantage is however the need to constraint your model to make sure things talk to C#, or you have to go the full nine yards in F# (talking to XAML is still a disaster in this case).</li>
<li>how to optimize the calculations and to make sure that inside loops you break early and branch as much as possible</li>
<li>dropping unnecessary properties of the particles and make the particle system as lightweight as possible</li>
</ul>
<p>Within the Silverlight/WP context my experience is that if you use the Canvas/UIElement combination you easily hit a wall around <strong>500 particles</strong> due to the way the rendering engine is functioning and the fact that the UIElement (or FrameworkElement) are too ‘heavy’. The inter-particle calculation will still run OK with 500 particles but the end-result is that it’s not a good solution altogether.</p>
<p>One can keep the Canvas as underlying surface and render particles as inherited Shape items but this doesn’t push the boundary much.</p>
<p>A step further is to replace the Canvas with a bitmap and use direct pixel printing. This is easily done with the <a href="http://writeablebitmapex.codeplex.com/" target="_blank">WriteableBitmapEx</a> project you can find on CodePlex. This boosts your rendering to <strong>5000 particles</strong> if you drop interactions. However, if you switch on inter-particle forces you hit a calculational wall around <strong>1500 particles</strong> which is still way better than the retained rendering solution but still somewhat underwhelming.</p>
<p>So, as a next step it would be great to use parallel processing for the algorithmic part but unfortunately there is no task parallel processing library (TPL) for Silverlight (though somewhat made an unofficial port). Another possibility would be to use MailboxProcessor of F# but it would entail threading issues and an altogether more difficult system.</p>
<p>Various samples and libraries are available for XNA and Windows Phone which allow one to create particle effects but</p>
<ul>
<li>the often use sprites or similar techniques to minimize the particle count. The results are usually very good but the aim in the context of games is usually to create a certain effect rather than to give a physically correct simulation.</li>
<li>they often do not exhibit inter-particle forces. If they do the particle count is low.</li>
<li>they focus on effects and not physics</li>
</ul>
<p>See also the Flint-sharp library which is a port of the Flash/Flex library.</p>
<p>The conclusion is that one can perform simulations and develop particle system in Silverlight if the need is not above, say, a thousand particles. Above this threshold things become more difficult and Silverlight as a medium becomes less suitable for this type of task. Obviously there are high-tech solutions on the basis of C++ and GPU instructions but this was outside my client’s scope (and my technical know-how as well).</p>
<div class="bluebox">
<a href="/downloads/stuff/SilverlightParticles.zip">Attached is simple and fun particle system on the basis of WriteableBitmapEx and lightweight particles.</a>
</div>
<p><strong>Note</strong>: on different level you need to note in the attached simulation sample that</p>
<ul>
<li>the average speed varies a lot while the amount of interacting particles stays the same</li>
<li>the <strong>ergodic phenomenon</strong> if easily shown; that the average velocity over the sample equals the average of one item in time</li>
<li>it&#8217;s quite difficult to create stable configurations; which mimics well how the real world is also constantly influenced by quantum fluctuations</li>
</ul>
<h2>References</h2>
<ul>
<li><a href="http://www.damonpayne.com/post/2011/02/07/Task-Parallel-Library-Ported-to-Silverlight-4.aspx" target="_blank">TPL library ported to Silverlight 4</a></li>
<li><a href="http://create.msdn.com/en-US/education/catalog/sample/particle" target="_blank">Particle system for games (XNA &amp; WP)</a></li>
<li><a href="http://code.google.com/p/flint-sharp/" target="_blank">Flint for C#</a></li>
<li><a href="http://writeablebitmapex.codeplex.com/" target="_blank">WriteableBitmapEx on CodePlex</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://visualizationtools.net/default/silverlight-particle-systems/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cannot connect to WMI provider. You do not have permission or the server is unreachable.</title>
		<link>http://visualizationtools.net/default/wmi-provider-error/</link>
		<comments>http://visualizationtools.net/default/wmi-provider-error/#comments</comments>
		<pubDate>Fri, 24 Jun 2011 04:41:43 +0000</pubDate>
		<dc:creator>Francois Vanderseypen</dc:creator>
				<category><![CDATA[Business Inteliigence]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[BI]]></category>

		<guid isPermaLink="false">http://visualizationtools.net/default/?p=5508</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p>During upgrade or installation of some SQL Server packages you might encounter the error “<span style="color: #ff0000;">Cannot connect to WMI provider. You do not have permission or the server is unreachable</span>”. In particular, during the setup of <a href="http://msftdbprodsamples.codeplex.com/" target="_blank">the AdventureWorks sample databases</a> this often happens</p>
<p><a href="http://visualizationtools.net/default/wp-content/uploads/2011/06/image3.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;" title="image" src="http://visualizationtools.net/default/wp-content/uploads/2011/06/image_thumb3.png" border="0" alt="image" width="244" height="155" /></a></p>
<p>Most probably, if you receive this error you also will get it when opening the SQL Server Configuration tool with a message stating</p>
<p><a href="http://visualizationtools.net/default/wp-content/uploads/2011/06/image4.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;" title="image" src="http://visualizationtools.net/default/wp-content/uploads/2011/06/image_thumb4.png" border="0" alt="image" width="301" height="125" /></a></p>
<p>&nbsp;</p>
<p>It seems this happens when you run a 64bit version of SQL Server and have removed a 32bit one earlier. Both versions share the same WMI configuration and, hence, things get lost when uninstalling some stuff. In this Microsoft Support report you can find some background where the essential bit is:</p>
<p><strong>mofcomp &#8220;%programfiles(x86)%\Microsoft\Microsoft SQL Server\100\Shared\sqlmgmproviderxpsp2up.mof&#8221;</strong></p>
<p>(run this in an elevated  CMD) as well as reconfiguring the server to enable file streaming.</p>
<p><strong>EXEC sp_configure filestream_access_level, 1<br />
</strong><strong>RECONFIGURE</strong></p>
<p>(run this as a SQL query).</p>
<p>Lo and behold, if you go through this you can move on and use SQL Server WMI again. If you&#8217;re curious about <em>mofcomp</em>, it&#8217;s the <a href="http://msdn.microsoft.com/en-us/library/aa392389%28v=vs.85%29.aspx" target="_blank">managed object format compiler parser</a> but very little info is available about this tool and it seems to me that it&#8217;s one of those things (like the registry) you better handle with silk gloves.</p>
]]></content:encoded>
			<wfw:commentRss>http://visualizationtools.net/default/wmi-provider-error/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  visualizationtools.net/default/feed/ ) in 2.90543 seconds, on May 19th, 2012 at 10:18 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on May 19th, 2012 at 11:18 am UTC -->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- Quick Cache Is Fully Functional :-) ... A Quick Cache file was just served for (  visualizationtools.net/default/feed/ ) in 0.00226 seconds, on May 19th, 2012 at 10:38 am UTC. -->
