giovedì 24 marzo 2016

How to move a web project from Glassfish to Tomcat?

Today I stumbled upon a web project that was configured for Glassfish instead of Tomcat. Since I am very fond of Tomcat, which integrates perfectly with my Eclipse IDE, I did not want to change my forma mentis and tools just to comply with the former configuration.

Unfortunately, when I tried to launch the project, it was not reading properly static resources (e.g., CSS files), since Glassfish has an XML configuration file (which is a substitute of the plain old web.xml file) in which you specify the context root of your project, i.e.:

http://localhost:8080/context-root/the-page-you-want

Tomcat is configured so as to use as context root the name of the project. So, how to fix this?
A solution can be found here, and is applied as follows:

  1. Project -> Properties -> Web Project Settings
  2. Modify the context root with the name you want and confirm
  3. Stop Tomcat
  4. Clean the Tomcat server by right-clicking on the server itself and selecting Clean
  5. Restart the server
  6. Confirm that you want to change the context root
...and you are done!

venerdì 11 marzo 2016

Use JPA with Maven projects in Eclipse

Want to build your own metamodel in Eclipse?

Prerequisite: use EclipseLink
Add the following to your pom.xml file:


        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.1</version>
            <scope>provided</scope>
        </dependency>

First step: Modify your pom.xml file
The pom.xml file has to be modified to include the following plugin:

            <plugin>
                <groupId>org.bsc.maven</groupId>
                <artifactId>maven-processor-plugin</artifactId>
                <version>2.2.4</version>
                <executions>
                    <execution>
                        <id>process</id>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <processors>
                                <processor>org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor</processor>
                            </processors>
                            <outputDirectory>${project.build.directory}/generated-sources/meta-model</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    
                    <dependency>
                        <groupId>org.eclipse.persistence</groupId>
                        <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
                        <version>2.6.1</version>
                    </dependency>
                </dependencies>
            </plugin>

Second step: add the Generated-sources folder
Then, open the project properties and move to Java Build Path. In the "Source" tab, select "Add folder" and then add "target/generated-sources".
Build the project.

Third step: compile
Finally, again in project properties, select the JPA menu, select EclipseLink as a platform, allow Discover annotated classes automatically, and then press Apply. From this point on, the generated-sources folder should contain your compiled metamodel.

lunedì 29 febbraio 2016

What is a model? A nice definition

Eric Evans, during this talk about Domain-Driven Design (DDD), defines nicely a model as

a system of abstractions that describes selected aspects of a domain and can be used to solve problems related to that domain

mercoledì 10 febbraio 2016

CPM tutorial

You can find a nice and practical guide to the Critical Path Method here.

JPA in Eclipse: drop database at each test

If you want to drop your database at each test launch, open the file persistence.xml (under JPA) and modify the following line:

 <property name="javax.persistence.schema-generation.database.action" value="create"/>

to become as follows:

 <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>

giovedì 10 dicembre 2015

Levenshtein distance, AKA "how to compute similarity between strings"

The Levenshtein distance is a useful distance metrics that you can use to state whether two strings are similar between each other. It computes the number of different characters of a pair of strings.
The Wikipedia page proposes two alternatives:

  1. the first one is recursive
  2. the second one is iterative
Although they are fully working, and the recursive one is elegant to be read, when I found myself in implementing it in JavaScript to compare pairs of tweet texts, the recursive one wouldn't come to a result. It took ages and did not finish the computation for the pair of provided strings!
Thus, I found this nice test case which compares the two versions (in JavaScript):

[Oh, well, why JavaScript? Tweets are collected in JSON format and stored in MongoDB, and when you are required to analyze in a quick-and-dirty manner your dataset, you can JavaScript functions directly via the MongoDB console]

martedì 1 dicembre 2015

Classification of imbalanced datasets

How to overcome these binary text classification problems?
1) Highly imbalanced dataset (less than 10% positive samples)
2) Positive class subjectivity

To overcome problem 1, one could use more refined classification systems (boosting, bagging, e.g., AdaBoost for starters), although a very large dataset (even if imbalanced) could help ease the problem. This complicates the situation "a little bit": to collect enough positive samples one has to annotate tons of instances.

To overcome problem 2, one could find a god to pray.