Optaplanner与Hibernate-一些DRL规则不起作用

时间:2018-08-19 17:36:16

标签: hibernate persistence drools optaplanner drools-planner

我正在使用 Optaplanner 申请员工名册。没有持久性,一切都很好。现在,我想添加Hibernate集成。 我想从MySQL数据库中获取信息,并将其用作计划输入。

在数据库中,我有位置技能表。

现在,

员工数据,时间段工作分配已在应用中进行了硬编码。

我的域课程,技能

import javax.persistence.*;

@Entity
@Table(name = "SKILL")
public class Skill {

    private long skillId;
    private String name;

    public Skill(String name) {
        this.name = name;
    }

    public Skill() {}

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "SKILL_ID")
    public long getSkillId() {
        return this.skillId;
    }

    public void setSkillId(long skillId) {
        this.skillId = skillId;
    }

    @Column(name = "SKILL_NAME", nullable = false, length=250)
    public String getSkillName() {
        return this.name;
    }

    public void setSkillName(String skillName) {
        this.name = skillName;
    }

    @Override
    public String toString() {
        return name;
    }

}

位置

import javax.persistence.*;

@Entity
@Table(name = "POSITION")
public class Position {

    private long positionId;
    private String name;
    private Skill requiredSkill;

    public Position(String name, Skill requiredSkill) {
        this.name = name;
        this.requiredSkill = requiredSkill;
    }

    public Position() {}

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "POSITION_ID")
    public long getPositionId() {
        return this.positionId;
    }

    public void setPositionId(long positionId) {
        this.positionId = positionId;
    }

    @Column(name="POSITION_NAME", nullable=false, length = 100)
    public String getPositionName() {
        return this.name;
    }

    public void setPositionName(String positionName) {
        this.name = positionName;
    }

    @OneToOne(cascade = CascadeType.ALL)
    public Skill getRequiredSkill() {
        return this.requiredSkill;
    }

    public void setRequiredSkill(Skill requiredSkill) {
        this.requiredSkill = requiredSkill;
    }

    @Override
    public String toString() {
        return name;
    }
}

无效的规则:

rule "Employee works only on positions with skills he has"
    when
        Assignment(
                employee != null,
                !getEmployee().getSkillSet().contains(getPosition().getRequiredSkill()))
    then
        scoreHolder.addHardConstraintMatch(kcontext, -100);
end

我认为数据库还可以,因为其中的数据之前是使用此应用创建的。另外,我还有另一个有效的DRL规则。我没有收到任何警告/错误-解决过程中没有考虑上述规则。

在解决过程中,我得到了

08:40:11.453 [main] DEBUG org.drools.core.common.DefaultAgenda - State was INACTIVE is now FIRING_ALL_RULES
08:40:11.453 [main] DEBUG org.drools.core.common.DefaultAgenda - State was FIRING_ALL_RULES is now HALTING
08:40:11.453 [main] DEBUG org.drools.core.common.DefaultAgenda - State was HALTING is now INACTIVE
08:40:11.453 [main] DEBUG org.optaplanner.core.impl.constructionheuristic.DefaultConstructionHeuristicPhase -     CH step (1), time spent (134), score (-14init/-202hard/0soft), selected move count (7), picked move (domain.Assignment@4d354a3e {null -> domain.Employee@1511d157}).

此外,在开始时,我会收到以下警告:

Failed to add the foreign key constraint. Missing index for constraint 'FKqx7n7alhjrnaw173dc48i47y0' in the referenced table 'skill'

1 个答案:

答案 0 :(得分:0)

看看我们如何在optaweb-employee-rostering中添加JPA休眠持久性。

具体地说,乐观地锁定Shift类提供了一个挑战,我们已修复了like this