Pages

Showing posts with label groovy. Show all posts
Showing posts with label groovy. Show all posts

Labs

avro-log4j   -  serialization mechanism to provide a layout for log4j

firetester   -  A simple RESTful services testing tool written in Groovy Griffon framework

gitter - Publishes github activities to Twitter

jfilemagic (jfm) is an utility for identifying files using magic numbers or signatures

cometd-chat - a comet based chatter for fun



A peek into metaprogramming

Metaprogramming is about programming acting on other programs. Modifying the programs on the fly. Consider a compiler which is a program written to act,parse,execute the code written, analyzed on grammatical domain of its own lexical structure. It interacts and compose programs of small code components like importing required classes, #define macros, message passing etc. when we are to discuss about compilers, they are translating the source to it machine representation. In meta programming, its about source to source and machine independent. In C++, there are templates which is a data object for a set of programs,generates the code that is executed. The compilers use the program analysis method of type system, a check for the type correctness.an oracle, that is,some method for checking whether the system under test has behaved correctly on a particular execution. It is the assertions embedded in the code.Many programs read an input sequence and produce an output sequence, maintaining a logical correspondence between the input and output structures. When the type come into picture in a dynamic way,the system needs to be intact. This is being handled by the compiler itself as it asserts the type. Usually a program will have a structure ; a syntax tree,like an AST.The meta programs can manipulate these representations. The abstract syntax can then be used as an intermediate language, such that multiple languages can be expressed in it, and meta-programs can be reused for several source languages. Functional languages can have polymorphic higher-order functions take other functions as arguments and returns functions, treating functions as values. Java uses AOP, which introduces pointcuts to a program that can then be modified at runtime (using bytecode manipulation etc).

Take, Javascript, we can attach additional properties to an object ("expando").


var testObject = {};
testObject.variable1 = 'string';
testObject.variable2 = 3;


The type system is dynamic.

It has functions as first class objects.


testObject.functionA = function(){};

Function can return other functions as well as passed in as parameters.


testObject.functionC = function(functionA){ return functionB(){};};


Closure occurs when a function is defined within another function,and the inner function refers to local variables of the outer function. Currying a function is modified with each call so that the arguments passed into it become part of the function which will be helpful in partially evaluating functions. It is implemented using closures.

for the simple stupid code ,


function show(){

alert(testAB('A','B'));//AB
var testB = testAB('A');
alert(testB);
alert(testB('B'));//AB

}

function testAB(a,b){
if (arguments.length < 1) {
return add;
} else if (arguments.length < 2) {
return function(c) { return a + c }
} else {
return a + b;
}
}

In Groovy,


def testAB = { a, b -< a + b }
assert testAB('A', 'B') == 'AB'// or testAB.call('A','B')

def testB = testAB.curry('A')
assert testB('B', 'World') == 'AB'


In Groovy,each object has it is own meta class defines the behavior of any given Groovy or Java class.Alter the metaclass (getMetaClass()) and change the object's behavior at runtime. A special MetaClass called an ExpandoMetaClass that allows dynamically adding methods or properties.


String.metaClass.echo = {->
return "This is an echo"
}

println "Test".echo() //will print This is an echo.



String is final, even methods can be added !

The behavior of program changes with a concise code.Instead of test driven development,the methodology of behavior-driven development do follow test-first code-writing. In this case tests are considered specs, or "expectations" about how code will behave. Its is checking what code should do, than what it had done. To understand the concepts and practical uses, need to explore more

Groovy and some Griffon experiments

Groovy is dynamic with its superset java syntax to have fun with and be productive.It can be compiled to bytecode in .class and interpreted at runtime as a script.Adds closures,dynamic typing (duck), auto imports,no semicolon,no return... like a ruby-fied java.There can came railing Grails.Griffon for desktop, is very similar to developing web applications with Grails, basically the MVC + "convention over configuration".In Griffon, the Model is a POGO,the view is SwingBuilder and the controller (injected to) manage the Model and View together ().We know the applet coding and managing is cumbursome, for eg an applet form populated by an OrderDO etc.The coupling of model logic along with the applet controllers is mostly seen in web applications.So I decided to have some fun on this cute creature,Griffon.

A quick work around to display a bindable xml to the text area.I tried with tables, but some issues with events, so I will try later.Developing is easy and with less code.Scripting makes a flexible (agile) application layers like views and controller.Groovy can act as a super glue for different layers.It could combine XML parsing,widgets,networking...Each widget node written in the view classes is syntactically a method call.All of these method calls are dynamically dispatched.Most don’t actually exist in bytecode.Child closures create hierarchical relations.Closures are first class objects.This hierarchy of components would normally be created through a series of repetitive instantiations, setters, and finally attaching child to its respective parent.Child widgets are added to parent containers.
Like a ScrollPane having a text area etc.An event listener method is equivalent to adding a listener with that method; actionPerformed : { ... } ;in Groovy.

For this same Java does as

button.addActionListener(new ActionListener() {
void actionPerformed(ActionEvent evt) { ...}
})

Samples



source
Download source
Follow the quickstart guide and run the app from griffon-test directory

Groovy rocks!! says http://onestepback.org/articles/groovy/index.html

More here
http://groovy.codehaus.org/Griffon+Quick+Start
http://groovy.codehaus.org/Swing+Builder
http://griffon.codehaus.org/