CSC/ECE 517 Fall 2012/ch1 1w9 av

From PG_Wiki
Jump to: navigation, search

Object-Oriented Domain-Specific Languages (OODSL)

An OODSL[1][2] is a Domain-specific language (DSL) that exhibits characteristics that have traditionally been attributed to Object-Oriented Languages

Although OO languages and Domain specific languages have been around for several years, OODSLs are relatively new.


Contents

Overview

OO languages can be either general purpose like C++, Java and Ruby, or domain specific, like Jquery and Kiddo. The following example shows how the two kinds of languages would work if used in the real world.


The General Purpose Programming way of ordering hash browns at Waffle House[3]

Harvest the potato, wash it, and chop it into little pieces. Put the pieces in a pan with oil and fry till they turn golden brown, and then drain the oil away and put them on a plate. Now cut some jalapeno peppers and spread on the potatoes. Then add cheese, which you can get by milking a cow and.....


The OODSL way of ordering hash browns at Waffle House

I would like it Scattered, Smothered, and Peppered please. Thanks!


For a person who works at Waffle House, or goes there regularly to eat, it is a no brainer that the second method is much more efficient. OODSLs take advantage of the context within which communication takes place. By using implicitly understood jargon, the task at hand can be accomplished in a way that is easier to comprehend for a specialized type of user.


OODSLs can be of 2 types:


Internal OODSLs are languages that are built on top of existing languages. An internal OODSL makes use of the host language syntax to provide the feel of a domain specific language. It is not universally defined as to when a language/library becomes an OODSL. JQuery is one such example. It is a JavaScript library, which offers an easy way to perform functions like element selection. It can also be argued that JavaScript itself is a domain specific language.

External OODSLs are those that are built from scratch. With the current technology, it is far more easy to churn out several internal OODSLs than to build one from scratch. External OODSLs have their own syntax, as well as their own methods of compilation/interpretation.

History

OODSLs cannot be spoken about without first a brief introduction to Domain Specific Languages and the Object-Oriented programming paradigm.

In a nutshell, DSL[4] is a language that is specific to a particular domain. DSLs are different from general purpose programming languages like C++ and Java. They take advantage of the fact that there are groups of users who rather than communicating every single thing, are able to implicitly understand certain terms and notations. This makes communication much more efficient for such a group. Eg: 2 basketball fans will talk in basketball jargon. Not everyone will understand what zone defence, 3 pointers and jump ball mean. The jargon serves the specific purpose of easing communication between 2 basketball fans. Some would argue that DSLs have been around for a far longer time dating back to even before the advent of computers. Subway maps, electronics circuit diagrams all demonstrate properties of a DSL. Since then, DSLs have come a long way. Domain Specific Languages (DSLs), as they are spoken about now, have been around since the 1970's and their origin can be traced back to a tool called Draco. Since Draco, computers have made it possible to design a whole host of other useful DSLs. Some of the more popular ones among them are SQL, Prolog, Verilog, HTML etc.

Object-Oriented Programming (OO) is a programming paradigm that tries to apply real world principles to make programming easier. OO has been around since the 1950's. Starting with Simula and Smalltalk, OO languages have come a long way since then, and has grown to become one of the most popular programming paradigms employed worldwide. Some of the modern object oriented languages include static languages like C++, Java, C#, and dynamic languages like Perl, Ruby etc.

OODSLs, that bring out the features of both DSLs as well as OO languages are a fairly recent invention. OODSLs have been around since the 1980's/1990's. They are now primarily used in the realm of web development, game development, and are also used in certain specialized industries like music.

Examples

PHP

PHP is an OODSL that is commonly used for web development. It is an open source web development framework that is designed to make web development simpler and easier. The language can be perceived as a general purpose language, but it also has domain specific components designed for the web domain alone. PHP is in general embedded into HTML to perform server side computations before the page is sent to the client.

OODSL compiler for FPGA

An object oriented domain specific compiler was developed for programming Field Programmable Gate Arrays.[5] The compiler allows the hardware programmer to make use of object-oriented style while programming. The research paper claims that this way of programming the hardware brings down the time taken for design by several notches and still results in good optimal performance.

