Saturday, December 31, 2011

Project Euler Problem 6 in Scala

This is 6 in a series.  The previous post is at, Project Euler Problem 5 in Scala.


Problem 6 was particularly satisfying for me.  You will see why when you see the code below.  I think the code deserves some discussion because it takes advantage of some pretty nice Scala features.

Project Euler problem 6 reads:
The sum of the squares of the first ten natural numbers is,  12 + 22 + ... + 102 = 385The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)2 = 552 = 3025Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is   3025-385 = 2640. Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
So reading this question, we want to come up with an algorithm that iterates all the numbers 1..100.  During each iteration, we want to capture the sum of the squares, and the sum of the number.  Finally, we want to square the sum of the number and subtract the two values.  

Using some of Scala's syntactic sugar and its excellent functional handling we get:


object Problem6 {
  def main(args: Array[String]): Unit = {
    val results = ((0, 0) /: (1 to 100))((i, s) => {
      (i._1 + (s * s), i._2 + s)
    })
    println((results._2 * results._2) - results._1);
  }
}

If we take out the object definition and main method call, you can see that this is a two-liner (I could make it a one-liner, but I think that would sacrifice readability).  Once again, I am using the foldeLeft operator (/:).  This time, however, I am folding left on a sequence of values instead of a single value.  Because of this, you see the (0,0) declaration.  Scala also infers this type inside the body of the function, so I am storing to values i._1 and i._2.  Finally, we square the sums and subtract the sum of the squares.


Note that I actually multiplied results._2 by itself.  the '^' operator in Scala seems to work differently than I expected, and was giving me weird results.  I might have to look into that, but for now, we have a very succinct solution to this problem. 

Tuesday, December 27, 2011

Project Euler Problem 5 in Scala

This is 4 in a series.  The previous post is at, Project Euler Problem 4 in Scala.


Here we are, arriving at problem 5 from the Project Euler site.  For those of you that may have stumbled in on these posts, and haven't read why I am doing this, you might want to go back to the beginning and find out here.  The long and short of it is, I'm using these exercises as a way to solve fun problems while learning a new language (Scala).  I've added a twist to try an help me think in a functional way - for each solution, I want my final version of code to have the least amount of mutable state possible.

As it turns out, this has been a fun and enlightening exercise, and I'm only a few problems in!  If you've read any of my previous posts, you know that they were concentrating on turning my Java-like, OO style code into more functional style code without any mutable state.  I must be improving slightly, because this post is going to focus more on creating the right algorithm than on functional vs. OO style.  Without further adieu, I present to you, Project Euler Problem 5:
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
Well, my first solution, as always, was a brute-force way to solve the problem.  I will simply find my 'upper limit' value, which is the product of all the numbers 11 to 20 (A very large number), and iterate all values up to that number, checking if each number has a remainder when divided by any of the numbers 11 to 20.  Simple right?  It was simple, but it also takes 2 minutes to run on my Lenovo ThinkPad running an Intel Dual Core i7 CPU @ 2.67GHz.  Here's the implementation:


object Problem5 {
  def divisible(number: BigDecimal): Boolean = {
    for (i <- 11 to 20 ) {
      if (number.remainder(new BigDecimal(i)).longValue > 0)
        return false
    }
    return true
  }

  def main(args: Array[String]): Unit = {
  var start = System.currentTimeMillis
    val upperBound = (new BigDecimal(1) /: (11 to 20))(_ multiply new BigDecimal(_))
    println("Upper bound is " + upperBound)
    var i = 1
    for (i <- 20 to upperBound.intValue() if divisible(new BigDecimal(i))) {
       println("Smallest # divisible by 1 .. 20 is " + i);
       println("Run time seconds " + ((System.currentTimeMillis()-start)/1000));
        return
    }
  }
}

After I solved the problem, I started thinking about it more.  Iterating through every number seems wasteful, doesn't it?  After all, there are a lot of numbers that will not evenly divide by ANY number in that sequence.  So, I thought, I should really be iterating through a list of numbers that is the product of at least ONE of the numbers in the sequence.  If I selected, 15, for instance,  I could iterate through the list: (15, 30, 45, 60...).  Of course, If I think about that more, the most efficient number to use will be the highest number in the list, 20.  So, I created a new version of the code that iterates in increments of 20, and then checks to see if 11 to 19 are also divisible.  This runs on my machine in about 2 seconds, making this about 60 times faster:


