Class AAlgorithm<I,​O>

  • All Implemented Interfaces:
    java.lang.Iterable<org.api4.java.algorithm.events.IAlgorithmEvent>, java.util.concurrent.Callable<O>, java.util.Iterator<org.api4.java.algorithm.events.IAlgorithmEvent>, org.api4.java.algorithm.IAlgorithm<I,​O>, org.api4.java.common.control.ICancelable, org.api4.java.common.control.ILoggingCustomizable
    Direct Known Subclasses:
    ASolutionCandidateIterator, LDSRelationComputer

    public abstract class AAlgorithm<I,​O>
    extends java.lang.Object
    implements org.api4.java.algorithm.IAlgorithm<I,​O>, org.api4.java.common.control.ILoggingCustomizable
    • Constructor Detail

      • AAlgorithm

        protected AAlgorithm​(I input)
        C'tor providing the input for the algorithm already.
        Parameters:
        input - The input for the algorithm.
      • AAlgorithm

        protected AAlgorithm​(IOwnerBasedAlgorithmConfig config,
                             I input)
        Internal c'tore overwriting the internal configuration and setting the input.
        Parameters:
        input - The input for the algorithm.
        config - The configuration to take as the internal configuration object.
    • Method Detail

      • iterator

        public java.util.Iterator<org.api4.java.algorithm.events.IAlgorithmEvent> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<I>
      • hasNext

        public boolean hasNext()
        Specified by:
        hasNext in interface java.util.Iterator<I>
      • next

        public org.api4.java.algorithm.events.IAlgorithmEvent next()
        Specified by:
        next in interface java.util.Iterator<I>
      • getInput

        public I getInput()
        Specified by:
        getInput in interface org.api4.java.algorithm.IAlgorithm<I,​O>
      • registerListener

        public void registerListener​(java.lang.Object listener)
        Specified by:
        registerListener in interface org.api4.java.algorithm.IAlgorithm<I,​O>
      • getListeners

        public java.util.List<java.lang.Object> getListeners()
      • getNumCPUs

        public int getNumCPUs()
        Specified by:
        getNumCPUs in interface org.api4.java.algorithm.IAlgorithm<I,​O>
      • setNumCPUs

        public void setNumCPUs​(int numberOfCPUs)
        Specified by:
        setNumCPUs in interface org.api4.java.algorithm.IAlgorithm<I,​O>
      • setMaxNumThreads

        public void setMaxNumThreads​(int maxNumberOfThreads)
        Specified by:
        setMaxNumThreads in interface org.api4.java.algorithm.IAlgorithm<I,​O>
      • setTimeout

        public final void setTimeout​(long timeout,
                                     java.util.concurrent.TimeUnit timeUnit)
        Specified by:
        setTimeout in interface org.api4.java.algorithm.IAlgorithm<I,​O>
      • setTimeout

        public void setTimeout​(org.api4.java.algorithm.Timeout timeout)
        Specified by:
        setTimeout in interface org.api4.java.algorithm.IAlgorithm<I,​O>
      • getTimeoutPrecautionOffset

        public int getTimeoutPrecautionOffset()
      • setTimeoutPrecautionOffset

        public void setTimeoutPrecautionOffset​(int timeoutPrecautionOffset)
      • getTimeout

        public org.api4.java.algorithm.Timeout getTimeout()
        Specified by:
        getTimeout in interface org.api4.java.algorithm.IAlgorithm<I,​O>
      • isTimeouted

        public boolean isTimeouted()
      • getRemainingTimeToDeadline

        protected org.api4.java.algorithm.Timeout getRemainingTimeToDeadline()
      • isStopCriterionSatisfied

        public boolean isStopCriterionSatisfied()
      • isCanceled

        public boolean isCanceled()
        Returns:
        Flag denoting whether this algorithm has been canceled.
      • getId

        public java.lang.String getId()
        Specified by:
        getId in interface org.api4.java.algorithm.IAlgorithm<I,​O>
      • checkTermination

        protected void checkTermination​(boolean shutdownOnStoppingCriterion)
                                 throws java.lang.InterruptedException,
                                        org.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledException,
                                        org.api4.java.algorithm.exceptions.AlgorithmTimeoutedException
        Throws:
        java.lang.InterruptedException
        org.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledException
        org.api4.java.algorithm.exceptions.AlgorithmTimeoutedException
      • checkAndConductTermination

        protected void checkAndConductTermination()
                                           throws java.lang.InterruptedException,
                                                  org.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledException,
                                                  org.api4.java.algorithm.exceptions.AlgorithmTimeoutedException
        Throws:
        java.lang.InterruptedException
        org.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledException
        org.api4.java.algorithm.exceptions.AlgorithmTimeoutedException
      • shutdown

        protected void shutdown()
        This method does two things: 1. it interrupts all threads that are registered to be active inside this algorithm 2. it cancels the (possibly created) timeout thread This method should be called ALWAYS when the algorithm activity ceases. This method takes effect only once. Further invocations will be ignored.
      • interruptThreadAsPartOfShutdown

        protected void interruptThreadAsPartOfShutdown​(java.lang.Thread t)
      • hasThreadBeenInterruptedDuringShutdown

        public boolean hasThreadBeenInterruptedDuringShutdown​(java.lang.Thread t)
      • resolveShutdownInterruptOnCurrentThread

        protected void resolveShutdownInterruptOnCurrentThread()
                                                        throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • avoidReinterruptionOnShutdownOnCurrentThread

        protected void avoidReinterruptionOnShutdownOnCurrentThread()
      • isShutdownInitialized

        public boolean isShutdownInitialized()
      • unregisterThreadAndShutdown

        protected void unregisterThreadAndShutdown()
      • registerActiveThread

        protected void registerActiveThread()
      • unregisterActiveThread

        protected void unregisterActiveThread()
      • getActivationTime

        public long getActivationTime()
      • getState

        public EAlgorithmState getState()
        Returns:
        The current state of the algorithm.
      • setState

        protected void setState​(EAlgorithmState state)
        Parameters:
        state - The new state of the algorithm.
      • cancel

        public void cancel()
        Specified by:
        cancel in interface org.api4.java.common.control.ICancelable
      • activate

        protected AlgorithmInitializedEvent activate()
        This method - defines the definite deadline for when the algorithm must have finished - sets the algorithm state to ACTIVE - sends the mandatory AlgorithmInitializedEvent over the event bus. Should only be called once and as before the state is set to something else.
      • setDeadline

        protected void setDeadline()
      • terminate

        protected AlgorithmFinishedEvent terminate()
        This methods terminates the algorithm, setting the internal state to inactive and emitting the mandatory AlgorithmFinishedEvent over the event bus.
        Returns:
        The algorithm finished event.
      • post

        protected void post​(java.lang.Object e)
        This methods allows for posting an event on the algorithm's event bus.
        Parameters:
        e - The event to post on the event bus.
      • getConfig

        public IOwnerBasedAlgorithmConfig getConfig()
        Specified by:
        getConfig in interface org.api4.java.algorithm.IAlgorithm<I,​O>
      • setConfig

        public void setConfig​(IOwnerBasedAlgorithmConfig config)
        Sets the config object to the new config object.
        Parameters:
        config - The new config object.
      • setLoggerName

        public void setLoggerName​(java.lang.String name)
        Specified by:
        setLoggerName in interface org.api4.java.common.control.ILoggingCustomizable
      • getLoggerName

        public java.lang.String getLoggerName()
        Specified by:
        getLoggerName in interface org.api4.java.common.control.ILoggingCustomizable
      • announceTimeoutDetected

        protected void announceTimeoutDetected()
      • computeTimeoutAware

        protected <T> T computeTimeoutAware​(java.util.concurrent.Callable<T> r,
                                            java.lang.String reasonToLogOnTimeout,
                                            boolean shutdownOnStoppingCriterionSatisfied)
                                     throws java.lang.InterruptedException,
                                            org.api4.java.algorithm.exceptions.AlgorithmException,
                                            org.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledException,
                                            org.api4.java.algorithm.exceptions.AlgorithmTimeoutedException
        Throws:
        java.lang.InterruptedException
        org.api4.java.algorithm.exceptions.AlgorithmException
        org.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledException
        org.api4.java.algorithm.exceptions.AlgorithmTimeoutedException