Skip to content

Commit

Permalink
reimplement Retract
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Jun 23, 2020
1 parent 581f84b commit 73d0e0e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,19 @@ import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.SideEffect
import it.unibo.tuprolog.solve.Solve
import it.unibo.tuprolog.solve.primitive.UnaryPredicate
import it.unibo.tuprolog.theory.RetractResult
import it.unibo.tuprolog.unify.Unificator.Companion.mguWith
import it.unibo.tuprolog.utils.buffered

// TODO reimplement
object Retract : UnaryPredicate<ExecutionContext>("retract") {
override fun Solve.Request<ExecutionContext>.computeAll(first: Term): Sequence<Solve.Response> {
ensuringArgumentIsStruct(0)
val clause = if (first is Clause) first else Rule.of(first as Struct, Var.anonymous())
return sequence {
var dynamicKb = context.dynamicKb
while (true) {
val result = dynamicKb.retract(clause)
if (result is RetractResult.Success) {
dynamicKb = result.theory
val substitution = when (first) {
is Clause -> (first mguWith result.firstClause) as Substitution.Unifier
else -> (first mguWith result.firstClause.head!!) as Substitution.Unifier
}
yield(
replySuccess(substitution, null, SideEffect.RemoveDynamicClauses(result.firstClause))
)
} else {
break
}
return context.dynamicKb[clause].buffered().map {
val substitution = when (first) {
is Clause -> (first mguWith it) as Substitution.Unifier
else -> (first mguWith it.head!!) as Substitution.Unifier
}
yield(replyFail())
replySuccess(substitution, null, SideEffect.RemoveDynamicClauses(it))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ internal class SolverTestImpl(private val solverFactory: SolverFactory) : Solver
assertSolutionEquals(
ktListOf(
query.yes("X" to listOf(2, 3), "Y" to listOf(1)),
query.yes("X" to listOf(2), "Y" to listOf(1, 2)),
query.yes("X" to listOf(3), "Y" to listOf(1, 2)),
query.yes("X" to emptyList(), "Y" to listOf(1, 2, 3))
),
solutions
Expand Down Expand Up @@ -942,8 +942,7 @@ internal class SolverTestImpl(private val solverFactory: SolverFactory) : Solver
assertSolutionEquals(
ktListOf(
query.yes("X" to 1),
query.yes("X" to 2),
query.no()
query.yes("X" to 2)

),
solutions
Expand Down

0 comments on commit 73d0e0e

Please sign in to comment.