List of usage examples for java.util TreeSet contains
public boolean contains(Object o)
From source file:ca.uvic.cs.tagsea.statistics.svn.jobs.SVNCommentScanningJob.java
protected IStatus run(IProgressMonitor monitor) { IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); ISVNClientAdapter client;/*from w w w . j a v a 2 s . com*/ List svnResources = new LinkedList(); for (int i = 0; i < projects.length; i++) { IProject project = projects[i]; ISVNLocalResource r = SVNWorkspaceRoot.getSVNResourceFor(project); try { if (r != null && r.isManaged()) { svnResources.add(r); } } catch (SVNException e) { //do nothing, continue t the next } } monitor.beginTask("Scanning subversion projects...", svnResources.size() * 1000000); IPath state = SVNStatistics.getDefault().getStateLocation(); File tempdir = state.append("temp").toFile(); if (!tempdir.isDirectory()) { if (tempdir.exists()) { tempdir.delete(); } tempdir.mkdir(); } deleteTemps(tempdir); for (Iterator it = svnResources.iterator(); it.hasNext();) { ISVNLocalResource svnProject = (ISVNLocalResource) it.next(); //used to make sure that we don't repeat old comments. HashMap fileCommentsMap = new HashMap(); fileEntryMap = new HashMap(); monitor.subTask("Getting project information for " + svnProject.getName()); //create a temp file for each project. They will be uploaded //to the server. String projectName = svnProject.getName(); //names are guaranteed unique try { ISVNRemoteResource remote = null; for (int tries = 0; remote == null && tries < 10; tries++) { try { remote = svnProject.getLatestRemoteResource(); } catch (Exception e) { } ; if (remote == null) { SVNStatistics.getDefault().getLog().log(new Status(IStatus.WARNING, SVNStatistics.PLUGIN_ID, IStatus.WARNING, "could not get remote resource for " + svnProject.getName() + "... trying again.", null)); try { // @tag tagsea.bug.subclipse : it seems that sublcipse has a synchronization problem. Wait a little while and try again. Thread.sleep(1000); } catch (InterruptedException e1) { return new Status(IStatus.ERROR, SVNProviderPlugin.ID, IStatus.ERROR, "Could not communicate with remote resource.", null); } } } if (remote == null) { SVNStatistics.getDefault().getLog().log(new Status(IStatus.ERROR, SVNStatistics.PLUGIN_ID, 0, "Could not get a remote resource", null)); monitor.worked(1000000); continue; } ISVNRepositoryLocation repository = remote.getRepository(); client = repository.getSVNClient(); // @tag tagsea.statistics.enhance : It seems best to use this password callback because that way, the passwords can be saved with the workspace. But, it might be confusing to see for the first time. client.addPasswordCallback(SVNProviderPlugin.getPlugin().getSvnPromptUserPassword()); SVNRevision.Number revision = remote.getLastChangedRevision(); long revNum = revision.getNumber(); int revisionWork = 1000000; ILogEntry[] entries; try { entries = remote.getLogEntries(new NullProgressMonitor()); } catch (TeamException e1) { monitor.worked(revisionWork); e1.printStackTrace(); continue; } if (revNum > 0) { revisionWork = 1000000 / (int) revNum; } for (int ei = 0; ei < entries.length; ei++) { ILogEntry entry = entries[ei]; revision = entry.getRevision(); File tempFile = state.append( projectName + "." + getDateString() + "." + revision.getNumber() + ".comments.txt") .toFile(); if (tempFile.exists()) { tempFile.delete(); } try { tempFile.createNewFile(); } catch (IOException e) { //skip to the next one. continue; } PrintStream out; try { out = new PrintStream(tempFile); } catch (IOException e) { continue; } out.println(remote.getUrl() + " Revision:" + revision.getNumber()); monitor.subTask("Finding java resources: " + svnProject.getName() + "..."); SubProgressMonitor revMonitor = new SubProgressMonitor(monitor, revisionWork); if (monitor.isCanceled()) { return Status.CANCEL_STATUS; } monitor.subTask("temporarily checking out " + svnProject.getName() + "..."); SubProgressMonitor subPm = new SubProgressMonitor(revMonitor, 10); try { OperationManager.getInstance().beginOperation(client, new OperationProgressNotifyListener(subPm)); client.checkout(remote.getUrl(), new File(tempdir, svnProject.getName()), revision, true); } catch (SVNClientException e) { //I wish that there were a better way to do this, but it seem that we //have to just keep decrementing it. revMonitor.done(); revNum--; revision = new SVNRevision.Number(revNum); continue; } finally { OperationManager.getInstance().endOperation(); subPm.done(); } if (monitor.isCanceled()) { return Status.CANCEL_STATUS; } List files = findJavaFiles(tempdir); int work = 0; if (files.size() > 0) { work = (revisionWork - 20) / files.size(); for (Iterator fit = files.iterator(); fit.hasNext();) { File file = (File) fit.next(); monitor.subTask("Scanning java file...."); TreeSet commentSet = (TreeSet) fileCommentsMap.get(file.getAbsolutePath()); if (commentSet == null) { commentSet = new TreeSet(); fileCommentsMap.put(file.getAbsolutePath(), commentSet); } FileReader reader = new FileReader(file); StringBuilder builder = new StringBuilder(); char[] buffer = new char[1024]; int read = 0; while ((read = reader.read(buffer)) >= 0) { builder.append(buffer, 0, read); } reader.close(); ISVNAnnotations ann = null; try { //get blame information. List fileLogs = getLogEntries(file, client, repository); //don't do extra work if this file doesn't have a log for this revision. if (!checkRevision(fileLogs, revision)) { monitor.worked(work); //System.out.println("Skipped " + file.getAbsolutePath() + " revision " + revision.getNumber()); continue; } ann = client.annotate(file, revision, revision); } catch (SVNClientException e) { } catch (TeamException e) { } if (monitor.isCanceled()) { return Status.CANCEL_STATUS; } SubProgressMonitor scanMonitor = new SubProgressMonitor(revMonitor, work); Stats s = SimpleJavaCodeScanner.scan(builder.toString(), scanMonitor); if (monitor.isCanceled()) { return Status.CANCEL_STATUS; } monitor.worked(work); out.println("New/Changed Tags:"); for (int ci = 0; ci < s.TAGS.length; ci++) { Comment c = s.TAGS[ci]; if (!commentSet.contains(c)) { commentSet.add(c); String author = getAuthor(c, ann); out.println(c.toString() + "\tauthor=" + author); } } out.println("New/Changed Tasks:"); for (int ci = 0; ci < s.TASKS.length; ci++) { Comment c = s.TASKS[ci]; if (!commentSet.contains(c)) { commentSet.add(c); String author = getAuthor(c, ann); out.println(c.toString() + "\tauthor=" + author); } } out.println("New/Changed Other:"); for (int ci = 0; ci < s.NONTAGS.length; ci++) { Comment c = s.NONTAGS[ci]; if (!commentSet.contains(c)) { commentSet.add(c); String author = getAuthor(c, ann); out.println(c.toString() + "\tauthor=" + author); } } if (monitor.isCanceled()) { return Status.CANCEL_STATUS; } } } if (work == 0) { revMonitor.worked(revisionWork - 10); } monitor.subTask("Sending and Deleting temporary files..."); out.close(); sendFile(tempFile); deleteTemps(tempdir); if (monitor.isCanceled()) { return Status.CANCEL_STATUS; } revMonitor.done(); monitor.worked(revisionWork - 20); } } catch (SVNException e) { return new Status(IStatus.ERROR, SVNStatistics.PLUGIN_ID, 0, e.getMessage(), e); } catch (IOException e) { return new Status(IStatus.ERROR, SVNStatistics.PLUGIN_ID, 0, e.getMessage(), e); } } return Status.OK_STATUS; }
From source file:org.gvsig.framework.web.service.impl.OGCInfoServiceImpl.java
/** * Recursive method to add list layers get by the WMSServer into tree list * * @param children Represents child layers of parentNode * @param tree Tree of layers//from w w w.ja v a2 s. com * @param crs CRS that must have the layers to add these to the tree * @param parentNode Represents parent layer * @param layersMap Represents the map that contains the layers obtained * @param isCalledByWizard Indicate if the method is called by the wizard */ private void generateWMSChildrenNodes(ArrayList<WMSLayer> children, List<TreeNode> tree, TreeSet<String> listCrs, TreeNode parentNode, Map<String, org.gvsig.framework.web.ogc.WMSLayer> layersMap, WMSInfo wmsInfo) { for (WMSLayer layerChild : children) { // get crs (srs) (belong to layer) Vector crsVector = layerChild.getAllSrs(); // Only get the layers with have crs parameter or if crs is null if (listCrs.isEmpty() || !Collections.disjoint(crsVector, listCrs)) { ArrayList<WMSLayer> layerChildChildren = layerChild.getChildren(); TreeNode layerChildNode = new TreeNode(layerChild.getName()); layerChildNode.setTitle(layerChild.getTitle()); // Get the children and their information if (layerChildChildren.isEmpty()) { layerChildNode.setFolder(false); // Add layer to layer map org.gvsig.framework.web.ogc.WMSLayer wmsLayer = new org.gvsig.framework.web.ogc.WMSLayer(); TreeSet<String> crsSet = new TreeSet<String>(); crsSet.addAll(layerChild.getAllSrs()); wmsLayer.setCrs(crsSet); List<WMSStyle> wmsStyles = createListWMSStyles(layerChild.getStyles()); wmsLayer.setStyles(wmsStyles); wmsLayer.setTitle(layerChild.getTitle()); wmsLayer.setName(layerChild.getName()); layersMap.put(layerChild.getName(), wmsLayer); // add to wmsinfo the layers supported by this layer TreeSet<String> crsSupported = wmsInfo.getCrsSupported(); crsSupported.addAll(layerChild.getAllSrs()); wmsInfo.setCrsSupported(crsSupported); //create one child for each crs of the layer if (listCrs.isEmpty() || listCrs.size() > 1) { for (String crs : crsSet) { if (StringUtils.isNotEmpty(crs) && (listCrs.isEmpty() || listCrs.contains(crs))) { TreeNode crsNode = new TreeNode(crs); crsNode.setHideCheckbox(true); crsNode.setUnselectable(true); crsNode.setIconclass(" "); layerChildNode.addChild(crsNode); } } } } else { layerChildNode.setFolder(true); layerChildNode.setExpanded(true); generateWMSChildrenNodes(layerChildChildren, tree, listCrs, layerChildNode, layersMap, wmsInfo); } parentNode.addChild(layerChildNode); } } }
From source file:org.unitime.timetable.action.PersonalizedExamReportAction.java
public PdfWebTable getStudentConflits(boolean html, TreeSet<ExamAssignmentInfo> exams, Student student) { String nl = (html ? "<br>" : "\n"); boolean showBackToBack = ApplicationProperty.ExaminationReportsStudentBackToBacks.isTrue(); PdfWebTable table = new PdfWebTable(6, student.getSession().getLabel() + " Examination Conflicts" + (showBackToBack ? " and/or Back-To-Back Examinations" : "") + " for " + student.getName(DepartmentalInstructor.sNameFormatLastFist), "personalSchedule.do?o3=%%&q=" + QueryEncoderBackend .encode(student.getExternalUniqueId() + ":" + student.getSession().getUniqueId()), new String[] { "Type", "Class / Course", "Date", "Time", "Room", "Distance" }, new String[] { "left", "left", "left", "left", "left", "left" }, new boolean[] { true, true, true, true, true, true }); table.setRowStyle("white-space:nowrap"); String noRoom = ApplicationProperty.ExaminationsNoRoomText.value(); for (ExamAssignmentInfo exam : exams) { for (DirectConflict conflict : exam.getDirectConflicts()) { if (conflict.getOtherExam() != null && exam.compareTo(conflict.getOtherExam()) >= 0 && exams.contains(conflict.getOtherExam())) continue; if (!conflict.getStudents().contains(student.getUniqueId())) continue; String classes = "", date = "", time = "", room = ""; boolean firstSection = true; for (ExamSectionInfo section : exam.getSectionsIncludeCrosslistedDummies()) { if (!section.getStudentIds().contains(student.getUniqueId())) continue; if (classes.length() > 0) { classes += nl;/* w ww. ja v a 2 s . c o m*/ date += nl; time += nl; room += nl; } classes += section.getName(); if (firstSection) { date += exam.getDate(false); time += exam.getTime(false); room += (exam.getNrRooms() == 0 ? noRoom : html ? exam.getRoomsNameWithHint(false, ", ") : exam.getRoomsName(false, ", ")); } firstSection = false; } firstSection = true; if (conflict.getOtherExam() != null) { for (ExamSectionInfo section : conflict.getOtherExam().getSectionsIncludeCrosslistedDummies()) { if (!section.getStudentIds().contains(student.getUniqueId())) continue; if (classes.length() > 0) { classes += nl; date += nl; time += nl; room += nl; } classes += section.getName(); if (firstSection) { room += (conflict.getOtherExam().getNrRooms() == 0 ? noRoom : conflict.getOtherExam().getRoomsName(false, ", ")); } firstSection = false; } } else if (conflict.getOtherEventId() != null) { classes += nl; date += nl; time += nl; room += nl; classes += conflict.getOtherEventName(); room += conflict.getOtherEventRoom(); //date += conflict.getOtherEventDate(); time += conflict.getOtherEventTime(); } table.addLine( new String[] { (html ? "<font color='" + PreferenceLevel.prolog2color("P") + "'>" : "") + (conflict.getOtherEventId() != null ? conflict.isOtherClass() ? "Class" : "Event" : "Direct") + (html ? "</font>" : ""), classes, date, time, room, "" }, new Comparable[] { new MultiComparable(-exam.getExamType().getType(), 0, exam, 0), new MultiComparable(-exam.getExamType().getType(), exam, exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getPeriodOrd(), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getPeriod().getStartSlot(), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getRoomsName(":"), exam, 0), new MultiComparable(-exam.getExamType().getType(), -1.0, exam, 0) }); } if (showBackToBack) for (BackToBackConflict conflict : exam.getBackToBackConflicts()) { if (exam.compareTo(conflict.getOtherExam()) >= 0 && exams.contains(conflict.getOtherExam())) continue; if (!conflict.getStudents().contains(student.getUniqueId())) continue; String classes = "", date = "", time = "", room = ""; boolean firstSection = true; for (ExamSectionInfo section : exam.getSectionsIncludeCrosslistedDummies()) { if (!section.getStudentIds().contains(student.getUniqueId())) continue; if (classes.length() > 0) { classes += nl; date += nl; time += nl; room += nl; } classes += section.getName(); if (firstSection) { date += exam.getDate(false); time += exam.getTime(false); room += (exam.getNrRooms() == 0 ? noRoom : exam.getRoomsName(false, ", ")); ; } firstSection = false; } firstSection = true; for (ExamSectionInfo section : conflict.getOtherExam().getSectionsIncludeCrosslistedDummies()) { if (!section.getStudentIds().contains(student.getUniqueId())) continue; if (classes.length() > 0) { classes += nl; date += nl; time += nl; room += nl; } classes += section.getName(); if (firstSection) { time += conflict.getOtherExam().getTime(false); room += (conflict.getOtherExam().getNrRooms() == 0 ? noRoom : conflict.getOtherExam().getRoomsName(false, ", ")); } firstSection = false; } table.addLine( new String[] { (html ? "<font color='" + PreferenceLevel.prolog2color("1") + "'>" : "") + "Back-To-Back" + (html ? "</font>" : ""), classes, date, time, room, (int) (conflict.getDistance() * 10.0) + " m" }, new Comparable[] { new MultiComparable(-exam.getExamType().getType(), 2, exam, 0), new MultiComparable(-exam.getExamType().getType(), exam, exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getPeriodOrd(), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getPeriod().getStartSlot(), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getRoomsName(":"), exam, 0), new MultiComparable(-exam.getExamType().getType(), conflict.getDistance(), exam, 0) }); } conflicts: for (MoreThanTwoADayConflict conflict : exam.getMoreThanTwoADaysConflicts()) { for (ExamAssignment other : conflict.getOtherExams()) if (exam.compareTo(other) >= 0 && exams.contains(other)) continue conflicts; if (!conflict.getStudents().contains(student.getUniqueId())) continue; String classes = "", date = "", time = "", room = ""; boolean firstSection = true; for (ExamSectionInfo section : exam.getSectionsIncludeCrosslistedDummies()) { if (!section.getStudentIds().contains(student.getUniqueId())) continue; if (classes.length() > 0) { classes += nl; date += nl; time += nl; room += nl; } classes += section.getName(); if (firstSection) { date += exam.getDate(false); time += exam.getTime(false); room += (exam.getNrRooms() == 0 ? noRoom : exam.getRoomsName(false, ", ")); } firstSection = false; } for (ExamAssignment other : conflict.getOtherExams()) { firstSection = true; for (ExamSectionInfo section : other.getSectionsIncludeCrosslistedDummies()) { if (!section.getStudentIds().contains(student.getUniqueId())) continue; if (classes.length() > 0) { classes += nl; date += nl; time += nl; room += nl; } classes += section.getName(); if (firstSection) { time += other.getTime(false); room += (other.getNrRooms() == 0 ? noRoom : other.getRoomsName(false, ", ")); } firstSection = false; } } table.addLine( (sessionContext.hasPermission(exam, Right.ExaminationDetail) ? "onClick=\"document.location='examDetail.do?examId=" + exam.getExamId() + "';\"" : ""), new String[] { (html ? "<font color='" + PreferenceLevel.prolog2color("2") + "'>" : "") + (html ? ">" : "") + "2 A Day" + (html ? "</font>" : ""), classes, date, time, room, "" }, new Comparable[] { new MultiComparable(-exam.getExamType().getType(), 1, exam, 0), new MultiComparable(-exam.getExamType().getType(), exam, exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getPeriodOrd(), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getPeriod().getStartSlot(), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getRoomsName(":"), exam, 0), new MultiComparable(-exam.getExamType().getType(), -1.0, exam, 0) }, exam.getExamId().toString()); } } table.setWebTableTweakStyle(new WebTableTweakStyle() { public String getStyleHtml(WebTableLine current, WebTableLine next, int order) { if (next != null && ((MultiComparable) current.getOrderBy()[0]).getContent()[0] .compareTo(((MultiComparable) next.getOrderBy()[0]).getContent()[0]) != 0) return "border-bottom: rgb(81,81,81) 1px dashed"; return null; } }); return table; }
From source file:org.unitime.timetable.action.PersonalizedExamReportAction.java
public PdfWebTable getStudentConflits(boolean html, TreeSet<ExamAssignmentInfo> exams, DepartmentalInstructor instructor) { String nl = (html ? "<br>" : "\n"); PdfWebTable table = new PdfWebTable(8, instructor.getDepartment().getSession().getLabel() + " Examination Conflicts for " + instructor.getName(DepartmentalInstructor.sNameFormatLastFist), "personalSchedule.do?o5=%%&q=" + QueryEncoderBackend.encode(instructor.getExternalUniqueId() + ":" + instructor.getDepartment().getSession().getUniqueId()), new String[] { "Name", "Type", "Class / Course", "Enrollment", "Seating" + nl + "Type", "Date", "Time", "Room" }, new String[] { "left", "left", "left", "right", "center", "left", "left", "left" }, new boolean[] { true, true, true, true, true, true, true, true }); table.setRowStyle("white-space:nowrap"); String noRoom = ApplicationProperty.ExaminationsNoRoomText.value(); table.setBlankWhenSame(true);/*from www.j a v a 2 s. c o m*/ for (ExamAssignmentInfo exam : exams) { for (DirectConflict conflict : exam.getDirectConflicts()) { if (conflict.getOtherExam() != null && exam.compareTo(conflict.getOtherExam()) >= 0 && exams.contains(conflict.getOtherExam())) continue; for (Long studentId : conflict.getStudents()) { Student student = new StudentDAO().get(studentId); String id = student.getExternalUniqueId(); String name = student.getName(DepartmentalInstructor.sNameFormatLastFist); String classes = "", enrollment = "", seating = "", date = "", time = "", room = ""; boolean firstSection = true; for (ExamSectionInfo section : exam.getSectionsIncludeCrosslistedDummies()) { if (!section.getStudentIds().contains(studentId)) continue; if (classes.length() > 0) { classes += nl; enrollment += nl; seating += nl; date += nl; time += nl; room += nl; } classes += section.getName(); enrollment += String.valueOf(section.getNrStudents()); if (firstSection) { seating += Exam.sSeatingTypes[exam.getSeatingType()]; date += exam.getDate(false); time += exam.getTime(false); room += (exam.getNrRooms() == 0 ? noRoom : exam.getRoomsName(false, ", ")); } firstSection = false; } firstSection = true; if (conflict.getOtherExam() != null) { for (ExamSectionInfo section : conflict.getOtherExam() .getSectionsIncludeCrosslistedDummies()) { if (!section.getStudentIds().contains(studentId)) continue; if (classes.length() > 0) { classes += nl; enrollment += nl; seating += nl; date += nl; time += nl; room += nl; } classes += section.getName(); enrollment += String.valueOf(section.getNrStudents()); if (firstSection) { seating += Exam.sSeatingTypes[exam.getSeatingType()]; room += (conflict.getOtherExam().getNrRooms() == 0 ? noRoom : conflict.getOtherExam().getRoomsName(false, ", ")); } firstSection = false; } } else if (conflict.getOtherEventId() != null) { classes += nl; enrollment += nl; seating += nl; date += nl; time += nl; room += nl; classes += conflict.getOtherEventName(); enrollment += conflict.getOtherEventSize(); seating += conflict.isOtherClass() ? "Class" : "Event"; room += conflict.getOtherEventRoom(); //date += conflict.getOtherEventDate(); time += conflict.getOtherEventTime(); } table.addLine( new String[] { name, (html ? "<font color='" + PreferenceLevel.prolog2color("P") + "'>" : "") + "Direct" + (html ? "</font>" : ""), classes, enrollment, seating, date, time, room }, new Comparable[] { new MultiComparable(-exam.getExamType().getType(), name, id, exam, 0), new MultiComparable(-exam.getExamType().getType(), 0, exam, 0), new MultiComparable(-exam.getExamType().getType(), exam, exam, 0), new MultiComparable(-exam.getExamType().getType(), -exam.getNrStudents() - (conflict.getOtherExam() == null ? 0 : conflict.getOtherExam().getNrStudents()), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getExamTypeLabel(), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getPeriodOrd(), exam, 0), new MultiComparable(-exam.getExamType().getType(), (exam.getPeriod() == null ? -1 : exam.getPeriod().getStartSlot()), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getRoomsName(":"), exam, 0) }); } } /* for (BackToBackConflict conflict : exam.getBackToBackConflicts()) { if (exam.compareTo(conflict.getOtherExam())>=0 && exams.contains(conflict.getOtherExam())) continue; for (Long studentId : conflict.getStudents()) { Student student = new StudentDAO().get(studentId); String id = student.getExternalUniqueId(); String name = student.getName(DepartmentalInstructor.sNameFormatLastFist); String classes = "", enrollment = "", seating = "", date = "", time = "", room = "", distance = "", blank=""; boolean firstSection = true; for (ExamSectionInfo section : exam.getSectionsIncludeCrosslistedDummies()) { if (!section.getStudentIds().contains(studentId)) continue; if (classes.length()>0) { blank+=nl; classes += nl; enrollment += nl; seating += nl; date += nl; time += nl; room += nl; distance += nl; } classes += section.getName(); enrollment += String.valueOf(section.getNrStudents()); if (firstSection) { seating += Exam.sSeatingTypes[exam.getSeatingType()]; date += exam.getDate(false); time += exam.getTime(false); room += (exam.getNrRooms()==0?noRoom:exam.getRoomsName(false,", ")); } firstSection = false; } firstSection = true; for (ExamSectionInfo section : conflict.getOtherExam().getSectionsIncludeCrosslistedDummies()) { if (!section.getStudentIds().contains(studentId)) continue; if (classes.length()>0) { blank+=nl; classes += nl; enrollment += nl; seating += nl; date += nl; time += nl; room += nl; distance += nl; } classes += section.getName(); enrollment += String.valueOf(section.getNrStudents()); if (firstSection) { seating += Exam.sSeatingTypes[exam.getSeatingType()]; time += conflict.getOtherExam().getTime(false); room += (conflict.getOtherExam().getNrRooms()==0?noRoom:conflict.getOtherExam().getRoomsName(false,", ")); } firstSection = false; } table.addLine( new String[] { name, (html?"<font color='"+PreferenceLevel.prolog2color("1")+"'>":"")+"Back-To-Back"+(html?"</font>":""), classes, enrollment, seating, date, time, room }, new Comparable[] { new MultiComparable(-exam.getExamType(), name, id, exam, 0), new MultiComparable(-exam.getExamType(), 2, exam, 0), new MultiComparable(-exam.getExamType(), exam, exam, 0), new MultiComparable(-exam.getExamType(), -exam.getNrStudents()-conflict.getOtherExam().getNrStudents(), exam, 0), new MultiComparable(-exam.getExamType(), exam.getExamType(), exam, 0), new MultiComparable(-exam.getExamType(), exam.getPeriodOrd(), exam, 0), new MultiComparable(-exam.getExamType(), exam.getPeriod().getStartSlot(), exam, 0), new MultiComparable(-exam.getExamType(), exam.getRoomsName(":"), exam, 0) }); } } */ conflicts: for (MoreThanTwoADayConflict conflict : exam.getMoreThanTwoADaysConflicts()) { for (ExamAssignment other : conflict.getOtherExams()) if (exam.compareTo(other) >= 0 && exams.contains(other)) continue conflicts; for (Long studentId : conflict.getStudents()) { Student student = new StudentDAO().get(studentId); String id = student.getExternalUniqueId(); String name = student.getName(DepartmentalInstructor.sNameFormatLastFist); String classes = "", enrollment = "", seating = "", date = "", time = "", room = ""; int nrStudents = exam.getNrStudents(); boolean firstSection = true; for (ExamSectionInfo section : exam.getSectionsIncludeCrosslistedDummies()) { if (!section.getStudentIds().contains(studentId)) continue; if (classes.length() > 0) { classes += nl; enrollment += nl; seating += nl; date += nl; time += nl; room += nl; } classes += section.getName(); enrollment += String.valueOf(section.getNrStudents()); if (firstSection) { seating += Exam.sSeatingTypes[exam.getSeatingType()]; date += exam.getDate(false); time += exam.getTime(false); room += (exam.getNrRooms() == 0 ? noRoom : exam.getRoomsName(false, ", ")); } firstSection = false; } for (ExamAssignment other : conflict.getOtherExams()) { firstSection = true; nrStudents += other.getNrStudents(); for (ExamSectionInfo section : other.getSectionsIncludeCrosslistedDummies()) { if (!section.getStudentIds().contains(studentId)) continue; if (classes.length() > 0) { classes += nl; enrollment += nl; seating += nl; date += nl; time += nl; room += nl; } classes += section.getName(); enrollment += String.valueOf(section.getNrStudents()); if (firstSection) { seating += Exam.sSeatingTypes[exam.getSeatingType()]; time += other.getTime(false); room += (other.getNrRooms() == 0 ? noRoom : other.getRoomsName(false, ", ")); } firstSection = false; } } table.addLine( new String[] { name, (html ? "<font color='" + PreferenceLevel.prolog2color("2") + "'>" : "") + (html ? ">" : "") + "2 A Day" + (html ? "</font>" : ""), classes, enrollment, seating, date, time, room }, new Comparable[] { new MultiComparable(-exam.getExamType().getType(), name, id, exam, 0), new MultiComparable(-exam.getExamType().getType(), 1, exam, 0), new MultiComparable(-exam.getExamType().getType(), exam, exam, 0), new MultiComparable(-exam.getExamType().getType(), -nrStudents, exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getExamTypeLabel(), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getPeriodOrd(), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getPeriod().getStartSlot(), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getRoomsName(":"), exam, 0), }); } } } table.setWebTableTweakStyle(new WebTableTweakStyle() { public String getStyleHtml(WebTableLine current, WebTableLine next, int order) { if (next != null && ((MultiComparable) current.getOrderBy()[0]).getContent()[0] .compareTo(((MultiComparable) next.getOrderBy()[0]).getContent()[0]) != 0) return "border-bottom: rgb(81,81,81) 1px solid"; return null; } }); return table; }
From source file:edu.duke.cs.osprey.ematrix.epic.SeriesFitter.java
static double[] fitSeriesIterative(DoubleMatrix1D[] samp, double trueVals[], double weights[], double lambda, boolean includeConst, int order, double bCutoffs[], double bCutoffs2[], int PCOrder, boolean isPC[]) { long startTime = System.currentTimeMillis(); System.out.println("Starting fitSeriesIterative..."); int numSamples = samp.length; int nd = samp[0].size(); if (bCutoffs.length == 1) {//single bCutoff to be used double bCutoff = bCutoffs[0]; bCutoffs = new double[numSamples]; Arrays.fill(bCutoffs, bCutoff); }/*from ww w . j ava 2 s . c o m*/ //now bCutoffs has a cutoff for each sample int numParams = getNumParams(nd, includeConst, order); if (PCOrder > order) {//add in parameters for PC orders int numPCs = countTrue(isPC); for (int n = order + 1; n <= PCOrder; n++) numParams += getNumParamsForOrder(numPCs, n); } //now set up the data for the iterative fits //samples can be turned on and off using fitWeights //we will need to create two entries for samples with trueVals between bCutoff and bCutoff2 //since they may be turned on either to penalize deviation from bCutoff or from trueVal //first entry for each entry will penalize deviation from bCutoff if trueVal>=bCutoff //secondEntries are for trueVals between bCutoff and bCutoff2 ArrayList<Integer> secondEntries = new ArrayList<>();//list of samples needing second entry HashMap<Integer, Integer> revSecondEntries = new HashMap<>();//reverse lookup for (int s = 0; s < numSamples; s++) { if ((trueVals[s] >= bCutoffs[s]) && (trueVals[s] < bCutoffs2[s])) {//as in isRestraintActive revSecondEntries.put(s, secondEntries.size()); secondEntries.add(s); } } int numRestraints = numSamples + secondEntries.size(); //data for basic least-squares fits DoubleMatrix1D[] fitSamp = new DoubleMatrix1D[numRestraints]; double fitTrueVals[] = new double[numRestraints]; double fitWeights[] = new double[numRestraints]; for (int s = 0; s < numSamples; s++) {//"normal" entries fitSamp[s] = samp[s]; if (trueVals[s] >= bCutoffs[s]) { fitWeights[s] = 0; fitTrueVals[s] = bCutoffs[s]; } else { fitWeights[s] = weights[s]; fitTrueVals[s] = trueVals[s]; } } for (int s2 = 0; s2 < secondEntries.size(); s2++) { fitSamp[numSamples + s2] = samp[secondEntries.get(s2)]; fitWeights[numSamples + s2] = 0; fitTrueVals[numSamples + s2] = trueVals[secondEntries.get(s2)]; } //Initial guess of set P is all points with trueVals[s] >= bCutoff //that is, all points that have possible series values that make the restraint inactive boolean done = false; double coeffs[] = null; double meanResidual = 0, weightSum = 0; double prevResid = Double.POSITIVE_INFINITY; double oldCoeffs[] = null; double oldSerVals[] = new double[numSamples];//values of series at each sample, for previous iteration //preallocating to all infinity because all trueVals[s]>=bCutoff points start outside P Arrays.fill(oldSerVals, Double.POSITIVE_INFINITY); //for updating boolean firstFit = true;//first fit is not an update DoubleMatrix1D c = DoubleFactory1D.dense.make(numParams);//matrices we update (used in fit) DoubleMatrix2D M = DoubleFactory2D.dense.make(numParams, numParams); double oldFitWeights[] = null; //double fitWeightsCheck[] = fitWeights.clone();//DEBUG!!! while (!done) { if (firstFit) { coeffs = fitSeries(fitSamp, fitTrueVals, fitWeights, lambda, includeConst, order, PCOrder, isPC, false, c, M); firstFit = false; } else { double weightDiffs[] = fitWeights.clone(); for (int s = 0; s < numRestraints; s++) weightDiffs[s] -= oldFitWeights[s]; coeffs = fitSeries(fitSamp, fitTrueVals, weightDiffs, lambda, includeConst, order, PCOrder, isPC, true, c, M); //DEBUG!!! /* for(int s=0; s<numRestraints; s++){ fitWeightsCheck[s] += weightDiffs[s]; if(fitWeightsCheck[s] != fitWeights[s]){ int cefAO = 1111; } } double checkCoeffs[] = fitSeries(fitSamp, fitTrueVals, fitWeights, lambda, includeConst, order, PCOrder, isPC, false, null, null); for(int a=0; a<coeffs.length; a++){ if(Math.abs(checkCoeffs[a]-coeffs[a])>1e-10){ int abc=123; } } DoubleMatrix1D c2 = DoubleFactory1D.dense.make(numParams); DoubleMatrix2D M2 = DoubleFactory2D.dense.make(numParams,numParams); double checkCoeffs2[] = fitSeries(fitSamp, fitTrueVals, fitWeights, lambda, includeConst, order, PCOrder, isPC, false, c2, M2); for(int a=0; a<coeffs.length; a++){ if(Math.abs(checkCoeffs2[a]-coeffs[a])>1e-10){ int abc=123; } }*/ //DEBUG!!! } oldFitWeights = fitWeights.clone(); done = true; ArrayList<SampleCutoffCrossing> scc = new ArrayList<SampleCutoffCrossing>(); meanResidual = 0; weightSum = 0; //boolean doneNoTol = true, done2 = true;//DEBUG!!! //values of series at each sample, based on coeffs double serVals[] = new double[numSamples]; for (int s = 0; s < numSamples; s++) { serVals[s] = evalSeries(coeffs, samp[s], nd, includeConst, order, PCOrder, isPC); //If each series value is below or above bcutoff according to whether the //coeffs were generated by fitting with or without that value included //(respectively), then we have found a local and thus the global minimum //in this quadratic piece of the objective function, so we're done //check for doneness first, using tolerance //i.e. are coeffs (derived from fitWeights) consistent with fitWeights, //within numerical error? If so we have a global minimum if (trueVals[s] >= bCutoffs[s]) { if (fitWeights[s] > 0) { if (!isRestraintTypeActive(trueVals[s], serVals[s], bCutoffs[s], bCutoffs2[s], false, -1e-6)) done = false;//fitWeights penalizing deviation from bCutoff, and this isn't right at coeffs } else { boolean secondRestraintOn = revSecondEntries.containsKey(s); if (secondRestraintOn) secondRestraintOn = (fitWeights[numSamples + revSecondEntries.get(s)] > 0); if (secondRestraintOn) { if (!isRestraintTypeActive(trueVals[s], serVals[s], bCutoffs[s], bCutoffs2[s], true, -1e-6)) done = false;//fitWeights penalizing deviation from trueVal, and this isn't right at coeffs } else {//restraints currently off if (isRestraintActive(trueVals[s], serVals[s], bCutoffs[s], bCutoffs2[s], 1e-6)) done = false;//a restraint should be on at coeffs } } } //for trueVals below bCutoff, restraints don't turn on and off //DEBUG!!!!! //trying to calculate done w/o tol /* if( trueVals[s]>=bCutoffs[s] ){ if(fitWeights[s]>0){ if(!isRestraintTypeActive(trueVals[s],serVals[s],bCutoffs[s],bCutoffs2[s],false,0)) doneNoTol = false;//fitWeights penalizing deviation from bCutoff, and this isn't right at coeffs } else { boolean secondRestraintOn = revSecondEntries.containsKey(s); if(secondRestraintOn) secondRestraintOn = (fitWeights[numSamples+revSecondEntries.get(s)]>0); if(secondRestraintOn){ if(!isRestraintTypeActive(trueVals[s],serVals[s],bCutoffs[s],bCutoffs2[s],true,0)) doneNoTol = false;//fitWeights penalizing deviation from trueVal, and this isn't right at coeffs } else {//restraints currently off if(isRestraintActive(trueVals[s],serVals[s],bCutoffs[s],bCutoffs2[s],0)) doneNoTol = false;//a restraint should be on at coeffs } } } //OK now done2 will be calculated and should be the same but will be calculated like //the weight changes below if( trueVals[s]>=bCutoffs[s] ){ if(isRestraintTypeActive(trueVals[s],serVals[s],bCutoffs[s],bCutoffs2[s],false)){ //activate penalty for deviating from bCutoff if(fitWeights[s]!=weights[s]) done2 = false; if(revSecondEntries.containsKey(s)){ if(fitWeights[numSamples+revSecondEntries.get(s)]!=0) done2 = false; } } else if(isRestraintTypeActive(trueVals[s],serVals[s],bCutoffs[s],bCutoffs2[s],true)){ //activate penalty for deviating from trueVal if(fitWeights[s] != 0) done2 = false; if(revSecondEntries.containsKey(s)){ if(fitWeights[numSamples+revSecondEntries.get(s)] != weights[s]) done2 = false; } else throw new RuntimeException("ERROR: should have second entry for restraint but don't!!"); } else { //deactivate all penalties if(fitWeights[s] != 0) done2 = false; if(revSecondEntries.containsKey(s)){ if(fitWeights[numSamples+revSecondEntries.get(s)] != 0) done2 = false; } //no contribution to residual } }*/ //DEBUG!!! //Now calculate mean residual and crossing points, and update fitWeights double residTerm = 0; if (trueVals[s] >= bCutoffs[s]) { if (isRestraintTypeActive(trueVals[s], serVals[s], bCutoffs[s], bCutoffs2[s], false)) { //activate penalty for deviating from bCutoff fitWeights[s] = weights[s]; if (revSecondEntries.containsKey(s)) fitWeights[numSamples + revSecondEntries.get(s)] = 0; residTerm = (serVals[s] - bCutoffs[s]) * (serVals[s] - bCutoffs[s]); } else if (isRestraintTypeActive(trueVals[s], serVals[s], bCutoffs[s], bCutoffs2[s], true)) { //activate penalty for deviating from trueVal fitWeights[s] = 0; if (revSecondEntries.containsKey(s)) fitWeights[numSamples + revSecondEntries.get(s)] = weights[s]; else throw new RuntimeException("ERROR: should have second entry for restraint but don't!!"); residTerm = (serVals[s] - trueVals[s]) * (serVals[s] - trueVals[s]); } else { //deactivate all penalties fitWeights[s] = 0; if (revSecondEntries.containsKey(s)) fitWeights[numSamples + revSecondEntries.get(s)] = 0; //no contribution to residual } } else //normal least-squares penalty. fitWeights[s] will stay at weights[s] residTerm = (serVals[s] - trueVals[s]) * (serVals[s] - trueVals[s]); meanResidual += weights[s] * residTerm; //If want sample-by-sample output... //System.out.println("TRAININGSET TRUE: "+trueVals[s]+" SER: "+serVals[s]); weightSum += weights[s]; } meanResidual /= weightSum; if (meanResidual == prevResid) System.out.println(); //DEBUG!!! /* if(done!=doneNoTol || done2!=done){ //Let's see what happens if we remove the tolerance... done = doneNoTol; } if(done){ double checkCoeffs[] = fitSeries(fitSamp, fitTrueVals, fitWeights, lambda, includeConst, order, PCOrder, isPC, false, null, null); for(int a=0; a<coeffs.length; a++){ if(Math.abs(checkCoeffs[a]-coeffs[a])>1e-10){ int abc=123; } } }*/ //DEBUG!!! if ((!done) && (meanResidual >= prevResid)) { //Did not obtain a decrease using the Newton step //Let's do an exact line search to rectify the situation if (!useLineSearch) { System.out.println("Skipping line search, returning with residual " + prevResid); return oldCoeffs; } System.out.println("LINE SEARCH"); for (int s = 0; s < numSamples; s++) { //If we go in or out of either type of restraint between serVals and oldSerVals, we create //a SampleCutoffCrossing of the appropriate type (upper or lower (ordinary) restraint) if ((isRestraintTypeActive(trueVals[s], oldSerVals[s], bCutoffs[s], bCutoffs2[s], false)) != (isRestraintTypeActive(trueVals[s], serVals[s], bCutoffs[s], bCutoffs2[s], false))) { //If the restraint disappears at one end we know trueVal>=bCutoff here //create lower restraint SampleCutoffCrossing double crossingPoint = (bCutoffs[s] - oldSerVals[s]) / (serVals[s] - oldSerVals[s]); scc.add(new SampleCutoffCrossing(s, crossingPoint, false)); } if ((isRestraintTypeActive(trueVals[s], oldSerVals[s], bCutoffs[s], bCutoffs2[s], true)) != (isRestraintTypeActive(trueVals[s], serVals[s], bCutoffs[s], bCutoffs2[s], true))) { //upper double crossingPoint2 = (trueVals[s] - oldSerVals[s]) / (serVals[s] - oldSerVals[s]); scc.add(new SampleCutoffCrossing(s, crossingPoint2, true)); } } int changeCount = scc.size(); Collections.sort(scc); TreeSet<Integer> crossingIndices = new TreeSet<Integer>(); for (SampleCutoffCrossing cr : scc) { int s = cr.sampleIndex; crossingIndices.add(s); if (cr.upperRestraint) {//penalizing difference from trueVal cr.quadTerm = weights[s] * (serVals[s] - oldSerVals[s]) * (serVals[s] - oldSerVals[s]); cr.linTerm = 2 * weights[s] * (serVals[s] - oldSerVals[s]) * (oldSerVals[s] - trueVals[s]); cr.constTerm = weights[s] * (oldSerVals[s] - trueVals[s]) * (oldSerVals[s] - trueVals[s]); } else {//penalizing difference from lesser of bCutoff, trueVal cr.quadTerm = weights[s] * (serVals[s] - oldSerVals[s]) * (serVals[s] - oldSerVals[s]); double baseVal = Math.min(trueVals[s], bCutoffs[s]); cr.linTerm = 2 * weights[s] * (serVals[s] - oldSerVals[s]) * (oldSerVals[s] - baseVal); cr.constTerm = weights[s] * (oldSerVals[s] - baseVal) * (oldSerVals[s] - baseVal); //cr.linTerm = 2*weights[s]*(serVals[s]-oldSerVals[s])*(oldSerVals[s]-trueVals[s]); //cr.constTerm = weights[s]*(oldSerVals[s]-trueVals[s])*(oldSerVals[s]-trueVals[s]); } } //Set up quadratic function double quadTerm = 0; double linTerm = 0; double constTerm = 0; //Add in contributions from all non-cutoff-crossing points for (int s = 0; s < numSamples; s++) { if (!crossingIndices.contains(s)) { if (isRestraintTypeActive(trueVals[s], oldSerVals[s], bCutoffs[s], bCutoffs2[s], false)) { //if(trueVals[s]<bCutoffs[s]||serVals[s]<bCutoffs[s]){//penalizing difference from lesser of bCutoff, trueVal quadTerm += weights[s] * (serVals[s] - oldSerVals[s]) * (serVals[s] - oldSerVals[s]); double baseVal = Math.min(trueVals[s], bCutoffs[s]); linTerm += 2 * weights[s] * (serVals[s] - oldSerVals[s]) * (oldSerVals[s] - baseVal); constTerm += weights[s] * (oldSerVals[s] - baseVal) * (oldSerVals[s] - baseVal); //linTerm += 2*weights[s]*(serVals[s]-oldSerVals[s])*(oldSerVals[s]-trueVals[s]); //constTerm += weights[s]*(oldSerVals[s]-trueVals[s])*(oldSerVals[s]-trueVals[s]); } else if (isRestraintTypeActive(trueVals[s], oldSerVals[s], bCutoffs[s], bCutoffs2[s], true)) { //else if(isRestraintActive(trueVals[s],serVals[s],bCutoffs[s],bCutoffs2[s],0)){//penalizing difference from trueVal quadTerm += weights[s] * (serVals[s] - oldSerVals[s]) * (serVals[s] - oldSerVals[s]); linTerm += 2 * weights[s] * (serVals[s] - oldSerVals[s]) * (oldSerVals[s] - trueVals[s]); constTerm += weights[s] * (oldSerVals[s] - trueVals[s]) * (oldSerVals[s] - trueVals[s]); } } } //contributions from cutoff-crossing points at the beginning of the interval //(i.e. at coeffs) for (SampleCutoffCrossing cr : scc) { int s = cr.sampleIndex; if (isRestraintTypeActive(trueVals[s], serVals[s], bCutoffs[s], bCutoffs2[s], cr.upperRestraint)) { //check if this particular restraint (rather than either restraint for this s) is active //if(serVals[s]<bCutoffs[s]){ quadTerm += cr.quadTerm; linTerm += cr.linTerm; constTerm += cr.constTerm; } } //double checkMeanResid = (quadTerm+linTerm+constTerm)/weightSum;//evaluate objective function at a=1 //should match meanResidual! double prevNodeResid = Double.POSITIVE_INFINITY; //The first increase we may consider is from node 0 to 1 //Now walk back until we get an increase //then the minimum will be in one of the last two quadratic pieces int lowestNodeIndex = 0; for (int curChangeIndex = changeCount - 1; curChangeIndex >= 0; curChangeIndex--) { SampleCutoffCrossing cr = scc.get(curChangeIndex); double a = cr.crossingPoint; double curNodeResid = (a * a * quadTerm + a * linTerm + constTerm) / weightSum; int s = cr.sampleIndex; if (curNodeResid > prevNodeResid) { lowestNodeIndex = curChangeIndex + 1; break; } //if the restraint is being removed going from serVals to oldSerVals, remove its coefficients from the quadratic function //if it's being added, add them if (isRestraintTypeActive(trueVals[s], serVals[s], bCutoffs[s], bCutoffs2[s], cr.upperRestraint)) { quadTerm -= cr.quadTerm; linTerm -= cr.linTerm; constTerm -= cr.constTerm; } else { quadTerm += cr.quadTerm; linTerm += cr.linTerm; constTerm += cr.constTerm; } prevNodeResid = curNodeResid; } //At this point, we know our minimum is in either the piece with the //current quad, lin, constTerms or the one we looked at right before //(where lowestNodeIndex is the node separating them) double a_min = -linTerm / (2 * quadTerm); SampleCutoffCrossing cr = scc.get(lowestNodeIndex); if (a_min > cr.crossingPoint) {//minimum must be in previous piece //revert quad and linTerms int s = cr.sampleIndex; //change back quadratic-function coefficients if (isRestraintTypeActive(trueVals[s], serVals[s], bCutoffs[s], bCutoffs2[s], cr.upperRestraint)) { quadTerm += cr.quadTerm; linTerm += cr.linTerm; constTerm += cr.constTerm; } else { quadTerm -= cr.quadTerm; linTerm -= cr.linTerm; constTerm -= cr.constTerm; } a_min = -linTerm / (2 * quadTerm); } //double minResid = (a_min*a_min*quadTerm + a_min*linTerm + constTerm)/weightSum; for (int p = 0; p < numParams; p++) coeffs[p] = coeffs[p] * a_min + (1 - a_min) * oldCoeffs[p]; double minResid = 0; for (int s = 0; s < numSamples; s++) { serVals[s] = evalSeries(coeffs, samp[s], nd, includeConst, order, PCOrder, isPC); if (trueVals[s] >= bCutoffs[s]) { if (isRestraintTypeActive(trueVals[s], serVals[s], bCutoffs[s], bCutoffs2[s], false)) { //activate penalty for deviating from bCutoff fitWeights[s] = weights[s]; if (revSecondEntries.containsKey(s)) fitWeights[numSamples + revSecondEntries.get(s)] = 0; minResid += weights[s] * (serVals[s] - bCutoffs[s]) * (serVals[s] - bCutoffs[s]); } else if (isRestraintTypeActive(trueVals[s], serVals[s], bCutoffs[s], bCutoffs2[s], true)) { minResid += weights[s] * (serVals[s] - trueVals[s]) * (serVals[s] - trueVals[s]); //activate penalty for deviating from trueVal fitWeights[s] = 0; fitWeights[numSamples + revSecondEntries.get(s)] = weights[s]; } else { //deactivate all penalties fitWeights[s] = 0; if (revSecondEntries.containsKey(s)) fitWeights[numSamples + revSecondEntries.get(s)] = 0; } } else minResid += weights[s] * (serVals[s] - trueVals[s]) * (serVals[s] - trueVals[s]); } minResid /= weightSum; if (minResid >= prevResid) { //consider to have converged //NOTE THIS CAN HAPPEN IF THE QUADRATIC APPROXIMATION AT OLDCOEFFS HAS SOLUTION //FAR FROM THE EXACT VALUE (ASSUMING EXACT FITSERIES) OF 1 //THIS CAN HAPPEN IF WE'RE GETTING BELOW THE NUMERICAL PRECISION OF FITSERIES System.out.println("TRAINING SET MEAN RESIDUAL:" + prevResid); System.out.println("CONVERGED IN LINE SEARCH, line search min: " + minResid); System.out.println("fitSeriesIterative time (ms): " + (System.currentTimeMillis() - startTime)); return oldCoeffs; } meanResidual = minResid; } /* //trying backtracking line search, hoping it'll be more numerically stable double backtrackRate = 0.5; double a = 1;//how far we want to be on the line from oldCoeffs (which should allow some descent //along the line) to coeffs (which overshoots) double minResid; double aCoeffs[] = new double[coeffs.length];//coefficients at our point on the line do { a *= backtrackRate; for(int p=0; p<numParams; p++) aCoeffs[p] = coeffs[p]*a + (1-a)*oldCoeffs[p]; minResid = 0; for(int s=0; s<numSamples; s++){ serVals[s] = evalSeries(aCoeffs,samp[s],nd,includeConst,order,PCOrder,isPC); if( trueVals[s]>=bCutoffs[s] ){ if(isRestraintTypeActive(trueVals[s],serVals[s],bCutoffs[s],bCutoffs2[s],false)){ minResid += weights[s]*(serVals[s]-bCutoffs[s])*(serVals[s]-bCutoffs[s]); weights2[s] = weights[s]; } else if(isRestraintTypeActive(trueVals[s],serVals[s],bCutoffs[s],bCutoffs2[s],true)){ minResid += weights[s]*(serVals[s]-trueVals[s])*(serVals[s]-trueVals[s]); weights2[s] = weights[s]; } else weights2[s] = 0; } else minResid += weights[s]*(serVals[s]-trueVals[s])*(serVals[s]-trueVals[s]); } minResid /= weightSum; } while(minResid>prevResid); if(a<1e-4) System.out.println("Warning: line search a got down to "+a); meanResidual = minResid; } */ oldCoeffs = coeffs; System.out.println("STEP RESIDUAL: " + meanResidual); prevResid = meanResidual; oldSerVals = serVals; } System.out.println("TRAINING SET MEAN RESIDUAL:" + meanResidual); System.out.println("fitSeriesIterative time (ms): " + (System.currentTimeMillis() - startTime)); //DEBUG!!! //Note this assumes weights are all 1! /* int numCoeffs = coeffs.length; DoubleMatrix1D residGrad = DoubleFactory1D.dense.make(numCoeffs); double checkResid = 0; for(int s=0; s<samp.length; s++){ double diff = 0; double fitVal = evalSeries(coeffs,samp[s],nd,includeConst,order,PCOrder,isPC); if(trueVals[s]>=bCutoffs[s] && fitVal<bCutoffs[s]){ diff = fitVal-bCutoffs[s]; if(fitWeights[s]==0){ int allosaurus = 15; } if(revSecondEntries.containsKey(s)){ if(fitWeights[numSamples+revSecondEntries.get(s)]>0){ int allosaurus=15; } } } else if( trueVals[s]<bCutoffs[s] || (trueVals[s]<bCutoffs2[s] && fitVal>trueVals[s]) ){ diff = fitVal-trueVals[s]; if(trueVals[s]<bCutoffs[s]){ if(fitWeights[s]==0){ int allosaurus = 15; } } else if(fitWeights[numSamples+revSecondEntries.get(s)]==0 || fitWeights[s]>0){ int allosaurus=15; } } else if(fitWeights[s]>0){ int allosaurus=15; } else if(revSecondEntries.containsKey(s)){ if(fitWeights[numSamples+revSecondEntries.get(s)]>0){ int allosaurus=15; } } checkResid += diff*diff; DoubleMatrix1D termMonomials = DoubleFactory1D.dense.make(numCoeffs); SeriesFitter.calcSampParamCoeffs( termMonomials, samp[s], nd, includeConst, order, PCOrder, isPC ); residGrad.assign( termMonomials, Functions.plusMult(2*diff) ); } checkResid /= samp.length; residGrad.assign( Functions.mult(1./samp.length) ); double checkCoeffs[] = fitSeries(fitSamp, fitTrueVals, fitWeights, lambda, includeConst, order, PCOrder, isPC, false, null, null); double checkResid2 = 0; DoubleMatrix1D residGrad2 = DoubleFactory1D.dense.make(numCoeffs); for(int s=0; s<samp.length; s++){ double diff = 0; double fitVal = evalSeries(checkCoeffs,samp[s],nd,includeConst,order,PCOrder,isPC); if(trueVals[s]>=bCutoffs[s] && fitVal<bCutoffs[s]){ diff = fitVal-bCutoffs[s]; if(fitWeights[s]==0){ int allosaurus = 15; } if(revSecondEntries.containsKey(s)){ if(fitWeights[numSamples+revSecondEntries.get(s)]>0){ int allosaurus=15; } } } else if( trueVals[s]<bCutoffs[s] || (trueVals[s]<bCutoffs2[s] && fitVal>trueVals[s]) ){ diff = fitVal-trueVals[s]; if(trueVals[s]<bCutoffs[s]){ if(fitWeights[s]==0){ int allosaurus = 15; } } else if(fitWeights[numSamples+revSecondEntries.get(s)]==0 || fitWeights[s]>0){ int allosaurus=15; } } else if(fitWeights[s]>0){ int allosaurus=15; } else if(revSecondEntries.containsKey(s)){ if(fitWeights[numSamples+revSecondEntries.get(s)]>0){ int allosaurus=15; } } checkResid2 += diff*diff; DoubleMatrix1D termMonomials = DoubleFactory1D.dense.make(numCoeffs); SeriesFitter.calcSampParamCoeffs( termMonomials, samp[s], nd, includeConst, order, PCOrder, isPC ); residGrad2.assign( termMonomials, Functions.plusMult(2*diff) ); } checkResid2 /= samp.length; residGrad2.assign( Functions.mult(1./samp.length) ); if(residGrad.zDotProduct(residGrad)>1e-10){ int struthiomimus = 23; } int cubist = -1; */ //DEBUG!! return coeffs; }
From source file:org.unitime.timetable.action.PersonalizedExamReportAction.java
public PdfWebTable getInstructorConflits(boolean html, TreeSet<ExamAssignmentInfo> exams, DepartmentalInstructor instructor) { String nl = (html ? "<br>" : "\n"); boolean showBackToBack = ApplicationProperty.ExaminationReportsInstructorBackToBacks.isTrue(); PdfWebTable table = new PdfWebTable(8, instructor.getDepartment().getSession().getLabel() + " Examination Instructor Conflicts" + (showBackToBack ? " and/or Back-To-Back Examinations" : "") + " for " + instructor.getName(DepartmentalInstructor.sNameFormatLastFist), "personalSchedule.do?o4=%%&q=" + QueryEncoderBackend.encode(instructor.getExternalUniqueId() + ":" + instructor.getDepartment().getSession().getUniqueId()), new String[] { "Type", "Class / Course", "Enrollment", "Seating" + nl + "Type", "Date", "Time", "Room", "Distance" }, new String[] { "left", "left", "right", "center", "left", "left", "left", "left" }, new boolean[] { true, true, true, true, true, true, true, true }); table.setRowStyle("white-space:nowrap"); String noRoom = ApplicationProperty.ExaminationsNoRoomText.value(); for (ExamAssignmentInfo exam : exams) { for (DirectConflict conflict : exam.getInstructorDirectConflicts()) { if (conflict.getOtherExam() != null && exam.compareTo(conflict.getOtherExam()) >= 0 && exams.contains(conflict.getOtherExam())) continue; String classes = "", enrollment = "", seating = "", date = "", time = "", room = ""; boolean firstSection = true; for (ExamSectionInfo section : exam.getSectionsIncludeCrosslistedDummies()) { if (classes.length() > 0) { classes += nl;//w ww . j a v a 2 s . c om enrollment += nl; seating += nl; date += nl; time += nl; room += nl; } classes += section.getName(); enrollment += String.valueOf(section.getNrStudents()); if (firstSection) { seating += Exam.sSeatingTypes[exam.getSeatingType()]; date += exam.getDate(false); time += exam.getTime(false); room += (exam.getNrRooms() == 0 ? noRoom : exam.getRoomsName(false, ", ")); } firstSection = false; } firstSection = true; if (conflict.getOtherExam() != null) { for (ExamSectionInfo section : conflict.getOtherExam().getSectionsIncludeCrosslistedDummies()) { if (classes.length() > 0) { classes += nl; enrollment += nl; seating += nl; date += nl; time += nl; room += nl; } classes += section.getName(); enrollment += String.valueOf(section.getNrStudents()); if (firstSection) { seating += Exam.sSeatingTypes[conflict.getOtherExam().getSeatingType()]; room += (conflict.getOtherExam().getNrRooms() == 0 ? conflict.getOtherExam() : exam.getRoomsName(false, ", ")); } firstSection = false; } } else if (conflict.getOtherEventId() != null) { classes += nl; enrollment += nl; seating += nl; date += nl; time += nl; room += nl; classes += conflict.getOtherEventName(); enrollment += conflict.getOtherEventSize(); seating += conflict.isOtherClass() ? "Class" : "Event"; room += conflict.getOtherEventRoom(); //date += conflict.getOtherEventDate(); time += conflict.getOtherEventTime(); } table.addLine( new String[] { (html ? "<font color='" + PreferenceLevel.prolog2color("P") + "'>" : "") + (conflict.getOtherEventId() != null ? conflict.isOtherClass() ? "Class" : "Event" : "Direct") + (html ? "</font>" : ""), classes, enrollment, seating, date, time, room, "" }, new Comparable[] { new MultiComparable(-exam.getExamType().getType(), 0, exam, 0), new MultiComparable(-exam.getExamType().getType(), exam, exam, 0), new MultiComparable(-exam.getExamType().getType(), -exam.getNrStudents() - (conflict.getOtherExam() == null ? 0 : conflict.getOtherExam().getNrStudents()), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getExamTypeLabel(), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getPeriodOrd(), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getPeriod().getStartSlot(), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getRoomsName(":"), exam, 0), new MultiComparable(-exam.getExamType().getType(), -1.0, exam, 0) }); } if (showBackToBack) for (BackToBackConflict conflict : exam.getInstructorBackToBackConflicts()) { if (exam.compareTo(conflict.getOtherExam()) >= 0 && exams.contains(conflict.getOtherExam())) continue; String classes = "", enrollment = "", seating = "", date = "", time = "", room = ""; boolean firstSection = true; for (ExamSectionInfo section : exam.getSectionsIncludeCrosslistedDummies()) { if (classes.length() > 0) { classes += nl; enrollment += nl; seating += nl; date += nl; time += nl; room += nl; } classes += section.getName(); enrollment += String.valueOf(section.getNrStudents()); if (firstSection) { seating += Exam.sSeatingTypes[exam.getSeatingType()]; date += exam.getDate(false); time += exam.getTime(false); room += (exam.getNrRooms() == 0 ? noRoom : exam.getRoomsName(false, ", ")); } firstSection = false; } firstSection = true; for (ExamSectionInfo section : conflict.getOtherExam().getSectionsIncludeCrosslistedDummies()) { if (classes.length() > 0) { classes += nl; enrollment += nl; seating += nl; date += nl; time += nl; room += nl; } classes += section.getName(); enrollment += String.valueOf(section.getNrStudents()); if (firstSection) { seating += Exam.sSeatingTypes[conflict.getOtherExam().getSeatingType()]; time += conflict.getOtherExam().getTime(false); room += (conflict.getOtherExam().getNrRooms() == 0 ? conflict.getOtherExam() : exam.getRoomsName(false, ", ")); } firstSection = false; } table.addLine( new String[] { (html ? "<font color='" + PreferenceLevel.prolog2color("1") + "'>" : "") + "Back-To-Back" + (html ? "</font>" : ""), classes, enrollment, seating, date, time, room, (int) (conflict.getDistance() * 10.0) + " m" }, new Comparable[] { new MultiComparable(-exam.getExamType().getType(), 2, exam, 0), new MultiComparable(-exam.getExamType().getType(), exam, exam, 0), new MultiComparable(-exam.getExamType().getType(), -exam.getNrStudents() - (conflict.getOtherExam() == null ? 0 : conflict.getOtherExam().getNrStudents()), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getExamTypeLabel(), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getPeriodOrd(), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getPeriod().getStartSlot(), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getRoomsName(":"), exam, 0), new MultiComparable(-exam.getExamType().getType(), conflict.getDistance(), exam, 0) }); } conflicts: for (MoreThanTwoADayConflict conflict : exam.getInstructorMoreThanTwoADaysConflicts()) { for (ExamAssignment other : conflict.getOtherExams()) if (exam.compareTo(other) >= 0 && exams.contains(other)) continue conflicts; String classes = "", enrollment = "", seating = "", date = "", time = "", room = ""; int nrStudents = exam.getNrStudents(); boolean firstSection = true; for (ExamSectionInfo section : exam.getSectionsIncludeCrosslistedDummies()) { if (classes.length() > 0) { classes += nl; enrollment += nl; seating += nl; date += nl; time += nl; room += nl; } classes += section.getName(); enrollment += String.valueOf(section.getNrStudents()); if (firstSection) { seating += Exam.sSeatingTypes[exam.getSeatingType()]; date += exam.getDate(false); time += exam.getTime(false); room += (exam.getNrRooms() == 0 ? noRoom : exam.getRoomsName(false, ", ")); } firstSection = false; } for (ExamAssignment other : conflict.getOtherExams()) { firstSection = true; nrStudents += other.getNrStudents(); for (ExamSectionInfo section : other.getSectionsIncludeCrosslistedDummies()) { if (classes.length() > 0) { classes += nl; enrollment += nl; seating += nl; date += nl; time += nl; room += nl; } classes += section.getName(); enrollment += String.valueOf(section.getNrStudents()); if (firstSection) { seating += Exam.sSeatingTypes[exam.getSeatingType()]; time += other.getTime(false); room += (other.getNrRooms() == 0 ? noRoom : other.getRoomsName(false, ", ")); } firstSection = false; } } table.addLine( (sessionContext.hasPermission(exam, Right.ExaminationDetail) ? "onClick=\"document.location='examDetail.do?examId=" + exam.getExamId() + "';\"" : ""), new String[] { (html ? "<font color='" + PreferenceLevel.prolog2color("2") + "'>" : "") + (html ? ">" : "") + "2 A Day" + (html ? "</font>" : ""), classes, enrollment, seating, date, time, room, "" }, new Comparable[] { new MultiComparable(-exam.getExamType().getType(), 1, exam, 0), new MultiComparable(-exam.getExamType().getType(), exam, exam, 0), new MultiComparable(-exam.getExamType().getType(), -nrStudents, exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getExamTypeLabel(), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getPeriodOrd(), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getPeriod().getStartSlot(), exam, 0), new MultiComparable(-exam.getExamType().getType(), exam.getRoomsName(":"), exam, 0), new MultiComparable(-exam.getExamType().getType(), -1.0, exam, 0) }, exam.getExamId().toString()); } } table.setWebTableTweakStyle(new WebTableTweakStyle() { public String getStyleHtml(WebTableLine current, WebTableLine next, int order) { if (next != null && ((MultiComparable) current.getOrderBy()[0]).getContent()[0] .compareTo(((MultiComparable) next.getOrderBy()[0]).getContent()[0]) != 0) return "border-bottom: rgb(81,81,81) 1px dashed"; return null; } }); return table; }
From source file:org.exist.dom.ElementImpl.java
/** * Method toString./*ww w.j av a 2 s .c o m*/ * */ public String toString(boolean top, TreeSet<String> namespaces) { final StringBuilder buf = new StringBuilder(); final StringBuilder attributes = new StringBuilder(); final StringBuilder children = new StringBuilder(); buf.append('<'); buf.append(nodeName); //Remove false to have a verbose output //if (top && false) { //buf.append(" xmlns:exist=\""+ Namespaces.EXIST_NS + "\""); //buf.append(" exist:id=\""); //buf.append(getNodeId()); //buf.append("\" exist:document=\""); //buf.append(((DocumentImpl)getOwnerDocument()).getFileURI()); //buf.append("\""); //} if (declaresNamespacePrefixes()) { // declare namespaces used by this element Map.Entry<String, String> entry; String namespace, prefix; for (final Iterator<Map.Entry<String, String>> i = namespaceMappings.entrySet().iterator(); i .hasNext();) { entry = i.next(); prefix = entry.getKey(); namespace = entry.getValue(); if (prefix.length() == 0) { buf.append(" xmlns=\""); //buf.append(namespace); buf.append("..."); } else { buf.append(" xmlns:"); buf.append(prefix); buf.append("=\""); //buf.append(namespace); buf.append("..."); } buf.append("\" "); namespaces.add(namespace); } } if (nodeName.getNamespaceURI().length() > 0 && (!namespaces.contains(nodeName.getNamespaceURI()))) { buf.append(" xmlns:").append(nodeName.getPrefix()).append("=\""); buf.append(nodeName.getNamespaceURI()); buf.append("\" "); } final NodeList childNodes = getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { final Node child = childNodes.item(i); switch (child.getNodeType()) { case Node.ATTRIBUTE_NODE: attributes.append(' '); attributes.append(((Attr) child).getName()); attributes.append("=\""); attributes.append(escapeXml(child)); attributes.append("\""); break; case Node.ELEMENT_NODE: children.append(((ElementImpl) child).toString(false, namespaces)); break; default: children.append(child.toString()); } } if (attributes.length() > 0) { buf.append(attributes.toString()); } if (childNodes.getLength() > 0) { buf.append(">"); buf.append(children.toString()); buf.append("</"); buf.append(nodeName); buf.append(">"); } else { buf.append("/>"); } return buf.toString(); }
From source file:org.gvsig.framework.web.service.impl.OGCInfoServiceImpl.java
public WMTSInfo getCapabilitiesFromWMTS(String urlServerWMTS, TreeSet<String> listCrs, boolean useCrsSelected) throws ServerGeoException { TreeSet<String> formatsSupported = new TreeSet<String>(); TreeSet<String> crsSupported = new TreeSet<String>(); boolean isFormatsSupported = false; WMTSInfo wmtsInfo = new WMTSInfo(); // put url on object WMSInfo wmtsInfo.setServiceUrl(urlServerWMTS); // Create hashmap to add the layers getted to the WMTSInfo object Map<String, org.gvsig.framework.web.ogc.WMTSLayer> layersMap = new HashMap<String, org.gvsig.framework.web.ogc.WMTSLayer>(); // get WMTS manager WMTSOGCManager wmtsMan = WMTSOGCLocator.getManager(); try {/* w ww .j a v a2 s. c o m*/ WMTSClient wmtsClient = wmtsMan.createWMTSClient(urlServerWMTS); wmtsClient.connect(true, null); WMTSServiceIdentification wmtsServIden = wmtsClient.getServiceIdentification(); // set server info wmtsInfo.setServiceAbstract(wmtsServIden.getAbstract()); wmtsInfo.setServiceTitle(wmtsServIden.getTitle()); wmtsInfo.setVersion(wmtsServIden.getServiceTypeVersion()); wmtsInfo.setServiceType(wmtsServIden.getServiceType()); // set id of the request wmst (service title + calendar) int hashCode = (wmtsServIden.getTitle() + Calendar.getInstance()).hashCode(); wmtsInfo.setId(hashCode); // set tile matrix and check if has support to crs of the map List<String> patternList = new ArrayList<String>(); if (!listCrs.isEmpty()) { for (String crs : listCrs) { String[] crsSplit = crs.split(":"); String pattern = "(.*)(:?)".concat(crsSplit[0]).concat("((:)(.*)(:)").concat(crsSplit[1]) .concat("|(:)").concat(crsSplit[1]).concat(")"); patternList.add(pattern); } } // hashmap with: identifier of tile matrix, supported crs Map<String, String> tileMatrixCrsSupported = new HashMap<String, String>(); TreeSet<String> tileMatrixSelectedId = new TreeSet<String>(); List<WMTSTileMatrixSet> tileMatrixSet = wmtsClient.getTileMatrixSet(); for (int i = 0; i < tileMatrixSet.size(); i++) { WMTSTileMatrixSet tileMatrix = tileMatrixSet.get(i); String identifier = tileMatrix.getIdentifier(); String supportedCRS = tileMatrix.getSupportedCRS(); crsSupported.add(supportedCRS); // add to map the tile matrix with its crs supported tileMatrixCrsSupported.put(identifier, supportedCRS); if (!listCrs.isEmpty()) { if (listCrs.contains(supportedCRS)) { tileMatrixSelectedId.add(identifier); } else { // check supportedCrs with the expReg generated by the // list of crs passed for (String expReg : patternList) { if (supportedCRS.matches(expReg)) { tileMatrixSelectedId.add(identifier); } } } } } // Add map of tile matrix and the tile matrix selected to WMTSInfo // object wmtsInfo.setTileMatrixCrsSupported(tileMatrixCrsSupported); wmtsInfo.setTileMatrixSelectedId(tileMatrixSelectedId); // Only set layers if has a tile matrix with crs of the map // supported // or crs is null WMTSThemes layerListAsThemes = wmtsClient.getLayerListAsThemes(); // Create tree with layer values List<TreeNode> tree = new ArrayList<TreeNode>(); // Create children layers for (int i = 0; i < layerListAsThemes.getChildCount(); i++) { WMTSTheme wmtsTheme = layerListAsThemes.getChildren(i); WMTSLayer layer = wmtsTheme.getLayer(); TreeSet<String> wmtsLinkSelected = new TreeSet<String>(); TreeSet<String> wmtsLinkSupported = new TreeSet<String>(); // check crs List<WMTSTileMatrixSetLink> tileMatrixSetLink = layer.getTileMatrixSetLink(); for (int j = 0; j < tileMatrixSetLink.size(); j++) { WMTSTileMatrixSetLink wmtsLink = tileMatrixSetLink.get(j); wmtsLinkSupported.add(wmtsLink.getTileMatrixSetId()); if (!tileMatrixSelectedId.isEmpty() && tileMatrixSelectedId.contains(wmtsLink.getTileMatrixSetId())) { wmtsLinkSelected.add(wmtsLink.getTileMatrixSetId()); } } // check format TreeSet<String> setFormats = new TreeSet<String>(); setFormats.addAll(layer.getFormat()); String format = getFirstFormatSupported(setFormats); formatsSupported.addAll(setFormats); if ((!wmtsLinkSelected.isEmpty() || listCrs.isEmpty()) && format != null) { isFormatsSupported = true; TreeNode node = new TreeNode(layer.getIdentifier()); node.setTitle(layer.getTitle()); node.setFolder(false); tree.add(node); // Add layer to layer map org.gvsig.framework.web.ogc.WMTSLayer wmtsLayer = new org.gvsig.framework.web.ogc.WMTSLayer(); TreeSet<String> crsSet = new TreeSet<String>(); crsSet.addAll(layer.getSrsList()); wmtsLayer.setCrs(crsSet); wmtsLayer.setName(layer.getIdentifier()); wmtsLayer.setTitle(layer.getTitle()); wmtsLayer.setFormatSelected(format); wmtsLayer.setFormatsSupported(setFormats); if (listCrs.isEmpty()) { wmtsLayer.setTileMatrixSelected(wmtsLinkSupported); } else { wmtsLayer.setTileMatrixSelected(wmtsLinkSelected); } layersMap.put(layer.getIdentifier(), wmtsLayer); } } wmtsInfo.setFormatsSupported(formatsSupported); wmtsInfo.setLayersTree(tree); wmtsInfo.setLayers(layersMap); wmtsInfo.setIsFormatsSupported(isFormatsSupported); wmtsInfo.setCrsSupported(crsSupported); } catch (Exception exc) { logger.error("Exception on getCapabilitiesFromWMS", exc); throw new ServerGeoException(); } return wmtsInfo; }
From source file:org.dasein.cloud.aws.compute.EC2Instance.java
private @Nonnull List<VirtualMachine> runInstances(@Nonnull VMLaunchOptions cfg, @Nonnegative int instanceCount) throws CloudException, InternalException { List<VirtualMachine> servers = new ArrayList<VirtualMachine>(); // instance cache List<String> instanceIds = new ArrayList<String>(); // instanceId cache ProviderContext ctx = getProvider().getContext(); if (ctx == null) { throw new CloudException("No context was established for this request"); }//from w ww.j av a 2 s . c om MachineImage img = null; final ComputeServices computeServices = getProvider().getComputeServices(); if (computeServices != null) { img = computeServices.getImageSupport().getImage(cfg.getMachineImageId()); } if (img == null) { throw new AWSResourceNotFoundException("No such machine image: " + cfg.getMachineImageId()); } Map<String, String> parameters = getProvider().getStandardParameters(getProvider().getContext(), EC2Method.RUN_INSTANCES); String ramdiskImage = (String) cfg.getMetaData().get("ramdiskImageId"), kernelImage = (String) cfg.getMetaData().get("kernelImageId"); EC2Method method; NodeList blocks; Document doc; parameters.put("ImageId", cfg.getMachineImageId()); parameters.put("MinCount", String.valueOf(instanceCount)); parameters.put("MaxCount", String.valueOf(instanceCount)); parameters.put("InstanceType", cfg.getStandardProductId()); AWSCloud.addValueIfNotNull(parameters, "ramdiskId", ramdiskImage); AWSCloud.addValueIfNotNull(parameters, "kernelId", kernelImage); AWSCloud.addValueIfNotNull(parameters, "IamInstanceProfile.Arn", cfg.getRoleId()); if (cfg.getUserData() != null) { try { parameters.put("UserData", Base64.encodeBase64String(cfg.getUserData().getBytes("utf-8"))); } catch (UnsupportedEncodingException e) { throw new InternalException(e); } } if (cfg.isPreventApiTermination()) { parameters.put("DisableApiTermination", "true"); } if (cfg.getDataCenterId() != null) { parameters.put("Placement.AvailabilityZone", cfg.getDataCenterId()); } else if (cfg.getVolumes().length > 0) { for (VolumeAttachment a : cfg.getVolumes()) { if (a.volumeToCreate != null) { String dc = a.volumeToCreate.getDataCenterId(); if (dc != null) { cfg.inDataCenter(dc); break; } } } } AWSCloud.addValueIfNotNull(parameters, "KeyName", cfg.getBootstrapKey()); if (getProvider().getEC2Provider().isAWS()) { parameters.put("Monitoring.Enabled", String.valueOf(cfg.isExtendedAnalytics())); } if (cfg.isIoOptimized()) { parameters.put("EbsOptimized", "true"); } AWSCloud.addValueIfNotNull(parameters, "Placement.GroupName", cfg.getAffinityGroupId()); final ArrayList<VolumeAttachment> existingVolumes = new ArrayList<VolumeAttachment>(); TreeSet<String> deviceIds = new TreeSet<String>(); if (cfg.getVolumes().length > 0) { Iterable<String> possibles = getProvider().getComputeServices().getVolumeSupport() .listPossibleDeviceIds(img.getPlatform()); int i = 1; for (VolumeAttachment a : cfg.getVolumes()) { if (a.deviceId != null) { deviceIds.add(a.deviceId); } else if (a.volumeToCreate != null && a.volumeToCreate.getDeviceId() != null) { deviceIds.add(a.volumeToCreate.getDeviceId()); a.deviceId = a.volumeToCreate.getDeviceId(); } } for (VolumeAttachment a : cfg.getVolumes()) { if (a.deviceId == null) { for (String id : possibles) { if (!deviceIds.contains(id)) { a.deviceId = id; deviceIds.add(id); } } if (a.deviceId == null) { throw new InternalException("Unable to identify a device ID for volume"); } } if (a.existingVolumeId == null) { parameters.put("BlockDeviceMapping." + i + ".DeviceName", a.deviceId); VolumeProduct prd = getProvider().getComputeServices().getVolumeSupport() .getVolumeProduct(a.volumeToCreate.getVolumeProductId()); parameters.put("BlockDeviceMapping." + i + ".Ebs.VolumeType", prd.getProviderProductId()); if (a.volumeToCreate.getIops() > 0) { parameters.put("BlockDeviceMapping." + i + ".Ebs.Iops", String.valueOf(a.volumeToCreate.getIops())); } if (a.volumeToCreate.getSnapshotId() != null) { parameters.put("BlockDeviceMapping." + i + ".Ebs.SnapshotId", a.volumeToCreate.getSnapshotId()); } else { parameters.put("BlockDeviceMapping." + i + ".Ebs.VolumeSize", String.valueOf(a.volumeToCreate.getVolumeSize().getQuantity().intValue())); } i++; } else { existingVolumes.add(a); } } } if (cfg.getSubnetId() == null) { AWSCloud.addIndexedParameters(parameters, "SecurityGroupId.", cfg.getFirewallIds()); } else if (cfg.getNetworkInterfaces() != null && cfg.getNetworkInterfaces().length > 0) { VMLaunchOptions.NICConfig[] nics = cfg.getNetworkInterfaces(); int i = 1; for (VMLaunchOptions.NICConfig c : nics) { parameters.put("NetworkInterface." + i + ".DeviceIndex", String.valueOf(i)); // this only applies for the first NIC if (i == 1) { parameters.put("NetworkInterface.1.AssociatePublicIpAddress", String.valueOf(cfg.isAssociatePublicIpAddress())); } if (c.nicId == null) { parameters.put("NetworkInterface." + i + ".SubnetId", c.nicToCreate.getSubnetId()); parameters.put("NetworkInterface." + i + ".Description", c.nicToCreate.getDescription()); AWSCloud.addValueIfNotNull(parameters, "NetworkInterface." + i + ".PrivateIpAddress", c.nicToCreate.getIpAddress()); AWSCloud.addIndexedParameters(parameters, "NetworkInterface." + i + ".SecurityGroupId.", c.nicToCreate.getFirewallIds()); } else { parameters.put("NetworkInterface." + i + ".NetworkInterfaceId", c.nicId); } i++; } } else { parameters.put("NetworkInterface.1.DeviceIndex", "0"); parameters.put("NetworkInterface.1.SubnetId", cfg.getSubnetId()); parameters.put("NetworkInterface.1.AssociatePublicIpAddress", String.valueOf(cfg.isAssociatePublicIpAddress())); AWSCloud.addValueIfNotNull(parameters, "NetworkInterface.1.PrivateIpAddress", cfg.getPrivateIp()); AWSCloud.addIndexedParameters(parameters, "NetworkInterface.1.SecurityGroupId.", cfg.getFirewallIds()); } // Send request to AWS method = new EC2Method(getProvider(), parameters); try { doc = method.invoke(); } catch (EC2Exception e) { String code = e.getCode(); if (code != null && code.equals("InsufficientInstanceCapacity")) { return servers; } logger.error(e.getSummary()); throw new CloudException(e); } blocks = doc.getElementsByTagName("instancesSet"); for (int i = 0; i < blocks.getLength(); i++) { NodeList instances = blocks.item(i).getChildNodes(); for (int j = 0; j < instances.getLength(); j++) { Node instance = instances.item(j); if (instance.getNodeName().equals("item")) { VirtualMachine server = toVirtualMachine(ctx, instance, new ArrayList<IpAddress>() /* can't be an elastic IP */); if (server != null) { servers.add(server); instanceIds.add(server.getProviderVirtualMachineId()); } } } } // Wait for EC2 to figure out the server exists List<VirtualMachine> serversCopy = describeInstances(instanceIds.toArray(new String[instanceIds.size()])); long timeout = System.currentTimeMillis() + CalendarWrapper.MINUTE; while (timeout > System.currentTimeMillis() && serversCopy.size() < servers.size()) { try { Thread.sleep(5000L); } catch (InterruptedException ignore) { } try { serversCopy = describeInstances(instanceIds.toArray(new String[instanceIds.size()])); } catch (Throwable ignore) { } } // FIXME: not clear what is to be done if time is out but `serversCopy` is still less than `servers` // Set all instances their tags List<Tag> tags = new ArrayList<Tag>(); Map<String, Object> meta = cfg.getMetaData(); for (Map.Entry<String, Object> entry : meta.entrySet()) { if (entry.getKey().equalsIgnoreCase("name") || entry.getKey().equalsIgnoreCase("description")) { continue; } // Tag value can be null, make sure we are careful tags.add(new Tag(entry.getKey(), entry.getValue() == null ? "" : entry.getValue().toString())); } tags.add(new Tag("Name", cfg.getFriendlyName())); tags.add(new Tag("Description", cfg.getDescription())); if (cfg.getVirtualMachineGroup() != null) { tags.add(new Tag("dsnVMGroup", cfg.getVirtualMachineGroup())); } getProvider().createTags(instanceIds.toArray(new String[instanceIds.size()]), tags.toArray(new Tag[tags.size()])); // Set all instances their passwords and attach volumes for (VirtualMachine server : servers) { if (cfg.isIpForwardingAllowed()) { enableIpForwarding(server.getProviderVirtualMachineId()); } if (cfg.isIpForwardingAllowed()) { enableIpForwarding(server.getProviderVirtualMachineId()); } if (server != null && cfg.getBootstrapKey() != null) { try { final String sid = server.getProviderVirtualMachineId(); try { Callable<String> callable = new GetPassCallable(sid, getProvider()); String password = callable.call(); if (password == null) { server.setRootPassword(null); server.setPasswordCallback(callable); } else { server.setRootPassword(password); } server.setPlatform(Platform.WINDOWS); } catch (CloudException e) { logger.warn(e.getMessage()); } } catch (Throwable t) { logger.warn("Unable to retrieve password for " + server.getProviderVirtualMachineId() + ", Let's hope it's Unix: " + t.getMessage()); } } if (!existingVolumes.isEmpty()) { final VirtualMachine vm = server; getProvider().hold(); Thread thread = new Thread() { public void run() { try { for (VolumeAttachment a : existingVolumes) { try { if (computeServices != null) { computeServices.getVolumeSupport().attach(a.existingVolumeId, vm.getProviderMachineImageId(), a.deviceId); } } catch (Throwable t) { // ignore all errors } } } finally { getProvider().release(); } } }; thread.setName("Volume Mounter for " + server); thread.start(); } } return servers; }
From source file:com.actelion.research.table.view.JVisualization.java
protected TreeMap<byte[], VisualizationPoint> createReferenceMap(int referencingColumn, int referencedColumn) { // create list of referencing keys TreeSet<byte[]> set = new TreeSet<byte[]>(new ByteArrayComparator()); for (VisualizationPoint vp : mPoint) { byte[] data = (byte[]) vp.record.getData(referencingColumn); if (data != null) for (String ref : mTableModel.separateEntries(new String(data))) set.add(ref.getBytes()); }//from w w w . j a v a 2 s .c o m // create map of existing and referenced VisualizationPoints TreeMap<byte[], VisualizationPoint> map = new TreeMap<byte[], VisualizationPoint>( new ByteArrayComparator()); for (VisualizationPoint vp : mPoint) { byte[] key = (byte[]) vp.record.getData(referencedColumn); if (set.contains(key)) map.put(key, vp); } return map; }