最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Droolsstreamintegration-創(chuàng)新互聯(lián)

This passage discusses how to integrate a provided drools package into datastream application.

創(chuàng)新互聯(lián)2013年至今,先為洮南等服務(wù)建站,洮南等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為洮南企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

Packaging:
If a maven project is provided by customer. In this case, you need to ensure that the pom file contains the following:


    
      
        org.drools
        drools-bom
        pom
        xxx
        import
      
    
   
  
    
      org.kie
      kie-api
    
    
      org.drools
      drools-compiler
      runtime
    
    other dependencies 
  
  
    
      
        org.kie
        kie-maven-plugin
        xxx
        true
      
    
   

In addition, a file kmodule.xml must be added to src\main\resources\META-INF folder. A minimum kmodule.xml likes like the following.

  
    
        
    

The default stateless ksession is mandatory.

Rule files can be put in main/resources as normal

The command to create jar file is still mvn package as normal. However, the jar created is a bit different. Here is a screenshot
Drools stream integration

Note that there is knowledge base cache file and kmodule file in META-INF. Two rule files in main/resources are shifted out into the root folder.

What if the customer does not provide a maven project? I guess the best strategy is to create a maven project by ourselves. If source code is provided, we just import source code into the maven project, otherwise, use customer provided jar as a maven dependency?

Note that kie module is introduced only after drools 6. So I don't think this will work for drools 5 and below. Also, for drools integration in streamtau, we are using the latest version 7.2.1. So whether earlier version like 6.x is fully compatible still remains a question.

Invocation:
Load rules:
First create a KieServices singleton instance.
private final KieServices kieServices = KieServices.Factory.get();

Load the drools package into system:

 protected DroolsDataHolder doLoadDroolsModule(DroolsLoadParam droolsLoadParam) {
        DroolsParameters origParams = droolsLoadParam.getDroolsParam();
        String moduleName = origParams.getModuleName();
        try {
            InputStream is = droolsDataLoader.getDroolsModuleAsStream(droolsLoadParam);
            KieContainer curContainer = DroolsUtils.buildContainer(kieServices, is);
            return new DroolsDataHolder(curContainer);
        }
        catch (Exception ex) {
            logger.error("Error loading drools " + moduleName, ex);
        }
        return null;
    }

DroolsDataLoader is an interface that is designed to loads drools package as stream (via either file system or restful interface)
DroolsUtils is the utility class that builds a KieContainer from stream.

public static KieContainer buildContainer(KieServices kieServices, InputStream stream) throws Exception {
        Resource wrapped = kieServices.getResources().newInputStreamResource(stream);
        KieModule curModule = kieServices.getRepository().addKieModule(wrapped);
        ReleaseId releaseId = curModule.getReleaseId();
        logger.info("Release id generated for module: {}", releaseId);
        KieContainer kContainer = kieServices.newKieContainer(releaseId, DroolsUtils.class.getClassLoader());
        return kContainer;
    }

The returned DroolsDataHolder is merely a wrapper of KieContainer

public class DroolsDataHolder {
    private final KieContainer kieContainer;

    public DroolsDataHolder(KieContainer kieContainer) {
        this.kieContainer = kieContainer;
    }

    public KieContainer getKieContainer() {
        return kieContainer;
    }

    public void destroy() {
        kieContainer.dispose();
    }
}

The loaded DroolsDataHolder will be cached unless rule is changed, which triggers a reload operation

public DroolsDataHolder getOrLoadDroolsModule(DroolsLoadParam droolsLoadParam) {
        DroolsParameters origParams = droolsLoadParam.getDroolsParam();
        String moduleName = origParams.getModuleName();
        dataLock.readLock().lock();
        try {
            DroolsDataHolder curHolder = containers.get(moduleName);
            if (curHolder != null) {
                return curHolder;
            }
            dataLock.readLock().unlock();
            dataLock.writeLock().lock();
            try {
                return doUpdateDroolsModule(droolsLoadParam);
            }
            finally {
                dataLock.readLock().lock();
                dataLock.writeLock().unlock();
            }
        }
        finally {
            dataLock.readLock().unlock();
        }
    }

