我是新来的drools
。我找到了根据文件中的规则自动从数据库中插入事实的lhs
方法drl
。
如果有这样的规定,
1: rule "simple rule"
2: when
3: Student( name == "Andy" )
4: then
5: end
然后我从db
使用中发现了事实hibernate
StudentRepository.findByName("Andy");
但是,我不知道如何lhs
在代码中使用该规则。请让我知道有一种方法以及如何做到这一点。
我几乎浪费了两个星期来做这件事......
添加我的代码) 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 {
}
}
}
恭维消息.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
因此,我从规则或生产内存或节点内存中读取 Member(cumulativeDaybookRecord >= 3),查询“select * from Member wherecumulativeDaybookRecord >= 3”并将结果插入工作内存