martes, abril 29, 2008

... and all he captures is endless rain.


At last entry i wrote about developing a Taget Adapter for SpringIntegration Module. In thi sentry i will talk about developing a SourceAdapter.
As my last entry, Jabber server will be used, and Smack API for connecting and receiving messages.
From previous entry server configuration, and some classes will be used, so strictly, only SourceAdapter and its configuration are going to be explain.
Firstly, but, a little explanation about two possible kind of communication for source adapters:
  • Poll: which means that every period of time the program are checking if new content is available. For example FTP Source Adapter is an example because ftp server doesn't throw an interruption when another user upload a file.
  • Driven-Event: which means that an asynchronous event is thrown and can be captured. Our IM Adapter is an example.

Each SourceAdapter must implements SourceAdapter interface. But as TargetAdapter, there are some classes that help developers in writting an Adapter. At least two classes are provided for developing a SourceAdapter that, of course, implements SourceAdapter interface:
  • AbstractSourceAdapter: A base class providing common behavior for source adapters. It has some methods for sending messages to a channel, set message mappers, ...
  • PollingSourceAdapter: A base class for polling if new message is available to be sent. It has some methods like period between polls, initial delay, ...

Our case requires extending AbstractSourceAdapter.
IMSourceAdapter extends AbstractSourceAdapter implements
InitializingBean

IMMessage is a class that represents a message sent through channel. IMMessage is a self developed class and its explanation can be found on previous post.
Because of event communication, Smack API requires that you subscribe to an event implementing an interface, so each time an event occurs, that interface is executed. This initialization is developed in initialize() Spring method and next piece of code is the business logic.


final XMPPConnection connection = new XMPPConnection(this.server); (1)
try {
connection.connect();
connection.login(this.user, this.password);
} catch (final XMPPException e) {
throw new MessageHandlingException("Error starting", e);
}

final PacketFilter packetFilter = new MessageTypeFilter(
Message.Type.chat);
connection.createPacketCollector(packetFilter); (2)

final PacketListener packetListener = new PacketListener() { (3)

@Override
public void processPacket(Packet arg0) {
String msn = ((Message) arg0).getBody(); (4)
SimpleIMMessage simpleIMMessage = new SimpleIMMessage();
simpleIMMessage.setText(msn); (5)
IMSourceAdapter.this.sendToChannel(simpleIMMessage); (6)
}
};
connection.addPacketListener(packetListener, packetFilter); (7)

(1) Uses Smack XMPPConnection for connecting to server.
(2) Creats a filter for throwing only events when a new chat message is received.
(3) Creats the business logic that must be executed each time a message is received.
(4) Gets the body of the message. (What the other user has written).
(5) Creats an IMMessage for sending through channel.
(6) Send the message through MessageChannel.
(7) Register the event.
As can be seen, the only important call is sendToChannel that sends the message to spring integration structure, the other logic may vary depending on source adapter that are going to be developed.
XML configuration file is so easy:
<?xml version="1.0" encoding="UTF-8"?>
<
beans:beans xmlns="http://www.springframework.org/schema/integration"
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:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

<message-bus />
<annotation-driven />

<channel id="inMessage"></channel>
<beans:bean id="sourceIM" class="test.spring.integration.im.IMSourceAdapter">
<beans:property name="user" value="logger">
</beans:property>
<beans:property name="password" value="logger">
</beans:property>
<beans:property name="server" value="localhost">
</beans:property>
<beans:property name="channel" ref="inMessage">
</beans:property>
<beans:property name="messageMapper">
<beans:bean class="test.spring.integration.im.TextIMMessageMapper">
</beans:bean>
</beans:property>
</beans:bean>

<file-target directory="/temp" channel="inMessage"/>
</beans:beans>

For running the example, start the Jabber Server (OpenFire), start a client Spark, and talk to logger user. Each message will be saved one file.
SpringIntegration 1.0M3 has been used for developing the TargetAdapter.


martes, abril 15, 2008

There are four new colours on the rainbow, an old man takes polaroids ...


