Drools Rules Not Fired

290 views
Skip to first unread message

Bora SAYINER

unread,
Mar 25, 2017, 7:32:18 AM3/25/17
to OptaPlanner development
hi! i'm developing an application like optaplanner example curriculum. i have 3 class SimpleSolution, Lecture and Room. 
SimpleSolution.java is @PlanningSolution.
Lecture.java is @PlanningEntity.
And Room.java is only fact.
if i use SimpleScoreCalculation everything is ok.
but when i use drools rules my rule is never fired.

The rule is like that;
rule "roomCapacity"
    when
        $room : Room($capacity : capacity)
        Lecture(room == $room, studentSize > $capacity, $studentSize : studentSize)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, ($capacity - $studentSize));
        System.out.println("asdasd");
end

Room.java
public class Room implements Serializable, Comparable<Room> {

private int id;
private String name;
private int capacity;

public Room() {
this(0, null, 0);
}

public Room(int id, String name, int capacity) {
this.id = id;
this.name = name;
this.capacity = capacity;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

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

public int getCapacity() {
return capacity;
}

public void setCapacity(int capacity) {
this.capacity = capacity;
}

@Override
public String toString() {
return "Room [id=" + id + ", name=" + name + ", capacity=" + capacity + "]";
}

@Override
public int compareTo(Room other) {
return new CompareToBuilder().append(getClass().getName(), other.getClass().getName()).append(id, other.id).toComparison();
}

}

Lecture.java
@PlanningEntity
public class Lecture implements Serializable, Comparable<Lecture> {

private int id;
private String code;
private int studentSize;
private Room room;
private Period period;

public Lecture() {
this(0, null, 0);
}

public Lecture(int id, String code, int studentSize) {
this.id = id;
this.code = code;
this.studentSize = studentSize;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public int getStudentSize() {
return studentSize;
}

public void setStudentSize(int studentSize) {
this.studentSize = studentSize;
}

@PlanningVariable(valueRangeProviderRefs = { "roomProvider" })
public Room getRoom() {
return room;
}

public void setRoom(Room room) {
this.room = room;
}

@PlanningVariable(valueRangeProviderRefs = { "periodProvider" })
public Period getPeriod() {
return period;
}

public void setPeriod(Period period) {
this.period = period;
}

@Override
public String toString() {
return "Lecture [id=" + id + ", code=" + code + ", studentSize=" + studentSize + ", room=" + room + ", period=" + period + "]";
}

@Override
public int compareTo(Lecture other) {
return new CompareToBuilder().append(getClass().getName(), other.getClass().getName()).append(id, other.id).toComparison();
}

}

And CourseSchedule.java
@PlanningSolution
public class CourseSchedule implements Solution<HardSoftScore> {

private String name;
private List<Room> rooms;
private List<Period> periods;
private List<Lecture> lectures;
private HardSoftScore score;

public CourseSchedule() {
this("Unknown Schedule");
}
public CourseSchedule(String name) {
this.name = name;
}

public String getName() {
return name;
}

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

@ValueRangeProvider(id = "roomProvider")
public List<Room> getRooms() {
return rooms;
}

public void setRooms(List<Room> rooms) {
this.rooms = rooms;
}

@ValueRangeProvider(id = "periodProvider")
public List<Period> getPeriods() {
return periods;
}

public void setPeriods(List<Period> periods) {
this.periods = periods;
}

@PlanningEntityCollectionProperty
public List<Lecture> getLectures() {
return lectures;
}

public void setLectures(List<Lecture> lectures) {
this.lectures = lectures;
}

@Override
public HardSoftScore getScore() {
return score;
}

@Override
public void setScore(HardSoftScore score) {
this.score = score;
}

@Override
public Collection<? extends Object> getProblemFacts() {
List<Object> facts = new ArrayList<>();
facts.add(rooms);
facts.add(periods);
return facts;
}

}

Main.java
public class Driver {

public static void main(String[] args) {
SolverFactory<CourseSchedule> solverFactory = SolverFactory.createFromXmlResource("solver/config/resourcePlannerSolverConfig.xml");
Solver<CourseSchedule> solver = solverFactory.buildSolver();
CourseSchedule unsolved = new CourseSchedule();
unsolved.setRooms(createRandomRooms(3));
System.out.println("------------------------------------------------");
unsolved.setPeriods(createRandomPeriods(3));
System.out.println("------------------------------------------------");
unsolved.setLectures(createRandomLectures(10));
System.out.println("------------------------------------------------");
CourseSchedule solved = solver.solve(unsolved);
List<Lecture> lectures = solved.getLectures();
for (Lecture lecture : lectures) {
System.out.println(lecture);
}
}

public static List<Room> createRandomRooms(int size) {
List<Room> rooms = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < size; i++) {
rooms.add(new Room(i, String.format("R-%d", i), (random.nextInt(25) + 1)));
System.out.println(rooms.get(i));
}
return rooms;
}

public static List<Period> createRandomPeriods(int size) {
List<Period> periods = new ArrayList<>();
int count = 0;
for (int i = 0; i < size; i++) {
for (int j = 0; j < 5; j++) {
Period period = new Period(count++, i, j);
periods.add(period);
System.out.println(period);
}
}
return periods;
}

public static List<Lecture> createRandomLectures(int size) {
List<Lecture> lectures = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < size; i++) {
lectures.add(new Lecture(i, String.format("L-%d", i), random.nextInt(25) + 1));
System.out.println(lectures.get(i));
}
return lectures;
}
}

What is the problem?
Thanks

Geoffrey De Smet

unread,
Mar 25, 2017, 1:42:18 PM3/25/17
to optaplanner-dev
facts.add(rooms);

Use addAll() instead.

--
You received this message because you are subscribed to the Google Groups "OptaPlanner development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to optaplanner-dev+unsubscribe@googlegroups.com.
To post to this group, send email to optaplanner-dev@googlegroups.com.
Visit this group at https://groups.google.com/group/optaplanner-dev.
For more options, visit https://groups.google.com/d/optout.



--
With kind regards / Met vriendelijke groeten,
Geoffrey De Smet

Bora SAYINER

unread,
Mar 25, 2017, 1:59:41 PM3/25/17
to OptaPlanner development
Hi Geoffrey.
This is crazy fault for me :). I try this and works. Thank you so much.

25 Mart 2017 Cumartesi 20:42:18 UTC+3 tarihinde Geoffrey De Smet yazdı:
facts.add(rooms);

Use addAll() instead.

To unsubscribe from this group and stop receiving emails from it, send an email to optaplanner-d...@googlegroups.com.
To post to this group, send email to optapla...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages