Skip to content

Commit

Permalink
idea priority
Browse files Browse the repository at this point in the history
  • Loading branch information
ManApart committed Jan 18, 2024
1 parent b006ad9 commit 199e80b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
15 changes: 8 additions & 7 deletions src/commonMain/kotlin/core/ai/composableExp/AIPackage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ package core.ai.composableExp
import core.events.Event
import core.thing.Thing

//How does priority work? Is it just based on order definition? Then what about sub modules
//If everything is order based, then all top level ideas happen before any children, and children are priority per order
//Could also provide priority override map of name to priority (string,Int)
//give a base priority, and then packages can override that priority, with the top package overriding the override of a child package
//When evaluating which idea to go with, map all ideas by priority, and grab the first idea that matches at a given priority list (or random at that level?)

//TODO - make name unique by prefixing it with parant ai package? How do we prevent name collisions?
data class Idea(val name: String, val criteria: suspend (Thing) -> Boolean, val action: suspend (Thing) -> List<Event>)
data class Idea(val name: String, val priority: Int, val criteria: suspend (Thing) -> Boolean, val action: suspend (Thing) -> List<Event>)

//TODO - we can validate string references before flattening
data class AIPackageTemplate(val name: String, val subPackages: List<String>, val ideas: List<Idea>) {
//Validate
// - package name unique
// - idea name unique
// - string references exist
data class AIPackageTemplate(val name: String, val subPackages: List<String>, val priorityOverride: Map<String, Int>, val ideas: List<Idea>) {
fun flatten(reference: Map<String, AIPackageTemplate>, flattenedReference: MutableMap<String, AIPackage>): AIPackage {
return flattenedReference[name] ?: let {
val subIdeas = subPackages.mapNotNull { reference[it] }.flatMap { subPackage ->
flattenedReference[subPackage.name]?.ideas
?: subPackage.flatten(reference, flattenedReference).also { flattenedReference[subPackage.name] = it }.ideas
}.map { idea ->
priorityOverride[it.name]?.let { idea.copy(priority = it) } ?: idea
}
AIPackage(name, ideas + subIdeas)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package core.ai.composableExp

import core.events.Event
import core.thing.Thing

//Build all the templates, but don't flatten them
Expand All @@ -12,18 +11,25 @@ import core.thing.Thing

class AIPackageTemplateBuilder(val name: String) {
private val templates = mutableListOf<String>()
private val ideas = mutableListOf<Idea>()
private val ideas = mutableListOf<IdeaBuilder>()
private val criteriaChildren = mutableMapOf<suspend (Thing) -> Boolean, IdeasBuilder>()

private val priorityOverride = mutableMapOf<String, Int>()

fun build(): AIPackageTemplate {
return AIPackageTemplate(name, templates, ideas)
return AIPackageTemplate(name, templates, priorityOverride, ideas.map { it.build(name) })
}

fun template(vararg names: String) = this.templates.addAll(names)

fun idea(name: String, initializer: IdeaBuilder.() -> Unit = {}) {
ideas.add(IdeaBuilder(name).apply(initializer).build())
/**
Override the priority of an idea inherited from a template.
*/
fun priority(ideaName: String, newPriority: Int){

}

fun idea(name: String, priority: Int = 20, initializer: IdeaBuilder.() -> Unit = {}) {
ideas.add(IdeaBuilder(name, priority).apply(initializer))
}

fun criteria(criteria: suspend (Thing) -> Boolean, initializer: IdeasBuilder.() -> Unit){
Expand Down
9 changes: 4 additions & 5 deletions src/commonMain/kotlin/core/ai/composableExp/IdeaBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import core.events.Event
import core.thing.Thing
import kotlinx.coroutines.NonCancellable.children

class IdeaBuilder(val name: String) {
class IdeaBuilder(val name: String, val priority: Int) {
internal var criteria: suspend (Thing) -> Boolean = { true }
private var action: suspend (Thing) -> List<Event> = { listOf() }


fun build(packageName: String) = Idea("$packageName-$name", criteria, action)
fun build(packageName: String) = Idea("$packageName-$name", priority, criteria, action)

fun criteria(criteria: suspend (Thing) -> Boolean) {
this.criteria = criteria
Expand Down Expand Up @@ -41,8 +40,8 @@ class IdeasBuilder {
children.add(item)
}

fun idea(name: String, initializer: IdeaBuilder.() -> Unit) {
children.add(IdeaBuilder(name).apply(initializer))
fun idea(name: String, priority: Int = 20, initializer: IdeaBuilder.() -> Unit) {
children.add(IdeaBuilder(name, priority).apply(initializer))
}

fun criteria(criteria: suspend (Thing) -> Boolean, initializer: IdeasBuilder.() -> Unit) {
Expand Down
3 changes: 2 additions & 1 deletion src/commonMain/kotlin/core/ai/composableExp/TestSheet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fun testAiPackages() {
aiPackage("animal") {
criteria({ !it.isSafe() }) {
criteria({ !it.isSafe() }) {
idea("eat") {
idea("eat", 10) {
criteria { !it.isSafe() }
action { EatFoodEvent(it, it) }
}
Expand All @@ -20,6 +20,7 @@ fun testAiPackages() {
}
}
aiPackage("predator") {
priority("eat", 15)
template("animal")
idea("hunt") {
criteria { it.soul.getConditions().isEmpty() }
Expand Down

0 comments on commit 199e80b

Please sign in to comment.