No rules are fired in helloworld using Spring rest controller!

804 views
Skip to first unread message

Wei Xiong

unread,
Apr 24, 2017, 2:46:15 PM4/24/17
to Drools Usage
Hi All,


I am using Drools 6.5.0 with Spring boot. I have a very weird problem. I can successfully execute the helloworld rule in test environment but not through Spring rest controller. Both are using the same function from a service. Could you please help?


I have the following rules from Drools examples:


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

rule "Hello World"

    dialect "mvel"

    when

        m : Message( status == Message.HELLO, message : message )

    then

        System.out.println( message );

//        modify ( m ) { setMessage( "Goodbyte cruel world" ),

//                       setStatus( Message.GOODBYE ) };

    modify ( m ) { message = "Goodbye cruel world",

                   status = Message.GOODBYE };

end


rule "Good Bye"

    dialect "java"

    when

        Message( status == Message.GOODBYE, message : message )

    then

        System.out.println( message );

end


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


And I have the following as a service

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

@Service

@Transactional

public class RuleService {


private final Logger log = LoggerFactory.getLogger(RuleService.class);


    @Autowired

    private KieContainer kieContainer;

    

RuleService() {

}


public String ruleTest() {

log.debug("in ruleTest()");


KieSession ksession = this.kieContainer.newKieSession("PriceRulesKS");


ksession.setGlobal("list", new ArrayList<Object>());


final Message message = new Message();

message.setMessage("Hello World");

message.setStatus(Message.HELLO);

ksession.insert(message);


// and fire the rules

int ruleFiredCount = ksession.fireAllRules();

System.out.println("Rules checked: " + ruleFiredCount);

log.debug("Rules checked: " + ruleFiredCount);


ksession.dispose();

return message.getMessage();

}


}



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


And I have the following config


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


@Configuration

public class RuleConfiguration {


    @Bean

    public KieContainer kieContainer() throws IOException {

        KieServices ks = KieServices.Factory.get();

        return ks.getKieClasspathContainer();

    }

}


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


For the following test environment, I was able to have 2 rules fired. 


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

@RunWith(SpringRunner.class)

@SpringBootTest(classes = SmartPriceApp.class)

@Transactional

public class RuleServiceIntTest {

    @Autowired

    private RuleService ruleService;

    

    @Test

    public void testRuleService() {

    String msg = ruleService.ruleTest();         //Two rules are fired!

    System.out.println(msg);

    }

}

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


However, if I am using the spring rest controller, No rules were fired. I need your help.


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


@RestController

@RequestMapping("/api")

public class PriceResource {


private final RuleService ruleService;


public PriceResource(RuleService ruleService) {

this.ruleService = ruleService;

}

@PostMapping(path="/priceres")

public PriceResult priceres(@RequestBody RequestWrapper requestWrapper) {

System.out.println(requestWrapper.product);

System.out.println(requestWrapper.customer);


String msg = this.ruleService.ruleTest();      //No rules are fired!

return new PriceResult(234.7, msg);

}

}


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


Thanks,
Mengda

Mario Fusco

unread,
Apr 26, 2017, 9:56:44 AM4/26/17
to Drools Usage
Hi Mengda,

It would be really helpful you could open a issue on our issues tracker ( https://issues.jboss.org/projects/DROOLS ) and attaching there a small project reproducing your problem. I'll investigate this as soon as you'll provide that reproducer.

Thanks,
Mario

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage+unsubscribe@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/drools-usage/f5cd7be0-05c6-4677-95e4-b00b60886543%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Wei Xiong

unread,
Apr 28, 2017, 7:20:40 PM4/28/17
to Drools Usage
Thanks! The issue was reported here: https://issues.jboss.org/browse/DROOLS-1540

Basically, drools does not work with spring-boot-devtools.



在 2017年4月26日星期三 UTC-7上午6:56:44,Mario Fusco写道:
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.

le...@sph.com.sg

unread,
Oct 22, 2017, 11:34:08 PM10/22/17
to Drools Usage
Thanks! It saved my day.
Reply all
Reply to author
Forward
0 new messages