Wednesday, August 22, 2012

Drawing in Graphiti

It seems Eclipse Graphiti is really popular lately. Germany is using it to build cars, Engineers in India are getting to speed with it as well.

Finally I made a try as well and fell in love with the simple APIs and helpful docs. The only thing that struck me is that most diagram editors out there are used mostly to draw just boxes and connections between them. Is that really all you can do!?!??

Ok so you here's what I did, a simple drawing tool in Graphiti:

Click below to see it in action! And Fork it on Github

Youtube video:

Thursday, August 9, 2012

Trouble signing Apps for OSX Mountain Lion

If you're building desktop Apps for OSX, chances are you'll need or you're already working on getting them signed with Apple's certificate. Otherwise your users might see this, starting with Mountain Lion:
I just signing few apps and scored new items in my erroropedia:

Error 1: "Error signing data"

$ productsign --sign 'Developer ID Application: YourCompany' from.pkg to.pkg
productsign: signing product with identity "Developer ID Application: YourCompany" from keychain /Users/user/Library/Keychains/login.keychain
Error signing data.
productsign: error: Failed to sign the product.
Reason: keychain is locked for access from SSH. Try
security unlock-keychain -p
You might also need to enable access to certificate private key for all applications:

Error 2: "MyApp.app: object file format unrecognized, invalid, or unsuitable"

One solution that worked for me:
export CODESIGN_ALLOCATE="/Applications/Xcode.app/Contents/Developer/usr/bin/codesign_allocate"
but it didn't work for other .app package I was trying.

Error 3: "codesign_allocate: the __LINKEDIT segment does not cover the end of the file (can't be processed) in: MyApp.app/Contents/MacOS/MyApp"

No Idea yet... If you figured this out, let me know. I saw that for the same .app package it showed up on OSX 10.5.8, 10.7, but not on OSX 10.8. So maybe it's a matter of updating XCode/OSX?

Wednesday, May 23, 2012

How to create Restful API in Zend Framework 2

Your ZF module just needs to provide Controller that speaks REST (for example RestfulController) and make sure that generated response will be formatted in JSON, in other words configure JSON rendering strategy. Below you can see an example controller. Note that it has convenient methods for to get all elements, single element, create/update and delete an element:
use Zend\Mvc\Controller\RestfulController;

class UserController extends RestfulController
{
 public function getList() {
  return array("users" => array());
 }

 public function get($id) {
  return array("id" => $id);
 }

 public function create($data) {
  return array("created" => "yes");
 }

 public function update($id, $data) {
  return array("updated" => "yes");
 }

 public function delete($id) {
  return array("deleted" => $i);
 }

}
When associating this controller to route, make sure to NOT use 'action' parameter. Otherwise, your controller will look for '<action>Action' method, completely overlooking the above methods. Now let's configure JSON rendering strategy. Your Module.php should look like this after all:
class Module implements AutoloaderProvider
{
    public function init(Manager $moduleManager)
    {
        $events       = $moduleManager->events();
        $sharedEvents = $events->getSharedManager();
        $sharedEvents->attach('bootstrap', 'bootstrap', array($this, 'bootstrap'), 100);
    }
    
    public function bootstrap($e)
    {
     $app = $e->getParam('application');
        $app->events()->attach('render', array($this, 'registerJsonStrategy'), 100);
    }
    
    public function registerJsonStrategy($e)
    {
        $app          = $e->getTarget();
        $locator      = $app->getLocator();
        $view         = $locator->get('Zend\View\View');
        $jsonStrategy = $locator->get('Zend\View\Strategy\JsonStrategy');

        // Attach strategy, which is a listener aggregate, at high priority
        $view->events()->attach($jsonStrategy, 100);
    }
}
Finally, let's try the new API. Note that RestfulController responds only to requests with 'Accept: application/json' header, so trying it in browser may not work out of the box :-)
curl -H 'Accept: application/json' http://zf2sample:10088/user
{"content":{"users":[]}}

curl -H 'Accept: application/json' http://zf2localhost:10088/user?id=1
{"content":{"id":"1"}}

curl -X DELETE -H 'Accept: application/json' http://zf2localhost:10088/user?id=1
{"content":{"deleted": "1"}}

Saturday, April 7, 2012

Wedding table problem

Here's the old ancient wedding table problem that each man inevitably has to solve on his way to honeymoon.

