random thoughts

Build your own Universe ?

I have always wondered about this, what if we are just objects in a game like a strategy  game Caesar/Age of Empires/Zoo Tycoon and we are just instances of a class interacting with each other and some kid is watching us and playing for his own amusement.

Then I heard this article on NPR in this article scientist theorize that one day you will be able to build your own universe.  What if …………………….. somebody has actually done this?

Imagine this, instead of presenting your nephew with an xbox360 you give him a do-it-yourself Home Universe Kit. Open the box, plug it in and there it is a universe ready to run and amuse you. Pick a time line, setup some rules and watch it play.

We maybe just part of this kit. Come to think of it, why is it that every civilization that ever existed has the notion of God? Is that some kind of base rule that was setup? Are ghosts just memory leaks? Is sleep just a way to run Garbage Collection?

Will we ever know ? And more important , do I want to know ?

General

Follow up to the Use Ruby to Prefix all lines /Postfix all lines/Remove empty lines/Replace strings in all lines of a file

In my previous post, I showed a ruby program that can help you iterate over lines of a file and make changes to the file, here I rewritten the code in a much more ruby-way, where I am using code blocks and iterators take a look
module DevFileutils
include FileUtils::Verbose

# Open file, iterate through each line and yield on each line, take results of yield
def each_line_of_file file_name
temp = File.new(file_name+”.tmp”, ‘a+’)
IO.foreach(file_name) do |line|
value = yield(line)
temp.puts yield(line) if value
end
temp.close
mv file_name+’.tmp’, file_name
end

# Opens a file and adds prefix to each line
def prefix  file_name, prefix
each_line_of_file(file_name) { |line| prefix + line }
end

# Opens a file and adds postfix to each line
def postfix file_name, postfix
each_line_of_file(file_name) { |line| line.chomp + postfix }
end

#removes all empty lines from a file
def remove_empty_lines file_name
each_line_of_file(file_name) { |line| line if !line.chomp.empty? }
end

def replace file_name,string,with_string
each_line_of_file(file_name) { |line| line.gsub(string,with_string) }
end
end

General, ruby, Tech

Use Ruby to Prefix all lines /Postfix all lines/Remove empty lines/Replace strings in all lines of a file

For some reason or the other I had to do bulk operations on files, like prefix/postfix all lines or remove empty lines
, yeah I could do it with some unix command, but I am already using ruby to massage the data just wanted to extand that.So here
is what I did wrote a ruby script to do it now I can do stuff like

prefix ( ‘c:\tmp\zip.txt’,’some-prefix’ )

The script, hmmm I wonder if I could submit this to ruby people.

require 'fileutils'

module DevFileutils
include FileUtils::Verbose

# Opens a file and adds prefix to each line
def prefix file_name, prefix
temp = File.new(file_name+".tmp", 'a+')
IO.foreach(file_name) do |line|
temp.puts prefix + line
end
temp.close
mv file_name+'.tmp', file_name
end

# Opens a file and adds postfix to each line
def postfix file_name, postfix
temp = File.new(file_name+".tmp", 'a+')
IO.foreach(file_name) do |line|
temp.puts line.chomp + postfix
end
temp.close
mv file_name+'.tmp', file_name
end

#removes all empty lines from a file
def remove_empty_lines file_name
temp = File.new(file_name+".tmp", 'a+')
IO.foreach(file_name) do |line|
temp.puts line if !line.chomp.empty?
end
temp.close
mv file_name+'.tmp', file_name
end

def replace file_name,string,with_string
temp = File.new(file_name+".tmp", 'a+')
IO.foreach(file_name) do |line|
temp.puts line.gsub(string,with_string)
end
temp.close
mv file_name+'.tmp', file_name
end
end

General

New Job, New Domain

As of October 16th I started a new job, things are finally settling down and life is coming back to order.

Now I work for Apollo Group (also know as University of Phoenix), including this job now I have worked in 4 different domains

