Lift: Replacing JavaScript with JVM Applets

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

Trend-Indicator: “Unilateral Reporting”

March 25th, 2010

The Following directed graph shows the “top 5″ countries in the news (grouped by countries).

The query was done for all stored articles since January 2010 until now.

The edges are labelled with the percentage share of all articles for the origin country:

Q1 Country Interest Crunched

Q1 Country Interest

You can find the original picture here.

NoSQL and Web applications

March 24th, 2010

If I’m asked to draw an “easy to understand” diagram about the next-generation architecture for Web-Applications, one of my sketches would look like this:

SQL And NoSQL

SQL And NoSQL

An obvious question about this picture is:

Why are two different Database-Systems necessary?

My artless answer is as follows:

In some crucial cases a NoSQL Database is considerably less expensive than a SQL Database, but a NoSQL Database cannot completely replace a SQL Database.

For example:

  • Try to store millions of Media Files or other large Documents on a distributed RDBMS. Each document has to be quickly accessible and updatable.
  • Try to create a very large dynamic key/value table as a replacement for an overgrown in-memory Hashmap.

Of course you can achieve this also with a relational database, but you will be faced with following problems:

  • RDBMS BLOBS are slow. You can compensate this problem with database clustering but the typical workaround is to store a link or file-path and use the file system as data storage. In this case you always have to check for inconsistencies.
  • You can create a key/value store in your SQL Database. But each access creates a heavy processing overhead. (Transaction handling, logging and versioning.)

NoSQL-Databases are optimized and designed to:

  • Store and access large “binary” objects very fast.
  • Accomplish fast data replication and use table separation (sharding) between different storages.
  • They allow very fast key-value stores.

(Easy schema modification is often stated as NoSQL advantage, but I think this is also a trivial task with DDL.

– Don’t forget to set the initial value of the new column and enable the trigger after the table alteration. )

In contrast there are tasks you should use a SQL – Database:

  • Data Presentation: In most cases visual entities (for example table lines and columns), aren’t 1:1 presentations from Database objects. They are sorted, mapped, reduced (filtered) and often consist of joins between different tables. Map-Reduce and sorting can be done efficiently with NoSQL. But joins aren’t supported. Of course you can rewrite the JOIN operation in your application. For example a simplified HashMap JOIN could look like this:
    var tableBMap : HashMap[idxType, Type of tableB] = tableB.foreach( line => tableBMap += (line.joinAttributB -> line))
    tableA.map( line => (line, tableBMap.get(line.joinAttributA)))

    The Disadvantage is, that you have to materialize the complete object / table-line.

  • NoSQL = No Transaction. You cannot use NoSQL for reliable database transactions (reliable like in ACID). This is a serious restriction for massive parallel database updates. (Hence, you should avoid programming the popular Bank Account example with a NoSQL Database.)

Why I prefer MongoDB as NoSQL Database System:

  • Very Fast: Sequential write and random read operations are done very fast on an “average” Server. (You need a 64 Bit OS if your database is larger than 2GB)
  • Scala(Java) Support: Several drivers are available scamongo, mongo-scala-driver or akka-persistence
  • Easy to use: There is no setup, parameter or table type “magic”. In less than an hour you can setup a secure and robust Database Server. The client uses JS/JSON.
  • A nice community: The Forum is very active. Questions should be answered in a couple of hours.

Addendum to my recent Post: FSM stands for “Flying Spaghetti Monster”

In my recent post I used the abbreviation FSM for “Finite State Machine”.

I was informed that FSM is the common abbreviation for the Flying Spaghetti Monster.

I searched for this term in the Kungle News document-storage and found some evidence for this claim:

Small Screendump

Small Screendump

Here is the complete list of references:

Query:

Start Main

New Hist Defined with:

Primary: List(Flying Spaghetti Monster, Pastafarianism, Pastafarian)

Secondary in: List(FSM)

Secondary out: List()

Calculated Interval: { “publishingDate” : { “$gt” : “2010-01-10T23:00:00.000Z” , “$lte” : “2010-03-22T23:00:00.000Z”} , “originalLanguage” : “ENGLISH”}