Research on the topic shows that some philosophers simplify this problem to set partitioning problem (not sure if they were on any wedding, or are they just dinning philosophers). Whereas others assign the problem to the social nature of participants. But I guess that for all my readers it's obvious that this is strictly mathematical subject and should be analysed from the algorithms perspective.

Problem statement:
Newlyweds are holding a party of n guests, where 2m of them comes in couples and the rest comes single. There's one long table with n chairs, split equally on both sides and each guest needs to have a chair assigned. Each couple has to together. Guests sitting next to each other, or on the opposite sides of the table are in talking range. There's a graph G(V, E), where each vertex represents a couple or single guest and each edge represents an acquaintance relationship between guests. Guests should have chairs assigned in such a way that the maximum acquaintances will be sitting in talking range.

Input data:
n
m
v1 e11 e12 e13.. e1n1
v2 e21 e22 e23.. e2n2
...
vm em1 em2 em3.. emnm
...
v(n-m) e(n-m)1 e(n-m)2... e(n-m)n(n-m)

where e11, e12, ... e(n-m)n(n-m) are in v1..v(n-m);
n1, n2, ... n(n-m) are in 0..(n-m);

Output data:

_____
vout1 [o o] vout2
vout1 [o o] vout2
... [o o] ...
... [o___o] voutn


Example:

in:
5 - n = 5 guests
2 - m = 2 couples
1 2 - first couple is acquainted with 2nd couple
2 1 - and vice versa
3 - single has no acquaintances

out:

_____
1 [o o] 3
2 [o o] 4
5 [o___o]

Couple one is opposite to couple two and single guest sits at the remaining chair.

There are modifications to this problem, such that the table is not straight, but e.g. T-shaped, L-shaped, E-shaped, round or other. This affects the talking range definition. In some situations, acquaintance graph G is replaced with avoid graph Gavoid(V,Eavoid) where talk range must not cover the avoid edges. In an extreme case Ghate(V,Ehate) graph models situation where distance between guests on the graph edges must be maximized. Most practiced philosophers solve the problem that includes all three relationships.

Typical problems are of size n > 50, so be careful with the computational complexity of your solution!

Tuesday, March 20, 2012

Choosing JS build tool

Lately I was tasked to setup a JS based project together with full build and testing automation. Today I'd like to share the build and testing part, as it turned out to be quite interesting problem.

In short, we have a little JavaScript library that is designed for browsers. During build it should be packaged from source (e.g. multiple extensively documented files) to several different versions (e.g. bundled with other files, minified, etc.); ran against unit tests and validated with JSLint.

Assuming that someone would like to try this library, they might be willing to build and test it as well, so a JS-friendly technology would be a perfect hit, won't it?

Ok, so it was time for a little research on what do others use for building their JS libs. Half of the internet seems to be using Makefiles, while the other half is deep in love with Ant. To be specific, jQuery, Bootstrap, Code9 IDE were using make, while for example Dojo and Firebug were on Ant!
It's actually quite relieving that such serious projects strive for standardization even at the price of dependencies such as Java (Ant) or Linux (make).. Well theoretically you can run make on Mac too, but still that leaves Windows users out.

As much as I wanted to follow best practices, I wished I could avoid build script (be it Ant or Makefile) mixed with external Javascript code calls. Yes build had to be written in Javascript, as it was going to use all JS tooling goodness that already appeared out there, such as JSLint, UglifyJS.

There seemed no other way than just picking Jake and going with it for better or for worse.

Monday, March 5, 2012

Google Summer of Code + Eclipse PHP Tools !?

It's that time of year again when students join open source projects, disrupt the old traditions and bring new ideas into life as part of Google Summer of Code!

Last year I was mentoring a great project led by Karol Gusak, which was crossing the edge of Web and desktop developer tools.
Karol built Eclipse PDT into Orion web tools, bringing core PHP coding support to cloud.



Click to see the video

Do you hate how Eclipse PDT works, absolutely can't stand what great feature it's missing, or wondering why it still doesn't integrate with that other awesome technology in your mind? Start thinking and chime in with your ideas on Eclipse PDT email list, post it on ideas wiki or contact me!

I'd be happy to mentor this year again, and I'm on look out for anything cloud mobile revolutionary lego embedded wordpress css javascript php zendframework symfony automotive node mongo and smart tv too.