Tools for Creating OODSL

When creating an OODSL, one can either choose to start from scratch or use a tool that is already available. Creating a language from scratch gives the programmer a certain amount of flexibility to define it the way he wants to, but it takes a lot of time. Therefore, in general, software tools are used to aid in the process of creating Domain Specific Languages. This is because it not only saves time, but also helps to make the language easy to use due to familiarity with the syntax of the underlying language.

Boo

Boo[6] is an open source object oriented language that is used to create Domain Specific languages. Defining basic data structures like array, hash, variables etc are inherently supported in Boo and it makes their creation easier. Although Boo is a statically typed language, these restrictions can be bent once in a while so that the type of objects can be found and manipulated in run time as well (Duck typing).

Visual Studio

Visual Studio has Domain-specific language tools that are designed to help the designer to build a language focusing on the language grammar and characteristics rather than paying much attention to the internal details of the compiler.[7] Visual tools are used that help focus on the business requirements of the OODSL rather than worrying about how it works internally.

Groovy

Groovy [8] is a dynamic language for the Java Virtual Machine. Also, the syntax is groovy is similar to that of Java, making designing of new OODSLs easier and similar to programming in Java. An example for developing an OODSL in Groovy.

OODSL vs. non-OODSL

When defining a DSL, choosing whether to go with an OODSL or a non-OODSL depends on several factors like the domain for which it is defined, existing DSLs for that domain etc. Non-OODSLs are domain specific languages that do not necessarily embrace object oriented concepts like abstraction, polymorphism etc. OODSLs can be well suited for some situations whereas in other situations, they might be an overkill. Defining a new OODSL when there is an already widely accepted non-OODSL may not be very useful. For example, HTML is the standard method for displaying web pages and it is non-OODSL. Defining a new non-OODSL that replaces HTML may not prove to be significantly advantageous.

Advantages and Disadvantages

Some of the advantages of OODSLs would be:


Though OODSLs in general have several advantages over other languages, one must also consider their disadvantages:

A Running Example

Given here is a simple example [9] for creating an OODSL. The DSL defined here simply prints the text we have specified in the program. This is defined in Groovy.

The definition of our new OODSL is as follows:

package com.solutionsfit.dsl.memotemplate
import groovy.xml.MarkupBuilder
class MemoDsl {
	String toString
	String fromString
	String messageString
	def sections = []
	/**
	* Make a closure. Main definition of the DSL program
	*/
	def static make(closure) {
		MemoDsl memoDsl = new MemoDsl()
		closure.delegate = memoDsl
		closure()
	}
	/**
	* Store the values in the local variables
	*/
	def to(String toString){
		this.toString = toString
	}
	def from(String fromText){
		this.fromString = bodyString
	}
	def message(String messageString){
		this.messageString = messageString
	}
	/**
	* The getText method gets called by the DSL by delegation
	*/
	def getText(){
		doText(this)
	}
	private static doText(MemoDsl memodsl){
		String template = "Memo\nTo: ${memoDsl.toText}\nFrom: 	${memoDsl.fromText}\n${memoDsl.body}\n"
		def sectionStrings =""
		for (s in memoDsl.sections) {
			sectionStrings += s.title.toUpperCase() + "\n" + s.body + "\n"
		}
		template += sectionStrings
		println template
	}
}

A sample program in our newly developed OODSL would be:

package com.solutionsfit.dsl.memotemplate

class MemolDslTest extends GroovyTestCase {
	void testDslUsage_outputText() {
		MemoDsl.make {
			to "Nirav Assar"
			from "Barack Obama"
			message "How are things? We are doing well. Take care"
			text
		}
	}
}

References

  1. OODSL 1[1]
  2. OODSL 2[2]
  3. Building DSLs [3]
  4. DSL examples[4]
  5. Object-Oriented Domain Specific Compilers for Programming FPGAs[5]
  6. Boo Home[6]
  7. Domain specific development[7]
  8. Groovy Home[8]
  9. Example DSL in groovy[9]
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox