Thursday, 28 February 2008

Transforming Pojo / Web / Enterprise Services into Semantic Services / Agents using jSemanticService

Let's suppose that you have an existing service (pojo in this case) in a Spring context, (or that you need to write a new service) that needs to use semantics.

How would you do that? Well, in just three steps:

First Step

Writing a SemanticServiceFactory bean (and it's collaborators if needed) in the Spring Context file.

Second Step.

Either inject a reference to the SemanticServiceFactory directly in the service or extend the SemanticServiceSupport abstract class.

public class EvenService {

private SemanticSessionFactory semanticSessionFactory = null;

public Collection check(Collection numbers) {
SemanticSession session = semanticSessionFactory.getInstance();

Collection results = session.execute(numbers);
session.dispose();
session = null;
return results;
}


public void semanticSessionFactory(SemanticSessionFactory semanticSessionFactory){
this.semanticSessionFactory = semanticSessionFactory:
}

}


Now a method that uses the new added semantics functionality can be written. For that matter there is a method in the base class: getInstance(). This method returns a Semantic Session instance that is bound to the current thread. If we dispose the session then we'll get a new one. If not, the same session will be reused.

Third Step

Inject the Semantic Session Factory in the service bean.

And that's all, now the Semantic Service is ready to roll!.

Read the complete article in:

http://code.google.com/p/jservicerules/wiki/TransformServicesIntoSemantics

No comments: