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

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

Go to download

Experimental, state-machine-based implementation of an OR-Concurrent, Prolog-like logic solver, based on Kotlin coroutines

There is a newer version: 1.0.4
Show newest version
package it.unibo.tuprolog.solve.concurrent.fsm

import it.unibo.tuprolog.core.prepareForExecution
import it.unibo.tuprolog.solve.Solution
import it.unibo.tuprolog.solve.concurrent.ConcurrentExecutionContext
import it.unibo.tuprolog.unify.Unificator.Companion.mguWith

data class StateRuleExecution(override val context: ConcurrentExecutionContext) : State {

    private val failureState: EndState
        get() = StateEnd(
            solution = Solution.no(context.query),
            context = context.copy(step = nextStep())
        )

    override fun next(): Iterable {
        val substitution = context.goals.current!! mguWith context.rule!!.head
        return listOf(
            when {
                substitution.isSuccess -> {
                    val newSubstitution = (context.substitution + substitution).castToUnifier()
                    val subGoals = context.rule.prepareForExecution(newSubstitution).body[newSubstitution]
                    StateGoalSelection(
                        context.copy(
                            goals = subGoals.toGoals(),
                            rule = null,
                            substitution = newSubstitution,
                            step = nextStep()
                        )
                    )
                }
                else -> failureState
            }
        )
    }

    override fun clone(context: ConcurrentExecutionContext): State = copy(context = context)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy