Skip to content

Commit ee4e657

Browse files
committed
docs: add README and Simple example
1 parent 71df6ac commit ee4e657

File tree

5 files changed

+120
-4
lines changed

5 files changed

+120
-4
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# kotlin-flow-graph
2+
3+
A simple flow graph implementation in Kotlin.
4+
It allows you to create a graph of processing nodes that can be connected together to form a processing pipeline.
5+
6+
## Usage
7+
8+
Gradle:
9+
10+
```kotlin
11+
repositories {
12+
maven("https://repo.silenium.dev/releases")
13+
}
14+
15+
dependencies {
16+
implementation("dev.silenium.libs.flow-graph:kotlin-flow-graph:0.1.0")
17+
}
18+
```
19+
20+
Examples:
21+
22+
- [Simple](./examples/src/main/kotlin/dev/silenium/libs/flows/examples/Simple.kt)

build.gradle.kts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ repositories {
1515

1616
dependencies {
1717
val coroutines = "1.8.1"
18-
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines")
19-
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-debug:$coroutines")
20-
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:$coroutines")
21-
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactive:$coroutines")
18+
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines")
19+
api("org.jetbrains.kotlinx:kotlinx-coroutines-debug:$coroutines")
20+
api("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:$coroutines")
21+
api("org.jetbrains.kotlinx:kotlinx-coroutines-reactive:$coroutines")
2222

2323
val kotest = "5.9.1"
2424
testImplementation("io.kotest:kotest-runner-junit5-jvm:$kotest")

examples/build.gradle.kts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
plugins {
2+
kotlin("jvm")
3+
}
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
implementation(project(":"))
11+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package dev.silenium.libs.flows.examples
2+
3+
import dev.silenium.libs.flows.api.FlowItem
4+
import dev.silenium.libs.flows.api.Sink
5+
import dev.silenium.libs.flows.api.Transformer
6+
import dev.silenium.libs.flows.base.SourceBase
7+
import dev.silenium.libs.flows.impl.FlowGraph
8+
import kotlinx.coroutines.Dispatchers
9+
import kotlinx.coroutines.runBlocking
10+
11+
data class MyData(val value: Int)
12+
data class MyMetadata(val negativeNumbers: Boolean)
13+
14+
// A simple source that publishes numbers from 0 to 100 to two different output pads
15+
class MySource : SourceBase<MyData, MyMetadata>() {
16+
init {
17+
// Create two output pads, one for positive numbers and one for negative numbers
18+
metadata[0u] = MyMetadata(false)
19+
metadata[1u] = MyMetadata(true)
20+
}
21+
22+
// Publish numbers from 0 to 100
23+
// Publishing can be done from any thread/coroutine, as the flow is thread-safe
24+
suspend fun run() {
25+
for (i in 0..100) {
26+
if (i % 2 == 0) {
27+
publish(FlowItem(0u, metadata[0u]!!, MyData(i)))
28+
} else {
29+
publish(FlowItem(1u, metadata[1u]!!, MyData(i)))
30+
}
31+
}
32+
}
33+
}
34+
35+
// A simple processor that doubles the value of the input data
36+
class MyTransformer : Transformer<MyData, MyMetadata, MyData, MyMetadata>, SourceBase<MyData, MyMetadata>() {
37+
private val inputMetadata_ = mutableMapOf<UInt, MyMetadata?>()
38+
override val inputMetadata: Map<UInt, MyMetadata?> get() = inputMetadata_
39+
40+
override fun configure(pad: UInt, metadata: MyMetadata): Result<Unit> {
41+
inputMetadata_[pad] = metadata
42+
this.metadata[pad] = metadata
43+
return Result.success(Unit)
44+
}
45+
46+
override suspend fun receive(item: FlowItem<MyData, MyMetadata>): Result<Unit> = runCatching {
47+
publish(FlowItem(item.pad, item.metadata, MyData(item.value.value * 2)))
48+
}
49+
}
50+
51+
// A simple sink that prints the received data
52+
class MySink : Sink<MyData, MyMetadata> {
53+
private val inputMetadata_ = mutableMapOf<UInt, MyMetadata?>()
54+
override val inputMetadata: Map<UInt, MyMetadata?> get() = inputMetadata_
55+
56+
override suspend fun receive(item: FlowItem<MyData, MyMetadata>): Result<Unit> = runCatching {
57+
println("Received $item")
58+
}
59+
60+
override fun close() {
61+
println("Sink closed")
62+
}
63+
64+
override fun configure(pad: UInt, metadata: MyMetadata): Result<Unit> {
65+
inputMetadata_[pad] = metadata
66+
return Result.success(Unit)
67+
}
68+
}
69+
70+
fun main() = runBlocking {
71+
val graph = FlowGraph(Dispatchers.Default) {
72+
val source = source(MySource(), "source")
73+
val processor = transformer(MyTransformer(), "transformer")
74+
val sink = sink(MySink(), "sink")
75+
76+
source.connectTo(processor).getOrThrow()
77+
processor.connectTo(sink).getOrThrow()
78+
}
79+
graph.source<MySource>("source")!!.impl.run()
80+
graph.close()
81+
}

settings.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ plugins {
22
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
33
}
44
rootProject.name = "kotlin-flow-graph"
5+
6+
include(":examples")

0 commit comments

Comments
 (0)