Spring Integration is a new project from Spring Framework developers. As it says on its website:
"Provides a programming model to support the well-known Enterprise Integration Patterns (...) and enables simple messaging within Spring-based applications and integrates with external systems via simple adapters."
In this entry, but is supposed that some basic concepts about Spring Integration are known. What we are going to explain is the development of a new adapter.
But what is an Adapter? In Spring Integration, an adapter is a component that interacts with external systems (servers, other components ...). As its name suggests, an adapter adapts the input of an external component and it transforms to something that can be sent through spring-integration pipe, and also adapts what is received from the pipe for sending to external component.
That pipe is MessageChannel class. Spring Integration is built with different adapters, JMS, RMI, Files, Mail, ... That's good while you are working with systems that required adapters are already implemented, but, what's happens if your application requires communication with an external system which an adapter has not already developed? The solution is easy, implement yourself the adapter.
That's what I am going to explain, how to implements a TargetAdapter (adapter for output messaging). In next entry would be explain how to implement a SourceAdapter (adapter for incoming messages).
Only two classes are really important org.springframework.integration.handler.MessageHandler that is used for handle the messages, and org.springframework.integration.message.AbstractMessageMapper that transform:
  1. From specific input to Spring Integration Message.
  2. From Spring Integration Message to specific output.

In this example, integration to instant messaging system will be used. The basic idea is develop a system that sent a message from Spring Integration to a XMPP server (a.k.a Jabber) user account.
Full schema of application is:

For running, apart from Spring/Spring Integration jars, JDK ..., two programs and an API are required:
  1. OpenFire, that's a XMPP server. XMPP is an open, XML-inspired protocol for near real time, extensible instant messaging (IM) and presence information. http://www.igniterealtime.org/projects/openfire/index.jsp
  2. Spark, a client for communicating with XMPP server. http://www.igniterealtime.org/projects/spark/index.jsp
  3. Smack, an API for communicating with XMPP server. http://www.igniterealtime.org/projects/smack/index.jsp

First install OpenFire server and add two users:
- login) logger password) logger
- login) incidence password) incidence
Second, install Spark, and then login with logger account and add incidence user as buddy and logout, after that login with incidence and add logger user as buddy, and don't logout.
Third, put smack jar and its dependencies into class path (Spring dependencies are there too).
Now, start the development of two most important classes:
class IMTargetAdapter implements MessageHandler, InitializingBean


Acts as an adapter through internal messages to external messages. The most important method is handle.


