I'm new in drools
. I've found way to automatically insert fact from database based on rule's lhs
in drl
file.
if there is rule like this,
1: rule "simple rule"
2: when
3: Student( name == "Andy" )
4: then
5: end
Then I found fact from db
using hibernate
StudentRepository.findByName("Andy");
But, I can't know how can I use the rule's lhs
in code. Please let me know there is a way and how to do it.
I almost waste 2 weeks to do this...
add my code) OnDemandMessageRuleUnit.java
package com.joel.messageRule.service;
import com.joel.messageRule.model.Daybook;
import com.joel.messageRule.model.Member;
import com.joel.messageRule.repositories.DaybookRepository;
import com.joel.messageRule.repositories.MemberRepository;
import lombok.Getter;
import org.kie.api.runtime.rule.DataSource;
import org.kie.api.runtime.rule.RuleUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Getter
@Component
public class OnDemandMessageRuleUnit implements RuleUnit {
private DataSource<Member> members;
private DataSource<Daybook> daybooks;
private MemberRepository memberRepository;
private DaybookRepository daybookRepository;
private final Logger logger = LoggerFactory.getLogger(OnDemandMessageRuleUnit.class);
public OnDemandMessageRuleUnit(MemberRepository memberRepository, DaybookRepository daybookRepository){
this.memberRepository = memberRepository;
this.daybookRepository = daybookRepository;
this.members = DataSource.create();
this.daybooks = DataSource.create();
}
@Override
public void onStart() {
// in this part i want to insert daybook objects which meet the condition written as rule lhs to data source or kiesession
logger.info("OnDemandMessageRuleUnit Started");
for(Member m:members){
List<Daybook> daybookList = m.getDaybooks();
if(!daybookList.isEmpty()){
for(Daybook d:daybookList){
daybooks.insert(d);
}
}
}
}
@Override
public void onEnd() {
logger.info("OnDemandMessageRuleUnit Ended");
try{
for(Member m:members){
memberRepository.save(m);
}
for(Daybook d:daybooks){
daybookRepository.save(d);
}
}
catch (NullPointerException e){
e.printStackTrace();
throw e;
}
finally {
}
}
}
complimentMessage.drl
package com.joel.messageRule.service
unit OnDemandMessageRuleUnit
import java.lang.Number;
import com.joel.messageRule.model.*;
import org.slf4j.Logger;
rule "complimentMessage"
dialect "mvel"
when
$receiver : Member(cumulativeDaybookRecord >= 3) from members
then
logger.info($receiver.getName() + "님에게 칭찬 메시지를 전송하였습니다.");
end
So, i read Member(cumulativeDaybookRecord >= 3) from rule or Production Memory or Node Memory, make query "select * from Member where cumulativeDaybookRecord >= 3" and insert the result to Working Memory
Hi you need insert your database repository in Working memory. Let's say your Hibernate return only one Student object then rule drl as follow.
And add your student database repository like below
And here is another example if your database repository returns Students lists then needs to modify the rules
So similar way to you use as follow
And in Member Repository
Member repository and loaded into working memory