Skip to main content

Scarlet – A Scala DSL for web applications


Work in Progress
The Scarlet project is currently under development. Please keep an eye on the sourceforge project site (sourceforge.net/projects/scarlets) or contact personally for updates.

Introduction to Domain Specific Language (DSL)

Domain-specific languages have lately become a hot topic and a lot is being spoken and written about them all over the internet; much of the buzz around functional languages is their applicability to (once again) put the power and control of an application in exactly the right place where it belongs, i.e. in the hands of its users.

By defining a simple language that the application users can understand, feel comfortable to communicate and use, programmers can effectively reduce their involvement in the never-ending cycle of UI requests and enhancements and can let users create scripts and other tools that allow them to create newer behavior in the applications they use. A perfect example of a wildly successful DSL is the Microsoft® Office Excel "language" used to express the various calculations and contents of the cells in the spreadsheets.

Well, some might even go far and suggest that SQL itself is a DSL, this time a language aimed specifically at interacting with the relational database (Imagine if programmers had to fetch data out of Oracle via traditional API read()/write() calls — Eshhhh! Now that’s a nightmare).

What is Scarlet…?

To be quick to the point: Scarlets is an attempt to create “platform-independent”, “compilable” and “executable” pseudo-codes.

Pseudo-codes are a kind of structured english for describing algorithms; which enables the designer to focus on the logic of the algorithm without being distracted by details of language syntax. The intent is to describe the entire logic of the algorithm with finer details so that implementation becomes a rote mechanical task of translating line by line into source code.

We have known pseudo-codes probably since the beginning of times at software engineering. And although we understand that pseudo-codes are capable of describing the entire logic of an algorithm to such an extent that implementation remains to be a simple mechanical task of translating lines of plain english into source code; yet, this so-called simple mechanical task is till date performed with human efforts.

The Scarlets project intends minimize this human intervention by defining a Scala based domain-specific language which is very much like structured english and is a narrative for someone who knows the requirements (problem domain); so that he/she can write a pseudo code and see it run with zero or minimal involvement of a programmer who knows the programming language (implementation domain).

A Sample Scarlet

The following is a sample implementation of Scarlet –

AccountApp.scala :

val userName = theParameter named "user" in request
if (userName equals "nothing") {
~~~this renders "AccountUserForm.jsp"
} else {
~~~theAttribute named "LoggedInUser" in session is userName

~~~val accountHolder = new AccountHolder("B123456", "Rahul", "Roy")
~~~val account = new Account("SA-001", accountHolder, 100.00)

~~~theAttribute named "AccountObject" in session is account

~~~this renders "AccountDisplay.jsp"
}

Do we need a description for the above implementation…? Well, I’d rather leave it for you to understand. Meanwhile here is how the AccountUserForm and AccountDisplay Java server-pages (JSP) look like.

AccountUserForm.jsp :

<html>
~~~<head>
~~~~~~<title>Scarlet Sample</title>
~~~</head>
~~~<body>
~~~~~~<form action="AccountApp" method="GET">
~~~~~~~~~Username : <input type="text" name="user" />
~~~~~~~~~<input type=submit value="Login" />
~~~~~~</form>
~~~</body>
</html>

AccountDisplay.jsp :

<%
~~~~~~account = session.getAttribute("AccountObject");
~~~~~~LoggedInUser = session.getAttribute("LoggedInUser").toString();
%>
<html>
~~~<head>
~~~<head>
~~~~~~<title>Scarlet Sample</title>
~~~</head>
~~~<body>
~~~~~~<%=LoggedInUser%>'s Account : <%=account%>
~~~</body>
</html>


I hope you found Scarlets to be interesting; anyways please do feel free to share your thoughts and comments here so that I can better it with your cooperation.

Comments

Popular posts from this blog

Shard – A Database Design

Scaling Database is one of the most common and important issue that every business confronts in order to accommodate the growing business and thus caused exponential data storage and availability demand. There two principle approaches to accomplish database scaling; v.i.z. vertical and horizontal. Regardless of which ever scaling strategy one decides to follow, we usual land-up buying ever bigger, faster, and more expensive machines; to either move the database on them for vertical scale-up or cluster them together to scale horizontally. While this arrangement is great if one has ample financial support, it doesn't work so well for the bank accounts of some of our heroic system builders who need to scale well past what they can afford. In this write-up, I intend to explain a revolutionary and fairly new database architecture; termed as Sharding, that some websites like Friendster and Flickr have been using since quite sometime now. The concept defines an affordable approach t...

FAINT - Search for faces

Lately, I've been playing around a bit with facial pattern recognition algorithms and their open source implementations. I came across many reference implementation but a very few were implemented in Java, and the Eigenfaces algorithm by far happens to be the best amongst them all. During my research around the said topic i happened to stumble-upon an implementation called FAINT (The Face Annotation Interface - http://faint.sourceforge.net). Faint by far the best facial pattern recognition API and as you must have already guessed, it implements the Eigenfaces algorithm. Now enough of theory talks, how about implementing an example with faint...? Here is one for all you face-recognition enthusiasts. The following example simply searches for faces in a given photograph and thumbnails them. Now, I know thats not face recognition; but be a little creative here. Once you have the facial thumbnails extracted, its never a big deal to look further in the Faint API and find methods which ca...

Is Java String really immutable...?

In many texts String is cited as the ultimate benchmark of Java's various immutable classes. Well, I'm sure you'd have to think the other way once you have read this article. To start with, let's get back to the books and read the definition of immutability. The Wikipedia defines it as follows - 'In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created.' I personally find this definition good as it mentions that an immutable instance's state should not be allowed to be modified after it's construction. Now keeping this in the back of our minds, let's decompile Java's standard String implementation and peep into the hashCode() method - public int hashCode() { int h = hash; if (h == 0) { int off = offset; char val[] = value; int len = count; for (int i = 0; i h = 31*h + val[off++]; } hash = h; } return h; } A detailed ...