All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.it.unibo.tuprolog.solve.concurrent.fsm.AbstractState.kt Maven / Gradle / Ivy

package it.unibo.tuprolog.solve.concurrent.fsm

import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.TimeInstant
import it.unibo.tuprolog.solve.concurrent.ConcurrentExecutionContext
import it.unibo.tuprolog.solve.concurrent.stdlib.primitive.Throw
import it.unibo.tuprolog.solve.currentTimeInstant
import it.unibo.tuprolog.solve.exception.TimeOutException

abstract class AbstractState(override val context: ConcurrentExecutionContext) : State {
    protected val executionTime: TimeInstant by lazy {
        currentTime()
    }

    protected open val isTimeout: Boolean
        get() = executionTime - context.startTime > context.maxDuration

    override fun next(): Iterable {
        return if (isTimeout) {
            listOf(
                StateHalt(
                    TimeOutException(
                        exceededDuration = context.maxDuration,
                        context = context,
                    ),
                    context.copy(step = nextStep()),
                ),
            )
        } else {
            computeNext()
        }
    }

    protected abstract fun computeNext(): Iterable

    protected fun currentTime(): TimeInstant = currentTimeInstant()

    protected fun nextStep(): Long = context.step + 1

    protected fun ConcurrentExecutionContext.skipThrow(): ExecutionContext =
        pathToRoot.first {
            it.procedure?.functor != Throw.functor
        }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy