Thursday, December 6, 2012

RXTXComm lirary NullPointerException at gnu.io.RXTXPort.setSerialPortParams(RXTXPort.java:178)

RXTXComm library is great, but if you want to use it from not signed applet - you could get NullPointerException exception like this:
java.lang.NullPointerException
     at gnu.io.RXTXPort.setSerialPortParams(RXTXPort.java:178)
     at JAppletExample$Captura$1.run(JAppletExample.java:79)
     at java.security.AccessController.doPrivileged(Native Method)
     at JAppletExample$Captura.connect(JAppletExample.java:62)
     at JAppletExample.actionPerformed(JAppletExample.java:45)
     at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
     at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
     at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
     at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
     at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
     at java.awt.Component.processMouseEvent(Unknown Source)
     at javax.swing.JComponent.processMouseEvent(Unknown Source)
     at java.awt.Component.processEvent(Unknown Source)
     at java.awt.Container.processEvent(Unknown Source)
     at java.awt.Component.dispatchEventImpl(Unknown Source)
     at java.awt.Container.dispatchEventImpl(Unknown Source)
     at java.awt.Component.dispatchEvent(Unknown Source)
     at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
     at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
     at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
     at java.awt.Container.dispatchEventImpl(Unknown Source)
     at java.awt.Component.dispatchEvent(Unknown Source)
     at java.awt.EventQueue.dispatchEvent(Unknown Source)
     at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
     at java.awt.EventDispatchThread.run(Unknown Source)
    

To fix it - add this line to java.policy file:
permission "java.util.PropertyPermission" "gnu.io.log.mode", "read";

Tuesday, June 19, 2012

JQWicket date picker

JQWicket is JQeury wrapper for Wicket. To get started import maven dependency:
   <dependency> 
       <groupId>com.google.code.jqwicket</groupId> 
       <artifactId>jqwicket</artifactId> 
       <version>0.8</version> 
   </dependency>
Java code:
In you application init method add these lines. They will include JQeury javascript libs to your pages.
import com.google.code.jqwicket.JQComponentOnBeforeRenderListener;
import com.google.code.jqwicket.JQContributionConfig;

super.getComponentPreOnBeforeRenderListeners().add(
    new JQComponentOnBeforeRenderListener(
        new JQContributionConfig().withDefaultJQueryUi()));

Then in your page:
import com.google.code.jqwicket.ui.datetimepicker.DateTimePickerTextField;


public class DatePickerPage extends WebPage {
    Logger log = LoggerFactory.getLogger(DatePickerPage.class);
    Date date = new Date();

    public DatePickerPage() {
        Form form = new Form("form"){
            @Override
            protected void onSubmit() {
                log.info("return value:"+date);
            }
        };
        form.add(new DateTimePickerTextField<Date>("datetimepicker", new PropertyModel<Date>(this, "date")));
        form.add(new SubmitLink("submit"));
        add(form);
    }
//Add getter and setter for a date
}
And your HTML should look something like this:
!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<form wicket:id="form">
    Date: <input type="text" wicket:id="datetimepicker"/>
    <a wicket:id="submit">Go</a>
</form>
</body>
</html> 

Result:


But I still like more another date picker, because out of the box this one:
1. It looks over sized;
2. You need to convert its value from String to Date manually.

Tuesday, June 12, 2012

Zebra Puzzle gets solved in Java

For one of job interview I was asked for Zebra Puzzle solution in Java.
Considering you have input file provided bellow you need to create output xml.
Input:
5
SAME;nationality;English;color;Red
SAME;nationality;Spaniard;pet;Dog
SAME;drink;Coffee;color;Green
SAME;drink;Tea;nationality;Ukrainian
TO_THE_LEFT_OF;color;Green;color;Ivory
SAME;smoke;Old gold;pet;Snails
SAME;smoke;Kools;color;Yellow
SAME;drink;Milk;position;3
SAME;nationality;Norwegian;position;1
NEXT_TO;pet;Fox;smoke;Chesterfields
NEXT_TO;smoke;Kools;pet;Horse
SAME;smoke;Lucky strike;drink;Orange juice
SAME;smoke;Parliaments;nationality;Japanese
NEXT_TO;nationality;Norwegian;color;Blue
SAME;drink;Water
SAME;pet;Zebra

Output:
<solutions>
  <solution>
    <house color="Yellow" drink="Water" nationality="Norwegian" pet="Fox" position="1" smoke="Kools"/>
    <house color="Blue" drink="Tea" nationality="Ukrainian" pet="Horse" position="2" smoke="Chesterfields"/>
    <house color="Red" drink="Milk" nationality="English" pet="Snails" position="3" smoke="Old gold"/>
    <house color="Ivory" drink="Orange juice" nationality="Spaniard" pet="Dog" position="4" smoke="Lucky strike"/>
    <house color="Green" drink="Coffee" nationality="Japanese" pet="Zebra" position="5" smoke="Parliaments"/>
  </solution>
</solutions>

You can download source code and JUnit tests here. Inside that zip you will also find xslt to render xml nice way.

Solution

So, we have to fill [5,6] array. The easiest way would be to check all possible combinations, but that will be slow because there are (5!)^6=2985984000000 combinations. To speedup we prefill array with data we already know is correct: we have all nationalities and additional attribute to them. That will leave us to fill array [5,4] i.e. (5!)^4=207360000. It is already 10000 times less combinations.
Next step is to start guessing values. But not randomly. Lets start from color because it has biggest amounts of clues and because of that some rules will fail immediately and we will not need to go deeper and fill other values. Then - go deeper and take another key with highest amount of clues.
The code provides solution in 90-1460 iterations.

Monday, June 11, 2012

Nice Action buttons meets Wicket

