Question

I was trying to use Java Rules API for the first time and got the following exception:

No RuleServiceProvider registered against URI: http://drools.org/), root cause(null)

This exception came for the following code:

RuleServiceProvider ruleServiceProvider =
      RuleServiceProviderManager.getRuleServiceProvider("http://drools.org/");

I want to use Java rules API for DRL and DSL files in Drools. Can anybody help ?

The code that I have used is the following :-

try {

        Class.forName("org.drools.jsr94.rules.RuleServiceProviderImpl");

        RuleServiceProvider ruleServiceProvider = RuleServiceProviderManager.getRuleServiceProvider("http://drools.org/");

        RuleAdministrator ruleAdministrator = ruleServiceProvider.getRuleAdministrator();

        LocalRuleExecutionSetProvider ruleExecutionSetProvider =
        ruleAdministrator.getLocalRuleExecutionSetProvider(null);

        Reader drlReader = new FileReader(new File("CustomRules.drl"));

        RuleExecutionSet ruleExecutionSet = ruleExecutionSetProvider.createRuleExecutionSet(drlReader, null);

        String uri = ruleExecutionSet.getName();
        ruleAdministrator.registerRuleExecutionSet(uri, ruleExecutionSet, null);

        RuleRuntime ruleRuntime = ruleServiceProvider.getRuleRuntime();

        StatefulRuleSession session =
                (StatefulRuleSession)ruleRuntime.createRuleSession( uri,
                null,
                RuleRuntime.STATEFUL_SESSION_TYPE );
                Applicant a = new Applicant();
                a.setAge(16);
                a.setValid(true);
                System.out.println(a.isValid());
                session.addObject(a);
                session.executeRules();
                System.out.println(a.isValid());

    } catch (Exception e) {
        e.printStackTrace();
    }

No correct solution

OTHER TIPS

Have you register the service provider ? if not then use below line

Class.forName("org.drools.jsr94.rules.RuleServiceProviderImpl");

First register rule service provider as below and then get it.

Put below code after Class.forName(...)

// Register Service provider class

RuleServiceProviderManager.registerRuleServiceProvider(
    "http://drools.org",
    "org.drools.jsr94.rules.RuleServiceProviderImpl");

// Get rule service provider

RuleServiceProvider ruleServiceProvider = RuleServiceProviderManager .getRuleServiceProvider("http://drools.org");

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top