Search code examples
spring-bootdrools

Getting "unable to resolve method using strict-mode: org.drools.core.spi.KnowledgeHelper.throw()" when throwing an exception on DRL file


I am trying drools integration with Spring Boot and would like to throw an exception if a property of a model is not satisfied.

For this case, I have a Client model and would like to check if its lastName field is NULL - if so then throw an exception.

However, I am getting an "unable to resolve method using strict-mode: org.drools.core.spi.KnowledgeHelper.throw()" during compile time. What is the correct way to try this?

I have tried to use global to update a field and it is working however, for convenience, I would just like to throw an exception with a message.

Below is my config file:

@Configuration
public class CreateClientRuleEngineConfig {

    public static final String bhfCreateClientRule = "CreateClientRule.drl";

    @Bean
    public KieContainer kieContainer() {
        KieServices kieServices = KieServices.Factory.get();

        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
        kieFileSystem.write(ResourceFactory.newClassPathResource(bhfCreateClientRule));
        KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
        kieBuilder.buildAll();
        KieModule kieModule = kieBuilder.getKieModule();

        return kieServices.newKieContainer(kieModule.getReleaseId());

    }

}

========================================= Below is my DRL file:

import com.test.rulengine.entity.Client;
import com.test.rulengine.exception.CreateClientException;

dialect  "mvel"

rule "Required fields"
    when
        clientInstance: Client( lastName == null);
    then
        throw new CreateClientException("LastName is required");
end

=========================================

Below is my Maven Dependency (note: I did not include other dependencies not related Drools/Kie):

    <properties>
        <java.version>1.8</java.version>
        <drools-version>7.0.0.Final</drools-version>
        <kie-version>7.0.0.Final</kie-version>
    </properties>

    <dependencies>

        <!-- Drools/Kie dependencies -->
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-core</artifactId>
            <version>${drools-version}</version>
        </dependency>
        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-spring</artifactId>
            <version>${kie-version}</version>
        </dependency>

=========================================

Here is the error during compile:

Unable to Analyse Expression throw new CreateClientException("LastName is required");: [Error: unable to resolve method using strict-mode: org.drools.core.spi.KnowledgeHelper.throw()] [Near : {... throw new CreateClientExceptio ....}] ^ [Line: 6, Column: 0] : [Rule name='Required fields']

I have checked this link regarding this and I really cant figure out what I am missing. Thank you in advance


Solution

  • This seem to be limitation of mvel. Use java method in then block, below is just an example. Dedicate a class with static methods and execute them in respective then blocks. Write java code in java code...

    import org.droolsassert.Client;
    import org.droolsassert.CreateClientException;
    import com.google.common.base.Preconditions;
    
    dialect  "mvel"
    
    rule "Required fields"
        when
            clientInstance: Client( lastName == null)
        then
            Preconditions.checkArgument(false, "LastName is required");
    end