object Problem5 {
  /*Let's skin this cat a different way...*/


  def main(args: Array[String]): Unit = {
    for (i <- 1 to 320000000) {
      val mot = i * 20;
      val divisibleByAll = fitsAll(mot)
      if (divisibleByAll) {
        println("Smallest is " + mot);
        return
      }
    }


    def fitsAll(number: Int): Boolean = {
      for (i <- 11 to 19) {
        if (number % i > 0)
          return false;
      }
      return true
    }
  }
}


I think I can further improve this code with some of Scala's features.  For instance, I think I can iterate the sequence 1 to 320000000 by increments of 20 without needing the variable mot.  Notince, though, that I do not have any mutable variables in the code - I declare 2 variables, but they are both val and not var, and so cannot be re-assigned.  I don't think this is 'perfect functional thinking', but I suspect, I'm slowly catching on.



Friday, December 23, 2011

Project Euler Problem 4 in Scala

This is 4 in a series.  The previous post is at, Project Euler Problem 3 in Scala.

So I've already worked through 3 problems in Euler in hopes of using the exercises to help me learn some Scala.  In each of the previous solutions, I had a LOT of mutable state in my code.  As I get more comfortable with the language, however, I think I'm improving quite a bit.  Even so, it appears I still had a mutable variable in my first solution to this problem.  Problem 4 on the Project Euler site reads:

 A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91  99.
 Find the largest palindrome made from the product of two 3-digit numbers.
This doesn't seem too difficult to solve.  We can cast our integer values to strings, reverse them, and compare them to determine if they are a palindrome, and iterating all the 3 digit numbers, 100-999 is easy.  So, here's my first solution:

 object Problem4 {


  def main(args: Array[String]): Unit = {  
var highestPalendrome = 0;
    for(i <- 100 to 999) {
for(y <- 100 to 999) {
 var product = i * y;
 var productStr = product toString;
 
 if(productStr.reverse equals(productStr)) { 
   println(productStr)
   highestPalendrome = highestPalendrome max product;
 }
}
}
    println("Highest is " + highestPalendrome);
  }
}


In this solution I iterate the numbers and store the highest palendrome as I go.  Of course, the purpose of doing these exercises is to try and code without mutable state, so I had to get rid of that pesky highestPalendrome variable.  To do this, I took advantage of the Scala fold left facilities.  Turns out, this is a handy way to iterate a list and record results.  Here's version 2:



object Problem4 {
  def main(args: Array[String]): Unit = {  
val highestPalendrome = (0 /: (100 to 999))((highest, next)=>{
(0 /: (100 to 999))((tot2, nex2)=>{
 val product = next*nex2;
 if(product.toString.reverse.equals(product.toString)) {
     next*nex2
 } else {
 tot2
 }
}) max highest 
})
    println("Highest is " + highestPalendrome);
  }
}


You'll notice that this is essentially 1 statement, if you remove the main method and println.  You should also notice that I'm using some of Scala's syntactic goodness to make the code easier to read - the end of that statement, 'max highest' would be much uglier in Java - .max(highest).

Well, on to #5.  Maybe this time, I won't need a second iteration of the code...

Project Euler Problem 3 in Scala

This is 3 in a series.  The previous post is at, Project Euler Problem 2 in Scala.


I've been busy again this fall, but now that I have a couple of days off over the holiday, I thought I'd play around with some more Scala.  It is pretty slow going learning the language at this pace, but the day job calls...


Problem 3 on the Euler site states:
The prime factors of 13195 are 5, 7, 13 and 29.  What is the largest prime factor of the number 600851475143 ?
For this solution, I reused the isPrime method from an earlier problem.  Essentially I end up looping through all the values up to the square root of the number and record the primes.  The largest prime is the last one.


Here's my first solution:

object Problem3 {
  val number : Long =  600851475143L;

