StandAlone DroolsApplication работает нормально, но то же приложение не работает, когда в комплекте с моим продуктом - PullRequest
0 голосов
/ 12 ноября 2018

Я ИСПОЛЬЗУЮ 6.2.0 FINAL. Я создал 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 IS НИЖЕ

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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...