Created
April 10, 2012 06:55
-
-
Save pstutz/2348886 to your computer and use it in GitHub Desktop.
RandomWalk Example Signal/Collect 1.1.2
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
import com.signalcollect._ | |
import com.signalcollect.interfaces.MessageBus | |
import scala.util.Random | |
case class RandomWalker(idList: List[Any] = List(), maxLength: Int = 5) { | |
def extend(id: Any): Option[RandomWalker] = { | |
if (idList.size < maxLength) { | |
Some(RandomWalker(id :: idList, maxLength)) | |
} else { | |
println("This walk has ended, idList = " + idList) | |
None | |
} | |
} | |
} | |
class RandomWalkVertex(id: Any, initialState: List[RandomWalker] = List()) | |
extends DataFlowVertex(id, initialState, resetState = List()) { | |
type Signal = RandomWalker | |
def collect(oldState: State, uncollectedSignals: Iterable[RandomWalker]): List[RandomWalker] = { | |
uncollectedSignals flatMap (_.extend(id)) toList | |
} | |
override def doSignal(messageBus: MessageBus[Any]) { | |
for (randomWalker <- state) { | |
val edgeIndex = Random.nextInt(outgoingEdges.size) | |
val edgeIterator = outgoingEdges.values.iterator | |
var currentIndex = 0 | |
var edge = edgeIterator.next | |
while (currentIndex < edgeIndex) { | |
edge = edgeIterator.next | |
currentIndex += 1 | |
} | |
graphEditor.sendSignalToVertex(randomWalker, edge.id.targetId) | |
} | |
} | |
} | |
object RandomWalk extends App { | |
val graph = GraphBuilder.build | |
graph.addVertex(new RandomWalkVertex(1)) | |
graph.addVertex(new RandomWalkVertex(2)) | |
graph.addVertex(new RandomWalkVertex(3)) | |
graph.addVertex(new RandomWalkVertex(4)) | |
graph.addVertex(new RandomWalkVertex(5)) | |
graph.addVertex(new RandomWalkVertex(6)) | |
graph.addEdge(new StateForwarderEdge(1, 2)) | |
graph.addEdge(new StateForwarderEdge(2, 3)) | |
graph.addEdge(new StateForwarderEdge(3, 4)) | |
graph.addEdge(new StateForwarderEdge(1, 5)) | |
graph.addEdge(new StateForwarderEdge(4, 6)) | |
graph.addEdge(new StateForwarderEdge(5, 6)) | |
graph.addEdge(new StateForwarderEdge(6, 1)) | |
graph.sendSignalToVertex(RandomWalker(), 1) | |
graph.sendSignalToVertex(RandomWalker(), 1) | |
graph.sendSignalToVertex(RandomWalker(), 1) | |
graph.sendSignalToVertex(RandomWalker(), 1) | |
val stats = graph.execute | |
println(stats) | |
graph.foreachVertex(println(_)) | |
graph.shutdown | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Philip,
Not sure if this is the right interpretation in the new api. please review when you get a chance
import com.signalcollect._
import com.signalcollect.interfaces.MessageBus
import scala.util.Random
import com.signalcollect._
import com.signalcollect.interfaces.MessageBus
import scala.util.Random
import com.signalcollect.OptionalSignalEdge
case class RandomWalker(idList: List[Any] = List(), maxLength: Int = 5) {
def extend(id: Any): Option[RandomWalker] = {
if (idList.size < maxLength) {
Some(RandomWalker(id :: idList, maxLength))
} else {
println("This walk has ended, idList = " + idList)
None
}
}
}
class RandomWalkVertex(id: Any, initialState: List[RandomWalker] = List(RandomWalker()))
extends DataFlowVertex(id, initialState) {
type Signal = List[RandomWalker]
def collect(signal: List[RandomWalker]): List[RandomWalker] = {
signal.map(f=> f.extend(id) match{
case None => f
case Some(x) => x
})
}
}
class RandomWalkEdge(t: Any) extends OptionalSignalEdge(t){
var count = 0
type Source = RandomWalkVertex
def signal: Signal = {
val randomLength = Random.nextInt(5)
source.state.length compare randomLength match{
case 0 => Some(source.state)
case 1 => Some(source.state.take(randomLength))
case -1 => None
}
}
}
object RandomWalk extends App {
val graph = GraphBuilder.build
graph.addVertex(new RandomWalkVertex(1))
graph.addVertex(new RandomWalkVertex(2))
graph.addVertex(new RandomWalkVertex(3))
graph.addVertex(new RandomWalkVertex(4))
graph.addVertex(new RandomWalkVertex(5))
graph.addVertex(new RandomWalkVertex(6))
graph.addEdge(1, new RandomWalkEdge(2))
graph.addEdge(2, new RandomWalkEdge(3))
graph.addEdge(3, new RandomWalkEdge(4))
graph.addEdge(1, new RandomWalkEdge(5))
graph.addEdge(4, new RandomWalkEdge(6))
graph.addEdge(5, new RandomWalkEdge(6))
graph.addEdge(6, new RandomWalkEdge(1))
val stats = graph.execute
println(stats)
graph.foreachVertex(println(_))
graph.shutdown
}