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

commonMain.it.unibo.tuprolog.solve.stdlib.primitive.AbstractCollectingPrimitive.kt Maven / Gradle / Ivy

There is a newer version: 1.0.4
Show newest version
package it.unibo.tuprolog.solve.stdlib.primitive

import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.Solution
import it.unibo.tuprolog.solve.exception.TimeOutException
import it.unibo.tuprolog.solve.primitive.Solve
import it.unibo.tuprolog.solve.primitive.TernaryRelation

abstract class AbstractCollectingPrimitive(name: String) : TernaryRelation.WithoutSideEffects(name) {
    protected fun Solve.Request.computeIntermediateSolutions(goal: Struct): List =
        buildList {
            for (solution in solve(goal, context.remainingTime)) {
                when {
                    context.remainingTime <= 0 -> {
                        throw TimeOutException(
                            exceededDuration = context.maxDuration,
                            context = context,
                        )
                    }
                    solution.isHalt -> {
                        throw solution.exception!!.pushContext(context)
                    }
                    solution.isNo -> {
                        break
                    }
                    else -> {
                        add(solution.castToYes())
                    }
                }
            }
        }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy