Archive for the 'Programming' category

The Accord.NET Framework

31 May 2012 by

Accord.NET Framework

Accord.NET Framework

Accord.NET Framework is one among the best open source .NET framework written in C# for researcher in scientific computing. Here is the description of what containing in this framework. For more information please visit: http://code.google.com/p/accord/

Scientific Computing

  • Accord.Math – Contains a matrix extension library, along with a suite of numerical matrix decomposition methods, numerical optimization algorithms for constrained and unconstrained problems, special functions and other tools for scientific applications.
  • Accord.Statistics – Probability distributions, statistical models and methods such as Linear and Logistic regression, Hidden Markov Models, (Hidden) Conditional Random Fields, Principal Component Analysis, Partial Least Squares, Discriminant Analysis, Kernel methods and functions and many other related techniques.
  • Accord.MachineLearning – Support Vector Machines, Decision Trees, Naive Bayesian models, K-means, Gaussian Mixture models and general algorithms such as Ransac, Cross-validation and Grid-Search for machine-learning applications.
  • Accord.Neuro – Neural learning algorithms such as Levenberg-Marquardt, Parallel Resilient Backpropagation, initialization procedures such as Nguyen-Widrow and other neural network related methods.

Signal and Image Processing

  • Accord.Imaging – Interest point detectors (Harris, SURF and FAST), image matching and image stitching methods
  • Accord.Audio – Process, transforms, filters and handle audio signals for machine learning and statistical applications.
  • Accord.Vision – Real-time face detection and tracking, as well as general methods for detecting, tracking and transforming objects in image streams. Contains cascade definitions, Camshift and Dynamic Template Matching trackers.Support Libraries
  • Accord.Controls – Histograms, scatterplots and tabular data viewers for scientific applications.
  • Accord.Controls.Imaging – Windows Forms controls to show and handle images. Contains a convenient ImageBox control which mimics the traditional MessageBox behavior for quickly displaying or inspecting images.
  • Accord.Controls.Audio – Windows Forms controls to display waveforms and audio-related information.
  • Accord.Controls.Vision – Windows Forms components and controls to track head, face and hand movements and other computer vision related tasks.

No responses yet

Example of ClickOnce Deployment

20 May 2012 by

Without any tool, deployment of desktop application is really painful. In the old day, developers need to write an auto-updated application by themselves. However, now ClickOnce, Microsoft product, could leverage this task easily. ClickOnce allows user to setup and run program directly from a webpage. The deployment files will be published to the a friendly URL and then access at the client computer. There is an option for client to check update every program run or any point in the program. This technique is very similar to Java Web Start.

I will show how to achieve it in this demo.

 1. Architecture

ClickOnce Architecture

2. Source Code

I just want to show a snapshot of code in LoginForm and UpdateForm.

LoginForm

using System;
using System.Deployment.Application;
using System.Windows.Forms;

namespace RVD.Win.View
{
  public partial class LoginForm : Form
  {
    public LoginForm()
    {
      InitializeComponent();
    }

    private void LoginForm_Load(object sender, EventArgs e)
    {
      CheckForUpdate();
    }

    private void CheckForUpdate()
    {
      var appDeploy = ApplicationDeployment.CurrentDeployment;

      //Check for update
      var update = appDeploy.CheckForDetailedUpdate();
      if(appDeploy.CheckForUpdate())
      {
        MessageBox.Show(@"The system needs to be updated to version: " + update.AvailableVersion, @"Update",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
        var deployForm = new AppDeployPopup {AppDeploy = appDeploy, IsUpdateAvailable = true};
        deployForm.ShowDialog();

        if (deployForm.ShouldAppRestart)
        {
          Visible = false;
          Close();  
          Application.Restart(); 
        }
      }
    }
  }
}

UpdateForm here, I would call AppDeployPopup. We are invoking
UpdateAsync()
method to update the software asynchronously. This method support two events called AsyncCompletedEvent and DeploymentProgressChangedEvent. We will use this event to show downloading progress in the form as well as closing the form after updating is completed.

using System;
using System.ComponentModel;
using System.Deployment.Application;
using System.Windows.Forms;

namespace RVD.Win.View.Popup
{
  public partial class AppDeployPopup : Form
  {
    public AppDeployPopup()
    {
      InitializeComponent();
    }