Invoke the drools module:
In stream environment, only stateless drools knowledge session is supported for now. The main reason is that stream is executed in a distributed environment. The session will be created on multiple JVMS, so it is virtually hard to share all the facts globally. Evaluating the rule is quite simple, it is composed of 3 steps:

  1. convert stream data to rule input pojo
    public Class getRulePojoClass(DroolsLoadParam droolsLoadParam, String inputPojoClassName) {
        DroolsParameters origParams = droolsLoadParam.getDroolsParam();
        String moduleName = origParams.getModuleName();
        DroolsDataHolder curDataHolder = this.getOrLoadDroolsModule(droolsLoadParam);
        if (curDataHolder == null) {
            throw new IllegalArgumentException("No drools module found by name: " + moduleName);
        }
        try {
            ClassLoader cl = curDataHolder.getKieContainer().getClassLoader();
            Class inputPojoClass = cl.loadClass(inputPojoClassName);
            return inputPojoClass;
        } catch (Exception e) {
            throw RtException.from(e);
        }
    }

The good thing about drools module is that it provides a self contained class loading environment. So third party jar dependencies are unlikely to cause conflict with the outside runtime environment. However, when we build an input event to drools engine, we need to use the KieContainer's class loader to find the input event class referenced in rule.

  1. build a stateless kie session and invoke the rule

    public List evaluate(DroolsLoadParam droolsLoadParam, List facts) {
        if (logger.isDebugEnabled()) {
            logger.debug("Start evaluating drools, input is: {}, module name is: {}", Arrays.asList(facts),
                    droolsLoadParam.getDroolsParam().getModuleName());
        }
        DroolsParameters origParams = droolsLoadParam.getDroolsParam();
        String moduleName = origParams.getModuleName();
        DroolsDataHolder curDataHolder = this.getOrLoadDroolsModule(droolsLoadParam);
        if (curDataHolder == null) {
            throw new IllegalArgumentException("No drools module found by name: " + moduleName);
        }
        StatelessKieSession curSession = curDataHolder.getKieContainer().newStatelessKieSession();
        curSession.execute(facts);
        return facts;
    }
    
  2. convert rule evaluation result back to stream data
  3. Under the hood:
    Drools class relations
    Drools stream integration

    Drools package loading
    Drools stream integration

    Things to note:
    Drools package can be large and the current approach caches all loaded drools package in memory. The loading time and memory consumption might be a bottleneck of scalability. A better approach will be building a standalone rule server, where it manages rules and exposes a rest api to stream application.

    Find out input metadata for rule: it is possible to find out java class of each rule variable. This is useful as a hint to map stream data to rule input.

    public static Map> getRuleInputMeta(KieBase kieBase,
                String rulePkgName, String ruleName) {
            RuleImpl r = (RuleImpl)kieBase.getRule(rulePkgName, ruleName);
            List elements = r.getLhs().getChildren();
            Pattern curPattern = null;
            String curId = null;
            ObjectType curObjType = null;
            Map> result = new HashMap>();
            for (RuleConditionElement nextElem : elements) {
                if (nextElem instanceof Pattern) {
                    curPattern = (Pattern)nextElem;
                    curObjType = curPattern.getObjectType();
                    curId = curPattern.getDeclaration().getIdentifier();
                    result.put(curId, curObjType.getValueType().getClassType());
                }
            }
            return result;
        }

    Maven shade plugin and drools jar:
    To use the drools java api, multiple jars need to be included as maven dependency.

    However, the special thing about drools jars is that each one contains a file kie.conf (Eg. drools-core.jar, kie-internal.jar). The default behavior of maven shade plugin is that kie.conf will overwrite each other and causes a runtime error when deploying the shaded jar to flink. Mitigation to this problem is to configure maven shadow plugin parameters properly so that the content of each kie.conf will be appended to the combined file instead of overwritten.

    
            
                    
                            org.apache.maven.plugins
                            maven-shade-plugin
                            
                                    
                                            package
                                            
                                                    shade
                                            
                                            
                                            
                                                            
                                                                    META-INF/kie.conf
                                                            
                                                    
                                            
                                    
                            
                    
            
    

    另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。


    當(dāng)前文章:Droolsstreamintegration-創(chuàng)新互聯(lián)
    URL鏈接:http://fisionsoft.com.cn/article/hghje.html