Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Thursday, June 4, 2009

Ubiquity fun (at the Scripting Challenge)



The PSW command I told you about in the previous post was my entry for the Scripting Challenge at the Scripting and Development for the Semantic Web Workshop, which took place on Sunday, in Crete, collocated with ESWC 2009. The challenge idea is to create a < 1000 lines script which uses the Semantic Web to create real-world applications.

I couldn't go there to present it, so I created a screencast / demo to replace my live presentation:



Unfortunately I didn't win, it was the Researchers map that took the prize this year, but I got quite nice feedback (thanks everybody!) and new Twitter friends :). Also, it was very fun from one end to the other: from creating the script, to the paper describing the script, to creating the screencast (interesting experience, long live recordMyDesktop and mencoder) and getting feedback about it almost live on Twitter and Facebook, as the scripts were presented at the workshop and I was home having (late) lunch.

I'm now working on advancing the idea, to also have it a part of my MSc (that's why blogging takes a while, because MSc takes the other whiles in my life :) ).

Saturday, May 16, 2009

Ubiquity fun

And my new Ubiquity command is done!

It's a simple prototype of a semantic search command, using data from semantic data repositories available through SPARQL endpoints to answer user questions like find director of Closer or find author of Parallel Algorithms in dblp and microformats to insert the result in the web page, to preserve the semantic markup of the response when user is editing web document (rich text areas or simple text areas).

Have a look at it on its info.uaic home: http://students.info.uaic.ro/~lucaa/psw/. (Update: the new 'home' is at http://www.infoiasi.ro/~lucaa/psw/ since my infoiasi account changed).

It can be heavily improved in terms of data operation, as well as user interface, but it's quite a nice toy.

(I'd love to show you how nice it finds the license of XWiki on dbpedia but somehow my print screen command does not work when Ubiquity is up, and I also need to find out why it fails to load the results when I add it from an extension and register it programatically, but that's gonna be another night...)

Saturday, May 24, 2008

Look, ma, no code!



I read the other days an article about a guy that devised a method to turn off the lights in his bedroom by tweeting about it. My mind immediately started flying towards writing Jabber protocol extensions for communicating with the fridge, the washing machine, the dvd player or the heating system. E.g. "coming home with a girl / guy" on the home group would trigger synchronized actions: heat a little up, cooling wine (only if it's white), start slow music, etc, etc. Since it works for Twitter it would definitely work for Jabber, on both ends: the sender and the home appliance implementation: it's just the same thing (same 80, same plain text, same openness). Jabber also feels more like the right way to implement it: its extension mechanism can allow from a simple text message (which you could actually send without extensions) to sophisticated any-purpose structured data, all thanks to XML almighty. Actually I start to wonder right now if that does not exist already, it seems so clean and cool and not even rocket science.

It was no earlier than today that I asked myself: ok, then WHY didn't the guy (or anybody) do it for Jabber? It's obviously a better choice. But it isn't as simple as Twitter. Jabber is a heavy protocol, although open and everything, there still are 2 (or 7, if you want to be strict) RFCs about it. Yeah, a lot of libraries come to save us from reading them but still, library? look, on Twitter I can only do it with a simple regexp! ("Look, ma, no code!"). It hit me that this might be the explanation for all the "twitter hysteria": not the need for social interaction (bla, bla), the need for microblogging in an ever time constrained world, the need of accessibility in the increasingly mobile world or whatever, just its simpleness. And it's no surprise, in the end: all the right things in life are simple.

Definitely something to remember, if we happened to forget it, for the next world changing application we design!

Thursday, October 11, 2007

Java Firefox Extension



I know, you won't find this as the first search result on Google, but anyway...

Because I promised some (many) posts ago and because recently I found the time to create a decent example (actually, I HAD to find the time), here's the long waited demo of a Java Firefox extension, i. e. Firefox extension that uses Java code. I first got this idea from Simile's Piggybank extension, and then I got their example from http://simile.mit.edu/wiki/Java_Firefox_Extension. I found that to be quite complicated so I created a new one, simpler (and optimized in some points) and, I think, easier to understand.
I assume you are familiar with the Firefox extension development. If you're not, have a look at http://developer.mozilla.org/en/docs/Extensions.
What we already have in Firefox:


  • LiveConnect

  • that allows us to connect to Java code from javascript code. We can write stuff like var x = new java.lang.String("test") and x will hold a reference to a Java object. We can then call all x's functions from javascript and get the results back. We can not send javascript objects other than numbers (integer, double), strings and arrays to Java. I mean, we can, but we'll get a JSObject object which is kind of empty and useless (no functions, no members).


  • XPCOM

  • that allows us to build a COM-like component for Firefox, platform independent (because it is written in javascript).


We will create a XPCOM component that will use LiveConnect to get to the Java code.

Why can't we do it only with LiveConnect? Because XPCOM allows a single instantiation of the component through all the browser instances (shared). Now you decide for yourself why you might need that (I personally needed it because I had some native libraries loaded by the Java code and I had to be sure that happenned only once since I would get an error otherwise).
And it's pretty cool to learn how to do both the Java access and the XPCOM.
Now get the code from this temporary deposit (change extension in xpi and drag it to a Firefox window to install) and follow me:
Any XPCOM component must implement an interface, at least nsISupports, which is the most general so that it can be managed by the Firefox component manager. The trick is that we don't need any function from the interface, we will get the javascript object that implements the interface and call its functions, so we should implement an interface with no functions at all -- we can create our own (like simile's example does) and go through all the mess of compiling the idl file in a xpt interface definition file or we can simply implement nsISupports.
Once that is done (see the GreeterComponent.js file in /components) we can simply call:


Components.classes["@lucaa.students.info.uaic.ro/je-greeter;1"]
.getService(Components.interfaces.nsISupports);

to get an instance to our component ("@lucaa.students.info.uaic.ro/je-greeter;1" is the component's contract id, specified in the implementation).

But what's the trick to access the Java code? One might say that we have LiveConnect's java object and that is enough. Well, not quite, since our own classes ar not loaded and we can not access them. We need to load them dynamically through our own class loader (this is done by the component upon initialization):

var greeterURL = new this._java.io.File(greeterJarLocation.path).toURL();
//create the classLoader
var classLoader = new this._java.net.URLClassLoader([greeterURL]);
//get an instance of the GreetingGenerator through reflection
var greetingGeneratorClass = this._java.lang.Class.forName(
"ro.uaic.info.students.lucaa.je.GreetingGenerator",
true, classLoader);
this._greeter = greetingGeneratorClass.newInstance();

Then, the call to the java functions of the GreetingGenerator can be simply done through:

this._greeter.generateGreeting()

Remember that the java LiveConnect object is not provided by default to the components environment and we must pass it from the calling code, upon initializing the wrappedJSObject of the component:

//get a component
var greetComponent = Components.classes["@lucaa.students.info.uaic.ro/je-greeter;1"]
.getService(Components.interfaces.nsISupports);
//initialize the wrappedJSObject
greetComponent.wrappedJSObject.initialize(java, true);

In the example, the GreetingGenerator class in Java holds a counter incremented by each call to the generateGreeting function. Go to the Tools menu, the "Greet!" menu item and get your greeting. Now open a new browser instance and do the same. The counter goes on, as a sign that it's the exact same object called.

Starting from this, you can do all sorts of stuff: you've just created yourself an unique entrypoint in the Java code, making sure that if you have configurations to set up, servers to start, etc, etc, those will happen exactly once.

Happy coding!

Wednesday, February 21, 2007

Stupid feelings...



Why do I necessarily feel stupid when using other people's libraries for REAL things?


I'm using now bdb xml and saxon8 for some code and I really feel retard... Nothing goes right, just when I think I nailed it I find out about another 'undocumented feature' or 'oh, so well known and so reported bug' to workaround, not to talk about the situations when I just want to find out more, or to do things 'by the book' (when I only have the 'quick-quickest-in-the-flashiest-of-the-flashes getting started' guide).


I admit, I'm no specialist in anything, I just happen to be able to handle multiple domains and, in my humble opinion, I should be able to work with things... I'm not saying I'm that smart, I'm just saying that things should be stupid enough... :)


I read a blog about why one hates frameworks, and, since then, I tend to find those flaws in everything I use: huuuuuge bushy architectures (incompatible, just to make things more fun), some interfaces to access the huge bushy things (don't instantiate it, GET an instance) and the polymorphism that isn't always the best choice (don't get an instance since the framework does not dispatch your call properly, just instantiate the FactoryImpl).


I know, I know, I choose java (known for some other bad things besides being slow) so I did it consciously to myself but I appeal to your better nature!


All the libraries are great as long as you just want to see if and how they work (that would be to take the "Hello, world!" and modify it in "Bonjour, monde!"), or maybe use it as a 'buzz-import' (you know, that import com.net.x.y.sophisticated-(non)abbreviated-name.framework.BigSmartFactory that we all love because it proves that we know how to 're-use code' and 'not reinvent the wheel'). When one gets over this and really wants to understand what is happening there, write code that not only works but is also readable, safe and in fully agreement with the principles of writing programs (if X.createY() actually returns a cooler subclass Z of Y, but the documentation states that it should return Y, I will never cast Y to Z), then one is in big trouble and the clouds of complete idiocy threaten to darken his/her blue skies of knowledge aspiration.


P.S. The title is NOT wrong, is a pun!