-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix tests on time-related behaviours and make sleep/1 a standard builtin
- Loading branch information
Showing
6 changed files
with
51 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/stdlib/primitive/Sleep.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 0 additions & 31 deletions
31
test-solve/src/commonMain/kotlin/it/unibo/tuprolog/solve/TestingPrimitives.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters