Wednesday, May 4, 2011

Extending Your Applications with Bean Scripting Framework



Listing 1
package stat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collections;
import java.util.HashMap;

public class Stats {

public ArrayList nums;

public Stats(ArrayList someNums){
nums = new ArrayList(someNums);
}

public Stats(double[] someNums){
nums = new ArrayList(someNums.length);

for (int index=0; index  < someNums.length; index++){
nums.add(new Double(someNums[index]));
}
}



public double getMean (){
return this.getMean(true);
}

public double getMean (boolean sample){
// Define mean that finds two types of mean, namely:
// population mean and sample mean
double sum=0.0;
double average=0.0;
Iterator iterator = nums.iterator();

while(iterator.hasNext())
sum = sum + ((Double)iterator.next()).doubleValue();


// Check to see if this is a sample mean
if(sample)
average = sum / nums.size()-1;
else
average = sum / nums.size();

return average;
}

public ArrayList getRange (){
// Find the range. Returns a tuple with the minimum,
// maximum, and range value
double min, max;
ArrayList ranges;

min = ((Double)Collections.min(nums)).doubleValue();
max = ((Double)Collections.max(nums)).doubleValue();


ranges = new ArrayList();
ranges.add(new Double (min));
ranges.add(new Double (max));
ranges.add(new Double (max-min));

return ranges;
}

public double getMedian (){
// Find the Median number

// create a duplicate since we are going to modify the 
   // sequence
ArrayList seq = new ArrayList(nums);

// sort the list of numbers
Collections.sort(seq);

double median = 0.0; // to hold the median value

int length = seq.size(); // to hold the length of the 
                         // sequence
int index=0;

// Check to see if the length is an even number
if ( ( length % 2) == 0){
// since it is an even number
// add the two middle number together
index = length / 2;
double m1 = ((Double)seq.get(index-1)).doubleValue();
double m2 = ((Double)seq.get(index)).doubleValue();
median = (m1 + m2) /2.0;
}
else{
// since it is an odd number
// just grab the middle number
index = (length / 2);
median = ((Double)seq.get(index)).doubleValue();
}
return median;

}

private int countMode(Object object, ArrayList list){
int index = 0;
int count = 0;
do {
index = Collections.binarySearch(list, object);
if(index >=0)list.remove(index);
count++;
}
while (index >=0);
return count;
}

public double getMode (){
// Find the number that repeats the most.

// make a duplicate copy of the nums argument
ArrayList duplicate = new ArrayList(nums);

Collections.sort(duplicate);
double highest_count = -100;
double mode = -100;


Iterator iterator = nums.iterator();
// iterate through nums removing each item out of the duplicate
// calculate the highest_count and the mode
while(iterator.hasNext()){
double count = 0;
Object item = iterator.next();

// Count the number of times the item occurs in the list
// If Count is 0 go to the next iteration
count = countMode(item, duplicate);
if (count == 0) continue;

// determine the highest count. The highest counted item is the mode.
if (count > highest_count){
highest_count = count;
mode = ((Double)item).doubleValue();
}
}


return mode;
}

}

Listing 2
print "The mode of the houses is %2.2f" %  houses.mode
print "The mean of the houses is %2.2f" % houses.mean
print "The median of the houses is %2.2f" % houses.median

Listing 3
import com.ibm.cs.util.IOUtils;
import com.ibm.bsf.BSFManager;
import com.ibm.bsf.BSFEngine;
import java.io.FileReader;
import stat.Stats;

class Test {
public static void main (String [] args){
BSFManager manager = new BSFManager ();


//Register JPython into the scripting manager.
String[] extensions = {"py"};
manager.registerScriptingEngine ("jpython",
"com.ibm.bsf.engines.jpython.JPythonEngine",
extensions);

try{
//Load JPython engine
BSFEngine jpythonEngine = manager.loadScriptingEngine ("jpython");

//Execute an expression.
Object result = manager.eval ("jpython", "testString", 0, 0, "2+32");
System.out.println("eval="+result);

//Map some objects that the script will use.
double[] houses=new double [] {100.0e3, 130.0e3, 140e3, 150e3};
Stats stats = new Stats(houses);
manager.declareBean("houses", stats, Stats.class);

//Load the script and execute it.
String fileName = "TestStat2.py";
String language = manager.getLangFromFilename(fileName);
String script = IOUtils.getStringFromReader(new FileReader(fileName));
manager.exec(language, fileName, 0, 0, script);

}//try
catch (Exception e){
System.out.println(""+e);
}//catch

}//main
}//Test

No comments:

Post a Comment