TitlePublished
Recorded
PublisherCitation
Mississippi dips its toe into antirealityTue Jan 19 15:00:00 CET 2010Discover MagazineFinally they will spread the message of how we were all created by the Flying Spaghetti Monster
"Senator Webb (D) shows fear: ""Suspend All Votes On Health Care Bill Until Senator-Elect Brown Is Seated"""Thu Jan 21 00:00:28 CET 2010Crooks and Liarsit'll be approximately Dec 2012, slightly before she takes office, so I figure what better time for Jebus, Buddah, Allah, Ra, Flying Spaghetti Monster, the Mayans, The 'V' lizard people, the Vogons, Daleks, V'Ger, etc to arrive?
FSM protect us!Tue Jan 26 19:40:17 CET 2010Discover MagazineSome people say the Church of the Flying Spaghetti Monster was a joke made ...
Video – Who Knew He Could Be A Swedish Hero?Mon Feb 01 16:00:11 CET 2010Dvorak UncensoredI wonder if it will work with the Flying Spaghetti Monster?
Minority Contractors Receive Just 2 Percent of Highway Stimulus CashThu Feb 04 20:31:08 CET 2010InfrastructuristWhat if only 2% of all infrastructure construction companies are women or minority owned? Setting a goal of having 10% of all contract recipients be minority or women owned is about as useful as setting a goal of having 10% of all contract recipients go to Wild Hildabeest and Flying Spaghetti Monster owned businesses.
When Did Jesus Become a Republican (or, for that matter, a Democrat?Mon Feb 15 00:44:01 CET 2010Care2 NewsI'm a Pastafarian. To me it's the only pure religion.
Do we really need a religious bill of rights?Mon Feb 15 15:27:14 CET 2010Discover MagazineIf the act passes, we need a Pastafarian as an agent provocateur.
Church of the Flying Spaghetti Monster FSM StoreFri Feb 19 02:33:10 CET 2010Suite101Church of the Flying Spaghetti Monster FSM Store
Iraq still embracing the magicWed Feb 24 02:02:34 CET 2010Discover MagazineNo, just kidding, it’s “For Flying Spaghetti Monster’s Sake”
Miss Beverly Hills tries to one-up Carrie Prejean, says it’s divine law that gays be put to death.Wed Feb 24 15:55:45 CET 2010Think ProgressThe Flying Spaghetti Monster offers more in life than any pagan based worship.
Video: Republican legislator says disabled children are 'God's punishment' for abortionWed Feb 24 19:00:09 CET 2010Crooks and LiarsI think my Pastafarianism makes me less able to understand why so many people think a superior being gives a flying noodle what they do or say?
South Dakota legislators tell schools to teach ‘astrological’ explanation for global warming.Thu Feb 25 19:49:40 CET 2010Think ProgressAll hail the Flying Spaghetti Monster!
"End of an Era: ""Lasts"" for Shuttle Program"Fri Feb 26 18:32:36 CET 2010Universe TodaySome spectacular pictures from the final SRB test. FSM-17, (that's flight support motor, not Flying Spaghetti Monster) burned for approximately 123 seconds — the same time each reusable solid rocket motor burns during an actual space shuttle launch.
Atheist Groups Visit The White House Causing A Right Wing TizzyMon Mar 01 15:45:41 CET 2010Dvorak Uncensored(Before the religious start jumping up and down “See, atheists ARE a religion”, the whole thing is a joke, like the Flying Spaghetti Monster)
Creationists And Climate Deniers Take On Teaching Climate Science In SchoolsThu Mar 04 17:20:14 CET 2010HuffingtonpostI think we can all look forward to the time when these three theories are given equal time in our science classrooms across the country, and eventually the world; One third time for Intelligent Design, one third time for Flying Spaghetti Monsterism (Pastafarianism), and one third time for logical conjecture based on overwhelming observable evidence.
Massa Will Resign MondayFri Mar 05 20:43:18 CET 2010Talking Points MemoThere is no doubt that there is a Flying Spaghetti Monster. The question is just how it flies, and what kind of sauce it's covered in.
ARD TV drama sparks Scientology's ireMon Mar 08 11:34:00 CET 2010The Local GermanyWhat Would the Flying Spaghetti Monster Do?
Christian leaders urge Congress to ignore misinformation on abortion provisions and pass health reform.Sat Mar 13 18:17:21 CET 2010Think ProgressSince a Pastafarian, I will say RAmen. ;)
Kreutz Comet VIDEO: WATCH Newly-Discovered Comet's Collision Course With The SunSun Mar 14 15:36:13 CET 2010HuffingtonpostIt's the great noodly appendage of the Flying Spaghetti Monster.
"To The 9th Circuit Court Of Appeals, God Is ""Patriotic"" And No Longer ""Religious"""Sun Mar 14 16:00:51 CET 2010Crooks and LiarsI'd tell him to substitute Flying Spaghetti Monster where appropriate.
Boehner Claims Student Loan Reform Will ‘Eliminate Every Bank In The Country’Fri Mar 19 23:42:19 CET 2010Think ProgressThe universe could have been created by a Flying Spaghetti Monster, or it could have always existed.

2; 0; 19

Topic Connections:

(fsm,1)(monster,1)(store,1)(flying,1)(church,1)(spaghetti,1)

Emotions:

(love,14)(hope,13) (+)/(-) (hate,7)(fear,7)

Public Tendency:

10; 5; 6

Country:

gb; us; no; cn; jp; in; se; au; ru; de; fr; ie; gr; nz; ca;

0; 20; 0; 0; 0; 0; 1; 0; 0; 0; 0; 0; 0; 0; 0;

Publisher Tendency:

(Discover Magazine,3)(Huffingtonpost,2)(The Local Germany,1)(Think Progress,0)(Talking Points Memo,-1)(Crooks and Liars,-3)

Calculation Done

Trend – Visualization

March 22nd, 2010

Since January the New “Corpus Engine” is in development and recorded about 302.000 articles. All in all 1.130.000 “news headlines” and summaries were stored since Kungle.de went online.

Now new algorithms were developed to:

  • Identify the public opinion about political and economic topics.
  • Follow the image status of brands, corporations or companies.
  • Track public feelings and emotions about actual events.

The Challenge

The actual trend calculation, based on static dictionaries, isn’t able to identify new events like an ‘earthquake’ or a
‘political reform’. The “topic-tagging” is static and limited to 9 topics “Science, Economy, Politic, Technology, Entertainment, Sport, Boulevard, Adult and Religion”.

It would be an exhausting task to code every new subtopic or event in a FSM (Finite State Machine).

Therefore the new engine identifies topics by itself. So not only the trend is calculated dynamically also the topic classification is “calculated”.

How is this done?

A simplified breakdown:

NLP (Natural Language Processing) is based on two strategies for text analysis: Tagging via Dictionaries and word / N-gram frequency analysis.

For Example:

This is an animation of a small section from the Kungle English – Dictionary (about 300.000 words) since January. The daily word count (one hour = one frame) is represented in the column height. The Column color changes from green to red if the word occurred in more than 10 percent of all articles. The overall word count frequency decreases from left to right.

Bigrams:

This is an even smaller section from the weighted bigram Matrix (about 100.000 x 100.000 words) in the same timeframe. Also this animation is compressed you can identify some horizontal and vertical lines. These lines occur if a topic is heavily discussed.

Votes on Kungle.de

October 30th, 2009

A short summary about the new vote feature.

Last Week Results

Total Votes: 623

Votes by Topic:

TopicVotes
Science183
Politic101
Technology95
Economy80
Boulevard78
Entertainment50
Religion24
Sport9
Adult3

Pie Chart:

VotesOct

Why should you vote?

Because with all collected votes it will be possible to calculate better results for following matters:

  • Offer new subtopics
    For example visitors are especially interested in Science/Health, Entertainment/Bollywood or Politic/War. Subtopics will be implemented in future versions of  the application.
  • Forecast the period of time a news entry is relevant
    First indications are, that Boulevard-News have the longest lasting relevance.
  • Estimate publisher/editor reputations
    More votes mean a higher reputation for a publisher.

Vote Guidance

You cast a vote by pressing the check-mark on the right-hand side of the news-entry.

unselected

You have ten votes in total.

Your Privace

Kungle.de will not store any personal information about your visit.

In Detail

Kungles web server like any other web server on the internet stores your IP in a log file. Independent from this logfile  Kungles application server stores your selection and votes temporarily in memory.  It is very difficult to connect these two information sources to generate an individualized user profile.

Finally the database counts the votes passed by the application server and logs theses events independent from the two other logs.

architecture

I’m not interested in individual user behavior. 

Recent events showed that you should be very careful with your personal data:

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

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

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

Therefore I see the necessity to inform you about Kungles information processing.

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

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

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.

Kungle.de: Automatic Translation and UTF Bugfix

September 21st, 2009

I have just uploaded the first Kungle release with an automatic translation of foreign languages and an improved utf encoder.

FirstTranslation

Click on the [Translated] sign to read the translation.

Technical Trouble

September 18th, 2009

Hacker and Botnets had been very alive this week. Several night-shifts were necessary to keep kungle alive. However some drop outs were unavoidable.

Features:

The development of  the translation feature has begun. The results are adequate so far.

Kungle.de as Pie Chart / New Features

September 11th, 2009

Some fresh statistics from my database:

New: I’m now following political, cultural and news blogs.

If you want to participate create your own free blog (for example here wordpress.com or here www.blogger.com) and leave me a note or comment with your address of your website.

If you like, use your native language. I will add a new feature  “automatic translation” in the next few days.