  def main(args: Array[String]): Unit = {  
val sqrt = Math.sqrt(number);
    var largest = 0L;
var current : Long = 2L;
while(current < sqrt.toLong) {
 if(number % current == 0) {
 if(isPrime(current)) {
    largest = current;
 }
      }
 current = current + 1L;

println("Largest " + largest);

  }

  
  *//**
   * Brute force method.  Perhaps a better method 
   * can be implemented here?  
   *//*
  def isPrime(number: Long) : Boolean = {
    var half = Math.sqrt(number) 
    var current = 2;
    while(current < half) {
      if(number % current == 0) {
        return false;
      }
      current = current+1;
    }
    return true;
  }
}


After looking at my first solution to this problem, you can see that I have mutable state AGAIN.  Something about my Object Oriented brain just can't seem to get into the stateless-state-of-mind.  This is interesting for another reason, though. Mutable state, in general, isn't a particularly good thing.  That is, if you can solve a programming problem without mutable state, it is better than the solution with mutable state.  It shouldn't matter if you are using OO languages or Functional ones.  It seems the industries years of OO teachings have led to we engineers sort of ignoring this fact.  I guess mutable state isn't as clear in an OO language if you have good Encapsulation in your code, but its still something we should be paying attention to.  Luckily, I was able to refactor my solution.  Here's a better version:


object Problem3 {
  val number : Long =  600851475143L;

  def main(args: Array[String]): Unit = {  
 println("Largest is " + largestPrime(2L, Math.sqrt(number), 2L));
  }
  
  @tailrec def largestPrime(initialPrime : Long, limit : Double, currentNumber : Long) : Long = {
    if(currentNumber >= limit.toLong) {
      initialPrime
    } else {
      if(isPrime(currentNumber) && number % currentNumber == 0) {
        largestPrime(currentNumber, limit,  currentNumber + 1);
      } else {
        largestPrime(initialPrime, limit,  currentNumber + 1);
      }
    }
  }
  
 /**
   * Brute force method.  Perhaps a better method 
   * can be implemented here?  
   */
 def isPrime(number: Long) : Boolean = {
    var root = Math sqrt number
    var current = 2;
    while(current < root) {
      if(number % current == 0) {
        return false;
      }
      current = current+1;
    }
    return true;
  }
}


Here we have a new solution where all our mutable state has been removed.  I start by declaring the number for which we are trying to find the largest prime.  It is declared as a val, though, so we are guaranteed that the value can't change.  This is different than any other language I've used, but it seems a handy facility.  Next, I call the recursive largestPrime method, which uses Tail recursion to iterate the list.  


Of course, I still think there is a better way to determine if a number is prime or not, but this method is pretty efficient the way it is.


That's my solution for now.  Stay tuned for Problem 4....

Friday, October 21, 2011

Project Euler Problem 2 in Scala

This is 2 in a series.  The Previous post is at, Project Euler and Scala Problem 1.

Here is the second problem on the Project Euler(PE) site.

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.


Once again, I was able to solve this pretty easily, but I had lots of mutable state in my code.  Here was my first solution:


object Problem2 {
val fourMillion : Integer = 4000000;


var first : Integer = 0;
var second : Integer = 1;

def nextFib() : Integer = {
var returnValue:Integer = prev1+prev2;
return returnValue;
}

var sum : Long = 0;


def main(args: Array[String]): Unit = { 
 var next:Integer = 0;
 while( (next <= fourMillion) ) {
 next = nextFib();
 if(next % 2 == 0) {
   sum = sum + next;
 }
 
 }
 print(sum)
}
}


You will see that I have 3 mutable variables in there -  first, second, and sum.  I also have a rather ugly while loop.  I'd like to get rid of the iteration here and use recursion instead.  While doing so, I can also get rid of the mutable state.  Here is the rewritten code:


object Problem2 {
//Tail recursive...
def fibList(currentList : Array[Int], limit : Int, previous : Int) : Array[Int] = {
 if(previous > limit) {
  return currentList;

 return fibList((currentList :+ previous), limit, (currentList.last + previous));
}


def main(args: Array[String]): Unit = { 
 print((0 /: fibList(Array[Int](0,1), 4000000, 1))((total, next)=> {
     if(next % 2 == 0) { 
         next + total
  } else {
 total
  }  
}))
}
}



I hope you notice the comment in there - Tail Recursive.  This is a very interesting feature of Scala, and most other functional languages.  You see, the big concern in Java when using recursion is that you run out of stack space for all your recursions.  In Scala, the compiler can optimize this code and get rid of the stack if you don't need to keep state in your method.  I've carefully constructed the fibList here so that it can be optimized.  This way, we can't fill up the stack.  go ahead, change 4000000 to 4000000000.  While you may overflow the int values, the recursive function won't blow up the memory.  


This feature of the language is really quite interesting, and from my understanding not a small feat to pull off given the current bytecode standard.  This could allow for some very long-running calculations that might otherwise have caused memory problems if done other ways.  


That's all for this problem.  On to #3...   

Saturday, October 15, 2011

Project Euler and Scala problem 1

I recently found a Web site called, Project Euler (PE).  Its a site full of problems that are intended to be solved by coding.  As I am also experimenting with Scala, I figured this would be a fun way to keep my skills sharp solving problems while learning the nuances of another language.


What I am finding is very interesting.  First, my 'functional' skills need work.  I'm attempting to solve each problem with no mutable state, but as you will see, my first iteration usually has some sort of mutable state and it takes me a little bit to refactor it.  Part of this is due to the fact that I spend my day job programming in an OO language, part of it is due to learning Scala, but part of it is just that I need more practice in the functional way of thinking.


I am fortunate in that I have two friends who are also interested in PE and who have started working through the problems as well, so we swap notes after we solve the solutions, which further helps understanding.  


I thought I would blog about the problems as I work through them.  The format I have settled on this this:

