You are on page 1of 2

import io.sarl.core.

Behaviors
import io.sarl.core.Initialize
import io.sarl.core.InnerContextAccess
import io.sarl.core.Lifecycle
import io.sarl.core.Logging
import io.sarl.core.Schedules
import io.sarl.lang.core.Address
agent Auctioneer {
uses Behaviors, Lifecycle, InnerContextAccess, Schedules, Logging
var maxBid = 0f
var winner : Address
var hasBid = false
var isAuctionOpened = true
on Initialize {
for(i : 1..3) {
spawnInContext(Bidder, innerContext)
}

wake(new Price(50))
in(10000) [
val waitTask = task("wait-task")
waitTask.every(10000) [
synchronized(this) {
if (!isAuctionOpened) {
if (!hasMemberAgent) {
waitTask.cancel
killMe
}
} else {
if (!hasBid) {
isAuctionOpened = false
if (winner === null) {
println("No winner")
} else {
println("The winner is " + winner
+ " with the bid of " + maxBid)
}
wake(new StopAuction)
}
hasBid = false
}
}
]
]
}
on Bid [ isAuctionOpened ] {
synchronized(this) {
hasBid = true
if (occurrence.value > maxBid) {
maxBid = occurrence.value
winner = occurrence.source
}
}
}
}

You might also like