Friday, April 8, 2016

Java Regular Expressions with small example

Problem: 
We will get the string with some format which we need to resolve into Airthemetic operations and give final result:
format for the input string:
 (operator val1 val2 (operator val3 val4 val5)  (operator val6  (operator val7 val8)))
like this
below is one example:
(+ 3 4 5) = 3+ 4+ 5 = 12
(* 2 (+ 1 3) )= 2 * (1+3) = 2*4 = 8

Solution:
I chose to solve this using Java regular expression which fits my custom needs and can solve this without relying on some third party solutions and which is also best in performance as i am using Pattern to compile and then using Matcher to find matches which will boost up the performance rather than using Pattern.matches() on the string.

Hope the below functions gives you are glance on how to solve the issue:
public static boolean hasInnerSubquery(String str){
String patternStyle = "(\\(.*(.*\\(.*\\).*).*\\)) ";
Pattern pattern = Pattern.compile(patternStyle);
Matcher matcher = pattern.matcher(str);
while(matcher.find()){
return true;
}
return false;
}

/* this is used to resolve existing inner subqueries
* and replace the inner subquery in the string with
* the computed result and return the final replaced string
*/
public static String resolveInnerSubquery(String str) {
String patternStyle = "(\\(.*(.*\\(.*\\).*).*\\)) "; 
                //in regex grouping is done using (), and if say ([abc])([def]) that means there are
               // two groups one group which checks either a or b or c and other one group alterative
              // similarly the patternstyle i have used above checks for () inside the parenthesis --( ( ) )
             //  and try to solve the code inside the inner parenthesis.
Pattern pattern = Pattern.compile(patternStyle);
Matcher matcher = pattern.matcher(str);
int groupCount = matcher.groupCount();
while(matcher.find()){
System.out.println(matcher.group(groupCount));
String innerSubquery= matcher.group(groupCount);
innerSubquery = innerSubquery.substring(innerSubquery.indexOf('('), innerSubquery.indexOf(')')+1);
double result=calculate(innerSubquery.substring(1,innerSubquery.length()-1));
str= str.replace(innerSubquery,String.valueOf(result));
System.out.println(result);
}
return str;
}
     
     /*
* this is used to compute the final double result from the input string
* the format is like <operator> <val1> <val2> [ <val3> ...]
*/
public static double calculate(String str) {
List<String> params = null;
double result = 0;
String operator = "";
String[] strs1 = str.split("\\s+");
params = Arrays.asList(strs1);
if (params.size() >= 3) {
Iterator<String> iterator = params.iterator();
operator = iterator.next();
result = Double.parseDouble(iterator.next());
while (iterator.hasNext()) {
switch (operator) {
case "+":
result += Double.parseDouble(iterator.next());
break;
case "-":
result -= Double.parseDouble(iterator.next());
break;
case "*":
result *= Double.parseDouble(iterator.next());
break;
case "/":
result /= Double.parseDouble(iterator.next());
break;
case "%":
result %= Double.parseDouble(iterator.next());
break;
}
}
}

return result;
}

public static double performOperation(String str){
double result=0;
String input = str.substring(1,str.lastIndexOf(')'));
while(hasInnerSubquery(input)){
input=resolveInnerSubquery(input);
}
                result = calculate(input);
      }

Try this example out with your own requirements and have fun.

Monday, March 14, 2016

Creating Git Repository

Today we will try to create a Git Repository in Ubuntu Server (which is a VM) and try to access the Repository from Windows.

The steps involved are as follows:

We should be having the below prerequisites for Installing Git and (Stash/BitBucket which is Optional).

Installing Java JDK 1.8 on Ubuntu:
-----------------------------
vijay@ubuntu:~$ sudo add-apt-repository ppa:webupd8team/java
vijay@ubuntu:~$ sudo apt-get update
vijay@ubuntu:~$ sudo apt-get install oracle-java8-installer
vijay@ubuntu:~$ javac -version
javac 1.8.0_74
vijay@ubuntu:~$

Installing Perl 5.5 or above:
----------------------
vijay@ubuntu:~$ apt-get install -y perl
vijay@ubuntu:~$ perl --version
This is perl 5, version 20, subversion 2 (v5.20.2) built for x86_64-linux-gnu-thread-multi


