From 59528dfd9877eca88ab0426bac6c26b2d6fe886d Mon Sep 17 00:00:00 2001 From: erik Date: Thu, 23 Mar 2006 20:18:45 +0000 Subject: [PATCH] Added priority mechanism. Corrected stylesheets to deal with recording conflicts as well, this status was missing. --- .../src/org/wamblee/crawler/impl/App.java | 2 - crawler/kiss/conf/kiss/programs.xml | 2 + crawler/kiss/conf/kiss/reportToHtml.xsl | 3 ++ crawler/kiss/conf/kiss/reportToText.xsl | 5 ++ .../kiss/main/ProgramActionExecutor.java | 52 +++++++++++++++---- .../kiss/main/ProgramConfigurationParser.java | 15 ++++-- .../kiss/main/RecordProgramAction.java | 4 +- 7 files changed, 65 insertions(+), 18 deletions(-) diff --git a/crawler/basic/src/org/wamblee/crawler/impl/App.java b/crawler/basic/src/org/wamblee/crawler/impl/App.java index c1af31b2..d4ca4709 100644 --- a/crawler/basic/src/org/wamblee/crawler/impl/App.java +++ b/crawler/basic/src/org/wamblee/crawler/impl/App.java @@ -5,8 +5,6 @@ import java.io.FileInputStream; import java.io.InputStream; import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.dom4j.Element; import org.wamblee.crawler.Action; import org.wamblee.crawler.Configuration; diff --git a/crawler/kiss/conf/kiss/programs.xml b/crawler/kiss/conf/kiss/programs.xml index 82de82fa..57f14cf6 100644 --- a/crawler/kiss/conf/kiss/programs.xml +++ b/crawler/kiss/conf/kiss/programs.xml @@ -48,10 +48,12 @@ + 10 battlestar + 10 star trek diff --git a/crawler/kiss/conf/kiss/reportToHtml.xsl b/crawler/kiss/conf/kiss/reportToHtml.xsl index f3901ea3..9017431d 100644 --- a/crawler/kiss/conf/kiss/reportToHtml.xsl +++ b/crawler/kiss/conf/kiss/reportToHtml.xsl @@ -44,6 +44,9 @@ Already recorded programs + + Conflicts with other recorded program + Programs that could not be recorded for technical reasons. diff --git a/crawler/kiss/conf/kiss/reportToText.xsl b/crawler/kiss/conf/kiss/reportToText.xsl index e46e4328..9d85cfaa 100644 --- a/crawler/kiss/conf/kiss/reportToText.xsl +++ b/crawler/kiss/conf/kiss/reportToText.xsl @@ -33,6 +33,11 @@ + + Conflicts with other recorded program + + + Programs that could not be recorded for technical reasons. diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/main/ProgramActionExecutor.java b/crawler/kiss/src/org/wamblee/crawler/kiss/main/ProgramActionExecutor.java index 44132b5f..4b2476d0 100644 --- a/crawler/kiss/src/org/wamblee/crawler/kiss/main/ProgramActionExecutor.java +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/main/ProgramActionExecutor.java @@ -17,6 +17,7 @@ package org.wamblee.crawler.kiss.main; import java.util.EnumMap; +import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.TreeMap; @@ -27,6 +28,7 @@ import org.apache.commons.logging.LogFactory; import org.dom4j.DocumentFactory; import org.dom4j.Element; import org.wamblee.crawler.kiss.guide.Program; +import org.wamblee.crawler.kiss.guide.TimeInterval; import org.wamblee.crawler.kiss.guide.Program.RecordingResult; /** @@ -35,8 +37,9 @@ import org.wamblee.crawler.kiss.guide.Program.RecordingResult; * in what order and makes decisions in case of conflicts. */ public class ProgramActionExecutor { - - private static final Log LOG = LogFactory.getLog(ProgramActionExecutor.class); + + private static final Log LOG = LogFactory + .getLog(ProgramActionExecutor.class); /** * A map of category name to a set of program. Useful for displaying the @@ -45,9 +48,9 @@ public class ProgramActionExecutor { private Map> _interestingShows; /** - * Set of programs to record. + * Map of priority to set of programs. */ - private Set _showsToRecord; + private Map> _showsToRecord; /** * Map or recording result to a set of programs. @@ -60,7 +63,7 @@ public class ProgramActionExecutor { */ public ProgramActionExecutor() { _interestingShows = new TreeMap>(); - _showsToRecord = new TreeSet(new Program.TimeSorter()); + _showsToRecord = new TreeMap>(); _recordings = new EnumMap>( RecordingResult.class); for (RecordingResult result : RecordingResult.values()) { @@ -79,7 +82,14 @@ public class ProgramActionExecutor { */ public void recordProgram(int aPriority, Program aProgram) { LOG.info("priority = " + aPriority + ", program: " + aProgram); - _showsToRecord.add(aProgram); + // Putting -priority into the set makes sure that iteration order + // over the priorities will go from higher priority to lower priority. + Set programs = _showsToRecord.get(-aPriority); + if (programs == null) { + programs = new TreeSet(new Program.TimeSorter()); + _showsToRecord.put(-aPriority, programs); + } + programs.add(aProgram); } /** @@ -105,10 +115,34 @@ public class ProgramActionExecutor { * */ public void commit() { - for (Program program : _showsToRecord) { - RecordingResult result = program.record(); - _recordings.get(result).add(program); + Set previouslyRecorded = new HashSet(); + for (Integer priority : _showsToRecord.keySet()) { + for (Program program : _showsToRecord.get(priority)) { + TimeInterval interval = program.getInterval(); + if ( recordingConflictExists(previouslyRecorded, interval)) { + _recordings.get(RecordingResult.CONFLICT).add(program); + } else { + RecordingResult result = program.record(); + _recordings.get(result).add(program); + previouslyRecorded.add(interval); + } + } + } + } + + /** + * Checks an interval for overlap with a previously recorded program. + * @param aPreviouslyRecorded Previously recorded programs. + * @param interval Interval. + * @return True iff there is a recording conflict. + */ + private boolean recordingConflictExists(Set aPreviouslyRecorded, TimeInterval interval) { + for (TimeInterval recordedInterval: aPreviouslyRecorded ) { + if ( interval.overlap(recordedInterval)) { + return true; + } } + return false; } /** diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/main/ProgramConfigurationParser.java b/crawler/kiss/src/org/wamblee/crawler/kiss/main/ProgramConfigurationParser.java index 103ec070..c16f4f93 100644 --- a/crawler/kiss/src/org/wamblee/crawler/kiss/main/ProgramConfigurationParser.java +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/main/ProgramConfigurationParser.java @@ -38,11 +38,9 @@ import org.wamblee.crawler.kiss.notification.Notifier; * Parse the configuration of desired programs. */ class ProgramConfigurationParser { - - /** - * - */ private static final int DEFAULT_SMTP_PORT = 25; + + private static final int DEFAULT_PRIORITY = 1; private static final String ELEM_PASSWORD = "password"; @@ -74,6 +72,8 @@ class ProgramConfigurationParser { // Configuration of interesting programs. private static final String ELEM_PROGRAM = "program"; + + private static final String ELEM_PRIORITY = "priority"; private static final String ELEM_PATTERN = "match"; @@ -117,7 +117,12 @@ class ProgramConfigurationParser { } Element actionElem = program.element(ELEM_ACTION); - ProgramAction action = new RecordProgramAction(1); + int priority = DEFAULT_PRIORITY; + String priorityString = program.elementTextTrim(ELEM_PRIORITY); + if ( priorityString != null ) { + priority = Integer.valueOf(priorityString); + } + ProgramAction action = new RecordProgramAction(priority); if (actionElem != null) { if (actionElem.getText().equals(ACTION_NOTIFY)) { action = new InterestingProgramAction(category); diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/main/RecordProgramAction.java b/crawler/kiss/src/org/wamblee/crawler/kiss/main/RecordProgramAction.java index 8458a957..4cbe12bc 100644 --- a/crawler/kiss/src/org/wamblee/crawler/kiss/main/RecordProgramAction.java +++ b/crawler/kiss/src/org/wamblee/crawler/kiss/main/RecordProgramAction.java @@ -27,8 +27,8 @@ public class RecordProgramAction implements ProgramAction { /** * Constructs the action. - * @param aPriority Priority of the recording action. Lower values mean - * higher priority. + * @param aPriority Priority of the recording action. Higher values have higher + * priority. */ public RecordProgramAction(int aPriority) { _priority = aPriority; -- 2.31.1