Äú¿ÉÒÔ¾èÖú£¬Ö§³ÖÎÒÃǵĹ«ÒæÊÂÒµ¡£

1Ôª 10Ôª 50Ôª





ÈÏÖ¤Â룺  ÑéÖ¤Âë,¿´²»Çå³þ?Çëµã»÷Ë¢ÐÂÑéÖ¤Âë ±ØÌî



  ÇóÖª ÎÄÕ ÎÄ¿â Lib ÊÓÆµ iPerson ¿Î³Ì ÈÏÖ¤ ×Éѯ ¹¤¾ß ½²×ù Model Center   Code  
»áÔ±   
   
 
     
   
 ¶©ÔÄ
  ¾èÖú
JAVA¹æÔòÒýÇæ[Drools]--ʵÀýƪ
 
  5556  次浏览      29
 2018-3-27 
 
±à¼­ÍƼö:
±¾ÎÄÀ´×ÔÓÚ°¢ÀïÔÆ£¬±¾ÎÄͨ¹ýÒ»¸öÀý×Ó°ÑÏîÄ¿µÄ¿ª·¢¹æÔò²¿·Ö¶ÀÁ¢ÌáÈ¡³öÀ´£¬ÌåÏÖÁËDroolsµÄ¼ÛÖµ¾ÍµÃµ½¡£

Ëæ×Å»¥ÁªÍøÓ¦ÓõķÉËÙ·¢Õ¹£¬¸÷ÖÖÒµÎñÐèÇóÒ²Ó¦Ô˶øÉú£¬¶ÔÓÚ²»¶Ï±ä¸üºÍ¸ïеÄÒµÎñ¹æÔò¶øÑÔ£¬ÏîÄ¿µÄ¿ª·¢¾ÍºÜÓбØÒª°Ñ¹æÔò²¿·Ö¶ÀÁ¢ÌáÈ¡³öÀ´£¬´ËʱDroolsµÄ¼ÛÖµ¾ÍµÃµ½ÁËÌåÏÖ¡£·Ï»°²»¶à˵£¬¿´Ò»¸ö¼òµ¥µÄÀý×Ó¡£

ÕâÀï¾ÙÒ»¸öÊÖ»ú»°·ÑµÄÀý×Ó¡£

Ò»¡¢¶¨Ò广Ôò£º

Ê×ÏÈÒª·ÖÎö×Ô¼ºµÄÒµÎñÂß¼­£¬¸ù¾ÝÒµÎñ¶øÖƶ¨³ö²»Í¬µÄ¹æÔò£¬ÕâÀïÎÒÃǼÙÉèÓÐ3¸ö¹æÔò¡£

1¡¢¶ÔÓÚпª»§µÄÊÖ»úÓû§ËÍ20Ôª»°·Ñ¡£

2¡¢ÔÚ2014Äê10Ôµ½12ÆÚ¼ä³äÖµµÄÓû§£¬²»ÂÛ½ð¶î¶àÉÙ³äÖµÂú3´Î¾ÍÔùËÍ5Ôª»°·Ñ¡£

3¡¢µ±Ô³äÖµ½ð¶î´ïµ½100ÒÔÉÏ£¬Ã¿´ïµ½Ò»´ÎÔùËÍ»°·Ñ10Ôª¡£

¶þ¡¢´´½¨Fact¶ÔÏó£º

·ÖÎöÕâЩ¹æÔò£¬²¢°ÑËûÃdzéÏó³ÉÒ»¸öFact¶ÔÏó¡£

package com.core.drools;

import java.util.UUID;

