Monday, February 11, 2013

Back propagation neural network Java example source code

This example implements a multi-layer neural network, which will behave as an XOR gate after being trained using the back propagation 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


You can find the NeuralNetwork.jar in the following link which includes the complete source code, compiled classes and images.
After downloading the jar file you can run the application by executing the following command.

java -classpath NeuralNetwork.jar com.neural.network.main.AppMain



First press Train button to calculate the weights. A message will be displayed saying you have successfully trained and the weights will get populated to the weight text boxes.

Afterwards you can select input values using the x1 an x2 combo boxes and Press Run button to test whether the neural network produces the correct output.



Saturday, February 9, 2013

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'/">");
}
});

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);
                    }
          });
}

Enable TCP/IP remote connections for SQL server and give user permission

1)  Open Microsoft SQL Server Management Studio and create a new user Security -> Logins -> New Login


2)  Grant privileges to the newly created user


3)  Right click server instance and go to Server Properties and make sure that remote connections are allowed to this server.


4)  Now open up SQL Server Configuration Manager


5)  In SQL Server Services make sure the SQL Server is running


Note: Some times when you select SQL Server Services, right hand side shows an error server threw an exception [0x80010105]. Solution for this is, go to Services and scroll down to Windows Management Instrumentation service and Restart it. Now when you close and reopen the Server Configuration Manager, you will see the full list of SQL Server Services.

6)  Next select Protocols for the server instance and make sure the TCP/IP is enabled


7)  Finally go to TCP/IP properties. In localhost(in this instance IP9) and in IP All, remove all dynamic ports and add default port 1433 as TCP port



Access SQL server through Eclipse IDE

This article explains how to make a connection to SQL server database through Eclipse IDE and view/manipulate data in Database Development perspective.

1)  First of all you have to download the database driver. In my case since I am using SQL Server 2008 R2, I should use sqljdbc4.jar  and depending on the server version the jar can change to sqljdbc.jar

2)  Then you have to add the driver definition in Eclipse preferences.
Go to Window -> Preferences -> Data Management -> Connectivity -> Driver Definitions -> Add

Select driver template



Select driver jar file


Fill in the properties


3)  Then open up the Database Development perspective




4)  Next, right click Database Connections create new connection. Select the connection profile for SQL Server, give a name for the connection and press next



Specify the driver and connection details


Here you can select the driver that we defined in step 2 and before testing the connection make sure the Remote connection via TCP/IP is enabled for SQL server and user has the permission for the connection.

You can refer to the article Enable TCP/IP remote connections for SQL server and give user permission for more details on this.

If you select Use Integrated Authentication, the connection will use windows authentication. But this will require sqljdbc_auth.dll and little bit more configurations which will not be covered in this article.

5)  So finally you can test and finalize the connection. After creating the connection you can view and manipulate table data in the SQL Server databases.