Skip to content

Commit

Permalink
format code in :solve-classic
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Sep 14, 2020
1 parent 7959b04 commit 63c3396
Show file tree
Hide file tree
Showing 65 changed files with 207 additions and 271 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ sealed class ChoicePointContext(
get() = executionContext?.procedure

override fun toString(): String = "$typeName(" +
"alternatives=$alternatives, " +
if (executionContext === null) {
"executionContext=${executionContext}, "
} else {
"executionContextDepth=${executionContextDepth}, "
"executionContextProcedure=${executionContextProcedure}, "
} +
"depth=$depth" +
")"
"alternatives=$alternatives, " +
if (executionContext === null) {
"executionContext=$executionContext, "
} else {
"executionContextDepth=$executionContextDepth, "
"executionContextProcedure=$executionContextProcedure, "
} +
"depth=$depth" +
")"

protected abstract val typeName: String

Expand Down Expand Up @@ -144,4 +144,4 @@ fun ChoicePointContext?.appendPrimitives(
fun ChoicePointContext?.appendRules(
alternatives: Cursor<out Rule>,
executionContext: ClassicExecutionContext? = null
): ChoicePointContext? = ChoicePointContext.Rules(alternatives, executionContext, this, nextDepth())
): ChoicePointContext? = ChoicePointContext.Rules(alternatives, executionContext, this, nextDepth())
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package it.unibo.tuprolog.solve

import it.unibo.tuprolog.core.*
import it.unibo.tuprolog.core.Rule
import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.core.Substitution
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.core.Truth
import it.unibo.tuprolog.core.Var
import it.unibo.tuprolog.core.operators.OperatorSet
import it.unibo.tuprolog.solve.channel.InputChannel
import it.unibo.tuprolog.solve.channel.OutputChannel
Expand Down Expand Up @@ -125,20 +130,20 @@ data class ClassicExecutionContext(

override fun toString(): String {
return "ClassicExecutionContext(" +
"query=$query, " +
"procedure=$procedure, " +
"substitution=$substitution, " +
"goals=$goals, " +
"rules=$rules, " +
"primitives=$primitives, " +
"startTime=$startTime, " +
"operators=${operators.joinToString(",", "{", "}") { "'${it.functor}':${it.specifier}" }}, " +
"inputChannels=${inputChannels.keys}, " +
"outputChannels=${outputChannels.keys}, " +
"maxDuration=$maxDuration, " +
"choicePoints=$choicePoints, " +
"depth=$depth, " +
"step=$step" +
")"
"query=$query, " +
"procedure=$procedure, " +
"substitution=$substitution, " +
"goals=$goals, " +
"rules=$rules, " +
"primitives=$primitives, " +
"startTime=$startTime, " +
"operators=${operators.joinToString(",", "{", "}") { "'${it.functor}':${it.specifier}" }}, " +
"inputChannels=${inputChannels.keys}, " +
"outputChannels=${outputChannels.keys}, " +
"maxDuration=$maxDuration, " +
"choicePoints=$choicePoints, " +
"depth=$depth, " +
"step=$step" +
")"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ internal open class ClassicSolver(
require(newState.context.step == newStep)
state = newState
}.asSequence()

}

override val libraries: Libraries
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ object ClassicSolverFactory : SolverFactory {
mapOf(STDIN to stdIn),
mapOf(STDOUT to stdOut, STDERR to stdErr, WARNINGS to warnings)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,4 @@ internal class MutableClassicSolver(
}
return result
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ internal class SolutionIterator(
private fun Substitution.Unifier.cleanUp(toRetain: Set<Var>): Substitution.Unifier {
return filter { _, term -> (term !is Var) || (term in toRetain) }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@file:JvmName("SolverExtensions")

package it.unibo.tuprolog.solve

import it.unibo.tuprolog.solve.channel.InputChannel
Expand Down Expand Up @@ -59,4 +60,4 @@ fun MutableSolver.Companion.classicWithDefaultBuiltins(
stdErr: OutputChannel<String> = ClassicSolverFactory.defaultErrorChannel,
warnings: OutputChannel<PrologWarning> = ClassicSolverFactory.defaultWarningsChannel
): MutableSolver =
ClassicSolverFactory.mutableSolverWithDefaultBuiltins(libraries, flags, staticKb, dynamicKb, stdIn, stdOut, stdErr, warnings)
ClassicSolverFactory.mutableSolverWithDefaultBuiltins(libraries, flags, staticKb, dynamicKb, stdIn, stdOut, stdErr, warnings)
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ internal abstract class AbstractEndState(
) : EndState, AbstractState(context) {

override fun computeNext(): State = throw NoSuchElementException()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,3 @@ internal abstract class AbstractState(override val context: ClassicExecutionCont

protected fun previousDepth(): Int = context.depth - 1
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ interface EndState : State {

val hasOpenAlternatives: Boolean
get() = solution is Solution.Yes && context.hasOpenAlternatives
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import it.unibo.tuprolog.solve.exception.TuPrologRuntimeException

interface ExceptionalState : State {
val exception: TuPrologRuntimeException
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,3 @@ fun State.clone(context: ClassicExecutionContext = this.context): State =
is StateRuleSelection -> copy(context)
else -> throw IllegalStateException()
}


Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ internal data class StateBacktracking(override val context: ClassicExecutionCont
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ internal data class StateEnd(
super.computeNext()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,4 @@ internal data class StateException(
else -> finalState
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import it.unibo.tuprolog.solve.Solution

internal data class StateGoalSelection(override val context: ClassicExecutionContext) : AbstractState(context) {
override fun computeNext(): State {

return if (context.goals.isOver) {
if (context.isRoot) {
StateEnd(
Expand Down Expand Up @@ -44,4 +43,4 @@ internal data class StateGoalSelection(override val context: ClassicExecutionCon
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ internal data class StateHalt(
override val exception: TuPrologRuntimeException,
override val context: ClassicExecutionContext
) : ExceptionalState, AbstractEndState(
Solution.Halt(context.query, exception), context
)
Solution.Halt(context.query, exception),
context
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ internal data class StateInit(override val context: ClassicExecutionContext) : A
).appendRulesAndChoicePoints(Cursor.empty())
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ internal data class StatePrimitiveExecution(override val context: ClassicExecuti
)
}


override fun computeNext(): State = try {
when (val sol = context.primitives.current?.solution) {
is Solution.Yes -> {
Expand Down Expand Up @@ -55,5 +54,4 @@ internal data class StatePrimitiveExecution(override val context: ClassicExecuti
context.copy(step = nextStep())
)
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ internal data class StatePrimitiveSelection(override val context: ClassicExecuti
val signature = goal.extractSignature()

if (libraries.hasPrimitive(signature)) {

val req = toRequest(goal, signature)
val primitive = libraries.primitives[signature]
?: error("Inconsistent behaviour of Library.contains and Library.get")
Expand Down Expand Up @@ -69,5 +68,4 @@ internal data class StatePrimitiveSelection(override val context: ClassicExecuti
}
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ internal data class StateRuleExecution(override val context: ClassicExecutionCon
else -> failureState
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package it.unibo.tuprolog.solve.fsm

import it.unibo.tuprolog.core.*
import it.unibo.tuprolog.core.Atom
import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.core.Truth
import it.unibo.tuprolog.core.Var
import it.unibo.tuprolog.solve.ChoicePointContext
import it.unibo.tuprolog.solve.ClassicExecutionContext
import it.unibo.tuprolog.solve.Signature
Expand Down Expand Up @@ -84,25 +88,25 @@ internal data class StateRuleSelection(override val context: ClassicExecutionCon

private fun ClassicExecutionContext.computeCutLimit(magicCut: Boolean = false): CutLimit {
val cutLimit = if (magicCut) {
this.pathToRoot.firstOrNull()
} else {
this.pathToRoot.firstOrNull { it.procedure?.extractSignature() !in transparentToCut }
}
return if (cutLimit == null) {
CutLimit.None
} else {
CutLimit.Actual(cutLimit.depth, cutLimit.procedure)
}
this.pathToRoot.firstOrNull()
} else {
this.pathToRoot.firstOrNull { it.procedure?.extractSignature() !in transparentToCut }
}
return if (cutLimit == null) {
CutLimit.None
} else {
CutLimit.Actual(cutLimit.depth, cutLimit.procedure)
}
}

private fun ChoicePointContext?.performCut(cutLimit: CutLimit): ChoicePointContext? {
return when {
this === null -> {
null
}
cutLimit is CutLimit.None ||
cutLimit.depthToCut > executionContext!!.depth ||
cutLimit.depthToCut == executionContext!!.depth && cutLimit.procedure != executionContext!!.procedure -> {
cutLimit.depthToCut > executionContext!!.depth ||
cutLimit.depthToCut == executionContext!!.depth && cutLimit.procedure != executionContext!!.procedure -> {
this
}
else -> {
Expand All @@ -122,7 +126,6 @@ internal data class StateRuleSelection(override val context: ClassicExecutionCon
}

override fun computeNext(): State {

return when (val currentGoal = context.currentGoal!!) {
is Var -> {
exceptionalState(
Expand Down Expand Up @@ -183,4 +186,4 @@ internal data class StateRuleSelection(override val context: ClassicExecutionCon
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package it.unibo.tuprolog.solve.fsm

import it.unibo.tuprolog.core.*
import it.unibo.tuprolog.solve.*
import it.unibo.tuprolog.core.Clause
import it.unibo.tuprolog.core.Rule
import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.core.Tuple
import it.unibo.tuprolog.core.Var
import it.unibo.tuprolog.solve.ClassicExecutionContext
import it.unibo.tuprolog.solve.Signature
import it.unibo.tuprolog.solve.appendPrimitives
import it.unibo.tuprolog.solve.appendRules
import it.unibo.tuprolog.solve.primitive.Solve
import it.unibo.tuprolog.utils.Cursor
import it.unibo.tuprolog.utils.cursor


fun Sequence<Clause>.ensureRules(): Cursor<out Rule> =
@Suppress("USELESS_CAST")
map { require(it is Rule); it as Rule }.cursor()
Expand Down Expand Up @@ -99,4 +106,4 @@ fun ClassicExecutionContext.toRequest(
substitution = substitution
),
executionMaxDuration = maxDuration
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ object DefaultBuiltins : AliasedLibrary by CommonBuiltins {
Throw
).map { it.descriptionPair }.toMap()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package it.unibo.tuprolog.solve.stdlib.primitive
import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.core.Term
import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.primitive.Solve
import it.unibo.tuprolog.solve.exception.PrologError
import it.unibo.tuprolog.solve.exception.TuPrologRuntimeException
import it.unibo.tuprolog.solve.exception.error.MessageError
import it.unibo.tuprolog.solve.primitive.Solve
import it.unibo.tuprolog.solve.primitive.UnaryPredicate

object Throw : UnaryPredicate<ExecutionContext>("throw") {
Expand All @@ -30,4 +30,4 @@ object Throw : UnaryPredicate<ExecutionContext>("throw") {
)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ object Call : RuleWrapper<ClassicExecutionContext>("call", 1) {
structOf(EnsureExecutable.functor, varOf("G")),
varOf("G")
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ object Catch : RuleWrapper<ClassicExecutionContext>("catch", 3) {

override val Scope.body: Term
get() = varOf("G")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ object Comma : RuleWrapper<ClassicExecutionContext>(",", 2) {

override val Scope.body: Term
get() = tupleOf(varOf("A"), varOf("B"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package it.unibo.tuprolog.solve.stdlib.rule
import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.rule.RuleWrapper

object Cut : RuleWrapper<ExecutionContext>("!", 0)
object Cut : RuleWrapper<ExecutionContext>("!", 0)
Loading

0 comments on commit 63c3396

Please sign in to comment.