0

我正在使用 6.2.0 最终版本。我已将 droolengine 创建为独立应用程序。它按预期工作正常,并且正在触发 DRL 文件中存在的所有规则。但是相同的应用程序已与我的产品集成,但此处没有触发 DRL 文件中存在的规则。

现在我在这里也发布了我的 DRL 文件

package com.myorg.rules;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.password.droolsengine.ValidatePasswordWorkflowData;
global java.lang.Integer noOfIdentifierstobecosecutive;
global java.lang.Integer noOfSameIdentifierToBeRepeated;
global java.lang.String NoOfIdentifierstobecosecutiveErrorCode;
global java.lang.String noOfSameIdentifierToBeRepeatedErrorCode;
global java.lang.String NoOfIdentifierstobecosecutiveCustomeMsg;
global java.lang.String noOfSameIdentifierToBeRepeatedCustomMsg;
global java.lang.Boolean ValidPassword;

rule "CONSECUTIVEANDSAMEIDENTIFIER"

    when
        validatePasswordWorkflowData : ValidatePasswordWorkflowData(passwordFromEB:password)
    then
         noOfIdentifierstobecosecutive=4;
         noOfSameIdentifierToBeRepeated=4;

         System.out.println("starting of then ");



  validatePasswordWorkflowData.setValidatePasswordWorkflowData(ConsecutiveAndSameIdentifierRepetationValidation(passwordFromEB,noOfIdentifierstobecosecutive,noOfSameIdentifierToBeRepeated));
     System.out.println("ending of then ");  
 end

function ValidatePasswordWorkflowData ConsecutiveAndSameIdentifierRepetationValidation(String password,
            int noOfIdentifierstobecosecutive, int noOfSameIdentifierToBeRepeated)

    {   

     System.out.println("starting of ConsecutiveAndSameIdentifierRepetationValidation method ");
     boolean ValidPassword=true;
           String NoOfIdentifierstobecosecutiveErrorCode="LV00302";
           String noOfSameIdentifierToBeRepeatedErrorCode="LV00303";
           String NoOfIdentifierstobecosecutiveCustomeMsg="password must not contain , at a time   consecutive identifiers   more than ";
           String noOfSameIdentifierToBeRepeatedCustomMsg="and no of same identifier repetition should no grater than";


    ValidatePasswordWorkflowData validatePassword=new ValidatePasswordWorkflowData();

        /*
         * 
   LV00301=Error during Password Validation. Reason {0}
   LV00302=Error during Password Validation. Reason password must not contain , at a time   consecutive identifiers more than 
   LV00303=Error during Password Validation. Reason password must not contain,at a time,  no of same identifier repetition should no greter than   
           **/

        StringBuilder conscutiveIndentifierRegex = new StringBuilder();
        StringBuilder sameIndentifierRegex = new StringBuilder();

        Pattern ConsecutiveIdentifierRegex = Pattern.compile(
                buildRegex(conscutiveIndentifierRegex, noOfIdentifierstobecosecutive), Pattern.CASE_INSENSITIVE);

        Pattern sameidentifierRepeatRegex = Pattern.compile(
                buildRegexRepeatedString(sameIndentifierRegex, noOfSameIdentifierToBeRepeated),
                Pattern.CASE_INSENSITIVE);

        conscutiveIndentifierRegex.setLength(0);
        sameIndentifierRegex.setLength(0);

        Matcher ConsecutiveIdentifierRegexMatcher = ConsecutiveIdentifierRegex.matcher(password);

        Matcher sameidentifierRepeatRegexMatcher = sameidentifierRepeatRegex.matcher(password);

        if(ConsecutiveIdentifierRegexMatcher.find())
            {   if(noOfIdentifierstobecosecutive==3)
                validatePassword.setErrorCode(NoOfIdentifierstobecosecutiveErrorCode);
            else
                validatePassword.setCustomErrorMessage(NoOfIdentifierstobecosecutiveCustomeMsg+" "+noOfIdentifierstobecosecutive +noOfSameIdentifierToBeRepeatedCustomMsg+" "+noOfSameIdentifierToBeRepeated);
            }

             if( (sameidentifierRepeatRegexMatcher.find()))
            {

                if(noOfSameIdentifierToBeRepeated==3)

                validatePassword.setErrorCode(noOfSameIdentifierToBeRepeatedErrorCode);
                else if(noOfSameIdentifierToBeRepeated!=3)
                    validatePassword.setCustomErrorMessage(NoOfIdentifierstobecosecutiveCustomeMsg+" "+noOfIdentifierstobecosecutive +""+noOfSameIdentifierToBeRepeatedCustomMsg+" "+noOfSameIdentifierToBeRepeated);
        }
            else if(validatePassword.getErrorCode()==null && validatePassword.getCustomErrorMessage()==null)
                   validatePassword.setValidPassword(ValidPassword);

        System.out.println("ending of ConsecutiveAndSameIdentifierRepetationValidation method ");
return validatePassword;
    }

    function String buildRegex(StringBuilder sb, int seqStart) {
        for (int i = seqStart; i <= seqStart; i++) {
            buildRegexCharGroup(sb, i, '0', '9');
            // buildRegexCharGroup(sb, i, 'A', 'Z');
            buildRegexCharGroup(sb, i, 'a', 'z');
            // buildRegexRepeatedString(sb, i);
        }
        return sb.toString();
    }

    function void buildRegexCharGroup(StringBuilder sb, int seqLength, char start, char end) {
        for (char c = start; c <= end - seqLength + 1; c++) {
            char ch = c;
            if (sb.length() > 0) {
                sb.append('|');
            }
            for (int i = 0; i < seqLength; i++) {
                sb.append(ch++);
            }
        }
    }

    function String buildRegexRepeatedString(StringBuilder sb, int seqLength) {
        // sb.append('|');
        sb.append("([a-z\\d])");
        for (int i = 1; i < seqLength; i++) {
            sb.append("\\1");
        }
        return sb.toString();
    }

