Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Sunday, July 7, 2013

How To Connect MySQL Database Using Java

Hello

In this article, I'll show you how to connect MySQL database using java. For this, you should configure the settings about MySQL connector in the JAVA IDE first. So all things what we need are;


  • IDE (NetBeans or EClipse is so good!)
  • MySQL Connector

  • MySQL Connector is a jar file which is so small size. You can download MySQL Connector here and check this line up:
    JDBC Driver for MySQL (Connector/J)
    After downloading, you have to introduce IDE and Connector. I use NetBeans. Because of I will show you how to introduce on the NetBeans already. Actually It doesn't matter which program has used. For that reason This process is the same for EClipse.

    Screen views given above show us the process about introducing. You click OK and it's finish. Your MySQL driver and JAVA IDE know each other from on now. Let's create a db class on there.
    void vt() {
            Connection conn = null;
            String hostName = "jdbc:mysql://localhost:3306/";
            String dbName = "DatabaseName";
            String driver = "com.mysql.jdbc.Driver";
            String userName = "root";
            String password = "*****";
            try {
                conn = DriverManager.getConnection(
                    hostName+dbName,userName,password);
                System.out.println("Connected to the database");
                Statement st = conn.createStatement();
                ResultSet result = st.executeQuery("SELECT * FROM  test");
                while (result.next()) {
                   int id = result.getInt("id");
                   String name = result.getString("name");
                   System.out.println(id + "\t" + name);
            }
                conn.close();
            } catch (Exception e) {
                System.out.println("No Connection!");
            }
    }
    

    In the try..catch debugging part, if there is no connection, You are going to see "No Connection!" alert on the screen. If not, You'll get names of the test table from your database. This code given above is not so good, but useful. If you want to code better, you can use OOP for example. Create a class of database connection, and use it each time connection.
    See you next article!

    Friday, August 17, 2012

    A nice video on RCaller 2.0


    A nice video on RCaller is in Youtube now. The original link is in Quantlabs.net. Thanks for the uploader.

    Wednesday, August 8, 2012

    libjvm.so: cannot open shared object file: No such file or directory


    In my Ubuntu Linux, I was trying to call Java from C++ using JNI. Compiling progress was succeed but at runtime I saw an error on concole:

    libjvm.so: cannot open shared object file: No such file or directory

    Finding the right solution took five seconds but it was quite easy. I modified the LD_LIBRARY_PATH environment variable using export command in the Linux shell:

    export LD_LIBRARY_PATH=/usr/lib/jvm/default-java/jre/lib/i386:/usr/lib/jvm/default-java/jre/lib/i386/client

    The location of JDK is always changed because of updates but Ubuntu stores the links of current JVM in default-java directory. It is /usr/lib/jvm/default-java in my Linux. Two directories must be added to LD_LIBRARY_PATH. The first one is jre/lib/i386 and the second one is jre/lib/i386/client or jre/lib/i386/server in default-java directory. Use of export solves my problem.

    Good luck!


    Wednesday, May 30, 2012

    An informative video on RCaller

    Somebody on the Internet submitted an informative video on how to use RCaller for calling R from withing Java applications in YouTube.

    It is nice to see RCaller has an higher usage rates after its 2.0 version.

    You can see the embedded video in this entry. Have a nice training!

    Saturday, March 10, 2012

    Developing a Basic Application on Android: "Welcome Page"

    Hi Everyone! Today, I'll show you an Android application which is about welcome pages. As known, before when opened an app, game, web site, DVD movie, etc, a welcome page has shown us by masters of'em. That's why is showing what the brand is. For example, Angry Birds, EA Games, Microsoft Games.

    This article is going to be a basic application like "Hello World!". But Before we'll be on welcome page and see the welcome message on it. Let's see how to do it!

    Necessary equipment (Install all of them)
    1. Eclipse Editor
    2. SDK (Software Development Kid)
    3. Android: WindowsLinuxMac
    First, open the Eclipse editor, then follow this directory: File/New/Project/Android/Android Project
    My Project Name : WelcomePage
    My Android Version : V2.2
    My Package Name : welcome.page
    You can fix this properties as you wish.

    I think, you know how to do an "hello world" app on Android, so I won't explain this. The important thing of the app for me is relationship of Activities and Threads. For those, we're going to do this pages:
    src/WelcomePageActivity.class //This is a default file
    src/WelcomePage.class
    res/layout/main.xml //This is also a default file
    res/layout/page2.xml
    AndroidManifest.xml //This is configuration file
    
    Create a class file: 
    1. directory: src/(Right Click)New/Class
    2. call: WelcomePage
    3. Click the "Finish" Button
    Code for WelcomePage.java:
    package welcome.page;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class WelcomePage extends Activity { //Created WelcomePage Activity
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main); //The content to display from main.xml
     }
    }
    

    The "R" is a library which used for xml files. Every xml file can used on it. We'd just used main.xml file for we need to go home page of our Android app after showing welcome page.

    Create a xml file:
    1. directory: res/layout/(Right Click)Other/File/Android/Android Xml File
    2. call: page2
    3. Click the "Finish" Button
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is a welcome page!" />
    
    </LinearLayout>
    

    When we look at this page up, should see like that:
    page2.xml file view
    Create WelcomePageActivity.java file
    package welcome.page;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    
    public class WelcomePageActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.page2); //The content to display from page2.xml file
            
            Thread WelcomeScreen = new Thread() { //Must create a thread, because opening another page is a different event, as sync
             public void run() {
              try { //This is try-catch
               sleep(5000); //Waiting five seconds
               startActivity(new Intent(getApplicationContext(),WelcomePage.class)); 
               //After waiting, shown the content from WelcomePage.java file
              } catch (InterruptedException e) {
               e.printStackTrace();
              }
              finally {
               finish(); //We finish this thread.
              }
             }
            };
            WelcomeScreen.start(); //Starting the thread created.
        }
    }

    Maybe, you may not get the line, 
    "startActivity(new Intent(getApplicationContext(),WelcomePage.class));". 
    Just remember that we used main.xml content to get on WelcomePage.java page.

    And the last one we have to do, update AndroidManifest.xml file! Remember that we create a new Activity, so must define this Activity. For that reason, just add this code to AndroidManifest.xml file:
    And yes, finished! Let's work this on our Emulator! For this, call: 


    WelcomePage Project(Right Click)/Run as/Android Project

    Welcome page view
    After 5 seconds this page, we will see main.xml file:

    main.xml file on my application
    We'll see you guys next article!

    Wednesday, January 18, 2012

    When to use Java Native Interface (JNI) ?

    Programming in Java is totally fun, especially, if you use it for general purposes. The term 'general purposes" is stated here as "the program that needs file, socket, web, gui operations" opposite to a special problem. That is, a special problem may be a scientific one which requires loops, array operations and basic operations. A program like this can be easily written in other languages.

    Sometimes, we need other libraries which are not yet implemented for Java or some operations that perform some low level operations which are not handled by the JVM. For instance, a library written in C can not be directly used in Java. You have to either re-write it in Java or use the Java Native Interface (JNI).

    But there is something worth to talk about. People generally think that a native function that called from Java must run faster than in Java. But it is not true. Yes, a native function (we use the term native for compiled codes) may run faster and via versa. The important point here is that Java can not directly perform a function call directly, that is, there is some preparation process needed.

    To be clear, let me give an example. Suppose that we have a C++ function 'mean' which stands for calculating the arithmetic mean of a double array. We have also a function 'jmean' which is written in Java for the same purpose. Now, suppose that we have a double array with size of 1,000,000 and has values from 0.0 to 999,999.
    The processes of passing this array reference to C++ and getting the calculated result costs 112 milliseconds. When I tried the same test for the function written in Java, I got the result of 10-12 milliseconds. It seems there is a significant performance difference between them!

    In this example, the biggest amount of time is consumed by the calling preparation, not the calculation mean itself.

    We can summarize those results as :
    1) Calling a native method for millions times makes a significant performance loss.
    2) Use JNI if you have to. Maybe, if it is performing a low level operation or converting the C code to Java is hard. For example accessing to LPT port is not possible in Java and you need to use JNI here.

    and I can personally say that:
    "An increase on performance depends on the job performed by the native method."

    Finally, the old book, Essential JNI - Java Native Interface is a good book if you really interested in JNI. JNI is generally used for calling C/C++ functions from Java and accessing Java objects from C/C++. JNI also stands for "creating JVM's in C/C++" code and that means you can manipulate java programs as acting like the JVM itself.

    JNI is in everyday Java life. JCurses library uses JNI for coloured console for the people who got bored using System.out.println. QJambi uses JNI for constructing a bridge between Java and Qt classes. Java's standard libraries such as AWT also bridges between native GUI API's and Java. Charva uses JNI for GUI'd text terminals. And, of cource, there are lots of examples on this.



    Friday, January 6, 2012

    Lets implement a simple Lisp Interpreter in Java

    Edit 2015.03.20 : If you are really interested in implementing a LISP like language in Java, you can visit this, this and this

    You are probably programing with C++, Java or .Net thingies but not Lisp. Why? Because it is the most beautiful language that human ever made :) Let me explain.

    If you write in a programming language, you have to look and understand the interpreter's or compiler's principals. If you didn't do that yet, let me say, when you look inside an interpreter you will see a picture in which hundreds of objects being copied from one data structure to another called stacks. When you trace the interpreter, you will see nothing except those abstract data structures. The scene you will see is like a casino with hundreds of card dealers...

    But when you program in Lisp, the interpreter exactly does what you write. There is no abstraction, no hacking. You will deal the cards in your fully controlled casino. Having knowledge about what the interpreter does is everything.

    Lets look at the following Lisp expression

    (+ 2 (+ 3 2))

    which states that interpreter first will sum 2 and 3 and the gained number will be summed with 2. The result should be 7. How did we do that? Because of our primary school skills. Lets do that with a stupid machine that can only handle basic stack operations as in assembly language.

    Suppose that we have a stack object and we are sequentially adding tokens into that stack. A token is defined as an atomic element of this expression. So our tokens are (, +, 2, (, +, 3, 2, ) and ) , respectively.

    Now, take the first token and put it into the stack. Our stack is like that:


    /


    Lets add more elements until we encounter a closing parenthesis.


    (+2(+32)

    Again, suppose that, whenever we encounter a closing parenthesis, we collect some elements from the stack until we get an opening parenthesis, say that, we have now


    (+32)

    collected from the stack. Calculate this simple expression and put the result back to our stack. Since result of the sub expression (+ 3 2) is 5, our stack comes to shape of


    (+25

    We didn't finish reading the tokens yet. We have one more token to push into the stack.


    (+25)

    We encounter a closing parenthesis again and that means we have to handle the sub expression and put back the result to the stack. Since there is no sub expressions, we have the root expression at hand. Reading until having an opening parenthesis and performing calculations will give the result of 7 after all.

    Note that, all we did here spans a 50 lines of Java class. I hope taking a glance at this code will help you to capture the whole story.



    package smalllisp;
    
    import java.util.Stack;
    
    public class SmallLisp {
    
      Stack<String> stack;
      
      public SmallLisp(){
        String[] tokens = new String[]{"(","+","2","(","+","3","2",")",")"};
        stack = new Stack<String>();
        for (int i=0;i<tokens.length;i++){
          stack.push(tokens[i]);
          if(tokens[i].equals(")")) Interprete(); 
        }
      }
      
      public void Interprete(){
        String tok;
        Stack<String> callStack = new Stack<String>();
        tok = stack.pop(); /* This is the ) character */
        while(!(tok=stack.pop()).equals("(")){
          callStack.push(tok);
        }
        Call(callStack);
      }
      
      public void Call(Stack<String> callStack){
        String func = callStack.pop(); /* This is the operator or function */
        if(func.equals("+")) {
          double result = Plus(callStack);
          stack.push(String.valueOf(result));
        }
        //if(func.equals("-")) Minus(callStack);
      }
      
      public double Plus(Stack<String> callStack){
        double a = Double.parseDouble(callStack.pop());
        double b = Double.parseDouble(callStack.pop());
        System.out.println("Answer is "+(a+b));
        return(a+b);
      } 
      
      public static void main(String[] args) {
        new SmallLisp();
      }
    }
    


    Todo:
    • Implement the minus function yourself.
    • Implement the product function yourself.
    Well done !

    Monday, November 28, 2011

    String ambiguity in Java



    Java has two kind of data types. The first one includes the primitive data types. They are int, long, double
    float, short, byte, boolean and char. Defining a single variable with one of the data types given above is similar
    to variable declaration in C or C++. As in C brother, Java simply allocates a proper memory area for the given
    type and maps the variable name for it when we type

    int i;
    

    in a program. It is simple to understand and clear. The mechanism underlying instantiating a class to create a new object
    is similar to C++. For example, we create an object by instantiating a CCObject class using

    CCObject *obj = new CCObject();
    


    in C++, whereas, it is

    CCObject obj = new CCObject();
    


    in Java. In C++ example, a memory area is allocated for the CCObject and it is mapped to pointer obj which is shown as *obj.
    We call its method "meth" using a code similar to

    obj->meth();
    

    whereas, it is

    obj.meth();
    

    in java. They are both created using a dynamic loading mechanism, that is, they does not exist in compile time and they
    are created in runtime. In this use of "->" operator, object instantiating mechanisms are similar. However there is an
    other method of creating objects in C++ which is like

    CCObject obj;
    obj.meth();
    


    and it is totally different from the examples above. In this example, the object obj is created at the compile time
    and it is faster. The dot operator is also different from the same operator in Java. Understanding the dynamic class
    instantiating and compile time creating is important.

    Lets have a look at the String class in Java, which is defined in the package java.lang in the Java core library. We can
    create two Strings using a code

    String s1 = "This is string 1";
    String s2 = "And this is the second one!";
    

    and we can do

    String cat = s1 + s2;
    

    which requires a "operator overloading" operation in C++. In Java, there is no operator overloading, that is, you can
    not define a behavior for a given operator on a given class. But String class do that!

    String class is an exception and it has got different properties when compared to others. Java compiler behaves different
    when it compiles the codes with a String object. The another ambiguity is using literals with class methods. Look that:

    String s = "Hello, this is a curse Java string";
    

    This use seems like the Java Strings are built-in data types rather than objects! Ofcourse, Java compiler changes this line to

    String s = new String("Hello, this is a curse Java string");
    

    but what about this? :

    int l = "Hello, this is a curse Java string".length;
    

    If a Java student looks this line, the Java String seems to be an object again! This is because of the weird design of Java Strings.
    Java compiles them in a consistent way but being Java Strings a exception obstructs the clean pattern of Java language.

    What is the solution?

    Firstly, for my personel opinion, operator overloading would be a good property for Java language. So, it would be a more elegant solution
    to drop this

    Matrix A = new Matrix (data1);
    Matrix B = new Matrix (data2);
    Matrix C = A.transpose().product(A).inverse().product(A.transpose().product(B.transpose));
    

    and replace with

    Matrix A = new Matrix (data1);
    Matrix B = new Matrix (data2);
    Matrix C = (A.transpose() * A).inverse() * A.transpose() * B.transpose();
    

    This provides the consistency in the use of summation operator with String classes.

    Dropping the ambiguity in the cases that "a string content".length and String s = "This is a string" is hard because millions of
    Java code uses this syntax. Deprecating this use and dropping it in next revision is a solution. It would be still as it is. But
    remember the Basic language and remember how difficult to parse it was. Writing code in an easy way is not the whole art. A consistent
    language is like an deterministic toolbox.

    My radical idea is to use the C++ syntax in Java virtual machine. Something must be an object, something must be a pointer to an object. Operators
    would be overloaded as "Java does it to its Strings!". So, how it would be nice if we compile this code into the JVM:

    String *s = new String("Hello there, it would be a Java String!");
    int length = s->length;
    System::out::println(*s->chars);
    

    and of course

    String s = String("Hello there... it is also a Java String");
    int length = s.length;
    System::out::println(&s.chars);
    

    :)

    Moreoever, there would be some strangers around who want their C++ syntaxed Java code compiled into the JVM!

    Wednesday, September 28, 2011

    Passing plain Java objects to R using RCaller

    Well, you are using RCaller for your statistical calculations. Probably, you are passing your double arrays to R and type some R commands in order to get the desired outputs. After a calculation process, you handle the returned arrays through the parser. This is the general use of RCaller.

    Suppose that you have got a Java class which has got some variables with data types int, short, long, float, double and String. This class also includes some arrays of types int[], double[], ..., String[]. Of course it may include some functions, constructors or anything else. But we don't care about this for now.  How about passing this class with its publicly defined variables to R? Yeah! It is possible in its last submitted revision.

    Have a look at the Java class below:


    class TestClass {
    
      public int i = 9;
      public float f = 10.0f;
      public double d = 3.14;
      public boolean b = true;
      public String s = "test";
    }
    

    This class simply includes five publicly defined variables with basic data types. Our other class inherits the TestClass and defines some additional arrays:


    class TestClassWithArrays extends TestClass {
    
      public int[] ia = new int[]{1, 2, 3, 4, 5};
      public double[] da = new double[]{1.0, 2.0, 3.0, 4.0, 9.9, 10.1};
      public String[] sa = new String[]{"One", "Two", "Three"};
      public boolean[] ba = new boolean[]{true, true, false};
    }
    

    Ok, they are very simple but there is no reason those classes not to have any methods. Whatever those classes have methods, we consider them as data structures.

    Lets pass this data structure to R:


    TestClassWithArrays tcwa = new TestClassWithArrays();
        JavaObject jo = new JavaObject("tcwa", tcwa);
    
        RCaller rcaller = new RCaller();
        rcaller.setRscriptExecutable("/usr/bin/Rscript");
        rcaller.cleanRCode();
    
        rcaller.addRCode(jo.produceRCode());
        rcaller.runAndReturnResult("tcwa");
    


    Well, if there is no expection we have the results in a R list named "tcwa". This R list includes all of the elements that included in TestClassWithArrays and TestClass with their values.

    This is an example of proof, the related @Test method is ready for browsing in the Google Code:

    @Test
      public void TestClassWithArrays() throws IllegalAccessException, IOException {
        TestClassWithArrays tcwa = new TestClassWithArrays();
        JavaObject jo = new JavaObject("tcwa", tcwa);
    
        RCaller rcaller = new RCaller();
        rcaller.setRscriptExecutable("/usr/bin/Rscript");
        rcaller.cleanRCode();
    
        rcaller.addRCode(jo.produceRCode());
        rcaller.runAndReturnResult("tcwa");
    
        int[] expectedIntArray = rcaller.getParser().getAsIntArray("ia");
        for (int i = 0; i < tcwa.ia.length; i++) {
          assertEquals(expectedIntArray[i], tcwa.ia[i]);
        }
    
    
        double[] expectedDoubleArray = rcaller.getParser().getAsDoubleArray("da");
        for (int i = 0; i < tcwa.da.length; i++) {
          assertEquals(expectedDoubleArray[i], tcwa.da[i], delta);
        }
    
        String[] expectedStringArray = rcaller.getParser().getAsStringArray("sa");
        for (int i = 0; i < tcwa.sa.length; i++) {
          assertEquals(expectedStringArray[i], tcwa.sa[i]);
        }
    
      }
    

    It is shown that in examples, in R side, we can access to elements with their original names that defined in the Java class. That sounds good.

    Finally, we can pass our Java objects with defined contents. This use of RCaller narrows the code of addDoubleArray, addIntArray and reduce all of them to simple command of

     JavaObject jo = new JavaObject("tcwa", tcwa);
    .
    .
    .
    rcaller.addRCode ( jo.produceRCode() );
     
    
    It is simplicity...
    

    Saturday, September 17, 2011

    RCaller: Support for sequential commands with a single process

    I think, this revision will be the foundation of the version  2.1. RCaller is supposed to be slow but the easiest way of calling R from Java.

    Finally I have implemented the method runAndReturnResultOnline() for running sequential commands in a single process. What does this stand for? Let me give an example to explain this:

    Suppose that you want to perform a simulation study to measure the success of your new procedure. For this, you decide to draw random numbers from a distribution and calculate something and handle the results in Java. RCaller creates  Rscript processes for each single iteration. This cause to too many operating system calls.

    Latest release of RCaller includes the method for this. Lets have a look at the Test file:


    @Test
      public void onlineCalculationTest() {
        RCaller rcaller = new RCaller();
        rcaller.setRExecutable("/usr/bin/R");
        rcaller.cleanRCode();
        rcaller.addRCode("a<-1:10");
        rcaller.runAndReturnResultOnline("a");
        assertEquals(rcaller.getParser().getAsIntArray("a")[0], 1);
    
        rcaller.cleanRCode();
        rcaller.addRCode("b<-1:10");
        rcaller.addRCode("m<-mean(b)");
        rcaller.runAndReturnResultOnline("m");
        assertEquals(rcaller.getParser().getAsDoubleArray("m")[0], 5.5, 0.000001);
    
        rcaller.cleanRCode();
        rcaller.addRCode("a<-1:99");
        rcaller.addRCode("k<-median(a)");
        rcaller.runAndReturnResultOnline("k");
        assertEquals(rcaller.getParser().getAsDoubleArray("k")[0], 50.0, 0.000001);
      }
      }
     
    
    In first stage,we are creating an integer vector and getting the first element. In the second one, we are creating the same integer vector with a different name and calculating the arithmetic mean. In the last one, we are recreating the vector a and getting the median, which is equal to 50.

    This example uses the same RCaller object. In first stage, the R executable file (it is /usr/bin/R in my Ubuntu Linux) is created once. In second stage the same R file is used and no longer processes are created again. In this stage, the vector a is accessible and still remains alive. At the last stage, b is alive again and a is recreated. So this example does not cause the R to open and close three times but only once.

    This modification speeds up the RCaller, but it can be still considered as slow.
    However, it is still easy to implement and much more faster than the previous implementation.

    Have Fun!


    Thursday, September 15, 2011

    Handling R lists with RCaller 2.0

    Since RCaller creates an Rscript process for each single run, it is said to be in-efficient for most cases. But there are useful non-hack methods to improve the method. Suppose that your aim is to calculate medians of two double vector like this:












    @Test
      public void singleResultTest() {
        double delta = 0.0000001;
        RCaller rcaller = new RCaller();
        rcaller.setRscriptExecutable("/usr/bin/Rscript");
        rcaller.cleanRCode();
        rcaller.addRCode("x <- c(6 ,8, 3.4, 1, 2)");
        rcaller.addRCode("med <- median(x)");
    
        rcaller.runAndReturnResult("med");
    
        double[] result = rcaller.getParser().getAsDoubleArray("med");
    
        assertEquals(result[0], 3.4, delta);
      }
    
    However, this example considers only computing the median of x, effort for computing medians of three variables needs three process which is very slow. Lists are "vector of vector" objects but they are different from matrices. A list object in R can handle several types of vector with their names. For example


    alist <- list (
    s = c("string1", "string2", "string3") , 
    i = c(5,4,7,6),
    d = c(5.5, 6.7, 8.9)
    )
     
    
    the list object alist is formed by three different kind of vectors: string vector s, integer vector i and double vector d. Also their names are s, i and d, respectively. Accessing elements of this list is straightforward. There are two ways to access to elements. First one is conventional way using indices. When the example above runs, strvec is set to String vector s.
    
    

    alist <- list (
    strvec <- alist[1]
    
    While a list object can handle R objects with their names, we can handle more than more result in a single RCaller run. Back to our example, we wanted to calculate medians of three double vectors in a single run.
    @Test
      public void TestLists2()throws Exception {
        double delta = 0.0000001;
        RCaller rcaller = new RCaller();
        rcaller.setRscriptExecutable("/usr/bin/Rscript");
        rcaller.cleanRCode();
        rcaller.addRCode("x <- c(6 ,8, 3.4, 1, 2)");
        rcaller.addRCode("med1 <- median(x)");
    
        rcaller.addRCode("y <- c(16 ,18, 13.4, 11,12)");
        rcaller.addRCode("med2 <- median(y)");
    
        rcaller.addRCode("z <- c(116 ,118, 113.4,111,112)");
        rcaller.addRCode("med3 <- median(z)");
    
        rcaller.addRCode("results <- list(m1 = med1, m2 = med2, m3 = med3)");
    
        rcaller.runAndReturnResult("results");
    
        double[] result = rcaller.getParser().getAsDoubleArray("m1");
        assertEquals(result[0], 3.4, delta);
    
        result = rcaller.getParser().getAsDoubleArray("m2");
        assertEquals(result[0], 13.4, delta);
    
        result = rcaller.getParser().getAsDoubleArray("m3");
        assertEquals(result[0], 113.4, delta);
      }
    
    This code passes the tests. By the result at hand, we have three medians of three different vectors with one pass calculation. With this way, an huge number of vectors can be accepted as a result from R and this method may be considered efficient... these test files were integrated to source structure of project in http://code.google.com/p/rcaller/

    hope works!

    Wednesday, September 7, 2011

    Embedding R in Java Applications using Renjin

    Effort of embedding R in other languages is not a short history for programmers. Rserve, Rjava, RCaller and Renjin are prominent efforts for doing this. Their approaches are completely different. RServe opens server sockets and listens for connections whatever the client is. It uses its own protocol to communicate with clients and it passes commands to R which were sent by clients. This is the neatest idea for me.

    RJava uses the JNI (Java Native Library) way to interoperate R and Java. This is the most common and intuitive way for me.

    RCaller sends commands to R interpreter by creating a process for each single call. Then it handles the results as XML and parses it. It is the easiest and the most in-efficient way of calling R from Java. But it works.

    And finally, Renjin, is a re-implementation of R for the Java Virtual Machine. I think, this will be the most rational way of calling R from Java because it is something like

    Renjin,
    is not for calling R from Java,
    is for calling itself and maybe it can be said that: it is for calling java from java :),
    for Java programmers who aimed to use R in their projects


    So that is why I participated this project. External function calls are always make pain whatever the way you use.

    Renjin is an R implementation in Java.

    I think all these paragraphs tell the whole story.

    How can we embed Renjin to our Java projects? Lets do something... But we have some requirements:

    1. renjin-core-0.1.2-SNAPSHOT.jar (Download from http://code.google.com/p/renjin/wiki/Downloads?tm=2)
    2. commons-vfs-1.0.jar (Part of apache commons)
    3. commons-logging-1.1.1.jar (Part of apache commons)
    4. guava-r07.jar (http://code.google.com/p/guava-libraries/downloads/list)
    5. commons-math-2.1.jar (Part of apache commons)

    Ok. These are the renjin and required Jar files. Lets evaluate the R expression "x<-1:10" which creates a vector of integers from one to ten. Tracking the code is straightforward.
    package renjincall;
    
    
    
    import java.io.StringReader;
    
    import r.lang.Context;
    
    import r.lang.SEXP;
    
    import r.parser.ParseOptions;
    
    import r.parser.ParseState;
    
    import r.parser.RLexer;
    
    import r.parser.RParser;
    
    import r.lang.EvalResult;
    
    
    
    public class RenjinCall {
    
    
    
      public RenjinCall() {
    
        Context topLevelContext = Context.newTopLevelContext();
    
        try {
    
          topLevelContext.init();
    
        } catch (Exception e) {
    
        }
    
        StringReader reader = new StringReader("x<-1:10\n");
        ParseOptions options = ParseOptions.defaults();
        ParseState state = new ParseState();
        RLexer lexer = new RLexer(options, state, reader);
        RParser parser = new RParser(options, state, lexer);
        try {
          parser.parse();
        } catch (Exception e) {
          System.out.println("Cannot parse: " + e.toString());
        }
        SEXP result = parser.getResult();
        System.out.println(result);
      }
    
      public static void main(String[] args) {
        new RenjinCall();
      }
    }
    
    


    We are initializing the library, creating the lexer and the parser and hadling the result as a SEXP. Finally we are printing the SEXP object (not itself, its String representation)


    <-(x, :(1.0, 10.0))
    
    This is the parsed version of our "x<-1:10", it contains the same amount of information but it is a little bit different in form. Since we only parsed the content but it has not been evaluated. Track the code:
    EvalResult eva = result.evaluate(topLevelContext, topLevelContext.getEnvironment());
    System.out.println(eva.getExpression().toString());
    


    Now, the output is

    c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    

    and this is the well known representation of R integer vectors. Of course printing the result in String format is not all the work. We would handle the elements of this array rather than print it. Lets do some work on it:

    IntVector vector = (IntVector) eva.getExpression();
        for (int i = 0; i < vector.length(); i++) {
          System.out.println(
    i + ". element of this vector is: " + vector.getElementAsInt(i)
    );
        }
    

    IntVector is defined in renjin core library and is for handling integer vectors. We simple used the .length() and .getElementAsInt() methods like using Java's ArrayList class. Finally the result is

    0. element of this vector is: 1
    1. element of this vector is: 2
    2. element of this vector is: 3
    3. element of this vector is: 4
    4. element of this vector is: 5
    5. element of this vector is: 6
    6. element of this vector is: 7
    7. element of this vector is: 8
    8. element of this vector is: 9
    9. element of this vector is: 10
    

    It is nice, hah?

    Thursday, August 25, 2011

    Renjin - R interpreter written in Java

    Today, I stumbled upon a web page under the Google Project Hosting, which is a re-implementation of R in Java. It is a good news for R & Java programmers because it opens a new way to call R functions from Java directly. Project is open source and distributed under the GNU GPL v3. Many functions were implemented, R interpreter works well. I think there is much more work to do, especially, speed is the main issue. Unfortunately, there are only three developers that i only saw and this is a wonderful work. Finally, it would be good to be involved this project. The project web page is http://code.google.com/p/renjin/ and there is a live demo of the interpreter in site http://renjindemo.appspot.com/.

    More contributors needed for the project RCaller

    We need new contributors to enhance the functionality of RCaller. We need also feedbacks about
    • type of projects that RCaller used in
    • frequently used functions of R
    • new functionality required.
    • Bug reports
    We also need a web page, rather then http://www.mhsatman.com/rcaller. A Logo would be good.

    We need developers, testers, documenters which may have skills on Java, R, LaTeX or HTML.

    We can enlarge the space spanned by RCaller, say that, PhpCaller, CCaller or something derivative may be included for Php and C, respectively. Note that, there are already some libraries for calling R from other languages. RCaller has lesser efficiency on run time but higher speed on development time.

    Please join the project.
    google code page: https://code.google.com/p/rcaller/

    Friday, July 22, 2011

    Random Number Generation with RCaller 2.0

    Java has two standard libraries for generating random numbers. The java.lang.Math class has a random method with is used for generating uniform distributed random numbers. The second one is the java.util.Random class which has got several functions for generating random numbers. We can draw random numbers from several distribution using the probability integral transform. But R has many internal functions for random number generation from several probability distributions including the gamma, the binomial, the normal etc.


    RCaller has a wrapper class, under the package statistics, for generating random number for those distributions. The class statistics. RandomNumberGenerator has these functions:


    public double[] randomNormal(int n, double mean, double standardDeviation)
    public double[] randomLogNormal(int n, double logmean, double logStandardDeviation) 
    public double[] randomUniform(int n, double min, double max) 
    public double[] randomBeta(int n, double shape1, double shape2)
    public double[] randomCauchy(int n, double location, double scale) 
    public double[] randomT(int n, int df) 
    public double[] randomChisqyare(int n, int df)
    public double[] randomF(int n, int df1, int df2)
    public double[] randomPoisson(int n, double lambda) 
    public double[] randomBinom(int n, int size, double p)
    public double[] randomNegativeBinom(int n, int size, double p)
    public double[] randomMultinomial(int n, int size, double p)
    public double[] randomGeometric(int n, double p) 
    public double[] randomWeibull(int n, double shape, double scale) throws 
    public double[] randomHyperGeometric(int amount, int n, int m, int k) 
    public double[] randomExponential(int n, double theta) throws Exception 
    public double[] randomGamma(int n, double shape, double rate, double scale) 
    


    One can see the usage of class in the Test5 class in the source of RCaller 2.0.
    http://code.google.com/p/rcaller/source/browse/RCaller/src/test/Test5.java

    Tuesday, July 12, 2011

    Debugging the R output of RCaller

    RCaller 2.0 has been submitted to Google Code before two or three days. Many RCaller users are testing it and except a Windows installation it seems not to be so problematic.

    RCaller 2.0 receives the R outputs as XML files. If the user does not know the returned variable names or if there was a problem with the results some debugging stuff is needed.

    Now the function 'getXMLFileAsString()' is implemented in RCaller, by using it, the converted R output can be investigated.

    Suppose that we want to run some R code from Java and we want to have a look at the returned XML content. Have a look at these codes:



    package test;

    import rcaller.RCaller;

    /**
     *
     * @author Mehmet Hakan Satman
     */

    public class Test4 {
       
        public static void main (String[] args){
            new Test4();
        }
       
        public Test4(){
            try{
                /*
                 * Creating an instance of RCaller
                 */
                RCaller caller = new RCaller();
               
                /*
                 * Defining the Rscript executable
                 */
                caller.setRscriptExecutable("/usr/bin/Rscript");
               
                /*
                 * Some R Stuff
                 */
                caller.addRCode("set.seed(123)");
                caller.addRCode("x<-rnorm(10)");
                caller.addRCode("y<-rnorm(10)");
                caller.addRCode("ols<-lm(y~x)");
               
                /*
                 * We want to handle the object 'ols'
                 */
                caller.runAndReturnResult("ols");
               
                /*
                 * Getting R results as XML
                 * for debugging issues.
                 */
                System.out.println(caller.getParser().getXMLFileAsString());
            }catch(Exception e){
                System.out.println(e.toString());
            }
        }
    }



    Because of the "set.seed()"  function of R, this code should produce the same results for all machines. The XML output is






    This structure of this XML file is simple and one can see that each single variable is encapsulated within a "" and a "" tag.

    Another way of handling the variable names is to use "ROutputParser.getNames()". This function simply returns an ArrayList which includes the variable names returned by R.

    Saturday, May 14, 2011

    Handling plots with rcaller

    Using RCaller is a simple way of calling R scripts from Java. Unfortunately image handling routines was not implemented so user must handle this stuff himself. In R the result of a plot object can be saved in a simple way like this:











    #Data generating process
    x<-rnorm(100, 0, 2)
    #we generated a normal sample with mean 0 and standard deviation 2

    png("path/to/file.png")
    plot.ts(x)
    dev.off()

    After running this short script, no screen output is produced but path/to/file.png is created as a png image. After calling this script from Java, produced image can be loaded like this:

    ImageIcon myIcon = new ImageIcon("/path/to/file.png");
     
    This image can be easly drawn on a swing container using

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        myIcon.paintIcon(this, g, 0, 0);
    }
     

    Thursday, August 5, 2010

    Using Beanshell Scripts in cgi-bin Directory

    In this tutorial, we demonstrate to use Bean Shell Java interpreter as a cgi language. Beanshell is a Java interpreter that can be called dynamically in Java applications or linux console. You can download and install Beanshell in Ubuntu using

    # sudo apt-get install bsh

    After installing you can test it by typing bsh in linux shell:

    root@stdio:/# bsh
    BeanShell 2.0b4 - by Pat Niemeyer (pat@pat.net)
    bsh % System.out.println("Hello world!");
    Hello world!
    bsh % double[] d=new double[]{1,2,3.14159};
    bsh % for (int i=0;i
    System.out.println("d["+i+"] is "+d[i]); 
    }
    d[0] is 1.0
    d[1] is 2.0
    d[2] is 3.14159



    You have to create a file called .jline-bsh.Interpreter.history in directory /var/www and this file has to be writable rights. You can create and give writable rights like this:

    # touch /var/www/.jline-bsh.Interpreter.history
    # chmod a+w /var/www/.jline-bsh.Interpreter.history

    Now we can create our Beanshell cgi script using a text editor like vim.

    # cd /usr/lib/cgi-bin/
    stdio:/usr/lib/cgi-bin# vim 1.bsh

    A sample 1.bsh file includes these lines of codes:

    #!/usr/bin/bsh
    System.out.println("Content-type: text/html\n\n");
    System.out.println("Hello from java cgi");
    System.out.println(System.getenv("QUERY_STRING"));


    In first line of this script, we say this script will be handled using /usr/bin/bsh.
    Secondly, content type of this file is text/html so browser will display the output as html.
    In third line, we send a hello world string to client and finally we write variables which are defined in URL.

    In order to run this code, we have to give executable rights to file using

    # chmod a+x 1.bsh

    Now, we are ready to run this code using our browser. Be sure that your apache server is running using

    # sudo /etc/init.d/apache2 status

    and it is not running use

    # sudo /etc/init.d/apache2 start

    to start it. After that, open a browser and type,

    http://localhost/cgi-bin/1.bsh

    If everything is ok, we can see something like that:

     
    We can pass some variables like this

    http://localhost/cgi-bin/1.bsh?var1=3&var2=stdio&var3=aValue