1. Education ( present )
2. Government ( Arizona state retirement system )
3. Financial ( vitalps, now know as http://www.tsysacquiring.com/)
4. Travel (Sabre Inc)

Each with its own unique characteristics/problems and unique solutions. It has been great, maybe I’ll write a post about each domain.

Tech

Bank Of America Login Script

def login
@id = ‘USERNAME’
@pwd = ‘PASSWORD’
puts “logging in ..”
ie = Watir::IE::start(‘http://www.bankofamerica.com/index.cfm’)
ie.set_fast_speed
puts “entering online id”
ie.text_field(:id, ‘id’).set(@id)
window = ie.ie.Document.parentWindow
window.execScript(“doPassmarkSignIn()”)
puts “Enter passcode”
ie.wait
ie.text_field(:index ,1).set(@pwd)
window = ie.ie.Document.parentWindow
window.execScript(“verifyImageForm_0_submit()”)
puts “done.”
end

Tech

Using Jboss Rules With Spring

http://wiki.jboss.org/wiki/Wiki.jsp?page=RulesWithSpringFramework is an excellent article on how to do this. I have made some slight modifications to this to use excel files instead of drl files and pass in a path rather than specify each file individually. Here is the code and configuration

<bean id=”huntGroupRuleBase” class=”edu.apollogrp.qtask.routing.util.RuleBaseBeanFactory”>
<property name=”drlResourcePath” value=”classpath:hunt-group-rules/*.xls”/>
<property name=”packageBuilderConfiguration”>
<bean class=”org.drools.compiler.PackageBuilderConfiguration”>
<!–<property name=”compiler” value=”PackageBuilderConfiguration.JANINO”/> –>
<property name=”compiler” value=”1″/>
</bean>
</property
</bean>

package edu.apollogrp.qtask.routing.util;

import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.List;

import org.apache.log4j.Logger;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.compiler.PackageBuilder;
import org.drools.compiler.PackageBuilderConfiguration;
import org.drools.decisiontable.InputType;
import org.drools.decisiontable.SpreadsheetCompiler;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;

/**
* @author Geoffrey De Smet
* @see
http://wiki.jboss.org/wiki/Wiki.jsp?page=RulesWithSpringFramework
*
*/
public class RuleBaseBeanFactory implements FactoryBean, InitializingBean,
ApplicationContextAware {

private static final Logger log = Logger
.getLogger(RuleBaseBeanFactory.class);

private String drlResourcePath;

private RuleBase ruleBase;

private PackageBuilderConfiguration packageBuilderConfiguration;

private ApplicationContext applicationContext;

public void afterPropertiesSet() throws Exception {
Resource[] drlResourceList = applicationContext
.getResources(drlResourcePath);

Assert.notEmpty(drlResourceList, “drlResourceList must not be empty”);

PackageBuilder builder = (packageBuilderConfiguration != null ? new PackageBuilder(
packageBuilderConfiguration)
: new PackageBuilder());

SpreadsheetCompiler compiler = new SpreadsheetCompiler();

for (Resource resource : drlResourceList) {
log.info(“Reading file: ” + resource.getFilename());

Reader input = null;

// check the extension to see if this is a decision table or regular
// drl
if (resource.getFilename().endsWith(“drl”)) {
input = new InputStreamReader(resource.getInputStream());
} else if (resource.getFilename().endsWith(“xls”)) {
String drl = compiler.compile(resource.getInputStream(),
InputType.XLS);
log.debug(drl);
input = new StringReader(drl);
} else {
log.warn(“Drools resource has unknown file type: ”
+ resource.getFilename());
}
if (input != null) {
builder.addPackageFromDrl(input);
}
}
ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage(builder.getPackage());
}

public RuleBase getObject() {
return ruleBase;
}

public Class getObjectType() {
return RuleBase.class;
}

public boolean isSingleton() {
return true;
}

public void setPackageBuilderConfiguration(
PackageBuilderConfiguration packageBuilderConfiguration) {
this.packageBuilderConfiguration = packageBuilderConfiguration;
}

public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}

public void setDrlResourcePath(String drlResourcePath) {
this.drlResourcePath = drlResourcePath;
}

}

General, Tech

Behind every successful project is a set of really good people

After a lot of thinking and years of experience if it is one thing I have realized it is this, Behind every successful project is a set of really good people.

Software development is like running a kitchen, if you have great cooks and great helpers in your kitchen, no matter how bad the recipes are the dishes will turn out fine. If you have bad cooks no matter how good and detailed your recipes are the dishes are ruined (Read “Big Macs vs. The Naked Chef ” Joel Spolsky).

There are other factors too like using a good methodology and using the right tools, but I think these other factors just add up to 10% .

Like a real good chef a really good programmer is agile, if he finds that there is no Source control, he will just set on up on his machine (or commandeer an old machine set up Linux and SVN on it), if he finds that there is no Wiki, he will set up a TiddlyWiki, if the requirements don’t make sense he will contact the user to clear things up. If he doesn’t have the right tools he will just get it, now it will help if the right tools are already in place.

It also helps to have a good project lead who will be open to adopting new practices but that goes back to having good people on your team, after all a project lead is also part of your team right ?

 

Tech

Practices of an Agile Developer

I just finished reading “Practices of an Agile Developer” by Venkat Subramaniam and Andy Hunt, now don’t let the title fool you, this book is not only for developers but for anyone working on the team (yes it is for you too managers, leads and architects)One of the practices they mention is “Deploy your application automatically from the start”

This practice makes total sense, invest in building and deploying automatically, trust me this will save you a whole lot of headache later. If you are still manually building you application then beat yourself with a stick cause every manual step can lead to a mistake and hours of headache.

Not to mention you will have one guy who knows how to do it god forbid he goes on vacation and if something goes wrong your highly paid programmers will by spending hours trying to figure out what the hell went wrong and that is not a nice way to spend cash.

And with your luck it will happen in the most crucial moment. (99% SLA, Saturday night, 2 hour window to deploy anything and your application takes 2 hours to build) And don’t tell me your build cannot be automated, yes it can !! put you best developers on it and give em some time and see what happens. And if you are going to automate it start from day one.

Tech

Using Spring and Quartz

At ASRS we use a combination of spring and quartz to schedule and run many jobs. A job is a java program that we want to run automatically at a specified time/date.

To understand how spring and quartz work together to create jobs take a look at chapter 22 of the spring reference book http://www.springframework.org/docs/reference/scheduling.html , this will give you a good understanding on how it works also take a look at Quartz itself, http://www.opensymphony.com/quartz/

How to define your own Job

Lets say you have a bean

public class MyBean { public void doSomeThing(){ ?.. } }

You want the method doSomeThing to be executed every day, to do this

1. Define your bean in your spring file as you would any other bean

<ean id=?myBean? class=?MyBean?/>

2. Then define a Job Detail bean, this is done to let quartz know what bean and what method to call

<bean id=”myBeanJobDetail” class=”org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean”>

<!–Name of your bean goes here –>

<property name=”targetObject” ref=”myBean “/>

<!– Name of your method goes here –>

<property name=”targetMethod” value=”doSomeThing “/>

<!– This lets quartz know that 2 instances of this job cannot be run at the same time –>

<property name=”concurrent” value=”false”/>

</bean>

3. Now define the trigger, this tells quartz on what schedule you want it to run

<bean id=”myBeanTrigger”

class=”org.springframework.scheduling.quartz.CronTriggerBean”>

<!–Name of the JobDetail you are scheduling–>

<property name=”jobDetail” ref=” myBeanJobDetail “/>

<!– If an job is not fired at the right Time what to do about it –>

<property name=”misfireInstructionName”

value=”MISFIRE_INSTRUCTION_FIRE_ONCE_NOW”/>

<!–Finally at what schedule you want it to run –>

<!– Fire at 00:00 am 1 day of the month –>

<property name=”cronExpression” value=” * 0/5 * * * ?”/>

</bean>

To learn more about using corn expressions please check http://wiki.opensymphony.com/display/QRTZ1/CronTriggers+Tutorial

4. Most important step initiate your trigger , to do this go to the asrs-quartz-jobs project and add your trigger to the already existing triggers

<description>All Quartz Jobs</description>

<bean class=”org.springframework.scheduling.quartz.SchedulerFactoryBean”>

<property name=”triggers”>

<list>

<!– Mail Trigger –>

<ref bean=”mailcronTrigger”/>

<!– Workflow –>

<ref bean=”workflowBatchProcessTrigger”/>

<!–My Trigger –>

<ref bean=”myBeanTrigger”/>

</list>

</property>

</bean>

All the triggers must be added to the spring file, as soon as the spring context is loaded you triggers will be scheduled by the Scheduler Factory and run at your defined times