An open API service indexing awesome lists of open source software.

https://github.com/bethibande/actors

Simple actor framework for kotlin
https://github.com/bethibande/actors

Last synced: 2 months ago
JSON representation

Simple actor framework for kotlin

Awesome Lists containing this project

README

          

[![Quality Gate Status](https://sonar.bethibande.com/api/project_badges/measure?project=actors&metric=alert_status&token=sqb_b82c46a46c368d896f0fae5f1475dd81767fb825)](https://sonar.bethibande.com/dashboard?id=actors)
[![Coverage](https://sonar.bethibande.com/api/project_badges/measure?project=actors&metric=coverage&token=sqb_b82c46a46c368d896f0fae5f1475dd81767fb825)](https://sonar.bethibande.com/dashboard?id=actors)
[![Reliability Rating](https://sonar.bethibande.com/api/project_badges/measure?project=actors&metric=reliability_rating&token=sqb_b82c46a46c368d896f0fae5f1475dd81767fb825)](https://sonar.bethibande.com/dashboard?id=actors)
[![Security Rating](https://sonar.bethibande.com/api/project_badges/measure?project=actors&metric=security_rating&token=sqb_b82c46a46c368d896f0fae5f1475dd81767fb825)](https://sonar.bethibande.com/dashboard?id=actors)
[![Maintainability Rating](https://sonar.bethibande.com/api/project_badges/measure?project=actors&metric=sqale_rating&token=sqb_b82c46a46c368d896f0fae5f1475dd81767fb825)](https://sonar.bethibande.com/dashboard?id=actors)

[![wakatime](https://wakatime.com/badge/user/72047d25-7643-4124-9850-1dd48ddf85f0/project/018d4022-e313-4df5-8fc4-262be0c59caf.svg)](https://wakatime.com/badge/user/72047d25-7643-4124-9850-1dd48ddf85f0/project/018d4022-e313-4df5-8fc4-262be0c59caf)
# Actors
A simple actor based programming framework for kotlin.
Currently only compatible with kotlin jvm.

> [!WARNING]
> This project is still experimental and lacking in features.
> The implementation still needs some work and refactoring, especially the KSP processor.
> This framework is not ready for production use, there are no official maven releases yet!

### Example (Experimental)
Please note, this API is still experimental.

[PersonState.kt](example/src/main/kotlin/com/bethibande/example/person/PersonState.kt)
```kotlin
import com.bethibande.actors.annotations.ActorState

/**
* An actor implementation will be generated from this class.
*/
@ActorState
data class PersonState(
var name: String,
var age: Int,
)
```
[Main.kt](example/src/main/kotlin/com/bethibande/example/Main.kt), usage of the generated code
```kotlin
runBlocking {
// Create actor-system
val system = Person.localActorSystem()
// Create a new actor
val person: Person = system.new(PersonState("Max", 17))

// Use the actor
println("${person.getName()}: ${person.getAge()}")
person.setAge(18)
println("${person.getName()}: ${person.getAge()}")

// Custom behavior/command (see com.bethibande.example.person.CustomFunctionality.kt)
val (name, age) = person.getNameAndAge()
println("Custom: $name, $age")

// Send close command
person.close()
}
```
[CustomFunctionality.kt](example/src/main/kotlin/com/bethibande/example/person/CustomFunctionality.kt), adds custom functionallity / commands to the generated actor
```kotlin
import com.bethibande.actors.Actor
import com.bethibande.actors.behavior.Behavior
import com.bethibande.example.person.command.PersonCommand
import kotlinx.coroutines.CompletableDeferred

data class PersonCommandGetNameAndAge(
val deferred: CompletableDeferred>
): PersonCommand {
companion object: Behavior {
init {
// Adds the behavior to all actors of the Person type, also affects existing actors.
Person.BehaviorMap.add(PersonCommandGetNameAndAge::class.java, this)
}

override suspend fun accept(
command: PersonCommandGetNameAndAge,
state: PersonState,
actor: Actor
) {
command.deferred.complete(state.name to state.age)
}
}
}

suspend fun Person.getNameAndAge(): Pair {
val deferred = CompletableDeferred>()
send(PersonCommandGetNameAndAge(deferred))
return deferred.await()
}
```

### Dependencies
- [Google KSP](https://github.com/google/ksp) for processing symbols like annotations
- [Kotlinpoet](https://github.com/square/kotlinpoet) for generating kotlin source code
- [Kotlinx Coroutines](https://github.com/Kotlin/kotlinx.coroutines) for asynchronous programming