BPKC is a lightweight approach to Behavioral Programming, using Kotlin’s Channel system and Coroutines to define and manage behaviors in a concurrent environment. It allows developers to model system behaviors and manage asynchronous tasks in a straightforward way.
Behavioral Programming models systems by dividing them into individual "behaviors" that interact with each other. Each behavior operates independently and responds to specific events in the system.
- BThread: Short for "Behavior Thread", it represents a unit of behavior. Each BThread runs its own flow and reacts to certain events in the system.
- sync: A method used by BThreads to coordinate with other events. It can request, wait for, or block certain events, allowing behaviors to collaborate without conflicts.
BPKC is a lightweight library designed for easy use of Behavioral Programming concepts. It simplifies the handling of asynchronous systems and parallel tasks.
BPKC is implemented using Kotlin’s native primitives: Channels for communication and Coroutines for asynchronous execution. This ensures non-blocking code and seamless interaction between behaviors.
BPKC is designed with a Kotlin-specific DSL, making it easy to write and understand. Developers can define and manage behaviors using concise and intuitive syntax, such as bThread
and sync
, which follow Kotlin’s idiomatic style.
Using the sync
method, BPKC allows BThreads to coordinate with each other. They can request or wait for events, and block conflicting events to ensure smooth collaboration between behaviors.
-
Designed for Educational and Learning Purposes
BPKC is primarily designed for learning and demonstrating the basics of Behavioral Programming. It is not optimized for production environments. -
No Dynamic BThread Addition
The current version of BPKC does not support adding or removingbThreads
dynamically during execution. All behaviors must be defined at the start of the program. -
Limited Testing for Various Use Cases and Priorities
BPKC has not undergone extensive testing across different real-world scenarios. Particularly, complex scenarios involving behavior priorities and conflict resolution require more in-depth testing.
enum class WaterEvent : Event {
ADD_HOT, ADD_COLD
}
// Define the Hot Water BThread
val hotWater = bThread(name = "Hot Water") {
for (i in 1..3) {
sync(request = setOf(WaterEvent.ADD_HOT), waitFor = None, blockEvent = None)
}
}
// Define the Cold Water BThread
val coldWater = bThread(name = "Cold Water") {
for (i in 1..3) {
sync(request = setOf(WaterEvent.ADD_COLD))
}
}
// Define the Interleave BThread
val interleave = bThread(name = "Interleave") {
for (i in 1..3) {
sync(waitFor = setOf(WaterEvent.ADD_HOT), blockEvent = setOf(WaterEvent.ADD_COLD))
sync(waitFor = setOf(WaterEvent.ADD_COLD), blockEvent = setOf(WaterEvent.ADD_HOT))
}
}
// Define the Display BThread
val display = bThread(name = "Display") {
while(true) {
sync(waitFor = All)
println("[${this.name}] turned water tap: $lastEvent")
}
}
// Create and run the BProgram
val program = bProgram(
hotWater,
coldWater,
interleave,
display
)
program.enableDebug()
program.runAllBThreads()
In this example, the Hot Water
and Cold Water
behaviors alternate execution three times each, controlled by the Interleave
BThread. The Display
BThread prints the current event each time it occurs.
- Behavioral Programming Research
- BPJ Library
- Introduction to Behavioral Programming Video
- Behavioral Programming in Clojure
- Crash Course on Behavioral Programming
- Behavioral Programming written in Kotlin and optimized for Android
- Behavioral Programming for JavaScript
BPKC is a useful tool for modeling asynchronous tasks and complex behavior interactions. However, as it is primarily designed for educational purposes, it lacks dynamic behavior management and needs more testing for advanced scenarios.