-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
383 additions
and
13 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
8 changes: 8 additions & 0 deletions
8
js7-base/shared/src/test/scala/js7/base/utils/BigDecimalTest.scala
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,8 @@ | ||
package js7.base.utils | ||
|
||
import js7.base.test.OurTestSuite | ||
|
||
final class BigDecimalTest extends OurTestSuite: | ||
|
||
"toLong does not convert numerically (but bitwise)" in: | ||
assert((BigDecimal(Long.MaxValue) + 1).toLong == Long.MinValue) |
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
62 changes: 62 additions & 0 deletions
62
js7-data/jvm/src/main/scala/js7/data/execution/workflow/instructions/SleepExecutor.scala
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,62 @@ | ||
package js7.data.execution.workflow.instructions | ||
|
||
import js7.base.problem.Checked | ||
import js7.base.time.ScalaTime.* | ||
import js7.base.utils.ScalaUtils.syntax.RichBoolean | ||
import js7.data.event.KeyedEvent | ||
import js7.data.execution.workflow.instructions.SleepExecutor.* | ||
import js7.data.order.OrderEvent.{OrderMoved, OrderSleeping, OrderStarted} | ||
import js7.data.order.OrderObstacle.WaitingForOtherTime | ||
import js7.data.order.{Order, OrderObstacleCalculator} | ||
import js7.data.state.StateView | ||
import js7.data.value.NumberValue | ||
import js7.data.workflow.instructions.Sleep | ||
import scala.concurrent.duration.* | ||
|
||
private[instructions] final class SleepExecutor(protected val service: InstructionExecutorService) | ||
extends EventInstructionExecutor: | ||
|
||
type Instr = Sleep | ||
val instructionClass = classOf[Sleep] | ||
|
||
def toEvents(instr: Sleep, order: Order[Order.State], state: StateView) | ||
: Checked[List[KeyedEvent[OrderStarted | OrderSleeping | OrderMoved]]] = | ||
start(order).getOrElse: | ||
order.ifState[Order.Ready].map: order => | ||
for | ||
scope <- state.toImpureOrderExecutingScope(order, clock.now()) | ||
value <- instr.duration.eval(scope).map(_.missingTo(NumberValue.Zero)) | ||
number <- value.toNumberValue | ||
duration = bigDecimalSecondsToDuration(number.number) | ||
yield | ||
if duration.isPositive then | ||
(order.id <-: OrderSleeping(clock.now() + duration)) :: Nil | ||
else | ||
(order.id <-: OrderMoved(order.position.increment)) :: Nil | ||
.orElse: | ||
order.ifState[Order.Sleeping].map: order => | ||
Right: | ||
order.state.until <= clock.now() thenList: | ||
order.id <-: OrderMoved(order.position.increment) | ||
.getOrElse: | ||
Right(Nil) | ||
|
||
override def toObstacles(order: Order[Order.State], calculator: OrderObstacleCalculator) = | ||
order.state match | ||
case Order.Sleeping(until) => | ||
Right(Set(WaitingForOtherTime(until))) | ||
|
||
case _ => | ||
super.toObstacles(order, calculator) | ||
|
||
|
||
object SleepExecutor: | ||
|
||
private[instructions] def bigDecimalSecondsToDuration(number: BigDecimal): FiniteDuration = | ||
val nanos = number * 1_000_000_000 | ||
if nanos > Long.MaxValue then | ||
1.h * 24 * 365 * 100 | ||
else if nanos < 0 then | ||
ZeroDuration | ||
else | ||
nanos.toLong.ns |
13 changes: 13 additions & 0 deletions
13
js7-data/jvm/src/test/scala/js7/data/execution/workflow/instructions/SleepExecutorTest.scala
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,13 @@ | ||
package js7.data.execution.workflow.instructions | ||
|
||
import js7.base.test.OurTestSuite | ||
import js7.base.time.ScalaTime.* | ||
import js7.data.execution.workflow.instructions.SleepExecutor.bigDecimalSecondsToDuration | ||
import scala.concurrent.duration.FiniteDuration | ||
|
||
final class SleepExecutorTest extends OurTestSuite: | ||
|
||
"bigDecimalSecondsToDuration" in: | ||
assert(bigDecimalSecondsToDuration(BigDecimal(Long.MaxValue) + 1) == 1.h * 24 * 365 * 100) | ||
assert(bigDecimalSecondsToDuration(-1) == 0.s) | ||
assert(bigDecimalSecondsToDuration(1.234567890) == 1234567890.ns) |
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
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
20 changes: 20 additions & 0 deletions
20
js7-data/shared/src/main/scala/js7/data/workflow/instructions/Sleep.scala
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,20 @@ | ||
package js7.data.workflow.instructions | ||
|
||
import io.circe.Codec | ||
import io.circe.generic.semiauto.deriveCodec | ||
import js7.data.source.SourcePos | ||
import js7.data.value.expression.Expression | ||
import js7.data.workflow.Instruction | ||
|
||
final case class Sleep(duration: Expression, sourcePos: Option[SourcePos] = None) | ||
extends Instruction.NoInstructionBlock: | ||
|
||
def withoutSourcePos: Sleep = | ||
copy(sourcePos = None) | ||
|
||
override def toString: String = | ||
s"sleep $duration$sourcePosToString" | ||
|
||
|
||
object Sleep: | ||
given Codec.AsObject[Sleep] = deriveCodec[Sleep] |
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
Oops, something went wrong.