CherryBlossom
I’ve created a project page for the CherryBlossom programming language. You can check it out here. The interpreter is written in perl.
I’ve created a project page for the CherryBlossom programming language. You can check it out here. The interpreter is written in perl.
Over the past month, I’ve been working on a new project. It’s called CherryBlossom, and it’s a way to write programs using haikus. Strictly speaking, CherryBlossom is a brainfuck analog. I actually spent more time writing the obligatory “Hello World” program in CherryBlossom than I did writing the interpreter for the language. The idea behind CherryBlossom is simple. Brainfuck instructions are mapped to words that convey the essence of the Brainfuck instruction. Of course, this is a little subjective and also a little abstract.
Ultimately, it serves as a way to make program code not just functional, but beautiful and artistic. Thus, we introduce a new criteria to programming. Your code must not only be elegant algorithmically, but must also be poetic and artistic (also, since program code consists of haikus, you need to represent your code in sets of 3 lines with the first and last lines having 5 syllables, and the second line 7. That is, conforming to haiku rules). CherryBlossom serves to blend the programmer and the poet into one entity (hopefully with amazing results).
Here is an example of “Hello World!” in CherryBlossom. I have opted to use a spruced up div tag instead of enclosing my beautiful poem in soulless sourcecode tags.
Read more…
Guus was kind enough to make a maven project for the Generic Tree. He also fixed an error in my equals method for the GenericTreeNode (I intended to do Object.equals(Object obj) but was doing GenericTreeNode.equals(GenericTreeNode obj). Since it’s a maven project, you can easily create a jar out of it and add it as a dependency to your project. You can download the project here. Thanks Guus!
Last week I was solving a problem at work that required the use of a Generic (n-ary) Tree. An n-ary tree is a tree where node can have between 0 and n children. There is a special case of n-ary trees where each node can have at most n nodes (k-ary tree). This implementation focuses on the most general case, where any node can have between 0 and n children. Java doesn’t have a Tree or Tree Node data structure. I couldn’t find any third-party implementations either (like in commons-lang). So I decided to write my own.
Read more…
I was talking to my CSE 200 (my first CS class at ASU) professor Richard Whitehouse about bAdkOde and he (rightly) pointed out that it didn’t have an explicit selection statement. He also said that unless I wanted it to be really ugly I’d need to have a selection statement. However, since I was going for ugly I figured that I’d just emulate the operation of an if and an if-else with the existing while statement.
If you know assembly, then you know that a while is simply a set of statements wrapped with a conditional branch at the top and a backwards branch at the bottom (or in other words, an if with a goto at the end. A do-while is simply a set of statements with a conditional branch (at the bottom) that branches to the top of the loop. In fact, in assembly programming there really aren’t any for loops or while loops. These keywords are simply abstractions and syntactic sugar. In bAdkOde, you can implement an if with the existing while statement if you explicitly make it break out of the loop. For example, let’s say that we want to check whether the user entered the character “0″:
?a
-48a
# print a line break
"10
# if a is zero
{=a
# print the string "zero"
"122"101"114"111"10
# set the a register to a non-zero value so that we can break out of the loop
>1a
}
I knew there was a way to implement an if-else with just while statements but I didn’t remember exactly how. Then my friend and co-worker Juan reminded me that I needed two variables. In our case, we need to use two registers:
?a
-48a
# copy the value of a into b
>ab
"10
# if a is zero
{=a
# print the string "zero"
"122"101"114"111"10
# set the a register to a non-zero value so that we can break out of the loop
>1a
}
# if the top loop failed, it means that b (which holds the same value as a) is
# non-zero and so we can enter this block.
# if the top loop was successful, it means that b (which holds the same value
# as a) is zero and so we won't enter this block. Hence, the second block acts
# as an 'else'
{!b
# print the string "non-zero"
"110"111"110"45"122"101"114"111"10
# zero out the b register
>0b
}
Yes, quite ugly. But that’s what I’m going for!
I’ve added another project to the projects page. It’s called bAdkOde, an interpreter for an esoteric language that I designed. The very first incarnation of bAdkOde was written in Java and I actually posted it (or made a blog entry about it) over 8 years ago. For some reason I took it down. Probably because I stopped working on it. Anyway, I redesigned the language and wrote an interpreter for it in Perl about 4 or 5 years ago. I finally got around to posting it. Check out the project page for more details. Let me know what you think.
I’ve been doing a little bit of JSTL over the past week, especially custom tags. I’ve written custom tags in Grails before, and there you use actual Groovy code. I guess this was how custom tags used to be written (in Java), but now you can can build your own custom tags using the standard tag library. The standard tag library is still pretty useful when it comes to building custom tags. Since it’s not straight Java, it forces you think really hard about your logic. You don’t want to put any business or application logic in your tag, and you want to restrict everything to view or presentation logic. A side effect of it not being Java is that if you want to do anything extremely complicated, you’re probably better off writing the tag in Java (making sure that you don’t let any business logic creep in).
While writing my own custom tag, I noticed that although instanceof is a reserved word in the JSTL EL (expression language), it is not supported as an operator. The reason I wanted to use the instanceof operator is that I have an attribute that could either be a List or a Map and depending on the type, I wanted to do different things.
Another thing I was trying to do, was to inspect the incoming object to see if it had a certain property (reflection). JSTL uses reflection so that you can access the properties of an object via dot notation, if they follow the JavaBean naming-convention. However, there was no way for me to see if an object had a certain property. To solve both these problems, I wrote my own JSTL functions.
Read more…
About a month ago at work, I was trying out a bunch of different performance-testing tools to figure out which one to use to performance-test our software. I ended up discovering a tool called The Grinder which uses Jython to build performance-testing scripts that you can then use to test your application. I even built a little framework in Jython to make the test scripts more modular and reusable. Over the next few days I’ll be publishing three articles (this one included) that talk about Grinder.
Introduction
In this article I’ll go over installing and setting up Grinder. I am not going to go into too many details since my aim is to provide information that will enable you to have Grinder up and running quickly. If you want more information, you can look at the rather thorough Grinder User Guide. At the end of this guide, you’ll know how to install and set up grinder, record a test, modify (and/or parameterize) it, start running it through the console, and record data from the test. Note: The instructions in this guide relate to a Linux environment. You can run Grinder in Windows; the set-up is not much different. The only differences will be in installation locations and shell scripts. If you want more information about setting up Grinder in a Windows environment, please take a look here.
Read more…
This is an update to my instructions on running the JavaFX 1.0 SDK on Linux. Those instructions do not work on the dmg image for the 1.1 version of the SDK.
Mike (thanks Mike!) posted a comment on that blog mentioning a small change that needed to be made. To get JavaFX 1.1 on Linux, first follow the steps in the original guide. When you need to mount the dmg, you need to provide an offset. So instead of the original command, do the following:
vivin@dauntless ~ $; sudo mount -o loop,offset=$((1024*17)) -t hfsplus javafx_sdk-1_0-macosx-universal.dmg.out javafx
The dmg should be mounted now.
I’ve recently started working with Groovy on Grails at work. Groovy is a dynamic language that runs on the JVM (Java Virtual Machine), and Grails is a high-productivity open-source web-application framework that leverages the Groovy language. Grails follows the “convention over configuration” principle, taking away a lot of the grunt work that goes into creating web applications. It also uses proven technology like Hibernate and Spring with a consistent interface. You can have a Grails app up and ready to go with only a few lines of code. This high productivity is also due to the fact that Grails uses Groovy, which is a dynamic language and is much more expressive than Java. Finally, because Groovy runs on the JVM, it has full access to Java’s rich API.
Groovy comes with a number of built-in tags which allow you to quickly create forms, form controls, or any number of other HTML constructs or elements. However, I noticed that Groovy didn’t provide a built-in tag to build a SELECT that includes OPTGROUPs. OPTGROUPs allow you to group options within a drop-down select-box. But this problem can be solved because Groovy supports the creation of custom tags. After reading a bunch of documentation and looking at a few examples, I was able to create my own SELECT tag that supports OPTGROUPs. Here is an example of the usage of the tag:
<g:selectWithOptGroup name = "song.id"
from = "${Song.list()}"
optionKey = "id"
optionValue = "songName"
groupBy = "album" />
And the code to implement this tag:
import org.springframework.beans.SimpleTypeConverter
import org.springframework.web.servlet.support.RequestContextUtils as RCU
import org.codehaus.groovy.grails.commons.DomainClassArtefactHandler
class MyAppTagLib {
def selectWithOptGroup = {attrs ->
def messageSource = grailsAttributes.getApplicationContext().getBean("messageSource")
def locale = RCU.getLocale(request)
def writer = out
def from = attrs.remove('from')
def keys = attrs.remove('keys')
def optionKey = attrs.remove('optionKey')
def optionValue = attrs.remove('optionValue')
def groupBy = attrs.remove('groupBy')
def value = attrs.remove('value')
def valueMessagePrefix = attrs.remove('valueMessagePrefix')
def noSelection = attrs.remove('noSelection')
def disabled = attrs.remove('disabled')
Set optGroupSet = new TreeSet();
attrs.id = attrs.id ? attrs.id : attrs.name
if (value instanceof Collection && attrs.multiple == null) {
attrs.multiple = 'multiple'
}
if (noSelection != null) {
noSelection = noSelection.entrySet().iterator().next()
}
if (disabled && Boolean.valueOf(disabled)) {
attrs.disabled = 'disabled'
}
// figure out the groups
from.each {
optGroupSet.add(it.properties[groupBy])
}
writer << "<select name=\"${attrs.remove('name')}\" "
// process remaining attributes
outputAttributes(attrs)
writer << '>'
writer.println()
if (noSelection) {
renderNoSelectionOption(noSelection.key, noSelection.value, value)
writer.println()
}
// create options from list
if (from) {
//iterate through group set
for(optGroup in optGroupSet) {
writer << " <optgroup label=\"${optGroup.encodeAsHTML()}\">"
writer.println()
from.eachWithIndex {el, i ->
if(el.properties[groupBy].equals(optGroup)) {
def keyValue = null
writer << '<option '
if (keys) {
keyValue = keys[i]
writeValueAndCheckIfSelected(keyValue, value, writer)
}
else if (optionKey) {
if (optionKey instanceof Closure) {
keyValue = optionKey(el)
}
else if (el != null && optionKey == 'id' && grailsApplication.getArtefact(DomainClassArtefactHandler.TYPE, el.getClass().name)) {
keyValue = el.ident()
}
else {
keyValue = el[optionKey]
}
writeValueAndCheckIfSelected(keyValue, value, writer)
}
else {
keyValue = el
writeValueAndCheckIfSelected(keyValue, value, writer)
}
writer << '>'
if (optionValue) {
if (optionValue instanceof Closure) {
writer << optionValue(el).toString().encodeAsHTML()
}
else {
writer << el[optionValue].toString().encodeAsHTML()
}
}
else if (valueMessagePrefix) {
def message = messageSource.getMessage("${valueMessagePrefix}.${keyValue}", null, null, locale)
if (message != null) {
writer << message.encodeAsHTML()
}
else if (keyValue) {
writer << keyValue.encodeAsHTML()
}
else {
def s = el.toString()
if (s) writer << s.encodeAsHTML()
}
}
else {
def s = el.toString()
if (s) writer << s.encodeAsHTML()
}
writer << '</option>'
writer.println()
}
}
writer << '</optgroup>'
writer.println()
}
}
// close tag
writer << '</select>'
}
void outputAttributes(attrs) {
attrs.remove('tagName') // Just in case one is left
attrs.each {k, v ->
out << k << "=\"" << v.encodeAsHTML() << "\" "
}
}
def typeConverter = new SimpleTypeConverter()
private writeValueAndCheckIfSelected(keyValue, value, writer) {
boolean selected = false
def keyClass = keyValue?.getClass()
if (keyClass.isInstance(value)) {
selected = (keyValue == value)
}
else if (value instanceof Collection) {
selected = value.contains(keyValue)
}
else if (keyClass && value) {
try {
value = typeConverter.convertIfNecessary(value, keyClass)
selected = (keyValue == value)
} catch (Exception) {
// ignore
}
}
writer << "value=\"${keyValue}\" "
if (selected) {
writer << 'selected="selected" '
}
}
def renderNoSelectionOption = {noSelectionKey, noSelectionValue, value ->
// If a label for the '--Please choose--' first item is supplied, write it out
out << '<option value="' << (noSelectionKey == null ? "" : noSelectionKey) << '"'
if (noSelectionKey.equals(value)) {
out << ' selected="selected" '
}
out << '>' << noSelectionValue.encodeAsHTML() << '</option>'
}
private String optionValueToString(def el, def optionValue) {
if (optionValue instanceof Closure) {
return optionValue(el).toString().encodeAsHTML()
}
el[optionValue].toString().encodeAsHTML()
}
}
This is my very first attempt and so I’m sure some not-so-best practices abound. Feedback and comments are welcome.
Update