Posts Tagged ‘Programming’

Lift: Replacing JavaScript with JVM Applets

Tuesday, May 18th, 2010

Java applets were originally introduced to provide interactive features for web applications (Wikipedia). With javascript based libraries (jquery, yui), proprietary plugins (flash, silverlight or javaFX) and the oncoming HTML5 this intended purpose disappeared, but java applets remain useful for following reasons:

  • Scala/Java is type safe: You can find errors early.
  • You can use the same language like you used for your web application.
  • The Implementation is browser independent.

Example: Distributed Computation of Perfect Numbers

Definition: Perfect Number (Wikipedia)

Testing numbers for perfection is a simple but process-time consuming task. It isn’t complicated to distribute the calculation on several computers.  You can easily think of other domains where similar types of computations could be useful.

Application Overview

Instead of a javascript a java applet is used to factorize numbers. With the typical features of the lift framework, the application looks as follows:

PerfectNumbers Overview

Details: Server -> Client Communication

The snippet: You can deploy your java/scala applet with your web application.


You can invoke your applet method like a javascript function:

// OnLoad(JsRaw("findFactors('"+ c.toJson +"');"))
   OnLoad(JsRaw(c.toJsCall))

Update your case class:

case class JobPackage(ticket: Int,
                   start: Long,
                   end: Long,
                   master: Long) {
  import net.liftweb.json._
  import net.liftweb.json.JsonDSL._
  def toJson = {
    val json = ("ticket" -> ticket) ~
      ("start" -> start) ~
      ("end"-> end) ~
      ("master" -> master)

    compact(JsonAST.render(json))
  }

  def toJsCall = "$('#some-div').html($('#clientEngine').get(0).doCalc(" + ticket
  + ", " + start + ", "+ end +", " + master +"));"

Client -> Server Communication

Add your ajax call to your applet:

JSONObject serverPackage = new JSONObject();
...
private void sendResult() {
		try {
			String sps = serverPackage.toString();
			getAppletContext().showDocument(
					new URL("javascript:replyResult('replyResult', '" + sps + "')"));
		} catch (MalformedURLException e) {
			errorText = "Error: " + e.getMessage();
		}
	}

and dispatch the result:

  def PMCall(s: String) = { 

    logger.info("PMCall "  + s)

    val cResJs = net.liftweb.json.JsonParser.parse(s)
    val ticket  = (cResJs \\ "ticket").extract[Int]

    // With 2.8: facs.extract[List[Long]]
    val facs = for{
      JInt(fac) <- cResJs \\ "factors"
    } yield fac.extract[Long]

    PerfectNumberMaster ! PerfectNumberMaster.CalcDone(ticket , facs)
    Empty
  }

Download and run the example

The Sourcecode is available here.
Type: mvn jetty:run and open the url http://localhost:8080

This Week on Kungle.de: Nobel Prizes, Riots in Pakistan and the “Balloon Boy”

Friday, October 16th, 2009

Three issues with hundreds of similar news publications blocked the front page of Kungle.de. Each publication is interesting and informative by itself  but together they are hiding other noteworthy information.

I concluded that it was about time to build a new subsystem to reduce the amount of identical information. You can still find all articles via the new “related link”.

The new Subsystem “IssueMerger“ now merges  news with similar content. The older news entries are the more likely  they are consolidated to one issue.

For this, I defined a function to calculate the proximity of two entries. (The Result is 1 if two news entries  are identical and 0 if they completely different.)

It is necessary to  build a complete “News Topology” (A Matrix with up to 1.5 million elements) which defines the proximities of all entry combinations.

The calculation for all topics requires up to 40 hours. The Algorithm itself was coded in 80 lines of scala.

You can find a calculated result here:
http://www.kungle.de/Trend/entry/220033

Update 1: In comparison this merge was hand made:

http://www.kungle.de/Trend/entry/225189

Trend Engine V0.73

Friday, October 9th, 2009

With the 73th version of the Kungle trend engine it is now possible to track a meaningful trend again.

It was difficult to rebuild a functional trend prediction after the decision to remove the search engine results from my calculations.

Cause of the decision:

The search terms got more and more insignificant. For Example on Sep. 29 (The Tsunami Day)  the highest ranked search phrase was “pink toed tarantula”.  Other examples were “who is mysterion” or “out of memory at line 130″ !

The Trend Analysis is now extremely fast. The response time is about  30 minutes after an occurrence. In contrary, searchengine results are delayed up to four hours!

I’m now using microblogs, blogs, and newsfeeds as datasource.

The Trend Score is calculated by following factors:

  • Time of publication
  • Frequency of occurrence
  • Subject significance (by Dictionary)
  • Subject insignificance (by Dictionary)
  • Registration Date
  • Publisher significance

Votes:

I have added a voting mechanism to compare the trend results with my readers opinions. To offer an incentive I have added a Trend bonus for every vote.

Next Steps:

Dependent on the success of Kungle.de and the related earnings:

  1. Switch to bigger hardware.
  2. Improve the topic identification.
  3. Deep text analysis.
  4. Global Impact calculation

Improved user interaction:

Kungle.de is a Scala/Lift Project based on HTML and Ajax.  Next Steps would require to build Restful Web Service and a Rich Internet Application. for a better user experience.

Scala’s simple strategy to reduce debugging costs: “Some” and “None”

Sunday, August 9th, 2009

Null Pointer Exceptions (NPEs) are most common runtime errors in Java (more than 500.000 results in Google). Most Java libraries are interspersed with methods returning null if the computation could not be finished. Invoking method calls on null raises Null Pointer Exceptions.

The best practice in Java to avoid such errors is a test against null.

if(aObject == null) {...

or pass the null test to another function.

For example, Instead of writing:

if(aResultString.equals(“literal”)) {
...

you write:

if(“literal”.equals(aResultString)) {
...

More than eleven years ago (beginning with the java era) a data type existed, avoiding the NPE problem:

data  Maybe a =  Nothing | Just a deriving (Eq, Ord, Read, Show)

A Haskell one-liner ported to Java could have prevented thousands of errors.

Why?

You cannot forget to test against None (unlike the null check) because this would raise a compile time error. Based on Haskells Maybe-Type, Scala defines the Some and None Type with:

final case class Some[+A](x: A) extends Option[A]
...

and

case object None extends Option[Nothing]
...

To test an Option Type against None you write:

val result =  a getOrElse b..

If a is Some(_) than result is _. If result is None, than result is b. The value of result is therefore always defined!

Runtime Errors on my News Page Kungle.de so far

To Keep it short:
None

New Application On The Way

Thursday, August 7th, 2008

Again, an application written months ago awaits its public appearance. I have updated to Java3d 1.5.2 and checked the compatibility with the old mac 5 vm (to many problems with 64 bit).

Citation from the first user feedback: “Nice, why don’t you code something useful.”