1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 16:28:12 -07:00

Restore the DynamicGraph name.

This commit is contained in:
Cedric Beust 2016-04-17 10:29:50 -07:00
parent 63ba0ea63e
commit 408e00c0ae
4 changed files with 30 additions and 30 deletions

View file

@ -23,7 +23,7 @@ class Node<T>(val value: T) {
override fun toString() = value.toString()
}
class DG<T> {
class DynamicGraph<T> {
val VERBOSE = 1
val values : Collection<T> get() = nodes.map { it.value }
val nodes = hashSetOf<Node<T>>()
@ -101,7 +101,7 @@ class DG<T> {
}
}
interface IWorker2<T> : Callable<TaskResult2<T>> {
interface IWorker<T> : Callable<TaskResult2<T>> {
/**
* @return list of tasks this worker is working on.
*/
@ -113,7 +113,7 @@ interface IWorker2<T> : Callable<TaskResult2<T>> {
val priority : Int
}
interface IThreadWorkerFactory2<T> {
interface IThreadWorkerFactory<T> {
/**
* Creates {@code IWorker} for specified set of tasks. It is not necessary that
@ -122,10 +122,10 @@ interface IThreadWorkerFactory2<T> {
* @param nodes tasks that need to be executed
* @return list of workers
*/
fun createWorkers(nodes: Collection<T>) : List<IWorker2<T>>
fun createWorkers(nodes: Collection<T>) : List<IWorker<T>>
}
class DGExecutor<T>(val graph : DG<T>, val factory: IThreadWorkerFactory2<T>) {
class DynamicGraphExecutor<T>(val graph : DynamicGraph<T>, val factory: IThreadWorkerFactory<T>) {
val executor = Executors.newFixedThreadPool(5, NamedThreadFactory("DynamicGraphExecutor"))
val completion = ExecutorCompletionService<TaskResult2<T>>(executor)
@ -144,7 +144,7 @@ class DGExecutor<T>(val graph : DG<T>, val factory: IThreadWorkerFactory2<T>) {
var newFreeNodes = HashSet<T>(graph.freeNodes)
while (! gotError && running > 0 || newFreeNodes.size > 0) {
nodesRun.addAll(newFreeNodes)
val callables : List<IWorker2<T>> = factory.createWorkers(newFreeNodes)
val callables : List<IWorker<T>> = factory.createWorkers(newFreeNodes)
callables.forEach { completion.submit(it) }
running += callables.size
@ -184,7 +184,7 @@ class DGExecutor<T>(val graph : DG<T>, val factory: IThreadWorkerFactory2<T>) {
}
fun main(argv: Array<String>) {
val dg = DG<String>().apply {
val dg = DynamicGraph<String>().apply {
// a -> b
// b -> c, d
// e
@ -193,10 +193,10 @@ fun main(argv: Array<String>) {
addEdge("b", "d")
addNode("e")
}
val factory = object : IThreadWorkerFactory2<String> {
override fun createWorkers(nodes: Collection<String>): List<IWorker2<String>> {
val factory = object : IThreadWorkerFactory<String> {
override fun createWorkers(nodes: Collection<String>): List<IWorker<String>> {
return nodes.map {
object: IWorker2<String> {
object: IWorker<String> {
override fun call(): TaskResult2<String>? {
log(1, " Running worker $it")
return TaskResult2(true, null, it)
@ -208,5 +208,5 @@ fun main(argv: Array<String>) {
}
}
DGExecutor(dg, factory).run()
DynamicGraphExecutor(dg, factory).run()
}

View file

@ -103,12 +103,12 @@ public class TaskManager @Inject constructor(val args: Args,
//
log(2, "About to run graph:\n ${graph.dump()} ")
val factory = object : IThreadWorkerFactory2<PluginTask> {
val factory = object : IThreadWorkerFactory<PluginTask> {
override fun createWorkers(nodes: Collection<PluginTask>)
= nodes.map { TaskWorker(listOf(it), args.dryRun, messages) }
}
val executor = DGExecutor(graph, factory)
val executor = DynamicGraphExecutor(graph, factory)
val thisResult = executor.run()
if (thisResult != 0) {
log(2, "Marking project ${project.name} as failed")
@ -129,8 +129,8 @@ public class TaskManager @Inject constructor(val args: Args,
alwaysRunAfter: TreeMultimap<String, String>,
toName: (T) -> String,
accept: (T) -> Boolean):
DG<T> {
val graph = DG<T>()
DynamicGraph<T> {
val graph = DynamicGraph<T>()
taskNames.forEach { taskName ->
val ti = TaskInfo(taskName)
if (!dependencies.keys().contains(ti.taskName)) {
@ -387,7 +387,7 @@ public class TaskManager @Inject constructor(val args: Args,
}
class TaskWorker(val tasks: List<PluginTask>, val dryRun: Boolean, val messages: MutableList<String>)
: IWorker2<PluginTask> {
: IWorker<PluginTask> {
override fun call() : TaskResult2<PluginTask> {
if (tasks.size > 0) {

View file

@ -8,23 +8,23 @@ import java.util.*
class DynamicGraphTest {
private fun <T> assertFreeNodesEquals(graph: DG<T>, expected: Array<T>) {
private fun <T> assertFreeNodesEquals(graph: DynamicGraph<T>, expected: Array<T>) {
val h = HashSet(graph.freeNodes)
val e = HashSet(expected.toList())
Assert.assertEquals(h, e)
}
private fun <T> createFactory(runNodes: ArrayList<T>, errorFunction: (T) -> Boolean) : IThreadWorkerFactory2<T> {
return object: IThreadWorkerFactory2<T> {
override fun createWorkers(nodes: Collection<T>): List<IWorker2<T>> {
val result = arrayListOf<IWorker2<T>>()
private fun <T> createFactory(runNodes: ArrayList<T>, errorFunction: (T) -> Boolean) : IThreadWorkerFactory<T> {
return object: IThreadWorkerFactory<T> {
override fun createWorkers(nodes: Collection<T>): List<IWorker<T>> {
val result = arrayListOf<IWorker<T>>()
nodes.forEach { result.add(Worker(runNodes, it, errorFunction)) }
return result
}
}
}
class Worker<T>(val runNodes: ArrayList<T>, val n: T, val errorFunction: (T) -> Boolean) : IWorker2<T> {
class Worker<T>(val runNodes: ArrayList<T>, val n: T, val errorFunction: (T) -> Boolean) : IWorker<T> {
override val priority = 0
override fun call() : TaskResult2<T> {
@ -36,14 +36,14 @@ class DynamicGraphTest {
@Test
fun testExecutor() {
val dg = DG<String>();
val dg = DynamicGraph<String>();
dg.addEdge("compile", "runApt")
dg.addEdge("compile", "generateVersion")
val runNodes = arrayListOf<String>()
val factory = createFactory(runNodes, { true })
DGExecutor(dg, factory).run()
DynamicGraphExecutor(dg, factory).run()
Assert.assertEquals(runNodes.size, 3)
}
@ -51,7 +51,7 @@ class DynamicGraphTest {
@Test
private fun testExecutorWithSkip() {
val g = DG<Int>()
val g = DynamicGraph<Int>()
// 2 and 3 depend on 1, 4 depend on 3, 10 depends on 4
// 3 will blow up, which should make 4 and 10 skipped
g.addEdge(2, 1)
@ -61,7 +61,7 @@ class DynamicGraphTest {
g.addEdge(5, 2)
val runNodes = arrayListOf<Int>()
val factory = createFactory(runNodes, { n -> n != 3 })
val ex = DGExecutor(g, factory)
val ex = DynamicGraphExecutor(g, factory)
ex.run()
Thread.`yield`()
Assert.assertTrue(! runNodes.contains(4))
@ -70,7 +70,7 @@ class DynamicGraphTest {
@Test
fun test8() {
DG<String>().apply {
DynamicGraph<String>().apply {
addEdge("b1", "a1")
addEdge("b1", "a2")
addEdge("b2", "a1")
@ -97,7 +97,7 @@ class DynamicGraphTest {
@Test
fun test2() {
DG<String>().apply {
DynamicGraph<String>().apply {
addEdge("b1", "a1")
addEdge("b1", "a2")
addNode("x")
@ -131,7 +131,7 @@ class DynamicGraphTest {
@Test
fun runAfter() {
DG<String>().apply {
DynamicGraph<String>().apply {
// a -> b
// b -> c, d
// e

View file

@ -11,7 +11,7 @@ import org.testng.annotations.Test
@Guice(modules = arrayOf(TestModule::class))
class TaskManagerTest @Inject constructor(val taskManager: TaskManager) {
class DryRunGraphExecutor<T>(val graph: DG<T>) {
class DryRunGraphExecutor<T>(val graph: DynamicGraph<T>) {
fun run() : List<T> {
val result = arrayListOf<T>()
var freeNodes = graph.freeNodes