Installing git in Ubuntu:
--------------------
vijay@ubuntu:~$ sudo apt-get install -y git
vijay@ubuntu:~$ git --version
git version 2.1.4

After Installing Git we should start working with it by acknowledging it who you are with the below two commands:

git config --global user.email "chvijay.1990@gmail.com"
git config --global user.name "vijay"

now create a folder under /opt/git/ hierarchy as below:

sudo mkdir --mode=u+rwx,g+rws,o-rwx repo1.git
sudo chown vijay.gitgroup repo1.git

This will ensure that repo1.git follow some permissions for vijay the user and gitgtoup which is our group

now run the below command inside the repo1.git directory:
git init --bare

now it will have the settings of a git repository:

Now we will try to clone it somewhere else and see whether the files got updated or not:

Create a directory in home folder as below:

sudo mkdir --mode=u+rwx,g+rws,o-rwx repos
sudo chown vijay.gitgroup repos

move to the repos directory created in your home folder and clone earlier repository

git clone vijay@192.168.183.128:/opt/git/repo1.git

Now this will create a repo1 folder in repos folder:

move to repo folder and create a file named as one.txt

append some text to it and follow the below commands:

git add one.txt
git commit -am 'fix 1'
git push origin master

After testing this inside Ubuntu VM, now you can start the work with your host OS Windows, Now before working with GIT you should install it in Windows using below link:

downloadableLink

After installing this in Windows, launch command prompt and introduce your self to Git using the earlier two commands once again:

git config --global user.email "chvijay.1990@gmail.com"
git config --global user.name "vijay"

now create a directory repos in D: directory using below commands:

D:\>mkdir repos
D:\>cd repos
D:\repos>

Now Inside this directory run the below command to get the main repository cloned to this place:

git clone vijay@192.168.183.128:/opt/git/repo1.git

After running this command the repo1 folder will be created in repos directory, in the above command 192.168.183.128 is the ipAddress of the the Ubuntu VM.

Now start working with the repository using GIT shell commands.

Have a nice time and thanks for visiting our blog :-)


Sunday, March 13, 2016

Monday, March 7, 2016

Working with SubShell in Linux:

When you want to make some task got forked and get the child process run as a subshell, then you can go for the below shell script which does the same for you, increase sleep time to check the execution status of the child process with the command "ps -ef | grep sub" in one more terminal:

#!/bin/bash
while :
do
echo "[ hit CTRL+C to stop]"
echo Enter Command to Process:;
read cmd;
echo $cmd > tmp2;
(sleep 3;bash -s <tmp2 );
done


Friday, February 26, 2016

Java Reflection Work

I made small work on Java Reflection API, when ever an object is passed to the inspect() method in my code, it will try to find the attributes and their values and try to insert to DB with the same name as of the ClassName==TableName and propertiesNames==ColumnNames, this is almost similar concept to Hibernate.

I thought of resembling the same and made small assignment on my own so that it will be useful for us in future..

U can download the program from the below link:
downloadCode

I passed the object of Employee class as below:

public class Employee {

int eno;
String ename;
double sal;
int dept;
String doj;
//generate getters and setter for this
}

package org.myown;

public class Person {
int id;
String name;
String occupation;
String sex;
//generate getters and setter for this
}

U can pass any Object and see the result for yourself..!

Sunday, February 14, 2016

How to Launch Application/Server from bat file on double click? 

Hi Friends,
This is a very easy task and helpful once, u can create a shortcut for some bat files on your desktop and launch your applications straight away on double click the below is the batch program for the same, save it as a .bat file and create shortcut for it on your desktop and use it this is a very useful one and hence i thought of sharing with my friends.

setlocal
CHDIR /D D:\Tools\cis\9.1\cis-sdk\bin\
start "C:\Windows\System32\cmd.exe" launch.bat runCISAgent.py


Tuesday, November 13, 2012

A-View Great Platform for Distance Learing


This Has been Launched by the President and there is a lot of BackGround Work done by IIT's and NIT's inorder to make this a grand success.

please have a visit at this site: A-view and make a comment on this to make it more popular so that people can get benifitted.

Thanks a lot for watching the video...