Tuesday, November 3, 2009

Call a Struts 2 action using a javascript and load the response to a div

A common problem that Struts 2 frame work users face is how to call a Struts 2 action using a java script and load the response to a div, specially when you are not using Struts dojo tags. So by using the following function you can achieve this easily.

Note: You should include this function at the top most parent page.

dojo.require("dojo.io.IframeIO");
dojo.require("dojo.dom");

function sendToTarget(submitUrl , formId , divId, parameter1, parameter2){
dojo.require("dojo.io.IframeIO");
dojo.io.bind({
url:submitUrl,
formNode: dojo.byId(formId),
mimetype: "text/html",
transport: "IframeTransport",
preventCache: true,
multipart: true,

content:{
urlParameter1: parameter1,
urlParameter2: parameter2
},

load: function(type, data, evt) {
var divDisplay = dojo.byId(divId);
data = data.body.innerHTML;
divDisplay.innerHTML = data;},

error: function(type, errObj){
alert("error " + errObj.message);
}
});
}

Thursday, May 14, 2009

How to do a javascript validation in struts when using < sx : submit > to submit a form


Key here is to use the 'beforeNotifyTopics' attribute of the . In the value of this
attribute you have to specify the name of the javascript method that sould be called.

For example,

< sx : submit value="submit" targets="mainContentDiv" showloadingtext="false"
indicator="loadingImage"
beforenotifytopics="/beforeQuestionnaireSubmit"
id="submitQuestionnaireButton" / >

Then you have to add the javascript method that should do the validation in the following way

dojo.event.topic.subscribe("/beforeQuestionnaireSubmit", function(event, widget) {
var btn = dojo.byId("submitQuestionnaireButton");
btn.disabled = true;

var validationSuccess = true;
for(var i = 1; i < isanswered =" false;" j =" 0;" isanswered =" true" isanswered =" true" classname = "normal" classname = "normal" validationsuccess =" false;" classname = "highlight" classname = "highlight" validationsuccess ="=" disabled =" false;" cancel =" true;" name="'global.enquete.allanswer'/">");
}
});

Monday, May 11, 2009

Complete Source Code of a Back Propagation Neural Network in Java Using a Simple Example




Introduction

This example implements a Multi Layer Neural Network, which will behave as an XOR gate after being trained using the Back Propergation Algorithm.



The network has only two layers, the input layer and the output layer. It does not have any hidden layers. The input layer consist of two neurons and the output layer consist of only one neuron


Complete Source Code

import java.awt.*;

import java.awt.event.*;

import java.awt.image.*;

import javax.swing.*;

import java.io.*;


//*********************************************************************

class ImagePanel extends JPanel {

Image image;

public ImagePanel(Image image) {

this.image = image;

}

public void paintComponent(Graphics g) {

super.paintComponent(g); //paint background

//Draw image at its natural size.

g.drawImage(image, 0, 0, this); //85x62 image

}

}

//*********************************************************************

public class MultiLayer{

public static void main(String args[]){

Operations op=new Operations();

op.getInterface();

}

}

//*********************************************************************

class Operations{

//create arrays to hold the weights

double w1[]=new double[3];

double w2[]=new double[3];

double w3[]=new double[3];

double u;

void getInterface(){

String imageFile = "MultiLayer.gif";

String[] values={"0","1"};

Image image = Toolkit.getDefaultToolkit().getImage(imageFile);

if (image!=null){

final JFrame tstFrame = new JFrame("Multi Layer Neural Network");

tstFrame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

Container tstPane=tstFrame.getContentPane();

ImagePanel imagePanel = new ImagePanel(image);

SpringLayout layout = new SpringLayout();

imagePanel.setLayout(layout);

tstPane.add(imagePanel, BorderLayout.CENTER);

//text fields for the weights

final JTextField w[]=new JTextField[9];

w[0]=new JTextField("", 5);

w[0].setEditable(false);

imagePanel.add(w[0]);

w[1]=new JTextField("", 5);

w[1].setEditable(false);

imagePanel.add(w[1]);

w[2]=new JTextField("", 5);

w[2].setEditable(false);

imagePanel.add(w[2]);

w[3]=new JTextField("", 5);

w[3].setEditable(false);

imagePanel.add(w[3]);

w[4]=new JTextField("", 5);

w[4].setEditable(false);

imagePanel.add(w[4]);

w[5]=new JTextField("", 5);

w[5].setEditable(false);

imagePanel.add(w[5]);

w[6]=new JTextField("", 5);

w[6].setEditable(false);

imagePanel.add(w[6]);

w[7]=new JTextField("", 5);

w[7].setEditable(false);

imagePanel.add(w[7]);

w[8]=new JTextField("", 5);

w[8].setEditable(false);

imagePanel.add(w[8]);

final JTextField outText=new JTextField("", 5);

outText.setEditable(false);

imagePanel.add(outText);

final JTextField rateText=new JTextField("0.5", 5);

imagePanel.add(rateText);

final JComboBox tstcombo1=new JComboBox(values);

imagePanel.add(tstcombo1);

final JComboBox tstcombo2=new JComboBox(values);

imagePanel.add(tstcombo2);

JButton tstButton = new JButton("Run");

imagePanel.add(tstButton);

tstButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent event){

int x1=Integer.parseInt((String)tstcombo1.getSelectedItem());

int x2=Integer.parseInt((String)tstcombo2.getSelectedItem());

outText.setText(""+run(x1,x2)+"");

}

});

JButton trnButton = new JButton("Train");

imagePanel.add(trnButton);

trnButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent event) {

u=Double.parseDouble((String)rateText.getText());

performTrain(w);

JOptionPane.showMessageDialog(tstFrame, "Successfully Trained");

}

});

layout.putConstraint(SpringLayout.WEST,w[0],230,SpringLayout.WEST, imagePanel);

layout.putConstraint(SpringLayout.NORTH,w[0],121,SpringLayout.NORTH, imagePanel);

layout.putConstraint(SpringLayout.WEST,w[1],230,SpringLayout.WEST, imagePanel);

layout.putConstraint(SpringLayout.NORTH,w[1],78,SpringLayout.NORTH, imagePanel);

layout.putConstraint(SpringLayout.WEST,w[2],230,SpringLayout.WEST, imagePanel);

layout.putConstraint(SpringLayout.NORTH,w[2],171,SpringLayout.NORTH, imagePanel);

layout.putConstraint(SpringLayout.WEST,w[3],230,SpringLayout.WEST, imagePanel);

layout.putConstraint(SpringLayout.NORTH,w[3],292,SpringLayout.NORTH, imagePanel);

layout.putConstraint(SpringLayout.WEST,w[4],230,SpringLayout.WEST, imagePanel);

layout.putConstraint(SpringLayout.NORTH,w[4],352,SpringLayout.NORTH, imagePanel);

layout.putConstraint(SpringLayout.WEST,w[5],230,SpringLayout.WEST, imagePanel);

layout.putConstraint(SpringLayout.NORTH,w[5],403,SpringLayout.NORTH, imagePanel);

layout.putConstraint(SpringLayout.WEST,w[6],379,SpringLayout.WEST, imagePanel);

layout.putConstraint(SpringLayout.NORTH,w[6],170,SpringLayout.NORTH, imagePanel);

layout.putConstraint(SpringLayout.WEST,w[7],379,SpringLayout.WEST, imagePanel);

layout.putConstraint(SpringLayout.NORTH,w[7],221,SpringLayout.NORTH, imagePanel);

layout.putConstraint(SpringLayout.WEST,w[8],379,SpringLayout.WEST, imagePanel);

layout.putConstraint(SpringLayout.NORTH,w[8],280,SpringLayout.NORTH, imagePanel);

layout.putConstraint(SpringLayout.WEST,rateText,535,SpringLayout.WEST, imagePanel);

layout.putConstraint(SpringLayout.NORTH,rateText,78,SpringLayout.NORTH, imagePanel);

layout.putConstraint(SpringLayout.WEST,outText,540,SpringLayout.WEST, imagePanel);

layout.putConstraint(SpringLayout.NORTH,outText,218,SpringLayout.NORTH, imagePanel);

layout.putConstraint(SpringLayout.WEST,tstButton,430,SpringLayout.WEST, imagePanel);

layout.putConstraint(SpringLayout.NORTH,tstButton,130,SpringLayout.NORTH, imagePanel);

layout.putConstraint(SpringLayout.WEST,trnButton,500,SpringLayout.WEST, imagePanel);

layout.putConstraint(SpringLayout.NORTH,trnButton,130,SpringLayout.NORTH, imagePanel);

layout.putConstraint(SpringLayout.WEST,tstcombo1,70,SpringLayout.WEST, imagePanel);

layout.putConstraint(SpringLayout.NORTH,tstcombo1,118,SpringLayout.NORTH, imagePanel);

layout.putConstraint(SpringLayout.WEST,tstcombo2,75,SpringLayout.WEST, imagePanel);