  1. I will present my first, rough, solution to the problem, along with a brief summary of the PEdocumentation.
  2. I will present a refactored version of the same problem that makes the code much more functional and maintainable.
  3. If the PE documentation reveals a math or programming concept that would improve the code, another version of the algorithm may be posted.
It's my hope that presenting my progress in this fashion is both interesting and revealing for other programmers. I work primarily in Java, so this might also be a good way for Java programmers to see how things translate to Scala.  Of course I am not a Scala expert, so I'm sure there will be lots of comments with room for improvement.  At least, that's my hope.


Here is PE's first problem:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.Find the sum of all the multiples of 3 or 5 below 1000.
Well, this seems simple enough.  Simply iterate all the numbers to 1000, testing if they are divisible by 3 or 5 along the way.  The first version of my code looks like this:

object Problem1 {


  def main(args: Array[String]): Unit = {  
    var sum = 0;
    for( i <- Iterator.range(0,1000) if i % 3 == 0 || i % 5 == 0) {
      sum += i;
    }
    print(sum);
  }
}


There's nothing inherently wrong with the code above.  Except I have that mutable variable, sum,  in there.  So, what's a more functional way to think about this?  Thanks to the inspiration from my co-worker, Sean, we have a more functional version of the code above:


object Problem1 {
  def main(args: Array[String]): Unit = {
    val count = (1 to 999);
    val sum = (0 /: count)((total, next) =>
      {
        if (next % 3 == 0 || next % 5 == 0) {
          total + next;
        } else {
          total;
        }
      });
    println(sum);
  }
}


In this version, we've made the sum immutable (by using val instead of var), so it can only be assigned once.  We've also used the left-fold operator, \:, to iterate the range.  Finally, we've converted the contents of the for loop to a function block that gets executed on each 'fold'.  This version looks pretty good, but after reviewing the PE docs, we pick up another little math trick.  


Instead of iterating the numbers, we can take the sum of all the multiples of 3, sum of all the multiples of 5 and then subtract all the multiples of 15.  There's also a proof that demonstrates how to find the sum (I won't reprint it here, if you are interested, unlock the problem on the PE site).


The final version of our code looks like:


object Problem1 {
  def main(args: Array[String]): Unit = {
println(sumDivisibleBy(999,3) + sumDivisibleBy(999, 5) - sumDivisibleBy(999, 15));
  }
  