For my Block A Sender project I needed nice action buttons. I was lucky to find this post.  Here is how they look:
Google style buttons
To get them in Wicket is really simple:
  1. Grab CSS from this post. and add to your project
  2. Add some link in java:
     add(new BookmarkablePageLink("home_link", HomePage.class));
  3. And HTML
    <button class="action bluebtn" wicket:id="home_link">Home</button>
That's it :)

Friday, May 18, 2012

Wicket & Guice - how to start service on application start

I have Wicket web application and I am using Guice to inject services whatever I want. Some thing like here. Everything works great, but I needed to run some recovery routine on application startup. The only way I found is this:
public class WicketApplication extends WebApplication{
    @Override
 public void init()
 {
  super.init();

        GuiceModule guiceModule = new GuiceModule(usesDeploymentConfig());
        GuiceComponentInjector injector = new GuiceComponentInjector(this, guiceModule);
        getComponentInstantiationListeners().add(injector);
        
        //Here is interesting part
        GuiceInjectorHolder holder = Application.get().getMetaData(GuiceInjectorHolder.INJECTOR_KEY);
        holder.getInjector().getProvider(EmailSenderService.class).get().recover();
    }
}

Enjoy!


Thursday, April 26, 2012

Predicting Forex with Google preditct - Atempt #1

Since there lot of math involved in AI I was very happy to find that Google has product exactly for that. And to use it is relatively simple. My first attempt was to predict trading signals for Forex i.e. where to buy  (close buy) and to where sell (close sell). So I went through the price history and tagged time points as I would be actual trading on historical data. Here is an example:
Purple - SELL, yellow - CLOSE_SELL, green - BUY, blue - CLOSE_BUY
So after few days I had 44177 time points tagged with 5 different values: BUY, CLOSE_BUY, SELL, CLOSE_SELL and NONE. From all of these time points - 4724 were tagged with some signal and all others as NONE.
Then it was time to prepare prediction data. My learning data was similar to this:
SELL, 99 price values before time point, price at current time point
NONE, 99 price values before time point, price at current time point
NONE, 99 price values before time point, price at current time point
NONE, 99 price values before time point, price at current time point
....
CLOSE_SELL, 99 price values before time point, price at current time point
...
Model training took about 1.5 hours and Google Predict ended up with "classificationAccuracy": 0.9 
It was time to test the model.  To test the model I again use historical data. I sent about 60 request (some of them were from the same data I used to train model) and for all of then response was NONE. So I consider this as failure, but there is also bright side:
  1. Found a way how not to use AI and what learning data is considered bad;
  2. Learned how to use OAuth2 and how to use Google services from my code;
  3. Got experience with MongoDB (noSQL database);
  4. Refreshed my knowledge in Wicket, Swing and JFreeChart;
  5. It was fun and I got new ideas to test.

Wednesday, April 25, 2012

Google Predict - how to predict from your code using OAuth2

I had an idea to try Google Predict product to predict Forex. I will make another post about my attempt and in this post will be example of how to start using Google Predict and how actually access prediction from your code.
To get started please read this article. The article covers all the steps you need to to except the last one: how to get prediction in your code? While I was trying to do this I found Google libraries for this - don't try them - it is hard to understand them and they use old accessing framework OAuth1 which will be turned off soon. The good thing that Google also support new OAuth2 framework which is much more simple than ver1. I wrote an article about how easy is to use OAuth2 from any programing language.
Simple steps to get prediction:
  1. Create training data, upload it and train your model as described here.
  2. Get access token as described. Note: then creating URL to request user approve, in scope parameter include also 'https://www.googleapis.com/auth/prediction'
  3. Send you data sample to get prediction. The Java code is below.

try {
    String access_token="the token you received from Google from step 2";
    String modelId = "trained model ID you created in step 1";
    List<Object> values; //here should be all your data which will be sent to Google Predict to get prediction
    
    Scheme httpsScheme = new Scheme("https", 443, new EasySSLSocketFactory());
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(httpsScheme);

    ClientConnectionManager cm = new BasicClientConnectionManager(schemeRegistry);
    HttpClient httpClient = new DefaultHttpClient(cm);

    HttpPost httppost = new HttpPost("https://www.googleapis.com/prediction/v1.5/trainedmodels/"+modelId+"/predict?access_token="+access_token);
    httppost.setEntity(new StringEntity(getRequestBody(values)));
    httppost.setHeader("Content-type", "application/json");

    log.debug("executing request to get token:" + httppost.getRequestLine());

    HttpResponse response = httpClient.execute(httppost);

    HttpEntity entity = response.getEntity();

    log.debug("response status:" + response.getStatusLine());
    ContentType contentType = ContentType.getOrDefault(entity);
    if (contentType.getMimeType().equals("application/json")) {
        //Here is reply from Google Predict
        String responseStr = IOUtils.toString(entity.getContent());
        log.debug("parsing json response:"+responseStr);
    }
    EntityUtils.consume(entity);
    httppost.releaseConnection();
    httpClient.getConnectionManager().shutdown();
} catch (Exception e) {
    log.error("Error retrieving prediction", e);
}


/*
Google predict expects input as this:
{
"input":{
  "csvInstance":[ col1_value, col2_value, ... ]
}
}
*/
private String getRequestBody(List<Object> values){
    StringBuffer sb = new StringBuffer("{\n\"input\":{\n\"csvInstance\":[");
    for(Object o:values){
        sb.append(o).append(",");
    }
    sb.deleteCharAt(sb.length()-1); //delete last coma(,)
    sb.append("]\n}\n}");
    log.debug("request body:"+sb.toString());
    return sb.toString();
}

 Now then you know basics you can read about other web services Google prediction support. Good luck predicting!