public Message handle(final Message msn{
final IMMessage iMessage = this.textMessage
.fromMessage((Message) msn); (1)
iMessage.setTo(this.getReceiver());
try {
this.xmppSender.send((SimpleIMMessage) iMessage); (2)
} catch (final IMException e) {
throw new MessageHandlingException("Error while sending message.",
e); (3)
}
return null;
}
(1) using a MessageMapper the spring integration message class (msn variable) is transformed to specific external system messages.
(2) using xmppSender (Facade for accessing Smack Library [out of scope of this entry]), the message is sent.
(3) case that an error occurs, MessageHandlingException must be thrown.

And finally class TextIMMessageMapper extends
AbstractMessageMapper
That has two important methods:

public IMMessage fromMessage(final Message msn) { (1)
final SimpleIMMessage simpleIMMessage = new SimpleIMMessage();
simpleIMMessage.setText(msn.getPayload());
return simpleIMMessage;
}
public Message toMessage(final IMMessage msn) { (2)
return new StringMessage(((SimpleIMMessage) msn).getText());
}
(1) transforms spring integration message to external message. SimpleIMMessage is a class that wraps messages that are sent using Smack API.
(2) transforms an external message to spring integration message (used in SourceAdapters).
Then it is time to configure the Spring XML file for configure channels and TargetAdapters so messages can be delivered to Instant Message Service.

<channel id="output"></channel> (1)

<beans:bean id="defaultMessageEndpoint" class="org.springframework.integration.endpoint.DefaultMessageEndpoint">(2)
<beans:property name="handler">
<beans:bean class="test.spring.integration.im.IMTargetAdapter">(3)
<beans:property name="xmppSender" ref="xmppIM"></beans:property>
<beans:property name="receiver" value="incidence@grumpy"></beans:property>
</beans:bean>
</beans:property>
<beans:property name="subscription">(4)
<beans:bean class="org.springframework.integration.scheduling.Subscription">
<beans:constructor-arg type="java.lang.String" value="output"></beans:constructor-arg>
</beans:bean>
</beans:property>
</beans:bean>


(1) Defines the output channel.
(2) Each component that is connected into MessageChannel must be a MessageEndpoint. DefaultMessageEndpoint is a helper class for this porpoise. Because TargetAdapter is not an Endpoint, it must be wrapped into one.
(3) Handler method is method that will be invoked by MessageEndpoint when a message is received. In this case, a MessageHandler (IMTargetAdapter) is provided, with required information (Facade for communicating with Smack API, and the receiver of messages (incidence@).
(4) MessageEndpoint must be subscribed into a channel.
Using Namehandler is out of scope of this document; of course it would be cleaner using Ext. XML authoring.
And that's all; you can download source code for trying, but remember to change receiver value.
The example has been developed using:
JDK 6.0
Eclipse 3.3

P.S. This entry was written using spring integration 1.0.0 M3


sábado, agosto 25, 2007

Fishing (none technical entry :P)

Well after all my work mates laught at me with my last (and unsuccessful) fishing experience, (too long to explain in a blog sorry), the other day i went to fish again, and this time of course "who doubt it", the experience was successful. Five gilthead bream.

Of course, i won't tell where we go cause this is like mushroom fields, nobody tells the secret place. But the view is beautiful.



Well as i told, i went to fish with Jessi and two friends, with two fishing rods and ten worms from 22:00 to 02:00.

Cause im sure my work mates won't believe me, i upload some photos.



And of course next day we got a great launch with our treasures.






viernes, noviembre 03, 2006

After Spring, the Summer Comes



Hello, in blog "After Spring, the Summer Comes", I will summarize interfaces of Spring Core Framework for extending its funcionalities. I'm an active member in Spring Forum, so I have seen some questions about extending funcionalities in Spring or how to modify some behavior of Framework, for this reason I have decided to write down some extension points of Core.

  • First interface is (application/Bean)FactoryAware if your class implements previous interface, and also have an attribute of (application/BeanFactory) and a setter method, Spring will inject the current loaded class. (See 3.3.8 for more information). Moreover it is explained how to avoid the intrusion of Spring into our code.
  • Next interface MethodReplacer is used for changing implementation of a method. Spring will inject new implementation to existing class method. (See 3.3.8.2 for an example).
  • Another interesting interface is org.springframework.beans.factory.config.Scope. Spring 2.0, implements five different scopes, prototype, singleton, request, session, global session. Of course, Spring offers the possibility of creating our scope by implementing Scope interface. In chapter 3.4.4, an example can be found.
  • Two interfaces are provided to control lifecycle of bean. These two interfaces are org.springframework.beans.factory.InitializingBean that are executed after properties of bean are setted, and org.springframework.beans.factory.DisposableBean that is called when container containing the bean is being destroyed. Of course it is better to use next approach, that is using init-method and destroy-method from Spring file, so no Spring dependencies are inserted into our code. This last option is preferred but these two exceptions also exists. (See Chapter 3.5.1).
  • Another interface is org.springframework.beans.factory.BeanNameAware. The BeanFactory will call the bean through this interface to inform the bean of the id it was deployed under. (See 3.5.2.2 for a little description).
  • Next interface BeanPostProcessor is used if you want to do some custom logic after the Spring container has finished instantiating, configuring and otherwise initializing a bean, you can plug in one or more BeanPostProcessor implementations. (See Chapter 3.7.1).
  • The next extension point that we will look at is the org.springframework.beans.factory.config.BeanFactoryPostProcessor. BeanFactoryPostProcessors reads the configuration metadata and potentially change it before the container has actually instantied any other beans. Typical example is PropertyPlaceholderConfigurer, where values injected into a bean are changed for .properties file. (See Chapter 3.7.2 for full information).
  • Next very important interface is org.springframework.beans.factory.FactoryBean. This interface must be implemented by objects that are themselves factories, and you want to use Spring IoC. If you have some complex initialization code that is better expressed in Java as opposed to a (potentially) verbose amount of XML, you can create your own FactoryBean, write the complex initialization inside that class, and then plug your custom FactoryBean into the container. (See Chapter 3.7.3).
  • Spring has events, two interfaces must be implemented ApplicationContext and ApplicationEvent. Every time an ApplicationEvent gets published to the ApplicationContext, that bean will be notified.
  • Spring's Resource interface is meant to be a more capable interface for abstracting access to low-level resources. It is used in Spring and by Spring.
  • Spring's features a Validator interface that you can use to validate objects. The Validator interface works using an Errors object so that while validating, validators can report validation failures to the Errors object.
  • PropertyEditorSupport is a class not interface, but also it is used for extending Spring. PropertyEditors are used for converting String format to the complex type of a property. For example Spring provides LocaleEditor that converts an String value to a Locale class. (In chapter 5.4.2.1 an example is provided).
  • The last class I explain for extending Spring is NamespaceHandler and interface BeanDefinitionParser. Both classes are used for extending basic XML Spring file. You can write your own custom XML bean definition parsers and integrating such parsers into the Spring IoC container. This approach is used by Spring 2.0 for transactions, utils, ... You can create your own definition as is explainde in Appendix B.

Now Summer has become. You have some interfaces to extend Spring Framework so it can adapt to your necessities.

lunes, julio 17, 2006

Offspring matters ....





As you know, in XSD files, there is an attribute to mark an element as abstract. This means that other element will extend it. Typical example of Figure, with Circle, Square, ... as children.

Ok, that's perfect. Imagine you are using XmlBeans (XML binding). You have a list of FigureType, and want to iterate over all figures, and depending its type, make one thing or another (for example drawing the figure). In this case an instanceof approach won't work because in XML Beans we are working with interfaces, not with implementations object, so for example SquareType interface doesn't extends from FigureType interface. So how can we implements the previous example?

Using two thing, one working wiht DOM repesentation instead of object representation, and next using type attribute that all extentended tags require. An example:

<figure type="SquareType" area="c*c" sides="4">

Take a look that the tag is figure but square information is present.

Now let's see the java code to treat this example:

It is as simple as we can imagine:

First we return the DOM representation

final Node node = figureType.getDomNode();

Next we gets the type attribute using DOM library.

final NamedNodeMap nodeAttMap = node.getAttributes();

final String type = nodeAttMap.getNamedItemNS("http://www.w3.org/2001/XMLSchema-instance", "type").getNodeValue();

Remember to change the first parameter of getNameItemNS if namespace of type attribute is changed.

and finally simply a hell if-then-else sequence:

if("SquareType".equals(type)) {

(SquareType) figureType.changeType(SquareType.type));

} else {
...
}

Take a look at line inside if; we must cast from FigureType to SquareType using changeType method.

And that's all, simple and easy, only tedious because of if-then-else chain.

martes, julio 11, 2006

Debianizing Java


When you go to java.sun.com to download JDK, you find a disagreeable surprise, only a RPM or a BIN file is found. What's happening if you have a Debin/Ubuntu distribution? Probably you would like to work with a DEB file, but it is not available in java sun site. A simple solution can be done.

First you download the BIN JDK file you want to install. Then using synaptic, you download fakeroot aplication and make-jpkg application. And finally, you apply the next command, fakeroot make-jpkg .bin.

After a few moments you will have a .deb file, so you can install using dpkg tool. After that, because we want to execute by default this new java virtual machine/compiler, execute update-alternatives --config java, and choose the new jdk installed.

And thats all, as simple as you can imagine.

viernes, marzo 17, 2006

Cramer Versus Cramer

Cramer vs Cramer, Java Virtual Machine vs Java Virtual Machine. This article try to give information of what is the best java virtual machine for a given problem. To do this we have choosen two benchmarks so we can give an objective opinion of which are the best virtual machine. There is a lot of benchmarks and a lot of java virtual machines, but we have focused into two virtual machines, Java HotSpot(TM) Client VM (build 1.5.0_05-b05) and IBM J9 VM (build 2.3, J2RE 1.5.0 IBM J9 2.3 Linux x86-32 j9vmxi3223-20051103 (JIT enabled), and two benchmarks, JGF Benchmark http://www.epcc.ed.ac.uk/javagrande/javag.html and Richards and DeltaBlue Benchmark http://research.sun.com/people/mario/java_benchmarking/download2.html.

I would try to explain both benchmarks, but for more information please visit its site.

JGF is composed by three big groups of benchmarks, Sequential Benchmark, Multi-Threaded Benchmark and MPJ Benchmark.

  • The sequential benchmarks, suitable for single processor execution.
  • The multi-threaded benchmarks, suitable for parallel execution on shared memory multiprocessors.
  • The MPJ benchmarks, suitable for parallel execution on distributed memory multiprocessors.

For our porpouse only the first and second benchmarks would be executed.

Sequential Benchmark also have the next subsections:

  • Arith: execution of arithmetic operations
  • Assign: variable assignment
  • Cast: casting
  • Create: creating objects and arrays
  • Loop: Loop overheads
  • Math: execution of maths library operations
  • Method: method invocation
  • Serial: Serialisation
  • Exception Exception handling
  • Serial: Read/Write Lists
  • Method: executing methods in same object or foreign object

  • Series: Fourier coefficient analysis
  • LUFact: LU Factorisation
  • SOR: Successive over-relaxation
  • HeapSort: Integer sorting
  • Crypt: IDEA encryption
  • FFT: FFT
  • Sparse: Sparse Matrix multiplication

  • Search: Alpha-beta pruned search
  • Euler: Computational Fluid Dynamics
  • MD: Molecular Dynamics simulation
  • MC: Monte Carlo simulation
  • Ray Tracer: 3D Ray Tracer

Multi-Thread Benchmark also have the next subsection:

  • ForkJoin: forking and joining threads
  • Barrier: barrier synchronisation
  • Sync: synchronized blocks and methods
  • Series: Fourier coefficient analysis
  • LUFact: LU Factorisation
  • SOR: Successive over-relaxation
  • Crypt: IDEA encryption
  • Sparse: Sparse Matrix multiplication

  • MolDyn: Molecular Dynamics simulation
  • MonteCarlo: Monte Carlo simulation
  • RayTracer: 3D Ray Tracer


At the heart of Richards is a task dispatcher (Richards.schedule). Tasks come in 4 different flavors, each represented by a class (DeviceTask, HandlerTask, IdleTask, WorkTask, all subclasses of Task). Each kind of task has an associated work function (fn). At startup (Richards.run), a particular task mix is created, and then the tasks are scheduled, each having its work function invoked. The work functions manipulate work packets and packet queues. At the end of the benchmark (in Richards.run) the number of queued and held packets is checked against the correct value to assist in verifying that the benchmark ran correctly.

For our pourpouse we have executed all sequential benchmark. And only the first group of multi-thread becnhmark. Also, we have runned Richards Benchmark.

All this benchmarks has been runned on a Pentium IV 3 GHz and 512 MB RAM, on Ubuntu Linux 5.10. The optional arguments for java command has been -Xms256m -Xmx512m because there is some tests that needs as memory as possible or a RuntimeException would be thrown.

Lets see the results of the first section of sequencial benchmark.


In this graph we can see that for mathematical operations is faster Java HotSpot than IBM. IBM Java Virtual Machine only wins in creations of objects and rounding numbers, in all other operations, like managing lists, arrays or casting is better Java HotSpot.

About section 2 in sequencial benchmark, we can see that we have the same tests but with three different results, that is because the size of data increases in every test. See JGF homepage to see how the values increases because there is no standard growth between the tests.


We can say that in all tests Java HotSpot is much faster that IBM JVM, this is because HotSpot is better in numerical problems than IBM Java Virtual Machine.


Although the differences has been minimized, Java HotSpot is faster than IBM JVM.



When the load is heavy, IBM JVM in some tests are faster than Java HotSpot, surely we can say that IBM JVM manages memory data better than Java HotSpot.

Now, let's do the same, but with the second group of benchmarks, remember that only the first group, because we are not interested in knowing how fast is a JVM executing mathematical operations in parallel, because we have already executed when I was executing sequential, what we really wants to know is which Java Virtual Machine manages the threads faster.














As we can see, with only one thread, in this case (and all future cases), IBM Virtual Machine is faster than Sun Java HotSpot. See that synchronizers in IBM is spectacular in comparision to Java HotSpot.

Note that in forkjoin a negative value is calculated. I have tried three times the same test and with one thread, always the result has been negative. I think that an overfload has been produced. Of course it is impossible a negative count of forkjoins.

















With two threads, the difference between both JVM has been reduced in three first tests, but in synchronizations, IBM still be the best ione in difference.


With 5 , 10 and 20 threads happens the same as 2 threads. IBM still being faster than Java HotSpot.















































Finally, although first tests sun VM is faster than IBM, we can say but that with a lot of threads synchronizating, IBM VM is faster than sun VM.














































In summary, in thread environment like web servers, IBM JVM seems to be better than SUN VM. On the other hand, SUN VM in standard applications offer a better throughput.

jueves, diciembre 15, 2005

... and winter arrives to woodchuck.

In this blog, I would try to justify why use Hibernate instead of plain JDBC.

What is Hibernate? Hibernate is a powerful, high performance object/relational persistence and query service. What offer Hibernate? What makes better from other solutions?

  • First of all, Hibernate is considerated a professional open-source project.
  • It implements all Object Oriented Programming features like, associations, inheritance, polymorphism, composition and collections.
  • It is transparent to JDBC connections, but doesn't hide.
  • It has its query engine, HQL, Criteria and Query by Example, as well as native SQL.
  • Hibernate decouples business objects from RDMS, thanks of dialects. You can change your RDMS without changing business database process.
  • Hibernate lets you implements quality code, because it takes you to use good practices in patterns world. So easy to develop a Generic DAO pattern.
  • Easy to use. It is so easy to implements typical operations of the data layer. For example to insert a tuple into database it is as easy as call session.save(Object obj).
  • The model objects (Value Objects or Tranfer Objects), haven't any dependency to hibernate, you musn't extend them from any hibernate class. Hibernate would use reflection and configuration files to know which classes and fields must be supported by database operations.
  • Hibernate can work with cache services and transactional services. Typically JTA and JBossCache, but you can use any other.
  • Model objects have to be configured into Hibernate Engine. For this configuration, you have three possible strategies, XML Files, JDK5.0 (EJB3) Annotations or Hibernate XDoclet, so developer has variability at his point.
  • Hibernate Validator is an annotated solution for validating model objects, you can validate fields of your object model without implementing any conditional, only using field annotations.

martes, diciembre 06, 2005

Cross-Validation VS Bootstrap

When you develop a Machine Learning Technique, you need to know how better is your solution compared with other solutions.
There are a lot of methods, but the most used are Fold-Cross Validation and Bootstrap. Both are commonly used in classifiers system. In my thesis "Rule Induction Using Ants", we find not knowing with of two techniques we would use.

In paper "A study of Cross-Validation and Bootstrap dor Accuracy Estimation and Model Selection" written by Ron Kohavi are some experiments in C4.5 and Naive-Bayesian Classifiers. The results with that algorithms and six datasets are:

  • Bootstrap has low variance, but large bias in some problems.
  • K -Fold Cross Validation with moderate values (10-20), reduce the variance but increase bias.
  • Using Stratified strategy is better in terms of variance and bias, comparated with Regular Cross Validation.
So it seems that 10-Fold-Cross Validation is the best strategy tu use, but, are any better technique rather than Cross Validation and Bootstrap? And more important, that study has used only six datasets, has anybody know any study that say in which cases are better Bootstrap and which cases are K-Fold-Cross Validation?

That's all folks, I wish this Post could help someone.

Hello Everyone

Hello, this is my first Blog. I just don't know when I could write off another, but I promise writing new entries every time I can.

This blog try to be a discussion blog of Artificial Intelligence and Computer in General, but of course I am open to talk about any theme it could be interesting.

Also I would try to publish my research results in Artificial Intelligence and concretaly Emergent Intelligence.

I wish everyone could post comments and different points of view.