protelis.state.nonselfstabilizing.time.pt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of protelis-lang Show documentation
Show all versions of protelis-lang Show documentation
Essential libraries for the Protelis language
module protelis:state:nonselfstabilizing:time
import protelis:state:time
/**
* @param event bool, event
* @return bool, true if the event is always verified
*/
public def allTime(event) {
rep (t <- event) {
t && event
}
}
/**
* @param event bool, event
* @return bool, whether the event has occurred at least once
*/
public def anyTime(event) {
rep (t <- event) {
t || event
}
}
/**
* Counter.
*
* @param start num, counter initial value
* @param inc num, how much to increase the counter
* @return num, current status of the counter
*/
public def counter(start, inc) {
rep (c <- start) {
c + inc
}
}
/**
* How many times an event occurred.
*
* @param event bool, event to be monitored
* @return num, how many times an event occurred
*/
public def countTrue(event) {
rep (c <- 0) {
c + if (event) { 1 } else { 0 }
}
}
/**
* @param timeout num, timeout
* @param event bool, event
* @return bool, true if the event did not occurred within last time period
*/
public def falseDuringLast(timeout, event) {
falseDuringLastWithDt(timeout, event, self.getDeltaTime())
}
/**
* @param timeout num, timeout
* @param event bool, event
* @param dt num, dt
* @return bool, true if the event did not occurred within last time period
*/
public def falseDuringLastWithDt(timeout, event, dt) {
trueDuringLastWithDt(timeout, !event, dt)
}
/**
* @param timeout num, timeout
* @param event bool, event
* @return bool, true if a false condition has persisted for time
*/
public def falseFor(time, event) {
falseForWithDt(time, event, self.getDeltaTime())
}
/**
* @param timeout num, timeout
* @param event bool, event
* @param dt num, dt
* @return bool, true if a false condition has persisted for time
*/
public def falseForWithDt(time, event, dt) {
trueForWithDt(time, !event, dt)
}
/**
* Flip two values at each round. Return b at first.
*
* @param a num, first value
* @param b num, second value
* @return num, (ba)+ sequence of values
*/
public def flip(a, b) {
rep (c <- a) {
(a + b) - c
}
}
/**
* Evaluate an expression once, then store and return the result thereafter
*
* @param f local function of no arguments, run once and then its result is stored
* @return the value produced by evaluating f
*/
public def once(f) {
rep (v <- f.apply()) { v }
}
/**
* @param timeout num, timeout
* @param event bool, event
* @return bool, true if the event occurred within last time period
*/
public def trueDuringLast(timeout, event) {
trueDuringLastWithDt(timeout, event, self.getDeltaTime())
}
/**
* @param timeout num, timeout
* @param event bool, event
* @param dt num, dt
* @return bool, true if the event occurred within last time period
*/
public def trueDuringLastWithDt(timeout, event, dt) {
if (event) {
true
} else {
timeSinceStartWithDt(dt) <= timeout
}
}
/**
* @param timeout num, timeout
* @param event bool, event
* @return bool, true if a true condition has persisted for time
*/
public def trueFor(time, event) {
trueForWithDt(time, event, self.getDeltaTime())
}
/**
* @param timeout num, timeout
* @param event bool, event
* @param dt num, dt
* @return bool, true if a true condition has persisted for time
*/
public def trueForWithDt(time, event, dt) {
if (event) {
timeSinceStartWithDt(dt) >= time
} else {
false
}
}
/**
* @param f () -> T, what to execute first
* @param condition (T) -> bool, whether it is possible to execute continuation
* @param g (T) -> T', what to execute next
* @return T|T', apply f until condition becomes true, apply g next
*/
public def sequence(f, condition, g) {
rep (v <- [false, f.apply()]) {
if (v.get(0) || condition.apply(v.get(1))) {
[true, g.apply(v.get(1))]
} else {
[false, f.apply()]
}
}.get(1)
}
/**
* @param f () -> T, what to execute first
* @param condition bool, boolean condition
* @param g (T) -> T', what to execute next
* @return T|T', apply f until condition becomes true in any device,
* apply g next
*/
public def sequenceIfAny(f, condition, g) {
sequence(f, (v) -> {
share (_,ev <- condition) {
anyHood(ev) || condition
}
}, g)
}
/**
* @param f () -> T, what to execute first
* @param condition bool, boolean condition
* @param g (T) -> T', what to execute next
* @return T|T', apply f until condition becomes true in all the devices,
* apply g next
*/
public def sequenceIfAll(f, condition, g) {
sequence(f, (ev) -> {
allHood(nbr(condition)) && condition
}, g)
}
/**
* Time flow.
*
* @return num, time since start
*/
public def timeSinceStart() {
timeSinceStartWithDt(self.getDeltaTime())
}
/**
* Time flow.
* @param dt num, increase the counter of dt
* @return num, time since start
*/
public def timeSinceStartWithDt(dt) {
counter(0, dt)
}