Package ai.libs.jaicore.basic.algorithm
Class AAlgorithm<I,O>
- java.lang.Object
-
- ai.libs.jaicore.basic.algorithm.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 Summary
Constructors Modifier Constructor Description protectedAAlgorithm(IOwnerBasedAlgorithmConfig config, I input)Internal c'tore overwriting the internal configuration and setting the input.protectedAAlgorithm(I input)C'tor providing the input for the algorithm already.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected AlgorithmInitializedEventactivate()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.protected voidannounceTimeoutDetected()protected voidavoidReinterruptionOnShutdownOnCurrentThread()voidcancel()protected voidcheckAndConductTermination()protected voidcheckTermination(boolean shutdownOnStoppingCriterion)protected <T> TcomputeTimeoutAware(java.util.concurrent.Callable<T> r, java.lang.String reasonToLogOnTimeout, boolean shutdownOnStoppingCriterionSatisfied)longgetActivationTime()IOwnerBasedAlgorithmConfiggetConfig()java.lang.StringgetId()IgetInput()java.util.List<java.lang.Object>getListeners()java.lang.StringgetLoggerName()intgetNumCPUs()protected org.api4.java.algorithm.TimeoutgetRemainingTimeToDeadline()EAlgorithmStategetState()org.api4.java.algorithm.TimeoutgetTimeout()intgetTimeoutPrecautionOffset()booleanhasNext()booleanhasThreadBeenInterruptedDuringShutdown(java.lang.Thread t)protected voidinterruptThreadAsPartOfShutdown(java.lang.Thread t)booleanisCanceled()booleanisShutdownInitialized()booleanisStopCriterionSatisfied()booleanisTimeouted()java.util.Iterator<org.api4.java.algorithm.events.IAlgorithmEvent>iterator()org.api4.java.algorithm.events.IAlgorithmEventnext()protected voidpost(java.lang.Object e)This methods allows for posting an event on the algorithm's event bus.protected voidregisterActiveThread()voidregisterListener(java.lang.Object listener)protected voidresolveShutdownInterruptOnCurrentThread()voidsetConfig(IOwnerBasedAlgorithmConfig config)Sets the config object to the new config object.protected voidsetDeadline()voidsetLoggerName(java.lang.String name)voidsetMaxNumThreads(int maxNumberOfThreads)voidsetNumCPUs(int numberOfCPUs)protected voidsetState(EAlgorithmState state)voidsetTimeout(long timeout, java.util.concurrent.TimeUnit timeUnit)voidsetTimeout(org.api4.java.algorithm.Timeout timeout)voidsetTimeoutPrecautionOffset(int timeoutPrecautionOffset)protected voidshutdown()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.protected AlgorithmFinishedEventterminate()This methods terminates the algorithm, setting the internal state to inactive and emitting the mandatory AlgorithmFinishedEvent over the event bus.protected voidunregisterActiveThread()protected voidunregisterThreadAndShutdown()
-
-
-
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:
iteratorin interfacejava.lang.Iterable<I>
-
hasNext
public boolean hasNext()
- Specified by:
hasNextin interfacejava.util.Iterator<I>
-
next
public org.api4.java.algorithm.events.IAlgorithmEvent next()
- Specified by:
nextin interfacejava.util.Iterator<I>
-
getInput
public I getInput()
-
registerListener
public void registerListener(java.lang.Object listener)
-
getListeners
public java.util.List<java.lang.Object> getListeners()
-
getNumCPUs
public int getNumCPUs()
-
setNumCPUs
public void setNumCPUs(int numberOfCPUs)
-
setMaxNumThreads
public void setMaxNumThreads(int maxNumberOfThreads)
-
setTimeout
public final void setTimeout(long timeout, java.util.concurrent.TimeUnit timeUnit)
-
setTimeout
public void setTimeout(org.api4.java.algorithm.Timeout timeout)
-
getTimeoutPrecautionOffset
public int getTimeoutPrecautionOffset()
-
setTimeoutPrecautionOffset
public void setTimeoutPrecautionOffset(int timeoutPrecautionOffset)
-
getTimeout
public org.api4.java.algorithm.Timeout getTimeout()
-
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()
-
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.InterruptedExceptionorg.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledExceptionorg.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.InterruptedExceptionorg.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledExceptionorg.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:
cancelin interfaceorg.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()
-
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:
setLoggerNamein interfaceorg.api4.java.common.control.ILoggingCustomizable
-
getLoggerName
public java.lang.String getLoggerName()
- Specified by:
getLoggerNamein interfaceorg.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.InterruptedExceptionorg.api4.java.algorithm.exceptions.AlgorithmExceptionorg.api4.java.algorithm.exceptions.AlgorithmExecutionCanceledExceptionorg.api4.java.algorithm.exceptions.AlgorithmTimeoutedException
-
-