void notifyStart(AlignCalcWorkerI worker);
/**
- * check if a calculation of this type is already active
- *
- * @param worker
- * @return
- */
- boolean alreadyDoing(AlignCalcWorkerI worker);
-
- /**
* tell manager that worker is now processing data
*
* @param worker
void enableWorker(AlignCalcWorkerI worker);
/**
+ * Answers true if the worker is disabled from running
+ *
+ * @param worker
+ * @return
+ */
+ boolean isDisabled(AlignCalcWorkerI worker);
+
+ /**
* launch a new worker
*
* @param worker
*
* @param workerClass
*/
- void updateAnnotationFor(Class workerClass);
+ void updateAnnotationFor(Class<? extends AlignCalcWorkerI> workerClass);
/**
* return any registered workers of the given class
* @param workerClass
* @return null or one or more workers of the given class
*/
- List<AlignCalcWorkerI> getRegisteredWorkersOfClass(Class workerClass);
-
- /**
- * start any workers of the given class
- *
- * @param workerClass
- * @return false if no workers of given class were registered (note -
- * blacklisted classes cannot be restarted, so this method will return
- * true for blacklisted workers)
- */
- boolean startRegisteredWorkersOfClass(Class workerClass);
+ List<AlignCalcWorkerI> getRegisteredWorkersOfClass(
+ Class<? extends AlignCalcWorkerI> workerClass);
/**
* work out if there is an instance of a worker that is *waiting* to start
*
* @param typeToRemove
*/
- void removeRegisteredWorkersOfClass(Class typeToRemove);
+ void removeRegisteredWorkersOfClass(
+ Class<? extends AlignCalcWorkerI> typeToRemove);
/**
* Removes the worker that produces the given annotation, provided it is
* marked as 'deletable'. Some workers may need to continue to run as the
- * results of their calculations are needed elsewhere e.g. for colour schemes.
+ * results of their calculations are needed, e.g. for colour schemes.
*
* @param ann
*/
*/
public FeatureRenderer(AlignmentViewport av)
{
- super();
- this.av = av;
+ super(av);
setTransparencyAvailable(!System.getProperty("java.version")
.startsWith("1.1"));
*/
public FeatureRenderer(AlignmentPanel ap)
{
- super();
+ super(ap.av);
this.ap = ap;
- this.av = ap.av;
if (ap != null && ap.getSeqPanel() != null
&& ap.getSeqPanel().seqCanvas != null
&& ap.getSeqPanel().seqCanvas.fr != null)
*/
package jalview.renderer.seqfeatures;
+import jalview.api.AlignViewportI;
import jalview.datamodel.SequenceFeature;
import jalview.datamodel.SequenceI;
import jalview.viewmodel.seqfeatures.FeatureRendererModel;
boolean av_validCharWidth, av_isShowSeqFeatureHeight;
+ /**
+ * Constructor given a viewport
+ *
+ * @param viewport
+ */
+ public FeatureRenderer(AlignViewportI viewport)
+ {
+ this.av = viewport;
+ }
+
protected void updateAvConfig()
{
av_charHeight = av.getCharHeight();
import jalview.datamodel.SequenceI;
import jalview.renderer.seqfeatures.FeatureRenderer;
import jalview.schemes.GraduatedColor;
-import jalview.viewmodel.AlignmentViewport;
import java.awt.Color;
import java.beans.PropertyChangeListener;
protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
this);
- protected AlignmentViewport av;
+ protected AlignViewportI av;
/*
* map holds per feature type, {{min, max}, {min, max}} feature score
public class AlignCalcManager implements AlignCalcManagerI
{
- private volatile List<AlignCalcWorkerI> restartable = Collections
- .synchronizedList(new ArrayList<AlignCalcWorkerI>());
+ /*
+ * list of registered workers
+ */
+ private volatile List<AlignCalcWorkerI> restartable;
- private volatile List<AlignCalcWorkerI> blackList = Collections
- .synchronizedList(new ArrayList<AlignCalcWorkerI>());
+ /*
+ * types of worker _not_ to run (for example, because they have
+ * previously thrown errors)
+ */
+ private volatile List<Class<? extends AlignCalcWorkerI>> blackList;
- /**
+ /*
* global record of calculations in progress
*/
- private volatile Map<Class, AlignCalcWorkerI> inProgress = Collections
- .synchronizedMap(new Hashtable<Class, AlignCalcWorkerI>());
+ private volatile List<AlignCalcWorkerI> inProgress;
- /**
+ /*
* record of calculations pending or in progress in the current context
*/
- private volatile Map<Class, List<AlignCalcWorkerI>> updating = Collections
- .synchronizedMap(new Hashtable<Class, List<AlignCalcWorkerI>>());
+ private volatile Map<Class<? extends AlignCalcWorkerI>, List<AlignCalcWorkerI>> updating;
+
+ /*
+ * workers that have run to completion so are candidates for visual-only
+ * update of their results
+ */
+ private HashSet<AlignCalcWorkerI> canUpdate;
+
+ /**
+ * Constructor
+ */
+ public AlignCalcManager()
+ {
+ restartable = Collections
+ .synchronizedList(new ArrayList<AlignCalcWorkerI>());
+ blackList = Collections
+ .synchronizedList(new ArrayList<Class<? extends AlignCalcWorkerI>>());
+ inProgress = Collections
+ .synchronizedList(new ArrayList<AlignCalcWorkerI>());
+ updating = Collections
+ .synchronizedMap(new Hashtable<Class<? extends AlignCalcWorkerI>, List<AlignCalcWorkerI>>());
+ canUpdate = new HashSet<AlignCalcWorkerI>();
+ }
@Override
public void notifyStart(AlignCalcWorkerI worker)
}
}
- @Override
- public boolean alreadyDoing(AlignCalcWorkerI worker)
- {
- synchronized (inProgress)
- {
- return inProgress.containsKey(worker.getClass());
- }
- }
-
/*
* (non-Javadoc)
*
}
}
- // TODO make into api method if needed ?
- public int numberLive(AlignCalcWorkerI worker)
- {
- synchronized (updating)
- {
- List<AlignCalcWorkerI> upd = updating.get(worker.getClass());
- if (upd == null)
- {
- return 0;
- }
- ;
- return upd.size();
- }
- }
-
@Override
public boolean notifyWorking(AlignCalcWorkerI worker)
{
synchronized (inProgress)
{
- // TODO: decide if we should throw exceptions here if multiple workers
- // start to work
- if (inProgress.get(worker.getClass()) != null)
+ if (inProgress.contains(worker))
{
- if (false)
- {
- System.err
- .println("Warning: Multiple workers are running of type "
- + worker.getClass());
- }
- return false;
+ System.err.println("Implementation error: duplicate run of worker "
+ + worker);
+ }
+ else
+ {
+ inProgress.add(worker);
}
- inProgress.put(worker.getClass(), worker);
}
return true;
}
- private final HashSet<AlignCalcWorkerI> canUpdate = new HashSet<AlignCalcWorkerI>();
-
@Override
public void workerComplete(AlignCalcWorkerI worker)
{
synchronized (inProgress)
{
- // System.err.println("Worker "+worker.getClass()+" marked as complete.");
- inProgress.remove(worker.getClass());
+ // System.err.println("Worker " + worker + " marked as complete.");
+ inProgress.remove(worker);
List<AlignCalcWorkerI> upd = updating.get(worker.getClass());
if (upd != null)
{
{
synchronized (blackList)
{
- blackList.add(worker);
+ blackList.add(worker.getClass());
}
}
- public boolean isBlackListed(AlignCalcWorkerI worker)
+ @Override
+ public boolean isDisabled(AlignCalcWorkerI worker)
{
synchronized (blackList)
{
- return blackList.contains(worker);
+ return blackList.contains(worker.getClass());
}
}
@Override
public void startWorker(AlignCalcWorkerI worker)
{
- if (!isBlackListed(worker))
+ if (!isDisabled(worker))
{
Thread tw = new Thread(worker);
tw.setName(worker.getClass().toString());
synchronized (inProgress)
{// System.err.println("isWorking : worker "+(worker!=null ?
// worker.getClass():"null")+ " "+hashCode());
- return worker != null && inProgress.get(worker.getClass()) == worker;
+ return worker != null && inProgress.contains(worker);
}
}
{
synchronized (inProgress)
{
- for (AlignCalcWorkerI worker : inProgress.values())
+ for (AlignCalcWorkerI worker : inProgress)
{
if (worker.involves(alignmentAnnotation))
{
}
@Override
- public void updateAnnotationFor(Class workerClass)
+ public void updateAnnotationFor(
+ Class<? extends AlignCalcWorkerI> workerClass)
{
AlignCalcWorkerI[] workers;
@Override
public List<AlignCalcWorkerI> getRegisteredWorkersOfClass(
- Class workerClass)
+ Class<? extends AlignCalcWorkerI> workerClass)
{
List<AlignCalcWorkerI> workingClass = new ArrayList<AlignCalcWorkerI>();
- AlignCalcWorkerI[] workers;
synchronized (canUpdate)
{
- workers = canUpdate.toArray(new AlignCalcWorkerI[canUpdate.size()]);
- }
- for (AlignCalcWorkerI worker : workers)
- {
- if (workerClass.equals(worker.getClass()))
+ for (AlignCalcWorkerI worker : canUpdate)
{
- workingClass.add(worker);
+ if (workerClass.equals(worker.getClass()))
+ {
+ workingClass.add(worker);
+ }
}
}
return (workingClass.size() == 0) ? null : workingClass;
}
@Override
- public boolean startRegisteredWorkersOfClass(Class workerClass)
- {
- List<AlignCalcWorkerI> workers = getRegisteredWorkersOfClass(workerClass);
- if (workers == null)
- {
- return false;
- }
- for (AlignCalcWorkerI worker : workers)
- {
- if (!isPending(worker))
- {
- startWorker(worker);
- }
- else
- {
- System.err.println("Pending exists for " + workerClass);
- }
- }
- return true;
- }
-
- @Override
public void enableWorker(AlignCalcWorkerI worker)
{
synchronized (blackList)
{
- blackList.remove(worker);
+ blackList.remove(worker.getClass());
}
}
@Override
- public void removeRegisteredWorkersOfClass(Class typeToRemove)
+ public void removeRegisteredWorkersOfClass(
+ Class<? extends AlignCalcWorkerI> typeToRemove)
{
- List<AlignCalcWorkerI> workers = getRegisteredWorkersOfClass(typeToRemove);
List<AlignCalcWorkerI> removable = new ArrayList<AlignCalcWorkerI>();
Set<AlignCalcWorkerI> toremovannot = new HashSet<AlignCalcWorkerI>();
synchronized (restartable)
}
/*
- * remove all references to the workers
+ * remove all references to deleted workers so any references
+ * they hold to annotation data can be garbage collected
*/
for (AlignCalcWorkerI worker : toRemove)
{
restartable.remove(worker);
- blackList.remove(worker);
+ blackList.remove(worker.getClass());
+ inProgress.remove(worker);
+ canUpdate.remove(worker);
+ synchronized (updating)
+ {
+ List<AlignCalcWorkerI> upd = updating.get(worker.getClass());
+ if (upd != null)
+ {
+ upd.remove(worker);
+ }
+ }
}
}
}
package jalview.workers;
+import jalview.api.AlignViewportI;
+import jalview.api.AlignmentViewPanel;
import jalview.datamodel.AlignmentAnnotation;
import jalview.datamodel.Annotation;
import jalview.gui.AlignFrame;
* such as Groovy) to 'register and forget' an alignment annotation calculator. <br>
* Currently supports two flavours of calculator:
* <ul>
- * <li>a 'feature counter' which can count any desired property derivable from
+ * <li>a simple 'feature counter' which counts any desired score derivable from
* residue value and any sequence features at each position of the alignment</li>
- * <li>a 'general purpose' calculator which computes one more complete
+ * <li>a 'general purpose' calculator which computes one or more complete
* AlignmentAnnotation objects</li>
* </ul>
*/
*/
public static void newCalculator(FeatureCounterI counter)
{
- if (Desktop.getCurrentAlignFrame() != null)
+ // TODO need an interface for AlignFrame by which to access
+ // its AlignViewportI and AlignmentViewPanel
+ AlignFrame currentAlignFrame = Desktop.getCurrentAlignFrame();
+ if (currentAlignFrame != null)
{
- newCalculator(Desktop.getCurrentAlignFrame(), counter);
+ newCalculator(currentAlignFrame.getViewport(), currentAlignFrame
+ .getAlignPanels().get(0), counter);
}
else
{
/**
* Constructs and registers a new alignment annotation worker
*
- * @param af
- * the AlignFrame for which the annotation is to be calculated
+ * @param viewport
+ * @param panel
* @param counter
* provider of feature counts per alignment position
*/
- public static void newCalculator(AlignFrame af, FeatureCounterI counter)
+ public static void newCalculator(AlignViewportI viewport,
+ AlignmentViewPanel panel, FeatureCounterI counter)
{
- new ColumnCounterWorker(af, counter);
+ new ColumnCounterWorker(viewport, panel, counter);
}
/**
*/
public static void newCalculator(AnnotationProviderI calculator)
{
- if (Desktop.getCurrentAlignFrame() != null)
+ // TODO need an interface for AlignFrame by which to access
+ // its AlignViewportI and AlignmentViewPanel
+ AlignFrame currentAlignFrame = Desktop.getCurrentAlignFrame();
+ if (currentAlignFrame != null)
{
- newCalculator(Desktop.getCurrentAlignFrame(), calculator);
+ newCalculator(currentAlignFrame.getViewport(), currentAlignFrame
+ .getAlignPanels().get(0), calculator);
}
else
{
/**
* Constructs and registers a new alignment annotation worker
*
- * @param af
- * the AlignFrame for which the annotation is to be calculated
+ * @param viewport
+ * @param panel
* @param calculator
* provider of AlignmentAnnotation for the alignment
*/
- public static void newCalculator(AlignFrame af,
+ public static void newCalculator(AlignViewportI viewport,
+ AlignmentViewPanel panel,
AnnotationProviderI calculator)
{
- new AnnotationWorker(af, calculator);
+ new AnnotationWorker(viewport, panel, calculator);
}
/**
package jalview.workers;
+import jalview.api.FeatureRenderer;
import jalview.datamodel.AlignmentAnnotation;
import jalview.datamodel.AlignmentI;
-import jalview.gui.FeatureRenderer;
import java.util.List;
*/
package jalview.workers;
+import jalview.api.AlignViewportI;
+import jalview.api.AlignmentViewPanel;
import jalview.datamodel.AlignmentAnnotation;
import jalview.datamodel.AlignmentI;
-import jalview.gui.AlignFrame;
-import jalview.gui.AlignmentPanel;
-import jalview.gui.FeatureRenderer;
+import jalview.renderer.seqfeatures.FeatureRenderer;
import java.util.ArrayList;
import java.util.List;
* @param af
* @param counter
*/
- public AnnotationWorker(AlignFrame af, AnnotationProviderI counter)
+ public AnnotationWorker(AlignViewportI viewport,
+ AlignmentViewPanel panel, AnnotationProviderI counter)
{
- super(af.getViewport(), af.alignPanel);
+ super(viewport, panel);
ourAnnots = new ArrayList<AlignmentAnnotation>();
this.counter = counter;
calcMan.registerWorker(this);
try
{
List<AlignmentAnnotation> anns = counter.calculateAnnotation(
- alignment, new FeatureRenderer((AlignmentPanel) ap));
+ alignment, new FeatureRenderer(alignViewport));
for (AlignmentAnnotation ann : anns)
{
ann.showAllColLabels = true;
*/
package jalview.workers;
+import jalview.api.AlignViewportI;
+import jalview.api.AlignmentViewPanel;
import jalview.datamodel.AlignmentAnnotation;
import jalview.datamodel.AlignmentI;
import jalview.datamodel.Annotation;
import jalview.datamodel.SequenceFeature;
import jalview.datamodel.SequenceI;
-import jalview.gui.AlignFrame;
-import jalview.gui.AlignmentPanel;
-import jalview.gui.FeatureRenderer;
+import jalview.renderer.seqfeatures.FeatureRenderer;
import jalview.util.ColorUtils;
import jalview.util.Comparison;
* @param af
* @param counter
*/
- public ColumnCounterWorker(AlignFrame af, FeatureCounterI counter)
+ public ColumnCounterWorker(AlignViewportI viewport,
+ AlignmentViewPanel panel, FeatureCounterI counter)
{
- super(af.getViewport(), af.alignPanel);
+ super(viewport, panel);
ourAnnots = new ArrayList<AlignmentAnnotation>();
this.counter = counter;
calcMan.registerWorker(this);
*/
void computeAnnotations()
{
- FeatureRenderer fr = new FeatureRenderer((AlignmentPanel) ap);
+ FeatureRenderer fr = new FeatureRenderer(alignViewport);
// TODO use the commented out code once JAL-2075 is fixed
// to get adequate performance on genomic length sequence
AlignmentI alignment = alignViewport.getAlignment();
--- /dev/null
+package jalview.workers;
+
+import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.assertFalse;
+import static org.testng.AssertJUnit.assertTrue;
+
+import jalview.api.AlignCalcManagerI;
+import jalview.api.AlignCalcWorkerI;
+import jalview.api.FeatureRenderer;
+import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.Annotation;
+import jalview.datamodel.Sequence;
+import jalview.datamodel.SequenceI;
+import jalview.gui.AlignFrame;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+public class AlignCalcManagerTest
+{
+ private AlignFrame alignFrame;
+
+ /**
+ * Test the method that removes a worker associated with an annotation,
+ * provided the worker is marked as 'deletable' (some workers should continue
+ * to run even when their results are no longer displayed)
+ */
+ @Test(groups = "Functional")
+ public void testRemoveWorkerForAnnotation()
+ {
+ AlignCalcManagerI acm = alignFrame.getViewport().getCalcManager();
+ final AlignmentAnnotation ann1 = new AlignmentAnnotation("Ann1",
+ "desc",
+ new Annotation[] {});
+ final AlignmentAnnotation ann2 = new AlignmentAnnotation("Ann2",
+ "desc",
+ new Annotation[] {});
+
+ /*
+ * make two workers for ann1, one deletable, one not
+ * and no worker for ann2
+ */
+ AlignCalcWorkerI worker1 = makeWorker(ann1, true);
+ AlignCalcWorkerI worker2 = makeWorker(ann1, false);
+
+ /*
+ * The new workers will get run each in their own thread.
+ * We can't tell here whether they have finished, or not yet started.
+ * They have to finish to be 'seen' by getRegisteredWorkersOfClass()
+ * registerWorker adds to the 'restartable' list but
+ * getRegisteredWorkers reads from the 'canUpdate' list
+ * (which is only updated after a worker has run) - why?
+ * So just give workers time to start and finish
+ */
+ synchronized (this)
+ {
+ try
+ {
+ wait(100);
+ } catch (InterruptedException e)
+ {
+ //
+ }
+ }
+
+ List<AlignCalcWorkerI> workers = acm.getRegisteredWorkersOfClass(worker1.getClass());
+ assertEquals(2, workers.size());
+ assertTrue(workers.contains(worker1));
+ assertTrue(workers.contains(worker2));
+ assertFalse(acm.isDisabled(worker1));
+ assertFalse(acm.isDisabled(worker2));
+
+ /*
+ * remove workers for ann2 (there aren't any)
+ */
+ acm.removeWorkerForAnnotation(ann2);
+ assertTrue(acm.getRegisteredWorkersOfClass(worker1.getClass())
+ .contains(worker1));
+ assertTrue(acm.getRegisteredWorkersOfClass(worker1.getClass())
+ .contains(worker2));
+ assertFalse(acm.isDisabled(worker1));
+ assertFalse(acm.isDisabled(worker2));
+
+ /*
+ * remove worker2 for ann1
+ * - should delete worker1 but not worker2
+ */
+ acm.removeWorkerForAnnotation(ann1);
+ assertEquals(1, acm.getRegisteredWorkersOfClass(worker1.getClass())
+ .size());
+ assertTrue(acm.getRegisteredWorkersOfClass(worker1.getClass())
+ .contains(worker2));
+ assertFalse(acm.isDisabled(worker1));
+ assertFalse(acm.isDisabled(worker2));
+ }
+
+ /**
+ * Make a worker linked to the given annotation
+ *
+ * @param ann
+ * @param deletable
+ * @return
+ */
+ AnnotationWorker makeWorker(final AlignmentAnnotation ann,
+ final boolean deletable)
+ {
+ AnnotationProviderI annotationProvider = new AnnotationProviderI()
+ {
+ @Override
+ public List<AlignmentAnnotation> calculateAnnotation(AlignmentI al,
+ FeatureRenderer fr)
+ {
+ return Collections.singletonList(ann);
+ }
+ };
+ return new AnnotationWorker(alignFrame.getViewport(),
+ alignFrame.alignPanel,
+ annotationProvider)
+ {
+ @Override
+ public boolean isDeletable()
+ {
+ return deletable;
+ }
+
+ @Override
+ public boolean involves(AlignmentAnnotation ann1)
+ {
+ return ann == ann1;
+ }
+ };
+ }
+
+ @BeforeMethod(alwaysRun = true)
+ public void setUp()
+ {
+ AlignmentI al = new Alignment(new SequenceI[] { new Sequence("Seq1",
+ "ABC") });
+ al.setDataset(null);
+ alignFrame = new AlignFrame(al, 3, 1);
+ }
+}