Archive

Archive for June, 2015

Drools 6 Decision Table with Spring MVC

June 8, 2015 Leave a comment

Drools is the Java Rules engine from Jboss. This article covers on how to use the Drools Decision Table using excel sheets with a Spring MVC project.

To start with, create a basic Spring MVC project using Spring STS. Add the following dependencies for drools on the pom file generated by the template in STS.


<dependency>
 <groupId>org.drools</groupId>
 <artifactId>drools-core</artifactId>
 <version>6.2.0.Final</version> 
 </dependency>
 <dependency>
 <groupId>org.drools</groupId>
 <artifactId>drools-compiler</artifactId>
 <version>6.2.0.Final</version> 
 </dependency>
 <dependency>
 <groupId>org.kie</groupId>
 <artifactId>kie-spring</artifactId>
 <version>6.2.0.Final</version> 
 </dependency>
 <dependency>
 <groupId>org.kie</groupId>
 <artifactId>kie-api</artifactId>
 <version>6.2.0.Final</version> 
 </dependency>
 <dependency>
 <groupId>org.kie</groupId>
 <artifactId>kie-internal</artifactId>
 <version>6.2.0.Final</version> 
 </dependency>

Create a spring-context.xml file in the following location /WEB-INF/spring with the following content


<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:kie="http://drools.org/schema/kie-spring"
 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://drools.org/schema/kie-spring http://drools.org/schema/kie-spring.xsd">

<kie:kmodule id="kmodule1">
 <kie:kbase name="kbase1" packages="com.codesilo.wordpress.rules">
 <kie:ksession name="ksession1" type="stateless" />
 </kie:kbase>
 </kie:kmodule>

<beans:bean id="kiePostProcessor"
 class="org.kie.spring.KModuleBeanFactoryPostProcessor" />

</beans:beans>

The package here “com.codesilo.wordpress.rules” denotes the location where the decision tables are located. We will create that one we have created the POJO.

Add the /WEB-INF/spring/spring-context.xml on the contextConfigLocation in web.xml. The contextConfigLocation should now look like


<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/spring/root-context.xml
 /WEB-INF/spring/root-context.xml</param-value>
 </context-param>

We create the POJO named as RouteAttribute in the following package com.wordpress.codesilo.model. This object will be used as the fact for the rules defined,


package com.wordpress.codesilo.model;

public class RouteAttribute {

private String country;
 private String state;
 private boolean newEntry;
 
 //Result
 private String ansOnRule;

public String getCountry() {
 return country;
 }

public void setCountry(String country) {
 this.country = country;
 }

public String getState() {
 return state;
 }

public void setState(String state) {
 this.state = state;
 }

public boolean isNewEntry() {
 return newEntry;
 }

public void setNewEntry(boolean newEntry) {
 this.newEntry = newEntry;
 }

public String getAnsOnRule() {
 return ansOnRule;
 }

public void setAnsOnRule(String ansOnRule) {
 this.ansOnRule = ansOnRule;
 }
 
}


Now we create the decision table (Rules.xls) on the following location com.codesilo.wordpress.rules

Rules

The file can also be downloaded from here.

As a last step, we change HomeController.java to fire the rules. This can be later moved to any controller/MVC flow. We inject the KieBase object by the following..

@Autowired
private KieBase kieBase;

Add the following in the default method on the HomeController class.


RouteAttribute routingComponent = new RouteAttribute();

routingComponent.setCountry("US");

StatelessKieSession statelessKieSession =  kbase.newStatelessKieSession();

statelessKieSession.execute(routingComponent);

Now, if we bring up the server, the default method is going to run and call the rules from above.

Advertisements

Monoprice 10x 6.25 Graphic Tablet on Ubuntu

June 1, 2015 Leave a comment

I recently purchased a 10×6.25 Monoprice Graphic tablet from Monoprice. (wasn’t at a spot to shell out the amount that Wacom was asking for it’s tablet). The tablet has lived upto my expectations so far but did not work as expected when I tried using it on Ubuntu 14.04.

Checking the output from lsusb shows that the graphic tablet is a rebranded UC Logic tablet. The following output shows on running lsusb on the terminal

Bus 005 Device 002: ID 5543:0781 UC-Logic Technology Corp.

