My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
IntroductionGuide  

Introduction

I'm sure that you have read or heard in many occasions about business rules, BRE's (business rules engines) or semantics because in the last years have gained a lot of momentum due to it's relationship with SOA and BPM's. Still it is an underrated tool maybe because the following reasons:

1.The BRE's never been easy to use, not to mention the configuration. That's true if we are talking about the open source BRE's, even though that has improved in the case of Drools.

2. There are not many open source BRE's out there. True again, but the Drools option is enough for most of the cases. Jess is another good option, is not open source but is cheap though.

3.The BRE's usually are isolated from the programming / execution environments leaving that task to the developer. That's true, usually you are left on your own.

4. Developers are not used to functional programming neither to semantics. Well, of course you have learn how to write rules/semantics, but almost everything can be written in java and it's a lot easier than learning SQL for instance. Rules sometimes are nothing more that complex if-elses.

With this framework I've tried to overcome some of that problems making easier to user a BRE:

In which cases you may use this framework or just a BRE for that matter?

# If you need to develop smart agents or services (for example a service that will recommend the best shows of the day).

# If you have a complex business model and need to write a semantic model to process it.

# If you have some business rules that would change over the time. # If you have some business rules that you'd like to isolate from the code. # If you have business rules that you'd need to edit in a simple way using a scripting language (so you don't need to compile them).

# If you need to process or recognize different types of events or objects.

Framework Features

# Write o transform your services into Semantic Services or smart agents in no time.

# Drools 4 Implementation provided.

# Creates a layer of Semantic Services for sSOA (Semantic SOA).

# Fast and easy configuration (Spring/Annotations /Code).

# Hides the complexity behind a BRE allowing developers to concentrate in the development of the semantics and rules.

# Agnostic and focused API with the most common features that you'd need using rules/ semantics.

# New features that helps the BRE to interact with the environment (internal/external), for example invoking external services by rules (that are located in a external context (as a Spring context) or using a Knowledge Database.

# Support package that provides integration out of the box for Spring, JPA, Mule etc..

Example using annotations.

Ok, after all that theory let's see an actual example of how to write a Semantic Service (a service that uses semantics or rules, that is).

There is a complete set of annotations provided within the framework (you will need jdk 5+, though) so you can set up your service on the fly:

@SemanticService(rulesFile="org/jsemantic/support/examples/energy/tariff.drl")

@SpringExternalContext(contextFile="org/jsemantic/support/examples/energy/services.xml")

public class InvoiceService extends AbstractSemanticService {
	
	public Invoice createInvoice(Customer customer, MeterReading reading) {

		SemanticSession session = super.getInstance();
		session.execute(customer.getTariff());

		Tariff tariff = (Tariff) session.getContext().getResult("tariff");

		session.dispose();
	}
}

This service creates electric energy invoices taking as parameters the customer and the reading. The different tariffs are stored as rules, so they can be easily updated, removed or added.

The AbstractSemanticService is a support class that makes easier the process of writing or transforming your services but is not compulsory.

The @SemanticService annotation indicates that this is a Semantic Service and that the rules file associated is tarilff.drl This rules file (in collaboration with other annotations) will create the Semantic Session Factory:, that it's the factory that instantiates the Semantic Session objects.

The Semantic Session object is central within the jSemanticService framework as it is the responsible of evaluating the rules/semantics and returning the results (among many other functions). This object is bounded to the calling thread and will be reused until it is disposed.

Therefore you will need a Semantic Session to evaluate the rules and at least one rules file with one rule:

rule "2.0.1 tariff"

	when
		tariff_type:String()
		eval (tariff_type.equals("2.0.1")) // java, not the ony way to do this.
	then
		BigDecimal kwh = (BigDecimal)ctx.getSessionVariable("kwh");
		BigDecimal ppower = (BigDecimal)ctx.getSessionVariable("ppower");
		Integer months = (Integer)ctx.getSessionVariable("months");
		
		Tariff tariff = new Tariff();
		tariff.setKwh(kwh);
		tariff.setPpower(ppower);
		tariff.setPowerMode("1");
		tariff.setPower(new BigDecimal(5.50));
		tariff.setMonths(months);
		ctx.addResult("tariff", tariff);
end

I've used java code to recognize the tariff (for that matter you can use the function eval()), but that's not the only way to do it. When the type of tariff is found, the object Tariff is created and returned to the service.

The @SpringExternalContext annotation specifies a Spring context that can be used by the Semantic Session (for example, a set of external services). This context is created outside of the Semantic Context.

global org.jsemantic.core.context.SemanticContext ctx;

PrinterService service = (PrinterService)ctx.getExternalContext().getObject("printerService");
service.execute();

Sign in to add a comment
Powered by Google Project Hosting