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!

Tuesday, November 15, 2011

Web 2.0 Tech and a Jquery Auto Search Complete Application!

Hi! Today I want to talk about web 2.0 technology. As you know, web 2.0 is being used by many of us. Especially frameworks developed with javascript and ajax, used by everyone. Well, we can say that huge companies which are google, yahoo, facebook and twitter etc. use web 2.0 too.

I would like to make an application on Jquery and share with you guys. So, in 2006, Jquery's started to be developed as an open source project. When you see "The write less, do more", you should understand that is Jquery! There is a powerful library of javascript, that's why we can do good jobs with the less code on Jquery.

If you want to see more information, you can visit the official web site of jquery. You can also find documentation and examples on it. That is here!

You must call as include jquery library on your web site, for the successful operation of your jquery code. You can find the library on Jquery Web Site easily.

The point I wanted to show you here is an ajax application on jquery. However, I am thinkin' that I should show you couple simple examples, before starting to applicaton.

$('#divID').slideUp(1000); //Sliding the div up
$('#divID').slideDown(1000); //Sliding the div down
$('#divID').slideToggle(1000); //Sliding the div up and down
$('#divID').FadeToggle(1000); //Displaying or hiding the div
$('#divID').html(“Hello World!”); //Printing something as HTML tags

$(‘a’).click(function() {
 $("#divID").animate({left:'+=100px'}, 2000); //when you click the mouse, do something
});

As you have seen, when you code jquery, you can control the web site all the way.
The application I'll show you, is an AUTO SEARCH COMPLETE!


This is the text field we'll use when searching.


This is the div layer will open during our php script works.
// JavaScript Document
$(document).ready(function() {
 $('#TextBox').keyup(function() {
  document.getElementById("result").style.display="block";
  var variable=$('#TextBox').val();
  $.ajax({
      url:"data.php",
      data:"TextBox="+variable,
      cache:false,
      async:false,
      method:'post',
      success:function(kitchen) {
          var message="Loading..";
          $('#result').html(message).load();
      }
  });
 });       
});

As you have seen, when keyup() function works, the system is getting data from data.php page. data.php page is shown below

 include("database.php");
 $data=$_REQUEST["TextBox"];
 if(!$data==0) { 
  $MyData=mysql_query("select * from names where name like '%$data%'");
  if(mysql_num_rows($MyData)<"1") {
   echo "There is nothing about it!"; 
  } else {
   echo "Languages!
"; while($MyList=mysql_fetch_array($MyData)) { echo $MyList["name"]."\n"; } } } else { echo "Please, start to write something.."; }


Today, we made a jquery search auto complete application. If you visit and see this application, just click demo page!

See you guys next article!

Monday, November 14, 2011

A Nokia application for system room temperature controlling.

One of the our previous article ( http://stdioe.blogspot.com/2011/10/checking-system-room-temperature-with.html ) was explaining how to get temperature information from some devices. We showed that related scripts saves the temperature value to a MySQL table. In this article, we are explaining publishing only the current temperature value on a public web page. The Nokia application will read that value to generate alarms when the value exceeds a critical value. The Nokia application is written by jbytecode to follow up via cell phones. If system room temperature reaches a predefined critical value, your mobile cell phone will create an alarm sound. Therefore you will be informed where ever you are.

The mobile phone application is an open source project and is being hosted on Google Code web site. You can access .jar and .jad files and source code on http://code.google.com/p/atlantis-networking-utilities web page address.Note that we call that "Nokia Application" because we only tested it with a Nokia E-72. Of course it is generally a Midlet application which can run in any supported device.

You can start the application by simply clicking the icon,

You have to define a temperature reader url on that program. If you need, you can change the checking period of system room temperature and define a critical temperature value,

After editing the configuration you can start the application,


The application needs internet connectivity. When it starts, it will show a confirmation alert and wait your response. Allow it.


Finally, it will start to read the current temperature value for the given time period. Received temperature values are logged in a text box.


This application is in its early stages. If you need extra features etc. let us know please. It's already an open-source project on code.google.com site. Every body can help to develop it or write to us ...


The current temperature publishing page is like following: You need to replace some parts on the code. MySQL server, user and password,DB_name, table_name and "temp" field names should be changed manually.

Also you may want to sort the historical values by date, so I am adding a date field on the sample db.


<?php
$link = mysql_connect("MySQL_Server","MySQL_User","MySQL_Password") or die("Error message");

mysql_select_db("DB_Name") or die("Error message");

$query = "SELECT * FROM `table_name ORDER by `date` DESC";
$result = mysql_query($query);
$line = mysql_fetch_array($result);
$temp = $line["temp"];
print $temp;
mysql_close($link);
?>

You will get only the curreny temperature value as a plain text whenever the page is accessed by the TempAlarmer. Please let us know your comments to develop it more.

Have Fun!


Monday, October 31, 2011

Handling Plots with RCallerPhp Edition

Now we have RCaller Php edition, named RCallerPhp, which is able to handle images generated by R. I hope it will bring an other easy solution for calling R from other platforms.

With this feature, web developers that use Php as their main language and need calculations done by R will easly use this library.

It is distributed as its big brother, RCaller Java Edition, with the LGPL (Lesser GNU Public License).
RCallerPhp is intended to be compatible as much as possible with the Java version. So, investigating the old examples may be helpful for understanding this new release.

In a time of less than a week, we released this version without the plotting support. By now, generating R plots and showing them in a browser is implemented. Generated plots are stored in the temp directory instead www directory. That is why we are encoding generated plots inside the img src tags with base64 encoding. You can have a look at the source code at RCaller source code at Google project hosting.

Handling plots with RCallerPhp is quite easy. Let's have a look at the code below:

<?php

require_once("../RCode.php");
require_once("../RCaller.php");
require_once("simpletest.php");

$caller = new RCaller();
$code = new RCode("");

$plot = $code->startPlot();
$code->addRCode("plot.ts(rnorm(10))");
$code->endPlot();

$caller->setRscriptExecutable("/usr/bin/Rscript");
$caller->setRCode($code);
$caller->runOnly();

print($caller->getPlot($plot));
?>

Here is the generated output, which is copied from the web browser:


Nothing is easier than this! Do not hasitate to ask anything about RCaller.
We hope you enjoy...
Have fun...

Sunday, October 30, 2011

RCallerPhp is ready for testing

Hey web guys! RCaller now supports Php and we are planning to carry RCaller to other platforms and languages. The first step of our attack plan was to implement a Php edition and it is ready for testing now.

The second step is to implement RCaller for Perl and Python. We have now our Perl developer and he is in progress. Python is not our primary language and we are waiting for your helps. If you are familier with R and a developer of one of those languages below, join us. We are planning to carry RCaller to

  • Python
  • .Net
at first.

And... How it looks like.. Let's give up Java and speak Php for a minute:

 1 <?php
 2 include_once ("RCaller.php");
 3 
 4 $rcaller = new RCaller();
 5   $rcaller->setRscriptExecutable("/usr/bin/Rscript");
 6   $rcode = new RCode("");
 7   $rcode->clear();
 8   $rcode->addRCode("mylist <- list(x=1:3, y=c(7,8,9))");
 9 
10   $rcaller->setRCode($rcode);
11   $rcaller->runAndReturnResult("mylist");
12 
13   $x = $rcaller->getParser()->getAsStringArray("x");
14   $y = $rcaller->getParser()->getAsStringArray("y");
15 
16   echo ("X is <br>");
17   print_r ($x);
18 
19   echo ("<br><br>Y is <br>");
20   print_r ($y);
21 ?>


Waaav! Nothings changes! When you run this code, you will see values of x as 1, 2 and 3 and values of y as 7, 8, 9... The code above seems 100% compatible with the original library...

If you have used RCaller (Java edition) before, you will probably
understand the whole code. If not, lets have a look at the page RCaller 2.0 - Calling R from Java.


Note that, it is as in-efficient as the original version. Because RCaller creates external Rscript processes in each time RunAndReturnResult() thingies called. Be careful before using it in big and critical projects. Another note is about using it with too many users. RCaller uses temp directory to store its R codes and outputs. You need to clear this directory periodically. Otherwise you can have a "too many files" error.


Finally, source of is ready for use and development. Please visit the RCaller source code and downloads page. Php codes are stored as a separate project with name RCallerPhp.

Test it and do not hasitate to ask us!