001package org.cpsolver.coursett; 002 003import java.io.File; 004import java.io.FileOutputStream; 005import java.io.IOException; 006import java.text.DecimalFormat; 007import java.text.DecimalFormatSymbols; 008import java.util.ArrayList; 009import java.util.BitSet; 010import java.util.Collections; 011import java.util.Date; 012import java.util.HashSet; 013import java.util.HashMap; 014import java.util.Iterator; 015import java.util.List; 016import java.util.Locale; 017import java.util.Map; 018import java.util.Set; 019import java.util.TreeSet; 020 021 022import org.cpsolver.coursett.constraint.ClassLimitConstraint; 023import org.cpsolver.coursett.constraint.DiscouragedRoomConstraint; 024import org.cpsolver.coursett.constraint.FlexibleConstraint; 025import org.cpsolver.coursett.constraint.GroupConstraint; 026import org.cpsolver.coursett.constraint.IgnoreStudentConflictsConstraint; 027import org.cpsolver.coursett.constraint.InstructorConstraint; 028import org.cpsolver.coursett.constraint.MinimizeNumberOfUsedGroupsOfTime; 029import org.cpsolver.coursett.constraint.MinimizeNumberOfUsedRoomsConstraint; 030import org.cpsolver.coursett.constraint.RoomConstraint; 031import org.cpsolver.coursett.constraint.SpreadConstraint; 032import org.cpsolver.coursett.model.Configuration; 033import org.cpsolver.coursett.model.Lecture; 034import org.cpsolver.coursett.model.Placement; 035import org.cpsolver.coursett.model.RoomLocation; 036import org.cpsolver.coursett.model.RoomSharingModel; 037import org.cpsolver.coursett.model.Student; 038import org.cpsolver.coursett.model.TimeLocation; 039import org.cpsolver.ifs.model.Constraint; 040import org.cpsolver.ifs.solver.Solver; 041import org.cpsolver.ifs.util.Progress; 042import org.cpsolver.ifs.util.ToolBox; 043import org.dom4j.Document; 044import org.dom4j.DocumentHelper; 045import org.dom4j.Element; 046import org.dom4j.io.OutputFormat; 047import org.dom4j.io.XMLWriter; 048 049/** 050 * This class saves the resultant solution in the XML format. <br> 051 * <br> 052 * Parameters: 053 * <table border='1' summary='Related Solver Parameters'> 054 * <tr> 055 * <th>Parameter</th> 056 * <th>Type</th> 057 * <th>Comment</th> 058 * </tr> 059 * <tr> 060 * <td>General.Output</td> 061 * <td>{@link String}</td> 062 * <td>Folder with the output solution in XML format (solution.xml)</td> 063 * </tr> 064 * <tr> 065 * <td>Xml.ConvertIds</td> 066 * <td>{@link Boolean}</td> 067 * <td>If true, ids are converted (to be able to make input data public)</td> 068 * </tr> 069 * <tr> 070 * <td>Xml.ShowNames</td> 071 * <td>{@link Boolean}</td> 072 * <td>If false, names are not exported (to be able to make input data public)</td> 073 * </tr> 074 * <tr> 075 * <td>Xml.SaveBest</td> 076 * <td>{@link Boolean}</td> 077 * <td>If true, best solution is saved.</td> 078 * </tr> 079 * <tr> 080 * <td>Xml.SaveInitial</td> 081 * <td>{@link Boolean}</td> 082 * <td>If true, initial solution is saved.</td> 083 * </tr> 084 * <tr> 085 * <td>Xml.SaveCurrent</td> 086 * <td>{@link Boolean}</td> 087 * <td>If true, current solution is saved.</td> 088 * </tr> 089 * <tr> 090 * <td>Xml.ExportStudentSectioning</td> 091 * <td>{@link Boolean}</td> 092 * <td>If true, student sectioning is saved even when there is no solution.</td> 093 * </tr> 094 * </table> 095 * 096 * @version CourseTT 1.3 (University Course Timetabling)<br> 097 * Copyright (C) 2006 - 2014 Tomas Muller<br> 098 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 099 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 100 * <br> 101 * This library is free software; you can redistribute it and/or modify 102 * it under the terms of the GNU Lesser General Public License as 103 * published by the Free Software Foundation; either version 3 of the 104 * License, or (at your option) any later version. <br> 105 * <br> 106 * This library is distributed in the hope that it will be useful, but 107 * WITHOUT ANY WARRANTY; without even the implied warranty of 108 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 109 * Lesser General Public License for more details. <br> 110 * <br> 111 * You should have received a copy of the GNU Lesser General Public 112 * License along with this library; if not see 113 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 114 */ 115 116public class TimetableXMLSaver extends TimetableSaver { 117 private static org.apache.log4j.Logger sLogger = org.apache.log4j.Logger.getLogger(TimetableXMLSaver.class); 118 private static DecimalFormat[] sDF = { new DecimalFormat(""), new DecimalFormat("0"), new DecimalFormat("00"), 119 new DecimalFormat("000"), new DecimalFormat("0000"), new DecimalFormat("00000"), 120 new DecimalFormat("000000"), new DecimalFormat("0000000") }; 121 private static DecimalFormat sStudentWeightFormat = new DecimalFormat("0.0000", new DecimalFormatSymbols(Locale.US)); 122 public static boolean ANONYMISE = false; 123 124 private boolean iConvertIds = false; 125 private boolean iShowNames = false; 126 private File iOutputFolder = null; 127 private boolean iSaveBest = false; 128 private boolean iSaveInitial = false; 129 private boolean iSaveCurrent = false; 130 private boolean iExportStudentSectioning = false; 131 132 private IdConvertor iIdConvertor = null; 133 134 public TimetableXMLSaver(Solver<Lecture, Placement> solver) { 135 super(solver); 136 137 138 iOutputFolder = new File(getModel().getProperties().getProperty("General.Output", 139 "." + File.separator + "output")); 140 iShowNames = getModel().getProperties().getPropertyBoolean("Xml.ShowNames", false); 141 iExportStudentSectioning = getModel().getProperties().getPropertyBoolean("Xml.ExportStudentSectioning", false); 142 if (ANONYMISE) { 143 // anonymise saved XML file -- if not set otherwise in the 144 // configuration 145 iConvertIds = getModel().getProperties().getPropertyBoolean("Xml.ConvertIds", true); 146 iSaveBest = getModel().getProperties().getPropertyBoolean("Xml.SaveBest", false); 147 iSaveInitial = getModel().getProperties().getPropertyBoolean("Xml.SaveInitial", false); 148 iSaveCurrent = getModel().getProperties().getPropertyBoolean("Xml.SaveCurrent", true); 149 } else { 150 // normal operation -- if not set otherwise in the configuration 151 iConvertIds = getModel().getProperties().getPropertyBoolean("Xml.ConvertIds", false); 152 iSaveBest = getModel().getProperties().getPropertyBoolean("Xml.SaveBest", true); 153 iSaveInitial = getModel().getProperties().getPropertyBoolean("Xml.SaveInitial", true); 154 iSaveCurrent = getModel().getProperties().getPropertyBoolean("Xml.SaveCurrent", true); 155 } 156 } 157 158 private String getId(String type, String id) { 159 if (!iConvertIds) 160 return id.toString(); 161 if (iIdConvertor == null) 162 iIdConvertor = new IdConvertor(getModel().getProperties().getProperty("Xml.IdConv")); 163 return iIdConvertor.convert(type, id); 164 } 165 166 private String getId(String type, Number id) { 167 return getId(type, id.toString()); 168 } 169 170 private static String bitset2string(BitSet b) { 171 StringBuffer sb = new StringBuffer(); 172 for (int i = 0; i < b.length(); i++) 173 sb.append(b.get(i) ? "1" : "0"); 174 return sb.toString(); 175 } 176 177 @Override 178 public void save() throws Exception { 179 save(null); 180 } 181 182 public void save(File outFile) throws Exception { 183 if (outFile == null) 184 outFile = new File(iOutputFolder, "solution.xml"); 185 outFile.getParentFile().mkdirs(); 186 sLogger.debug("Writting XML data to:" + outFile); 187 188 Document document = DocumentHelper.createDocument(); 189 document.addComment("University Course Timetabling"); 190 191 if (iSaveCurrent && getAssignment().nrAssignedVariables() != 0) { 192 StringBuffer comments = new StringBuffer("Solution Info:\n"); 193 Map<String, String> solutionInfo = (getSolution() == null ? getModel().getExtendedInfo(getAssignment()) : getSolution().getExtendedInfo()); 194 for (String key : new TreeSet<String>(solutionInfo.keySet())) { 195 String value = solutionInfo.get(key); 196 comments.append(" " + key + ": " + value + "\n"); 197 } 198 document.addComment(comments.toString()); 199 } 200 201 Element root = document.addElement("timetable"); 202 root.addAttribute("version", "2.5"); 203 root.addAttribute("initiative", getModel().getProperties().getProperty("Data.Initiative")); 204 root.addAttribute("term", getModel().getProperties().getProperty("Data.Term")); 205 root.addAttribute("year", String.valueOf(getModel().getYear())); 206 root.addAttribute("created", String.valueOf(new Date())); 207 root.addAttribute("nrDays", String.valueOf(Constants.DAY_CODES.length)); 208 root.addAttribute("slotsPerDay", String.valueOf(Constants.SLOTS_PER_DAY)); 209 if (!iConvertIds && getModel().getProperties().getProperty("General.SessionId") != null) 210 root.addAttribute("session", getModel().getProperties().getProperty("General.SessionId")); 211 if (iShowNames && !iConvertIds && getModel().getProperties().getProperty("General.SolverGroupId") != null) 212 root.addAttribute("solverGroup", getId("solverGroup", getModel().getProperties().getProperty( 213 "General.SolverGroupId"))); 214 215 HashMap<String, Element> roomElements = new HashMap<String, Element>(); 216 217 Element roomsEl = root.addElement("rooms"); 218 for (RoomConstraint roomConstraint : getModel().getRoomConstraints()) { 219 Element roomEl = roomsEl.addElement("room").addAttribute("id", 220 getId("room", roomConstraint.getResourceId())); 221 roomEl.addAttribute("constraint", "true"); 222 if (roomConstraint instanceof DiscouragedRoomConstraint) 223 roomEl.addAttribute("discouraged", "true"); 224 if (iShowNames) { 225 roomEl.addAttribute("name", roomConstraint.getRoomName()); 226 } 227 if (!iConvertIds && roomConstraint.getBuildingId() != null) 228 roomEl.addAttribute("building", getId("bldg", roomConstraint.getBuildingId())); 229 roomElements.put(getId("room", roomConstraint.getResourceId()), roomEl); 230 roomEl.addAttribute("capacity", String.valueOf(roomConstraint.getCapacity())); 231 if (roomConstraint.getPosX() != null && roomConstraint.getPosY() != null) 232 roomEl.addAttribute("location", roomConstraint.getPosX() + "," + roomConstraint.getPosY()); 233 if (roomConstraint.getIgnoreTooFar()) 234 roomEl.addAttribute("ignoreTooFar", "true"); 235 if (!roomConstraint.getConstraint()) 236 roomEl.addAttribute("fake", "true"); 237 if (roomConstraint.getSharingModel() != null) { 238 RoomSharingModel sharingModel = roomConstraint.getSharingModel(); 239 Element sharingEl = roomEl.addElement("sharing"); 240 sharingEl.addElement("pattern").addAttribute("unit", String.valueOf(sharingModel.getStep())).setText(sharingModel.getPreferences()); 241 sharingEl.addElement("freeForAll").addAttribute("value", 242 String.valueOf(sharingModel.getFreeForAllPrefChar())); 243 sharingEl.addElement("notAvailable").addAttribute("value", 244 String.valueOf(sharingModel.getNotAvailablePrefChar())); 245 for (int i = 0; i < sharingModel.getNrDepartments(); i++) { 246 sharingEl.addElement("department").addAttribute("value", String.valueOf((char) ('0' + i))) 247 .addAttribute("id", getId("dept", sharingModel.getDepartmentIds()[i])); 248 } 249 } 250 if (roomConstraint.getType() != null && iShowNames) 251 roomEl.addAttribute("type", roomConstraint.getType().toString()); 252 253 Map<Long, Integer> travelTimes = getModel().getDistanceMetric().getTravelTimes().get(roomConstraint.getResourceId()); 254 if (travelTimes != null) 255 for (Map.Entry<Long, Integer> time: travelTimes.entrySet()) 256 roomEl.addElement("travel-time").addAttribute("id", getId("room", time.getKey())).addAttribute("minutes", time.getValue().toString()); 257 } 258 259 Element instructorsEl = root.addElement("instructors"); 260 261 Element departmentsEl = root.addElement("departments"); 262 HashMap<Long, String> depts = new HashMap<Long, String>(); 263 264 Element configsEl = (iShowNames ? root.addElement("configurations") : null); 265 HashSet<Configuration> configs = new HashSet<Configuration>(); 266 267 Element classesEl = root.addElement("classes"); 268 HashMap<Long, Element> classElements = new HashMap<Long, Element>(); 269 List<Lecture> vars = new ArrayList<Lecture>(getModel().variables()); 270 if (getModel().hasConstantVariables()) 271 vars.addAll(getModel().constantVariables()); 272 for (Lecture lecture : vars) { 273 Placement placement = getAssignment().getValue(lecture); 274 if (lecture.isCommitted() && placement == null) 275 placement = lecture.getInitialAssignment(); 276 Placement initialPlacement = lecture.getInitialAssignment(); 277 // if (initialPlacement==null) initialPlacement = 278 // (Placement)lecture.getAssignment(); 279 Placement bestPlacement = lecture.getBestAssignment(); 280 Element classEl = classesEl.addElement("class").addAttribute("id", getId("class", lecture.getClassId())); 281 classElements.put(lecture.getClassId(), classEl); 282 if (iShowNames && lecture.getNote() != null) 283 classEl.addAttribute("note", lecture.getNote()); 284 if (iShowNames && !lecture.isCommitted()) 285 classEl.addAttribute("ord", String.valueOf(lecture.getOrd())); 286 if (lecture.getWeight() != 1.0) 287 classEl.addAttribute("weight", String.valueOf(lecture.getWeight())); 288 if (iShowNames && lecture.getSolverGroupId() != null) 289 classEl.addAttribute("solverGroup", getId("solverGroup", lecture.getSolverGroupId())); 290 if (lecture.getParent() == null && lecture.getConfiguration() != null) { 291 if (!iShowNames) 292 classEl.addAttribute("offering", getId("offering", lecture.getConfiguration().getOfferingId() 293 .toString())); 294 classEl.addAttribute("config", getId("config", lecture.getConfiguration().getConfigId().toString())); 295 if (iShowNames && configs.add(lecture.getConfiguration())) { 296 configsEl.addElement("config").addAttribute("id", 297 getId("config", lecture.getConfiguration().getConfigId().toString())).addAttribute("limit", 298 String.valueOf(lecture.getConfiguration().getLimit())).addAttribute("offering", 299 getId("offering", lecture.getConfiguration().getOfferingId().toString())); 300 } 301 } 302 classEl.addAttribute("committed", (lecture.isCommitted() ? "true" : "false")); 303 if (lecture.getParent() != null) 304 classEl.addAttribute("parent", getId("class", lecture.getParent().getClassId())); 305 if (lecture.getSchedulingSubpartId() != null) 306 classEl.addAttribute("subpart", getId("subpart", lecture.getSchedulingSubpartId())); 307 if (iShowNames && lecture.isCommitted() && placement != null && placement.getAssignmentId() != null) { 308 classEl.addAttribute("assignment", getId("assignment", placement.getAssignmentId())); 309 } 310 if (!lecture.isCommitted()) { 311 if (lecture.minClassLimit() == lecture.maxClassLimit()) { 312 classEl.addAttribute("classLimit", String.valueOf(lecture.maxClassLimit())); 313 } else { 314 classEl.addAttribute("minClassLimit", String.valueOf(lecture.minClassLimit())); 315 classEl.addAttribute("maxClassLimit", String.valueOf(lecture.maxClassLimit())); 316 } 317 if (lecture.roomToLimitRatio() != 1.0f) 318 classEl.addAttribute("roomToLimitRatio", sStudentWeightFormat.format(lecture.roomToLimitRatio())); 319 } 320 if (lecture.getNrRooms() != 1) 321 classEl.addAttribute("nrRooms", String.valueOf(lecture.getNrRooms())); 322 if (lecture.getNrRooms() > 1 && lecture.getMaxRoomCombinations() > 0) 323 classEl.addAttribute("maxRoomCombinations", String.valueOf(lecture.getMaxRoomCombinations())); 324 if (iShowNames) 325 classEl.addAttribute("name", lecture.getName()); 326 if (lecture.getDeptSpreadConstraint() != null) { 327 classEl.addAttribute("department", getId("dept", lecture.getDeptSpreadConstraint().getDepartmentId())); 328 depts.put(lecture.getDeptSpreadConstraint().getDepartmentId(), lecture.getDeptSpreadConstraint() 329 .getName()); 330 } 331 if (lecture.getScheduler() != null) 332 classEl.addAttribute("scheduler", getId("dept", lecture.getScheduler())); 333 for (InstructorConstraint ic : lecture.getInstructorConstraints()) { 334 Element instrEl = classEl.addElement("instructor") 335 .addAttribute("id", getId("inst", ic.getResourceId())); 336 if ((lecture.isCommitted() || iSaveCurrent) && placement != null) 337 instrEl.addAttribute("solution", "true"); 338 if (iSaveInitial && initialPlacement != null) 339 instrEl.addAttribute("initial", "true"); 340 if (iSaveBest && bestPlacement != null && !bestPlacement.equals(placement)) 341 instrEl.addAttribute("best", "true"); 342 } 343 for (RoomLocation rl : lecture.roomLocations()) { 344 Element roomLocationEl = classEl.addElement("room"); 345 roomLocationEl.addAttribute("id", getId("room", rl.getId())); 346 roomLocationEl.addAttribute("pref", String.valueOf(rl.getPreference())); 347 if ((lecture.isCommitted() || iSaveCurrent) && placement != null 348 && placement.hasRoomLocation(rl.getId())) 349 roomLocationEl.addAttribute("solution", "true"); 350 if (iSaveInitial && initialPlacement != null && initialPlacement.hasRoomLocation(rl.getId())) 351 roomLocationEl.addAttribute("initial", "true"); 352 if (iSaveBest && bestPlacement != null && !bestPlacement.equals(placement) 353 && bestPlacement.hasRoomLocation(rl.getId())) 354 roomLocationEl.addAttribute("best", "true"); 355 if (!roomElements.containsKey(getId("room", rl.getId()))) { 356 // room location without room constraint 357 Element roomEl = roomsEl.addElement("room").addAttribute("id", getId("room", rl.getId())); 358 roomEl.addAttribute("constraint", "false"); 359 if (!iConvertIds && rl.getBuildingId() != null) 360 roomEl.addAttribute("building", getId("bldg", rl.getBuildingId())); 361 if (iShowNames) { 362 roomEl.addAttribute("name", rl.getName()); 363 } 364 roomElements.put(getId("room", rl.getId()), roomEl); 365 roomEl.addAttribute("capacity", String.valueOf(rl.getRoomSize())); 366 if (rl.getPosX() != null && rl.getPosY() != null) 367 roomEl.addAttribute("location", rl.getPosX() + "," + rl.getPosY()); 368 if (rl.getIgnoreTooFar()) 369 roomEl.addAttribute("ignoreTooFar", "true"); 370 } 371 } 372 boolean first = true; 373 Set<Long> dp = new HashSet<Long>(); 374 for (TimeLocation tl : lecture.timeLocations()) { 375 Element timeLocationEl = classEl.addElement("time"); 376 timeLocationEl.addAttribute("days", sDF[7].format(Long.parseLong(Integer 377 .toBinaryString(tl.getDayCode())))); 378 timeLocationEl.addAttribute("start", String.valueOf(tl.getStartSlot())); 379 timeLocationEl.addAttribute("length", String.valueOf(tl.getLength())); 380 timeLocationEl.addAttribute("breakTime", String.valueOf(tl.getBreakTime())); 381 if (iShowNames) { 382 timeLocationEl.addAttribute("pref", String.valueOf(tl.getPreference())); 383 timeLocationEl.addAttribute("npref", String.valueOf(tl.getNormalizedPreference())); 384 } else { 385 timeLocationEl.addAttribute("pref", String.valueOf(tl.getNormalizedPreference())); 386 } 387 if (!iConvertIds && tl.getTimePatternId() != null) 388 timeLocationEl.addAttribute("pattern", getId("pat", tl.getTimePatternId())); 389 if (tl.getDatePatternId() != null && dp.add(tl.getDatePatternId())) { 390 Element dateEl = classEl.addElement("date"); 391 dateEl.addAttribute("id", getId("dpat", String.valueOf(tl.getDatePatternId()))); 392 if (iShowNames) 393 dateEl.addAttribute("name", tl.getDatePatternName()); 394 dateEl.addAttribute("pattern", bitset2string(tl.getWeekCode())); 395 } 396 if (tl.getDatePatternPreference() != 0) 397 timeLocationEl.addAttribute("datePref", String.valueOf(tl.getDatePatternPreference())); 398 if (tl.getTimePatternId() == null && first) { 399 if (iShowNames) 400 classEl.addAttribute("datePatternName", tl.getDatePatternName()); 401 classEl.addAttribute("dates", bitset2string(tl.getWeekCode())); 402 first = false; 403 } 404 if (tl.getDatePatternId() != null) { 405 timeLocationEl.addAttribute("date", getId("dpat", String.valueOf(tl.getDatePatternId()))); 406 } 407 if ((lecture.isCommitted() || iSaveCurrent) && placement != null 408 && placement.getTimeLocation().equals(tl)) 409 timeLocationEl.addAttribute("solution", "true"); 410 if (iSaveInitial && initialPlacement != null && initialPlacement.getTimeLocation().equals(tl)) 411 timeLocationEl.addAttribute("initial", "true"); 412 if (iSaveBest && bestPlacement != null && !bestPlacement.equals(placement) 413 && bestPlacement.getTimeLocation().equals(tl)) 414 timeLocationEl.addAttribute("best", "true"); 415 } 416 } 417 418 for (InstructorConstraint ic : getModel().getInstructorConstraints()) { 419 if (iShowNames || ic.isIgnoreDistances()) { 420 Element instrEl = instructorsEl.addElement("instructor").addAttribute("id", 421 getId("inst", ic.getResourceId())); 422 if (iShowNames) { 423 if (ic.getPuid() != null && ic.getPuid().length() > 0) 424 instrEl.addAttribute("puid", ic.getPuid()); 425 instrEl.addAttribute("name", ic.getName()); 426 if (ic.getType() != null && iShowNames) 427 instrEl.addAttribute("type", ic.getType().toString()); 428 } 429 if (ic.isIgnoreDistances()) { 430 instrEl.addAttribute("ignDist", "true"); 431 } 432 } 433 if (ic.getUnavailabilities() != null) { 434 for (Placement placement: ic.getUnavailabilities()) { 435 Lecture lecture = placement.variable(); 436 Element classEl = classElements.get(lecture.getClassId()); 437 classEl.addElement("instructor").addAttribute("id", getId("inst", ic.getResourceId())).addAttribute("solution", "true"); 438 } 439 } 440 } 441 if (instructorsEl.elements().isEmpty()) 442 root.remove(instructorsEl); 443 444 Element grConstraintsEl = root.addElement("groupConstraints"); 445 for (GroupConstraint gc : getModel().getGroupConstraints()) { 446 Element grEl = grConstraintsEl.addElement("constraint").addAttribute("id", 447 getId("gr", String.valueOf(gc.getId()))); 448 grEl.addAttribute("type", gc.getType().reference()); 449 grEl.addAttribute("pref", gc.getPrologPreference()); 450 for (Lecture l : gc.variables()) { 451 grEl.addElement("class").addAttribute("id", getId("class", l.getClassId())); 452 } 453 } 454 for (SpreadConstraint spread : getModel().getSpreadConstraints()) { 455 Element grEl = grConstraintsEl.addElement("constraint").addAttribute("id", 456 getId("gr", String.valueOf(spread.getId()))); 457 grEl.addAttribute("type", "SPREAD"); 458 grEl.addAttribute("pref", Constants.sPreferenceRequired); 459 if (iShowNames) 460 grEl.addAttribute("name", spread.getName()); 461 for (Lecture l : spread.variables()) { 462 grEl.addElement("class").addAttribute("id", getId("class", l.getClassId())); 463 } 464 } 465 for (Constraint<Lecture, Placement> c : getModel().constraints()) { 466 if (c instanceof MinimizeNumberOfUsedRoomsConstraint) { 467 Element grEl = grConstraintsEl.addElement("constraint").addAttribute("id", 468 getId("gr", String.valueOf(c.getId()))); 469 grEl.addAttribute("type", "MIN_ROOM_USE"); 470 grEl.addAttribute("pref", Constants.sPreferenceRequired); 471 for (Lecture l : c.variables()) { 472 grEl.addElement("class").addAttribute("id", getId("class", l.getClassId())); 473 } 474 } 475 if (c instanceof MinimizeNumberOfUsedGroupsOfTime) { 476 Element grEl = grConstraintsEl.addElement("constraint").addAttribute("id", 477 getId("gr", String.valueOf(c.getId()))); 478 grEl.addAttribute("type", ((MinimizeNumberOfUsedGroupsOfTime) c).getConstraintName()); 479 grEl.addAttribute("pref", Constants.sPreferenceRequired); 480 for (Lecture l : c.variables()) { 481 grEl.addElement("class").addAttribute("id", getId("class", l.getClassId())); 482 } 483 } 484 if (c instanceof IgnoreStudentConflictsConstraint) { 485 Element grEl = grConstraintsEl.addElement("constraint").addAttribute("id", getId("gr", String.valueOf(c.getId()))); 486 grEl.addAttribute("type", IgnoreStudentConflictsConstraint.REFERENCE); 487 grEl.addAttribute("pref", Constants.sPreferenceRequired); 488 for (Lecture l : c.variables()) { 489 grEl.addElement("class").addAttribute("id", getId("class", l.getClassId())); 490 } 491 } 492 } 493 for (ClassLimitConstraint clc : getModel().getClassLimitConstraints()) { 494 Element grEl = grConstraintsEl.addElement("constraint").addAttribute("id", 495 getId("gr", String.valueOf(clc.getId()))); 496 grEl.addAttribute("type", "CLASS_LIMIT"); 497 grEl.addAttribute("pref", Constants.sPreferenceRequired); 498 if (clc.getParentLecture() != null) { 499 grEl.addElement("parentClass").addAttribute("id", getId("class", clc.getParentLecture().getClassId())); 500 } else 501 grEl.addAttribute("courseLimit", String.valueOf(clc.classLimit() - clc.getClassLimitDelta())); 502 if (clc.getClassLimitDelta() != 0) 503 grEl.addAttribute("delta", String.valueOf(clc.getClassLimitDelta())); 504 if (iShowNames) 505 grEl.addAttribute("name", clc.getName()); 506 for (Lecture l : clc.variables()) { 507 grEl.addElement("class").addAttribute("id", getId("class", l.getClassId())); 508 } 509 } 510 for (FlexibleConstraint gc : getModel().getFlexibleConstraints()) { 511 Element flEl = grConstraintsEl.addElement("constraint").addAttribute("id", 512 getId("gr", String.valueOf(gc.getId()))); 513 flEl.addAttribute("reference", gc.getReference()); 514 flEl.addAttribute("owner", gc.getOwner()); 515 flEl.addAttribute("pref", gc.getPrologPreference()); 516 flEl.addAttribute("type", gc.getType().toString()); 517 for (Lecture l : gc.variables()) { 518 flEl.addElement("class").addAttribute("id", getId("class", l.getClassId())); 519 } 520 } 521 522 HashMap<Student, List<String>> students = new HashMap<Student, List<String>>(); 523 for (Lecture lecture : vars) { 524 for (Student student : lecture.students()) { 525 List<String> enrls = students.get(student); 526 if (enrls == null) { 527 enrls = new ArrayList<String>(); 528 students.put(student, enrls); 529 } 530 enrls.add(getId("class", lecture.getClassId())); 531 } 532 } 533 534 Element studentsEl = root.addElement("students"); 535 for (Student student: new TreeSet<Student>(students.keySet())) { 536 Element stEl = studentsEl.addElement("student").addAttribute("id", getId("student", student.getId())); 537 if (iShowNames) { 538 if (student.getAcademicArea() != null) 539 stEl.addAttribute("area", student.getAcademicArea()); 540 if (student.getAcademicClassification() != null) 541 stEl.addAttribute("classification", student.getAcademicClassification()); 542 if (student.getMajor() != null) 543 stEl.addAttribute("major", student.getMajor()); 544 if (student.getCurriculum() != null) 545 stEl.addAttribute("curriculum", student.getCurriculum()); 546 } 547 for (Map.Entry<Long, Double> entry : student.getOfferingsMap().entrySet()) { 548 Long offeringId = entry.getKey(); 549 Double weight = entry.getValue(); 550 Element offEl = stEl.addElement("offering") 551 .addAttribute("id", getId("offering", offeringId.toString())); 552 if (weight.doubleValue() != 1.0) 553 offEl.addAttribute("weight", sStudentWeightFormat.format(weight)); 554 Double priority = student.getPriority(offeringId); 555 if (priority != null) 556 offEl.addAttribute("priority", priority.toString()); 557 } 558 if (iExportStudentSectioning || getModel().nrUnassignedVariables(getAssignment()) == 0 || student.getOfferingsMap().isEmpty()) { 559 List<String> lectures = students.get(student); 560 Collections.sort(lectures); 561 for (String classId : lectures) { 562 stEl.addElement("class").addAttribute("id", classId); 563 } 564 } 565 Map<Long, Set<Lecture>> canNotEnroll = student.canNotEnrollSections(); 566 if (canNotEnroll != null) { 567 for (Set<Lecture> canNotEnrollLects: canNotEnroll.values()) { 568 for (Iterator<Lecture> i3 = canNotEnrollLects.iterator(); i3.hasNext();) { 569 stEl.addElement("prohibited-class") 570 .addAttribute("id", getId("class", (i3.next()).getClassId())); 571 } 572 } 573 } 574 575 if (student.getCommitedPlacements() != null) { 576 for (Placement placement : student.getCommitedPlacements()) { 577 stEl.addElement("class").addAttribute("id", getId("class", placement.variable().getClassId())); 578 } 579 } 580 581 if (student.getInstructor() != null) 582 stEl.addAttribute("instructor", getId("inst", student.getInstructor().getResourceId())); 583 } 584 585 if (getModel().getProperties().getPropertyInt("MPP.GenTimePert", 0) > 0) { 586 Element perturbationsEl = root.addElement("perturbations"); 587 int nrChanges = getModel().getProperties().getPropertyInt("MPP.GenTimePert", 0); 588 List<Lecture> lectures = new ArrayList<Lecture>(); 589 while (lectures.size() < nrChanges) { 590 Lecture lecture = ToolBox.random(getAssignment().assignedVariables()); 591 if (lecture.isCommitted() || lecture.timeLocations().size() <= 1 || lectures.contains(lecture)) 592 continue; 593 Placement placement = getAssignment().getValue(lecture); 594 TimeLocation tl = placement.getTimeLocation(); 595 perturbationsEl.addElement("class").addAttribute("id", getId("class", lecture.getClassId())) 596 .addAttribute("days", sDF[7].format(Long.parseLong(Integer.toBinaryString(tl.getDayCode())))) 597 .addAttribute("start", String.valueOf(tl.getStartSlot())).addAttribute("length", 598 String.valueOf(tl.getLength())); 599 lectures.add(lecture); 600 } 601 } 602 603 for (Map.Entry<Long, String> entry : depts.entrySet()) { 604 Long id = entry.getKey(); 605 String name = entry.getValue(); 606 if (iShowNames) { 607 departmentsEl.addElement("department").addAttribute("id", getId("dept", id.toString())).addAttribute( 608 "name", name); 609 } 610 } 611 if (departmentsEl.elements().isEmpty()) 612 root.remove(departmentsEl); 613 614 if (iShowNames) { 615 Progress.getInstance(getModel()).save(root); 616 617 try { 618 getSolver().getClass().getMethod("save", new Class[] { Element.class }).invoke(getSolver(), 619 new Object[] { root }); 620 } catch (Exception e) { 621 } 622 } 623 624 FileOutputStream fos = null; 625 try { 626 fos = new FileOutputStream(outFile); 627 (new XMLWriter(fos, OutputFormat.createPrettyPrint())).write(document); 628 fos.flush(); 629 fos.close(); 630 fos = null; 631 } finally { 632 try { 633 if (fos != null) 634 fos.close(); 635 } catch (IOException e) { 636 } 637 } 638 639 if (iConvertIds) 640 iIdConvertor.save(); 641 } 642}