我的Java类正在使用下面的DRL文件======================================== ==================

package com.password.droolsengine;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.kie.api.KieBase;
import org.kie.api.KieBaseConfiguration;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.Message;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

public class PasswordValidatorEngine {

    public static final void main(String[] args) {

        ValidatePasswordWorkflowData passwordWorkflowData = new ValidatePasswordWorkflowData();
        passwordWorkflowData.setPassword("1234551157813151");
        String st = executeStatefull(passwordWorkflowData, "src/main/resources/Myorg/drl/Myorg.drl");


    }

    public static String executeStatefull(ValidatePasswordWorkflowData passwordWorkflowData, String drlFilePath) {

        String error = "";

        try {
            KieSession kieSession = null;
            String content = readFile(new File(drlFilePath));
            KieServices ks = KieServices.Factory.get();
            String inMemoryDrlFileName = "src/main/resources/stateFuSesramamasionRule.drl";
            KieFileSystem kfs = ks.newKieFileSystem();

            kfs.write(inMemoryDrlFileName,
                    ks.getResources().newReaderResource(new FileReader(drlFilePath)).setResourceType(ResourceType.DRL));

            KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
            if (kieBuilder.getResults().hasMessages(Message.Level.ERROR)) {
                System.out.println(kieBuilder.getResults().toString());
            }
            KieContainer kContainer = ks.newKieContainer(kieBuilder.getKieModule().getReleaseId());
            KieBaseConfiguration kbconf = ks.newKieBaseConfiguration();
            KieBase kbase = kContainer.newKieBase(kbconf);
            kieSession = kbase.newKieSession();

            kieSession.insert(passwordWorkflowData);
            kieSession.fireAllRules();

            ValidatePasswordWorkflowData validatePasswordWorkflowData = passwordWorkflowData
                    .getValidatePasswordWorkflowData();

            System.out.println("custom_msg " + validatePasswordWorkflowData.getCustomErrorMessage());
            System.out.println("error_code" + validatePasswordWorkflowData.getErrorCode());
            System.out.println(validatePasswordWorkflowData.isValidPassword());

            kieSession.dispose();
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return error;
    }

    private static String readFile(File file) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line = null;
        StringBuilder stringBuilder = new StringBuilder();
        String ls = System.getProperty("line.separator");

        try {
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
                stringBuilder.append(ls);
            }

            return stringBuilder.toString();
        } finally {
            reader.close();
        }
    }

}

ValidatePasswordWorkflowData 是 POJO 类,我们通过它向 DRL 文件提供输入,并且在处理 DRL 文件之后,将设置这个 POJO 的结果。ValidatePasswordWorkflowData POJO 低于

package com.password.droolsengine;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidatePasswordWorkflowData {

    private String password = null;

    private boolean isValidPassword = false;

    private String errorCode = null;



    private String customErrorMessage = null;

    private ValidatePasswordWorkflowData validatePasswordWorkflowData = null;

    public ValidatePasswordWorkflowData getValidatePasswordWorkflowData() {
        System.out.println("getter method");
        return validatePasswordWorkflowData;

    }

    public void setValidatePasswordWorkflowData(ValidatePasswordWorkflowData passwordWorkflowData) {
        validatePasswordWorkflowData = passwordWorkflowData;
        System.out.println("setter method");
    }

    public String getCustomErrorMessage() {
        return customErrorMessage;
    }

    public void setCustomErrorMessage(String customErrorMessage) {
        this.customErrorMessage = customErrorMessage;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isValidPassword() {
        return isValidPassword;
    }

    public void setValidPassword(boolean isValidPassword) {
        this.isValidPassword = isValidPassword;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }



}

在我在路径中添加的罐子下面

    drools-compiler-6.2.0.Final.jar
kie-internal-6.2.0.Final.jar
antlr-runtime-3.5.jar
ecj-4.3.1.jar
mvel2-2.2.4.Final.jar
protobuf-java-2.5.0.jar
drools-core-6.2.0.Final.jar
drools-decisiontables-6.2.0.Final.jar
drools-templates-6.2.0.Final.jar
poi-ooxml-3.10.1.jar
poi-ooxml-schemas-3.10.1.jar
dom4j-1.6.1.jar
poi-3.10.1.jar
xmlbeans-2.6.0.jar
stax-api-1.0.1.jar
kie-api-6.2.0.Final.jar
xstream-1.4.10.jar
hamcrest-core-1.3.jar
xpp3_min-1.1.4c.jar
xmlpull-1.1.3.1.jar

在 Pom.xml 下面我用来创建应用程序..我在将我的应用程序与我的产品捆绑时使用的相同依赖项

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.peogrammertech</groupId>
    <artifactId>droolsCache</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-compiler</artifactId>
            <version>6.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-core</artifactId>
            <version>6.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-decisiontables</artifactId>
            <version>6.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>2.6.0</version>
        </dependency>

        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-api</artifactId>
            <version>6.2.0.Final</version>
        </dependency>
    </dependencies>

</project>
4

0 回答 0