  def sumDivisibleBy(limit:Int, divisor:Int) : Int =  {
val p = limit/divisor;  
divisor * (p*(p+1))/2; 
  }
}


So in the end, we don't iterate the range at all.  This solution would also scale well (what if we wanted to add all the multiples of 3 and five for all numbers under 1 million...)


So there it is. A brief walk through my process on Project Euler problem 1. Let me know what you think.

Sunday, July 17, 2011

GWT JSNI Reference

I've recently had to go through the GWT JSNI interface, and I found the documentation a little lacking.  While the official JSNI page has nice examples and clearly shows how things integrate, it leaves out some reference information that would be helpful.  My guess is that they didn't want to duplicate the JNI reference, but its still a little frustrating to have to look up the type codes in a separate area.  And the type codes aren't exactly intuitive, I mean, who would guess that 'Z' is the representation for boolean?

So, here is a little cheat sheet for those of you looking for the GWT JSNI type codes.  These are, of course, the same codes that can be found in the JNI reference.

Native Type Java Language Type Description Type signature
unsigned char jboolean unsigned 8 bits Z
signed char jbyte signed 8 bits B
unsigned short jchar unsigned 16 bits C
short jshort signed 16 bits S
long jint signed 32 bits I

long long

__int64
jlong signed 64 bits J
float jfloat 32 bits F
double jdouble 64 bits D

Table taken from Wikipedia, 5/7/2011


In addition to the primitive types, any object can be referenced using its fully qualified name like this:

LFully.Qualified.Name;

For example, to access a string, you use:  Ljava.lang.String;

To reference an array of the same type, prepend [ like this:

[Ljava.langString;

So that's my quick cheat sheet.  Here are a couple of additional references that help:

Coding Basics - Javascript Native Interface (JSNI) 

JDK 6 Java Native Interface-related APIs & Developer Guides

Thursday, April 28, 2011

Master of Software Engineering

Finally, I am an MSE. That's, Master of Software Engineering. I just finished presenting my capstone project at Carroll University.

A lot of people don't know what Software Engineering is. Is it related to Development? Is it really an Engineering discipline? I'd like to take this time to talk a little bit about what I think a Software Engineer is.

Let's start with the official definition of 'Software Engineer'. Any search these days starts with Wikipedia, and in fact, Wikipedia has a nice listing for 'Software Engineering', with lots of references. Here is the first paragraph:

Software engineering (SE) is a profession dedicated to designing, implementing, and modifying software so that it is of higher quality, more affordable, maintainable, and faster to build. It is a "systematic approach to the analysis, design, assessment, implementation, test, maintenance and re-engineering of a software by applying engineering to the software".[1] The term software engineering first appeared in the 1968 NATO Software Engineering Conference, and was meant to provoke thought regarding the perceived "software crisis" at the time.[2][3] Since the field is still relatively young compared to its sister fields of engineering, there is still much debate around what software engineering actually is, and if it conforms to the classical definition of engineering.[4] The IEEE Computer Society's Software Engineering Body of Knowledge defines "software engineering" as the application of a systematic, disciplined, quantifiable approach to the development, operation, and maintenance of software, and the study of these approaches; that is, the application of engineering to software.[5] It is the application of Engineering to software because it integrates significant mathematics, computer science and practices whose origins are in Engineering.[6]
Wikipedia, Dec 24, 2010

If I were to put that rather lengthy description into my own short words, it would be: Software Engineering is a way to build better software by studying and applying a systematic, disciplined and quantifiable approach to the development process.

You see, to me, the key characteristic is that while Software Engineering includes software development, it really goes beyond just writing code or applications.  It is the study and practice of the processes that will make writing applications better.  Better may not mean faster or cheaper but will always mean higher quality. Although, sometimes, it will mean faster and cheaper, but usually in the long run.  So Software Engineering really expands into all the different tasks surrounding a system - business definition, requirements gathering, analysis, design, testing, and support.  A Software engineer should know about all these phases in the life cycle and should be involved with all of them.

But developing software is not exactly akin to designing a bridge or a car.  There's still a component of craftsmanship or artistry involved with software development. Methodologies can be used to improve estimates, quality, and outcomes, but there is also a large component that is more akin to writing a novel or creating a movie than to building a bridge or designing a car.

It is in that part of the software development process where the art of development presents itself.  Writing software is a creative and abstract task with many different ways to solve a problem.  In some cases, a single, best, solution can be found, but in so many others, there isn't one.  In that regard, some developers are artists, some developers really aren’t, and never will be. The rest of us are aspiring to be one of the great ones. We are competent technicians, writing our works, but not gifted with the ability to produce masterpieces.  Here is where what we learn and the techniques we apply from Software Engineering Study allow us to produce pieces of software that are in fact, solid and elegant.  We won't be writing the next Mona Lisa, but by following proper techniques, using the best practices, studying what works, and applying them consistently and rigorously, we can produce consistent, appealing work with high levels of quality.  If we work hard, people might one day also recognize us as an artist.

Perhaps not a Michelangelo, but maybe a Bob Ross.

Sunday, April 24, 2011

My First Scala App

I've been looking at learning another development language for some time. Over the years I've dabbled in PHP, Groovy, and even C/C++. While I learned a lot in doing this, I never became an 'expert' in any of them. For the most part, I used them for the project in which they were needed or for the scenario where they were most appropriate and then went back to Java for my primary development platform.

The reason is that I'd become so familiar with the Java tool set, language, and available libraries. I was (and am) most productive in that environment. But Java is showing its age and limitations. Its becoming clear that some of the most vexing problems facing developers will need a full range of tools to solve them, and while Java's OO support is good, its functional support is not. I, therefore, decided that I needed to expand and really learn another language pretty well. I decided that language is going to be Scala.

Why Scala? Well, I'll go over that in a different post. This post is about my experience writing my first Scala app. So, here it goes.

The Application
Since I am changing jobs (again), I decided I would leave something behind for my co-workers.  In our current work environment, we have a shared drive where server error logs are stored in 'pseudo' XML format.  It was always annoying to me that we had to navigate the file structure, open a text file, and hunt through the file to find an error we would be looking for.  It was also difficult to determine how often an error was occurring.  Now, the easy way to solve this is to use the log4j XML appender and read the files with Apache Chainsaw, but this is a large company, and it decided to go with a proprietary format.  I decided to use Scala to write a little desktop app that the developers could use to view the log files in a tabular format, then select a row to see the stacktrace detail.

The Functionality
The application needed 3 essential things : a table to show rows, a TextArea to show the detail, and a FileChooser to select which log to view.  In the background it also needed the following processing capabilities:  read in a file, modify the text to make it valid XML, parse the XML tags.  That's about it.

The Results
Well, I whipped out the app pretty quickly.  It would have been much more quickly if I hadn't been learning Scala along the way.  There were some really nice things about Scala that I found out.  First, its swing wrapper classes are nice, and their pub/sub model is succinct.  Second, the XML processing is excellent.  The built in support for XML files allowed me to parse the logs in a couple of lines of code.  Finally, the Actor metaphor for multi threading was also very easy to use and implement.

I had a few frustrations along the way as well.  The Eclipse plugin was nice, but it still doesn't have code-complete (One of the biggest productivity tools in my mind).  The code-complete is supposed to be in the plugin, but for some reason, doesn't seem to work for me.  I've tried on 3 separate computers. This meant that I was reading a lot of documentation.  Its also hard to read the documentation.  Since Scala has so much support for so many features, the docs can get a little complicated to read.  I'm sure as I become more familiar with the libraries and syntax this will get easier, but I can see how it would be daunting for a junior developer.

Conclusion 
I'm by no means a Scala expert at this point, but I enjoyed the language enough, and it seems to have enough benefits that I'm looking forward to learning more.  Who knows, maybe one day Scala will be my primary language...

Saturday, February 5, 2011

GWT and SmartGWT - JNI and Unsatisfied Link Error

GWT uses the Java JNI mechanism in a really unique way.  JNI was originally intended to work with native code on the computer where you had your JDK, like C++.  I've had limited exposure to the JNI interface - I've only had to use it for a serial communication component.  Even then, I was using a library that relied on JNI, so I didn't generate the code, I just had to make sure the native library was on my classpath.

The Google Engineers applied this mechanism to Javascript, however.  If you want to write 'Native' Javascript in your GWT classes, its easy.  Just flag your method as native and put your javascript code, in comments, before your terminator.  Here's a sample from the Google Web site.


public static native void alert(String msg) /*-{
  $wnd.alert(msg);
}-*/;


Viola!  Instant native interface for your JavaScript CLIENT code.  That's right, just try running this method on the server.  While working on my current project, I ran into this issue accidently while trying to use a SmartGWT class.  I got a JNI error on the server because the VM couldn't find the native code.  Of course, there isn't any native code on the server.  In this case, the 'native' code is JavaScript.

I was trying to pass a SmartGWT ListGridRecord to the server so I could map it to a persistent bean.  Of course, once I saw the error, I followed the inheritance hierarchy for ListGridRecord all the way up the chain, and what did I find at the top?  JsObject.  Of course, I didn't even have to go that far.  After looking at the Record code, it relies on static methods in JSOHelper, which uses a lot of native JavaScript

In the end I had make transfer objects to use to send the data over GWT RPC. This was a good reminder, though, that while GWT looks like Java, what's being executed really isn't.

Sunday, January 30, 2011

SmartGWT and GWT RPC Integration

I am currently finishing my Capstone project for my MSE at Carroll University. While the application I am writing itself doesn't have a lot of twists in it, I decided to use this opportunity to experiment with some different technologies.  I'm using GWT, SmartGWT, and Hibernate in the application.  I've used Hibernate and GWT pretty extensively in my professional career, but the twists I've added for this development are to configure Hibernate using the JPA standard, and to use GWT Designer for mockups.  I've already posted about my experiences with GWT Designer.

I haven't really used SmartGWT before.  My experience has been with the ExtGWT library, which I loved.  I chose to use SmartGWT on this build because it is fully LGPL, while Ext is dual licensed.  While working with SmartGWT I've found some things that I was not expecting.

One of the selling points of SmartGWT is that it has built in support for 'Data Binding' with Tables and the like.  This is done through DataSource objects.  The problem is, the published DataSource implementations don't work with GWT RPC. And the DataSource base class has dozens of methods (Just take a look at the JavaDoc).  It really isn't clear how to define your own custom data source, or the work it will take to do so.  This was a sticking point for me because I really like the GWT RPC mechanism, and I had already come up with a nice security model, which I posted about here.

It took a while, but I finally found a post about integrating GWT RPC on the SmartClient Forums.  The post is a bit dated and says you need to compile from source, but that is not the case.  The version of SmartGWT I am using, 2.4 already has the clientCustom flag built in.  The developer who posted this also supplied 5 files, Including a GwtRpcDataSource abstract class, and a sample implementation.  The class comments well what you need to do to implement your datasource. With this setup, a new GWT data source can be defined by implementing 4 methods.  

I hope this post helps others find the solution much faster than I was able to.

Saturday, January 8, 2011

dynaTrace Enlightenment

My company recently sent me for dynaTrace training.  dynaTrace is a tool that allows you to instrument your code in real time in order to measure and monitor performance.  I'm not going to go into detail about what it can do, you can read all about it on the dynaTrace website.  If your company has the money, however, I highly recommend it.  It is impressive to say the least.

One of the things dynaTrace offers is the ability to see all the exceptions being thrown in your application, not just the the ones being propagated to your code.  This is important in our applications because we use third-party libraries, and we don't always know what they are doing.  In instrumenting the application we are working on, we found that Spring was throwing hundreds of thousands of exceptions and burying them.  Now each one of these is small in cost, and the application works as expected.  The cost of the exceptions, however, was starting to get expensive - one, two or even a few hundred probably wouldn't be cause for concern, but hundreds of thousands?

So What was the cause?

This application is also a Web application that uses Struts2.  The application was set to use annotation-based configuration.  But when we looked at the Exceptions, it appeared that Spring was ignoring the configuration and attempting to configure itself using other methods, throwing exceptions, and then trying the next one. Multiple exceptions occurred every time the application accessed a Spring managed bean.

As it turns out, there is another flag in Struts 2 to set that says, "No we really, really want to use this configuration."  Once that flag was set, the exceptions disappeared.

So What's the Point?

In todays development environment, we are heavily reliant on 3rd party libraries in order to leverage previous code and get things done faster.  Many applications use dozens of libraries from many different sources.  In some ways, this is a necessary evil.  Could you imagine the time it would take to write a Web application completely from scratch?  I mean completely, no application container, no MVC, nothing.  Start with the sockets to listen to requests.  So, as engineers, we decide to use a component created from another source that matches the criteria we need.  We use application containers, M-V-C frameworks, JavaScript toolkits, Persistence mappers, Logging libraries.... The list goes on and on.

While all of this is generally a good thing, sometimes its not because we don't truly know what's going on in our black boxes.  I think tools like dynaTrace are going to become more popular as companies look to improve quality while still enjoying the advantage of third party software.

That's it for now, it's time to go figure out why the Struts taglibs are adding 500 ms to our page rendering times....