Wednesday, 31 March 2010

Protocol Buffer

This article is related to serialization using protobuf a Google's data interchange format. I've been using XML serialization for several years in my Delphi win32 applications, and I was looking for a nicer system. Then I discovered this interesting system that is able to serialize / deserialize objects in a plain format, more human readable instead of the cumbersome XML format. It's a pity that the library isn't implemented on Delphi, but at least there are a lot of programming languages where the library is now implemented. You can find a list here. For testing the power of the library I decided to give it a go in a Java project. This will help me to brush up my Java skills and go further with a protobuf implementation. But one of the primary points behind Google Protocol Buffers is to have a more efficient data packet. Their particular approach uses a stream of binary data rather than the text based on XML. This makes for much less data being sent around and for a much faster parsing. My example will show you the general idea, the easy to build and use, the quick parse and the simplicity of the project.
To some extend I think that the idea is very good, but I'll wait changing my XML serialization until there is a Delphi release of the Protocol Buffer (I'm crossing my fingers). Anyway here you can see a simple example defining the ".proto" file, and the generation of the parser class.

Graph.proto:

package Graphs;

option java_package = "example.graph";
option java_outer_classname = "Graph";

message Node {
  required string id = 1;
  required int32 weight = 2;
}

message Edge {
  required string id = 1;
  required int32 weight = 2;
  required Node source = 3;
  required Node target = 4;
}


For generating the parser class, we need to download the last protobuf compiler (for win32):

C:\protoc-2.3.0-win32>protoc Graph.proto --java_out=C:\temp

After that, we'll get the Graph.java class that will help us serializing our objects. I've created a Serializator class that will handle the node class and will serialize it using the protobuf class:

node.java:

package example.graph;

public class node {
 private String id;
 private int weight;
 
 public node(String id, int weight) {
  setId(id);
  setWeight(weight);
 }

 public void setId(String id) {
  this.id = id;
 }

 public String getId() {
  return id;
 }

 public void setWeight(int weight) {
  this.weight = weight;
 }

 public int getWeight() {
  return weight;
 }
 
 public String toString() {
  return "Id: " + id + " Weight: " + String.valueOf(weight);
 }
}

Serializator.java:

package example.graph;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import example.graph.Graph.Node;

public class Serializator {
 public void SerializeNode(node n) throws IOException {
  FileOutputStream output = new FileOutputStream("node" + String.valueOf(n.getId()));
  
  Node.Builder nodeS = Node.newBuilder();
  nodeS.setId(n.getId());
  nodeS.setWeight(n.getWeight());
  nodeS.build().writeTo(output); 
 }
 
 public node DeserializeNode(int nodeId) throws FileNotFoundException, IOException {
  Node nodeD = Node.parseFrom(new FileInputStream("node" + String.valueOf(nodeId)));
  node n = new node(nodeD.getId(), nodeD.getWeight());
  return n;
 }
}

exampleGraphMain.java:

package example.graph;

import java.io.IOException;

public class exampleGraphMain {

 /**
  * @param args
  * @throws IOException 
  */
 public static void main(String[] args) throws IOException {

  //Serialization (Creating Nodes)
  node n = new node("1", 3);
  node n2 = new node("2", 10);
  
  System.out.println(n.toString());
  System.out.println(n2.toString());
  
  Serializator sr = new Serializator();
  sr.SerializeNode(n);
  sr.SerializeNode(n2);
  
  //Deserialization (Reconstructing Nodes)
  node n3 = sr.DeserializeNode(1);
  node n4 = sr.DeserializeNode(2);

  System.out.println(n3.toString());
  System.out.println(n4.toString());  
 }
}


If you see the generated files "node1 and node2" you'll see them in a byte code format.

Enjoy the reading!.

Friday, 26 March 2010

Creating a nice email signature with WiseStamp

Some days ago, one of my clients asked me for changing the email signature adding some images and links. The best difficulty here is that most of the email clients only allow plain text signatures. Then, surfing over the net, I bumped into WiseStamp a nice extension for firefox and other web browsers. With this little complement, you can built you email signature and when you are writing your message it'll show automatically the signature you've configured into WiseStamp extension. With all this, you can set up your own signature using HTML notation, encrusting some images and links and create a nice email signature in few minutes.

Here you can see my mail signature:



