Example usage for java.util.concurrent.locks Lock lock

List of usage examples for java.util.concurrent.locks Lock lock

Introduction

In this page you can find the example usage for java.util.concurrent.locks Lock lock.

Prototype

lock

Source Link

Usage

From source file:org.unitime.timetable.solver.TimetableSolver.java

public ConflictStatisticsInfo getCbsInfo() {
    ConflictStatistics cbs = null;/*from  www .j a  v  a  2 s  .  c  o m*/
    for (Extension ext : getExtensions()) {
        if (ext instanceof ConflictStatistics) {
            cbs = (ConflictStatistics) ext;
            break;
        }
    }
    if (cbs == null || cbs.getNoGoods().isEmpty()) {
        if (iCbsInfo != null)
            return iCbsInfo;
        return null;
    }
    ConflictStatisticsInfo info = new ConflictStatisticsInfo();
    Lock lock = currentSolution().getLock().readLock();
    lock.lock();
    try {
        info.load(this, cbs);
    } finally {
        lock.unlock();
    }
    return info;
}

From source file:org.unitime.timetable.solver.TimetableSolver.java

public ConflictStatisticsInfo getCbsInfo(Long classId) {
    ConflictStatistics cbs = null;//from ww  w  .ja  v  a  2 s . co m
    for (Extension ext : getExtensions()) {
        if (ext instanceof ConflictStatistics) {
            cbs = (ConflictStatistics) ext;
            break;
        }
    }
    if (cbs == null || cbs.getNoGoods().isEmpty()) {
        if (iCbsInfo != null)
            return iCbsInfo;
        return null;
    }
    ConflictStatisticsInfo info = new ConflictStatisticsInfo();
    Lock lock = currentSolution().getLock().readLock();
    lock.lock();
    try {
        info.load(this, cbs, classId);
    } finally {
        lock.unlock();
    }
    return info;
}

From source file:org.unitime.timetable.solver.TimetableSolver.java

public Vector getChangesToInitial() {
    Vector ret = new Vector();
    Lock lock = currentSolution().getLock().readLock();
    lock.lock();
    try {/*from  w w  w .  j a v  a2s  .  co m*/
        for (Lecture lecture : currentSolution().getModel().variables()) {
            if (!ToolBox.equals(lecture.getInitialAssignment(),
                    currentSolution().getAssignment().getValue(lecture))) {
                RecordedAssignment a = new RecordedAssignment(this, (Placement) lecture.getInitialAssignment(),
                        currentSolution().getAssignment().getValue(lecture));
                if (lecture.getInitialAssignment() != null) {
                    a.getBefore().setDetails(new ClassAssignmentDetails(this, lecture,
                            (Placement) lecture.getInitialAssignment(), false));
                }
                if (currentSolution().getAssignment().getValue(lecture) != null) {
                    a.getAfter().setDetails(new ClassAssignmentDetails(this, lecture, false));
                }
                ret.addElement(a);
            }
        }
    } finally {
        lock.unlock();
    }
    return ret;
}

From source file:org.unitime.timetable.solver.TimetableSolver.java

public Vector getChangesToBest() {
    Vector ret = new Vector();
    Lock lock = currentSolution().getLock().readLock();
    lock.lock();
    try {/*  w  ww. jav  a  2s .co m*/
        for (Lecture lecture : currentSolution().getModel().variables()) {
            Placement placement = currentSolution().getAssignment().getValue(lecture);
            if (!ToolBox.equals(lecture.getBestAssignment(), placement)) {
                RecordedAssignment a = new RecordedAssignment(this, (Placement) lecture.getBestAssignment(),
                        placement);
                if (lecture.getBestAssignment() != null) {
                    a.getBefore().setDetails(new ClassAssignmentDetails(this, lecture,
                            (Placement) lecture.getBestAssignment(), false));
                }
                if (placement != null) {
                    a.getAfter().setDetails(new ClassAssignmentDetails(this, lecture, false));
                }
                ret.addElement(a);
            }
        }
    } finally {
        lock.unlock();
    }
    return ret;
}

