+++ /dev/null
-/*
- * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
- * Copyright (C) $$Year-Rel$$ The Jalview Authors
- *
- * This file is part of Jalview.
- *
- * Jalview is free software: you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation, either version 3
- * of the License, or (at your option) any later version.
- *
- * Jalview is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty
- * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
- * The Jalview Authors are detailed in the 'AUTHORS' file.
- */
-package jalview.api;
-
-import jalview.datamodel.AlignmentAnnotation;
-
-import java.util.List;
-
-public interface AlignCalcManagerI
-{
-
- /**
- * tell manager that a worker is initialised and has started to run
- *
- * @param worker
- */
- void notifyStarted(AlignCalcWorkerI worker);
-
- /**
- * tell manager that a thread running worker's run() loop is ready to start
- * processing data
- *
- * @param worker
- * @return true if worker should start processing, false if another thread is
- * in progress
- */
- boolean notifyWorking(AlignCalcWorkerI worker);
-
- /**
- * notify manager that the worker has completed, and results may be ready to
- * collect
- *
- * @param worker
- */
- void workerComplete(AlignCalcWorkerI worker);
-
- /**
- * indicate that a worker like this cannot run on the platform and shouldn't
- * be started again
- *
- * @param worker
- */
- void disableWorker(AlignCalcWorkerI worker);
-
- /**
- * indicate that a worker like this may be run on the platform.
- *
- * @param worker
- * of class to be removed from the execution blacklist
- */
- 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
- */
- void startWorker(AlignCalcWorkerI worker);
-
- /**
- *
- * @param worker
- * @return true if the worker is currently running
- */
- boolean isWorking(AlignCalcWorkerI worker);
-
- /**
- * if any worker thread is operational, return true!
- *
- * @return
- */
- boolean isWorking();
-
- /**
- * register a restartable worker
- *
- * @param worker
- */
- void registerWorker(AlignCalcWorkerI worker);
-
- /**
- * restart any registered workers
- */
- void restartWorkers();
-
- /**
- *
- * @param alignmentAnnotation
- * @return true if a currently registered and working worker indicates its
- * involvement with the given alignmentAnnotation
- */
- boolean workingInvolvedWith(AlignmentAnnotation alignmentAnnotation);
-
- /**
- * kick any known instances of the given worker class to update their
- * annotation
- *
- * @param 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<? extends AlignCalcWorkerI> workerClass);
-
- /**
- * work out if there is an instance of a worker that is *waiting* to start
- * calculating
- *
- * @param workingClass
- * @return true if workingClass is already waiting to calculate. false if it
- * is calculating, or not queued.
- */
- boolean isPending(AlignCalcWorkerI workingClass);
-
- /**
- * deregister and otherwise remove any registered and working instances of the
- * given worker type
- *
- * @param typeToRemove
- */
- void removeWorkersOfClass(
- 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, e.g. for colour schemes.
- *
- * @param ann
- */
- void removeWorkerForAnnotation(AlignmentAnnotation ann);
-}
+++ /dev/null
-/*
- * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
- * Copyright (C) $$Year-Rel$$ The Jalview Authors
- *
- * This file is part of Jalview.
- *
- * Jalview is free software: you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation, either version 3
- * of the License, or (at your option) any later version.
- *
- * Jalview is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty
- * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
- * The Jalview Authors are detailed in the 'AUTHORS' file.
- */
-package jalview.workers;
-
-import jalview.api.AlignCalcManagerI;
-import jalview.api.AlignCalcWorkerI;
-import jalview.bin.Cache;
-import jalview.datamodel.AlignmentAnnotation;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Hashtable;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-public class AlignCalcManager implements AlignCalcManagerI
-{
- /*
- * list of registered workers
- */
- private final List<AlignCalcWorkerI> restartable = Collections
- .synchronizedList(new ArrayList<AlignCalcWorkerI>());
-
- /*
- * types of worker _not_ to run (for example, because they have
- * previously thrown errors)
- */
- private final List<Class<? extends AlignCalcWorkerI>> blackList = Collections
- .synchronizedList(new ArrayList<Class<? extends AlignCalcWorkerI>>());
-
- /*
- * global record of calculations in progress
- */
- private final List<AlignCalcWorkerI> inProgress = Collections
- .synchronizedList(new ArrayList<AlignCalcWorkerI>());
-
- /*
- * record of calculations pending or in progress in the current context
- */
- private final Map<Class<? extends AlignCalcWorkerI>, List<AlignCalcWorkerI>> updating =
- new Hashtable<Class<? extends AlignCalcWorkerI>, List<AlignCalcWorkerI>>();
-
- /*
- * workers that have run to completion so are candidates for visual-only
- * update of their results
- */
- private HashSet<AlignCalcWorkerI> canUpdate = new HashSet<>();;
-
- private static boolean listContains(List<AlignCalcWorkerI> upd,
- AlignCalcWorkerI worker)
- {
- // avoid use of 'Contains' in case
- for (AlignCalcWorkerI _otherworker : upd)
- {
- if (_otherworker == upd)
- {
- return true;
- }
- }
- return false;
- }
- @Override
- public void notifyStarted(AlignCalcWorkerI worker)
- {
- synchronized (updating)
- {
- List<AlignCalcWorkerI> upd = updating.get(worker.getClass());
- if (upd == null)
- {
- updating.put(worker.getClass(), upd = Collections
- .synchronizedList(new ArrayList<AlignCalcWorkerI>()));
- }
- synchronized (upd)
- {
- if (listContains(upd, worker))
- {
- Cache.log.debug(
- "Ignoring second call to notifyStart for worker "
- + worker);
- }
- else
- {
- upd.add(worker);
- }
- }
- }
- }
-
- /*
- * (non-Javadoc)
- *
- * @see jalview.api.AlignCalcManagerI#isPending(jalview.api.AlignCalcWorkerI)
- */
- @Override
- public boolean isPending(AlignCalcWorkerI workingClass)
- {
- synchronized (updating)
- {
- List<AlignCalcWorkerI> upd = updating.get(workingClass.getClass());
- return upd != null && upd.size() > 1;
- }
- }
-
- @Override
- public boolean notifyWorking(AlignCalcWorkerI worker)
- {
- synchronized (inProgress)
- {
- if (listContains(inProgress, worker))
- {
- return false; // worker is already working, so ask caller to wait around
- }
- else
- {
- inProgress.add(worker);
- }
- }
- return true;
- }
-
- @Override
- public void workerComplete(AlignCalcWorkerI worker)
- {
- synchronized (inProgress)
- {
- Cache.log.debug("Worker " + worker + " marked as complete.");
- inProgress.remove(worker);
- List<AlignCalcWorkerI> upd = updating.get(worker.getClass());
- if (upd != null)
- {
- synchronized (upd)
- {
- upd.remove(worker);
- }
- canUpdate.add(worker);
- }
- }
- }
-
- @Override
- public void disableWorker(AlignCalcWorkerI worker)
- {
- synchronized (blackList)
- {
- blackList.add(worker.getClass());
- }
- }
-
- @Override
- public boolean isDisabled(AlignCalcWorkerI worker)
- {
- synchronized (blackList)
- {
- return blackList.contains(worker.getClass());
- }
- }
-
- @Override
- public void startWorker(AlignCalcWorkerI worker)
- {
- if (!isDisabled(worker))
- {
- Thread tw = new Thread(() -> {
- try
- {
- worker.run();
- } catch (Throwable e)
- {
- e.printStackTrace();
- }
- });
- tw.setName(worker.getClass().toString());
- tw.start();
- }
- }
-
- @Override
- public boolean isWorking(AlignCalcWorkerI worker)
- {
- synchronized (inProgress)
- {// System.err.println("isWorking : worker "+(worker!=null ?
- // worker.getClass():"null")+ " "+hashCode());
- return worker != null && inProgress.contains(worker);
- }
- }
-
- @Override
- public boolean isWorking()
- {
- synchronized (inProgress)
- {
- // System.err.println("isWorking "+hashCode());
- return inProgress.size() > 0;
- }
- }
-
- public int getQueueLength() {
- return inProgress.size();
- }
-
- @Override
- public void registerWorker(AlignCalcWorkerI worker)
- {
- synchronized (restartable)
- {
- if (!listContains(restartable, worker))
- {
- restartable.add(worker);
- }
- startWorker(worker);
- }
- }
-
- @Override
- public void restartWorkers()
- {
- synchronized (restartable)
- {
- for (AlignCalcWorkerI worker : restartable)
- {
- startWorker(worker);
- }
- }
- }
-
- @Override
- public boolean workingInvolvedWith(
- AlignmentAnnotation alignmentAnnotation)
- {
- synchronized (inProgress)
- {
- for (AlignCalcWorkerI worker : inProgress)
- {
- if (worker.involves(alignmentAnnotation))
- {
- return true;
- }
- }
- }
- synchronized (updating)
- {
- for (List<AlignCalcWorkerI> workers : updating.values())
- {
- for (AlignCalcWorkerI worker : workers)
- {
- if (worker.involves(alignmentAnnotation))
- {
- return true;
- }
- }
- }
- }
- return false;
- }
-
- @Override
- public void updateAnnotationFor(
- Class<? extends AlignCalcWorkerI> workerClass)
- {
-
- AlignCalcWorkerI[] workers;
- synchronized (canUpdate)
- {
- workers = canUpdate.toArray(new AlignCalcWorkerI[0]);
- }
- for (AlignCalcWorkerI worker : workers)
- {
- if (workerClass.equals(worker.getClass()))
- {
- worker.updateAnnotation();
- }
- }
- }
-
- @Override
- public List<AlignCalcWorkerI> getRegisteredWorkersOfClass(
- Class<? extends AlignCalcWorkerI> workerClass)
- {
- List<AlignCalcWorkerI> workingClass = new ArrayList<>();
- synchronized (canUpdate)
- {
- for (AlignCalcWorkerI worker : canUpdate)
- {
- if (workerClass.equals(worker.getClass()))
- {
- workingClass.add(worker);
- }
- }
- }
- return (workingClass.size() == 0) ? null : workingClass;
- }
-
- @Override
- public void enableWorker(AlignCalcWorkerI worker)
- {
- synchronized (blackList)
- {
- blackList.remove(worker.getClass());
- }
- }
-
- @Override
- public void removeWorkersOfClass(
- Class<? extends AlignCalcWorkerI> typeToRemove)
- {
- List<AlignCalcWorkerI> removable = new ArrayList<>();
- Set<AlignCalcWorkerI> toremovannot = new HashSet<>();
- synchronized (restartable)
- {
- for (AlignCalcWorkerI worker : restartable)
- {
- if (typeToRemove.equals(worker.getClass()))
- {
- removable.add(worker);
- toremovannot.add(worker);
- }
- }
- restartable.removeAll(removable);
- }
- synchronized (canUpdate)
- {
- for (AlignCalcWorkerI worker : canUpdate)
- {
- if (typeToRemove.equals(worker.getClass()))
- {
- removable.add(worker);
- toremovannot.add(worker);
- }
- }
- canUpdate.removeAll(removable);
- }
- // TODO: finish testing this extension
-
- /*
- * synchronized (inProgress) { // need to kill or mark as dead any running
- * threads... (inProgress.get(typeToRemove)); }
- *
- * if (workers == null) { return; } for (AlignCalcWorkerI worker : workers)
- * {
- *
- * if (isPending(worker)) { worker.abortAndDestroy(); startWorker(worker); }
- * else { System.err.println("Pending exists for " + workerClass); } }
- */
- }
-
- /**
- * Deletes the worker that update the given annotation, provided it is marked
- * as deletable.
- */
- @Override
- public void removeWorkerForAnnotation(AlignmentAnnotation ann)
- {
- /*
- * first just find those to remove (to avoid
- * ConcurrentModificationException)
- */
- List<AlignCalcWorkerI> toRemove = new ArrayList<>();
- for (AlignCalcWorkerI worker : restartable)
- {
- if (worker.involves(ann))
- {
- if (worker.isDeletable())
- {
- toRemove.add(worker);
- }
- }
- }
-
- /*
- * 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.getClass());
- inProgress.remove(worker);
- canUpdate.remove(worker);
- synchronized (updating)
- {
- List<AlignCalcWorkerI> upd = updating.get(worker.getClass());
- if (upd != null)
- {
- upd.remove(worker);
- }
- }
- }
- }
-}