Searching on Google showed me that UC Logic graphic tablets can be configured for Ubuntu by using the Wizardpen driver. The DIGImend drivers do not support it but they have a good article on how to configure these rebranded tablets. The steps below are just a summary of what I did based on the article found here (https://digimend.github.io/support/howto/drivers/wizardpen/)

  • Download the Wizardpen driver using the following command
wget https://launchpad.net/~doctormo/+archive/xorg-wizardpen/+files/xserver-xorg-input-wizardpen_0.8.1-0ubuntu3.tar.gz
  • Untar the driver that we downloaded above
tar xvzf xserver-xorg-input-wizardpen_0.8.1-0ubuntu3.tar.gz
  • Install the required dependencies from Wizardpen
sudo apt-get install build-essential xutils-dev xutils libx11-dev libxext-dev xautomation xinput xserver-xorg-dev autoconf libtool pkg-config
  • Go to the extracted Wizardpen dir above and run the following commands
$ ./autogen.sh --prefix=/usr
$ make
$ sudo make install
  • Go to the x.org.conf dir in usr/share/X11 and create the file 52-tablet.conf and add the following
Section "InputClass"
 Identifier "Tablet on WizardPen"
 #MatchIsTablet "on"
 MatchProduct "keyword"
 MatchDevicePath "/dev/input/event*"
 Driver "wizardpen"
 # Apply custom Options below.
EndSection
  • The keyword above needs to be replaced by the product id for your graphic tablet. In my case I ran the xinput list with the tablet plugged in and got the following in the output
↳ UC-LOGIC TWHA60 id=11 [slave pointer (2)]
⎜ ↳ UC-LOGIC TWHA60 id=12 [slave pointer (2)]
⎜ ↳ UC-LOGIC TWHA60 id=13 [slave pointer (2)]
  • Based on the output above, I changed the keyword in the conf and the conf now looked like this..
Section "InputClass"
 Identifier "Tablet on WizardPen"
 #MatchIsTablet "on"
 MatchProduct "UC-LOGIC TWHA60"
 MatchDevicePath "/dev/input/event*"
 Driver "wizardpen"
 # Apply custom Options below.
EndSection
  • After going through the above steps, my graphics tablet was detected but the stylus did not position itself correctly. I decided to try calibrating the tablet as suggested in the DIGImend pages. This requires to run a calibrate command for wizardpen from the “calibrate” dir in the downloaded wizardpen files. Before doing that we need to identify the tablet-event to be supplied to the command. In order to get it, run the following command in the terminal
ls /dev/input/by-id/
  • The output produced by the command above in my case was the following..
usb-UC-LOGIC_TWHA60-event-mouse
usb-UC-LOGIC_TWHA60-if01-event-mouse
usb-UC-LOGIC_TWHA60-if01-mouse
usb-UC-LOGIC_TWHA60-if02-event-kbd
usb-UC-LOGIC_TWHA60-mouse
  • Now run the following command to calibrate (needs to be run in the calibrate folder in wizardpen)
sudo ./wizardpen-calibrate /dev/input/by-id/usb-UC-LOGIC_TWHA60-if01-event-mouse

The command then prompts you to click on one of the corners on the graphic tablet using the stylus. Once clicked, it asks to click on the opposite end of the tablet. The full output after the clicks looks like follows.

Please, press the stilus at ANY
corner of your desired working area: ok, got 9,6

Please, press the stilus at OPPOSITE
corner of your desired working area: ok, got 2047,2047

According to your input you may put the following
lines into your XF86Config/X.Org configuration file:

 Driver "wizardpen"
 Option "Device" "/dev/input/by-id/usb-UC-LOGIC_TWHA60-event-mouse"
 Option "TopX" "9"
 Option "TopY" "6"
 Option "BottomX" "2047"
 Option "BottomY" "2047"
  • Apply this to the conf file created earlier. My conf file looked like this after the changes.
Section "InputClass"
 Identifier "Tablet on WizardPen"
 MatchIsTablet "on"
 MatchProduct "UC-LOGIC TWHA60"
 MatchDevicePath "/dev/input/event*"
 Driver "wizardpen"
 # Apply custom Options below.
 Option "Device" "/dev/input/by-id/usb-UC-LOGIC_TWHA60-event-mouse"
 Option "TopX" "7"
 Option "TopY" "18"
 Option "BottomX" "2047"
 Option "BottomY" "2047"
EndSection
  • Restarting the system should get the configuration in effect and the tablet should start working fine.