From source file:org.unitime.timetable.solver.TimetableSolver.java

public boolean backup(File folder, String puid) {
    folder.mkdirs();//w  w w.  j av a  2s  .c  o  m
    if (currentSolution() == null)
        return false;
    Lock lock = currentSolution().getLock().readLock();
    lock.lock();
    try {
        TimetableXMLSaver saver = new TimetableXMLSaver(this);
        File outXmlFile = new File(folder, puid + BackupFileFilter.sXmlExtension);
        File outPropertiesFile = new File(folder, puid + BackupFileFilter.sPropertiesExtension);
        try {
            saver.save(outXmlFile);
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(outPropertiesFile);
                getProperties().store(fos, "Backup file");
                fos.flush();
                fos.close();
                fos = null;
            } finally {
                try {
                    if (fos != null)
                        fos.close();
                } catch (IOException e) {
                }
            }
            return true;
        } catch (Exception e) {
            sLog.error(e.getMessage(), e);
            if (outXmlFile.exists())
                outXmlFile.delete();
            if (outPropertiesFile.exists())
                outPropertiesFile.delete();
        }
    } finally {
        lock.unlock();
    }
    return false;
}

From source file:org.unitime.timetable.solver.TimetableSolver.java

protected void onFinish() {
    super.onFinish();
    try {//from w w  w.j a  va2s .c  o  m
        iWorking = true;
        if (currentSolution().getBestInfo() != null)
            currentSolution().restoreBest();
        if (getProperties().getPropertyBoolean("General.SwitchStudents", true)) {
            ((TimetableModel) currentSolution().getModel()).switchStudents(currentSolution().getAssignment());
            currentSolution().saveBest();
        }
        if (currentSolution().getBestInfo() != null
                && getProperties().getPropertyBoolean("General.Save", false)) {
            TimetableDatabaseSaver saver = new TimetableDatabaseSaver(this);
            Lock lock = currentSolution().getLock().readLock();
            lock.lock();
            try {
                saver.save();
            } finally {
                lock.unlock();
            }
        }
        int repeat = getProperties().getPropertyInt("Test.Repeat", 0);
        if (repeat > 0) {
            getProperties().setProperty("Test.Repeat", String.valueOf(repeat - 1));
            getProperties().remove("General.SolutionId");
            load(getProperties());
            return;
        }
        if (getProperties().getPropertyBoolean("General.Unload", false)) {
            dispose();
        } else {
            Progress.getInstance(currentSolution().getModel()).setStatus("Awaiting commands ...");
        }
    } finally {
        iWorking = false;
    }
}

From source file:org.unitime.timetable.solver.TimetableSolver.java

public Hashtable conflictInfo(Collection hints) {
    Hashtable conflictTable = new Hashtable();
    Lock lock = currentSolution().getLock().readLock();
    lock.lock();
    try {//from ww  w. j  a v a 2  s .com
        HashSet done = new HashSet();
        for (Iterator i = hints.iterator(); i.hasNext();) {
            Hint hint = (Hint) i.next();
            Placement p = hint.getPlacement((TimetableModel) currentSolution().getModel());
            if (p == null)
                continue;
            for (Constraint constraint : p.variable().hardConstraints()) {
                HashSet conflicts = new HashSet();
                constraint.computeConflicts(currentSolution().getAssignment(), p, conflicts);
                if (conflicts != null && !conflicts.isEmpty()) {
                    for (Iterator j = conflicts.iterator(); j.hasNext();) {
                        Placement conflict = (Placement) j.next();
                        Hint confHint = new Hint(this, conflict);
                        if (done.contains(confHint))
                            continue;
                        if (!conflictTable.containsKey(confHint)) {
                            String name = constraint.getName();
                            if (constraint instanceof RoomConstraint) {
                                name = "Room " + constraint.getName();
                            } else if (constraint instanceof InstructorConstraint) {
                                name = "Instructor " + constraint.getName();
                            } else if (constraint instanceof GroupConstraint) {
                                name = "Distribution " + constraint.getName();
                            } else if (constraint instanceof DepartmentSpreadConstraint) {
                                name = "Balancing of department " + constraint.getName();
                            } else if (constraint instanceof SpreadConstraint) {
                                name = "Same subpart spread " + constraint.getName();
                            } else if (constraint instanceof ClassLimitConstraint) {
                                name = "Class limit " + constraint.getName();
                            }
                            conflictTable.put(confHint, name);
                        }
                    }
                }
            }
            done.add(hint);
        }
    } finally {
        lock.unlock();
    }
    return conflictTable;
}

