Skip to content

Commit

Permalink
fix tests on time-related behaviours and make sleep/1 a standard builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Jun 5, 2020
1 parent 552db2d commit d961a89
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ object CommonBuiltins : AliasedLibrary by Library.of(
Halt,
Integer,
Is,
TermNotIdentical,
Natural,
NewLine,
NonVar,
NotUnifiableWith,
Number,
Sleep,
TermIdentical,
TermNotIdentical,
UnifiesWith,
Var,
Write
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package it.unibo.tuprolog.solve.stdlib.primitive

import it.unibo.tuprolog.core.Integer
import it.unibo.tuprolog.solve.ExecutionContext
import it.unibo.tuprolog.solve.Solve
import it.unibo.tuprolog.solve.currentTimeInstant
import it.unibo.tuprolog.solve.exception.TimeOutException
import it.unibo.tuprolog.solve.primitive.PrimitiveWrapper

/**
* Implements predicate `sleep(+N)` where `N` must be instantiated as an integer.
* The predicate execution always succeeds, unless the resolution process is halted because of a [TimeOutException].
* Furthermore, the resolution of a `sleep(N)` sub-goal is guaranteed to require at least `N` milliseconds
*/
object Sleep : PrimitiveWrapper<ExecutionContext>("sleep", 1) {
override fun uncheckedImplementation(request: Solve.Request<ExecutionContext>): Sequence<Solve.Response> =
sequence {
request.ensuringAllArgumentsAreInstantiated()
.ensuringArgumentIsInteger(0).let {
val initialTime = currentTimeInstant()
val threshold = request.arguments[0].castTo<Integer>().intValue.toLongExact()
while (currentTimeInstant() - initialTime < threshold);
yield(request.replySuccess())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import it.unibo.tuprolog.solve.TestingClauseTheories.simpleCutAndConjunctionTheo
import it.unibo.tuprolog.solve.TestingClauseTheories.simpleCutTheoryNotableGoalToSolutions
import it.unibo.tuprolog.solve.TestingClauseTheories.simpleFactTheoryNotableGoalToSolutions
import it.unibo.tuprolog.solve.TimeRelatedTheories.lessThan500MsGoalToSolution
import it.unibo.tuprolog.solve.TimeRelatedTheories.slightlyMoreThan1100MsGoalToSolution
import it.unibo.tuprolog.solve.TimeRelatedTheories.slightlyMoreThan1800MsGoalToSolution
import it.unibo.tuprolog.solve.TimeRelatedTheories.slightlyMoreThan600MsGoalToSolution
import it.unibo.tuprolog.solve.TimeRelatedTheories.slightlyMoreThan700MsGoalToSolution
import it.unibo.tuprolog.solve.TimeRelatedTheories.slightlyMoreThan500MsGoalToSolution

/** A prototype class for testing solver implementations */
Expand Down Expand Up @@ -74,10 +74,10 @@ interface SolverTest {
/** Test with [slightlyMoreThan500MsGoalToSolution] */
fun testTimeout2()

/** Test with [slightlyMoreThan1100MsGoalToSolution] */
/** Test with [slightlyMoreThan600MsGoalToSolution] */
fun testTimeout3()

/** Test with [slightlyMoreThan1800MsGoalToSolution] */
/** Test with [slightlyMoreThan700MsGoalToSolution] */
fun testTimeout4()

/** Test with [ifThen1ToSolution] */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ import it.unibo.tuprolog.solve.TestingClauseTheories.simpleCutTheory
import it.unibo.tuprolog.solve.TestingClauseTheories.simpleCutTheoryNotableGoalToSolutions
import it.unibo.tuprolog.solve.TestingClauseTheories.simpleFactTheory
import it.unibo.tuprolog.solve.TestingClauseTheories.simpleFactTheoryNotableGoalToSolutions
import it.unibo.tuprolog.solve.TestingPrimitives.timeLibrary
import it.unibo.tuprolog.solve.TimeRelatedTheories.lessThan500MsGoalToSolution
import it.unibo.tuprolog.solve.TimeRelatedTheories.slightlyMoreThan1100MsGoalToSolution
import it.unibo.tuprolog.solve.TimeRelatedTheories.slightlyMoreThan1800MsGoalToSolution
import it.unibo.tuprolog.solve.TimeRelatedTheories.slightlyMoreThan600MsGoalToSolution
import it.unibo.tuprolog.solve.TimeRelatedTheories.slightlyMoreThan700MsGoalToSolution
import it.unibo.tuprolog.solve.TimeRelatedTheories.slightlyMoreThan500MsGoalToSolution
import it.unibo.tuprolog.solve.TimeRelatedTheories.timeRelatedTheory
import it.unibo.tuprolog.solve.exception.TimeOutException
Expand Down Expand Up @@ -283,8 +282,7 @@ internal class SolverTestImpl(private val solverFactory: SolverFactory) : Solver
override fun testTimeout1() {
assertSolverSolutionsCorrect(
solver = solverFactory.solverWithDefaultBuiltins(
staticKb = timeRelatedTheory,
otherLibraries = Libraries(timeLibrary)
staticKb = timeRelatedTheory
),
goalToSolutions = lessThan500MsGoalToSolution,
maxDuration = 400L
Expand All @@ -295,35 +293,32 @@ internal class SolverTestImpl(private val solverFactory: SolverFactory) : Solver
override fun testTimeout2() {
assertSolverSolutionsCorrect(
solver = solverFactory.solverWithDefaultBuiltins(
staticKb = timeRelatedTheory,
otherLibraries = Libraries(timeLibrary)
staticKb = timeRelatedTheory
),
goalToSolutions = slightlyMoreThan500MsGoalToSolution,
maxDuration = 1000L
maxDuration = 599L
)
}

/** Test with [slightlyMoreThan1100MsGoalToSolution] */
/** Test with [slightlyMoreThan600MsGoalToSolution] */
override fun testTimeout3() {
assertSolverSolutionsCorrect(
solver = solverFactory.solverWithDefaultBuiltins(
staticKb = timeRelatedTheory,
otherLibraries = Libraries(timeLibrary)
staticKb = timeRelatedTheory
),
goalToSolutions = slightlyMoreThan1100MsGoalToSolution,
maxDuration = 1700L
goalToSolutions = slightlyMoreThan600MsGoalToSolution,
maxDuration = 699L
)
}

/** Test with [slightlyMoreThan1800MsGoalToSolution] */
/** Test with [slightlyMoreThan700MsGoalToSolution] */
override fun testTimeout4() {
assertSolverSolutionsCorrect(
solver = solverFactory.solverWithDefaultBuiltins(
staticKb = timeRelatedTheory,
otherLibraries = Libraries(timeLibrary)
staticKb = timeRelatedTheory
),
goalToSolutions = slightlyMoreThan1800MsGoalToSolution,
maxDuration = 2000L
goalToSolutions = slightlyMoreThan700MsGoalToSolution,
maxDuration = 799L
)
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ object TimeRelatedTheories {
/**
* Notable request goal over [timeRelatedTheory] and respective expected [Solution]s.
* In particular the requested goal assumes the resolution terminates in slightly more than 500 ms
* (and, in any case, within 1100 ms),
* (and, in any case, within 600 ms),
* thus a [TimeOutException] is returned as the second solution
*/
val slightlyMoreThan500MsGoalToSolution by lazy {
Expand All @@ -63,11 +63,11 @@ object TimeRelatedTheories {

/**
* Notable request goal over [timeRelatedTheory] and respective expected [Solution]s.
* In particular the requested goal assumes the resolution terminates in slightly more than 1100 ms
* (and, in any case, within 1800 ms),
* In particular the requested goal assumes the resolution terminates in slightly more than 600 ms
* (and, in any case, within 700 ms),
* thus a [TimeOutException] is returned as the third solution
*/
val slightlyMoreThan1100MsGoalToSolution by lazy {
val slightlyMoreThan600MsGoalToSolution by lazy {
prolog {
ktListOf(
"a"("X").hasSolutions(
Expand All @@ -81,10 +81,10 @@ object TimeRelatedTheories {

/**
* Notable request goal over [timeRelatedTheory] and respective expected [Solution]s.
* In particular the requested goal assumes the resolution terminates in slightly more than 1800 ms,
* In particular the requested goal assumes the resolution terminates in slightly more than 700 ms,
* thus no [TimeOutException] is returned and 3 positive solutions are returned instead
*/
val slightlyMoreThan1800MsGoalToSolution by lazy {
val slightlyMoreThan700MsGoalToSolution by lazy {
prolog {
ktListOf(
"a"("X").hasSolutions(
Expand Down

0 comments on commit d961a89

Please sign in to comment.