As you can see, you can built nice signatures with simple HTML, getting the images you need from your server and configuring the exit links:

Once you have your design and have installed the extension, you'll see a little icon on the right at bottom. With the WiseStamp icon, you can configure the settings for you signature:
From here, you can import your HTML template in order to design better signatures. You can find a lot of examples in the sample section in WiseStamp.

Saturday, 20 March 2010

Friday, 19 March 2010

Don't miss: Andy Hunt's Pragmatic Thinking

This June 21st will be held the second Pragmatic Thinking event, lead by Andy Hunt at Skills Matter in London. If you are interested on improving your skills and want to know how your brain works I recommend you this course. I attended to the first event last October and it was very thrilling, with a lot of interesting stuff and exercises. In this course you will learn a lot of techniques to help your brain to work better and acquire pragmatic thinking and learning skills. 


Here you can find the extended program and the different topics you'll go through during the event:

LEARN HOW TO:
  • Take advantage of pole-bridging and integration thinking.
  • Compare different laterally-specialized functions, including synthesis vs. analysis and sequential processing vs. pattern-matching.
  • Discover the one simple habit that separates the geniuses from the "wanna-bees."
PROGRAMME

Journey from Novice to Expert
  • The Five Dreyfus Model Stages
  • Dreyfus at Work: Herding Racehorses and Racing Sheep
  • Using the Dreyfus Model Effectively
This Is Your Brain
  • Your Dual-CPU Modes
  • Linear and Rich Characteristics
  • DIY Brain Surgery and Neuroplasticity
Get in Your Right Mind
  • Draw on the Right Side
  • Engage an R-mode to L-mode Flow
  • Harvest R-mode
Debug Your Mind
  • Meet Your Cognitive Biases
  • Recognize Your Generational Affinity
  • Codifying Your Personality Tendencies
  • Exposing Hardware Bugs
Learn Deliberately
  • Create a Pragmatic Investment Plan
  • Use Enhanced Learning Techniques
  • Read Deliberately with SQ3R
  • Visualize Insight with Mind Maps
  • Learn by Teaching
Gain Experience
  • Play in Order to Learn
  • Embed Failing in Practice
  • Learn About the Inner Game
  • Sensory Overrides
Manage Focus
  • Increase Focus and Attention
  • Manage Your Knowledge
  • Control Context
Beyond Expertise
  • Effective Change
  • What to Do Tomorrow Morning
  • Beyond Expertise

Sunday, 14 March 2010

Showing the folder dialog in Delphi

Some time ago, I published one post showing the way we can open the folder dialog with Delphi. But this snippet of code doesn't work for Delphi 2010. Then, I found this little code that allows you to open the folder dialog with no problems.

unit folderDialog;

interface

uses
    SysUtils, ShlObj, Windows, ActiveX, Forms;

function GetFolderDialog2(const ACaption: string; out ADirectory: string): boolean;
function BrowseForFolder: string;

implementation

function BrowseForFolder: string;
begin
  GetFolderDialog('Add directory:', Result);
end;

function GetFolderDialog(const ACaption: string; out ADirectory: string): boolean;
const
  BIF_NEWDIALOGSTYLE       = $0040;
  BIF_NONEWFOLDERBUTTON    = $0200;
  BIF_USENEWUI             = BIF_NEWDIALOGSTYLE or BIF_EDITBOX;
var
  pWindowList: Pointer;
  tbsBrowseInfo: TBrowseInfo;
  pBuffer: PChar;
  iOldErrorMode: cardinal;
  pIemIDList: PItemIDList;
  pShellMalloc: IMalloc;
begin
  CoInitialize(nil);
  try
    Result := false;
    ADirectory := '';
    FillChar(tbsBrowseInfo, sizeof(tbsBrowseInfo), 0);
    if (ShGetMalloc(pShellMalloc) = S_OK) and Assigned(pShellMalloc) then
    begin
      pBuffer := pShellMalloc.Alloc(MAX_PATH);
      try
        with tbsBrowseInfo do
        begin
          hwndOwner := Application.Handle;
          pidlRoot := nil;
          pszDisplayName := pBuffer;
          lpszTitle := PChar(ACaption);
          ulFlags := BIF_USENEWUI or BIF_RETURNONLYFSDIRS or BIF_NONEWFOLDERBUTTON;
          lParam := 0;
        end;
        pWindowList := DisableTaskWindows(0);
        iOldErrorMode := SetErrorMode(SEM_FAILCRITICALERRORS);
        try
          pIemIDList := ShBrowseForFolder(tbsBrowseInfo);
        finally
          SetErrorMode(iOldErrorMode);
          EnableTaskWindows(pWindowList);
        end;
        Result := Assigned(pIemIDList);
        if Result then
        begin
          ShGetPathFromIDList(pIemIDList, pBuffer);
          pShellMalloc.Free(pIemIDList);
          ADirectory := pBuffer;
        end;
      finally
        pShellMalloc.Free(pBuffer);
      end;
    end;
  finally
    CoUninitialize;
  end;
end;

end.

Now we can see the result after the execution of the code:


Sunday, 7 March 2010

If you like my work - Please support me!.

Recently I've added the donate link where you can support my work. All my programs and articles had their origin in applications that I wanted to have but weren't existing or were done in another programming language. I've spent a lot of hours on doing research and coding those applications and I love it. I've been helping people for free, and I want to keep it in that way!. Sometimes I've spent time in coding just for my appreciated users and followers, because I like their ideas and that has given me some new coding challenges and opportunities.

However, you can support me by donating something, just to give me some idea that my articles and software are useful or interesting for you!. Donation only has influence on my motivation and further development to ensure a professional user experience. In any case I'll be demonstrating the same passion for my blog as always!.

This week I'll set up an advertisement and sponsoring section where the companies can support my work. If you are interested on that, don't hesitate to contact me.

Thank you for following me!.

Wednesday, 3 March 2010

In the eye of the beholder

These days I've been thinking about the blog and "How it is turning into something new". Talking with one of my partners I cast back my mind to the previous version of the blog which seemed more interactive and where I had more visitor's comments. Now I'm trying to turn over a new leaf and changing bad routines by posting more interesting and catching things to the blog (for the bloggers sometimes is difficult not to replicate other people's work). I know that the last months I've been obsessed with my applications "Thundax XYZ," but in the meantime I had more time to carry out extensive research and being prepared for the next months with more interesting articles and works.

Now I can tell you that I'm setting up new articles with topics as interesting as SIFT (Scale-Invariant feature transform), that's a very interesting group of algorithms of image treatment, and I promised one article of this to one of my readers: Miguel Angel. I'm also looking for an algorithm for drawing graphs like Manhattan Routing or TaxiCab Distance. These algorithms are used to drawing lines into VLSI Routing architectures, and some well-known drawing applications use this algorithm to connect things using a grid as a layout.

As you can see, I have a lot of work here!!. Anyway, if you know anything about these kind of algorithm, I would be grate to know about them!. I'm trying to improve the drawing system of P-zaggy and I'm thinking on using one of these architectures for the drawing system. I also have in mind the A star algorithm that I published months ago, but It's commonly used in video games platforms.

Another thing I would like to talk is in a new project I'm totally immersed called Zaluum. This is the first open Source industrial automation platform and it's still on an early stage, but I think it'll work!. I'll let you know how it goes!.

But, What I would really like is to have a feedback about what I'm posting into the Blog. I know that sometimes it's difficult, but in order to gain confidence with the things I'm writing, I would appreciate that. The blog is running well, I have more than one thousand of visits per month and I've noticed that there are months that the visits have gone beyond two thousands. I've also made some improvements to the page and in order to gain exposure I enrolled myself to the most known sites like: technorati, delicious, flickr, digg, twitter, linkedIn, Youtube, SourceForge, jobsket, XING and DelphiFeeds. If you have time, don't forget to vote for me in these links in order to get more exposure or if you want to be in my RSS feed, you can access to the feedburner RSS feed.

Now, If you have a little of time, please, let me know what you think, and don't hesitate to contact me if you have any doubt on any of my previous articles.

Enjoy the reading!.

PD: Like the title of the article, "I'm the beholder", taking care of my blog and using heuristic tools to measure different things about the visitors in order to see what they are looking for!. However, I don't have a lot of time, and sometimes I'll find difficult to post news as I'm keen to finalize my degree in Computer Engineering and I have a lot homework and projects to do. (and sorry for my English!) .