    #region Properties

    public ApplicationDeployment AppDeploy { get; set; }

    public bool IsUpdateAvailable { get; set; }

    public bool ShouldAppRestart { get; set; }

    #endregion

    private void AppDeployPopup_Load(object sender, EventArgs e)
    {
      Cursor = Cursors.WaitCursor;

      try
      {
        if (IsUpdateAvailable) BeginUpdate();
      }catch(Exception exception)
      {
        MessageBox.Show(@"Unexpected error during updating this software. \n" + exception.Message);
      }finally
      {
        Cursor = Cursors.Default;
      }
    }

    private void BeginUpdate()
    {

      AppDeploy.UpdateCompleted += new AsyncCompletedEventHandler(AppDeploy_UpdateCompleted);

      AppDeploy.UpdateProgressChanged += new DeploymentProgressChangedEventHandler(AppDeploy_UpdateProgressChanged);

      AppDeploy.UpdateAsync();
    }

    private void AppDeploy_UpdateCompleted(object sender, AsyncCompletedEventArgs e)
    {
      ShouldAppRestart = false;
      if (e.Cancelled)
      {
        MessageBox.Show(@"The update of the application's latest version was cancelled.", @"Warning",
                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
      }
      else if (e.Error != null)
      {
        MessageBox.Show(@"There was an error during updating. Please contact your system administrator.", @"Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
      else
      {
        ShouldAppRestart = true;
      }
      Close();
    }

    private void AppDeploy_UpdateProgressChanged(object sender, DeploymentProgressChangedEventArgs e)
    {
      var progressText = String.Format("Downloading: {0:D}K out of {1:D}K downloaded - {2:D}% complete", e.BytesCompleted/1024,
                                          e.BytesTotal/1024, e.ProgressPercentage);
      lblDownloadStatus.Text = progressText;
      pgbDownload.Value = e.ProgressPercentage;
    }
  }
}

Continue Reading »

No responses yet

NHibernate 3.2 Mapping by Code: Component Mapping

12 Oct 2011 by

Component mapping in NHibernate 3.2 Mapping by code provides a very flexible way. You can have multiples static method for returning Action<IComponentMapper<Address>> for the same component class. So that you can reuse it in different place with more customization.

In this example, we have Veteran class and Address class as a component.

 

Entity class files

Address.cs

  public class Address
  {
    public virtual string HouseNo { get; set; }
    public virtual string Street { get; set; }
    public virtual string Commune { get; set; }
    public virtual string District { get; set; }
    public virtual string Province { get; set; }
  }

Veteran.cs

  public class Veteran
  {
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    public virtual Address Address { get; set; }
  }

Entity mapping class files

AddressMap.cs

  public class AddressMap
  {
    public static Action<IComponentMapper<Address>> Mapping()
    {
      return c =>
               {
                 c.Property(p => p.HouseNo);
                 c.Property(p => p.Street);
                 c.Property(p => p.Commune);
                 c.Property(p => p.District);
                 c.Property(p => p.Province);
               };
    }
  }

VeteranMap.cs

  public class VeteranMap : ClassMapping
  {
    public VeteranMap()
    {
      Id(p=>p.Id, map=>map.Generator(Generators.GuidComb));
      Property(p=>p.Name);
      Component(p=>p.Address, AddressMap.Mapping());
    }
  }

3 responses so far

What is the most popular programming language?

04 Oct 2011 by

The data from langpop.com and IEEE Spectrum magazine released on Oct 2011 about the popularity of programming languages recently.

Popular Programming Languages

Popular Programming Languages

No responses yet

NHibernate 3.2 Mapping by Code – Enforce Referential Integrity

28 Sep 2011 by

In the previous post, I show an example of a simple parent/child mapping by code in NHibernate 3.2. In this article, I will show how to enforce referential integrity in way the that deleting a parent record will yield to child record deletion as well.

The data model is exactly the same as previous post. The different is mapping technique.

ProvinceMap.cs

  public class ProvinceMap : ClassMapping
  {
    public ProvinceMap()
    {
      Id(p=>p.Id, map=>map.Generator(Generators.Assigned));
      Property(p => p.Code, map =>
                              {
                                map.Length(3);
                                map.NotNullable(true);
                              });
      Property(p=>p.Name);

      Bag(p => p.Districts, map =>
                              {
                                map.Key(k =>
                                          {
                                            k.Column("ProvinceId");
                                            k.OnDelete(OnDeleteAction.Cascade);
                                          });
                                map.Cascade(Cascade.All);
                                map.Inverse(true);
                              }, ce => ce.OneToMany());
    }
  }

DistrictMap.cs

  public class DistrictMap : ClassMapping
  {
    public DistrictMap()
    {
      Id(p=>p.Id, map=>map.Generator(Generators.Assigned));
      Property(p=>p.Name);
      ManyToOne(p=>p.Province, map=>map.Column("ProvinceId"));
    }
  }

NUnit Test

    [Test]
    public void Referential_integrity_should_work()
    {
      var province = new Province() {Id = 1, Code = "BMC", Name = "Banteay Meanchey"};
      var districtA = new District() {Id = 101, Name = "AA", Province = province};
      var districtB = new District() {Id = 102, Name = "BB", Province = province};

      province.Districts.Add(districtA);
      province.Districts.Add(districtB);

      using(var tx=NhSession.BeginTransaction())
      {
        NhSession.Save(province);
        tx.Commit();
      }

      var provinceBMC = NhSession.Get(province.Id);
      Assert.AreEqual("BMC", provinceBMC.Code);
      Assert.AreEqual(2, provinceBMC.Districts.Count);

      using(var tx = NhSession.BeginTransaction())
      {
        NhSession.Delete(provinceBMC);
        tx.Commit();
      }

      var queryDistrictA = NhSession.Get(districtA.Id);
      Assert.IsNull(queryDistrictA);
    }

 

5 responses so far

NHibernate 3.2 Mapping by Code – Basic Mapping

27 Sep 2011 by

NHibernate 3.2 is coming with default mapping interface that based on ConfORM interface NOT Fluent NHibernate interface. Since its released, there is little document on how to manage mapping by code with this new interface. So this post will contribute to the short.

I have a simple schema as follow:

I have these files to show you.

  1. App.Config, configuration file
  2. ApplicationContext.cs, manage NHibernate configuration and SessionFactory
  3. Province.cs, District.cs, a domain model as depicted in the figure above
  4. ProvinceMap.cs, DistrictMap.cs, a class-by-class mapping
  5. SchemaTest.cs, a drop and create schema for testing
  6. A full source code will be uploaded later while tutorial is completed
App.Config


  
    
  
  
    
  

ApplicaionContext.cs
  public class ApplicationContext
  {
    public static Configuration NHConfiguration { get; set; }
    public static ISessionFactory SessionFactory { get; set; }

    public static void AppConfigure()
    {
      #region NHibernate configuration

      NHConfiguration = ConfigureNHibernate();

      SessionFactory = NHConfiguration.BuildSessionFactory();

      #endregion
    }

    private static Configuration ConfigureNHibernate()
    {
      var configure = new Configuration();
      configure.SessionFactoryName("BuildIt");

      configure.DataBaseIntegration(db =>
      {
        db.Dialect();
        db.Driver();
        db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
        db.IsolationLevel = IsolationLevel.ReadCommitted;

        db.ConnectionStringName = "NH3";
        db.Timeout = 10;

        // enabled for testing
        db.LogFormattedSql = true;
        db.LogSqlInConsole = true;
        db.AutoCommentSql = true;
      });

      var mapping = GetMappings();
      configure.AddDeserializedMapping(mapping, "NHSchemaTest");
      SchemaMetadataUpdater.QuoteTableAndColumns(configure);

      return configure;
    }

    private static HbmMapping GetMappings()
    {
      var mapper = new ModelMapper();

      mapper.AddMappings(Assembly.GetAssembly(typeof(ProvinceMap)).GetExportedTypes());
      var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();

      return mapping;
    }
  }
Province.cs
  public class Province
  {
    public Province()
    {
      Districts = new List();
    }

    public virtual long Id { get; set; }
    public virtual string Code { get; set; }
    public virtual string Name { get; set; }
    public virtual IList Districts { get; set; }
  }
District.cs
  public class District
  {
    public virtual long Id { get; set; }
    public virtual string Name { get; set; }
    public virtual Province Province { get; set; }
  }
ProvinceMap.cs
  public class ProvinceMap : ClassMapping
  {
    public ProvinceMap()
    {
      Id(p=>p.Id, map=>map.Generator(Generators.Assigned));
      Property(p => p.Code, map =>
                              {
                                map.Length(3);
                                map.NotNullable(true);
                              });
      Property(p=>p.Name);

      Bag(p => p.Districts, map => map.Key(k => k.Column("ProvinceId")), ce => ce.OneToMany());
    }
  }
DistrictMap.cs
  public class DistrictMap : ClassMapping
  {
    public DistrictMap()
    {
      Id(p=>p.Id, map=>map.Generator(Generators.Assigned));
      Property(p=>p.Name);
      ManyToOne(p=>p.Province, map=>map.Column("ProvinceId"));
    }
  }
SchemaTest.cs
  public class SchemaTest
  {
    [Test, Explicit]
    public void Create()
    {
      ApplicationContext.AppConfigure();
      new SchemaExport(ApplicationContext.NHConfiguration).Create(false, true);
    }

    [Test, Explicit]
    public void Drop()
    {
      ApplicationContext.AppConfigure();
      new SchemaExport(ApplicationContext.NHConfiguration).Drop(false, true);
    }
  }

5 responses so far

Selenium Grid

22 Apr 2011 by

Selenium Grid Architecture

Selenium Grid Architecture

Selenium is a fantastic web application testing package that comes with different form factors such as Selenium IDE, Selenium Remote Control (RC), Selenium WebDriver and finally Selenium Grid. It highly depends on quality assurance to choose whichever appropriated. This article we see Selenium Grid as it is a good example of distributed systems.

What is Selenium Grid actually?

In distributed systems, Cloud computing is usually running on homogeneous platform while Grid computing is running heterogeneous platform. Similarly, Selenium Grid is running on different (heterogeneous) platform, support various programming languages, support multiples unit test framework, and more importantly is cross browsers.

Continue Reading »

No responses yet

Web Application Testing System – Selenium

25 Mar 2011 by


Selenium

Selenium

Selenium is an open source for web application testing. Selenium is set of different software tools with each different approach to supporting test automation. A variant, Selenium Grid is a distributed systems for speeding up testing very large scale web application.

The main advantages of Selenium are:

  • Open source
  • Small learning curve
  • Cross browsers and platforms
  • Support many popular unit test frameworks and programming languages
  • Distributed testing on its grid

Continue Reading »

No responses yet

Multicast Programming with Java

06 Mar 2011 by

Multicast Overview

Multicast Overview

Multicast is a special feature of UDP protocol that enable programmer to send message to a group of receivers on a specific multicast IP address and port. Multicast has advantage in this scenario.

Let us say I want to send “Hello” message to 100 computers on my network. Perhaps, my first solution is to send the “Hello” message to each of them via UDP or TCP.

What a problem is this scenario?

There are 3 generic problems:

  • Consume a lot of processing power on sender as it needs to send to every receiver
  • Bandwidth flooding
  • The arrival time is not the same for every receiver

Continue Reading »

10 responses so far

TCP Programming with Java

04 Mar 2011 by

TCP Programming has been developed for the sake of reliability in data transmission. Unique feature of TCP is Acknowledgment scheme, Packet ordering and congestion control. These make TCP more popular in network transmission control protocol. Nowadays, the protocol dominant in the Internet is HTTP but be remembered that it relies on TCP architecture. There are several variant of TCP protocols that have been extensively researched. For example, TCP Tahoe, TCP Reno, TCP New Reno, TCP Westwood, TCP Vegas, etc. In contrast with UDP Programming, TCP is much easier.

Continue Reading »

4 responses so far