/**
* EntityRule-Model
* @author Candy
*
*/
public class EntityRule {

private String username;

/** Whether for new account. */
private boolean account;

/** The number of add. */
private int addtime;

/** The sum of the current account. */
private double currentmoney;

/** The totail amount added. */
private double totailaddmoney;

/**
* Record the serial number of the operation.
* @param username
* @param currentmoney
*/
public void getSerialnumber(String username,double currentmoney){
System.out.println("Account£º"+username+" Balance£º£¤"+currentmoney);
System.out.println("Serial Number£º"+UUID.randomUUID().toString());
}

Èý¡¢¶¨Ò广ÔòÒýÇæ£º

ÒµÎñºÍ¹æÔò¶¼ÕûÀíÇå³þÁËÎÒÃǾͿÉÒÔ¿ªÊ¼¹æÔòÒýÇæµÄºËÐIJ¿·ÖÀ²£¬ÕâÀïÎÒ¶¨ÒåµÄÊǽӿںÍʵÏÖÀà¡£

package com.core.drools;

/**
* RuleEngine-Interface
* @author Candy
*
*/
public interface RuleEngine {

/**
* Initializes the rules engine.
*/
public void init();

/**
* Refresh the rules engine.
*/
public void refresh();

/**
* Execute the rules engine.
*/
public void execute(final EntityRule entityRule);

}

package com.core.drools;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import org.drools.RuleBase;
import org.drools.StatefulSession;
import org.drools.compiler.DroolsParserException;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;
import org.drools.spi.Activation;

/**
* RuleEngine-Implements
* @author Candy
*
*/
public class RuleEngineImpl implements

RuleEngine{

private RuleBase ruleBase;

/*
* (non-Javadoc)
* @see com.core.drools.RuleEngine#init()
*/
@Override
public void init() {
/** Sets the system time format. */
System.setProperty("drools.dateformat",

"yyyy-MM-dd HH:mm:ss");
/** Get the base factory. */
ruleBase =SingleRuleFactory.getRuleBase();
try {
/** Get the rule files has bean read. */
PackageBuilder backageBuilder = getPack

ageBuilderFile();
/** Add the package to the 'RuleBase'. */
ruleBase.addPackages(backageBuilder.

getPackages());
} catch (DroolsParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

/*
* (non-Javadoc)
* @see com.core.drools.RuleEngine#refresh()
*/
@Override
public void refresh() {
ruleBase = SingleRuleFactory.getRuleBase();
Package[] packages = ruleBase.getPackages();
for(Package items :packages){
ruleBase.removePackage(items.getName());
}
init();
}

/*
* (non-Javadoc)
* @see com.core.drools.RuleEngine#execute

(com.core.drools.EntityRule)
*/
@Override
public void execute(final EntityRule entityRule) {
if(null == ruleBase.getPackages() || 0 == ruleBase.getPackages().length) {
return;
}

StatefulSession statefulSession =

ruleBase.newStatefulSession();
statefulSession.insert(entityRule);
statefulSession.fireAllRules(new org.

drools.spi.AgendaFilter() {
public boolean accept(Activation activation) {
return !activation.getRule().getName().

contains("_test");
}
});
statefulSession.dispose();
}

/**
* Read the rule files.
* @return
* @throws Exception
*/
private PackageBuilder getPackageBuild

erFile()throws Exception {
/** Get the rule files. */
List<String> drlFilePath = getRuleFile();
/** Sets the file to 'readers'. */
List<Reader> readers = loadRuleFile(drlFilePath);
/** To create the 'backageBuilder'. */
PackageBuilder backageBuilder = new PackageBuilder();
for (Reader r : readers) {
backageBuilder.addPackageFromDrl(r);
}
/** Check if the script has a problem. */
if(backageBuilder.hasErrors()) {
throw new Exception(backageBuilder.

getErrors().toString());
}
return backageBuilder;
}

/**
* Load the script files.
* @param drlFilePath
* @return
* @throws FileNotFoundException
*/
private List<Reader> loadRuleFile(List

<String> drlFilePath)
throws FileNotFoundException {
if (null == drlFilePath || 0 == drlFilePath.size()) {
return null;
}
List<Reader> readers = new ArrayList<Reader>();
for (String ruleFilePath : drlFilePath) {
readers.add(new FileReader(new File(ruleFilePath)));
}
return readers;
}

/**
* Get the rule files.
* @return
*/
private List<String> getRuleFile(){
List<String> drlFilePath = new ArrayList<String>();
String path="D:/utils/my/DroolsProject/src/

com/core/drools/drools_rule.drl";
drlFilePath.add(path);
return drlFilePath;
}

}

ÕâÀﶨÒåÒ»¸öµ¥ÀýµÄRuleBase¹¤³§Àà¡£

package com.core.drools;

import org.drools.RuleBase;
import org.drools.RuleBaseFactory;

/**
* To create a singleton factory.
* @author Candy
*
*/
public class SingleRuleFactory {

private static RuleBase ruleBase;

/**
* Get the base factory.
* @return
*/
public static RuleBase getRuleBase(){
return null != ruleBase ? ruleBase : RuleBaseFactory.newRuleBase();
}

}

ËÄ¡¢±àд¹æÔòÎļþ£º

¹æÔòÎļþ¿ÉÒÔ¸ù¾Ý×Ô¼ºµÄÒµÎñÐèÇó¶¨Òå¶à¸öÎļþ£¬ÕâÀïÎÒÖ»¶¨ÒåÁËÒ»¸ö¡£

package com.core.drools

import com.core.drools.EntityRule;

rule accountEntity
//One
salience 100
lock-on-active true
when
$entityRule : EntityRule(account == true)
then
System.out.println("The new account:Present

£¤20.0");
$entityRule.setCurrentmoney($entityRule.

getCurrentmoney()+20);
$entityRule.getSerialnumber($entityRule.

getUsername(),$entityRule.getCurrentmoney());
System.out.println
end

rule billEntity
//two
salience 99
lock-on-active true
date-effective "2014-010-01 00:00:00"
date-expires "2014-012-31 23:59:59"
when
$entityRule : EntityRule(addtime >= 3)
then
System.out.println("Prepaid phone number

reach "+$entityRule.getAddtime()
+" times:Present £¤"+$entityRule.getAddtime()/3*5);
$entityRule.setCurrentmoney($entityRule.

getCurrentmoney()+$entityRule.getAddtime()/3*5);
$entityRule.getSerialnumber($entityRule.

getUsername(),$entityRule.getCurrentmoney());
System.out.println
end

rule addMoney
//Three
salience 98
lock-on-active true
when
$entityRule : EntityRule(totailaddmoney >= 100)
then
System.out.println("The account for

the month top-up totail amount is "
+$entityRule.getTotailaddmoney()+":Present £¤"+(int)$entityRule.getTotailaddmoney()/100*10);
$entityRule.setCurrentmoney($entityRule.getCurrentmoney()
+(int)$entityRule.getTotailaddmoney()/100 * 10);
$entityRule.getSerialnumber($entityRule.

getUsername(),$entityRule.getCurrentmoney());
end

Îå¡¢²âÊÔÒýÇæ£º

package com.test;

import java.io.IOException;
import com.core.drools.EntityRule;
import com.core.drools.RuleEngine;
import com.core.drools.RuleEngineImpl;

/**
* Test Drools
* @author Candy
*
*/
public class DroolsTest {

public static void main(String[] args) throws IOException {
RuleEngine engineImpl =new RuleEngineImpl();
engineImpl.init();
final EntityRule entityRule= new EntityRule();
entityRule.setCurrentmoney(350d);
entityRule.setUsername("Candy");
entityRule.setAccount(true);
entityRule.setTotailaddmoney(350d);
entityRule.setAddtime(7);
engineImpl.execute(entityRule);
}

}

Áù¡¢²âÊÔ½á¹û£º

The new account:Present £¤20.0

Account£ºCandy Balance£º£¤370.0

Serial Number£º0fd98593-caa2-444d-a4ff-b4001cfb3260

------------------------------------------------------------------------------

Prepaid phone number reach 7 times:Present £¤10

Account£ºCandy Balance£º£¤380.0

Serial Number£ºa118b211-c28e-4035-aa60-2f417f62b2f3

------------------------------------------------------------------------------

The account for the month top-up totail amount is 350.0: Present £¤30

Account£ºCandy Balance£º£¤410.0

Serial Number£º2bfc02c2-267f-47a2-9cda-5a4196e2b7cf

   
5556 ´Îä¯ÀÀ       29
Ïà¹ØÎÄÕÂ

Java΢·þÎñÐÂÉú´úÖ®Nacos
ÉîÈëÀí½âJavaÖеÄÈÝÆ÷
JavaÈÝÆ÷Ïê½â
Java´úÂëÖÊÁ¿¼ì²é¹¤¾ß¼°Ê¹Óð¸Àý
Ïà¹ØÎĵµ

JavaÐÔÄÜÓÅ»¯
Spring¿ò¼Ü
SSM¿ò¼Ü¼òµ¥¼òÉÜ
´ÓÁ㿪ʼѧjava±à³Ì¾­µä
Ïà¹Ø¿Î³Ì

¸ßÐÔÄÜJava±à³ÌÓëϵͳÐÔÄÜÓÅ»¯
JavaEE¼Ü¹¹¡¢ Éè¼ÆÄ£Ê½¼°ÐÔÄܵ÷ÓÅ
Java±à³Ì»ù´¡µ½Ó¦Óÿª·¢
JAVAÐéÄâ»úÔ­ÀíÆÊÎö