layout.putConstraint(SpringLayout.NORTH,tstcombo2,353,SpringLayout.NORTH, imagePanel);

tstFrame.setSize(new Dimension(650,510));

tstFrame.setLocationRelativeTo(null);

tstFrame.setVisible(true);

}

}

void performTrain(JTextField w[]){

for(int i=0;i<3;i++){

w1[i]=Math.random();

w2[i]=Math.random();

w3[i]=Math.random();

}

w[0].setText((Math.round(10000*w1[0])/10000)+"");

w[1].setText((Math.round(10000*w1[1])/10000)+"");

w[2].setText((Math.round(10000*w1[2])/10000)+"");

w[3].setText((Math.round(10000*w2[0])/10000)+"");

w[4].setText((Math.round(10000*w2[1])/10000)+"");

w[5].setText((Math.round(10000*w2[2])/10000)+"");

w[6].setText((Math.round(10000*w3[0])/10000)+"");

w[7].setText((Math.round(10000*w3[1])/10000)+"");

w[8].setText((Math.round(10000*w3[2])/10000)+"");

for (int i=0;i<100000;i++){

train(0,0,0,w);

train(0,1,1,w);

train(1,0,1,w);

train(1,1,0,w);

}

w[0].setText((Math.round(10000*w1[0])/10000)+"");

w[1].setText((Math.round(10000*w1[1])/10000)+"");

w[2].setText((Math.round(10000*w1[2])/10000)+"");

w[3].setText((Math.round(10000*w2[0])/10000)+"");

w[4].setText((Math.round(10000*w2[1])/10000)+"");

w[5].setText((Math.round(10000*w2[2])/10000)+"");

w[6].setText((Math.round(10000*w3[0])/10000)+"");

w[7].setText((Math.round(10000*w3[1])/10000)+"");

w[8].setText((Math.round(10000*w3[2])/10000)+"");

}

void train(double a,double b,double c,JTextField w[]){

double x1=a;

double x2=b;

double t=c;

double m=(1*w1[0])+(x1*w1[1])+(x2*w1[2]);

double y1=1/(1+Math.exp(-m));

double n=(1*w2[0])+(x1*w2[1])+(x2*w2[2]);

double y2=1/(1+Math.exp(-n));

double p=(1*w3[0])+(y1*w3[1])+(y2*w3[2]);

double z =1/(1+Math.exp(-p));

if (t==1 && z>0.95){

return;

}

else{

double d3=z*(1-z)*(t-z);

double d2=y2*(1-y2)*w3[2]*d3;

double d1=y1*(1-y1)*w3[1]*d3;

w1[0]=w1[0]+(u*1*d1);

w1[1]=w1[1]+(u*x1*d1);

w1[2]=w1[2]+(u*x2*d1);

w2[0]=w2[0]+(u*1*d2);

w2[1]=w2[1]+(u*x1*d2);

w2[2]=w2[2]+(u*x2*d2);

w3[0]=w3[0]+(u*1*d3);

w3[1]=w3[1]+(u*x1*d3);

w3[2]=w3[2]+(u*x2*d3);

}

if (t==0 && z<0.05){

return;

}

else{

double d3=z*(1-z)*(t-z);

double d2=y2*(1-y2)*w3[2]*d3;

double d1=y1*(1-y1)*w3[1]*d3;

w1[0]=w1[0]+(u*1*d1);

w1[1]=w1[1]+(u*x1*d1);

w1[2]=w1[2]+(u*x2*d1);

w2[0]=w2[0]+(u*1*d2);

w2[1]=w2[1]+(u*x1*d2);

w2[2]=w2[2]+(u*x2*d2);

w3[0]=w3[0]+(u*1*d3);

w3[1]=w3[1]+(u*x1*d3);

w3[2]=w3[2]+(u*x2*d3);

}

}

double run(double a,double b){

double x1=a;

double x2=b;

double m=(1*w1[0])+(x1*w1[1])+(x2*w1[2]);

double y1=1/(1+Math.exp(-m));

double n=(1*w2[0])+(x1*w2[1])+(x2*w2[2]);

double y2=1/(1+Math.exp(-n));

double p=(1*w3[0])+(y1*w3[1])+(y2*w3[2]);

double z =1/(1+Math.exp(-p));

return Math.round(z);

}

}

Background Image (MultiLayer.gif)

Use above image as the Background Image. Sorry for not uploading a complete description of the coding. But sure that all you smart guys out there will be able to understand this simple code and if I find time I will upload a complete description of the source and the back propagation algorithm.