From source file:org.unitime.timetable.solver.TimetableSolver.java

public void assign(Collection hints) {
    Lock lock = currentSolution().getLock().writeLock();
    lock.lock();
    try {//from ww  w  . j  a v a 2  s.  c  om
        Hashtable initialAssignments = new Hashtable();
        for (Placement placement : currentSolution().getAssignment().assignedValues()) {
            initialAssignments.put(placement.variable(), placement);
        }
        AssignmentRecord record = new AssignmentRecord(this);
        for (Iterator i = hints.iterator(); i.hasNext();) {
            Hint hint = (Hint) i.next();
            Placement p = hint.getPlacement((TimetableModel) currentSolution().getModel());
            if (p != null) {
                Placement ini = (Placement) initialAssignments.get(p.variable());
                record.add(ini, p);
                Progress.getInstance(currentSolution().getModel())
                        .info(p.variable().getName() + ": "
                                + (ini == null ? "not assigned" : ini.getLongName(useAmPm())) + " → "
                                + p.getLongName(useAmPm()));
                if (ini != null)
                    currentSolution().getAssignment().unassign(0, p.variable());
            } else if (hint.getDays() == 0) {
                Lecture lecture = null;
                for (Lecture l : currentSolution().getModel().variables())
                    if (l.getClassId().equals(hint.getClassId())) {
                        lecture = l;
                    }
                if (lecture != null && !lecture.isCommitted())
                    currentSolution().getAssignment().unassign(0, lecture);
            }
        }
        for (Iterator i = hints.iterator(); i.hasNext();) {
            Hint hint = (Hint) i.next();
            Placement p = hint.getPlacement((TimetableModel) currentSolution().getModel());
            if (p != null)
                currentSolution().getAssignment().assign(0, p);
        }
        for (Lecture lec : currentSolution().getModel()
                .unassignedVariables(currentSolution().getAssignment())) {
            Placement p = (Placement) initialAssignments.get(lec);
            if (p != null) {
                record.add(p, null);
                Progress.getInstance(currentSolution().getModel()).info(
                        p.variable().getName() + ": " + p.getLongName(useAmPm()) + " → not assigned");
            }
        }
        record.done();
        iAssignmentRecords.addElement(record);
    } finally {
        lock.unlock();
    }
}

From source file:org.unitime.timetable.solver.TimetableSolver.java

public Assignment getAssignment(Long classId) {
    Lock lock = currentSolution().getLock().readLock();
    lock.lock();
    try {/*from  www  .j ava2s  . c  om*/
        Lecture lecture = null;
        for (Lecture l : currentSolution().getModel().variables()) {
            if (l.getClassId().equals(classId)) {
                lecture = l;
                break;
            }
        }
        if (lecture == null)
            return null;
        Placement placement = (Placement) currentSolution().getAssignment().getValue(lecture);
        if (placement == null)
            return null;
        Assignment assignment = new Assignment();
        assignment.setClassName(lecture.getName());
        assignment.setDays(new Integer(placement.getTimeLocation().getDayCode()));
        assignment.setStartSlot(new Integer(placement.getTimeLocation().getStartSlot()));
        if (placement.getTimeLocation().getDatePatternId() != null) {
            assignment.setDatePattern(
                    DatePatternDAO.getInstance().get(placement.getTimeLocation().getDatePatternId()));
        }
        assignment.setSlotsPerMtg(placement.getTimeLocation().getLength());
        assignment.setBreakTime(placement.getTimeLocation().getBreakTime());
        HashSet rooms = new HashSet();
        if (placement.isMultiRoom()) {
            for (RoomLocation r : placement.getRoomLocations()) {
                Location room = (new LocationDAO()).get(r.getId());
                if (room != null)
                    rooms.add(room);
            }
        } else {
            Location room = (new LocationDAO()).get(placement.getRoomLocation().getId());
            if (room != null)
                rooms.add(room);
        }
        assignment.setRooms(rooms);
        TimePattern pattern = (new TimePatternDAO()).get(placement.getTimeLocation().getTimePatternId());
        assignment.setTimePattern(pattern);
        HashSet instructors = new HashSet();
        for (InstructorConstraint ic : lecture.getInstructorConstraints()) {
            DepartmentalInstructor instructor = null;
            if (ic.getResourceId() != null) {
                instructor = (new DepartmentalInstructorDAO()).get(ic.getResourceId());
            }
            if (instructor != null)
                instructors.add(instructor);

        }
        assignment.setInstructors(instructors);
        return assignment;
    } finally {
        lock.unlock();
    }
}

From source file:org.unitime.timetable.solver.TimetableSolver.java

public Vector getChangesToSolution(Long solutionId, boolean closeSession) throws Exception {
    Lock lock = currentSolution().getLock().readLock();
    lock.lock();
    try {//from  ww w  . jav a  2 s. c o m
        Session hibSession = (new SolutionDAO()).getSession();
        Transaction tx = null;
        Vector ret = new Vector();
        try {
            tx = hibSession.beginTransaction();
            Solution solution = (new SolutionDAO()).get(solutionId, hibSession);
            if (solution == null)
                return null;
            Long ownerId = solution.getOwner().getUniqueId();
            Long[] ownerIds = getOwnerId();
            boolean sameOwner = false;
            for (int i = 0; i < ownerIds.length; i++)
                if (ownerId.equals(ownerIds[i])) {
                    sameOwner = true;
                    break;
                }
            if (!sameOwner)
                return null;
            HashSet ids = new HashSet();
            for (Iterator i = solution.getAssignments().iterator(); i.hasNext();) {
                Assignment assignment = (Assignment) i.next();
                Lecture lecture = null;
                for (Lecture l : currentSolution().getModel().variables()) {
                    if (l.getClassId().equals(assignment.getClassId())) {
                        lecture = l;
                        break;
                    }
                }
                ids.add(assignment.getClassId());
                Placement placement = (lecture == null ? null
                        : currentSolution().getAssignment().getValue(lecture));
                if (lecture == null || placement == null) {
                    RecordedAssignment a = new RecordedAssignment(this, assignment.getPlacement(), null);
                    a.getBefore().setDetails(
                            new ClassAssignmentDetails(solution, assignment, false, hibSession, null));
                    ret.addElement(a);
                } else {
                    if (placement.equals(assignment.getPlacement()))
                        continue;
                    RecordedAssignment a = new RecordedAssignment(this, assignment.getPlacement(), placement);
                    a.getBefore().setDetails(
                            new ClassAssignmentDetails(solution, assignment, false, hibSession, null));
                    a.getAfter().setDetails(new ClassAssignmentDetails(this, lecture, false));
                    ret.addElement(a);
                }
            }
            for (Lecture lecture : currentSolution().getModel().variables()) {
                Placement placement = currentSolution().getAssignment().getValue(lecture);
                if (ids.contains(lecture.getClassId()) || placement == null)
                    continue;
                if (!ownerId.equals(lecture.getSolverGroupId()))
                    continue;
                RecordedAssignment a = new RecordedAssignment(this, null, placement);
                a.getAfter().setDetails(new ClassAssignmentDetails(this, lecture, false));
                ret.addElement(a);
            }
            if (tx != null)
                tx.commit();
        } catch (Exception e) {
            if (tx != null)
                tx.rollback();
            throw e;
        } finally {
            //here we still need to close the session since it can be called by the remote solver as well
            if (closeSession && hibSession != null && hibSession.isOpen())
                hibSession.close();
        }
        return ret;
    } finally {
        lock.unlock();
    }
}