Computer Science Experimentation

Friday, November 23, 2012

Using NoSQL/Redis in Windows with F#


Celso Axelrud 
11/23/2012 
 

(Also available as Skydrive MSWord at https://skydrive.live.com/#!/edit.aspx?cid=BDC87EF39B001785&resid=BDC87EF39B001785%211518&app=Word&nd=1)

 
This document reports results related to testing Redis in Windows with F#.  
 
Redis for Windows is available for download from Microsoft Open Technologies Inc. at  https://github.com/MSOpenTech.  
 
The Redis server was started using redis-server.exe at a command prompt window. 
 
The tests were performed using F# interactive inside Visual Studio Express 2012 for Web. 
 
The Redis .NET driver used was from ServiceStack (https://github.com/ServiceStack/ServiceStack.Redis ) and downloaded from https://github.com/ServiceStack/ServiceStack.Redis/downloads . 
 
The following script shows the tests:
 
// Redis - example from 
//C:\opentech\ServiceStack.Redis-master\ServiceStack.Redis-master\tests\ServiceStack.Redis.Tests\Examples\TodoApp.cs 
// System 
open System 
open System.Collections.Generic 
#r "C:\opentech\ServiceStack.Redis-v3.9.28\ServiceStack.Common.dll" 
#r "C:\opentech\ServiceStack.Redis-v3.9.28\ServiceStack.Interfaces.dll" 
#r "C:\opentech\ServiceStack.Redis-v3.9.28\ServiceStack.Text.dll" 
#r "C:\opentech\ServiceStack.Redis-v3.9.28\ServiceStack.Redis.dll" 
open ServiceStack.Common.Extensions 
open ServiceStack.Text 
open ServiceStack.Redis 
  
type Todo= {mutable Id:int64; mutable Content:string;mutable Order:int;mutable Done:bool} 
let redisClient = new RedisClient("localhost") //6379 
redisClient.FlushAll();; 
let redisTodos = redisClient.As<Todo>();; 
(*> val redisTodos : Generic.IRedisTypedClient<Todo> *) 
let todo= {Id=redisTodos.GetNextSequence();Content = "Learn Redis";Order = 1;Done=false};; 
(*> val todo : Todo = {Id = 1L; 
Content = "Learn Redis"; 
Order = 1; 
Done = false;} *) 
redisTodos.Store(todo);; 
let savedTodo = redisTodos.GetById(todo.Id);; 
(*> val savedTodo : Todo = {Id = 1L; 
Content = "Learn Redis"; 
Order = 1; 
Done = false;} *) 
let allTodos = redisTodos.GetAll();; 
assert(allTodos.Count=1);; 
[for i in allTodos -> i.Content];; 
(*> val it : string list = ["Learn Redis"] *) 
savedTodo.Done <- span="span">true;; 
redisTodos.Store(savedTodo);; 
let savedTodo2 = redisTodos.GetById(todo.Id);; 
(*> val savedTodo2 : Todo = {Id = 1L; 
Content = "Learn Redis"; 
Order = 1; 
Done = true;} *) 
redisTodos.DeleteById(savedTodo.Id);; 
let allTodos2 = redisTodos.GetAll() 
assert(allTodos2.Count=0) 
 
 
 

Monday, September 3, 2012

Three-tier web application with F# and javascript

In the previous posts, I described several examples of services using F# with WCF.
 In this post, and in a series of future ones, I will describe a high efficient web application technology using three-tier architecture: host server, database server and web browser interface.

The software resources used in these posts are:

·        Host server:

o   F# language

o   Windows Communication Foundation (WCF) – services oriented application framework

·        Database server:

o   SQL Server Compact – database engine

o   Entity Framework – object relational mapper

o   F# type providers – information rich application component

·        Web client:

o   HTML5 – web markup language

o   javascript – web scripting language

o   jquery – javascript library

o   jqWidgets – web user interface library

o   knockoutjs –model-view-view-model javascript library

In my opinion, these set of software technologies represent the state-of-art available for web applications. The advantages are:

·        All software, except the jqWidgets and knockoutjs, are supplied by Microsoft

·        Microsoft Visual Studio is the only development tool required

·        All software, except jqWidgets, are free if you own the Window Operation System

·        Host server has high performance due to compiled code and the use of REST and Json

·        Application can be self-hosted or part of a server application as IIS

·        No SQL commands are needed to access the database

·        Client is available in all devices that support HTML5

·        Several components are used to hide complexity resulting in application with few lines of code


A user authentication web application is presented as an example.
Check the full article including source code at:
 




Friday, August 10, 2012

HTML5 Web Workers- Agent based computantion for the Web Browser


This post presents an example using HTML5 Web Workers.

The Web Workers are very similar to the Agent based computation presented in previous posts:

·        Agent based example using F# MailboxProcessor


·        Real-time Computation Engine with F#


Example:


This example use one HTML5 main page and one Web Worker.

The example consists of two parts:

1.      Non-state-based request/reply messages to the Web Worker:

Sum: send a message with two values and receive back the sum of them

2.      State-based interaction with the Web Worker:

Check State: receives from the Web Worker its state (idle, active)

Go to Active: starts in the Web Worker a periodic procedure every 15 sec that generate two random numbers and keeps an accumulation of them

Clear Accumulation

Go to Idle: stops the periodic procedure in the Web Worker 

Check the full article at:
https://skydrive.live.com/?cid=BDC87EF39B001785&id=BDC87EF39B001785%21110#!/view.aspx?cid=BDC87EF39B001785&resid=BDC87EF39B001785%21730



Chat Application using Web Sockets Self-Hosted Server with F#

This post presents an Chat application in F# using the library:

The Chat application is based on Paul Batum example at:
The server is self-hosted.
I tried to make the application to be hosted in F# interactive console by making the configuration programmatically without success. Because of that, the F# application is compiled in order to use the App.cfg .

Using System.Json in F#


The System.Json library is only available for Silverligh environment.

A beta version for .NET can be obtained from Nuget (http://nuget.org/packages/System.Json).

This is the documentation at MSDN:
(http://msdn.microsoft.com/en-us/library/system.json(v=vs.95).aspx)

The System.Json namespace provides standards-based support for the serialization of JavaScript Object Notation (JSON).

JSON is a lightweight, text-based data interchange format that is used to serialize structured data for transmission over networks. It has three primitive data types: string, number, and Boolean. It has two data structures: array and object. An array is an ordered collection of values, where the value can be a JSON primitive, object or array. An object is an unordered set of key/value pairs. The key is a string and the value, as with the array, can be a JSON primitive, object, or array.

In this documentation, the serialized textual representation of JSON is referred to as “text-based JSON” to distinguish it from the deserialized representation of JSON as a Common Language Runtime (CLR) type, which is referred to here as a “JSON CLR type” or as a “JSON CLR object” if we are referring to an instance of the type.

JSON CLR types represent the three text-based JSON primitives with the JsonPrimitive type. The JsonType() property indicates which primitive is contained in the type instance: String, Number, or Boolean.

JSON CLR types represent the two text-based structured types with JsonArray and JsonObject. A JsonArray is an ordered sequence of zero or more JsonValue objects. A JsonValue is the JSON CLR representation of a JSON text-based value, which can be either a primitive or structured type. A JsonObject is an unordered collection of zero or more String/JsonValue pairs, which represent the key/value pairs of the text-based JSON object.

F# Example:

//Using System.Json.dll
//By Celso Axelrud on 8/10/2012

open System
open System.Collections.Generic

#r @"C:\Users\caxelrud\Documents\Visual Studio 2012\Projects\WebSck_Host_2\packages\System.Json.4.0.20126.16343\lib\net40\System.Json.dll"
open System.Json

let j="[{\"a\":\"foo\",\"b\":\"bar\"},{\"a\":\"another foo\",\"b\":\"another bar\",\"c\":100}]"
let jsonObj = JsonValue.Parse(j)

jsonObj.Count;;
(*> val it : int = 2 *)

jsonObj.[0].["a"].ReadAs<string>()
(*> val it : string = "foo" *)

jsonObj.[1].["c"].ReadAs<int>();;
(*> val it : int = 100 *)

//New Json object
let jsonNew = new JsonObject(new KeyValuePair<string,JsonValue>("mainValue", JsonPrimitive(12345)))

jsonNew.ToString()
(*> val it : string = "{"mainValue":12345}" *)