Drools 6.0 Getting Started: Clock examples

Source: Internet
Author: User
Tags new set jboss

The blog about Drools 6.0 has been written for a long time. On the one hand is recently more rambling, although also learned not to click on the open link less new things, but are not willing to record down; on the other hand, drools itself is more cumbersome, even if the introduction also needs to involve a lot of internal things, more write more feel the power is insufficient. This is the third time that this article has been written from the beginning.

First of all, introduce drools. Drools is a Java implementation of the rule Engine Open Source Library, managed by the JBoss Foundation, the latest is version 6.0.1. In version 6.0, Drools introduced a new set of APIs based on the Kie concept, which was designed to simplify the cumbersome invocation and loading process of the rule engine in previous releases, tension pile.

At present, the information about Drools 6.0 is relatively few. Here are two better tutorials I've found for learning:

Http://blog.xiongzhijun.com/?cat=48 (Chinese, drools introductory tutorials that involve a lot of drools basic concepts, structures, and grammars)

Http://docs.jboss.org/drools/release/6.0.1.Final/drools-docs/html_single/index.html (English, 6.0.1 Official guide, covering all aspects of drools, suitable for advanced learning)

http://blog.csdn.net/quzishen/article/details/6163012 (Chinese, 5.x version, which can be used to control, and the explanation of DRL rules file is available for learning)

Here is an example of a clock based on Drools 6.0 that I have written.

Java code:

Src/main/java/com.ibm.clock.clock.java

Package com.ibm.clock;

public class Clock {

	private int hour;
	private int minute;
	private int second;
	
	public int Gethour () {return
		hour;
	}
	public void Sethour (int hour) {
		this.hour = hour;
	}
	public int Getminute () {return
		minute;
	}
	public void Setminute (int minute) {
		this.minute = minute;
	}
	public int Getsecond () {return
		second;
	}
	public void Setsecond (int second) {
		this.second = second;
	}
	
	@Override public
	String toString () {return
		hour + ":" + Minute + ":" + second;
	}
	
}

Src/main/java/com.ibm.clock.clocktest.java

Package com.ibm.clock;

Import org.kie.api.KieServices;
Import Org.kie.api.runtime.KieContainer;
Import org.kie.api.runtime.KieSession;

public class Clocktest {public

	static void Main (string[] args) {
		kieservices ks = KieServices.Factory.get (); 
  
   kiecontainer Kcontainer = Ks.getkieclasspathcontainer ();
    	Kiesession ksession = kcontainer.newkiesession ("Session-clock");
    	
    	Ksession.insert (New Clock ());
    	Ksession.fireallrules ();
    	Ksession.dispose ();
    	
	}

  

DRL rule file:

Src/main/resources/clock/clock.drl

Package Com.ibm.clock rule

"Run"
	salience 1
	when 
		c:clock (! hour = = 1 && second = 1))
	then
		System.out.println (c);
		Thread.Sleep (1000);
		C.setsecond (C.getsecond () + 1);
		Update (c);

"Second"
	salience 2 when
		c:clock (second = =);
	Then
		C.setminute (C.getminute () + 1);
		C.setsecond (0);
		Update (c);

"Minute"
	salience 3 when
		c:clock (minute = =) then C.sethour
	(
		c.gethour () + 1 );
		C.setminute (0);
		Update (c);
End

Drools configuration file:

Src/main/resources/meta-inf/kmodule.xml

<?xml version= "1.0" encoding= "UTF-8"?> <kmodule xmlns=
"Http://jboss.org/kie/6.0.0/kmodule" >
    <kbase name= "clock" packages= "clock" >
        <ksession name= "Session-clock"/>
    </kbase>
</kmodule>

What needs to be stated is:

1. Descriptions of the drools functional classes and Kmodule.xml configuration files in the above code can be found in the two links tutorial above;

2. The Drools project itself is a MAVEN project, so src/main/resources/meta-inf/maven/pom.properties files must be available and have been set up GroupID, Artifactid, version, otherwise a nullpointerexception exception can occur, and if you create a drools project from Eclipse, the associated properties of Maven are required to be filled in during the innovation process, otherwise the project cannot be created;

3. The properties that can be set before the DRL rule file are as follows:

No-loop: In the then clause of DRL, if a method such as INSERT, update, modify, retract is used to make a modification to an instance (Fact), the rule is triggered after the current rule has been executed so that it is executed again ; setting No-loop to True forces the rule to perform a
Boolean only once, with the default value of False

Ruleflow-group: Grouping rules based on Ruleflow
string with the above method. , no default

Lock-on-active: Rules can be repeatedly executed by other rule calls, and lock-on-active to true can force the rule to be executed only once under any condition, as a no-loop-enhanced
Boolean, default value is False

Salience: Sets the execution priority of the current rule, the smaller the number, the higher the priority
Int, the default is 0

Agenda-group: Group rules based on agenda ; The rule of the group is executed
string, default is main

Auto-focus: Used with Agenda-group, and whether the focus can be automatically acquired when a agenda group gets the focus
Boolean, default value is False

Activation-group: grouping rules without any criteria
string, no default

Dialect: Setting the language used by the rule
String, the default value is based on the package value, the range is either Java or Mvel

Data-effective: The current rule's effective time
string, no default value, and the value contains the date and time

Data-expires: Invalid time for current rule
String, no default value, value contains date and time

Duration: Set how long to delay the execution of a DRL file after it starts executing
long, no default

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.