Skip to content

Commit

Permalink
performance improvment about requisites
Browse files Browse the repository at this point in the history
  • Loading branch information
skarahoda committed Jun 18, 2015
1 parent 3546f8c commit 9094452
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 15 deletions.
6 changes: 4 additions & 2 deletions src/io/scheduler/data/Course.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.scheduler.data;

import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

Expand Down Expand Up @@ -155,9 +156,10 @@ public boolean isCheckedForReq() {
return isCheckedForReq;
}

public boolean hasPreRequisiteRestriction() throws SQLException {
public boolean hasPreRequisiteRestriction(Collection<Course> courses)
throws SQLException {
if (preReq != null) {
if (preReq.isValid(TakenCourse.getAll()) == false) {
if (preReq.isValid(courses) == false) {
return false;
}
}
Expand Down
66 changes: 58 additions & 8 deletions src/io/scheduler/data/Requisite.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public enum Operation {

@DatabaseField(columnName = RIGHT_FIELD_NAME)
private int rightId;
private Requisite right;
private Requisite left;

Requisite() {
}
Expand All @@ -62,8 +64,8 @@ public Requisite(int leftId, int rightId, boolean isAnd)
throws SQLException, IllegalArgumentException {
this.leftId = leftId;
this.rightId = rightId;
Requisite left = getWithId(leftId);
Requisite right = getWithId(rightId);
this.left = getWithId(leftId);
this.right = getWithId(rightId);
if (left == null || right == null)
throw new IllegalArgumentException();
this.operation = isAnd ? Operation.AND : Operation.OR;
Expand All @@ -85,9 +87,9 @@ public String toString() {
case UNARY:
return courseCode;
case AND:
return "( " + leftId + " and " + rightId + " )";
return "( " + leftId + " and " + getRight() + " )";
case OR:
return "( " + leftId + " or " + rightId + " )";
return "( " + leftId + " or " + getRight() + " )";
}
return null;
}
Expand All @@ -104,14 +106,14 @@ public boolean isValid(Collection<Course> courses) throws SQLException {
}
return false;
case AND:
left = getWithId(leftId);
right = getWithId(rightId);
left = getRight();
right = getLeft();
if (left == null || right == null)
return false;
return left.isValid(courses) && right.isValid(courses);
case OR:
left = getWithId(leftId);
right = getWithId(rightId);
left = getRight();
right = getLeft();
if (left == null || right == null)
return false;
return left.isValid(courses) || right.isValid(courses);
Expand All @@ -123,4 +125,52 @@ public boolean isValid(Collection<Course> courses) throws SQLException {
public int getId() {
return id;
}

/**
* @return the right
*/
private Requisite getRight() {
setRight();
return right;
}

/**
* @param right
* the right to set
*/
private void setRight() {
if (right != null)
return;
try {
right = getWithId(rightId);
} catch (SQLException e) {
right = null;
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* @return the left
*/
private Requisite getLeft() {
setLeft();
return left;
}

/**
* @param left
* the left to set
*/
private void setLeft() {
if (left != null)
return;
try {
left = getWithId(leftId);
} catch (SQLException e) {
left = null;
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
6 changes: 4 additions & 2 deletions src/io/scheduler/data/SUClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,14 @@ public static SUClass create(String crn, String instructor, String section,
}

/**
* @param courses
* @return
* @throws SQLException
* @see io.scheduler.data.Course#hasPreRequisiteRestriction()
*/
public boolean hasPreRequisiteRestriction() throws SQLException {
return course.hasPreRequisiteRestriction();
public boolean hasPreRequisiteRestriction(Collection<Course> courses)
throws SQLException {
return course.hasPreRequisiteRestriction(courses);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/io/scheduler/data/handler/FiltersSUClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.scheduler.data.SUClass;
import io.scheduler.data.SUClass.ComparisonOperator;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Date;

Expand Down Expand Up @@ -61,4 +62,20 @@ public boolean apply(SUClass arg0) {
}
});
}

public static Collection<SUClass> filterPreReq(Collection<SUClass> classes,
final Collection<Course> courses) {
return Collections2.filter(classes, new Predicate<SUClass>() {
@Override
public boolean apply(SUClass arg0) {
try {
return arg0.hasPreRequisiteRestriction(courses);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return true;
}
}
});
}
}
19 changes: 16 additions & 3 deletions src/io/scheduler/gui/OptionSUClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class OptionSUClass {
private Collection<SUClass> filteredSuClasses;
private Collection<SUClass> suClasses;
private JCheckBox checkBoxCoReq;
private JCheckBox checkBoxPreReq;
private JCheckBox checkBoxTaken;
private JComboBox<Object> comboBoxOp;
private JSpinner spinnerTime;
Expand All @@ -58,13 +59,14 @@ public OptionSUClass(Collection<SUClass> suClasses)
this.suClasses = suClasses;
JPanel optionPanel = initOptionPanel();
checkBoxCoReq = new JCheckBox("Courses without corequisite");
checkBoxPreReq = new JCheckBox("Courses that is valid for prerequisite");
checkBoxTaken = new JCheckBox("Hide taken courses");
JPanel panelTimeComparer = initPanelTimeComparer();
addEventListeners();
fillScrollCourse();
Object[] message = { panelTimeComparer, checkBoxCoReq, checkBoxTaken,
optionPanel };
option = JOptionPane.showConfirmDialog(null, message, "Classes",
Object[] messages = { panelTimeComparer, checkBoxCoReq, checkBoxPreReq,
checkBoxTaken, optionPanel };
option = JOptionPane.showConfirmDialog(null, messages, "Classes",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
}

Expand Down Expand Up @@ -144,6 +146,7 @@ public void actionPerformed(ActionEvent e) {
}
};
checkBoxCoReq.addActionListener(filterListener);
checkBoxPreReq.addActionListener(filterListener);
checkBoxTaken.addActionListener(filterListener);
comboBoxOp.addActionListener(filterListener);
spinnerTime.addChangeListener(new ChangeListener() {
Expand All @@ -160,6 +163,16 @@ protected void filter() {
} else {
filteredSuClasses = new ArrayList<SUClass>(suClasses);
}
if (checkBoxPreReq.isSelected()) {
try {
List<Course> courses = TakenCourse.getAll();
filteredSuClasses = FiltersSUClass.filterPreReq(
filteredSuClasses, courses);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (checkBoxTaken.isSelected()) {
try {
List<Course> courses = TakenCourse.getAll();
Expand Down

0 comments on commit 9094452

Please sign in to comment.